blob: f947fe5e77f5338b86c6157ebb8e946c7e97b1c4 [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"
Bjorn Mellem175aa2e2018-11-08 11:23:22 -080031#include "api/test/loopback_media_transport.h"
Steve Anton10542f22019-01-11 09:11:00 -080032#include "api/uma_metrics.h"
Anders Carlsson67537952018-05-03 11:28:29 +020033#include "api/video_codecs/sdp_video_format.h"
Qingsi Wang7685e862018-06-11 20:15:46 -070034#include "call/call.h"
35#include "logging/rtc_event_log/fake_rtc_event_log_factory.h"
Steve Anton10542f22019-01-11 09:11:00 -080036#include "media/engine/fake_webrtc_video_engine.h"
37#include "media/engine/webrtc_media_engine.h"
Danil Chapovalov9da25bd2019-06-20 10:19:42 +020038#include "media/engine/webrtc_media_engine_defaults.h"
Per Ã…hgrencc73ed32020-04-26 23:56:17 +020039#include "modules/audio_processing/test/audio_processing_builder_for_testing.h"
Qingsi Wang25ec8882019-11-15 12:33:05 -080040#include "p2p/base/fake_ice_transport.h"
Steve Anton10542f22019-01-11 09:11:00 -080041#include "p2p/base/mock_async_resolver.h"
42#include "p2p/base/p2p_constants.h"
43#include "p2p/base/port_interface.h"
44#include "p2p/base/test_stun_server.h"
45#include "p2p/base/test_turn_customizer.h"
46#include "p2p/base/test_turn_server.h"
47#include "p2p/client/basic_port_allocator.h"
48#include "pc/dtmf_sender.h"
49#include "pc/local_audio_source.h"
50#include "pc/media_session.h"
51#include "pc/peer_connection.h"
52#include "pc/peer_connection_factory.h"
53#include "pc/rtp_media_utils.h"
54#include "pc/session_description.h"
55#include "pc/test/fake_audio_capture_module.h"
56#include "pc/test/fake_periodic_video_track_source.h"
57#include "pc/test/fake_rtc_certificate_generator.h"
58#include "pc/test/fake_video_track_renderer.h"
59#include "pc/test/mock_peer_connection_observers.h"
Jonas Olssonb75d9e92019-02-22 10:33:29 +010060#include "rtc_base/fake_clock.h"
Qingsi Wangecd30542019-05-22 14:34:56 -070061#include "rtc_base/fake_mdns_responder.h"
Steve Anton10542f22019-01-11 09:11:00 -080062#include "rtc_base/fake_network.h"
63#include "rtc_base/firewall_socket_server.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020064#include "rtc_base/gunit.h"
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +020065#include "rtc_base/numerics/safe_conversions.h"
Steve Anton10542f22019-01-11 09:11:00 -080066#include "rtc_base/test_certificate_verifier.h"
67#include "rtc_base/time_utils.h"
68#include "rtc_base/virtual_socket_server.h"
Mirko Bonadei17f48782018-09-28 08:51:10 +020069#include "system_wrappers/include/metrics.h"
Qingsi Wangc129c352019-04-18 10:41:58 -070070#include "test/field_trial.h"
Elad Alon99c3fe52017-10-13 16:29:40 +020071#include "test/gmock.h"
deadbeef1dcb1642017-03-29 21:08:16 -070072
Mirko Bonadeiab64e8a2018-12-12 12:10:18 +010073namespace webrtc {
74namespace {
75
76using ::cricket::ContentInfo;
77using ::cricket::StreamParams;
78using ::rtc::SocketAddress;
79using ::testing::_;
Seth Hampson2f0d7022018-02-20 11:54:42 -080080using ::testing::Combine;
Steve Anton64b626b2019-01-28 17:25:26 -080081using ::testing::Contains;
Mirko Bonadeie46f5db2019-03-26 20:14:46 +010082using ::testing::DoAll;
Steve Antonede9ca52017-10-16 13:04:27 -070083using ::testing::ElementsAre;
Qingsi Wang1dac6d82018-12-12 15:28:47 -080084using ::testing::NiceMock;
Steve Anton64b626b2019-01-28 17:25:26 -080085using ::testing::Return;
Zach Stein6fcdc2f2018-08-23 16:25:55 -070086using ::testing::SetArgPointee;
Steve Antonffa6ce42018-11-30 09:26:08 -080087using ::testing::UnorderedElementsAreArray;
Mirko Bonadeiab64e8a2018-12-12 12:10:18 +010088using ::testing::Values;
Steve Anton74255ff2018-01-24 18:32:57 -080089using RTCConfiguration = PeerConnectionInterface::RTCConfiguration;
deadbeef1dcb1642017-03-29 21:08:16 -070090
91static const int kDefaultTimeout = 10000;
92static const int kMaxWaitForStatsMs = 3000;
93static const int kMaxWaitForActivationMs = 5000;
94static const int kMaxWaitForFramesMs = 10000;
95// Default number of audio/video frames to wait for before considering a test
96// successful.
97static const int kDefaultExpectedAudioFrameCount = 3;
98static const int kDefaultExpectedVideoFrameCount = 3;
99
deadbeef1dcb1642017-03-29 21:08:16 -0700100static const char kDataChannelLabel[] = "data_channel";
101
102// SRTP cipher name negotiated by the tests. This must be updated if the
103// default changes.
Taylor Brandstetterfd350d72018-04-03 16:29:26 -0700104static const int kDefaultSrtpCryptoSuite = rtc::SRTP_AES128_CM_SHA1_80;
deadbeef1dcb1642017-03-29 21:08:16 -0700105static const int kDefaultSrtpCryptoSuiteGcm = rtc::SRTP_AEAD_AES_256_GCM;
106
Steve Antonede9ca52017-10-16 13:04:27 -0700107static const SocketAddress kDefaultLocalAddress("192.168.1.1", 0);
108
deadbeef1dcb1642017-03-29 21:08:16 -0700109// Helper function for constructing offer/answer options to initiate an ICE
110// restart.
111PeerConnectionInterface::RTCOfferAnswerOptions IceRestartOfferAnswerOptions() {
112 PeerConnectionInterface::RTCOfferAnswerOptions options;
113 options.ice_restart = true;
114 return options;
115}
116
deadbeefd8ad7882017-04-18 16:01:17 -0700117// Remove all stream information (SSRCs, track IDs, etc.) and "msid-semantic"
118// attribute from received SDP, simulating a legacy endpoint.
119void RemoveSsrcsAndMsids(cricket::SessionDescription* desc) {
120 for (ContentInfo& content : desc->contents()) {
Steve Antonb1c1de12017-12-21 15:14:30 -0800121 content.media_description()->mutable_streams().clear();
deadbeefd8ad7882017-04-18 16:01:17 -0700122 }
123 desc->set_msid_supported(false);
Henrik Boström5b147782018-12-04 11:25:05 +0100124 desc->set_msid_signaling(0);
deadbeefd8ad7882017-04-18 16:01:17 -0700125}
126
Seth Hampson5897a6e2018-04-03 11:16:33 -0700127// Removes all stream information besides the stream ids, simulating an
128// endpoint that only signals a=msid lines to convey stream_ids.
129void RemoveSsrcsAndKeepMsids(cricket::SessionDescription* desc) {
130 for (ContentInfo& content : desc->contents()) {
Steve Antondf527fd2018-04-27 15:52:03 -0700131 std::string track_id;
Seth Hampson5897a6e2018-04-03 11:16:33 -0700132 std::vector<std::string> stream_ids;
133 if (!content.media_description()->streams().empty()) {
Steve Antondf527fd2018-04-27 15:52:03 -0700134 const StreamParams& first_stream =
135 content.media_description()->streams()[0];
136 track_id = first_stream.id;
137 stream_ids = first_stream.stream_ids();
Seth Hampson5897a6e2018-04-03 11:16:33 -0700138 }
139 content.media_description()->mutable_streams().clear();
Steve Antondf527fd2018-04-27 15:52:03 -0700140 StreamParams new_stream;
141 new_stream.id = track_id;
Seth Hampson5897a6e2018-04-03 11:16:33 -0700142 new_stream.set_stream_ids(stream_ids);
143 content.media_description()->AddStream(new_stream);
144 }
145}
146
zhihuangf8164932017-05-19 13:09:47 -0700147int FindFirstMediaStatsIndexByKind(
148 const std::string& kind,
149 const std::vector<const webrtc::RTCMediaStreamTrackStats*>&
150 media_stats_vec) {
151 for (size_t i = 0; i < media_stats_vec.size(); i++) {
152 if (media_stats_vec[i]->kind.ValueToString() == kind) {
153 return i;
154 }
155 }
156 return -1;
157}
158
deadbeef1dcb1642017-03-29 21:08:16 -0700159class SignalingMessageReceiver {
160 public:
Steve Antona3a92c22017-12-07 10:27:41 -0800161 virtual void ReceiveSdpMessage(SdpType type, const std::string& msg) = 0;
deadbeef1dcb1642017-03-29 21:08:16 -0700162 virtual void ReceiveIceMessage(const std::string& sdp_mid,
163 int sdp_mline_index,
164 const std::string& msg) = 0;
165
166 protected:
167 SignalingMessageReceiver() {}
168 virtual ~SignalingMessageReceiver() {}
169};
170
171class MockRtpReceiverObserver : public webrtc::RtpReceiverObserverInterface {
172 public:
173 explicit MockRtpReceiverObserver(cricket::MediaType media_type)
174 : expected_media_type_(media_type) {}
175
176 void OnFirstPacketReceived(cricket::MediaType media_type) override {
177 ASSERT_EQ(expected_media_type_, media_type);
178 first_packet_received_ = true;
179 }
180
181 bool first_packet_received() const { return first_packet_received_; }
182
183 virtual ~MockRtpReceiverObserver() {}
184
185 private:
186 bool first_packet_received_ = false;
187 cricket::MediaType expected_media_type_;
188};
189
190// Helper class that wraps a peer connection, observes it, and can accept
191// signaling messages from another wrapper.
192//
193// Uses a fake network, fake A/V capture, and optionally fake
194// encoders/decoders, though they aren't used by default since they don't
195// advertise support of any codecs.
Steve Anton94286cb2017-09-26 16:20:19 -0700196// TODO(steveanton): See how this could become a subclass of
Seth Hampson2f0d7022018-02-20 11:54:42 -0800197// PeerConnectionWrapper defined in peerconnectionwrapper.h.
deadbeef1dcb1642017-03-29 21:08:16 -0700198class PeerConnectionWrapper : public webrtc::PeerConnectionObserver,
Steve Anton15324772018-01-16 10:26:49 -0800199 public SignalingMessageReceiver {
deadbeef1dcb1642017-03-29 21:08:16 -0700200 public:
201 // Different factory methods for convenience.
202 // TODO(deadbeef): Could use the pattern of:
203 //
204 // PeerConnectionWrapper =
205 // WrapperBuilder.WithConfig(...).WithOptions(...).build();
206 //
207 // To reduce some code duplication.
208 static PeerConnectionWrapper* CreateWithDtlsIdentityStore(
209 const std::string& debug_name,
210 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator,
211 rtc::Thread* network_thread,
212 rtc::Thread* worker_thread) {
213 PeerConnectionWrapper* client(new PeerConnectionWrapper(debug_name));
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700214 webrtc::PeerConnectionDependencies dependencies(nullptr);
215 dependencies.cert_generator = std::move(cert_generator);
Niels Möllerf06f9232018-08-07 12:32:18 +0200216 if (!client->Init(nullptr, nullptr, std::move(dependencies), network_thread,
Bjorn Mellem175aa2e2018-11-08 11:23:22 -0800217 worker_thread, nullptr,
Johannes Kron3e983682020-03-29 22:17:00 +0200218 /*media_transport_factory=*/nullptr,
219 /*reset_encoder_factory=*/false,
220 /*reset_decoder_factory=*/false)) {
deadbeef1dcb1642017-03-29 21:08:16 -0700221 delete client;
222 return nullptr;
223 }
224 return client;
225 }
226
deadbeef2f425aa2017-04-14 10:41:32 -0700227 webrtc::PeerConnectionFactoryInterface* pc_factory() const {
228 return peer_connection_factory_.get();
229 }
230
deadbeef1dcb1642017-03-29 21:08:16 -0700231 webrtc::PeerConnectionInterface* pc() const { return peer_connection_.get(); }
232
233 // If a signaling message receiver is set (via ConnectFakeSignaling), this
234 // will set the whole offer/answer exchange in motion. Just need to wait for
235 // the signaling state to reach "stable".
236 void CreateAndSetAndSignalOffer() {
Eldar Rello5ab79e62019-10-09 18:29:44 +0300237 auto offer = CreateOfferAndWait();
deadbeef1dcb1642017-03-29 21:08:16 -0700238 ASSERT_NE(nullptr, offer);
239 EXPECT_TRUE(SetLocalDescriptionAndSendSdpMessage(std::move(offer)));
240 }
241
242 // Sets the options to be used when CreateAndSetAndSignalOffer is called, or
243 // when a remote offer is received (via fake signaling) and an answer is
244 // generated. By default, uses default options.
245 void SetOfferAnswerOptions(
246 const PeerConnectionInterface::RTCOfferAnswerOptions& options) {
247 offer_answer_options_ = options;
248 }
249
250 // Set a callback to be invoked when SDP is received via the fake signaling
251 // channel, which provides an opportunity to munge (modify) the SDP. This is
252 // used to test SDP being applied that a PeerConnection would normally not
253 // generate, but a non-JSEP endpoint might.
254 void SetReceivedSdpMunger(
255 std::function<void(cricket::SessionDescription*)> munger) {
Mirko Bonadeic61ce0d2017-11-21 17:04:20 +0100256 received_sdp_munger_ = std::move(munger);
deadbeef1dcb1642017-03-29 21:08:16 -0700257 }
258
deadbeefc964d0b2017-04-03 10:03:35 -0700259 // Similar to the above, but this is run on SDP immediately after it's
deadbeef1dcb1642017-03-29 21:08:16 -0700260 // generated.
261 void SetGeneratedSdpMunger(
262 std::function<void(cricket::SessionDescription*)> munger) {
Mirko Bonadeic61ce0d2017-11-21 17:04:20 +0100263 generated_sdp_munger_ = std::move(munger);
deadbeef1dcb1642017-03-29 21:08:16 -0700264 }
265
Seth Hampson2f0d7022018-02-20 11:54:42 -0800266 // Set a callback to be invoked when a remote offer is received via the fake
267 // signaling channel. This provides an opportunity to change the
268 // PeerConnection state before an answer is created and sent to the caller.
269 void SetRemoteOfferHandler(std::function<void()> handler) {
270 remote_offer_handler_ = std::move(handler);
271 }
272
Qingsi Wang1dac6d82018-12-12 15:28:47 -0800273 void SetRemoteAsyncResolver(rtc::MockAsyncResolver* resolver) {
274 remote_async_resolver_ = resolver;
Zach Stein6fcdc2f2018-08-23 16:25:55 -0700275 }
276
Steve Antonede9ca52017-10-16 13:04:27 -0700277 // Every ICE connection state in order that has been seen by the observer.
278 std::vector<PeerConnectionInterface::IceConnectionState>
279 ice_connection_state_history() const {
280 return ice_connection_state_history_;
281 }
Steve Anton6f25b092017-10-23 09:39:20 -0700282 void clear_ice_connection_state_history() {
283 ice_connection_state_history_.clear();
284 }
Steve Antonede9ca52017-10-16 13:04:27 -0700285
Jonas Olssonacd8ae72019-02-25 15:26:24 +0100286 // Every standardized ICE connection state in order that has been seen by the
287 // observer.
288 std::vector<PeerConnectionInterface::IceConnectionState>
289 standardized_ice_connection_state_history() const {
290 return standardized_ice_connection_state_history_;
291 }
292
Jonas Olsson635474e2018-10-18 15:58:17 +0200293 // Every PeerConnection state in order that has been seen by the observer.
294 std::vector<PeerConnectionInterface::PeerConnectionState>
295 peer_connection_state_history() const {
296 return peer_connection_state_history_;
297 }
298
Steve Antonede9ca52017-10-16 13:04:27 -0700299 // Every ICE gathering state in order that has been seen by the observer.
300 std::vector<PeerConnectionInterface::IceGatheringState>
301 ice_gathering_state_history() const {
302 return ice_gathering_state_history_;
deadbeef1dcb1642017-03-29 21:08:16 -0700303 }
Alex Drake00c7ecf2019-08-06 10:54:47 -0700304 std::vector<cricket::CandidatePairChangeEvent>
305 ice_candidate_pair_change_history() const {
306 return ice_candidate_pair_change_history_;
307 }
deadbeef1dcb1642017-03-29 21:08:16 -0700308
Eldar Rello5ab79e62019-10-09 18:29:44 +0300309 // Every PeerConnection signaling state in order that has been seen by the
310 // observer.
311 std::vector<PeerConnectionInterface::SignalingState>
312 peer_connection_signaling_state_history() const {
313 return peer_connection_signaling_state_history_;
314 }
315
Steve Anton15324772018-01-16 10:26:49 -0800316 void AddAudioVideoTracks() {
317 AddAudioTrack();
318 AddVideoTrack();
deadbeef1dcb1642017-03-29 21:08:16 -0700319 }
320
Steve Anton74255ff2018-01-24 18:32:57 -0800321 rtc::scoped_refptr<RtpSenderInterface> AddAudioTrack() {
322 return AddTrack(CreateLocalAudioTrack());
323 }
deadbeef1dcb1642017-03-29 21:08:16 -0700324
Steve Anton74255ff2018-01-24 18:32:57 -0800325 rtc::scoped_refptr<RtpSenderInterface> AddVideoTrack() {
326 return AddTrack(CreateLocalVideoTrack());
327 }
deadbeef1dcb1642017-03-29 21:08:16 -0700328
329 rtc::scoped_refptr<webrtc::AudioTrackInterface> CreateLocalAudioTrack() {
Niels Möller2d02e082018-05-21 11:23:35 +0200330 cricket::AudioOptions options;
deadbeef1dcb1642017-03-29 21:08:16 -0700331 // Disable highpass filter so that we can get all the test audio frames.
Niels Möller2d02e082018-05-21 11:23:35 +0200332 options.highpass_filter = false;
deadbeef1dcb1642017-03-29 21:08:16 -0700333 rtc::scoped_refptr<webrtc::AudioSourceInterface> source =
Niels Möller2d02e082018-05-21 11:23:35 +0200334 peer_connection_factory_->CreateAudioSource(options);
deadbeef1dcb1642017-03-29 21:08:16 -0700335 // TODO(perkj): Test audio source when it is implemented. Currently audio
336 // always use the default input.
deadbeefb1a15d72017-09-07 14:12:05 -0700337 return peer_connection_factory_->CreateAudioTrack(rtc::CreateRandomUuid(),
deadbeef1dcb1642017-03-29 21:08:16 -0700338 source);
339 }
340
341 rtc::scoped_refptr<webrtc::VideoTrackInterface> CreateLocalVideoTrack() {
Johannes Kron965e7942018-09-13 15:36:20 +0200342 webrtc::FakePeriodicVideoSource::Config config;
343 config.timestamp_offset_ms = rtc::TimeMillis();
344 return CreateLocalVideoTrackInternal(config);
deadbeef1dcb1642017-03-29 21:08:16 -0700345 }
346
347 rtc::scoped_refptr<webrtc::VideoTrackInterface>
Niels Möller5c7efe72018-05-11 10:34:46 +0200348 CreateLocalVideoTrackWithConfig(
349 webrtc::FakePeriodicVideoSource::Config config) {
350 return CreateLocalVideoTrackInternal(config);
deadbeef1dcb1642017-03-29 21:08:16 -0700351 }
352
353 rtc::scoped_refptr<webrtc::VideoTrackInterface>
354 CreateLocalVideoTrackWithRotation(webrtc::VideoRotation rotation) {
Niels Möller5c7efe72018-05-11 10:34:46 +0200355 webrtc::FakePeriodicVideoSource::Config config;
356 config.rotation = rotation;
Johannes Kron965e7942018-09-13 15:36:20 +0200357 config.timestamp_offset_ms = rtc::TimeMillis();
Niels Möller5c7efe72018-05-11 10:34:46 +0200358 return CreateLocalVideoTrackInternal(config);
deadbeef1dcb1642017-03-29 21:08:16 -0700359 }
360
Steve Anton74255ff2018-01-24 18:32:57 -0800361 rtc::scoped_refptr<RtpSenderInterface> AddTrack(
362 rtc::scoped_refptr<MediaStreamTrackInterface> track,
Seth Hampson845e8782018-03-02 11:34:10 -0800363 const std::vector<std::string>& stream_ids = {}) {
364 auto result = pc()->AddTrack(track, stream_ids);
Steve Anton15324772018-01-16 10:26:49 -0800365 EXPECT_EQ(RTCErrorType::NONE, result.error().type());
Steve Anton74255ff2018-01-24 18:32:57 -0800366 return result.MoveValue();
367 }
368
369 std::vector<rtc::scoped_refptr<RtpReceiverInterface>> GetReceiversOfType(
370 cricket::MediaType media_type) {
371 std::vector<rtc::scoped_refptr<RtpReceiverInterface>> receivers;
Mirko Bonadei739baf02019-01-27 17:29:42 +0100372 for (const auto& receiver : pc()->GetReceivers()) {
Steve Anton74255ff2018-01-24 18:32:57 -0800373 if (receiver->media_type() == media_type) {
374 receivers.push_back(receiver);
375 }
376 }
377 return receivers;
deadbeef1dcb1642017-03-29 21:08:16 -0700378 }
379
Seth Hampson2f0d7022018-02-20 11:54:42 -0800380 rtc::scoped_refptr<RtpTransceiverInterface> GetFirstTransceiverOfType(
381 cricket::MediaType media_type) {
382 for (auto transceiver : pc()->GetTransceivers()) {
383 if (transceiver->receiver()->media_type() == media_type) {
384 return transceiver;
385 }
386 }
387 return nullptr;
388 }
389
deadbeef1dcb1642017-03-29 21:08:16 -0700390 bool SignalingStateStable() {
391 return pc()->signaling_state() == webrtc::PeerConnectionInterface::kStable;
392 }
393
394 void CreateDataChannel() { CreateDataChannel(nullptr); }
395
396 void CreateDataChannel(const webrtc::DataChannelInit* init) {
Steve Antonda6c0952017-10-23 11:41:54 -0700397 CreateDataChannel(kDataChannelLabel, init);
398 }
399
400 void CreateDataChannel(const std::string& label,
401 const webrtc::DataChannelInit* init) {
402 data_channel_ = pc()->CreateDataChannel(label, init);
deadbeef1dcb1642017-03-29 21:08:16 -0700403 ASSERT_TRUE(data_channel_.get() != nullptr);
404 data_observer_.reset(new MockDataChannelObserver(data_channel_));
405 }
406
407 DataChannelInterface* data_channel() { return data_channel_; }
408 const MockDataChannelObserver* data_observer() const {
409 return data_observer_.get();
410 }
411
412 int audio_frames_received() const {
413 return fake_audio_capture_module_->frames_received();
414 }
415
416 // Takes minimum of video frames received for each track.
417 //
418 // Can be used like:
419 // EXPECT_GE(expected_frames, min_video_frames_received_per_track());
420 //
421 // To ensure that all video tracks received at least a certain number of
422 // frames.
423 int min_video_frames_received_per_track() const {
424 int min_frames = INT_MAX;
Anders Carlsson5f2bb622018-05-14 09:48:06 +0200425 if (fake_video_renderers_.empty()) {
426 return 0;
deadbeef1dcb1642017-03-29 21:08:16 -0700427 }
deadbeef1dcb1642017-03-29 21:08:16 -0700428
Anders Carlsson5f2bb622018-05-14 09:48:06 +0200429 for (const auto& pair : fake_video_renderers_) {
430 min_frames = std::min(min_frames, pair.second->num_rendered_frames());
deadbeef1dcb1642017-03-29 21:08:16 -0700431 }
Anders Carlsson5f2bb622018-05-14 09:48:06 +0200432 return min_frames;
deadbeef1dcb1642017-03-29 21:08:16 -0700433 }
434
435 // Returns a MockStatsObserver in a state after stats gathering finished,
436 // which can be used to access the gathered stats.
deadbeefd8ad7882017-04-18 16:01:17 -0700437 rtc::scoped_refptr<MockStatsObserver> OldGetStatsForTrack(
deadbeef1dcb1642017-03-29 21:08:16 -0700438 webrtc::MediaStreamTrackInterface* track) {
439 rtc::scoped_refptr<MockStatsObserver> observer(
440 new rtc::RefCountedObject<MockStatsObserver>());
441 EXPECT_TRUE(peer_connection_->GetStats(
442 observer, nullptr, PeerConnectionInterface::kStatsOutputLevelStandard));
443 EXPECT_TRUE_WAIT(observer->called(), kDefaultTimeout);
444 return observer;
445 }
446
447 // Version that doesn't take a track "filter", and gathers all stats.
deadbeefd8ad7882017-04-18 16:01:17 -0700448 rtc::scoped_refptr<MockStatsObserver> OldGetStats() {
449 return OldGetStatsForTrack(nullptr);
450 }
451
452 // Synchronously gets stats and returns them. If it times out, fails the test
453 // and returns null.
454 rtc::scoped_refptr<const webrtc::RTCStatsReport> NewGetStats() {
455 rtc::scoped_refptr<webrtc::MockRTCStatsCollectorCallback> callback(
456 new rtc::RefCountedObject<webrtc::MockRTCStatsCollectorCallback>());
457 peer_connection_->GetStats(callback);
458 EXPECT_TRUE_WAIT(callback->called(), kDefaultTimeout);
459 return callback->report();
deadbeef1dcb1642017-03-29 21:08:16 -0700460 }
461
462 int rendered_width() {
463 EXPECT_FALSE(fake_video_renderers_.empty());
464 return fake_video_renderers_.empty()
465 ? 0
466 : fake_video_renderers_.begin()->second->width();
467 }
468
469 int rendered_height() {
470 EXPECT_FALSE(fake_video_renderers_.empty());
471 return fake_video_renderers_.empty()
472 ? 0
473 : fake_video_renderers_.begin()->second->height();
474 }
475
476 double rendered_aspect_ratio() {
477 if (rendered_height() == 0) {
478 return 0.0;
479 }
480 return static_cast<double>(rendered_width()) / rendered_height();
481 }
482
483 webrtc::VideoRotation rendered_rotation() {
484 EXPECT_FALSE(fake_video_renderers_.empty());
485 return fake_video_renderers_.empty()
486 ? webrtc::kVideoRotation_0
487 : fake_video_renderers_.begin()->second->rotation();
488 }
489
490 int local_rendered_width() {
491 return local_video_renderer_ ? local_video_renderer_->width() : 0;
492 }
493
494 int local_rendered_height() {
495 return local_video_renderer_ ? local_video_renderer_->height() : 0;
496 }
497
498 double local_rendered_aspect_ratio() {
499 if (local_rendered_height() == 0) {
500 return 0.0;
501 }
502 return static_cast<double>(local_rendered_width()) /
503 local_rendered_height();
504 }
505
506 size_t number_of_remote_streams() {
507 if (!pc()) {
508 return 0;
509 }
510 return pc()->remote_streams()->count();
511 }
512
513 StreamCollectionInterface* remote_streams() const {
514 if (!pc()) {
515 ADD_FAILURE();
516 return nullptr;
517 }
518 return pc()->remote_streams();
519 }
520
521 StreamCollectionInterface* local_streams() {
522 if (!pc()) {
523 ADD_FAILURE();
524 return nullptr;
525 }
526 return pc()->local_streams();
527 }
528
529 webrtc::PeerConnectionInterface::SignalingState signaling_state() {
530 return pc()->signaling_state();
531 }
532
533 webrtc::PeerConnectionInterface::IceConnectionState ice_connection_state() {
534 return pc()->ice_connection_state();
535 }
536
Jonas Olsson7a6739e2019-01-15 16:31:55 +0100537 webrtc::PeerConnectionInterface::IceConnectionState
538 standardized_ice_connection_state() {
539 return pc()->standardized_ice_connection_state();
540 }
541
deadbeef1dcb1642017-03-29 21:08:16 -0700542 webrtc::PeerConnectionInterface::IceGatheringState ice_gathering_state() {
543 return pc()->ice_gathering_state();
544 }
545
546 // Returns a MockRtpReceiverObserver for each RtpReceiver returned by
547 // GetReceivers. They're updated automatically when a remote offer/answer
548 // from the fake signaling channel is applied, or when
549 // ResetRtpReceiverObservers below is called.
550 const std::vector<std::unique_ptr<MockRtpReceiverObserver>>&
551 rtp_receiver_observers() {
552 return rtp_receiver_observers_;
553 }
554
555 void ResetRtpReceiverObservers() {
556 rtp_receiver_observers_.clear();
Mirko Bonadeic61ce0d2017-11-21 17:04:20 +0100557 for (const rtc::scoped_refptr<RtpReceiverInterface>& receiver :
558 pc()->GetReceivers()) {
deadbeef1dcb1642017-03-29 21:08:16 -0700559 std::unique_ptr<MockRtpReceiverObserver> observer(
560 new MockRtpReceiverObserver(receiver->media_type()));
561 receiver->SetObserver(observer.get());
562 rtp_receiver_observers_.push_back(std::move(observer));
563 }
564 }
565
Qingsi Wangecd30542019-05-22 14:34:56 -0700566 rtc::FakeNetworkManager* network_manager() const {
Steve Antonede9ca52017-10-16 13:04:27 -0700567 return fake_network_manager_.get();
568 }
569 cricket::PortAllocator* port_allocator() const { return port_allocator_; }
570
Qingsi Wang7685e862018-06-11 20:15:46 -0700571 webrtc::FakeRtcEventLogFactory* event_log_factory() const {
572 return event_log_factory_;
573 }
574
Qingsi Wangc129c352019-04-18 10:41:58 -0700575 const cricket::Candidate& last_candidate_gathered() const {
576 return last_candidate_gathered_;
577 }
Eldar Relloda13ea22019-06-01 12:23:43 +0300578 const cricket::IceCandidateErrorEvent& error_event() const {
579 return error_event_;
580 }
Qingsi Wangc129c352019-04-18 10:41:58 -0700581
Qingsi Wangecd30542019-05-22 14:34:56 -0700582 // Sets the mDNS responder for the owned fake network manager and keeps a
583 // reference to the responder.
584 void SetMdnsResponder(
585 std::unique_ptr<webrtc::FakeMdnsResponder> mdns_responder) {
586 RTC_DCHECK(mdns_responder != nullptr);
587 mdns_responder_ = mdns_responder.get();
588 network_manager()->set_mdns_responder(std::move(mdns_responder));
589 }
590
Eldar Rello5ab79e62019-10-09 18:29:44 +0300591 // Returns null on failure.
592 std::unique_ptr<SessionDescriptionInterface> CreateOfferAndWait() {
593 rtc::scoped_refptr<MockCreateSessionDescriptionObserver> observer(
594 new rtc::RefCountedObject<MockCreateSessionDescriptionObserver>());
595 pc()->CreateOffer(observer, offer_answer_options_);
596 return WaitForDescriptionFromObserver(observer);
597 }
Eldar Rellod9ebe012020-03-18 20:41:45 +0200598 bool Rollback() {
599 return SetRemoteDescription(
600 webrtc::CreateSessionDescription(SdpType::kRollback, ""));
601 }
Eldar Rello5ab79e62019-10-09 18:29:44 +0300602
deadbeef1dcb1642017-03-29 21:08:16 -0700603 private:
604 explicit PeerConnectionWrapper(const std::string& debug_name)
605 : debug_name_(debug_name) {}
606
Bjorn Mellem175aa2e2018-11-08 11:23:22 -0800607 bool Init(
608 const PeerConnectionFactory::Options* options,
609 const PeerConnectionInterface::RTCConfiguration* config,
610 webrtc::PeerConnectionDependencies dependencies,
611 rtc::Thread* network_thread,
612 rtc::Thread* worker_thread,
613 std::unique_ptr<webrtc::FakeRtcEventLogFactory> event_log_factory,
Johannes Kron3e983682020-03-29 22:17:00 +0200614 std::unique_ptr<webrtc::MediaTransportFactory> media_transport_factory,
615 bool reset_encoder_factory,
616 bool reset_decoder_factory) {
deadbeef1dcb1642017-03-29 21:08:16 -0700617 // There's an error in this test code if Init ends up being called twice.
618 RTC_DCHECK(!peer_connection_);
619 RTC_DCHECK(!peer_connection_factory_);
620
621 fake_network_manager_.reset(new rtc::FakeNetworkManager());
Steve Antonede9ca52017-10-16 13:04:27 -0700622 fake_network_manager_->AddInterface(kDefaultLocalAddress);
deadbeef1dcb1642017-03-29 21:08:16 -0700623
624 std::unique_ptr<cricket::PortAllocator> port_allocator(
625 new cricket::BasicPortAllocator(fake_network_manager_.get()));
Steve Antonede9ca52017-10-16 13:04:27 -0700626 port_allocator_ = port_allocator.get();
deadbeef1dcb1642017-03-29 21:08:16 -0700627 fake_audio_capture_module_ = FakeAudioCaptureModule::Create();
628 if (!fake_audio_capture_module_) {
629 return false;
630 }
deadbeef1dcb1642017-03-29 21:08:16 -0700631 rtc::Thread* const signaling_thread = rtc::Thread::Current();
Qingsi Wang7685e862018-06-11 20:15:46 -0700632
633 webrtc::PeerConnectionFactoryDependencies pc_factory_dependencies;
634 pc_factory_dependencies.network_thread = network_thread;
635 pc_factory_dependencies.worker_thread = worker_thread;
636 pc_factory_dependencies.signaling_thread = signaling_thread;
Danil Chapovalov9da25bd2019-06-20 10:19:42 +0200637 pc_factory_dependencies.task_queue_factory =
638 webrtc::CreateDefaultTaskQueueFactory();
639 cricket::MediaEngineDependencies media_deps;
640 media_deps.task_queue_factory =
641 pc_factory_dependencies.task_queue_factory.get();
642 media_deps.adm = fake_audio_capture_module_;
643 webrtc::SetMediaEngineDefaults(&media_deps);
Johannes Kron3e983682020-03-29 22:17:00 +0200644
645 if (reset_encoder_factory) {
646 media_deps.video_encoder_factory.reset();
647 }
648 if (reset_decoder_factory) {
649 media_deps.video_decoder_factory.reset();
650 }
651
Per Ã…hgrencc73ed32020-04-26 23:56:17 +0200652 if (!media_deps.audio_processing) {
653 // If the standard Creation method for APM returns a null pointer, instead
654 // use the builder for testing to create an APM object.
655 media_deps.audio_processing = AudioProcessingBuilderForTesting().Create();
656 }
657
Qingsi Wang7685e862018-06-11 20:15:46 -0700658 pc_factory_dependencies.media_engine =
Danil Chapovalov9da25bd2019-06-20 10:19:42 +0200659 cricket::CreateMediaEngine(std::move(media_deps));
Qingsi Wang7685e862018-06-11 20:15:46 -0700660 pc_factory_dependencies.call_factory = webrtc::CreateCallFactory();
661 if (event_log_factory) {
662 event_log_factory_ = event_log_factory.get();
663 pc_factory_dependencies.event_log_factory = std::move(event_log_factory);
664 } else {
665 pc_factory_dependencies.event_log_factory =
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200666 std::make_unique<webrtc::RtcEventLogFactory>(
Danil Chapovalov9da25bd2019-06-20 10:19:42 +0200667 pc_factory_dependencies.task_queue_factory.get());
Qingsi Wang7685e862018-06-11 20:15:46 -0700668 }
Bjorn Mellem175aa2e2018-11-08 11:23:22 -0800669 if (media_transport_factory) {
670 pc_factory_dependencies.media_transport_factory =
671 std::move(media_transport_factory);
672 }
Qingsi Wang7685e862018-06-11 20:15:46 -0700673 peer_connection_factory_ = webrtc::CreateModularPeerConnectionFactory(
674 std::move(pc_factory_dependencies));
675
deadbeef1dcb1642017-03-29 21:08:16 -0700676 if (!peer_connection_factory_) {
677 return false;
678 }
679 if (options) {
680 peer_connection_factory_->SetOptions(*options);
681 }
Seth Hampson2f0d7022018-02-20 11:54:42 -0800682 if (config) {
683 sdp_semantics_ = config->sdp_semantics;
684 }
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700685
686 dependencies.allocator = std::move(port_allocator);
Niels Möllerf06f9232018-08-07 12:32:18 +0200687 peer_connection_ = CreatePeerConnection(config, std::move(dependencies));
deadbeef1dcb1642017-03-29 21:08:16 -0700688 return peer_connection_.get() != nullptr;
689 }
690
691 rtc::scoped_refptr<webrtc::PeerConnectionInterface> CreatePeerConnection(
deadbeef1dcb1642017-03-29 21:08:16 -0700692 const PeerConnectionInterface::RTCConfiguration* config,
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700693 webrtc::PeerConnectionDependencies dependencies) {
deadbeef1dcb1642017-03-29 21:08:16 -0700694 PeerConnectionInterface::RTCConfiguration modified_config;
695 // If |config| is null, this will result in a default configuration being
696 // used.
697 if (config) {
698 modified_config = *config;
699 }
700 // Disable resolution adaptation; we don't want it interfering with the
701 // test results.
702 // TODO(deadbeef): Do something more robust. Since we're testing for aspect
703 // ratios and not specific resolutions, is this even necessary?
704 modified_config.set_cpu_adaptation(false);
705
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700706 dependencies.observer = this;
deadbeef1dcb1642017-03-29 21:08:16 -0700707 return peer_connection_factory_->CreatePeerConnection(
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700708 modified_config, std::move(dependencies));
deadbeef1dcb1642017-03-29 21:08:16 -0700709 }
710
711 void set_signaling_message_receiver(
712 SignalingMessageReceiver* signaling_message_receiver) {
713 signaling_message_receiver_ = signaling_message_receiver;
714 }
715
716 void set_signaling_delay_ms(int delay_ms) { signaling_delay_ms_ = delay_ms; }
717
Steve Antonede9ca52017-10-16 13:04:27 -0700718 void set_signal_ice_candidates(bool signal) {
719 signal_ice_candidates_ = signal;
720 }
721
deadbeef1dcb1642017-03-29 21:08:16 -0700722 rtc::scoped_refptr<webrtc::VideoTrackInterface> CreateLocalVideoTrackInternal(
Niels Möller5c7efe72018-05-11 10:34:46 +0200723 webrtc::FakePeriodicVideoSource::Config config) {
deadbeef1dcb1642017-03-29 21:08:16 -0700724 // Set max frame rate to 10fps to reduce the risk of test flakiness.
725 // TODO(deadbeef): Do something more robust.
Niels Möller5c7efe72018-05-11 10:34:46 +0200726 config.frame_interval_ms = 100;
deadbeef1dcb1642017-03-29 21:08:16 -0700727
Niels Möller5c7efe72018-05-11 10:34:46 +0200728 video_track_sources_.emplace_back(
Niels Möller0f405822018-05-17 09:16:41 +0200729 new rtc::RefCountedObject<webrtc::FakePeriodicVideoTrackSource>(
730 config, false /* remote */));
deadbeef1dcb1642017-03-29 21:08:16 -0700731 rtc::scoped_refptr<webrtc::VideoTrackInterface> track(
Niels Möller5c7efe72018-05-11 10:34:46 +0200732 peer_connection_factory_->CreateVideoTrack(
733 rtc::CreateRandomUuid(), video_track_sources_.back()));
deadbeef1dcb1642017-03-29 21:08:16 -0700734 if (!local_video_renderer_) {
735 local_video_renderer_.reset(new webrtc::FakeVideoTrackRenderer(track));
736 }
737 return track;
738 }
739
740 void HandleIncomingOffer(const std::string& msg) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100741 RTC_LOG(LS_INFO) << debug_name_ << ": HandleIncomingOffer";
Steve Antona3a92c22017-12-07 10:27:41 -0800742 std::unique_ptr<SessionDescriptionInterface> desc =
743 webrtc::CreateSessionDescription(SdpType::kOffer, msg);
deadbeef1dcb1642017-03-29 21:08:16 -0700744 if (received_sdp_munger_) {
745 received_sdp_munger_(desc->description());
746 }
747
748 EXPECT_TRUE(SetRemoteDescription(std::move(desc)));
749 // Setting a remote description may have changed the number of receivers,
750 // so reset the receiver observers.
751 ResetRtpReceiverObservers();
Seth Hampson2f0d7022018-02-20 11:54:42 -0800752 if (remote_offer_handler_) {
753 remote_offer_handler_();
754 }
deadbeef1dcb1642017-03-29 21:08:16 -0700755 auto answer = CreateAnswer();
756 ASSERT_NE(nullptr, answer);
757 EXPECT_TRUE(SetLocalDescriptionAndSendSdpMessage(std::move(answer)));
758 }
759
760 void HandleIncomingAnswer(const std::string& msg) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100761 RTC_LOG(LS_INFO) << debug_name_ << ": HandleIncomingAnswer";
Steve Antona3a92c22017-12-07 10:27:41 -0800762 std::unique_ptr<SessionDescriptionInterface> desc =
763 webrtc::CreateSessionDescription(SdpType::kAnswer, msg);
deadbeef1dcb1642017-03-29 21:08:16 -0700764 if (received_sdp_munger_) {
765 received_sdp_munger_(desc->description());
766 }
767
768 EXPECT_TRUE(SetRemoteDescription(std::move(desc)));
769 // Set the RtpReceiverObserver after receivers are created.
770 ResetRtpReceiverObservers();
771 }
772
773 // Returns null on failure.
deadbeef1dcb1642017-03-29 21:08:16 -0700774 std::unique_ptr<SessionDescriptionInterface> CreateAnswer() {
775 rtc::scoped_refptr<MockCreateSessionDescriptionObserver> observer(
776 new rtc::RefCountedObject<MockCreateSessionDescriptionObserver>());
777 pc()->CreateAnswer(observer, offer_answer_options_);
778 return WaitForDescriptionFromObserver(observer);
779 }
780
781 std::unique_ptr<SessionDescriptionInterface> WaitForDescriptionFromObserver(
Mirko Bonadeic61ce0d2017-11-21 17:04:20 +0100782 MockCreateSessionDescriptionObserver* observer) {
deadbeef1dcb1642017-03-29 21:08:16 -0700783 EXPECT_EQ_WAIT(true, observer->called(), kDefaultTimeout);
784 if (!observer->result()) {
785 return nullptr;
786 }
787 auto description = observer->MoveDescription();
788 if (generated_sdp_munger_) {
789 generated_sdp_munger_(description->description());
790 }
791 return description;
792 }
793
794 // Setting the local description and sending the SDP message over the fake
795 // signaling channel are combined into the same method because the SDP
796 // message needs to be sent as soon as SetLocalDescription finishes, without
797 // waiting for the observer to be called. This ensures that ICE candidates
798 // don't outrace the description.
799 bool SetLocalDescriptionAndSendSdpMessage(
800 std::unique_ptr<SessionDescriptionInterface> desc) {
801 rtc::scoped_refptr<MockSetSessionDescriptionObserver> observer(
802 new rtc::RefCountedObject<MockSetSessionDescriptionObserver>());
Mirko Bonadei675513b2017-11-09 11:09:25 +0100803 RTC_LOG(LS_INFO) << debug_name_ << ": SetLocalDescriptionAndSendSdpMessage";
Steve Antona3a92c22017-12-07 10:27:41 -0800804 SdpType type = desc->GetType();
deadbeef1dcb1642017-03-29 21:08:16 -0700805 std::string sdp;
806 EXPECT_TRUE(desc->ToString(&sdp));
Bjorn A Mellemb689af42019-08-21 10:44:59 -0700807 RTC_LOG(LS_INFO) << debug_name_ << ": local SDP contents=\n" << sdp;
deadbeef1dcb1642017-03-29 21:08:16 -0700808 pc()->SetLocalDescription(observer, desc.release());
Seth Hampson2f0d7022018-02-20 11:54:42 -0800809 if (sdp_semantics_ == SdpSemantics::kUnifiedPlan) {
810 RemoveUnusedVideoRenderers();
811 }
deadbeef1dcb1642017-03-29 21:08:16 -0700812 // As mentioned above, we need to send the message immediately after
813 // SetLocalDescription.
814 SendSdpMessage(type, sdp);
815 EXPECT_TRUE_WAIT(observer->called(), kDefaultTimeout);
816 return true;
817 }
818
819 bool SetRemoteDescription(std::unique_ptr<SessionDescriptionInterface> desc) {
820 rtc::scoped_refptr<MockSetSessionDescriptionObserver> observer(
821 new rtc::RefCountedObject<MockSetSessionDescriptionObserver>());
Mirko Bonadei675513b2017-11-09 11:09:25 +0100822 RTC_LOG(LS_INFO) << debug_name_ << ": SetRemoteDescription";
deadbeef1dcb1642017-03-29 21:08:16 -0700823 pc()->SetRemoteDescription(observer, desc.release());
Seth Hampson2f0d7022018-02-20 11:54:42 -0800824 if (sdp_semantics_ == SdpSemantics::kUnifiedPlan) {
825 RemoveUnusedVideoRenderers();
826 }
deadbeef1dcb1642017-03-29 21:08:16 -0700827 EXPECT_TRUE_WAIT(observer->called(), kDefaultTimeout);
828 return observer->result();
829 }
830
Seth Hampson2f0d7022018-02-20 11:54:42 -0800831 // This is a work around to remove unused fake_video_renderers from
832 // transceivers that have either stopped or are no longer receiving.
833 void RemoveUnusedVideoRenderers() {
834 auto transceivers = pc()->GetTransceivers();
835 for (auto& transceiver : transceivers) {
836 if (transceiver->receiver()->media_type() != cricket::MEDIA_TYPE_VIDEO) {
837 continue;
838 }
839 // Remove fake video renderers from any stopped transceivers.
840 if (transceiver->stopped()) {
841 auto it =
842 fake_video_renderers_.find(transceiver->receiver()->track()->id());
843 if (it != fake_video_renderers_.end()) {
844 fake_video_renderers_.erase(it);
845 }
846 }
847 // Remove fake video renderers from any transceivers that are no longer
848 // receiving.
849 if ((transceiver->current_direction() &&
850 !webrtc::RtpTransceiverDirectionHasRecv(
851 *transceiver->current_direction()))) {
852 auto it =
853 fake_video_renderers_.find(transceiver->receiver()->track()->id());
854 if (it != fake_video_renderers_.end()) {
855 fake_video_renderers_.erase(it);
856 }
857 }
858 }
859 }
860
deadbeef1dcb1642017-03-29 21:08:16 -0700861 // Simulate sending a blob of SDP with delay |signaling_delay_ms_| (0 by
862 // default).
Steve Antona3a92c22017-12-07 10:27:41 -0800863 void SendSdpMessage(SdpType type, const std::string& msg) {
deadbeef1dcb1642017-03-29 21:08:16 -0700864 if (signaling_delay_ms_ == 0) {
865 RelaySdpMessageIfReceiverExists(type, msg);
866 } else {
867 invoker_.AsyncInvokeDelayed<void>(
868 RTC_FROM_HERE, rtc::Thread::Current(),
869 rtc::Bind(&PeerConnectionWrapper::RelaySdpMessageIfReceiverExists,
870 this, type, msg),
871 signaling_delay_ms_);
872 }
873 }
874
Steve Antona3a92c22017-12-07 10:27:41 -0800875 void RelaySdpMessageIfReceiverExists(SdpType type, const std::string& msg) {
deadbeef1dcb1642017-03-29 21:08:16 -0700876 if (signaling_message_receiver_) {
877 signaling_message_receiver_->ReceiveSdpMessage(type, msg);
878 }
879 }
880
881 // Simulate trickling an ICE candidate with delay |signaling_delay_ms_| (0 by
882 // default).
883 void SendIceMessage(const std::string& sdp_mid,
884 int sdp_mline_index,
885 const std::string& msg) {
886 if (signaling_delay_ms_ == 0) {
887 RelayIceMessageIfReceiverExists(sdp_mid, sdp_mline_index, msg);
888 } else {
889 invoker_.AsyncInvokeDelayed<void>(
890 RTC_FROM_HERE, rtc::Thread::Current(),
891 rtc::Bind(&PeerConnectionWrapper::RelayIceMessageIfReceiverExists,
892 this, sdp_mid, sdp_mline_index, msg),
893 signaling_delay_ms_);
894 }
895 }
896
897 void RelayIceMessageIfReceiverExists(const std::string& sdp_mid,
898 int sdp_mline_index,
899 const std::string& msg) {
900 if (signaling_message_receiver_) {
901 signaling_message_receiver_->ReceiveIceMessage(sdp_mid, sdp_mline_index,
902 msg);
903 }
904 }
905
906 // SignalingMessageReceiver callbacks.
Steve Antona3a92c22017-12-07 10:27:41 -0800907 void ReceiveSdpMessage(SdpType type, const std::string& msg) override {
908 if (type == SdpType::kOffer) {
deadbeef1dcb1642017-03-29 21:08:16 -0700909 HandleIncomingOffer(msg);
910 } else {
911 HandleIncomingAnswer(msg);
912 }
913 }
914
915 void ReceiveIceMessage(const std::string& sdp_mid,
916 int sdp_mline_index,
917 const std::string& msg) override {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100918 RTC_LOG(LS_INFO) << debug_name_ << ": ReceiveIceMessage";
deadbeef1dcb1642017-03-29 21:08:16 -0700919 std::unique_ptr<webrtc::IceCandidateInterface> candidate(
920 webrtc::CreateIceCandidate(sdp_mid, sdp_mline_index, msg, nullptr));
921 EXPECT_TRUE(pc()->AddIceCandidate(candidate.get()));
922 }
923
924 // PeerConnectionObserver callbacks.
925 void OnSignalingChange(
926 webrtc::PeerConnectionInterface::SignalingState new_state) override {
927 EXPECT_EQ(pc()->signaling_state(), new_state);
Eldar Rello5ab79e62019-10-09 18:29:44 +0300928 peer_connection_signaling_state_history_.push_back(new_state);
deadbeef1dcb1642017-03-29 21:08:16 -0700929 }
Steve Anton15324772018-01-16 10:26:49 -0800930 void OnAddTrack(rtc::scoped_refptr<RtpReceiverInterface> receiver,
931 const std::vector<rtc::scoped_refptr<MediaStreamInterface>>&
932 streams) override {
933 if (receiver->media_type() == cricket::MEDIA_TYPE_VIDEO) {
934 rtc::scoped_refptr<VideoTrackInterface> video_track(
935 static_cast<VideoTrackInterface*>(receiver->track().get()));
936 ASSERT_TRUE(fake_video_renderers_.find(video_track->id()) ==
deadbeef1dcb1642017-03-29 21:08:16 -0700937 fake_video_renderers_.end());
Steve Anton15324772018-01-16 10:26:49 -0800938 fake_video_renderers_[video_track->id()] =
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200939 std::make_unique<FakeVideoTrackRenderer>(video_track);
deadbeef1dcb1642017-03-29 21:08:16 -0700940 }
941 }
Steve Anton15324772018-01-16 10:26:49 -0800942 void OnRemoveTrack(
943 rtc::scoped_refptr<RtpReceiverInterface> receiver) override {
944 if (receiver->media_type() == cricket::MEDIA_TYPE_VIDEO) {
945 auto it = fake_video_renderers_.find(receiver->track()->id());
946 RTC_DCHECK(it != fake_video_renderers_.end());
947 fake_video_renderers_.erase(it);
948 }
949 }
deadbeef1dcb1642017-03-29 21:08:16 -0700950 void OnRenegotiationNeeded() override {}
951 void OnIceConnectionChange(
952 webrtc::PeerConnectionInterface::IceConnectionState new_state) override {
953 EXPECT_EQ(pc()->ice_connection_state(), new_state);
Steve Antonede9ca52017-10-16 13:04:27 -0700954 ice_connection_state_history_.push_back(new_state);
deadbeef1dcb1642017-03-29 21:08:16 -0700955 }
Jonas Olssonacd8ae72019-02-25 15:26:24 +0100956 void OnStandardizedIceConnectionChange(
957 webrtc::PeerConnectionInterface::IceConnectionState new_state) override {
958 standardized_ice_connection_state_history_.push_back(new_state);
959 }
Jonas Olsson635474e2018-10-18 15:58:17 +0200960 void OnConnectionChange(
961 webrtc::PeerConnectionInterface::PeerConnectionState new_state) override {
962 peer_connection_state_history_.push_back(new_state);
963 }
964
deadbeef1dcb1642017-03-29 21:08:16 -0700965 void OnIceGatheringChange(
966 webrtc::PeerConnectionInterface::IceGatheringState new_state) override {
deadbeef1dcb1642017-03-29 21:08:16 -0700967 EXPECT_EQ(pc()->ice_gathering_state(), new_state);
Steve Antonede9ca52017-10-16 13:04:27 -0700968 ice_gathering_state_history_.push_back(new_state);
deadbeef1dcb1642017-03-29 21:08:16 -0700969 }
Alex Drake00c7ecf2019-08-06 10:54:47 -0700970
971 void OnIceSelectedCandidatePairChanged(
972 const cricket::CandidatePairChangeEvent& event) {
973 ice_candidate_pair_change_history_.push_back(event);
974 }
Alex Drake43faee02019-08-12 16:27:34 -0700975
deadbeef1dcb1642017-03-29 21:08:16 -0700976 void OnIceCandidate(const webrtc::IceCandidateInterface* candidate) override {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100977 RTC_LOG(LS_INFO) << debug_name_ << ": OnIceCandidate";
deadbeef1dcb1642017-03-29 21:08:16 -0700978
Qingsi Wang1dac6d82018-12-12 15:28:47 -0800979 if (remote_async_resolver_) {
980 const auto& local_candidate = candidate->candidate();
Qingsi Wang1dac6d82018-12-12 15:28:47 -0800981 if (local_candidate.address().IsUnresolvedIP()) {
982 RTC_DCHECK(local_candidate.type() == cricket::LOCAL_PORT_TYPE);
983 rtc::SocketAddress resolved_addr(local_candidate.address());
Qingsi Wangecd30542019-05-22 14:34:56 -0700984 const auto resolved_ip = mdns_responder_->GetMappedAddressForName(
Qingsi Wang1dac6d82018-12-12 15:28:47 -0800985 local_candidate.address().hostname());
986 RTC_DCHECK(!resolved_ip.IsNil());
987 resolved_addr.SetResolvedIP(resolved_ip);
988 EXPECT_CALL(*remote_async_resolver_, GetResolvedAddress(_, _))
989 .WillOnce(DoAll(SetArgPointee<1>(resolved_addr), Return(true)));
990 EXPECT_CALL(*remote_async_resolver_, Destroy(_));
Zach Stein6fcdc2f2018-08-23 16:25:55 -0700991 }
Zach Stein6fcdc2f2018-08-23 16:25:55 -0700992 }
993
deadbeef1dcb1642017-03-29 21:08:16 -0700994 std::string ice_sdp;
Qingsi Wang1dac6d82018-12-12 15:28:47 -0800995 EXPECT_TRUE(candidate->ToString(&ice_sdp));
Steve Antonede9ca52017-10-16 13:04:27 -0700996 if (signaling_message_receiver_ == nullptr || !signal_ice_candidates_) {
deadbeef1dcb1642017-03-29 21:08:16 -0700997 // Remote party may be deleted.
998 return;
999 }
Qingsi Wang1dac6d82018-12-12 15:28:47 -08001000 SendIceMessage(candidate->sdp_mid(), candidate->sdp_mline_index(), ice_sdp);
Qingsi Wangc129c352019-04-18 10:41:58 -07001001 last_candidate_gathered_ = candidate->candidate();
deadbeef1dcb1642017-03-29 21:08:16 -07001002 }
Eldar Rello0095d372019-12-02 22:22:07 +02001003 void OnIceCandidateError(const std::string& address,
1004 int port,
Eldar Relloda13ea22019-06-01 12:23:43 +03001005 const std::string& url,
1006 int error_code,
1007 const std::string& error_text) override {
Eldar Rello0095d372019-12-02 22:22:07 +02001008 error_event_ = cricket::IceCandidateErrorEvent(address, port, url,
Eldar Relloda13ea22019-06-01 12:23:43 +03001009 error_code, error_text);
1010 }
deadbeef1dcb1642017-03-29 21:08:16 -07001011 void OnDataChannel(
1012 rtc::scoped_refptr<DataChannelInterface> data_channel) override {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001013 RTC_LOG(LS_INFO) << debug_name_ << ": OnDataChannel";
deadbeef1dcb1642017-03-29 21:08:16 -07001014 data_channel_ = data_channel;
1015 data_observer_.reset(new MockDataChannelObserver(data_channel));
1016 }
1017
deadbeef1dcb1642017-03-29 21:08:16 -07001018 std::string debug_name_;
1019
1020 std::unique_ptr<rtc::FakeNetworkManager> fake_network_manager_;
Qingsi Wangecd30542019-05-22 14:34:56 -07001021 // Reference to the mDNS responder owned by |fake_network_manager_| after set.
1022 webrtc::FakeMdnsResponder* mdns_responder_ = nullptr;
deadbeef1dcb1642017-03-29 21:08:16 -07001023
1024 rtc::scoped_refptr<webrtc::PeerConnectionInterface> peer_connection_;
1025 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface>
1026 peer_connection_factory_;
1027
Steve Antonede9ca52017-10-16 13:04:27 -07001028 cricket::PortAllocator* port_allocator_;
deadbeef1dcb1642017-03-29 21:08:16 -07001029 // Needed to keep track of number of frames sent.
1030 rtc::scoped_refptr<FakeAudioCaptureModule> fake_audio_capture_module_;
1031 // Needed to keep track of number of frames received.
1032 std::map<std::string, std::unique_ptr<webrtc::FakeVideoTrackRenderer>>
1033 fake_video_renderers_;
1034 // Needed to ensure frames aren't received for removed tracks.
1035 std::vector<std::unique_ptr<webrtc::FakeVideoTrackRenderer>>
1036 removed_fake_video_renderers_;
deadbeef1dcb1642017-03-29 21:08:16 -07001037
1038 // For remote peer communication.
1039 SignalingMessageReceiver* signaling_message_receiver_ = nullptr;
1040 int signaling_delay_ms_ = 0;
Steve Antonede9ca52017-10-16 13:04:27 -07001041 bool signal_ice_candidates_ = true;
Qingsi Wangc129c352019-04-18 10:41:58 -07001042 cricket::Candidate last_candidate_gathered_;
Eldar Relloda13ea22019-06-01 12:23:43 +03001043 cricket::IceCandidateErrorEvent error_event_;
deadbeef1dcb1642017-03-29 21:08:16 -07001044
Niels Möller5c7efe72018-05-11 10:34:46 +02001045 // Store references to the video sources we've created, so that we can stop
deadbeef1dcb1642017-03-29 21:08:16 -07001046 // them, if required.
Niels Möller5c7efe72018-05-11 10:34:46 +02001047 std::vector<rtc::scoped_refptr<webrtc::VideoTrackSource>>
1048 video_track_sources_;
deadbeef1dcb1642017-03-29 21:08:16 -07001049 // |local_video_renderer_| attached to the first created local video track.
1050 std::unique_ptr<webrtc::FakeVideoTrackRenderer> local_video_renderer_;
1051
Seth Hampson2f0d7022018-02-20 11:54:42 -08001052 SdpSemantics sdp_semantics_;
deadbeef1dcb1642017-03-29 21:08:16 -07001053 PeerConnectionInterface::RTCOfferAnswerOptions offer_answer_options_;
1054 std::function<void(cricket::SessionDescription*)> received_sdp_munger_;
1055 std::function<void(cricket::SessionDescription*)> generated_sdp_munger_;
Seth Hampson2f0d7022018-02-20 11:54:42 -08001056 std::function<void()> remote_offer_handler_;
Qingsi Wang1dac6d82018-12-12 15:28:47 -08001057 rtc::MockAsyncResolver* remote_async_resolver_ = nullptr;
deadbeef1dcb1642017-03-29 21:08:16 -07001058 rtc::scoped_refptr<DataChannelInterface> data_channel_;
1059 std::unique_ptr<MockDataChannelObserver> data_observer_;
1060
1061 std::vector<std::unique_ptr<MockRtpReceiverObserver>> rtp_receiver_observers_;
1062
Steve Antonede9ca52017-10-16 13:04:27 -07001063 std::vector<PeerConnectionInterface::IceConnectionState>
1064 ice_connection_state_history_;
Jonas Olssonacd8ae72019-02-25 15:26:24 +01001065 std::vector<PeerConnectionInterface::IceConnectionState>
1066 standardized_ice_connection_state_history_;
Jonas Olsson635474e2018-10-18 15:58:17 +02001067 std::vector<PeerConnectionInterface::PeerConnectionState>
1068 peer_connection_state_history_;
Steve Antonede9ca52017-10-16 13:04:27 -07001069 std::vector<PeerConnectionInterface::IceGatheringState>
1070 ice_gathering_state_history_;
Alex Drake00c7ecf2019-08-06 10:54:47 -07001071 std::vector<cricket::CandidatePairChangeEvent>
1072 ice_candidate_pair_change_history_;
Eldar Rello5ab79e62019-10-09 18:29:44 +03001073 std::vector<PeerConnectionInterface::SignalingState>
1074 peer_connection_signaling_state_history_;
Qingsi Wang7685e862018-06-11 20:15:46 -07001075 webrtc::FakeRtcEventLogFactory* event_log_factory_;
1076
deadbeef1dcb1642017-03-29 21:08:16 -07001077 rtc::AsyncInvoker invoker_;
1078
Seth Hampson2f0d7022018-02-20 11:54:42 -08001079 friend class PeerConnectionIntegrationBaseTest;
deadbeef1dcb1642017-03-29 21:08:16 -07001080};
1081
Elad Alon99c3fe52017-10-13 16:29:40 +02001082class MockRtcEventLogOutput : public webrtc::RtcEventLogOutput {
1083 public:
1084 virtual ~MockRtcEventLogOutput() = default;
Danil Chapovalov3a353122020-05-15 11:16:53 +02001085 MOCK_METHOD(bool, IsActive, (), (const, override));
1086 MOCK_METHOD(bool, Write, (const std::string&), (override));
Elad Alon99c3fe52017-10-13 16:29:40 +02001087};
1088
Seth Hampson2f0d7022018-02-20 11:54:42 -08001089// This helper object is used for both specifying how many audio/video frames
1090// are expected to be received for a caller/callee. It provides helper functions
1091// to specify these expectations. The object initially starts in a state of no
1092// expectations.
1093class MediaExpectations {
1094 public:
1095 enum ExpectFrames {
1096 kExpectSomeFrames,
1097 kExpectNoFrames,
1098 kNoExpectation,
1099 };
1100
1101 void ExpectBidirectionalAudioAndVideo() {
1102 ExpectBidirectionalAudio();
1103 ExpectBidirectionalVideo();
1104 }
1105
1106 void ExpectBidirectionalAudio() {
1107 CallerExpectsSomeAudio();
1108 CalleeExpectsSomeAudio();
1109 }
1110
1111 void ExpectNoAudio() {
1112 CallerExpectsNoAudio();
1113 CalleeExpectsNoAudio();
1114 }
1115
1116 void ExpectBidirectionalVideo() {
1117 CallerExpectsSomeVideo();
1118 CalleeExpectsSomeVideo();
1119 }
1120
1121 void ExpectNoVideo() {
1122 CallerExpectsNoVideo();
1123 CalleeExpectsNoVideo();
1124 }
1125
1126 void CallerExpectsSomeAudioAndVideo() {
1127 CallerExpectsSomeAudio();
1128 CallerExpectsSomeVideo();
1129 }
1130
1131 void CalleeExpectsSomeAudioAndVideo() {
1132 CalleeExpectsSomeAudio();
1133 CalleeExpectsSomeVideo();
1134 }
1135
1136 // Caller's audio functions.
1137 void CallerExpectsSomeAudio(
1138 int expected_audio_frames = kDefaultExpectedAudioFrameCount) {
1139 caller_audio_expectation_ = kExpectSomeFrames;
1140 caller_audio_frames_expected_ = expected_audio_frames;
1141 }
1142
1143 void CallerExpectsNoAudio() {
1144 caller_audio_expectation_ = kExpectNoFrames;
1145 caller_audio_frames_expected_ = 0;
1146 }
1147
1148 // Caller's video functions.
1149 void CallerExpectsSomeVideo(
1150 int expected_video_frames = kDefaultExpectedVideoFrameCount) {
1151 caller_video_expectation_ = kExpectSomeFrames;
1152 caller_video_frames_expected_ = expected_video_frames;
1153 }
1154
1155 void CallerExpectsNoVideo() {
1156 caller_video_expectation_ = kExpectNoFrames;
1157 caller_video_frames_expected_ = 0;
1158 }
1159
1160 // Callee's audio functions.
1161 void CalleeExpectsSomeAudio(
1162 int expected_audio_frames = kDefaultExpectedAudioFrameCount) {
1163 callee_audio_expectation_ = kExpectSomeFrames;
1164 callee_audio_frames_expected_ = expected_audio_frames;
1165 }
1166
1167 void CalleeExpectsNoAudio() {
1168 callee_audio_expectation_ = kExpectNoFrames;
1169 callee_audio_frames_expected_ = 0;
1170 }
1171
1172 // Callee's video functions.
1173 void CalleeExpectsSomeVideo(
1174 int expected_video_frames = kDefaultExpectedVideoFrameCount) {
1175 callee_video_expectation_ = kExpectSomeFrames;
1176 callee_video_frames_expected_ = expected_video_frames;
1177 }
1178
1179 void CalleeExpectsNoVideo() {
1180 callee_video_expectation_ = kExpectNoFrames;
1181 callee_video_frames_expected_ = 0;
1182 }
1183
1184 ExpectFrames caller_audio_expectation_ = kNoExpectation;
1185 ExpectFrames caller_video_expectation_ = kNoExpectation;
1186 ExpectFrames callee_audio_expectation_ = kNoExpectation;
1187 ExpectFrames callee_video_expectation_ = kNoExpectation;
1188 int caller_audio_frames_expected_ = 0;
1189 int caller_video_frames_expected_ = 0;
1190 int callee_audio_frames_expected_ = 0;
1191 int callee_video_frames_expected_ = 0;
1192};
1193
Qingsi Wang25ec8882019-11-15 12:33:05 -08001194class MockIceTransport : public webrtc::IceTransportInterface {
1195 public:
1196 MockIceTransport(const std::string& name, int component)
1197 : internal_(std::make_unique<cricket::FakeIceTransport>(
1198 name,
1199 component,
1200 nullptr /* network_thread */)) {}
1201 ~MockIceTransport() = default;
1202 cricket::IceTransportInternal* internal() { return internal_.get(); }
1203
1204 private:
1205 std::unique_ptr<cricket::FakeIceTransport> internal_;
1206};
1207
1208class MockIceTransportFactory : public IceTransportFactory {
1209 public:
1210 ~MockIceTransportFactory() override = default;
1211 rtc::scoped_refptr<IceTransportInterface> CreateIceTransport(
1212 const std::string& transport_name,
1213 int component,
1214 IceTransportInit init) {
1215 RecordIceTransportCreated();
1216 return new rtc::RefCountedObject<MockIceTransport>(transport_name,
1217 component);
1218 }
Danil Chapovalov3a353122020-05-15 11:16:53 +02001219 MOCK_METHOD(void, RecordIceTransportCreated, ());
Qingsi Wang25ec8882019-11-15 12:33:05 -08001220};
1221
deadbeef1dcb1642017-03-29 21:08:16 -07001222// Tests two PeerConnections connecting to each other end-to-end, using a
1223// virtual network, fake A/V capture and fake encoder/decoders. The
1224// PeerConnections share the threads/socket servers, but use separate versions
1225// of everything else (including "PeerConnectionFactory"s).
Mirko Bonadei6a489f22019-04-09 15:11:12 +02001226class PeerConnectionIntegrationBaseTest : public ::testing::Test {
deadbeef1dcb1642017-03-29 21:08:16 -07001227 public:
Seth Hampson2f0d7022018-02-20 11:54:42 -08001228 explicit PeerConnectionIntegrationBaseTest(SdpSemantics sdp_semantics)
1229 : sdp_semantics_(sdp_semantics),
1230 ss_(new rtc::VirtualSocketServer()),
Steve Antonede9ca52017-10-16 13:04:27 -07001231 fss_(new rtc::FirewallSocketServer(ss_.get())),
1232 network_thread_(new rtc::Thread(fss_.get())),
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001233 worker_thread_(rtc::Thread::Create()),
1234 loopback_media_transports_(network_thread_.get()) {
Sebastian Jansson8a793a02018-03-13 15:21:48 +01001235 network_thread_->SetName("PCNetworkThread", this);
1236 worker_thread_->SetName("PCWorkerThread", this);
deadbeef1dcb1642017-03-29 21:08:16 -07001237 RTC_CHECK(network_thread_->Start());
1238 RTC_CHECK(worker_thread_->Start());
Qingsi Wang7fc821d2018-07-12 12:54:53 -07001239 webrtc::metrics::Reset();
deadbeef1dcb1642017-03-29 21:08:16 -07001240 }
1241
Seth Hampson2f0d7022018-02-20 11:54:42 -08001242 ~PeerConnectionIntegrationBaseTest() {
Seth Hampsonaed71642018-06-11 07:41:32 -07001243 // The PeerConnections should deleted before the TurnCustomizers.
1244 // A TurnPort is created with a raw pointer to a TurnCustomizer. The
1245 // TurnPort has the same lifetime as the PeerConnection, so it's expected
1246 // that the TurnCustomizer outlives the life of the PeerConnection or else
1247 // when Send() is called it will hit a seg fault.
deadbeef1dcb1642017-03-29 21:08:16 -07001248 if (caller_) {
1249 caller_->set_signaling_message_receiver(nullptr);
Seth Hampsonaed71642018-06-11 07:41:32 -07001250 delete SetCallerPcWrapperAndReturnCurrent(nullptr);
deadbeef1dcb1642017-03-29 21:08:16 -07001251 }
1252 if (callee_) {
1253 callee_->set_signaling_message_receiver(nullptr);
Seth Hampsonaed71642018-06-11 07:41:32 -07001254 delete SetCalleePcWrapperAndReturnCurrent(nullptr);
deadbeef1dcb1642017-03-29 21:08:16 -07001255 }
Seth Hampsonaed71642018-06-11 07:41:32 -07001256
1257 // If turn servers were created for the test they need to be destroyed on
1258 // the network thread.
1259 network_thread()->Invoke<void>(RTC_FROM_HERE, [this] {
1260 turn_servers_.clear();
1261 turn_customizers_.clear();
1262 });
deadbeef1dcb1642017-03-29 21:08:16 -07001263 }
1264
1265 bool SignalingStateStable() {
1266 return caller_->SignalingStateStable() && callee_->SignalingStateStable();
1267 }
1268
deadbeef71452802017-05-07 17:21:01 -07001269 bool DtlsConnected() {
Alex Loiko9289eda2018-11-23 16:18:59 +00001270 // TODO(deadbeef): kIceConnectionConnected currently means both ICE and DTLS
1271 // are connected. This is an important distinction. Once we have separate
1272 // ICE and DTLS state, this check needs to use the DTLS state.
1273 return (callee()->ice_connection_state() ==
1274 webrtc::PeerConnectionInterface::kIceConnectionConnected ||
1275 callee()->ice_connection_state() ==
1276 webrtc::PeerConnectionInterface::kIceConnectionCompleted) &&
1277 (caller()->ice_connection_state() ==
1278 webrtc::PeerConnectionInterface::kIceConnectionConnected ||
1279 caller()->ice_connection_state() ==
1280 webrtc::PeerConnectionInterface::kIceConnectionCompleted);
deadbeef71452802017-05-07 17:21:01 -07001281 }
1282
Qingsi Wang7685e862018-06-11 20:15:46 -07001283 // When |event_log_factory| is null, the default implementation of the event
1284 // log factory will be used.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001285 std::unique_ptr<PeerConnectionWrapper> CreatePeerConnectionWrapper(
1286 const std::string& debug_name,
Seth Hampson2f0d7022018-02-20 11:54:42 -08001287 const PeerConnectionFactory::Options* options,
1288 const RTCConfiguration* config,
Qingsi Wang7685e862018-06-11 20:15:46 -07001289 webrtc::PeerConnectionDependencies dependencies,
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001290 std::unique_ptr<webrtc::FakeRtcEventLogFactory> event_log_factory,
Johannes Kron3e983682020-03-29 22:17:00 +02001291 std::unique_ptr<webrtc::MediaTransportFactory> media_transport_factory,
1292 bool reset_encoder_factory,
1293 bool reset_decoder_factory) {
Seth Hampson2f0d7022018-02-20 11:54:42 -08001294 RTCConfiguration modified_config;
1295 if (config) {
1296 modified_config = *config;
1297 }
Steve Anton3acffc32018-04-12 17:21:03 -07001298 modified_config.sdp_semantics = sdp_semantics_;
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001299 if (!dependencies.cert_generator) {
1300 dependencies.cert_generator =
Mirko Bonadei317a1f02019-09-17 17:06:18 +02001301 std::make_unique<FakeRTCCertificateGenerator>();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001302 }
1303 std::unique_ptr<PeerConnectionWrapper> client(
1304 new PeerConnectionWrapper(debug_name));
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001305
Niels Möllerf06f9232018-08-07 12:32:18 +02001306 if (!client->Init(options, &modified_config, std::move(dependencies),
1307 network_thread_.get(), worker_thread_.get(),
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001308 std::move(event_log_factory),
Johannes Kron3e983682020-03-29 22:17:00 +02001309 std::move(media_transport_factory), reset_encoder_factory,
1310 reset_decoder_factory)) {
Seth Hampson2f0d7022018-02-20 11:54:42 -08001311 return nullptr;
1312 }
1313 return client;
1314 }
1315
Qingsi Wang7685e862018-06-11 20:15:46 -07001316 std::unique_ptr<PeerConnectionWrapper>
1317 CreatePeerConnectionWrapperWithFakeRtcEventLog(
1318 const std::string& debug_name,
Qingsi Wang7685e862018-06-11 20:15:46 -07001319 const PeerConnectionFactory::Options* options,
1320 const RTCConfiguration* config,
1321 webrtc::PeerConnectionDependencies dependencies) {
1322 std::unique_ptr<webrtc::FakeRtcEventLogFactory> event_log_factory(
1323 new webrtc::FakeRtcEventLogFactory(rtc::Thread::Current()));
Johannes Kron3e983682020-03-29 22:17:00 +02001324 return CreatePeerConnectionWrapper(
1325 debug_name, options, config, std::move(dependencies),
1326 std::move(event_log_factory),
1327 /*media_transport_factory=*/nullptr, /*reset_encoder_factory=*/false,
1328 /*reset_decoder_factory=*/false);
Qingsi Wang7685e862018-06-11 20:15:46 -07001329 }
1330
deadbeef1dcb1642017-03-29 21:08:16 -07001331 bool CreatePeerConnectionWrappers() {
1332 return CreatePeerConnectionWrappersWithConfig(
1333 PeerConnectionInterface::RTCConfiguration(),
1334 PeerConnectionInterface::RTCConfiguration());
1335 }
1336
Steve Anton3acffc32018-04-12 17:21:03 -07001337 bool CreatePeerConnectionWrappersWithSdpSemantics(
1338 SdpSemantics caller_semantics,
1339 SdpSemantics callee_semantics) {
1340 // Can't specify the sdp_semantics in the passed-in configuration since it
1341 // will be overwritten by CreatePeerConnectionWrapper with whatever is
1342 // stored in sdp_semantics_. So get around this by modifying the instance
1343 // variable before calling CreatePeerConnectionWrapper for the caller and
1344 // callee PeerConnections.
1345 SdpSemantics original_semantics = sdp_semantics_;
1346 sdp_semantics_ = caller_semantics;
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001347 caller_ = CreatePeerConnectionWrapper(
Niels Möllerf06f9232018-08-07 12:32:18 +02001348 "Caller", nullptr, nullptr, webrtc::PeerConnectionDependencies(nullptr),
Johannes Kron3e983682020-03-29 22:17:00 +02001349 nullptr, /*media_transport_factory=*/nullptr,
1350 /*reset_encoder_factory=*/false,
1351 /*reset_decoder_factory=*/false);
Steve Anton3acffc32018-04-12 17:21:03 -07001352 sdp_semantics_ = callee_semantics;
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001353 callee_ = CreatePeerConnectionWrapper(
Niels Möllerf06f9232018-08-07 12:32:18 +02001354 "Callee", nullptr, nullptr, webrtc::PeerConnectionDependencies(nullptr),
Johannes Kron3e983682020-03-29 22:17:00 +02001355 nullptr, /*media_transport_factory=*/nullptr,
1356 /*reset_encoder_factory=*/false,
1357 /*reset_decoder_factory=*/false);
Steve Anton3acffc32018-04-12 17:21:03 -07001358 sdp_semantics_ = original_semantics;
1359 return caller_ && callee_;
1360 }
1361
deadbeef1dcb1642017-03-29 21:08:16 -07001362 bool CreatePeerConnectionWrappersWithConfig(
1363 const PeerConnectionInterface::RTCConfiguration& caller_config,
1364 const PeerConnectionInterface::RTCConfiguration& callee_config) {
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001365 caller_ = CreatePeerConnectionWrapper(
Niels Möllerf06f9232018-08-07 12:32:18 +02001366 "Caller", nullptr, &caller_config,
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001367 webrtc::PeerConnectionDependencies(nullptr), nullptr,
Johannes Kron3e983682020-03-29 22:17:00 +02001368 /*media_transport_factory=*/nullptr, /*reset_encoder_factory=*/false,
1369 /*reset_decoder_factory=*/false);
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001370 callee_ = CreatePeerConnectionWrapper(
Niels Möllerf06f9232018-08-07 12:32:18 +02001371 "Callee", nullptr, &callee_config,
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001372 webrtc::PeerConnectionDependencies(nullptr), nullptr,
Johannes Kron3e983682020-03-29 22:17:00 +02001373 /*media_transport_factory=*/nullptr, /*reset_encoder_factory=*/false,
1374 /*reset_decoder_factory=*/false);
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001375 return caller_ && callee_;
1376 }
1377
1378 bool CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
1379 const PeerConnectionInterface::RTCConfiguration& caller_config,
1380 const PeerConnectionInterface::RTCConfiguration& callee_config,
1381 std::unique_ptr<webrtc::MediaTransportFactory> caller_factory,
1382 std::unique_ptr<webrtc::MediaTransportFactory> callee_factory) {
Johannes Kron3e983682020-03-29 22:17:00 +02001383 caller_ = CreatePeerConnectionWrapper(
1384 "Caller", nullptr, &caller_config,
1385 webrtc::PeerConnectionDependencies(nullptr), nullptr,
1386 std::move(caller_factory), /*reset_encoder_factory=*/false,
1387 /*reset_decoder_factory=*/false);
1388 callee_ = CreatePeerConnectionWrapper(
1389 "Callee", nullptr, &callee_config,
1390 webrtc::PeerConnectionDependencies(nullptr), nullptr,
1391 std::move(callee_factory), /*reset_encoder_factory=*/false,
1392 /*reset_decoder_factory=*/false);
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001393 return caller_ && callee_;
1394 }
1395
1396 bool CreatePeerConnectionWrappersWithConfigAndDeps(
1397 const PeerConnectionInterface::RTCConfiguration& caller_config,
1398 webrtc::PeerConnectionDependencies caller_dependencies,
1399 const PeerConnectionInterface::RTCConfiguration& callee_config,
1400 webrtc::PeerConnectionDependencies callee_dependencies) {
Johannes Kron3e983682020-03-29 22:17:00 +02001401 caller_ = CreatePeerConnectionWrapper(
1402 "Caller", nullptr, &caller_config, std::move(caller_dependencies),
1403 nullptr,
1404 /*media_transport_factory=*/nullptr, /*reset_encoder_factory=*/false,
1405 /*reset_decoder_factory=*/false);
1406 callee_ = CreatePeerConnectionWrapper(
1407 "Callee", nullptr, &callee_config, std::move(callee_dependencies),
1408 nullptr,
1409 /*media_transport_factory=*/nullptr, /*reset_encoder_factory=*/false,
1410 /*reset_decoder_factory=*/false);
deadbeef1dcb1642017-03-29 21:08:16 -07001411 return caller_ && callee_;
1412 }
1413
1414 bool CreatePeerConnectionWrappersWithOptions(
1415 const PeerConnectionFactory::Options& caller_options,
1416 const PeerConnectionFactory::Options& callee_options) {
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001417 caller_ = CreatePeerConnectionWrapper(
Niels Möllerf06f9232018-08-07 12:32:18 +02001418 "Caller", &caller_options, nullptr,
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001419 webrtc::PeerConnectionDependencies(nullptr), nullptr,
Johannes Kron3e983682020-03-29 22:17:00 +02001420 /*media_transport_factory=*/nullptr, /*reset_encoder_factory=*/false,
1421 /*reset_decoder_factory=*/false);
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001422 callee_ = CreatePeerConnectionWrapper(
Niels Möllerf06f9232018-08-07 12:32:18 +02001423 "Callee", &callee_options, nullptr,
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001424 webrtc::PeerConnectionDependencies(nullptr), nullptr,
Johannes Kron3e983682020-03-29 22:17:00 +02001425 /*media_transport_factory=*/nullptr, /*reset_encoder_factory=*/false,
1426 /*reset_decoder_factory=*/false);
Qingsi Wang7685e862018-06-11 20:15:46 -07001427 return caller_ && callee_;
1428 }
1429
1430 bool CreatePeerConnectionWrappersWithFakeRtcEventLog() {
1431 PeerConnectionInterface::RTCConfiguration default_config;
1432 caller_ = CreatePeerConnectionWrapperWithFakeRtcEventLog(
Niels Möllerf06f9232018-08-07 12:32:18 +02001433 "Caller", nullptr, &default_config,
Qingsi Wang7685e862018-06-11 20:15:46 -07001434 webrtc::PeerConnectionDependencies(nullptr));
1435 callee_ = CreatePeerConnectionWrapperWithFakeRtcEventLog(
Niels Möllerf06f9232018-08-07 12:32:18 +02001436 "Callee", nullptr, &default_config,
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001437 webrtc::PeerConnectionDependencies(nullptr));
deadbeef1dcb1642017-03-29 21:08:16 -07001438 return caller_ && callee_;
1439 }
1440
Seth Hampson2f0d7022018-02-20 11:54:42 -08001441 std::unique_ptr<PeerConnectionWrapper>
1442 CreatePeerConnectionWrapperWithAlternateKey() {
deadbeef1dcb1642017-03-29 21:08:16 -07001443 std::unique_ptr<FakeRTCCertificateGenerator> cert_generator(
1444 new FakeRTCCertificateGenerator());
1445 cert_generator->use_alternate_key();
1446
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001447 webrtc::PeerConnectionDependencies dependencies(nullptr);
1448 dependencies.cert_generator = std::move(cert_generator);
Johannes Kron3e983682020-03-29 22:17:00 +02001449 return CreatePeerConnectionWrapper(
1450 "New Peer", nullptr, nullptr, std::move(dependencies), nullptr,
1451 /*media_transport_factory=*/nullptr, /*reset_encoder_factory=*/false,
1452 /*reset_decoder_factory=*/false);
1453 }
1454
1455 bool CreateOneDirectionalPeerConnectionWrappers(bool caller_to_callee) {
1456 caller_ = CreatePeerConnectionWrapper(
1457 "Caller", nullptr, nullptr, webrtc::PeerConnectionDependencies(nullptr),
1458 nullptr, /*media_transport_factory=*/nullptr,
1459 /*reset_encoder_factory=*/!caller_to_callee,
1460 /*reset_decoder_factory=*/caller_to_callee);
1461 callee_ = CreatePeerConnectionWrapper(
1462 "Callee", nullptr, nullptr, webrtc::PeerConnectionDependencies(nullptr),
1463 nullptr, /*media_transport_factory=*/nullptr,
1464 /*reset_encoder_factory=*/caller_to_callee,
1465 /*reset_decoder_factory=*/!caller_to_callee);
1466 return caller_ && callee_;
deadbeef1dcb1642017-03-29 21:08:16 -07001467 }
1468
Seth Hampsonaed71642018-06-11 07:41:32 -07001469 cricket::TestTurnServer* CreateTurnServer(
1470 rtc::SocketAddress internal_address,
1471 rtc::SocketAddress external_address,
1472 cricket::ProtocolType type = cricket::ProtocolType::PROTO_UDP,
1473 const std::string& common_name = "test turn server") {
1474 rtc::Thread* thread = network_thread();
1475 std::unique_ptr<cricket::TestTurnServer> turn_server =
1476 network_thread()->Invoke<std::unique_ptr<cricket::TestTurnServer>>(
1477 RTC_FROM_HERE,
1478 [thread, internal_address, external_address, type, common_name] {
Mirko Bonadei317a1f02019-09-17 17:06:18 +02001479 return std::make_unique<cricket::TestTurnServer>(
Seth Hampsonaed71642018-06-11 07:41:32 -07001480 thread, internal_address, external_address, type,
1481 /*ignore_bad_certs=*/true, common_name);
1482 });
1483 turn_servers_.push_back(std::move(turn_server));
1484 // Interactions with the turn server should be done on the network thread.
1485 return turn_servers_.back().get();
1486 }
1487
1488 cricket::TestTurnCustomizer* CreateTurnCustomizer() {
1489 std::unique_ptr<cricket::TestTurnCustomizer> turn_customizer =
1490 network_thread()->Invoke<std::unique_ptr<cricket::TestTurnCustomizer>>(
1491 RTC_FROM_HERE,
Mirko Bonadei317a1f02019-09-17 17:06:18 +02001492 [] { return std::make_unique<cricket::TestTurnCustomizer>(); });
Seth Hampsonaed71642018-06-11 07:41:32 -07001493 turn_customizers_.push_back(std::move(turn_customizer));
1494 // Interactions with the turn customizer should be done on the network
1495 // thread.
1496 return turn_customizers_.back().get();
1497 }
1498
1499 // Checks that the function counters for a TestTurnCustomizer are greater than
1500 // 0.
1501 void ExpectTurnCustomizerCountersIncremented(
1502 cricket::TestTurnCustomizer* turn_customizer) {
1503 unsigned int allow_channel_data_counter =
1504 network_thread()->Invoke<unsigned int>(
1505 RTC_FROM_HERE, [turn_customizer] {
1506 return turn_customizer->allow_channel_data_cnt_;
1507 });
1508 EXPECT_GT(allow_channel_data_counter, 0u);
1509 unsigned int modify_counter = network_thread()->Invoke<unsigned int>(
1510 RTC_FROM_HERE,
1511 [turn_customizer] { return turn_customizer->modify_cnt_; });
1512 EXPECT_GT(modify_counter, 0u);
1513 }
1514
deadbeef1dcb1642017-03-29 21:08:16 -07001515 // Once called, SDP blobs and ICE candidates will be automatically signaled
1516 // between PeerConnections.
1517 void ConnectFakeSignaling() {
1518 caller_->set_signaling_message_receiver(callee_.get());
1519 callee_->set_signaling_message_receiver(caller_.get());
1520 }
1521
Steve Antonede9ca52017-10-16 13:04:27 -07001522 // Once called, SDP blobs will be automatically signaled between
1523 // PeerConnections. Note that ICE candidates will not be signaled unless they
1524 // are in the exchanged SDP blobs.
1525 void ConnectFakeSignalingForSdpOnly() {
1526 ConnectFakeSignaling();
1527 SetSignalIceCandidates(false);
1528 }
1529
deadbeef1dcb1642017-03-29 21:08:16 -07001530 void SetSignalingDelayMs(int delay_ms) {
1531 caller_->set_signaling_delay_ms(delay_ms);
1532 callee_->set_signaling_delay_ms(delay_ms);
1533 }
1534
Steve Antonede9ca52017-10-16 13:04:27 -07001535 void SetSignalIceCandidates(bool signal) {
1536 caller_->set_signal_ice_candidates(signal);
1537 callee_->set_signal_ice_candidates(signal);
1538 }
1539
deadbeef1dcb1642017-03-29 21:08:16 -07001540 // Messages may get lost on the unreliable DataChannel, so we send multiple
1541 // times to avoid test flakiness.
1542 void SendRtpDataWithRetries(webrtc::DataChannelInterface* dc,
1543 const std::string& data,
1544 int retries) {
1545 for (int i = 0; i < retries; ++i) {
1546 dc->Send(DataBuffer(data));
1547 }
1548 }
1549
1550 rtc::Thread* network_thread() { return network_thread_.get(); }
1551
1552 rtc::VirtualSocketServer* virtual_socket_server() { return ss_.get(); }
1553
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001554 webrtc::MediaTransportPair* loopback_media_transports() {
1555 return &loopback_media_transports_;
1556 }
1557
deadbeef1dcb1642017-03-29 21:08:16 -07001558 PeerConnectionWrapper* caller() { return caller_.get(); }
1559
1560 // Set the |caller_| to the |wrapper| passed in and return the
1561 // original |caller_|.
1562 PeerConnectionWrapper* SetCallerPcWrapperAndReturnCurrent(
1563 PeerConnectionWrapper* wrapper) {
1564 PeerConnectionWrapper* old = caller_.release();
1565 caller_.reset(wrapper);
1566 return old;
1567 }
1568
1569 PeerConnectionWrapper* callee() { return callee_.get(); }
1570
1571 // Set the |callee_| to the |wrapper| passed in and return the
1572 // original |callee_|.
1573 PeerConnectionWrapper* SetCalleePcWrapperAndReturnCurrent(
1574 PeerConnectionWrapper* wrapper) {
1575 PeerConnectionWrapper* old = callee_.release();
1576 callee_.reset(wrapper);
1577 return old;
1578 }
1579
Qingsi Wang1dac6d82018-12-12 15:28:47 -08001580 void SetPortAllocatorFlags(uint32_t caller_flags, uint32_t callee_flags) {
1581 network_thread()->Invoke<void>(
1582 RTC_FROM_HERE, rtc::Bind(&cricket::PortAllocator::set_flags,
1583 caller()->port_allocator(), caller_flags));
1584 network_thread()->Invoke<void>(
1585 RTC_FROM_HERE, rtc::Bind(&cricket::PortAllocator::set_flags,
1586 callee()->port_allocator(), callee_flags));
1587 }
1588
Steve Antonede9ca52017-10-16 13:04:27 -07001589 rtc::FirewallSocketServer* firewall() const { return fss_.get(); }
1590
Seth Hampson2f0d7022018-02-20 11:54:42 -08001591 // Expects the provided number of new frames to be received within
1592 // kMaxWaitForFramesMs. The new expected frames are specified in
1593 // |media_expectations|. Returns false if any of the expectations were
1594 // not met.
1595 bool ExpectNewFrames(const MediaExpectations& media_expectations) {
1596 // First initialize the expected frame counts based upon the current
1597 // frame count.
1598 int total_caller_audio_frames_expected = caller()->audio_frames_received();
1599 if (media_expectations.caller_audio_expectation_ ==
1600 MediaExpectations::kExpectSomeFrames) {
1601 total_caller_audio_frames_expected +=
1602 media_expectations.caller_audio_frames_expected_;
1603 }
1604 int total_caller_video_frames_expected =
deadbeef1dcb1642017-03-29 21:08:16 -07001605 caller()->min_video_frames_received_per_track();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001606 if (media_expectations.caller_video_expectation_ ==
1607 MediaExpectations::kExpectSomeFrames) {
1608 total_caller_video_frames_expected +=
1609 media_expectations.caller_video_frames_expected_;
1610 }
1611 int total_callee_audio_frames_expected = callee()->audio_frames_received();
1612 if (media_expectations.callee_audio_expectation_ ==
1613 MediaExpectations::kExpectSomeFrames) {
1614 total_callee_audio_frames_expected +=
1615 media_expectations.callee_audio_frames_expected_;
1616 }
1617 int total_callee_video_frames_expected =
deadbeef1dcb1642017-03-29 21:08:16 -07001618 callee()->min_video_frames_received_per_track();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001619 if (media_expectations.callee_video_expectation_ ==
1620 MediaExpectations::kExpectSomeFrames) {
1621 total_callee_video_frames_expected +=
1622 media_expectations.callee_video_frames_expected_;
1623 }
deadbeef1dcb1642017-03-29 21:08:16 -07001624
Seth Hampson2f0d7022018-02-20 11:54:42 -08001625 // Wait for the expected frames.
deadbeef1dcb1642017-03-29 21:08:16 -07001626 EXPECT_TRUE_WAIT(caller()->audio_frames_received() >=
Seth Hampson2f0d7022018-02-20 11:54:42 -08001627 total_caller_audio_frames_expected &&
deadbeef1dcb1642017-03-29 21:08:16 -07001628 caller()->min_video_frames_received_per_track() >=
Seth Hampson2f0d7022018-02-20 11:54:42 -08001629 total_caller_video_frames_expected &&
deadbeef1dcb1642017-03-29 21:08:16 -07001630 callee()->audio_frames_received() >=
Seth Hampson2f0d7022018-02-20 11:54:42 -08001631 total_callee_audio_frames_expected &&
deadbeef1dcb1642017-03-29 21:08:16 -07001632 callee()->min_video_frames_received_per_track() >=
Seth Hampson2f0d7022018-02-20 11:54:42 -08001633 total_callee_video_frames_expected,
1634 kMaxWaitForFramesMs);
1635 bool expectations_correct =
1636 caller()->audio_frames_received() >=
1637 total_caller_audio_frames_expected &&
1638 caller()->min_video_frames_received_per_track() >=
1639 total_caller_video_frames_expected &&
1640 callee()->audio_frames_received() >=
1641 total_callee_audio_frames_expected &&
1642 callee()->min_video_frames_received_per_track() >=
1643 total_callee_video_frames_expected;
deadbeef1dcb1642017-03-29 21:08:16 -07001644
Seth Hampson2f0d7022018-02-20 11:54:42 -08001645 // After the combined wait, print out a more detailed message upon
1646 // failure.
deadbeef1dcb1642017-03-29 21:08:16 -07001647 EXPECT_GE(caller()->audio_frames_received(),
Seth Hampson2f0d7022018-02-20 11:54:42 -08001648 total_caller_audio_frames_expected);
deadbeef1dcb1642017-03-29 21:08:16 -07001649 EXPECT_GE(caller()->min_video_frames_received_per_track(),
Seth Hampson2f0d7022018-02-20 11:54:42 -08001650 total_caller_video_frames_expected);
deadbeef1dcb1642017-03-29 21:08:16 -07001651 EXPECT_GE(callee()->audio_frames_received(),
Seth Hampson2f0d7022018-02-20 11:54:42 -08001652 total_callee_audio_frames_expected);
deadbeef1dcb1642017-03-29 21:08:16 -07001653 EXPECT_GE(callee()->min_video_frames_received_per_track(),
Seth Hampson2f0d7022018-02-20 11:54:42 -08001654 total_callee_video_frames_expected);
1655
1656 // We want to make sure nothing unexpected was received.
1657 if (media_expectations.caller_audio_expectation_ ==
1658 MediaExpectations::kExpectNoFrames) {
1659 EXPECT_EQ(caller()->audio_frames_received(),
1660 total_caller_audio_frames_expected);
1661 if (caller()->audio_frames_received() !=
1662 total_caller_audio_frames_expected) {
1663 expectations_correct = false;
1664 }
1665 }
1666 if (media_expectations.caller_video_expectation_ ==
1667 MediaExpectations::kExpectNoFrames) {
1668 EXPECT_EQ(caller()->min_video_frames_received_per_track(),
1669 total_caller_video_frames_expected);
1670 if (caller()->min_video_frames_received_per_track() !=
1671 total_caller_video_frames_expected) {
1672 expectations_correct = false;
1673 }
1674 }
1675 if (media_expectations.callee_audio_expectation_ ==
1676 MediaExpectations::kExpectNoFrames) {
1677 EXPECT_EQ(callee()->audio_frames_received(),
1678 total_callee_audio_frames_expected);
1679 if (callee()->audio_frames_received() !=
1680 total_callee_audio_frames_expected) {
1681 expectations_correct = false;
1682 }
1683 }
1684 if (media_expectations.callee_video_expectation_ ==
1685 MediaExpectations::kExpectNoFrames) {
1686 EXPECT_EQ(callee()->min_video_frames_received_per_track(),
1687 total_callee_video_frames_expected);
1688 if (callee()->min_video_frames_received_per_track() !=
1689 total_callee_video_frames_expected) {
1690 expectations_correct = false;
1691 }
1692 }
1693 return expectations_correct;
deadbeef1dcb1642017-03-29 21:08:16 -07001694 }
1695
Steve Antond91969e2019-05-30 12:27:03 -07001696 void ClosePeerConnections() {
1697 caller()->pc()->Close();
1698 callee()->pc()->Close();
1699 }
1700
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07001701 void TestNegotiatedCipherSuite(
1702 const PeerConnectionFactory::Options& caller_options,
1703 const PeerConnectionFactory::Options& callee_options,
1704 int expected_cipher_suite) {
deadbeef1dcb1642017-03-29 21:08:16 -07001705 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(caller_options,
1706 callee_options));
deadbeef1dcb1642017-03-29 21:08:16 -07001707 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001708 caller()->AddAudioVideoTracks();
1709 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001710 caller()->CreateAndSetAndSignalOffer();
Qingsi Wang7fc821d2018-07-12 12:54:53 -07001711 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07001712 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(expected_cipher_suite),
deadbeefd8ad7882017-04-18 16:01:17 -07001713 caller()->OldGetStats()->SrtpCipher(), kDefaultTimeout);
Qingsi Wang7fc821d2018-07-12 12:54:53 -07001714 // TODO(bugs.webrtc.org/9456): Fix it.
Ying Wangef3998f2019-12-09 13:06:53 +01001715 EXPECT_METRIC_EQ(1, webrtc::metrics::NumEvents(
1716 "WebRTC.PeerConnection.SrtpCryptoSuite.Audio",
1717 expected_cipher_suite));
deadbeef1dcb1642017-03-29 21:08:16 -07001718 }
1719
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07001720 void TestGcmNegotiationUsesCipherSuite(bool local_gcm_enabled,
1721 bool remote_gcm_enabled,
Philipp Hancke2ebbff82019-10-26 06:12:55 +02001722 bool aes_ctr_enabled,
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07001723 int expected_cipher_suite) {
1724 PeerConnectionFactory::Options caller_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07001725 caller_options.crypto_options.srtp.enable_gcm_crypto_suites =
1726 local_gcm_enabled;
Philipp Hancke2ebbff82019-10-26 06:12:55 +02001727 caller_options.crypto_options.srtp.enable_aes128_sha1_80_crypto_cipher =
1728 aes_ctr_enabled;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07001729 PeerConnectionFactory::Options callee_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07001730 callee_options.crypto_options.srtp.enable_gcm_crypto_suites =
1731 remote_gcm_enabled;
Philipp Hancke2ebbff82019-10-26 06:12:55 +02001732 callee_options.crypto_options.srtp.enable_aes128_sha1_80_crypto_cipher =
1733 aes_ctr_enabled;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07001734 TestNegotiatedCipherSuite(caller_options, callee_options,
1735 expected_cipher_suite);
1736 }
1737
Seth Hampson2f0d7022018-02-20 11:54:42 -08001738 protected:
Steve Anton3acffc32018-04-12 17:21:03 -07001739 SdpSemantics sdp_semantics_;
Seth Hampson2f0d7022018-02-20 11:54:42 -08001740
deadbeef1dcb1642017-03-29 21:08:16 -07001741 private:
1742 // |ss_| is used by |network_thread_| so it must be destroyed later.
deadbeef1dcb1642017-03-29 21:08:16 -07001743 std::unique_ptr<rtc::VirtualSocketServer> ss_;
Steve Antonede9ca52017-10-16 13:04:27 -07001744 std::unique_ptr<rtc::FirewallSocketServer> fss_;
deadbeef1dcb1642017-03-29 21:08:16 -07001745 // |network_thread_| and |worker_thread_| are used by both
1746 // |caller_| and |callee_| so they must be destroyed
1747 // later.
1748 std::unique_ptr<rtc::Thread> network_thread_;
1749 std::unique_ptr<rtc::Thread> worker_thread_;
Seth Hampsonaed71642018-06-11 07:41:32 -07001750 // The turn servers and turn customizers should be accessed & deleted on the
1751 // network thread to avoid a race with the socket read/write that occurs
1752 // on the network thread.
1753 std::vector<std::unique_ptr<cricket::TestTurnServer>> turn_servers_;
1754 std::vector<std::unique_ptr<cricket::TestTurnCustomizer>> turn_customizers_;
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001755 webrtc::MediaTransportPair loopback_media_transports_;
deadbeef1dcb1642017-03-29 21:08:16 -07001756 std::unique_ptr<PeerConnectionWrapper> caller_;
1757 std::unique_ptr<PeerConnectionWrapper> callee_;
1758};
1759
Seth Hampson2f0d7022018-02-20 11:54:42 -08001760class PeerConnectionIntegrationTest
1761 : public PeerConnectionIntegrationBaseTest,
1762 public ::testing::WithParamInterface<SdpSemantics> {
1763 protected:
1764 PeerConnectionIntegrationTest()
1765 : PeerConnectionIntegrationBaseTest(GetParam()) {}
1766};
1767
Yves Gerey100fe632020-01-17 19:15:53 +01001768// Fake clock must be set before threads are started to prevent race on
1769// Set/GetClockForTesting().
1770// To achieve that, multiple inheritance is used as a mixin pattern
1771// where order of construction is finely controlled.
1772// This also ensures peerconnection is closed before switching back to non-fake
1773// clock, avoiding other races and DCHECK failures such as in rtp_sender.cc.
1774class FakeClockForTest : public rtc::ScopedFakeClock {
1775 protected:
1776 FakeClockForTest() {
1777 // Some things use a time of "0" as a special value, so we need to start out
1778 // the fake clock at a nonzero time.
1779 // TODO(deadbeef): Fix this.
Danil Chapovalov0c626af2020-02-10 11:16:00 +01001780 AdvanceTime(webrtc::TimeDelta::Seconds(1));
Yves Gerey100fe632020-01-17 19:15:53 +01001781 }
1782
1783 // Explicit handle.
1784 ScopedFakeClock& FakeClock() { return *this; }
1785};
1786
1787// Ensure FakeClockForTest is constructed first (see class for rationale).
1788class PeerConnectionIntegrationTestWithFakeClock
1789 : public FakeClockForTest,
1790 public PeerConnectionIntegrationTest {};
1791
Seth Hampson2f0d7022018-02-20 11:54:42 -08001792class PeerConnectionIntegrationTestPlanB
1793 : public PeerConnectionIntegrationBaseTest {
1794 protected:
1795 PeerConnectionIntegrationTestPlanB()
1796 : PeerConnectionIntegrationBaseTest(SdpSemantics::kPlanB) {}
1797};
1798
1799class PeerConnectionIntegrationTestUnifiedPlan
1800 : public PeerConnectionIntegrationBaseTest {
1801 protected:
1802 PeerConnectionIntegrationTestUnifiedPlan()
1803 : PeerConnectionIntegrationBaseTest(SdpSemantics::kUnifiedPlan) {}
1804};
1805
deadbeef1dcb1642017-03-29 21:08:16 -07001806// Test the OnFirstPacketReceived callback from audio/video RtpReceivers. This
1807// includes testing that the callback is invoked if an observer is connected
1808// after the first packet has already been received.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001809TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07001810 RtpReceiverObserverOnFirstPacketReceived) {
1811 ASSERT_TRUE(CreatePeerConnectionWrappers());
1812 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001813 caller()->AddAudioVideoTracks();
1814 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001815 // Start offer/answer exchange and wait for it to complete.
1816 caller()->CreateAndSetAndSignalOffer();
1817 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1818 // Should be one receiver each for audio/video.
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02001819 EXPECT_EQ(2U, caller()->rtp_receiver_observers().size());
1820 EXPECT_EQ(2U, callee()->rtp_receiver_observers().size());
deadbeef1dcb1642017-03-29 21:08:16 -07001821 // Wait for all "first packet received" callbacks to be fired.
1822 EXPECT_TRUE_WAIT(
Steve Anton64b626b2019-01-28 17:25:26 -08001823 absl::c_all_of(caller()->rtp_receiver_observers(),
1824 [](const std::unique_ptr<MockRtpReceiverObserver>& o) {
1825 return o->first_packet_received();
1826 }),
deadbeef1dcb1642017-03-29 21:08:16 -07001827 kMaxWaitForFramesMs);
1828 EXPECT_TRUE_WAIT(
Steve Anton64b626b2019-01-28 17:25:26 -08001829 absl::c_all_of(callee()->rtp_receiver_observers(),
1830 [](const std::unique_ptr<MockRtpReceiverObserver>& o) {
1831 return o->first_packet_received();
1832 }),
deadbeef1dcb1642017-03-29 21:08:16 -07001833 kMaxWaitForFramesMs);
1834 // If new observers are set after the first packet was already received, the
1835 // callback should still be invoked.
1836 caller()->ResetRtpReceiverObservers();
1837 callee()->ResetRtpReceiverObservers();
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02001838 EXPECT_EQ(2U, caller()->rtp_receiver_observers().size());
1839 EXPECT_EQ(2U, callee()->rtp_receiver_observers().size());
deadbeef1dcb1642017-03-29 21:08:16 -07001840 EXPECT_TRUE(
Steve Anton64b626b2019-01-28 17:25:26 -08001841 absl::c_all_of(caller()->rtp_receiver_observers(),
1842 [](const std::unique_ptr<MockRtpReceiverObserver>& o) {
1843 return o->first_packet_received();
1844 }));
deadbeef1dcb1642017-03-29 21:08:16 -07001845 EXPECT_TRUE(
Steve Anton64b626b2019-01-28 17:25:26 -08001846 absl::c_all_of(callee()->rtp_receiver_observers(),
1847 [](const std::unique_ptr<MockRtpReceiverObserver>& o) {
1848 return o->first_packet_received();
1849 }));
deadbeef1dcb1642017-03-29 21:08:16 -07001850}
1851
1852class DummyDtmfObserver : public DtmfSenderObserverInterface {
1853 public:
1854 DummyDtmfObserver() : completed_(false) {}
1855
1856 // Implements DtmfSenderObserverInterface.
1857 void OnToneChange(const std::string& tone) override {
1858 tones_.push_back(tone);
1859 if (tone.empty()) {
1860 completed_ = true;
1861 }
1862 }
1863
1864 const std::vector<std::string>& tones() const { return tones_; }
1865 bool completed() const { return completed_; }
1866
1867 private:
1868 bool completed_;
1869 std::vector<std::string> tones_;
1870};
1871
1872// Assumes |sender| already has an audio track added and the offer/answer
1873// exchange is done.
1874void TestDtmfFromSenderToReceiver(PeerConnectionWrapper* sender,
1875 PeerConnectionWrapper* receiver) {
Steve Anton15324772018-01-16 10:26:49 -08001876 // We should be able to get a DTMF sender from the local sender.
1877 rtc::scoped_refptr<DtmfSenderInterface> dtmf_sender =
1878 sender->pc()->GetSenders().at(0)->GetDtmfSender();
1879 ASSERT_TRUE(dtmf_sender);
deadbeef1dcb1642017-03-29 21:08:16 -07001880 DummyDtmfObserver observer;
deadbeef1dcb1642017-03-29 21:08:16 -07001881 dtmf_sender->RegisterObserver(&observer);
1882
1883 // Test the DtmfSender object just created.
1884 EXPECT_TRUE(dtmf_sender->CanInsertDtmf());
1885 EXPECT_TRUE(dtmf_sender->InsertDtmf("1a", 100, 50));
1886
1887 EXPECT_TRUE_WAIT(observer.completed(), kDefaultTimeout);
1888 std::vector<std::string> tones = {"1", "a", ""};
1889 EXPECT_EQ(tones, observer.tones());
1890 dtmf_sender->UnregisterObserver();
1891 // TODO(deadbeef): Verify the tones were actually received end-to-end.
1892}
1893
1894// Verifies the DtmfSenderObserver callbacks for a DtmfSender (one in each
1895// direction).
Seth Hampson2f0d7022018-02-20 11:54:42 -08001896TEST_P(PeerConnectionIntegrationTest, DtmfSenderObserver) {
deadbeef1dcb1642017-03-29 21:08:16 -07001897 ASSERT_TRUE(CreatePeerConnectionWrappers());
1898 ConnectFakeSignaling();
1899 // Only need audio for DTMF.
Steve Anton15324772018-01-16 10:26:49 -08001900 caller()->AddAudioTrack();
1901 callee()->AddAudioTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07001902 caller()->CreateAndSetAndSignalOffer();
1903 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
deadbeef71452802017-05-07 17:21:01 -07001904 // DTLS must finish before the DTMF sender can be used reliably.
1905 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07001906 TestDtmfFromSenderToReceiver(caller(), callee());
1907 TestDtmfFromSenderToReceiver(callee(), caller());
1908}
1909
1910// Basic end-to-end test, verifying media can be encoded/transmitted/decoded
1911// between two connections, using DTLS-SRTP.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001912TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithDtls) {
deadbeef1dcb1642017-03-29 21:08:16 -07001913 ASSERT_TRUE(CreatePeerConnectionWrappers());
1914 ConnectFakeSignaling();
Harald Alvestrand194939b2018-01-24 16:04:13 +01001915
deadbeef1dcb1642017-03-29 21:08:16 -07001916 // Do normal offer/answer and wait for some frames to be received in each
1917 // direction.
Steve Anton15324772018-01-16 10:26:49 -08001918 caller()->AddAudioVideoTracks();
1919 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001920 caller()->CreateAndSetAndSignalOffer();
1921 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001922 MediaExpectations media_expectations;
1923 media_expectations.ExpectBidirectionalAudioAndVideo();
1924 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Ying Wangef3998f2019-12-09 13:06:53 +01001925 EXPECT_METRIC_LE(
1926 2, webrtc::metrics::NumEvents("WebRTC.PeerConnection.KeyProtocol",
1927 webrtc::kEnumCounterKeyProtocolDtls));
1928 EXPECT_METRIC_EQ(
1929 0, webrtc::metrics::NumEvents("WebRTC.PeerConnection.KeyProtocol",
1930 webrtc::kEnumCounterKeyProtocolSdes));
deadbeef1dcb1642017-03-29 21:08:16 -07001931}
1932
1933// Uses SDES instead of DTLS for key agreement.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001934TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithSdes) {
deadbeef1dcb1642017-03-29 21:08:16 -07001935 PeerConnectionInterface::RTCConfiguration sdes_config;
1936 sdes_config.enable_dtls_srtp.emplace(false);
1937 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(sdes_config, sdes_config));
1938 ConnectFakeSignaling();
1939
1940 // Do normal offer/answer and wait for some frames to be received in each
1941 // direction.
Steve Anton15324772018-01-16 10:26:49 -08001942 caller()->AddAudioVideoTracks();
1943 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001944 caller()->CreateAndSetAndSignalOffer();
1945 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001946 MediaExpectations media_expectations;
1947 media_expectations.ExpectBidirectionalAudioAndVideo();
1948 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Ying Wangef3998f2019-12-09 13:06:53 +01001949 EXPECT_METRIC_LE(
1950 2, webrtc::metrics::NumEvents("WebRTC.PeerConnection.KeyProtocol",
1951 webrtc::kEnumCounterKeyProtocolSdes));
1952 EXPECT_METRIC_EQ(
1953 0, webrtc::metrics::NumEvents("WebRTC.PeerConnection.KeyProtocol",
1954 webrtc::kEnumCounterKeyProtocolDtls));
deadbeef1dcb1642017-03-29 21:08:16 -07001955}
1956
Steve Anton9a44b2d2019-07-12 12:58:30 -07001957// Basic end-to-end test specifying the |enable_encrypted_rtp_header_extensions|
1958// option to offer encrypted versions of all header extensions alongside the
1959// unencrypted versions.
1960TEST_P(PeerConnectionIntegrationTest,
1961 EndToEndCallWithEncryptedRtpHeaderExtensions) {
1962 CryptoOptions crypto_options;
1963 crypto_options.srtp.enable_encrypted_rtp_header_extensions = true;
1964 PeerConnectionInterface::RTCConfiguration config;
1965 config.crypto_options = crypto_options;
1966 // Note: This allows offering >14 RTP header extensions.
1967 config.offer_extmap_allow_mixed = true;
1968 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
1969 ConnectFakeSignaling();
1970
1971 // Do normal offer/answer and wait for some frames to be received in each
1972 // direction.
1973 caller()->AddAudioVideoTracks();
1974 callee()->AddAudioVideoTracks();
1975 caller()->CreateAndSetAndSignalOffer();
1976 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1977 MediaExpectations media_expectations;
1978 media_expectations.ExpectBidirectionalAudioAndVideo();
1979 ASSERT_TRUE(ExpectNewFrames(media_expectations));
1980}
1981
Steve Anton8c0f7a72017-10-03 10:03:10 -07001982// Tests that the GetRemoteAudioSSLCertificate method returns the remote DTLS
1983// certificate once the DTLS handshake has finished.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001984TEST_P(PeerConnectionIntegrationTest,
Steve Anton8c0f7a72017-10-03 10:03:10 -07001985 GetRemoteAudioSSLCertificateReturnsExchangedCertificate) {
1986 auto GetRemoteAudioSSLCertificate = [](PeerConnectionWrapper* wrapper) {
1987 auto pci = reinterpret_cast<PeerConnectionProxy*>(wrapper->pc());
1988 auto pc = reinterpret_cast<PeerConnection*>(pci->internal());
1989 return pc->GetRemoteAudioSSLCertificate();
1990 };
Zhi Huang70b820f2018-01-27 14:16:15 -08001991 auto GetRemoteAudioSSLCertChain = [](PeerConnectionWrapper* wrapper) {
1992 auto pci = reinterpret_cast<PeerConnectionProxy*>(wrapper->pc());
1993 auto pc = reinterpret_cast<PeerConnection*>(pci->internal());
1994 return pc->GetRemoteAudioSSLCertChain();
1995 };
Steve Anton8c0f7a72017-10-03 10:03:10 -07001996
1997 auto caller_cert = rtc::RTCCertificate::FromPEM(kRsaPems[0]);
1998 auto callee_cert = rtc::RTCCertificate::FromPEM(kRsaPems[1]);
1999
2000 // Configure each side with a known certificate so they can be compared later.
2001 PeerConnectionInterface::RTCConfiguration caller_config;
2002 caller_config.enable_dtls_srtp.emplace(true);
2003 caller_config.certificates.push_back(caller_cert);
2004 PeerConnectionInterface::RTCConfiguration callee_config;
2005 callee_config.enable_dtls_srtp.emplace(true);
2006 callee_config.certificates.push_back(callee_cert);
2007 ASSERT_TRUE(
2008 CreatePeerConnectionWrappersWithConfig(caller_config, callee_config));
2009 ConnectFakeSignaling();
2010
2011 // When first initialized, there should not be a remote SSL certificate (and
2012 // calling this method should not crash).
2013 EXPECT_EQ(nullptr, GetRemoteAudioSSLCertificate(caller()));
2014 EXPECT_EQ(nullptr, GetRemoteAudioSSLCertificate(callee()));
Zhi Huang70b820f2018-01-27 14:16:15 -08002015 EXPECT_EQ(nullptr, GetRemoteAudioSSLCertChain(caller()));
2016 EXPECT_EQ(nullptr, GetRemoteAudioSSLCertChain(callee()));
Steve Anton8c0f7a72017-10-03 10:03:10 -07002017
Steve Anton15324772018-01-16 10:26:49 -08002018 caller()->AddAudioTrack();
2019 callee()->AddAudioTrack();
Steve Anton8c0f7a72017-10-03 10:03:10 -07002020 caller()->CreateAndSetAndSignalOffer();
2021 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2022 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
2023
2024 // Once DTLS has been connected, each side should return the other's SSL
2025 // certificate when calling GetRemoteAudioSSLCertificate.
2026
2027 auto caller_remote_cert = GetRemoteAudioSSLCertificate(caller());
2028 ASSERT_TRUE(caller_remote_cert);
Benjamin Wright6c6c9df2018-10-25 01:16:26 -07002029 EXPECT_EQ(callee_cert->GetSSLCertificate().ToPEMString(),
Steve Anton8c0f7a72017-10-03 10:03:10 -07002030 caller_remote_cert->ToPEMString());
2031
2032 auto callee_remote_cert = GetRemoteAudioSSLCertificate(callee());
2033 ASSERT_TRUE(callee_remote_cert);
Benjamin Wright6c6c9df2018-10-25 01:16:26 -07002034 EXPECT_EQ(caller_cert->GetSSLCertificate().ToPEMString(),
Steve Anton8c0f7a72017-10-03 10:03:10 -07002035 callee_remote_cert->ToPEMString());
Zhi Huang70b820f2018-01-27 14:16:15 -08002036
2037 auto caller_remote_cert_chain = GetRemoteAudioSSLCertChain(caller());
2038 ASSERT_TRUE(caller_remote_cert_chain);
2039 ASSERT_EQ(1U, caller_remote_cert_chain->GetSize());
2040 auto remote_cert = &caller_remote_cert_chain->Get(0);
Benjamin Wright6c6c9df2018-10-25 01:16:26 -07002041 EXPECT_EQ(callee_cert->GetSSLCertificate().ToPEMString(),
Zhi Huang70b820f2018-01-27 14:16:15 -08002042 remote_cert->ToPEMString());
2043
2044 auto callee_remote_cert_chain = GetRemoteAudioSSLCertChain(callee());
2045 ASSERT_TRUE(callee_remote_cert_chain);
2046 ASSERT_EQ(1U, callee_remote_cert_chain->GetSize());
2047 remote_cert = &callee_remote_cert_chain->Get(0);
Benjamin Wright6c6c9df2018-10-25 01:16:26 -07002048 EXPECT_EQ(caller_cert->GetSSLCertificate().ToPEMString(),
Zhi Huang70b820f2018-01-27 14:16:15 -08002049 remote_cert->ToPEMString());
Steve Anton8c0f7a72017-10-03 10:03:10 -07002050}
2051
deadbeef1dcb1642017-03-29 21:08:16 -07002052// This test sets up a call between two parties with a source resolution of
2053// 1280x720 and verifies that a 16:9 aspect ratio is received.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002054TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07002055 Send1280By720ResolutionAndReceive16To9AspectRatio) {
2056 ASSERT_TRUE(CreatePeerConnectionWrappers());
2057 ConnectFakeSignaling();
2058
Niels Möller5c7efe72018-05-11 10:34:46 +02002059 // Add video tracks with 16:9 aspect ratio, size 1280 x 720.
2060 webrtc::FakePeriodicVideoSource::Config config;
2061 config.width = 1280;
2062 config.height = 720;
Johannes Kron965e7942018-09-13 15:36:20 +02002063 config.timestamp_offset_ms = rtc::TimeMillis();
Niels Möller5c7efe72018-05-11 10:34:46 +02002064 caller()->AddTrack(caller()->CreateLocalVideoTrackWithConfig(config));
2065 callee()->AddTrack(callee()->CreateLocalVideoTrackWithConfig(config));
deadbeef1dcb1642017-03-29 21:08:16 -07002066
2067 // Do normal offer/answer and wait for at least one frame to be received in
2068 // each direction.
2069 caller()->CreateAndSetAndSignalOffer();
2070 ASSERT_TRUE_WAIT(caller()->min_video_frames_received_per_track() > 0 &&
2071 callee()->min_video_frames_received_per_track() > 0,
2072 kMaxWaitForFramesMs);
2073
2074 // Check rendered aspect ratio.
2075 EXPECT_EQ(16.0 / 9, caller()->local_rendered_aspect_ratio());
2076 EXPECT_EQ(16.0 / 9, caller()->rendered_aspect_ratio());
2077 EXPECT_EQ(16.0 / 9, callee()->local_rendered_aspect_ratio());
2078 EXPECT_EQ(16.0 / 9, callee()->rendered_aspect_ratio());
2079}
2080
2081// This test sets up an one-way call, with media only from caller to
2082// callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002083TEST_P(PeerConnectionIntegrationTest, OneWayMediaCall) {
deadbeef1dcb1642017-03-29 21:08:16 -07002084 ASSERT_TRUE(CreatePeerConnectionWrappers());
2085 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002086 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002087 caller()->CreateAndSetAndSignalOffer();
Seth Hampson2f0d7022018-02-20 11:54:42 -08002088 MediaExpectations media_expectations;
2089 media_expectations.CalleeExpectsSomeAudioAndVideo();
2090 media_expectations.CallerExpectsNoAudio();
2091 media_expectations.CallerExpectsNoVideo();
2092 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002093}
2094
Johannes Kron3e983682020-03-29 22:17:00 +02002095// Tests that send only works without the caller having a decoder factory and
2096// the callee having an encoder factory.
2097TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithSendOnlyVideo) {
2098 ASSERT_TRUE(
2099 CreateOneDirectionalPeerConnectionWrappers(/*caller_to_callee=*/true));
2100 ConnectFakeSignaling();
2101 // Add one-directional video, from caller to callee.
2102 rtc::scoped_refptr<webrtc::VideoTrackInterface> caller_track =
2103 caller()->CreateLocalVideoTrack();
2104 caller()->AddTrack(caller_track);
2105 PeerConnectionInterface::RTCOfferAnswerOptions options;
2106 options.offer_to_receive_video = 0;
2107 caller()->SetOfferAnswerOptions(options);
2108 caller()->CreateAndSetAndSignalOffer();
2109 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2110 ASSERT_EQ(callee()->pc()->GetReceivers().size(), 1u);
2111
2112 // Expect video to be received in one direction.
2113 MediaExpectations media_expectations;
2114 media_expectations.CallerExpectsNoVideo();
2115 media_expectations.CalleeExpectsSomeVideo();
2116
2117 EXPECT_TRUE(ExpectNewFrames(media_expectations));
2118}
2119
2120// Tests that receive only works without the caller having an encoder factory
2121// and the callee having a decoder factory.
2122TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithReceiveOnlyVideo) {
2123 ASSERT_TRUE(
2124 CreateOneDirectionalPeerConnectionWrappers(/*caller_to_callee=*/false));
2125 ConnectFakeSignaling();
2126 // Add one-directional video, from callee to caller.
2127 rtc::scoped_refptr<webrtc::VideoTrackInterface> callee_track =
2128 callee()->CreateLocalVideoTrack();
2129 callee()->AddTrack(callee_track);
2130 PeerConnectionInterface::RTCOfferAnswerOptions options;
2131 options.offer_to_receive_video = 1;
2132 caller()->SetOfferAnswerOptions(options);
2133 caller()->CreateAndSetAndSignalOffer();
2134 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2135 ASSERT_EQ(caller()->pc()->GetReceivers().size(), 1u);
2136
2137 // Expect video to be received in one direction.
2138 MediaExpectations media_expectations;
2139 media_expectations.CallerExpectsSomeVideo();
2140 media_expectations.CalleeExpectsNoVideo();
2141
2142 EXPECT_TRUE(ExpectNewFrames(media_expectations));
2143}
2144
2145TEST_P(PeerConnectionIntegrationTest,
2146 EndToEndCallAddReceiveVideoToSendOnlyCall) {
2147 ASSERT_TRUE(CreatePeerConnectionWrappers());
2148 ConnectFakeSignaling();
2149 // Add one-directional video, from caller to callee.
2150 rtc::scoped_refptr<webrtc::VideoTrackInterface> caller_track =
2151 caller()->CreateLocalVideoTrack();
2152 caller()->AddTrack(caller_track);
2153 caller()->CreateAndSetAndSignalOffer();
2154 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2155
2156 // Add receive video.
2157 rtc::scoped_refptr<webrtc::VideoTrackInterface> callee_track =
2158 callee()->CreateLocalVideoTrack();
2159 callee()->AddTrack(callee_track);
2160 caller()->CreateAndSetAndSignalOffer();
2161 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2162
2163 // Ensure that video frames are received end-to-end.
2164 MediaExpectations media_expectations;
2165 media_expectations.ExpectBidirectionalVideo();
2166 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2167}
2168
2169TEST_P(PeerConnectionIntegrationTest,
2170 EndToEndCallAddSendVideoToReceiveOnlyCall) {
2171 ASSERT_TRUE(CreatePeerConnectionWrappers());
2172 ConnectFakeSignaling();
2173 // Add one-directional video, from callee to caller.
2174 rtc::scoped_refptr<webrtc::VideoTrackInterface> callee_track =
2175 callee()->CreateLocalVideoTrack();
2176 callee()->AddTrack(callee_track);
2177 caller()->CreateAndSetAndSignalOffer();
2178 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2179
2180 // Add send video.
2181 rtc::scoped_refptr<webrtc::VideoTrackInterface> caller_track =
2182 caller()->CreateLocalVideoTrack();
2183 caller()->AddTrack(caller_track);
2184 caller()->CreateAndSetAndSignalOffer();
2185 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2186
2187 // Expect video to be received in one direction.
2188 MediaExpectations media_expectations;
2189 media_expectations.ExpectBidirectionalVideo();
2190 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2191}
2192
2193TEST_P(PeerConnectionIntegrationTest,
2194 EndToEndCallRemoveReceiveVideoFromSendReceiveCall) {
2195 ASSERT_TRUE(CreatePeerConnectionWrappers());
2196 ConnectFakeSignaling();
2197 // Add send video, from caller to callee.
2198 rtc::scoped_refptr<webrtc::VideoTrackInterface> caller_track =
2199 caller()->CreateLocalVideoTrack();
2200 rtc::scoped_refptr<webrtc::RtpSenderInterface> caller_sender =
2201 caller()->AddTrack(caller_track);
2202 // Add receive video, from callee to caller.
2203 rtc::scoped_refptr<webrtc::VideoTrackInterface> callee_track =
2204 callee()->CreateLocalVideoTrack();
2205
2206 rtc::scoped_refptr<webrtc::RtpSenderInterface> callee_sender =
2207 callee()->AddTrack(callee_track);
2208 caller()->CreateAndSetAndSignalOffer();
2209 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2210
2211 // Remove receive video (i.e., callee sender track).
2212 callee()->pc()->RemoveTrack(callee_sender);
2213
2214 caller()->CreateAndSetAndSignalOffer();
2215 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2216
2217 // Expect one-directional video.
2218 MediaExpectations media_expectations;
2219 media_expectations.CallerExpectsNoVideo();
2220 media_expectations.CalleeExpectsSomeVideo();
2221
2222 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2223}
2224
2225TEST_P(PeerConnectionIntegrationTest,
2226 EndToEndCallRemoveSendVideoFromSendReceiveCall) {
2227 ASSERT_TRUE(CreatePeerConnectionWrappers());
2228 ConnectFakeSignaling();
2229 // Add send video, from caller to callee.
2230 rtc::scoped_refptr<webrtc::VideoTrackInterface> caller_track =
2231 caller()->CreateLocalVideoTrack();
2232 rtc::scoped_refptr<webrtc::RtpSenderInterface> caller_sender =
2233 caller()->AddTrack(caller_track);
2234 // Add receive video, from callee to caller.
2235 rtc::scoped_refptr<webrtc::VideoTrackInterface> callee_track =
2236 callee()->CreateLocalVideoTrack();
2237
2238 rtc::scoped_refptr<webrtc::RtpSenderInterface> callee_sender =
2239 callee()->AddTrack(callee_track);
2240 caller()->CreateAndSetAndSignalOffer();
2241 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2242
2243 // Remove send video (i.e., caller sender track).
2244 caller()->pc()->RemoveTrack(caller_sender);
2245
2246 caller()->CreateAndSetAndSignalOffer();
2247 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2248
2249 // Expect one-directional video.
2250 MediaExpectations media_expectations;
2251 media_expectations.CalleeExpectsNoVideo();
2252 media_expectations.CallerExpectsSomeVideo();
2253
2254 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2255}
2256
deadbeef1dcb1642017-03-29 21:08:16 -07002257// This test sets up a audio call initially, with the callee rejecting video
2258// initially. Then later the callee decides to upgrade to audio/video, and
2259// initiates a new offer/answer exchange.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002260TEST_P(PeerConnectionIntegrationTest, AudioToVideoUpgrade) {
deadbeef1dcb1642017-03-29 21:08:16 -07002261 ASSERT_TRUE(CreatePeerConnectionWrappers());
2262 ConnectFakeSignaling();
2263 // Initially, offer an audio/video stream from the caller, but refuse to
2264 // send/receive video on the callee side.
Steve Anton15324772018-01-16 10:26:49 -08002265 caller()->AddAudioVideoTracks();
2266 callee()->AddAudioTrack();
Seth Hampson2f0d7022018-02-20 11:54:42 -08002267 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2268 PeerConnectionInterface::RTCOfferAnswerOptions options;
2269 options.offer_to_receive_video = 0;
2270 callee()->SetOfferAnswerOptions(options);
2271 } else {
2272 callee()->SetRemoteOfferHandler([this] {
2273 callee()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)->Stop();
2274 });
2275 }
deadbeef1dcb1642017-03-29 21:08:16 -07002276 // Do offer/answer and make sure audio is still received end-to-end.
2277 caller()->CreateAndSetAndSignalOffer();
2278 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002279 {
2280 MediaExpectations media_expectations;
2281 media_expectations.ExpectBidirectionalAudio();
2282 media_expectations.ExpectNoVideo();
2283 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2284 }
deadbeef1dcb1642017-03-29 21:08:16 -07002285 // Sanity check that the callee's description has a rejected video section.
2286 ASSERT_NE(nullptr, callee()->pc()->local_description());
2287 const ContentInfo* callee_video_content =
2288 GetFirstVideoContent(callee()->pc()->local_description()->description());
2289 ASSERT_NE(nullptr, callee_video_content);
2290 EXPECT_TRUE(callee_video_content->rejected);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002291
deadbeef1dcb1642017-03-29 21:08:16 -07002292 // Now negotiate with video and ensure negotiation succeeds, with video
2293 // frames and additional audio frames being received.
Steve Anton15324772018-01-16 10:26:49 -08002294 callee()->AddVideoTrack();
Seth Hampson2f0d7022018-02-20 11:54:42 -08002295 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2296 PeerConnectionInterface::RTCOfferAnswerOptions options;
2297 options.offer_to_receive_video = 1;
2298 callee()->SetOfferAnswerOptions(options);
2299 } else {
2300 callee()->SetRemoteOfferHandler(nullptr);
2301 caller()->SetRemoteOfferHandler([this] {
2302 // The caller creates a new transceiver to receive video on when receiving
2303 // the offer, but by default it is send only.
2304 auto transceivers = caller()->pc()->GetTransceivers();
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02002305 ASSERT_EQ(3U, transceivers.size());
Seth Hampson2f0d7022018-02-20 11:54:42 -08002306 ASSERT_EQ(cricket::MEDIA_TYPE_VIDEO,
2307 transceivers[2]->receiver()->media_type());
2308 transceivers[2]->sender()->SetTrack(caller()->CreateLocalVideoTrack());
2309 transceivers[2]->SetDirection(RtpTransceiverDirection::kSendRecv);
2310 });
2311 }
deadbeef1dcb1642017-03-29 21:08:16 -07002312 callee()->CreateAndSetAndSignalOffer();
2313 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002314 {
2315 // Expect additional audio frames to be received after the upgrade.
2316 MediaExpectations media_expectations;
2317 media_expectations.ExpectBidirectionalAudioAndVideo();
2318 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2319 }
deadbeef1dcb1642017-03-29 21:08:16 -07002320}
2321
deadbeef4389b4d2017-09-07 09:07:36 -07002322// Simpler than the above test; just add an audio track to an established
2323// video-only connection.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002324TEST_P(PeerConnectionIntegrationTest, AddAudioToVideoOnlyCall) {
deadbeef4389b4d2017-09-07 09:07:36 -07002325 ASSERT_TRUE(CreatePeerConnectionWrappers());
2326 ConnectFakeSignaling();
2327 // Do initial offer/answer with just a video track.
Steve Anton15324772018-01-16 10:26:49 -08002328 caller()->AddVideoTrack();
2329 callee()->AddVideoTrack();
deadbeef4389b4d2017-09-07 09:07:36 -07002330 caller()->CreateAndSetAndSignalOffer();
2331 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2332 // Now add an audio track and do another offer/answer.
Steve Anton15324772018-01-16 10:26:49 -08002333 caller()->AddAudioTrack();
2334 callee()->AddAudioTrack();
deadbeef4389b4d2017-09-07 09:07:36 -07002335 caller()->CreateAndSetAndSignalOffer();
2336 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2337 // Ensure both audio and video frames are received end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002338 MediaExpectations media_expectations;
2339 media_expectations.ExpectBidirectionalAudioAndVideo();
2340 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef4389b4d2017-09-07 09:07:36 -07002341}
2342
deadbeef1dcb1642017-03-29 21:08:16 -07002343// This test sets up a call that's transferred to a new caller with a different
2344// DTLS fingerprint.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002345TEST_P(PeerConnectionIntegrationTest, CallTransferredForCallee) {
deadbeef1dcb1642017-03-29 21:08:16 -07002346 ASSERT_TRUE(CreatePeerConnectionWrappers());
2347 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002348 caller()->AddAudioVideoTracks();
2349 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002350 caller()->CreateAndSetAndSignalOffer();
2351 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2352
2353 // Keep the original peer around which will still send packets to the
2354 // receiving client. These SRTP packets will be dropped.
2355 std::unique_ptr<PeerConnectionWrapper> original_peer(
2356 SetCallerPcWrapperAndReturnCurrent(
Seth Hampson2f0d7022018-02-20 11:54:42 -08002357 CreatePeerConnectionWrapperWithAlternateKey().release()));
deadbeef1dcb1642017-03-29 21:08:16 -07002358 // TODO(deadbeef): Why do we call Close here? That goes against the comment
2359 // directly above.
2360 original_peer->pc()->Close();
2361
2362 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002363 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002364 caller()->CreateAndSetAndSignalOffer();
2365 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2366 // Wait for some additional frames to be transmitted end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002367 MediaExpectations media_expectations;
2368 media_expectations.ExpectBidirectionalAudioAndVideo();
2369 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002370}
2371
2372// This test sets up a call that's transferred to a new callee with a different
2373// DTLS fingerprint.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002374TEST_P(PeerConnectionIntegrationTest, CallTransferredForCaller) {
deadbeef1dcb1642017-03-29 21:08:16 -07002375 ASSERT_TRUE(CreatePeerConnectionWrappers());
2376 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002377 caller()->AddAudioVideoTracks();
2378 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002379 caller()->CreateAndSetAndSignalOffer();
2380 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2381
2382 // Keep the original peer around which will still send packets to the
2383 // receiving client. These SRTP packets will be dropped.
2384 std::unique_ptr<PeerConnectionWrapper> original_peer(
2385 SetCalleePcWrapperAndReturnCurrent(
Seth Hampson2f0d7022018-02-20 11:54:42 -08002386 CreatePeerConnectionWrapperWithAlternateKey().release()));
deadbeef1dcb1642017-03-29 21:08:16 -07002387 // TODO(deadbeef): Why do we call Close here? That goes against the comment
2388 // directly above.
2389 original_peer->pc()->Close();
2390
2391 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002392 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002393 caller()->SetOfferAnswerOptions(IceRestartOfferAnswerOptions());
2394 caller()->CreateAndSetAndSignalOffer();
2395 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2396 // Wait for some additional frames to be transmitted end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002397 MediaExpectations media_expectations;
2398 media_expectations.ExpectBidirectionalAudioAndVideo();
2399 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002400}
2401
2402// This test sets up a non-bundled call and negotiates bundling at the same
2403// time as starting an ICE restart. When bundling is in effect in the restart,
2404// the DTLS-SRTP context should be successfully reset.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002405TEST_P(PeerConnectionIntegrationTest, BundlingEnabledWhileIceRestartOccurs) {
deadbeef1dcb1642017-03-29 21:08:16 -07002406 ASSERT_TRUE(CreatePeerConnectionWrappers());
2407 ConnectFakeSignaling();
2408
Steve Anton15324772018-01-16 10:26:49 -08002409 caller()->AddAudioVideoTracks();
2410 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002411 // Remove the bundle group from the SDP received by the callee.
2412 callee()->SetReceivedSdpMunger([](cricket::SessionDescription* desc) {
2413 desc->RemoveGroupByName("BUNDLE");
2414 });
2415 caller()->CreateAndSetAndSignalOffer();
2416 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002417 {
2418 MediaExpectations media_expectations;
2419 media_expectations.ExpectBidirectionalAudioAndVideo();
2420 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2421 }
deadbeef1dcb1642017-03-29 21:08:16 -07002422 // Now stop removing the BUNDLE group, and trigger an ICE restart.
2423 callee()->SetReceivedSdpMunger(nullptr);
2424 caller()->SetOfferAnswerOptions(IceRestartOfferAnswerOptions());
2425 caller()->CreateAndSetAndSignalOffer();
2426 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2427
2428 // Expect additional frames to be received after the ICE restart.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002429 {
2430 MediaExpectations media_expectations;
2431 media_expectations.ExpectBidirectionalAudioAndVideo();
2432 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2433 }
deadbeef1dcb1642017-03-29 21:08:16 -07002434}
2435
2436// Test CVO (Coordination of Video Orientation). If a video source is rotated
2437// and both peers support the CVO RTP header extension, the actual video frames
2438// don't need to be encoded in different resolutions, since the rotation is
2439// communicated through the RTP header extension.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002440TEST_P(PeerConnectionIntegrationTest, RotatedVideoWithCVOExtension) {
deadbeef1dcb1642017-03-29 21:08:16 -07002441 ASSERT_TRUE(CreatePeerConnectionWrappers());
2442 ConnectFakeSignaling();
2443 // Add rotated video tracks.
Steve Anton15324772018-01-16 10:26:49 -08002444 caller()->AddTrack(
deadbeef1dcb1642017-03-29 21:08:16 -07002445 caller()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_90));
Steve Anton15324772018-01-16 10:26:49 -08002446 callee()->AddTrack(
deadbeef1dcb1642017-03-29 21:08:16 -07002447 callee()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_270));
2448
2449 // Wait for video frames to be received by both sides.
2450 caller()->CreateAndSetAndSignalOffer();
2451 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2452 ASSERT_TRUE_WAIT(caller()->min_video_frames_received_per_track() > 0 &&
2453 callee()->min_video_frames_received_per_track() > 0,
2454 kMaxWaitForFramesMs);
2455
2456 // Ensure that the aspect ratio is unmodified.
2457 // TODO(deadbeef): Where does 4:3 come from? Should be explicit in the test,
2458 // not just assumed.
2459 EXPECT_EQ(4.0 / 3, caller()->local_rendered_aspect_ratio());
2460 EXPECT_EQ(4.0 / 3, caller()->rendered_aspect_ratio());
2461 EXPECT_EQ(4.0 / 3, callee()->local_rendered_aspect_ratio());
2462 EXPECT_EQ(4.0 / 3, callee()->rendered_aspect_ratio());
2463 // Ensure that the CVO bits were surfaced to the renderer.
2464 EXPECT_EQ(webrtc::kVideoRotation_270, caller()->rendered_rotation());
2465 EXPECT_EQ(webrtc::kVideoRotation_90, callee()->rendered_rotation());
2466}
2467
2468// Test that when the CVO extension isn't supported, video is rotated the
2469// old-fashioned way, by encoding rotated frames.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002470TEST_P(PeerConnectionIntegrationTest, RotatedVideoWithoutCVOExtension) {
deadbeef1dcb1642017-03-29 21:08:16 -07002471 ASSERT_TRUE(CreatePeerConnectionWrappers());
2472 ConnectFakeSignaling();
2473 // Add rotated video tracks.
Steve Anton15324772018-01-16 10:26:49 -08002474 caller()->AddTrack(
deadbeef1dcb1642017-03-29 21:08:16 -07002475 caller()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_90));
Steve Anton15324772018-01-16 10:26:49 -08002476 callee()->AddTrack(
deadbeef1dcb1642017-03-29 21:08:16 -07002477 callee()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_270));
2478
2479 // Remove the CVO extension from the offered SDP.
2480 callee()->SetReceivedSdpMunger([](cricket::SessionDescription* desc) {
2481 cricket::VideoContentDescription* video =
2482 GetFirstVideoContentDescription(desc);
2483 video->ClearRtpHeaderExtensions();
2484 });
2485 // Wait for video frames to be received by both sides.
2486 caller()->CreateAndSetAndSignalOffer();
2487 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2488 ASSERT_TRUE_WAIT(caller()->min_video_frames_received_per_track() > 0 &&
2489 callee()->min_video_frames_received_per_track() > 0,
2490 kMaxWaitForFramesMs);
2491
2492 // Expect that the aspect ratio is inversed to account for the 90/270 degree
2493 // rotation.
2494 // TODO(deadbeef): Where does 4:3 come from? Should be explicit in the test,
2495 // not just assumed.
2496 EXPECT_EQ(3.0 / 4, caller()->local_rendered_aspect_ratio());
2497 EXPECT_EQ(3.0 / 4, caller()->rendered_aspect_ratio());
2498 EXPECT_EQ(3.0 / 4, callee()->local_rendered_aspect_ratio());
2499 EXPECT_EQ(3.0 / 4, callee()->rendered_aspect_ratio());
2500 // Expect that each endpoint is unaware of the rotation of the other endpoint.
2501 EXPECT_EQ(webrtc::kVideoRotation_0, caller()->rendered_rotation());
2502 EXPECT_EQ(webrtc::kVideoRotation_0, callee()->rendered_rotation());
2503}
2504
deadbeef1dcb1642017-03-29 21:08:16 -07002505// Test that if the answerer rejects the audio m= section, no audio is sent or
2506// received, but video still can be.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002507TEST_P(PeerConnectionIntegrationTest, AnswererRejectsAudioSection) {
deadbeef1dcb1642017-03-29 21:08:16 -07002508 ASSERT_TRUE(CreatePeerConnectionWrappers());
2509 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002510 caller()->AddAudioVideoTracks();
Seth Hampson2f0d7022018-02-20 11:54:42 -08002511 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2512 // Only add video track for callee, and set offer_to_receive_audio to 0, so
2513 // it will reject the audio m= section completely.
2514 PeerConnectionInterface::RTCOfferAnswerOptions options;
2515 options.offer_to_receive_audio = 0;
2516 callee()->SetOfferAnswerOptions(options);
2517 } else {
2518 // Stopping the audio RtpTransceiver will cause the media section to be
2519 // rejected in the answer.
2520 callee()->SetRemoteOfferHandler([this] {
2521 callee()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_AUDIO)->Stop();
2522 });
2523 }
Steve Anton15324772018-01-16 10:26:49 -08002524 callee()->AddTrack(callee()->CreateLocalVideoTrack());
deadbeef1dcb1642017-03-29 21:08:16 -07002525 // Do offer/answer and wait for successful end-to-end video frames.
2526 caller()->CreateAndSetAndSignalOffer();
2527 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002528 MediaExpectations media_expectations;
2529 media_expectations.ExpectBidirectionalVideo();
2530 media_expectations.ExpectNoAudio();
2531 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2532
deadbeef1dcb1642017-03-29 21:08:16 -07002533 // Sanity check that the callee's description has a rejected audio section.
2534 ASSERT_NE(nullptr, callee()->pc()->local_description());
2535 const ContentInfo* callee_audio_content =
2536 GetFirstAudioContent(callee()->pc()->local_description()->description());
2537 ASSERT_NE(nullptr, callee_audio_content);
2538 EXPECT_TRUE(callee_audio_content->rejected);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002539 if (sdp_semantics_ == SdpSemantics::kUnifiedPlan) {
2540 // The caller's transceiver should have stopped after receiving the answer.
2541 EXPECT_TRUE(caller()
2542 ->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_AUDIO)
2543 ->stopped());
2544 }
deadbeef1dcb1642017-03-29 21:08:16 -07002545}
2546
2547// Test that if the answerer rejects the video m= section, no video is sent or
2548// received, but audio still can be.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002549TEST_P(PeerConnectionIntegrationTest, AnswererRejectsVideoSection) {
deadbeef1dcb1642017-03-29 21:08:16 -07002550 ASSERT_TRUE(CreatePeerConnectionWrappers());
2551 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002552 caller()->AddAudioVideoTracks();
Seth Hampson2f0d7022018-02-20 11:54:42 -08002553 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2554 // Only add audio track for callee, and set offer_to_receive_video to 0, so
2555 // it will reject the video m= section completely.
2556 PeerConnectionInterface::RTCOfferAnswerOptions options;
2557 options.offer_to_receive_video = 0;
2558 callee()->SetOfferAnswerOptions(options);
2559 } else {
2560 // Stopping the video RtpTransceiver will cause the media section to be
2561 // rejected in the answer.
2562 callee()->SetRemoteOfferHandler([this] {
2563 callee()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)->Stop();
2564 });
2565 }
Steve Anton15324772018-01-16 10:26:49 -08002566 callee()->AddTrack(callee()->CreateLocalAudioTrack());
deadbeef1dcb1642017-03-29 21:08:16 -07002567 // Do offer/answer and wait for successful end-to-end audio frames.
2568 caller()->CreateAndSetAndSignalOffer();
2569 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002570 MediaExpectations media_expectations;
2571 media_expectations.ExpectBidirectionalAudio();
2572 media_expectations.ExpectNoVideo();
2573 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2574
deadbeef1dcb1642017-03-29 21:08:16 -07002575 // Sanity check that the callee's description has a rejected video section.
2576 ASSERT_NE(nullptr, callee()->pc()->local_description());
2577 const ContentInfo* callee_video_content =
2578 GetFirstVideoContent(callee()->pc()->local_description()->description());
2579 ASSERT_NE(nullptr, callee_video_content);
2580 EXPECT_TRUE(callee_video_content->rejected);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002581 if (sdp_semantics_ == SdpSemantics::kUnifiedPlan) {
2582 // The caller's transceiver should have stopped after receiving the answer.
2583 EXPECT_TRUE(caller()
2584 ->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)
2585 ->stopped());
2586 }
deadbeef1dcb1642017-03-29 21:08:16 -07002587}
2588
2589// Test that if the answerer rejects both audio and video m= sections, nothing
2590// bad happens.
2591// TODO(deadbeef): Test that a data channel still works. Currently this doesn't
2592// test anything but the fact that negotiation succeeds, which doesn't mean
2593// much.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002594TEST_P(PeerConnectionIntegrationTest, AnswererRejectsAudioAndVideoSections) {
deadbeef1dcb1642017-03-29 21:08:16 -07002595 ASSERT_TRUE(CreatePeerConnectionWrappers());
2596 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002597 caller()->AddAudioVideoTracks();
Seth Hampson2f0d7022018-02-20 11:54:42 -08002598 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2599 // Don't give the callee any tracks, and set offer_to_receive_X to 0, so it
2600 // will reject both audio and video m= sections.
2601 PeerConnectionInterface::RTCOfferAnswerOptions options;
2602 options.offer_to_receive_audio = 0;
2603 options.offer_to_receive_video = 0;
2604 callee()->SetOfferAnswerOptions(options);
2605 } else {
2606 callee()->SetRemoteOfferHandler([this] {
2607 // Stopping all transceivers will cause all media sections to be rejected.
Mirko Bonadei739baf02019-01-27 17:29:42 +01002608 for (const auto& transceiver : callee()->pc()->GetTransceivers()) {
Seth Hampson2f0d7022018-02-20 11:54:42 -08002609 transceiver->Stop();
2610 }
2611 });
2612 }
deadbeef1dcb1642017-03-29 21:08:16 -07002613 // Do offer/answer and wait for stable signaling state.
2614 caller()->CreateAndSetAndSignalOffer();
2615 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002616
deadbeef1dcb1642017-03-29 21:08:16 -07002617 // Sanity check that the callee's description has rejected m= sections.
2618 ASSERT_NE(nullptr, callee()->pc()->local_description());
2619 const ContentInfo* callee_audio_content =
2620 GetFirstAudioContent(callee()->pc()->local_description()->description());
2621 ASSERT_NE(nullptr, callee_audio_content);
2622 EXPECT_TRUE(callee_audio_content->rejected);
2623 const ContentInfo* callee_video_content =
2624 GetFirstVideoContent(callee()->pc()->local_description()->description());
2625 ASSERT_NE(nullptr, callee_video_content);
2626 EXPECT_TRUE(callee_video_content->rejected);
2627}
2628
2629// This test sets up an audio and video call between two parties. After the
2630// call runs for a while, the caller sends an updated offer with video being
2631// rejected. Once the re-negotiation is done, the video flow should stop and
2632// the audio flow should continue.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002633TEST_P(PeerConnectionIntegrationTest, VideoRejectedInSubsequentOffer) {
deadbeef1dcb1642017-03-29 21:08:16 -07002634 ASSERT_TRUE(CreatePeerConnectionWrappers());
2635 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002636 caller()->AddAudioVideoTracks();
2637 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002638 caller()->CreateAndSetAndSignalOffer();
2639 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002640 {
2641 MediaExpectations media_expectations;
2642 media_expectations.ExpectBidirectionalAudioAndVideo();
2643 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2644 }
deadbeef1dcb1642017-03-29 21:08:16 -07002645 // Renegotiate, rejecting the video m= section.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002646 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2647 caller()->SetGeneratedSdpMunger(
2648 [](cricket::SessionDescription* description) {
2649 for (cricket::ContentInfo& content : description->contents()) {
2650 if (cricket::IsVideoContent(&content)) {
2651 content.rejected = true;
2652 }
2653 }
2654 });
2655 } else {
2656 caller()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)->Stop();
2657 }
deadbeef1dcb1642017-03-29 21:08:16 -07002658 caller()->CreateAndSetAndSignalOffer();
2659 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
2660
2661 // Sanity check that the caller's description has a rejected video section.
2662 ASSERT_NE(nullptr, caller()->pc()->local_description());
2663 const ContentInfo* caller_video_content =
2664 GetFirstVideoContent(caller()->pc()->local_description()->description());
2665 ASSERT_NE(nullptr, caller_video_content);
2666 EXPECT_TRUE(caller_video_content->rejected);
deadbeef1dcb1642017-03-29 21:08:16 -07002667 // Wait for some additional audio frames to be received.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002668 {
2669 MediaExpectations media_expectations;
2670 media_expectations.ExpectBidirectionalAudio();
2671 media_expectations.ExpectNoVideo();
2672 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2673 }
deadbeef1dcb1642017-03-29 21:08:16 -07002674}
2675
Taylor Brandstetter60c8dc82018-04-11 15:20:27 -07002676// Do one offer/answer with audio, another that disables it (rejecting the m=
2677// section), and another that re-enables it. Regression test for:
2678// bugs.webrtc.org/6023
2679TEST_F(PeerConnectionIntegrationTestPlanB, EnableAudioAfterRejecting) {
2680 ASSERT_TRUE(CreatePeerConnectionWrappers());
2681 ConnectFakeSignaling();
2682
2683 // Add audio track, do normal offer/answer.
2684 rtc::scoped_refptr<webrtc::AudioTrackInterface> track =
2685 caller()->CreateLocalAudioTrack();
2686 rtc::scoped_refptr<webrtc::RtpSenderInterface> sender =
2687 caller()->pc()->AddTrack(track, {"stream"}).MoveValue();
2688 caller()->CreateAndSetAndSignalOffer();
2689 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2690
2691 // Remove audio track, and set offer_to_receive_audio to false to cause the
2692 // m= section to be completely disabled, not just "recvonly".
2693 caller()->pc()->RemoveTrack(sender);
2694 PeerConnectionInterface::RTCOfferAnswerOptions options;
2695 options.offer_to_receive_audio = 0;
2696 caller()->SetOfferAnswerOptions(options);
2697 caller()->CreateAndSetAndSignalOffer();
2698 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2699
2700 // Add the audio track again, expecting negotiation to succeed and frames to
2701 // flow.
2702 sender = caller()->pc()->AddTrack(track, {"stream"}).MoveValue();
2703 options.offer_to_receive_audio = 1;
2704 caller()->SetOfferAnswerOptions(options);
2705 caller()->CreateAndSetAndSignalOffer();
2706 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2707
2708 MediaExpectations media_expectations;
2709 media_expectations.CalleeExpectsSomeAudio();
2710 EXPECT_TRUE(ExpectNewFrames(media_expectations));
2711}
2712
deadbeef1dcb1642017-03-29 21:08:16 -07002713// Basic end-to-end test, but without SSRC/MSID signaling. This functionality
2714// is needed to support legacy endpoints.
2715// TODO(deadbeef): When we support the MID extension and demuxing on MID, also
2716// add a test for an end-to-end test without MID signaling either (basically,
2717// the minimum acceptable SDP).
Seth Hampson2f0d7022018-02-20 11:54:42 -08002718TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithoutSsrcOrMsidSignaling) {
deadbeef1dcb1642017-03-29 21:08:16 -07002719 ASSERT_TRUE(CreatePeerConnectionWrappers());
2720 ConnectFakeSignaling();
2721 // Add audio and video, testing that packets can be demuxed on payload type.
Steve Anton15324772018-01-16 10:26:49 -08002722 caller()->AddAudioVideoTracks();
2723 callee()->AddAudioVideoTracks();
deadbeefd8ad7882017-04-18 16:01:17 -07002724 // Remove SSRCs and MSIDs from the received offer SDP.
2725 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
deadbeef1dcb1642017-03-29 21:08:16 -07002726 caller()->CreateAndSetAndSignalOffer();
2727 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002728 MediaExpectations media_expectations;
2729 media_expectations.ExpectBidirectionalAudioAndVideo();
2730 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002731}
2732
Seth Hampson5897a6e2018-04-03 11:16:33 -07002733// Basic end-to-end test, without SSRC signaling. This means that the track
2734// was created properly and frames are delivered when the MSIDs are communicated
2735// with a=msid lines and no a=ssrc lines.
2736TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
2737 EndToEndCallWithoutSsrcSignaling) {
2738 const char kStreamId[] = "streamId";
2739 ASSERT_TRUE(CreatePeerConnectionWrappers());
2740 ConnectFakeSignaling();
2741 // Add just audio tracks.
2742 caller()->AddTrack(caller()->CreateLocalAudioTrack(), {kStreamId});
2743 callee()->AddAudioTrack();
2744
2745 // Remove SSRCs from the received offer SDP.
2746 callee()->SetReceivedSdpMunger(RemoveSsrcsAndKeepMsids);
2747 caller()->CreateAndSetAndSignalOffer();
2748 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2749 MediaExpectations media_expectations;
2750 media_expectations.ExpectBidirectionalAudio();
2751 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2752}
2753
Johannes Kron3e983682020-03-29 22:17:00 +02002754TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
2755 EndToEndCallAddReceiveVideoToSendOnlyCall) {
2756 ASSERT_TRUE(CreatePeerConnectionWrappers());
2757 ConnectFakeSignaling();
2758 // Add one-directional video, from caller to callee.
2759 rtc::scoped_refptr<webrtc::VideoTrackInterface> track =
2760 caller()->CreateLocalVideoTrack();
2761
2762 RtpTransceiverInit video_transceiver_init;
2763 video_transceiver_init.stream_ids = {"video1"};
2764 video_transceiver_init.direction = RtpTransceiverDirection::kSendOnly;
2765 auto video_sender =
2766 caller()->pc()->AddTransceiver(track, video_transceiver_init).MoveValue();
2767 caller()->CreateAndSetAndSignalOffer();
2768 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2769
2770 // Add receive direction.
2771 video_sender->SetDirection(RtpTransceiverDirection::kSendRecv);
2772
2773 rtc::scoped_refptr<webrtc::VideoTrackInterface> callee_track =
2774 callee()->CreateLocalVideoTrack();
2775
2776 callee()->AddTrack(callee_track);
2777 caller()->CreateAndSetAndSignalOffer();
2778 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2779 // Ensure that video frames are received end-to-end.
2780 MediaExpectations media_expectations;
2781 media_expectations.ExpectBidirectionalVideo();
2782 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2783}
2784
Steve Antondf527fd2018-04-27 15:52:03 -07002785// Tests that video flows between multiple video tracks when SSRCs are not
2786// signaled. This exercises the MID RTP header extension which is needed to
2787// demux the incoming video tracks.
2788TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
2789 EndToEndCallWithTwoVideoTracksAndNoSignaledSsrc) {
2790 ASSERT_TRUE(CreatePeerConnectionWrappers());
2791 ConnectFakeSignaling();
2792 caller()->AddVideoTrack();
2793 caller()->AddVideoTrack();
2794 callee()->AddVideoTrack();
2795 callee()->AddVideoTrack();
2796
2797 caller()->SetReceivedSdpMunger(&RemoveSsrcsAndKeepMsids);
2798 callee()->SetReceivedSdpMunger(&RemoveSsrcsAndKeepMsids);
2799 caller()->CreateAndSetAndSignalOffer();
2800 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2801 ASSERT_EQ(2u, caller()->pc()->GetReceivers().size());
2802 ASSERT_EQ(2u, callee()->pc()->GetReceivers().size());
2803
2804 // Expect video to be received in both directions on both tracks.
2805 MediaExpectations media_expectations;
2806 media_expectations.ExpectBidirectionalVideo();
2807 EXPECT_TRUE(ExpectNewFrames(media_expectations));
2808}
2809
Henrik Boström5b147782018-12-04 11:25:05 +01002810TEST_F(PeerConnectionIntegrationTestUnifiedPlan, NoStreamsMsidLinePresent) {
2811 ASSERT_TRUE(CreatePeerConnectionWrappers());
2812 ConnectFakeSignaling();
2813 caller()->AddAudioTrack();
2814 caller()->AddVideoTrack();
2815 caller()->CreateAndSetAndSignalOffer();
2816 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2817 auto callee_receivers = callee()->pc()->GetReceivers();
2818 ASSERT_EQ(2u, callee_receivers.size());
2819 EXPECT_TRUE(callee_receivers[0]->stream_ids().empty());
2820 EXPECT_TRUE(callee_receivers[1]->stream_ids().empty());
2821}
2822
2823TEST_F(PeerConnectionIntegrationTestUnifiedPlan, NoStreamsMsidLineMissing) {
2824 ASSERT_TRUE(CreatePeerConnectionWrappers());
2825 ConnectFakeSignaling();
2826 caller()->AddAudioTrack();
2827 caller()->AddVideoTrack();
2828 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
2829 caller()->CreateAndSetAndSignalOffer();
2830 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2831 auto callee_receivers = callee()->pc()->GetReceivers();
2832 ASSERT_EQ(2u, callee_receivers.size());
2833 ASSERT_EQ(1u, callee_receivers[0]->stream_ids().size());
2834 ASSERT_EQ(1u, callee_receivers[1]->stream_ids().size());
2835 EXPECT_EQ(callee_receivers[0]->stream_ids()[0],
2836 callee_receivers[1]->stream_ids()[0]);
2837 EXPECT_EQ(callee_receivers[0]->streams()[0],
2838 callee_receivers[1]->streams()[0]);
2839}
2840
deadbeef1dcb1642017-03-29 21:08:16 -07002841// Test that if two video tracks are sent (from caller to callee, in this test),
2842// they're transmitted correctly end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002843TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithTwoVideoTracks) {
deadbeef1dcb1642017-03-29 21:08:16 -07002844 ASSERT_TRUE(CreatePeerConnectionWrappers());
2845 ConnectFakeSignaling();
2846 // Add one audio/video stream, and one video-only stream.
Steve Anton15324772018-01-16 10:26:49 -08002847 caller()->AddAudioVideoTracks();
2848 caller()->AddVideoTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07002849 caller()->CreateAndSetAndSignalOffer();
2850 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Steve Anton15324772018-01-16 10:26:49 -08002851 ASSERT_EQ(3u, callee()->pc()->GetReceivers().size());
Seth Hampson2f0d7022018-02-20 11:54:42 -08002852
2853 MediaExpectations media_expectations;
2854 media_expectations.CalleeExpectsSomeAudioAndVideo();
2855 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002856}
2857
2858static void MakeSpecCompliantMaxBundleOffer(cricket::SessionDescription* desc) {
2859 bool first = true;
2860 for (cricket::ContentInfo& content : desc->contents()) {
2861 if (first) {
2862 first = false;
2863 continue;
2864 }
2865 content.bundle_only = true;
2866 }
2867 first = true;
2868 for (cricket::TransportInfo& transport : desc->transport_infos()) {
2869 if (first) {
2870 first = false;
2871 continue;
2872 }
2873 transport.description.ice_ufrag.clear();
2874 transport.description.ice_pwd.clear();
2875 transport.description.connection_role = cricket::CONNECTIONROLE_NONE;
2876 transport.description.identity_fingerprint.reset(nullptr);
2877 }
2878}
2879
2880// Test that if applying a true "max bundle" offer, which uses ports of 0,
2881// "a=bundle-only", omitting "a=fingerprint", "a=setup", "a=ice-ufrag" and
2882// "a=ice-pwd" for all but the audio "m=" section, negotiation still completes
2883// successfully and media flows.
2884// TODO(deadbeef): Update this test to also omit "a=rtcp-mux", once that works.
2885// TODO(deadbeef): Won't need this test once we start generating actual
2886// standards-compliant SDP.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002887TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07002888 EndToEndCallWithSpecCompliantMaxBundleOffer) {
2889 ASSERT_TRUE(CreatePeerConnectionWrappers());
2890 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002891 caller()->AddAudioVideoTracks();
2892 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002893 // Do the equivalent of setting the port to 0, adding a=bundle-only, and
2894 // removing a=ice-ufrag, a=ice-pwd, a=fingerprint and a=setup from all
2895 // but the first m= section.
2896 callee()->SetReceivedSdpMunger(MakeSpecCompliantMaxBundleOffer);
2897 caller()->CreateAndSetAndSignalOffer();
2898 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002899 MediaExpectations media_expectations;
2900 media_expectations.ExpectBidirectionalAudioAndVideo();
2901 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002902}
2903
2904// Test that we can receive the audio output level from a remote audio track.
2905// TODO(deadbeef): Use a fake audio source and verify that the output level is
2906// exactly what the source on the other side was configured with.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002907TEST_P(PeerConnectionIntegrationTest, GetAudioOutputLevelStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-29 21:08:16 -07002908 ASSERT_TRUE(CreatePeerConnectionWrappers());
2909 ConnectFakeSignaling();
2910 // Just add an audio track.
Steve Anton15324772018-01-16 10:26:49 -08002911 caller()->AddAudioTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07002912 caller()->CreateAndSetAndSignalOffer();
2913 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2914
2915 // Get the audio output level stats. Note that the level is not available
2916 // until an RTCP packet has been received.
deadbeefd8ad7882017-04-18 16:01:17 -07002917 EXPECT_TRUE_WAIT(callee()->OldGetStats()->AudioOutputLevel() > 0,
deadbeef1dcb1642017-03-29 21:08:16 -07002918 kMaxWaitForFramesMs);
2919}
2920
2921// Test that an audio input level is reported.
2922// TODO(deadbeef): Use a fake audio source and verify that the input level is
2923// exactly what the source was configured with.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002924TEST_P(PeerConnectionIntegrationTest, GetAudioInputLevelStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-29 21:08:16 -07002925 ASSERT_TRUE(CreatePeerConnectionWrappers());
2926 ConnectFakeSignaling();
2927 // Just add an audio track.
Steve Anton15324772018-01-16 10:26:49 -08002928 caller()->AddAudioTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07002929 caller()->CreateAndSetAndSignalOffer();
2930 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2931
2932 // Get the audio input level stats. The level should be available very
2933 // soon after the test starts.
deadbeefd8ad7882017-04-18 16:01:17 -07002934 EXPECT_TRUE_WAIT(caller()->OldGetStats()->AudioInputLevel() > 0,
deadbeef1dcb1642017-03-29 21:08:16 -07002935 kMaxWaitForStatsMs);
2936}
2937
2938// Test that we can get incoming byte counts from both audio and video tracks.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002939TEST_P(PeerConnectionIntegrationTest, GetBytesReceivedStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-29 21:08:16 -07002940 ASSERT_TRUE(CreatePeerConnectionWrappers());
2941 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002942 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002943 // Do offer/answer, wait for the callee to receive some frames.
2944 caller()->CreateAndSetAndSignalOffer();
2945 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002946
2947 MediaExpectations media_expectations;
2948 media_expectations.CalleeExpectsSomeAudioAndVideo();
2949 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002950
2951 // Get a handle to the remote tracks created, so they can be used as GetStats
2952 // filters.
Mirko Bonadei739baf02019-01-27 17:29:42 +01002953 for (const auto& receiver : callee()->pc()->GetReceivers()) {
Steve Anton15324772018-01-16 10:26:49 -08002954 // We received frames, so we definitely should have nonzero "received bytes"
2955 // stats at this point.
2956 EXPECT_GT(callee()->OldGetStatsForTrack(receiver->track())->BytesReceived(),
2957 0);
2958 }
deadbeef1dcb1642017-03-29 21:08:16 -07002959}
2960
2961// Test that we can get outgoing byte counts from both audio and video tracks.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002962TEST_P(PeerConnectionIntegrationTest, GetBytesSentStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-29 21:08:16 -07002963 ASSERT_TRUE(CreatePeerConnectionWrappers());
2964 ConnectFakeSignaling();
2965 auto audio_track = caller()->CreateLocalAudioTrack();
2966 auto video_track = caller()->CreateLocalVideoTrack();
Steve Anton15324772018-01-16 10:26:49 -08002967 caller()->AddTrack(audio_track);
2968 caller()->AddTrack(video_track);
deadbeef1dcb1642017-03-29 21:08:16 -07002969 // Do offer/answer, wait for the callee to receive some frames.
2970 caller()->CreateAndSetAndSignalOffer();
2971 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002972 MediaExpectations media_expectations;
2973 media_expectations.CalleeExpectsSomeAudioAndVideo();
2974 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002975
2976 // The callee received frames, so we definitely should have nonzero "sent
2977 // bytes" stats at this point.
deadbeefd8ad7882017-04-18 16:01:17 -07002978 EXPECT_GT(caller()->OldGetStatsForTrack(audio_track)->BytesSent(), 0);
2979 EXPECT_GT(caller()->OldGetStatsForTrack(video_track)->BytesSent(), 0);
2980}
2981
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002982// Test that we can get capture start ntp time.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002983TEST_P(PeerConnectionIntegrationTest, GetCaptureStartNtpTimeWithOldStatsApi) {
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002984 ASSERT_TRUE(CreatePeerConnectionWrappers());
2985 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002986 caller()->AddAudioTrack();
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002987
Steve Anton15324772018-01-16 10:26:49 -08002988 callee()->AddAudioTrack();
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002989
2990 // Do offer/answer, wait for the callee to receive some frames.
2991 caller()->CreateAndSetAndSignalOffer();
2992 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2993
2994 // Get the remote audio track created on the receiver, so they can be used as
2995 // GetStats filters.
Steve Antonfc853712018-03-01 13:48:58 -08002996 auto receivers = callee()->pc()->GetReceivers();
2997 ASSERT_EQ(1u, receivers.size());
2998 auto remote_audio_track = receivers[0]->track();
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002999
3000 // Get the audio output level stats. Note that the level is not available
3001 // until an RTCP packet has been received.
Zhi Huange830e682018-03-30 10:48:35 -07003002 EXPECT_TRUE_WAIT(
3003 callee()->OldGetStatsForTrack(remote_audio_track)->CaptureStartNtpTime() >
3004 0,
3005 2 * kMaxWaitForFramesMs);
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02003006}
3007
Steve Antona41959e2018-11-28 11:15:33 -08003008// Test that the track ID is associated with all local and remote SSRC stats
3009// using the old GetStats() and more than 1 audio and more than 1 video track.
3010// This is a regression test for crbug.com/906988
3011TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
3012 OldGetStatsAssociatesTrackIdForManyMediaSections) {
3013 ASSERT_TRUE(CreatePeerConnectionWrappers());
3014 ConnectFakeSignaling();
3015 auto audio_sender_1 = caller()->AddAudioTrack();
3016 auto video_sender_1 = caller()->AddVideoTrack();
3017 auto audio_sender_2 = caller()->AddAudioTrack();
3018 auto video_sender_2 = caller()->AddVideoTrack();
3019 caller()->CreateAndSetAndSignalOffer();
3020 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3021
3022 MediaExpectations media_expectations;
3023 media_expectations.CalleeExpectsSomeAudioAndVideo();
3024 ASSERT_TRUE_WAIT(ExpectNewFrames(media_expectations), kDefaultTimeout);
3025
3026 std::vector<std::string> track_ids = {
3027 audio_sender_1->track()->id(), video_sender_1->track()->id(),
3028 audio_sender_2->track()->id(), video_sender_2->track()->id()};
3029
3030 auto caller_stats = caller()->OldGetStats();
3031 EXPECT_THAT(caller_stats->TrackIds(), UnorderedElementsAreArray(track_ids));
3032 auto callee_stats = callee()->OldGetStats();
3033 EXPECT_THAT(callee_stats->TrackIds(), UnorderedElementsAreArray(track_ids));
3034}
3035
Steve Antonffa6ce42018-11-30 09:26:08 -08003036// Test that the new GetStats() returns stats for all outgoing/incoming streams
3037// with the correct track IDs if there are more than one audio and more than one
3038// video senders/receivers.
3039TEST_P(PeerConnectionIntegrationTest, NewGetStatsManyAudioAndManyVideoStreams) {
3040 ASSERT_TRUE(CreatePeerConnectionWrappers());
3041 ConnectFakeSignaling();
3042 auto audio_sender_1 = caller()->AddAudioTrack();
3043 auto video_sender_1 = caller()->AddVideoTrack();
3044 auto audio_sender_2 = caller()->AddAudioTrack();
3045 auto video_sender_2 = caller()->AddVideoTrack();
3046 caller()->CreateAndSetAndSignalOffer();
3047 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3048
3049 MediaExpectations media_expectations;
3050 media_expectations.CalleeExpectsSomeAudioAndVideo();
3051 ASSERT_TRUE_WAIT(ExpectNewFrames(media_expectations), kDefaultTimeout);
3052
3053 std::vector<std::string> track_ids = {
3054 audio_sender_1->track()->id(), video_sender_1->track()->id(),
3055 audio_sender_2->track()->id(), video_sender_2->track()->id()};
3056
3057 rtc::scoped_refptr<const webrtc::RTCStatsReport> caller_report =
3058 caller()->NewGetStats();
3059 ASSERT_TRUE(caller_report);
3060 auto outbound_stream_stats =
3061 caller_report->GetStatsOfType<webrtc::RTCOutboundRTPStreamStats>();
Henrik Boströma0ff50c2020-05-05 15:54:46 +02003062 ASSERT_EQ(outbound_stream_stats.size(), 4u);
Steve Antonffa6ce42018-11-30 09:26:08 -08003063 std::vector<std::string> outbound_track_ids;
3064 for (const auto& stat : outbound_stream_stats) {
3065 ASSERT_TRUE(stat->bytes_sent.is_defined());
3066 EXPECT_LT(0u, *stat->bytes_sent);
Rasmus Brandt2efae772019-06-27 14:29:34 +02003067 if (*stat->kind == "video") {
3068 ASSERT_TRUE(stat->key_frames_encoded.is_defined());
3069 EXPECT_GT(*stat->key_frames_encoded, 0u);
3070 ASSERT_TRUE(stat->frames_encoded.is_defined());
3071 EXPECT_GE(*stat->frames_encoded, *stat->key_frames_encoded);
3072 }
Steve Antonffa6ce42018-11-30 09:26:08 -08003073 ASSERT_TRUE(stat->track_id.is_defined());
3074 const auto* track_stat =
3075 caller_report->GetAs<webrtc::RTCMediaStreamTrackStats>(*stat->track_id);
3076 ASSERT_TRUE(track_stat);
3077 outbound_track_ids.push_back(*track_stat->track_identifier);
3078 }
3079 EXPECT_THAT(outbound_track_ids, UnorderedElementsAreArray(track_ids));
3080
3081 rtc::scoped_refptr<const webrtc::RTCStatsReport> callee_report =
3082 callee()->NewGetStats();
3083 ASSERT_TRUE(callee_report);
3084 auto inbound_stream_stats =
3085 callee_report->GetStatsOfType<webrtc::RTCInboundRTPStreamStats>();
3086 ASSERT_EQ(4u, inbound_stream_stats.size());
3087 std::vector<std::string> inbound_track_ids;
3088 for (const auto& stat : inbound_stream_stats) {
3089 ASSERT_TRUE(stat->bytes_received.is_defined());
3090 EXPECT_LT(0u, *stat->bytes_received);
Rasmus Brandt2efae772019-06-27 14:29:34 +02003091 if (*stat->kind == "video") {
3092 ASSERT_TRUE(stat->key_frames_decoded.is_defined());
3093 EXPECT_GT(*stat->key_frames_decoded, 0u);
3094 ASSERT_TRUE(stat->frames_decoded.is_defined());
3095 EXPECT_GE(*stat->frames_decoded, *stat->key_frames_decoded);
3096 }
Steve Antonffa6ce42018-11-30 09:26:08 -08003097 ASSERT_TRUE(stat->track_id.is_defined());
3098 const auto* track_stat =
3099 callee_report->GetAs<webrtc::RTCMediaStreamTrackStats>(*stat->track_id);
3100 ASSERT_TRUE(track_stat);
3101 inbound_track_ids.push_back(*track_stat->track_identifier);
3102 }
3103 EXPECT_THAT(inbound_track_ids, UnorderedElementsAreArray(track_ids));
3104}
3105
3106// Test that we can get stats (using the new stats implementation) for
deadbeefd8ad7882017-04-18 16:01:17 -07003107// unsignaled streams. Meaning when SSRCs/MSIDs aren't signaled explicitly in
3108// SDP.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003109TEST_P(PeerConnectionIntegrationTest,
deadbeefd8ad7882017-04-18 16:01:17 -07003110 GetStatsForUnsignaledStreamWithNewStatsApi) {
3111 ASSERT_TRUE(CreatePeerConnectionWrappers());
3112 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08003113 caller()->AddAudioTrack();
deadbeefd8ad7882017-04-18 16:01:17 -07003114 // Remove SSRCs and MSIDs from the received offer SDP.
3115 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
3116 caller()->CreateAndSetAndSignalOffer();
3117 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08003118 MediaExpectations media_expectations;
3119 media_expectations.CalleeExpectsSomeAudio(1);
3120 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeefd8ad7882017-04-18 16:01:17 -07003121
3122 // We received a frame, so we should have nonzero "bytes received" stats for
3123 // the unsignaled stream, if stats are working for it.
3124 rtc::scoped_refptr<const webrtc::RTCStatsReport> report =
3125 callee()->NewGetStats();
3126 ASSERT_NE(nullptr, report);
3127 auto inbound_stream_stats =
3128 report->GetStatsOfType<webrtc::RTCInboundRTPStreamStats>();
3129 ASSERT_EQ(1U, inbound_stream_stats.size());
3130 ASSERT_TRUE(inbound_stream_stats[0]->bytes_received.is_defined());
3131 ASSERT_GT(*inbound_stream_stats[0]->bytes_received, 0U);
zhihuangf8164932017-05-19 13:09:47 -07003132 ASSERT_TRUE(inbound_stream_stats[0]->track_id.is_defined());
3133}
3134
Taylor Brandstettera4653442018-06-19 09:44:26 -07003135// Same as above but for the legacy stats implementation.
3136TEST_P(PeerConnectionIntegrationTest,
3137 GetStatsForUnsignaledStreamWithOldStatsApi) {
3138 ASSERT_TRUE(CreatePeerConnectionWrappers());
3139 ConnectFakeSignaling();
3140 caller()->AddAudioTrack();
3141 // Remove SSRCs and MSIDs from the received offer SDP.
3142 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
3143 caller()->CreateAndSetAndSignalOffer();
3144 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3145
3146 // Note that, since the old stats implementation associates SSRCs with tracks
3147 // using SDP, when SSRCs aren't signaled in SDP these stats won't have an
3148 // associated track ID. So we can't use the track "selector" argument.
3149 //
3150 // Also, we use "EXPECT_TRUE_WAIT" because the stats collector may decide to
3151 // return cached stats if not enough time has passed since the last update.
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02003152 EXPECT_TRUE_WAIT(callee()->OldGetStats()->BytesReceived() > 0,
Taylor Brandstettera4653442018-06-19 09:44:26 -07003153 kDefaultTimeout);
3154}
3155
zhihuangf8164932017-05-19 13:09:47 -07003156// Test that we can successfully get the media related stats (audio level
3157// etc.) for the unsignaled stream.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003158TEST_P(PeerConnectionIntegrationTest,
zhihuangf8164932017-05-19 13:09:47 -07003159 GetMediaStatsForUnsignaledStreamWithNewStatsApi) {
3160 ASSERT_TRUE(CreatePeerConnectionWrappers());
3161 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08003162 caller()->AddAudioVideoTracks();
zhihuangf8164932017-05-19 13:09:47 -07003163 // Remove SSRCs and MSIDs from the received offer SDP.
3164 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
3165 caller()->CreateAndSetAndSignalOffer();
3166 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08003167 MediaExpectations media_expectations;
3168 media_expectations.CalleeExpectsSomeAudio(1);
3169 media_expectations.CalleeExpectsSomeVideo(1);
3170 ASSERT_TRUE(ExpectNewFrames(media_expectations));
zhihuangf8164932017-05-19 13:09:47 -07003171
3172 rtc::scoped_refptr<const webrtc::RTCStatsReport> report =
3173 callee()->NewGetStats();
3174 ASSERT_NE(nullptr, report);
3175
3176 auto media_stats = report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
3177 auto audio_index = FindFirstMediaStatsIndexByKind("audio", media_stats);
3178 ASSERT_GE(audio_index, 0);
3179 EXPECT_TRUE(media_stats[audio_index]->audio_level.is_defined());
deadbeef1dcb1642017-03-29 21:08:16 -07003180}
3181
deadbeef4e2deab2017-09-20 13:56:21 -07003182// Helper for test below.
3183void ModifySsrcs(cricket::SessionDescription* desc) {
3184 for (ContentInfo& content : desc->contents()) {
Steve Antondf527fd2018-04-27 15:52:03 -07003185 for (StreamParams& stream :
Steve Antonb1c1de12017-12-21 15:14:30 -08003186 content.media_description()->mutable_streams()) {
deadbeef4e2deab2017-09-20 13:56:21 -07003187 for (uint32_t& ssrc : stream.ssrcs) {
3188 ssrc = rtc::CreateRandomId();
3189 }
3190 }
3191 }
3192}
3193
3194// Test that the "RTCMediaSteamTrackStats" object is updated correctly when
3195// SSRCs are unsignaled, and the SSRC of the received (audio) stream changes.
3196// This should result in two "RTCInboundRTPStreamStats", but only one
3197// "RTCMediaStreamTrackStats", whose counters go up continuously rather than
3198// being reset to 0 once the SSRC change occurs.
3199//
3200// Regression test for this bug:
3201// https://bugs.chromium.org/p/webrtc/issues/detail?id=8158
3202//
3203// The bug causes the track stats to only represent one of the two streams:
3204// whichever one has the higher SSRC. So with this bug, there was a 50% chance
3205// that the track stat counters would reset to 0 when the new stream is
3206// received, and a 50% chance that they'll stop updating (while
3207// "concealed_samples" continues increasing, due to silence being generated for
3208// the inactive stream).
Seth Hampson2f0d7022018-02-20 11:54:42 -08003209TEST_P(PeerConnectionIntegrationTest,
Steve Anton83119dd2017-11-10 16:19:52 -08003210 TrackStatsUpdatedCorrectlyWhenUnsignaledSsrcChanges) {
deadbeef4e2deab2017-09-20 13:56:21 -07003211 ASSERT_TRUE(CreatePeerConnectionWrappers());
3212 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08003213 caller()->AddAudioTrack();
deadbeef4e2deab2017-09-20 13:56:21 -07003214 // Remove SSRCs and MSIDs from the received offer SDP, simulating an endpoint
3215 // that doesn't signal SSRCs (from the callee's perspective).
3216 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
3217 caller()->CreateAndSetAndSignalOffer();
3218 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3219 // Wait for 50 audio frames (500ms of audio) to be received by the callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003220 {
3221 MediaExpectations media_expectations;
3222 media_expectations.CalleeExpectsSomeAudio(50);
3223 ASSERT_TRUE(ExpectNewFrames(media_expectations));
3224 }
deadbeef4e2deab2017-09-20 13:56:21 -07003225 // Some audio frames were received, so we should have nonzero "samples
3226 // received" for the track.
3227 rtc::scoped_refptr<const webrtc::RTCStatsReport> report =
3228 callee()->NewGetStats();
3229 ASSERT_NE(nullptr, report);
3230 auto track_stats = report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
3231 ASSERT_EQ(1U, track_stats.size());
3232 ASSERT_TRUE(track_stats[0]->total_samples_received.is_defined());
3233 ASSERT_GT(*track_stats[0]->total_samples_received, 0U);
3234 // uint64_t prev_samples_received = *track_stats[0]->total_samples_received;
3235
3236 // Create a new offer and munge it to cause the caller to use a new SSRC.
3237 caller()->SetGeneratedSdpMunger(ModifySsrcs);
3238 caller()->CreateAndSetAndSignalOffer();
3239 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3240 // Wait for 25 more audio frames (250ms of audio) to be received, from the new
3241 // SSRC.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003242 {
3243 MediaExpectations media_expectations;
3244 media_expectations.CalleeExpectsSomeAudio(25);
3245 ASSERT_TRUE(ExpectNewFrames(media_expectations));
3246 }
deadbeef4e2deab2017-09-20 13:56:21 -07003247
3248 report = callee()->NewGetStats();
3249 ASSERT_NE(nullptr, report);
3250 track_stats = report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
3251 ASSERT_EQ(1U, track_stats.size());
3252 ASSERT_TRUE(track_stats[0]->total_samples_received.is_defined());
3253 // The "total samples received" stat should only be greater than it was
3254 // before.
3255 // TODO(deadbeef): Uncomment this assertion once the bug is completely fixed.
3256 // Right now, the new SSRC will cause the counters to reset to 0.
3257 // EXPECT_GT(*track_stats[0]->total_samples_received, prev_samples_received);
3258
3259 // Additionally, the percentage of concealed samples (samples generated to
Steve Anton83119dd2017-11-10 16:19:52 -08003260 // conceal packet loss) should be less than 50%. If it's greater, that's a
deadbeef4e2deab2017-09-20 13:56:21 -07003261 // good sign that we're seeing stats from the old stream that's no longer
3262 // receiving packets, and is generating concealed samples of silence.
Steve Anton83119dd2017-11-10 16:19:52 -08003263 constexpr double kAcceptableConcealedSamplesPercentage = 0.50;
deadbeef4e2deab2017-09-20 13:56:21 -07003264 ASSERT_TRUE(track_stats[0]->concealed_samples.is_defined());
3265 EXPECT_LT(*track_stats[0]->concealed_samples,
3266 *track_stats[0]->total_samples_received *
3267 kAcceptableConcealedSamplesPercentage);
3268
3269 // Also ensure that we have two "RTCInboundRTPStreamStats" as expected, as a
3270 // sanity check that the SSRC really changed.
3271 // TODO(deadbeef): This isn't working right now, because we're not returning
3272 // *any* stats for the inactive stream. Uncomment when the bug is completely
3273 // fixed.
3274 // auto inbound_stream_stats =
3275 // report->GetStatsOfType<webrtc::RTCInboundRTPStreamStats>();
3276 // ASSERT_EQ(2U, inbound_stream_stats.size());
3277}
3278
deadbeef1dcb1642017-03-29 21:08:16 -07003279// Test that DTLS 1.0 is used if both sides only support DTLS 1.0.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003280TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithDtls10) {
deadbeef1dcb1642017-03-29 21:08:16 -07003281 PeerConnectionFactory::Options dtls_10_options;
3282 dtls_10_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
3283 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(dtls_10_options,
3284 dtls_10_options));
3285 ConnectFakeSignaling();
3286 // Do normal offer/answer and wait for some frames to be received in each
3287 // direction.
Steve Anton15324772018-01-16 10:26:49 -08003288 caller()->AddAudioVideoTracks();
3289 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003290 caller()->CreateAndSetAndSignalOffer();
3291 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08003292 MediaExpectations media_expectations;
3293 media_expectations.ExpectBidirectionalAudioAndVideo();
3294 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003295}
3296
3297// Test getting cipher stats and UMA metrics when DTLS 1.0 is negotiated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003298TEST_P(PeerConnectionIntegrationTest, Dtls10CipherStatsAndUmaMetrics) {
deadbeef1dcb1642017-03-29 21:08:16 -07003299 PeerConnectionFactory::Options dtls_10_options;
3300 dtls_10_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
3301 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(dtls_10_options,
3302 dtls_10_options));
3303 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08003304 caller()->AddAudioVideoTracks();
3305 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003306 caller()->CreateAndSetAndSignalOffer();
Qingsi Wang7fc821d2018-07-12 12:54:53 -07003307 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07003308 EXPECT_TRUE_WAIT(rtc::SSLStreamAdapter::IsAcceptableCipher(
deadbeefd8ad7882017-04-18 16:01:17 -07003309 caller()->OldGetStats()->DtlsCipher(), rtc::KT_DEFAULT),
deadbeef1dcb1642017-03-29 21:08:16 -07003310 kDefaultTimeout);
3311 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
deadbeefd8ad7882017-04-18 16:01:17 -07003312 caller()->OldGetStats()->SrtpCipher(), kDefaultTimeout);
Qingsi Wang7fc821d2018-07-12 12:54:53 -07003313 // TODO(bugs.webrtc.org/9456): Fix it.
Ying Wangef3998f2019-12-09 13:06:53 +01003314 EXPECT_METRIC_EQ(1, webrtc::metrics::NumEvents(
3315 "WebRTC.PeerConnection.SrtpCryptoSuite.Audio",
3316 kDefaultSrtpCryptoSuite));
deadbeef1dcb1642017-03-29 21:08:16 -07003317}
3318
3319// Test getting cipher stats and UMA metrics when DTLS 1.2 is negotiated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003320TEST_P(PeerConnectionIntegrationTest, Dtls12CipherStatsAndUmaMetrics) {
deadbeef1dcb1642017-03-29 21:08:16 -07003321 PeerConnectionFactory::Options dtls_12_options;
3322 dtls_12_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
3323 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(dtls_12_options,
3324 dtls_12_options));
3325 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08003326 caller()->AddAudioVideoTracks();
3327 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003328 caller()->CreateAndSetAndSignalOffer();
Qingsi Wang7fc821d2018-07-12 12:54:53 -07003329 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07003330 EXPECT_TRUE_WAIT(rtc::SSLStreamAdapter::IsAcceptableCipher(
deadbeefd8ad7882017-04-18 16:01:17 -07003331 caller()->OldGetStats()->DtlsCipher(), rtc::KT_DEFAULT),
deadbeef1dcb1642017-03-29 21:08:16 -07003332 kDefaultTimeout);
3333 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
deadbeefd8ad7882017-04-18 16:01:17 -07003334 caller()->OldGetStats()->SrtpCipher(), kDefaultTimeout);
Qingsi Wang7fc821d2018-07-12 12:54:53 -07003335 // TODO(bugs.webrtc.org/9456): Fix it.
Ying Wangef3998f2019-12-09 13:06:53 +01003336 EXPECT_METRIC_EQ(1, webrtc::metrics::NumEvents(
3337 "WebRTC.PeerConnection.SrtpCryptoSuite.Audio",
3338 kDefaultSrtpCryptoSuite));
deadbeef1dcb1642017-03-29 21:08:16 -07003339}
3340
3341// Test that DTLS 1.0 can be used if the caller supports DTLS 1.2 and the
3342// callee only supports 1.0.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003343TEST_P(PeerConnectionIntegrationTest, CallerDtls12ToCalleeDtls10) {
deadbeef1dcb1642017-03-29 21:08:16 -07003344 PeerConnectionFactory::Options caller_options;
3345 caller_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
3346 PeerConnectionFactory::Options callee_options;
3347 callee_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
3348 ASSERT_TRUE(
3349 CreatePeerConnectionWrappersWithOptions(caller_options, callee_options));
3350 ConnectFakeSignaling();
3351 // Do normal offer/answer and wait for some frames to be received in each
3352 // direction.
Steve Anton15324772018-01-16 10:26:49 -08003353 caller()->AddAudioVideoTracks();
3354 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003355 caller()->CreateAndSetAndSignalOffer();
3356 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08003357 MediaExpectations media_expectations;
3358 media_expectations.ExpectBidirectionalAudioAndVideo();
3359 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003360}
3361
3362// Test that DTLS 1.0 can be used if the caller only supports DTLS 1.0 and the
3363// callee supports 1.2.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003364TEST_P(PeerConnectionIntegrationTest, CallerDtls10ToCalleeDtls12) {
deadbeef1dcb1642017-03-29 21:08:16 -07003365 PeerConnectionFactory::Options caller_options;
3366 caller_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
3367 PeerConnectionFactory::Options callee_options;
3368 callee_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
3369 ASSERT_TRUE(
3370 CreatePeerConnectionWrappersWithOptions(caller_options, callee_options));
3371 ConnectFakeSignaling();
3372 // Do normal offer/answer and wait for some frames to be received in each
3373 // direction.
Steve Anton15324772018-01-16 10:26:49 -08003374 caller()->AddAudioVideoTracks();
3375 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003376 caller()->CreateAndSetAndSignalOffer();
3377 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08003378 MediaExpectations media_expectations;
3379 media_expectations.ExpectBidirectionalAudioAndVideo();
3380 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003381}
3382
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07003383// The three tests below verify that "enable_aes128_sha1_32_crypto_cipher"
3384// works as expected; the cipher should only be used if enabled by both sides.
3385TEST_P(PeerConnectionIntegrationTest,
3386 Aes128Sha1_32_CipherNotUsedWhenOnlyCallerSupported) {
3387 PeerConnectionFactory::Options caller_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07003388 caller_options.crypto_options.srtp.enable_aes128_sha1_32_crypto_cipher = true;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07003389 PeerConnectionFactory::Options callee_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07003390 callee_options.crypto_options.srtp.enable_aes128_sha1_32_crypto_cipher =
3391 false;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07003392 int expected_cipher_suite = rtc::SRTP_AES128_CM_SHA1_80;
3393 TestNegotiatedCipherSuite(caller_options, callee_options,
3394 expected_cipher_suite);
3395}
3396
3397TEST_P(PeerConnectionIntegrationTest,
3398 Aes128Sha1_32_CipherNotUsedWhenOnlyCalleeSupported) {
3399 PeerConnectionFactory::Options caller_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07003400 caller_options.crypto_options.srtp.enable_aes128_sha1_32_crypto_cipher =
3401 false;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07003402 PeerConnectionFactory::Options callee_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07003403 callee_options.crypto_options.srtp.enable_aes128_sha1_32_crypto_cipher = true;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07003404 int expected_cipher_suite = rtc::SRTP_AES128_CM_SHA1_80;
3405 TestNegotiatedCipherSuite(caller_options, callee_options,
3406 expected_cipher_suite);
3407}
3408
3409TEST_P(PeerConnectionIntegrationTest, Aes128Sha1_32_CipherUsedWhenSupported) {
3410 PeerConnectionFactory::Options caller_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07003411 caller_options.crypto_options.srtp.enable_aes128_sha1_32_crypto_cipher = true;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07003412 PeerConnectionFactory::Options callee_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07003413 callee_options.crypto_options.srtp.enable_aes128_sha1_32_crypto_cipher = true;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07003414 int expected_cipher_suite = rtc::SRTP_AES128_CM_SHA1_32;
3415 TestNegotiatedCipherSuite(caller_options, callee_options,
3416 expected_cipher_suite);
3417}
3418
deadbeef1dcb1642017-03-29 21:08:16 -07003419// Test that a non-GCM cipher is used if both sides only support non-GCM.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003420TEST_P(PeerConnectionIntegrationTest, NonGcmCipherUsedWhenGcmNotSupported) {
deadbeef1dcb1642017-03-29 21:08:16 -07003421 bool local_gcm_enabled = false;
3422 bool remote_gcm_enabled = false;
Philipp Hancke2ebbff82019-10-26 06:12:55 +02003423 bool aes_ctr_enabled = true;
deadbeef1dcb1642017-03-29 21:08:16 -07003424 int expected_cipher_suite = kDefaultSrtpCryptoSuite;
3425 TestGcmNegotiationUsesCipherSuite(local_gcm_enabled, remote_gcm_enabled,
Philipp Hancke2ebbff82019-10-26 06:12:55 +02003426 aes_ctr_enabled, expected_cipher_suite);
deadbeef1dcb1642017-03-29 21:08:16 -07003427}
3428
Philipp Hancke2ebbff82019-10-26 06:12:55 +02003429// Test that a GCM cipher is used if both ends support it and non-GCM is
3430// disabled.
3431TEST_P(PeerConnectionIntegrationTest, GcmCipherUsedWhenOnlyGcmSupported) {
deadbeef1dcb1642017-03-29 21:08:16 -07003432 bool local_gcm_enabled = true;
3433 bool remote_gcm_enabled = true;
Philipp Hancke2ebbff82019-10-26 06:12:55 +02003434 bool aes_ctr_enabled = false;
deadbeef1dcb1642017-03-29 21:08:16 -07003435 int expected_cipher_suite = kDefaultSrtpCryptoSuiteGcm;
3436 TestGcmNegotiationUsesCipherSuite(local_gcm_enabled, remote_gcm_enabled,
Philipp Hancke2ebbff82019-10-26 06:12:55 +02003437 aes_ctr_enabled, expected_cipher_suite);
deadbeef1dcb1642017-03-29 21:08:16 -07003438}
3439
deadbeef7914b8c2017-04-21 03:23:33 -07003440// Verify that media can be transmitted end-to-end when GCM crypto suites are
3441// enabled. Note that the above tests, such as GcmCipherUsedWhenGcmSupported,
3442// only verify that a GCM cipher is negotiated, and not necessarily that SRTP
3443// works with it.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003444TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithGcmCipher) {
deadbeef7914b8c2017-04-21 03:23:33 -07003445 PeerConnectionFactory::Options gcm_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07003446 gcm_options.crypto_options.srtp.enable_gcm_crypto_suites = true;
Philipp Hancke2ebbff82019-10-26 06:12:55 +02003447 gcm_options.crypto_options.srtp.enable_aes128_sha1_80_crypto_cipher = false;
deadbeef7914b8c2017-04-21 03:23:33 -07003448 ASSERT_TRUE(
3449 CreatePeerConnectionWrappersWithOptions(gcm_options, gcm_options));
3450 ConnectFakeSignaling();
3451 // Do normal offer/answer and wait for some frames to be received in each
3452 // direction.
Steve Anton15324772018-01-16 10:26:49 -08003453 caller()->AddAudioVideoTracks();
3454 callee()->AddAudioVideoTracks();
deadbeef7914b8c2017-04-21 03:23:33 -07003455 caller()->CreateAndSetAndSignalOffer();
3456 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08003457 MediaExpectations media_expectations;
3458 media_expectations.ExpectBidirectionalAudioAndVideo();
3459 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef7914b8c2017-04-21 03:23:33 -07003460}
3461
deadbeef1dcb1642017-03-29 21:08:16 -07003462// This test sets up a call between two parties with audio, video and an RTP
3463// data channel.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003464TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithRtpDataChannel) {
Niels Möllerf06f9232018-08-07 12:32:18 +02003465 PeerConnectionInterface::RTCConfiguration rtc_config;
3466 rtc_config.enable_rtp_data_channel = true;
3467 rtc_config.enable_dtls_srtp = false;
3468 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(rtc_config, rtc_config));
deadbeef1dcb1642017-03-29 21:08:16 -07003469 ConnectFakeSignaling();
3470 // Expect that data channel created on caller side will show up for callee as
3471 // well.
3472 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08003473 caller()->AddAudioVideoTracks();
3474 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003475 caller()->CreateAndSetAndSignalOffer();
3476 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3477 // Ensure the existence of the RTP data channel didn't impede audio/video.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003478 MediaExpectations media_expectations;
3479 media_expectations.ExpectBidirectionalAudioAndVideo();
3480 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003481 ASSERT_NE(nullptr, caller()->data_channel());
3482 ASSERT_NE(nullptr, callee()->data_channel());
3483 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3484 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3485
3486 // Ensure data can be sent in both directions.
3487 std::string data = "hello world";
3488 SendRtpDataWithRetries(caller()->data_channel(), data, 5);
3489 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3490 kDefaultTimeout);
3491 SendRtpDataWithRetries(callee()->data_channel(), data, 5);
3492 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3493 kDefaultTimeout);
3494}
3495
Eldar Rellod9ebe012020-03-18 20:41:45 +02003496TEST_P(PeerConnectionIntegrationTest, RtpDataChannelWorksAfterRollback) {
3497 PeerConnectionInterface::RTCConfiguration rtc_config;
3498 rtc_config.enable_rtp_data_channel = true;
3499 rtc_config.enable_dtls_srtp = false;
3500 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(rtc_config, rtc_config));
3501 ConnectFakeSignaling();
3502 auto data_channel = caller()->pc()->CreateDataChannel("label_1", nullptr);
3503 ASSERT_TRUE(data_channel.get() != nullptr);
3504 caller()->CreateAndSetAndSignalOffer();
3505 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3506
3507 caller()->CreateDataChannel("label_2", nullptr);
3508 rtc::scoped_refptr<MockSetSessionDescriptionObserver> observer(
3509 new rtc::RefCountedObject<MockSetSessionDescriptionObserver>());
3510 caller()->pc()->SetLocalDescription(observer,
3511 caller()->CreateOfferAndWait().release());
3512 EXPECT_TRUE_WAIT(observer->called(), kDefaultTimeout);
3513 caller()->Rollback();
3514
3515 std::string data = "hello world";
3516 SendRtpDataWithRetries(data_channel, data, 5);
3517 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3518 kDefaultTimeout);
3519}
3520
deadbeef1dcb1642017-03-29 21:08:16 -07003521// Ensure that an RTP data channel is signaled as closed for the caller when
3522// the callee rejects it in a subsequent offer.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003523TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07003524 RtpDataChannelSignaledClosedInCalleeOffer) {
3525 // Same procedure as above test.
Niels Möllerf06f9232018-08-07 12:32:18 +02003526 PeerConnectionInterface::RTCConfiguration rtc_config;
3527 rtc_config.enable_rtp_data_channel = true;
3528 rtc_config.enable_dtls_srtp = false;
3529 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(rtc_config, rtc_config));
deadbeef1dcb1642017-03-29 21:08:16 -07003530 ConnectFakeSignaling();
3531 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08003532 caller()->AddAudioVideoTracks();
3533 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003534 caller()->CreateAndSetAndSignalOffer();
3535 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3536 ASSERT_NE(nullptr, caller()->data_channel());
3537 ASSERT_NE(nullptr, callee()->data_channel());
3538 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3539 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3540
3541 // Close the data channel on the callee, and do an updated offer/answer.
3542 callee()->data_channel()->Close();
3543 callee()->CreateAndSetAndSignalOffer();
3544 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3545 EXPECT_FALSE(caller()->data_observer()->IsOpen());
3546 EXPECT_FALSE(callee()->data_observer()->IsOpen());
3547}
3548
3549// Tests that data is buffered in an RTP data channel until an observer is
3550// registered for it.
3551//
3552// NOTE: RTP data channels can receive data before the underlying
3553// transport has detected that a channel is writable and thus data can be
3554// received before the data channel state changes to open. That is hard to test
3555// but the same buffering is expected to be used in that case.
Yves Gerey100fe632020-01-17 19:15:53 +01003556//
3557// Use fake clock and simulated network delay so that we predictably can wait
3558// until an SCTP message has been delivered without "sleep()"ing.
3559TEST_P(PeerConnectionIntegrationTestWithFakeClock,
deadbeef1dcb1642017-03-29 21:08:16 -07003560 DataBufferedUntilRtpDataChannelObserverRegistered) {
deadbeef1dcb1642017-03-29 21:08:16 -07003561 virtual_socket_server()->set_delay_mean(5); // 5 ms per hop.
3562 virtual_socket_server()->UpdateDelayDistribution();
3563
Niels Möllerf06f9232018-08-07 12:32:18 +02003564 PeerConnectionInterface::RTCConfiguration rtc_config;
3565 rtc_config.enable_rtp_data_channel = true;
3566 rtc_config.enable_dtls_srtp = false;
3567 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(rtc_config, rtc_config));
deadbeef1dcb1642017-03-29 21:08:16 -07003568 ConnectFakeSignaling();
3569 caller()->CreateDataChannel();
3570 caller()->CreateAndSetAndSignalOffer();
3571 ASSERT_TRUE(caller()->data_channel() != nullptr);
3572 ASSERT_TRUE_SIMULATED_WAIT(callee()->data_channel() != nullptr,
Yves Gerey100fe632020-01-17 19:15:53 +01003573 kDefaultTimeout, FakeClock());
deadbeef1dcb1642017-03-29 21:08:16 -07003574 ASSERT_TRUE_SIMULATED_WAIT(caller()->data_observer()->IsOpen(),
Yves Gerey100fe632020-01-17 19:15:53 +01003575 kDefaultTimeout, FakeClock());
deadbeef1dcb1642017-03-29 21:08:16 -07003576 ASSERT_EQ_SIMULATED_WAIT(DataChannelInterface::kOpen,
3577 callee()->data_channel()->state(), kDefaultTimeout,
Yves Gerey100fe632020-01-17 19:15:53 +01003578 FakeClock());
deadbeef1dcb1642017-03-29 21:08:16 -07003579
3580 // Unregister the observer which is normally automatically registered.
3581 callee()->data_channel()->UnregisterObserver();
3582 // Send data and advance fake clock until it should have been received.
3583 std::string data = "hello world";
3584 caller()->data_channel()->Send(DataBuffer(data));
Yves Gerey100fe632020-01-17 19:15:53 +01003585 SIMULATED_WAIT(false, 50, FakeClock());
deadbeef1dcb1642017-03-29 21:08:16 -07003586
3587 // Attach data channel and expect data to be received immediately. Note that
3588 // EXPECT_EQ_WAIT is used, such that the simulated clock is not advanced any
3589 // further, but data can be received even if the callback is asynchronous.
3590 MockDataChannelObserver new_observer(callee()->data_channel());
3591 EXPECT_EQ_SIMULATED_WAIT(data, new_observer.last_message(), kDefaultTimeout,
Yves Gerey100fe632020-01-17 19:15:53 +01003592 FakeClock());
deadbeef1dcb1642017-03-29 21:08:16 -07003593}
3594
3595// This test sets up a call between two parties with audio, video and but only
3596// the caller client supports RTP data channels.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003597TEST_P(PeerConnectionIntegrationTest, RtpDataChannelsRejectedByCallee) {
Niels Möllerf06f9232018-08-07 12:32:18 +02003598 PeerConnectionInterface::RTCConfiguration rtc_config_1;
3599 rtc_config_1.enable_rtp_data_channel = true;
deadbeef1dcb1642017-03-29 21:08:16 -07003600 // Must disable DTLS to make negotiation succeed.
Niels Möllerf06f9232018-08-07 12:32:18 +02003601 rtc_config_1.enable_dtls_srtp = false;
3602 PeerConnectionInterface::RTCConfiguration rtc_config_2;
3603 rtc_config_2.enable_dtls_srtp = false;
3604 rtc_config_2.enable_dtls_srtp = false;
3605 ASSERT_TRUE(
3606 CreatePeerConnectionWrappersWithConfig(rtc_config_1, rtc_config_2));
deadbeef1dcb1642017-03-29 21:08:16 -07003607 ConnectFakeSignaling();
3608 caller()->CreateDataChannel();
Harald Alvestrandf3736ed2019-04-08 13:09:30 +02003609 ASSERT_TRUE(caller()->data_channel() != nullptr);
Steve Anton15324772018-01-16 10:26:49 -08003610 caller()->AddAudioVideoTracks();
3611 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003612 caller()->CreateAndSetAndSignalOffer();
3613 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3614 // The caller should still have a data channel, but it should be closed, and
3615 // one should ever have been created for the callee.
3616 EXPECT_TRUE(caller()->data_channel() != nullptr);
3617 EXPECT_FALSE(caller()->data_observer()->IsOpen());
3618 EXPECT_EQ(nullptr, callee()->data_channel());
3619}
3620
3621// This test sets up a call between two parties with audio, and video. When
3622// audio and video is setup and flowing, an RTP data channel is negotiated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003623TEST_P(PeerConnectionIntegrationTest, AddRtpDataChannelInSubsequentOffer) {
Niels Möllerf06f9232018-08-07 12:32:18 +02003624 PeerConnectionInterface::RTCConfiguration rtc_config;
3625 rtc_config.enable_rtp_data_channel = true;
3626 rtc_config.enable_dtls_srtp = false;
3627 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(rtc_config, rtc_config));
deadbeef1dcb1642017-03-29 21:08:16 -07003628 ConnectFakeSignaling();
3629 // Do initial offer/answer with audio/video.
Steve Anton15324772018-01-16 10:26:49 -08003630 caller()->AddAudioVideoTracks();
3631 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003632 caller()->CreateAndSetAndSignalOffer();
3633 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3634 // Create data channel and do new offer and answer.
3635 caller()->CreateDataChannel();
3636 caller()->CreateAndSetAndSignalOffer();
3637 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3638 ASSERT_NE(nullptr, caller()->data_channel());
3639 ASSERT_NE(nullptr, callee()->data_channel());
3640 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3641 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3642 // Ensure data can be sent in both directions.
3643 std::string data = "hello world";
3644 SendRtpDataWithRetries(caller()->data_channel(), data, 5);
3645 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3646 kDefaultTimeout);
3647 SendRtpDataWithRetries(callee()->data_channel(), data, 5);
3648 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3649 kDefaultTimeout);
3650}
3651
3652#ifdef HAVE_SCTP
3653
3654// This test sets up a call between two parties with audio, video and an SCTP
3655// data channel.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003656TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithSctpDataChannel) {
deadbeef1dcb1642017-03-29 21:08:16 -07003657 ASSERT_TRUE(CreatePeerConnectionWrappers());
3658 ConnectFakeSignaling();
3659 // Expect that data channel created on caller side will show up for callee as
3660 // well.
3661 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08003662 caller()->AddAudioVideoTracks();
3663 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003664 caller()->CreateAndSetAndSignalOffer();
3665 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3666 // Ensure the existence of the SCTP data channel didn't impede audio/video.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003667 MediaExpectations media_expectations;
3668 media_expectations.ExpectBidirectionalAudioAndVideo();
3669 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003670 // Caller data channel should already exist (it created one). Callee data
3671 // channel may not exist yet, since negotiation happens in-band, not in SDP.
3672 ASSERT_NE(nullptr, caller()->data_channel());
3673 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3674 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3675 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3676
3677 // Ensure data can be sent in both directions.
3678 std::string data = "hello world";
3679 caller()->data_channel()->Send(DataBuffer(data));
3680 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3681 kDefaultTimeout);
3682 callee()->data_channel()->Send(DataBuffer(data));
3683 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3684 kDefaultTimeout);
3685}
3686
3687// Ensure that when the callee closes an SCTP data channel, the closing
3688// procedure results in the data channel being closed for the caller as well.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003689TEST_P(PeerConnectionIntegrationTest, CalleeClosesSctpDataChannel) {
deadbeef1dcb1642017-03-29 21:08:16 -07003690 // Same procedure as above test.
3691 ASSERT_TRUE(CreatePeerConnectionWrappers());
3692 ConnectFakeSignaling();
3693 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08003694 caller()->AddAudioVideoTracks();
3695 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003696 caller()->CreateAndSetAndSignalOffer();
3697 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3698 ASSERT_NE(nullptr, caller()->data_channel());
3699 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3700 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3701 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3702
3703 // Close the data channel on the callee side, and wait for it to reach the
3704 // "closed" state on both sides.
3705 callee()->data_channel()->Close();
3706 EXPECT_TRUE_WAIT(!caller()->data_observer()->IsOpen(), kDefaultTimeout);
3707 EXPECT_TRUE_WAIT(!callee()->data_observer()->IsOpen(), kDefaultTimeout);
3708}
3709
Seth Hampson2f0d7022018-02-20 11:54:42 -08003710TEST_P(PeerConnectionIntegrationTest, SctpDataChannelConfigSentToOtherSide) {
Steve Antonda6c0952017-10-23 11:41:54 -07003711 ASSERT_TRUE(CreatePeerConnectionWrappers());
3712 ConnectFakeSignaling();
3713 webrtc::DataChannelInit init;
3714 init.id = 53;
3715 init.maxRetransmits = 52;
3716 caller()->CreateDataChannel("data-channel", &init);
Steve Anton15324772018-01-16 10:26:49 -08003717 caller()->AddAudioVideoTracks();
3718 callee()->AddAudioVideoTracks();
Steve Antonda6c0952017-10-23 11:41:54 -07003719 caller()->CreateAndSetAndSignalOffer();
3720 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Steve Anton074dece2017-10-24 13:04:12 -07003721 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3722 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
Harald Alvestrand5c4d2ee2019-04-01 12:58:15 +02003723 // Since "negotiated" is false, the "id" parameter should be ignored.
3724 EXPECT_NE(init.id, callee()->data_channel()->id());
Steve Antonda6c0952017-10-23 11:41:54 -07003725 EXPECT_EQ("data-channel", callee()->data_channel()->label());
3726 EXPECT_EQ(init.maxRetransmits, callee()->data_channel()->maxRetransmits());
3727 EXPECT_FALSE(callee()->data_channel()->negotiated());
3728}
3729
deadbeef1dcb1642017-03-29 21:08:16 -07003730// Test usrsctp's ability to process unordered data stream, where data actually
3731// arrives out of order using simulated delays. Previously there have been some
3732// bugs in this area.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003733TEST_P(PeerConnectionIntegrationTest, StressTestUnorderedSctpDataChannel) {
deadbeef1dcb1642017-03-29 21:08:16 -07003734 // Introduce random network delays.
3735 // Otherwise it's not a true "unordered" test.
3736 virtual_socket_server()->set_delay_mean(20);
3737 virtual_socket_server()->set_delay_stddev(5);
3738 virtual_socket_server()->UpdateDelayDistribution();
3739 // Normal procedure, but with unordered data channel config.
3740 ASSERT_TRUE(CreatePeerConnectionWrappers());
3741 ConnectFakeSignaling();
3742 webrtc::DataChannelInit init;
3743 init.ordered = false;
3744 caller()->CreateDataChannel(&init);
3745 caller()->CreateAndSetAndSignalOffer();
3746 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3747 ASSERT_NE(nullptr, caller()->data_channel());
3748 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3749 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3750 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3751
3752 static constexpr int kNumMessages = 100;
3753 // Deliberately chosen to be larger than the MTU so messages get fragmented.
3754 static constexpr size_t kMaxMessageSize = 4096;
3755 // Create and send random messages.
3756 std::vector<std::string> sent_messages;
3757 for (int i = 0; i < kNumMessages; ++i) {
3758 size_t length =
3759 (rand() % kMaxMessageSize) + 1; // NOLINT (rand_r instead of rand)
3760 std::string message;
3761 ASSERT_TRUE(rtc::CreateRandomString(length, &message));
3762 caller()->data_channel()->Send(DataBuffer(message));
3763 callee()->data_channel()->Send(DataBuffer(message));
3764 sent_messages.push_back(message);
3765 }
3766
3767 // Wait for all messages to be received.
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02003768 EXPECT_EQ_WAIT(rtc::checked_cast<size_t>(kNumMessages),
deadbeef1dcb1642017-03-29 21:08:16 -07003769 caller()->data_observer()->received_message_count(),
3770 kDefaultTimeout);
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02003771 EXPECT_EQ_WAIT(rtc::checked_cast<size_t>(kNumMessages),
deadbeef1dcb1642017-03-29 21:08:16 -07003772 callee()->data_observer()->received_message_count(),
3773 kDefaultTimeout);
3774
3775 // Sort and compare to make sure none of the messages were corrupted.
3776 std::vector<std::string> caller_received_messages =
3777 caller()->data_observer()->messages();
3778 std::vector<std::string> callee_received_messages =
3779 callee()->data_observer()->messages();
Steve Anton64b626b2019-01-28 17:25:26 -08003780 absl::c_sort(sent_messages);
3781 absl::c_sort(caller_received_messages);
3782 absl::c_sort(callee_received_messages);
deadbeef1dcb1642017-03-29 21:08:16 -07003783 EXPECT_EQ(sent_messages, caller_received_messages);
3784 EXPECT_EQ(sent_messages, callee_received_messages);
3785}
3786
3787// This test sets up a call between two parties with audio, and video. When
3788// audio and video are setup and flowing, an SCTP data channel is negotiated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003789TEST_P(PeerConnectionIntegrationTest, AddSctpDataChannelInSubsequentOffer) {
deadbeef1dcb1642017-03-29 21:08:16 -07003790 ASSERT_TRUE(CreatePeerConnectionWrappers());
3791 ConnectFakeSignaling();
3792 // Do initial offer/answer with audio/video.
Steve Anton15324772018-01-16 10:26:49 -08003793 caller()->AddAudioVideoTracks();
3794 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003795 caller()->CreateAndSetAndSignalOffer();
3796 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3797 // Create data channel and do new offer and answer.
3798 caller()->CreateDataChannel();
3799 caller()->CreateAndSetAndSignalOffer();
3800 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3801 // Caller data channel should already exist (it created one). Callee data
3802 // channel may not exist yet, since negotiation happens in-band, not in SDP.
3803 ASSERT_NE(nullptr, caller()->data_channel());
3804 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3805 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3806 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3807 // Ensure data can be sent in both directions.
3808 std::string data = "hello world";
3809 caller()->data_channel()->Send(DataBuffer(data));
3810 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3811 kDefaultTimeout);
3812 callee()->data_channel()->Send(DataBuffer(data));
3813 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3814 kDefaultTimeout);
3815}
3816
deadbeef7914b8c2017-04-21 03:23:33 -07003817// Set up a connection initially just using SCTP data channels, later upgrading
3818// to audio/video, ensuring frames are received end-to-end. Effectively the
3819// inverse of the test above.
3820// This was broken in M57; see https://crbug.com/711243
Seth Hampson2f0d7022018-02-20 11:54:42 -08003821TEST_P(PeerConnectionIntegrationTest, SctpDataChannelToAudioVideoUpgrade) {
deadbeef7914b8c2017-04-21 03:23:33 -07003822 ASSERT_TRUE(CreatePeerConnectionWrappers());
3823 ConnectFakeSignaling();
3824 // Do initial offer/answer with just data channel.
3825 caller()->CreateDataChannel();
3826 caller()->CreateAndSetAndSignalOffer();
3827 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3828 // Wait until data can be sent over the data channel.
3829 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3830 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3831 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3832
3833 // Do subsequent offer/answer with two-way audio and video. Audio and video
3834 // should end up bundled on the DTLS/ICE transport already used for data.
Steve Anton15324772018-01-16 10:26:49 -08003835 caller()->AddAudioVideoTracks();
3836 callee()->AddAudioVideoTracks();
deadbeef7914b8c2017-04-21 03:23:33 -07003837 caller()->CreateAndSetAndSignalOffer();
3838 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08003839 MediaExpectations media_expectations;
3840 media_expectations.ExpectBidirectionalAudioAndVideo();
3841 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef7914b8c2017-04-21 03:23:33 -07003842}
3843
deadbeef8b7e9ad2017-05-25 09:38:55 -07003844static void MakeSpecCompliantSctpOffer(cricket::SessionDescription* desc) {
Harald Alvestrand5fc28b12019-05-13 13:36:16 +02003845 cricket::SctpDataContentDescription* dcd_offer =
3846 GetFirstSctpDataContentDescription(desc);
Harald Alvestrand17ea0682019-12-13 11:51:04 +01003847 // See https://crbug.com/webrtc/11211 - this function is a no-op
Steve Antonb1c1de12017-12-21 15:14:30 -08003848 ASSERT_TRUE(dcd_offer);
deadbeef8b7e9ad2017-05-25 09:38:55 -07003849 dcd_offer->set_use_sctpmap(false);
3850 dcd_offer->set_protocol("UDP/DTLS/SCTP");
3851}
3852
3853// Test that the data channel works when a spec-compliant SCTP m= section is
3854// offered (using "a=sctp-port" instead of "a=sctpmap", and using
3855// "UDP/DTLS/SCTP" as the protocol).
Seth Hampson2f0d7022018-02-20 11:54:42 -08003856TEST_P(PeerConnectionIntegrationTest,
deadbeef8b7e9ad2017-05-25 09:38:55 -07003857 DataChannelWorksWhenSpecCompliantSctpOfferReceived) {
3858 ASSERT_TRUE(CreatePeerConnectionWrappers());
3859 ConnectFakeSignaling();
3860 caller()->CreateDataChannel();
3861 caller()->SetGeneratedSdpMunger(MakeSpecCompliantSctpOffer);
3862 caller()->CreateAndSetAndSignalOffer();
3863 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3864 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3865 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3866 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3867
3868 // Ensure data can be sent in both directions.
3869 std::string data = "hello world";
3870 caller()->data_channel()->Send(DataBuffer(data));
3871 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3872 kDefaultTimeout);
3873 callee()->data_channel()->Send(DataBuffer(data));
3874 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3875 kDefaultTimeout);
3876}
3877
Bjorn A Mellemb689af42019-08-21 10:44:59 -07003878// Tests that the datagram transport to SCTP fallback works correctly when
3879// datagram transport negotiation fails.
3880TEST_P(PeerConnectionIntegrationTest,
3881 DatagramTransportDataChannelFallbackToSctp) {
3882 PeerConnectionInterface::RTCConfiguration rtc_config;
3883 rtc_config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyRequire;
3884 rtc_config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
3885 rtc_config.use_datagram_transport_for_data_channels = true;
3886
3887 // Configure one endpoint to use datagram transport for data channels while
3888 // the other does not.
3889 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
3890 rtc_config, RTCConfiguration(),
3891 loopback_media_transports()->first_factory(), nullptr));
3892 ConnectFakeSignaling();
3893
3894 // The caller offers a data channel using either datagram transport or SCTP.
3895 caller()->CreateDataChannel();
3896 caller()->AddAudioVideoTracks();
3897 callee()->AddAudioVideoTracks();
3898 caller()->CreateAndSetAndSignalOffer();
3899 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3900
3901 // Negotiation should fallback to SCTP, allowing the data channel to be
3902 // established.
3903 ASSERT_NE(nullptr, caller()->data_channel());
3904 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3905 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3906 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3907
3908 // Ensure data can be sent in both directions.
3909 std::string data = "hello world";
3910 caller()->data_channel()->Send(DataBuffer(data));
3911 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3912 kDefaultTimeout);
3913 callee()->data_channel()->Send(DataBuffer(data));
3914 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3915 kDefaultTimeout);
3916
3917 // Ensure that failure of the datagram negotiation doesn't impede media flow.
3918 MediaExpectations media_expectations;
3919 media_expectations.ExpectBidirectionalAudioAndVideo();
3920 ASSERT_TRUE(ExpectNewFrames(media_expectations));
3921}
3922
Bjorn A Mellemfc604aa2019-09-24 14:59:21 -07003923// Tests that the data channel transport works correctly when datagram transport
3924// negotiation succeeds and does not fall back to SCTP.
3925TEST_P(PeerConnectionIntegrationTest,
3926 DatagramTransportDataChannelDoesNotFallbackToSctp) {
3927 PeerConnectionInterface::RTCConfiguration rtc_config;
3928 rtc_config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyRequire;
3929 rtc_config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
3930 rtc_config.use_datagram_transport_for_data_channels = true;
3931
3932 // Configure one endpoint to use datagram transport for data channels while
3933 // the other does not.
3934 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
3935 rtc_config, rtc_config, loopback_media_transports()->first_factory(),
3936 loopback_media_transports()->second_factory()));
3937 ConnectFakeSignaling();
3938
3939 // The caller offers a data channel using either datagram transport or SCTP.
3940 caller()->CreateDataChannel();
3941 caller()->AddAudioVideoTracks();
3942 callee()->AddAudioVideoTracks();
3943 caller()->CreateAndSetAndSignalOffer();
3944 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3945
3946 // Ensure that the data channel transport is ready.
3947 loopback_media_transports()->SetState(webrtc::MediaTransportState::kWritable);
3948 loopback_media_transports()->FlushAsyncInvokes();
3949
3950 // Negotiation should succeed, allowing the data channel to be established.
3951 ASSERT_NE(nullptr, caller()->data_channel());
3952 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3953 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3954 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3955
3956 // Ensure data can be sent in both directions.
3957 std::string data = "hello world";
3958 caller()->data_channel()->Send(DataBuffer(data));
3959 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3960 kDefaultTimeout);
3961 callee()->data_channel()->Send(DataBuffer(data));
3962 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3963 kDefaultTimeout);
3964
3965 // Ensure that failure of the datagram negotiation doesn't impede media flow.
3966 MediaExpectations media_expectations;
3967 media_expectations.ExpectBidirectionalAudioAndVideo();
3968 ASSERT_TRUE(ExpectNewFrames(media_expectations));
3969}
3970
Bjorn A Mellem0cda7b82020-01-28 17:06:55 -08003971// Tests that the datagram transport to SCTP fallback works correctly when
3972// datagram transports do not advertise compatible transport parameters.
3973TEST_P(PeerConnectionIntegrationTest,
3974 DatagramTransportIncompatibleParametersFallsBackToSctp) {
3975 PeerConnectionInterface::RTCConfiguration rtc_config;
3976 rtc_config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyRequire;
3977 rtc_config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
3978 rtc_config.use_datagram_transport_for_data_channels = true;
3979
3980 // By default, only equal parameters are compatible.
3981 loopback_media_transports()->SetFirstDatagramTransportParameters("foo");
3982 loopback_media_transports()->SetSecondDatagramTransportParameters("bar");
3983
3984 // Configure one endpoint to use datagram transport for data channels while
3985 // the other does not.
3986 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
3987 rtc_config, rtc_config, loopback_media_transports()->first_factory(),
3988 loopback_media_transports()->second_factory()));
3989 ConnectFakeSignaling();
3990
3991 // The caller offers a data channel using either datagram transport or SCTP.
3992 caller()->CreateDataChannel();
3993 caller()->AddAudioVideoTracks();
3994 callee()->AddAudioVideoTracks();
3995 caller()->CreateAndSetAndSignalOffer();
3996 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3997
3998 // Negotiation should fallback to SCTP, allowing the data channel to be
3999 // established.
4000 ASSERT_NE(nullptr, caller()->data_channel());
4001 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
4002 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
4003 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
4004
4005 // Both endpoints should agree to use SCTP for data channels.
4006 EXPECT_NE(nullptr, caller()->pc()->GetSctpTransport());
4007 EXPECT_NE(nullptr, callee()->pc()->GetSctpTransport());
4008
4009 // Ensure data can be sent in both directions.
4010 std::string data = "hello world";
4011 caller()->data_channel()->Send(DataBuffer(data));
4012 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
4013 kDefaultTimeout);
4014 callee()->data_channel()->Send(DataBuffer(data));
4015 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
4016 kDefaultTimeout);
4017
4018 // Ensure that failure of the datagram negotiation doesn't impede media flow.
4019 MediaExpectations media_expectations;
4020 media_expectations.ExpectBidirectionalAudioAndVideo();
4021 ASSERT_TRUE(ExpectNewFrames(media_expectations));
4022}
4023
4024// Tests that the datagram transport to SCTP fallback works correctly when
4025// only the answerer believes datagram transport parameters are incompatible.
4026TEST_P(PeerConnectionIntegrationTest,
4027 DatagramTransportIncompatibleParametersOnAnswererFallsBackToSctp) {
4028 PeerConnectionInterface::RTCConfiguration rtc_config;
4029 rtc_config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyRequire;
4030 rtc_config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
4031 rtc_config.use_datagram_transport_for_data_channels = true;
4032
4033 // By default, only equal parameters are compatible.
4034 loopback_media_transports()->SetFirstDatagramTransportParameters("foo");
4035 loopback_media_transports()->SetSecondDatagramTransportParameters("bar");
4036
4037 // Set the offerer to accept different parameters, while the answerer rejects
4038 // them.
4039 loopback_media_transports()->SetFirstDatagramTransportParametersComparison(
4040 [](absl::string_view a, absl::string_view b) { return true; });
4041 loopback_media_transports()->SetSecondDatagramTransportParametersComparison(
4042 [](absl::string_view a, absl::string_view b) { return false; });
4043
4044 // Configure one endpoint to use datagram transport for data channels while
4045 // the other does not.
4046 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
4047 rtc_config, rtc_config, loopback_media_transports()->first_factory(),
4048 loopback_media_transports()->second_factory()));
4049 ConnectFakeSignaling();
4050
4051 // The caller offers a data channel using either datagram transport or SCTP.
4052 caller()->CreateDataChannel();
4053 caller()->AddAudioVideoTracks();
4054 callee()->AddAudioVideoTracks();
4055 caller()->CreateAndSetAndSignalOffer();
4056 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4057
4058 // Negotiation should fallback to SCTP, allowing the data channel to be
4059 // established.
4060 ASSERT_NE(nullptr, caller()->data_channel());
4061 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
4062 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
4063 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
4064
4065 // Both endpoints should agree to use SCTP for data channels.
4066 EXPECT_NE(nullptr, caller()->pc()->GetSctpTransport());
4067 EXPECT_NE(nullptr, callee()->pc()->GetSctpTransport());
4068
4069 // Ensure data can be sent in both directions.
4070 std::string data = "hello world";
4071 caller()->data_channel()->Send(DataBuffer(data));
4072 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
4073 kDefaultTimeout);
4074 callee()->data_channel()->Send(DataBuffer(data));
4075 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
4076 kDefaultTimeout);
4077
4078 // Ensure that failure of the datagram negotiation doesn't impede media flow.
4079 MediaExpectations media_expectations;
4080 media_expectations.ExpectBidirectionalAudioAndVideo();
4081 ASSERT_TRUE(ExpectNewFrames(media_expectations));
4082}
4083
4084// Tests that the data channel transport works correctly when datagram
4085// transports provide different, but compatible, transport parameters.
4086TEST_P(PeerConnectionIntegrationTest,
4087 DatagramTransportCompatibleParametersDoNotFallbackToSctp) {
4088 PeerConnectionInterface::RTCConfiguration rtc_config;
4089 rtc_config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyRequire;
4090 rtc_config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
4091 rtc_config.use_datagram_transport_for_data_channels = true;
4092
4093 // By default, only equal parameters are compatible.
4094 loopback_media_transports()->SetFirstDatagramTransportParameters("foo");
4095 loopback_media_transports()->SetSecondDatagramTransportParameters("bar");
4096
4097 // Change the comparison used to treat these transport parameters are
4098 // compatible (on both sides).
4099 loopback_media_transports()->SetFirstDatagramTransportParametersComparison(
4100 [](absl::string_view a, absl::string_view b) { return true; });
4101 loopback_media_transports()->SetSecondDatagramTransportParametersComparison(
4102 [](absl::string_view a, absl::string_view b) { return true; });
4103
4104 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
4105 rtc_config, rtc_config, loopback_media_transports()->first_factory(),
4106 loopback_media_transports()->second_factory()));
4107 ConnectFakeSignaling();
4108
4109 // The caller offers a data channel using either datagram transport or SCTP.
4110 caller()->CreateDataChannel();
4111 caller()->AddAudioVideoTracks();
4112 callee()->AddAudioVideoTracks();
4113 caller()->CreateAndSetAndSignalOffer();
4114 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4115
4116 // Ensure that the data channel transport is ready.
4117 loopback_media_transports()->SetState(webrtc::MediaTransportState::kWritable);
4118 loopback_media_transports()->FlushAsyncInvokes();
4119
4120 // Negotiation should succeed, allowing the data channel to be established.
4121 ASSERT_NE(nullptr, caller()->data_channel());
4122 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
4123 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
4124 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
4125
4126 // Both endpoints should agree to use datagram transport for data channels.
4127 EXPECT_EQ(nullptr, caller()->pc()->GetSctpTransport());
4128 EXPECT_EQ(nullptr, callee()->pc()->GetSctpTransport());
4129
4130 // Ensure data can be sent in both directions.
4131 std::string data = "hello world";
4132 caller()->data_channel()->Send(DataBuffer(data));
4133 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
4134 kDefaultTimeout);
4135 callee()->data_channel()->Send(DataBuffer(data));
4136 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
4137 kDefaultTimeout);
4138
4139 // Ensure that failure of the datagram negotiation doesn't impede media flow.
4140 MediaExpectations media_expectations;
4141 media_expectations.ExpectBidirectionalAudioAndVideo();
4142 ASSERT_TRUE(ExpectNewFrames(media_expectations));
4143}
4144
Bjorn A Mellem8e1343a2019-09-30 15:12:47 -07004145TEST_P(PeerConnectionIntegrationTest,
4146 DatagramTransportDataChannelWithMediaOnCaller) {
4147 // Configure the caller to attempt use of datagram transport for media and
4148 // data channels.
4149 PeerConnectionInterface::RTCConfiguration offerer_config;
4150 offerer_config.rtcp_mux_policy =
4151 PeerConnectionInterface::kRtcpMuxPolicyRequire;
4152 offerer_config.bundle_policy =
4153 PeerConnectionInterface::kBundlePolicyMaxBundle;
4154 offerer_config.use_datagram_transport_for_data_channels = true;
4155 offerer_config.use_datagram_transport = true;
4156
4157 // Configure the callee to only use datagram transport for data channels.
4158 PeerConnectionInterface::RTCConfiguration answerer_config;
4159 answerer_config.rtcp_mux_policy =
4160 PeerConnectionInterface::kRtcpMuxPolicyRequire;
4161 answerer_config.bundle_policy =
4162 PeerConnectionInterface::kBundlePolicyMaxBundle;
4163 answerer_config.use_datagram_transport_for_data_channels = true;
4164
4165 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
4166 offerer_config, answerer_config,
4167 loopback_media_transports()->first_factory(),
4168 loopback_media_transports()->second_factory()));
4169 ConnectFakeSignaling();
4170
4171 // Offer both media and data.
4172 caller()->AddAudioVideoTracks();
4173 callee()->AddAudioVideoTracks();
4174 caller()->CreateDataChannel();
4175 caller()->CreateAndSetAndSignalOffer();
4176 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4177
4178 // Ensure that the data channel transport is ready.
4179 loopback_media_transports()->SetState(webrtc::MediaTransportState::kWritable);
4180 loopback_media_transports()->FlushAsyncInvokes();
4181
4182 ASSERT_NE(nullptr, caller()->data_channel());
4183 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
4184 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
4185 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
4186
4187 // Both endpoints should agree to use datagram transport for data channels.
4188 EXPECT_EQ(nullptr, caller()->pc()->GetSctpTransport());
4189 EXPECT_EQ(nullptr, callee()->pc()->GetSctpTransport());
4190
4191 // Ensure data can be sent in both directions.
4192 std::string data = "hello world";
4193 caller()->data_channel()->Send(DataBuffer(data));
4194 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
4195 kDefaultTimeout);
4196 callee()->data_channel()->Send(DataBuffer(data));
4197 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
4198 kDefaultTimeout);
4199
4200 // Media flow should not be impacted.
4201 MediaExpectations media_expectations;
4202 media_expectations.ExpectBidirectionalAudioAndVideo();
4203 ASSERT_TRUE(ExpectNewFrames(media_expectations));
4204}
4205
4206TEST_P(PeerConnectionIntegrationTest,
4207 DatagramTransportMediaWithDataChannelOnCaller) {
4208 // Configure the caller to attempt use of datagram transport for media and
4209 // data channels.
4210 PeerConnectionInterface::RTCConfiguration offerer_config;
4211 offerer_config.rtcp_mux_policy =
4212 PeerConnectionInterface::kRtcpMuxPolicyRequire;
4213 offerer_config.bundle_policy =
4214 PeerConnectionInterface::kBundlePolicyMaxBundle;
4215 offerer_config.use_datagram_transport_for_data_channels = true;
4216 offerer_config.use_datagram_transport = true;
4217
4218 // Configure the callee to only use datagram transport for media.
4219 PeerConnectionInterface::RTCConfiguration answerer_config;
4220 answerer_config.rtcp_mux_policy =
4221 PeerConnectionInterface::kRtcpMuxPolicyRequire;
4222 answerer_config.bundle_policy =
4223 PeerConnectionInterface::kBundlePolicyMaxBundle;
4224 answerer_config.use_datagram_transport = true;
4225
4226 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
4227 offerer_config, answerer_config,
4228 loopback_media_transports()->first_factory(),
4229 loopback_media_transports()->second_factory()));
4230 ConnectFakeSignaling();
4231
4232 // Offer both media and data.
4233 caller()->AddAudioVideoTracks();
4234 callee()->AddAudioVideoTracks();
4235 caller()->CreateDataChannel();
4236 caller()->CreateAndSetAndSignalOffer();
4237 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4238
4239 // Ensure that the data channel transport is ready.
4240 loopback_media_transports()->SetState(webrtc::MediaTransportState::kWritable);
4241 loopback_media_transports()->FlushAsyncInvokes();
4242
4243 ASSERT_NE(nullptr, caller()->data_channel());
4244 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
4245 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
4246 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
4247
4248 // Both endpoints should agree to use SCTP for data channels.
4249 EXPECT_NE(nullptr, caller()->pc()->GetSctpTransport());
4250 EXPECT_NE(nullptr, callee()->pc()->GetSctpTransport());
4251
4252 // Ensure data can be sent in both directions.
4253 std::string data = "hello world";
4254 caller()->data_channel()->Send(DataBuffer(data));
4255 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
4256 kDefaultTimeout);
4257 callee()->data_channel()->Send(DataBuffer(data));
4258 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
4259 kDefaultTimeout);
4260
4261 // Media flow should not be impacted.
4262 MediaExpectations media_expectations;
4263 media_expectations.ExpectBidirectionalAudioAndVideo();
4264 ASSERT_TRUE(ExpectNewFrames(media_expectations));
4265}
4266
4267TEST_P(PeerConnectionIntegrationTest,
4268 DatagramTransportDataChannelWithMediaOnCallee) {
4269 // Configure the caller to attempt use of datagram transport for data
4270 // channels.
4271 PeerConnectionInterface::RTCConfiguration offerer_config;
4272 offerer_config.rtcp_mux_policy =
4273 PeerConnectionInterface::kRtcpMuxPolicyRequire;
4274 offerer_config.bundle_policy =
4275 PeerConnectionInterface::kBundlePolicyMaxBundle;
4276 offerer_config.use_datagram_transport_for_data_channels = true;
4277
4278 // Configure the callee to use datagram transport for data channels and media.
4279 PeerConnectionInterface::RTCConfiguration answerer_config;
4280 answerer_config.rtcp_mux_policy =
4281 PeerConnectionInterface::kRtcpMuxPolicyRequire;
4282 answerer_config.bundle_policy =
4283 PeerConnectionInterface::kBundlePolicyMaxBundle;
4284 answerer_config.use_datagram_transport_for_data_channels = true;
4285 answerer_config.use_datagram_transport = true;
4286
4287 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
4288 offerer_config, answerer_config,
4289 loopback_media_transports()->first_factory(),
4290 loopback_media_transports()->second_factory()));
4291 ConnectFakeSignaling();
4292
4293 // Offer both media and data.
4294 caller()->AddAudioVideoTracks();
4295 callee()->AddAudioVideoTracks();
4296 caller()->CreateDataChannel();
4297 caller()->CreateAndSetAndSignalOffer();
4298 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4299
4300 // Ensure that the data channel transport is ready.
4301 loopback_media_transports()->SetState(webrtc::MediaTransportState::kWritable);
4302 loopback_media_transports()->FlushAsyncInvokes();
4303
4304 ASSERT_NE(nullptr, caller()->data_channel());
4305 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
4306 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
4307 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
4308
4309 // Both endpoints should agree to use datagram transport for data channels.
4310 EXPECT_EQ(nullptr, caller()->pc()->GetSctpTransport());
4311 EXPECT_EQ(nullptr, callee()->pc()->GetSctpTransport());
4312
4313 // Ensure data can be sent in both directions.
4314 std::string data = "hello world";
4315 caller()->data_channel()->Send(DataBuffer(data));
4316 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
4317 kDefaultTimeout);
4318 callee()->data_channel()->Send(DataBuffer(data));
4319 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
4320 kDefaultTimeout);
4321
4322 // Media flow should not be impacted.
4323 MediaExpectations media_expectations;
4324 media_expectations.ExpectBidirectionalAudioAndVideo();
4325 ASSERT_TRUE(ExpectNewFrames(media_expectations));
4326}
4327
4328TEST_P(PeerConnectionIntegrationTest,
4329 DatagramTransportMediaWithDataChannelOnCallee) {
4330 // Configure the caller to attempt use of datagram transport for media.
4331 PeerConnectionInterface::RTCConfiguration offerer_config;
4332 offerer_config.rtcp_mux_policy =
4333 PeerConnectionInterface::kRtcpMuxPolicyRequire;
4334 offerer_config.bundle_policy =
4335 PeerConnectionInterface::kBundlePolicyMaxBundle;
4336 offerer_config.use_datagram_transport = true;
4337
4338 // Configure the callee to only use datagram transport for media and data
4339 // channels.
4340 PeerConnectionInterface::RTCConfiguration answerer_config;
4341 answerer_config.rtcp_mux_policy =
4342 PeerConnectionInterface::kRtcpMuxPolicyRequire;
4343 answerer_config.bundle_policy =
4344 PeerConnectionInterface::kBundlePolicyMaxBundle;
4345 answerer_config.use_datagram_transport = true;
4346 answerer_config.use_datagram_transport_for_data_channels = true;
4347
4348 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
4349 offerer_config, answerer_config,
4350 loopback_media_transports()->first_factory(),
4351 loopback_media_transports()->second_factory()));
4352 ConnectFakeSignaling();
4353
4354 // Offer both media and data.
4355 caller()->AddAudioVideoTracks();
4356 callee()->AddAudioVideoTracks();
4357 caller()->CreateDataChannel();
4358 caller()->CreateAndSetAndSignalOffer();
4359 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4360
4361 // Ensure that the data channel transport is ready.
4362 loopback_media_transports()->SetState(webrtc::MediaTransportState::kWritable);
4363 loopback_media_transports()->FlushAsyncInvokes();
4364
4365 ASSERT_NE(nullptr, caller()->data_channel());
4366 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
4367 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
4368 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
4369
4370 // Both endpoints should agree to use SCTP for data channels.
4371 EXPECT_NE(nullptr, caller()->pc()->GetSctpTransport());
4372 EXPECT_NE(nullptr, callee()->pc()->GetSctpTransport());
4373
4374 // Ensure data can be sent in both directions.
4375 std::string data = "hello world";
4376 caller()->data_channel()->Send(DataBuffer(data));
4377 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
4378 kDefaultTimeout);
4379 callee()->data_channel()->Send(DataBuffer(data));
4380 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
4381 kDefaultTimeout);
4382
4383 // Media flow should not be impacted.
4384 MediaExpectations media_expectations;
4385 media_expectations.ExpectBidirectionalAudioAndVideo();
4386 ASSERT_TRUE(ExpectNewFrames(media_expectations));
4387}
4388
4389TEST_P(PeerConnectionIntegrationTest, DatagramTransportDataChannelAndMedia) {
4390 // Configure the caller to use datagram transport for data channels and media.
4391 PeerConnectionInterface::RTCConfiguration offerer_config;
4392 offerer_config.rtcp_mux_policy =
4393 PeerConnectionInterface::kRtcpMuxPolicyRequire;
4394 offerer_config.bundle_policy =
4395 PeerConnectionInterface::kBundlePolicyMaxBundle;
4396 offerer_config.use_datagram_transport_for_data_channels = true;
4397 offerer_config.use_datagram_transport = true;
4398
4399 // Configure the callee to use datagram transport for data channels and media.
4400 PeerConnectionInterface::RTCConfiguration answerer_config;
4401 answerer_config.rtcp_mux_policy =
4402 PeerConnectionInterface::kRtcpMuxPolicyRequire;
4403 answerer_config.bundle_policy =
4404 PeerConnectionInterface::kBundlePolicyMaxBundle;
4405 answerer_config.use_datagram_transport_for_data_channels = true;
4406 answerer_config.use_datagram_transport = true;
4407
4408 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
4409 offerer_config, answerer_config,
4410 loopback_media_transports()->first_factory(),
4411 loopback_media_transports()->second_factory()));
4412 ConnectFakeSignaling();
4413
4414 // Offer both media and data.
4415 caller()->AddAudioVideoTracks();
4416 callee()->AddAudioVideoTracks();
4417 caller()->CreateDataChannel();
4418 caller()->CreateAndSetAndSignalOffer();
4419 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4420
4421 // Ensure that the data channel transport is ready.
4422 loopback_media_transports()->SetState(webrtc::MediaTransportState::kWritable);
4423 loopback_media_transports()->FlushAsyncInvokes();
4424
4425 ASSERT_NE(nullptr, caller()->data_channel());
4426 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
4427 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
4428 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
4429
4430 // Both endpoints should agree to use datagram transport for data channels.
4431 EXPECT_EQ(nullptr, caller()->pc()->GetSctpTransport());
4432 EXPECT_EQ(nullptr, callee()->pc()->GetSctpTransport());
4433
4434 // Ensure data can be sent in both directions.
4435 std::string data = "hello world";
4436 caller()->data_channel()->Send(DataBuffer(data));
4437 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
4438 kDefaultTimeout);
4439 callee()->data_channel()->Send(DataBuffer(data));
4440 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
4441 kDefaultTimeout);
4442
4443 // Media flow should not be impacted.
4444 MediaExpectations media_expectations;
4445 media_expectations.ExpectBidirectionalAudioAndVideo();
4446 ASSERT_TRUE(ExpectNewFrames(media_expectations));
4447}
4448
Bjorn A Mellem7da4e562019-09-26 11:02:11 -07004449// Tests that data channels use SCTP instead of datagram transport if datagram
4450// transport is configured in receive-only mode on the caller.
4451TEST_P(PeerConnectionIntegrationTest,
4452 DatagramTransportDataChannelReceiveOnlyOnCallerUsesSctp) {
4453 PeerConnectionInterface::RTCConfiguration rtc_config;
4454 rtc_config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyRequire;
4455 rtc_config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
4456 rtc_config.use_datagram_transport_for_data_channels = true;
4457 rtc_config.use_datagram_transport_for_data_channels_receive_only = true;
4458
4459 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
4460 rtc_config, rtc_config, loopback_media_transports()->first_factory(),
4461 loopback_media_transports()->second_factory()));
4462 ConnectFakeSignaling();
4463
4464 // The caller should offer a data channel using SCTP.
4465 caller()->CreateDataChannel();
4466 caller()->AddAudioVideoTracks();
4467 callee()->AddAudioVideoTracks();
4468 caller()->CreateAndSetAndSignalOffer();
4469 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4470
4471 ASSERT_NE(nullptr, caller()->data_channel());
4472 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
4473 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
4474 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
4475
4476 // SCTP transports should be present, since they are in use.
4477 EXPECT_NE(caller()->pc()->GetSctpTransport(), nullptr);
4478 EXPECT_NE(callee()->pc()->GetSctpTransport(), nullptr);
4479
4480 // Ensure data can be sent in both directions.
4481 std::string data = "hello world";
4482 caller()->data_channel()->Send(DataBuffer(data));
4483 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
4484 kDefaultTimeout);
4485 callee()->data_channel()->Send(DataBuffer(data));
4486 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
4487 kDefaultTimeout);
4488}
4489
deadbeef1dcb1642017-03-29 21:08:16 -07004490#endif // HAVE_SCTP
4491
Bjorn A Mellem7da4e562019-09-26 11:02:11 -07004492// Tests that a callee configured for receive-only use of datagram transport
4493// data channels accepts them on incoming calls.
4494TEST_P(PeerConnectionIntegrationTest,
4495 DatagramTransportDataChannelReceiveOnlyOnCallee) {
4496 PeerConnectionInterface::RTCConfiguration offerer_config;
4497 offerer_config.rtcp_mux_policy =
4498 PeerConnectionInterface::kRtcpMuxPolicyRequire;
4499 offerer_config.bundle_policy =
4500 PeerConnectionInterface::kBundlePolicyMaxBundle;
4501 offerer_config.use_datagram_transport_for_data_channels = true;
4502
4503 PeerConnectionInterface::RTCConfiguration answerer_config;
4504 answerer_config.rtcp_mux_policy =
4505 PeerConnectionInterface::kRtcpMuxPolicyRequire;
4506 answerer_config.bundle_policy =
4507 PeerConnectionInterface::kBundlePolicyMaxBundle;
4508 answerer_config.use_datagram_transport_for_data_channels = true;
4509 answerer_config.use_datagram_transport_for_data_channels_receive_only = true;
4510
4511 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
4512 offerer_config, answerer_config,
4513 loopback_media_transports()->first_factory(),
4514 loopback_media_transports()->second_factory()));
4515 ConnectFakeSignaling();
4516
4517 caller()->CreateDataChannel();
4518 caller()->CreateAndSetAndSignalOffer();
4519 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4520
4521 // Ensure that the data channel transport is ready.
4522 loopback_media_transports()->SetState(webrtc::MediaTransportState::kWritable);
4523 loopback_media_transports()->FlushAsyncInvokes();
4524
4525 ASSERT_NE(nullptr, caller()->data_channel());
4526 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
4527 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
4528 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
4529
4530 // SCTP transports should not be present, since datagram transport is used.
4531 EXPECT_EQ(caller()->pc()->GetSctpTransport(), nullptr);
4532 EXPECT_EQ(callee()->pc()->GetSctpTransport(), nullptr);
4533
4534 // Ensure data can be sent in both directions.
4535 std::string data = "hello world";
4536 caller()->data_channel()->Send(DataBuffer(data));
4537 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
4538 kDefaultTimeout);
4539 callee()->data_channel()->Send(DataBuffer(data));
4540 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
4541 kDefaultTimeout);
4542}
4543
Bjorn A Mellemb689af42019-08-21 10:44:59 -07004544// This test sets up a call between two parties with a datagram transport data
4545// channel.
4546TEST_P(PeerConnectionIntegrationTest, DatagramTransportDataChannelEndToEnd) {
4547 PeerConnectionInterface::RTCConfiguration rtc_config;
4548 rtc_config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyRequire;
4549 rtc_config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
4550 rtc_config.use_datagram_transport_for_data_channels = true;
4551 rtc_config.enable_dtls_srtp = false;
4552 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
4553 rtc_config, rtc_config, loopback_media_transports()->first_factory(),
4554 loopback_media_transports()->second_factory()));
4555 ConnectFakeSignaling();
4556
4557 // Expect that data channel created on caller side will show up for callee as
4558 // well.
4559 caller()->CreateDataChannel();
4560 caller()->CreateAndSetAndSignalOffer();
4561 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4562
Bjorn A Mellemfc604aa2019-09-24 14:59:21 -07004563 // Ensure that the data channel transport is ready.
Bjorn A Mellemb689af42019-08-21 10:44:59 -07004564 loopback_media_transports()->SetState(webrtc::MediaTransportState::kWritable);
4565 loopback_media_transports()->FlushAsyncInvokes();
4566
4567 // Caller data channel should already exist (it created one). Callee data
4568 // channel may not exist yet, since negotiation happens in-band, not in SDP.
4569 ASSERT_NE(nullptr, caller()->data_channel());
4570 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
4571 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
4572 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
4573
4574 // Ensure data can be sent in both directions.
4575 std::string data = "hello world";
4576 caller()->data_channel()->Send(DataBuffer(data));
4577 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
4578 kDefaultTimeout);
4579 callee()->data_channel()->Send(DataBuffer(data));
4580 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
4581 kDefaultTimeout);
4582}
4583
Bjorn A Mellembc3eebc2019-09-23 14:53:54 -07004584// Tests that 'zero-rtt' data channel transports (which are ready-to-send as
4585// soon as they're created) work correctly.
4586TEST_P(PeerConnectionIntegrationTest, DatagramTransportDataChannelZeroRtt) {
4587 PeerConnectionInterface::RTCConfiguration rtc_config;
4588 rtc_config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyRequire;
4589 rtc_config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
4590 rtc_config.use_datagram_transport_for_data_channels = true;
4591 rtc_config.enable_dtls_srtp = false; // SDES is required for media transport.
4592 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
4593 rtc_config, rtc_config, loopback_media_transports()->first_factory(),
4594 loopback_media_transports()->second_factory()));
4595 ConnectFakeSignaling();
4596
4597 // Ensure that the callee's media transport is ready-to-send immediately.
4598 // Note that only the callee can become writable in zero RTTs. The caller
4599 // must wait for the callee's answer.
4600 loopback_media_transports()->SetSecondStateAfterConnect(
4601 webrtc::MediaTransportState::kWritable);
4602 loopback_media_transports()->FlushAsyncInvokes();
4603
4604 // Expect that data channel created on caller side will show up for callee as
4605 // well.
4606 caller()->CreateDataChannel();
4607 caller()->CreateAndSetAndSignalOffer();
4608 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4609
4610 loopback_media_transports()->SetFirstState(
4611 webrtc::MediaTransportState::kWritable);
4612 loopback_media_transports()->FlushAsyncInvokes();
4613
4614 // Caller data channel should already exist (it created one). Callee data
4615 // channel may not exist yet, since negotiation happens in-band, not in SDP.
4616 ASSERT_NE(nullptr, caller()->data_channel());
4617 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
4618 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
4619 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
4620
4621 // Ensure data can be sent in both directions.
4622 std::string data = "hello world";
4623 caller()->data_channel()->Send(DataBuffer(data));
4624 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
4625 kDefaultTimeout);
4626 callee()->data_channel()->Send(DataBuffer(data));
4627 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
4628 kDefaultTimeout);
4629}
4630
Bjorn A Mellemb689af42019-08-21 10:44:59 -07004631// Ensures that when the callee closes a datagram transport data channel, the
4632// closing procedure results in the data channel being closed for the caller
4633// as well.
4634TEST_P(PeerConnectionIntegrationTest,
4635 DatagramTransportDataChannelCalleeCloses) {
4636 PeerConnectionInterface::RTCConfiguration rtc_config;
4637 rtc_config.use_datagram_transport_for_data_channels = true;
4638 rtc_config.enable_dtls_srtp = false;
4639 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
4640 rtc_config, rtc_config, loopback_media_transports()->first_factory(),
4641 loopback_media_transports()->second_factory()));
4642 ConnectFakeSignaling();
4643
4644 // Create a data channel on the caller and signal it to the callee.
4645 caller()->CreateDataChannel();
4646 caller()->CreateAndSetAndSignalOffer();
4647 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4648
Bjorn A Mellemfc604aa2019-09-24 14:59:21 -07004649 // Ensure that the data channel transport is ready.
Bjorn A Mellemb689af42019-08-21 10:44:59 -07004650 loopback_media_transports()->SetState(webrtc::MediaTransportState::kWritable);
4651 loopback_media_transports()->FlushAsyncInvokes();
4652
4653 // Data channels exist and open on both ends of the connection.
4654 ASSERT_NE(nullptr, caller()->data_channel());
4655 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
4656 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
4657 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
4658
4659 // Close the data channel on the callee side, and wait for it to reach the
4660 // "closed" state on both sides.
4661 callee()->data_channel()->Close();
4662 EXPECT_TRUE_WAIT(!caller()->data_observer()->IsOpen(), kDefaultTimeout);
4663 EXPECT_TRUE_WAIT(!callee()->data_observer()->IsOpen(), kDefaultTimeout);
4664}
4665
4666// Tests that datagram transport data channels can do in-band negotiation.
4667TEST_P(PeerConnectionIntegrationTest,
4668 DatagramTransportDataChannelConfigSentToOtherSide) {
4669 PeerConnectionInterface::RTCConfiguration rtc_config;
4670 rtc_config.use_datagram_transport_for_data_channels = true;
4671 rtc_config.enable_dtls_srtp = false;
4672 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
4673 rtc_config, rtc_config, loopback_media_transports()->first_factory(),
4674 loopback_media_transports()->second_factory()));
4675 ConnectFakeSignaling();
4676
4677 // Create a data channel with a non-default configuration and signal it to the
4678 // callee.
4679 webrtc::DataChannelInit init;
4680 init.id = 53;
4681 init.maxRetransmits = 52;
4682 caller()->CreateDataChannel("data-channel", &init);
4683 caller()->CreateAndSetAndSignalOffer();
4684 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4685
Bjorn A Mellemfc604aa2019-09-24 14:59:21 -07004686 // Ensure that the data channel transport is ready.
Bjorn A Mellemb689af42019-08-21 10:44:59 -07004687 loopback_media_transports()->SetState(webrtc::MediaTransportState::kWritable);
4688 loopback_media_transports()->FlushAsyncInvokes();
4689
4690 // Ensure that the data channel exists on the callee with the correct
4691 // configuration.
4692 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
4693 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
4694 // Since "negotiate" is false, the "id" parameter is ignored.
4695 EXPECT_NE(init.id, callee()->data_channel()->id());
4696 EXPECT_EQ("data-channel", callee()->data_channel()->label());
4697 EXPECT_EQ(init.maxRetransmits, callee()->data_channel()->maxRetransmits());
4698 EXPECT_FALSE(callee()->data_channel()->negotiated());
4699}
4700
4701TEST_P(PeerConnectionIntegrationTest,
4702 DatagramTransportDataChannelRejectedWithNoFallback) {
4703 PeerConnectionInterface::RTCConfiguration offerer_config;
4704 offerer_config.rtcp_mux_policy =
4705 PeerConnectionInterface::kRtcpMuxPolicyRequire;
4706 offerer_config.bundle_policy =
4707 PeerConnectionInterface::kBundlePolicyMaxBundle;
4708 offerer_config.use_datagram_transport_for_data_channels = true;
4709 // Disabling DTLS precludes a fallback to SCTP.
4710 offerer_config.enable_dtls_srtp = false;
4711
4712 PeerConnectionInterface::RTCConfiguration answerer_config;
4713 answerer_config.rtcp_mux_policy =
4714 PeerConnectionInterface::kRtcpMuxPolicyRequire;
4715 answerer_config.bundle_policy =
4716 PeerConnectionInterface::kBundlePolicyMaxBundle;
4717 // Both endpoints must disable DTLS or SetRemoteDescription will fail.
4718 answerer_config.enable_dtls_srtp = false;
4719
4720 // Configure one endpoint to use datagram transport for data channels while
4721 // the other does not.
4722 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
4723 offerer_config, answerer_config,
4724 loopback_media_transports()->first_factory(), nullptr));
4725 ConnectFakeSignaling();
4726
4727 // The caller offers a data channel using either datagram transport or SCTP.
4728 caller()->CreateDataChannel();
4729 caller()->AddAudioVideoTracks();
4730 callee()->AddAudioVideoTracks();
4731 caller()->CreateAndSetAndSignalOffer();
4732 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4733
4734 // Caller data channel should already exist (it created one). Callee data
4735 // channel should not exist, since negotiation happens in-band, not in SDP.
4736 EXPECT_NE(nullptr, caller()->data_channel());
4737 EXPECT_EQ(nullptr, callee()->data_channel());
4738
4739 // The caller's data channel should close when the datagram transport is
4740 // rejected.
4741 EXPECT_FALSE(caller()->data_observer()->IsOpen());
4742
4743 // Media flow should not be impacted by the failed data channel.
4744 MediaExpectations media_expectations;
4745 media_expectations.ExpectBidirectionalAudioAndVideo();
4746 ASSERT_TRUE(ExpectNewFrames(media_expectations));
4747}
4748
deadbeef1dcb1642017-03-29 21:08:16 -07004749// Test that the ICE connection and gathering states eventually reach
4750// "complete".
Seth Hampson2f0d7022018-02-20 11:54:42 -08004751TEST_P(PeerConnectionIntegrationTest, IceStatesReachCompletion) {
deadbeef1dcb1642017-03-29 21:08:16 -07004752 ASSERT_TRUE(CreatePeerConnectionWrappers());
4753 ConnectFakeSignaling();
4754 // Do normal offer/answer.
Steve Anton15324772018-01-16 10:26:49 -08004755 caller()->AddAudioVideoTracks();
4756 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07004757 caller()->CreateAndSetAndSignalOffer();
4758 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4759 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceGatheringComplete,
4760 caller()->ice_gathering_state(), kMaxWaitForFramesMs);
4761 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceGatheringComplete,
4762 callee()->ice_gathering_state(), kMaxWaitForFramesMs);
4763 // After the best candidate pair is selected and all candidates are signaled,
4764 // the ICE connection state should reach "complete".
4765 // TODO(deadbeef): Currently, the ICE "controlled" agent (the
4766 // answerer/"callee" by default) only reaches "connected". When this is
4767 // fixed, this test should be updated.
4768 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
4769 caller()->ice_connection_state(), kDefaultTimeout);
Alex Loiko9289eda2018-11-23 16:18:59 +00004770 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
4771 callee()->ice_connection_state(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07004772}
4773
Qingsi Wang1dac6d82018-12-12 15:28:47 -08004774constexpr int kOnlyLocalPorts = cricket::PORTALLOCATOR_DISABLE_STUN |
4775 cricket::PORTALLOCATOR_DISABLE_RELAY |
4776 cricket::PORTALLOCATOR_DISABLE_TCP;
Zach Stein6fcdc2f2018-08-23 16:25:55 -07004777
Qingsi Wang1dac6d82018-12-12 15:28:47 -08004778// Use a mock resolver to resolve the hostname back to the original IP on both
4779// sides and check that the ICE connection connects.
Zach Stein6fcdc2f2018-08-23 16:25:55 -07004780TEST_P(PeerConnectionIntegrationTest,
4781 IceStatesReachCompletionWithRemoteHostname) {
Qingsi Wang1dac6d82018-12-12 15:28:47 -08004782 auto caller_resolver_factory =
Mirko Bonadei317a1f02019-09-17 17:06:18 +02004783 std::make_unique<NiceMock<webrtc::MockAsyncResolverFactory>>();
Qingsi Wang1dac6d82018-12-12 15:28:47 -08004784 auto callee_resolver_factory =
Mirko Bonadei317a1f02019-09-17 17:06:18 +02004785 std::make_unique<NiceMock<webrtc::MockAsyncResolverFactory>>();
Qingsi Wang1dac6d82018-12-12 15:28:47 -08004786 NiceMock<rtc::MockAsyncResolver> callee_async_resolver;
4787 NiceMock<rtc::MockAsyncResolver> caller_async_resolver;
Zach Stein6fcdc2f2018-08-23 16:25:55 -07004788
4789 // This also verifies that the injected AsyncResolverFactory is used by
4790 // P2PTransportChannel.
Qingsi Wang1dac6d82018-12-12 15:28:47 -08004791 EXPECT_CALL(*caller_resolver_factory, Create())
4792 .WillOnce(Return(&caller_async_resolver));
4793 webrtc::PeerConnectionDependencies caller_deps(nullptr);
4794 caller_deps.async_resolver_factory = std::move(caller_resolver_factory);
4795
4796 EXPECT_CALL(*callee_resolver_factory, Create())
4797 .WillOnce(Return(&callee_async_resolver));
4798 webrtc::PeerConnectionDependencies callee_deps(nullptr);
4799 callee_deps.async_resolver_factory = std::move(callee_resolver_factory);
4800
4801 PeerConnectionInterface::RTCConfiguration config;
4802 config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
4803 config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyRequire;
4804
4805 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndDeps(
4806 config, std::move(caller_deps), config, std::move(callee_deps)));
4807
4808 caller()->SetRemoteAsyncResolver(&callee_async_resolver);
4809 callee()->SetRemoteAsyncResolver(&caller_async_resolver);
4810
4811 // Enable hostname candidates with mDNS names.
Qingsi Wangecd30542019-05-22 14:34:56 -07004812 caller()->SetMdnsResponder(
Mirko Bonadei317a1f02019-09-17 17:06:18 +02004813 std::make_unique<webrtc::FakeMdnsResponder>(network_thread()));
Qingsi Wangecd30542019-05-22 14:34:56 -07004814 callee()->SetMdnsResponder(
Mirko Bonadei317a1f02019-09-17 17:06:18 +02004815 std::make_unique<webrtc::FakeMdnsResponder>(network_thread()));
Qingsi Wang1dac6d82018-12-12 15:28:47 -08004816
4817 SetPortAllocatorFlags(kOnlyLocalPorts, kOnlyLocalPorts);
Zach Stein6fcdc2f2018-08-23 16:25:55 -07004818
4819 ConnectFakeSignaling();
4820 caller()->AddAudioVideoTracks();
4821 callee()->AddAudioVideoTracks();
4822 caller()->CreateAndSetAndSignalOffer();
4823 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4824 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
4825 caller()->ice_connection_state(), kDefaultTimeout);
4826 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
4827 callee()->ice_connection_state(), kDefaultTimeout);
Jeroen de Borst833979f2018-12-13 08:25:54 -08004828
Ying Wangef3998f2019-12-09 13:06:53 +01004829 EXPECT_METRIC_EQ(1, webrtc::metrics::NumEvents(
4830 "WebRTC.PeerConnection.CandidatePairType_UDP",
4831 webrtc::kIceCandidatePairHostNameHostName));
Zach Stein6fcdc2f2018-08-23 16:25:55 -07004832}
4833
Steve Antonede9ca52017-10-16 13:04:27 -07004834// Test that firewalling the ICE connection causes the clients to identify the
4835// disconnected state and then removing the firewall causes them to reconnect.
4836class PeerConnectionIntegrationIceStatesTest
Seth Hampson2f0d7022018-02-20 11:54:42 -08004837 : public PeerConnectionIntegrationBaseTest,
4838 public ::testing::WithParamInterface<
4839 std::tuple<SdpSemantics, std::tuple<std::string, uint32_t>>> {
Steve Antonede9ca52017-10-16 13:04:27 -07004840 protected:
Seth Hampson2f0d7022018-02-20 11:54:42 -08004841 PeerConnectionIntegrationIceStatesTest()
4842 : PeerConnectionIntegrationBaseTest(std::get<0>(GetParam())) {
4843 port_allocator_flags_ = std::get<1>(std::get<1>(GetParam()));
Steve Antonede9ca52017-10-16 13:04:27 -07004844 }
4845
4846 void StartStunServer(const SocketAddress& server_address) {
4847 stun_server_.reset(
4848 cricket::TestStunServer::Create(network_thread(), server_address));
4849 }
4850
4851 bool TestIPv6() {
4852 return (port_allocator_flags_ & cricket::PORTALLOCATOR_ENABLE_IPV6);
4853 }
4854
4855 void SetPortAllocatorFlags() {
Qingsi Wang1dac6d82018-12-12 15:28:47 -08004856 PeerConnectionIntegrationBaseTest::SetPortAllocatorFlags(
4857 port_allocator_flags_, port_allocator_flags_);
Steve Antonede9ca52017-10-16 13:04:27 -07004858 }
4859
4860 std::vector<SocketAddress> CallerAddresses() {
4861 std::vector<SocketAddress> addresses;
4862 addresses.push_back(SocketAddress("1.1.1.1", 0));
4863 if (TestIPv6()) {
4864 addresses.push_back(SocketAddress("1111:0:a:b:c:d:e:f", 0));
4865 }
4866 return addresses;
4867 }
4868
4869 std::vector<SocketAddress> CalleeAddresses() {
4870 std::vector<SocketAddress> addresses;
4871 addresses.push_back(SocketAddress("2.2.2.2", 0));
4872 if (TestIPv6()) {
4873 addresses.push_back(SocketAddress("2222:0:a:b:c:d:e:f", 0));
4874 }
4875 return addresses;
4876 }
4877
4878 void SetUpNetworkInterfaces() {
4879 // Remove the default interfaces added by the test infrastructure.
Qingsi Wangecd30542019-05-22 14:34:56 -07004880 caller()->network_manager()->RemoveInterface(kDefaultLocalAddress);
4881 callee()->network_manager()->RemoveInterface(kDefaultLocalAddress);
Steve Antonede9ca52017-10-16 13:04:27 -07004882
4883 // Add network addresses for test.
4884 for (const auto& caller_address : CallerAddresses()) {
Qingsi Wangecd30542019-05-22 14:34:56 -07004885 caller()->network_manager()->AddInterface(caller_address);
Steve Antonede9ca52017-10-16 13:04:27 -07004886 }
4887 for (const auto& callee_address : CalleeAddresses()) {
Qingsi Wangecd30542019-05-22 14:34:56 -07004888 callee()->network_manager()->AddInterface(callee_address);
Steve Antonede9ca52017-10-16 13:04:27 -07004889 }
4890 }
4891
4892 private:
4893 uint32_t port_allocator_flags_;
4894 std::unique_ptr<cricket::TestStunServer> stun_server_;
4895};
4896
Yves Gerey100fe632020-01-17 19:15:53 +01004897// Ensure FakeClockForTest is constructed first (see class for rationale).
4898class PeerConnectionIntegrationIceStatesTestWithFakeClock
4899 : public FakeClockForTest,
4900 public PeerConnectionIntegrationIceStatesTest {};
4901
Steve Antonede9ca52017-10-16 13:04:27 -07004902// Tests that the PeerConnection goes through all the ICE gathering/connection
4903// states over the duration of the call. This includes Disconnected and Failed
4904// states, induced by putting a firewall between the peers and waiting for them
4905// to time out.
Yves Gerey100fe632020-01-17 19:15:53 +01004906TEST_P(PeerConnectionIntegrationIceStatesTestWithFakeClock, VerifyIceStates) {
Steve Antonede9ca52017-10-16 13:04:27 -07004907 const SocketAddress kStunServerAddress =
4908 SocketAddress("99.99.99.1", cricket::STUN_SERVER_PORT);
4909 StartStunServer(kStunServerAddress);
4910
4911 PeerConnectionInterface::RTCConfiguration config;
4912 PeerConnectionInterface::IceServer ice_stun_server;
4913 ice_stun_server.urls.push_back(
4914 "stun:" + kStunServerAddress.HostAsURIString() + ":" +
4915 kStunServerAddress.PortAsString());
4916 config.servers.push_back(ice_stun_server);
4917
4918 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
4919 ConnectFakeSignaling();
4920 SetPortAllocatorFlags();
4921 SetUpNetworkInterfaces();
Steve Anton15324772018-01-16 10:26:49 -08004922 caller()->AddAudioVideoTracks();
4923 callee()->AddAudioVideoTracks();
Steve Antonede9ca52017-10-16 13:04:27 -07004924
4925 // Initial state before anything happens.
4926 ASSERT_EQ(PeerConnectionInterface::kIceGatheringNew,
4927 caller()->ice_gathering_state());
4928 ASSERT_EQ(PeerConnectionInterface::kIceConnectionNew,
4929 caller()->ice_connection_state());
Jonas Olsson7a6739e2019-01-15 16:31:55 +01004930 ASSERT_EQ(PeerConnectionInterface::kIceConnectionNew,
4931 caller()->standardized_ice_connection_state());
Steve Antonede9ca52017-10-16 13:04:27 -07004932
4933 // Start the call by creating the offer, setting it as the local description,
4934 // then sending it to the peer who will respond with an answer. This happens
4935 // asynchronously so that we can watch the states as it runs in the
4936 // background.
4937 caller()->CreateAndSetAndSignalOffer();
4938
Steve Antona9b67ce2020-01-16 14:00:44 -08004939 ASSERT_EQ_SIMULATED_WAIT(PeerConnectionInterface::kIceConnectionCompleted,
4940 caller()->ice_connection_state(), kDefaultTimeout,
Yves Gerey100fe632020-01-17 19:15:53 +01004941 FakeClock());
Steve Antona9b67ce2020-01-16 14:00:44 -08004942 ASSERT_EQ_SIMULATED_WAIT(PeerConnectionInterface::kIceConnectionCompleted,
4943 caller()->standardized_ice_connection_state(),
Yves Gerey100fe632020-01-17 19:15:53 +01004944 kDefaultTimeout, FakeClock());
Steve Antonede9ca52017-10-16 13:04:27 -07004945
4946 // Verify that the observer was notified of the intermediate transitions.
4947 EXPECT_THAT(caller()->ice_connection_state_history(),
4948 ElementsAre(PeerConnectionInterface::kIceConnectionChecking,
4949 PeerConnectionInterface::kIceConnectionConnected,
4950 PeerConnectionInterface::kIceConnectionCompleted));
Jonas Olssonacd8ae72019-02-25 15:26:24 +01004951 EXPECT_THAT(caller()->standardized_ice_connection_state_history(),
4952 ElementsAre(PeerConnectionInterface::kIceConnectionChecking,
4953 PeerConnectionInterface::kIceConnectionConnected,
4954 PeerConnectionInterface::kIceConnectionCompleted));
Jonas Olsson635474e2018-10-18 15:58:17 +02004955 EXPECT_THAT(
4956 caller()->peer_connection_state_history(),
4957 ElementsAre(PeerConnectionInterface::PeerConnectionState::kConnecting,
Jonas Olsson635474e2018-10-18 15:58:17 +02004958 PeerConnectionInterface::PeerConnectionState::kConnected));
Steve Antonede9ca52017-10-16 13:04:27 -07004959 EXPECT_THAT(caller()->ice_gathering_state_history(),
4960 ElementsAre(PeerConnectionInterface::kIceGatheringGathering,
4961 PeerConnectionInterface::kIceGatheringComplete));
4962
4963 // Block connections to/from the caller and wait for ICE to become
4964 // disconnected.
4965 for (const auto& caller_address : CallerAddresses()) {
4966 firewall()->AddRule(false, rtc::FP_ANY, rtc::FD_ANY, caller_address);
4967 }
Mirko Bonadei675513b2017-11-09 11:09:25 +01004968 RTC_LOG(LS_INFO) << "Firewall rules applied";
Jonas Olssonb75d9e92019-02-22 10:33:29 +01004969 ASSERT_EQ_SIMULATED_WAIT(PeerConnectionInterface::kIceConnectionDisconnected,
4970 caller()->ice_connection_state(), kDefaultTimeout,
Yves Gerey100fe632020-01-17 19:15:53 +01004971 FakeClock());
Jonas Olssonb75d9e92019-02-22 10:33:29 +01004972 ASSERT_EQ_SIMULATED_WAIT(PeerConnectionInterface::kIceConnectionDisconnected,
4973 caller()->standardized_ice_connection_state(),
Yves Gerey100fe632020-01-17 19:15:53 +01004974 kDefaultTimeout, FakeClock());
Steve Antonede9ca52017-10-16 13:04:27 -07004975
4976 // Let ICE re-establish by removing the firewall rules.
4977 firewall()->ClearRules();
Mirko Bonadei675513b2017-11-09 11:09:25 +01004978 RTC_LOG(LS_INFO) << "Firewall rules cleared";
Jonas Olssonb75d9e92019-02-22 10:33:29 +01004979 ASSERT_EQ_SIMULATED_WAIT(PeerConnectionInterface::kIceConnectionCompleted,
4980 caller()->ice_connection_state(), kDefaultTimeout,
Yves Gerey100fe632020-01-17 19:15:53 +01004981 FakeClock());
Jonas Olssonacd8ae72019-02-25 15:26:24 +01004982 ASSERT_EQ_SIMULATED_WAIT(PeerConnectionInterface::kIceConnectionCompleted,
Jonas Olssonb75d9e92019-02-22 10:33:29 +01004983 caller()->standardized_ice_connection_state(),
Yves Gerey100fe632020-01-17 19:15:53 +01004984 kDefaultTimeout, FakeClock());
Steve Antonede9ca52017-10-16 13:04:27 -07004985
4986 // According to RFC7675, if there is no response within 30 seconds then the
4987 // peer should consider the other side to have rejected the connection. This
Steve Anton83119dd2017-11-10 16:19:52 -08004988 // is signaled by the state transitioning to "failed".
Steve Antonede9ca52017-10-16 13:04:27 -07004989 constexpr int kConsentTimeout = 30000;
4990 for (const auto& caller_address : CallerAddresses()) {
4991 firewall()->AddRule(false, rtc::FP_ANY, rtc::FD_ANY, caller_address);
4992 }
Mirko Bonadei675513b2017-11-09 11:09:25 +01004993 RTC_LOG(LS_INFO) << "Firewall rules applied again";
Jonas Olssonb75d9e92019-02-22 10:33:29 +01004994 ASSERT_EQ_SIMULATED_WAIT(PeerConnectionInterface::kIceConnectionFailed,
4995 caller()->ice_connection_state(), kConsentTimeout,
Yves Gerey100fe632020-01-17 19:15:53 +01004996 FakeClock());
Jonas Olssonb75d9e92019-02-22 10:33:29 +01004997 ASSERT_EQ_SIMULATED_WAIT(PeerConnectionInterface::kIceConnectionFailed,
4998 caller()->standardized_ice_connection_state(),
Yves Gerey100fe632020-01-17 19:15:53 +01004999 kConsentTimeout, FakeClock());
Jonas Olssonb75d9e92019-02-22 10:33:29 +01005000}
5001
5002// Tests that if the connection doesn't get set up properly we eventually reach
5003// the "failed" iceConnectionState.
Yves Gerey100fe632020-01-17 19:15:53 +01005004TEST_P(PeerConnectionIntegrationIceStatesTestWithFakeClock,
5005 IceStateSetupFailure) {
Jonas Olssonb75d9e92019-02-22 10:33:29 +01005006 // Block connections to/from the caller and wait for ICE to become
5007 // disconnected.
5008 for (const auto& caller_address : CallerAddresses()) {
5009 firewall()->AddRule(false, rtc::FP_ANY, rtc::FD_ANY, caller_address);
5010 }
5011
5012 ASSERT_TRUE(CreatePeerConnectionWrappers());
5013 ConnectFakeSignaling();
5014 SetPortAllocatorFlags();
5015 SetUpNetworkInterfaces();
5016 caller()->AddAudioVideoTracks();
5017 caller()->CreateAndSetAndSignalOffer();
5018
5019 // According to RFC7675, if there is no response within 30 seconds then the
5020 // peer should consider the other side to have rejected the connection. This
5021 // is signaled by the state transitioning to "failed".
5022 constexpr int kConsentTimeout = 30000;
5023 ASSERT_EQ_SIMULATED_WAIT(PeerConnectionInterface::kIceConnectionFailed,
5024 caller()->standardized_ice_connection_state(),
Yves Gerey100fe632020-01-17 19:15:53 +01005025 kConsentTimeout, FakeClock());
Steve Antonede9ca52017-10-16 13:04:27 -07005026}
5027
5028// Tests that the best connection is set to the appropriate IPv4/IPv6 connection
5029// and that the statistics in the metric observers are updated correctly.
5030TEST_P(PeerConnectionIntegrationIceStatesTest, VerifyBestConnection) {
5031 ASSERT_TRUE(CreatePeerConnectionWrappers());
5032 ConnectFakeSignaling();
5033 SetPortAllocatorFlags();
5034 SetUpNetworkInterfaces();
Steve Anton15324772018-01-16 10:26:49 -08005035 caller()->AddAudioVideoTracks();
5036 callee()->AddAudioVideoTracks();
Steve Antonede9ca52017-10-16 13:04:27 -07005037 caller()->CreateAndSetAndSignalOffer();
5038
5039 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Steve Anton692f3c72020-01-16 14:12:31 -08005040 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
5041 caller()->ice_connection_state(), kDefaultTimeout);
5042 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
5043 callee()->ice_connection_state(), kDefaultTimeout);
Steve Antonede9ca52017-10-16 13:04:27 -07005044
Qingsi Wang7fc821d2018-07-12 12:54:53 -07005045 // TODO(bugs.webrtc.org/9456): Fix it.
5046 const int num_best_ipv4 = webrtc::metrics::NumEvents(
5047 "WebRTC.PeerConnection.IPMetrics", webrtc::kBestConnections_IPv4);
5048 const int num_best_ipv6 = webrtc::metrics::NumEvents(
5049 "WebRTC.PeerConnection.IPMetrics", webrtc::kBestConnections_IPv6);
Steve Antonede9ca52017-10-16 13:04:27 -07005050 if (TestIPv6()) {
5051 // When IPv6 is enabled, we should prefer an IPv6 connection over an IPv4
5052 // connection.
Ying Wangef3998f2019-12-09 13:06:53 +01005053 EXPECT_METRIC_EQ(0, num_best_ipv4);
5054 EXPECT_METRIC_EQ(1, num_best_ipv6);
Steve Antonede9ca52017-10-16 13:04:27 -07005055 } else {
Ying Wangef3998f2019-12-09 13:06:53 +01005056 EXPECT_METRIC_EQ(1, num_best_ipv4);
5057 EXPECT_METRIC_EQ(0, num_best_ipv6);
Steve Antonede9ca52017-10-16 13:04:27 -07005058 }
5059
Ying Wangef3998f2019-12-09 13:06:53 +01005060 EXPECT_METRIC_EQ(0, webrtc::metrics::NumEvents(
5061 "WebRTC.PeerConnection.CandidatePairType_UDP",
5062 webrtc::kIceCandidatePairHostHost));
5063 EXPECT_METRIC_EQ(1, webrtc::metrics::NumEvents(
5064 "WebRTC.PeerConnection.CandidatePairType_UDP",
5065 webrtc::kIceCandidatePairHostPublicHostPublic));
Steve Antonede9ca52017-10-16 13:04:27 -07005066}
5067
5068constexpr uint32_t kFlagsIPv4NoStun = cricket::PORTALLOCATOR_DISABLE_TCP |
5069 cricket::PORTALLOCATOR_DISABLE_STUN |
5070 cricket::PORTALLOCATOR_DISABLE_RELAY;
5071constexpr uint32_t kFlagsIPv6NoStun =
5072 cricket::PORTALLOCATOR_DISABLE_TCP | cricket::PORTALLOCATOR_DISABLE_STUN |
5073 cricket::PORTALLOCATOR_ENABLE_IPV6 | cricket::PORTALLOCATOR_DISABLE_RELAY;
5074constexpr uint32_t kFlagsIPv4Stun =
5075 cricket::PORTALLOCATOR_DISABLE_TCP | cricket::PORTALLOCATOR_DISABLE_RELAY;
5076
Mirko Bonadeic84f6612019-01-31 12:20:57 +01005077INSTANTIATE_TEST_SUITE_P(
Seth Hampson2f0d7022018-02-20 11:54:42 -08005078 PeerConnectionIntegrationTest,
5079 PeerConnectionIntegrationIceStatesTest,
5080 Combine(Values(SdpSemantics::kPlanB, SdpSemantics::kUnifiedPlan),
5081 Values(std::make_pair("IPv4 no STUN", kFlagsIPv4NoStun),
5082 std::make_pair("IPv6 no STUN", kFlagsIPv6NoStun),
5083 std::make_pair("IPv4 with STUN", kFlagsIPv4Stun))));
Steve Antonede9ca52017-10-16 13:04:27 -07005084
Yves Gerey100fe632020-01-17 19:15:53 +01005085INSTANTIATE_TEST_SUITE_P(
5086 PeerConnectionIntegrationTest,
5087 PeerConnectionIntegrationIceStatesTestWithFakeClock,
5088 Combine(Values(SdpSemantics::kPlanB, SdpSemantics::kUnifiedPlan),
5089 Values(std::make_pair("IPv4 no STUN", kFlagsIPv4NoStun),
5090 std::make_pair("IPv6 no STUN", kFlagsIPv6NoStun),
5091 std::make_pair("IPv4 with STUN", kFlagsIPv4Stun))));
5092
deadbeef1dcb1642017-03-29 21:08:16 -07005093// This test sets up a call between two parties with audio and video.
5094// During the call, the caller restarts ICE and the test verifies that
5095// new ICE candidates are generated and audio and video still can flow, and the
5096// ICE state reaches completed again.
Seth Hampson2f0d7022018-02-20 11:54:42 -08005097TEST_P(PeerConnectionIntegrationTest, MediaContinuesFlowingAfterIceRestart) {
deadbeef1dcb1642017-03-29 21:08:16 -07005098 ASSERT_TRUE(CreatePeerConnectionWrappers());
5099 ConnectFakeSignaling();
5100 // Do normal offer/answer and wait for ICE to complete.
Steve Anton15324772018-01-16 10:26:49 -08005101 caller()->AddAudioVideoTracks();
5102 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07005103 caller()->CreateAndSetAndSignalOffer();
5104 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5105 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
5106 caller()->ice_connection_state(), kMaxWaitForFramesMs);
Alex Loiko9289eda2018-11-23 16:18:59 +00005107 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
5108 callee()->ice_connection_state(), kMaxWaitForFramesMs);
deadbeef1dcb1642017-03-29 21:08:16 -07005109
5110 // To verify that the ICE restart actually occurs, get
5111 // ufrag/password/candidates before and after restart.
5112 // Create an SDP string of the first audio candidate for both clients.
5113 const webrtc::IceCandidateCollection* audio_candidates_caller =
5114 caller()->pc()->local_description()->candidates(0);
5115 const webrtc::IceCandidateCollection* audio_candidates_callee =
5116 callee()->pc()->local_description()->candidates(0);
5117 ASSERT_GT(audio_candidates_caller->count(), 0u);
5118 ASSERT_GT(audio_candidates_callee->count(), 0u);
5119 std::string caller_candidate_pre_restart;
5120 ASSERT_TRUE(
5121 audio_candidates_caller->at(0)->ToString(&caller_candidate_pre_restart));
5122 std::string callee_candidate_pre_restart;
5123 ASSERT_TRUE(
5124 audio_candidates_callee->at(0)->ToString(&callee_candidate_pre_restart));
5125 const cricket::SessionDescription* desc =
5126 caller()->pc()->local_description()->description();
5127 std::string caller_ufrag_pre_restart =
5128 desc->transport_infos()[0].description.ice_ufrag;
5129 desc = callee()->pc()->local_description()->description();
5130 std::string callee_ufrag_pre_restart =
5131 desc->transport_infos()[0].description.ice_ufrag;
5132
Alex Drake00c7ecf2019-08-06 10:54:47 -07005133 EXPECT_EQ(caller()->ice_candidate_pair_change_history().size(), 1u);
deadbeef1dcb1642017-03-29 21:08:16 -07005134 // Have the caller initiate an ICE restart.
5135 caller()->SetOfferAnswerOptions(IceRestartOfferAnswerOptions());
5136 caller()->CreateAndSetAndSignalOffer();
5137 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5138 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
5139 caller()->ice_connection_state(), kMaxWaitForFramesMs);
Alex Loiko9289eda2018-11-23 16:18:59 +00005140 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
deadbeef1dcb1642017-03-29 21:08:16 -07005141 callee()->ice_connection_state(), kMaxWaitForFramesMs);
5142
5143 // Grab the ufrags/candidates again.
5144 audio_candidates_caller = caller()->pc()->local_description()->candidates(0);
5145 audio_candidates_callee = callee()->pc()->local_description()->candidates(0);
5146 ASSERT_GT(audio_candidates_caller->count(), 0u);
5147 ASSERT_GT(audio_candidates_callee->count(), 0u);
5148 std::string caller_candidate_post_restart;
5149 ASSERT_TRUE(
5150 audio_candidates_caller->at(0)->ToString(&caller_candidate_post_restart));
5151 std::string callee_candidate_post_restart;
5152 ASSERT_TRUE(
5153 audio_candidates_callee->at(0)->ToString(&callee_candidate_post_restart));
5154 desc = caller()->pc()->local_description()->description();
5155 std::string caller_ufrag_post_restart =
5156 desc->transport_infos()[0].description.ice_ufrag;
5157 desc = callee()->pc()->local_description()->description();
5158 std::string callee_ufrag_post_restart =
5159 desc->transport_infos()[0].description.ice_ufrag;
5160 // Sanity check that an ICE restart was actually negotiated in SDP.
5161 ASSERT_NE(caller_candidate_pre_restart, caller_candidate_post_restart);
5162 ASSERT_NE(callee_candidate_pre_restart, callee_candidate_post_restart);
5163 ASSERT_NE(caller_ufrag_pre_restart, caller_ufrag_post_restart);
5164 ASSERT_NE(callee_ufrag_pre_restart, callee_ufrag_post_restart);
Alex Drake00c7ecf2019-08-06 10:54:47 -07005165 EXPECT_GT(caller()->ice_candidate_pair_change_history().size(), 1u);
deadbeef1dcb1642017-03-29 21:08:16 -07005166
5167 // Ensure that additional frames are received after the ICE restart.
Seth Hampson2f0d7022018-02-20 11:54:42 -08005168 MediaExpectations media_expectations;
5169 media_expectations.ExpectBidirectionalAudioAndVideo();
5170 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07005171}
5172
5173// Verify that audio/video can be received end-to-end when ICE renomination is
5174// enabled.
Seth Hampson2f0d7022018-02-20 11:54:42 -08005175TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithIceRenomination) {
deadbeef1dcb1642017-03-29 21:08:16 -07005176 PeerConnectionInterface::RTCConfiguration config;
5177 config.enable_ice_renomination = true;
5178 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
5179 ConnectFakeSignaling();
5180 // Do normal offer/answer and wait for some frames to be received in each
5181 // direction.
Steve Anton15324772018-01-16 10:26:49 -08005182 caller()->AddAudioVideoTracks();
5183 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07005184 caller()->CreateAndSetAndSignalOffer();
5185 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5186 // Sanity check that ICE renomination was actually negotiated.
5187 const cricket::SessionDescription* desc =
5188 caller()->pc()->local_description()->description();
5189 for (const cricket::TransportInfo& info : desc->transport_infos()) {
Steve Anton64b626b2019-01-28 17:25:26 -08005190 ASSERT_THAT(info.description.transport_options, Contains("renomination"));
deadbeef1dcb1642017-03-29 21:08:16 -07005191 }
5192 desc = callee()->pc()->local_description()->description();
5193 for (const cricket::TransportInfo& info : desc->transport_infos()) {
Steve Anton64b626b2019-01-28 17:25:26 -08005194 ASSERT_THAT(info.description.transport_options, Contains("renomination"));
deadbeef1dcb1642017-03-29 21:08:16 -07005195 }
Seth Hampson2f0d7022018-02-20 11:54:42 -08005196 MediaExpectations media_expectations;
5197 media_expectations.ExpectBidirectionalAudioAndVideo();
5198 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07005199}
5200
Steve Anton6f25b092017-10-23 09:39:20 -07005201// With a max bundle policy and RTCP muxing, adding a new media description to
5202// the connection should not affect ICE at all because the new media will use
5203// the existing connection.
Seth Hampson2f0d7022018-02-20 11:54:42 -08005204TEST_P(PeerConnectionIntegrationTest,
Steve Anton83119dd2017-11-10 16:19:52 -08005205 AddMediaToConnectedBundleDoesNotRestartIce) {
Steve Anton6f25b092017-10-23 09:39:20 -07005206 PeerConnectionInterface::RTCConfiguration config;
5207 config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
5208 config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyRequire;
5209 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(
5210 config, PeerConnectionInterface::RTCConfiguration()));
5211 ConnectFakeSignaling();
5212
Steve Anton15324772018-01-16 10:26:49 -08005213 caller()->AddAudioTrack();
Steve Anton6f25b092017-10-23 09:39:20 -07005214 caller()->CreateAndSetAndSignalOffer();
5215 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Steve Antonff52f1b2017-10-26 12:24:50 -07005216 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceConnectionCompleted,
5217 caller()->ice_connection_state(), kDefaultTimeout);
Steve Anton6f25b092017-10-23 09:39:20 -07005218
5219 caller()->clear_ice_connection_state_history();
5220
Steve Anton15324772018-01-16 10:26:49 -08005221 caller()->AddVideoTrack();
Steve Anton6f25b092017-10-23 09:39:20 -07005222 caller()->CreateAndSetAndSignalOffer();
5223 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5224
5225 EXPECT_EQ(0u, caller()->ice_connection_state_history().size());
5226}
5227
deadbeef1dcb1642017-03-29 21:08:16 -07005228// This test sets up a call between two parties with audio and video. It then
5229// renegotiates setting the video m-line to "port 0", then later renegotiates
5230// again, enabling video.
Seth Hampson2f0d7022018-02-20 11:54:42 -08005231TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07005232 VideoFlowsAfterMediaSectionIsRejectedAndRecycled) {
5233 ASSERT_TRUE(CreatePeerConnectionWrappers());
5234 ConnectFakeSignaling();
5235
5236 // Do initial negotiation, only sending media from the caller. Will result in
5237 // video and audio recvonly "m=" sections.
Steve Anton15324772018-01-16 10:26:49 -08005238 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07005239 caller()->CreateAndSetAndSignalOffer();
5240 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5241
5242 // Negotiate again, disabling the video "m=" section (the callee will set the
5243 // port to 0 due to offer_to_receive_video = 0).
Seth Hampson2f0d7022018-02-20 11:54:42 -08005244 if (sdp_semantics_ == SdpSemantics::kPlanB) {
5245 PeerConnectionInterface::RTCOfferAnswerOptions options;
5246 options.offer_to_receive_video = 0;
5247 callee()->SetOfferAnswerOptions(options);
5248 } else {
5249 callee()->SetRemoteOfferHandler([this] {
5250 callee()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)->Stop();
5251 });
5252 }
deadbeef1dcb1642017-03-29 21:08:16 -07005253 caller()->CreateAndSetAndSignalOffer();
5254 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5255 // Sanity check that video "m=" section was actually rejected.
5256 const ContentInfo* answer_video_content = cricket::GetFirstVideoContent(
5257 callee()->pc()->local_description()->description());
5258 ASSERT_NE(nullptr, answer_video_content);
5259 ASSERT_TRUE(answer_video_content->rejected);
5260
5261 // Enable video and do negotiation again, making sure video is received
5262 // end-to-end, also adding media stream to callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08005263 if (sdp_semantics_ == SdpSemantics::kPlanB) {
5264 PeerConnectionInterface::RTCOfferAnswerOptions options;
5265 options.offer_to_receive_video = 1;
5266 callee()->SetOfferAnswerOptions(options);
5267 } else {
5268 // The caller's transceiver is stopped, so we need to add another track.
5269 auto caller_transceiver =
5270 caller()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO);
5271 EXPECT_TRUE(caller_transceiver->stopped());
5272 caller()->AddVideoTrack();
5273 }
5274 callee()->AddVideoTrack();
5275 callee()->SetRemoteOfferHandler(nullptr);
deadbeef1dcb1642017-03-29 21:08:16 -07005276 caller()->CreateAndSetAndSignalOffer();
5277 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08005278
deadbeef1dcb1642017-03-29 21:08:16 -07005279 // Verify the caller receives frames from the newly added stream, and the
5280 // callee receives additional frames from the re-enabled video m= section.
Seth Hampson2f0d7022018-02-20 11:54:42 -08005281 MediaExpectations media_expectations;
5282 media_expectations.CalleeExpectsSomeAudio();
5283 media_expectations.ExpectBidirectionalVideo();
5284 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07005285}
5286
deadbeef1dcb1642017-03-29 21:08:16 -07005287// This tests that if we negotiate after calling CreateSender but before we
5288// have a track, then set a track later, frames from the newly-set track are
5289// received end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08005290TEST_F(PeerConnectionIntegrationTestPlanB,
deadbeef1dcb1642017-03-29 21:08:16 -07005291 MediaFlowsAfterEarlyWarmupWithCreateSender) {
5292 ASSERT_TRUE(CreatePeerConnectionWrappers());
5293 ConnectFakeSignaling();
5294 auto caller_audio_sender =
5295 caller()->pc()->CreateSender("audio", "caller_stream");
5296 auto caller_video_sender =
5297 caller()->pc()->CreateSender("video", "caller_stream");
5298 auto callee_audio_sender =
5299 callee()->pc()->CreateSender("audio", "callee_stream");
5300 auto callee_video_sender =
5301 callee()->pc()->CreateSender("video", "callee_stream");
5302 caller()->CreateAndSetAndSignalOffer();
5303 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
5304 // Wait for ICE to complete, without any tracks being set.
5305 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
5306 caller()->ice_connection_state(), kMaxWaitForFramesMs);
5307 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
5308 callee()->ice_connection_state(), kMaxWaitForFramesMs);
5309 // Now set the tracks, and expect frames to immediately start flowing.
5310 EXPECT_TRUE(caller_audio_sender->SetTrack(caller()->CreateLocalAudioTrack()));
5311 EXPECT_TRUE(caller_video_sender->SetTrack(caller()->CreateLocalVideoTrack()));
5312 EXPECT_TRUE(callee_audio_sender->SetTrack(callee()->CreateLocalAudioTrack()));
5313 EXPECT_TRUE(callee_video_sender->SetTrack(callee()->CreateLocalVideoTrack()));
Seth Hampson2f0d7022018-02-20 11:54:42 -08005314 MediaExpectations media_expectations;
5315 media_expectations.ExpectBidirectionalAudioAndVideo();
5316 ASSERT_TRUE(ExpectNewFrames(media_expectations));
5317}
5318
5319// This tests that if we negotiate after calling AddTransceiver but before we
5320// have a track, then set a track later, frames from the newly-set tracks are
5321// received end-to-end.
5322TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
5323 MediaFlowsAfterEarlyWarmupWithAddTransceiver) {
5324 ASSERT_TRUE(CreatePeerConnectionWrappers());
5325 ConnectFakeSignaling();
5326 auto audio_result = caller()->pc()->AddTransceiver(cricket::MEDIA_TYPE_AUDIO);
5327 ASSERT_EQ(RTCErrorType::NONE, audio_result.error().type());
5328 auto caller_audio_sender = audio_result.MoveValue()->sender();
5329 auto video_result = caller()->pc()->AddTransceiver(cricket::MEDIA_TYPE_VIDEO);
5330 ASSERT_EQ(RTCErrorType::NONE, video_result.error().type());
5331 auto caller_video_sender = video_result.MoveValue()->sender();
5332 callee()->SetRemoteOfferHandler([this] {
5333 ASSERT_EQ(2u, callee()->pc()->GetTransceivers().size());
5334 callee()->pc()->GetTransceivers()[0]->SetDirection(
5335 RtpTransceiverDirection::kSendRecv);
5336 callee()->pc()->GetTransceivers()[1]->SetDirection(
5337 RtpTransceiverDirection::kSendRecv);
5338 });
5339 caller()->CreateAndSetAndSignalOffer();
5340 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
5341 // Wait for ICE to complete, without any tracks being set.
5342 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
5343 caller()->ice_connection_state(), kMaxWaitForFramesMs);
5344 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
5345 callee()->ice_connection_state(), kMaxWaitForFramesMs);
5346 // Now set the tracks, and expect frames to immediately start flowing.
5347 auto callee_audio_sender = callee()->pc()->GetSenders()[0];
5348 auto callee_video_sender = callee()->pc()->GetSenders()[1];
5349 ASSERT_TRUE(caller_audio_sender->SetTrack(caller()->CreateLocalAudioTrack()));
5350 ASSERT_TRUE(caller_video_sender->SetTrack(caller()->CreateLocalVideoTrack()));
5351 ASSERT_TRUE(callee_audio_sender->SetTrack(callee()->CreateLocalAudioTrack()));
5352 ASSERT_TRUE(callee_video_sender->SetTrack(callee()->CreateLocalVideoTrack()));
5353 MediaExpectations media_expectations;
5354 media_expectations.ExpectBidirectionalAudioAndVideo();
5355 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07005356}
5357
5358// This test verifies that a remote video track can be added via AddStream,
5359// and sent end-to-end. For this particular test, it's simply echoed back
5360// from the caller to the callee, rather than being forwarded to a third
5361// PeerConnection.
Seth Hampson2f0d7022018-02-20 11:54:42 -08005362TEST_F(PeerConnectionIntegrationTestPlanB, CanSendRemoteVideoTrack) {
deadbeef1dcb1642017-03-29 21:08:16 -07005363 ASSERT_TRUE(CreatePeerConnectionWrappers());
5364 ConnectFakeSignaling();
5365 // Just send a video track from the caller.
Steve Anton15324772018-01-16 10:26:49 -08005366 caller()->AddVideoTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07005367 caller()->CreateAndSetAndSignalOffer();
5368 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02005369 ASSERT_EQ(1U, callee()->remote_streams()->count());
deadbeef1dcb1642017-03-29 21:08:16 -07005370
5371 // Echo the stream back, and do a new offer/anwer (initiated by callee this
5372 // time).
5373 callee()->pc()->AddStream(callee()->remote_streams()->at(0));
5374 callee()->CreateAndSetAndSignalOffer();
5375 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
5376
Seth Hampson2f0d7022018-02-20 11:54:42 -08005377 MediaExpectations media_expectations;
5378 media_expectations.ExpectBidirectionalVideo();
5379 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07005380}
5381
5382// Test that we achieve the expected end-to-end connection time, using a
5383// fake clock and simulated latency on the media and signaling paths.
5384// We use a TURN<->TURN connection because this is usually the quickest to
5385// set up initially, especially when we're confident the connection will work
5386// and can start sending media before we get a STUN response.
5387//
5388// With various optimizations enabled, here are the network delays we expect to
5389// be on the critical path:
5390// 1. 2 signaling trips: Signaling offer and offerer's TURN candidate, then
5391// signaling answer (with DTLS fingerprint).
5392// 2. 9 media hops: Rest of the DTLS handshake. 3 hops in each direction when
5393// using TURN<->TURN pair, and DTLS exchange is 4 packets,
5394// the first of which should have arrived before the answer.
Yves Gerey100fe632020-01-17 19:15:53 +01005395TEST_P(PeerConnectionIntegrationTestWithFakeClock,
5396 EndToEndConnectionTimeWithTurnTurnPair) {
deadbeef1dcb1642017-03-29 21:08:16 -07005397 static constexpr int media_hop_delay_ms = 50;
5398 static constexpr int signaling_trip_delay_ms = 500;
5399 // For explanation of these values, see comment above.
5400 static constexpr int required_media_hops = 9;
5401 static constexpr int required_signaling_trips = 2;
5402 // For internal delays (such as posting an event asychronously).
5403 static constexpr int allowed_internal_delay_ms = 20;
5404 static constexpr int total_connection_time_ms =
5405 media_hop_delay_ms * required_media_hops +
5406 signaling_trip_delay_ms * required_signaling_trips +
5407 allowed_internal_delay_ms;
5408
5409 static const rtc::SocketAddress turn_server_1_internal_address{"88.88.88.0",
5410 3478};
5411 static const rtc::SocketAddress turn_server_1_external_address{"88.88.88.1",
5412 0};
5413 static const rtc::SocketAddress turn_server_2_internal_address{"99.99.99.0",
5414 3478};
5415 static const rtc::SocketAddress turn_server_2_external_address{"99.99.99.1",
5416 0};
Seth Hampsonaed71642018-06-11 07:41:32 -07005417 cricket::TestTurnServer* turn_server_1 = CreateTurnServer(
5418 turn_server_1_internal_address, turn_server_1_external_address);
Jonas Orelandbdcee282017-10-10 14:01:40 +02005419
Seth Hampsonaed71642018-06-11 07:41:32 -07005420 cricket::TestTurnServer* turn_server_2 = CreateTurnServer(
5421 turn_server_2_internal_address, turn_server_2_external_address);
deadbeef1dcb1642017-03-29 21:08:16 -07005422 // Bypass permission check on received packets so media can be sent before
5423 // the candidate is signaled.
Seth Hampsonaed71642018-06-11 07:41:32 -07005424 network_thread()->Invoke<void>(RTC_FROM_HERE, [turn_server_1] {
5425 turn_server_1->set_enable_permission_checks(false);
5426 });
5427 network_thread()->Invoke<void>(RTC_FROM_HERE, [turn_server_2] {
5428 turn_server_2->set_enable_permission_checks(false);
5429 });
deadbeef1dcb1642017-03-29 21:08:16 -07005430
5431 PeerConnectionInterface::RTCConfiguration client_1_config;
5432 webrtc::PeerConnectionInterface::IceServer ice_server_1;
5433 ice_server_1.urls.push_back("turn:88.88.88.0:3478");
5434 ice_server_1.username = "test";
5435 ice_server_1.password = "test";
5436 client_1_config.servers.push_back(ice_server_1);
5437 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
5438 client_1_config.presume_writable_when_fully_relayed = true;
5439
5440 PeerConnectionInterface::RTCConfiguration client_2_config;
5441 webrtc::PeerConnectionInterface::IceServer ice_server_2;
5442 ice_server_2.urls.push_back("turn:99.99.99.0:3478");
5443 ice_server_2.username = "test";
5444 ice_server_2.password = "test";
5445 client_2_config.servers.push_back(ice_server_2);
5446 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
5447 client_2_config.presume_writable_when_fully_relayed = true;
5448
5449 ASSERT_TRUE(
5450 CreatePeerConnectionWrappersWithConfig(client_1_config, client_2_config));
5451 // Set up the simulated delays.
5452 SetSignalingDelayMs(signaling_trip_delay_ms);
5453 ConnectFakeSignaling();
5454 virtual_socket_server()->set_delay_mean(media_hop_delay_ms);
5455 virtual_socket_server()->UpdateDelayDistribution();
5456
5457 // Set "offer to receive audio/video" without adding any tracks, so we just
5458 // set up ICE/DTLS with no media.
5459 PeerConnectionInterface::RTCOfferAnswerOptions options;
5460 options.offer_to_receive_audio = 1;
5461 options.offer_to_receive_video = 1;
5462 caller()->SetOfferAnswerOptions(options);
5463 caller()->CreateAndSetAndSignalOffer();
deadbeef71452802017-05-07 17:21:01 -07005464 EXPECT_TRUE_SIMULATED_WAIT(DtlsConnected(), total_connection_time_ms,
Yves Gerey100fe632020-01-17 19:15:53 +01005465 FakeClock());
Seth Hampson1d4a76d2018-06-19 14:31:41 -07005466 // Closing the PeerConnections destroys the ports before the ScopedFakeClock.
5467 // If this is not done a DCHECK can be hit in ports.cc, because a large
5468 // negative number is calculated for the rtt due to the global clock changing.
Steve Antond91969e2019-05-30 12:27:03 -07005469 ClosePeerConnections();
deadbeef1dcb1642017-03-29 21:08:16 -07005470}
5471
Jonas Orelandbdcee282017-10-10 14:01:40 +02005472// Verify that a TurnCustomizer passed in through RTCConfiguration
5473// is actually used by the underlying TURN candidate pair.
5474// Note that turnport_unittest.cc contains more detailed, lower-level tests.
Seth Hampson2f0d7022018-02-20 11:54:42 -08005475TEST_P(PeerConnectionIntegrationTest, TurnCustomizerUsedForTurnConnections) {
Jonas Orelandbdcee282017-10-10 14:01:40 +02005476 static const rtc::SocketAddress turn_server_1_internal_address{"88.88.88.0",
5477 3478};
5478 static const rtc::SocketAddress turn_server_1_external_address{"88.88.88.1",
5479 0};
5480 static const rtc::SocketAddress turn_server_2_internal_address{"99.99.99.0",
5481 3478};
5482 static const rtc::SocketAddress turn_server_2_external_address{"99.99.99.1",
5483 0};
Seth Hampsonaed71642018-06-11 07:41:32 -07005484 CreateTurnServer(turn_server_1_internal_address,
5485 turn_server_1_external_address);
5486 CreateTurnServer(turn_server_2_internal_address,
5487 turn_server_2_external_address);
Jonas Orelandbdcee282017-10-10 14:01:40 +02005488
5489 PeerConnectionInterface::RTCConfiguration client_1_config;
5490 webrtc::PeerConnectionInterface::IceServer ice_server_1;
5491 ice_server_1.urls.push_back("turn:88.88.88.0:3478");
5492 ice_server_1.username = "test";
5493 ice_server_1.password = "test";
5494 client_1_config.servers.push_back(ice_server_1);
5495 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
Seth Hampsonaed71642018-06-11 07:41:32 -07005496 auto* customizer1 = CreateTurnCustomizer();
5497 client_1_config.turn_customizer = customizer1;
Jonas Orelandbdcee282017-10-10 14:01:40 +02005498
5499 PeerConnectionInterface::RTCConfiguration client_2_config;
5500 webrtc::PeerConnectionInterface::IceServer ice_server_2;
5501 ice_server_2.urls.push_back("turn:99.99.99.0:3478");
5502 ice_server_2.username = "test";
5503 ice_server_2.password = "test";
5504 client_2_config.servers.push_back(ice_server_2);
5505 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
Seth Hampsonaed71642018-06-11 07:41:32 -07005506 auto* customizer2 = CreateTurnCustomizer();
5507 client_2_config.turn_customizer = customizer2;
Jonas Orelandbdcee282017-10-10 14:01:40 +02005508
5509 ASSERT_TRUE(
5510 CreatePeerConnectionWrappersWithConfig(client_1_config, client_2_config));
5511 ConnectFakeSignaling();
5512
5513 // Set "offer to receive audio/video" without adding any tracks, so we just
5514 // set up ICE/DTLS with no media.
5515 PeerConnectionInterface::RTCOfferAnswerOptions options;
5516 options.offer_to_receive_audio = 1;
5517 options.offer_to_receive_video = 1;
5518 caller()->SetOfferAnswerOptions(options);
5519 caller()->CreateAndSetAndSignalOffer();
5520 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
5521
Seth Hampsonaed71642018-06-11 07:41:32 -07005522 ExpectTurnCustomizerCountersIncremented(customizer1);
5523 ExpectTurnCustomizerCountersIncremented(customizer2);
Jonas Orelandbdcee282017-10-10 14:01:40 +02005524}
5525
Benjamin Wright2d5f3cb2018-05-22 14:46:06 -07005526// Verifies that you can use TCP instead of UDP to connect to a TURN server and
5527// send media between the caller and the callee.
5528TEST_P(PeerConnectionIntegrationTest, TCPUsedForTurnConnections) {
5529 static const rtc::SocketAddress turn_server_internal_address{"88.88.88.0",
5530 3478};
5531 static const rtc::SocketAddress turn_server_external_address{"88.88.88.1", 0};
5532
5533 // Enable TCP for the fake turn server.
Seth Hampsonaed71642018-06-11 07:41:32 -07005534 CreateTurnServer(turn_server_internal_address, turn_server_external_address,
5535 cricket::PROTO_TCP);
Benjamin Wright2d5f3cb2018-05-22 14:46:06 -07005536
5537 webrtc::PeerConnectionInterface::IceServer ice_server;
5538 ice_server.urls.push_back("turn:88.88.88.0:3478?transport=tcp");
5539 ice_server.username = "test";
5540 ice_server.password = "test";
5541
5542 PeerConnectionInterface::RTCConfiguration client_1_config;
5543 client_1_config.servers.push_back(ice_server);
5544 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
5545
5546 PeerConnectionInterface::RTCConfiguration client_2_config;
5547 client_2_config.servers.push_back(ice_server);
5548 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
5549
5550 ASSERT_TRUE(
5551 CreatePeerConnectionWrappersWithConfig(client_1_config, client_2_config));
5552
5553 // Do normal offer/answer and wait for ICE to complete.
5554 ConnectFakeSignaling();
5555 caller()->AddAudioVideoTracks();
5556 callee()->AddAudioVideoTracks();
5557 caller()->CreateAndSetAndSignalOffer();
5558 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5559 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
5560 callee()->ice_connection_state(), kMaxWaitForFramesMs);
5561
5562 MediaExpectations media_expectations;
5563 media_expectations.ExpectBidirectionalAudioAndVideo();
5564 EXPECT_TRUE(ExpectNewFrames(media_expectations));
5565}
5566
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07005567// Verify that a SSLCertificateVerifier passed in through
5568// PeerConnectionDependencies is actually used by the underlying SSL
5569// implementation to determine whether a certificate presented by the TURN
5570// server is accepted by the client. Note that openssladapter_unittest.cc
5571// contains more detailed, lower-level tests.
5572TEST_P(PeerConnectionIntegrationTest,
5573 SSLCertificateVerifierUsedForTurnConnections) {
5574 static const rtc::SocketAddress turn_server_internal_address{"88.88.88.0",
5575 3478};
5576 static const rtc::SocketAddress turn_server_external_address{"88.88.88.1", 0};
5577
5578 // Enable TCP-TLS for the fake turn server. We need to pass in 88.88.88.0 so
5579 // that host name verification passes on the fake certificate.
Seth Hampsonaed71642018-06-11 07:41:32 -07005580 CreateTurnServer(turn_server_internal_address, turn_server_external_address,
5581 cricket::PROTO_TLS, "88.88.88.0");
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07005582
5583 webrtc::PeerConnectionInterface::IceServer ice_server;
5584 ice_server.urls.push_back("turns:88.88.88.0:3478?transport=tcp");
5585 ice_server.username = "test";
5586 ice_server.password = "test";
5587
5588 PeerConnectionInterface::RTCConfiguration client_1_config;
5589 client_1_config.servers.push_back(ice_server);
5590 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
5591
5592 PeerConnectionInterface::RTCConfiguration client_2_config;
5593 client_2_config.servers.push_back(ice_server);
5594 // Setting the type to kRelay forces the connection to go through a TURN
5595 // server.
5596 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
5597
5598 // Get a copy to the pointer so we can verify calls later.
5599 rtc::TestCertificateVerifier* client_1_cert_verifier =
5600 new rtc::TestCertificateVerifier();
5601 client_1_cert_verifier->verify_certificate_ = true;
5602 rtc::TestCertificateVerifier* client_2_cert_verifier =
5603 new rtc::TestCertificateVerifier();
5604 client_2_cert_verifier->verify_certificate_ = true;
5605
5606 // Create the dependencies with the test certificate verifier.
5607 webrtc::PeerConnectionDependencies client_1_deps(nullptr);
5608 client_1_deps.tls_cert_verifier =
5609 std::unique_ptr<rtc::TestCertificateVerifier>(client_1_cert_verifier);
5610 webrtc::PeerConnectionDependencies client_2_deps(nullptr);
5611 client_2_deps.tls_cert_verifier =
5612 std::unique_ptr<rtc::TestCertificateVerifier>(client_2_cert_verifier);
5613
5614 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndDeps(
5615 client_1_config, std::move(client_1_deps), client_2_config,
5616 std::move(client_2_deps)));
5617 ConnectFakeSignaling();
5618
5619 // Set "offer to receive audio/video" without adding any tracks, so we just
5620 // set up ICE/DTLS with no media.
5621 PeerConnectionInterface::RTCOfferAnswerOptions options;
5622 options.offer_to_receive_audio = 1;
5623 options.offer_to_receive_video = 1;
5624 caller()->SetOfferAnswerOptions(options);
5625 caller()->CreateAndSetAndSignalOffer();
5626 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
5627
5628 EXPECT_GT(client_1_cert_verifier->call_count_, 0u);
5629 EXPECT_GT(client_2_cert_verifier->call_count_, 0u);
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07005630}
5631
5632TEST_P(PeerConnectionIntegrationTest,
5633 SSLCertificateVerifierFailureUsedForTurnConnectionsFailsConnection) {
5634 static const rtc::SocketAddress turn_server_internal_address{"88.88.88.0",
5635 3478};
5636 static const rtc::SocketAddress turn_server_external_address{"88.88.88.1", 0};
5637
5638 // Enable TCP-TLS for the fake turn server. We need to pass in 88.88.88.0 so
5639 // that host name verification passes on the fake certificate.
Seth Hampsonaed71642018-06-11 07:41:32 -07005640 CreateTurnServer(turn_server_internal_address, turn_server_external_address,
5641 cricket::PROTO_TLS, "88.88.88.0");
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07005642
5643 webrtc::PeerConnectionInterface::IceServer ice_server;
5644 ice_server.urls.push_back("turns:88.88.88.0:3478?transport=tcp");
5645 ice_server.username = "test";
5646 ice_server.password = "test";
5647
5648 PeerConnectionInterface::RTCConfiguration client_1_config;
5649 client_1_config.servers.push_back(ice_server);
5650 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
5651
5652 PeerConnectionInterface::RTCConfiguration client_2_config;
5653 client_2_config.servers.push_back(ice_server);
5654 // Setting the type to kRelay forces the connection to go through a TURN
5655 // server.
5656 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
5657
5658 // Get a copy to the pointer so we can verify calls later.
5659 rtc::TestCertificateVerifier* client_1_cert_verifier =
5660 new rtc::TestCertificateVerifier();
5661 client_1_cert_verifier->verify_certificate_ = false;
5662 rtc::TestCertificateVerifier* client_2_cert_verifier =
5663 new rtc::TestCertificateVerifier();
5664 client_2_cert_verifier->verify_certificate_ = false;
5665
5666 // Create the dependencies with the test certificate verifier.
5667 webrtc::PeerConnectionDependencies client_1_deps(nullptr);
5668 client_1_deps.tls_cert_verifier =
5669 std::unique_ptr<rtc::TestCertificateVerifier>(client_1_cert_verifier);
5670 webrtc::PeerConnectionDependencies client_2_deps(nullptr);
5671 client_2_deps.tls_cert_verifier =
5672 std::unique_ptr<rtc::TestCertificateVerifier>(client_2_cert_verifier);
5673
5674 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndDeps(
5675 client_1_config, std::move(client_1_deps), client_2_config,
5676 std::move(client_2_deps)));
5677 ConnectFakeSignaling();
5678
5679 // Set "offer to receive audio/video" without adding any tracks, so we just
5680 // set up ICE/DTLS with no media.
5681 PeerConnectionInterface::RTCOfferAnswerOptions options;
5682 options.offer_to_receive_audio = 1;
5683 options.offer_to_receive_video = 1;
5684 caller()->SetOfferAnswerOptions(options);
5685 caller()->CreateAndSetAndSignalOffer();
5686 bool wait_res = true;
5687 // TODO(bugs.webrtc.org/9219): When IceConnectionState is implemented
5688 // properly, should be able to just wait for a state of "failed" instead of
5689 // waiting a fixed 10 seconds.
5690 WAIT_(DtlsConnected(), kDefaultTimeout, wait_res);
5691 ASSERT_FALSE(wait_res);
5692
5693 EXPECT_GT(client_1_cert_verifier->call_count_, 0u);
5694 EXPECT_GT(client_2_cert_verifier->call_count_, 0u);
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07005695}
5696
Qingsi Wang25ec8882019-11-15 12:33:05 -08005697// Test that the injected ICE transport factory is used to create ICE transports
5698// for WebRTC connections.
5699TEST_P(PeerConnectionIntegrationTest, IceTransportFactoryUsedForConnections) {
5700 PeerConnectionInterface::RTCConfiguration default_config;
5701 PeerConnectionDependencies dependencies(nullptr);
5702 auto ice_transport_factory = std::make_unique<MockIceTransportFactory>();
5703 EXPECT_CALL(*ice_transport_factory, RecordIceTransportCreated()).Times(1);
5704 dependencies.ice_transport_factory = std::move(ice_transport_factory);
Johannes Kron3e983682020-03-29 22:17:00 +02005705 auto wrapper = CreatePeerConnectionWrapper(
5706 "Caller", nullptr, &default_config, std::move(dependencies), nullptr,
5707 nullptr, /*reset_encoder_factory=*/false,
5708 /*reset_decoder_factory=*/false);
Qingsi Wang25ec8882019-11-15 12:33:05 -08005709 ASSERT_TRUE(wrapper);
5710 wrapper->CreateDataChannel();
5711 rtc::scoped_refptr<MockSetSessionDescriptionObserver> observer(
5712 new rtc::RefCountedObject<MockSetSessionDescriptionObserver>());
5713 wrapper->pc()->SetLocalDescription(observer,
5714 wrapper->CreateOfferAndWait().release());
5715}
5716
deadbeefc964d0b2017-04-03 10:03:35 -07005717// Test that audio and video flow end-to-end when codec names don't use the
5718// expected casing, given that they're supposed to be case insensitive. To test
5719// this, all but one codec is removed from each media description, and its
5720// casing is changed.
5721//
5722// In the past, this has regressed and caused crashes/black video, due to the
5723// fact that code at some layers was doing case-insensitive comparisons and
5724// code at other layers was not.
Seth Hampson2f0d7022018-02-20 11:54:42 -08005725TEST_P(PeerConnectionIntegrationTest, CodecNamesAreCaseInsensitive) {
deadbeefc964d0b2017-04-03 10:03:35 -07005726 ASSERT_TRUE(CreatePeerConnectionWrappers());
5727 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08005728 caller()->AddAudioVideoTracks();
5729 callee()->AddAudioVideoTracks();
deadbeefc964d0b2017-04-03 10:03:35 -07005730
5731 // Remove all but one audio/video codec (opus and VP8), and change the
5732 // casing of the caller's generated offer.
5733 caller()->SetGeneratedSdpMunger([](cricket::SessionDescription* description) {
5734 cricket::AudioContentDescription* audio =
5735 GetFirstAudioContentDescription(description);
5736 ASSERT_NE(nullptr, audio);
5737 auto audio_codecs = audio->codecs();
5738 audio_codecs.erase(std::remove_if(audio_codecs.begin(), audio_codecs.end(),
5739 [](const cricket::AudioCodec& codec) {
5740 return codec.name != "opus";
5741 }),
5742 audio_codecs.end());
5743 ASSERT_EQ(1u, audio_codecs.size());
5744 audio_codecs[0].name = "OpUs";
5745 audio->set_codecs(audio_codecs);
5746
5747 cricket::VideoContentDescription* video =
5748 GetFirstVideoContentDescription(description);
5749 ASSERT_NE(nullptr, video);
5750 auto video_codecs = video->codecs();
5751 video_codecs.erase(std::remove_if(video_codecs.begin(), video_codecs.end(),
5752 [](const cricket::VideoCodec& codec) {
5753 return codec.name != "VP8";
5754 }),
5755 video_codecs.end());
5756 ASSERT_EQ(1u, video_codecs.size());
5757 video_codecs[0].name = "vP8";
5758 video->set_codecs(video_codecs);
5759 });
5760
5761 caller()->CreateAndSetAndSignalOffer();
5762 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5763
5764 // Verify frames are still received end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08005765 MediaExpectations media_expectations;
5766 media_expectations.ExpectBidirectionalAudioAndVideo();
5767 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeefc964d0b2017-04-03 10:03:35 -07005768}
5769
Jonas Oreland49ac5952018-09-26 16:04:32 +02005770TEST_P(PeerConnectionIntegrationTest, GetSourcesAudio) {
hbos8d609f62017-04-10 07:39:05 -07005771 ASSERT_TRUE(CreatePeerConnectionWrappers());
5772 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08005773 caller()->AddAudioTrack();
hbos8d609f62017-04-10 07:39:05 -07005774 caller()->CreateAndSetAndSignalOffer();
5775 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
deadbeefd8ad7882017-04-18 16:01:17 -07005776 // Wait for one audio frame to be received by the callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08005777 MediaExpectations media_expectations;
5778 media_expectations.CalleeExpectsSomeAudio(1);
5779 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Jonas Oreland49ac5952018-09-26 16:04:32 +02005780 ASSERT_EQ(callee()->pc()->GetReceivers().size(), 1u);
hbos8d609f62017-04-10 07:39:05 -07005781 auto receiver = callee()->pc()->GetReceivers()[0];
5782 ASSERT_EQ(receiver->media_type(), cricket::MEDIA_TYPE_AUDIO);
Jonas Oreland49ac5952018-09-26 16:04:32 +02005783 auto sources = receiver->GetSources();
hbos8d609f62017-04-10 07:39:05 -07005784 ASSERT_GT(receiver->GetParameters().encodings.size(), 0u);
5785 EXPECT_EQ(receiver->GetParameters().encodings[0].ssrc,
Jonas Oreland49ac5952018-09-26 16:04:32 +02005786 sources[0].source_id());
5787 EXPECT_EQ(webrtc::RtpSourceType::SSRC, sources[0].source_type());
5788}
5789
5790TEST_P(PeerConnectionIntegrationTest, GetSourcesVideo) {
5791 ASSERT_TRUE(CreatePeerConnectionWrappers());
5792 ConnectFakeSignaling();
5793 caller()->AddVideoTrack();
5794 caller()->CreateAndSetAndSignalOffer();
5795 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5796 // Wait for one video frame to be received by the callee.
5797 MediaExpectations media_expectations;
5798 media_expectations.CalleeExpectsSomeVideo(1);
5799 ASSERT_TRUE(ExpectNewFrames(media_expectations));
5800 ASSERT_EQ(callee()->pc()->GetReceivers().size(), 1u);
5801 auto receiver = callee()->pc()->GetReceivers()[0];
5802 ASSERT_EQ(receiver->media_type(), cricket::MEDIA_TYPE_VIDEO);
5803 auto sources = receiver->GetSources();
5804 ASSERT_GT(receiver->GetParameters().encodings.size(), 0u);
Yves Gereyf781bb52019-07-23 19:15:39 +02005805 ASSERT_GT(sources.size(), 0u);
Jonas Oreland49ac5952018-09-26 16:04:32 +02005806 EXPECT_EQ(receiver->GetParameters().encodings[0].ssrc,
5807 sources[0].source_id());
5808 EXPECT_EQ(webrtc::RtpSourceType::SSRC, sources[0].source_type());
hbos8d609f62017-04-10 07:39:05 -07005809}
5810
deadbeef2f425aa2017-04-14 10:41:32 -07005811// Test that if a track is removed and added again with a different stream ID,
5812// the new stream ID is successfully communicated in SDP and media continues to
5813// flow end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08005814// TODO(webrtc.bugs.org/8734): This test does not work for Unified Plan because
5815// it will not reuse a transceiver that has already been sending. After creating
5816// a new transceiver it tries to create an offer with two senders of the same
5817// track ids and it fails.
5818TEST_F(PeerConnectionIntegrationTestPlanB, RemoveAndAddTrackWithNewStreamId) {
deadbeef2f425aa2017-04-14 10:41:32 -07005819 ASSERT_TRUE(CreatePeerConnectionWrappers());
5820 ConnectFakeSignaling();
5821
deadbeef2f425aa2017-04-14 10:41:32 -07005822 // Add track using stream 1, do offer/answer.
5823 rtc::scoped_refptr<webrtc::AudioTrackInterface> track =
5824 caller()->CreateLocalAudioTrack();
5825 rtc::scoped_refptr<webrtc::RtpSenderInterface> sender =
Steve Antond78323f2018-07-11 11:13:44 -07005826 caller()->AddTrack(track, {"stream_1"});
deadbeef2f425aa2017-04-14 10:41:32 -07005827 caller()->CreateAndSetAndSignalOffer();
5828 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08005829 {
5830 MediaExpectations media_expectations;
5831 media_expectations.CalleeExpectsSomeAudio(1);
5832 ASSERT_TRUE(ExpectNewFrames(media_expectations));
5833 }
deadbeef2f425aa2017-04-14 10:41:32 -07005834 // Remove the sender, and create a new one with the new stream.
5835 caller()->pc()->RemoveTrack(sender);
Steve Antond78323f2018-07-11 11:13:44 -07005836 sender = caller()->AddTrack(track, {"stream_2"});
deadbeef2f425aa2017-04-14 10:41:32 -07005837 caller()->CreateAndSetAndSignalOffer();
5838 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5839 // Wait for additional audio frames to be received by the callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08005840 {
5841 MediaExpectations media_expectations;
5842 media_expectations.CalleeExpectsSomeAudio();
5843 ASSERT_TRUE(ExpectNewFrames(media_expectations));
5844 }
deadbeef2f425aa2017-04-14 10:41:32 -07005845}
5846
Seth Hampson2f0d7022018-02-20 11:54:42 -08005847TEST_P(PeerConnectionIntegrationTest, RtcEventLogOutputWriteCalled) {
Elad Alon99c3fe52017-10-13 16:29:40 +02005848 ASSERT_TRUE(CreatePeerConnectionWrappers());
5849 ConnectFakeSignaling();
5850
Mirko Bonadei317a1f02019-09-17 17:06:18 +02005851 auto output = std::make_unique<testing::NiceMock<MockRtcEventLogOutput>>();
Mirko Bonadei6a489f22019-04-09 15:11:12 +02005852 ON_CALL(*output, IsActive()).WillByDefault(::testing::Return(true));
5853 ON_CALL(*output, Write(::testing::_)).WillByDefault(::testing::Return(true));
Elad Alon99c3fe52017-10-13 16:29:40 +02005854 EXPECT_CALL(*output, Write(::testing::_)).Times(::testing::AtLeast(1));
Bjorn Tereliusde939432017-11-20 17:38:14 +01005855 EXPECT_TRUE(caller()->pc()->StartRtcEventLog(
5856 std::move(output), webrtc::RtcEventLog::kImmediateOutput));
Elad Alon99c3fe52017-10-13 16:29:40 +02005857
Steve Anton15324772018-01-16 10:26:49 -08005858 caller()->AddAudioVideoTracks();
Elad Alon99c3fe52017-10-13 16:29:40 +02005859 caller()->CreateAndSetAndSignalOffer();
5860 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5861}
5862
Steve Antonede9ca52017-10-16 13:04:27 -07005863// Test that if candidates are only signaled by applying full session
5864// descriptions (instead of using AddIceCandidate), the peers can connect to
5865// each other and exchange media.
Seth Hampson2f0d7022018-02-20 11:54:42 -08005866TEST_P(PeerConnectionIntegrationTest, MediaFlowsWhenCandidatesSetOnlyInSdp) {
Steve Antonede9ca52017-10-16 13:04:27 -07005867 ASSERT_TRUE(CreatePeerConnectionWrappers());
5868 // Each side will signal the session descriptions but not candidates.
5869 ConnectFakeSignalingForSdpOnly();
5870
5871 // Add audio video track and exchange the initial offer/answer with media
5872 // information only. This will start ICE gathering on each side.
Steve Anton15324772018-01-16 10:26:49 -08005873 caller()->AddAudioVideoTracks();
5874 callee()->AddAudioVideoTracks();
Steve Antonede9ca52017-10-16 13:04:27 -07005875 caller()->CreateAndSetAndSignalOffer();
5876
5877 // Wait for all candidates to be gathered on both the caller and callee.
5878 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceGatheringComplete,
5879 caller()->ice_gathering_state(), kDefaultTimeout);
5880 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceGatheringComplete,
5881 callee()->ice_gathering_state(), kDefaultTimeout);
5882
5883 // The candidates will now be included in the session description, so
5884 // signaling them will start the ICE connection.
5885 caller()->CreateAndSetAndSignalOffer();
5886 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5887
5888 // Ensure that media flows in both directions.
Seth Hampson2f0d7022018-02-20 11:54:42 -08005889 MediaExpectations media_expectations;
5890 media_expectations.ExpectBidirectionalAudioAndVideo();
5891 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Antonede9ca52017-10-16 13:04:27 -07005892}
5893
henrika5f6bf242017-11-01 11:06:56 +01005894// Test that SetAudioPlayout can be used to disable audio playout from the
5895// start, then later enable it. This may be useful, for example, if the caller
5896// needs to play a local ringtone until some event occurs, after which it
5897// switches to playing the received audio.
Seth Hampson2f0d7022018-02-20 11:54:42 -08005898TEST_P(PeerConnectionIntegrationTest, DisableAndEnableAudioPlayout) {
henrika5f6bf242017-11-01 11:06:56 +01005899 ASSERT_TRUE(CreatePeerConnectionWrappers());
5900 ConnectFakeSignaling();
5901
5902 // Set up audio-only call where audio playout is disabled on caller's side.
5903 caller()->pc()->SetAudioPlayout(false);
Steve Anton15324772018-01-16 10:26:49 -08005904 caller()->AddAudioTrack();
5905 callee()->AddAudioTrack();
henrika5f6bf242017-11-01 11:06:56 +01005906 caller()->CreateAndSetAndSignalOffer();
5907 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5908
5909 // Pump messages for a second.
5910 WAIT(false, 1000);
5911 // Since audio playout is disabled, the caller shouldn't have received
5912 // anything (at the playout level, at least).
5913 EXPECT_EQ(0, caller()->audio_frames_received());
5914 // As a sanity check, make sure the callee (for which playout isn't disabled)
5915 // did still see frames on its audio level.
5916 ASSERT_GT(callee()->audio_frames_received(), 0);
5917
5918 // Enable playout again, and ensure audio starts flowing.
5919 caller()->pc()->SetAudioPlayout(true);
Seth Hampson2f0d7022018-02-20 11:54:42 -08005920 MediaExpectations media_expectations;
5921 media_expectations.ExpectBidirectionalAudio();
5922 ASSERT_TRUE(ExpectNewFrames(media_expectations));
henrika5f6bf242017-11-01 11:06:56 +01005923}
5924
5925double GetAudioEnergyStat(PeerConnectionWrapper* pc) {
5926 auto report = pc->NewGetStats();
5927 auto track_stats_list =
5928 report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
5929 const webrtc::RTCMediaStreamTrackStats* remote_track_stats = nullptr;
5930 for (const auto* track_stats : track_stats_list) {
5931 if (track_stats->remote_source.is_defined() &&
5932 *track_stats->remote_source) {
5933 remote_track_stats = track_stats;
5934 break;
5935 }
5936 }
5937
5938 if (!remote_track_stats->total_audio_energy.is_defined()) {
5939 return 0.0;
5940 }
5941 return *remote_track_stats->total_audio_energy;
5942}
5943
5944// Test that if audio playout is disabled via the SetAudioPlayout() method, then
5945// incoming audio is still processed and statistics are generated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08005946TEST_P(PeerConnectionIntegrationTest,
henrika5f6bf242017-11-01 11:06:56 +01005947 DisableAudioPlayoutStillGeneratesAudioStats) {
5948 ASSERT_TRUE(CreatePeerConnectionWrappers());
5949 ConnectFakeSignaling();
5950
5951 // Set up audio-only call where playout is disabled but audio-processing is
5952 // still active.
Steve Anton15324772018-01-16 10:26:49 -08005953 caller()->AddAudioTrack();
5954 callee()->AddAudioTrack();
henrika5f6bf242017-11-01 11:06:56 +01005955 caller()->pc()->SetAudioPlayout(false);
5956
5957 caller()->CreateAndSetAndSignalOffer();
5958 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5959
5960 // Wait for the callee to receive audio stats.
5961 EXPECT_TRUE_WAIT(GetAudioEnergyStat(caller()) > 0, kMaxWaitForFramesMs);
5962}
5963
henrika4f167df2017-11-01 14:45:55 +01005964// Test that SetAudioRecording can be used to disable audio recording from the
5965// start, then later enable it. This may be useful, for example, if the caller
5966// wants to ensure that no audio resources are active before a certain state
5967// is reached.
Seth Hampson2f0d7022018-02-20 11:54:42 -08005968TEST_P(PeerConnectionIntegrationTest, DisableAndEnableAudioRecording) {
henrika4f167df2017-11-01 14:45:55 +01005969 ASSERT_TRUE(CreatePeerConnectionWrappers());
5970 ConnectFakeSignaling();
5971
5972 // Set up audio-only call where audio recording is disabled on caller's side.
5973 caller()->pc()->SetAudioRecording(false);
Steve Anton15324772018-01-16 10:26:49 -08005974 caller()->AddAudioTrack();
5975 callee()->AddAudioTrack();
henrika4f167df2017-11-01 14:45:55 +01005976 caller()->CreateAndSetAndSignalOffer();
5977 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5978
5979 // Pump messages for a second.
5980 WAIT(false, 1000);
5981 // Since caller has disabled audio recording, the callee shouldn't have
5982 // received anything.
5983 EXPECT_EQ(0, callee()->audio_frames_received());
5984 // As a sanity check, make sure the caller did still see frames on its
5985 // audio level since audio recording is enabled on the calle side.
5986 ASSERT_GT(caller()->audio_frames_received(), 0);
5987
5988 // Enable audio recording again, and ensure audio starts flowing.
5989 caller()->pc()->SetAudioRecording(true);
Seth Hampson2f0d7022018-02-20 11:54:42 -08005990 MediaExpectations media_expectations;
5991 media_expectations.ExpectBidirectionalAudio();
5992 ASSERT_TRUE(ExpectNewFrames(media_expectations));
henrika4f167df2017-11-01 14:45:55 +01005993}
5994
Taylor Brandstetter389a97c2018-01-03 16:26:06 -08005995// Test that after closing PeerConnections, they stop sending any packets (ICE,
5996// DTLS, RTP...).
Seth Hampson2f0d7022018-02-20 11:54:42 -08005997TEST_P(PeerConnectionIntegrationTest, ClosingConnectionStopsPacketFlow) {
Taylor Brandstetter389a97c2018-01-03 16:26:06 -08005998 // Set up audio/video/data, wait for some frames to be received.
5999 ASSERT_TRUE(CreatePeerConnectionWrappers());
6000 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08006001 caller()->AddAudioVideoTracks();
Taylor Brandstetter389a97c2018-01-03 16:26:06 -08006002#ifdef HAVE_SCTP
6003 caller()->CreateDataChannel();
6004#endif
6005 caller()->CreateAndSetAndSignalOffer();
6006 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08006007 MediaExpectations media_expectations;
6008 media_expectations.CalleeExpectsSomeAudioAndVideo();
6009 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Taylor Brandstetter389a97c2018-01-03 16:26:06 -08006010 // Close PeerConnections.
Steve Antond91969e2019-05-30 12:27:03 -07006011 ClosePeerConnections();
Taylor Brandstetter389a97c2018-01-03 16:26:06 -08006012 // Pump messages for a second, and ensure no new packets end up sent.
6013 uint32_t sent_packets_a = virtual_socket_server()->sent_packets();
6014 WAIT(false, 1000);
6015 uint32_t sent_packets_b = virtual_socket_server()->sent_packets();
6016 EXPECT_EQ(sent_packets_a, sent_packets_b);
6017}
6018
Steve Anton7eca0932018-03-30 15:18:41 -07006019// Test that transport stats are generated by the RTCStatsCollector for a
6020// connection that only involves data channels. This is a regression test for
6021// crbug.com/826972.
6022#ifdef HAVE_SCTP
6023TEST_P(PeerConnectionIntegrationTest,
6024 TransportStatsReportedForDataChannelOnlyConnection) {
6025 ASSERT_TRUE(CreatePeerConnectionWrappers());
6026 ConnectFakeSignaling();
6027 caller()->CreateDataChannel();
6028
6029 caller()->CreateAndSetAndSignalOffer();
6030 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
6031 ASSERT_TRUE_WAIT(callee()->data_channel(), kDefaultTimeout);
6032
6033 auto caller_report = caller()->NewGetStats();
6034 EXPECT_EQ(1u, caller_report->GetStatsOfType<RTCTransportStats>().size());
6035 auto callee_report = callee()->NewGetStats();
6036 EXPECT_EQ(1u, callee_report->GetStatsOfType<RTCTransportStats>().size());
6037}
6038#endif // HAVE_SCTP
6039
Qingsi Wang7685e862018-06-11 20:15:46 -07006040TEST_P(PeerConnectionIntegrationTest,
6041 IceEventsGeneratedAndLoggedInRtcEventLog) {
6042 ASSERT_TRUE(CreatePeerConnectionWrappersWithFakeRtcEventLog());
6043 ConnectFakeSignaling();
6044 PeerConnectionInterface::RTCOfferAnswerOptions options;
6045 options.offer_to_receive_audio = 1;
6046 caller()->SetOfferAnswerOptions(options);
6047 caller()->CreateAndSetAndSignalOffer();
6048 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
6049 ASSERT_NE(nullptr, caller()->event_log_factory());
6050 ASSERT_NE(nullptr, callee()->event_log_factory());
6051 webrtc::FakeRtcEventLog* caller_event_log =
6052 static_cast<webrtc::FakeRtcEventLog*>(
6053 caller()->event_log_factory()->last_log_created());
6054 webrtc::FakeRtcEventLog* callee_event_log =
6055 static_cast<webrtc::FakeRtcEventLog*>(
6056 callee()->event_log_factory()->last_log_created());
6057 ASSERT_NE(nullptr, caller_event_log);
6058 ASSERT_NE(nullptr, callee_event_log);
6059 int caller_ice_config_count = caller_event_log->GetEventCount(
6060 webrtc::RtcEvent::Type::IceCandidatePairConfig);
6061 int caller_ice_event_count = caller_event_log->GetEventCount(
6062 webrtc::RtcEvent::Type::IceCandidatePairEvent);
6063 int callee_ice_config_count = callee_event_log->GetEventCount(
6064 webrtc::RtcEvent::Type::IceCandidatePairConfig);
6065 int callee_ice_event_count = callee_event_log->GetEventCount(
6066 webrtc::RtcEvent::Type::IceCandidatePairEvent);
6067 EXPECT_LT(0, caller_ice_config_count);
6068 EXPECT_LT(0, caller_ice_event_count);
6069 EXPECT_LT(0, callee_ice_config_count);
6070 EXPECT_LT(0, callee_ice_event_count);
6071}
6072
Qingsi Wangc129c352019-04-18 10:41:58 -07006073TEST_P(PeerConnectionIntegrationTest, RegatherAfterChangingIceTransportType) {
Qingsi Wangc129c352019-04-18 10:41:58 -07006074 static const rtc::SocketAddress turn_server_internal_address{"88.88.88.0",
6075 3478};
6076 static const rtc::SocketAddress turn_server_external_address{"88.88.88.1", 0};
6077
6078 CreateTurnServer(turn_server_internal_address, turn_server_external_address);
6079
6080 webrtc::PeerConnectionInterface::IceServer ice_server;
6081 ice_server.urls.push_back("turn:88.88.88.0:3478");
6082 ice_server.username = "test";
6083 ice_server.password = "test";
6084
6085 PeerConnectionInterface::RTCConfiguration caller_config;
6086 caller_config.servers.push_back(ice_server);
6087 caller_config.type = webrtc::PeerConnectionInterface::kRelay;
6088 caller_config.continual_gathering_policy = PeerConnection::GATHER_CONTINUALLY;
Qingsi Wang1fe119f2019-05-31 16:55:33 -07006089 caller_config.surface_ice_candidates_on_ice_transport_type_changed = true;
Qingsi Wangc129c352019-04-18 10:41:58 -07006090
6091 PeerConnectionInterface::RTCConfiguration callee_config;
6092 callee_config.servers.push_back(ice_server);
6093 callee_config.type = webrtc::PeerConnectionInterface::kRelay;
6094 callee_config.continual_gathering_policy = PeerConnection::GATHER_CONTINUALLY;
Qingsi Wang1fe119f2019-05-31 16:55:33 -07006095 callee_config.surface_ice_candidates_on_ice_transport_type_changed = true;
Qingsi Wangc129c352019-04-18 10:41:58 -07006096
6097 ASSERT_TRUE(
6098 CreatePeerConnectionWrappersWithConfig(caller_config, callee_config));
6099
6100 // Do normal offer/answer and wait for ICE to complete.
6101 ConnectFakeSignaling();
6102 caller()->AddAudioVideoTracks();
6103 callee()->AddAudioVideoTracks();
6104 caller()->CreateAndSetAndSignalOffer();
6105 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
6106 // Since we are doing continual gathering, the ICE transport does not reach
6107 // kIceGatheringComplete (see
6108 // P2PTransportChannel::OnCandidatesAllocationDone), and consequently not
6109 // kIceConnectionComplete.
6110 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
6111 caller()->ice_connection_state(), kDefaultTimeout);
6112 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
6113 callee()->ice_connection_state(), kDefaultTimeout);
6114 // Note that we cannot use the metric
6115 // |WebRTC.PeerConnection.CandidatePairType_UDP| in this test since this
6116 // metric is only populated when we reach kIceConnectionComplete in the
6117 // current implementation.
6118 EXPECT_EQ(cricket::RELAY_PORT_TYPE,
6119 caller()->last_candidate_gathered().type());
6120 EXPECT_EQ(cricket::RELAY_PORT_TYPE,
6121 callee()->last_candidate_gathered().type());
6122
6123 // Loosen the caller's candidate filter.
6124 caller_config = caller()->pc()->GetConfiguration();
6125 caller_config.type = webrtc::PeerConnectionInterface::kAll;
6126 caller()->pc()->SetConfiguration(caller_config);
6127 // We should have gathered a new host candidate.
6128 EXPECT_EQ_WAIT(cricket::LOCAL_PORT_TYPE,
6129 caller()->last_candidate_gathered().type(), kDefaultTimeout);
6130
6131 // Loosen the callee's candidate filter.
6132 callee_config = callee()->pc()->GetConfiguration();
6133 callee_config.type = webrtc::PeerConnectionInterface::kAll;
6134 callee()->pc()->SetConfiguration(callee_config);
6135 EXPECT_EQ_WAIT(cricket::LOCAL_PORT_TYPE,
6136 callee()->last_candidate_gathered().type(), kDefaultTimeout);
Jonas Orelande3096512020-05-27 09:01:05 +02006137
6138 // Create an offer and verify that it does not contain an ICE restart (i.e new
6139 // ice credentials).
6140 std::string caller_ufrag_pre_offer = caller()
6141 ->pc()
6142 ->local_description()
6143 ->description()
6144 ->transport_infos()[0]
6145 .description.ice_ufrag;
6146 caller()->CreateAndSetAndSignalOffer();
6147 std::string caller_ufrag_post_offer = caller()
6148 ->pc()
6149 ->local_description()
6150 ->description()
6151 ->transport_infos()[0]
6152 .description.ice_ufrag;
6153 EXPECT_EQ(caller_ufrag_pre_offer, caller_ufrag_post_offer);
Qingsi Wangc129c352019-04-18 10:41:58 -07006154}
6155
Eldar Relloda13ea22019-06-01 12:23:43 +03006156TEST_P(PeerConnectionIntegrationTest, OnIceCandidateError) {
Eldar Relloda13ea22019-06-01 12:23:43 +03006157 static const rtc::SocketAddress turn_server_internal_address{"88.88.88.0",
6158 3478};
6159 static const rtc::SocketAddress turn_server_external_address{"88.88.88.1", 0};
6160
6161 CreateTurnServer(turn_server_internal_address, turn_server_external_address);
6162
6163 webrtc::PeerConnectionInterface::IceServer ice_server;
6164 ice_server.urls.push_back("turn:88.88.88.0:3478");
6165 ice_server.username = "test";
6166 ice_server.password = "123";
6167
6168 PeerConnectionInterface::RTCConfiguration caller_config;
6169 caller_config.servers.push_back(ice_server);
6170 caller_config.type = webrtc::PeerConnectionInterface::kRelay;
6171 caller_config.continual_gathering_policy = PeerConnection::GATHER_CONTINUALLY;
6172
6173 PeerConnectionInterface::RTCConfiguration callee_config;
6174 callee_config.servers.push_back(ice_server);
6175 callee_config.type = webrtc::PeerConnectionInterface::kRelay;
6176 callee_config.continual_gathering_policy = PeerConnection::GATHER_CONTINUALLY;
6177
6178 ASSERT_TRUE(
6179 CreatePeerConnectionWrappersWithConfig(caller_config, callee_config));
6180
6181 // Do normal offer/answer and wait for ICE to complete.
6182 ConnectFakeSignaling();
6183 caller()->AddAudioVideoTracks();
6184 callee()->AddAudioVideoTracks();
6185 caller()->CreateAndSetAndSignalOffer();
6186 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
6187 EXPECT_EQ_WAIT(401, caller()->error_event().error_code, kDefaultTimeout);
6188 EXPECT_EQ("Unauthorized", caller()->error_event().error_text);
6189 EXPECT_EQ("turn:88.88.88.0:3478?transport=udp", caller()->error_event().url);
Eldar Rello0095d372019-12-02 22:22:07 +02006190 EXPECT_NE(caller()->error_event().address, "");
Eldar Relloda13ea22019-06-01 12:23:43 +03006191}
6192
Eldar Rellofa8019c2020-05-14 11:59:33 +03006193TEST_P(PeerConnectionIntegrationTest, OnIceCandidateErrorWithEmptyAddress) {
6194 webrtc::PeerConnectionInterface::IceServer ice_server;
6195 ice_server.urls.push_back("turn:127.0.0.1:3478?transport=tcp");
6196 ice_server.username = "test";
6197 ice_server.password = "test";
6198
6199 PeerConnectionInterface::RTCConfiguration caller_config;
6200 caller_config.servers.push_back(ice_server);
6201 caller_config.type = webrtc::PeerConnectionInterface::kRelay;
6202 caller_config.continual_gathering_policy = PeerConnection::GATHER_CONTINUALLY;
6203
6204 PeerConnectionInterface::RTCConfiguration callee_config;
6205 callee_config.servers.push_back(ice_server);
6206 callee_config.type = webrtc::PeerConnectionInterface::kRelay;
6207 callee_config.continual_gathering_policy = PeerConnection::GATHER_CONTINUALLY;
6208
6209 ASSERT_TRUE(
6210 CreatePeerConnectionWrappersWithConfig(caller_config, callee_config));
6211
6212 // Do normal offer/answer and wait for ICE to complete.
6213 ConnectFakeSignaling();
6214 caller()->AddAudioVideoTracks();
6215 callee()->AddAudioVideoTracks();
6216 caller()->CreateAndSetAndSignalOffer();
6217 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
6218 EXPECT_EQ_WAIT(701, caller()->error_event().error_code, kDefaultTimeout);
6219 EXPECT_EQ(caller()->error_event().address, "");
6220}
6221
Eldar Rello5ab79e62019-10-09 18:29:44 +03006222TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
6223 AudioKeepsFlowingAfterImplicitRollback) {
6224 PeerConnectionInterface::RTCConfiguration config;
6225 config.sdp_semantics = SdpSemantics::kUnifiedPlan;
6226 config.enable_implicit_rollback = true;
6227 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
6228 ConnectFakeSignaling();
6229 caller()->AddAudioTrack();
6230 callee()->AddAudioTrack();
6231 caller()->CreateAndSetAndSignalOffer();
6232 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
6233 MediaExpectations media_expectations;
6234 media_expectations.ExpectBidirectionalAudio();
6235 ASSERT_TRUE(ExpectNewFrames(media_expectations));
6236 SetSignalIceCandidates(false); // Workaround candidate outrace sdp.
6237 caller()->AddVideoTrack();
6238 callee()->AddVideoTrack();
6239 rtc::scoped_refptr<MockSetSessionDescriptionObserver> observer(
6240 new rtc::RefCountedObject<MockSetSessionDescriptionObserver>());
6241 callee()->pc()->SetLocalDescription(observer,
6242 callee()->CreateOfferAndWait().release());
6243 EXPECT_TRUE_WAIT(observer->called(), kDefaultTimeout);
6244 caller()->CreateAndSetAndSignalOffer(); // Implicit rollback.
6245 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
6246 ASSERT_TRUE(ExpectNewFrames(media_expectations));
6247}
6248
6249TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
6250 ImplicitRollbackVisitsStableState) {
6251 RTCConfiguration config;
6252 config.sdp_semantics = SdpSemantics::kUnifiedPlan;
6253 config.enable_implicit_rollback = true;
6254
6255 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
6256
6257 rtc::scoped_refptr<MockSetSessionDescriptionObserver> sld_observer(
6258 new rtc::RefCountedObject<MockSetSessionDescriptionObserver>());
6259 callee()->pc()->SetLocalDescription(sld_observer,
6260 callee()->CreateOfferAndWait().release());
6261 EXPECT_TRUE_WAIT(sld_observer->called(), kDefaultTimeout);
6262 EXPECT_EQ(sld_observer->error(), "");
6263
6264 rtc::scoped_refptr<MockSetSessionDescriptionObserver> srd_observer(
6265 new rtc::RefCountedObject<MockSetSessionDescriptionObserver>());
6266 callee()->pc()->SetRemoteDescription(
6267 srd_observer, caller()->CreateOfferAndWait().release());
6268 EXPECT_TRUE_WAIT(srd_observer->called(), kDefaultTimeout);
6269 EXPECT_EQ(srd_observer->error(), "");
6270
6271 EXPECT_THAT(callee()->peer_connection_signaling_state_history(),
6272 ElementsAre(PeerConnectionInterface::kHaveLocalOffer,
6273 PeerConnectionInterface::kStable,
6274 PeerConnectionInterface::kHaveRemoteOffer));
6275}
6276
Mirko Bonadeic84f6612019-01-31 12:20:57 +01006277INSTANTIATE_TEST_SUITE_P(PeerConnectionIntegrationTest,
6278 PeerConnectionIntegrationTest,
6279 Values(SdpSemantics::kPlanB,
6280 SdpSemantics::kUnifiedPlan));
Steve Antond3679212018-01-17 17:41:02 -08006281
Yves Gerey100fe632020-01-17 19:15:53 +01006282INSTANTIATE_TEST_SUITE_P(PeerConnectionIntegrationTest,
6283 PeerConnectionIntegrationTestWithFakeClock,
6284 Values(SdpSemantics::kPlanB,
6285 SdpSemantics::kUnifiedPlan));
6286
Steve Anton74255ff2018-01-24 18:32:57 -08006287// Tests that verify interoperability between Plan B and Unified Plan
6288// PeerConnections.
6289class PeerConnectionIntegrationInteropTest
Seth Hampson2f0d7022018-02-20 11:54:42 -08006290 : public PeerConnectionIntegrationBaseTest,
Steve Anton74255ff2018-01-24 18:32:57 -08006291 public ::testing::WithParamInterface<
6292 std::tuple<SdpSemantics, SdpSemantics>> {
6293 protected:
Seth Hampson2f0d7022018-02-20 11:54:42 -08006294 // Setting the SdpSemantics for the base test to kDefault does not matter
6295 // because we specify not to use the test semantics when creating
6296 // PeerConnectionWrappers.
Steve Anton74255ff2018-01-24 18:32:57 -08006297 PeerConnectionIntegrationInteropTest()
Steve Anton3acffc32018-04-12 17:21:03 -07006298 : PeerConnectionIntegrationBaseTest(SdpSemantics::kPlanB),
Seth Hampson2f0d7022018-02-20 11:54:42 -08006299 caller_semantics_(std::get<0>(GetParam())),
Steve Anton74255ff2018-01-24 18:32:57 -08006300 callee_semantics_(std::get<1>(GetParam())) {}
6301
6302 bool CreatePeerConnectionWrappersWithSemantics() {
Steve Anton3acffc32018-04-12 17:21:03 -07006303 return CreatePeerConnectionWrappersWithSdpSemantics(caller_semantics_,
6304 callee_semantics_);
Steve Anton74255ff2018-01-24 18:32:57 -08006305 }
6306
6307 const SdpSemantics caller_semantics_;
6308 const SdpSemantics callee_semantics_;
6309};
6310
6311TEST_P(PeerConnectionIntegrationInteropTest, NoMediaLocalToNoMediaRemote) {
6312 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
6313 ConnectFakeSignaling();
6314
6315 caller()->CreateAndSetAndSignalOffer();
6316 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
6317}
6318
6319TEST_P(PeerConnectionIntegrationInteropTest, OneAudioLocalToNoMediaRemote) {
6320 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
6321 ConnectFakeSignaling();
6322 auto audio_sender = caller()->AddAudioTrack();
6323
6324 caller()->CreateAndSetAndSignalOffer();
6325 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
6326
6327 // Verify that one audio receiver has been created on the remote and that it
6328 // has the same track ID as the sending track.
6329 auto receivers = callee()->pc()->GetReceivers();
6330 ASSERT_EQ(1u, receivers.size());
6331 EXPECT_EQ(cricket::MEDIA_TYPE_AUDIO, receivers[0]->media_type());
6332 EXPECT_EQ(receivers[0]->track()->id(), audio_sender->track()->id());
6333
Seth Hampson2f0d7022018-02-20 11:54:42 -08006334 MediaExpectations media_expectations;
6335 media_expectations.CalleeExpectsSomeAudio();
6336 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08006337}
6338
6339TEST_P(PeerConnectionIntegrationInteropTest, OneAudioOneVideoToNoMediaRemote) {
6340 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
6341 ConnectFakeSignaling();
6342 auto video_sender = caller()->AddVideoTrack();
6343 auto audio_sender = caller()->AddAudioTrack();
6344
6345 caller()->CreateAndSetAndSignalOffer();
6346 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
6347
6348 // Verify that one audio and one video receiver have been created on the
6349 // remote and that they have the same track IDs as the sending tracks.
6350 auto audio_receivers =
6351 callee()->GetReceiversOfType(cricket::MEDIA_TYPE_AUDIO);
6352 ASSERT_EQ(1u, audio_receivers.size());
6353 EXPECT_EQ(audio_receivers[0]->track()->id(), audio_sender->track()->id());
6354 auto video_receivers =
6355 callee()->GetReceiversOfType(cricket::MEDIA_TYPE_VIDEO);
6356 ASSERT_EQ(1u, video_receivers.size());
6357 EXPECT_EQ(video_receivers[0]->track()->id(), video_sender->track()->id());
6358
Seth Hampson2f0d7022018-02-20 11:54:42 -08006359 MediaExpectations media_expectations;
6360 media_expectations.CalleeExpectsSomeAudioAndVideo();
6361 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08006362}
6363
6364TEST_P(PeerConnectionIntegrationInteropTest,
6365 OneAudioOneVideoLocalToOneAudioOneVideoRemote) {
6366 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
6367 ConnectFakeSignaling();
6368 caller()->AddAudioVideoTracks();
6369 callee()->AddAudioVideoTracks();
6370
6371 caller()->CreateAndSetAndSignalOffer();
6372 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
6373
Seth Hampson2f0d7022018-02-20 11:54:42 -08006374 MediaExpectations media_expectations;
6375 media_expectations.ExpectBidirectionalAudioAndVideo();
6376 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08006377}
6378
6379TEST_P(PeerConnectionIntegrationInteropTest,
6380 ReverseRolesOneAudioLocalToOneVideoRemote) {
6381 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
6382 ConnectFakeSignaling();
6383 caller()->AddAudioTrack();
6384 callee()->AddVideoTrack();
6385
6386 caller()->CreateAndSetAndSignalOffer();
6387 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
6388
6389 // Verify that only the audio track has been negotiated.
6390 EXPECT_EQ(0u, caller()->GetReceiversOfType(cricket::MEDIA_TYPE_VIDEO).size());
6391 // Might also check that the callee's NegotiationNeeded flag is set.
6392
6393 // Reverse roles.
6394 callee()->CreateAndSetAndSignalOffer();
6395 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
6396
Seth Hampson2f0d7022018-02-20 11:54:42 -08006397 MediaExpectations media_expectations;
6398 media_expectations.CallerExpectsSomeVideo();
6399 media_expectations.CalleeExpectsSomeAudio();
6400 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08006401}
6402
Mirko Bonadeic84f6612019-01-31 12:20:57 +01006403INSTANTIATE_TEST_SUITE_P(
Steve Antonba42e992018-04-09 14:10:01 -07006404 PeerConnectionIntegrationTest,
6405 PeerConnectionIntegrationInteropTest,
6406 Values(std::make_tuple(SdpSemantics::kPlanB, SdpSemantics::kUnifiedPlan),
6407 std::make_tuple(SdpSemantics::kUnifiedPlan, SdpSemantics::kPlanB)));
6408
6409// Test that if the Unified Plan side offers two video tracks then the Plan B
6410// side will only see the first one and ignore the second.
6411TEST_F(PeerConnectionIntegrationTestPlanB, TwoVideoUnifiedPlanToNoMediaPlanB) {
Steve Anton3acffc32018-04-12 17:21:03 -07006412 ASSERT_TRUE(CreatePeerConnectionWrappersWithSdpSemantics(
6413 SdpSemantics::kUnifiedPlan, SdpSemantics::kPlanB));
Steve Anton74255ff2018-01-24 18:32:57 -08006414 ConnectFakeSignaling();
6415 auto first_sender = caller()->AddVideoTrack();
6416 caller()->AddVideoTrack();
6417
6418 caller()->CreateAndSetAndSignalOffer();
6419 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
6420
6421 // Verify that there is only one receiver and it corresponds to the first
6422 // added track.
6423 auto receivers = callee()->pc()->GetReceivers();
6424 ASSERT_EQ(1u, receivers.size());
6425 EXPECT_TRUE(receivers[0]->track()->enabled());
6426 EXPECT_EQ(first_sender->track()->id(), receivers[0]->track()->id());
6427
Seth Hampson2f0d7022018-02-20 11:54:42 -08006428 MediaExpectations media_expectations;
6429 media_expectations.CalleeExpectsSomeVideo();
6430 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08006431}
6432
Steve Anton2bed3972019-01-04 17:04:30 -08006433// Test that if the initial offer tagged BUNDLE section is rejected due to its
6434// associated RtpTransceiver being stopped and another transceiver is added,
6435// then renegotiation causes the callee to receive the new video track without
6436// error.
6437// This is a regression test for bugs.webrtc.org/9954
6438TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
6439 ReOfferWithStoppedBundleTaggedTransceiver) {
6440 RTCConfiguration config;
6441 config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
6442 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
6443 ConnectFakeSignaling();
6444 auto audio_transceiver_or_error =
6445 caller()->pc()->AddTransceiver(caller()->CreateLocalAudioTrack());
6446 ASSERT_TRUE(audio_transceiver_or_error.ok());
6447 auto audio_transceiver = audio_transceiver_or_error.MoveValue();
6448
6449 caller()->CreateAndSetAndSignalOffer();
6450 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
6451 {
6452 MediaExpectations media_expectations;
6453 media_expectations.CalleeExpectsSomeAudio();
6454 ASSERT_TRUE(ExpectNewFrames(media_expectations));
6455 }
6456
6457 audio_transceiver->Stop();
6458 caller()->pc()->AddTransceiver(caller()->CreateLocalVideoTrack());
6459
6460 caller()->CreateAndSetAndSignalOffer();
6461 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
6462 {
6463 MediaExpectations media_expectations;
6464 media_expectations.CalleeExpectsSomeVideo();
6465 ASSERT_TRUE(ExpectNewFrames(media_expectations));
6466 }
6467}
6468
Harald Alvestrandd61f2a72019-05-08 20:20:59 +02006469#ifdef HAVE_SCTP
6470
6471TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
6472 EndToEndCallWithBundledSctpDataChannel) {
6473 ASSERT_TRUE(CreatePeerConnectionWrappers());
6474 ConnectFakeSignaling();
6475 caller()->CreateDataChannel();
6476 caller()->AddAudioVideoTracks();
6477 callee()->AddAudioVideoTracks();
Harald Alvestrandd61f2a72019-05-08 20:20:59 +02006478 caller()->CreateAndSetAndSignalOffer();
6479 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Harald Alvestrand17ea0682019-12-13 11:51:04 +01006480 ASSERT_EQ_WAIT(SctpTransportState::kConnected,
6481 caller()->pc()->GetSctpTransport()->Information().state(),
6482 kDefaultTimeout);
6483 ASSERT_TRUE_WAIT(callee()->data_channel(), kDefaultTimeout);
6484 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
6485}
6486
6487TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
6488 EndToEndCallWithDataChannelOnlyConnects) {
6489 ASSERT_TRUE(CreatePeerConnectionWrappers());
6490 ConnectFakeSignaling();
6491 caller()->CreateDataChannel();
6492 caller()->CreateAndSetAndSignalOffer();
6493 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
6494 ASSERT_TRUE_WAIT(callee()->data_channel(), kDefaultTimeout);
6495 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
6496 ASSERT_TRUE(caller()->data_observer()->IsOpen());
Harald Alvestrandd61f2a72019-05-08 20:20:59 +02006497}
6498
Harald Alvestrand2697ac12019-12-16 10:37:04 +01006499TEST_F(PeerConnectionIntegrationTestUnifiedPlan, DataChannelClosesWhenClosed) {
6500 ASSERT_TRUE(CreatePeerConnectionWrappers());
6501 ConnectFakeSignaling();
6502 caller()->CreateDataChannel();
6503 caller()->CreateAndSetAndSignalOffer();
6504 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
6505 ASSERT_TRUE_WAIT(callee()->data_observer(), kDefaultTimeout);
6506 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
6507 caller()->data_channel()->Close();
6508 ASSERT_TRUE_WAIT(!callee()->data_observer()->IsOpen(), kDefaultTimeout);
6509}
6510
6511TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
6512 DataChannelClosesWhenClosedReverse) {
6513 ASSERT_TRUE(CreatePeerConnectionWrappers());
6514 ConnectFakeSignaling();
6515 caller()->CreateDataChannel();
6516 caller()->CreateAndSetAndSignalOffer();
6517 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
6518 ASSERT_TRUE_WAIT(callee()->data_observer(), kDefaultTimeout);
6519 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
6520 callee()->data_channel()->Close();
6521 ASSERT_TRUE_WAIT(!caller()->data_observer()->IsOpen(), kDefaultTimeout);
6522}
6523
6524TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
6525 DataChannelClosesWhenPeerConnectionClosed) {
6526 ASSERT_TRUE(CreatePeerConnectionWrappers());
6527 ConnectFakeSignaling();
6528 caller()->CreateDataChannel();
6529 caller()->CreateAndSetAndSignalOffer();
6530 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
6531 ASSERT_TRUE_WAIT(callee()->data_observer(), kDefaultTimeout);
6532 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
6533 caller()->pc()->Close();
6534 ASSERT_TRUE_WAIT(!callee()->data_observer()->IsOpen(), kDefaultTimeout);
6535}
6536
Harald Alvestrandd61f2a72019-05-08 20:20:59 +02006537#endif // HAVE_SCTP
6538
deadbeef1dcb1642017-03-29 21:08:16 -07006539} // namespace
Mirko Bonadeiab64e8a2018-12-12 12:10:18 +01006540} // namespace webrtc
deadbeef1dcb1642017-03-29 21:08:16 -07006541
6542#endif // if !defined(THREAD_SANITIZER)