blob: df231f572c042286e106338e8e58843a6152a6c5 [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"
Qingsi Wang25ec8882019-11-15 12:33:05 -080039#include "p2p/base/fake_ice_transport.h"
Steve Anton10542f22019-01-11 09:11:00 -080040#include "p2p/base/mock_async_resolver.h"
41#include "p2p/base/p2p_constants.h"
42#include "p2p/base/port_interface.h"
43#include "p2p/base/test_stun_server.h"
44#include "p2p/base/test_turn_customizer.h"
45#include "p2p/base/test_turn_server.h"
46#include "p2p/client/basic_port_allocator.h"
47#include "pc/dtmf_sender.h"
48#include "pc/local_audio_source.h"
49#include "pc/media_session.h"
50#include "pc/peer_connection.h"
51#include "pc/peer_connection_factory.h"
52#include "pc/rtp_media_utils.h"
53#include "pc/session_description.h"
54#include "pc/test/fake_audio_capture_module.h"
55#include "pc/test/fake_periodic_video_track_source.h"
56#include "pc/test/fake_rtc_certificate_generator.h"
57#include "pc/test/fake_video_track_renderer.h"
58#include "pc/test/mock_peer_connection_observers.h"
Jonas Olssonb75d9e92019-02-22 10:33:29 +010059#include "rtc_base/fake_clock.h"
Qingsi Wangecd30542019-05-22 14:34:56 -070060#include "rtc_base/fake_mdns_responder.h"
Steve Anton10542f22019-01-11 09:11:00 -080061#include "rtc_base/fake_network.h"
62#include "rtc_base/firewall_socket_server.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020063#include "rtc_base/gunit.h"
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +020064#include "rtc_base/numerics/safe_conversions.h"
Steve Anton10542f22019-01-11 09:11:00 -080065#include "rtc_base/test_certificate_verifier.h"
66#include "rtc_base/time_utils.h"
67#include "rtc_base/virtual_socket_server.h"
Mirko Bonadei17f48782018-09-28 08:51:10 +020068#include "system_wrappers/include/metrics.h"
Qingsi Wangc129c352019-04-18 10:41:58 -070069#include "test/field_trial.h"
Elad Alon99c3fe52017-10-13 16:29:40 +020070#include "test/gmock.h"
deadbeef1dcb1642017-03-29 21:08:16 -070071
Mirko Bonadeiab64e8a2018-12-12 12:10:18 +010072namespace webrtc {
73namespace {
74
75using ::cricket::ContentInfo;
76using ::cricket::StreamParams;
77using ::rtc::SocketAddress;
78using ::testing::_;
Seth Hampson2f0d7022018-02-20 11:54:42 -080079using ::testing::Combine;
Steve Anton64b626b2019-01-28 17:25:26 -080080using ::testing::Contains;
Mirko Bonadeie46f5db2019-03-26 20:14:46 +010081using ::testing::DoAll;
Steve Antonede9ca52017-10-16 13:04:27 -070082using ::testing::ElementsAre;
Qingsi Wang1dac6d82018-12-12 15:28:47 -080083using ::testing::NiceMock;
Steve Anton64b626b2019-01-28 17:25:26 -080084using ::testing::Return;
Zach Stein6fcdc2f2018-08-23 16:25:55 -070085using ::testing::SetArgPointee;
Steve Antonffa6ce42018-11-30 09:26:08 -080086using ::testing::UnorderedElementsAreArray;
Mirko Bonadeiab64e8a2018-12-12 12:10:18 +010087using ::testing::Values;
Steve Anton74255ff2018-01-24 18:32:57 -080088using RTCConfiguration = PeerConnectionInterface::RTCConfiguration;
deadbeef1dcb1642017-03-29 21:08:16 -070089
90static const int kDefaultTimeout = 10000;
91static const int kMaxWaitForStatsMs = 3000;
92static const int kMaxWaitForActivationMs = 5000;
93static const int kMaxWaitForFramesMs = 10000;
94// Default number of audio/video frames to wait for before considering a test
95// successful.
96static const int kDefaultExpectedAudioFrameCount = 3;
97static const int kDefaultExpectedVideoFrameCount = 3;
98
deadbeef1dcb1642017-03-29 21:08:16 -070099static const char kDataChannelLabel[] = "data_channel";
100
101// SRTP cipher name negotiated by the tests. This must be updated if the
102// default changes.
Taylor Brandstetterfd350d72018-04-03 16:29:26 -0700103static const int kDefaultSrtpCryptoSuite = rtc::SRTP_AES128_CM_SHA1_80;
deadbeef1dcb1642017-03-29 21:08:16 -0700104static const int kDefaultSrtpCryptoSuiteGcm = rtc::SRTP_AEAD_AES_256_GCM;
105
Steve Antonede9ca52017-10-16 13:04:27 -0700106static const SocketAddress kDefaultLocalAddress("192.168.1.1", 0);
107
deadbeef1dcb1642017-03-29 21:08:16 -0700108// Helper function for constructing offer/answer options to initiate an ICE
109// restart.
110PeerConnectionInterface::RTCOfferAnswerOptions IceRestartOfferAnswerOptions() {
111 PeerConnectionInterface::RTCOfferAnswerOptions options;
112 options.ice_restart = true;
113 return options;
114}
115
deadbeefd8ad7882017-04-18 16:01:17 -0700116// Remove all stream information (SSRCs, track IDs, etc.) and "msid-semantic"
117// attribute from received SDP, simulating a legacy endpoint.
118void RemoveSsrcsAndMsids(cricket::SessionDescription* desc) {
119 for (ContentInfo& content : desc->contents()) {
Steve Antonb1c1de12017-12-21 15:14:30 -0800120 content.media_description()->mutable_streams().clear();
deadbeefd8ad7882017-04-18 16:01:17 -0700121 }
122 desc->set_msid_supported(false);
Henrik Boström5b147782018-12-04 11:25:05 +0100123 desc->set_msid_signaling(0);
deadbeefd8ad7882017-04-18 16:01:17 -0700124}
125
Seth Hampson5897a6e2018-04-03 11:16:33 -0700126// Removes all stream information besides the stream ids, simulating an
127// endpoint that only signals a=msid lines to convey stream_ids.
128void RemoveSsrcsAndKeepMsids(cricket::SessionDescription* desc) {
129 for (ContentInfo& content : desc->contents()) {
Steve Antondf527fd2018-04-27 15:52:03 -0700130 std::string track_id;
Seth Hampson5897a6e2018-04-03 11:16:33 -0700131 std::vector<std::string> stream_ids;
132 if (!content.media_description()->streams().empty()) {
Steve Antondf527fd2018-04-27 15:52:03 -0700133 const StreamParams& first_stream =
134 content.media_description()->streams()[0];
135 track_id = first_stream.id;
136 stream_ids = first_stream.stream_ids();
Seth Hampson5897a6e2018-04-03 11:16:33 -0700137 }
138 content.media_description()->mutable_streams().clear();
Steve Antondf527fd2018-04-27 15:52:03 -0700139 StreamParams new_stream;
140 new_stream.id = track_id;
Seth Hampson5897a6e2018-04-03 11:16:33 -0700141 new_stream.set_stream_ids(stream_ids);
142 content.media_description()->AddStream(new_stream);
143 }
144}
145
zhihuangf8164932017-05-19 13:09:47 -0700146int FindFirstMediaStatsIndexByKind(
147 const std::string& kind,
148 const std::vector<const webrtc::RTCMediaStreamTrackStats*>&
149 media_stats_vec) {
150 for (size_t i = 0; i < media_stats_vec.size(); i++) {
151 if (media_stats_vec[i]->kind.ValueToString() == kind) {
152 return i;
153 }
154 }
155 return -1;
156}
157
deadbeef1dcb1642017-03-29 21:08:16 -0700158class SignalingMessageReceiver {
159 public:
Steve Antona3a92c22017-12-07 10:27:41 -0800160 virtual void ReceiveSdpMessage(SdpType type, const std::string& msg) = 0;
deadbeef1dcb1642017-03-29 21:08:16 -0700161 virtual void ReceiveIceMessage(const std::string& sdp_mid,
162 int sdp_mline_index,
163 const std::string& msg) = 0;
164
165 protected:
166 SignalingMessageReceiver() {}
167 virtual ~SignalingMessageReceiver() {}
168};
169
170class MockRtpReceiverObserver : public webrtc::RtpReceiverObserverInterface {
171 public:
172 explicit MockRtpReceiverObserver(cricket::MediaType media_type)
173 : expected_media_type_(media_type) {}
174
175 void OnFirstPacketReceived(cricket::MediaType media_type) override {
176 ASSERT_EQ(expected_media_type_, media_type);
177 first_packet_received_ = true;
178 }
179
180 bool first_packet_received() const { return first_packet_received_; }
181
182 virtual ~MockRtpReceiverObserver() {}
183
184 private:
185 bool first_packet_received_ = false;
186 cricket::MediaType expected_media_type_;
187};
188
189// Helper class that wraps a peer connection, observes it, and can accept
190// signaling messages from another wrapper.
191//
192// Uses a fake network, fake A/V capture, and optionally fake
193// encoders/decoders, though they aren't used by default since they don't
194// advertise support of any codecs.
Steve Anton94286cb2017-09-26 16:20:19 -0700195// TODO(steveanton): See how this could become a subclass of
Seth Hampson2f0d7022018-02-20 11:54:42 -0800196// PeerConnectionWrapper defined in peerconnectionwrapper.h.
deadbeef1dcb1642017-03-29 21:08:16 -0700197class PeerConnectionWrapper : public webrtc::PeerConnectionObserver,
Steve Anton15324772018-01-16 10:26:49 -0800198 public SignalingMessageReceiver {
deadbeef1dcb1642017-03-29 21:08:16 -0700199 public:
200 // Different factory methods for convenience.
201 // TODO(deadbeef): Could use the pattern of:
202 //
203 // PeerConnectionWrapper =
204 // WrapperBuilder.WithConfig(...).WithOptions(...).build();
205 //
206 // To reduce some code duplication.
207 static PeerConnectionWrapper* CreateWithDtlsIdentityStore(
208 const std::string& debug_name,
209 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator,
210 rtc::Thread* network_thread,
211 rtc::Thread* worker_thread) {
212 PeerConnectionWrapper* client(new PeerConnectionWrapper(debug_name));
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700213 webrtc::PeerConnectionDependencies dependencies(nullptr);
214 dependencies.cert_generator = std::move(cert_generator);
Niels Möllerf06f9232018-08-07 12:32:18 +0200215 if (!client->Init(nullptr, nullptr, std::move(dependencies), network_thread,
Bjorn Mellem175aa2e2018-11-08 11:23:22 -0800216 worker_thread, nullptr,
Johannes Kron9bac68c2020-01-23 13:12:25 +0000217 /*media_transport_factory=*/nullptr,
218 /*reset_encoder_factory=*/false,
219 /*reset_decoder_factory=*/false)) {
deadbeef1dcb1642017-03-29 21:08:16 -0700220 delete client;
221 return nullptr;
222 }
223 return client;
224 }
225
deadbeef2f425aa2017-04-14 10:41:32 -0700226 webrtc::PeerConnectionFactoryInterface* pc_factory() const {
227 return peer_connection_factory_.get();
228 }
229
deadbeef1dcb1642017-03-29 21:08:16 -0700230 webrtc::PeerConnectionInterface* pc() const { return peer_connection_.get(); }
231
232 // If a signaling message receiver is set (via ConnectFakeSignaling), this
233 // will set the whole offer/answer exchange in motion. Just need to wait for
234 // the signaling state to reach "stable".
235 void CreateAndSetAndSignalOffer() {
Eldar Rello5ab79e62019-10-09 18:29:44 +0300236 auto offer = CreateOfferAndWait();
deadbeef1dcb1642017-03-29 21:08:16 -0700237 ASSERT_NE(nullptr, offer);
238 EXPECT_TRUE(SetLocalDescriptionAndSendSdpMessage(std::move(offer)));
239 }
240
241 // Sets the options to be used when CreateAndSetAndSignalOffer is called, or
242 // when a remote offer is received (via fake signaling) and an answer is
243 // generated. By default, uses default options.
244 void SetOfferAnswerOptions(
245 const PeerConnectionInterface::RTCOfferAnswerOptions& options) {
246 offer_answer_options_ = options;
247 }
248
249 // Set a callback to be invoked when SDP is received via the fake signaling
250 // channel, which provides an opportunity to munge (modify) the SDP. This is
251 // used to test SDP being applied that a PeerConnection would normally not
252 // generate, but a non-JSEP endpoint might.
253 void SetReceivedSdpMunger(
254 std::function<void(cricket::SessionDescription*)> munger) {
Mirko Bonadeic61ce0d2017-11-21 17:04:20 +0100255 received_sdp_munger_ = std::move(munger);
deadbeef1dcb1642017-03-29 21:08:16 -0700256 }
257
deadbeefc964d0b2017-04-03 10:03:35 -0700258 // Similar to the above, but this is run on SDP immediately after it's
deadbeef1dcb1642017-03-29 21:08:16 -0700259 // generated.
260 void SetGeneratedSdpMunger(
261 std::function<void(cricket::SessionDescription*)> munger) {
Mirko Bonadeic61ce0d2017-11-21 17:04:20 +0100262 generated_sdp_munger_ = std::move(munger);
deadbeef1dcb1642017-03-29 21:08:16 -0700263 }
264
Seth Hampson2f0d7022018-02-20 11:54:42 -0800265 // Set a callback to be invoked when a remote offer is received via the fake
266 // signaling channel. This provides an opportunity to change the
267 // PeerConnection state before an answer is created and sent to the caller.
268 void SetRemoteOfferHandler(std::function<void()> handler) {
269 remote_offer_handler_ = std::move(handler);
270 }
271
Qingsi Wang1dac6d82018-12-12 15:28:47 -0800272 void SetRemoteAsyncResolver(rtc::MockAsyncResolver* resolver) {
273 remote_async_resolver_ = resolver;
Zach Stein6fcdc2f2018-08-23 16:25:55 -0700274 }
275
Steve Antonede9ca52017-10-16 13:04:27 -0700276 // Every ICE connection state in order that has been seen by the observer.
277 std::vector<PeerConnectionInterface::IceConnectionState>
278 ice_connection_state_history() const {
279 return ice_connection_state_history_;
280 }
Steve Anton6f25b092017-10-23 09:39:20 -0700281 void clear_ice_connection_state_history() {
282 ice_connection_state_history_.clear();
283 }
Steve Antonede9ca52017-10-16 13:04:27 -0700284
Jonas Olssonacd8ae72019-02-25 15:26:24 +0100285 // Every standardized ICE connection state in order that has been seen by the
286 // observer.
287 std::vector<PeerConnectionInterface::IceConnectionState>
288 standardized_ice_connection_state_history() const {
289 return standardized_ice_connection_state_history_;
290 }
291
Jonas Olsson635474e2018-10-18 15:58:17 +0200292 // Every PeerConnection state in order that has been seen by the observer.
293 std::vector<PeerConnectionInterface::PeerConnectionState>
294 peer_connection_state_history() const {
295 return peer_connection_state_history_;
296 }
297
Steve Antonede9ca52017-10-16 13:04:27 -0700298 // Every ICE gathering state in order that has been seen by the observer.
299 std::vector<PeerConnectionInterface::IceGatheringState>
300 ice_gathering_state_history() const {
301 return ice_gathering_state_history_;
deadbeef1dcb1642017-03-29 21:08:16 -0700302 }
Alex Drake00c7ecf2019-08-06 10:54:47 -0700303 std::vector<cricket::CandidatePairChangeEvent>
304 ice_candidate_pair_change_history() const {
305 return ice_candidate_pair_change_history_;
306 }
deadbeef1dcb1642017-03-29 21:08:16 -0700307
Eldar Rello5ab79e62019-10-09 18:29:44 +0300308 // Every PeerConnection signaling state in order that has been seen by the
309 // observer.
310 std::vector<PeerConnectionInterface::SignalingState>
311 peer_connection_signaling_state_history() const {
312 return peer_connection_signaling_state_history_;
313 }
314
Steve Anton15324772018-01-16 10:26:49 -0800315 void AddAudioVideoTracks() {
316 AddAudioTrack();
317 AddVideoTrack();
deadbeef1dcb1642017-03-29 21:08:16 -0700318 }
319
Steve Anton74255ff2018-01-24 18:32:57 -0800320 rtc::scoped_refptr<RtpSenderInterface> AddAudioTrack() {
321 return AddTrack(CreateLocalAudioTrack());
322 }
deadbeef1dcb1642017-03-29 21:08:16 -0700323
Steve Anton74255ff2018-01-24 18:32:57 -0800324 rtc::scoped_refptr<RtpSenderInterface> AddVideoTrack() {
325 return AddTrack(CreateLocalVideoTrack());
326 }
deadbeef1dcb1642017-03-29 21:08:16 -0700327
328 rtc::scoped_refptr<webrtc::AudioTrackInterface> CreateLocalAudioTrack() {
Niels Möller2d02e082018-05-21 11:23:35 +0200329 cricket::AudioOptions options;
deadbeef1dcb1642017-03-29 21:08:16 -0700330 // Disable highpass filter so that we can get all the test audio frames.
Niels Möller2d02e082018-05-21 11:23:35 +0200331 options.highpass_filter = false;
deadbeef1dcb1642017-03-29 21:08:16 -0700332 rtc::scoped_refptr<webrtc::AudioSourceInterface> source =
Niels Möller2d02e082018-05-21 11:23:35 +0200333 peer_connection_factory_->CreateAudioSource(options);
deadbeef1dcb1642017-03-29 21:08:16 -0700334 // TODO(perkj): Test audio source when it is implemented. Currently audio
335 // always use the default input.
deadbeefb1a15d72017-09-07 14:12:05 -0700336 return peer_connection_factory_->CreateAudioTrack(rtc::CreateRandomUuid(),
deadbeef1dcb1642017-03-29 21:08:16 -0700337 source);
338 }
339
340 rtc::scoped_refptr<webrtc::VideoTrackInterface> CreateLocalVideoTrack() {
Johannes Kron965e7942018-09-13 15:36:20 +0200341 webrtc::FakePeriodicVideoSource::Config config;
342 config.timestamp_offset_ms = rtc::TimeMillis();
343 return CreateLocalVideoTrackInternal(config);
deadbeef1dcb1642017-03-29 21:08:16 -0700344 }
345
346 rtc::scoped_refptr<webrtc::VideoTrackInterface>
Niels Möller5c7efe72018-05-11 10:34:46 +0200347 CreateLocalVideoTrackWithConfig(
348 webrtc::FakePeriodicVideoSource::Config config) {
349 return CreateLocalVideoTrackInternal(config);
deadbeef1dcb1642017-03-29 21:08:16 -0700350 }
351
352 rtc::scoped_refptr<webrtc::VideoTrackInterface>
353 CreateLocalVideoTrackWithRotation(webrtc::VideoRotation rotation) {
Niels Möller5c7efe72018-05-11 10:34:46 +0200354 webrtc::FakePeriodicVideoSource::Config config;
355 config.rotation = rotation;
Johannes Kron965e7942018-09-13 15:36:20 +0200356 config.timestamp_offset_ms = rtc::TimeMillis();
Niels Möller5c7efe72018-05-11 10:34:46 +0200357 return CreateLocalVideoTrackInternal(config);
deadbeef1dcb1642017-03-29 21:08:16 -0700358 }
359
Steve Anton74255ff2018-01-24 18:32:57 -0800360 rtc::scoped_refptr<RtpSenderInterface> AddTrack(
361 rtc::scoped_refptr<MediaStreamTrackInterface> track,
Seth Hampson845e8782018-03-02 11:34:10 -0800362 const std::vector<std::string>& stream_ids = {}) {
363 auto result = pc()->AddTrack(track, stream_ids);
Steve Anton15324772018-01-16 10:26:49 -0800364 EXPECT_EQ(RTCErrorType::NONE, result.error().type());
Steve Anton74255ff2018-01-24 18:32:57 -0800365 return result.MoveValue();
366 }
367
368 std::vector<rtc::scoped_refptr<RtpReceiverInterface>> GetReceiversOfType(
369 cricket::MediaType media_type) {
370 std::vector<rtc::scoped_refptr<RtpReceiverInterface>> receivers;
Mirko Bonadei739baf02019-01-27 17:29:42 +0100371 for (const auto& receiver : pc()->GetReceivers()) {
Steve Anton74255ff2018-01-24 18:32:57 -0800372 if (receiver->media_type() == media_type) {
373 receivers.push_back(receiver);
374 }
375 }
376 return receivers;
deadbeef1dcb1642017-03-29 21:08:16 -0700377 }
378
Seth Hampson2f0d7022018-02-20 11:54:42 -0800379 rtc::scoped_refptr<RtpTransceiverInterface> GetFirstTransceiverOfType(
380 cricket::MediaType media_type) {
381 for (auto transceiver : pc()->GetTransceivers()) {
382 if (transceiver->receiver()->media_type() == media_type) {
383 return transceiver;
384 }
385 }
386 return nullptr;
387 }
388
deadbeef1dcb1642017-03-29 21:08:16 -0700389 bool SignalingStateStable() {
390 return pc()->signaling_state() == webrtc::PeerConnectionInterface::kStable;
391 }
392
393 void CreateDataChannel() { CreateDataChannel(nullptr); }
394
395 void CreateDataChannel(const webrtc::DataChannelInit* init) {
Steve Antonda6c0952017-10-23 11:41:54 -0700396 CreateDataChannel(kDataChannelLabel, init);
397 }
398
399 void CreateDataChannel(const std::string& label,
400 const webrtc::DataChannelInit* init) {
401 data_channel_ = pc()->CreateDataChannel(label, init);
deadbeef1dcb1642017-03-29 21:08:16 -0700402 ASSERT_TRUE(data_channel_.get() != nullptr);
403 data_observer_.reset(new MockDataChannelObserver(data_channel_));
404 }
405
406 DataChannelInterface* data_channel() { return data_channel_; }
407 const MockDataChannelObserver* data_observer() const {
408 return data_observer_.get();
409 }
410
411 int audio_frames_received() const {
412 return fake_audio_capture_module_->frames_received();
413 }
414
415 // Takes minimum of video frames received for each track.
416 //
417 // Can be used like:
418 // EXPECT_GE(expected_frames, min_video_frames_received_per_track());
419 //
420 // To ensure that all video tracks received at least a certain number of
421 // frames.
422 int min_video_frames_received_per_track() const {
423 int min_frames = INT_MAX;
Anders Carlsson5f2bb622018-05-14 09:48:06 +0200424 if (fake_video_renderers_.empty()) {
425 return 0;
deadbeef1dcb1642017-03-29 21:08:16 -0700426 }
deadbeef1dcb1642017-03-29 21:08:16 -0700427
Anders Carlsson5f2bb622018-05-14 09:48:06 +0200428 for (const auto& pair : fake_video_renderers_) {
429 min_frames = std::min(min_frames, pair.second->num_rendered_frames());
deadbeef1dcb1642017-03-29 21:08:16 -0700430 }
Anders Carlsson5f2bb622018-05-14 09:48:06 +0200431 return min_frames;
deadbeef1dcb1642017-03-29 21:08:16 -0700432 }
433
434 // Returns a MockStatsObserver in a state after stats gathering finished,
435 // which can be used to access the gathered stats.
deadbeefd8ad7882017-04-18 16:01:17 -0700436 rtc::scoped_refptr<MockStatsObserver> OldGetStatsForTrack(
deadbeef1dcb1642017-03-29 21:08:16 -0700437 webrtc::MediaStreamTrackInterface* track) {
438 rtc::scoped_refptr<MockStatsObserver> observer(
439 new rtc::RefCountedObject<MockStatsObserver>());
440 EXPECT_TRUE(peer_connection_->GetStats(
441 observer, nullptr, PeerConnectionInterface::kStatsOutputLevelStandard));
442 EXPECT_TRUE_WAIT(observer->called(), kDefaultTimeout);
443 return observer;
444 }
445
446 // Version that doesn't take a track "filter", and gathers all stats.
deadbeefd8ad7882017-04-18 16:01:17 -0700447 rtc::scoped_refptr<MockStatsObserver> OldGetStats() {
448 return OldGetStatsForTrack(nullptr);
449 }
450
451 // Synchronously gets stats and returns them. If it times out, fails the test
452 // and returns null.
453 rtc::scoped_refptr<const webrtc::RTCStatsReport> NewGetStats() {
454 rtc::scoped_refptr<webrtc::MockRTCStatsCollectorCallback> callback(
455 new rtc::RefCountedObject<webrtc::MockRTCStatsCollectorCallback>());
456 peer_connection_->GetStats(callback);
457 EXPECT_TRUE_WAIT(callback->called(), kDefaultTimeout);
458 return callback->report();
deadbeef1dcb1642017-03-29 21:08:16 -0700459 }
460
461 int rendered_width() {
462 EXPECT_FALSE(fake_video_renderers_.empty());
463 return fake_video_renderers_.empty()
464 ? 0
465 : fake_video_renderers_.begin()->second->width();
466 }
467
468 int rendered_height() {
469 EXPECT_FALSE(fake_video_renderers_.empty());
470 return fake_video_renderers_.empty()
471 ? 0
472 : fake_video_renderers_.begin()->second->height();
473 }
474
475 double rendered_aspect_ratio() {
476 if (rendered_height() == 0) {
477 return 0.0;
478 }
479 return static_cast<double>(rendered_width()) / rendered_height();
480 }
481
482 webrtc::VideoRotation rendered_rotation() {
483 EXPECT_FALSE(fake_video_renderers_.empty());
484 return fake_video_renderers_.empty()
485 ? webrtc::kVideoRotation_0
486 : fake_video_renderers_.begin()->second->rotation();
487 }
488
489 int local_rendered_width() {
490 return local_video_renderer_ ? local_video_renderer_->width() : 0;
491 }
492
493 int local_rendered_height() {
494 return local_video_renderer_ ? local_video_renderer_->height() : 0;
495 }
496
497 double local_rendered_aspect_ratio() {
498 if (local_rendered_height() == 0) {
499 return 0.0;
500 }
501 return static_cast<double>(local_rendered_width()) /
502 local_rendered_height();
503 }
504
505 size_t number_of_remote_streams() {
506 if (!pc()) {
507 return 0;
508 }
509 return pc()->remote_streams()->count();
510 }
511
512 StreamCollectionInterface* remote_streams() const {
513 if (!pc()) {
514 ADD_FAILURE();
515 return nullptr;
516 }
517 return pc()->remote_streams();
518 }
519
520 StreamCollectionInterface* local_streams() {
521 if (!pc()) {
522 ADD_FAILURE();
523 return nullptr;
524 }
525 return pc()->local_streams();
526 }
527
528 webrtc::PeerConnectionInterface::SignalingState signaling_state() {
529 return pc()->signaling_state();
530 }
531
532 webrtc::PeerConnectionInterface::IceConnectionState ice_connection_state() {
533 return pc()->ice_connection_state();
534 }
535
Jonas Olsson7a6739e2019-01-15 16:31:55 +0100536 webrtc::PeerConnectionInterface::IceConnectionState
537 standardized_ice_connection_state() {
538 return pc()->standardized_ice_connection_state();
539 }
540
deadbeef1dcb1642017-03-29 21:08:16 -0700541 webrtc::PeerConnectionInterface::IceGatheringState ice_gathering_state() {
542 return pc()->ice_gathering_state();
543 }
544
545 // Returns a MockRtpReceiverObserver for each RtpReceiver returned by
546 // GetReceivers. They're updated automatically when a remote offer/answer
547 // from the fake signaling channel is applied, or when
548 // ResetRtpReceiverObservers below is called.
549 const std::vector<std::unique_ptr<MockRtpReceiverObserver>>&
550 rtp_receiver_observers() {
551 return rtp_receiver_observers_;
552 }
553
554 void ResetRtpReceiverObservers() {
555 rtp_receiver_observers_.clear();
Mirko Bonadeic61ce0d2017-11-21 17:04:20 +0100556 for (const rtc::scoped_refptr<RtpReceiverInterface>& receiver :
557 pc()->GetReceivers()) {
deadbeef1dcb1642017-03-29 21:08:16 -0700558 std::unique_ptr<MockRtpReceiverObserver> observer(
559 new MockRtpReceiverObserver(receiver->media_type()));
560 receiver->SetObserver(observer.get());
561 rtp_receiver_observers_.push_back(std::move(observer));
562 }
563 }
564
Qingsi Wangecd30542019-05-22 14:34:56 -0700565 rtc::FakeNetworkManager* network_manager() const {
Steve Antonede9ca52017-10-16 13:04:27 -0700566 return fake_network_manager_.get();
567 }
568 cricket::PortAllocator* port_allocator() const { return port_allocator_; }
569
Qingsi Wang7685e862018-06-11 20:15:46 -0700570 webrtc::FakeRtcEventLogFactory* event_log_factory() const {
571 return event_log_factory_;
572 }
573
Qingsi Wangc129c352019-04-18 10:41:58 -0700574 const cricket::Candidate& last_candidate_gathered() const {
575 return last_candidate_gathered_;
576 }
Eldar Relloda13ea22019-06-01 12:23:43 +0300577 const cricket::IceCandidateErrorEvent& error_event() const {
578 return error_event_;
579 }
Qingsi Wangc129c352019-04-18 10:41:58 -0700580
Qingsi Wangecd30542019-05-22 14:34:56 -0700581 // Sets the mDNS responder for the owned fake network manager and keeps a
582 // reference to the responder.
583 void SetMdnsResponder(
584 std::unique_ptr<webrtc::FakeMdnsResponder> mdns_responder) {
585 RTC_DCHECK(mdns_responder != nullptr);
586 mdns_responder_ = mdns_responder.get();
587 network_manager()->set_mdns_responder(std::move(mdns_responder));
588 }
589
Eldar Rello5ab79e62019-10-09 18:29:44 +0300590 // Returns null on failure.
591 std::unique_ptr<SessionDescriptionInterface> CreateOfferAndWait() {
592 rtc::scoped_refptr<MockCreateSessionDescriptionObserver> observer(
593 new rtc::RefCountedObject<MockCreateSessionDescriptionObserver>());
594 pc()->CreateOffer(observer, offer_answer_options_);
595 return WaitForDescriptionFromObserver(observer);
596 }
597
deadbeef1dcb1642017-03-29 21:08:16 -0700598 private:
599 explicit PeerConnectionWrapper(const std::string& debug_name)
600 : debug_name_(debug_name) {}
601
Bjorn Mellem175aa2e2018-11-08 11:23:22 -0800602 bool Init(
603 const PeerConnectionFactory::Options* options,
604 const PeerConnectionInterface::RTCConfiguration* config,
605 webrtc::PeerConnectionDependencies dependencies,
606 rtc::Thread* network_thread,
607 rtc::Thread* worker_thread,
608 std::unique_ptr<webrtc::FakeRtcEventLogFactory> event_log_factory,
Johannes Kron9bac68c2020-01-23 13:12:25 +0000609 std::unique_ptr<webrtc::MediaTransportFactory> media_transport_factory,
610 bool reset_encoder_factory,
611 bool reset_decoder_factory) {
deadbeef1dcb1642017-03-29 21:08:16 -0700612 // There's an error in this test code if Init ends up being called twice.
613 RTC_DCHECK(!peer_connection_);
614 RTC_DCHECK(!peer_connection_factory_);
615
616 fake_network_manager_.reset(new rtc::FakeNetworkManager());
Steve Antonede9ca52017-10-16 13:04:27 -0700617 fake_network_manager_->AddInterface(kDefaultLocalAddress);
deadbeef1dcb1642017-03-29 21:08:16 -0700618
619 std::unique_ptr<cricket::PortAllocator> port_allocator(
620 new cricket::BasicPortAllocator(fake_network_manager_.get()));
Steve Antonede9ca52017-10-16 13:04:27 -0700621 port_allocator_ = port_allocator.get();
deadbeef1dcb1642017-03-29 21:08:16 -0700622 fake_audio_capture_module_ = FakeAudioCaptureModule::Create();
623 if (!fake_audio_capture_module_) {
624 return false;
625 }
deadbeef1dcb1642017-03-29 21:08:16 -0700626 rtc::Thread* const signaling_thread = rtc::Thread::Current();
Qingsi Wang7685e862018-06-11 20:15:46 -0700627
628 webrtc::PeerConnectionFactoryDependencies pc_factory_dependencies;
629 pc_factory_dependencies.network_thread = network_thread;
630 pc_factory_dependencies.worker_thread = worker_thread;
631 pc_factory_dependencies.signaling_thread = signaling_thread;
Danil Chapovalov9da25bd2019-06-20 10:19:42 +0200632 pc_factory_dependencies.task_queue_factory =
633 webrtc::CreateDefaultTaskQueueFactory();
634 cricket::MediaEngineDependencies media_deps;
635 media_deps.task_queue_factory =
636 pc_factory_dependencies.task_queue_factory.get();
637 media_deps.adm = fake_audio_capture_module_;
638 webrtc::SetMediaEngineDefaults(&media_deps);
Johannes Kron9bac68c2020-01-23 13:12:25 +0000639
640 if (reset_encoder_factory) {
641 media_deps.video_encoder_factory.reset();
642 }
643 if (reset_decoder_factory) {
644 media_deps.video_decoder_factory.reset();
645 }
646
Qingsi Wang7685e862018-06-11 20:15:46 -0700647 pc_factory_dependencies.media_engine =
Danil Chapovalov9da25bd2019-06-20 10:19:42 +0200648 cricket::CreateMediaEngine(std::move(media_deps));
Qingsi Wang7685e862018-06-11 20:15:46 -0700649 pc_factory_dependencies.call_factory = webrtc::CreateCallFactory();
650 if (event_log_factory) {
651 event_log_factory_ = event_log_factory.get();
652 pc_factory_dependencies.event_log_factory = std::move(event_log_factory);
653 } else {
654 pc_factory_dependencies.event_log_factory =
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200655 std::make_unique<webrtc::RtcEventLogFactory>(
Danil Chapovalov9da25bd2019-06-20 10:19:42 +0200656 pc_factory_dependencies.task_queue_factory.get());
Qingsi Wang7685e862018-06-11 20:15:46 -0700657 }
Bjorn Mellem175aa2e2018-11-08 11:23:22 -0800658 if (media_transport_factory) {
659 pc_factory_dependencies.media_transport_factory =
660 std::move(media_transport_factory);
661 }
Qingsi Wang7685e862018-06-11 20:15:46 -0700662 peer_connection_factory_ = webrtc::CreateModularPeerConnectionFactory(
663 std::move(pc_factory_dependencies));
664
deadbeef1dcb1642017-03-29 21:08:16 -0700665 if (!peer_connection_factory_) {
666 return false;
667 }
668 if (options) {
669 peer_connection_factory_->SetOptions(*options);
670 }
Seth Hampson2f0d7022018-02-20 11:54:42 -0800671 if (config) {
672 sdp_semantics_ = config->sdp_semantics;
673 }
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700674
675 dependencies.allocator = std::move(port_allocator);
Niels Möllerf06f9232018-08-07 12:32:18 +0200676 peer_connection_ = CreatePeerConnection(config, std::move(dependencies));
deadbeef1dcb1642017-03-29 21:08:16 -0700677 return peer_connection_.get() != nullptr;
678 }
679
680 rtc::scoped_refptr<webrtc::PeerConnectionInterface> CreatePeerConnection(
deadbeef1dcb1642017-03-29 21:08:16 -0700681 const PeerConnectionInterface::RTCConfiguration* config,
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700682 webrtc::PeerConnectionDependencies dependencies) {
deadbeef1dcb1642017-03-29 21:08:16 -0700683 PeerConnectionInterface::RTCConfiguration modified_config;
684 // If |config| is null, this will result in a default configuration being
685 // used.
686 if (config) {
687 modified_config = *config;
688 }
689 // Disable resolution adaptation; we don't want it interfering with the
690 // test results.
691 // TODO(deadbeef): Do something more robust. Since we're testing for aspect
692 // ratios and not specific resolutions, is this even necessary?
693 modified_config.set_cpu_adaptation(false);
694
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700695 dependencies.observer = this;
deadbeef1dcb1642017-03-29 21:08:16 -0700696 return peer_connection_factory_->CreatePeerConnection(
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700697 modified_config, std::move(dependencies));
deadbeef1dcb1642017-03-29 21:08:16 -0700698 }
699
700 void set_signaling_message_receiver(
701 SignalingMessageReceiver* signaling_message_receiver) {
702 signaling_message_receiver_ = signaling_message_receiver;
703 }
704
705 void set_signaling_delay_ms(int delay_ms) { signaling_delay_ms_ = delay_ms; }
706
Steve Antonede9ca52017-10-16 13:04:27 -0700707 void set_signal_ice_candidates(bool signal) {
708 signal_ice_candidates_ = signal;
709 }
710
deadbeef1dcb1642017-03-29 21:08:16 -0700711 rtc::scoped_refptr<webrtc::VideoTrackInterface> CreateLocalVideoTrackInternal(
Niels Möller5c7efe72018-05-11 10:34:46 +0200712 webrtc::FakePeriodicVideoSource::Config config) {
deadbeef1dcb1642017-03-29 21:08:16 -0700713 // Set max frame rate to 10fps to reduce the risk of test flakiness.
714 // TODO(deadbeef): Do something more robust.
Niels Möller5c7efe72018-05-11 10:34:46 +0200715 config.frame_interval_ms = 100;
deadbeef1dcb1642017-03-29 21:08:16 -0700716
Niels Möller5c7efe72018-05-11 10:34:46 +0200717 video_track_sources_.emplace_back(
Niels Möller0f405822018-05-17 09:16:41 +0200718 new rtc::RefCountedObject<webrtc::FakePeriodicVideoTrackSource>(
719 config, false /* remote */));
deadbeef1dcb1642017-03-29 21:08:16 -0700720 rtc::scoped_refptr<webrtc::VideoTrackInterface> track(
Niels Möller5c7efe72018-05-11 10:34:46 +0200721 peer_connection_factory_->CreateVideoTrack(
722 rtc::CreateRandomUuid(), video_track_sources_.back()));
deadbeef1dcb1642017-03-29 21:08:16 -0700723 if (!local_video_renderer_) {
724 local_video_renderer_.reset(new webrtc::FakeVideoTrackRenderer(track));
725 }
726 return track;
727 }
728
729 void HandleIncomingOffer(const std::string& msg) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100730 RTC_LOG(LS_INFO) << debug_name_ << ": HandleIncomingOffer";
Steve Antona3a92c22017-12-07 10:27:41 -0800731 std::unique_ptr<SessionDescriptionInterface> desc =
732 webrtc::CreateSessionDescription(SdpType::kOffer, msg);
deadbeef1dcb1642017-03-29 21:08:16 -0700733 if (received_sdp_munger_) {
734 received_sdp_munger_(desc->description());
735 }
736
737 EXPECT_TRUE(SetRemoteDescription(std::move(desc)));
738 // Setting a remote description may have changed the number of receivers,
739 // so reset the receiver observers.
740 ResetRtpReceiverObservers();
Seth Hampson2f0d7022018-02-20 11:54:42 -0800741 if (remote_offer_handler_) {
742 remote_offer_handler_();
743 }
deadbeef1dcb1642017-03-29 21:08:16 -0700744 auto answer = CreateAnswer();
745 ASSERT_NE(nullptr, answer);
746 EXPECT_TRUE(SetLocalDescriptionAndSendSdpMessage(std::move(answer)));
747 }
748
749 void HandleIncomingAnswer(const std::string& msg) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100750 RTC_LOG(LS_INFO) << debug_name_ << ": HandleIncomingAnswer";
Steve Antona3a92c22017-12-07 10:27:41 -0800751 std::unique_ptr<SessionDescriptionInterface> desc =
752 webrtc::CreateSessionDescription(SdpType::kAnswer, msg);
deadbeef1dcb1642017-03-29 21:08:16 -0700753 if (received_sdp_munger_) {
754 received_sdp_munger_(desc->description());
755 }
756
757 EXPECT_TRUE(SetRemoteDescription(std::move(desc)));
758 // Set the RtpReceiverObserver after receivers are created.
759 ResetRtpReceiverObservers();
760 }
761
762 // Returns null on failure.
deadbeef1dcb1642017-03-29 21:08:16 -0700763 std::unique_ptr<SessionDescriptionInterface> CreateAnswer() {
764 rtc::scoped_refptr<MockCreateSessionDescriptionObserver> observer(
765 new rtc::RefCountedObject<MockCreateSessionDescriptionObserver>());
766 pc()->CreateAnswer(observer, offer_answer_options_);
767 return WaitForDescriptionFromObserver(observer);
768 }
769
770 std::unique_ptr<SessionDescriptionInterface> WaitForDescriptionFromObserver(
Mirko Bonadeic61ce0d2017-11-21 17:04:20 +0100771 MockCreateSessionDescriptionObserver* observer) {
deadbeef1dcb1642017-03-29 21:08:16 -0700772 EXPECT_EQ_WAIT(true, observer->called(), kDefaultTimeout);
773 if (!observer->result()) {
774 return nullptr;
775 }
776 auto description = observer->MoveDescription();
777 if (generated_sdp_munger_) {
778 generated_sdp_munger_(description->description());
779 }
780 return description;
781 }
782
783 // Setting the local description and sending the SDP message over the fake
784 // signaling channel are combined into the same method because the SDP
785 // message needs to be sent as soon as SetLocalDescription finishes, without
786 // waiting for the observer to be called. This ensures that ICE candidates
787 // don't outrace the description.
788 bool SetLocalDescriptionAndSendSdpMessage(
789 std::unique_ptr<SessionDescriptionInterface> desc) {
790 rtc::scoped_refptr<MockSetSessionDescriptionObserver> observer(
791 new rtc::RefCountedObject<MockSetSessionDescriptionObserver>());
Mirko Bonadei675513b2017-11-09 11:09:25 +0100792 RTC_LOG(LS_INFO) << debug_name_ << ": SetLocalDescriptionAndSendSdpMessage";
Steve Antona3a92c22017-12-07 10:27:41 -0800793 SdpType type = desc->GetType();
deadbeef1dcb1642017-03-29 21:08:16 -0700794 std::string sdp;
795 EXPECT_TRUE(desc->ToString(&sdp));
Bjorn A Mellemb689af42019-08-21 10:44:59 -0700796 RTC_LOG(LS_INFO) << debug_name_ << ": local SDP contents=\n" << sdp;
deadbeef1dcb1642017-03-29 21:08:16 -0700797 pc()->SetLocalDescription(observer, desc.release());
Seth Hampson2f0d7022018-02-20 11:54:42 -0800798 if (sdp_semantics_ == SdpSemantics::kUnifiedPlan) {
799 RemoveUnusedVideoRenderers();
800 }
deadbeef1dcb1642017-03-29 21:08:16 -0700801 // As mentioned above, we need to send the message immediately after
802 // SetLocalDescription.
803 SendSdpMessage(type, sdp);
804 EXPECT_TRUE_WAIT(observer->called(), kDefaultTimeout);
805 return true;
806 }
807
808 bool SetRemoteDescription(std::unique_ptr<SessionDescriptionInterface> desc) {
809 rtc::scoped_refptr<MockSetSessionDescriptionObserver> observer(
810 new rtc::RefCountedObject<MockSetSessionDescriptionObserver>());
Mirko Bonadei675513b2017-11-09 11:09:25 +0100811 RTC_LOG(LS_INFO) << debug_name_ << ": SetRemoteDescription";
deadbeef1dcb1642017-03-29 21:08:16 -0700812 pc()->SetRemoteDescription(observer, desc.release());
Seth Hampson2f0d7022018-02-20 11:54:42 -0800813 if (sdp_semantics_ == SdpSemantics::kUnifiedPlan) {
814 RemoveUnusedVideoRenderers();
815 }
deadbeef1dcb1642017-03-29 21:08:16 -0700816 EXPECT_TRUE_WAIT(observer->called(), kDefaultTimeout);
817 return observer->result();
818 }
819
Seth Hampson2f0d7022018-02-20 11:54:42 -0800820 // This is a work around to remove unused fake_video_renderers from
821 // transceivers that have either stopped or are no longer receiving.
822 void RemoveUnusedVideoRenderers() {
823 auto transceivers = pc()->GetTransceivers();
824 for (auto& transceiver : transceivers) {
825 if (transceiver->receiver()->media_type() != cricket::MEDIA_TYPE_VIDEO) {
826 continue;
827 }
828 // Remove fake video renderers from any stopped transceivers.
829 if (transceiver->stopped()) {
830 auto it =
831 fake_video_renderers_.find(transceiver->receiver()->track()->id());
832 if (it != fake_video_renderers_.end()) {
833 fake_video_renderers_.erase(it);
834 }
835 }
836 // Remove fake video renderers from any transceivers that are no longer
837 // receiving.
838 if ((transceiver->current_direction() &&
839 !webrtc::RtpTransceiverDirectionHasRecv(
840 *transceiver->current_direction()))) {
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 }
848 }
849
deadbeef1dcb1642017-03-29 21:08:16 -0700850 // Simulate sending a blob of SDP with delay |signaling_delay_ms_| (0 by
851 // default).
Steve Antona3a92c22017-12-07 10:27:41 -0800852 void SendSdpMessage(SdpType type, const std::string& msg) {
deadbeef1dcb1642017-03-29 21:08:16 -0700853 if (signaling_delay_ms_ == 0) {
854 RelaySdpMessageIfReceiverExists(type, msg);
855 } else {
856 invoker_.AsyncInvokeDelayed<void>(
857 RTC_FROM_HERE, rtc::Thread::Current(),
858 rtc::Bind(&PeerConnectionWrapper::RelaySdpMessageIfReceiverExists,
859 this, type, msg),
860 signaling_delay_ms_);
861 }
862 }
863
Steve Antona3a92c22017-12-07 10:27:41 -0800864 void RelaySdpMessageIfReceiverExists(SdpType type, const std::string& msg) {
deadbeef1dcb1642017-03-29 21:08:16 -0700865 if (signaling_message_receiver_) {
866 signaling_message_receiver_->ReceiveSdpMessage(type, msg);
867 }
868 }
869
870 // Simulate trickling an ICE candidate with delay |signaling_delay_ms_| (0 by
871 // default).
872 void SendIceMessage(const std::string& sdp_mid,
873 int sdp_mline_index,
874 const std::string& msg) {
875 if (signaling_delay_ms_ == 0) {
876 RelayIceMessageIfReceiverExists(sdp_mid, sdp_mline_index, msg);
877 } else {
878 invoker_.AsyncInvokeDelayed<void>(
879 RTC_FROM_HERE, rtc::Thread::Current(),
880 rtc::Bind(&PeerConnectionWrapper::RelayIceMessageIfReceiverExists,
881 this, sdp_mid, sdp_mline_index, msg),
882 signaling_delay_ms_);
883 }
884 }
885
886 void RelayIceMessageIfReceiverExists(const std::string& sdp_mid,
887 int sdp_mline_index,
888 const std::string& msg) {
889 if (signaling_message_receiver_) {
890 signaling_message_receiver_->ReceiveIceMessage(sdp_mid, sdp_mline_index,
891 msg);
892 }
893 }
894
895 // SignalingMessageReceiver callbacks.
Steve Antona3a92c22017-12-07 10:27:41 -0800896 void ReceiveSdpMessage(SdpType type, const std::string& msg) override {
897 if (type == SdpType::kOffer) {
deadbeef1dcb1642017-03-29 21:08:16 -0700898 HandleIncomingOffer(msg);
899 } else {
900 HandleIncomingAnswer(msg);
901 }
902 }
903
904 void ReceiveIceMessage(const std::string& sdp_mid,
905 int sdp_mline_index,
906 const std::string& msg) override {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100907 RTC_LOG(LS_INFO) << debug_name_ << ": ReceiveIceMessage";
deadbeef1dcb1642017-03-29 21:08:16 -0700908 std::unique_ptr<webrtc::IceCandidateInterface> candidate(
909 webrtc::CreateIceCandidate(sdp_mid, sdp_mline_index, msg, nullptr));
910 EXPECT_TRUE(pc()->AddIceCandidate(candidate.get()));
911 }
912
913 // PeerConnectionObserver callbacks.
914 void OnSignalingChange(
915 webrtc::PeerConnectionInterface::SignalingState new_state) override {
916 EXPECT_EQ(pc()->signaling_state(), new_state);
Eldar Rello5ab79e62019-10-09 18:29:44 +0300917 peer_connection_signaling_state_history_.push_back(new_state);
deadbeef1dcb1642017-03-29 21:08:16 -0700918 }
Steve Anton15324772018-01-16 10:26:49 -0800919 void OnAddTrack(rtc::scoped_refptr<RtpReceiverInterface> receiver,
920 const std::vector<rtc::scoped_refptr<MediaStreamInterface>>&
921 streams) override {
922 if (receiver->media_type() == cricket::MEDIA_TYPE_VIDEO) {
923 rtc::scoped_refptr<VideoTrackInterface> video_track(
924 static_cast<VideoTrackInterface*>(receiver->track().get()));
925 ASSERT_TRUE(fake_video_renderers_.find(video_track->id()) ==
deadbeef1dcb1642017-03-29 21:08:16 -0700926 fake_video_renderers_.end());
Steve Anton15324772018-01-16 10:26:49 -0800927 fake_video_renderers_[video_track->id()] =
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200928 std::make_unique<FakeVideoTrackRenderer>(video_track);
deadbeef1dcb1642017-03-29 21:08:16 -0700929 }
930 }
Steve Anton15324772018-01-16 10:26:49 -0800931 void OnRemoveTrack(
932 rtc::scoped_refptr<RtpReceiverInterface> receiver) override {
933 if (receiver->media_type() == cricket::MEDIA_TYPE_VIDEO) {
934 auto it = fake_video_renderers_.find(receiver->track()->id());
935 RTC_DCHECK(it != fake_video_renderers_.end());
936 fake_video_renderers_.erase(it);
937 }
938 }
deadbeef1dcb1642017-03-29 21:08:16 -0700939 void OnRenegotiationNeeded() override {}
940 void OnIceConnectionChange(
941 webrtc::PeerConnectionInterface::IceConnectionState new_state) override {
942 EXPECT_EQ(pc()->ice_connection_state(), new_state);
Steve Antonede9ca52017-10-16 13:04:27 -0700943 ice_connection_state_history_.push_back(new_state);
deadbeef1dcb1642017-03-29 21:08:16 -0700944 }
Jonas Olssonacd8ae72019-02-25 15:26:24 +0100945 void OnStandardizedIceConnectionChange(
946 webrtc::PeerConnectionInterface::IceConnectionState new_state) override {
947 standardized_ice_connection_state_history_.push_back(new_state);
948 }
Jonas Olsson635474e2018-10-18 15:58:17 +0200949 void OnConnectionChange(
950 webrtc::PeerConnectionInterface::PeerConnectionState new_state) override {
951 peer_connection_state_history_.push_back(new_state);
952 }
953
deadbeef1dcb1642017-03-29 21:08:16 -0700954 void OnIceGatheringChange(
955 webrtc::PeerConnectionInterface::IceGatheringState new_state) override {
deadbeef1dcb1642017-03-29 21:08:16 -0700956 EXPECT_EQ(pc()->ice_gathering_state(), new_state);
Steve Antonede9ca52017-10-16 13:04:27 -0700957 ice_gathering_state_history_.push_back(new_state);
deadbeef1dcb1642017-03-29 21:08:16 -0700958 }
Alex Drake00c7ecf2019-08-06 10:54:47 -0700959
960 void OnIceSelectedCandidatePairChanged(
961 const cricket::CandidatePairChangeEvent& event) {
962 ice_candidate_pair_change_history_.push_back(event);
963 }
Alex Drake43faee02019-08-12 16:27:34 -0700964
deadbeef1dcb1642017-03-29 21:08:16 -0700965 void OnIceCandidate(const webrtc::IceCandidateInterface* candidate) override {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100966 RTC_LOG(LS_INFO) << debug_name_ << ": OnIceCandidate";
deadbeef1dcb1642017-03-29 21:08:16 -0700967
Qingsi Wang1dac6d82018-12-12 15:28:47 -0800968 if (remote_async_resolver_) {
969 const auto& local_candidate = candidate->candidate();
Qingsi Wang1dac6d82018-12-12 15:28:47 -0800970 if (local_candidate.address().IsUnresolvedIP()) {
971 RTC_DCHECK(local_candidate.type() == cricket::LOCAL_PORT_TYPE);
972 rtc::SocketAddress resolved_addr(local_candidate.address());
Qingsi Wangecd30542019-05-22 14:34:56 -0700973 const auto resolved_ip = mdns_responder_->GetMappedAddressForName(
Qingsi Wang1dac6d82018-12-12 15:28:47 -0800974 local_candidate.address().hostname());
975 RTC_DCHECK(!resolved_ip.IsNil());
976 resolved_addr.SetResolvedIP(resolved_ip);
977 EXPECT_CALL(*remote_async_resolver_, GetResolvedAddress(_, _))
978 .WillOnce(DoAll(SetArgPointee<1>(resolved_addr), Return(true)));
979 EXPECT_CALL(*remote_async_resolver_, Destroy(_));
Zach Stein6fcdc2f2018-08-23 16:25:55 -0700980 }
Zach Stein6fcdc2f2018-08-23 16:25:55 -0700981 }
982
deadbeef1dcb1642017-03-29 21:08:16 -0700983 std::string ice_sdp;
Qingsi Wang1dac6d82018-12-12 15:28:47 -0800984 EXPECT_TRUE(candidate->ToString(&ice_sdp));
Steve Antonede9ca52017-10-16 13:04:27 -0700985 if (signaling_message_receiver_ == nullptr || !signal_ice_candidates_) {
deadbeef1dcb1642017-03-29 21:08:16 -0700986 // Remote party may be deleted.
987 return;
988 }
Qingsi Wang1dac6d82018-12-12 15:28:47 -0800989 SendIceMessage(candidate->sdp_mid(), candidate->sdp_mline_index(), ice_sdp);
Qingsi Wangc129c352019-04-18 10:41:58 -0700990 last_candidate_gathered_ = candidate->candidate();
deadbeef1dcb1642017-03-29 21:08:16 -0700991 }
Eldar Rello0095d372019-12-02 22:22:07 +0200992 void OnIceCandidateError(const std::string& address,
993 int port,
Eldar Relloda13ea22019-06-01 12:23:43 +0300994 const std::string& url,
995 int error_code,
996 const std::string& error_text) override {
Eldar Rello0095d372019-12-02 22:22:07 +0200997 error_event_ = cricket::IceCandidateErrorEvent(address, port, url,
Eldar Relloda13ea22019-06-01 12:23:43 +0300998 error_code, error_text);
999 }
deadbeef1dcb1642017-03-29 21:08:16 -07001000 void OnDataChannel(
1001 rtc::scoped_refptr<DataChannelInterface> data_channel) override {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001002 RTC_LOG(LS_INFO) << debug_name_ << ": OnDataChannel";
deadbeef1dcb1642017-03-29 21:08:16 -07001003 data_channel_ = data_channel;
1004 data_observer_.reset(new MockDataChannelObserver(data_channel));
1005 }
1006
deadbeef1dcb1642017-03-29 21:08:16 -07001007 std::string debug_name_;
1008
1009 std::unique_ptr<rtc::FakeNetworkManager> fake_network_manager_;
Qingsi Wangecd30542019-05-22 14:34:56 -07001010 // Reference to the mDNS responder owned by |fake_network_manager_| after set.
1011 webrtc::FakeMdnsResponder* mdns_responder_ = nullptr;
deadbeef1dcb1642017-03-29 21:08:16 -07001012
1013 rtc::scoped_refptr<webrtc::PeerConnectionInterface> peer_connection_;
1014 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface>
1015 peer_connection_factory_;
1016
Steve Antonede9ca52017-10-16 13:04:27 -07001017 cricket::PortAllocator* port_allocator_;
deadbeef1dcb1642017-03-29 21:08:16 -07001018 // Needed to keep track of number of frames sent.
1019 rtc::scoped_refptr<FakeAudioCaptureModule> fake_audio_capture_module_;
1020 // Needed to keep track of number of frames received.
1021 std::map<std::string, std::unique_ptr<webrtc::FakeVideoTrackRenderer>>
1022 fake_video_renderers_;
1023 // Needed to ensure frames aren't received for removed tracks.
1024 std::vector<std::unique_ptr<webrtc::FakeVideoTrackRenderer>>
1025 removed_fake_video_renderers_;
deadbeef1dcb1642017-03-29 21:08:16 -07001026
1027 // For remote peer communication.
1028 SignalingMessageReceiver* signaling_message_receiver_ = nullptr;
1029 int signaling_delay_ms_ = 0;
Steve Antonede9ca52017-10-16 13:04:27 -07001030 bool signal_ice_candidates_ = true;
Qingsi Wangc129c352019-04-18 10:41:58 -07001031 cricket::Candidate last_candidate_gathered_;
Eldar Relloda13ea22019-06-01 12:23:43 +03001032 cricket::IceCandidateErrorEvent error_event_;
deadbeef1dcb1642017-03-29 21:08:16 -07001033
Niels Möller5c7efe72018-05-11 10:34:46 +02001034 // Store references to the video sources we've created, so that we can stop
deadbeef1dcb1642017-03-29 21:08:16 -07001035 // them, if required.
Niels Möller5c7efe72018-05-11 10:34:46 +02001036 std::vector<rtc::scoped_refptr<webrtc::VideoTrackSource>>
1037 video_track_sources_;
deadbeef1dcb1642017-03-29 21:08:16 -07001038 // |local_video_renderer_| attached to the first created local video track.
1039 std::unique_ptr<webrtc::FakeVideoTrackRenderer> local_video_renderer_;
1040
Seth Hampson2f0d7022018-02-20 11:54:42 -08001041 SdpSemantics sdp_semantics_;
deadbeef1dcb1642017-03-29 21:08:16 -07001042 PeerConnectionInterface::RTCOfferAnswerOptions offer_answer_options_;
1043 std::function<void(cricket::SessionDescription*)> received_sdp_munger_;
1044 std::function<void(cricket::SessionDescription*)> generated_sdp_munger_;
Seth Hampson2f0d7022018-02-20 11:54:42 -08001045 std::function<void()> remote_offer_handler_;
Qingsi Wang1dac6d82018-12-12 15:28:47 -08001046 rtc::MockAsyncResolver* remote_async_resolver_ = nullptr;
deadbeef1dcb1642017-03-29 21:08:16 -07001047 rtc::scoped_refptr<DataChannelInterface> data_channel_;
1048 std::unique_ptr<MockDataChannelObserver> data_observer_;
1049
1050 std::vector<std::unique_ptr<MockRtpReceiverObserver>> rtp_receiver_observers_;
1051
Steve Antonede9ca52017-10-16 13:04:27 -07001052 std::vector<PeerConnectionInterface::IceConnectionState>
1053 ice_connection_state_history_;
Jonas Olssonacd8ae72019-02-25 15:26:24 +01001054 std::vector<PeerConnectionInterface::IceConnectionState>
1055 standardized_ice_connection_state_history_;
Jonas Olsson635474e2018-10-18 15:58:17 +02001056 std::vector<PeerConnectionInterface::PeerConnectionState>
1057 peer_connection_state_history_;
Steve Antonede9ca52017-10-16 13:04:27 -07001058 std::vector<PeerConnectionInterface::IceGatheringState>
1059 ice_gathering_state_history_;
Alex Drake00c7ecf2019-08-06 10:54:47 -07001060 std::vector<cricket::CandidatePairChangeEvent>
1061 ice_candidate_pair_change_history_;
Eldar Rello5ab79e62019-10-09 18:29:44 +03001062 std::vector<PeerConnectionInterface::SignalingState>
1063 peer_connection_signaling_state_history_;
Qingsi Wang7685e862018-06-11 20:15:46 -07001064 webrtc::FakeRtcEventLogFactory* event_log_factory_;
1065
deadbeef1dcb1642017-03-29 21:08:16 -07001066 rtc::AsyncInvoker invoker_;
1067
Seth Hampson2f0d7022018-02-20 11:54:42 -08001068 friend class PeerConnectionIntegrationBaseTest;
deadbeef1dcb1642017-03-29 21:08:16 -07001069};
1070
Elad Alon99c3fe52017-10-13 16:29:40 +02001071class MockRtcEventLogOutput : public webrtc::RtcEventLogOutput {
1072 public:
1073 virtual ~MockRtcEventLogOutput() = default;
1074 MOCK_CONST_METHOD0(IsActive, bool());
1075 MOCK_METHOD1(Write, bool(const std::string&));
1076};
1077
Seth Hampson2f0d7022018-02-20 11:54:42 -08001078// This helper object is used for both specifying how many audio/video frames
1079// are expected to be received for a caller/callee. It provides helper functions
1080// to specify these expectations. The object initially starts in a state of no
1081// expectations.
1082class MediaExpectations {
1083 public:
1084 enum ExpectFrames {
1085 kExpectSomeFrames,
1086 kExpectNoFrames,
1087 kNoExpectation,
1088 };
1089
1090 void ExpectBidirectionalAudioAndVideo() {
1091 ExpectBidirectionalAudio();
1092 ExpectBidirectionalVideo();
1093 }
1094
1095 void ExpectBidirectionalAudio() {
1096 CallerExpectsSomeAudio();
1097 CalleeExpectsSomeAudio();
1098 }
1099
1100 void ExpectNoAudio() {
1101 CallerExpectsNoAudio();
1102 CalleeExpectsNoAudio();
1103 }
1104
1105 void ExpectBidirectionalVideo() {
1106 CallerExpectsSomeVideo();
1107 CalleeExpectsSomeVideo();
1108 }
1109
1110 void ExpectNoVideo() {
1111 CallerExpectsNoVideo();
1112 CalleeExpectsNoVideo();
1113 }
1114
1115 void CallerExpectsSomeAudioAndVideo() {
1116 CallerExpectsSomeAudio();
1117 CallerExpectsSomeVideo();
1118 }
1119
1120 void CalleeExpectsSomeAudioAndVideo() {
1121 CalleeExpectsSomeAudio();
1122 CalleeExpectsSomeVideo();
1123 }
1124
1125 // Caller's audio functions.
1126 void CallerExpectsSomeAudio(
1127 int expected_audio_frames = kDefaultExpectedAudioFrameCount) {
1128 caller_audio_expectation_ = kExpectSomeFrames;
1129 caller_audio_frames_expected_ = expected_audio_frames;
1130 }
1131
1132 void CallerExpectsNoAudio() {
1133 caller_audio_expectation_ = kExpectNoFrames;
1134 caller_audio_frames_expected_ = 0;
1135 }
1136
1137 // Caller's video functions.
1138 void CallerExpectsSomeVideo(
1139 int expected_video_frames = kDefaultExpectedVideoFrameCount) {
1140 caller_video_expectation_ = kExpectSomeFrames;
1141 caller_video_frames_expected_ = expected_video_frames;
1142 }
1143
1144 void CallerExpectsNoVideo() {
1145 caller_video_expectation_ = kExpectNoFrames;
1146 caller_video_frames_expected_ = 0;
1147 }
1148
1149 // Callee's audio functions.
1150 void CalleeExpectsSomeAudio(
1151 int expected_audio_frames = kDefaultExpectedAudioFrameCount) {
1152 callee_audio_expectation_ = kExpectSomeFrames;
1153 callee_audio_frames_expected_ = expected_audio_frames;
1154 }
1155
1156 void CalleeExpectsNoAudio() {
1157 callee_audio_expectation_ = kExpectNoFrames;
1158 callee_audio_frames_expected_ = 0;
1159 }
1160
1161 // Callee's video functions.
1162 void CalleeExpectsSomeVideo(
1163 int expected_video_frames = kDefaultExpectedVideoFrameCount) {
1164 callee_video_expectation_ = kExpectSomeFrames;
1165 callee_video_frames_expected_ = expected_video_frames;
1166 }
1167
1168 void CalleeExpectsNoVideo() {
1169 callee_video_expectation_ = kExpectNoFrames;
1170 callee_video_frames_expected_ = 0;
1171 }
1172
1173 ExpectFrames caller_audio_expectation_ = kNoExpectation;
1174 ExpectFrames caller_video_expectation_ = kNoExpectation;
1175 ExpectFrames callee_audio_expectation_ = kNoExpectation;
1176 ExpectFrames callee_video_expectation_ = kNoExpectation;
1177 int caller_audio_frames_expected_ = 0;
1178 int caller_video_frames_expected_ = 0;
1179 int callee_audio_frames_expected_ = 0;
1180 int callee_video_frames_expected_ = 0;
1181};
1182
Qingsi Wang25ec8882019-11-15 12:33:05 -08001183class MockIceTransport : public webrtc::IceTransportInterface {
1184 public:
1185 MockIceTransport(const std::string& name, int component)
1186 : internal_(std::make_unique<cricket::FakeIceTransport>(
1187 name,
1188 component,
1189 nullptr /* network_thread */)) {}
1190 ~MockIceTransport() = default;
1191 cricket::IceTransportInternal* internal() { return internal_.get(); }
1192
1193 private:
1194 std::unique_ptr<cricket::FakeIceTransport> internal_;
1195};
1196
1197class MockIceTransportFactory : public IceTransportFactory {
1198 public:
1199 ~MockIceTransportFactory() override = default;
1200 rtc::scoped_refptr<IceTransportInterface> CreateIceTransport(
1201 const std::string& transport_name,
1202 int component,
1203 IceTransportInit init) {
1204 RecordIceTransportCreated();
1205 return new rtc::RefCountedObject<MockIceTransport>(transport_name,
1206 component);
1207 }
1208 MOCK_METHOD0(RecordIceTransportCreated, void());
1209};
1210
deadbeef1dcb1642017-03-29 21:08:16 -07001211// Tests two PeerConnections connecting to each other end-to-end, using a
1212// virtual network, fake A/V capture and fake encoder/decoders. The
1213// PeerConnections share the threads/socket servers, but use separate versions
1214// of everything else (including "PeerConnectionFactory"s).
Mirko Bonadei6a489f22019-04-09 15:11:12 +02001215class PeerConnectionIntegrationBaseTest : public ::testing::Test {
deadbeef1dcb1642017-03-29 21:08:16 -07001216 public:
Seth Hampson2f0d7022018-02-20 11:54:42 -08001217 explicit PeerConnectionIntegrationBaseTest(SdpSemantics sdp_semantics)
1218 : sdp_semantics_(sdp_semantics),
1219 ss_(new rtc::VirtualSocketServer()),
Steve Antonede9ca52017-10-16 13:04:27 -07001220 fss_(new rtc::FirewallSocketServer(ss_.get())),
1221 network_thread_(new rtc::Thread(fss_.get())),
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001222 worker_thread_(rtc::Thread::Create()),
1223 loopback_media_transports_(network_thread_.get()) {
Sebastian Jansson8a793a02018-03-13 15:21:48 +01001224 network_thread_->SetName("PCNetworkThread", this);
1225 worker_thread_->SetName("PCWorkerThread", this);
deadbeef1dcb1642017-03-29 21:08:16 -07001226 RTC_CHECK(network_thread_->Start());
1227 RTC_CHECK(worker_thread_->Start());
Qingsi Wang7fc821d2018-07-12 12:54:53 -07001228 webrtc::metrics::Reset();
deadbeef1dcb1642017-03-29 21:08:16 -07001229 }
1230
Seth Hampson2f0d7022018-02-20 11:54:42 -08001231 ~PeerConnectionIntegrationBaseTest() {
Seth Hampsonaed71642018-06-11 07:41:32 -07001232 // The PeerConnections should deleted before the TurnCustomizers.
1233 // A TurnPort is created with a raw pointer to a TurnCustomizer. The
1234 // TurnPort has the same lifetime as the PeerConnection, so it's expected
1235 // that the TurnCustomizer outlives the life of the PeerConnection or else
1236 // when Send() is called it will hit a seg fault.
deadbeef1dcb1642017-03-29 21:08:16 -07001237 if (caller_) {
1238 caller_->set_signaling_message_receiver(nullptr);
Seth Hampsonaed71642018-06-11 07:41:32 -07001239 delete SetCallerPcWrapperAndReturnCurrent(nullptr);
deadbeef1dcb1642017-03-29 21:08:16 -07001240 }
1241 if (callee_) {
1242 callee_->set_signaling_message_receiver(nullptr);
Seth Hampsonaed71642018-06-11 07:41:32 -07001243 delete SetCalleePcWrapperAndReturnCurrent(nullptr);
deadbeef1dcb1642017-03-29 21:08:16 -07001244 }
Seth Hampsonaed71642018-06-11 07:41:32 -07001245
1246 // If turn servers were created for the test they need to be destroyed on
1247 // the network thread.
1248 network_thread()->Invoke<void>(RTC_FROM_HERE, [this] {
1249 turn_servers_.clear();
1250 turn_customizers_.clear();
1251 });
deadbeef1dcb1642017-03-29 21:08:16 -07001252 }
1253
1254 bool SignalingStateStable() {
1255 return caller_->SignalingStateStable() && callee_->SignalingStateStable();
1256 }
1257
deadbeef71452802017-05-07 17:21:01 -07001258 bool DtlsConnected() {
Alex Loiko9289eda2018-11-23 16:18:59 +00001259 // TODO(deadbeef): kIceConnectionConnected currently means both ICE and DTLS
1260 // are connected. This is an important distinction. Once we have separate
1261 // ICE and DTLS state, this check needs to use the DTLS state.
1262 return (callee()->ice_connection_state() ==
1263 webrtc::PeerConnectionInterface::kIceConnectionConnected ||
1264 callee()->ice_connection_state() ==
1265 webrtc::PeerConnectionInterface::kIceConnectionCompleted) &&
1266 (caller()->ice_connection_state() ==
1267 webrtc::PeerConnectionInterface::kIceConnectionConnected ||
1268 caller()->ice_connection_state() ==
1269 webrtc::PeerConnectionInterface::kIceConnectionCompleted);
deadbeef71452802017-05-07 17:21:01 -07001270 }
1271
Qingsi Wang7685e862018-06-11 20:15:46 -07001272 // When |event_log_factory| is null, the default implementation of the event
1273 // log factory will be used.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001274 std::unique_ptr<PeerConnectionWrapper> CreatePeerConnectionWrapper(
1275 const std::string& debug_name,
Seth Hampson2f0d7022018-02-20 11:54:42 -08001276 const PeerConnectionFactory::Options* options,
1277 const RTCConfiguration* config,
Qingsi Wang7685e862018-06-11 20:15:46 -07001278 webrtc::PeerConnectionDependencies dependencies,
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001279 std::unique_ptr<webrtc::FakeRtcEventLogFactory> event_log_factory,
Johannes Kron9bac68c2020-01-23 13:12:25 +00001280 std::unique_ptr<webrtc::MediaTransportFactory> media_transport_factory,
1281 bool reset_encoder_factory,
1282 bool reset_decoder_factory) {
Seth Hampson2f0d7022018-02-20 11:54:42 -08001283 RTCConfiguration modified_config;
1284 if (config) {
1285 modified_config = *config;
1286 }
Steve Anton3acffc32018-04-12 17:21:03 -07001287 modified_config.sdp_semantics = sdp_semantics_;
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001288 if (!dependencies.cert_generator) {
1289 dependencies.cert_generator =
Mirko Bonadei317a1f02019-09-17 17:06:18 +02001290 std::make_unique<FakeRTCCertificateGenerator>();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001291 }
1292 std::unique_ptr<PeerConnectionWrapper> client(
1293 new PeerConnectionWrapper(debug_name));
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001294
Niels Möllerf06f9232018-08-07 12:32:18 +02001295 if (!client->Init(options, &modified_config, std::move(dependencies),
1296 network_thread_.get(), worker_thread_.get(),
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001297 std::move(event_log_factory),
Johannes Kron9bac68c2020-01-23 13:12:25 +00001298 std::move(media_transport_factory), reset_encoder_factory,
1299 reset_decoder_factory)) {
Seth Hampson2f0d7022018-02-20 11:54:42 -08001300 return nullptr;
1301 }
1302 return client;
1303 }
1304
Qingsi Wang7685e862018-06-11 20:15:46 -07001305 std::unique_ptr<PeerConnectionWrapper>
1306 CreatePeerConnectionWrapperWithFakeRtcEventLog(
1307 const std::string& debug_name,
Qingsi Wang7685e862018-06-11 20:15:46 -07001308 const PeerConnectionFactory::Options* options,
1309 const RTCConfiguration* config,
1310 webrtc::PeerConnectionDependencies dependencies) {
1311 std::unique_ptr<webrtc::FakeRtcEventLogFactory> event_log_factory(
1312 new webrtc::FakeRtcEventLogFactory(rtc::Thread::Current()));
Johannes Kron9bac68c2020-01-23 13:12:25 +00001313 return CreatePeerConnectionWrapper(
1314 debug_name, options, config, std::move(dependencies),
1315 std::move(event_log_factory),
1316 /*media_transport_factory=*/nullptr, /*reset_encoder_factory=*/false,
1317 /*reset_decoder_factory=*/false);
Qingsi Wang7685e862018-06-11 20:15:46 -07001318 }
1319
deadbeef1dcb1642017-03-29 21:08:16 -07001320 bool CreatePeerConnectionWrappers() {
1321 return CreatePeerConnectionWrappersWithConfig(
1322 PeerConnectionInterface::RTCConfiguration(),
1323 PeerConnectionInterface::RTCConfiguration());
1324 }
1325
Steve Anton3acffc32018-04-12 17:21:03 -07001326 bool CreatePeerConnectionWrappersWithSdpSemantics(
1327 SdpSemantics caller_semantics,
1328 SdpSemantics callee_semantics) {
1329 // Can't specify the sdp_semantics in the passed-in configuration since it
1330 // will be overwritten by CreatePeerConnectionWrapper with whatever is
1331 // stored in sdp_semantics_. So get around this by modifying the instance
1332 // variable before calling CreatePeerConnectionWrapper for the caller and
1333 // callee PeerConnections.
1334 SdpSemantics original_semantics = sdp_semantics_;
1335 sdp_semantics_ = caller_semantics;
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001336 caller_ = CreatePeerConnectionWrapper(
Niels Möllerf06f9232018-08-07 12:32:18 +02001337 "Caller", nullptr, nullptr, webrtc::PeerConnectionDependencies(nullptr),
Johannes Kron9bac68c2020-01-23 13:12:25 +00001338 nullptr, /*media_transport_factory=*/nullptr,
1339 /*reset_encoder_factory=*/false,
1340 /*reset_decoder_factory=*/false);
Steve Anton3acffc32018-04-12 17:21:03 -07001341 sdp_semantics_ = callee_semantics;
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001342 callee_ = CreatePeerConnectionWrapper(
Niels Möllerf06f9232018-08-07 12:32:18 +02001343 "Callee", nullptr, nullptr, webrtc::PeerConnectionDependencies(nullptr),
Johannes Kron9bac68c2020-01-23 13:12:25 +00001344 nullptr, /*media_transport_factory=*/nullptr,
1345 /*reset_encoder_factory=*/false,
1346 /*reset_decoder_factory=*/false);
Steve Anton3acffc32018-04-12 17:21:03 -07001347 sdp_semantics_ = original_semantics;
1348 return caller_ && callee_;
1349 }
1350
deadbeef1dcb1642017-03-29 21:08:16 -07001351 bool CreatePeerConnectionWrappersWithConfig(
1352 const PeerConnectionInterface::RTCConfiguration& caller_config,
1353 const PeerConnectionInterface::RTCConfiguration& callee_config) {
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001354 caller_ = CreatePeerConnectionWrapper(
Niels Möllerf06f9232018-08-07 12:32:18 +02001355 "Caller", nullptr, &caller_config,
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001356 webrtc::PeerConnectionDependencies(nullptr), nullptr,
Johannes Kron9bac68c2020-01-23 13:12:25 +00001357 /*media_transport_factory=*/nullptr, /*reset_encoder_factory=*/false,
1358 /*reset_decoder_factory=*/false);
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001359 callee_ = CreatePeerConnectionWrapper(
Niels Möllerf06f9232018-08-07 12:32:18 +02001360 "Callee", nullptr, &callee_config,
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001361 webrtc::PeerConnectionDependencies(nullptr), nullptr,
Johannes Kron9bac68c2020-01-23 13:12:25 +00001362 /*media_transport_factory=*/nullptr, /*reset_encoder_factory=*/false,
1363 /*reset_decoder_factory=*/false);
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001364 return caller_ && callee_;
1365 }
1366
1367 bool CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
1368 const PeerConnectionInterface::RTCConfiguration& caller_config,
1369 const PeerConnectionInterface::RTCConfiguration& callee_config,
1370 std::unique_ptr<webrtc::MediaTransportFactory> caller_factory,
1371 std::unique_ptr<webrtc::MediaTransportFactory> callee_factory) {
Johannes Kron9bac68c2020-01-23 13:12:25 +00001372 caller_ = CreatePeerConnectionWrapper(
1373 "Caller", nullptr, &caller_config,
1374 webrtc::PeerConnectionDependencies(nullptr), nullptr,
1375 std::move(caller_factory), /*reset_encoder_factory=*/false,
1376 /*reset_decoder_factory=*/false);
1377 callee_ = CreatePeerConnectionWrapper(
1378 "Callee", nullptr, &callee_config,
1379 webrtc::PeerConnectionDependencies(nullptr), nullptr,
1380 std::move(callee_factory), /*reset_encoder_factory=*/false,
1381 /*reset_decoder_factory=*/false);
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001382 return caller_ && callee_;
1383 }
1384
1385 bool CreatePeerConnectionWrappersWithConfigAndDeps(
1386 const PeerConnectionInterface::RTCConfiguration& caller_config,
1387 webrtc::PeerConnectionDependencies caller_dependencies,
1388 const PeerConnectionInterface::RTCConfiguration& callee_config,
1389 webrtc::PeerConnectionDependencies callee_dependencies) {
Johannes Kron9bac68c2020-01-23 13:12:25 +00001390 caller_ = CreatePeerConnectionWrapper(
1391 "Caller", nullptr, &caller_config, std::move(caller_dependencies),
1392 nullptr,
1393 /*media_transport_factory=*/nullptr, /*reset_encoder_factory=*/false,
1394 /*reset_decoder_factory=*/false);
1395 callee_ = CreatePeerConnectionWrapper(
1396 "Callee", nullptr, &callee_config, std::move(callee_dependencies),
1397 nullptr,
1398 /*media_transport_factory=*/nullptr, /*reset_encoder_factory=*/false,
1399 /*reset_decoder_factory=*/false);
deadbeef1dcb1642017-03-29 21:08:16 -07001400 return caller_ && callee_;
1401 }
1402
1403 bool CreatePeerConnectionWrappersWithOptions(
1404 const PeerConnectionFactory::Options& caller_options,
1405 const PeerConnectionFactory::Options& callee_options) {
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001406 caller_ = CreatePeerConnectionWrapper(
Niels Möllerf06f9232018-08-07 12:32:18 +02001407 "Caller", &caller_options, nullptr,
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001408 webrtc::PeerConnectionDependencies(nullptr), nullptr,
Johannes Kron9bac68c2020-01-23 13:12:25 +00001409 /*media_transport_factory=*/nullptr, /*reset_encoder_factory=*/false,
1410 /*reset_decoder_factory=*/false);
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001411 callee_ = CreatePeerConnectionWrapper(
Niels Möllerf06f9232018-08-07 12:32:18 +02001412 "Callee", &callee_options, nullptr,
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001413 webrtc::PeerConnectionDependencies(nullptr), nullptr,
Johannes Kron9bac68c2020-01-23 13:12:25 +00001414 /*media_transport_factory=*/nullptr, /*reset_encoder_factory=*/false,
1415 /*reset_decoder_factory=*/false);
Qingsi Wang7685e862018-06-11 20:15:46 -07001416 return caller_ && callee_;
1417 }
1418
1419 bool CreatePeerConnectionWrappersWithFakeRtcEventLog() {
1420 PeerConnectionInterface::RTCConfiguration default_config;
1421 caller_ = CreatePeerConnectionWrapperWithFakeRtcEventLog(
Niels Möllerf06f9232018-08-07 12:32:18 +02001422 "Caller", nullptr, &default_config,
Qingsi Wang7685e862018-06-11 20:15:46 -07001423 webrtc::PeerConnectionDependencies(nullptr));
1424 callee_ = CreatePeerConnectionWrapperWithFakeRtcEventLog(
Niels Möllerf06f9232018-08-07 12:32:18 +02001425 "Callee", nullptr, &default_config,
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001426 webrtc::PeerConnectionDependencies(nullptr));
deadbeef1dcb1642017-03-29 21:08:16 -07001427 return caller_ && callee_;
1428 }
1429
Seth Hampson2f0d7022018-02-20 11:54:42 -08001430 std::unique_ptr<PeerConnectionWrapper>
1431 CreatePeerConnectionWrapperWithAlternateKey() {
deadbeef1dcb1642017-03-29 21:08:16 -07001432 std::unique_ptr<FakeRTCCertificateGenerator> cert_generator(
1433 new FakeRTCCertificateGenerator());
1434 cert_generator->use_alternate_key();
1435
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001436 webrtc::PeerConnectionDependencies dependencies(nullptr);
1437 dependencies.cert_generator = std::move(cert_generator);
Johannes Kron9bac68c2020-01-23 13:12:25 +00001438 return CreatePeerConnectionWrapper(
1439 "New Peer", nullptr, nullptr, std::move(dependencies), nullptr,
1440 /*media_transport_factory=*/nullptr, /*reset_encoder_factory=*/false,
1441 /*reset_decoder_factory=*/false);
1442 }
1443
1444 bool CreateOneDirectionalPeerConnectionWrappers(bool caller_to_callee) {
1445 caller_ = CreatePeerConnectionWrapper(
1446 "Caller", nullptr, nullptr, webrtc::PeerConnectionDependencies(nullptr),
1447 nullptr, /*media_transport_factory=*/nullptr,
1448 /*reset_encoder_factory=*/!caller_to_callee,
1449 /*reset_decoder_factory=*/caller_to_callee);
1450 callee_ = CreatePeerConnectionWrapper(
1451 "Callee", nullptr, nullptr, webrtc::PeerConnectionDependencies(nullptr),
1452 nullptr, /*media_transport_factory=*/nullptr,
1453 /*reset_encoder_factory=*/caller_to_callee,
1454 /*reset_decoder_factory=*/!caller_to_callee);
1455 return caller_ && callee_;
deadbeef1dcb1642017-03-29 21:08:16 -07001456 }
1457
Seth Hampsonaed71642018-06-11 07:41:32 -07001458 cricket::TestTurnServer* CreateTurnServer(
1459 rtc::SocketAddress internal_address,
1460 rtc::SocketAddress external_address,
1461 cricket::ProtocolType type = cricket::ProtocolType::PROTO_UDP,
1462 const std::string& common_name = "test turn server") {
1463 rtc::Thread* thread = network_thread();
1464 std::unique_ptr<cricket::TestTurnServer> turn_server =
1465 network_thread()->Invoke<std::unique_ptr<cricket::TestTurnServer>>(
1466 RTC_FROM_HERE,
1467 [thread, internal_address, external_address, type, common_name] {
Mirko Bonadei317a1f02019-09-17 17:06:18 +02001468 return std::make_unique<cricket::TestTurnServer>(
Seth Hampsonaed71642018-06-11 07:41:32 -07001469 thread, internal_address, external_address, type,
1470 /*ignore_bad_certs=*/true, common_name);
1471 });
1472 turn_servers_.push_back(std::move(turn_server));
1473 // Interactions with the turn server should be done on the network thread.
1474 return turn_servers_.back().get();
1475 }
1476
1477 cricket::TestTurnCustomizer* CreateTurnCustomizer() {
1478 std::unique_ptr<cricket::TestTurnCustomizer> turn_customizer =
1479 network_thread()->Invoke<std::unique_ptr<cricket::TestTurnCustomizer>>(
1480 RTC_FROM_HERE,
Mirko Bonadei317a1f02019-09-17 17:06:18 +02001481 [] { return std::make_unique<cricket::TestTurnCustomizer>(); });
Seth Hampsonaed71642018-06-11 07:41:32 -07001482 turn_customizers_.push_back(std::move(turn_customizer));
1483 // Interactions with the turn customizer should be done on the network
1484 // thread.
1485 return turn_customizers_.back().get();
1486 }
1487
1488 // Checks that the function counters for a TestTurnCustomizer are greater than
1489 // 0.
1490 void ExpectTurnCustomizerCountersIncremented(
1491 cricket::TestTurnCustomizer* turn_customizer) {
1492 unsigned int allow_channel_data_counter =
1493 network_thread()->Invoke<unsigned int>(
1494 RTC_FROM_HERE, [turn_customizer] {
1495 return turn_customizer->allow_channel_data_cnt_;
1496 });
1497 EXPECT_GT(allow_channel_data_counter, 0u);
1498 unsigned int modify_counter = network_thread()->Invoke<unsigned int>(
1499 RTC_FROM_HERE,
1500 [turn_customizer] { return turn_customizer->modify_cnt_; });
1501 EXPECT_GT(modify_counter, 0u);
1502 }
1503
deadbeef1dcb1642017-03-29 21:08:16 -07001504 // Once called, SDP blobs and ICE candidates will be automatically signaled
1505 // between PeerConnections.
1506 void ConnectFakeSignaling() {
1507 caller_->set_signaling_message_receiver(callee_.get());
1508 callee_->set_signaling_message_receiver(caller_.get());
1509 }
1510
Steve Antonede9ca52017-10-16 13:04:27 -07001511 // Once called, SDP blobs will be automatically signaled between
1512 // PeerConnections. Note that ICE candidates will not be signaled unless they
1513 // are in the exchanged SDP blobs.
1514 void ConnectFakeSignalingForSdpOnly() {
1515 ConnectFakeSignaling();
1516 SetSignalIceCandidates(false);
1517 }
1518
deadbeef1dcb1642017-03-29 21:08:16 -07001519 void SetSignalingDelayMs(int delay_ms) {
1520 caller_->set_signaling_delay_ms(delay_ms);
1521 callee_->set_signaling_delay_ms(delay_ms);
1522 }
1523
Steve Antonede9ca52017-10-16 13:04:27 -07001524 void SetSignalIceCandidates(bool signal) {
1525 caller_->set_signal_ice_candidates(signal);
1526 callee_->set_signal_ice_candidates(signal);
1527 }
1528
deadbeef1dcb1642017-03-29 21:08:16 -07001529 // Messages may get lost on the unreliable DataChannel, so we send multiple
1530 // times to avoid test flakiness.
1531 void SendRtpDataWithRetries(webrtc::DataChannelInterface* dc,
1532 const std::string& data,
1533 int retries) {
1534 for (int i = 0; i < retries; ++i) {
1535 dc->Send(DataBuffer(data));
1536 }
1537 }
1538
1539 rtc::Thread* network_thread() { return network_thread_.get(); }
1540
1541 rtc::VirtualSocketServer* virtual_socket_server() { return ss_.get(); }
1542
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001543 webrtc::MediaTransportPair* loopback_media_transports() {
1544 return &loopback_media_transports_;
1545 }
1546
deadbeef1dcb1642017-03-29 21:08:16 -07001547 PeerConnectionWrapper* caller() { return caller_.get(); }
1548
1549 // Set the |caller_| to the |wrapper| passed in and return the
1550 // original |caller_|.
1551 PeerConnectionWrapper* SetCallerPcWrapperAndReturnCurrent(
1552 PeerConnectionWrapper* wrapper) {
1553 PeerConnectionWrapper* old = caller_.release();
1554 caller_.reset(wrapper);
1555 return old;
1556 }
1557
1558 PeerConnectionWrapper* callee() { return callee_.get(); }
1559
1560 // Set the |callee_| to the |wrapper| passed in and return the
1561 // original |callee_|.
1562 PeerConnectionWrapper* SetCalleePcWrapperAndReturnCurrent(
1563 PeerConnectionWrapper* wrapper) {
1564 PeerConnectionWrapper* old = callee_.release();
1565 callee_.reset(wrapper);
1566 return old;
1567 }
1568
Qingsi Wang1dac6d82018-12-12 15:28:47 -08001569 void SetPortAllocatorFlags(uint32_t caller_flags, uint32_t callee_flags) {
1570 network_thread()->Invoke<void>(
1571 RTC_FROM_HERE, rtc::Bind(&cricket::PortAllocator::set_flags,
1572 caller()->port_allocator(), caller_flags));
1573 network_thread()->Invoke<void>(
1574 RTC_FROM_HERE, rtc::Bind(&cricket::PortAllocator::set_flags,
1575 callee()->port_allocator(), callee_flags));
1576 }
1577
Steve Antonede9ca52017-10-16 13:04:27 -07001578 rtc::FirewallSocketServer* firewall() const { return fss_.get(); }
1579
Seth Hampson2f0d7022018-02-20 11:54:42 -08001580 // Expects the provided number of new frames to be received within
1581 // kMaxWaitForFramesMs. The new expected frames are specified in
1582 // |media_expectations|. Returns false if any of the expectations were
1583 // not met.
1584 bool ExpectNewFrames(const MediaExpectations& media_expectations) {
1585 // First initialize the expected frame counts based upon the current
1586 // frame count.
1587 int total_caller_audio_frames_expected = caller()->audio_frames_received();
1588 if (media_expectations.caller_audio_expectation_ ==
1589 MediaExpectations::kExpectSomeFrames) {
1590 total_caller_audio_frames_expected +=
1591 media_expectations.caller_audio_frames_expected_;
1592 }
1593 int total_caller_video_frames_expected =
deadbeef1dcb1642017-03-29 21:08:16 -07001594 caller()->min_video_frames_received_per_track();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001595 if (media_expectations.caller_video_expectation_ ==
1596 MediaExpectations::kExpectSomeFrames) {
1597 total_caller_video_frames_expected +=
1598 media_expectations.caller_video_frames_expected_;
1599 }
1600 int total_callee_audio_frames_expected = callee()->audio_frames_received();
1601 if (media_expectations.callee_audio_expectation_ ==
1602 MediaExpectations::kExpectSomeFrames) {
1603 total_callee_audio_frames_expected +=
1604 media_expectations.callee_audio_frames_expected_;
1605 }
1606 int total_callee_video_frames_expected =
deadbeef1dcb1642017-03-29 21:08:16 -07001607 callee()->min_video_frames_received_per_track();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001608 if (media_expectations.callee_video_expectation_ ==
1609 MediaExpectations::kExpectSomeFrames) {
1610 total_callee_video_frames_expected +=
1611 media_expectations.callee_video_frames_expected_;
1612 }
deadbeef1dcb1642017-03-29 21:08:16 -07001613
Seth Hampson2f0d7022018-02-20 11:54:42 -08001614 // Wait for the expected frames.
deadbeef1dcb1642017-03-29 21:08:16 -07001615 EXPECT_TRUE_WAIT(caller()->audio_frames_received() >=
Seth Hampson2f0d7022018-02-20 11:54:42 -08001616 total_caller_audio_frames_expected &&
deadbeef1dcb1642017-03-29 21:08:16 -07001617 caller()->min_video_frames_received_per_track() >=
Seth Hampson2f0d7022018-02-20 11:54:42 -08001618 total_caller_video_frames_expected &&
deadbeef1dcb1642017-03-29 21:08:16 -07001619 callee()->audio_frames_received() >=
Seth Hampson2f0d7022018-02-20 11:54:42 -08001620 total_callee_audio_frames_expected &&
deadbeef1dcb1642017-03-29 21:08:16 -07001621 callee()->min_video_frames_received_per_track() >=
Seth Hampson2f0d7022018-02-20 11:54:42 -08001622 total_callee_video_frames_expected,
1623 kMaxWaitForFramesMs);
1624 bool expectations_correct =
1625 caller()->audio_frames_received() >=
1626 total_caller_audio_frames_expected &&
1627 caller()->min_video_frames_received_per_track() >=
1628 total_caller_video_frames_expected &&
1629 callee()->audio_frames_received() >=
1630 total_callee_audio_frames_expected &&
1631 callee()->min_video_frames_received_per_track() >=
1632 total_callee_video_frames_expected;
deadbeef1dcb1642017-03-29 21:08:16 -07001633
Seth Hampson2f0d7022018-02-20 11:54:42 -08001634 // After the combined wait, print out a more detailed message upon
1635 // failure.
deadbeef1dcb1642017-03-29 21:08:16 -07001636 EXPECT_GE(caller()->audio_frames_received(),
Seth Hampson2f0d7022018-02-20 11:54:42 -08001637 total_caller_audio_frames_expected);
deadbeef1dcb1642017-03-29 21:08:16 -07001638 EXPECT_GE(caller()->min_video_frames_received_per_track(),
Seth Hampson2f0d7022018-02-20 11:54:42 -08001639 total_caller_video_frames_expected);
deadbeef1dcb1642017-03-29 21:08:16 -07001640 EXPECT_GE(callee()->audio_frames_received(),
Seth Hampson2f0d7022018-02-20 11:54:42 -08001641 total_callee_audio_frames_expected);
deadbeef1dcb1642017-03-29 21:08:16 -07001642 EXPECT_GE(callee()->min_video_frames_received_per_track(),
Seth Hampson2f0d7022018-02-20 11:54:42 -08001643 total_callee_video_frames_expected);
1644
1645 // We want to make sure nothing unexpected was received.
1646 if (media_expectations.caller_audio_expectation_ ==
1647 MediaExpectations::kExpectNoFrames) {
1648 EXPECT_EQ(caller()->audio_frames_received(),
1649 total_caller_audio_frames_expected);
1650 if (caller()->audio_frames_received() !=
1651 total_caller_audio_frames_expected) {
1652 expectations_correct = false;
1653 }
1654 }
1655 if (media_expectations.caller_video_expectation_ ==
1656 MediaExpectations::kExpectNoFrames) {
1657 EXPECT_EQ(caller()->min_video_frames_received_per_track(),
1658 total_caller_video_frames_expected);
1659 if (caller()->min_video_frames_received_per_track() !=
1660 total_caller_video_frames_expected) {
1661 expectations_correct = false;
1662 }
1663 }
1664 if (media_expectations.callee_audio_expectation_ ==
1665 MediaExpectations::kExpectNoFrames) {
1666 EXPECT_EQ(callee()->audio_frames_received(),
1667 total_callee_audio_frames_expected);
1668 if (callee()->audio_frames_received() !=
1669 total_callee_audio_frames_expected) {
1670 expectations_correct = false;
1671 }
1672 }
1673 if (media_expectations.callee_video_expectation_ ==
1674 MediaExpectations::kExpectNoFrames) {
1675 EXPECT_EQ(callee()->min_video_frames_received_per_track(),
1676 total_callee_video_frames_expected);
1677 if (callee()->min_video_frames_received_per_track() !=
1678 total_callee_video_frames_expected) {
1679 expectations_correct = false;
1680 }
1681 }
1682 return expectations_correct;
deadbeef1dcb1642017-03-29 21:08:16 -07001683 }
1684
Steve Antond91969e2019-05-30 12:27:03 -07001685 void ClosePeerConnections() {
1686 caller()->pc()->Close();
1687 callee()->pc()->Close();
1688 }
1689
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07001690 void TestNegotiatedCipherSuite(
1691 const PeerConnectionFactory::Options& caller_options,
1692 const PeerConnectionFactory::Options& callee_options,
1693 int expected_cipher_suite) {
deadbeef1dcb1642017-03-29 21:08:16 -07001694 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(caller_options,
1695 callee_options));
deadbeef1dcb1642017-03-29 21:08:16 -07001696 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001697 caller()->AddAudioVideoTracks();
1698 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001699 caller()->CreateAndSetAndSignalOffer();
Qingsi Wang7fc821d2018-07-12 12:54:53 -07001700 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07001701 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(expected_cipher_suite),
deadbeefd8ad7882017-04-18 16:01:17 -07001702 caller()->OldGetStats()->SrtpCipher(), kDefaultTimeout);
Qingsi Wang7fc821d2018-07-12 12:54:53 -07001703 // TODO(bugs.webrtc.org/9456): Fix it.
Ying Wangef3998f2019-12-09 13:06:53 +01001704 EXPECT_METRIC_EQ(1, webrtc::metrics::NumEvents(
1705 "WebRTC.PeerConnection.SrtpCryptoSuite.Audio",
1706 expected_cipher_suite));
deadbeef1dcb1642017-03-29 21:08:16 -07001707 }
1708
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07001709 void TestGcmNegotiationUsesCipherSuite(bool local_gcm_enabled,
1710 bool remote_gcm_enabled,
Philipp Hancke2ebbff82019-10-26 06:12:55 +02001711 bool aes_ctr_enabled,
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07001712 int expected_cipher_suite) {
1713 PeerConnectionFactory::Options caller_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07001714 caller_options.crypto_options.srtp.enable_gcm_crypto_suites =
1715 local_gcm_enabled;
Philipp Hancke2ebbff82019-10-26 06:12:55 +02001716 caller_options.crypto_options.srtp.enable_aes128_sha1_80_crypto_cipher =
1717 aes_ctr_enabled;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07001718 PeerConnectionFactory::Options callee_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07001719 callee_options.crypto_options.srtp.enable_gcm_crypto_suites =
1720 remote_gcm_enabled;
Philipp Hancke2ebbff82019-10-26 06:12:55 +02001721 callee_options.crypto_options.srtp.enable_aes128_sha1_80_crypto_cipher =
1722 aes_ctr_enabled;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07001723 TestNegotiatedCipherSuite(caller_options, callee_options,
1724 expected_cipher_suite);
1725 }
1726
Seth Hampson2f0d7022018-02-20 11:54:42 -08001727 protected:
Steve Anton3acffc32018-04-12 17:21:03 -07001728 SdpSemantics sdp_semantics_;
Seth Hampson2f0d7022018-02-20 11:54:42 -08001729
deadbeef1dcb1642017-03-29 21:08:16 -07001730 private:
1731 // |ss_| is used by |network_thread_| so it must be destroyed later.
deadbeef1dcb1642017-03-29 21:08:16 -07001732 std::unique_ptr<rtc::VirtualSocketServer> ss_;
Steve Antonede9ca52017-10-16 13:04:27 -07001733 std::unique_ptr<rtc::FirewallSocketServer> fss_;
deadbeef1dcb1642017-03-29 21:08:16 -07001734 // |network_thread_| and |worker_thread_| are used by both
1735 // |caller_| and |callee_| so they must be destroyed
1736 // later.
1737 std::unique_ptr<rtc::Thread> network_thread_;
1738 std::unique_ptr<rtc::Thread> worker_thread_;
Seth Hampsonaed71642018-06-11 07:41:32 -07001739 // The turn servers and turn customizers should be accessed & deleted on the
1740 // network thread to avoid a race with the socket read/write that occurs
1741 // on the network thread.
1742 std::vector<std::unique_ptr<cricket::TestTurnServer>> turn_servers_;
1743 std::vector<std::unique_ptr<cricket::TestTurnCustomizer>> turn_customizers_;
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001744 webrtc::MediaTransportPair loopback_media_transports_;
deadbeef1dcb1642017-03-29 21:08:16 -07001745 std::unique_ptr<PeerConnectionWrapper> caller_;
1746 std::unique_ptr<PeerConnectionWrapper> callee_;
1747};
1748
Seth Hampson2f0d7022018-02-20 11:54:42 -08001749class PeerConnectionIntegrationTest
1750 : public PeerConnectionIntegrationBaseTest,
1751 public ::testing::WithParamInterface<SdpSemantics> {
1752 protected:
1753 PeerConnectionIntegrationTest()
1754 : PeerConnectionIntegrationBaseTest(GetParam()) {}
1755};
1756
Yves Gerey100fe632020-01-17 19:15:53 +01001757// Fake clock must be set before threads are started to prevent race on
1758// Set/GetClockForTesting().
1759// To achieve that, multiple inheritance is used as a mixin pattern
1760// where order of construction is finely controlled.
1761// This also ensures peerconnection is closed before switching back to non-fake
1762// clock, avoiding other races and DCHECK failures such as in rtp_sender.cc.
1763class FakeClockForTest : public rtc::ScopedFakeClock {
1764 protected:
1765 FakeClockForTest() {
1766 // Some things use a time of "0" as a special value, so we need to start out
1767 // the fake clock at a nonzero time.
1768 // TODO(deadbeef): Fix this.
1769 AdvanceTime(webrtc::TimeDelta::seconds(1));
1770 }
1771
1772 // Explicit handle.
1773 ScopedFakeClock& FakeClock() { return *this; }
1774};
1775
1776// Ensure FakeClockForTest is constructed first (see class for rationale).
1777class PeerConnectionIntegrationTestWithFakeClock
1778 : public FakeClockForTest,
1779 public PeerConnectionIntegrationTest {};
1780
Seth Hampson2f0d7022018-02-20 11:54:42 -08001781class PeerConnectionIntegrationTestPlanB
1782 : public PeerConnectionIntegrationBaseTest {
1783 protected:
1784 PeerConnectionIntegrationTestPlanB()
1785 : PeerConnectionIntegrationBaseTest(SdpSemantics::kPlanB) {}
1786};
1787
1788class PeerConnectionIntegrationTestUnifiedPlan
1789 : public PeerConnectionIntegrationBaseTest {
1790 protected:
1791 PeerConnectionIntegrationTestUnifiedPlan()
1792 : PeerConnectionIntegrationBaseTest(SdpSemantics::kUnifiedPlan) {}
1793};
1794
deadbeef1dcb1642017-03-29 21:08:16 -07001795// Test the OnFirstPacketReceived callback from audio/video RtpReceivers. This
1796// includes testing that the callback is invoked if an observer is connected
1797// after the first packet has already been received.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001798TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07001799 RtpReceiverObserverOnFirstPacketReceived) {
1800 ASSERT_TRUE(CreatePeerConnectionWrappers());
1801 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001802 caller()->AddAudioVideoTracks();
1803 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001804 // Start offer/answer exchange and wait for it to complete.
1805 caller()->CreateAndSetAndSignalOffer();
1806 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1807 // Should be one receiver each for audio/video.
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02001808 EXPECT_EQ(2U, caller()->rtp_receiver_observers().size());
1809 EXPECT_EQ(2U, callee()->rtp_receiver_observers().size());
deadbeef1dcb1642017-03-29 21:08:16 -07001810 // Wait for all "first packet received" callbacks to be fired.
1811 EXPECT_TRUE_WAIT(
Steve Anton64b626b2019-01-28 17:25:26 -08001812 absl::c_all_of(caller()->rtp_receiver_observers(),
1813 [](const std::unique_ptr<MockRtpReceiverObserver>& o) {
1814 return o->first_packet_received();
1815 }),
deadbeef1dcb1642017-03-29 21:08:16 -07001816 kMaxWaitForFramesMs);
1817 EXPECT_TRUE_WAIT(
Steve Anton64b626b2019-01-28 17:25:26 -08001818 absl::c_all_of(callee()->rtp_receiver_observers(),
1819 [](const std::unique_ptr<MockRtpReceiverObserver>& o) {
1820 return o->first_packet_received();
1821 }),
deadbeef1dcb1642017-03-29 21:08:16 -07001822 kMaxWaitForFramesMs);
1823 // If new observers are set after the first packet was already received, the
1824 // callback should still be invoked.
1825 caller()->ResetRtpReceiverObservers();
1826 callee()->ResetRtpReceiverObservers();
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02001827 EXPECT_EQ(2U, caller()->rtp_receiver_observers().size());
1828 EXPECT_EQ(2U, callee()->rtp_receiver_observers().size());
deadbeef1dcb1642017-03-29 21:08:16 -07001829 EXPECT_TRUE(
Steve Anton64b626b2019-01-28 17:25:26 -08001830 absl::c_all_of(caller()->rtp_receiver_observers(),
1831 [](const std::unique_ptr<MockRtpReceiverObserver>& o) {
1832 return o->first_packet_received();
1833 }));
deadbeef1dcb1642017-03-29 21:08:16 -07001834 EXPECT_TRUE(
Steve Anton64b626b2019-01-28 17:25:26 -08001835 absl::c_all_of(callee()->rtp_receiver_observers(),
1836 [](const std::unique_ptr<MockRtpReceiverObserver>& o) {
1837 return o->first_packet_received();
1838 }));
deadbeef1dcb1642017-03-29 21:08:16 -07001839}
1840
1841class DummyDtmfObserver : public DtmfSenderObserverInterface {
1842 public:
1843 DummyDtmfObserver() : completed_(false) {}
1844
1845 // Implements DtmfSenderObserverInterface.
1846 void OnToneChange(const std::string& tone) override {
1847 tones_.push_back(tone);
1848 if (tone.empty()) {
1849 completed_ = true;
1850 }
1851 }
1852
1853 const std::vector<std::string>& tones() const { return tones_; }
1854 bool completed() const { return completed_; }
1855
1856 private:
1857 bool completed_;
1858 std::vector<std::string> tones_;
1859};
1860
1861// Assumes |sender| already has an audio track added and the offer/answer
1862// exchange is done.
1863void TestDtmfFromSenderToReceiver(PeerConnectionWrapper* sender,
1864 PeerConnectionWrapper* receiver) {
Steve Anton15324772018-01-16 10:26:49 -08001865 // We should be able to get a DTMF sender from the local sender.
1866 rtc::scoped_refptr<DtmfSenderInterface> dtmf_sender =
1867 sender->pc()->GetSenders().at(0)->GetDtmfSender();
1868 ASSERT_TRUE(dtmf_sender);
deadbeef1dcb1642017-03-29 21:08:16 -07001869 DummyDtmfObserver observer;
deadbeef1dcb1642017-03-29 21:08:16 -07001870 dtmf_sender->RegisterObserver(&observer);
1871
1872 // Test the DtmfSender object just created.
1873 EXPECT_TRUE(dtmf_sender->CanInsertDtmf());
1874 EXPECT_TRUE(dtmf_sender->InsertDtmf("1a", 100, 50));
1875
1876 EXPECT_TRUE_WAIT(observer.completed(), kDefaultTimeout);
1877 std::vector<std::string> tones = {"1", "a", ""};
1878 EXPECT_EQ(tones, observer.tones());
1879 dtmf_sender->UnregisterObserver();
1880 // TODO(deadbeef): Verify the tones were actually received end-to-end.
1881}
1882
1883// Verifies the DtmfSenderObserver callbacks for a DtmfSender (one in each
1884// direction).
Seth Hampson2f0d7022018-02-20 11:54:42 -08001885TEST_P(PeerConnectionIntegrationTest, DtmfSenderObserver) {
deadbeef1dcb1642017-03-29 21:08:16 -07001886 ASSERT_TRUE(CreatePeerConnectionWrappers());
1887 ConnectFakeSignaling();
1888 // Only need audio for DTMF.
Steve Anton15324772018-01-16 10:26:49 -08001889 caller()->AddAudioTrack();
1890 callee()->AddAudioTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07001891 caller()->CreateAndSetAndSignalOffer();
1892 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
deadbeef71452802017-05-07 17:21:01 -07001893 // DTLS must finish before the DTMF sender can be used reliably.
1894 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07001895 TestDtmfFromSenderToReceiver(caller(), callee());
1896 TestDtmfFromSenderToReceiver(callee(), caller());
1897}
1898
1899// Basic end-to-end test, verifying media can be encoded/transmitted/decoded
1900// between two connections, using DTLS-SRTP.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001901TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithDtls) {
deadbeef1dcb1642017-03-29 21:08:16 -07001902 ASSERT_TRUE(CreatePeerConnectionWrappers());
1903 ConnectFakeSignaling();
Harald Alvestrand194939b2018-01-24 16:04:13 +01001904
deadbeef1dcb1642017-03-29 21:08:16 -07001905 // Do normal offer/answer and wait for some frames to be received in each
1906 // direction.
Steve Anton15324772018-01-16 10:26:49 -08001907 caller()->AddAudioVideoTracks();
1908 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001909 caller()->CreateAndSetAndSignalOffer();
1910 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001911 MediaExpectations media_expectations;
1912 media_expectations.ExpectBidirectionalAudioAndVideo();
1913 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Ying Wangef3998f2019-12-09 13:06:53 +01001914 EXPECT_METRIC_LE(
1915 2, webrtc::metrics::NumEvents("WebRTC.PeerConnection.KeyProtocol",
1916 webrtc::kEnumCounterKeyProtocolDtls));
1917 EXPECT_METRIC_EQ(
1918 0, webrtc::metrics::NumEvents("WebRTC.PeerConnection.KeyProtocol",
1919 webrtc::kEnumCounterKeyProtocolSdes));
deadbeef1dcb1642017-03-29 21:08:16 -07001920}
1921
1922// Uses SDES instead of DTLS for key agreement.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001923TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithSdes) {
deadbeef1dcb1642017-03-29 21:08:16 -07001924 PeerConnectionInterface::RTCConfiguration sdes_config;
1925 sdes_config.enable_dtls_srtp.emplace(false);
1926 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(sdes_config, sdes_config));
1927 ConnectFakeSignaling();
1928
1929 // Do normal offer/answer and wait for some frames to be received in each
1930 // direction.
Steve Anton15324772018-01-16 10:26:49 -08001931 caller()->AddAudioVideoTracks();
1932 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001933 caller()->CreateAndSetAndSignalOffer();
1934 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001935 MediaExpectations media_expectations;
1936 media_expectations.ExpectBidirectionalAudioAndVideo();
1937 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Ying Wangef3998f2019-12-09 13:06:53 +01001938 EXPECT_METRIC_LE(
1939 2, webrtc::metrics::NumEvents("WebRTC.PeerConnection.KeyProtocol",
1940 webrtc::kEnumCounterKeyProtocolSdes));
1941 EXPECT_METRIC_EQ(
1942 0, webrtc::metrics::NumEvents("WebRTC.PeerConnection.KeyProtocol",
1943 webrtc::kEnumCounterKeyProtocolDtls));
deadbeef1dcb1642017-03-29 21:08:16 -07001944}
1945
Steve Anton9a44b2d2019-07-12 12:58:30 -07001946// Basic end-to-end test specifying the |enable_encrypted_rtp_header_extensions|
1947// option to offer encrypted versions of all header extensions alongside the
1948// unencrypted versions.
1949TEST_P(PeerConnectionIntegrationTest,
1950 EndToEndCallWithEncryptedRtpHeaderExtensions) {
1951 CryptoOptions crypto_options;
1952 crypto_options.srtp.enable_encrypted_rtp_header_extensions = true;
1953 PeerConnectionInterface::RTCConfiguration config;
1954 config.crypto_options = crypto_options;
1955 // Note: This allows offering >14 RTP header extensions.
1956 config.offer_extmap_allow_mixed = true;
1957 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
1958 ConnectFakeSignaling();
1959
1960 // Do normal offer/answer and wait for some frames to be received in each
1961 // direction.
1962 caller()->AddAudioVideoTracks();
1963 callee()->AddAudioVideoTracks();
1964 caller()->CreateAndSetAndSignalOffer();
1965 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1966 MediaExpectations media_expectations;
1967 media_expectations.ExpectBidirectionalAudioAndVideo();
1968 ASSERT_TRUE(ExpectNewFrames(media_expectations));
1969}
1970
Steve Anton8c0f7a72017-10-03 10:03:10 -07001971// Tests that the GetRemoteAudioSSLCertificate method returns the remote DTLS
1972// certificate once the DTLS handshake has finished.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001973TEST_P(PeerConnectionIntegrationTest,
Steve Anton8c0f7a72017-10-03 10:03:10 -07001974 GetRemoteAudioSSLCertificateReturnsExchangedCertificate) {
1975 auto GetRemoteAudioSSLCertificate = [](PeerConnectionWrapper* wrapper) {
1976 auto pci = reinterpret_cast<PeerConnectionProxy*>(wrapper->pc());
1977 auto pc = reinterpret_cast<PeerConnection*>(pci->internal());
1978 return pc->GetRemoteAudioSSLCertificate();
1979 };
Zhi Huang70b820f2018-01-27 14:16:15 -08001980 auto GetRemoteAudioSSLCertChain = [](PeerConnectionWrapper* wrapper) {
1981 auto pci = reinterpret_cast<PeerConnectionProxy*>(wrapper->pc());
1982 auto pc = reinterpret_cast<PeerConnection*>(pci->internal());
1983 return pc->GetRemoteAudioSSLCertChain();
1984 };
Steve Anton8c0f7a72017-10-03 10:03:10 -07001985
1986 auto caller_cert = rtc::RTCCertificate::FromPEM(kRsaPems[0]);
1987 auto callee_cert = rtc::RTCCertificate::FromPEM(kRsaPems[1]);
1988
1989 // Configure each side with a known certificate so they can be compared later.
1990 PeerConnectionInterface::RTCConfiguration caller_config;
1991 caller_config.enable_dtls_srtp.emplace(true);
1992 caller_config.certificates.push_back(caller_cert);
1993 PeerConnectionInterface::RTCConfiguration callee_config;
1994 callee_config.enable_dtls_srtp.emplace(true);
1995 callee_config.certificates.push_back(callee_cert);
1996 ASSERT_TRUE(
1997 CreatePeerConnectionWrappersWithConfig(caller_config, callee_config));
1998 ConnectFakeSignaling();
1999
2000 // When first initialized, there should not be a remote SSL certificate (and
2001 // calling this method should not crash).
2002 EXPECT_EQ(nullptr, GetRemoteAudioSSLCertificate(caller()));
2003 EXPECT_EQ(nullptr, GetRemoteAudioSSLCertificate(callee()));
Zhi Huang70b820f2018-01-27 14:16:15 -08002004 EXPECT_EQ(nullptr, GetRemoteAudioSSLCertChain(caller()));
2005 EXPECT_EQ(nullptr, GetRemoteAudioSSLCertChain(callee()));
Steve Anton8c0f7a72017-10-03 10:03:10 -07002006
Steve Anton15324772018-01-16 10:26:49 -08002007 caller()->AddAudioTrack();
2008 callee()->AddAudioTrack();
Steve Anton8c0f7a72017-10-03 10:03:10 -07002009 caller()->CreateAndSetAndSignalOffer();
2010 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2011 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
2012
2013 // Once DTLS has been connected, each side should return the other's SSL
2014 // certificate when calling GetRemoteAudioSSLCertificate.
2015
2016 auto caller_remote_cert = GetRemoteAudioSSLCertificate(caller());
2017 ASSERT_TRUE(caller_remote_cert);
Benjamin Wright6c6c9df2018-10-25 01:16:26 -07002018 EXPECT_EQ(callee_cert->GetSSLCertificate().ToPEMString(),
Steve Anton8c0f7a72017-10-03 10:03:10 -07002019 caller_remote_cert->ToPEMString());
2020
2021 auto callee_remote_cert = GetRemoteAudioSSLCertificate(callee());
2022 ASSERT_TRUE(callee_remote_cert);
Benjamin Wright6c6c9df2018-10-25 01:16:26 -07002023 EXPECT_EQ(caller_cert->GetSSLCertificate().ToPEMString(),
Steve Anton8c0f7a72017-10-03 10:03:10 -07002024 callee_remote_cert->ToPEMString());
Zhi Huang70b820f2018-01-27 14:16:15 -08002025
2026 auto caller_remote_cert_chain = GetRemoteAudioSSLCertChain(caller());
2027 ASSERT_TRUE(caller_remote_cert_chain);
2028 ASSERT_EQ(1U, caller_remote_cert_chain->GetSize());
2029 auto remote_cert = &caller_remote_cert_chain->Get(0);
Benjamin Wright6c6c9df2018-10-25 01:16:26 -07002030 EXPECT_EQ(callee_cert->GetSSLCertificate().ToPEMString(),
Zhi Huang70b820f2018-01-27 14:16:15 -08002031 remote_cert->ToPEMString());
2032
2033 auto callee_remote_cert_chain = GetRemoteAudioSSLCertChain(callee());
2034 ASSERT_TRUE(callee_remote_cert_chain);
2035 ASSERT_EQ(1U, callee_remote_cert_chain->GetSize());
2036 remote_cert = &callee_remote_cert_chain->Get(0);
Benjamin Wright6c6c9df2018-10-25 01:16:26 -07002037 EXPECT_EQ(caller_cert->GetSSLCertificate().ToPEMString(),
Zhi Huang70b820f2018-01-27 14:16:15 -08002038 remote_cert->ToPEMString());
Steve Anton8c0f7a72017-10-03 10:03:10 -07002039}
2040
deadbeef1dcb1642017-03-29 21:08:16 -07002041// This test sets up a call between two parties with a source resolution of
2042// 1280x720 and verifies that a 16:9 aspect ratio is received.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002043TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07002044 Send1280By720ResolutionAndReceive16To9AspectRatio) {
2045 ASSERT_TRUE(CreatePeerConnectionWrappers());
2046 ConnectFakeSignaling();
2047
Niels Möller5c7efe72018-05-11 10:34:46 +02002048 // Add video tracks with 16:9 aspect ratio, size 1280 x 720.
2049 webrtc::FakePeriodicVideoSource::Config config;
2050 config.width = 1280;
2051 config.height = 720;
Johannes Kron965e7942018-09-13 15:36:20 +02002052 config.timestamp_offset_ms = rtc::TimeMillis();
Niels Möller5c7efe72018-05-11 10:34:46 +02002053 caller()->AddTrack(caller()->CreateLocalVideoTrackWithConfig(config));
2054 callee()->AddTrack(callee()->CreateLocalVideoTrackWithConfig(config));
deadbeef1dcb1642017-03-29 21:08:16 -07002055
2056 // Do normal offer/answer and wait for at least one frame to be received in
2057 // each direction.
2058 caller()->CreateAndSetAndSignalOffer();
2059 ASSERT_TRUE_WAIT(caller()->min_video_frames_received_per_track() > 0 &&
2060 callee()->min_video_frames_received_per_track() > 0,
2061 kMaxWaitForFramesMs);
2062
2063 // Check rendered aspect ratio.
2064 EXPECT_EQ(16.0 / 9, caller()->local_rendered_aspect_ratio());
2065 EXPECT_EQ(16.0 / 9, caller()->rendered_aspect_ratio());
2066 EXPECT_EQ(16.0 / 9, callee()->local_rendered_aspect_ratio());
2067 EXPECT_EQ(16.0 / 9, callee()->rendered_aspect_ratio());
2068}
2069
2070// This test sets up an one-way call, with media only from caller to
2071// callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002072TEST_P(PeerConnectionIntegrationTest, OneWayMediaCall) {
deadbeef1dcb1642017-03-29 21:08:16 -07002073 ASSERT_TRUE(CreatePeerConnectionWrappers());
2074 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002075 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002076 caller()->CreateAndSetAndSignalOffer();
Seth Hampson2f0d7022018-02-20 11:54:42 -08002077 MediaExpectations media_expectations;
2078 media_expectations.CalleeExpectsSomeAudioAndVideo();
2079 media_expectations.CallerExpectsNoAudio();
2080 media_expectations.CallerExpectsNoVideo();
2081 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002082}
2083
Johannes Kron9bac68c2020-01-23 13:12:25 +00002084// Tests that send only works without the caller having a decoder factory and
2085// the callee having an encoder factory.
2086TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithSendOnlyVideo) {
2087 ASSERT_TRUE(
2088 CreateOneDirectionalPeerConnectionWrappers(/*caller_to_callee=*/true));
2089 ConnectFakeSignaling();
2090 // Add one-directional video, from caller to callee.
2091 rtc::scoped_refptr<webrtc::VideoTrackInterface> track =
2092 caller()->CreateLocalVideoTrack();
2093 caller()->AddTrack(track);
2094 PeerConnectionInterface::RTCOfferAnswerOptions options;
2095 options.offer_to_receive_video = 0;
2096 caller()->SetOfferAnswerOptions(options);
2097 caller()->CreateAndSetAndSignalOffer();
2098 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2099 ASSERT_EQ(callee()->pc()->GetReceivers().size(), 1u);
2100
2101 // Expect video to be received in one direction.
2102 MediaExpectations media_expectations;
2103 media_expectations.CallerExpectsNoVideo();
2104 media_expectations.CalleeExpectsSomeVideo();
2105
2106 EXPECT_TRUE(ExpectNewFrames(media_expectations));
2107}
2108
2109// Tests that receive only works without the caller having an encoder factory
2110// and the callee having a dncoder factory.
2111TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithReceiveOnlyVideo) {
2112 ASSERT_TRUE(
2113 CreateOneDirectionalPeerConnectionWrappers(/*caller_to_callee=*/false));
2114 ConnectFakeSignaling();
2115 // Add one-directional video, from caller to callee.
2116 rtc::scoped_refptr<webrtc::VideoTrackInterface> track =
2117 callee()->CreateLocalVideoTrack();
2118 callee()->AddTrack(track);
2119 PeerConnectionInterface::RTCOfferAnswerOptions options;
2120 options.offer_to_receive_video = 1;
2121 caller()->SetOfferAnswerOptions(options);
2122 caller()->CreateAndSetAndSignalOffer();
2123 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2124 ASSERT_EQ(caller()->pc()->GetReceivers().size(), 1u);
2125
2126 // Expect video to be received in one direction.
2127 MediaExpectations media_expectations;
2128 media_expectations.CallerExpectsSomeVideo();
2129 media_expectations.CalleeExpectsNoVideo();
2130
2131 EXPECT_TRUE(ExpectNewFrames(media_expectations));
2132}
2133
deadbeef1dcb1642017-03-29 21:08:16 -07002134// This test sets up a audio call initially, with the callee rejecting video
2135// initially. Then later the callee decides to upgrade to audio/video, and
2136// initiates a new offer/answer exchange.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002137TEST_P(PeerConnectionIntegrationTest, AudioToVideoUpgrade) {
deadbeef1dcb1642017-03-29 21:08:16 -07002138 ASSERT_TRUE(CreatePeerConnectionWrappers());
2139 ConnectFakeSignaling();
2140 // Initially, offer an audio/video stream from the caller, but refuse to
2141 // send/receive video on the callee side.
Steve Anton15324772018-01-16 10:26:49 -08002142 caller()->AddAudioVideoTracks();
2143 callee()->AddAudioTrack();
Seth Hampson2f0d7022018-02-20 11:54:42 -08002144 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2145 PeerConnectionInterface::RTCOfferAnswerOptions options;
2146 options.offer_to_receive_video = 0;
2147 callee()->SetOfferAnswerOptions(options);
2148 } else {
2149 callee()->SetRemoteOfferHandler([this] {
2150 callee()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)->Stop();
2151 });
2152 }
deadbeef1dcb1642017-03-29 21:08:16 -07002153 // Do offer/answer and make sure audio is still received end-to-end.
2154 caller()->CreateAndSetAndSignalOffer();
2155 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002156 {
2157 MediaExpectations media_expectations;
2158 media_expectations.ExpectBidirectionalAudio();
2159 media_expectations.ExpectNoVideo();
2160 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2161 }
deadbeef1dcb1642017-03-29 21:08:16 -07002162 // Sanity check that the callee's description has a rejected video section.
2163 ASSERT_NE(nullptr, callee()->pc()->local_description());
2164 const ContentInfo* callee_video_content =
2165 GetFirstVideoContent(callee()->pc()->local_description()->description());
2166 ASSERT_NE(nullptr, callee_video_content);
2167 EXPECT_TRUE(callee_video_content->rejected);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002168
deadbeef1dcb1642017-03-29 21:08:16 -07002169 // Now negotiate with video and ensure negotiation succeeds, with video
2170 // frames and additional audio frames being received.
Steve Anton15324772018-01-16 10:26:49 -08002171 callee()->AddVideoTrack();
Seth Hampson2f0d7022018-02-20 11:54:42 -08002172 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2173 PeerConnectionInterface::RTCOfferAnswerOptions options;
2174 options.offer_to_receive_video = 1;
2175 callee()->SetOfferAnswerOptions(options);
2176 } else {
2177 callee()->SetRemoteOfferHandler(nullptr);
2178 caller()->SetRemoteOfferHandler([this] {
2179 // The caller creates a new transceiver to receive video on when receiving
2180 // the offer, but by default it is send only.
2181 auto transceivers = caller()->pc()->GetTransceivers();
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02002182 ASSERT_EQ(3U, transceivers.size());
Seth Hampson2f0d7022018-02-20 11:54:42 -08002183 ASSERT_EQ(cricket::MEDIA_TYPE_VIDEO,
2184 transceivers[2]->receiver()->media_type());
2185 transceivers[2]->sender()->SetTrack(caller()->CreateLocalVideoTrack());
2186 transceivers[2]->SetDirection(RtpTransceiverDirection::kSendRecv);
2187 });
2188 }
deadbeef1dcb1642017-03-29 21:08:16 -07002189 callee()->CreateAndSetAndSignalOffer();
2190 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002191 {
2192 // Expect additional audio frames to be received after the upgrade.
2193 MediaExpectations media_expectations;
2194 media_expectations.ExpectBidirectionalAudioAndVideo();
2195 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2196 }
deadbeef1dcb1642017-03-29 21:08:16 -07002197}
2198
deadbeef4389b4d2017-09-07 09:07:36 -07002199// Simpler than the above test; just add an audio track to an established
2200// video-only connection.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002201TEST_P(PeerConnectionIntegrationTest, AddAudioToVideoOnlyCall) {
deadbeef4389b4d2017-09-07 09:07:36 -07002202 ASSERT_TRUE(CreatePeerConnectionWrappers());
2203 ConnectFakeSignaling();
2204 // Do initial offer/answer with just a video track.
Steve Anton15324772018-01-16 10:26:49 -08002205 caller()->AddVideoTrack();
2206 callee()->AddVideoTrack();
deadbeef4389b4d2017-09-07 09:07:36 -07002207 caller()->CreateAndSetAndSignalOffer();
2208 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2209 // Now add an audio track and do another offer/answer.
Steve Anton15324772018-01-16 10:26:49 -08002210 caller()->AddAudioTrack();
2211 callee()->AddAudioTrack();
deadbeef4389b4d2017-09-07 09:07:36 -07002212 caller()->CreateAndSetAndSignalOffer();
2213 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2214 // Ensure both audio and video frames are received end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002215 MediaExpectations media_expectations;
2216 media_expectations.ExpectBidirectionalAudioAndVideo();
2217 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef4389b4d2017-09-07 09:07:36 -07002218}
2219
deadbeef1dcb1642017-03-29 21:08:16 -07002220// This test sets up a call that's transferred to a new caller with a different
2221// DTLS fingerprint.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002222TEST_P(PeerConnectionIntegrationTest, CallTransferredForCallee) {
deadbeef1dcb1642017-03-29 21:08:16 -07002223 ASSERT_TRUE(CreatePeerConnectionWrappers());
2224 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002225 caller()->AddAudioVideoTracks();
2226 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002227 caller()->CreateAndSetAndSignalOffer();
2228 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2229
2230 // Keep the original peer around which will still send packets to the
2231 // receiving client. These SRTP packets will be dropped.
2232 std::unique_ptr<PeerConnectionWrapper> original_peer(
2233 SetCallerPcWrapperAndReturnCurrent(
Seth Hampson2f0d7022018-02-20 11:54:42 -08002234 CreatePeerConnectionWrapperWithAlternateKey().release()));
deadbeef1dcb1642017-03-29 21:08:16 -07002235 // TODO(deadbeef): Why do we call Close here? That goes against the comment
2236 // directly above.
2237 original_peer->pc()->Close();
2238
2239 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002240 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002241 caller()->CreateAndSetAndSignalOffer();
2242 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2243 // Wait for some additional frames to be transmitted end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002244 MediaExpectations media_expectations;
2245 media_expectations.ExpectBidirectionalAudioAndVideo();
2246 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002247}
2248
2249// This test sets up a call that's transferred to a new callee with a different
2250// DTLS fingerprint.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002251TEST_P(PeerConnectionIntegrationTest, CallTransferredForCaller) {
deadbeef1dcb1642017-03-29 21:08:16 -07002252 ASSERT_TRUE(CreatePeerConnectionWrappers());
2253 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002254 caller()->AddAudioVideoTracks();
2255 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002256 caller()->CreateAndSetAndSignalOffer();
2257 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2258
2259 // Keep the original peer around which will still send packets to the
2260 // receiving client. These SRTP packets will be dropped.
2261 std::unique_ptr<PeerConnectionWrapper> original_peer(
2262 SetCalleePcWrapperAndReturnCurrent(
Seth Hampson2f0d7022018-02-20 11:54:42 -08002263 CreatePeerConnectionWrapperWithAlternateKey().release()));
deadbeef1dcb1642017-03-29 21:08:16 -07002264 // TODO(deadbeef): Why do we call Close here? That goes against the comment
2265 // directly above.
2266 original_peer->pc()->Close();
2267
2268 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002269 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002270 caller()->SetOfferAnswerOptions(IceRestartOfferAnswerOptions());
2271 caller()->CreateAndSetAndSignalOffer();
2272 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2273 // Wait for some additional frames to be transmitted end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002274 MediaExpectations media_expectations;
2275 media_expectations.ExpectBidirectionalAudioAndVideo();
2276 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002277}
2278
2279// This test sets up a non-bundled call and negotiates bundling at the same
2280// time as starting an ICE restart. When bundling is in effect in the restart,
2281// the DTLS-SRTP context should be successfully reset.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002282TEST_P(PeerConnectionIntegrationTest, BundlingEnabledWhileIceRestartOccurs) {
deadbeef1dcb1642017-03-29 21:08:16 -07002283 ASSERT_TRUE(CreatePeerConnectionWrappers());
2284 ConnectFakeSignaling();
2285
Steve Anton15324772018-01-16 10:26:49 -08002286 caller()->AddAudioVideoTracks();
2287 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002288 // Remove the bundle group from the SDP received by the callee.
2289 callee()->SetReceivedSdpMunger([](cricket::SessionDescription* desc) {
2290 desc->RemoveGroupByName("BUNDLE");
2291 });
2292 caller()->CreateAndSetAndSignalOffer();
2293 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002294 {
2295 MediaExpectations media_expectations;
2296 media_expectations.ExpectBidirectionalAudioAndVideo();
2297 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2298 }
deadbeef1dcb1642017-03-29 21:08:16 -07002299 // Now stop removing the BUNDLE group, and trigger an ICE restart.
2300 callee()->SetReceivedSdpMunger(nullptr);
2301 caller()->SetOfferAnswerOptions(IceRestartOfferAnswerOptions());
2302 caller()->CreateAndSetAndSignalOffer();
2303 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2304
2305 // Expect additional frames to be received after the ICE restart.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002306 {
2307 MediaExpectations media_expectations;
2308 media_expectations.ExpectBidirectionalAudioAndVideo();
2309 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2310 }
deadbeef1dcb1642017-03-29 21:08:16 -07002311}
2312
2313// Test CVO (Coordination of Video Orientation). If a video source is rotated
2314// and both peers support the CVO RTP header extension, the actual video frames
2315// don't need to be encoded in different resolutions, since the rotation is
2316// communicated through the RTP header extension.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002317TEST_P(PeerConnectionIntegrationTest, RotatedVideoWithCVOExtension) {
deadbeef1dcb1642017-03-29 21:08:16 -07002318 ASSERT_TRUE(CreatePeerConnectionWrappers());
2319 ConnectFakeSignaling();
2320 // Add rotated video tracks.
Steve Anton15324772018-01-16 10:26:49 -08002321 caller()->AddTrack(
deadbeef1dcb1642017-03-29 21:08:16 -07002322 caller()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_90));
Steve Anton15324772018-01-16 10:26:49 -08002323 callee()->AddTrack(
deadbeef1dcb1642017-03-29 21:08:16 -07002324 callee()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_270));
2325
2326 // Wait for video frames to be received by both sides.
2327 caller()->CreateAndSetAndSignalOffer();
2328 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2329 ASSERT_TRUE_WAIT(caller()->min_video_frames_received_per_track() > 0 &&
2330 callee()->min_video_frames_received_per_track() > 0,
2331 kMaxWaitForFramesMs);
2332
2333 // Ensure that the aspect ratio is unmodified.
2334 // TODO(deadbeef): Where does 4:3 come from? Should be explicit in the test,
2335 // not just assumed.
2336 EXPECT_EQ(4.0 / 3, caller()->local_rendered_aspect_ratio());
2337 EXPECT_EQ(4.0 / 3, caller()->rendered_aspect_ratio());
2338 EXPECT_EQ(4.0 / 3, callee()->local_rendered_aspect_ratio());
2339 EXPECT_EQ(4.0 / 3, callee()->rendered_aspect_ratio());
2340 // Ensure that the CVO bits were surfaced to the renderer.
2341 EXPECT_EQ(webrtc::kVideoRotation_270, caller()->rendered_rotation());
2342 EXPECT_EQ(webrtc::kVideoRotation_90, callee()->rendered_rotation());
2343}
2344
2345// Test that when the CVO extension isn't supported, video is rotated the
2346// old-fashioned way, by encoding rotated frames.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002347TEST_P(PeerConnectionIntegrationTest, RotatedVideoWithoutCVOExtension) {
deadbeef1dcb1642017-03-29 21:08:16 -07002348 ASSERT_TRUE(CreatePeerConnectionWrappers());
2349 ConnectFakeSignaling();
2350 // Add rotated video tracks.
Steve Anton15324772018-01-16 10:26:49 -08002351 caller()->AddTrack(
deadbeef1dcb1642017-03-29 21:08:16 -07002352 caller()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_90));
Steve Anton15324772018-01-16 10:26:49 -08002353 callee()->AddTrack(
deadbeef1dcb1642017-03-29 21:08:16 -07002354 callee()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_270));
2355
2356 // Remove the CVO extension from the offered SDP.
2357 callee()->SetReceivedSdpMunger([](cricket::SessionDescription* desc) {
2358 cricket::VideoContentDescription* video =
2359 GetFirstVideoContentDescription(desc);
2360 video->ClearRtpHeaderExtensions();
2361 });
2362 // Wait for video frames to be received by both sides.
2363 caller()->CreateAndSetAndSignalOffer();
2364 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2365 ASSERT_TRUE_WAIT(caller()->min_video_frames_received_per_track() > 0 &&
2366 callee()->min_video_frames_received_per_track() > 0,
2367 kMaxWaitForFramesMs);
2368
2369 // Expect that the aspect ratio is inversed to account for the 90/270 degree
2370 // rotation.
2371 // TODO(deadbeef): Where does 4:3 come from? Should be explicit in the test,
2372 // not just assumed.
2373 EXPECT_EQ(3.0 / 4, caller()->local_rendered_aspect_ratio());
2374 EXPECT_EQ(3.0 / 4, caller()->rendered_aspect_ratio());
2375 EXPECT_EQ(3.0 / 4, callee()->local_rendered_aspect_ratio());
2376 EXPECT_EQ(3.0 / 4, callee()->rendered_aspect_ratio());
2377 // Expect that each endpoint is unaware of the rotation of the other endpoint.
2378 EXPECT_EQ(webrtc::kVideoRotation_0, caller()->rendered_rotation());
2379 EXPECT_EQ(webrtc::kVideoRotation_0, callee()->rendered_rotation());
2380}
2381
deadbeef1dcb1642017-03-29 21:08:16 -07002382// Test that if the answerer rejects the audio m= section, no audio is sent or
2383// received, but video still can be.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002384TEST_P(PeerConnectionIntegrationTest, AnswererRejectsAudioSection) {
deadbeef1dcb1642017-03-29 21:08:16 -07002385 ASSERT_TRUE(CreatePeerConnectionWrappers());
2386 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002387 caller()->AddAudioVideoTracks();
Seth Hampson2f0d7022018-02-20 11:54:42 -08002388 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2389 // Only add video track for callee, and set offer_to_receive_audio to 0, so
2390 // it will reject the audio m= section completely.
2391 PeerConnectionInterface::RTCOfferAnswerOptions options;
2392 options.offer_to_receive_audio = 0;
2393 callee()->SetOfferAnswerOptions(options);
2394 } else {
2395 // Stopping the audio RtpTransceiver will cause the media section to be
2396 // rejected in the answer.
2397 callee()->SetRemoteOfferHandler([this] {
2398 callee()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_AUDIO)->Stop();
2399 });
2400 }
Steve Anton15324772018-01-16 10:26:49 -08002401 callee()->AddTrack(callee()->CreateLocalVideoTrack());
deadbeef1dcb1642017-03-29 21:08:16 -07002402 // Do offer/answer and wait for successful end-to-end video frames.
2403 caller()->CreateAndSetAndSignalOffer();
2404 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002405 MediaExpectations media_expectations;
2406 media_expectations.ExpectBidirectionalVideo();
2407 media_expectations.ExpectNoAudio();
2408 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2409
deadbeef1dcb1642017-03-29 21:08:16 -07002410 // Sanity check that the callee's description has a rejected audio section.
2411 ASSERT_NE(nullptr, callee()->pc()->local_description());
2412 const ContentInfo* callee_audio_content =
2413 GetFirstAudioContent(callee()->pc()->local_description()->description());
2414 ASSERT_NE(nullptr, callee_audio_content);
2415 EXPECT_TRUE(callee_audio_content->rejected);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002416 if (sdp_semantics_ == SdpSemantics::kUnifiedPlan) {
2417 // The caller's transceiver should have stopped after receiving the answer.
2418 EXPECT_TRUE(caller()
2419 ->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_AUDIO)
2420 ->stopped());
2421 }
deadbeef1dcb1642017-03-29 21:08:16 -07002422}
2423
2424// Test that if the answerer rejects the video m= section, no video is sent or
2425// received, but audio still can be.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002426TEST_P(PeerConnectionIntegrationTest, AnswererRejectsVideoSection) {
deadbeef1dcb1642017-03-29 21:08:16 -07002427 ASSERT_TRUE(CreatePeerConnectionWrappers());
2428 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002429 caller()->AddAudioVideoTracks();
Seth Hampson2f0d7022018-02-20 11:54:42 -08002430 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2431 // Only add audio track for callee, and set offer_to_receive_video to 0, so
2432 // it will reject the video m= section completely.
2433 PeerConnectionInterface::RTCOfferAnswerOptions options;
2434 options.offer_to_receive_video = 0;
2435 callee()->SetOfferAnswerOptions(options);
2436 } else {
2437 // Stopping the video RtpTransceiver will cause the media section to be
2438 // rejected in the answer.
2439 callee()->SetRemoteOfferHandler([this] {
2440 callee()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)->Stop();
2441 });
2442 }
Steve Anton15324772018-01-16 10:26:49 -08002443 callee()->AddTrack(callee()->CreateLocalAudioTrack());
deadbeef1dcb1642017-03-29 21:08:16 -07002444 // Do offer/answer and wait for successful end-to-end audio frames.
2445 caller()->CreateAndSetAndSignalOffer();
2446 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002447 MediaExpectations media_expectations;
2448 media_expectations.ExpectBidirectionalAudio();
2449 media_expectations.ExpectNoVideo();
2450 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2451
deadbeef1dcb1642017-03-29 21:08:16 -07002452 // Sanity check that the callee's description has a rejected video section.
2453 ASSERT_NE(nullptr, callee()->pc()->local_description());
2454 const ContentInfo* callee_video_content =
2455 GetFirstVideoContent(callee()->pc()->local_description()->description());
2456 ASSERT_NE(nullptr, callee_video_content);
2457 EXPECT_TRUE(callee_video_content->rejected);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002458 if (sdp_semantics_ == SdpSemantics::kUnifiedPlan) {
2459 // The caller's transceiver should have stopped after receiving the answer.
2460 EXPECT_TRUE(caller()
2461 ->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)
2462 ->stopped());
2463 }
deadbeef1dcb1642017-03-29 21:08:16 -07002464}
2465
2466// Test that if the answerer rejects both audio and video m= sections, nothing
2467// bad happens.
2468// TODO(deadbeef): Test that a data channel still works. Currently this doesn't
2469// test anything but the fact that negotiation succeeds, which doesn't mean
2470// much.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002471TEST_P(PeerConnectionIntegrationTest, AnswererRejectsAudioAndVideoSections) {
deadbeef1dcb1642017-03-29 21:08:16 -07002472 ASSERT_TRUE(CreatePeerConnectionWrappers());
2473 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002474 caller()->AddAudioVideoTracks();
Seth Hampson2f0d7022018-02-20 11:54:42 -08002475 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2476 // Don't give the callee any tracks, and set offer_to_receive_X to 0, so it
2477 // will reject both audio and video m= sections.
2478 PeerConnectionInterface::RTCOfferAnswerOptions options;
2479 options.offer_to_receive_audio = 0;
2480 options.offer_to_receive_video = 0;
2481 callee()->SetOfferAnswerOptions(options);
2482 } else {
2483 callee()->SetRemoteOfferHandler([this] {
2484 // Stopping all transceivers will cause all media sections to be rejected.
Mirko Bonadei739baf02019-01-27 17:29:42 +01002485 for (const auto& transceiver : callee()->pc()->GetTransceivers()) {
Seth Hampson2f0d7022018-02-20 11:54:42 -08002486 transceiver->Stop();
2487 }
2488 });
2489 }
deadbeef1dcb1642017-03-29 21:08:16 -07002490 // Do offer/answer and wait for stable signaling state.
2491 caller()->CreateAndSetAndSignalOffer();
2492 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002493
deadbeef1dcb1642017-03-29 21:08:16 -07002494 // Sanity check that the callee's description has rejected m= sections.
2495 ASSERT_NE(nullptr, callee()->pc()->local_description());
2496 const ContentInfo* callee_audio_content =
2497 GetFirstAudioContent(callee()->pc()->local_description()->description());
2498 ASSERT_NE(nullptr, callee_audio_content);
2499 EXPECT_TRUE(callee_audio_content->rejected);
2500 const ContentInfo* callee_video_content =
2501 GetFirstVideoContent(callee()->pc()->local_description()->description());
2502 ASSERT_NE(nullptr, callee_video_content);
2503 EXPECT_TRUE(callee_video_content->rejected);
2504}
2505
2506// This test sets up an audio and video call between two parties. After the
2507// call runs for a while, the caller sends an updated offer with video being
2508// rejected. Once the re-negotiation is done, the video flow should stop and
2509// the audio flow should continue.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002510TEST_P(PeerConnectionIntegrationTest, VideoRejectedInSubsequentOffer) {
deadbeef1dcb1642017-03-29 21:08:16 -07002511 ASSERT_TRUE(CreatePeerConnectionWrappers());
2512 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002513 caller()->AddAudioVideoTracks();
2514 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002515 caller()->CreateAndSetAndSignalOffer();
2516 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002517 {
2518 MediaExpectations media_expectations;
2519 media_expectations.ExpectBidirectionalAudioAndVideo();
2520 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2521 }
deadbeef1dcb1642017-03-29 21:08:16 -07002522 // Renegotiate, rejecting the video m= section.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002523 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2524 caller()->SetGeneratedSdpMunger(
2525 [](cricket::SessionDescription* description) {
2526 for (cricket::ContentInfo& content : description->contents()) {
2527 if (cricket::IsVideoContent(&content)) {
2528 content.rejected = true;
2529 }
2530 }
2531 });
2532 } else {
2533 caller()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)->Stop();
2534 }
deadbeef1dcb1642017-03-29 21:08:16 -07002535 caller()->CreateAndSetAndSignalOffer();
2536 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
2537
2538 // Sanity check that the caller's description has a rejected video section.
2539 ASSERT_NE(nullptr, caller()->pc()->local_description());
2540 const ContentInfo* caller_video_content =
2541 GetFirstVideoContent(caller()->pc()->local_description()->description());
2542 ASSERT_NE(nullptr, caller_video_content);
2543 EXPECT_TRUE(caller_video_content->rejected);
deadbeef1dcb1642017-03-29 21:08:16 -07002544 // Wait for some additional audio frames to be received.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002545 {
2546 MediaExpectations media_expectations;
2547 media_expectations.ExpectBidirectionalAudio();
2548 media_expectations.ExpectNoVideo();
2549 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2550 }
deadbeef1dcb1642017-03-29 21:08:16 -07002551}
2552
Taylor Brandstetter60c8dc82018-04-11 15:20:27 -07002553// Do one offer/answer with audio, another that disables it (rejecting the m=
2554// section), and another that re-enables it. Regression test for:
2555// bugs.webrtc.org/6023
2556TEST_F(PeerConnectionIntegrationTestPlanB, EnableAudioAfterRejecting) {
2557 ASSERT_TRUE(CreatePeerConnectionWrappers());
2558 ConnectFakeSignaling();
2559
2560 // Add audio track, do normal offer/answer.
2561 rtc::scoped_refptr<webrtc::AudioTrackInterface> track =
2562 caller()->CreateLocalAudioTrack();
2563 rtc::scoped_refptr<webrtc::RtpSenderInterface> sender =
2564 caller()->pc()->AddTrack(track, {"stream"}).MoveValue();
2565 caller()->CreateAndSetAndSignalOffer();
2566 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2567
2568 // Remove audio track, and set offer_to_receive_audio to false to cause the
2569 // m= section to be completely disabled, not just "recvonly".
2570 caller()->pc()->RemoveTrack(sender);
2571 PeerConnectionInterface::RTCOfferAnswerOptions options;
2572 options.offer_to_receive_audio = 0;
2573 caller()->SetOfferAnswerOptions(options);
2574 caller()->CreateAndSetAndSignalOffer();
2575 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2576
2577 // Add the audio track again, expecting negotiation to succeed and frames to
2578 // flow.
2579 sender = caller()->pc()->AddTrack(track, {"stream"}).MoveValue();
2580 options.offer_to_receive_audio = 1;
2581 caller()->SetOfferAnswerOptions(options);
2582 caller()->CreateAndSetAndSignalOffer();
2583 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2584
2585 MediaExpectations media_expectations;
2586 media_expectations.CalleeExpectsSomeAudio();
2587 EXPECT_TRUE(ExpectNewFrames(media_expectations));
2588}
2589
deadbeef1dcb1642017-03-29 21:08:16 -07002590// Basic end-to-end test, but without SSRC/MSID signaling. This functionality
2591// is needed to support legacy endpoints.
2592// TODO(deadbeef): When we support the MID extension and demuxing on MID, also
2593// add a test for an end-to-end test without MID signaling either (basically,
2594// the minimum acceptable SDP).
Seth Hampson2f0d7022018-02-20 11:54:42 -08002595TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithoutSsrcOrMsidSignaling) {
deadbeef1dcb1642017-03-29 21:08:16 -07002596 ASSERT_TRUE(CreatePeerConnectionWrappers());
2597 ConnectFakeSignaling();
2598 // Add audio and video, testing that packets can be demuxed on payload type.
Steve Anton15324772018-01-16 10:26:49 -08002599 caller()->AddAudioVideoTracks();
2600 callee()->AddAudioVideoTracks();
deadbeefd8ad7882017-04-18 16:01:17 -07002601 // Remove SSRCs and MSIDs from the received offer SDP.
2602 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
deadbeef1dcb1642017-03-29 21:08:16 -07002603 caller()->CreateAndSetAndSignalOffer();
2604 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002605 MediaExpectations media_expectations;
2606 media_expectations.ExpectBidirectionalAudioAndVideo();
2607 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002608}
2609
Seth Hampson5897a6e2018-04-03 11:16:33 -07002610// Basic end-to-end test, without SSRC signaling. This means that the track
2611// was created properly and frames are delivered when the MSIDs are communicated
2612// with a=msid lines and no a=ssrc lines.
2613TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
2614 EndToEndCallWithoutSsrcSignaling) {
2615 const char kStreamId[] = "streamId";
2616 ASSERT_TRUE(CreatePeerConnectionWrappers());
2617 ConnectFakeSignaling();
2618 // Add just audio tracks.
2619 caller()->AddTrack(caller()->CreateLocalAudioTrack(), {kStreamId});
2620 callee()->AddAudioTrack();
2621
2622 // Remove SSRCs from the received offer SDP.
2623 callee()->SetReceivedSdpMunger(RemoveSsrcsAndKeepMsids);
2624 caller()->CreateAndSetAndSignalOffer();
2625 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2626 MediaExpectations media_expectations;
2627 media_expectations.ExpectBidirectionalAudio();
2628 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2629}
2630
Steve Antondf527fd2018-04-27 15:52:03 -07002631// Tests that video flows between multiple video tracks when SSRCs are not
2632// signaled. This exercises the MID RTP header extension which is needed to
2633// demux the incoming video tracks.
2634TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
2635 EndToEndCallWithTwoVideoTracksAndNoSignaledSsrc) {
2636 ASSERT_TRUE(CreatePeerConnectionWrappers());
2637 ConnectFakeSignaling();
2638 caller()->AddVideoTrack();
2639 caller()->AddVideoTrack();
2640 callee()->AddVideoTrack();
2641 callee()->AddVideoTrack();
2642
2643 caller()->SetReceivedSdpMunger(&RemoveSsrcsAndKeepMsids);
2644 callee()->SetReceivedSdpMunger(&RemoveSsrcsAndKeepMsids);
2645 caller()->CreateAndSetAndSignalOffer();
2646 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2647 ASSERT_EQ(2u, caller()->pc()->GetReceivers().size());
2648 ASSERT_EQ(2u, callee()->pc()->GetReceivers().size());
2649
2650 // Expect video to be received in both directions on both tracks.
2651 MediaExpectations media_expectations;
2652 media_expectations.ExpectBidirectionalVideo();
2653 EXPECT_TRUE(ExpectNewFrames(media_expectations));
2654}
2655
Henrik Boström5b147782018-12-04 11:25:05 +01002656TEST_F(PeerConnectionIntegrationTestUnifiedPlan, NoStreamsMsidLinePresent) {
2657 ASSERT_TRUE(CreatePeerConnectionWrappers());
2658 ConnectFakeSignaling();
2659 caller()->AddAudioTrack();
2660 caller()->AddVideoTrack();
2661 caller()->CreateAndSetAndSignalOffer();
2662 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2663 auto callee_receivers = callee()->pc()->GetReceivers();
2664 ASSERT_EQ(2u, callee_receivers.size());
2665 EXPECT_TRUE(callee_receivers[0]->stream_ids().empty());
2666 EXPECT_TRUE(callee_receivers[1]->stream_ids().empty());
2667}
2668
2669TEST_F(PeerConnectionIntegrationTestUnifiedPlan, NoStreamsMsidLineMissing) {
2670 ASSERT_TRUE(CreatePeerConnectionWrappers());
2671 ConnectFakeSignaling();
2672 caller()->AddAudioTrack();
2673 caller()->AddVideoTrack();
2674 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
2675 caller()->CreateAndSetAndSignalOffer();
2676 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2677 auto callee_receivers = callee()->pc()->GetReceivers();
2678 ASSERT_EQ(2u, callee_receivers.size());
2679 ASSERT_EQ(1u, callee_receivers[0]->stream_ids().size());
2680 ASSERT_EQ(1u, callee_receivers[1]->stream_ids().size());
2681 EXPECT_EQ(callee_receivers[0]->stream_ids()[0],
2682 callee_receivers[1]->stream_ids()[0]);
2683 EXPECT_EQ(callee_receivers[0]->streams()[0],
2684 callee_receivers[1]->streams()[0]);
2685}
2686
deadbeef1dcb1642017-03-29 21:08:16 -07002687// Test that if two video tracks are sent (from caller to callee, in this test),
2688// they're transmitted correctly end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002689TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithTwoVideoTracks) {
deadbeef1dcb1642017-03-29 21:08:16 -07002690 ASSERT_TRUE(CreatePeerConnectionWrappers());
2691 ConnectFakeSignaling();
2692 // Add one audio/video stream, and one video-only stream.
Steve Anton15324772018-01-16 10:26:49 -08002693 caller()->AddAudioVideoTracks();
2694 caller()->AddVideoTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07002695 caller()->CreateAndSetAndSignalOffer();
2696 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Steve Anton15324772018-01-16 10:26:49 -08002697 ASSERT_EQ(3u, callee()->pc()->GetReceivers().size());
Seth Hampson2f0d7022018-02-20 11:54:42 -08002698
2699 MediaExpectations media_expectations;
2700 media_expectations.CalleeExpectsSomeAudioAndVideo();
2701 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002702}
2703
2704static void MakeSpecCompliantMaxBundleOffer(cricket::SessionDescription* desc) {
2705 bool first = true;
2706 for (cricket::ContentInfo& content : desc->contents()) {
2707 if (first) {
2708 first = false;
2709 continue;
2710 }
2711 content.bundle_only = true;
2712 }
2713 first = true;
2714 for (cricket::TransportInfo& transport : desc->transport_infos()) {
2715 if (first) {
2716 first = false;
2717 continue;
2718 }
2719 transport.description.ice_ufrag.clear();
2720 transport.description.ice_pwd.clear();
2721 transport.description.connection_role = cricket::CONNECTIONROLE_NONE;
2722 transport.description.identity_fingerprint.reset(nullptr);
2723 }
2724}
2725
2726// Test that if applying a true "max bundle" offer, which uses ports of 0,
2727// "a=bundle-only", omitting "a=fingerprint", "a=setup", "a=ice-ufrag" and
2728// "a=ice-pwd" for all but the audio "m=" section, negotiation still completes
2729// successfully and media flows.
2730// TODO(deadbeef): Update this test to also omit "a=rtcp-mux", once that works.
2731// TODO(deadbeef): Won't need this test once we start generating actual
2732// standards-compliant SDP.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002733TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07002734 EndToEndCallWithSpecCompliantMaxBundleOffer) {
2735 ASSERT_TRUE(CreatePeerConnectionWrappers());
2736 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002737 caller()->AddAudioVideoTracks();
2738 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002739 // Do the equivalent of setting the port to 0, adding a=bundle-only, and
2740 // removing a=ice-ufrag, a=ice-pwd, a=fingerprint and a=setup from all
2741 // but the first m= section.
2742 callee()->SetReceivedSdpMunger(MakeSpecCompliantMaxBundleOffer);
2743 caller()->CreateAndSetAndSignalOffer();
2744 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002745 MediaExpectations media_expectations;
2746 media_expectations.ExpectBidirectionalAudioAndVideo();
2747 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002748}
2749
2750// Test that we can receive the audio output level from a remote audio track.
2751// TODO(deadbeef): Use a fake audio source and verify that the output level is
2752// exactly what the source on the other side was configured with.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002753TEST_P(PeerConnectionIntegrationTest, GetAudioOutputLevelStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-29 21:08:16 -07002754 ASSERT_TRUE(CreatePeerConnectionWrappers());
2755 ConnectFakeSignaling();
2756 // Just add an audio track.
Steve Anton15324772018-01-16 10:26:49 -08002757 caller()->AddAudioTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07002758 caller()->CreateAndSetAndSignalOffer();
2759 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2760
2761 // Get the audio output level stats. Note that the level is not available
2762 // until an RTCP packet has been received.
deadbeefd8ad7882017-04-18 16:01:17 -07002763 EXPECT_TRUE_WAIT(callee()->OldGetStats()->AudioOutputLevel() > 0,
deadbeef1dcb1642017-03-29 21:08:16 -07002764 kMaxWaitForFramesMs);
2765}
2766
2767// Test that an audio input level is reported.
2768// TODO(deadbeef): Use a fake audio source and verify that the input level is
2769// exactly what the source was configured with.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002770TEST_P(PeerConnectionIntegrationTest, GetAudioInputLevelStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-29 21:08:16 -07002771 ASSERT_TRUE(CreatePeerConnectionWrappers());
2772 ConnectFakeSignaling();
2773 // Just add an audio track.
Steve Anton15324772018-01-16 10:26:49 -08002774 caller()->AddAudioTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07002775 caller()->CreateAndSetAndSignalOffer();
2776 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2777
2778 // Get the audio input level stats. The level should be available very
2779 // soon after the test starts.
deadbeefd8ad7882017-04-18 16:01:17 -07002780 EXPECT_TRUE_WAIT(caller()->OldGetStats()->AudioInputLevel() > 0,
deadbeef1dcb1642017-03-29 21:08:16 -07002781 kMaxWaitForStatsMs);
2782}
2783
2784// Test that we can get incoming byte counts from both audio and video tracks.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002785TEST_P(PeerConnectionIntegrationTest, GetBytesReceivedStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-29 21:08:16 -07002786 ASSERT_TRUE(CreatePeerConnectionWrappers());
2787 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002788 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002789 // Do offer/answer, wait for the callee to receive some frames.
2790 caller()->CreateAndSetAndSignalOffer();
2791 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002792
2793 MediaExpectations media_expectations;
2794 media_expectations.CalleeExpectsSomeAudioAndVideo();
2795 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002796
2797 // Get a handle to the remote tracks created, so they can be used as GetStats
2798 // filters.
Mirko Bonadei739baf02019-01-27 17:29:42 +01002799 for (const auto& receiver : callee()->pc()->GetReceivers()) {
Steve Anton15324772018-01-16 10:26:49 -08002800 // We received frames, so we definitely should have nonzero "received bytes"
2801 // stats at this point.
2802 EXPECT_GT(callee()->OldGetStatsForTrack(receiver->track())->BytesReceived(),
2803 0);
2804 }
deadbeef1dcb1642017-03-29 21:08:16 -07002805}
2806
2807// Test that we can get outgoing byte counts from both audio and video tracks.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002808TEST_P(PeerConnectionIntegrationTest, GetBytesSentStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-29 21:08:16 -07002809 ASSERT_TRUE(CreatePeerConnectionWrappers());
2810 ConnectFakeSignaling();
2811 auto audio_track = caller()->CreateLocalAudioTrack();
2812 auto video_track = caller()->CreateLocalVideoTrack();
Steve Anton15324772018-01-16 10:26:49 -08002813 caller()->AddTrack(audio_track);
2814 caller()->AddTrack(video_track);
deadbeef1dcb1642017-03-29 21:08:16 -07002815 // Do offer/answer, wait for the callee to receive some frames.
2816 caller()->CreateAndSetAndSignalOffer();
2817 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002818 MediaExpectations media_expectations;
2819 media_expectations.CalleeExpectsSomeAudioAndVideo();
2820 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002821
2822 // The callee received frames, so we definitely should have nonzero "sent
2823 // bytes" stats at this point.
deadbeefd8ad7882017-04-18 16:01:17 -07002824 EXPECT_GT(caller()->OldGetStatsForTrack(audio_track)->BytesSent(), 0);
2825 EXPECT_GT(caller()->OldGetStatsForTrack(video_track)->BytesSent(), 0);
2826}
2827
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002828// Test that we can get capture start ntp time.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002829TEST_P(PeerConnectionIntegrationTest, GetCaptureStartNtpTimeWithOldStatsApi) {
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002830 ASSERT_TRUE(CreatePeerConnectionWrappers());
2831 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002832 caller()->AddAudioTrack();
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002833
Steve Anton15324772018-01-16 10:26:49 -08002834 callee()->AddAudioTrack();
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002835
2836 // Do offer/answer, wait for the callee to receive some frames.
2837 caller()->CreateAndSetAndSignalOffer();
2838 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2839
2840 // Get the remote audio track created on the receiver, so they can be used as
2841 // GetStats filters.
Steve Antonfc853712018-03-01 13:48:58 -08002842 auto receivers = callee()->pc()->GetReceivers();
2843 ASSERT_EQ(1u, receivers.size());
2844 auto remote_audio_track = receivers[0]->track();
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002845
2846 // Get the audio output level stats. Note that the level is not available
2847 // until an RTCP packet has been received.
Zhi Huange830e682018-03-30 10:48:35 -07002848 EXPECT_TRUE_WAIT(
2849 callee()->OldGetStatsForTrack(remote_audio_track)->CaptureStartNtpTime() >
2850 0,
2851 2 * kMaxWaitForFramesMs);
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002852}
2853
Steve Antona41959e2018-11-28 11:15:33 -08002854// Test that the track ID is associated with all local and remote SSRC stats
2855// using the old GetStats() and more than 1 audio and more than 1 video track.
2856// This is a regression test for crbug.com/906988
2857TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
2858 OldGetStatsAssociatesTrackIdForManyMediaSections) {
2859 ASSERT_TRUE(CreatePeerConnectionWrappers());
2860 ConnectFakeSignaling();
2861 auto audio_sender_1 = caller()->AddAudioTrack();
2862 auto video_sender_1 = caller()->AddVideoTrack();
2863 auto audio_sender_2 = caller()->AddAudioTrack();
2864 auto video_sender_2 = caller()->AddVideoTrack();
2865 caller()->CreateAndSetAndSignalOffer();
2866 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2867
2868 MediaExpectations media_expectations;
2869 media_expectations.CalleeExpectsSomeAudioAndVideo();
2870 ASSERT_TRUE_WAIT(ExpectNewFrames(media_expectations), kDefaultTimeout);
2871
2872 std::vector<std::string> track_ids = {
2873 audio_sender_1->track()->id(), video_sender_1->track()->id(),
2874 audio_sender_2->track()->id(), video_sender_2->track()->id()};
2875
2876 auto caller_stats = caller()->OldGetStats();
2877 EXPECT_THAT(caller_stats->TrackIds(), UnorderedElementsAreArray(track_ids));
2878 auto callee_stats = callee()->OldGetStats();
2879 EXPECT_THAT(callee_stats->TrackIds(), UnorderedElementsAreArray(track_ids));
2880}
2881
Steve Antonffa6ce42018-11-30 09:26:08 -08002882// Test that the new GetStats() returns stats for all outgoing/incoming streams
2883// with the correct track IDs if there are more than one audio and more than one
2884// video senders/receivers.
2885TEST_P(PeerConnectionIntegrationTest, NewGetStatsManyAudioAndManyVideoStreams) {
2886 ASSERT_TRUE(CreatePeerConnectionWrappers());
2887 ConnectFakeSignaling();
2888 auto audio_sender_1 = caller()->AddAudioTrack();
2889 auto video_sender_1 = caller()->AddVideoTrack();
2890 auto audio_sender_2 = caller()->AddAudioTrack();
2891 auto video_sender_2 = caller()->AddVideoTrack();
2892 caller()->CreateAndSetAndSignalOffer();
2893 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2894
2895 MediaExpectations media_expectations;
2896 media_expectations.CalleeExpectsSomeAudioAndVideo();
2897 ASSERT_TRUE_WAIT(ExpectNewFrames(media_expectations), kDefaultTimeout);
2898
2899 std::vector<std::string> track_ids = {
2900 audio_sender_1->track()->id(), video_sender_1->track()->id(),
2901 audio_sender_2->track()->id(), video_sender_2->track()->id()};
2902
2903 rtc::scoped_refptr<const webrtc::RTCStatsReport> caller_report =
2904 caller()->NewGetStats();
2905 ASSERT_TRUE(caller_report);
2906 auto outbound_stream_stats =
2907 caller_report->GetStatsOfType<webrtc::RTCOutboundRTPStreamStats>();
2908 ASSERT_EQ(4u, outbound_stream_stats.size());
2909 std::vector<std::string> outbound_track_ids;
2910 for (const auto& stat : outbound_stream_stats) {
2911 ASSERT_TRUE(stat->bytes_sent.is_defined());
2912 EXPECT_LT(0u, *stat->bytes_sent);
Rasmus Brandt2efae772019-06-27 14:29:34 +02002913 if (*stat->kind == "video") {
2914 ASSERT_TRUE(stat->key_frames_encoded.is_defined());
2915 EXPECT_GT(*stat->key_frames_encoded, 0u);
2916 ASSERT_TRUE(stat->frames_encoded.is_defined());
2917 EXPECT_GE(*stat->frames_encoded, *stat->key_frames_encoded);
2918 }
Steve Antonffa6ce42018-11-30 09:26:08 -08002919 ASSERT_TRUE(stat->track_id.is_defined());
2920 const auto* track_stat =
2921 caller_report->GetAs<webrtc::RTCMediaStreamTrackStats>(*stat->track_id);
2922 ASSERT_TRUE(track_stat);
2923 outbound_track_ids.push_back(*track_stat->track_identifier);
2924 }
2925 EXPECT_THAT(outbound_track_ids, UnorderedElementsAreArray(track_ids));
2926
2927 rtc::scoped_refptr<const webrtc::RTCStatsReport> callee_report =
2928 callee()->NewGetStats();
2929 ASSERT_TRUE(callee_report);
2930 auto inbound_stream_stats =
2931 callee_report->GetStatsOfType<webrtc::RTCInboundRTPStreamStats>();
2932 ASSERT_EQ(4u, inbound_stream_stats.size());
2933 std::vector<std::string> inbound_track_ids;
2934 for (const auto& stat : inbound_stream_stats) {
2935 ASSERT_TRUE(stat->bytes_received.is_defined());
2936 EXPECT_LT(0u, *stat->bytes_received);
Rasmus Brandt2efae772019-06-27 14:29:34 +02002937 if (*stat->kind == "video") {
2938 ASSERT_TRUE(stat->key_frames_decoded.is_defined());
2939 EXPECT_GT(*stat->key_frames_decoded, 0u);
2940 ASSERT_TRUE(stat->frames_decoded.is_defined());
2941 EXPECT_GE(*stat->frames_decoded, *stat->key_frames_decoded);
2942 }
Steve Antonffa6ce42018-11-30 09:26:08 -08002943 ASSERT_TRUE(stat->track_id.is_defined());
2944 const auto* track_stat =
2945 callee_report->GetAs<webrtc::RTCMediaStreamTrackStats>(*stat->track_id);
2946 ASSERT_TRUE(track_stat);
2947 inbound_track_ids.push_back(*track_stat->track_identifier);
2948 }
2949 EXPECT_THAT(inbound_track_ids, UnorderedElementsAreArray(track_ids));
2950}
2951
2952// Test that we can get stats (using the new stats implementation) for
deadbeefd8ad7882017-04-18 16:01:17 -07002953// unsignaled streams. Meaning when SSRCs/MSIDs aren't signaled explicitly in
2954// SDP.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002955TEST_P(PeerConnectionIntegrationTest,
deadbeefd8ad7882017-04-18 16:01:17 -07002956 GetStatsForUnsignaledStreamWithNewStatsApi) {
2957 ASSERT_TRUE(CreatePeerConnectionWrappers());
2958 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002959 caller()->AddAudioTrack();
deadbeefd8ad7882017-04-18 16:01:17 -07002960 // Remove SSRCs and MSIDs from the received offer SDP.
2961 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
2962 caller()->CreateAndSetAndSignalOffer();
2963 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002964 MediaExpectations media_expectations;
2965 media_expectations.CalleeExpectsSomeAudio(1);
2966 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeefd8ad7882017-04-18 16:01:17 -07002967
2968 // We received a frame, so we should have nonzero "bytes received" stats for
2969 // the unsignaled stream, if stats are working for it.
2970 rtc::scoped_refptr<const webrtc::RTCStatsReport> report =
2971 callee()->NewGetStats();
2972 ASSERT_NE(nullptr, report);
2973 auto inbound_stream_stats =
2974 report->GetStatsOfType<webrtc::RTCInboundRTPStreamStats>();
2975 ASSERT_EQ(1U, inbound_stream_stats.size());
2976 ASSERT_TRUE(inbound_stream_stats[0]->bytes_received.is_defined());
2977 ASSERT_GT(*inbound_stream_stats[0]->bytes_received, 0U);
zhihuangf8164932017-05-19 13:09:47 -07002978 ASSERT_TRUE(inbound_stream_stats[0]->track_id.is_defined());
2979}
2980
Taylor Brandstettera4653442018-06-19 09:44:26 -07002981// Same as above but for the legacy stats implementation.
2982TEST_P(PeerConnectionIntegrationTest,
2983 GetStatsForUnsignaledStreamWithOldStatsApi) {
2984 ASSERT_TRUE(CreatePeerConnectionWrappers());
2985 ConnectFakeSignaling();
2986 caller()->AddAudioTrack();
2987 // Remove SSRCs and MSIDs from the received offer SDP.
2988 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
2989 caller()->CreateAndSetAndSignalOffer();
2990 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2991
2992 // Note that, since the old stats implementation associates SSRCs with tracks
2993 // using SDP, when SSRCs aren't signaled in SDP these stats won't have an
2994 // associated track ID. So we can't use the track "selector" argument.
2995 //
2996 // Also, we use "EXPECT_TRUE_WAIT" because the stats collector may decide to
2997 // return cached stats if not enough time has passed since the last update.
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02002998 EXPECT_TRUE_WAIT(callee()->OldGetStats()->BytesReceived() > 0,
Taylor Brandstettera4653442018-06-19 09:44:26 -07002999 kDefaultTimeout);
3000}
3001
zhihuangf8164932017-05-19 13:09:47 -07003002// Test that we can successfully get the media related stats (audio level
3003// etc.) for the unsignaled stream.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003004TEST_P(PeerConnectionIntegrationTest,
zhihuangf8164932017-05-19 13:09:47 -07003005 GetMediaStatsForUnsignaledStreamWithNewStatsApi) {
3006 ASSERT_TRUE(CreatePeerConnectionWrappers());
3007 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08003008 caller()->AddAudioVideoTracks();
zhihuangf8164932017-05-19 13:09:47 -07003009 // Remove SSRCs and MSIDs from the received offer SDP.
3010 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
3011 caller()->CreateAndSetAndSignalOffer();
3012 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08003013 MediaExpectations media_expectations;
3014 media_expectations.CalleeExpectsSomeAudio(1);
3015 media_expectations.CalleeExpectsSomeVideo(1);
3016 ASSERT_TRUE(ExpectNewFrames(media_expectations));
zhihuangf8164932017-05-19 13:09:47 -07003017
3018 rtc::scoped_refptr<const webrtc::RTCStatsReport> report =
3019 callee()->NewGetStats();
3020 ASSERT_NE(nullptr, report);
3021
3022 auto media_stats = report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
3023 auto audio_index = FindFirstMediaStatsIndexByKind("audio", media_stats);
3024 ASSERT_GE(audio_index, 0);
3025 EXPECT_TRUE(media_stats[audio_index]->audio_level.is_defined());
deadbeef1dcb1642017-03-29 21:08:16 -07003026}
3027
deadbeef4e2deab2017-09-20 13:56:21 -07003028// Helper for test below.
3029void ModifySsrcs(cricket::SessionDescription* desc) {
3030 for (ContentInfo& content : desc->contents()) {
Steve Antondf527fd2018-04-27 15:52:03 -07003031 for (StreamParams& stream :
Steve Antonb1c1de12017-12-21 15:14:30 -08003032 content.media_description()->mutable_streams()) {
deadbeef4e2deab2017-09-20 13:56:21 -07003033 for (uint32_t& ssrc : stream.ssrcs) {
3034 ssrc = rtc::CreateRandomId();
3035 }
3036 }
3037 }
3038}
3039
3040// Test that the "RTCMediaSteamTrackStats" object is updated correctly when
3041// SSRCs are unsignaled, and the SSRC of the received (audio) stream changes.
3042// This should result in two "RTCInboundRTPStreamStats", but only one
3043// "RTCMediaStreamTrackStats", whose counters go up continuously rather than
3044// being reset to 0 once the SSRC change occurs.
3045//
3046// Regression test for this bug:
3047// https://bugs.chromium.org/p/webrtc/issues/detail?id=8158
3048//
3049// The bug causes the track stats to only represent one of the two streams:
3050// whichever one has the higher SSRC. So with this bug, there was a 50% chance
3051// that the track stat counters would reset to 0 when the new stream is
3052// received, and a 50% chance that they'll stop updating (while
3053// "concealed_samples" continues increasing, due to silence being generated for
3054// the inactive stream).
Seth Hampson2f0d7022018-02-20 11:54:42 -08003055TEST_P(PeerConnectionIntegrationTest,
Steve Anton83119dd2017-11-10 16:19:52 -08003056 TrackStatsUpdatedCorrectlyWhenUnsignaledSsrcChanges) {
deadbeef4e2deab2017-09-20 13:56:21 -07003057 ASSERT_TRUE(CreatePeerConnectionWrappers());
3058 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08003059 caller()->AddAudioTrack();
deadbeef4e2deab2017-09-20 13:56:21 -07003060 // Remove SSRCs and MSIDs from the received offer SDP, simulating an endpoint
3061 // that doesn't signal SSRCs (from the callee's perspective).
3062 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
3063 caller()->CreateAndSetAndSignalOffer();
3064 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3065 // Wait for 50 audio frames (500ms of audio) to be received by the callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003066 {
3067 MediaExpectations media_expectations;
3068 media_expectations.CalleeExpectsSomeAudio(50);
3069 ASSERT_TRUE(ExpectNewFrames(media_expectations));
3070 }
deadbeef4e2deab2017-09-20 13:56:21 -07003071 // Some audio frames were received, so we should have nonzero "samples
3072 // received" for the track.
3073 rtc::scoped_refptr<const webrtc::RTCStatsReport> report =
3074 callee()->NewGetStats();
3075 ASSERT_NE(nullptr, report);
3076 auto track_stats = report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
3077 ASSERT_EQ(1U, track_stats.size());
3078 ASSERT_TRUE(track_stats[0]->total_samples_received.is_defined());
3079 ASSERT_GT(*track_stats[0]->total_samples_received, 0U);
3080 // uint64_t prev_samples_received = *track_stats[0]->total_samples_received;
3081
3082 // Create a new offer and munge it to cause the caller to use a new SSRC.
3083 caller()->SetGeneratedSdpMunger(ModifySsrcs);
3084 caller()->CreateAndSetAndSignalOffer();
3085 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3086 // Wait for 25 more audio frames (250ms of audio) to be received, from the new
3087 // SSRC.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003088 {
3089 MediaExpectations media_expectations;
3090 media_expectations.CalleeExpectsSomeAudio(25);
3091 ASSERT_TRUE(ExpectNewFrames(media_expectations));
3092 }
deadbeef4e2deab2017-09-20 13:56:21 -07003093
3094 report = callee()->NewGetStats();
3095 ASSERT_NE(nullptr, report);
3096 track_stats = report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
3097 ASSERT_EQ(1U, track_stats.size());
3098 ASSERT_TRUE(track_stats[0]->total_samples_received.is_defined());
3099 // The "total samples received" stat should only be greater than it was
3100 // before.
3101 // TODO(deadbeef): Uncomment this assertion once the bug is completely fixed.
3102 // Right now, the new SSRC will cause the counters to reset to 0.
3103 // EXPECT_GT(*track_stats[0]->total_samples_received, prev_samples_received);
3104
3105 // Additionally, the percentage of concealed samples (samples generated to
Steve Anton83119dd2017-11-10 16:19:52 -08003106 // conceal packet loss) should be less than 50%. If it's greater, that's a
deadbeef4e2deab2017-09-20 13:56:21 -07003107 // good sign that we're seeing stats from the old stream that's no longer
3108 // receiving packets, and is generating concealed samples of silence.
Steve Anton83119dd2017-11-10 16:19:52 -08003109 constexpr double kAcceptableConcealedSamplesPercentage = 0.50;
deadbeef4e2deab2017-09-20 13:56:21 -07003110 ASSERT_TRUE(track_stats[0]->concealed_samples.is_defined());
3111 EXPECT_LT(*track_stats[0]->concealed_samples,
3112 *track_stats[0]->total_samples_received *
3113 kAcceptableConcealedSamplesPercentage);
3114
3115 // Also ensure that we have two "RTCInboundRTPStreamStats" as expected, as a
3116 // sanity check that the SSRC really changed.
3117 // TODO(deadbeef): This isn't working right now, because we're not returning
3118 // *any* stats for the inactive stream. Uncomment when the bug is completely
3119 // fixed.
3120 // auto inbound_stream_stats =
3121 // report->GetStatsOfType<webrtc::RTCInboundRTPStreamStats>();
3122 // ASSERT_EQ(2U, inbound_stream_stats.size());
3123}
3124
deadbeef1dcb1642017-03-29 21:08:16 -07003125// Test that DTLS 1.0 is used if both sides only support DTLS 1.0.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003126TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithDtls10) {
deadbeef1dcb1642017-03-29 21:08:16 -07003127 PeerConnectionFactory::Options dtls_10_options;
3128 dtls_10_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
3129 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(dtls_10_options,
3130 dtls_10_options));
3131 ConnectFakeSignaling();
3132 // Do normal offer/answer and wait for some frames to be received in each
3133 // direction.
Steve Anton15324772018-01-16 10:26:49 -08003134 caller()->AddAudioVideoTracks();
3135 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003136 caller()->CreateAndSetAndSignalOffer();
3137 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08003138 MediaExpectations media_expectations;
3139 media_expectations.ExpectBidirectionalAudioAndVideo();
3140 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003141}
3142
3143// Test getting cipher stats and UMA metrics when DTLS 1.0 is negotiated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003144TEST_P(PeerConnectionIntegrationTest, Dtls10CipherStatsAndUmaMetrics) {
deadbeef1dcb1642017-03-29 21:08:16 -07003145 PeerConnectionFactory::Options dtls_10_options;
3146 dtls_10_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
3147 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(dtls_10_options,
3148 dtls_10_options));
3149 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08003150 caller()->AddAudioVideoTracks();
3151 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003152 caller()->CreateAndSetAndSignalOffer();
Qingsi Wang7fc821d2018-07-12 12:54:53 -07003153 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07003154 EXPECT_TRUE_WAIT(rtc::SSLStreamAdapter::IsAcceptableCipher(
deadbeefd8ad7882017-04-18 16:01:17 -07003155 caller()->OldGetStats()->DtlsCipher(), rtc::KT_DEFAULT),
deadbeef1dcb1642017-03-29 21:08:16 -07003156 kDefaultTimeout);
3157 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
deadbeefd8ad7882017-04-18 16:01:17 -07003158 caller()->OldGetStats()->SrtpCipher(), kDefaultTimeout);
Qingsi Wang7fc821d2018-07-12 12:54:53 -07003159 // TODO(bugs.webrtc.org/9456): Fix it.
Ying Wangef3998f2019-12-09 13:06:53 +01003160 EXPECT_METRIC_EQ(1, webrtc::metrics::NumEvents(
3161 "WebRTC.PeerConnection.SrtpCryptoSuite.Audio",
3162 kDefaultSrtpCryptoSuite));
deadbeef1dcb1642017-03-29 21:08:16 -07003163}
3164
3165// Test getting cipher stats and UMA metrics when DTLS 1.2 is negotiated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003166TEST_P(PeerConnectionIntegrationTest, Dtls12CipherStatsAndUmaMetrics) {
deadbeef1dcb1642017-03-29 21:08:16 -07003167 PeerConnectionFactory::Options dtls_12_options;
3168 dtls_12_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
3169 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(dtls_12_options,
3170 dtls_12_options));
3171 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08003172 caller()->AddAudioVideoTracks();
3173 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003174 caller()->CreateAndSetAndSignalOffer();
Qingsi Wang7fc821d2018-07-12 12:54:53 -07003175 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07003176 EXPECT_TRUE_WAIT(rtc::SSLStreamAdapter::IsAcceptableCipher(
deadbeefd8ad7882017-04-18 16:01:17 -07003177 caller()->OldGetStats()->DtlsCipher(), rtc::KT_DEFAULT),
deadbeef1dcb1642017-03-29 21:08:16 -07003178 kDefaultTimeout);
3179 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
deadbeefd8ad7882017-04-18 16:01:17 -07003180 caller()->OldGetStats()->SrtpCipher(), kDefaultTimeout);
Qingsi Wang7fc821d2018-07-12 12:54:53 -07003181 // TODO(bugs.webrtc.org/9456): Fix it.
Ying Wangef3998f2019-12-09 13:06:53 +01003182 EXPECT_METRIC_EQ(1, webrtc::metrics::NumEvents(
3183 "WebRTC.PeerConnection.SrtpCryptoSuite.Audio",
3184 kDefaultSrtpCryptoSuite));
deadbeef1dcb1642017-03-29 21:08:16 -07003185}
3186
3187// Test that DTLS 1.0 can be used if the caller supports DTLS 1.2 and the
3188// callee only supports 1.0.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003189TEST_P(PeerConnectionIntegrationTest, CallerDtls12ToCalleeDtls10) {
deadbeef1dcb1642017-03-29 21:08:16 -07003190 PeerConnectionFactory::Options caller_options;
3191 caller_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
3192 PeerConnectionFactory::Options callee_options;
3193 callee_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
3194 ASSERT_TRUE(
3195 CreatePeerConnectionWrappersWithOptions(caller_options, callee_options));
3196 ConnectFakeSignaling();
3197 // Do normal offer/answer and wait for some frames to be received in each
3198 // direction.
Steve Anton15324772018-01-16 10:26:49 -08003199 caller()->AddAudioVideoTracks();
3200 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003201 caller()->CreateAndSetAndSignalOffer();
3202 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08003203 MediaExpectations media_expectations;
3204 media_expectations.ExpectBidirectionalAudioAndVideo();
3205 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003206}
3207
3208// Test that DTLS 1.0 can be used if the caller only supports DTLS 1.0 and the
3209// callee supports 1.2.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003210TEST_P(PeerConnectionIntegrationTest, CallerDtls10ToCalleeDtls12) {
deadbeef1dcb1642017-03-29 21:08:16 -07003211 PeerConnectionFactory::Options caller_options;
3212 caller_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
3213 PeerConnectionFactory::Options callee_options;
3214 callee_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
3215 ASSERT_TRUE(
3216 CreatePeerConnectionWrappersWithOptions(caller_options, callee_options));
3217 ConnectFakeSignaling();
3218 // Do normal offer/answer and wait for some frames to be received in each
3219 // direction.
Steve Anton15324772018-01-16 10:26:49 -08003220 caller()->AddAudioVideoTracks();
3221 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003222 caller()->CreateAndSetAndSignalOffer();
3223 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08003224 MediaExpectations media_expectations;
3225 media_expectations.ExpectBidirectionalAudioAndVideo();
3226 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003227}
3228
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07003229// The three tests below verify that "enable_aes128_sha1_32_crypto_cipher"
3230// works as expected; the cipher should only be used if enabled by both sides.
3231TEST_P(PeerConnectionIntegrationTest,
3232 Aes128Sha1_32_CipherNotUsedWhenOnlyCallerSupported) {
3233 PeerConnectionFactory::Options caller_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07003234 caller_options.crypto_options.srtp.enable_aes128_sha1_32_crypto_cipher = true;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07003235 PeerConnectionFactory::Options callee_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07003236 callee_options.crypto_options.srtp.enable_aes128_sha1_32_crypto_cipher =
3237 false;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07003238 int expected_cipher_suite = rtc::SRTP_AES128_CM_SHA1_80;
3239 TestNegotiatedCipherSuite(caller_options, callee_options,
3240 expected_cipher_suite);
3241}
3242
3243TEST_P(PeerConnectionIntegrationTest,
3244 Aes128Sha1_32_CipherNotUsedWhenOnlyCalleeSupported) {
3245 PeerConnectionFactory::Options caller_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07003246 caller_options.crypto_options.srtp.enable_aes128_sha1_32_crypto_cipher =
3247 false;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07003248 PeerConnectionFactory::Options callee_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07003249 callee_options.crypto_options.srtp.enable_aes128_sha1_32_crypto_cipher = true;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07003250 int expected_cipher_suite = rtc::SRTP_AES128_CM_SHA1_80;
3251 TestNegotiatedCipherSuite(caller_options, callee_options,
3252 expected_cipher_suite);
3253}
3254
3255TEST_P(PeerConnectionIntegrationTest, Aes128Sha1_32_CipherUsedWhenSupported) {
3256 PeerConnectionFactory::Options caller_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07003257 caller_options.crypto_options.srtp.enable_aes128_sha1_32_crypto_cipher = true;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07003258 PeerConnectionFactory::Options callee_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07003259 callee_options.crypto_options.srtp.enable_aes128_sha1_32_crypto_cipher = true;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07003260 int expected_cipher_suite = rtc::SRTP_AES128_CM_SHA1_32;
3261 TestNegotiatedCipherSuite(caller_options, callee_options,
3262 expected_cipher_suite);
3263}
3264
deadbeef1dcb1642017-03-29 21:08:16 -07003265// Test that a non-GCM cipher is used if both sides only support non-GCM.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003266TEST_P(PeerConnectionIntegrationTest, NonGcmCipherUsedWhenGcmNotSupported) {
deadbeef1dcb1642017-03-29 21:08:16 -07003267 bool local_gcm_enabled = false;
3268 bool remote_gcm_enabled = false;
Philipp Hancke2ebbff82019-10-26 06:12:55 +02003269 bool aes_ctr_enabled = true;
deadbeef1dcb1642017-03-29 21:08:16 -07003270 int expected_cipher_suite = kDefaultSrtpCryptoSuite;
3271 TestGcmNegotiationUsesCipherSuite(local_gcm_enabled, remote_gcm_enabled,
Philipp Hancke2ebbff82019-10-26 06:12:55 +02003272 aes_ctr_enabled, expected_cipher_suite);
deadbeef1dcb1642017-03-29 21:08:16 -07003273}
3274
Philipp Hancke2ebbff82019-10-26 06:12:55 +02003275// Test that a GCM cipher is used if both ends support it and non-GCM is
3276// disabled.
3277TEST_P(PeerConnectionIntegrationTest, GcmCipherUsedWhenOnlyGcmSupported) {
deadbeef1dcb1642017-03-29 21:08:16 -07003278 bool local_gcm_enabled = true;
3279 bool remote_gcm_enabled = true;
Philipp Hancke2ebbff82019-10-26 06:12:55 +02003280 bool aes_ctr_enabled = false;
deadbeef1dcb1642017-03-29 21:08:16 -07003281 int expected_cipher_suite = kDefaultSrtpCryptoSuiteGcm;
3282 TestGcmNegotiationUsesCipherSuite(local_gcm_enabled, remote_gcm_enabled,
Philipp Hancke2ebbff82019-10-26 06:12:55 +02003283 aes_ctr_enabled, expected_cipher_suite);
deadbeef1dcb1642017-03-29 21:08:16 -07003284}
3285
deadbeef7914b8c2017-04-21 03:23:33 -07003286// Verify that media can be transmitted end-to-end when GCM crypto suites are
3287// enabled. Note that the above tests, such as GcmCipherUsedWhenGcmSupported,
3288// only verify that a GCM cipher is negotiated, and not necessarily that SRTP
3289// works with it.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003290TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithGcmCipher) {
deadbeef7914b8c2017-04-21 03:23:33 -07003291 PeerConnectionFactory::Options gcm_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07003292 gcm_options.crypto_options.srtp.enable_gcm_crypto_suites = true;
Philipp Hancke2ebbff82019-10-26 06:12:55 +02003293 gcm_options.crypto_options.srtp.enable_aes128_sha1_80_crypto_cipher = false;
deadbeef7914b8c2017-04-21 03:23:33 -07003294 ASSERT_TRUE(
3295 CreatePeerConnectionWrappersWithOptions(gcm_options, gcm_options));
3296 ConnectFakeSignaling();
3297 // Do normal offer/answer and wait for some frames to be received in each
3298 // direction.
Steve Anton15324772018-01-16 10:26:49 -08003299 caller()->AddAudioVideoTracks();
3300 callee()->AddAudioVideoTracks();
deadbeef7914b8c2017-04-21 03:23:33 -07003301 caller()->CreateAndSetAndSignalOffer();
3302 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08003303 MediaExpectations media_expectations;
3304 media_expectations.ExpectBidirectionalAudioAndVideo();
3305 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef7914b8c2017-04-21 03:23:33 -07003306}
3307
deadbeef1dcb1642017-03-29 21:08:16 -07003308// This test sets up a call between two parties with audio, video and an RTP
3309// data channel.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003310TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithRtpDataChannel) {
Niels Möllerf06f9232018-08-07 12:32:18 +02003311 PeerConnectionInterface::RTCConfiguration rtc_config;
3312 rtc_config.enable_rtp_data_channel = true;
3313 rtc_config.enable_dtls_srtp = false;
3314 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(rtc_config, rtc_config));
deadbeef1dcb1642017-03-29 21:08:16 -07003315 ConnectFakeSignaling();
3316 // Expect that data channel created on caller side will show up for callee as
3317 // well.
3318 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08003319 caller()->AddAudioVideoTracks();
3320 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003321 caller()->CreateAndSetAndSignalOffer();
3322 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3323 // Ensure the existence of the RTP data channel didn't impede audio/video.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003324 MediaExpectations media_expectations;
3325 media_expectations.ExpectBidirectionalAudioAndVideo();
3326 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003327 ASSERT_NE(nullptr, caller()->data_channel());
3328 ASSERT_NE(nullptr, callee()->data_channel());
3329 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3330 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3331
3332 // Ensure data can be sent in both directions.
3333 std::string data = "hello world";
3334 SendRtpDataWithRetries(caller()->data_channel(), data, 5);
3335 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3336 kDefaultTimeout);
3337 SendRtpDataWithRetries(callee()->data_channel(), data, 5);
3338 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3339 kDefaultTimeout);
3340}
3341
3342// Ensure that an RTP data channel is signaled as closed for the caller when
3343// the callee rejects it in a subsequent offer.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003344TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07003345 RtpDataChannelSignaledClosedInCalleeOffer) {
3346 // Same procedure as above test.
Niels Möllerf06f9232018-08-07 12:32:18 +02003347 PeerConnectionInterface::RTCConfiguration rtc_config;
3348 rtc_config.enable_rtp_data_channel = true;
3349 rtc_config.enable_dtls_srtp = false;
3350 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(rtc_config, rtc_config));
deadbeef1dcb1642017-03-29 21:08:16 -07003351 ConnectFakeSignaling();
3352 caller()->CreateDataChannel();
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);
3357 ASSERT_NE(nullptr, caller()->data_channel());
3358 ASSERT_NE(nullptr, callee()->data_channel());
3359 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3360 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3361
3362 // Close the data channel on the callee, and do an updated offer/answer.
3363 callee()->data_channel()->Close();
3364 callee()->CreateAndSetAndSignalOffer();
3365 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3366 EXPECT_FALSE(caller()->data_observer()->IsOpen());
3367 EXPECT_FALSE(callee()->data_observer()->IsOpen());
3368}
3369
3370// Tests that data is buffered in an RTP data channel until an observer is
3371// registered for it.
3372//
3373// NOTE: RTP data channels can receive data before the underlying
3374// transport has detected that a channel is writable and thus data can be
3375// received before the data channel state changes to open. That is hard to test
3376// but the same buffering is expected to be used in that case.
Yves Gerey100fe632020-01-17 19:15:53 +01003377//
3378// Use fake clock and simulated network delay so that we predictably can wait
3379// until an SCTP message has been delivered without "sleep()"ing.
3380TEST_P(PeerConnectionIntegrationTestWithFakeClock,
deadbeef1dcb1642017-03-29 21:08:16 -07003381 DataBufferedUntilRtpDataChannelObserverRegistered) {
deadbeef1dcb1642017-03-29 21:08:16 -07003382 virtual_socket_server()->set_delay_mean(5); // 5 ms per hop.
3383 virtual_socket_server()->UpdateDelayDistribution();
3384
Niels Möllerf06f9232018-08-07 12:32:18 +02003385 PeerConnectionInterface::RTCConfiguration rtc_config;
3386 rtc_config.enable_rtp_data_channel = true;
3387 rtc_config.enable_dtls_srtp = false;
3388 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(rtc_config, rtc_config));
deadbeef1dcb1642017-03-29 21:08:16 -07003389 ConnectFakeSignaling();
3390 caller()->CreateDataChannel();
3391 caller()->CreateAndSetAndSignalOffer();
3392 ASSERT_TRUE(caller()->data_channel() != nullptr);
3393 ASSERT_TRUE_SIMULATED_WAIT(callee()->data_channel() != nullptr,
Yves Gerey100fe632020-01-17 19:15:53 +01003394 kDefaultTimeout, FakeClock());
deadbeef1dcb1642017-03-29 21:08:16 -07003395 ASSERT_TRUE_SIMULATED_WAIT(caller()->data_observer()->IsOpen(),
Yves Gerey100fe632020-01-17 19:15:53 +01003396 kDefaultTimeout, FakeClock());
deadbeef1dcb1642017-03-29 21:08:16 -07003397 ASSERT_EQ_SIMULATED_WAIT(DataChannelInterface::kOpen,
3398 callee()->data_channel()->state(), kDefaultTimeout,
Yves Gerey100fe632020-01-17 19:15:53 +01003399 FakeClock());
deadbeef1dcb1642017-03-29 21:08:16 -07003400
3401 // Unregister the observer which is normally automatically registered.
3402 callee()->data_channel()->UnregisterObserver();
3403 // Send data and advance fake clock until it should have been received.
3404 std::string data = "hello world";
3405 caller()->data_channel()->Send(DataBuffer(data));
Yves Gerey100fe632020-01-17 19:15:53 +01003406 SIMULATED_WAIT(false, 50, FakeClock());
deadbeef1dcb1642017-03-29 21:08:16 -07003407
3408 // Attach data channel and expect data to be received immediately. Note that
3409 // EXPECT_EQ_WAIT is used, such that the simulated clock is not advanced any
3410 // further, but data can be received even if the callback is asynchronous.
3411 MockDataChannelObserver new_observer(callee()->data_channel());
3412 EXPECT_EQ_SIMULATED_WAIT(data, new_observer.last_message(), kDefaultTimeout,
Yves Gerey100fe632020-01-17 19:15:53 +01003413 FakeClock());
deadbeef1dcb1642017-03-29 21:08:16 -07003414}
3415
3416// This test sets up a call between two parties with audio, video and but only
3417// the caller client supports RTP data channels.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003418TEST_P(PeerConnectionIntegrationTest, RtpDataChannelsRejectedByCallee) {
Niels Möllerf06f9232018-08-07 12:32:18 +02003419 PeerConnectionInterface::RTCConfiguration rtc_config_1;
3420 rtc_config_1.enable_rtp_data_channel = true;
deadbeef1dcb1642017-03-29 21:08:16 -07003421 // Must disable DTLS to make negotiation succeed.
Niels Möllerf06f9232018-08-07 12:32:18 +02003422 rtc_config_1.enable_dtls_srtp = false;
3423 PeerConnectionInterface::RTCConfiguration rtc_config_2;
3424 rtc_config_2.enable_dtls_srtp = false;
3425 rtc_config_2.enable_dtls_srtp = false;
3426 ASSERT_TRUE(
3427 CreatePeerConnectionWrappersWithConfig(rtc_config_1, rtc_config_2));
deadbeef1dcb1642017-03-29 21:08:16 -07003428 ConnectFakeSignaling();
3429 caller()->CreateDataChannel();
Harald Alvestrandf3736ed2019-04-08 13:09:30 +02003430 ASSERT_TRUE(caller()->data_channel() != nullptr);
Steve Anton15324772018-01-16 10:26:49 -08003431 caller()->AddAudioVideoTracks();
3432 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003433 caller()->CreateAndSetAndSignalOffer();
3434 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3435 // The caller should still have a data channel, but it should be closed, and
3436 // one should ever have been created for the callee.
3437 EXPECT_TRUE(caller()->data_channel() != nullptr);
3438 EXPECT_FALSE(caller()->data_observer()->IsOpen());
3439 EXPECT_EQ(nullptr, callee()->data_channel());
3440}
3441
3442// This test sets up a call between two parties with audio, and video. When
3443// audio and video is setup and flowing, an RTP data channel is negotiated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003444TEST_P(PeerConnectionIntegrationTest, AddRtpDataChannelInSubsequentOffer) {
Niels Möllerf06f9232018-08-07 12:32:18 +02003445 PeerConnectionInterface::RTCConfiguration rtc_config;
3446 rtc_config.enable_rtp_data_channel = true;
3447 rtc_config.enable_dtls_srtp = false;
3448 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(rtc_config, rtc_config));
deadbeef1dcb1642017-03-29 21:08:16 -07003449 ConnectFakeSignaling();
3450 // Do initial offer/answer with audio/video.
Steve Anton15324772018-01-16 10:26:49 -08003451 caller()->AddAudioVideoTracks();
3452 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003453 caller()->CreateAndSetAndSignalOffer();
3454 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3455 // Create data channel and do new offer and answer.
3456 caller()->CreateDataChannel();
3457 caller()->CreateAndSetAndSignalOffer();
3458 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3459 ASSERT_NE(nullptr, caller()->data_channel());
3460 ASSERT_NE(nullptr, callee()->data_channel());
3461 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3462 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3463 // Ensure data can be sent in both directions.
3464 std::string data = "hello world";
3465 SendRtpDataWithRetries(caller()->data_channel(), data, 5);
3466 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3467 kDefaultTimeout);
3468 SendRtpDataWithRetries(callee()->data_channel(), data, 5);
3469 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3470 kDefaultTimeout);
3471}
3472
3473#ifdef HAVE_SCTP
3474
3475// This test sets up a call between two parties with audio, video and an SCTP
3476// data channel.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003477TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithSctpDataChannel) {
deadbeef1dcb1642017-03-29 21:08:16 -07003478 ASSERT_TRUE(CreatePeerConnectionWrappers());
3479 ConnectFakeSignaling();
3480 // Expect that data channel created on caller side will show up for callee as
3481 // well.
3482 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08003483 caller()->AddAudioVideoTracks();
3484 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003485 caller()->CreateAndSetAndSignalOffer();
3486 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3487 // Ensure the existence of the SCTP data channel didn't impede audio/video.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003488 MediaExpectations media_expectations;
3489 media_expectations.ExpectBidirectionalAudioAndVideo();
3490 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003491 // Caller data channel should already exist (it created one). Callee data
3492 // channel may not exist yet, since negotiation happens in-band, not in SDP.
3493 ASSERT_NE(nullptr, caller()->data_channel());
3494 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3495 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3496 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3497
3498 // Ensure data can be sent in both directions.
3499 std::string data = "hello world";
3500 caller()->data_channel()->Send(DataBuffer(data));
3501 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3502 kDefaultTimeout);
3503 callee()->data_channel()->Send(DataBuffer(data));
3504 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3505 kDefaultTimeout);
3506}
3507
3508// Ensure that when the callee closes an SCTP data channel, the closing
3509// procedure results in the data channel being closed for the caller as well.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003510TEST_P(PeerConnectionIntegrationTest, CalleeClosesSctpDataChannel) {
deadbeef1dcb1642017-03-29 21:08:16 -07003511 // Same procedure as above test.
3512 ASSERT_TRUE(CreatePeerConnectionWrappers());
3513 ConnectFakeSignaling();
3514 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08003515 caller()->AddAudioVideoTracks();
3516 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003517 caller()->CreateAndSetAndSignalOffer();
3518 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3519 ASSERT_NE(nullptr, caller()->data_channel());
3520 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3521 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3522 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3523
3524 // Close the data channel on the callee side, and wait for it to reach the
3525 // "closed" state on both sides.
3526 callee()->data_channel()->Close();
3527 EXPECT_TRUE_WAIT(!caller()->data_observer()->IsOpen(), kDefaultTimeout);
3528 EXPECT_TRUE_WAIT(!callee()->data_observer()->IsOpen(), kDefaultTimeout);
3529}
3530
Seth Hampson2f0d7022018-02-20 11:54:42 -08003531TEST_P(PeerConnectionIntegrationTest, SctpDataChannelConfigSentToOtherSide) {
Steve Antonda6c0952017-10-23 11:41:54 -07003532 ASSERT_TRUE(CreatePeerConnectionWrappers());
3533 ConnectFakeSignaling();
3534 webrtc::DataChannelInit init;
3535 init.id = 53;
3536 init.maxRetransmits = 52;
3537 caller()->CreateDataChannel("data-channel", &init);
Steve Anton15324772018-01-16 10:26:49 -08003538 caller()->AddAudioVideoTracks();
3539 callee()->AddAudioVideoTracks();
Steve Antonda6c0952017-10-23 11:41:54 -07003540 caller()->CreateAndSetAndSignalOffer();
3541 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Steve Anton074dece2017-10-24 13:04:12 -07003542 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3543 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
Harald Alvestrand5c4d2ee2019-04-01 12:58:15 +02003544 // Since "negotiated" is false, the "id" parameter should be ignored.
3545 EXPECT_NE(init.id, callee()->data_channel()->id());
Steve Antonda6c0952017-10-23 11:41:54 -07003546 EXPECT_EQ("data-channel", callee()->data_channel()->label());
3547 EXPECT_EQ(init.maxRetransmits, callee()->data_channel()->maxRetransmits());
3548 EXPECT_FALSE(callee()->data_channel()->negotiated());
3549}
3550
deadbeef1dcb1642017-03-29 21:08:16 -07003551// Test usrsctp's ability to process unordered data stream, where data actually
3552// arrives out of order using simulated delays. Previously there have been some
3553// bugs in this area.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003554TEST_P(PeerConnectionIntegrationTest, StressTestUnorderedSctpDataChannel) {
deadbeef1dcb1642017-03-29 21:08:16 -07003555 // Introduce random network delays.
3556 // Otherwise it's not a true "unordered" test.
3557 virtual_socket_server()->set_delay_mean(20);
3558 virtual_socket_server()->set_delay_stddev(5);
3559 virtual_socket_server()->UpdateDelayDistribution();
3560 // Normal procedure, but with unordered data channel config.
3561 ASSERT_TRUE(CreatePeerConnectionWrappers());
3562 ConnectFakeSignaling();
3563 webrtc::DataChannelInit init;
3564 init.ordered = false;
3565 caller()->CreateDataChannel(&init);
3566 caller()->CreateAndSetAndSignalOffer();
3567 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3568 ASSERT_NE(nullptr, caller()->data_channel());
3569 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3570 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3571 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3572
3573 static constexpr int kNumMessages = 100;
3574 // Deliberately chosen to be larger than the MTU so messages get fragmented.
3575 static constexpr size_t kMaxMessageSize = 4096;
3576 // Create and send random messages.
3577 std::vector<std::string> sent_messages;
3578 for (int i = 0; i < kNumMessages; ++i) {
3579 size_t length =
3580 (rand() % kMaxMessageSize) + 1; // NOLINT (rand_r instead of rand)
3581 std::string message;
3582 ASSERT_TRUE(rtc::CreateRandomString(length, &message));
3583 caller()->data_channel()->Send(DataBuffer(message));
3584 callee()->data_channel()->Send(DataBuffer(message));
3585 sent_messages.push_back(message);
3586 }
3587
3588 // Wait for all messages to be received.
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02003589 EXPECT_EQ_WAIT(rtc::checked_cast<size_t>(kNumMessages),
deadbeef1dcb1642017-03-29 21:08:16 -07003590 caller()->data_observer()->received_message_count(),
3591 kDefaultTimeout);
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02003592 EXPECT_EQ_WAIT(rtc::checked_cast<size_t>(kNumMessages),
deadbeef1dcb1642017-03-29 21:08:16 -07003593 callee()->data_observer()->received_message_count(),
3594 kDefaultTimeout);
3595
3596 // Sort and compare to make sure none of the messages were corrupted.
3597 std::vector<std::string> caller_received_messages =
3598 caller()->data_observer()->messages();
3599 std::vector<std::string> callee_received_messages =
3600 callee()->data_observer()->messages();
Steve Anton64b626b2019-01-28 17:25:26 -08003601 absl::c_sort(sent_messages);
3602 absl::c_sort(caller_received_messages);
3603 absl::c_sort(callee_received_messages);
deadbeef1dcb1642017-03-29 21:08:16 -07003604 EXPECT_EQ(sent_messages, caller_received_messages);
3605 EXPECT_EQ(sent_messages, callee_received_messages);
3606}
3607
3608// This test sets up a call between two parties with audio, and video. When
3609// audio and video are setup and flowing, an SCTP data channel is negotiated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003610TEST_P(PeerConnectionIntegrationTest, AddSctpDataChannelInSubsequentOffer) {
deadbeef1dcb1642017-03-29 21:08:16 -07003611 ASSERT_TRUE(CreatePeerConnectionWrappers());
3612 ConnectFakeSignaling();
3613 // Do initial offer/answer with audio/video.
Steve Anton15324772018-01-16 10:26:49 -08003614 caller()->AddAudioVideoTracks();
3615 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003616 caller()->CreateAndSetAndSignalOffer();
3617 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3618 // Create data channel and do new offer and answer.
3619 caller()->CreateDataChannel();
3620 caller()->CreateAndSetAndSignalOffer();
3621 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3622 // Caller data channel should already exist (it created one). Callee data
3623 // channel may not exist yet, since negotiation happens in-band, not in SDP.
3624 ASSERT_NE(nullptr, caller()->data_channel());
3625 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3626 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3627 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3628 // Ensure data can be sent in both directions.
3629 std::string data = "hello world";
3630 caller()->data_channel()->Send(DataBuffer(data));
3631 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3632 kDefaultTimeout);
3633 callee()->data_channel()->Send(DataBuffer(data));
3634 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3635 kDefaultTimeout);
3636}
3637
deadbeef7914b8c2017-04-21 03:23:33 -07003638// Set up a connection initially just using SCTP data channels, later upgrading
3639// to audio/video, ensuring frames are received end-to-end. Effectively the
3640// inverse of the test above.
3641// This was broken in M57; see https://crbug.com/711243
Seth Hampson2f0d7022018-02-20 11:54:42 -08003642TEST_P(PeerConnectionIntegrationTest, SctpDataChannelToAudioVideoUpgrade) {
deadbeef7914b8c2017-04-21 03:23:33 -07003643 ASSERT_TRUE(CreatePeerConnectionWrappers());
3644 ConnectFakeSignaling();
3645 // Do initial offer/answer with just data channel.
3646 caller()->CreateDataChannel();
3647 caller()->CreateAndSetAndSignalOffer();
3648 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3649 // Wait until data can be sent over the data channel.
3650 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3651 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3652 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3653
3654 // Do subsequent offer/answer with two-way audio and video. Audio and video
3655 // should end up bundled on the DTLS/ICE transport already used for data.
Steve Anton15324772018-01-16 10:26:49 -08003656 caller()->AddAudioVideoTracks();
3657 callee()->AddAudioVideoTracks();
deadbeef7914b8c2017-04-21 03:23:33 -07003658 caller()->CreateAndSetAndSignalOffer();
3659 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08003660 MediaExpectations media_expectations;
3661 media_expectations.ExpectBidirectionalAudioAndVideo();
3662 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef7914b8c2017-04-21 03:23:33 -07003663}
3664
deadbeef8b7e9ad2017-05-25 09:38:55 -07003665static void MakeSpecCompliantSctpOffer(cricket::SessionDescription* desc) {
Harald Alvestrand5fc28b12019-05-13 13:36:16 +02003666 cricket::SctpDataContentDescription* dcd_offer =
3667 GetFirstSctpDataContentDescription(desc);
Harald Alvestrand17ea0682019-12-13 11:51:04 +01003668 // See https://crbug.com/webrtc/11211 - this function is a no-op
Steve Antonb1c1de12017-12-21 15:14:30 -08003669 ASSERT_TRUE(dcd_offer);
deadbeef8b7e9ad2017-05-25 09:38:55 -07003670 dcd_offer->set_use_sctpmap(false);
3671 dcd_offer->set_protocol("UDP/DTLS/SCTP");
3672}
3673
3674// Test that the data channel works when a spec-compliant SCTP m= section is
3675// offered (using "a=sctp-port" instead of "a=sctpmap", and using
3676// "UDP/DTLS/SCTP" as the protocol).
Seth Hampson2f0d7022018-02-20 11:54:42 -08003677TEST_P(PeerConnectionIntegrationTest,
deadbeef8b7e9ad2017-05-25 09:38:55 -07003678 DataChannelWorksWhenSpecCompliantSctpOfferReceived) {
3679 ASSERT_TRUE(CreatePeerConnectionWrappers());
3680 ConnectFakeSignaling();
3681 caller()->CreateDataChannel();
3682 caller()->SetGeneratedSdpMunger(MakeSpecCompliantSctpOffer);
3683 caller()->CreateAndSetAndSignalOffer();
3684 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3685 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3686 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3687 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3688
3689 // Ensure data can be sent in both directions.
3690 std::string data = "hello world";
3691 caller()->data_channel()->Send(DataBuffer(data));
3692 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3693 kDefaultTimeout);
3694 callee()->data_channel()->Send(DataBuffer(data));
3695 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3696 kDefaultTimeout);
3697}
3698
Bjorn A Mellemb689af42019-08-21 10:44:59 -07003699// Tests that the datagram transport to SCTP fallback works correctly when
3700// datagram transport negotiation fails.
3701TEST_P(PeerConnectionIntegrationTest,
3702 DatagramTransportDataChannelFallbackToSctp) {
3703 PeerConnectionInterface::RTCConfiguration rtc_config;
3704 rtc_config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyRequire;
3705 rtc_config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
3706 rtc_config.use_datagram_transport_for_data_channels = true;
3707
3708 // Configure one endpoint to use datagram transport for data channels while
3709 // the other does not.
3710 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
3711 rtc_config, RTCConfiguration(),
3712 loopback_media_transports()->first_factory(), nullptr));
3713 ConnectFakeSignaling();
3714
3715 // The caller offers a data channel using either datagram transport or SCTP.
3716 caller()->CreateDataChannel();
3717 caller()->AddAudioVideoTracks();
3718 callee()->AddAudioVideoTracks();
3719 caller()->CreateAndSetAndSignalOffer();
3720 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3721
3722 // Negotiation should fallback to SCTP, allowing the data channel to be
3723 // established.
3724 ASSERT_NE(nullptr, caller()->data_channel());
3725 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3726 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3727 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3728
3729 // Ensure data can be sent in both directions.
3730 std::string data = "hello world";
3731 caller()->data_channel()->Send(DataBuffer(data));
3732 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3733 kDefaultTimeout);
3734 callee()->data_channel()->Send(DataBuffer(data));
3735 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3736 kDefaultTimeout);
3737
3738 // Ensure that failure of the datagram negotiation doesn't impede media flow.
3739 MediaExpectations media_expectations;
3740 media_expectations.ExpectBidirectionalAudioAndVideo();
3741 ASSERT_TRUE(ExpectNewFrames(media_expectations));
3742}
3743
Bjorn A Mellemfc604aa2019-09-24 14:59:21 -07003744// Tests that the data channel transport works correctly when datagram transport
3745// negotiation succeeds and does not fall back to SCTP.
3746TEST_P(PeerConnectionIntegrationTest,
3747 DatagramTransportDataChannelDoesNotFallbackToSctp) {
3748 PeerConnectionInterface::RTCConfiguration rtc_config;
3749 rtc_config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyRequire;
3750 rtc_config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
3751 rtc_config.use_datagram_transport_for_data_channels = true;
3752
3753 // Configure one endpoint to use datagram transport for data channels while
3754 // the other does not.
3755 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
3756 rtc_config, rtc_config, loopback_media_transports()->first_factory(),
3757 loopback_media_transports()->second_factory()));
3758 ConnectFakeSignaling();
3759
3760 // The caller offers a data channel using either datagram transport or SCTP.
3761 caller()->CreateDataChannel();
3762 caller()->AddAudioVideoTracks();
3763 callee()->AddAudioVideoTracks();
3764 caller()->CreateAndSetAndSignalOffer();
3765 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3766
3767 // Ensure that the data channel transport is ready.
3768 loopback_media_transports()->SetState(webrtc::MediaTransportState::kWritable);
3769 loopback_media_transports()->FlushAsyncInvokes();
3770
3771 // Negotiation should succeed, allowing the data channel to be established.
3772 ASSERT_NE(nullptr, caller()->data_channel());
3773 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3774 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3775 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3776
3777 // Ensure data can be sent in both directions.
3778 std::string data = "hello world";
3779 caller()->data_channel()->Send(DataBuffer(data));
3780 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3781 kDefaultTimeout);
3782 callee()->data_channel()->Send(DataBuffer(data));
3783 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3784 kDefaultTimeout);
3785
3786 // Ensure that failure of the datagram negotiation doesn't impede media flow.
3787 MediaExpectations media_expectations;
3788 media_expectations.ExpectBidirectionalAudioAndVideo();
3789 ASSERT_TRUE(ExpectNewFrames(media_expectations));
3790}
3791
Bjorn A Mellem8e1343a2019-09-30 15:12:47 -07003792TEST_P(PeerConnectionIntegrationTest,
3793 DatagramTransportDataChannelWithMediaOnCaller) {
3794 // Configure the caller to attempt use of datagram transport for media and
3795 // data channels.
3796 PeerConnectionInterface::RTCConfiguration offerer_config;
3797 offerer_config.rtcp_mux_policy =
3798 PeerConnectionInterface::kRtcpMuxPolicyRequire;
3799 offerer_config.bundle_policy =
3800 PeerConnectionInterface::kBundlePolicyMaxBundle;
3801 offerer_config.use_datagram_transport_for_data_channels = true;
3802 offerer_config.use_datagram_transport = true;
3803
3804 // Configure the callee to only use datagram transport for data channels.
3805 PeerConnectionInterface::RTCConfiguration answerer_config;
3806 answerer_config.rtcp_mux_policy =
3807 PeerConnectionInterface::kRtcpMuxPolicyRequire;
3808 answerer_config.bundle_policy =
3809 PeerConnectionInterface::kBundlePolicyMaxBundle;
3810 answerer_config.use_datagram_transport_for_data_channels = true;
3811
3812 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
3813 offerer_config, answerer_config,
3814 loopback_media_transports()->first_factory(),
3815 loopback_media_transports()->second_factory()));
3816 ConnectFakeSignaling();
3817
3818 // Offer both media and data.
3819 caller()->AddAudioVideoTracks();
3820 callee()->AddAudioVideoTracks();
3821 caller()->CreateDataChannel();
3822 caller()->CreateAndSetAndSignalOffer();
3823 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3824
3825 // Ensure that the data channel transport is ready.
3826 loopback_media_transports()->SetState(webrtc::MediaTransportState::kWritable);
3827 loopback_media_transports()->FlushAsyncInvokes();
3828
3829 ASSERT_NE(nullptr, caller()->data_channel());
3830 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3831 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3832 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3833
3834 // Both endpoints should agree to use datagram transport for data channels.
3835 EXPECT_EQ(nullptr, caller()->pc()->GetSctpTransport());
3836 EXPECT_EQ(nullptr, callee()->pc()->GetSctpTransport());
3837
3838 // Ensure data can be sent in both directions.
3839 std::string data = "hello world";
3840 caller()->data_channel()->Send(DataBuffer(data));
3841 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3842 kDefaultTimeout);
3843 callee()->data_channel()->Send(DataBuffer(data));
3844 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3845 kDefaultTimeout);
3846
3847 // Media flow should not be impacted.
3848 MediaExpectations media_expectations;
3849 media_expectations.ExpectBidirectionalAudioAndVideo();
3850 ASSERT_TRUE(ExpectNewFrames(media_expectations));
3851}
3852
3853TEST_P(PeerConnectionIntegrationTest,
3854 DatagramTransportMediaWithDataChannelOnCaller) {
3855 // Configure the caller to attempt use of datagram transport for media and
3856 // data channels.
3857 PeerConnectionInterface::RTCConfiguration offerer_config;
3858 offerer_config.rtcp_mux_policy =
3859 PeerConnectionInterface::kRtcpMuxPolicyRequire;
3860 offerer_config.bundle_policy =
3861 PeerConnectionInterface::kBundlePolicyMaxBundle;
3862 offerer_config.use_datagram_transport_for_data_channels = true;
3863 offerer_config.use_datagram_transport = true;
3864
3865 // Configure the callee to only use datagram transport for media.
3866 PeerConnectionInterface::RTCConfiguration answerer_config;
3867 answerer_config.rtcp_mux_policy =
3868 PeerConnectionInterface::kRtcpMuxPolicyRequire;
3869 answerer_config.bundle_policy =
3870 PeerConnectionInterface::kBundlePolicyMaxBundle;
3871 answerer_config.use_datagram_transport = true;
3872
3873 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
3874 offerer_config, answerer_config,
3875 loopback_media_transports()->first_factory(),
3876 loopback_media_transports()->second_factory()));
3877 ConnectFakeSignaling();
3878
3879 // Offer both media and data.
3880 caller()->AddAudioVideoTracks();
3881 callee()->AddAudioVideoTracks();
3882 caller()->CreateDataChannel();
3883 caller()->CreateAndSetAndSignalOffer();
3884 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3885
3886 // Ensure that the data channel transport is ready.
3887 loopback_media_transports()->SetState(webrtc::MediaTransportState::kWritable);
3888 loopback_media_transports()->FlushAsyncInvokes();
3889
3890 ASSERT_NE(nullptr, caller()->data_channel());
3891 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3892 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3893 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3894
3895 // Both endpoints should agree to use SCTP for data channels.
3896 EXPECT_NE(nullptr, caller()->pc()->GetSctpTransport());
3897 EXPECT_NE(nullptr, callee()->pc()->GetSctpTransport());
3898
3899 // Ensure data can be sent in both directions.
3900 std::string data = "hello world";
3901 caller()->data_channel()->Send(DataBuffer(data));
3902 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3903 kDefaultTimeout);
3904 callee()->data_channel()->Send(DataBuffer(data));
3905 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3906 kDefaultTimeout);
3907
3908 // Media flow should not be impacted.
3909 MediaExpectations media_expectations;
3910 media_expectations.ExpectBidirectionalAudioAndVideo();
3911 ASSERT_TRUE(ExpectNewFrames(media_expectations));
3912}
3913
3914TEST_P(PeerConnectionIntegrationTest,
3915 DatagramTransportDataChannelWithMediaOnCallee) {
3916 // Configure the caller to attempt use of datagram transport for data
3917 // channels.
3918 PeerConnectionInterface::RTCConfiguration offerer_config;
3919 offerer_config.rtcp_mux_policy =
3920 PeerConnectionInterface::kRtcpMuxPolicyRequire;
3921 offerer_config.bundle_policy =
3922 PeerConnectionInterface::kBundlePolicyMaxBundle;
3923 offerer_config.use_datagram_transport_for_data_channels = true;
3924
3925 // Configure the callee to use datagram transport for data channels and media.
3926 PeerConnectionInterface::RTCConfiguration answerer_config;
3927 answerer_config.rtcp_mux_policy =
3928 PeerConnectionInterface::kRtcpMuxPolicyRequire;
3929 answerer_config.bundle_policy =
3930 PeerConnectionInterface::kBundlePolicyMaxBundle;
3931 answerer_config.use_datagram_transport_for_data_channels = true;
3932 answerer_config.use_datagram_transport = true;
3933
3934 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
3935 offerer_config, answerer_config,
3936 loopback_media_transports()->first_factory(),
3937 loopback_media_transports()->second_factory()));
3938 ConnectFakeSignaling();
3939
3940 // Offer both media and data.
3941 caller()->AddAudioVideoTracks();
3942 callee()->AddAudioVideoTracks();
3943 caller()->CreateDataChannel();
3944 caller()->CreateAndSetAndSignalOffer();
3945 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3946
3947 // Ensure that the data channel transport is ready.
3948 loopback_media_transports()->SetState(webrtc::MediaTransportState::kWritable);
3949 loopback_media_transports()->FlushAsyncInvokes();
3950
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 // Both endpoints should agree to use datagram transport for data channels.
3957 EXPECT_EQ(nullptr, caller()->pc()->GetSctpTransport());
3958 EXPECT_EQ(nullptr, callee()->pc()->GetSctpTransport());
3959
3960 // Ensure data can be sent in both directions.
3961 std::string data = "hello world";
3962 caller()->data_channel()->Send(DataBuffer(data));
3963 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3964 kDefaultTimeout);
3965 callee()->data_channel()->Send(DataBuffer(data));
3966 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3967 kDefaultTimeout);
3968
3969 // Media flow should not be impacted.
3970 MediaExpectations media_expectations;
3971 media_expectations.ExpectBidirectionalAudioAndVideo();
3972 ASSERT_TRUE(ExpectNewFrames(media_expectations));
3973}
3974
3975TEST_P(PeerConnectionIntegrationTest,
3976 DatagramTransportMediaWithDataChannelOnCallee) {
3977 // Configure the caller to attempt use of datagram transport for media.
3978 PeerConnectionInterface::RTCConfiguration offerer_config;
3979 offerer_config.rtcp_mux_policy =
3980 PeerConnectionInterface::kRtcpMuxPolicyRequire;
3981 offerer_config.bundle_policy =
3982 PeerConnectionInterface::kBundlePolicyMaxBundle;
3983 offerer_config.use_datagram_transport = true;
3984
3985 // Configure the callee to only use datagram transport for media and data
3986 // channels.
3987 PeerConnectionInterface::RTCConfiguration answerer_config;
3988 answerer_config.rtcp_mux_policy =
3989 PeerConnectionInterface::kRtcpMuxPolicyRequire;
3990 answerer_config.bundle_policy =
3991 PeerConnectionInterface::kBundlePolicyMaxBundle;
3992 answerer_config.use_datagram_transport = true;
3993 answerer_config.use_datagram_transport_for_data_channels = true;
3994
3995 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
3996 offerer_config, answerer_config,
3997 loopback_media_transports()->first_factory(),
3998 loopback_media_transports()->second_factory()));
3999 ConnectFakeSignaling();
4000
4001 // Offer both media and data.
4002 caller()->AddAudioVideoTracks();
4003 callee()->AddAudioVideoTracks();
4004 caller()->CreateDataChannel();
4005 caller()->CreateAndSetAndSignalOffer();
4006 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4007
4008 // Ensure that the data channel transport is ready.
4009 loopback_media_transports()->SetState(webrtc::MediaTransportState::kWritable);
4010 loopback_media_transports()->FlushAsyncInvokes();
4011
4012 ASSERT_NE(nullptr, caller()->data_channel());
4013 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
4014 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
4015 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
4016
4017 // Both endpoints should agree to use SCTP for data channels.
4018 EXPECT_NE(nullptr, caller()->pc()->GetSctpTransport());
4019 EXPECT_NE(nullptr, callee()->pc()->GetSctpTransport());
4020
4021 // Ensure data can be sent in both directions.
4022 std::string data = "hello world";
4023 caller()->data_channel()->Send(DataBuffer(data));
4024 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
4025 kDefaultTimeout);
4026 callee()->data_channel()->Send(DataBuffer(data));
4027 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
4028 kDefaultTimeout);
4029
4030 // Media flow should not be impacted.
4031 MediaExpectations media_expectations;
4032 media_expectations.ExpectBidirectionalAudioAndVideo();
4033 ASSERT_TRUE(ExpectNewFrames(media_expectations));
4034}
4035
4036TEST_P(PeerConnectionIntegrationTest, DatagramTransportDataChannelAndMedia) {
4037 // Configure the caller to use datagram transport for data channels and media.
4038 PeerConnectionInterface::RTCConfiguration offerer_config;
4039 offerer_config.rtcp_mux_policy =
4040 PeerConnectionInterface::kRtcpMuxPolicyRequire;
4041 offerer_config.bundle_policy =
4042 PeerConnectionInterface::kBundlePolicyMaxBundle;
4043 offerer_config.use_datagram_transport_for_data_channels = true;
4044 offerer_config.use_datagram_transport = true;
4045
4046 // Configure the callee to use datagram transport for data channels and media.
4047 PeerConnectionInterface::RTCConfiguration answerer_config;
4048 answerer_config.rtcp_mux_policy =
4049 PeerConnectionInterface::kRtcpMuxPolicyRequire;
4050 answerer_config.bundle_policy =
4051 PeerConnectionInterface::kBundlePolicyMaxBundle;
4052 answerer_config.use_datagram_transport_for_data_channels = true;
4053 answerer_config.use_datagram_transport = true;
4054
4055 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
4056 offerer_config, answerer_config,
4057 loopback_media_transports()->first_factory(),
4058 loopback_media_transports()->second_factory()));
4059 ConnectFakeSignaling();
4060
4061 // Offer both media and data.
4062 caller()->AddAudioVideoTracks();
4063 callee()->AddAudioVideoTracks();
4064 caller()->CreateDataChannel();
4065 caller()->CreateAndSetAndSignalOffer();
4066 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4067
4068 // Ensure that the data channel transport is ready.
4069 loopback_media_transports()->SetState(webrtc::MediaTransportState::kWritable);
4070 loopback_media_transports()->FlushAsyncInvokes();
4071
4072 ASSERT_NE(nullptr, caller()->data_channel());
4073 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
4074 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
4075 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
4076
4077 // Both endpoints should agree to use datagram transport for data channels.
4078 EXPECT_EQ(nullptr, caller()->pc()->GetSctpTransport());
4079 EXPECT_EQ(nullptr, callee()->pc()->GetSctpTransport());
4080
4081 // Ensure data can be sent in both directions.
4082 std::string data = "hello world";
4083 caller()->data_channel()->Send(DataBuffer(data));
4084 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
4085 kDefaultTimeout);
4086 callee()->data_channel()->Send(DataBuffer(data));
4087 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
4088 kDefaultTimeout);
4089
4090 // Media flow should not be impacted.
4091 MediaExpectations media_expectations;
4092 media_expectations.ExpectBidirectionalAudioAndVideo();
4093 ASSERT_TRUE(ExpectNewFrames(media_expectations));
4094}
4095
Bjorn A Mellem7da4e562019-09-26 11:02:11 -07004096// Tests that data channels use SCTP instead of datagram transport if datagram
4097// transport is configured in receive-only mode on the caller.
4098TEST_P(PeerConnectionIntegrationTest,
4099 DatagramTransportDataChannelReceiveOnlyOnCallerUsesSctp) {
4100 PeerConnectionInterface::RTCConfiguration rtc_config;
4101 rtc_config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyRequire;
4102 rtc_config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
4103 rtc_config.use_datagram_transport_for_data_channels = true;
4104 rtc_config.use_datagram_transport_for_data_channels_receive_only = true;
4105
4106 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
4107 rtc_config, rtc_config, loopback_media_transports()->first_factory(),
4108 loopback_media_transports()->second_factory()));
4109 ConnectFakeSignaling();
4110
4111 // The caller should offer a data channel using SCTP.
4112 caller()->CreateDataChannel();
4113 caller()->AddAudioVideoTracks();
4114 callee()->AddAudioVideoTracks();
4115 caller()->CreateAndSetAndSignalOffer();
4116 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4117
4118 ASSERT_NE(nullptr, caller()->data_channel());
4119 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
4120 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
4121 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
4122
4123 // SCTP transports should be present, since they are in use.
4124 EXPECT_NE(caller()->pc()->GetSctpTransport(), nullptr);
4125 EXPECT_NE(callee()->pc()->GetSctpTransport(), nullptr);
4126
4127 // Ensure data can be sent in both directions.
4128 std::string data = "hello world";
4129 caller()->data_channel()->Send(DataBuffer(data));
4130 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
4131 kDefaultTimeout);
4132 callee()->data_channel()->Send(DataBuffer(data));
4133 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
4134 kDefaultTimeout);
4135}
4136
deadbeef1dcb1642017-03-29 21:08:16 -07004137#endif // HAVE_SCTP
4138
Bjorn A Mellem7da4e562019-09-26 11:02:11 -07004139// Tests that a callee configured for receive-only use of datagram transport
4140// data channels accepts them on incoming calls.
4141TEST_P(PeerConnectionIntegrationTest,
4142 DatagramTransportDataChannelReceiveOnlyOnCallee) {
4143 PeerConnectionInterface::RTCConfiguration offerer_config;
4144 offerer_config.rtcp_mux_policy =
4145 PeerConnectionInterface::kRtcpMuxPolicyRequire;
4146 offerer_config.bundle_policy =
4147 PeerConnectionInterface::kBundlePolicyMaxBundle;
4148 offerer_config.use_datagram_transport_for_data_channels = true;
4149
4150 PeerConnectionInterface::RTCConfiguration answerer_config;
4151 answerer_config.rtcp_mux_policy =
4152 PeerConnectionInterface::kRtcpMuxPolicyRequire;
4153 answerer_config.bundle_policy =
4154 PeerConnectionInterface::kBundlePolicyMaxBundle;
4155 answerer_config.use_datagram_transport_for_data_channels = true;
4156 answerer_config.use_datagram_transport_for_data_channels_receive_only = true;
4157
4158 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
4159 offerer_config, answerer_config,
4160 loopback_media_transports()->first_factory(),
4161 loopback_media_transports()->second_factory()));
4162 ConnectFakeSignaling();
4163
4164 caller()->CreateDataChannel();
4165 caller()->CreateAndSetAndSignalOffer();
4166 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4167
4168 // Ensure that the data channel transport is ready.
4169 loopback_media_transports()->SetState(webrtc::MediaTransportState::kWritable);
4170 loopback_media_transports()->FlushAsyncInvokes();
4171
4172 ASSERT_NE(nullptr, caller()->data_channel());
4173 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
4174 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
4175 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
4176
4177 // SCTP transports should not be present, since datagram transport is used.
4178 EXPECT_EQ(caller()->pc()->GetSctpTransport(), nullptr);
4179 EXPECT_EQ(callee()->pc()->GetSctpTransport(), nullptr);
4180
4181 // Ensure data can be sent in both directions.
4182 std::string data = "hello world";
4183 caller()->data_channel()->Send(DataBuffer(data));
4184 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
4185 kDefaultTimeout);
4186 callee()->data_channel()->Send(DataBuffer(data));
4187 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
4188 kDefaultTimeout);
4189}
4190
Bjorn A Mellemb689af42019-08-21 10:44:59 -07004191// This test sets up a call between two parties with a datagram transport data
4192// channel.
4193TEST_P(PeerConnectionIntegrationTest, DatagramTransportDataChannelEndToEnd) {
4194 PeerConnectionInterface::RTCConfiguration rtc_config;
4195 rtc_config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyRequire;
4196 rtc_config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
4197 rtc_config.use_datagram_transport_for_data_channels = true;
4198 rtc_config.enable_dtls_srtp = false;
4199 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
4200 rtc_config, rtc_config, loopback_media_transports()->first_factory(),
4201 loopback_media_transports()->second_factory()));
4202 ConnectFakeSignaling();
4203
4204 // Expect that data channel created on caller side will show up for callee as
4205 // well.
4206 caller()->CreateDataChannel();
4207 caller()->CreateAndSetAndSignalOffer();
4208 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4209
Bjorn A Mellemfc604aa2019-09-24 14:59:21 -07004210 // Ensure that the data channel transport is ready.
Bjorn A Mellemb689af42019-08-21 10:44:59 -07004211 loopback_media_transports()->SetState(webrtc::MediaTransportState::kWritable);
4212 loopback_media_transports()->FlushAsyncInvokes();
4213
4214 // Caller data channel should already exist (it created one). Callee data
4215 // channel may not exist yet, since negotiation happens in-band, not in SDP.
4216 ASSERT_NE(nullptr, caller()->data_channel());
4217 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
4218 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
4219 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
4220
4221 // Ensure data can be sent in both directions.
4222 std::string data = "hello world";
4223 caller()->data_channel()->Send(DataBuffer(data));
4224 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
4225 kDefaultTimeout);
4226 callee()->data_channel()->Send(DataBuffer(data));
4227 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
4228 kDefaultTimeout);
4229}
4230
Bjorn A Mellembc3eebc2019-09-23 14:53:54 -07004231// Tests that 'zero-rtt' data channel transports (which are ready-to-send as
4232// soon as they're created) work correctly.
4233TEST_P(PeerConnectionIntegrationTest, DatagramTransportDataChannelZeroRtt) {
4234 PeerConnectionInterface::RTCConfiguration rtc_config;
4235 rtc_config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyRequire;
4236 rtc_config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
4237 rtc_config.use_datagram_transport_for_data_channels = true;
4238 rtc_config.enable_dtls_srtp = false; // SDES is required for media transport.
4239 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
4240 rtc_config, rtc_config, loopback_media_transports()->first_factory(),
4241 loopback_media_transports()->second_factory()));
4242 ConnectFakeSignaling();
4243
4244 // Ensure that the callee's media transport is ready-to-send immediately.
4245 // Note that only the callee can become writable in zero RTTs. The caller
4246 // must wait for the callee's answer.
4247 loopback_media_transports()->SetSecondStateAfterConnect(
4248 webrtc::MediaTransportState::kWritable);
4249 loopback_media_transports()->FlushAsyncInvokes();
4250
4251 // Expect that data channel created on caller side will show up for callee as
4252 // well.
4253 caller()->CreateDataChannel();
4254 caller()->CreateAndSetAndSignalOffer();
4255 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4256
4257 loopback_media_transports()->SetFirstState(
4258 webrtc::MediaTransportState::kWritable);
4259 loopback_media_transports()->FlushAsyncInvokes();
4260
4261 // Caller data channel should already exist (it created one). Callee data
4262 // channel may not exist yet, since negotiation happens in-band, not in SDP.
4263 ASSERT_NE(nullptr, caller()->data_channel());
4264 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
4265 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
4266 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
4267
4268 // Ensure data can be sent in both directions.
4269 std::string data = "hello world";
4270 caller()->data_channel()->Send(DataBuffer(data));
4271 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
4272 kDefaultTimeout);
4273 callee()->data_channel()->Send(DataBuffer(data));
4274 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
4275 kDefaultTimeout);
4276}
4277
Bjorn A Mellemb689af42019-08-21 10:44:59 -07004278// Ensures that when the callee closes a datagram transport data channel, the
4279// closing procedure results in the data channel being closed for the caller
4280// as well.
4281TEST_P(PeerConnectionIntegrationTest,
4282 DatagramTransportDataChannelCalleeCloses) {
4283 PeerConnectionInterface::RTCConfiguration rtc_config;
4284 rtc_config.use_datagram_transport_for_data_channels = true;
4285 rtc_config.enable_dtls_srtp = false;
4286 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
4287 rtc_config, rtc_config, loopback_media_transports()->first_factory(),
4288 loopback_media_transports()->second_factory()));
4289 ConnectFakeSignaling();
4290
4291 // Create a data channel on the caller and signal it to the callee.
4292 caller()->CreateDataChannel();
4293 caller()->CreateAndSetAndSignalOffer();
4294 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4295
Bjorn A Mellemfc604aa2019-09-24 14:59:21 -07004296 // Ensure that the data channel transport is ready.
Bjorn A Mellemb689af42019-08-21 10:44:59 -07004297 loopback_media_transports()->SetState(webrtc::MediaTransportState::kWritable);
4298 loopback_media_transports()->FlushAsyncInvokes();
4299
4300 // Data channels exist and open on both ends of the connection.
4301 ASSERT_NE(nullptr, caller()->data_channel());
4302 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
4303 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
4304 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
4305
4306 // Close the data channel on the callee side, and wait for it to reach the
4307 // "closed" state on both sides.
4308 callee()->data_channel()->Close();
4309 EXPECT_TRUE_WAIT(!caller()->data_observer()->IsOpen(), kDefaultTimeout);
4310 EXPECT_TRUE_WAIT(!callee()->data_observer()->IsOpen(), kDefaultTimeout);
4311}
4312
4313// Tests that datagram transport data channels can do in-band negotiation.
4314TEST_P(PeerConnectionIntegrationTest,
4315 DatagramTransportDataChannelConfigSentToOtherSide) {
4316 PeerConnectionInterface::RTCConfiguration rtc_config;
4317 rtc_config.use_datagram_transport_for_data_channels = true;
4318 rtc_config.enable_dtls_srtp = false;
4319 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
4320 rtc_config, rtc_config, loopback_media_transports()->first_factory(),
4321 loopback_media_transports()->second_factory()));
4322 ConnectFakeSignaling();
4323
4324 // Create a data channel with a non-default configuration and signal it to the
4325 // callee.
4326 webrtc::DataChannelInit init;
4327 init.id = 53;
4328 init.maxRetransmits = 52;
4329 caller()->CreateDataChannel("data-channel", &init);
4330 caller()->CreateAndSetAndSignalOffer();
4331 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4332
Bjorn A Mellemfc604aa2019-09-24 14:59:21 -07004333 // Ensure that the data channel transport is ready.
Bjorn A Mellemb689af42019-08-21 10:44:59 -07004334 loopback_media_transports()->SetState(webrtc::MediaTransportState::kWritable);
4335 loopback_media_transports()->FlushAsyncInvokes();
4336
4337 // Ensure that the data channel exists on the callee with the correct
4338 // configuration.
4339 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
4340 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
4341 // Since "negotiate" is false, the "id" parameter is ignored.
4342 EXPECT_NE(init.id, callee()->data_channel()->id());
4343 EXPECT_EQ("data-channel", callee()->data_channel()->label());
4344 EXPECT_EQ(init.maxRetransmits, callee()->data_channel()->maxRetransmits());
4345 EXPECT_FALSE(callee()->data_channel()->negotiated());
4346}
4347
4348TEST_P(PeerConnectionIntegrationTest,
4349 DatagramTransportDataChannelRejectedWithNoFallback) {
4350 PeerConnectionInterface::RTCConfiguration offerer_config;
4351 offerer_config.rtcp_mux_policy =
4352 PeerConnectionInterface::kRtcpMuxPolicyRequire;
4353 offerer_config.bundle_policy =
4354 PeerConnectionInterface::kBundlePolicyMaxBundle;
4355 offerer_config.use_datagram_transport_for_data_channels = true;
4356 // Disabling DTLS precludes a fallback to SCTP.
4357 offerer_config.enable_dtls_srtp = false;
4358
4359 PeerConnectionInterface::RTCConfiguration answerer_config;
4360 answerer_config.rtcp_mux_policy =
4361 PeerConnectionInterface::kRtcpMuxPolicyRequire;
4362 answerer_config.bundle_policy =
4363 PeerConnectionInterface::kBundlePolicyMaxBundle;
4364 // Both endpoints must disable DTLS or SetRemoteDescription will fail.
4365 answerer_config.enable_dtls_srtp = false;
4366
4367 // Configure one endpoint to use datagram transport for data channels while
4368 // the other does not.
4369 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
4370 offerer_config, answerer_config,
4371 loopback_media_transports()->first_factory(), nullptr));
4372 ConnectFakeSignaling();
4373
4374 // The caller offers a data channel using either datagram transport or SCTP.
4375 caller()->CreateDataChannel();
4376 caller()->AddAudioVideoTracks();
4377 callee()->AddAudioVideoTracks();
4378 caller()->CreateAndSetAndSignalOffer();
4379 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4380
4381 // Caller data channel should already exist (it created one). Callee data
4382 // channel should not exist, since negotiation happens in-band, not in SDP.
4383 EXPECT_NE(nullptr, caller()->data_channel());
4384 EXPECT_EQ(nullptr, callee()->data_channel());
4385
4386 // The caller's data channel should close when the datagram transport is
4387 // rejected.
4388 EXPECT_FALSE(caller()->data_observer()->IsOpen());
4389
4390 // Media flow should not be impacted by the failed data channel.
4391 MediaExpectations media_expectations;
4392 media_expectations.ExpectBidirectionalAudioAndVideo();
4393 ASSERT_TRUE(ExpectNewFrames(media_expectations));
4394}
4395
deadbeef1dcb1642017-03-29 21:08:16 -07004396// Test that the ICE connection and gathering states eventually reach
4397// "complete".
Seth Hampson2f0d7022018-02-20 11:54:42 -08004398TEST_P(PeerConnectionIntegrationTest, IceStatesReachCompletion) {
deadbeef1dcb1642017-03-29 21:08:16 -07004399 ASSERT_TRUE(CreatePeerConnectionWrappers());
4400 ConnectFakeSignaling();
4401 // Do normal offer/answer.
Steve Anton15324772018-01-16 10:26:49 -08004402 caller()->AddAudioVideoTracks();
4403 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07004404 caller()->CreateAndSetAndSignalOffer();
4405 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4406 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceGatheringComplete,
4407 caller()->ice_gathering_state(), kMaxWaitForFramesMs);
4408 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceGatheringComplete,
4409 callee()->ice_gathering_state(), kMaxWaitForFramesMs);
4410 // After the best candidate pair is selected and all candidates are signaled,
4411 // the ICE connection state should reach "complete".
4412 // TODO(deadbeef): Currently, the ICE "controlled" agent (the
4413 // answerer/"callee" by default) only reaches "connected". When this is
4414 // fixed, this test should be updated.
4415 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
4416 caller()->ice_connection_state(), kDefaultTimeout);
Alex Loiko9289eda2018-11-23 16:18:59 +00004417 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
4418 callee()->ice_connection_state(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07004419}
4420
Qingsi Wang1dac6d82018-12-12 15:28:47 -08004421constexpr int kOnlyLocalPorts = cricket::PORTALLOCATOR_DISABLE_STUN |
4422 cricket::PORTALLOCATOR_DISABLE_RELAY |
4423 cricket::PORTALLOCATOR_DISABLE_TCP;
Zach Stein6fcdc2f2018-08-23 16:25:55 -07004424
Qingsi Wang1dac6d82018-12-12 15:28:47 -08004425// Use a mock resolver to resolve the hostname back to the original IP on both
4426// sides and check that the ICE connection connects.
Zach Stein6fcdc2f2018-08-23 16:25:55 -07004427TEST_P(PeerConnectionIntegrationTest,
4428 IceStatesReachCompletionWithRemoteHostname) {
Qingsi Wang1dac6d82018-12-12 15:28:47 -08004429 auto caller_resolver_factory =
Mirko Bonadei317a1f02019-09-17 17:06:18 +02004430 std::make_unique<NiceMock<webrtc::MockAsyncResolverFactory>>();
Qingsi Wang1dac6d82018-12-12 15:28:47 -08004431 auto callee_resolver_factory =
Mirko Bonadei317a1f02019-09-17 17:06:18 +02004432 std::make_unique<NiceMock<webrtc::MockAsyncResolverFactory>>();
Qingsi Wang1dac6d82018-12-12 15:28:47 -08004433 NiceMock<rtc::MockAsyncResolver> callee_async_resolver;
4434 NiceMock<rtc::MockAsyncResolver> caller_async_resolver;
Zach Stein6fcdc2f2018-08-23 16:25:55 -07004435
4436 // This also verifies that the injected AsyncResolverFactory is used by
4437 // P2PTransportChannel.
Qingsi Wang1dac6d82018-12-12 15:28:47 -08004438 EXPECT_CALL(*caller_resolver_factory, Create())
4439 .WillOnce(Return(&caller_async_resolver));
4440 webrtc::PeerConnectionDependencies caller_deps(nullptr);
4441 caller_deps.async_resolver_factory = std::move(caller_resolver_factory);
4442
4443 EXPECT_CALL(*callee_resolver_factory, Create())
4444 .WillOnce(Return(&callee_async_resolver));
4445 webrtc::PeerConnectionDependencies callee_deps(nullptr);
4446 callee_deps.async_resolver_factory = std::move(callee_resolver_factory);
4447
4448 PeerConnectionInterface::RTCConfiguration config;
4449 config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
4450 config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyRequire;
4451
4452 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndDeps(
4453 config, std::move(caller_deps), config, std::move(callee_deps)));
4454
4455 caller()->SetRemoteAsyncResolver(&callee_async_resolver);
4456 callee()->SetRemoteAsyncResolver(&caller_async_resolver);
4457
4458 // Enable hostname candidates with mDNS names.
Qingsi Wangecd30542019-05-22 14:34:56 -07004459 caller()->SetMdnsResponder(
Mirko Bonadei317a1f02019-09-17 17:06:18 +02004460 std::make_unique<webrtc::FakeMdnsResponder>(network_thread()));
Qingsi Wangecd30542019-05-22 14:34:56 -07004461 callee()->SetMdnsResponder(
Mirko Bonadei317a1f02019-09-17 17:06:18 +02004462 std::make_unique<webrtc::FakeMdnsResponder>(network_thread()));
Qingsi Wang1dac6d82018-12-12 15:28:47 -08004463
4464 SetPortAllocatorFlags(kOnlyLocalPorts, kOnlyLocalPorts);
Zach Stein6fcdc2f2018-08-23 16:25:55 -07004465
4466 ConnectFakeSignaling();
4467 caller()->AddAudioVideoTracks();
4468 callee()->AddAudioVideoTracks();
4469 caller()->CreateAndSetAndSignalOffer();
4470 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4471 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
4472 caller()->ice_connection_state(), kDefaultTimeout);
4473 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
4474 callee()->ice_connection_state(), kDefaultTimeout);
Jeroen de Borst833979f2018-12-13 08:25:54 -08004475
Ying Wangef3998f2019-12-09 13:06:53 +01004476 EXPECT_METRIC_EQ(1, webrtc::metrics::NumEvents(
4477 "WebRTC.PeerConnection.CandidatePairType_UDP",
4478 webrtc::kIceCandidatePairHostNameHostName));
Zach Stein6fcdc2f2018-08-23 16:25:55 -07004479}
4480
Steve Antonede9ca52017-10-16 13:04:27 -07004481// Test that firewalling the ICE connection causes the clients to identify the
4482// disconnected state and then removing the firewall causes them to reconnect.
4483class PeerConnectionIntegrationIceStatesTest
Seth Hampson2f0d7022018-02-20 11:54:42 -08004484 : public PeerConnectionIntegrationBaseTest,
4485 public ::testing::WithParamInterface<
4486 std::tuple<SdpSemantics, std::tuple<std::string, uint32_t>>> {
Steve Antonede9ca52017-10-16 13:04:27 -07004487 protected:
Seth Hampson2f0d7022018-02-20 11:54:42 -08004488 PeerConnectionIntegrationIceStatesTest()
4489 : PeerConnectionIntegrationBaseTest(std::get<0>(GetParam())) {
4490 port_allocator_flags_ = std::get<1>(std::get<1>(GetParam()));
Steve Antonede9ca52017-10-16 13:04:27 -07004491 }
4492
4493 void StartStunServer(const SocketAddress& server_address) {
4494 stun_server_.reset(
4495 cricket::TestStunServer::Create(network_thread(), server_address));
4496 }
4497
4498 bool TestIPv6() {
4499 return (port_allocator_flags_ & cricket::PORTALLOCATOR_ENABLE_IPV6);
4500 }
4501
4502 void SetPortAllocatorFlags() {
Qingsi Wang1dac6d82018-12-12 15:28:47 -08004503 PeerConnectionIntegrationBaseTest::SetPortAllocatorFlags(
4504 port_allocator_flags_, port_allocator_flags_);
Steve Antonede9ca52017-10-16 13:04:27 -07004505 }
4506
4507 std::vector<SocketAddress> CallerAddresses() {
4508 std::vector<SocketAddress> addresses;
4509 addresses.push_back(SocketAddress("1.1.1.1", 0));
4510 if (TestIPv6()) {
4511 addresses.push_back(SocketAddress("1111:0:a:b:c:d:e:f", 0));
4512 }
4513 return addresses;
4514 }
4515
4516 std::vector<SocketAddress> CalleeAddresses() {
4517 std::vector<SocketAddress> addresses;
4518 addresses.push_back(SocketAddress("2.2.2.2", 0));
4519 if (TestIPv6()) {
4520 addresses.push_back(SocketAddress("2222:0:a:b:c:d:e:f", 0));
4521 }
4522 return addresses;
4523 }
4524
4525 void SetUpNetworkInterfaces() {
4526 // Remove the default interfaces added by the test infrastructure.
Qingsi Wangecd30542019-05-22 14:34:56 -07004527 caller()->network_manager()->RemoveInterface(kDefaultLocalAddress);
4528 callee()->network_manager()->RemoveInterface(kDefaultLocalAddress);
Steve Antonede9ca52017-10-16 13:04:27 -07004529
4530 // Add network addresses for test.
4531 for (const auto& caller_address : CallerAddresses()) {
Qingsi Wangecd30542019-05-22 14:34:56 -07004532 caller()->network_manager()->AddInterface(caller_address);
Steve Antonede9ca52017-10-16 13:04:27 -07004533 }
4534 for (const auto& callee_address : CalleeAddresses()) {
Qingsi Wangecd30542019-05-22 14:34:56 -07004535 callee()->network_manager()->AddInterface(callee_address);
Steve Antonede9ca52017-10-16 13:04:27 -07004536 }
4537 }
4538
4539 private:
4540 uint32_t port_allocator_flags_;
4541 std::unique_ptr<cricket::TestStunServer> stun_server_;
4542};
4543
Yves Gerey100fe632020-01-17 19:15:53 +01004544// Ensure FakeClockForTest is constructed first (see class for rationale).
4545class PeerConnectionIntegrationIceStatesTestWithFakeClock
4546 : public FakeClockForTest,
4547 public PeerConnectionIntegrationIceStatesTest {};
4548
Steve Antonede9ca52017-10-16 13:04:27 -07004549// Tests that the PeerConnection goes through all the ICE gathering/connection
4550// states over the duration of the call. This includes Disconnected and Failed
4551// states, induced by putting a firewall between the peers and waiting for them
4552// to time out.
Yves Gerey100fe632020-01-17 19:15:53 +01004553TEST_P(PeerConnectionIntegrationIceStatesTestWithFakeClock, VerifyIceStates) {
Steve Antonede9ca52017-10-16 13:04:27 -07004554 const SocketAddress kStunServerAddress =
4555 SocketAddress("99.99.99.1", cricket::STUN_SERVER_PORT);
4556 StartStunServer(kStunServerAddress);
4557
4558 PeerConnectionInterface::RTCConfiguration config;
4559 PeerConnectionInterface::IceServer ice_stun_server;
4560 ice_stun_server.urls.push_back(
4561 "stun:" + kStunServerAddress.HostAsURIString() + ":" +
4562 kStunServerAddress.PortAsString());
4563 config.servers.push_back(ice_stun_server);
4564
4565 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
4566 ConnectFakeSignaling();
4567 SetPortAllocatorFlags();
4568 SetUpNetworkInterfaces();
Steve Anton15324772018-01-16 10:26:49 -08004569 caller()->AddAudioVideoTracks();
4570 callee()->AddAudioVideoTracks();
Steve Antonede9ca52017-10-16 13:04:27 -07004571
4572 // Initial state before anything happens.
4573 ASSERT_EQ(PeerConnectionInterface::kIceGatheringNew,
4574 caller()->ice_gathering_state());
4575 ASSERT_EQ(PeerConnectionInterface::kIceConnectionNew,
4576 caller()->ice_connection_state());
Jonas Olsson7a6739e2019-01-15 16:31:55 +01004577 ASSERT_EQ(PeerConnectionInterface::kIceConnectionNew,
4578 caller()->standardized_ice_connection_state());
Steve Antonede9ca52017-10-16 13:04:27 -07004579
4580 // Start the call by creating the offer, setting it as the local description,
4581 // then sending it to the peer who will respond with an answer. This happens
4582 // asynchronously so that we can watch the states as it runs in the
4583 // background.
4584 caller()->CreateAndSetAndSignalOffer();
4585
Steve Antona9b67ce2020-01-16 14:00:44 -08004586 ASSERT_EQ_SIMULATED_WAIT(PeerConnectionInterface::kIceConnectionCompleted,
4587 caller()->ice_connection_state(), kDefaultTimeout,
Yves Gerey100fe632020-01-17 19:15:53 +01004588 FakeClock());
Steve Antona9b67ce2020-01-16 14:00:44 -08004589 ASSERT_EQ_SIMULATED_WAIT(PeerConnectionInterface::kIceConnectionCompleted,
4590 caller()->standardized_ice_connection_state(),
Yves Gerey100fe632020-01-17 19:15:53 +01004591 kDefaultTimeout, FakeClock());
Steve Antonede9ca52017-10-16 13:04:27 -07004592
4593 // Verify that the observer was notified of the intermediate transitions.
4594 EXPECT_THAT(caller()->ice_connection_state_history(),
4595 ElementsAre(PeerConnectionInterface::kIceConnectionChecking,
4596 PeerConnectionInterface::kIceConnectionConnected,
4597 PeerConnectionInterface::kIceConnectionCompleted));
Jonas Olssonacd8ae72019-02-25 15:26:24 +01004598 EXPECT_THAT(caller()->standardized_ice_connection_state_history(),
4599 ElementsAre(PeerConnectionInterface::kIceConnectionChecking,
4600 PeerConnectionInterface::kIceConnectionConnected,
4601 PeerConnectionInterface::kIceConnectionCompleted));
Jonas Olsson635474e2018-10-18 15:58:17 +02004602 EXPECT_THAT(
4603 caller()->peer_connection_state_history(),
4604 ElementsAre(PeerConnectionInterface::PeerConnectionState::kConnecting,
Jonas Olsson635474e2018-10-18 15:58:17 +02004605 PeerConnectionInterface::PeerConnectionState::kConnected));
Steve Antonede9ca52017-10-16 13:04:27 -07004606 EXPECT_THAT(caller()->ice_gathering_state_history(),
4607 ElementsAre(PeerConnectionInterface::kIceGatheringGathering,
4608 PeerConnectionInterface::kIceGatheringComplete));
4609
4610 // Block connections to/from the caller and wait for ICE to become
4611 // disconnected.
4612 for (const auto& caller_address : CallerAddresses()) {
4613 firewall()->AddRule(false, rtc::FP_ANY, rtc::FD_ANY, caller_address);
4614 }
Mirko Bonadei675513b2017-11-09 11:09:25 +01004615 RTC_LOG(LS_INFO) << "Firewall rules applied";
Jonas Olssonb75d9e92019-02-22 10:33:29 +01004616 ASSERT_EQ_SIMULATED_WAIT(PeerConnectionInterface::kIceConnectionDisconnected,
4617 caller()->ice_connection_state(), kDefaultTimeout,
Yves Gerey100fe632020-01-17 19:15:53 +01004618 FakeClock());
Jonas Olssonb75d9e92019-02-22 10:33:29 +01004619 ASSERT_EQ_SIMULATED_WAIT(PeerConnectionInterface::kIceConnectionDisconnected,
4620 caller()->standardized_ice_connection_state(),
Yves Gerey100fe632020-01-17 19:15:53 +01004621 kDefaultTimeout, FakeClock());
Steve Antonede9ca52017-10-16 13:04:27 -07004622
4623 // Let ICE re-establish by removing the firewall rules.
4624 firewall()->ClearRules();
Mirko Bonadei675513b2017-11-09 11:09:25 +01004625 RTC_LOG(LS_INFO) << "Firewall rules cleared";
Jonas Olssonb75d9e92019-02-22 10:33:29 +01004626 ASSERT_EQ_SIMULATED_WAIT(PeerConnectionInterface::kIceConnectionCompleted,
4627 caller()->ice_connection_state(), kDefaultTimeout,
Yves Gerey100fe632020-01-17 19:15:53 +01004628 FakeClock());
Jonas Olssonacd8ae72019-02-25 15:26:24 +01004629 ASSERT_EQ_SIMULATED_WAIT(PeerConnectionInterface::kIceConnectionCompleted,
Jonas Olssonb75d9e92019-02-22 10:33:29 +01004630 caller()->standardized_ice_connection_state(),
Yves Gerey100fe632020-01-17 19:15:53 +01004631 kDefaultTimeout, FakeClock());
Steve Antonede9ca52017-10-16 13:04:27 -07004632
4633 // According to RFC7675, if there is no response within 30 seconds then the
4634 // peer should consider the other side to have rejected the connection. This
Steve Anton83119dd2017-11-10 16:19:52 -08004635 // is signaled by the state transitioning to "failed".
Steve Antonede9ca52017-10-16 13:04:27 -07004636 constexpr int kConsentTimeout = 30000;
4637 for (const auto& caller_address : CallerAddresses()) {
4638 firewall()->AddRule(false, rtc::FP_ANY, rtc::FD_ANY, caller_address);
4639 }
Mirko Bonadei675513b2017-11-09 11:09:25 +01004640 RTC_LOG(LS_INFO) << "Firewall rules applied again";
Jonas Olssonb75d9e92019-02-22 10:33:29 +01004641 ASSERT_EQ_SIMULATED_WAIT(PeerConnectionInterface::kIceConnectionFailed,
4642 caller()->ice_connection_state(), kConsentTimeout,
Yves Gerey100fe632020-01-17 19:15:53 +01004643 FakeClock());
Jonas Olssonb75d9e92019-02-22 10:33:29 +01004644 ASSERT_EQ_SIMULATED_WAIT(PeerConnectionInterface::kIceConnectionFailed,
4645 caller()->standardized_ice_connection_state(),
Yves Gerey100fe632020-01-17 19:15:53 +01004646 kConsentTimeout, FakeClock());
Jonas Olssonb75d9e92019-02-22 10:33:29 +01004647}
4648
4649// Tests that if the connection doesn't get set up properly we eventually reach
4650// the "failed" iceConnectionState.
Yves Gerey100fe632020-01-17 19:15:53 +01004651TEST_P(PeerConnectionIntegrationIceStatesTestWithFakeClock,
4652 IceStateSetupFailure) {
Jonas Olssonb75d9e92019-02-22 10:33:29 +01004653 // Block connections to/from the caller and wait for ICE to become
4654 // disconnected.
4655 for (const auto& caller_address : CallerAddresses()) {
4656 firewall()->AddRule(false, rtc::FP_ANY, rtc::FD_ANY, caller_address);
4657 }
4658
4659 ASSERT_TRUE(CreatePeerConnectionWrappers());
4660 ConnectFakeSignaling();
4661 SetPortAllocatorFlags();
4662 SetUpNetworkInterfaces();
4663 caller()->AddAudioVideoTracks();
4664 caller()->CreateAndSetAndSignalOffer();
4665
4666 // According to RFC7675, if there is no response within 30 seconds then the
4667 // peer should consider the other side to have rejected the connection. This
4668 // is signaled by the state transitioning to "failed".
4669 constexpr int kConsentTimeout = 30000;
4670 ASSERT_EQ_SIMULATED_WAIT(PeerConnectionInterface::kIceConnectionFailed,
4671 caller()->standardized_ice_connection_state(),
Yves Gerey100fe632020-01-17 19:15:53 +01004672 kConsentTimeout, FakeClock());
Steve Antonede9ca52017-10-16 13:04:27 -07004673}
4674
4675// Tests that the best connection is set to the appropriate IPv4/IPv6 connection
4676// and that the statistics in the metric observers are updated correctly.
4677TEST_P(PeerConnectionIntegrationIceStatesTest, VerifyBestConnection) {
4678 ASSERT_TRUE(CreatePeerConnectionWrappers());
4679 ConnectFakeSignaling();
4680 SetPortAllocatorFlags();
4681 SetUpNetworkInterfaces();
Steve Anton15324772018-01-16 10:26:49 -08004682 caller()->AddAudioVideoTracks();
4683 callee()->AddAudioVideoTracks();
Steve Antonede9ca52017-10-16 13:04:27 -07004684 caller()->CreateAndSetAndSignalOffer();
4685
4686 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Steve Anton692f3c72020-01-16 14:12:31 -08004687 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
4688 caller()->ice_connection_state(), kDefaultTimeout);
4689 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
4690 callee()->ice_connection_state(), kDefaultTimeout);
Steve Antonede9ca52017-10-16 13:04:27 -07004691
Qingsi Wang7fc821d2018-07-12 12:54:53 -07004692 // TODO(bugs.webrtc.org/9456): Fix it.
4693 const int num_best_ipv4 = webrtc::metrics::NumEvents(
4694 "WebRTC.PeerConnection.IPMetrics", webrtc::kBestConnections_IPv4);
4695 const int num_best_ipv6 = webrtc::metrics::NumEvents(
4696 "WebRTC.PeerConnection.IPMetrics", webrtc::kBestConnections_IPv6);
Steve Antonede9ca52017-10-16 13:04:27 -07004697 if (TestIPv6()) {
4698 // When IPv6 is enabled, we should prefer an IPv6 connection over an IPv4
4699 // connection.
Ying Wangef3998f2019-12-09 13:06:53 +01004700 EXPECT_METRIC_EQ(0, num_best_ipv4);
4701 EXPECT_METRIC_EQ(1, num_best_ipv6);
Steve Antonede9ca52017-10-16 13:04:27 -07004702 } else {
Ying Wangef3998f2019-12-09 13:06:53 +01004703 EXPECT_METRIC_EQ(1, num_best_ipv4);
4704 EXPECT_METRIC_EQ(0, num_best_ipv6);
Steve Antonede9ca52017-10-16 13:04:27 -07004705 }
4706
Ying Wangef3998f2019-12-09 13:06:53 +01004707 EXPECT_METRIC_EQ(0, webrtc::metrics::NumEvents(
4708 "WebRTC.PeerConnection.CandidatePairType_UDP",
4709 webrtc::kIceCandidatePairHostHost));
4710 EXPECT_METRIC_EQ(1, webrtc::metrics::NumEvents(
4711 "WebRTC.PeerConnection.CandidatePairType_UDP",
4712 webrtc::kIceCandidatePairHostPublicHostPublic));
Steve Antonede9ca52017-10-16 13:04:27 -07004713}
4714
4715constexpr uint32_t kFlagsIPv4NoStun = cricket::PORTALLOCATOR_DISABLE_TCP |
4716 cricket::PORTALLOCATOR_DISABLE_STUN |
4717 cricket::PORTALLOCATOR_DISABLE_RELAY;
4718constexpr uint32_t kFlagsIPv6NoStun =
4719 cricket::PORTALLOCATOR_DISABLE_TCP | cricket::PORTALLOCATOR_DISABLE_STUN |
4720 cricket::PORTALLOCATOR_ENABLE_IPV6 | cricket::PORTALLOCATOR_DISABLE_RELAY;
4721constexpr uint32_t kFlagsIPv4Stun =
4722 cricket::PORTALLOCATOR_DISABLE_TCP | cricket::PORTALLOCATOR_DISABLE_RELAY;
4723
Mirko Bonadeic84f6612019-01-31 12:20:57 +01004724INSTANTIATE_TEST_SUITE_P(
Seth Hampson2f0d7022018-02-20 11:54:42 -08004725 PeerConnectionIntegrationTest,
4726 PeerConnectionIntegrationIceStatesTest,
4727 Combine(Values(SdpSemantics::kPlanB, SdpSemantics::kUnifiedPlan),
4728 Values(std::make_pair("IPv4 no STUN", kFlagsIPv4NoStun),
4729 std::make_pair("IPv6 no STUN", kFlagsIPv6NoStun),
4730 std::make_pair("IPv4 with STUN", kFlagsIPv4Stun))));
Steve Antonede9ca52017-10-16 13:04:27 -07004731
Yves Gerey100fe632020-01-17 19:15:53 +01004732INSTANTIATE_TEST_SUITE_P(
4733 PeerConnectionIntegrationTest,
4734 PeerConnectionIntegrationIceStatesTestWithFakeClock,
4735 Combine(Values(SdpSemantics::kPlanB, SdpSemantics::kUnifiedPlan),
4736 Values(std::make_pair("IPv4 no STUN", kFlagsIPv4NoStun),
4737 std::make_pair("IPv6 no STUN", kFlagsIPv6NoStun),
4738 std::make_pair("IPv4 with STUN", kFlagsIPv4Stun))));
4739
deadbeef1dcb1642017-03-29 21:08:16 -07004740// This test sets up a call between two parties with audio and video.
4741// During the call, the caller restarts ICE and the test verifies that
4742// new ICE candidates are generated and audio and video still can flow, and the
4743// ICE state reaches completed again.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004744TEST_P(PeerConnectionIntegrationTest, MediaContinuesFlowingAfterIceRestart) {
deadbeef1dcb1642017-03-29 21:08:16 -07004745 ASSERT_TRUE(CreatePeerConnectionWrappers());
4746 ConnectFakeSignaling();
4747 // Do normal offer/answer and wait for ICE to complete.
Steve Anton15324772018-01-16 10:26:49 -08004748 caller()->AddAudioVideoTracks();
4749 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07004750 caller()->CreateAndSetAndSignalOffer();
4751 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4752 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
4753 caller()->ice_connection_state(), kMaxWaitForFramesMs);
Alex Loiko9289eda2018-11-23 16:18:59 +00004754 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
4755 callee()->ice_connection_state(), kMaxWaitForFramesMs);
deadbeef1dcb1642017-03-29 21:08:16 -07004756
4757 // To verify that the ICE restart actually occurs, get
4758 // ufrag/password/candidates before and after restart.
4759 // Create an SDP string of the first audio candidate for both clients.
4760 const webrtc::IceCandidateCollection* audio_candidates_caller =
4761 caller()->pc()->local_description()->candidates(0);
4762 const webrtc::IceCandidateCollection* audio_candidates_callee =
4763 callee()->pc()->local_description()->candidates(0);
4764 ASSERT_GT(audio_candidates_caller->count(), 0u);
4765 ASSERT_GT(audio_candidates_callee->count(), 0u);
4766 std::string caller_candidate_pre_restart;
4767 ASSERT_TRUE(
4768 audio_candidates_caller->at(0)->ToString(&caller_candidate_pre_restart));
4769 std::string callee_candidate_pre_restart;
4770 ASSERT_TRUE(
4771 audio_candidates_callee->at(0)->ToString(&callee_candidate_pre_restart));
4772 const cricket::SessionDescription* desc =
4773 caller()->pc()->local_description()->description();
4774 std::string caller_ufrag_pre_restart =
4775 desc->transport_infos()[0].description.ice_ufrag;
4776 desc = callee()->pc()->local_description()->description();
4777 std::string callee_ufrag_pre_restart =
4778 desc->transport_infos()[0].description.ice_ufrag;
4779
Alex Drake00c7ecf2019-08-06 10:54:47 -07004780 EXPECT_EQ(caller()->ice_candidate_pair_change_history().size(), 1u);
deadbeef1dcb1642017-03-29 21:08:16 -07004781 // Have the caller initiate an ICE restart.
4782 caller()->SetOfferAnswerOptions(IceRestartOfferAnswerOptions());
4783 caller()->CreateAndSetAndSignalOffer();
4784 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4785 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
4786 caller()->ice_connection_state(), kMaxWaitForFramesMs);
Alex Loiko9289eda2018-11-23 16:18:59 +00004787 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
deadbeef1dcb1642017-03-29 21:08:16 -07004788 callee()->ice_connection_state(), kMaxWaitForFramesMs);
4789
4790 // Grab the ufrags/candidates again.
4791 audio_candidates_caller = caller()->pc()->local_description()->candidates(0);
4792 audio_candidates_callee = callee()->pc()->local_description()->candidates(0);
4793 ASSERT_GT(audio_candidates_caller->count(), 0u);
4794 ASSERT_GT(audio_candidates_callee->count(), 0u);
4795 std::string caller_candidate_post_restart;
4796 ASSERT_TRUE(
4797 audio_candidates_caller->at(0)->ToString(&caller_candidate_post_restart));
4798 std::string callee_candidate_post_restart;
4799 ASSERT_TRUE(
4800 audio_candidates_callee->at(0)->ToString(&callee_candidate_post_restart));
4801 desc = caller()->pc()->local_description()->description();
4802 std::string caller_ufrag_post_restart =
4803 desc->transport_infos()[0].description.ice_ufrag;
4804 desc = callee()->pc()->local_description()->description();
4805 std::string callee_ufrag_post_restart =
4806 desc->transport_infos()[0].description.ice_ufrag;
4807 // Sanity check that an ICE restart was actually negotiated in SDP.
4808 ASSERT_NE(caller_candidate_pre_restart, caller_candidate_post_restart);
4809 ASSERT_NE(callee_candidate_pre_restart, callee_candidate_post_restart);
4810 ASSERT_NE(caller_ufrag_pre_restart, caller_ufrag_post_restart);
4811 ASSERT_NE(callee_ufrag_pre_restart, callee_ufrag_post_restart);
Alex Drake00c7ecf2019-08-06 10:54:47 -07004812 EXPECT_GT(caller()->ice_candidate_pair_change_history().size(), 1u);
deadbeef1dcb1642017-03-29 21:08:16 -07004813
4814 // Ensure that additional frames are received after the ICE restart.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004815 MediaExpectations media_expectations;
4816 media_expectations.ExpectBidirectionalAudioAndVideo();
4817 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07004818}
4819
4820// Verify that audio/video can be received end-to-end when ICE renomination is
4821// enabled.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004822TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithIceRenomination) {
deadbeef1dcb1642017-03-29 21:08:16 -07004823 PeerConnectionInterface::RTCConfiguration config;
4824 config.enable_ice_renomination = true;
4825 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
4826 ConnectFakeSignaling();
4827 // Do normal offer/answer and wait for some frames to be received in each
4828 // direction.
Steve Anton15324772018-01-16 10:26:49 -08004829 caller()->AddAudioVideoTracks();
4830 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07004831 caller()->CreateAndSetAndSignalOffer();
4832 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4833 // Sanity check that ICE renomination was actually negotiated.
4834 const cricket::SessionDescription* desc =
4835 caller()->pc()->local_description()->description();
4836 for (const cricket::TransportInfo& info : desc->transport_infos()) {
Steve Anton64b626b2019-01-28 17:25:26 -08004837 ASSERT_THAT(info.description.transport_options, Contains("renomination"));
deadbeef1dcb1642017-03-29 21:08:16 -07004838 }
4839 desc = callee()->pc()->local_description()->description();
4840 for (const cricket::TransportInfo& info : desc->transport_infos()) {
Steve Anton64b626b2019-01-28 17:25:26 -08004841 ASSERT_THAT(info.description.transport_options, Contains("renomination"));
deadbeef1dcb1642017-03-29 21:08:16 -07004842 }
Seth Hampson2f0d7022018-02-20 11:54:42 -08004843 MediaExpectations media_expectations;
4844 media_expectations.ExpectBidirectionalAudioAndVideo();
4845 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07004846}
4847
Steve Anton6f25b092017-10-23 09:39:20 -07004848// With a max bundle policy and RTCP muxing, adding a new media description to
4849// the connection should not affect ICE at all because the new media will use
4850// the existing connection.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004851TEST_P(PeerConnectionIntegrationTest,
Steve Anton83119dd2017-11-10 16:19:52 -08004852 AddMediaToConnectedBundleDoesNotRestartIce) {
Steve Anton6f25b092017-10-23 09:39:20 -07004853 PeerConnectionInterface::RTCConfiguration config;
4854 config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
4855 config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyRequire;
4856 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(
4857 config, PeerConnectionInterface::RTCConfiguration()));
4858 ConnectFakeSignaling();
4859
Steve Anton15324772018-01-16 10:26:49 -08004860 caller()->AddAudioTrack();
Steve Anton6f25b092017-10-23 09:39:20 -07004861 caller()->CreateAndSetAndSignalOffer();
4862 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Steve Antonff52f1b2017-10-26 12:24:50 -07004863 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceConnectionCompleted,
4864 caller()->ice_connection_state(), kDefaultTimeout);
Steve Anton6f25b092017-10-23 09:39:20 -07004865
4866 caller()->clear_ice_connection_state_history();
4867
Steve Anton15324772018-01-16 10:26:49 -08004868 caller()->AddVideoTrack();
Steve Anton6f25b092017-10-23 09:39:20 -07004869 caller()->CreateAndSetAndSignalOffer();
4870 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4871
4872 EXPECT_EQ(0u, caller()->ice_connection_state_history().size());
4873}
4874
deadbeef1dcb1642017-03-29 21:08:16 -07004875// This test sets up a call between two parties with audio and video. It then
4876// renegotiates setting the video m-line to "port 0", then later renegotiates
4877// again, enabling video.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004878TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07004879 VideoFlowsAfterMediaSectionIsRejectedAndRecycled) {
4880 ASSERT_TRUE(CreatePeerConnectionWrappers());
4881 ConnectFakeSignaling();
4882
4883 // Do initial negotiation, only sending media from the caller. Will result in
4884 // video and audio recvonly "m=" sections.
Steve Anton15324772018-01-16 10:26:49 -08004885 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07004886 caller()->CreateAndSetAndSignalOffer();
4887 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4888
4889 // Negotiate again, disabling the video "m=" section (the callee will set the
4890 // port to 0 due to offer_to_receive_video = 0).
Seth Hampson2f0d7022018-02-20 11:54:42 -08004891 if (sdp_semantics_ == SdpSemantics::kPlanB) {
4892 PeerConnectionInterface::RTCOfferAnswerOptions options;
4893 options.offer_to_receive_video = 0;
4894 callee()->SetOfferAnswerOptions(options);
4895 } else {
4896 callee()->SetRemoteOfferHandler([this] {
4897 callee()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)->Stop();
4898 });
4899 }
deadbeef1dcb1642017-03-29 21:08:16 -07004900 caller()->CreateAndSetAndSignalOffer();
4901 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4902 // Sanity check that video "m=" section was actually rejected.
4903 const ContentInfo* answer_video_content = cricket::GetFirstVideoContent(
4904 callee()->pc()->local_description()->description());
4905 ASSERT_NE(nullptr, answer_video_content);
4906 ASSERT_TRUE(answer_video_content->rejected);
4907
4908 // Enable video and do negotiation again, making sure video is received
4909 // end-to-end, also adding media stream to callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004910 if (sdp_semantics_ == SdpSemantics::kPlanB) {
4911 PeerConnectionInterface::RTCOfferAnswerOptions options;
4912 options.offer_to_receive_video = 1;
4913 callee()->SetOfferAnswerOptions(options);
4914 } else {
4915 // The caller's transceiver is stopped, so we need to add another track.
4916 auto caller_transceiver =
4917 caller()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO);
4918 EXPECT_TRUE(caller_transceiver->stopped());
4919 caller()->AddVideoTrack();
4920 }
4921 callee()->AddVideoTrack();
4922 callee()->SetRemoteOfferHandler(nullptr);
deadbeef1dcb1642017-03-29 21:08:16 -07004923 caller()->CreateAndSetAndSignalOffer();
4924 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08004925
deadbeef1dcb1642017-03-29 21:08:16 -07004926 // Verify the caller receives frames from the newly added stream, and the
4927 // callee receives additional frames from the re-enabled video m= section.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004928 MediaExpectations media_expectations;
4929 media_expectations.CalleeExpectsSomeAudio();
4930 media_expectations.ExpectBidirectionalVideo();
4931 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07004932}
4933
deadbeef1dcb1642017-03-29 21:08:16 -07004934// This tests that if we negotiate after calling CreateSender but before we
4935// have a track, then set a track later, frames from the newly-set track are
4936// received end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004937TEST_F(PeerConnectionIntegrationTestPlanB,
deadbeef1dcb1642017-03-29 21:08:16 -07004938 MediaFlowsAfterEarlyWarmupWithCreateSender) {
4939 ASSERT_TRUE(CreatePeerConnectionWrappers());
4940 ConnectFakeSignaling();
4941 auto caller_audio_sender =
4942 caller()->pc()->CreateSender("audio", "caller_stream");
4943 auto caller_video_sender =
4944 caller()->pc()->CreateSender("video", "caller_stream");
4945 auto callee_audio_sender =
4946 callee()->pc()->CreateSender("audio", "callee_stream");
4947 auto callee_video_sender =
4948 callee()->pc()->CreateSender("video", "callee_stream");
4949 caller()->CreateAndSetAndSignalOffer();
4950 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
4951 // Wait for ICE to complete, without any tracks being set.
4952 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
4953 caller()->ice_connection_state(), kMaxWaitForFramesMs);
4954 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
4955 callee()->ice_connection_state(), kMaxWaitForFramesMs);
4956 // Now set the tracks, and expect frames to immediately start flowing.
4957 EXPECT_TRUE(caller_audio_sender->SetTrack(caller()->CreateLocalAudioTrack()));
4958 EXPECT_TRUE(caller_video_sender->SetTrack(caller()->CreateLocalVideoTrack()));
4959 EXPECT_TRUE(callee_audio_sender->SetTrack(callee()->CreateLocalAudioTrack()));
4960 EXPECT_TRUE(callee_video_sender->SetTrack(callee()->CreateLocalVideoTrack()));
Seth Hampson2f0d7022018-02-20 11:54:42 -08004961 MediaExpectations media_expectations;
4962 media_expectations.ExpectBidirectionalAudioAndVideo();
4963 ASSERT_TRUE(ExpectNewFrames(media_expectations));
4964}
4965
4966// This tests that if we negotiate after calling AddTransceiver but before we
4967// have a track, then set a track later, frames from the newly-set tracks are
4968// received end-to-end.
4969TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
4970 MediaFlowsAfterEarlyWarmupWithAddTransceiver) {
4971 ASSERT_TRUE(CreatePeerConnectionWrappers());
4972 ConnectFakeSignaling();
4973 auto audio_result = caller()->pc()->AddTransceiver(cricket::MEDIA_TYPE_AUDIO);
4974 ASSERT_EQ(RTCErrorType::NONE, audio_result.error().type());
4975 auto caller_audio_sender = audio_result.MoveValue()->sender();
4976 auto video_result = caller()->pc()->AddTransceiver(cricket::MEDIA_TYPE_VIDEO);
4977 ASSERT_EQ(RTCErrorType::NONE, video_result.error().type());
4978 auto caller_video_sender = video_result.MoveValue()->sender();
4979 callee()->SetRemoteOfferHandler([this] {
4980 ASSERT_EQ(2u, callee()->pc()->GetTransceivers().size());
4981 callee()->pc()->GetTransceivers()[0]->SetDirection(
4982 RtpTransceiverDirection::kSendRecv);
4983 callee()->pc()->GetTransceivers()[1]->SetDirection(
4984 RtpTransceiverDirection::kSendRecv);
4985 });
4986 caller()->CreateAndSetAndSignalOffer();
4987 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
4988 // Wait for ICE to complete, without any tracks being set.
4989 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
4990 caller()->ice_connection_state(), kMaxWaitForFramesMs);
4991 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
4992 callee()->ice_connection_state(), kMaxWaitForFramesMs);
4993 // Now set the tracks, and expect frames to immediately start flowing.
4994 auto callee_audio_sender = callee()->pc()->GetSenders()[0];
4995 auto callee_video_sender = callee()->pc()->GetSenders()[1];
4996 ASSERT_TRUE(caller_audio_sender->SetTrack(caller()->CreateLocalAudioTrack()));
4997 ASSERT_TRUE(caller_video_sender->SetTrack(caller()->CreateLocalVideoTrack()));
4998 ASSERT_TRUE(callee_audio_sender->SetTrack(callee()->CreateLocalAudioTrack()));
4999 ASSERT_TRUE(callee_video_sender->SetTrack(callee()->CreateLocalVideoTrack()));
5000 MediaExpectations media_expectations;
5001 media_expectations.ExpectBidirectionalAudioAndVideo();
5002 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07005003}
5004
5005// This test verifies that a remote video track can be added via AddStream,
5006// and sent end-to-end. For this particular test, it's simply echoed back
5007// from the caller to the callee, rather than being forwarded to a third
5008// PeerConnection.
Seth Hampson2f0d7022018-02-20 11:54:42 -08005009TEST_F(PeerConnectionIntegrationTestPlanB, CanSendRemoteVideoTrack) {
deadbeef1dcb1642017-03-29 21:08:16 -07005010 ASSERT_TRUE(CreatePeerConnectionWrappers());
5011 ConnectFakeSignaling();
5012 // Just send a video track from the caller.
Steve Anton15324772018-01-16 10:26:49 -08005013 caller()->AddVideoTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07005014 caller()->CreateAndSetAndSignalOffer();
5015 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02005016 ASSERT_EQ(1U, callee()->remote_streams()->count());
deadbeef1dcb1642017-03-29 21:08:16 -07005017
5018 // Echo the stream back, and do a new offer/anwer (initiated by callee this
5019 // time).
5020 callee()->pc()->AddStream(callee()->remote_streams()->at(0));
5021 callee()->CreateAndSetAndSignalOffer();
5022 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
5023
Seth Hampson2f0d7022018-02-20 11:54:42 -08005024 MediaExpectations media_expectations;
5025 media_expectations.ExpectBidirectionalVideo();
5026 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07005027}
5028
5029// Test that we achieve the expected end-to-end connection time, using a
5030// fake clock and simulated latency on the media and signaling paths.
5031// We use a TURN<->TURN connection because this is usually the quickest to
5032// set up initially, especially when we're confident the connection will work
5033// and can start sending media before we get a STUN response.
5034//
5035// With various optimizations enabled, here are the network delays we expect to
5036// be on the critical path:
5037// 1. 2 signaling trips: Signaling offer and offerer's TURN candidate, then
5038// signaling answer (with DTLS fingerprint).
5039// 2. 9 media hops: Rest of the DTLS handshake. 3 hops in each direction when
5040// using TURN<->TURN pair, and DTLS exchange is 4 packets,
5041// the first of which should have arrived before the answer.
Yves Gerey100fe632020-01-17 19:15:53 +01005042TEST_P(PeerConnectionIntegrationTestWithFakeClock,
5043 EndToEndConnectionTimeWithTurnTurnPair) {
deadbeef1dcb1642017-03-29 21:08:16 -07005044 static constexpr int media_hop_delay_ms = 50;
5045 static constexpr int signaling_trip_delay_ms = 500;
5046 // For explanation of these values, see comment above.
5047 static constexpr int required_media_hops = 9;
5048 static constexpr int required_signaling_trips = 2;
5049 // For internal delays (such as posting an event asychronously).
5050 static constexpr int allowed_internal_delay_ms = 20;
5051 static constexpr int total_connection_time_ms =
5052 media_hop_delay_ms * required_media_hops +
5053 signaling_trip_delay_ms * required_signaling_trips +
5054 allowed_internal_delay_ms;
5055
5056 static const rtc::SocketAddress turn_server_1_internal_address{"88.88.88.0",
5057 3478};
5058 static const rtc::SocketAddress turn_server_1_external_address{"88.88.88.1",
5059 0};
5060 static const rtc::SocketAddress turn_server_2_internal_address{"99.99.99.0",
5061 3478};
5062 static const rtc::SocketAddress turn_server_2_external_address{"99.99.99.1",
5063 0};
Seth Hampsonaed71642018-06-11 07:41:32 -07005064 cricket::TestTurnServer* turn_server_1 = CreateTurnServer(
5065 turn_server_1_internal_address, turn_server_1_external_address);
Jonas Orelandbdcee282017-10-10 14:01:40 +02005066
Seth Hampsonaed71642018-06-11 07:41:32 -07005067 cricket::TestTurnServer* turn_server_2 = CreateTurnServer(
5068 turn_server_2_internal_address, turn_server_2_external_address);
deadbeef1dcb1642017-03-29 21:08:16 -07005069 // Bypass permission check on received packets so media can be sent before
5070 // the candidate is signaled.
Seth Hampsonaed71642018-06-11 07:41:32 -07005071 network_thread()->Invoke<void>(RTC_FROM_HERE, [turn_server_1] {
5072 turn_server_1->set_enable_permission_checks(false);
5073 });
5074 network_thread()->Invoke<void>(RTC_FROM_HERE, [turn_server_2] {
5075 turn_server_2->set_enable_permission_checks(false);
5076 });
deadbeef1dcb1642017-03-29 21:08:16 -07005077
5078 PeerConnectionInterface::RTCConfiguration client_1_config;
5079 webrtc::PeerConnectionInterface::IceServer ice_server_1;
5080 ice_server_1.urls.push_back("turn:88.88.88.0:3478");
5081 ice_server_1.username = "test";
5082 ice_server_1.password = "test";
5083 client_1_config.servers.push_back(ice_server_1);
5084 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
5085 client_1_config.presume_writable_when_fully_relayed = true;
5086
5087 PeerConnectionInterface::RTCConfiguration client_2_config;
5088 webrtc::PeerConnectionInterface::IceServer ice_server_2;
5089 ice_server_2.urls.push_back("turn:99.99.99.0:3478");
5090 ice_server_2.username = "test";
5091 ice_server_2.password = "test";
5092 client_2_config.servers.push_back(ice_server_2);
5093 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
5094 client_2_config.presume_writable_when_fully_relayed = true;
5095
5096 ASSERT_TRUE(
5097 CreatePeerConnectionWrappersWithConfig(client_1_config, client_2_config));
5098 // Set up the simulated delays.
5099 SetSignalingDelayMs(signaling_trip_delay_ms);
5100 ConnectFakeSignaling();
5101 virtual_socket_server()->set_delay_mean(media_hop_delay_ms);
5102 virtual_socket_server()->UpdateDelayDistribution();
5103
5104 // Set "offer to receive audio/video" without adding any tracks, so we just
5105 // set up ICE/DTLS with no media.
5106 PeerConnectionInterface::RTCOfferAnswerOptions options;
5107 options.offer_to_receive_audio = 1;
5108 options.offer_to_receive_video = 1;
5109 caller()->SetOfferAnswerOptions(options);
5110 caller()->CreateAndSetAndSignalOffer();
deadbeef71452802017-05-07 17:21:01 -07005111 EXPECT_TRUE_SIMULATED_WAIT(DtlsConnected(), total_connection_time_ms,
Yves Gerey100fe632020-01-17 19:15:53 +01005112 FakeClock());
Seth Hampson1d4a76d2018-06-19 14:31:41 -07005113 // Closing the PeerConnections destroys the ports before the ScopedFakeClock.
5114 // If this is not done a DCHECK can be hit in ports.cc, because a large
5115 // negative number is calculated for the rtt due to the global clock changing.
Steve Antond91969e2019-05-30 12:27:03 -07005116 ClosePeerConnections();
deadbeef1dcb1642017-03-29 21:08:16 -07005117}
5118
Jonas Orelandbdcee282017-10-10 14:01:40 +02005119// Verify that a TurnCustomizer passed in through RTCConfiguration
5120// is actually used by the underlying TURN candidate pair.
5121// Note that turnport_unittest.cc contains more detailed, lower-level tests.
Seth Hampson2f0d7022018-02-20 11:54:42 -08005122TEST_P(PeerConnectionIntegrationTest, TurnCustomizerUsedForTurnConnections) {
Jonas Orelandbdcee282017-10-10 14:01:40 +02005123 static const rtc::SocketAddress turn_server_1_internal_address{"88.88.88.0",
5124 3478};
5125 static const rtc::SocketAddress turn_server_1_external_address{"88.88.88.1",
5126 0};
5127 static const rtc::SocketAddress turn_server_2_internal_address{"99.99.99.0",
5128 3478};
5129 static const rtc::SocketAddress turn_server_2_external_address{"99.99.99.1",
5130 0};
Seth Hampsonaed71642018-06-11 07:41:32 -07005131 CreateTurnServer(turn_server_1_internal_address,
5132 turn_server_1_external_address);
5133 CreateTurnServer(turn_server_2_internal_address,
5134 turn_server_2_external_address);
Jonas Orelandbdcee282017-10-10 14:01:40 +02005135
5136 PeerConnectionInterface::RTCConfiguration client_1_config;
5137 webrtc::PeerConnectionInterface::IceServer ice_server_1;
5138 ice_server_1.urls.push_back("turn:88.88.88.0:3478");
5139 ice_server_1.username = "test";
5140 ice_server_1.password = "test";
5141 client_1_config.servers.push_back(ice_server_1);
5142 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
Seth Hampsonaed71642018-06-11 07:41:32 -07005143 auto* customizer1 = CreateTurnCustomizer();
5144 client_1_config.turn_customizer = customizer1;
Jonas Orelandbdcee282017-10-10 14:01:40 +02005145
5146 PeerConnectionInterface::RTCConfiguration client_2_config;
5147 webrtc::PeerConnectionInterface::IceServer ice_server_2;
5148 ice_server_2.urls.push_back("turn:99.99.99.0:3478");
5149 ice_server_2.username = "test";
5150 ice_server_2.password = "test";
5151 client_2_config.servers.push_back(ice_server_2);
5152 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
Seth Hampsonaed71642018-06-11 07:41:32 -07005153 auto* customizer2 = CreateTurnCustomizer();
5154 client_2_config.turn_customizer = customizer2;
Jonas Orelandbdcee282017-10-10 14:01:40 +02005155
5156 ASSERT_TRUE(
5157 CreatePeerConnectionWrappersWithConfig(client_1_config, client_2_config));
5158 ConnectFakeSignaling();
5159
5160 // Set "offer to receive audio/video" without adding any tracks, so we just
5161 // set up ICE/DTLS with no media.
5162 PeerConnectionInterface::RTCOfferAnswerOptions options;
5163 options.offer_to_receive_audio = 1;
5164 options.offer_to_receive_video = 1;
5165 caller()->SetOfferAnswerOptions(options);
5166 caller()->CreateAndSetAndSignalOffer();
5167 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
5168
Seth Hampsonaed71642018-06-11 07:41:32 -07005169 ExpectTurnCustomizerCountersIncremented(customizer1);
5170 ExpectTurnCustomizerCountersIncremented(customizer2);
Jonas Orelandbdcee282017-10-10 14:01:40 +02005171}
5172
Benjamin Wright2d5f3cb2018-05-22 14:46:06 -07005173// Verifies that you can use TCP instead of UDP to connect to a TURN server and
5174// send media between the caller and the callee.
5175TEST_P(PeerConnectionIntegrationTest, TCPUsedForTurnConnections) {
5176 static const rtc::SocketAddress turn_server_internal_address{"88.88.88.0",
5177 3478};
5178 static const rtc::SocketAddress turn_server_external_address{"88.88.88.1", 0};
5179
5180 // Enable TCP for the fake turn server.
Seth Hampsonaed71642018-06-11 07:41:32 -07005181 CreateTurnServer(turn_server_internal_address, turn_server_external_address,
5182 cricket::PROTO_TCP);
Benjamin Wright2d5f3cb2018-05-22 14:46:06 -07005183
5184 webrtc::PeerConnectionInterface::IceServer ice_server;
5185 ice_server.urls.push_back("turn:88.88.88.0:3478?transport=tcp");
5186 ice_server.username = "test";
5187 ice_server.password = "test";
5188
5189 PeerConnectionInterface::RTCConfiguration client_1_config;
5190 client_1_config.servers.push_back(ice_server);
5191 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
5192
5193 PeerConnectionInterface::RTCConfiguration client_2_config;
5194 client_2_config.servers.push_back(ice_server);
5195 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
5196
5197 ASSERT_TRUE(
5198 CreatePeerConnectionWrappersWithConfig(client_1_config, client_2_config));
5199
5200 // Do normal offer/answer and wait for ICE to complete.
5201 ConnectFakeSignaling();
5202 caller()->AddAudioVideoTracks();
5203 callee()->AddAudioVideoTracks();
5204 caller()->CreateAndSetAndSignalOffer();
5205 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5206 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
5207 callee()->ice_connection_state(), kMaxWaitForFramesMs);
5208
5209 MediaExpectations media_expectations;
5210 media_expectations.ExpectBidirectionalAudioAndVideo();
5211 EXPECT_TRUE(ExpectNewFrames(media_expectations));
5212}
5213
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07005214// Verify that a SSLCertificateVerifier passed in through
5215// PeerConnectionDependencies is actually used by the underlying SSL
5216// implementation to determine whether a certificate presented by the TURN
5217// server is accepted by the client. Note that openssladapter_unittest.cc
5218// contains more detailed, lower-level tests.
5219TEST_P(PeerConnectionIntegrationTest,
5220 SSLCertificateVerifierUsedForTurnConnections) {
5221 static const rtc::SocketAddress turn_server_internal_address{"88.88.88.0",
5222 3478};
5223 static const rtc::SocketAddress turn_server_external_address{"88.88.88.1", 0};
5224
5225 // Enable TCP-TLS for the fake turn server. We need to pass in 88.88.88.0 so
5226 // that host name verification passes on the fake certificate.
Seth Hampsonaed71642018-06-11 07:41:32 -07005227 CreateTurnServer(turn_server_internal_address, turn_server_external_address,
5228 cricket::PROTO_TLS, "88.88.88.0");
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07005229
5230 webrtc::PeerConnectionInterface::IceServer ice_server;
5231 ice_server.urls.push_back("turns:88.88.88.0:3478?transport=tcp");
5232 ice_server.username = "test";
5233 ice_server.password = "test";
5234
5235 PeerConnectionInterface::RTCConfiguration client_1_config;
5236 client_1_config.servers.push_back(ice_server);
5237 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
5238
5239 PeerConnectionInterface::RTCConfiguration client_2_config;
5240 client_2_config.servers.push_back(ice_server);
5241 // Setting the type to kRelay forces the connection to go through a TURN
5242 // server.
5243 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
5244
5245 // Get a copy to the pointer so we can verify calls later.
5246 rtc::TestCertificateVerifier* client_1_cert_verifier =
5247 new rtc::TestCertificateVerifier();
5248 client_1_cert_verifier->verify_certificate_ = true;
5249 rtc::TestCertificateVerifier* client_2_cert_verifier =
5250 new rtc::TestCertificateVerifier();
5251 client_2_cert_verifier->verify_certificate_ = true;
5252
5253 // Create the dependencies with the test certificate verifier.
5254 webrtc::PeerConnectionDependencies client_1_deps(nullptr);
5255 client_1_deps.tls_cert_verifier =
5256 std::unique_ptr<rtc::TestCertificateVerifier>(client_1_cert_verifier);
5257 webrtc::PeerConnectionDependencies client_2_deps(nullptr);
5258 client_2_deps.tls_cert_verifier =
5259 std::unique_ptr<rtc::TestCertificateVerifier>(client_2_cert_verifier);
5260
5261 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndDeps(
5262 client_1_config, std::move(client_1_deps), client_2_config,
5263 std::move(client_2_deps)));
5264 ConnectFakeSignaling();
5265
5266 // Set "offer to receive audio/video" without adding any tracks, so we just
5267 // set up ICE/DTLS with no media.
5268 PeerConnectionInterface::RTCOfferAnswerOptions options;
5269 options.offer_to_receive_audio = 1;
5270 options.offer_to_receive_video = 1;
5271 caller()->SetOfferAnswerOptions(options);
5272 caller()->CreateAndSetAndSignalOffer();
5273 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
5274
5275 EXPECT_GT(client_1_cert_verifier->call_count_, 0u);
5276 EXPECT_GT(client_2_cert_verifier->call_count_, 0u);
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07005277}
5278
5279TEST_P(PeerConnectionIntegrationTest,
5280 SSLCertificateVerifierFailureUsedForTurnConnectionsFailsConnection) {
5281 static const rtc::SocketAddress turn_server_internal_address{"88.88.88.0",
5282 3478};
5283 static const rtc::SocketAddress turn_server_external_address{"88.88.88.1", 0};
5284
5285 // Enable TCP-TLS for the fake turn server. We need to pass in 88.88.88.0 so
5286 // that host name verification passes on the fake certificate.
Seth Hampsonaed71642018-06-11 07:41:32 -07005287 CreateTurnServer(turn_server_internal_address, turn_server_external_address,
5288 cricket::PROTO_TLS, "88.88.88.0");
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07005289
5290 webrtc::PeerConnectionInterface::IceServer ice_server;
5291 ice_server.urls.push_back("turns:88.88.88.0:3478?transport=tcp");
5292 ice_server.username = "test";
5293 ice_server.password = "test";
5294
5295 PeerConnectionInterface::RTCConfiguration client_1_config;
5296 client_1_config.servers.push_back(ice_server);
5297 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
5298
5299 PeerConnectionInterface::RTCConfiguration client_2_config;
5300 client_2_config.servers.push_back(ice_server);
5301 // Setting the type to kRelay forces the connection to go through a TURN
5302 // server.
5303 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
5304
5305 // Get a copy to the pointer so we can verify calls later.
5306 rtc::TestCertificateVerifier* client_1_cert_verifier =
5307 new rtc::TestCertificateVerifier();
5308 client_1_cert_verifier->verify_certificate_ = false;
5309 rtc::TestCertificateVerifier* client_2_cert_verifier =
5310 new rtc::TestCertificateVerifier();
5311 client_2_cert_verifier->verify_certificate_ = false;
5312
5313 // Create the dependencies with the test certificate verifier.
5314 webrtc::PeerConnectionDependencies client_1_deps(nullptr);
5315 client_1_deps.tls_cert_verifier =
5316 std::unique_ptr<rtc::TestCertificateVerifier>(client_1_cert_verifier);
5317 webrtc::PeerConnectionDependencies client_2_deps(nullptr);
5318 client_2_deps.tls_cert_verifier =
5319 std::unique_ptr<rtc::TestCertificateVerifier>(client_2_cert_verifier);
5320
5321 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndDeps(
5322 client_1_config, std::move(client_1_deps), client_2_config,
5323 std::move(client_2_deps)));
5324 ConnectFakeSignaling();
5325
5326 // Set "offer to receive audio/video" without adding any tracks, so we just
5327 // set up ICE/DTLS with no media.
5328 PeerConnectionInterface::RTCOfferAnswerOptions options;
5329 options.offer_to_receive_audio = 1;
5330 options.offer_to_receive_video = 1;
5331 caller()->SetOfferAnswerOptions(options);
5332 caller()->CreateAndSetAndSignalOffer();
5333 bool wait_res = true;
5334 // TODO(bugs.webrtc.org/9219): When IceConnectionState is implemented
5335 // properly, should be able to just wait for a state of "failed" instead of
5336 // waiting a fixed 10 seconds.
5337 WAIT_(DtlsConnected(), kDefaultTimeout, wait_res);
5338 ASSERT_FALSE(wait_res);
5339
5340 EXPECT_GT(client_1_cert_verifier->call_count_, 0u);
5341 EXPECT_GT(client_2_cert_verifier->call_count_, 0u);
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07005342}
5343
Qingsi Wang25ec8882019-11-15 12:33:05 -08005344// Test that the injected ICE transport factory is used to create ICE transports
5345// for WebRTC connections.
5346TEST_P(PeerConnectionIntegrationTest, IceTransportFactoryUsedForConnections) {
5347 PeerConnectionInterface::RTCConfiguration default_config;
5348 PeerConnectionDependencies dependencies(nullptr);
5349 auto ice_transport_factory = std::make_unique<MockIceTransportFactory>();
5350 EXPECT_CALL(*ice_transport_factory, RecordIceTransportCreated()).Times(1);
5351 dependencies.ice_transport_factory = std::move(ice_transport_factory);
Johannes Kron9bac68c2020-01-23 13:12:25 +00005352 auto wrapper = CreatePeerConnectionWrapper(
5353 "Caller", nullptr, &default_config, std::move(dependencies), nullptr,
5354 nullptr, /*reset_encoder_factory=*/false,
5355 /*reset_decoder_factory=*/false);
Qingsi Wang25ec8882019-11-15 12:33:05 -08005356 ASSERT_TRUE(wrapper);
5357 wrapper->CreateDataChannel();
5358 rtc::scoped_refptr<MockSetSessionDescriptionObserver> observer(
5359 new rtc::RefCountedObject<MockSetSessionDescriptionObserver>());
5360 wrapper->pc()->SetLocalDescription(observer,
5361 wrapper->CreateOfferAndWait().release());
5362}
5363
deadbeefc964d0b2017-04-03 10:03:35 -07005364// Test that audio and video flow end-to-end when codec names don't use the
5365// expected casing, given that they're supposed to be case insensitive. To test
5366// this, all but one codec is removed from each media description, and its
5367// casing is changed.
5368//
5369// In the past, this has regressed and caused crashes/black video, due to the
5370// fact that code at some layers was doing case-insensitive comparisons and
5371// code at other layers was not.
Seth Hampson2f0d7022018-02-20 11:54:42 -08005372TEST_P(PeerConnectionIntegrationTest, CodecNamesAreCaseInsensitive) {
deadbeefc964d0b2017-04-03 10:03:35 -07005373 ASSERT_TRUE(CreatePeerConnectionWrappers());
5374 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08005375 caller()->AddAudioVideoTracks();
5376 callee()->AddAudioVideoTracks();
deadbeefc964d0b2017-04-03 10:03:35 -07005377
5378 // Remove all but one audio/video codec (opus and VP8), and change the
5379 // casing of the caller's generated offer.
5380 caller()->SetGeneratedSdpMunger([](cricket::SessionDescription* description) {
5381 cricket::AudioContentDescription* audio =
5382 GetFirstAudioContentDescription(description);
5383 ASSERT_NE(nullptr, audio);
5384 auto audio_codecs = audio->codecs();
5385 audio_codecs.erase(std::remove_if(audio_codecs.begin(), audio_codecs.end(),
5386 [](const cricket::AudioCodec& codec) {
5387 return codec.name != "opus";
5388 }),
5389 audio_codecs.end());
5390 ASSERT_EQ(1u, audio_codecs.size());
5391 audio_codecs[0].name = "OpUs";
5392 audio->set_codecs(audio_codecs);
5393
5394 cricket::VideoContentDescription* video =
5395 GetFirstVideoContentDescription(description);
5396 ASSERT_NE(nullptr, video);
5397 auto video_codecs = video->codecs();
5398 video_codecs.erase(std::remove_if(video_codecs.begin(), video_codecs.end(),
5399 [](const cricket::VideoCodec& codec) {
5400 return codec.name != "VP8";
5401 }),
5402 video_codecs.end());
5403 ASSERT_EQ(1u, video_codecs.size());
5404 video_codecs[0].name = "vP8";
5405 video->set_codecs(video_codecs);
5406 });
5407
5408 caller()->CreateAndSetAndSignalOffer();
5409 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5410
5411 // Verify frames are still received end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08005412 MediaExpectations media_expectations;
5413 media_expectations.ExpectBidirectionalAudioAndVideo();
5414 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeefc964d0b2017-04-03 10:03:35 -07005415}
5416
Jonas Oreland49ac5952018-09-26 16:04:32 +02005417TEST_P(PeerConnectionIntegrationTest, GetSourcesAudio) {
hbos8d609f62017-04-10 07:39:05 -07005418 ASSERT_TRUE(CreatePeerConnectionWrappers());
5419 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08005420 caller()->AddAudioTrack();
hbos8d609f62017-04-10 07:39:05 -07005421 caller()->CreateAndSetAndSignalOffer();
5422 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
deadbeefd8ad7882017-04-18 16:01:17 -07005423 // Wait for one audio frame to be received by the callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08005424 MediaExpectations media_expectations;
5425 media_expectations.CalleeExpectsSomeAudio(1);
5426 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Jonas Oreland49ac5952018-09-26 16:04:32 +02005427 ASSERT_EQ(callee()->pc()->GetReceivers().size(), 1u);
hbos8d609f62017-04-10 07:39:05 -07005428 auto receiver = callee()->pc()->GetReceivers()[0];
5429 ASSERT_EQ(receiver->media_type(), cricket::MEDIA_TYPE_AUDIO);
Jonas Oreland49ac5952018-09-26 16:04:32 +02005430 auto sources = receiver->GetSources();
hbos8d609f62017-04-10 07:39:05 -07005431 ASSERT_GT(receiver->GetParameters().encodings.size(), 0u);
5432 EXPECT_EQ(receiver->GetParameters().encodings[0].ssrc,
Jonas Oreland49ac5952018-09-26 16:04:32 +02005433 sources[0].source_id());
5434 EXPECT_EQ(webrtc::RtpSourceType::SSRC, sources[0].source_type());
5435}
5436
5437TEST_P(PeerConnectionIntegrationTest, GetSourcesVideo) {
5438 ASSERT_TRUE(CreatePeerConnectionWrappers());
5439 ConnectFakeSignaling();
5440 caller()->AddVideoTrack();
5441 caller()->CreateAndSetAndSignalOffer();
5442 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5443 // Wait for one video frame to be received by the callee.
5444 MediaExpectations media_expectations;
5445 media_expectations.CalleeExpectsSomeVideo(1);
5446 ASSERT_TRUE(ExpectNewFrames(media_expectations));
5447 ASSERT_EQ(callee()->pc()->GetReceivers().size(), 1u);
5448 auto receiver = callee()->pc()->GetReceivers()[0];
5449 ASSERT_EQ(receiver->media_type(), cricket::MEDIA_TYPE_VIDEO);
5450 auto sources = receiver->GetSources();
5451 ASSERT_GT(receiver->GetParameters().encodings.size(), 0u);
Yves Gereyf781bb52019-07-23 19:15:39 +02005452 ASSERT_GT(sources.size(), 0u);
Jonas Oreland49ac5952018-09-26 16:04:32 +02005453 EXPECT_EQ(receiver->GetParameters().encodings[0].ssrc,
5454 sources[0].source_id());
5455 EXPECT_EQ(webrtc::RtpSourceType::SSRC, sources[0].source_type());
hbos8d609f62017-04-10 07:39:05 -07005456}
5457
deadbeef2f425aa2017-04-14 10:41:32 -07005458// Test that if a track is removed and added again with a different stream ID,
5459// the new stream ID is successfully communicated in SDP and media continues to
5460// flow end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08005461// TODO(webrtc.bugs.org/8734): This test does not work for Unified Plan because
5462// it will not reuse a transceiver that has already been sending. After creating
5463// a new transceiver it tries to create an offer with two senders of the same
5464// track ids and it fails.
5465TEST_F(PeerConnectionIntegrationTestPlanB, RemoveAndAddTrackWithNewStreamId) {
deadbeef2f425aa2017-04-14 10:41:32 -07005466 ASSERT_TRUE(CreatePeerConnectionWrappers());
5467 ConnectFakeSignaling();
5468
deadbeef2f425aa2017-04-14 10:41:32 -07005469 // Add track using stream 1, do offer/answer.
5470 rtc::scoped_refptr<webrtc::AudioTrackInterface> track =
5471 caller()->CreateLocalAudioTrack();
5472 rtc::scoped_refptr<webrtc::RtpSenderInterface> sender =
Steve Antond78323f2018-07-11 11:13:44 -07005473 caller()->AddTrack(track, {"stream_1"});
deadbeef2f425aa2017-04-14 10:41:32 -07005474 caller()->CreateAndSetAndSignalOffer();
5475 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08005476 {
5477 MediaExpectations media_expectations;
5478 media_expectations.CalleeExpectsSomeAudio(1);
5479 ASSERT_TRUE(ExpectNewFrames(media_expectations));
5480 }
deadbeef2f425aa2017-04-14 10:41:32 -07005481 // Remove the sender, and create a new one with the new stream.
5482 caller()->pc()->RemoveTrack(sender);
Steve Antond78323f2018-07-11 11:13:44 -07005483 sender = caller()->AddTrack(track, {"stream_2"});
deadbeef2f425aa2017-04-14 10:41:32 -07005484 caller()->CreateAndSetAndSignalOffer();
5485 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5486 // Wait for additional audio frames to be received by the callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08005487 {
5488 MediaExpectations media_expectations;
5489 media_expectations.CalleeExpectsSomeAudio();
5490 ASSERT_TRUE(ExpectNewFrames(media_expectations));
5491 }
deadbeef2f425aa2017-04-14 10:41:32 -07005492}
5493
Seth Hampson2f0d7022018-02-20 11:54:42 -08005494TEST_P(PeerConnectionIntegrationTest, RtcEventLogOutputWriteCalled) {
Elad Alon99c3fe52017-10-13 16:29:40 +02005495 ASSERT_TRUE(CreatePeerConnectionWrappers());
5496 ConnectFakeSignaling();
5497
Mirko Bonadei317a1f02019-09-17 17:06:18 +02005498 auto output = std::make_unique<testing::NiceMock<MockRtcEventLogOutput>>();
Mirko Bonadei6a489f22019-04-09 15:11:12 +02005499 ON_CALL(*output, IsActive()).WillByDefault(::testing::Return(true));
5500 ON_CALL(*output, Write(::testing::_)).WillByDefault(::testing::Return(true));
Elad Alon99c3fe52017-10-13 16:29:40 +02005501 EXPECT_CALL(*output, Write(::testing::_)).Times(::testing::AtLeast(1));
Bjorn Tereliusde939432017-11-20 17:38:14 +01005502 EXPECT_TRUE(caller()->pc()->StartRtcEventLog(
5503 std::move(output), webrtc::RtcEventLog::kImmediateOutput));
Elad Alon99c3fe52017-10-13 16:29:40 +02005504
Steve Anton15324772018-01-16 10:26:49 -08005505 caller()->AddAudioVideoTracks();
Elad Alon99c3fe52017-10-13 16:29:40 +02005506 caller()->CreateAndSetAndSignalOffer();
5507 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5508}
5509
Steve Antonede9ca52017-10-16 13:04:27 -07005510// Test that if candidates are only signaled by applying full session
5511// descriptions (instead of using AddIceCandidate), the peers can connect to
5512// each other and exchange media.
Seth Hampson2f0d7022018-02-20 11:54:42 -08005513TEST_P(PeerConnectionIntegrationTest, MediaFlowsWhenCandidatesSetOnlyInSdp) {
Steve Antonede9ca52017-10-16 13:04:27 -07005514 ASSERT_TRUE(CreatePeerConnectionWrappers());
5515 // Each side will signal the session descriptions but not candidates.
5516 ConnectFakeSignalingForSdpOnly();
5517
5518 // Add audio video track and exchange the initial offer/answer with media
5519 // information only. This will start ICE gathering on each side.
Steve Anton15324772018-01-16 10:26:49 -08005520 caller()->AddAudioVideoTracks();
5521 callee()->AddAudioVideoTracks();
Steve Antonede9ca52017-10-16 13:04:27 -07005522 caller()->CreateAndSetAndSignalOffer();
5523
5524 // Wait for all candidates to be gathered on both the caller and callee.
5525 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceGatheringComplete,
5526 caller()->ice_gathering_state(), kDefaultTimeout);
5527 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceGatheringComplete,
5528 callee()->ice_gathering_state(), kDefaultTimeout);
5529
5530 // The candidates will now be included in the session description, so
5531 // signaling them will start the ICE connection.
5532 caller()->CreateAndSetAndSignalOffer();
5533 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5534
5535 // Ensure that media flows in both directions.
Seth Hampson2f0d7022018-02-20 11:54:42 -08005536 MediaExpectations media_expectations;
5537 media_expectations.ExpectBidirectionalAudioAndVideo();
5538 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Antonede9ca52017-10-16 13:04:27 -07005539}
5540
henrika5f6bf242017-11-01 11:06:56 +01005541// Test that SetAudioPlayout can be used to disable audio playout from the
5542// start, then later enable it. This may be useful, for example, if the caller
5543// needs to play a local ringtone until some event occurs, after which it
5544// switches to playing the received audio.
Seth Hampson2f0d7022018-02-20 11:54:42 -08005545TEST_P(PeerConnectionIntegrationTest, DisableAndEnableAudioPlayout) {
henrika5f6bf242017-11-01 11:06:56 +01005546 ASSERT_TRUE(CreatePeerConnectionWrappers());
5547 ConnectFakeSignaling();
5548
5549 // Set up audio-only call where audio playout is disabled on caller's side.
5550 caller()->pc()->SetAudioPlayout(false);
Steve Anton15324772018-01-16 10:26:49 -08005551 caller()->AddAudioTrack();
5552 callee()->AddAudioTrack();
henrika5f6bf242017-11-01 11:06:56 +01005553 caller()->CreateAndSetAndSignalOffer();
5554 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5555
5556 // Pump messages for a second.
5557 WAIT(false, 1000);
5558 // Since audio playout is disabled, the caller shouldn't have received
5559 // anything (at the playout level, at least).
5560 EXPECT_EQ(0, caller()->audio_frames_received());
5561 // As a sanity check, make sure the callee (for which playout isn't disabled)
5562 // did still see frames on its audio level.
5563 ASSERT_GT(callee()->audio_frames_received(), 0);
5564
5565 // Enable playout again, and ensure audio starts flowing.
5566 caller()->pc()->SetAudioPlayout(true);
Seth Hampson2f0d7022018-02-20 11:54:42 -08005567 MediaExpectations media_expectations;
5568 media_expectations.ExpectBidirectionalAudio();
5569 ASSERT_TRUE(ExpectNewFrames(media_expectations));
henrika5f6bf242017-11-01 11:06:56 +01005570}
5571
5572double GetAudioEnergyStat(PeerConnectionWrapper* pc) {
5573 auto report = pc->NewGetStats();
5574 auto track_stats_list =
5575 report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
5576 const webrtc::RTCMediaStreamTrackStats* remote_track_stats = nullptr;
5577 for (const auto* track_stats : track_stats_list) {
5578 if (track_stats->remote_source.is_defined() &&
5579 *track_stats->remote_source) {
5580 remote_track_stats = track_stats;
5581 break;
5582 }
5583 }
5584
5585 if (!remote_track_stats->total_audio_energy.is_defined()) {
5586 return 0.0;
5587 }
5588 return *remote_track_stats->total_audio_energy;
5589}
5590
5591// Test that if audio playout is disabled via the SetAudioPlayout() method, then
5592// incoming audio is still processed and statistics are generated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08005593TEST_P(PeerConnectionIntegrationTest,
henrika5f6bf242017-11-01 11:06:56 +01005594 DisableAudioPlayoutStillGeneratesAudioStats) {
5595 ASSERT_TRUE(CreatePeerConnectionWrappers());
5596 ConnectFakeSignaling();
5597
5598 // Set up audio-only call where playout is disabled but audio-processing is
5599 // still active.
Steve Anton15324772018-01-16 10:26:49 -08005600 caller()->AddAudioTrack();
5601 callee()->AddAudioTrack();
henrika5f6bf242017-11-01 11:06:56 +01005602 caller()->pc()->SetAudioPlayout(false);
5603
5604 caller()->CreateAndSetAndSignalOffer();
5605 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5606
5607 // Wait for the callee to receive audio stats.
5608 EXPECT_TRUE_WAIT(GetAudioEnergyStat(caller()) > 0, kMaxWaitForFramesMs);
5609}
5610
henrika4f167df2017-11-01 14:45:55 +01005611// Test that SetAudioRecording can be used to disable audio recording from the
5612// start, then later enable it. This may be useful, for example, if the caller
5613// wants to ensure that no audio resources are active before a certain state
5614// is reached.
Seth Hampson2f0d7022018-02-20 11:54:42 -08005615TEST_P(PeerConnectionIntegrationTest, DisableAndEnableAudioRecording) {
henrika4f167df2017-11-01 14:45:55 +01005616 ASSERT_TRUE(CreatePeerConnectionWrappers());
5617 ConnectFakeSignaling();
5618
5619 // Set up audio-only call where audio recording is disabled on caller's side.
5620 caller()->pc()->SetAudioRecording(false);
Steve Anton15324772018-01-16 10:26:49 -08005621 caller()->AddAudioTrack();
5622 callee()->AddAudioTrack();
henrika4f167df2017-11-01 14:45:55 +01005623 caller()->CreateAndSetAndSignalOffer();
5624 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5625
5626 // Pump messages for a second.
5627 WAIT(false, 1000);
5628 // Since caller has disabled audio recording, the callee shouldn't have
5629 // received anything.
5630 EXPECT_EQ(0, callee()->audio_frames_received());
5631 // As a sanity check, make sure the caller did still see frames on its
5632 // audio level since audio recording is enabled on the calle side.
5633 ASSERT_GT(caller()->audio_frames_received(), 0);
5634
5635 // Enable audio recording again, and ensure audio starts flowing.
5636 caller()->pc()->SetAudioRecording(true);
Seth Hampson2f0d7022018-02-20 11:54:42 -08005637 MediaExpectations media_expectations;
5638 media_expectations.ExpectBidirectionalAudio();
5639 ASSERT_TRUE(ExpectNewFrames(media_expectations));
henrika4f167df2017-11-01 14:45:55 +01005640}
5641
Taylor Brandstetter389a97c2018-01-03 16:26:06 -08005642// Test that after closing PeerConnections, they stop sending any packets (ICE,
5643// DTLS, RTP...).
Seth Hampson2f0d7022018-02-20 11:54:42 -08005644TEST_P(PeerConnectionIntegrationTest, ClosingConnectionStopsPacketFlow) {
Taylor Brandstetter389a97c2018-01-03 16:26:06 -08005645 // Set up audio/video/data, wait for some frames to be received.
5646 ASSERT_TRUE(CreatePeerConnectionWrappers());
5647 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08005648 caller()->AddAudioVideoTracks();
Taylor Brandstetter389a97c2018-01-03 16:26:06 -08005649#ifdef HAVE_SCTP
5650 caller()->CreateDataChannel();
5651#endif
5652 caller()->CreateAndSetAndSignalOffer();
5653 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08005654 MediaExpectations media_expectations;
5655 media_expectations.CalleeExpectsSomeAudioAndVideo();
5656 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Taylor Brandstetter389a97c2018-01-03 16:26:06 -08005657 // Close PeerConnections.
Steve Antond91969e2019-05-30 12:27:03 -07005658 ClosePeerConnections();
Taylor Brandstetter389a97c2018-01-03 16:26:06 -08005659 // Pump messages for a second, and ensure no new packets end up sent.
5660 uint32_t sent_packets_a = virtual_socket_server()->sent_packets();
5661 WAIT(false, 1000);
5662 uint32_t sent_packets_b = virtual_socket_server()->sent_packets();
5663 EXPECT_EQ(sent_packets_a, sent_packets_b);
5664}
5665
Steve Anton7eca0932018-03-30 15:18:41 -07005666// Test that transport stats are generated by the RTCStatsCollector for a
5667// connection that only involves data channels. This is a regression test for
5668// crbug.com/826972.
5669#ifdef HAVE_SCTP
5670TEST_P(PeerConnectionIntegrationTest,
5671 TransportStatsReportedForDataChannelOnlyConnection) {
5672 ASSERT_TRUE(CreatePeerConnectionWrappers());
5673 ConnectFakeSignaling();
5674 caller()->CreateDataChannel();
5675
5676 caller()->CreateAndSetAndSignalOffer();
5677 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5678 ASSERT_TRUE_WAIT(callee()->data_channel(), kDefaultTimeout);
5679
5680 auto caller_report = caller()->NewGetStats();
5681 EXPECT_EQ(1u, caller_report->GetStatsOfType<RTCTransportStats>().size());
5682 auto callee_report = callee()->NewGetStats();
5683 EXPECT_EQ(1u, callee_report->GetStatsOfType<RTCTransportStats>().size());
5684}
5685#endif // HAVE_SCTP
5686
Qingsi Wang7685e862018-06-11 20:15:46 -07005687TEST_P(PeerConnectionIntegrationTest,
5688 IceEventsGeneratedAndLoggedInRtcEventLog) {
5689 ASSERT_TRUE(CreatePeerConnectionWrappersWithFakeRtcEventLog());
5690 ConnectFakeSignaling();
5691 PeerConnectionInterface::RTCOfferAnswerOptions options;
5692 options.offer_to_receive_audio = 1;
5693 caller()->SetOfferAnswerOptions(options);
5694 caller()->CreateAndSetAndSignalOffer();
5695 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
5696 ASSERT_NE(nullptr, caller()->event_log_factory());
5697 ASSERT_NE(nullptr, callee()->event_log_factory());
5698 webrtc::FakeRtcEventLog* caller_event_log =
5699 static_cast<webrtc::FakeRtcEventLog*>(
5700 caller()->event_log_factory()->last_log_created());
5701 webrtc::FakeRtcEventLog* callee_event_log =
5702 static_cast<webrtc::FakeRtcEventLog*>(
5703 callee()->event_log_factory()->last_log_created());
5704 ASSERT_NE(nullptr, caller_event_log);
5705 ASSERT_NE(nullptr, callee_event_log);
5706 int caller_ice_config_count = caller_event_log->GetEventCount(
5707 webrtc::RtcEvent::Type::IceCandidatePairConfig);
5708 int caller_ice_event_count = caller_event_log->GetEventCount(
5709 webrtc::RtcEvent::Type::IceCandidatePairEvent);
5710 int callee_ice_config_count = callee_event_log->GetEventCount(
5711 webrtc::RtcEvent::Type::IceCandidatePairConfig);
5712 int callee_ice_event_count = callee_event_log->GetEventCount(
5713 webrtc::RtcEvent::Type::IceCandidatePairEvent);
5714 EXPECT_LT(0, caller_ice_config_count);
5715 EXPECT_LT(0, caller_ice_event_count);
5716 EXPECT_LT(0, callee_ice_config_count);
5717 EXPECT_LT(0, callee_ice_event_count);
5718}
5719
Qingsi Wangc129c352019-04-18 10:41:58 -07005720TEST_P(PeerConnectionIntegrationTest, RegatherAfterChangingIceTransportType) {
Qingsi Wangc129c352019-04-18 10:41:58 -07005721 static const rtc::SocketAddress turn_server_internal_address{"88.88.88.0",
5722 3478};
5723 static const rtc::SocketAddress turn_server_external_address{"88.88.88.1", 0};
5724
5725 CreateTurnServer(turn_server_internal_address, turn_server_external_address);
5726
5727 webrtc::PeerConnectionInterface::IceServer ice_server;
5728 ice_server.urls.push_back("turn:88.88.88.0:3478");
5729 ice_server.username = "test";
5730 ice_server.password = "test";
5731
5732 PeerConnectionInterface::RTCConfiguration caller_config;
5733 caller_config.servers.push_back(ice_server);
5734 caller_config.type = webrtc::PeerConnectionInterface::kRelay;
5735 caller_config.continual_gathering_policy = PeerConnection::GATHER_CONTINUALLY;
Qingsi Wang1fe119f2019-05-31 16:55:33 -07005736 caller_config.surface_ice_candidates_on_ice_transport_type_changed = true;
Qingsi Wangc129c352019-04-18 10:41:58 -07005737
5738 PeerConnectionInterface::RTCConfiguration callee_config;
5739 callee_config.servers.push_back(ice_server);
5740 callee_config.type = webrtc::PeerConnectionInterface::kRelay;
5741 callee_config.continual_gathering_policy = PeerConnection::GATHER_CONTINUALLY;
Qingsi Wang1fe119f2019-05-31 16:55:33 -07005742 callee_config.surface_ice_candidates_on_ice_transport_type_changed = true;
Qingsi Wangc129c352019-04-18 10:41:58 -07005743
5744 ASSERT_TRUE(
5745 CreatePeerConnectionWrappersWithConfig(caller_config, callee_config));
5746
5747 // Do normal offer/answer and wait for ICE to complete.
5748 ConnectFakeSignaling();
5749 caller()->AddAudioVideoTracks();
5750 callee()->AddAudioVideoTracks();
5751 caller()->CreateAndSetAndSignalOffer();
5752 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5753 // Since we are doing continual gathering, the ICE transport does not reach
5754 // kIceGatheringComplete (see
5755 // P2PTransportChannel::OnCandidatesAllocationDone), and consequently not
5756 // kIceConnectionComplete.
5757 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
5758 caller()->ice_connection_state(), kDefaultTimeout);
5759 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
5760 callee()->ice_connection_state(), kDefaultTimeout);
5761 // Note that we cannot use the metric
5762 // |WebRTC.PeerConnection.CandidatePairType_UDP| in this test since this
5763 // metric is only populated when we reach kIceConnectionComplete in the
5764 // current implementation.
5765 EXPECT_EQ(cricket::RELAY_PORT_TYPE,
5766 caller()->last_candidate_gathered().type());
5767 EXPECT_EQ(cricket::RELAY_PORT_TYPE,
5768 callee()->last_candidate_gathered().type());
5769
5770 // Loosen the caller's candidate filter.
5771 caller_config = caller()->pc()->GetConfiguration();
5772 caller_config.type = webrtc::PeerConnectionInterface::kAll;
5773 caller()->pc()->SetConfiguration(caller_config);
5774 // We should have gathered a new host candidate.
5775 EXPECT_EQ_WAIT(cricket::LOCAL_PORT_TYPE,
5776 caller()->last_candidate_gathered().type(), kDefaultTimeout);
5777
5778 // Loosen the callee's candidate filter.
5779 callee_config = callee()->pc()->GetConfiguration();
5780 callee_config.type = webrtc::PeerConnectionInterface::kAll;
5781 callee()->pc()->SetConfiguration(callee_config);
5782 EXPECT_EQ_WAIT(cricket::LOCAL_PORT_TYPE,
5783 callee()->last_candidate_gathered().type(), kDefaultTimeout);
5784}
5785
Eldar Relloda13ea22019-06-01 12:23:43 +03005786TEST_P(PeerConnectionIntegrationTest, OnIceCandidateError) {
Eldar Relloda13ea22019-06-01 12:23:43 +03005787 static const rtc::SocketAddress turn_server_internal_address{"88.88.88.0",
5788 3478};
5789 static const rtc::SocketAddress turn_server_external_address{"88.88.88.1", 0};
5790
5791 CreateTurnServer(turn_server_internal_address, turn_server_external_address);
5792
5793 webrtc::PeerConnectionInterface::IceServer ice_server;
5794 ice_server.urls.push_back("turn:88.88.88.0:3478");
5795 ice_server.username = "test";
5796 ice_server.password = "123";
5797
5798 PeerConnectionInterface::RTCConfiguration caller_config;
5799 caller_config.servers.push_back(ice_server);
5800 caller_config.type = webrtc::PeerConnectionInterface::kRelay;
5801 caller_config.continual_gathering_policy = PeerConnection::GATHER_CONTINUALLY;
5802
5803 PeerConnectionInterface::RTCConfiguration callee_config;
5804 callee_config.servers.push_back(ice_server);
5805 callee_config.type = webrtc::PeerConnectionInterface::kRelay;
5806 callee_config.continual_gathering_policy = PeerConnection::GATHER_CONTINUALLY;
5807
5808 ASSERT_TRUE(
5809 CreatePeerConnectionWrappersWithConfig(caller_config, callee_config));
5810
5811 // Do normal offer/answer and wait for ICE to complete.
5812 ConnectFakeSignaling();
5813 caller()->AddAudioVideoTracks();
5814 callee()->AddAudioVideoTracks();
5815 caller()->CreateAndSetAndSignalOffer();
5816 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5817 EXPECT_EQ_WAIT(401, caller()->error_event().error_code, kDefaultTimeout);
5818 EXPECT_EQ("Unauthorized", caller()->error_event().error_text);
5819 EXPECT_EQ("turn:88.88.88.0:3478?transport=udp", caller()->error_event().url);
Eldar Rello0095d372019-12-02 22:22:07 +02005820 EXPECT_NE(caller()->error_event().address, "");
Eldar Relloda13ea22019-06-01 12:23:43 +03005821}
5822
Eldar Rello5ab79e62019-10-09 18:29:44 +03005823TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
5824 AudioKeepsFlowingAfterImplicitRollback) {
5825 PeerConnectionInterface::RTCConfiguration config;
5826 config.sdp_semantics = SdpSemantics::kUnifiedPlan;
5827 config.enable_implicit_rollback = true;
5828 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
5829 ConnectFakeSignaling();
5830 caller()->AddAudioTrack();
5831 callee()->AddAudioTrack();
5832 caller()->CreateAndSetAndSignalOffer();
5833 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5834 MediaExpectations media_expectations;
5835 media_expectations.ExpectBidirectionalAudio();
5836 ASSERT_TRUE(ExpectNewFrames(media_expectations));
5837 SetSignalIceCandidates(false); // Workaround candidate outrace sdp.
5838 caller()->AddVideoTrack();
5839 callee()->AddVideoTrack();
5840 rtc::scoped_refptr<MockSetSessionDescriptionObserver> observer(
5841 new rtc::RefCountedObject<MockSetSessionDescriptionObserver>());
5842 callee()->pc()->SetLocalDescription(observer,
5843 callee()->CreateOfferAndWait().release());
5844 EXPECT_TRUE_WAIT(observer->called(), kDefaultTimeout);
5845 caller()->CreateAndSetAndSignalOffer(); // Implicit rollback.
5846 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5847 ASSERT_TRUE(ExpectNewFrames(media_expectations));
5848}
5849
5850TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
5851 ImplicitRollbackVisitsStableState) {
5852 RTCConfiguration config;
5853 config.sdp_semantics = SdpSemantics::kUnifiedPlan;
5854 config.enable_implicit_rollback = true;
5855
5856 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
5857
5858 rtc::scoped_refptr<MockSetSessionDescriptionObserver> sld_observer(
5859 new rtc::RefCountedObject<MockSetSessionDescriptionObserver>());
5860 callee()->pc()->SetLocalDescription(sld_observer,
5861 callee()->CreateOfferAndWait().release());
5862 EXPECT_TRUE_WAIT(sld_observer->called(), kDefaultTimeout);
5863 EXPECT_EQ(sld_observer->error(), "");
5864
5865 rtc::scoped_refptr<MockSetSessionDescriptionObserver> srd_observer(
5866 new rtc::RefCountedObject<MockSetSessionDescriptionObserver>());
5867 callee()->pc()->SetRemoteDescription(
5868 srd_observer, caller()->CreateOfferAndWait().release());
5869 EXPECT_TRUE_WAIT(srd_observer->called(), kDefaultTimeout);
5870 EXPECT_EQ(srd_observer->error(), "");
5871
5872 EXPECT_THAT(callee()->peer_connection_signaling_state_history(),
5873 ElementsAre(PeerConnectionInterface::kHaveLocalOffer,
5874 PeerConnectionInterface::kStable,
5875 PeerConnectionInterface::kHaveRemoteOffer));
5876}
5877
Mirko Bonadeic84f6612019-01-31 12:20:57 +01005878INSTANTIATE_TEST_SUITE_P(PeerConnectionIntegrationTest,
5879 PeerConnectionIntegrationTest,
5880 Values(SdpSemantics::kPlanB,
5881 SdpSemantics::kUnifiedPlan));
Steve Antond3679212018-01-17 17:41:02 -08005882
Yves Gerey100fe632020-01-17 19:15:53 +01005883INSTANTIATE_TEST_SUITE_P(PeerConnectionIntegrationTest,
5884 PeerConnectionIntegrationTestWithFakeClock,
5885 Values(SdpSemantics::kPlanB,
5886 SdpSemantics::kUnifiedPlan));
5887
Steve Anton74255ff2018-01-24 18:32:57 -08005888// Tests that verify interoperability between Plan B and Unified Plan
5889// PeerConnections.
5890class PeerConnectionIntegrationInteropTest
Seth Hampson2f0d7022018-02-20 11:54:42 -08005891 : public PeerConnectionIntegrationBaseTest,
Steve Anton74255ff2018-01-24 18:32:57 -08005892 public ::testing::WithParamInterface<
5893 std::tuple<SdpSemantics, SdpSemantics>> {
5894 protected:
Seth Hampson2f0d7022018-02-20 11:54:42 -08005895 // Setting the SdpSemantics for the base test to kDefault does not matter
5896 // because we specify not to use the test semantics when creating
5897 // PeerConnectionWrappers.
Steve Anton74255ff2018-01-24 18:32:57 -08005898 PeerConnectionIntegrationInteropTest()
Steve Anton3acffc32018-04-12 17:21:03 -07005899 : PeerConnectionIntegrationBaseTest(SdpSemantics::kPlanB),
Seth Hampson2f0d7022018-02-20 11:54:42 -08005900 caller_semantics_(std::get<0>(GetParam())),
Steve Anton74255ff2018-01-24 18:32:57 -08005901 callee_semantics_(std::get<1>(GetParam())) {}
5902
5903 bool CreatePeerConnectionWrappersWithSemantics() {
Steve Anton3acffc32018-04-12 17:21:03 -07005904 return CreatePeerConnectionWrappersWithSdpSemantics(caller_semantics_,
5905 callee_semantics_);
Steve Anton74255ff2018-01-24 18:32:57 -08005906 }
5907
5908 const SdpSemantics caller_semantics_;
5909 const SdpSemantics callee_semantics_;
5910};
5911
5912TEST_P(PeerConnectionIntegrationInteropTest, NoMediaLocalToNoMediaRemote) {
5913 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
5914 ConnectFakeSignaling();
5915
5916 caller()->CreateAndSetAndSignalOffer();
5917 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5918}
5919
5920TEST_P(PeerConnectionIntegrationInteropTest, OneAudioLocalToNoMediaRemote) {
5921 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
5922 ConnectFakeSignaling();
5923 auto audio_sender = caller()->AddAudioTrack();
5924
5925 caller()->CreateAndSetAndSignalOffer();
5926 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5927
5928 // Verify that one audio receiver has been created on the remote and that it
5929 // has the same track ID as the sending track.
5930 auto receivers = callee()->pc()->GetReceivers();
5931 ASSERT_EQ(1u, receivers.size());
5932 EXPECT_EQ(cricket::MEDIA_TYPE_AUDIO, receivers[0]->media_type());
5933 EXPECT_EQ(receivers[0]->track()->id(), audio_sender->track()->id());
5934
Seth Hampson2f0d7022018-02-20 11:54:42 -08005935 MediaExpectations media_expectations;
5936 media_expectations.CalleeExpectsSomeAudio();
5937 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08005938}
5939
5940TEST_P(PeerConnectionIntegrationInteropTest, OneAudioOneVideoToNoMediaRemote) {
5941 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
5942 ConnectFakeSignaling();
5943 auto video_sender = caller()->AddVideoTrack();
5944 auto audio_sender = caller()->AddAudioTrack();
5945
5946 caller()->CreateAndSetAndSignalOffer();
5947 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5948
5949 // Verify that one audio and one video receiver have been created on the
5950 // remote and that they have the same track IDs as the sending tracks.
5951 auto audio_receivers =
5952 callee()->GetReceiversOfType(cricket::MEDIA_TYPE_AUDIO);
5953 ASSERT_EQ(1u, audio_receivers.size());
5954 EXPECT_EQ(audio_receivers[0]->track()->id(), audio_sender->track()->id());
5955 auto video_receivers =
5956 callee()->GetReceiversOfType(cricket::MEDIA_TYPE_VIDEO);
5957 ASSERT_EQ(1u, video_receivers.size());
5958 EXPECT_EQ(video_receivers[0]->track()->id(), video_sender->track()->id());
5959
Seth Hampson2f0d7022018-02-20 11:54:42 -08005960 MediaExpectations media_expectations;
5961 media_expectations.CalleeExpectsSomeAudioAndVideo();
5962 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08005963}
5964
5965TEST_P(PeerConnectionIntegrationInteropTest,
5966 OneAudioOneVideoLocalToOneAudioOneVideoRemote) {
5967 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
5968 ConnectFakeSignaling();
5969 caller()->AddAudioVideoTracks();
5970 callee()->AddAudioVideoTracks();
5971
5972 caller()->CreateAndSetAndSignalOffer();
5973 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5974
Seth Hampson2f0d7022018-02-20 11:54:42 -08005975 MediaExpectations media_expectations;
5976 media_expectations.ExpectBidirectionalAudioAndVideo();
5977 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08005978}
5979
5980TEST_P(PeerConnectionIntegrationInteropTest,
5981 ReverseRolesOneAudioLocalToOneVideoRemote) {
5982 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
5983 ConnectFakeSignaling();
5984 caller()->AddAudioTrack();
5985 callee()->AddVideoTrack();
5986
5987 caller()->CreateAndSetAndSignalOffer();
5988 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5989
5990 // Verify that only the audio track has been negotiated.
5991 EXPECT_EQ(0u, caller()->GetReceiversOfType(cricket::MEDIA_TYPE_VIDEO).size());
5992 // Might also check that the callee's NegotiationNeeded flag is set.
5993
5994 // Reverse roles.
5995 callee()->CreateAndSetAndSignalOffer();
5996 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5997
Seth Hampson2f0d7022018-02-20 11:54:42 -08005998 MediaExpectations media_expectations;
5999 media_expectations.CallerExpectsSomeVideo();
6000 media_expectations.CalleeExpectsSomeAudio();
6001 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08006002}
6003
Mirko Bonadeic84f6612019-01-31 12:20:57 +01006004INSTANTIATE_TEST_SUITE_P(
Steve Antonba42e992018-04-09 14:10:01 -07006005 PeerConnectionIntegrationTest,
6006 PeerConnectionIntegrationInteropTest,
6007 Values(std::make_tuple(SdpSemantics::kPlanB, SdpSemantics::kUnifiedPlan),
6008 std::make_tuple(SdpSemantics::kUnifiedPlan, SdpSemantics::kPlanB)));
6009
6010// Test that if the Unified Plan side offers two video tracks then the Plan B
6011// side will only see the first one and ignore the second.
6012TEST_F(PeerConnectionIntegrationTestPlanB, TwoVideoUnifiedPlanToNoMediaPlanB) {
Steve Anton3acffc32018-04-12 17:21:03 -07006013 ASSERT_TRUE(CreatePeerConnectionWrappersWithSdpSemantics(
6014 SdpSemantics::kUnifiedPlan, SdpSemantics::kPlanB));
Steve Anton74255ff2018-01-24 18:32:57 -08006015 ConnectFakeSignaling();
6016 auto first_sender = caller()->AddVideoTrack();
6017 caller()->AddVideoTrack();
6018
6019 caller()->CreateAndSetAndSignalOffer();
6020 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
6021
6022 // Verify that there is only one receiver and it corresponds to the first
6023 // added track.
6024 auto receivers = callee()->pc()->GetReceivers();
6025 ASSERT_EQ(1u, receivers.size());
6026 EXPECT_TRUE(receivers[0]->track()->enabled());
6027 EXPECT_EQ(first_sender->track()->id(), receivers[0]->track()->id());
6028
Seth Hampson2f0d7022018-02-20 11:54:42 -08006029 MediaExpectations media_expectations;
6030 media_expectations.CalleeExpectsSomeVideo();
6031 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08006032}
6033
Steve Anton2bed3972019-01-04 17:04:30 -08006034// Test that if the initial offer tagged BUNDLE section is rejected due to its
6035// associated RtpTransceiver being stopped and another transceiver is added,
6036// then renegotiation causes the callee to receive the new video track without
6037// error.
6038// This is a regression test for bugs.webrtc.org/9954
6039TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
6040 ReOfferWithStoppedBundleTaggedTransceiver) {
6041 RTCConfiguration config;
6042 config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
6043 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
6044 ConnectFakeSignaling();
6045 auto audio_transceiver_or_error =
6046 caller()->pc()->AddTransceiver(caller()->CreateLocalAudioTrack());
6047 ASSERT_TRUE(audio_transceiver_or_error.ok());
6048 auto audio_transceiver = audio_transceiver_or_error.MoveValue();
6049
6050 caller()->CreateAndSetAndSignalOffer();
6051 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
6052 {
6053 MediaExpectations media_expectations;
6054 media_expectations.CalleeExpectsSomeAudio();
6055 ASSERT_TRUE(ExpectNewFrames(media_expectations));
6056 }
6057
6058 audio_transceiver->Stop();
6059 caller()->pc()->AddTransceiver(caller()->CreateLocalVideoTrack());
6060
6061 caller()->CreateAndSetAndSignalOffer();
6062 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
6063 {
6064 MediaExpectations media_expectations;
6065 media_expectations.CalleeExpectsSomeVideo();
6066 ASSERT_TRUE(ExpectNewFrames(media_expectations));
6067 }
6068}
6069
Harald Alvestrandd61f2a72019-05-08 20:20:59 +02006070#ifdef HAVE_SCTP
6071
6072TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
6073 EndToEndCallWithBundledSctpDataChannel) {
6074 ASSERT_TRUE(CreatePeerConnectionWrappers());
6075 ConnectFakeSignaling();
6076 caller()->CreateDataChannel();
6077 caller()->AddAudioVideoTracks();
6078 callee()->AddAudioVideoTracks();
Harald Alvestrandd61f2a72019-05-08 20:20:59 +02006079 caller()->CreateAndSetAndSignalOffer();
6080 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Harald Alvestrand17ea0682019-12-13 11:51:04 +01006081 ASSERT_EQ_WAIT(SctpTransportState::kConnected,
6082 caller()->pc()->GetSctpTransport()->Information().state(),
6083 kDefaultTimeout);
6084 ASSERT_TRUE_WAIT(callee()->data_channel(), kDefaultTimeout);
6085 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
6086}
6087
6088TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
6089 EndToEndCallWithDataChannelOnlyConnects) {
6090 ASSERT_TRUE(CreatePeerConnectionWrappers());
6091 ConnectFakeSignaling();
6092 caller()->CreateDataChannel();
6093 caller()->CreateAndSetAndSignalOffer();
6094 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
6095 ASSERT_TRUE_WAIT(callee()->data_channel(), kDefaultTimeout);
6096 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
6097 ASSERT_TRUE(caller()->data_observer()->IsOpen());
Harald Alvestrandd61f2a72019-05-08 20:20:59 +02006098}
6099
Harald Alvestrand2697ac12019-12-16 10:37:04 +01006100TEST_F(PeerConnectionIntegrationTestUnifiedPlan, DataChannelClosesWhenClosed) {
6101 ASSERT_TRUE(CreatePeerConnectionWrappers());
6102 ConnectFakeSignaling();
6103 caller()->CreateDataChannel();
6104 caller()->CreateAndSetAndSignalOffer();
6105 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
6106 ASSERT_TRUE_WAIT(callee()->data_observer(), kDefaultTimeout);
6107 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
6108 caller()->data_channel()->Close();
6109 ASSERT_TRUE_WAIT(!callee()->data_observer()->IsOpen(), kDefaultTimeout);
6110}
6111
6112TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
6113 DataChannelClosesWhenClosedReverse) {
6114 ASSERT_TRUE(CreatePeerConnectionWrappers());
6115 ConnectFakeSignaling();
6116 caller()->CreateDataChannel();
6117 caller()->CreateAndSetAndSignalOffer();
6118 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
6119 ASSERT_TRUE_WAIT(callee()->data_observer(), kDefaultTimeout);
6120 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
6121 callee()->data_channel()->Close();
6122 ASSERT_TRUE_WAIT(!caller()->data_observer()->IsOpen(), kDefaultTimeout);
6123}
6124
6125TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
6126 DataChannelClosesWhenPeerConnectionClosed) {
6127 ASSERT_TRUE(CreatePeerConnectionWrappers());
6128 ConnectFakeSignaling();
6129 caller()->CreateDataChannel();
6130 caller()->CreateAndSetAndSignalOffer();
6131 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
6132 ASSERT_TRUE_WAIT(callee()->data_observer(), kDefaultTimeout);
6133 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
6134 caller()->pc()->Close();
6135 ASSERT_TRUE_WAIT(!callee()->data_observer()->IsOpen(), kDefaultTimeout);
6136}
6137
Harald Alvestrandd61f2a72019-05-08 20:20:59 +02006138#endif // HAVE_SCTP
6139
deadbeef1dcb1642017-03-29 21:08:16 -07006140} // namespace
Mirko Bonadeiab64e8a2018-12-12 12:10:18 +01006141} // namespace webrtc
deadbeef1dcb1642017-03-29 21:08:16 -07006142
6143#endif // if !defined(THREAD_SANITIZER)