blob: a72dbc66d9d63b3e9377874fc7a1f1c964e2e060 [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
Taylor Brandstetterd3ef4992020-10-15 18:22:57 -070017#include <algorithm>
deadbeef1dcb1642017-03-29 21:08:16 -070018#include <functional>
19#include <list>
20#include <map>
21#include <memory>
22#include <utility>
23#include <vector>
24
Steve Anton64b626b2019-01-28 17:25:26 -080025#include "absl/algorithm/container.h"
Steve Anton10542f22019-01-11 09:11:00 -080026#include "api/media_stream_interface.h"
27#include "api/peer_connection_interface.h"
28#include "api/peer_connection_proxy.h"
Danil Chapovalov9da25bd2019-06-20 10:19:42 +020029#include "api/rtc_event_log/rtc_event_log_factory.h"
Steve Anton10542f22019-01-11 09:11:00 -080030#include "api/rtp_receiver_interface.h"
Danil Chapovalov9da25bd2019-06-20 10:19:42 +020031#include "api/task_queue/default_task_queue_factory.h"
Erik Språngceb44952020-09-22 11:36:35 +020032#include "api/transport/field_trial_based_config.h"
Steve Anton10542f22019-01-11 09:11:00 -080033#include "api/uma_metrics.h"
Anders Carlsson67537952018-05-03 11:28:29 +020034#include "api/video_codecs/sdp_video_format.h"
Qingsi Wang7685e862018-06-11 20:15:46 -070035#include "call/call.h"
36#include "logging/rtc_event_log/fake_rtc_event_log_factory.h"
Steve Anton10542f22019-01-11 09:11:00 -080037#include "media/engine/fake_webrtc_video_engine.h"
38#include "media/engine/webrtc_media_engine.h"
Danil Chapovalov9da25bd2019-06-20 10:19:42 +020039#include "media/engine/webrtc_media_engine_defaults.h"
Per Åhgrencc73ed32020-04-26 23:56:17 +020040#include "modules/audio_processing/test/audio_processing_builder_for_testing.h"
Qingsi Wang25ec8882019-11-15 12:33:05 -080041#include "p2p/base/fake_ice_transport.h"
Steve Anton10542f22019-01-11 09:11:00 -080042#include "p2p/base/mock_async_resolver.h"
43#include "p2p/base/p2p_constants.h"
44#include "p2p/base/port_interface.h"
45#include "p2p/base/test_stun_server.h"
46#include "p2p/base/test_turn_customizer.h"
47#include "p2p/base/test_turn_server.h"
48#include "p2p/client/basic_port_allocator.h"
49#include "pc/dtmf_sender.h"
50#include "pc/local_audio_source.h"
51#include "pc/media_session.h"
52#include "pc/peer_connection.h"
53#include "pc/peer_connection_factory.h"
54#include "pc/rtp_media_utils.h"
55#include "pc/session_description.h"
56#include "pc/test/fake_audio_capture_module.h"
57#include "pc/test/fake_periodic_video_track_source.h"
58#include "pc/test/fake_rtc_certificate_generator.h"
59#include "pc/test/fake_video_track_renderer.h"
60#include "pc/test/mock_peer_connection_observers.h"
Jonas Olssonb75d9e92019-02-22 10:33:29 +010061#include "rtc_base/fake_clock.h"
Qingsi Wangecd30542019-05-22 14:34:56 -070062#include "rtc_base/fake_mdns_responder.h"
Steve Anton10542f22019-01-11 09:11:00 -080063#include "rtc_base/fake_network.h"
64#include "rtc_base/firewall_socket_server.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020065#include "rtc_base/gunit.h"
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +020066#include "rtc_base/numerics/safe_conversions.h"
Steve Anton10542f22019-01-11 09:11:00 -080067#include "rtc_base/test_certificate_verifier.h"
68#include "rtc_base/time_utils.h"
69#include "rtc_base/virtual_socket_server.h"
Mirko Bonadei17f48782018-09-28 08:51:10 +020070#include "system_wrappers/include/metrics.h"
Qingsi Wangc129c352019-04-18 10:41:58 -070071#include "test/field_trial.h"
Elad Alon99c3fe52017-10-13 16:29:40 +020072#include "test/gmock.h"
deadbeef1dcb1642017-03-29 21:08:16 -070073
Mirko Bonadeiab64e8a2018-12-12 12:10:18 +010074namespace webrtc {
75namespace {
76
77using ::cricket::ContentInfo;
78using ::cricket::StreamParams;
79using ::rtc::SocketAddress;
80using ::testing::_;
Seth Hampson2f0d7022018-02-20 11:54:42 -080081using ::testing::Combine;
Steve Anton64b626b2019-01-28 17:25:26 -080082using ::testing::Contains;
Mirko Bonadeie46f5db2019-03-26 20:14:46 +010083using ::testing::DoAll;
Steve Antonede9ca52017-10-16 13:04:27 -070084using ::testing::ElementsAre;
Qingsi Wang1dac6d82018-12-12 15:28:47 -080085using ::testing::NiceMock;
Steve Anton64b626b2019-01-28 17:25:26 -080086using ::testing::Return;
Zach Stein6fcdc2f2018-08-23 16:25:55 -070087using ::testing::SetArgPointee;
Steve Antonffa6ce42018-11-30 09:26:08 -080088using ::testing::UnorderedElementsAreArray;
Mirko Bonadeiab64e8a2018-12-12 12:10:18 +010089using ::testing::Values;
Steve Anton74255ff2018-01-24 18:32:57 -080090using RTCConfiguration = PeerConnectionInterface::RTCConfiguration;
deadbeef1dcb1642017-03-29 21:08:16 -070091
92static const int kDefaultTimeout = 10000;
93static const int kMaxWaitForStatsMs = 3000;
94static const int kMaxWaitForActivationMs = 5000;
95static const int kMaxWaitForFramesMs = 10000;
96// Default number of audio/video frames to wait for before considering a test
97// successful.
98static const int kDefaultExpectedAudioFrameCount = 3;
99static const int kDefaultExpectedVideoFrameCount = 3;
100
deadbeef1dcb1642017-03-29 21:08:16 -0700101static const char kDataChannelLabel[] = "data_channel";
102
103// SRTP cipher name negotiated by the tests. This must be updated if the
104// default changes.
Taylor Brandstetterfd350d72018-04-03 16:29:26 -0700105static const int kDefaultSrtpCryptoSuite = rtc::SRTP_AES128_CM_SHA1_80;
deadbeef1dcb1642017-03-29 21:08:16 -0700106static const int kDefaultSrtpCryptoSuiteGcm = rtc::SRTP_AEAD_AES_256_GCM;
107
Steve Antonede9ca52017-10-16 13:04:27 -0700108static const SocketAddress kDefaultLocalAddress("192.168.1.1", 0);
109
deadbeef1dcb1642017-03-29 21:08:16 -0700110// Helper function for constructing offer/answer options to initiate an ICE
111// restart.
112PeerConnectionInterface::RTCOfferAnswerOptions IceRestartOfferAnswerOptions() {
113 PeerConnectionInterface::RTCOfferAnswerOptions options;
114 options.ice_restart = true;
115 return options;
116}
117
deadbeefd8ad7882017-04-18 16:01:17 -0700118// Remove all stream information (SSRCs, track IDs, etc.) and "msid-semantic"
119// attribute from received SDP, simulating a legacy endpoint.
120void RemoveSsrcsAndMsids(cricket::SessionDescription* desc) {
121 for (ContentInfo& content : desc->contents()) {
Steve Antonb1c1de12017-12-21 15:14:30 -0800122 content.media_description()->mutable_streams().clear();
deadbeefd8ad7882017-04-18 16:01:17 -0700123 }
124 desc->set_msid_supported(false);
Henrik Boström5b147782018-12-04 11:25:05 +0100125 desc->set_msid_signaling(0);
deadbeefd8ad7882017-04-18 16:01:17 -0700126}
127
Seth Hampson5897a6e2018-04-03 11:16:33 -0700128// Removes all stream information besides the stream ids, simulating an
129// endpoint that only signals a=msid lines to convey stream_ids.
130void RemoveSsrcsAndKeepMsids(cricket::SessionDescription* desc) {
131 for (ContentInfo& content : desc->contents()) {
Steve Antondf527fd2018-04-27 15:52:03 -0700132 std::string track_id;
Seth Hampson5897a6e2018-04-03 11:16:33 -0700133 std::vector<std::string> stream_ids;
134 if (!content.media_description()->streams().empty()) {
Steve Antondf527fd2018-04-27 15:52:03 -0700135 const StreamParams& first_stream =
136 content.media_description()->streams()[0];
137 track_id = first_stream.id;
138 stream_ids = first_stream.stream_ids();
Seth Hampson5897a6e2018-04-03 11:16:33 -0700139 }
140 content.media_description()->mutable_streams().clear();
Steve Antondf527fd2018-04-27 15:52:03 -0700141 StreamParams new_stream;
142 new_stream.id = track_id;
Seth Hampson5897a6e2018-04-03 11:16:33 -0700143 new_stream.set_stream_ids(stream_ids);
144 content.media_description()->AddStream(new_stream);
145 }
146}
147
zhihuangf8164932017-05-19 13:09:47 -0700148int FindFirstMediaStatsIndexByKind(
149 const std::string& kind,
150 const std::vector<const webrtc::RTCMediaStreamTrackStats*>&
151 media_stats_vec) {
152 for (size_t i = 0; i < media_stats_vec.size(); i++) {
153 if (media_stats_vec[i]->kind.ValueToString() == kind) {
154 return i;
155 }
156 }
157 return -1;
158}
159
deadbeef1dcb1642017-03-29 21:08:16 -0700160class SignalingMessageReceiver {
161 public:
Steve Antona3a92c22017-12-07 10:27:41 -0800162 virtual void ReceiveSdpMessage(SdpType type, const std::string& msg) = 0;
deadbeef1dcb1642017-03-29 21:08:16 -0700163 virtual void ReceiveIceMessage(const std::string& sdp_mid,
164 int sdp_mline_index,
165 const std::string& msg) = 0;
166
167 protected:
168 SignalingMessageReceiver() {}
169 virtual ~SignalingMessageReceiver() {}
170};
171
172class MockRtpReceiverObserver : public webrtc::RtpReceiverObserverInterface {
173 public:
174 explicit MockRtpReceiverObserver(cricket::MediaType media_type)
175 : expected_media_type_(media_type) {}
176
177 void OnFirstPacketReceived(cricket::MediaType media_type) override {
178 ASSERT_EQ(expected_media_type_, media_type);
179 first_packet_received_ = true;
180 }
181
182 bool first_packet_received() const { return first_packet_received_; }
183
184 virtual ~MockRtpReceiverObserver() {}
185
186 private:
187 bool first_packet_received_ = false;
188 cricket::MediaType expected_media_type_;
189};
190
191// Helper class that wraps a peer connection, observes it, and can accept
192// signaling messages from another wrapper.
193//
194// Uses a fake network, fake A/V capture, and optionally fake
195// encoders/decoders, though they aren't used by default since they don't
196// advertise support of any codecs.
Steve Anton94286cb2017-09-26 16:20:19 -0700197// TODO(steveanton): See how this could become a subclass of
Seth Hampson2f0d7022018-02-20 11:54:42 -0800198// PeerConnectionWrapper defined in peerconnectionwrapper.h.
deadbeef1dcb1642017-03-29 21:08:16 -0700199class PeerConnectionWrapper : public webrtc::PeerConnectionObserver,
Steve Anton15324772018-01-16 10:26:49 -0800200 public SignalingMessageReceiver {
deadbeef1dcb1642017-03-29 21:08:16 -0700201 public:
202 // Different factory methods for convenience.
203 // TODO(deadbeef): Could use the pattern of:
204 //
205 // PeerConnectionWrapper =
206 // WrapperBuilder.WithConfig(...).WithOptions(...).build();
207 //
208 // To reduce some code duplication.
209 static PeerConnectionWrapper* CreateWithDtlsIdentityStore(
210 const std::string& debug_name,
211 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator,
212 rtc::Thread* network_thread,
213 rtc::Thread* worker_thread) {
214 PeerConnectionWrapper* client(new PeerConnectionWrapper(debug_name));
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700215 webrtc::PeerConnectionDependencies dependencies(nullptr);
216 dependencies.cert_generator = std::move(cert_generator);
Niels Möllerf06f9232018-08-07 12:32:18 +0200217 if (!client->Init(nullptr, nullptr, std::move(dependencies), network_thread,
Bjorn Mellem175aa2e2018-11-08 11:23:22 -0800218 worker_thread, nullptr,
Johannes Kron3e983682020-03-29 22:17:00 +0200219 /*reset_encoder_factory=*/false,
220 /*reset_decoder_factory=*/false)) {
deadbeef1dcb1642017-03-29 21:08:16 -0700221 delete client;
222 return nullptr;
223 }
224 return client;
225 }
226
deadbeef2f425aa2017-04-14 10:41:32 -0700227 webrtc::PeerConnectionFactoryInterface* pc_factory() const {
228 return peer_connection_factory_.get();
229 }
230
deadbeef1dcb1642017-03-29 21:08:16 -0700231 webrtc::PeerConnectionInterface* pc() const { return peer_connection_.get(); }
232
233 // If a signaling message receiver is set (via ConnectFakeSignaling), this
234 // will set the whole offer/answer exchange in motion. Just need to wait for
235 // the signaling state to reach "stable".
236 void CreateAndSetAndSignalOffer() {
Eldar Rello5ab79e62019-10-09 18:29:44 +0300237 auto offer = CreateOfferAndWait();
deadbeef1dcb1642017-03-29 21:08:16 -0700238 ASSERT_NE(nullptr, offer);
239 EXPECT_TRUE(SetLocalDescriptionAndSendSdpMessage(std::move(offer)));
240 }
241
242 // Sets the options to be used when CreateAndSetAndSignalOffer is called, or
243 // when a remote offer is received (via fake signaling) and an answer is
244 // generated. By default, uses default options.
245 void SetOfferAnswerOptions(
246 const PeerConnectionInterface::RTCOfferAnswerOptions& options) {
247 offer_answer_options_ = options;
248 }
249
250 // Set a callback to be invoked when SDP is received via the fake signaling
251 // channel, which provides an opportunity to munge (modify) the SDP. This is
252 // used to test SDP being applied that a PeerConnection would normally not
253 // generate, but a non-JSEP endpoint might.
254 void SetReceivedSdpMunger(
255 std::function<void(cricket::SessionDescription*)> munger) {
Mirko Bonadeic61ce0d2017-11-21 17:04:20 +0100256 received_sdp_munger_ = std::move(munger);
deadbeef1dcb1642017-03-29 21:08:16 -0700257 }
258
deadbeefc964d0b2017-04-03 10:03:35 -0700259 // Similar to the above, but this is run on SDP immediately after it's
deadbeef1dcb1642017-03-29 21:08:16 -0700260 // generated.
261 void SetGeneratedSdpMunger(
262 std::function<void(cricket::SessionDescription*)> munger) {
Mirko Bonadeic61ce0d2017-11-21 17:04:20 +0100263 generated_sdp_munger_ = std::move(munger);
deadbeef1dcb1642017-03-29 21:08:16 -0700264 }
265
Seth Hampson2f0d7022018-02-20 11:54:42 -0800266 // Set a callback to be invoked when a remote offer is received via the fake
267 // signaling channel. This provides an opportunity to change the
268 // PeerConnection state before an answer is created and sent to the caller.
269 void SetRemoteOfferHandler(std::function<void()> handler) {
270 remote_offer_handler_ = std::move(handler);
271 }
272
Qingsi Wang1dac6d82018-12-12 15:28:47 -0800273 void SetRemoteAsyncResolver(rtc::MockAsyncResolver* resolver) {
274 remote_async_resolver_ = resolver;
Zach Stein6fcdc2f2018-08-23 16:25:55 -0700275 }
276
Steve Antonede9ca52017-10-16 13:04:27 -0700277 // Every ICE connection state in order that has been seen by the observer.
278 std::vector<PeerConnectionInterface::IceConnectionState>
279 ice_connection_state_history() const {
280 return ice_connection_state_history_;
281 }
Steve Anton6f25b092017-10-23 09:39:20 -0700282 void clear_ice_connection_state_history() {
283 ice_connection_state_history_.clear();
284 }
Steve Antonede9ca52017-10-16 13:04:27 -0700285
Jonas Olssonacd8ae72019-02-25 15:26:24 +0100286 // Every standardized ICE connection state in order that has been seen by the
287 // observer.
288 std::vector<PeerConnectionInterface::IceConnectionState>
289 standardized_ice_connection_state_history() const {
290 return standardized_ice_connection_state_history_;
291 }
292
Jonas Olsson635474e2018-10-18 15:58:17 +0200293 // Every PeerConnection state in order that has been seen by the observer.
294 std::vector<PeerConnectionInterface::PeerConnectionState>
295 peer_connection_state_history() const {
296 return peer_connection_state_history_;
297 }
298
Steve Antonede9ca52017-10-16 13:04:27 -0700299 // Every ICE gathering state in order that has been seen by the observer.
300 std::vector<PeerConnectionInterface::IceGatheringState>
301 ice_gathering_state_history() const {
302 return ice_gathering_state_history_;
deadbeef1dcb1642017-03-29 21:08:16 -0700303 }
Alex Drake00c7ecf2019-08-06 10:54:47 -0700304 std::vector<cricket::CandidatePairChangeEvent>
305 ice_candidate_pair_change_history() const {
306 return ice_candidate_pair_change_history_;
307 }
deadbeef1dcb1642017-03-29 21:08:16 -0700308
Eldar Rello5ab79e62019-10-09 18:29:44 +0300309 // Every PeerConnection signaling state in order that has been seen by the
310 // observer.
311 std::vector<PeerConnectionInterface::SignalingState>
312 peer_connection_signaling_state_history() const {
313 return peer_connection_signaling_state_history_;
314 }
315
Steve Anton15324772018-01-16 10:26:49 -0800316 void AddAudioVideoTracks() {
317 AddAudioTrack();
318 AddVideoTrack();
deadbeef1dcb1642017-03-29 21:08:16 -0700319 }
320
Steve Anton74255ff2018-01-24 18:32:57 -0800321 rtc::scoped_refptr<RtpSenderInterface> AddAudioTrack() {
322 return AddTrack(CreateLocalAudioTrack());
323 }
deadbeef1dcb1642017-03-29 21:08:16 -0700324
Steve Anton74255ff2018-01-24 18:32:57 -0800325 rtc::scoped_refptr<RtpSenderInterface> AddVideoTrack() {
326 return AddTrack(CreateLocalVideoTrack());
327 }
deadbeef1dcb1642017-03-29 21:08:16 -0700328
329 rtc::scoped_refptr<webrtc::AudioTrackInterface> CreateLocalAudioTrack() {
Niels Möller2d02e082018-05-21 11:23:35 +0200330 cricket::AudioOptions options;
deadbeef1dcb1642017-03-29 21:08:16 -0700331 // Disable highpass filter so that we can get all the test audio frames.
Niels Möller2d02e082018-05-21 11:23:35 +0200332 options.highpass_filter = false;
deadbeef1dcb1642017-03-29 21:08:16 -0700333 rtc::scoped_refptr<webrtc::AudioSourceInterface> source =
Niels Möller2d02e082018-05-21 11:23:35 +0200334 peer_connection_factory_->CreateAudioSource(options);
deadbeef1dcb1642017-03-29 21:08:16 -0700335 // TODO(perkj): Test audio source when it is implemented. Currently audio
336 // always use the default input.
deadbeefb1a15d72017-09-07 14:12:05 -0700337 return peer_connection_factory_->CreateAudioTrack(rtc::CreateRandomUuid(),
deadbeef1dcb1642017-03-29 21:08:16 -0700338 source);
339 }
340
341 rtc::scoped_refptr<webrtc::VideoTrackInterface> CreateLocalVideoTrack() {
Johannes Kron965e7942018-09-13 15:36:20 +0200342 webrtc::FakePeriodicVideoSource::Config config;
343 config.timestamp_offset_ms = rtc::TimeMillis();
344 return CreateLocalVideoTrackInternal(config);
deadbeef1dcb1642017-03-29 21:08:16 -0700345 }
346
347 rtc::scoped_refptr<webrtc::VideoTrackInterface>
Niels Möller5c7efe72018-05-11 10:34:46 +0200348 CreateLocalVideoTrackWithConfig(
349 webrtc::FakePeriodicVideoSource::Config config) {
350 return CreateLocalVideoTrackInternal(config);
deadbeef1dcb1642017-03-29 21:08:16 -0700351 }
352
353 rtc::scoped_refptr<webrtc::VideoTrackInterface>
354 CreateLocalVideoTrackWithRotation(webrtc::VideoRotation rotation) {
Niels Möller5c7efe72018-05-11 10:34:46 +0200355 webrtc::FakePeriodicVideoSource::Config config;
356 config.rotation = rotation;
Johannes Kron965e7942018-09-13 15:36:20 +0200357 config.timestamp_offset_ms = rtc::TimeMillis();
Niels Möller5c7efe72018-05-11 10:34:46 +0200358 return CreateLocalVideoTrackInternal(config);
deadbeef1dcb1642017-03-29 21:08:16 -0700359 }
360
Steve Anton74255ff2018-01-24 18:32:57 -0800361 rtc::scoped_refptr<RtpSenderInterface> AddTrack(
362 rtc::scoped_refptr<MediaStreamTrackInterface> track,
Seth Hampson845e8782018-03-02 11:34:10 -0800363 const std::vector<std::string>& stream_ids = {}) {
364 auto result = pc()->AddTrack(track, stream_ids);
Steve Anton15324772018-01-16 10:26:49 -0800365 EXPECT_EQ(RTCErrorType::NONE, result.error().type());
Steve Anton74255ff2018-01-24 18:32:57 -0800366 return result.MoveValue();
367 }
368
369 std::vector<rtc::scoped_refptr<RtpReceiverInterface>> GetReceiversOfType(
370 cricket::MediaType media_type) {
371 std::vector<rtc::scoped_refptr<RtpReceiverInterface>> receivers;
Mirko Bonadei739baf02019-01-27 17:29:42 +0100372 for (const auto& receiver : pc()->GetReceivers()) {
Steve Anton74255ff2018-01-24 18:32:57 -0800373 if (receiver->media_type() == media_type) {
374 receivers.push_back(receiver);
375 }
376 }
377 return receivers;
deadbeef1dcb1642017-03-29 21:08:16 -0700378 }
379
Seth Hampson2f0d7022018-02-20 11:54:42 -0800380 rtc::scoped_refptr<RtpTransceiverInterface> GetFirstTransceiverOfType(
381 cricket::MediaType media_type) {
382 for (auto transceiver : pc()->GetTransceivers()) {
383 if (transceiver->receiver()->media_type() == media_type) {
384 return transceiver;
385 }
386 }
387 return nullptr;
388 }
389
deadbeef1dcb1642017-03-29 21:08:16 -0700390 bool SignalingStateStable() {
391 return pc()->signaling_state() == webrtc::PeerConnectionInterface::kStable;
392 }
393
394 void CreateDataChannel() { CreateDataChannel(nullptr); }
395
396 void CreateDataChannel(const webrtc::DataChannelInit* init) {
Steve Antonda6c0952017-10-23 11:41:54 -0700397 CreateDataChannel(kDataChannelLabel, init);
398 }
399
400 void CreateDataChannel(const std::string& label,
401 const webrtc::DataChannelInit* init) {
402 data_channel_ = pc()->CreateDataChannel(label, init);
deadbeef1dcb1642017-03-29 21:08:16 -0700403 ASSERT_TRUE(data_channel_.get() != nullptr);
404 data_observer_.reset(new MockDataChannelObserver(data_channel_));
405 }
406
407 DataChannelInterface* data_channel() { return data_channel_; }
408 const MockDataChannelObserver* data_observer() const {
409 return data_observer_.get();
410 }
411
412 int audio_frames_received() const {
413 return fake_audio_capture_module_->frames_received();
414 }
415
416 // Takes minimum of video frames received for each track.
417 //
418 // Can be used like:
419 // EXPECT_GE(expected_frames, min_video_frames_received_per_track());
420 //
421 // To ensure that all video tracks received at least a certain number of
422 // frames.
423 int min_video_frames_received_per_track() const {
424 int min_frames = INT_MAX;
Anders Carlsson5f2bb622018-05-14 09:48:06 +0200425 if (fake_video_renderers_.empty()) {
426 return 0;
deadbeef1dcb1642017-03-29 21:08:16 -0700427 }
deadbeef1dcb1642017-03-29 21:08:16 -0700428
Anders Carlsson5f2bb622018-05-14 09:48:06 +0200429 for (const auto& pair : fake_video_renderers_) {
430 min_frames = std::min(min_frames, pair.second->num_rendered_frames());
deadbeef1dcb1642017-03-29 21:08:16 -0700431 }
Anders Carlsson5f2bb622018-05-14 09:48:06 +0200432 return min_frames;
deadbeef1dcb1642017-03-29 21:08:16 -0700433 }
434
435 // Returns a MockStatsObserver in a state after stats gathering finished,
436 // which can be used to access the gathered stats.
deadbeefd8ad7882017-04-18 16:01:17 -0700437 rtc::scoped_refptr<MockStatsObserver> OldGetStatsForTrack(
deadbeef1dcb1642017-03-29 21:08:16 -0700438 webrtc::MediaStreamTrackInterface* track) {
439 rtc::scoped_refptr<MockStatsObserver> observer(
440 new rtc::RefCountedObject<MockStatsObserver>());
441 EXPECT_TRUE(peer_connection_->GetStats(
442 observer, nullptr, PeerConnectionInterface::kStatsOutputLevelStandard));
443 EXPECT_TRUE_WAIT(observer->called(), kDefaultTimeout);
444 return observer;
445 }
446
447 // Version that doesn't take a track "filter", and gathers all stats.
deadbeefd8ad7882017-04-18 16:01:17 -0700448 rtc::scoped_refptr<MockStatsObserver> OldGetStats() {
449 return OldGetStatsForTrack(nullptr);
450 }
451
452 // Synchronously gets stats and returns them. If it times out, fails the test
453 // and returns null.
454 rtc::scoped_refptr<const webrtc::RTCStatsReport> NewGetStats() {
455 rtc::scoped_refptr<webrtc::MockRTCStatsCollectorCallback> callback(
456 new rtc::RefCountedObject<webrtc::MockRTCStatsCollectorCallback>());
457 peer_connection_->GetStats(callback);
458 EXPECT_TRUE_WAIT(callback->called(), kDefaultTimeout);
459 return callback->report();
deadbeef1dcb1642017-03-29 21:08:16 -0700460 }
461
462 int rendered_width() {
463 EXPECT_FALSE(fake_video_renderers_.empty());
464 return fake_video_renderers_.empty()
465 ? 0
466 : fake_video_renderers_.begin()->second->width();
467 }
468
469 int rendered_height() {
470 EXPECT_FALSE(fake_video_renderers_.empty());
471 return fake_video_renderers_.empty()
472 ? 0
473 : fake_video_renderers_.begin()->second->height();
474 }
475
476 double rendered_aspect_ratio() {
477 if (rendered_height() == 0) {
478 return 0.0;
479 }
480 return static_cast<double>(rendered_width()) / rendered_height();
481 }
482
483 webrtc::VideoRotation rendered_rotation() {
484 EXPECT_FALSE(fake_video_renderers_.empty());
485 return fake_video_renderers_.empty()
486 ? webrtc::kVideoRotation_0
487 : fake_video_renderers_.begin()->second->rotation();
488 }
489
490 int local_rendered_width() {
491 return local_video_renderer_ ? local_video_renderer_->width() : 0;
492 }
493
494 int local_rendered_height() {
495 return local_video_renderer_ ? local_video_renderer_->height() : 0;
496 }
497
498 double local_rendered_aspect_ratio() {
499 if (local_rendered_height() == 0) {
500 return 0.0;
501 }
502 return static_cast<double>(local_rendered_width()) /
503 local_rendered_height();
504 }
505
506 size_t number_of_remote_streams() {
507 if (!pc()) {
508 return 0;
509 }
510 return pc()->remote_streams()->count();
511 }
512
513 StreamCollectionInterface* remote_streams() const {
514 if (!pc()) {
515 ADD_FAILURE();
516 return nullptr;
517 }
518 return pc()->remote_streams();
519 }
520
521 StreamCollectionInterface* local_streams() {
522 if (!pc()) {
523 ADD_FAILURE();
524 return nullptr;
525 }
526 return pc()->local_streams();
527 }
528
529 webrtc::PeerConnectionInterface::SignalingState signaling_state() {
530 return pc()->signaling_state();
531 }
532
533 webrtc::PeerConnectionInterface::IceConnectionState ice_connection_state() {
534 return pc()->ice_connection_state();
535 }
536
Jonas Olsson7a6739e2019-01-15 16:31:55 +0100537 webrtc::PeerConnectionInterface::IceConnectionState
538 standardized_ice_connection_state() {
539 return pc()->standardized_ice_connection_state();
540 }
541
deadbeef1dcb1642017-03-29 21:08:16 -0700542 webrtc::PeerConnectionInterface::IceGatheringState ice_gathering_state() {
543 return pc()->ice_gathering_state();
544 }
545
546 // Returns a MockRtpReceiverObserver for each RtpReceiver returned by
547 // GetReceivers. They're updated automatically when a remote offer/answer
548 // from the fake signaling channel is applied, or when
549 // ResetRtpReceiverObservers below is called.
550 const std::vector<std::unique_ptr<MockRtpReceiverObserver>>&
551 rtp_receiver_observers() {
552 return rtp_receiver_observers_;
553 }
554
555 void ResetRtpReceiverObservers() {
556 rtp_receiver_observers_.clear();
Mirko Bonadeic61ce0d2017-11-21 17:04:20 +0100557 for (const rtc::scoped_refptr<RtpReceiverInterface>& receiver :
558 pc()->GetReceivers()) {
deadbeef1dcb1642017-03-29 21:08:16 -0700559 std::unique_ptr<MockRtpReceiverObserver> observer(
560 new MockRtpReceiverObserver(receiver->media_type()));
561 receiver->SetObserver(observer.get());
562 rtp_receiver_observers_.push_back(std::move(observer));
563 }
564 }
565
Qingsi Wangecd30542019-05-22 14:34:56 -0700566 rtc::FakeNetworkManager* network_manager() const {
Steve Antonede9ca52017-10-16 13:04:27 -0700567 return fake_network_manager_.get();
568 }
569 cricket::PortAllocator* port_allocator() const { return port_allocator_; }
570
Qingsi Wang7685e862018-06-11 20:15:46 -0700571 webrtc::FakeRtcEventLogFactory* event_log_factory() const {
572 return event_log_factory_;
573 }
574
Qingsi Wangc129c352019-04-18 10:41:58 -0700575 const cricket::Candidate& last_candidate_gathered() const {
576 return last_candidate_gathered_;
577 }
Eldar Relloda13ea22019-06-01 12:23:43 +0300578 const cricket::IceCandidateErrorEvent& error_event() const {
579 return error_event_;
580 }
Qingsi Wangc129c352019-04-18 10:41:58 -0700581
Qingsi Wangecd30542019-05-22 14:34:56 -0700582 // Sets the mDNS responder for the owned fake network manager and keeps a
583 // reference to the responder.
584 void SetMdnsResponder(
585 std::unique_ptr<webrtc::FakeMdnsResponder> mdns_responder) {
586 RTC_DCHECK(mdns_responder != nullptr);
587 mdns_responder_ = mdns_responder.get();
588 network_manager()->set_mdns_responder(std::move(mdns_responder));
589 }
590
Eldar Rello5ab79e62019-10-09 18:29:44 +0300591 // Returns null on failure.
592 std::unique_ptr<SessionDescriptionInterface> CreateOfferAndWait() {
593 rtc::scoped_refptr<MockCreateSessionDescriptionObserver> observer(
594 new rtc::RefCountedObject<MockCreateSessionDescriptionObserver>());
595 pc()->CreateOffer(observer, offer_answer_options_);
596 return WaitForDescriptionFromObserver(observer);
597 }
Eldar Rellod9ebe012020-03-18 20:41:45 +0200598 bool Rollback() {
599 return SetRemoteDescription(
600 webrtc::CreateSessionDescription(SdpType::kRollback, ""));
601 }
Eldar Rello5ab79e62019-10-09 18:29:44 +0300602
deadbeef1dcb1642017-03-29 21:08:16 -0700603 private:
604 explicit PeerConnectionWrapper(const std::string& debug_name)
605 : debug_name_(debug_name) {}
606
Bjorn Mellem175aa2e2018-11-08 11:23:22 -0800607 bool Init(
608 const PeerConnectionFactory::Options* options,
609 const PeerConnectionInterface::RTCConfiguration* config,
610 webrtc::PeerConnectionDependencies dependencies,
611 rtc::Thread* network_thread,
612 rtc::Thread* worker_thread,
613 std::unique_ptr<webrtc::FakeRtcEventLogFactory> event_log_factory,
Johannes Kron3e983682020-03-29 22:17:00 +0200614 bool reset_encoder_factory,
615 bool reset_decoder_factory) {
deadbeef1dcb1642017-03-29 21:08:16 -0700616 // There's an error in this test code if Init ends up being called twice.
617 RTC_DCHECK(!peer_connection_);
618 RTC_DCHECK(!peer_connection_factory_);
619
620 fake_network_manager_.reset(new rtc::FakeNetworkManager());
Steve Antonede9ca52017-10-16 13:04:27 -0700621 fake_network_manager_->AddInterface(kDefaultLocalAddress);
deadbeef1dcb1642017-03-29 21:08:16 -0700622
623 std::unique_ptr<cricket::PortAllocator> port_allocator(
624 new cricket::BasicPortAllocator(fake_network_manager_.get()));
Steve Antonede9ca52017-10-16 13:04:27 -0700625 port_allocator_ = port_allocator.get();
deadbeef1dcb1642017-03-29 21:08:16 -0700626 fake_audio_capture_module_ = FakeAudioCaptureModule::Create();
627 if (!fake_audio_capture_module_) {
628 return false;
629 }
deadbeef1dcb1642017-03-29 21:08:16 -0700630 rtc::Thread* const signaling_thread = rtc::Thread::Current();
Qingsi Wang7685e862018-06-11 20:15:46 -0700631
632 webrtc::PeerConnectionFactoryDependencies pc_factory_dependencies;
633 pc_factory_dependencies.network_thread = network_thread;
634 pc_factory_dependencies.worker_thread = worker_thread;
635 pc_factory_dependencies.signaling_thread = signaling_thread;
Danil Chapovalov9da25bd2019-06-20 10:19:42 +0200636 pc_factory_dependencies.task_queue_factory =
637 webrtc::CreateDefaultTaskQueueFactory();
Erik Språngceb44952020-09-22 11:36:35 +0200638 pc_factory_dependencies.trials = std::make_unique<FieldTrialBasedConfig>();
Danil Chapovalov9da25bd2019-06-20 10:19:42 +0200639 cricket::MediaEngineDependencies media_deps;
640 media_deps.task_queue_factory =
641 pc_factory_dependencies.task_queue_factory.get();
642 media_deps.adm = fake_audio_capture_module_;
643 webrtc::SetMediaEngineDefaults(&media_deps);
Johannes Kron3e983682020-03-29 22:17:00 +0200644
645 if (reset_encoder_factory) {
646 media_deps.video_encoder_factory.reset();
647 }
648 if (reset_decoder_factory) {
649 media_deps.video_decoder_factory.reset();
650 }
651
Per Åhgrencc73ed32020-04-26 23:56:17 +0200652 if (!media_deps.audio_processing) {
653 // If the standard Creation method for APM returns a null pointer, instead
654 // use the builder for testing to create an APM object.
655 media_deps.audio_processing = AudioProcessingBuilderForTesting().Create();
656 }
657
Erik Språngceb44952020-09-22 11:36:35 +0200658 media_deps.trials = pc_factory_dependencies.trials.get();
659
Qingsi Wang7685e862018-06-11 20:15:46 -0700660 pc_factory_dependencies.media_engine =
Danil Chapovalov9da25bd2019-06-20 10:19:42 +0200661 cricket::CreateMediaEngine(std::move(media_deps));
Qingsi Wang7685e862018-06-11 20:15:46 -0700662 pc_factory_dependencies.call_factory = webrtc::CreateCallFactory();
663 if (event_log_factory) {
664 event_log_factory_ = event_log_factory.get();
665 pc_factory_dependencies.event_log_factory = std::move(event_log_factory);
666 } else {
667 pc_factory_dependencies.event_log_factory =
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200668 std::make_unique<webrtc::RtcEventLogFactory>(
Danil Chapovalov9da25bd2019-06-20 10:19:42 +0200669 pc_factory_dependencies.task_queue_factory.get());
Qingsi Wang7685e862018-06-11 20:15:46 -0700670 }
671 peer_connection_factory_ = webrtc::CreateModularPeerConnectionFactory(
672 std::move(pc_factory_dependencies));
673
deadbeef1dcb1642017-03-29 21:08:16 -0700674 if (!peer_connection_factory_) {
675 return false;
676 }
677 if (options) {
678 peer_connection_factory_->SetOptions(*options);
679 }
Seth Hampson2f0d7022018-02-20 11:54:42 -0800680 if (config) {
681 sdp_semantics_ = config->sdp_semantics;
682 }
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700683
684 dependencies.allocator = std::move(port_allocator);
Niels Möllerf06f9232018-08-07 12:32:18 +0200685 peer_connection_ = CreatePeerConnection(config, std::move(dependencies));
deadbeef1dcb1642017-03-29 21:08:16 -0700686 return peer_connection_.get() != nullptr;
687 }
688
689 rtc::scoped_refptr<webrtc::PeerConnectionInterface> CreatePeerConnection(
deadbeef1dcb1642017-03-29 21:08:16 -0700690 const PeerConnectionInterface::RTCConfiguration* config,
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700691 webrtc::PeerConnectionDependencies dependencies) {
deadbeef1dcb1642017-03-29 21:08:16 -0700692 PeerConnectionInterface::RTCConfiguration modified_config;
693 // If |config| is null, this will result in a default configuration being
694 // used.
695 if (config) {
696 modified_config = *config;
697 }
698 // Disable resolution adaptation; we don't want it interfering with the
699 // test results.
700 // TODO(deadbeef): Do something more robust. Since we're testing for aspect
701 // ratios and not specific resolutions, is this even necessary?
702 modified_config.set_cpu_adaptation(false);
703
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700704 dependencies.observer = this;
deadbeef1dcb1642017-03-29 21:08:16 -0700705 return peer_connection_factory_->CreatePeerConnection(
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700706 modified_config, std::move(dependencies));
deadbeef1dcb1642017-03-29 21:08:16 -0700707 }
708
709 void set_signaling_message_receiver(
710 SignalingMessageReceiver* signaling_message_receiver) {
711 signaling_message_receiver_ = signaling_message_receiver;
712 }
713
714 void set_signaling_delay_ms(int delay_ms) { signaling_delay_ms_ = delay_ms; }
715
Steve Antonede9ca52017-10-16 13:04:27 -0700716 void set_signal_ice_candidates(bool signal) {
717 signal_ice_candidates_ = signal;
718 }
719
deadbeef1dcb1642017-03-29 21:08:16 -0700720 rtc::scoped_refptr<webrtc::VideoTrackInterface> CreateLocalVideoTrackInternal(
Niels Möller5c7efe72018-05-11 10:34:46 +0200721 webrtc::FakePeriodicVideoSource::Config config) {
deadbeef1dcb1642017-03-29 21:08:16 -0700722 // Set max frame rate to 10fps to reduce the risk of test flakiness.
723 // TODO(deadbeef): Do something more robust.
Niels Möller5c7efe72018-05-11 10:34:46 +0200724 config.frame_interval_ms = 100;
deadbeef1dcb1642017-03-29 21:08:16 -0700725
Niels Möller5c7efe72018-05-11 10:34:46 +0200726 video_track_sources_.emplace_back(
Niels Möller0f405822018-05-17 09:16:41 +0200727 new rtc::RefCountedObject<webrtc::FakePeriodicVideoTrackSource>(
728 config, false /* remote */));
deadbeef1dcb1642017-03-29 21:08:16 -0700729 rtc::scoped_refptr<webrtc::VideoTrackInterface> track(
Niels Möller5c7efe72018-05-11 10:34:46 +0200730 peer_connection_factory_->CreateVideoTrack(
731 rtc::CreateRandomUuid(), video_track_sources_.back()));
deadbeef1dcb1642017-03-29 21:08:16 -0700732 if (!local_video_renderer_) {
733 local_video_renderer_.reset(new webrtc::FakeVideoTrackRenderer(track));
734 }
735 return track;
736 }
737
738 void HandleIncomingOffer(const std::string& msg) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100739 RTC_LOG(LS_INFO) << debug_name_ << ": HandleIncomingOffer";
Steve Antona3a92c22017-12-07 10:27:41 -0800740 std::unique_ptr<SessionDescriptionInterface> desc =
741 webrtc::CreateSessionDescription(SdpType::kOffer, msg);
deadbeef1dcb1642017-03-29 21:08:16 -0700742 if (received_sdp_munger_) {
743 received_sdp_munger_(desc->description());
744 }
745
746 EXPECT_TRUE(SetRemoteDescription(std::move(desc)));
747 // Setting a remote description may have changed the number of receivers,
748 // so reset the receiver observers.
749 ResetRtpReceiverObservers();
Seth Hampson2f0d7022018-02-20 11:54:42 -0800750 if (remote_offer_handler_) {
751 remote_offer_handler_();
752 }
deadbeef1dcb1642017-03-29 21:08:16 -0700753 auto answer = CreateAnswer();
754 ASSERT_NE(nullptr, answer);
755 EXPECT_TRUE(SetLocalDescriptionAndSendSdpMessage(std::move(answer)));
756 }
757
758 void HandleIncomingAnswer(const std::string& msg) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100759 RTC_LOG(LS_INFO) << debug_name_ << ": HandleIncomingAnswer";
Steve Antona3a92c22017-12-07 10:27:41 -0800760 std::unique_ptr<SessionDescriptionInterface> desc =
761 webrtc::CreateSessionDescription(SdpType::kAnswer, msg);
deadbeef1dcb1642017-03-29 21:08:16 -0700762 if (received_sdp_munger_) {
763 received_sdp_munger_(desc->description());
764 }
765
766 EXPECT_TRUE(SetRemoteDescription(std::move(desc)));
767 // Set the RtpReceiverObserver after receivers are created.
768 ResetRtpReceiverObservers();
769 }
770
771 // Returns null on failure.
deadbeef1dcb1642017-03-29 21:08:16 -0700772 std::unique_ptr<SessionDescriptionInterface> CreateAnswer() {
773 rtc::scoped_refptr<MockCreateSessionDescriptionObserver> observer(
774 new rtc::RefCountedObject<MockCreateSessionDescriptionObserver>());
775 pc()->CreateAnswer(observer, offer_answer_options_);
776 return WaitForDescriptionFromObserver(observer);
777 }
778
779 std::unique_ptr<SessionDescriptionInterface> WaitForDescriptionFromObserver(
Mirko Bonadeic61ce0d2017-11-21 17:04:20 +0100780 MockCreateSessionDescriptionObserver* observer) {
deadbeef1dcb1642017-03-29 21:08:16 -0700781 EXPECT_EQ_WAIT(true, observer->called(), kDefaultTimeout);
782 if (!observer->result()) {
783 return nullptr;
784 }
785 auto description = observer->MoveDescription();
786 if (generated_sdp_munger_) {
787 generated_sdp_munger_(description->description());
788 }
789 return description;
790 }
791
792 // Setting the local description and sending the SDP message over the fake
793 // signaling channel are combined into the same method because the SDP
794 // message needs to be sent as soon as SetLocalDescription finishes, without
795 // waiting for the observer to be called. This ensures that ICE candidates
796 // don't outrace the description.
797 bool SetLocalDescriptionAndSendSdpMessage(
798 std::unique_ptr<SessionDescriptionInterface> desc) {
799 rtc::scoped_refptr<MockSetSessionDescriptionObserver> observer(
800 new rtc::RefCountedObject<MockSetSessionDescriptionObserver>());
Mirko Bonadei675513b2017-11-09 11:09:25 +0100801 RTC_LOG(LS_INFO) << debug_name_ << ": SetLocalDescriptionAndSendSdpMessage";
Steve Antona3a92c22017-12-07 10:27:41 -0800802 SdpType type = desc->GetType();
deadbeef1dcb1642017-03-29 21:08:16 -0700803 std::string sdp;
804 EXPECT_TRUE(desc->ToString(&sdp));
Bjorn A Mellemb689af42019-08-21 10:44:59 -0700805 RTC_LOG(LS_INFO) << debug_name_ << ": local SDP contents=\n" << sdp;
deadbeef1dcb1642017-03-29 21:08:16 -0700806 pc()->SetLocalDescription(observer, desc.release());
Harald Alvestrand6060df52020-08-11 09:54:02 +0200807 RemoveUnusedVideoRenderers();
deadbeef1dcb1642017-03-29 21:08:16 -0700808 // As mentioned above, we need to send the message immediately after
809 // SetLocalDescription.
810 SendSdpMessage(type, sdp);
811 EXPECT_TRUE_WAIT(observer->called(), kDefaultTimeout);
812 return true;
813 }
814
815 bool SetRemoteDescription(std::unique_ptr<SessionDescriptionInterface> desc) {
816 rtc::scoped_refptr<MockSetSessionDescriptionObserver> observer(
817 new rtc::RefCountedObject<MockSetSessionDescriptionObserver>());
Mirko Bonadei675513b2017-11-09 11:09:25 +0100818 RTC_LOG(LS_INFO) << debug_name_ << ": SetRemoteDescription";
deadbeef1dcb1642017-03-29 21:08:16 -0700819 pc()->SetRemoteDescription(observer, desc.release());
Harald Alvestrand6060df52020-08-11 09:54:02 +0200820 RemoveUnusedVideoRenderers();
deadbeef1dcb1642017-03-29 21:08:16 -0700821 EXPECT_TRUE_WAIT(observer->called(), kDefaultTimeout);
822 return observer->result();
823 }
824
Seth Hampson2f0d7022018-02-20 11:54:42 -0800825 // This is a work around to remove unused fake_video_renderers from
826 // transceivers that have either stopped or are no longer receiving.
827 void RemoveUnusedVideoRenderers() {
Harald Alvestrand6060df52020-08-11 09:54:02 +0200828 if (sdp_semantics_ != SdpSemantics::kUnifiedPlan) {
829 return;
830 }
Seth Hampson2f0d7022018-02-20 11:54:42 -0800831 auto transceivers = pc()->GetTransceivers();
Harald Alvestrand6060df52020-08-11 09:54:02 +0200832 std::set<std::string> active_renderers;
Seth Hampson2f0d7022018-02-20 11:54:42 -0800833 for (auto& transceiver : transceivers) {
Harald Alvestrand6060df52020-08-11 09:54:02 +0200834 // Note - we don't check for direction here. This function is called
835 // before direction is set, and in that case, we should not remove
836 // the renderer.
837 if (transceiver->receiver()->media_type() == cricket::MEDIA_TYPE_VIDEO) {
838 active_renderers.insert(transceiver->receiver()->track()->id());
Seth Hampson2f0d7022018-02-20 11:54:42 -0800839 }
Harald Alvestrand6060df52020-08-11 09:54:02 +0200840 }
841 for (auto it = fake_video_renderers_.begin();
842 it != fake_video_renderers_.end();) {
843 // Remove fake video renderers belonging to any non-active transceivers.
844 if (!active_renderers.count(it->first)) {
845 it = fake_video_renderers_.erase(it);
846 } else {
847 it++;
Seth Hampson2f0d7022018-02-20 11:54:42 -0800848 }
849 }
850 }
851
deadbeef1dcb1642017-03-29 21:08:16 -0700852 // Simulate sending a blob of SDP with delay |signaling_delay_ms_| (0 by
853 // default).
Steve Antona3a92c22017-12-07 10:27:41 -0800854 void SendSdpMessage(SdpType type, const std::string& msg) {
deadbeef1dcb1642017-03-29 21:08:16 -0700855 if (signaling_delay_ms_ == 0) {
856 RelaySdpMessageIfReceiverExists(type, msg);
857 } else {
858 invoker_.AsyncInvokeDelayed<void>(
859 RTC_FROM_HERE, rtc::Thread::Current(),
860 rtc::Bind(&PeerConnectionWrapper::RelaySdpMessageIfReceiverExists,
861 this, type, msg),
862 signaling_delay_ms_);
863 }
864 }
865
Steve Antona3a92c22017-12-07 10:27:41 -0800866 void RelaySdpMessageIfReceiverExists(SdpType type, const std::string& msg) {
deadbeef1dcb1642017-03-29 21:08:16 -0700867 if (signaling_message_receiver_) {
868 signaling_message_receiver_->ReceiveSdpMessage(type, msg);
869 }
870 }
871
872 // Simulate trickling an ICE candidate with delay |signaling_delay_ms_| (0 by
873 // default).
874 void SendIceMessage(const std::string& sdp_mid,
875 int sdp_mline_index,
876 const std::string& msg) {
877 if (signaling_delay_ms_ == 0) {
878 RelayIceMessageIfReceiverExists(sdp_mid, sdp_mline_index, msg);
879 } else {
880 invoker_.AsyncInvokeDelayed<void>(
881 RTC_FROM_HERE, rtc::Thread::Current(),
882 rtc::Bind(&PeerConnectionWrapper::RelayIceMessageIfReceiverExists,
883 this, sdp_mid, sdp_mline_index, msg),
884 signaling_delay_ms_);
885 }
886 }
887
888 void RelayIceMessageIfReceiverExists(const std::string& sdp_mid,
889 int sdp_mline_index,
890 const std::string& msg) {
891 if (signaling_message_receiver_) {
892 signaling_message_receiver_->ReceiveIceMessage(sdp_mid, sdp_mline_index,
893 msg);
894 }
895 }
896
897 // SignalingMessageReceiver callbacks.
Steve Antona3a92c22017-12-07 10:27:41 -0800898 void ReceiveSdpMessage(SdpType type, const std::string& msg) override {
899 if (type == SdpType::kOffer) {
deadbeef1dcb1642017-03-29 21:08:16 -0700900 HandleIncomingOffer(msg);
901 } else {
902 HandleIncomingAnswer(msg);
903 }
904 }
905
906 void ReceiveIceMessage(const std::string& sdp_mid,
907 int sdp_mline_index,
908 const std::string& msg) override {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100909 RTC_LOG(LS_INFO) << debug_name_ << ": ReceiveIceMessage";
deadbeef1dcb1642017-03-29 21:08:16 -0700910 std::unique_ptr<webrtc::IceCandidateInterface> candidate(
911 webrtc::CreateIceCandidate(sdp_mid, sdp_mline_index, msg, nullptr));
912 EXPECT_TRUE(pc()->AddIceCandidate(candidate.get()));
913 }
914
915 // PeerConnectionObserver callbacks.
916 void OnSignalingChange(
917 webrtc::PeerConnectionInterface::SignalingState new_state) override {
918 EXPECT_EQ(pc()->signaling_state(), new_state);
Eldar Rello5ab79e62019-10-09 18:29:44 +0300919 peer_connection_signaling_state_history_.push_back(new_state);
deadbeef1dcb1642017-03-29 21:08:16 -0700920 }
Steve Anton15324772018-01-16 10:26:49 -0800921 void OnAddTrack(rtc::scoped_refptr<RtpReceiverInterface> receiver,
922 const std::vector<rtc::scoped_refptr<MediaStreamInterface>>&
923 streams) override {
924 if (receiver->media_type() == cricket::MEDIA_TYPE_VIDEO) {
925 rtc::scoped_refptr<VideoTrackInterface> video_track(
926 static_cast<VideoTrackInterface*>(receiver->track().get()));
927 ASSERT_TRUE(fake_video_renderers_.find(video_track->id()) ==
deadbeef1dcb1642017-03-29 21:08:16 -0700928 fake_video_renderers_.end());
Steve Anton15324772018-01-16 10:26:49 -0800929 fake_video_renderers_[video_track->id()] =
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200930 std::make_unique<FakeVideoTrackRenderer>(video_track);
deadbeef1dcb1642017-03-29 21:08:16 -0700931 }
932 }
Steve Anton15324772018-01-16 10:26:49 -0800933 void OnRemoveTrack(
934 rtc::scoped_refptr<RtpReceiverInterface> receiver) override {
935 if (receiver->media_type() == cricket::MEDIA_TYPE_VIDEO) {
936 auto it = fake_video_renderers_.find(receiver->track()->id());
Harald Alvestrand6060df52020-08-11 09:54:02 +0200937 if (it != fake_video_renderers_.end()) {
938 fake_video_renderers_.erase(it);
939 } else {
940 RTC_LOG(LS_ERROR) << "OnRemoveTrack called for non-active renderer";
941 }
Steve Anton15324772018-01-16 10:26:49 -0800942 }
943 }
deadbeef1dcb1642017-03-29 21:08:16 -0700944 void OnRenegotiationNeeded() override {}
945 void OnIceConnectionChange(
946 webrtc::PeerConnectionInterface::IceConnectionState new_state) override {
947 EXPECT_EQ(pc()->ice_connection_state(), new_state);
Steve Antonede9ca52017-10-16 13:04:27 -0700948 ice_connection_state_history_.push_back(new_state);
deadbeef1dcb1642017-03-29 21:08:16 -0700949 }
Jonas Olssonacd8ae72019-02-25 15:26:24 +0100950 void OnStandardizedIceConnectionChange(
951 webrtc::PeerConnectionInterface::IceConnectionState new_state) override {
952 standardized_ice_connection_state_history_.push_back(new_state);
953 }
Jonas Olsson635474e2018-10-18 15:58:17 +0200954 void OnConnectionChange(
955 webrtc::PeerConnectionInterface::PeerConnectionState new_state) override {
956 peer_connection_state_history_.push_back(new_state);
957 }
958
deadbeef1dcb1642017-03-29 21:08:16 -0700959 void OnIceGatheringChange(
960 webrtc::PeerConnectionInterface::IceGatheringState new_state) override {
deadbeef1dcb1642017-03-29 21:08:16 -0700961 EXPECT_EQ(pc()->ice_gathering_state(), new_state);
Steve Antonede9ca52017-10-16 13:04:27 -0700962 ice_gathering_state_history_.push_back(new_state);
deadbeef1dcb1642017-03-29 21:08:16 -0700963 }
Alex Drake00c7ecf2019-08-06 10:54:47 -0700964
965 void OnIceSelectedCandidatePairChanged(
966 const cricket::CandidatePairChangeEvent& event) {
967 ice_candidate_pair_change_history_.push_back(event);
968 }
Alex Drake43faee02019-08-12 16:27:34 -0700969
deadbeef1dcb1642017-03-29 21:08:16 -0700970 void OnIceCandidate(const webrtc::IceCandidateInterface* candidate) override {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100971 RTC_LOG(LS_INFO) << debug_name_ << ": OnIceCandidate";
deadbeef1dcb1642017-03-29 21:08:16 -0700972
Qingsi Wang1dac6d82018-12-12 15:28:47 -0800973 if (remote_async_resolver_) {
974 const auto& local_candidate = candidate->candidate();
Qingsi Wang1dac6d82018-12-12 15:28:47 -0800975 if (local_candidate.address().IsUnresolvedIP()) {
976 RTC_DCHECK(local_candidate.type() == cricket::LOCAL_PORT_TYPE);
977 rtc::SocketAddress resolved_addr(local_candidate.address());
Qingsi Wangecd30542019-05-22 14:34:56 -0700978 const auto resolved_ip = mdns_responder_->GetMappedAddressForName(
Qingsi Wang1dac6d82018-12-12 15:28:47 -0800979 local_candidate.address().hostname());
980 RTC_DCHECK(!resolved_ip.IsNil());
981 resolved_addr.SetResolvedIP(resolved_ip);
982 EXPECT_CALL(*remote_async_resolver_, GetResolvedAddress(_, _))
983 .WillOnce(DoAll(SetArgPointee<1>(resolved_addr), Return(true)));
984 EXPECT_CALL(*remote_async_resolver_, Destroy(_));
Zach Stein6fcdc2f2018-08-23 16:25:55 -0700985 }
Zach Stein6fcdc2f2018-08-23 16:25:55 -0700986 }
987
deadbeef1dcb1642017-03-29 21:08:16 -0700988 std::string ice_sdp;
Qingsi Wang1dac6d82018-12-12 15:28:47 -0800989 EXPECT_TRUE(candidate->ToString(&ice_sdp));
Steve Antonede9ca52017-10-16 13:04:27 -0700990 if (signaling_message_receiver_ == nullptr || !signal_ice_candidates_) {
deadbeef1dcb1642017-03-29 21:08:16 -0700991 // Remote party may be deleted.
992 return;
993 }
Qingsi Wang1dac6d82018-12-12 15:28:47 -0800994 SendIceMessage(candidate->sdp_mid(), candidate->sdp_mline_index(), ice_sdp);
Qingsi Wangc129c352019-04-18 10:41:58 -0700995 last_candidate_gathered_ = candidate->candidate();
deadbeef1dcb1642017-03-29 21:08:16 -0700996 }
Eldar Rello0095d372019-12-02 22:22:07 +0200997 void OnIceCandidateError(const std::string& address,
998 int port,
Eldar Relloda13ea22019-06-01 12:23:43 +0300999 const std::string& url,
1000 int error_code,
1001 const std::string& error_text) override {
Eldar Rello0095d372019-12-02 22:22:07 +02001002 error_event_ = cricket::IceCandidateErrorEvent(address, port, url,
Eldar Relloda13ea22019-06-01 12:23:43 +03001003 error_code, error_text);
1004 }
deadbeef1dcb1642017-03-29 21:08:16 -07001005 void OnDataChannel(
1006 rtc::scoped_refptr<DataChannelInterface> data_channel) override {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001007 RTC_LOG(LS_INFO) << debug_name_ << ": OnDataChannel";
deadbeef1dcb1642017-03-29 21:08:16 -07001008 data_channel_ = data_channel;
1009 data_observer_.reset(new MockDataChannelObserver(data_channel));
1010 }
1011
deadbeef1dcb1642017-03-29 21:08:16 -07001012 std::string debug_name_;
1013
1014 std::unique_ptr<rtc::FakeNetworkManager> fake_network_manager_;
Qingsi Wangecd30542019-05-22 14:34:56 -07001015 // Reference to the mDNS responder owned by |fake_network_manager_| after set.
1016 webrtc::FakeMdnsResponder* mdns_responder_ = nullptr;
deadbeef1dcb1642017-03-29 21:08:16 -07001017
1018 rtc::scoped_refptr<webrtc::PeerConnectionInterface> peer_connection_;
1019 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface>
1020 peer_connection_factory_;
1021
Steve Antonede9ca52017-10-16 13:04:27 -07001022 cricket::PortAllocator* port_allocator_;
deadbeef1dcb1642017-03-29 21:08:16 -07001023 // Needed to keep track of number of frames sent.
1024 rtc::scoped_refptr<FakeAudioCaptureModule> fake_audio_capture_module_;
1025 // Needed to keep track of number of frames received.
1026 std::map<std::string, std::unique_ptr<webrtc::FakeVideoTrackRenderer>>
1027 fake_video_renderers_;
1028 // Needed to ensure frames aren't received for removed tracks.
1029 std::vector<std::unique_ptr<webrtc::FakeVideoTrackRenderer>>
1030 removed_fake_video_renderers_;
deadbeef1dcb1642017-03-29 21:08:16 -07001031
1032 // For remote peer communication.
1033 SignalingMessageReceiver* signaling_message_receiver_ = nullptr;
1034 int signaling_delay_ms_ = 0;
Steve Antonede9ca52017-10-16 13:04:27 -07001035 bool signal_ice_candidates_ = true;
Qingsi Wangc129c352019-04-18 10:41:58 -07001036 cricket::Candidate last_candidate_gathered_;
Eldar Relloda13ea22019-06-01 12:23:43 +03001037 cricket::IceCandidateErrorEvent error_event_;
deadbeef1dcb1642017-03-29 21:08:16 -07001038
Niels Möller5c7efe72018-05-11 10:34:46 +02001039 // Store references to the video sources we've created, so that we can stop
deadbeef1dcb1642017-03-29 21:08:16 -07001040 // them, if required.
Niels Möller5c7efe72018-05-11 10:34:46 +02001041 std::vector<rtc::scoped_refptr<webrtc::VideoTrackSource>>
1042 video_track_sources_;
deadbeef1dcb1642017-03-29 21:08:16 -07001043 // |local_video_renderer_| attached to the first created local video track.
1044 std::unique_ptr<webrtc::FakeVideoTrackRenderer> local_video_renderer_;
1045
Seth Hampson2f0d7022018-02-20 11:54:42 -08001046 SdpSemantics sdp_semantics_;
deadbeef1dcb1642017-03-29 21:08:16 -07001047 PeerConnectionInterface::RTCOfferAnswerOptions offer_answer_options_;
1048 std::function<void(cricket::SessionDescription*)> received_sdp_munger_;
1049 std::function<void(cricket::SessionDescription*)> generated_sdp_munger_;
Seth Hampson2f0d7022018-02-20 11:54:42 -08001050 std::function<void()> remote_offer_handler_;
Qingsi Wang1dac6d82018-12-12 15:28:47 -08001051 rtc::MockAsyncResolver* remote_async_resolver_ = nullptr;
deadbeef1dcb1642017-03-29 21:08:16 -07001052 rtc::scoped_refptr<DataChannelInterface> data_channel_;
1053 std::unique_ptr<MockDataChannelObserver> data_observer_;
1054
1055 std::vector<std::unique_ptr<MockRtpReceiverObserver>> rtp_receiver_observers_;
1056
Steve Antonede9ca52017-10-16 13:04:27 -07001057 std::vector<PeerConnectionInterface::IceConnectionState>
1058 ice_connection_state_history_;
Jonas Olssonacd8ae72019-02-25 15:26:24 +01001059 std::vector<PeerConnectionInterface::IceConnectionState>
1060 standardized_ice_connection_state_history_;
Jonas Olsson635474e2018-10-18 15:58:17 +02001061 std::vector<PeerConnectionInterface::PeerConnectionState>
1062 peer_connection_state_history_;
Steve Antonede9ca52017-10-16 13:04:27 -07001063 std::vector<PeerConnectionInterface::IceGatheringState>
1064 ice_gathering_state_history_;
Alex Drake00c7ecf2019-08-06 10:54:47 -07001065 std::vector<cricket::CandidatePairChangeEvent>
1066 ice_candidate_pair_change_history_;
Eldar Rello5ab79e62019-10-09 18:29:44 +03001067 std::vector<PeerConnectionInterface::SignalingState>
1068 peer_connection_signaling_state_history_;
Qingsi Wang7685e862018-06-11 20:15:46 -07001069 webrtc::FakeRtcEventLogFactory* event_log_factory_;
1070
deadbeef1dcb1642017-03-29 21:08:16 -07001071 rtc::AsyncInvoker invoker_;
1072
Seth Hampson2f0d7022018-02-20 11:54:42 -08001073 friend class PeerConnectionIntegrationBaseTest;
deadbeef1dcb1642017-03-29 21:08:16 -07001074};
1075
Elad Alon99c3fe52017-10-13 16:29:40 +02001076class MockRtcEventLogOutput : public webrtc::RtcEventLogOutput {
1077 public:
1078 virtual ~MockRtcEventLogOutput() = default;
Danil Chapovalov3a353122020-05-15 11:16:53 +02001079 MOCK_METHOD(bool, IsActive, (), (const, override));
1080 MOCK_METHOD(bool, Write, (const std::string&), (override));
Elad Alon99c3fe52017-10-13 16:29:40 +02001081};
1082
Seth Hampson2f0d7022018-02-20 11:54:42 -08001083// This helper object is used for both specifying how many audio/video frames
1084// are expected to be received for a caller/callee. It provides helper functions
1085// to specify these expectations. The object initially starts in a state of no
1086// expectations.
1087class MediaExpectations {
1088 public:
1089 enum ExpectFrames {
1090 kExpectSomeFrames,
1091 kExpectNoFrames,
1092 kNoExpectation,
1093 };
1094
1095 void ExpectBidirectionalAudioAndVideo() {
1096 ExpectBidirectionalAudio();
1097 ExpectBidirectionalVideo();
1098 }
1099
1100 void ExpectBidirectionalAudio() {
1101 CallerExpectsSomeAudio();
1102 CalleeExpectsSomeAudio();
1103 }
1104
1105 void ExpectNoAudio() {
1106 CallerExpectsNoAudio();
1107 CalleeExpectsNoAudio();
1108 }
1109
1110 void ExpectBidirectionalVideo() {
1111 CallerExpectsSomeVideo();
1112 CalleeExpectsSomeVideo();
1113 }
1114
1115 void ExpectNoVideo() {
1116 CallerExpectsNoVideo();
1117 CalleeExpectsNoVideo();
1118 }
1119
1120 void CallerExpectsSomeAudioAndVideo() {
1121 CallerExpectsSomeAudio();
1122 CallerExpectsSomeVideo();
1123 }
1124
1125 void CalleeExpectsSomeAudioAndVideo() {
1126 CalleeExpectsSomeAudio();
1127 CalleeExpectsSomeVideo();
1128 }
1129
1130 // Caller's audio functions.
1131 void CallerExpectsSomeAudio(
1132 int expected_audio_frames = kDefaultExpectedAudioFrameCount) {
1133 caller_audio_expectation_ = kExpectSomeFrames;
1134 caller_audio_frames_expected_ = expected_audio_frames;
1135 }
1136
1137 void CallerExpectsNoAudio() {
1138 caller_audio_expectation_ = kExpectNoFrames;
1139 caller_audio_frames_expected_ = 0;
1140 }
1141
1142 // Caller's video functions.
1143 void CallerExpectsSomeVideo(
1144 int expected_video_frames = kDefaultExpectedVideoFrameCount) {
1145 caller_video_expectation_ = kExpectSomeFrames;
1146 caller_video_frames_expected_ = expected_video_frames;
1147 }
1148
1149 void CallerExpectsNoVideo() {
1150 caller_video_expectation_ = kExpectNoFrames;
1151 caller_video_frames_expected_ = 0;
1152 }
1153
1154 // Callee's audio functions.
1155 void CalleeExpectsSomeAudio(
1156 int expected_audio_frames = kDefaultExpectedAudioFrameCount) {
1157 callee_audio_expectation_ = kExpectSomeFrames;
1158 callee_audio_frames_expected_ = expected_audio_frames;
1159 }
1160
1161 void CalleeExpectsNoAudio() {
1162 callee_audio_expectation_ = kExpectNoFrames;
1163 callee_audio_frames_expected_ = 0;
1164 }
1165
1166 // Callee's video functions.
1167 void CalleeExpectsSomeVideo(
1168 int expected_video_frames = kDefaultExpectedVideoFrameCount) {
1169 callee_video_expectation_ = kExpectSomeFrames;
1170 callee_video_frames_expected_ = expected_video_frames;
1171 }
1172
1173 void CalleeExpectsNoVideo() {
1174 callee_video_expectation_ = kExpectNoFrames;
1175 callee_video_frames_expected_ = 0;
1176 }
1177
1178 ExpectFrames caller_audio_expectation_ = kNoExpectation;
1179 ExpectFrames caller_video_expectation_ = kNoExpectation;
1180 ExpectFrames callee_audio_expectation_ = kNoExpectation;
1181 ExpectFrames callee_video_expectation_ = kNoExpectation;
1182 int caller_audio_frames_expected_ = 0;
1183 int caller_video_frames_expected_ = 0;
1184 int callee_audio_frames_expected_ = 0;
1185 int callee_video_frames_expected_ = 0;
1186};
1187
Qingsi Wang25ec8882019-11-15 12:33:05 -08001188class MockIceTransport : public webrtc::IceTransportInterface {
1189 public:
1190 MockIceTransport(const std::string& name, int component)
1191 : internal_(std::make_unique<cricket::FakeIceTransport>(
1192 name,
1193 component,
1194 nullptr /* network_thread */)) {}
1195 ~MockIceTransport() = default;
1196 cricket::IceTransportInternal* internal() { return internal_.get(); }
1197
1198 private:
1199 std::unique_ptr<cricket::FakeIceTransport> internal_;
1200};
1201
1202class MockIceTransportFactory : public IceTransportFactory {
1203 public:
1204 ~MockIceTransportFactory() override = default;
1205 rtc::scoped_refptr<IceTransportInterface> CreateIceTransport(
1206 const std::string& transport_name,
1207 int component,
1208 IceTransportInit init) {
1209 RecordIceTransportCreated();
1210 return new rtc::RefCountedObject<MockIceTransport>(transport_name,
1211 component);
1212 }
Danil Chapovalov3a353122020-05-15 11:16:53 +02001213 MOCK_METHOD(void, RecordIceTransportCreated, ());
Qingsi Wang25ec8882019-11-15 12:33:05 -08001214};
1215
deadbeef1dcb1642017-03-29 21:08:16 -07001216// Tests two PeerConnections connecting to each other end-to-end, using a
1217// virtual network, fake A/V capture and fake encoder/decoders. The
1218// PeerConnections share the threads/socket servers, but use separate versions
1219// of everything else (including "PeerConnectionFactory"s).
Mirko Bonadei6a489f22019-04-09 15:11:12 +02001220class PeerConnectionIntegrationBaseTest : public ::testing::Test {
deadbeef1dcb1642017-03-29 21:08:16 -07001221 public:
Seth Hampson2f0d7022018-02-20 11:54:42 -08001222 explicit PeerConnectionIntegrationBaseTest(SdpSemantics sdp_semantics)
1223 : sdp_semantics_(sdp_semantics),
1224 ss_(new rtc::VirtualSocketServer()),
Steve Antonede9ca52017-10-16 13:04:27 -07001225 fss_(new rtc::FirewallSocketServer(ss_.get())),
1226 network_thread_(new rtc::Thread(fss_.get())),
Niels Möller2a707032020-06-16 16:39:13 +02001227 worker_thread_(rtc::Thread::Create()) {
Sebastian Jansson8a793a02018-03-13 15:21:48 +01001228 network_thread_->SetName("PCNetworkThread", this);
1229 worker_thread_->SetName("PCWorkerThread", this);
deadbeef1dcb1642017-03-29 21:08:16 -07001230 RTC_CHECK(network_thread_->Start());
1231 RTC_CHECK(worker_thread_->Start());
Qingsi Wang7fc821d2018-07-12 12:54:53 -07001232 webrtc::metrics::Reset();
deadbeef1dcb1642017-03-29 21:08:16 -07001233 }
1234
Seth Hampson2f0d7022018-02-20 11:54:42 -08001235 ~PeerConnectionIntegrationBaseTest() {
Seth Hampsonaed71642018-06-11 07:41:32 -07001236 // The PeerConnections should deleted before the TurnCustomizers.
1237 // A TurnPort is created with a raw pointer to a TurnCustomizer. The
1238 // TurnPort has the same lifetime as the PeerConnection, so it's expected
1239 // that the TurnCustomizer outlives the life of the PeerConnection or else
1240 // when Send() is called it will hit a seg fault.
deadbeef1dcb1642017-03-29 21:08:16 -07001241 if (caller_) {
1242 caller_->set_signaling_message_receiver(nullptr);
Seth Hampsonaed71642018-06-11 07:41:32 -07001243 delete SetCallerPcWrapperAndReturnCurrent(nullptr);
deadbeef1dcb1642017-03-29 21:08:16 -07001244 }
1245 if (callee_) {
1246 callee_->set_signaling_message_receiver(nullptr);
Seth Hampsonaed71642018-06-11 07:41:32 -07001247 delete SetCalleePcWrapperAndReturnCurrent(nullptr);
deadbeef1dcb1642017-03-29 21:08:16 -07001248 }
Seth Hampsonaed71642018-06-11 07:41:32 -07001249
1250 // If turn servers were created for the test they need to be destroyed on
1251 // the network thread.
1252 network_thread()->Invoke<void>(RTC_FROM_HERE, [this] {
1253 turn_servers_.clear();
1254 turn_customizers_.clear();
1255 });
deadbeef1dcb1642017-03-29 21:08:16 -07001256 }
1257
1258 bool SignalingStateStable() {
1259 return caller_->SignalingStateStable() && callee_->SignalingStateStable();
1260 }
1261
deadbeef71452802017-05-07 17:21:01 -07001262 bool DtlsConnected() {
Alex Loiko9289eda2018-11-23 16:18:59 +00001263 // TODO(deadbeef): kIceConnectionConnected currently means both ICE and DTLS
1264 // are connected. This is an important distinction. Once we have separate
1265 // ICE and DTLS state, this check needs to use the DTLS state.
1266 return (callee()->ice_connection_state() ==
1267 webrtc::PeerConnectionInterface::kIceConnectionConnected ||
1268 callee()->ice_connection_state() ==
1269 webrtc::PeerConnectionInterface::kIceConnectionCompleted) &&
1270 (caller()->ice_connection_state() ==
1271 webrtc::PeerConnectionInterface::kIceConnectionConnected ||
1272 caller()->ice_connection_state() ==
1273 webrtc::PeerConnectionInterface::kIceConnectionCompleted);
deadbeef71452802017-05-07 17:21:01 -07001274 }
1275
Qingsi Wang7685e862018-06-11 20:15:46 -07001276 // When |event_log_factory| is null, the default implementation of the event
1277 // log factory will be used.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001278 std::unique_ptr<PeerConnectionWrapper> CreatePeerConnectionWrapper(
1279 const std::string& debug_name,
Seth Hampson2f0d7022018-02-20 11:54:42 -08001280 const PeerConnectionFactory::Options* options,
1281 const RTCConfiguration* config,
Qingsi Wang7685e862018-06-11 20:15:46 -07001282 webrtc::PeerConnectionDependencies dependencies,
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001283 std::unique_ptr<webrtc::FakeRtcEventLogFactory> event_log_factory,
Johannes Kron3e983682020-03-29 22:17:00 +02001284 bool reset_encoder_factory,
1285 bool reset_decoder_factory) {
Seth Hampson2f0d7022018-02-20 11:54:42 -08001286 RTCConfiguration modified_config;
1287 if (config) {
1288 modified_config = *config;
1289 }
Steve Anton3acffc32018-04-12 17:21:03 -07001290 modified_config.sdp_semantics = sdp_semantics_;
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001291 if (!dependencies.cert_generator) {
1292 dependencies.cert_generator =
Mirko Bonadei317a1f02019-09-17 17:06:18 +02001293 std::make_unique<FakeRTCCertificateGenerator>();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001294 }
1295 std::unique_ptr<PeerConnectionWrapper> client(
1296 new PeerConnectionWrapper(debug_name));
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001297
Niels Möllerf06f9232018-08-07 12:32:18 +02001298 if (!client->Init(options, &modified_config, std::move(dependencies),
1299 network_thread_.get(), worker_thread_.get(),
Niels Möller2a707032020-06-16 16:39:13 +02001300 std::move(event_log_factory), reset_encoder_factory,
Johannes Kron3e983682020-03-29 22:17:00 +02001301 reset_decoder_factory)) {
Seth Hampson2f0d7022018-02-20 11:54:42 -08001302 return nullptr;
1303 }
1304 return client;
1305 }
1306
Qingsi Wang7685e862018-06-11 20:15:46 -07001307 std::unique_ptr<PeerConnectionWrapper>
1308 CreatePeerConnectionWrapperWithFakeRtcEventLog(
1309 const std::string& debug_name,
Qingsi Wang7685e862018-06-11 20:15:46 -07001310 const PeerConnectionFactory::Options* options,
1311 const RTCConfiguration* config,
1312 webrtc::PeerConnectionDependencies dependencies) {
1313 std::unique_ptr<webrtc::FakeRtcEventLogFactory> event_log_factory(
1314 new webrtc::FakeRtcEventLogFactory(rtc::Thread::Current()));
Niels Möller2a707032020-06-16 16:39:13 +02001315 return CreatePeerConnectionWrapper(debug_name, options, config,
1316 std::move(dependencies),
1317 std::move(event_log_factory),
1318 /*reset_encoder_factory=*/false,
1319 /*reset_decoder_factory=*/false);
Qingsi Wang7685e862018-06-11 20:15:46 -07001320 }
1321
deadbeef1dcb1642017-03-29 21:08:16 -07001322 bool CreatePeerConnectionWrappers() {
1323 return CreatePeerConnectionWrappersWithConfig(
1324 PeerConnectionInterface::RTCConfiguration(),
1325 PeerConnectionInterface::RTCConfiguration());
1326 }
1327
Steve Anton3acffc32018-04-12 17:21:03 -07001328 bool CreatePeerConnectionWrappersWithSdpSemantics(
1329 SdpSemantics caller_semantics,
1330 SdpSemantics callee_semantics) {
1331 // Can't specify the sdp_semantics in the passed-in configuration since it
1332 // will be overwritten by CreatePeerConnectionWrapper with whatever is
1333 // stored in sdp_semantics_. So get around this by modifying the instance
1334 // variable before calling CreatePeerConnectionWrapper for the caller and
1335 // callee PeerConnections.
1336 SdpSemantics original_semantics = sdp_semantics_;
1337 sdp_semantics_ = caller_semantics;
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001338 caller_ = CreatePeerConnectionWrapper(
Niels Möllerf06f9232018-08-07 12:32:18 +02001339 "Caller", nullptr, nullptr, webrtc::PeerConnectionDependencies(nullptr),
Niels Möller2a707032020-06-16 16:39:13 +02001340 nullptr,
Johannes Kron3e983682020-03-29 22:17:00 +02001341 /*reset_encoder_factory=*/false,
1342 /*reset_decoder_factory=*/false);
Steve Anton3acffc32018-04-12 17:21:03 -07001343 sdp_semantics_ = callee_semantics;
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001344 callee_ = CreatePeerConnectionWrapper(
Niels Möllerf06f9232018-08-07 12:32:18 +02001345 "Callee", nullptr, nullptr, webrtc::PeerConnectionDependencies(nullptr),
Niels Möller2a707032020-06-16 16:39:13 +02001346 nullptr,
Johannes Kron3e983682020-03-29 22:17:00 +02001347 /*reset_encoder_factory=*/false,
1348 /*reset_decoder_factory=*/false);
Steve Anton3acffc32018-04-12 17:21:03 -07001349 sdp_semantics_ = original_semantics;
1350 return caller_ && callee_;
1351 }
1352
deadbeef1dcb1642017-03-29 21:08:16 -07001353 bool CreatePeerConnectionWrappersWithConfig(
1354 const PeerConnectionInterface::RTCConfiguration& caller_config,
1355 const PeerConnectionInterface::RTCConfiguration& callee_config) {
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001356 caller_ = CreatePeerConnectionWrapper(
Niels Möllerf06f9232018-08-07 12:32:18 +02001357 "Caller", nullptr, &caller_config,
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001358 webrtc::PeerConnectionDependencies(nullptr), nullptr,
Niels Möller2a707032020-06-16 16:39:13 +02001359 /*reset_encoder_factory=*/false,
Johannes Kron3e983682020-03-29 22:17:00 +02001360 /*reset_decoder_factory=*/false);
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001361 callee_ = CreatePeerConnectionWrapper(
Niels Möllerf06f9232018-08-07 12:32:18 +02001362 "Callee", nullptr, &callee_config,
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001363 webrtc::PeerConnectionDependencies(nullptr), nullptr,
Niels Möller2a707032020-06-16 16:39:13 +02001364 /*reset_encoder_factory=*/false,
Johannes Kron3e983682020-03-29 22:17:00 +02001365 /*reset_decoder_factory=*/false);
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001366 return caller_ && callee_;
1367 }
1368
1369 bool CreatePeerConnectionWrappersWithConfigAndDeps(
1370 const PeerConnectionInterface::RTCConfiguration& caller_config,
1371 webrtc::PeerConnectionDependencies caller_dependencies,
1372 const PeerConnectionInterface::RTCConfiguration& callee_config,
1373 webrtc::PeerConnectionDependencies callee_dependencies) {
Niels Möller2a707032020-06-16 16:39:13 +02001374 caller_ =
1375 CreatePeerConnectionWrapper("Caller", nullptr, &caller_config,
1376 std::move(caller_dependencies), nullptr,
1377 /*reset_encoder_factory=*/false,
1378 /*reset_decoder_factory=*/false);
1379 callee_ =
1380 CreatePeerConnectionWrapper("Callee", nullptr, &callee_config,
1381 std::move(callee_dependencies), nullptr,
1382 /*reset_encoder_factory=*/false,
1383 /*reset_decoder_factory=*/false);
deadbeef1dcb1642017-03-29 21:08:16 -07001384 return caller_ && callee_;
1385 }
1386
1387 bool CreatePeerConnectionWrappersWithOptions(
1388 const PeerConnectionFactory::Options& caller_options,
1389 const PeerConnectionFactory::Options& callee_options) {
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001390 caller_ = CreatePeerConnectionWrapper(
Niels Möllerf06f9232018-08-07 12:32:18 +02001391 "Caller", &caller_options, nullptr,
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001392 webrtc::PeerConnectionDependencies(nullptr), nullptr,
Niels Möller2a707032020-06-16 16:39:13 +02001393 /*reset_encoder_factory=*/false,
Johannes Kron3e983682020-03-29 22:17:00 +02001394 /*reset_decoder_factory=*/false);
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001395 callee_ = CreatePeerConnectionWrapper(
Niels Möllerf06f9232018-08-07 12:32:18 +02001396 "Callee", &callee_options, nullptr,
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001397 webrtc::PeerConnectionDependencies(nullptr), nullptr,
Niels Möller2a707032020-06-16 16:39:13 +02001398 /*reset_encoder_factory=*/false,
Johannes Kron3e983682020-03-29 22:17:00 +02001399 /*reset_decoder_factory=*/false);
Qingsi Wang7685e862018-06-11 20:15:46 -07001400 return caller_ && callee_;
1401 }
1402
1403 bool CreatePeerConnectionWrappersWithFakeRtcEventLog() {
1404 PeerConnectionInterface::RTCConfiguration default_config;
1405 caller_ = CreatePeerConnectionWrapperWithFakeRtcEventLog(
Niels Möllerf06f9232018-08-07 12:32:18 +02001406 "Caller", nullptr, &default_config,
Qingsi Wang7685e862018-06-11 20:15:46 -07001407 webrtc::PeerConnectionDependencies(nullptr));
1408 callee_ = CreatePeerConnectionWrapperWithFakeRtcEventLog(
Niels Möllerf06f9232018-08-07 12:32:18 +02001409 "Callee", nullptr, &default_config,
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001410 webrtc::PeerConnectionDependencies(nullptr));
deadbeef1dcb1642017-03-29 21:08:16 -07001411 return caller_ && callee_;
1412 }
1413
Seth Hampson2f0d7022018-02-20 11:54:42 -08001414 std::unique_ptr<PeerConnectionWrapper>
1415 CreatePeerConnectionWrapperWithAlternateKey() {
deadbeef1dcb1642017-03-29 21:08:16 -07001416 std::unique_ptr<FakeRTCCertificateGenerator> cert_generator(
1417 new FakeRTCCertificateGenerator());
1418 cert_generator->use_alternate_key();
1419
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001420 webrtc::PeerConnectionDependencies dependencies(nullptr);
1421 dependencies.cert_generator = std::move(cert_generator);
Niels Möller2a707032020-06-16 16:39:13 +02001422 return CreatePeerConnectionWrapper("New Peer", nullptr, nullptr,
1423 std::move(dependencies), nullptr,
1424 /*reset_encoder_factory=*/false,
1425 /*reset_decoder_factory=*/false);
Johannes Kron3e983682020-03-29 22:17:00 +02001426 }
1427
1428 bool CreateOneDirectionalPeerConnectionWrappers(bool caller_to_callee) {
1429 caller_ = CreatePeerConnectionWrapper(
1430 "Caller", nullptr, nullptr, webrtc::PeerConnectionDependencies(nullptr),
Niels Möller2a707032020-06-16 16:39:13 +02001431 nullptr,
Johannes Kron3e983682020-03-29 22:17:00 +02001432 /*reset_encoder_factory=*/!caller_to_callee,
1433 /*reset_decoder_factory=*/caller_to_callee);
1434 callee_ = CreatePeerConnectionWrapper(
1435 "Callee", nullptr, nullptr, webrtc::PeerConnectionDependencies(nullptr),
Niels Möller2a707032020-06-16 16:39:13 +02001436 nullptr,
Johannes Kron3e983682020-03-29 22:17:00 +02001437 /*reset_encoder_factory=*/caller_to_callee,
1438 /*reset_decoder_factory=*/!caller_to_callee);
1439 return caller_ && callee_;
deadbeef1dcb1642017-03-29 21:08:16 -07001440 }
1441
Seth Hampsonaed71642018-06-11 07:41:32 -07001442 cricket::TestTurnServer* CreateTurnServer(
1443 rtc::SocketAddress internal_address,
1444 rtc::SocketAddress external_address,
1445 cricket::ProtocolType type = cricket::ProtocolType::PROTO_UDP,
1446 const std::string& common_name = "test turn server") {
1447 rtc::Thread* thread = network_thread();
1448 std::unique_ptr<cricket::TestTurnServer> turn_server =
1449 network_thread()->Invoke<std::unique_ptr<cricket::TestTurnServer>>(
1450 RTC_FROM_HERE,
1451 [thread, internal_address, external_address, type, common_name] {
Mirko Bonadei317a1f02019-09-17 17:06:18 +02001452 return std::make_unique<cricket::TestTurnServer>(
Seth Hampsonaed71642018-06-11 07:41:32 -07001453 thread, internal_address, external_address, type,
1454 /*ignore_bad_certs=*/true, common_name);
1455 });
1456 turn_servers_.push_back(std::move(turn_server));
1457 // Interactions with the turn server should be done on the network thread.
1458 return turn_servers_.back().get();
1459 }
1460
1461 cricket::TestTurnCustomizer* CreateTurnCustomizer() {
1462 std::unique_ptr<cricket::TestTurnCustomizer> turn_customizer =
1463 network_thread()->Invoke<std::unique_ptr<cricket::TestTurnCustomizer>>(
1464 RTC_FROM_HERE,
Mirko Bonadei317a1f02019-09-17 17:06:18 +02001465 [] { return std::make_unique<cricket::TestTurnCustomizer>(); });
Seth Hampsonaed71642018-06-11 07:41:32 -07001466 turn_customizers_.push_back(std::move(turn_customizer));
1467 // Interactions with the turn customizer should be done on the network
1468 // thread.
1469 return turn_customizers_.back().get();
1470 }
1471
1472 // Checks that the function counters for a TestTurnCustomizer are greater than
1473 // 0.
1474 void ExpectTurnCustomizerCountersIncremented(
1475 cricket::TestTurnCustomizer* turn_customizer) {
1476 unsigned int allow_channel_data_counter =
1477 network_thread()->Invoke<unsigned int>(
1478 RTC_FROM_HERE, [turn_customizer] {
1479 return turn_customizer->allow_channel_data_cnt_;
1480 });
1481 EXPECT_GT(allow_channel_data_counter, 0u);
1482 unsigned int modify_counter = network_thread()->Invoke<unsigned int>(
1483 RTC_FROM_HERE,
1484 [turn_customizer] { return turn_customizer->modify_cnt_; });
1485 EXPECT_GT(modify_counter, 0u);
1486 }
1487
deadbeef1dcb1642017-03-29 21:08:16 -07001488 // Once called, SDP blobs and ICE candidates will be automatically signaled
1489 // between PeerConnections.
1490 void ConnectFakeSignaling() {
1491 caller_->set_signaling_message_receiver(callee_.get());
1492 callee_->set_signaling_message_receiver(caller_.get());
1493 }
1494
Steve Antonede9ca52017-10-16 13:04:27 -07001495 // Once called, SDP blobs will be automatically signaled between
1496 // PeerConnections. Note that ICE candidates will not be signaled unless they
1497 // are in the exchanged SDP blobs.
1498 void ConnectFakeSignalingForSdpOnly() {
1499 ConnectFakeSignaling();
1500 SetSignalIceCandidates(false);
1501 }
1502
deadbeef1dcb1642017-03-29 21:08:16 -07001503 void SetSignalingDelayMs(int delay_ms) {
1504 caller_->set_signaling_delay_ms(delay_ms);
1505 callee_->set_signaling_delay_ms(delay_ms);
1506 }
1507
Steve Antonede9ca52017-10-16 13:04:27 -07001508 void SetSignalIceCandidates(bool signal) {
1509 caller_->set_signal_ice_candidates(signal);
1510 callee_->set_signal_ice_candidates(signal);
1511 }
1512
deadbeef1dcb1642017-03-29 21:08:16 -07001513 // Messages may get lost on the unreliable DataChannel, so we send multiple
1514 // times to avoid test flakiness.
1515 void SendRtpDataWithRetries(webrtc::DataChannelInterface* dc,
1516 const std::string& data,
1517 int retries) {
1518 for (int i = 0; i < retries; ++i) {
1519 dc->Send(DataBuffer(data));
1520 }
1521 }
1522
1523 rtc::Thread* network_thread() { return network_thread_.get(); }
1524
1525 rtc::VirtualSocketServer* virtual_socket_server() { return ss_.get(); }
1526
1527 PeerConnectionWrapper* caller() { return caller_.get(); }
1528
1529 // Set the |caller_| to the |wrapper| passed in and return the
1530 // original |caller_|.
1531 PeerConnectionWrapper* SetCallerPcWrapperAndReturnCurrent(
1532 PeerConnectionWrapper* wrapper) {
1533 PeerConnectionWrapper* old = caller_.release();
1534 caller_.reset(wrapper);
1535 return old;
1536 }
1537
1538 PeerConnectionWrapper* callee() { return callee_.get(); }
1539
1540 // Set the |callee_| to the |wrapper| passed in and return the
1541 // original |callee_|.
1542 PeerConnectionWrapper* SetCalleePcWrapperAndReturnCurrent(
1543 PeerConnectionWrapper* wrapper) {
1544 PeerConnectionWrapper* old = callee_.release();
1545 callee_.reset(wrapper);
1546 return old;
1547 }
1548
Qingsi Wang1dac6d82018-12-12 15:28:47 -08001549 void SetPortAllocatorFlags(uint32_t caller_flags, uint32_t callee_flags) {
1550 network_thread()->Invoke<void>(
1551 RTC_FROM_HERE, rtc::Bind(&cricket::PortAllocator::set_flags,
1552 caller()->port_allocator(), caller_flags));
1553 network_thread()->Invoke<void>(
1554 RTC_FROM_HERE, rtc::Bind(&cricket::PortAllocator::set_flags,
1555 callee()->port_allocator(), callee_flags));
1556 }
1557
Steve Antonede9ca52017-10-16 13:04:27 -07001558 rtc::FirewallSocketServer* firewall() const { return fss_.get(); }
1559
Seth Hampson2f0d7022018-02-20 11:54:42 -08001560 // Expects the provided number of new frames to be received within
1561 // kMaxWaitForFramesMs. The new expected frames are specified in
1562 // |media_expectations|. Returns false if any of the expectations were
1563 // not met.
1564 bool ExpectNewFrames(const MediaExpectations& media_expectations) {
Harald Alvestrand6060df52020-08-11 09:54:02 +02001565 // Make sure there are no bogus tracks confusing the issue.
1566 caller()->RemoveUnusedVideoRenderers();
1567 callee()->RemoveUnusedVideoRenderers();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001568 // First initialize the expected frame counts based upon the current
1569 // frame count.
1570 int total_caller_audio_frames_expected = caller()->audio_frames_received();
1571 if (media_expectations.caller_audio_expectation_ ==
1572 MediaExpectations::kExpectSomeFrames) {
1573 total_caller_audio_frames_expected +=
1574 media_expectations.caller_audio_frames_expected_;
1575 }
1576 int total_caller_video_frames_expected =
deadbeef1dcb1642017-03-29 21:08:16 -07001577 caller()->min_video_frames_received_per_track();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001578 if (media_expectations.caller_video_expectation_ ==
1579 MediaExpectations::kExpectSomeFrames) {
1580 total_caller_video_frames_expected +=
1581 media_expectations.caller_video_frames_expected_;
1582 }
1583 int total_callee_audio_frames_expected = callee()->audio_frames_received();
1584 if (media_expectations.callee_audio_expectation_ ==
1585 MediaExpectations::kExpectSomeFrames) {
1586 total_callee_audio_frames_expected +=
1587 media_expectations.callee_audio_frames_expected_;
1588 }
1589 int total_callee_video_frames_expected =
deadbeef1dcb1642017-03-29 21:08:16 -07001590 callee()->min_video_frames_received_per_track();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001591 if (media_expectations.callee_video_expectation_ ==
1592 MediaExpectations::kExpectSomeFrames) {
1593 total_callee_video_frames_expected +=
1594 media_expectations.callee_video_frames_expected_;
1595 }
deadbeef1dcb1642017-03-29 21:08:16 -07001596
Seth Hampson2f0d7022018-02-20 11:54:42 -08001597 // Wait for the expected frames.
deadbeef1dcb1642017-03-29 21:08:16 -07001598 EXPECT_TRUE_WAIT(caller()->audio_frames_received() >=
Seth Hampson2f0d7022018-02-20 11:54:42 -08001599 total_caller_audio_frames_expected &&
deadbeef1dcb1642017-03-29 21:08:16 -07001600 caller()->min_video_frames_received_per_track() >=
Seth Hampson2f0d7022018-02-20 11:54:42 -08001601 total_caller_video_frames_expected &&
deadbeef1dcb1642017-03-29 21:08:16 -07001602 callee()->audio_frames_received() >=
Seth Hampson2f0d7022018-02-20 11:54:42 -08001603 total_callee_audio_frames_expected &&
deadbeef1dcb1642017-03-29 21:08:16 -07001604 callee()->min_video_frames_received_per_track() >=
Seth Hampson2f0d7022018-02-20 11:54:42 -08001605 total_callee_video_frames_expected,
1606 kMaxWaitForFramesMs);
1607 bool expectations_correct =
1608 caller()->audio_frames_received() >=
1609 total_caller_audio_frames_expected &&
1610 caller()->min_video_frames_received_per_track() >=
1611 total_caller_video_frames_expected &&
1612 callee()->audio_frames_received() >=
1613 total_callee_audio_frames_expected &&
1614 callee()->min_video_frames_received_per_track() >=
1615 total_callee_video_frames_expected;
deadbeef1dcb1642017-03-29 21:08:16 -07001616
Seth Hampson2f0d7022018-02-20 11:54:42 -08001617 // After the combined wait, print out a more detailed message upon
1618 // failure.
deadbeef1dcb1642017-03-29 21:08:16 -07001619 EXPECT_GE(caller()->audio_frames_received(),
Seth Hampson2f0d7022018-02-20 11:54:42 -08001620 total_caller_audio_frames_expected);
deadbeef1dcb1642017-03-29 21:08:16 -07001621 EXPECT_GE(caller()->min_video_frames_received_per_track(),
Seth Hampson2f0d7022018-02-20 11:54:42 -08001622 total_caller_video_frames_expected);
deadbeef1dcb1642017-03-29 21:08:16 -07001623 EXPECT_GE(callee()->audio_frames_received(),
Seth Hampson2f0d7022018-02-20 11:54:42 -08001624 total_callee_audio_frames_expected);
deadbeef1dcb1642017-03-29 21:08:16 -07001625 EXPECT_GE(callee()->min_video_frames_received_per_track(),
Seth Hampson2f0d7022018-02-20 11:54:42 -08001626 total_callee_video_frames_expected);
1627
1628 // We want to make sure nothing unexpected was received.
1629 if (media_expectations.caller_audio_expectation_ ==
1630 MediaExpectations::kExpectNoFrames) {
1631 EXPECT_EQ(caller()->audio_frames_received(),
1632 total_caller_audio_frames_expected);
1633 if (caller()->audio_frames_received() !=
1634 total_caller_audio_frames_expected) {
1635 expectations_correct = false;
1636 }
1637 }
1638 if (media_expectations.caller_video_expectation_ ==
1639 MediaExpectations::kExpectNoFrames) {
1640 EXPECT_EQ(caller()->min_video_frames_received_per_track(),
1641 total_caller_video_frames_expected);
1642 if (caller()->min_video_frames_received_per_track() !=
1643 total_caller_video_frames_expected) {
1644 expectations_correct = false;
1645 }
1646 }
1647 if (media_expectations.callee_audio_expectation_ ==
1648 MediaExpectations::kExpectNoFrames) {
1649 EXPECT_EQ(callee()->audio_frames_received(),
1650 total_callee_audio_frames_expected);
1651 if (callee()->audio_frames_received() !=
1652 total_callee_audio_frames_expected) {
1653 expectations_correct = false;
1654 }
1655 }
1656 if (media_expectations.callee_video_expectation_ ==
1657 MediaExpectations::kExpectNoFrames) {
1658 EXPECT_EQ(callee()->min_video_frames_received_per_track(),
1659 total_callee_video_frames_expected);
1660 if (callee()->min_video_frames_received_per_track() !=
1661 total_callee_video_frames_expected) {
1662 expectations_correct = false;
1663 }
1664 }
1665 return expectations_correct;
deadbeef1dcb1642017-03-29 21:08:16 -07001666 }
1667
Steve Antond91969e2019-05-30 12:27:03 -07001668 void ClosePeerConnections() {
1669 caller()->pc()->Close();
1670 callee()->pc()->Close();
1671 }
1672
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07001673 void TestNegotiatedCipherSuite(
1674 const PeerConnectionFactory::Options& caller_options,
1675 const PeerConnectionFactory::Options& callee_options,
1676 int expected_cipher_suite) {
deadbeef1dcb1642017-03-29 21:08:16 -07001677 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(caller_options,
1678 callee_options));
deadbeef1dcb1642017-03-29 21:08:16 -07001679 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001680 caller()->AddAudioVideoTracks();
1681 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001682 caller()->CreateAndSetAndSignalOffer();
Qingsi Wang7fc821d2018-07-12 12:54:53 -07001683 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07001684 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(expected_cipher_suite),
deadbeefd8ad7882017-04-18 16:01:17 -07001685 caller()->OldGetStats()->SrtpCipher(), kDefaultTimeout);
Qingsi Wang7fc821d2018-07-12 12:54:53 -07001686 // TODO(bugs.webrtc.org/9456): Fix it.
Ying Wangef3998f2019-12-09 13:06:53 +01001687 EXPECT_METRIC_EQ(1, webrtc::metrics::NumEvents(
1688 "WebRTC.PeerConnection.SrtpCryptoSuite.Audio",
1689 expected_cipher_suite));
deadbeef1dcb1642017-03-29 21:08:16 -07001690 }
1691
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07001692 void TestGcmNegotiationUsesCipherSuite(bool local_gcm_enabled,
1693 bool remote_gcm_enabled,
Philipp Hancke2ebbff82019-10-26 06:12:55 +02001694 bool aes_ctr_enabled,
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07001695 int expected_cipher_suite) {
1696 PeerConnectionFactory::Options caller_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07001697 caller_options.crypto_options.srtp.enable_gcm_crypto_suites =
1698 local_gcm_enabled;
Philipp Hancke2ebbff82019-10-26 06:12:55 +02001699 caller_options.crypto_options.srtp.enable_aes128_sha1_80_crypto_cipher =
1700 aes_ctr_enabled;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07001701 PeerConnectionFactory::Options callee_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07001702 callee_options.crypto_options.srtp.enable_gcm_crypto_suites =
1703 remote_gcm_enabled;
Philipp Hancke2ebbff82019-10-26 06:12:55 +02001704 callee_options.crypto_options.srtp.enable_aes128_sha1_80_crypto_cipher =
1705 aes_ctr_enabled;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07001706 TestNegotiatedCipherSuite(caller_options, callee_options,
1707 expected_cipher_suite);
1708 }
1709
Seth Hampson2f0d7022018-02-20 11:54:42 -08001710 protected:
Steve Anton3acffc32018-04-12 17:21:03 -07001711 SdpSemantics sdp_semantics_;
Seth Hampson2f0d7022018-02-20 11:54:42 -08001712
deadbeef1dcb1642017-03-29 21:08:16 -07001713 private:
1714 // |ss_| is used by |network_thread_| so it must be destroyed later.
deadbeef1dcb1642017-03-29 21:08:16 -07001715 std::unique_ptr<rtc::VirtualSocketServer> ss_;
Steve Antonede9ca52017-10-16 13:04:27 -07001716 std::unique_ptr<rtc::FirewallSocketServer> fss_;
deadbeef1dcb1642017-03-29 21:08:16 -07001717 // |network_thread_| and |worker_thread_| are used by both
1718 // |caller_| and |callee_| so they must be destroyed
1719 // later.
1720 std::unique_ptr<rtc::Thread> network_thread_;
1721 std::unique_ptr<rtc::Thread> worker_thread_;
Seth Hampsonaed71642018-06-11 07:41:32 -07001722 // The turn servers and turn customizers should be accessed & deleted on the
1723 // network thread to avoid a race with the socket read/write that occurs
1724 // on the network thread.
1725 std::vector<std::unique_ptr<cricket::TestTurnServer>> turn_servers_;
1726 std::vector<std::unique_ptr<cricket::TestTurnCustomizer>> turn_customizers_;
deadbeef1dcb1642017-03-29 21:08:16 -07001727 std::unique_ptr<PeerConnectionWrapper> caller_;
1728 std::unique_ptr<PeerConnectionWrapper> callee_;
1729};
1730
Seth Hampson2f0d7022018-02-20 11:54:42 -08001731class PeerConnectionIntegrationTest
1732 : public PeerConnectionIntegrationBaseTest,
1733 public ::testing::WithParamInterface<SdpSemantics> {
1734 protected:
1735 PeerConnectionIntegrationTest()
1736 : PeerConnectionIntegrationBaseTest(GetParam()) {}
1737};
1738
Yves Gerey100fe632020-01-17 19:15:53 +01001739// Fake clock must be set before threads are started to prevent race on
1740// Set/GetClockForTesting().
1741// To achieve that, multiple inheritance is used as a mixin pattern
1742// where order of construction is finely controlled.
1743// This also ensures peerconnection is closed before switching back to non-fake
1744// clock, avoiding other races and DCHECK failures such as in rtp_sender.cc.
1745class FakeClockForTest : public rtc::ScopedFakeClock {
1746 protected:
1747 FakeClockForTest() {
1748 // Some things use a time of "0" as a special value, so we need to start out
1749 // the fake clock at a nonzero time.
1750 // TODO(deadbeef): Fix this.
Danil Chapovalov0c626af2020-02-10 11:16:00 +01001751 AdvanceTime(webrtc::TimeDelta::Seconds(1));
Yves Gerey100fe632020-01-17 19:15:53 +01001752 }
1753
1754 // Explicit handle.
1755 ScopedFakeClock& FakeClock() { return *this; }
1756};
1757
1758// Ensure FakeClockForTest is constructed first (see class for rationale).
1759class PeerConnectionIntegrationTestWithFakeClock
1760 : public FakeClockForTest,
1761 public PeerConnectionIntegrationTest {};
1762
Seth Hampson2f0d7022018-02-20 11:54:42 -08001763class PeerConnectionIntegrationTestPlanB
1764 : public PeerConnectionIntegrationBaseTest {
1765 protected:
1766 PeerConnectionIntegrationTestPlanB()
1767 : PeerConnectionIntegrationBaseTest(SdpSemantics::kPlanB) {}
1768};
1769
1770class PeerConnectionIntegrationTestUnifiedPlan
1771 : public PeerConnectionIntegrationBaseTest {
1772 protected:
1773 PeerConnectionIntegrationTestUnifiedPlan()
1774 : PeerConnectionIntegrationBaseTest(SdpSemantics::kUnifiedPlan) {}
1775};
1776
deadbeef1dcb1642017-03-29 21:08:16 -07001777// Test the OnFirstPacketReceived callback from audio/video RtpReceivers. This
1778// includes testing that the callback is invoked if an observer is connected
1779// after the first packet has already been received.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001780TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07001781 RtpReceiverObserverOnFirstPacketReceived) {
1782 ASSERT_TRUE(CreatePeerConnectionWrappers());
1783 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001784 caller()->AddAudioVideoTracks();
1785 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001786 // Start offer/answer exchange and wait for it to complete.
1787 caller()->CreateAndSetAndSignalOffer();
1788 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1789 // Should be one receiver each for audio/video.
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02001790 EXPECT_EQ(2U, caller()->rtp_receiver_observers().size());
1791 EXPECT_EQ(2U, callee()->rtp_receiver_observers().size());
deadbeef1dcb1642017-03-29 21:08:16 -07001792 // Wait for all "first packet received" callbacks to be fired.
1793 EXPECT_TRUE_WAIT(
Steve Anton64b626b2019-01-28 17:25:26 -08001794 absl::c_all_of(caller()->rtp_receiver_observers(),
1795 [](const std::unique_ptr<MockRtpReceiverObserver>& o) {
1796 return o->first_packet_received();
1797 }),
deadbeef1dcb1642017-03-29 21:08:16 -07001798 kMaxWaitForFramesMs);
1799 EXPECT_TRUE_WAIT(
Steve Anton64b626b2019-01-28 17:25:26 -08001800 absl::c_all_of(callee()->rtp_receiver_observers(),
1801 [](const std::unique_ptr<MockRtpReceiverObserver>& o) {
1802 return o->first_packet_received();
1803 }),
deadbeef1dcb1642017-03-29 21:08:16 -07001804 kMaxWaitForFramesMs);
1805 // If new observers are set after the first packet was already received, the
1806 // callback should still be invoked.
1807 caller()->ResetRtpReceiverObservers();
1808 callee()->ResetRtpReceiverObservers();
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02001809 EXPECT_EQ(2U, caller()->rtp_receiver_observers().size());
1810 EXPECT_EQ(2U, callee()->rtp_receiver_observers().size());
deadbeef1dcb1642017-03-29 21:08:16 -07001811 EXPECT_TRUE(
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 EXPECT_TRUE(
Steve Anton64b626b2019-01-28 17:25:26 -08001817 absl::c_all_of(callee()->rtp_receiver_observers(),
1818 [](const std::unique_ptr<MockRtpReceiverObserver>& o) {
1819 return o->first_packet_received();
1820 }));
deadbeef1dcb1642017-03-29 21:08:16 -07001821}
1822
1823class DummyDtmfObserver : public DtmfSenderObserverInterface {
1824 public:
1825 DummyDtmfObserver() : completed_(false) {}
1826
1827 // Implements DtmfSenderObserverInterface.
1828 void OnToneChange(const std::string& tone) override {
1829 tones_.push_back(tone);
1830 if (tone.empty()) {
1831 completed_ = true;
1832 }
1833 }
1834
1835 const std::vector<std::string>& tones() const { return tones_; }
1836 bool completed() const { return completed_; }
1837
1838 private:
1839 bool completed_;
1840 std::vector<std::string> tones_;
1841};
1842
1843// Assumes |sender| already has an audio track added and the offer/answer
1844// exchange is done.
1845void TestDtmfFromSenderToReceiver(PeerConnectionWrapper* sender,
1846 PeerConnectionWrapper* receiver) {
Steve Anton15324772018-01-16 10:26:49 -08001847 // We should be able to get a DTMF sender from the local sender.
1848 rtc::scoped_refptr<DtmfSenderInterface> dtmf_sender =
1849 sender->pc()->GetSenders().at(0)->GetDtmfSender();
1850 ASSERT_TRUE(dtmf_sender);
deadbeef1dcb1642017-03-29 21:08:16 -07001851 DummyDtmfObserver observer;
deadbeef1dcb1642017-03-29 21:08:16 -07001852 dtmf_sender->RegisterObserver(&observer);
1853
1854 // Test the DtmfSender object just created.
1855 EXPECT_TRUE(dtmf_sender->CanInsertDtmf());
1856 EXPECT_TRUE(dtmf_sender->InsertDtmf("1a", 100, 50));
1857
1858 EXPECT_TRUE_WAIT(observer.completed(), kDefaultTimeout);
1859 std::vector<std::string> tones = {"1", "a", ""};
1860 EXPECT_EQ(tones, observer.tones());
1861 dtmf_sender->UnregisterObserver();
1862 // TODO(deadbeef): Verify the tones were actually received end-to-end.
1863}
1864
1865// Verifies the DtmfSenderObserver callbacks for a DtmfSender (one in each
1866// direction).
Seth Hampson2f0d7022018-02-20 11:54:42 -08001867TEST_P(PeerConnectionIntegrationTest, DtmfSenderObserver) {
deadbeef1dcb1642017-03-29 21:08:16 -07001868 ASSERT_TRUE(CreatePeerConnectionWrappers());
1869 ConnectFakeSignaling();
1870 // Only need audio for DTMF.
Steve Anton15324772018-01-16 10:26:49 -08001871 caller()->AddAudioTrack();
1872 callee()->AddAudioTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07001873 caller()->CreateAndSetAndSignalOffer();
1874 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
deadbeef71452802017-05-07 17:21:01 -07001875 // DTLS must finish before the DTMF sender can be used reliably.
1876 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07001877 TestDtmfFromSenderToReceiver(caller(), callee());
1878 TestDtmfFromSenderToReceiver(callee(), caller());
1879}
1880
1881// Basic end-to-end test, verifying media can be encoded/transmitted/decoded
1882// between two connections, using DTLS-SRTP.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001883TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithDtls) {
deadbeef1dcb1642017-03-29 21:08:16 -07001884 ASSERT_TRUE(CreatePeerConnectionWrappers());
1885 ConnectFakeSignaling();
Harald Alvestrand194939b2018-01-24 16:04:13 +01001886
deadbeef1dcb1642017-03-29 21:08:16 -07001887 // Do normal offer/answer and wait for some frames to be received in each
1888 // direction.
Steve Anton15324772018-01-16 10:26:49 -08001889 caller()->AddAudioVideoTracks();
1890 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001891 caller()->CreateAndSetAndSignalOffer();
1892 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001893 MediaExpectations media_expectations;
1894 media_expectations.ExpectBidirectionalAudioAndVideo();
1895 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Ying Wangef3998f2019-12-09 13:06:53 +01001896 EXPECT_METRIC_LE(
1897 2, webrtc::metrics::NumEvents("WebRTC.PeerConnection.KeyProtocol",
1898 webrtc::kEnumCounterKeyProtocolDtls));
1899 EXPECT_METRIC_EQ(
1900 0, webrtc::metrics::NumEvents("WebRTC.PeerConnection.KeyProtocol",
1901 webrtc::kEnumCounterKeyProtocolSdes));
deadbeef1dcb1642017-03-29 21:08:16 -07001902}
1903
1904// Uses SDES instead of DTLS for key agreement.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001905TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithSdes) {
deadbeef1dcb1642017-03-29 21:08:16 -07001906 PeerConnectionInterface::RTCConfiguration sdes_config;
1907 sdes_config.enable_dtls_srtp.emplace(false);
1908 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(sdes_config, sdes_config));
1909 ConnectFakeSignaling();
1910
1911 // Do normal offer/answer and wait for some frames to be received in each
1912 // direction.
Steve Anton15324772018-01-16 10:26:49 -08001913 caller()->AddAudioVideoTracks();
1914 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001915 caller()->CreateAndSetAndSignalOffer();
1916 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001917 MediaExpectations media_expectations;
1918 media_expectations.ExpectBidirectionalAudioAndVideo();
1919 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Ying Wangef3998f2019-12-09 13:06:53 +01001920 EXPECT_METRIC_LE(
1921 2, webrtc::metrics::NumEvents("WebRTC.PeerConnection.KeyProtocol",
1922 webrtc::kEnumCounterKeyProtocolSdes));
1923 EXPECT_METRIC_EQ(
1924 0, webrtc::metrics::NumEvents("WebRTC.PeerConnection.KeyProtocol",
1925 webrtc::kEnumCounterKeyProtocolDtls));
deadbeef1dcb1642017-03-29 21:08:16 -07001926}
1927
Steve Anton9a44b2d2019-07-12 12:58:30 -07001928// Basic end-to-end test specifying the |enable_encrypted_rtp_header_extensions|
1929// option to offer encrypted versions of all header extensions alongside the
1930// unencrypted versions.
1931TEST_P(PeerConnectionIntegrationTest,
1932 EndToEndCallWithEncryptedRtpHeaderExtensions) {
1933 CryptoOptions crypto_options;
1934 crypto_options.srtp.enable_encrypted_rtp_header_extensions = true;
1935 PeerConnectionInterface::RTCConfiguration config;
1936 config.crypto_options = crypto_options;
1937 // Note: This allows offering >14 RTP header extensions.
1938 config.offer_extmap_allow_mixed = true;
1939 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
1940 ConnectFakeSignaling();
1941
1942 // Do normal offer/answer and wait for some frames to be received in each
1943 // direction.
1944 caller()->AddAudioVideoTracks();
1945 callee()->AddAudioVideoTracks();
1946 caller()->CreateAndSetAndSignalOffer();
1947 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1948 MediaExpectations media_expectations;
1949 media_expectations.ExpectBidirectionalAudioAndVideo();
1950 ASSERT_TRUE(ExpectNewFrames(media_expectations));
1951}
1952
Steve Anton8c0f7a72017-10-03 10:03:10 -07001953// Tests that the GetRemoteAudioSSLCertificate method returns the remote DTLS
1954// certificate once the DTLS handshake has finished.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001955TEST_P(PeerConnectionIntegrationTest,
Steve Anton8c0f7a72017-10-03 10:03:10 -07001956 GetRemoteAudioSSLCertificateReturnsExchangedCertificate) {
1957 auto GetRemoteAudioSSLCertificate = [](PeerConnectionWrapper* wrapper) {
1958 auto pci = reinterpret_cast<PeerConnectionProxy*>(wrapper->pc());
1959 auto pc = reinterpret_cast<PeerConnection*>(pci->internal());
1960 return pc->GetRemoteAudioSSLCertificate();
1961 };
Zhi Huang70b820f2018-01-27 14:16:15 -08001962 auto GetRemoteAudioSSLCertChain = [](PeerConnectionWrapper* wrapper) {
1963 auto pci = reinterpret_cast<PeerConnectionProxy*>(wrapper->pc());
1964 auto pc = reinterpret_cast<PeerConnection*>(pci->internal());
1965 return pc->GetRemoteAudioSSLCertChain();
1966 };
Steve Anton8c0f7a72017-10-03 10:03:10 -07001967
1968 auto caller_cert = rtc::RTCCertificate::FromPEM(kRsaPems[0]);
1969 auto callee_cert = rtc::RTCCertificate::FromPEM(kRsaPems[1]);
1970
1971 // Configure each side with a known certificate so they can be compared later.
1972 PeerConnectionInterface::RTCConfiguration caller_config;
1973 caller_config.enable_dtls_srtp.emplace(true);
1974 caller_config.certificates.push_back(caller_cert);
1975 PeerConnectionInterface::RTCConfiguration callee_config;
1976 callee_config.enable_dtls_srtp.emplace(true);
1977 callee_config.certificates.push_back(callee_cert);
1978 ASSERT_TRUE(
1979 CreatePeerConnectionWrappersWithConfig(caller_config, callee_config));
1980 ConnectFakeSignaling();
1981
1982 // When first initialized, there should not be a remote SSL certificate (and
1983 // calling this method should not crash).
1984 EXPECT_EQ(nullptr, GetRemoteAudioSSLCertificate(caller()));
1985 EXPECT_EQ(nullptr, GetRemoteAudioSSLCertificate(callee()));
Zhi Huang70b820f2018-01-27 14:16:15 -08001986 EXPECT_EQ(nullptr, GetRemoteAudioSSLCertChain(caller()));
1987 EXPECT_EQ(nullptr, GetRemoteAudioSSLCertChain(callee()));
Steve Anton8c0f7a72017-10-03 10:03:10 -07001988
Steve Anton15324772018-01-16 10:26:49 -08001989 caller()->AddAudioTrack();
1990 callee()->AddAudioTrack();
Steve Anton8c0f7a72017-10-03 10:03:10 -07001991 caller()->CreateAndSetAndSignalOffer();
1992 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1993 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
1994
1995 // Once DTLS has been connected, each side should return the other's SSL
1996 // certificate when calling GetRemoteAudioSSLCertificate.
1997
1998 auto caller_remote_cert = GetRemoteAudioSSLCertificate(caller());
1999 ASSERT_TRUE(caller_remote_cert);
Benjamin Wright6c6c9df2018-10-25 01:16:26 -07002000 EXPECT_EQ(callee_cert->GetSSLCertificate().ToPEMString(),
Steve Anton8c0f7a72017-10-03 10:03:10 -07002001 caller_remote_cert->ToPEMString());
2002
2003 auto callee_remote_cert = GetRemoteAudioSSLCertificate(callee());
2004 ASSERT_TRUE(callee_remote_cert);
Benjamin Wright6c6c9df2018-10-25 01:16:26 -07002005 EXPECT_EQ(caller_cert->GetSSLCertificate().ToPEMString(),
Steve Anton8c0f7a72017-10-03 10:03:10 -07002006 callee_remote_cert->ToPEMString());
Zhi Huang70b820f2018-01-27 14:16:15 -08002007
2008 auto caller_remote_cert_chain = GetRemoteAudioSSLCertChain(caller());
2009 ASSERT_TRUE(caller_remote_cert_chain);
2010 ASSERT_EQ(1U, caller_remote_cert_chain->GetSize());
2011 auto remote_cert = &caller_remote_cert_chain->Get(0);
Benjamin Wright6c6c9df2018-10-25 01:16:26 -07002012 EXPECT_EQ(callee_cert->GetSSLCertificate().ToPEMString(),
Zhi Huang70b820f2018-01-27 14:16:15 -08002013 remote_cert->ToPEMString());
2014
2015 auto callee_remote_cert_chain = GetRemoteAudioSSLCertChain(callee());
2016 ASSERT_TRUE(callee_remote_cert_chain);
2017 ASSERT_EQ(1U, callee_remote_cert_chain->GetSize());
2018 remote_cert = &callee_remote_cert_chain->Get(0);
Benjamin Wright6c6c9df2018-10-25 01:16:26 -07002019 EXPECT_EQ(caller_cert->GetSSLCertificate().ToPEMString(),
Zhi Huang70b820f2018-01-27 14:16:15 -08002020 remote_cert->ToPEMString());
Steve Anton8c0f7a72017-10-03 10:03:10 -07002021}
2022
deadbeef1dcb1642017-03-29 21:08:16 -07002023// This test sets up a call between two parties with a source resolution of
2024// 1280x720 and verifies that a 16:9 aspect ratio is received.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002025TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07002026 Send1280By720ResolutionAndReceive16To9AspectRatio) {
2027 ASSERT_TRUE(CreatePeerConnectionWrappers());
2028 ConnectFakeSignaling();
2029
Niels Möller5c7efe72018-05-11 10:34:46 +02002030 // Add video tracks with 16:9 aspect ratio, size 1280 x 720.
2031 webrtc::FakePeriodicVideoSource::Config config;
2032 config.width = 1280;
2033 config.height = 720;
Johannes Kron965e7942018-09-13 15:36:20 +02002034 config.timestamp_offset_ms = rtc::TimeMillis();
Niels Möller5c7efe72018-05-11 10:34:46 +02002035 caller()->AddTrack(caller()->CreateLocalVideoTrackWithConfig(config));
2036 callee()->AddTrack(callee()->CreateLocalVideoTrackWithConfig(config));
deadbeef1dcb1642017-03-29 21:08:16 -07002037
2038 // Do normal offer/answer and wait for at least one frame to be received in
2039 // each direction.
2040 caller()->CreateAndSetAndSignalOffer();
2041 ASSERT_TRUE_WAIT(caller()->min_video_frames_received_per_track() > 0 &&
2042 callee()->min_video_frames_received_per_track() > 0,
2043 kMaxWaitForFramesMs);
2044
2045 // Check rendered aspect ratio.
2046 EXPECT_EQ(16.0 / 9, caller()->local_rendered_aspect_ratio());
2047 EXPECT_EQ(16.0 / 9, caller()->rendered_aspect_ratio());
2048 EXPECT_EQ(16.0 / 9, callee()->local_rendered_aspect_ratio());
2049 EXPECT_EQ(16.0 / 9, callee()->rendered_aspect_ratio());
2050}
2051
2052// This test sets up an one-way call, with media only from caller to
2053// callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002054TEST_P(PeerConnectionIntegrationTest, OneWayMediaCall) {
deadbeef1dcb1642017-03-29 21:08:16 -07002055 ASSERT_TRUE(CreatePeerConnectionWrappers());
2056 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002057 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002058 caller()->CreateAndSetAndSignalOffer();
Seth Hampson2f0d7022018-02-20 11:54:42 -08002059 MediaExpectations media_expectations;
2060 media_expectations.CalleeExpectsSomeAudioAndVideo();
2061 media_expectations.CallerExpectsNoAudio();
2062 media_expectations.CallerExpectsNoVideo();
2063 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002064}
2065
Johannes Kron3e983682020-03-29 22:17:00 +02002066// Tests that send only works without the caller having a decoder factory and
2067// the callee having an encoder factory.
2068TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithSendOnlyVideo) {
2069 ASSERT_TRUE(
2070 CreateOneDirectionalPeerConnectionWrappers(/*caller_to_callee=*/true));
2071 ConnectFakeSignaling();
2072 // Add one-directional video, from caller to callee.
2073 rtc::scoped_refptr<webrtc::VideoTrackInterface> caller_track =
2074 caller()->CreateLocalVideoTrack();
2075 caller()->AddTrack(caller_track);
2076 PeerConnectionInterface::RTCOfferAnswerOptions options;
2077 options.offer_to_receive_video = 0;
2078 caller()->SetOfferAnswerOptions(options);
2079 caller()->CreateAndSetAndSignalOffer();
2080 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2081 ASSERT_EQ(callee()->pc()->GetReceivers().size(), 1u);
2082
2083 // Expect video to be received in one direction.
2084 MediaExpectations media_expectations;
2085 media_expectations.CallerExpectsNoVideo();
2086 media_expectations.CalleeExpectsSomeVideo();
2087
2088 EXPECT_TRUE(ExpectNewFrames(media_expectations));
2089}
2090
2091// Tests that receive only works without the caller having an encoder factory
2092// and the callee having a decoder factory.
2093TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithReceiveOnlyVideo) {
2094 ASSERT_TRUE(
2095 CreateOneDirectionalPeerConnectionWrappers(/*caller_to_callee=*/false));
2096 ConnectFakeSignaling();
2097 // Add one-directional video, from callee to caller.
2098 rtc::scoped_refptr<webrtc::VideoTrackInterface> callee_track =
2099 callee()->CreateLocalVideoTrack();
2100 callee()->AddTrack(callee_track);
2101 PeerConnectionInterface::RTCOfferAnswerOptions options;
2102 options.offer_to_receive_video = 1;
2103 caller()->SetOfferAnswerOptions(options);
2104 caller()->CreateAndSetAndSignalOffer();
2105 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2106 ASSERT_EQ(caller()->pc()->GetReceivers().size(), 1u);
2107
2108 // Expect video to be received in one direction.
2109 MediaExpectations media_expectations;
2110 media_expectations.CallerExpectsSomeVideo();
2111 media_expectations.CalleeExpectsNoVideo();
2112
2113 EXPECT_TRUE(ExpectNewFrames(media_expectations));
2114}
2115
2116TEST_P(PeerConnectionIntegrationTest,
2117 EndToEndCallAddReceiveVideoToSendOnlyCall) {
2118 ASSERT_TRUE(CreatePeerConnectionWrappers());
2119 ConnectFakeSignaling();
2120 // Add one-directional video, from caller to callee.
2121 rtc::scoped_refptr<webrtc::VideoTrackInterface> caller_track =
2122 caller()->CreateLocalVideoTrack();
2123 caller()->AddTrack(caller_track);
2124 caller()->CreateAndSetAndSignalOffer();
2125 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2126
2127 // Add receive video.
2128 rtc::scoped_refptr<webrtc::VideoTrackInterface> callee_track =
2129 callee()->CreateLocalVideoTrack();
2130 callee()->AddTrack(callee_track);
2131 caller()->CreateAndSetAndSignalOffer();
2132 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2133
2134 // Ensure that video frames are received end-to-end.
2135 MediaExpectations media_expectations;
2136 media_expectations.ExpectBidirectionalVideo();
2137 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2138}
2139
2140TEST_P(PeerConnectionIntegrationTest,
2141 EndToEndCallAddSendVideoToReceiveOnlyCall) {
2142 ASSERT_TRUE(CreatePeerConnectionWrappers());
2143 ConnectFakeSignaling();
2144 // Add one-directional video, from callee to caller.
2145 rtc::scoped_refptr<webrtc::VideoTrackInterface> callee_track =
2146 callee()->CreateLocalVideoTrack();
2147 callee()->AddTrack(callee_track);
2148 caller()->CreateAndSetAndSignalOffer();
2149 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2150
2151 // Add send video.
2152 rtc::scoped_refptr<webrtc::VideoTrackInterface> caller_track =
2153 caller()->CreateLocalVideoTrack();
2154 caller()->AddTrack(caller_track);
2155 caller()->CreateAndSetAndSignalOffer();
2156 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2157
2158 // Expect video to be received in one direction.
2159 MediaExpectations media_expectations;
2160 media_expectations.ExpectBidirectionalVideo();
2161 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2162}
2163
2164TEST_P(PeerConnectionIntegrationTest,
2165 EndToEndCallRemoveReceiveVideoFromSendReceiveCall) {
2166 ASSERT_TRUE(CreatePeerConnectionWrappers());
2167 ConnectFakeSignaling();
2168 // Add send video, from caller to callee.
2169 rtc::scoped_refptr<webrtc::VideoTrackInterface> caller_track =
2170 caller()->CreateLocalVideoTrack();
2171 rtc::scoped_refptr<webrtc::RtpSenderInterface> caller_sender =
2172 caller()->AddTrack(caller_track);
2173 // Add receive video, from callee to caller.
2174 rtc::scoped_refptr<webrtc::VideoTrackInterface> callee_track =
2175 callee()->CreateLocalVideoTrack();
2176
2177 rtc::scoped_refptr<webrtc::RtpSenderInterface> callee_sender =
2178 callee()->AddTrack(callee_track);
2179 caller()->CreateAndSetAndSignalOffer();
2180 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2181
2182 // Remove receive video (i.e., callee sender track).
2183 callee()->pc()->RemoveTrack(callee_sender);
2184
2185 caller()->CreateAndSetAndSignalOffer();
2186 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2187
2188 // Expect one-directional video.
2189 MediaExpectations media_expectations;
2190 media_expectations.CallerExpectsNoVideo();
2191 media_expectations.CalleeExpectsSomeVideo();
2192
2193 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2194}
2195
2196TEST_P(PeerConnectionIntegrationTest,
2197 EndToEndCallRemoveSendVideoFromSendReceiveCall) {
2198 ASSERT_TRUE(CreatePeerConnectionWrappers());
2199 ConnectFakeSignaling();
2200 // Add send video, from caller to callee.
2201 rtc::scoped_refptr<webrtc::VideoTrackInterface> caller_track =
2202 caller()->CreateLocalVideoTrack();
2203 rtc::scoped_refptr<webrtc::RtpSenderInterface> caller_sender =
2204 caller()->AddTrack(caller_track);
2205 // Add receive video, from callee to caller.
2206 rtc::scoped_refptr<webrtc::VideoTrackInterface> callee_track =
2207 callee()->CreateLocalVideoTrack();
2208
2209 rtc::scoped_refptr<webrtc::RtpSenderInterface> callee_sender =
2210 callee()->AddTrack(callee_track);
2211 caller()->CreateAndSetAndSignalOffer();
2212 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2213
2214 // Remove send video (i.e., caller sender track).
2215 caller()->pc()->RemoveTrack(caller_sender);
2216
2217 caller()->CreateAndSetAndSignalOffer();
2218 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2219
2220 // Expect one-directional video.
2221 MediaExpectations media_expectations;
2222 media_expectations.CalleeExpectsNoVideo();
2223 media_expectations.CallerExpectsSomeVideo();
2224
2225 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2226}
2227
deadbeef1dcb1642017-03-29 21:08:16 -07002228// This test sets up a audio call initially, with the callee rejecting video
2229// initially. Then later the callee decides to upgrade to audio/video, and
2230// initiates a new offer/answer exchange.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002231TEST_P(PeerConnectionIntegrationTest, AudioToVideoUpgrade) {
deadbeef1dcb1642017-03-29 21:08:16 -07002232 ASSERT_TRUE(CreatePeerConnectionWrappers());
2233 ConnectFakeSignaling();
2234 // Initially, offer an audio/video stream from the caller, but refuse to
2235 // send/receive video on the callee side.
Steve Anton15324772018-01-16 10:26:49 -08002236 caller()->AddAudioVideoTracks();
2237 callee()->AddAudioTrack();
Seth Hampson2f0d7022018-02-20 11:54:42 -08002238 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2239 PeerConnectionInterface::RTCOfferAnswerOptions options;
2240 options.offer_to_receive_video = 0;
2241 callee()->SetOfferAnswerOptions(options);
2242 } else {
2243 callee()->SetRemoteOfferHandler([this] {
Harald Alvestrand6060df52020-08-11 09:54:02 +02002244 callee()
2245 ->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)
2246 ->StopInternal();
Seth Hampson2f0d7022018-02-20 11:54:42 -08002247 });
2248 }
deadbeef1dcb1642017-03-29 21:08:16 -07002249 // Do offer/answer and make sure audio is still received end-to-end.
2250 caller()->CreateAndSetAndSignalOffer();
2251 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002252 {
2253 MediaExpectations media_expectations;
2254 media_expectations.ExpectBidirectionalAudio();
2255 media_expectations.ExpectNoVideo();
2256 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2257 }
deadbeef1dcb1642017-03-29 21:08:16 -07002258 // Sanity check that the callee's description has a rejected video section.
2259 ASSERT_NE(nullptr, callee()->pc()->local_description());
2260 const ContentInfo* callee_video_content =
2261 GetFirstVideoContent(callee()->pc()->local_description()->description());
2262 ASSERT_NE(nullptr, callee_video_content);
2263 EXPECT_TRUE(callee_video_content->rejected);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002264
deadbeef1dcb1642017-03-29 21:08:16 -07002265 // Now negotiate with video and ensure negotiation succeeds, with video
2266 // frames and additional audio frames being received.
Steve Anton15324772018-01-16 10:26:49 -08002267 callee()->AddVideoTrack();
Seth Hampson2f0d7022018-02-20 11:54:42 -08002268 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2269 PeerConnectionInterface::RTCOfferAnswerOptions options;
2270 options.offer_to_receive_video = 1;
2271 callee()->SetOfferAnswerOptions(options);
2272 } else {
2273 callee()->SetRemoteOfferHandler(nullptr);
2274 caller()->SetRemoteOfferHandler([this] {
2275 // The caller creates a new transceiver to receive video on when receiving
2276 // the offer, but by default it is send only.
2277 auto transceivers = caller()->pc()->GetTransceivers();
Harald Alvestrand6060df52020-08-11 09:54:02 +02002278 ASSERT_EQ(2U, transceivers.size());
Seth Hampson2f0d7022018-02-20 11:54:42 -08002279 ASSERT_EQ(cricket::MEDIA_TYPE_VIDEO,
Harald Alvestrand6060df52020-08-11 09:54:02 +02002280 transceivers[1]->receiver()->media_type());
2281 transceivers[1]->sender()->SetTrack(caller()->CreateLocalVideoTrack());
2282 transceivers[1]->SetDirectionWithError(
2283 RtpTransceiverDirection::kSendRecv);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002284 });
2285 }
deadbeef1dcb1642017-03-29 21:08:16 -07002286 callee()->CreateAndSetAndSignalOffer();
2287 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002288 {
2289 // Expect additional audio frames to be received after the upgrade.
2290 MediaExpectations media_expectations;
2291 media_expectations.ExpectBidirectionalAudioAndVideo();
2292 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2293 }
deadbeef1dcb1642017-03-29 21:08:16 -07002294}
2295
deadbeef4389b4d2017-09-07 09:07:36 -07002296// Simpler than the above test; just add an audio track to an established
2297// video-only connection.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002298TEST_P(PeerConnectionIntegrationTest, AddAudioToVideoOnlyCall) {
deadbeef4389b4d2017-09-07 09:07:36 -07002299 ASSERT_TRUE(CreatePeerConnectionWrappers());
2300 ConnectFakeSignaling();
2301 // Do initial offer/answer with just a video track.
Steve Anton15324772018-01-16 10:26:49 -08002302 caller()->AddVideoTrack();
2303 callee()->AddVideoTrack();
deadbeef4389b4d2017-09-07 09:07:36 -07002304 caller()->CreateAndSetAndSignalOffer();
2305 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2306 // Now add an audio track and do another offer/answer.
Steve Anton15324772018-01-16 10:26:49 -08002307 caller()->AddAudioTrack();
2308 callee()->AddAudioTrack();
deadbeef4389b4d2017-09-07 09:07:36 -07002309 caller()->CreateAndSetAndSignalOffer();
2310 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2311 // Ensure both audio and video frames are received end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002312 MediaExpectations media_expectations;
2313 media_expectations.ExpectBidirectionalAudioAndVideo();
2314 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef4389b4d2017-09-07 09:07:36 -07002315}
2316
deadbeef1dcb1642017-03-29 21:08:16 -07002317// This test sets up a call that's transferred to a new caller with a different
2318// DTLS fingerprint.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002319TEST_P(PeerConnectionIntegrationTest, CallTransferredForCallee) {
deadbeef1dcb1642017-03-29 21:08:16 -07002320 ASSERT_TRUE(CreatePeerConnectionWrappers());
2321 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002322 caller()->AddAudioVideoTracks();
2323 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002324 caller()->CreateAndSetAndSignalOffer();
2325 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2326
2327 // Keep the original peer around which will still send packets to the
2328 // receiving client. These SRTP packets will be dropped.
2329 std::unique_ptr<PeerConnectionWrapper> original_peer(
2330 SetCallerPcWrapperAndReturnCurrent(
Seth Hampson2f0d7022018-02-20 11:54:42 -08002331 CreatePeerConnectionWrapperWithAlternateKey().release()));
deadbeef1dcb1642017-03-29 21:08:16 -07002332 // TODO(deadbeef): Why do we call Close here? That goes against the comment
2333 // directly above.
2334 original_peer->pc()->Close();
2335
2336 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002337 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002338 caller()->CreateAndSetAndSignalOffer();
2339 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2340 // Wait for some additional frames to be transmitted end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002341 MediaExpectations media_expectations;
2342 media_expectations.ExpectBidirectionalAudioAndVideo();
2343 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002344}
2345
2346// This test sets up a call that's transferred to a new callee with a different
2347// DTLS fingerprint.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002348TEST_P(PeerConnectionIntegrationTest, CallTransferredForCaller) {
deadbeef1dcb1642017-03-29 21:08:16 -07002349 ASSERT_TRUE(CreatePeerConnectionWrappers());
2350 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002351 caller()->AddAudioVideoTracks();
2352 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002353 caller()->CreateAndSetAndSignalOffer();
2354 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2355
2356 // Keep the original peer around which will still send packets to the
2357 // receiving client. These SRTP packets will be dropped.
2358 std::unique_ptr<PeerConnectionWrapper> original_peer(
2359 SetCalleePcWrapperAndReturnCurrent(
Seth Hampson2f0d7022018-02-20 11:54:42 -08002360 CreatePeerConnectionWrapperWithAlternateKey().release()));
deadbeef1dcb1642017-03-29 21:08:16 -07002361 // TODO(deadbeef): Why do we call Close here? That goes against the comment
2362 // directly above.
2363 original_peer->pc()->Close();
2364
2365 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002366 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002367 caller()->SetOfferAnswerOptions(IceRestartOfferAnswerOptions());
2368 caller()->CreateAndSetAndSignalOffer();
2369 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2370 // Wait for some additional frames to be transmitted end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002371 MediaExpectations media_expectations;
2372 media_expectations.ExpectBidirectionalAudioAndVideo();
2373 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002374}
2375
2376// This test sets up a non-bundled call and negotiates bundling at the same
2377// time as starting an ICE restart. When bundling is in effect in the restart,
2378// the DTLS-SRTP context should be successfully reset.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002379TEST_P(PeerConnectionIntegrationTest, BundlingEnabledWhileIceRestartOccurs) {
deadbeef1dcb1642017-03-29 21:08:16 -07002380 ASSERT_TRUE(CreatePeerConnectionWrappers());
2381 ConnectFakeSignaling();
2382
Steve Anton15324772018-01-16 10:26:49 -08002383 caller()->AddAudioVideoTracks();
2384 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002385 // Remove the bundle group from the SDP received by the callee.
2386 callee()->SetReceivedSdpMunger([](cricket::SessionDescription* desc) {
2387 desc->RemoveGroupByName("BUNDLE");
2388 });
2389 caller()->CreateAndSetAndSignalOffer();
2390 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002391 {
2392 MediaExpectations media_expectations;
2393 media_expectations.ExpectBidirectionalAudioAndVideo();
2394 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2395 }
deadbeef1dcb1642017-03-29 21:08:16 -07002396 // Now stop removing the BUNDLE group, and trigger an ICE restart.
2397 callee()->SetReceivedSdpMunger(nullptr);
2398 caller()->SetOfferAnswerOptions(IceRestartOfferAnswerOptions());
2399 caller()->CreateAndSetAndSignalOffer();
2400 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2401
2402 // Expect additional frames to be received after the ICE restart.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002403 {
2404 MediaExpectations media_expectations;
2405 media_expectations.ExpectBidirectionalAudioAndVideo();
2406 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2407 }
deadbeef1dcb1642017-03-29 21:08:16 -07002408}
2409
2410// Test CVO (Coordination of Video Orientation). If a video source is rotated
2411// and both peers support the CVO RTP header extension, the actual video frames
2412// don't need to be encoded in different resolutions, since the rotation is
2413// communicated through the RTP header extension.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002414TEST_P(PeerConnectionIntegrationTest, RotatedVideoWithCVOExtension) {
deadbeef1dcb1642017-03-29 21:08:16 -07002415 ASSERT_TRUE(CreatePeerConnectionWrappers());
2416 ConnectFakeSignaling();
2417 // Add rotated video tracks.
Steve Anton15324772018-01-16 10:26:49 -08002418 caller()->AddTrack(
deadbeef1dcb1642017-03-29 21:08:16 -07002419 caller()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_90));
Steve Anton15324772018-01-16 10:26:49 -08002420 callee()->AddTrack(
deadbeef1dcb1642017-03-29 21:08:16 -07002421 callee()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_270));
2422
2423 // Wait for video frames to be received by both sides.
2424 caller()->CreateAndSetAndSignalOffer();
2425 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2426 ASSERT_TRUE_WAIT(caller()->min_video_frames_received_per_track() > 0 &&
2427 callee()->min_video_frames_received_per_track() > 0,
2428 kMaxWaitForFramesMs);
2429
2430 // Ensure that the aspect ratio is unmodified.
2431 // TODO(deadbeef): Where does 4:3 come from? Should be explicit in the test,
2432 // not just assumed.
2433 EXPECT_EQ(4.0 / 3, caller()->local_rendered_aspect_ratio());
2434 EXPECT_EQ(4.0 / 3, caller()->rendered_aspect_ratio());
2435 EXPECT_EQ(4.0 / 3, callee()->local_rendered_aspect_ratio());
2436 EXPECT_EQ(4.0 / 3, callee()->rendered_aspect_ratio());
2437 // Ensure that the CVO bits were surfaced to the renderer.
2438 EXPECT_EQ(webrtc::kVideoRotation_270, caller()->rendered_rotation());
2439 EXPECT_EQ(webrtc::kVideoRotation_90, callee()->rendered_rotation());
2440}
2441
2442// Test that when the CVO extension isn't supported, video is rotated the
2443// old-fashioned way, by encoding rotated frames.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002444TEST_P(PeerConnectionIntegrationTest, RotatedVideoWithoutCVOExtension) {
deadbeef1dcb1642017-03-29 21:08:16 -07002445 ASSERT_TRUE(CreatePeerConnectionWrappers());
2446 ConnectFakeSignaling();
2447 // Add rotated video tracks.
Steve Anton15324772018-01-16 10:26:49 -08002448 caller()->AddTrack(
deadbeef1dcb1642017-03-29 21:08:16 -07002449 caller()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_90));
Steve Anton15324772018-01-16 10:26:49 -08002450 callee()->AddTrack(
deadbeef1dcb1642017-03-29 21:08:16 -07002451 callee()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_270));
2452
2453 // Remove the CVO extension from the offered SDP.
2454 callee()->SetReceivedSdpMunger([](cricket::SessionDescription* desc) {
2455 cricket::VideoContentDescription* video =
2456 GetFirstVideoContentDescription(desc);
2457 video->ClearRtpHeaderExtensions();
2458 });
2459 // Wait for video frames to be received by both sides.
2460 caller()->CreateAndSetAndSignalOffer();
2461 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2462 ASSERT_TRUE_WAIT(caller()->min_video_frames_received_per_track() > 0 &&
2463 callee()->min_video_frames_received_per_track() > 0,
2464 kMaxWaitForFramesMs);
2465
2466 // Expect that the aspect ratio is inversed to account for the 90/270 degree
2467 // rotation.
2468 // TODO(deadbeef): Where does 4:3 come from? Should be explicit in the test,
2469 // not just assumed.
2470 EXPECT_EQ(3.0 / 4, caller()->local_rendered_aspect_ratio());
2471 EXPECT_EQ(3.0 / 4, caller()->rendered_aspect_ratio());
2472 EXPECT_EQ(3.0 / 4, callee()->local_rendered_aspect_ratio());
2473 EXPECT_EQ(3.0 / 4, callee()->rendered_aspect_ratio());
2474 // Expect that each endpoint is unaware of the rotation of the other endpoint.
2475 EXPECT_EQ(webrtc::kVideoRotation_0, caller()->rendered_rotation());
2476 EXPECT_EQ(webrtc::kVideoRotation_0, callee()->rendered_rotation());
2477}
2478
deadbeef1dcb1642017-03-29 21:08:16 -07002479// Test that if the answerer rejects the audio m= section, no audio is sent or
2480// received, but video still can be.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002481TEST_P(PeerConnectionIntegrationTest, AnswererRejectsAudioSection) {
deadbeef1dcb1642017-03-29 21:08:16 -07002482 ASSERT_TRUE(CreatePeerConnectionWrappers());
2483 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002484 caller()->AddAudioVideoTracks();
Seth Hampson2f0d7022018-02-20 11:54:42 -08002485 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2486 // Only add video track for callee, and set offer_to_receive_audio to 0, so
2487 // it will reject the audio m= section completely.
2488 PeerConnectionInterface::RTCOfferAnswerOptions options;
2489 options.offer_to_receive_audio = 0;
2490 callee()->SetOfferAnswerOptions(options);
2491 } else {
2492 // Stopping the audio RtpTransceiver will cause the media section to be
2493 // rejected in the answer.
2494 callee()->SetRemoteOfferHandler([this] {
Harald Alvestrand6060df52020-08-11 09:54:02 +02002495 callee()
2496 ->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_AUDIO)
2497 ->StopInternal();
Seth Hampson2f0d7022018-02-20 11:54:42 -08002498 });
2499 }
Steve Anton15324772018-01-16 10:26:49 -08002500 callee()->AddTrack(callee()->CreateLocalVideoTrack());
deadbeef1dcb1642017-03-29 21:08:16 -07002501 // Do offer/answer and wait for successful end-to-end video frames.
2502 caller()->CreateAndSetAndSignalOffer();
2503 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002504 MediaExpectations media_expectations;
2505 media_expectations.ExpectBidirectionalVideo();
2506 media_expectations.ExpectNoAudio();
2507 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2508
deadbeef1dcb1642017-03-29 21:08:16 -07002509 // Sanity check that the callee's description has a rejected audio section.
2510 ASSERT_NE(nullptr, callee()->pc()->local_description());
2511 const ContentInfo* callee_audio_content =
2512 GetFirstAudioContent(callee()->pc()->local_description()->description());
2513 ASSERT_NE(nullptr, callee_audio_content);
2514 EXPECT_TRUE(callee_audio_content->rejected);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002515 if (sdp_semantics_ == SdpSemantics::kUnifiedPlan) {
Harald Alvestrand6060df52020-08-11 09:54:02 +02002516 // The caller's transceiver should have stopped after receiving the answer,
2517 // and thus no longer listed in transceivers.
2518 EXPECT_EQ(nullptr,
2519 caller()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_AUDIO));
Seth Hampson2f0d7022018-02-20 11:54:42 -08002520 }
deadbeef1dcb1642017-03-29 21:08:16 -07002521}
2522
2523// Test that if the answerer rejects the video m= section, no video is sent or
2524// received, but audio still can be.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002525TEST_P(PeerConnectionIntegrationTest, AnswererRejectsVideoSection) {
deadbeef1dcb1642017-03-29 21:08:16 -07002526 ASSERT_TRUE(CreatePeerConnectionWrappers());
2527 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002528 caller()->AddAudioVideoTracks();
Seth Hampson2f0d7022018-02-20 11:54:42 -08002529 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2530 // Only add audio track for callee, and set offer_to_receive_video to 0, so
2531 // it will reject the video m= section completely.
2532 PeerConnectionInterface::RTCOfferAnswerOptions options;
2533 options.offer_to_receive_video = 0;
2534 callee()->SetOfferAnswerOptions(options);
2535 } else {
2536 // Stopping the video RtpTransceiver will cause the media section to be
2537 // rejected in the answer.
2538 callee()->SetRemoteOfferHandler([this] {
Harald Alvestrand6060df52020-08-11 09:54:02 +02002539 callee()
2540 ->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)
2541 ->StopInternal();
Seth Hampson2f0d7022018-02-20 11:54:42 -08002542 });
2543 }
Steve Anton15324772018-01-16 10:26:49 -08002544 callee()->AddTrack(callee()->CreateLocalAudioTrack());
deadbeef1dcb1642017-03-29 21:08:16 -07002545 // Do offer/answer and wait for successful end-to-end audio frames.
2546 caller()->CreateAndSetAndSignalOffer();
2547 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002548 MediaExpectations media_expectations;
2549 media_expectations.ExpectBidirectionalAudio();
2550 media_expectations.ExpectNoVideo();
2551 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2552
deadbeef1dcb1642017-03-29 21:08:16 -07002553 // Sanity check that the callee's description has a rejected video section.
2554 ASSERT_NE(nullptr, callee()->pc()->local_description());
2555 const ContentInfo* callee_video_content =
2556 GetFirstVideoContent(callee()->pc()->local_description()->description());
2557 ASSERT_NE(nullptr, callee_video_content);
2558 EXPECT_TRUE(callee_video_content->rejected);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002559 if (sdp_semantics_ == SdpSemantics::kUnifiedPlan) {
Harald Alvestrand6060df52020-08-11 09:54:02 +02002560 // The caller's transceiver should have stopped after receiving the answer,
2561 // and thus is no longer present.
2562 EXPECT_EQ(nullptr,
2563 caller()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO));
Seth Hampson2f0d7022018-02-20 11:54:42 -08002564 }
deadbeef1dcb1642017-03-29 21:08:16 -07002565}
2566
2567// Test that if the answerer rejects both audio and video m= sections, nothing
2568// bad happens.
2569// TODO(deadbeef): Test that a data channel still works. Currently this doesn't
2570// test anything but the fact that negotiation succeeds, which doesn't mean
2571// much.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002572TEST_P(PeerConnectionIntegrationTest, AnswererRejectsAudioAndVideoSections) {
deadbeef1dcb1642017-03-29 21:08:16 -07002573 ASSERT_TRUE(CreatePeerConnectionWrappers());
2574 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002575 caller()->AddAudioVideoTracks();
Seth Hampson2f0d7022018-02-20 11:54:42 -08002576 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2577 // Don't give the callee any tracks, and set offer_to_receive_X to 0, so it
2578 // will reject both audio and video m= sections.
2579 PeerConnectionInterface::RTCOfferAnswerOptions options;
2580 options.offer_to_receive_audio = 0;
2581 options.offer_to_receive_video = 0;
2582 callee()->SetOfferAnswerOptions(options);
2583 } else {
2584 callee()->SetRemoteOfferHandler([this] {
2585 // Stopping all transceivers will cause all media sections to be rejected.
Mirko Bonadei739baf02019-01-27 17:29:42 +01002586 for (const auto& transceiver : callee()->pc()->GetTransceivers()) {
Harald Alvestrand6060df52020-08-11 09:54:02 +02002587 transceiver->StopInternal();
Seth Hampson2f0d7022018-02-20 11:54:42 -08002588 }
2589 });
2590 }
deadbeef1dcb1642017-03-29 21:08:16 -07002591 // Do offer/answer and wait for stable signaling state.
2592 caller()->CreateAndSetAndSignalOffer();
2593 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002594
deadbeef1dcb1642017-03-29 21:08:16 -07002595 // Sanity check that the callee's description has rejected m= sections.
2596 ASSERT_NE(nullptr, callee()->pc()->local_description());
2597 const ContentInfo* callee_audio_content =
2598 GetFirstAudioContent(callee()->pc()->local_description()->description());
2599 ASSERT_NE(nullptr, callee_audio_content);
2600 EXPECT_TRUE(callee_audio_content->rejected);
2601 const ContentInfo* callee_video_content =
2602 GetFirstVideoContent(callee()->pc()->local_description()->description());
2603 ASSERT_NE(nullptr, callee_video_content);
2604 EXPECT_TRUE(callee_video_content->rejected);
2605}
2606
2607// This test sets up an audio and video call between two parties. After the
2608// call runs for a while, the caller sends an updated offer with video being
2609// rejected. Once the re-negotiation is done, the video flow should stop and
2610// the audio flow should continue.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002611TEST_P(PeerConnectionIntegrationTest, VideoRejectedInSubsequentOffer) {
deadbeef1dcb1642017-03-29 21:08:16 -07002612 ASSERT_TRUE(CreatePeerConnectionWrappers());
2613 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002614 caller()->AddAudioVideoTracks();
2615 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002616 caller()->CreateAndSetAndSignalOffer();
2617 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002618 {
2619 MediaExpectations media_expectations;
2620 media_expectations.ExpectBidirectionalAudioAndVideo();
2621 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2622 }
deadbeef1dcb1642017-03-29 21:08:16 -07002623 // Renegotiate, rejecting the video m= section.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002624 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2625 caller()->SetGeneratedSdpMunger(
2626 [](cricket::SessionDescription* description) {
2627 for (cricket::ContentInfo& content : description->contents()) {
2628 if (cricket::IsVideoContent(&content)) {
2629 content.rejected = true;
2630 }
2631 }
2632 });
2633 } else {
Harald Alvestrand6060df52020-08-11 09:54:02 +02002634 caller()
2635 ->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)
2636 ->StopInternal();
Seth Hampson2f0d7022018-02-20 11:54:42 -08002637 }
deadbeef1dcb1642017-03-29 21:08:16 -07002638 caller()->CreateAndSetAndSignalOffer();
2639 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
2640
2641 // Sanity check that the caller's description has a rejected video section.
2642 ASSERT_NE(nullptr, caller()->pc()->local_description());
2643 const ContentInfo* caller_video_content =
2644 GetFirstVideoContent(caller()->pc()->local_description()->description());
2645 ASSERT_NE(nullptr, caller_video_content);
2646 EXPECT_TRUE(caller_video_content->rejected);
deadbeef1dcb1642017-03-29 21:08:16 -07002647 // Wait for some additional audio frames to be received.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002648 {
2649 MediaExpectations media_expectations;
2650 media_expectations.ExpectBidirectionalAudio();
2651 media_expectations.ExpectNoVideo();
2652 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2653 }
deadbeef1dcb1642017-03-29 21:08:16 -07002654}
2655
Taylor Brandstetter60c8dc82018-04-11 15:20:27 -07002656// Do one offer/answer with audio, another that disables it (rejecting the m=
2657// section), and another that re-enables it. Regression test for:
2658// bugs.webrtc.org/6023
2659TEST_F(PeerConnectionIntegrationTestPlanB, EnableAudioAfterRejecting) {
2660 ASSERT_TRUE(CreatePeerConnectionWrappers());
2661 ConnectFakeSignaling();
2662
2663 // Add audio track, do normal offer/answer.
2664 rtc::scoped_refptr<webrtc::AudioTrackInterface> track =
2665 caller()->CreateLocalAudioTrack();
2666 rtc::scoped_refptr<webrtc::RtpSenderInterface> sender =
2667 caller()->pc()->AddTrack(track, {"stream"}).MoveValue();
2668 caller()->CreateAndSetAndSignalOffer();
2669 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2670
2671 // Remove audio track, and set offer_to_receive_audio to false to cause the
2672 // m= section to be completely disabled, not just "recvonly".
2673 caller()->pc()->RemoveTrack(sender);
2674 PeerConnectionInterface::RTCOfferAnswerOptions options;
2675 options.offer_to_receive_audio = 0;
2676 caller()->SetOfferAnswerOptions(options);
2677 caller()->CreateAndSetAndSignalOffer();
2678 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2679
2680 // Add the audio track again, expecting negotiation to succeed and frames to
2681 // flow.
2682 sender = caller()->pc()->AddTrack(track, {"stream"}).MoveValue();
2683 options.offer_to_receive_audio = 1;
2684 caller()->SetOfferAnswerOptions(options);
2685 caller()->CreateAndSetAndSignalOffer();
2686 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2687
2688 MediaExpectations media_expectations;
2689 media_expectations.CalleeExpectsSomeAudio();
2690 EXPECT_TRUE(ExpectNewFrames(media_expectations));
2691}
2692
deadbeef1dcb1642017-03-29 21:08:16 -07002693// Basic end-to-end test, but without SSRC/MSID signaling. This functionality
2694// is needed to support legacy endpoints.
2695// TODO(deadbeef): When we support the MID extension and demuxing on MID, also
2696// add a test for an end-to-end test without MID signaling either (basically,
2697// the minimum acceptable SDP).
Seth Hampson2f0d7022018-02-20 11:54:42 -08002698TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithoutSsrcOrMsidSignaling) {
deadbeef1dcb1642017-03-29 21:08:16 -07002699 ASSERT_TRUE(CreatePeerConnectionWrappers());
2700 ConnectFakeSignaling();
2701 // Add audio and video, testing that packets can be demuxed on payload type.
Steve Anton15324772018-01-16 10:26:49 -08002702 caller()->AddAudioVideoTracks();
2703 callee()->AddAudioVideoTracks();
deadbeefd8ad7882017-04-18 16:01:17 -07002704 // Remove SSRCs and MSIDs from the received offer SDP.
2705 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
deadbeef1dcb1642017-03-29 21:08:16 -07002706 caller()->CreateAndSetAndSignalOffer();
2707 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002708 MediaExpectations media_expectations;
2709 media_expectations.ExpectBidirectionalAudioAndVideo();
2710 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002711}
2712
Seth Hampson5897a6e2018-04-03 11:16:33 -07002713// Basic end-to-end test, without SSRC signaling. This means that the track
2714// was created properly and frames are delivered when the MSIDs are communicated
2715// with a=msid lines and no a=ssrc lines.
2716TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
2717 EndToEndCallWithoutSsrcSignaling) {
2718 const char kStreamId[] = "streamId";
2719 ASSERT_TRUE(CreatePeerConnectionWrappers());
2720 ConnectFakeSignaling();
2721 // Add just audio tracks.
2722 caller()->AddTrack(caller()->CreateLocalAudioTrack(), {kStreamId});
2723 callee()->AddAudioTrack();
2724
2725 // Remove SSRCs from the received offer SDP.
2726 callee()->SetReceivedSdpMunger(RemoveSsrcsAndKeepMsids);
2727 caller()->CreateAndSetAndSignalOffer();
2728 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2729 MediaExpectations media_expectations;
2730 media_expectations.ExpectBidirectionalAudio();
2731 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2732}
2733
Johannes Kron3e983682020-03-29 22:17:00 +02002734TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
2735 EndToEndCallAddReceiveVideoToSendOnlyCall) {
2736 ASSERT_TRUE(CreatePeerConnectionWrappers());
2737 ConnectFakeSignaling();
2738 // Add one-directional video, from caller to callee.
2739 rtc::scoped_refptr<webrtc::VideoTrackInterface> track =
2740 caller()->CreateLocalVideoTrack();
2741
2742 RtpTransceiverInit video_transceiver_init;
2743 video_transceiver_init.stream_ids = {"video1"};
2744 video_transceiver_init.direction = RtpTransceiverDirection::kSendOnly;
2745 auto video_sender =
2746 caller()->pc()->AddTransceiver(track, video_transceiver_init).MoveValue();
2747 caller()->CreateAndSetAndSignalOffer();
2748 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2749
2750 // Add receive direction.
Harald Alvestrand6060df52020-08-11 09:54:02 +02002751 video_sender->SetDirectionWithError(RtpTransceiverDirection::kSendRecv);
Johannes Kron3e983682020-03-29 22:17:00 +02002752
2753 rtc::scoped_refptr<webrtc::VideoTrackInterface> callee_track =
2754 callee()->CreateLocalVideoTrack();
2755
2756 callee()->AddTrack(callee_track);
2757 caller()->CreateAndSetAndSignalOffer();
2758 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2759 // Ensure that video frames are received end-to-end.
2760 MediaExpectations media_expectations;
2761 media_expectations.ExpectBidirectionalVideo();
2762 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2763}
2764
Steve Antondf527fd2018-04-27 15:52:03 -07002765// Tests that video flows between multiple video tracks when SSRCs are not
2766// signaled. This exercises the MID RTP header extension which is needed to
2767// demux the incoming video tracks.
2768TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
2769 EndToEndCallWithTwoVideoTracksAndNoSignaledSsrc) {
2770 ASSERT_TRUE(CreatePeerConnectionWrappers());
2771 ConnectFakeSignaling();
2772 caller()->AddVideoTrack();
2773 caller()->AddVideoTrack();
2774 callee()->AddVideoTrack();
2775 callee()->AddVideoTrack();
2776
2777 caller()->SetReceivedSdpMunger(&RemoveSsrcsAndKeepMsids);
2778 callee()->SetReceivedSdpMunger(&RemoveSsrcsAndKeepMsids);
2779 caller()->CreateAndSetAndSignalOffer();
2780 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2781 ASSERT_EQ(2u, caller()->pc()->GetReceivers().size());
2782 ASSERT_EQ(2u, callee()->pc()->GetReceivers().size());
2783
2784 // Expect video to be received in both directions on both tracks.
2785 MediaExpectations media_expectations;
2786 media_expectations.ExpectBidirectionalVideo();
2787 EXPECT_TRUE(ExpectNewFrames(media_expectations));
2788}
2789
Taylor Brandstetterd3ef4992020-10-15 18:22:57 -07002790// Used for the test below.
2791void RemoveBundleGroupSsrcsAndMidExtension(cricket::SessionDescription* desc) {
2792 RemoveSsrcsAndKeepMsids(desc);
2793 desc->RemoveGroupByName("BUNDLE");
2794 for (ContentInfo& content : desc->contents()) {
2795 cricket::MediaContentDescription* media = content.media_description();
2796 cricket::RtpHeaderExtensions extensions = media->rtp_header_extensions();
2797 extensions.erase(std::remove_if(extensions.begin(), extensions.end(),
2798 [](const RtpExtension& extension) {
2799 return extension.uri ==
2800 RtpExtension::kMidUri;
2801 }),
2802 extensions.end());
2803 media->set_rtp_header_extensions(extensions);
2804 }
2805}
2806
2807// Tests that video flows between multiple video tracks when BUNDLE is not used,
2808// SSRCs are not signaled and the MID RTP header extension is not used. This
2809// relies on demuxing by payload type, which normally doesn't work if you have
2810// multiple media sections using the same payload type, but which should work as
2811// long as the media sections aren't bundled.
2812// Regression test for: http://crbug.com/webrtc/12023
2813TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
2814 EndToEndCallWithTwoVideoTracksNoBundleNoSignaledSsrcAndNoMid) {
2815 ASSERT_TRUE(CreatePeerConnectionWrappers());
2816 ConnectFakeSignaling();
2817 caller()->AddVideoTrack();
2818 caller()->AddVideoTrack();
2819 callee()->AddVideoTrack();
2820 callee()->AddVideoTrack();
2821 caller()->SetReceivedSdpMunger(&RemoveBundleGroupSsrcsAndMidExtension);
2822 callee()->SetReceivedSdpMunger(&RemoveBundleGroupSsrcsAndMidExtension);
2823 caller()->CreateAndSetAndSignalOffer();
2824 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2825 ASSERT_EQ(2u, caller()->pc()->GetReceivers().size());
2826 ASSERT_EQ(2u, callee()->pc()->GetReceivers().size());
2827 // Make sure we are not bundled.
2828 ASSERT_NE(caller()->pc()->GetSenders()[0]->dtls_transport(),
2829 caller()->pc()->GetSenders()[1]->dtls_transport());
2830
2831 // Expect video to be received in both directions on both tracks.
2832 MediaExpectations media_expectations;
2833 media_expectations.ExpectBidirectionalVideo();
2834 EXPECT_TRUE(ExpectNewFrames(media_expectations));
2835}
2836
2837// Used for the test below.
2838void ModifyPayloadTypesAndRemoveMidExtension(
2839 cricket::SessionDescription* desc) {
2840 int pt = 96;
2841 for (ContentInfo& content : desc->contents()) {
2842 cricket::MediaContentDescription* media = content.media_description();
2843 cricket::RtpHeaderExtensions extensions = media->rtp_header_extensions();
2844 extensions.erase(std::remove_if(extensions.begin(), extensions.end(),
2845 [](const RtpExtension& extension) {
2846 return extension.uri ==
2847 RtpExtension::kMidUri;
2848 }),
2849 extensions.end());
2850 media->set_rtp_header_extensions(extensions);
2851 cricket::VideoContentDescription* video = media->as_video();
2852 ASSERT_TRUE(video != nullptr);
2853 std::vector<cricket::VideoCodec> codecs = {{pt++, "VP8"}};
2854 video->set_codecs(codecs);
2855 }
2856}
2857
2858// Tests that two video tracks can be demultiplexed by payload type alone, by
2859// using different payload types for the same codec in different m= sections.
2860// This practice is discouraged but historically has been supported.
2861// Regression test for: http://crbug.com/webrtc/12029
2862TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
2863 EndToEndCallWithTwoVideoTracksDemultiplexedByPayloadType) {
2864 ASSERT_TRUE(CreatePeerConnectionWrappers());
2865 ConnectFakeSignaling();
2866 caller()->AddVideoTrack();
2867 caller()->AddVideoTrack();
2868 callee()->AddVideoTrack();
2869 callee()->AddVideoTrack();
2870 caller()->SetGeneratedSdpMunger(&ModifyPayloadTypesAndRemoveMidExtension);
2871 callee()->SetGeneratedSdpMunger(&ModifyPayloadTypesAndRemoveMidExtension);
2872 // We can't remove SSRCs from the generated SDP because then no send streams
2873 // would be created.
2874 caller()->SetReceivedSdpMunger(&RemoveSsrcsAndKeepMsids);
2875 callee()->SetReceivedSdpMunger(&RemoveSsrcsAndKeepMsids);
2876 caller()->CreateAndSetAndSignalOffer();
2877 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2878 ASSERT_EQ(2u, caller()->pc()->GetReceivers().size());
2879 ASSERT_EQ(2u, callee()->pc()->GetReceivers().size());
2880 // Make sure we are bundled.
2881 ASSERT_EQ(caller()->pc()->GetSenders()[0]->dtls_transport(),
2882 caller()->pc()->GetSenders()[1]->dtls_transport());
2883
2884 // Expect video to be received in both directions on both tracks.
2885 MediaExpectations media_expectations;
2886 media_expectations.ExpectBidirectionalVideo();
2887 EXPECT_TRUE(ExpectNewFrames(media_expectations));
2888}
2889
Henrik Boström5b147782018-12-04 11:25:05 +01002890TEST_F(PeerConnectionIntegrationTestUnifiedPlan, NoStreamsMsidLinePresent) {
2891 ASSERT_TRUE(CreatePeerConnectionWrappers());
2892 ConnectFakeSignaling();
2893 caller()->AddAudioTrack();
2894 caller()->AddVideoTrack();
2895 caller()->CreateAndSetAndSignalOffer();
2896 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2897 auto callee_receivers = callee()->pc()->GetReceivers();
2898 ASSERT_EQ(2u, callee_receivers.size());
2899 EXPECT_TRUE(callee_receivers[0]->stream_ids().empty());
2900 EXPECT_TRUE(callee_receivers[1]->stream_ids().empty());
2901}
2902
2903TEST_F(PeerConnectionIntegrationTestUnifiedPlan, NoStreamsMsidLineMissing) {
2904 ASSERT_TRUE(CreatePeerConnectionWrappers());
2905 ConnectFakeSignaling();
2906 caller()->AddAudioTrack();
2907 caller()->AddVideoTrack();
2908 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
2909 caller()->CreateAndSetAndSignalOffer();
2910 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2911 auto callee_receivers = callee()->pc()->GetReceivers();
2912 ASSERT_EQ(2u, callee_receivers.size());
2913 ASSERT_EQ(1u, callee_receivers[0]->stream_ids().size());
2914 ASSERT_EQ(1u, callee_receivers[1]->stream_ids().size());
2915 EXPECT_EQ(callee_receivers[0]->stream_ids()[0],
2916 callee_receivers[1]->stream_ids()[0]);
2917 EXPECT_EQ(callee_receivers[0]->streams()[0],
2918 callee_receivers[1]->streams()[0]);
2919}
2920
deadbeef1dcb1642017-03-29 21:08:16 -07002921// Test that if two video tracks are sent (from caller to callee, in this test),
2922// they're transmitted correctly end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002923TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithTwoVideoTracks) {
deadbeef1dcb1642017-03-29 21:08:16 -07002924 ASSERT_TRUE(CreatePeerConnectionWrappers());
2925 ConnectFakeSignaling();
2926 // Add one audio/video stream, and one video-only stream.
Steve Anton15324772018-01-16 10:26:49 -08002927 caller()->AddAudioVideoTracks();
2928 caller()->AddVideoTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07002929 caller()->CreateAndSetAndSignalOffer();
2930 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Steve Anton15324772018-01-16 10:26:49 -08002931 ASSERT_EQ(3u, callee()->pc()->GetReceivers().size());
Seth Hampson2f0d7022018-02-20 11:54:42 -08002932
2933 MediaExpectations media_expectations;
2934 media_expectations.CalleeExpectsSomeAudioAndVideo();
2935 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002936}
2937
2938static void MakeSpecCompliantMaxBundleOffer(cricket::SessionDescription* desc) {
2939 bool first = true;
2940 for (cricket::ContentInfo& content : desc->contents()) {
2941 if (first) {
2942 first = false;
2943 continue;
2944 }
2945 content.bundle_only = true;
2946 }
2947 first = true;
2948 for (cricket::TransportInfo& transport : desc->transport_infos()) {
2949 if (first) {
2950 first = false;
2951 continue;
2952 }
2953 transport.description.ice_ufrag.clear();
2954 transport.description.ice_pwd.clear();
2955 transport.description.connection_role = cricket::CONNECTIONROLE_NONE;
2956 transport.description.identity_fingerprint.reset(nullptr);
2957 }
2958}
2959
2960// Test that if applying a true "max bundle" offer, which uses ports of 0,
2961// "a=bundle-only", omitting "a=fingerprint", "a=setup", "a=ice-ufrag" and
2962// "a=ice-pwd" for all but the audio "m=" section, negotiation still completes
2963// successfully and media flows.
2964// TODO(deadbeef): Update this test to also omit "a=rtcp-mux", once that works.
2965// TODO(deadbeef): Won't need this test once we start generating actual
2966// standards-compliant SDP.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002967TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07002968 EndToEndCallWithSpecCompliantMaxBundleOffer) {
2969 ASSERT_TRUE(CreatePeerConnectionWrappers());
2970 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002971 caller()->AddAudioVideoTracks();
2972 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002973 // Do the equivalent of setting the port to 0, adding a=bundle-only, and
2974 // removing a=ice-ufrag, a=ice-pwd, a=fingerprint and a=setup from all
2975 // but the first m= section.
2976 callee()->SetReceivedSdpMunger(MakeSpecCompliantMaxBundleOffer);
2977 caller()->CreateAndSetAndSignalOffer();
2978 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002979 MediaExpectations media_expectations;
2980 media_expectations.ExpectBidirectionalAudioAndVideo();
2981 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002982}
2983
2984// Test that we can receive the audio output level from a remote audio track.
2985// TODO(deadbeef): Use a fake audio source and verify that the output level is
2986// exactly what the source on the other side was configured with.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002987TEST_P(PeerConnectionIntegrationTest, GetAudioOutputLevelStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-29 21:08:16 -07002988 ASSERT_TRUE(CreatePeerConnectionWrappers());
2989 ConnectFakeSignaling();
2990 // Just add an audio track.
Steve Anton15324772018-01-16 10:26:49 -08002991 caller()->AddAudioTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07002992 caller()->CreateAndSetAndSignalOffer();
2993 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2994
2995 // Get the audio output level stats. Note that the level is not available
2996 // until an RTCP packet has been received.
deadbeefd8ad7882017-04-18 16:01:17 -07002997 EXPECT_TRUE_WAIT(callee()->OldGetStats()->AudioOutputLevel() > 0,
deadbeef1dcb1642017-03-29 21:08:16 -07002998 kMaxWaitForFramesMs);
2999}
3000
3001// Test that an audio input level is reported.
3002// TODO(deadbeef): Use a fake audio source and verify that the input level is
3003// exactly what the source was configured with.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003004TEST_P(PeerConnectionIntegrationTest, GetAudioInputLevelStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-29 21:08:16 -07003005 ASSERT_TRUE(CreatePeerConnectionWrappers());
3006 ConnectFakeSignaling();
3007 // Just add an audio track.
Steve Anton15324772018-01-16 10:26:49 -08003008 caller()->AddAudioTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07003009 caller()->CreateAndSetAndSignalOffer();
3010 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3011
3012 // Get the audio input level stats. The level should be available very
3013 // soon after the test starts.
deadbeefd8ad7882017-04-18 16:01:17 -07003014 EXPECT_TRUE_WAIT(caller()->OldGetStats()->AudioInputLevel() > 0,
deadbeef1dcb1642017-03-29 21:08:16 -07003015 kMaxWaitForStatsMs);
3016}
3017
3018// Test that we can get incoming byte counts from both audio and video tracks.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003019TEST_P(PeerConnectionIntegrationTest, GetBytesReceivedStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-29 21:08:16 -07003020 ASSERT_TRUE(CreatePeerConnectionWrappers());
3021 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08003022 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003023 // Do offer/answer, wait for the callee to receive some frames.
3024 caller()->CreateAndSetAndSignalOffer();
3025 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08003026
3027 MediaExpectations media_expectations;
3028 media_expectations.CalleeExpectsSomeAudioAndVideo();
3029 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003030
3031 // Get a handle to the remote tracks created, so they can be used as GetStats
3032 // filters.
Mirko Bonadei739baf02019-01-27 17:29:42 +01003033 for (const auto& receiver : callee()->pc()->GetReceivers()) {
Steve Anton15324772018-01-16 10:26:49 -08003034 // We received frames, so we definitely should have nonzero "received bytes"
3035 // stats at this point.
3036 EXPECT_GT(callee()->OldGetStatsForTrack(receiver->track())->BytesReceived(),
3037 0);
3038 }
deadbeef1dcb1642017-03-29 21:08:16 -07003039}
3040
3041// Test that we can get outgoing byte counts from both audio and video tracks.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003042TEST_P(PeerConnectionIntegrationTest, GetBytesSentStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-29 21:08:16 -07003043 ASSERT_TRUE(CreatePeerConnectionWrappers());
3044 ConnectFakeSignaling();
3045 auto audio_track = caller()->CreateLocalAudioTrack();
3046 auto video_track = caller()->CreateLocalVideoTrack();
Steve Anton15324772018-01-16 10:26:49 -08003047 caller()->AddTrack(audio_track);
3048 caller()->AddTrack(video_track);
deadbeef1dcb1642017-03-29 21:08:16 -07003049 // Do offer/answer, wait for the callee to receive some frames.
3050 caller()->CreateAndSetAndSignalOffer();
3051 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08003052 MediaExpectations media_expectations;
3053 media_expectations.CalleeExpectsSomeAudioAndVideo();
3054 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003055
3056 // The callee received frames, so we definitely should have nonzero "sent
3057 // bytes" stats at this point.
deadbeefd8ad7882017-04-18 16:01:17 -07003058 EXPECT_GT(caller()->OldGetStatsForTrack(audio_track)->BytesSent(), 0);
3059 EXPECT_GT(caller()->OldGetStatsForTrack(video_track)->BytesSent(), 0);
3060}
3061
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02003062// Test that we can get capture start ntp time.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003063TEST_P(PeerConnectionIntegrationTest, GetCaptureStartNtpTimeWithOldStatsApi) {
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02003064 ASSERT_TRUE(CreatePeerConnectionWrappers());
3065 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08003066 caller()->AddAudioTrack();
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02003067
Steve Anton15324772018-01-16 10:26:49 -08003068 callee()->AddAudioTrack();
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02003069
3070 // Do offer/answer, wait for the callee to receive some frames.
3071 caller()->CreateAndSetAndSignalOffer();
3072 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3073
3074 // Get the remote audio track created on the receiver, so they can be used as
3075 // GetStats filters.
Steve Antonfc853712018-03-01 13:48:58 -08003076 auto receivers = callee()->pc()->GetReceivers();
3077 ASSERT_EQ(1u, receivers.size());
3078 auto remote_audio_track = receivers[0]->track();
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02003079
3080 // Get the audio output level stats. Note that the level is not available
3081 // until an RTCP packet has been received.
Zhi Huange830e682018-03-30 10:48:35 -07003082 EXPECT_TRUE_WAIT(
3083 callee()->OldGetStatsForTrack(remote_audio_track)->CaptureStartNtpTime() >
3084 0,
3085 2 * kMaxWaitForFramesMs);
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02003086}
3087
Steve Antona41959e2018-11-28 11:15:33 -08003088// Test that the track ID is associated with all local and remote SSRC stats
3089// using the old GetStats() and more than 1 audio and more than 1 video track.
3090// This is a regression test for crbug.com/906988
3091TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
3092 OldGetStatsAssociatesTrackIdForManyMediaSections) {
3093 ASSERT_TRUE(CreatePeerConnectionWrappers());
3094 ConnectFakeSignaling();
3095 auto audio_sender_1 = caller()->AddAudioTrack();
3096 auto video_sender_1 = caller()->AddVideoTrack();
3097 auto audio_sender_2 = caller()->AddAudioTrack();
3098 auto video_sender_2 = caller()->AddVideoTrack();
3099 caller()->CreateAndSetAndSignalOffer();
3100 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3101
3102 MediaExpectations media_expectations;
3103 media_expectations.CalleeExpectsSomeAudioAndVideo();
3104 ASSERT_TRUE_WAIT(ExpectNewFrames(media_expectations), kDefaultTimeout);
3105
3106 std::vector<std::string> track_ids = {
3107 audio_sender_1->track()->id(), video_sender_1->track()->id(),
3108 audio_sender_2->track()->id(), video_sender_2->track()->id()};
3109
3110 auto caller_stats = caller()->OldGetStats();
3111 EXPECT_THAT(caller_stats->TrackIds(), UnorderedElementsAreArray(track_ids));
3112 auto callee_stats = callee()->OldGetStats();
3113 EXPECT_THAT(callee_stats->TrackIds(), UnorderedElementsAreArray(track_ids));
3114}
3115
Steve Antonffa6ce42018-11-30 09:26:08 -08003116// Test that the new GetStats() returns stats for all outgoing/incoming streams
3117// with the correct track IDs if there are more than one audio and more than one
3118// video senders/receivers.
3119TEST_P(PeerConnectionIntegrationTest, NewGetStatsManyAudioAndManyVideoStreams) {
3120 ASSERT_TRUE(CreatePeerConnectionWrappers());
3121 ConnectFakeSignaling();
3122 auto audio_sender_1 = caller()->AddAudioTrack();
3123 auto video_sender_1 = caller()->AddVideoTrack();
3124 auto audio_sender_2 = caller()->AddAudioTrack();
3125 auto video_sender_2 = caller()->AddVideoTrack();
3126 caller()->CreateAndSetAndSignalOffer();
3127 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3128
3129 MediaExpectations media_expectations;
3130 media_expectations.CalleeExpectsSomeAudioAndVideo();
3131 ASSERT_TRUE_WAIT(ExpectNewFrames(media_expectations), kDefaultTimeout);
3132
3133 std::vector<std::string> track_ids = {
3134 audio_sender_1->track()->id(), video_sender_1->track()->id(),
3135 audio_sender_2->track()->id(), video_sender_2->track()->id()};
3136
3137 rtc::scoped_refptr<const webrtc::RTCStatsReport> caller_report =
3138 caller()->NewGetStats();
3139 ASSERT_TRUE(caller_report);
3140 auto outbound_stream_stats =
3141 caller_report->GetStatsOfType<webrtc::RTCOutboundRTPStreamStats>();
Henrik Boströma0ff50c2020-05-05 15:54:46 +02003142 ASSERT_EQ(outbound_stream_stats.size(), 4u);
Steve Antonffa6ce42018-11-30 09:26:08 -08003143 std::vector<std::string> outbound_track_ids;
3144 for (const auto& stat : outbound_stream_stats) {
3145 ASSERT_TRUE(stat->bytes_sent.is_defined());
3146 EXPECT_LT(0u, *stat->bytes_sent);
Rasmus Brandt2efae772019-06-27 14:29:34 +02003147 if (*stat->kind == "video") {
3148 ASSERT_TRUE(stat->key_frames_encoded.is_defined());
3149 EXPECT_GT(*stat->key_frames_encoded, 0u);
3150 ASSERT_TRUE(stat->frames_encoded.is_defined());
3151 EXPECT_GE(*stat->frames_encoded, *stat->key_frames_encoded);
3152 }
Steve Antonffa6ce42018-11-30 09:26:08 -08003153 ASSERT_TRUE(stat->track_id.is_defined());
3154 const auto* track_stat =
3155 caller_report->GetAs<webrtc::RTCMediaStreamTrackStats>(*stat->track_id);
3156 ASSERT_TRUE(track_stat);
3157 outbound_track_ids.push_back(*track_stat->track_identifier);
3158 }
3159 EXPECT_THAT(outbound_track_ids, UnorderedElementsAreArray(track_ids));
3160
3161 rtc::scoped_refptr<const webrtc::RTCStatsReport> callee_report =
3162 callee()->NewGetStats();
3163 ASSERT_TRUE(callee_report);
3164 auto inbound_stream_stats =
3165 callee_report->GetStatsOfType<webrtc::RTCInboundRTPStreamStats>();
3166 ASSERT_EQ(4u, inbound_stream_stats.size());
3167 std::vector<std::string> inbound_track_ids;
3168 for (const auto& stat : inbound_stream_stats) {
3169 ASSERT_TRUE(stat->bytes_received.is_defined());
3170 EXPECT_LT(0u, *stat->bytes_received);
Rasmus Brandt2efae772019-06-27 14:29:34 +02003171 if (*stat->kind == "video") {
3172 ASSERT_TRUE(stat->key_frames_decoded.is_defined());
3173 EXPECT_GT(*stat->key_frames_decoded, 0u);
3174 ASSERT_TRUE(stat->frames_decoded.is_defined());
3175 EXPECT_GE(*stat->frames_decoded, *stat->key_frames_decoded);
3176 }
Steve Antonffa6ce42018-11-30 09:26:08 -08003177 ASSERT_TRUE(stat->track_id.is_defined());
3178 const auto* track_stat =
3179 callee_report->GetAs<webrtc::RTCMediaStreamTrackStats>(*stat->track_id);
3180 ASSERT_TRUE(track_stat);
3181 inbound_track_ids.push_back(*track_stat->track_identifier);
3182 }
3183 EXPECT_THAT(inbound_track_ids, UnorderedElementsAreArray(track_ids));
3184}
3185
3186// Test that we can get stats (using the new stats implementation) for
deadbeefd8ad7882017-04-18 16:01:17 -07003187// unsignaled streams. Meaning when SSRCs/MSIDs aren't signaled explicitly in
3188// SDP.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003189TEST_P(PeerConnectionIntegrationTest,
deadbeefd8ad7882017-04-18 16:01:17 -07003190 GetStatsForUnsignaledStreamWithNewStatsApi) {
3191 ASSERT_TRUE(CreatePeerConnectionWrappers());
3192 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08003193 caller()->AddAudioTrack();
deadbeefd8ad7882017-04-18 16:01:17 -07003194 // Remove SSRCs and MSIDs from the received offer SDP.
3195 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
3196 caller()->CreateAndSetAndSignalOffer();
3197 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08003198 MediaExpectations media_expectations;
3199 media_expectations.CalleeExpectsSomeAudio(1);
3200 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeefd8ad7882017-04-18 16:01:17 -07003201
3202 // We received a frame, so we should have nonzero "bytes received" stats for
3203 // the unsignaled stream, if stats are working for it.
3204 rtc::scoped_refptr<const webrtc::RTCStatsReport> report =
3205 callee()->NewGetStats();
3206 ASSERT_NE(nullptr, report);
3207 auto inbound_stream_stats =
3208 report->GetStatsOfType<webrtc::RTCInboundRTPStreamStats>();
3209 ASSERT_EQ(1U, inbound_stream_stats.size());
3210 ASSERT_TRUE(inbound_stream_stats[0]->bytes_received.is_defined());
3211 ASSERT_GT(*inbound_stream_stats[0]->bytes_received, 0U);
zhihuangf8164932017-05-19 13:09:47 -07003212 ASSERT_TRUE(inbound_stream_stats[0]->track_id.is_defined());
3213}
3214
Taylor Brandstettera4653442018-06-19 09:44:26 -07003215// Same as above but for the legacy stats implementation.
3216TEST_P(PeerConnectionIntegrationTest,
3217 GetStatsForUnsignaledStreamWithOldStatsApi) {
3218 ASSERT_TRUE(CreatePeerConnectionWrappers());
3219 ConnectFakeSignaling();
3220 caller()->AddAudioTrack();
3221 // Remove SSRCs and MSIDs from the received offer SDP.
3222 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
3223 caller()->CreateAndSetAndSignalOffer();
3224 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3225
3226 // Note that, since the old stats implementation associates SSRCs with tracks
3227 // using SDP, when SSRCs aren't signaled in SDP these stats won't have an
3228 // associated track ID. So we can't use the track "selector" argument.
3229 //
3230 // Also, we use "EXPECT_TRUE_WAIT" because the stats collector may decide to
3231 // return cached stats if not enough time has passed since the last update.
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02003232 EXPECT_TRUE_WAIT(callee()->OldGetStats()->BytesReceived() > 0,
Taylor Brandstettera4653442018-06-19 09:44:26 -07003233 kDefaultTimeout);
3234}
3235
zhihuangf8164932017-05-19 13:09:47 -07003236// Test that we can successfully get the media related stats (audio level
3237// etc.) for the unsignaled stream.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003238TEST_P(PeerConnectionIntegrationTest,
zhihuangf8164932017-05-19 13:09:47 -07003239 GetMediaStatsForUnsignaledStreamWithNewStatsApi) {
3240 ASSERT_TRUE(CreatePeerConnectionWrappers());
3241 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08003242 caller()->AddAudioVideoTracks();
zhihuangf8164932017-05-19 13:09:47 -07003243 // Remove SSRCs and MSIDs from the received offer SDP.
3244 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
3245 caller()->CreateAndSetAndSignalOffer();
3246 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08003247 MediaExpectations media_expectations;
3248 media_expectations.CalleeExpectsSomeAudio(1);
3249 media_expectations.CalleeExpectsSomeVideo(1);
3250 ASSERT_TRUE(ExpectNewFrames(media_expectations));
zhihuangf8164932017-05-19 13:09:47 -07003251
3252 rtc::scoped_refptr<const webrtc::RTCStatsReport> report =
3253 callee()->NewGetStats();
3254 ASSERT_NE(nullptr, report);
3255
3256 auto media_stats = report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
3257 auto audio_index = FindFirstMediaStatsIndexByKind("audio", media_stats);
3258 ASSERT_GE(audio_index, 0);
3259 EXPECT_TRUE(media_stats[audio_index]->audio_level.is_defined());
deadbeef1dcb1642017-03-29 21:08:16 -07003260}
3261
deadbeef4e2deab2017-09-20 13:56:21 -07003262// Helper for test below.
3263void ModifySsrcs(cricket::SessionDescription* desc) {
3264 for (ContentInfo& content : desc->contents()) {
Steve Antondf527fd2018-04-27 15:52:03 -07003265 for (StreamParams& stream :
Steve Antonb1c1de12017-12-21 15:14:30 -08003266 content.media_description()->mutable_streams()) {
deadbeef4e2deab2017-09-20 13:56:21 -07003267 for (uint32_t& ssrc : stream.ssrcs) {
3268 ssrc = rtc::CreateRandomId();
3269 }
3270 }
3271 }
3272}
3273
3274// Test that the "RTCMediaSteamTrackStats" object is updated correctly when
3275// SSRCs are unsignaled, and the SSRC of the received (audio) stream changes.
3276// This should result in two "RTCInboundRTPStreamStats", but only one
3277// "RTCMediaStreamTrackStats", whose counters go up continuously rather than
3278// being reset to 0 once the SSRC change occurs.
3279//
3280// Regression test for this bug:
3281// https://bugs.chromium.org/p/webrtc/issues/detail?id=8158
3282//
3283// The bug causes the track stats to only represent one of the two streams:
3284// whichever one has the higher SSRC. So with this bug, there was a 50% chance
3285// that the track stat counters would reset to 0 when the new stream is
3286// received, and a 50% chance that they'll stop updating (while
3287// "concealed_samples" continues increasing, due to silence being generated for
3288// the inactive stream).
Seth Hampson2f0d7022018-02-20 11:54:42 -08003289TEST_P(PeerConnectionIntegrationTest,
Steve Anton83119dd2017-11-10 16:19:52 -08003290 TrackStatsUpdatedCorrectlyWhenUnsignaledSsrcChanges) {
deadbeef4e2deab2017-09-20 13:56:21 -07003291 ASSERT_TRUE(CreatePeerConnectionWrappers());
3292 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08003293 caller()->AddAudioTrack();
deadbeef4e2deab2017-09-20 13:56:21 -07003294 // Remove SSRCs and MSIDs from the received offer SDP, simulating an endpoint
3295 // that doesn't signal SSRCs (from the callee's perspective).
3296 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
3297 caller()->CreateAndSetAndSignalOffer();
3298 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3299 // Wait for 50 audio frames (500ms of audio) to be received by the callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003300 {
3301 MediaExpectations media_expectations;
3302 media_expectations.CalleeExpectsSomeAudio(50);
3303 ASSERT_TRUE(ExpectNewFrames(media_expectations));
3304 }
deadbeef4e2deab2017-09-20 13:56:21 -07003305 // Some audio frames were received, so we should have nonzero "samples
3306 // received" for the track.
3307 rtc::scoped_refptr<const webrtc::RTCStatsReport> report =
3308 callee()->NewGetStats();
3309 ASSERT_NE(nullptr, report);
3310 auto track_stats = report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
3311 ASSERT_EQ(1U, track_stats.size());
3312 ASSERT_TRUE(track_stats[0]->total_samples_received.is_defined());
3313 ASSERT_GT(*track_stats[0]->total_samples_received, 0U);
3314 // uint64_t prev_samples_received = *track_stats[0]->total_samples_received;
3315
3316 // Create a new offer and munge it to cause the caller to use a new SSRC.
3317 caller()->SetGeneratedSdpMunger(ModifySsrcs);
3318 caller()->CreateAndSetAndSignalOffer();
3319 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3320 // Wait for 25 more audio frames (250ms of audio) to be received, from the new
3321 // SSRC.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003322 {
3323 MediaExpectations media_expectations;
3324 media_expectations.CalleeExpectsSomeAudio(25);
3325 ASSERT_TRUE(ExpectNewFrames(media_expectations));
3326 }
deadbeef4e2deab2017-09-20 13:56:21 -07003327
3328 report = callee()->NewGetStats();
3329 ASSERT_NE(nullptr, report);
3330 track_stats = report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
3331 ASSERT_EQ(1U, track_stats.size());
3332 ASSERT_TRUE(track_stats[0]->total_samples_received.is_defined());
3333 // The "total samples received" stat should only be greater than it was
3334 // before.
3335 // TODO(deadbeef): Uncomment this assertion once the bug is completely fixed.
3336 // Right now, the new SSRC will cause the counters to reset to 0.
3337 // EXPECT_GT(*track_stats[0]->total_samples_received, prev_samples_received);
3338
3339 // Additionally, the percentage of concealed samples (samples generated to
Steve Anton83119dd2017-11-10 16:19:52 -08003340 // conceal packet loss) should be less than 50%. If it's greater, that's a
deadbeef4e2deab2017-09-20 13:56:21 -07003341 // good sign that we're seeing stats from the old stream that's no longer
3342 // receiving packets, and is generating concealed samples of silence.
Steve Anton83119dd2017-11-10 16:19:52 -08003343 constexpr double kAcceptableConcealedSamplesPercentage = 0.50;
deadbeef4e2deab2017-09-20 13:56:21 -07003344 ASSERT_TRUE(track_stats[0]->concealed_samples.is_defined());
3345 EXPECT_LT(*track_stats[0]->concealed_samples,
3346 *track_stats[0]->total_samples_received *
3347 kAcceptableConcealedSamplesPercentage);
3348
3349 // Also ensure that we have two "RTCInboundRTPStreamStats" as expected, as a
3350 // sanity check that the SSRC really changed.
3351 // TODO(deadbeef): This isn't working right now, because we're not returning
3352 // *any* stats for the inactive stream. Uncomment when the bug is completely
3353 // fixed.
3354 // auto inbound_stream_stats =
3355 // report->GetStatsOfType<webrtc::RTCInboundRTPStreamStats>();
3356 // ASSERT_EQ(2U, inbound_stream_stats.size());
3357}
3358
deadbeef1dcb1642017-03-29 21:08:16 -07003359// Test that DTLS 1.0 is used if both sides only support DTLS 1.0.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003360TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithDtls10) {
deadbeef1dcb1642017-03-29 21:08:16 -07003361 PeerConnectionFactory::Options dtls_10_options;
3362 dtls_10_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
3363 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(dtls_10_options,
3364 dtls_10_options));
3365 ConnectFakeSignaling();
3366 // Do normal offer/answer and wait for some frames to be received in each
3367 // direction.
Steve Anton15324772018-01-16 10:26:49 -08003368 caller()->AddAudioVideoTracks();
3369 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003370 caller()->CreateAndSetAndSignalOffer();
3371 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08003372 MediaExpectations media_expectations;
3373 media_expectations.ExpectBidirectionalAudioAndVideo();
3374 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003375}
3376
3377// Test getting cipher stats and UMA metrics when DTLS 1.0 is negotiated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003378TEST_P(PeerConnectionIntegrationTest, Dtls10CipherStatsAndUmaMetrics) {
deadbeef1dcb1642017-03-29 21:08:16 -07003379 PeerConnectionFactory::Options dtls_10_options;
3380 dtls_10_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
3381 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(dtls_10_options,
3382 dtls_10_options));
3383 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08003384 caller()->AddAudioVideoTracks();
3385 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003386 caller()->CreateAndSetAndSignalOffer();
Qingsi Wang7fc821d2018-07-12 12:54:53 -07003387 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07003388 EXPECT_TRUE_WAIT(rtc::SSLStreamAdapter::IsAcceptableCipher(
deadbeefd8ad7882017-04-18 16:01:17 -07003389 caller()->OldGetStats()->DtlsCipher(), rtc::KT_DEFAULT),
deadbeef1dcb1642017-03-29 21:08:16 -07003390 kDefaultTimeout);
3391 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
deadbeefd8ad7882017-04-18 16:01:17 -07003392 caller()->OldGetStats()->SrtpCipher(), kDefaultTimeout);
Qingsi Wang7fc821d2018-07-12 12:54:53 -07003393 // TODO(bugs.webrtc.org/9456): Fix it.
Ying Wangef3998f2019-12-09 13:06:53 +01003394 EXPECT_METRIC_EQ(1, webrtc::metrics::NumEvents(
3395 "WebRTC.PeerConnection.SrtpCryptoSuite.Audio",
3396 kDefaultSrtpCryptoSuite));
deadbeef1dcb1642017-03-29 21:08:16 -07003397}
3398
3399// Test getting cipher stats and UMA metrics when DTLS 1.2 is negotiated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003400TEST_P(PeerConnectionIntegrationTest, Dtls12CipherStatsAndUmaMetrics) {
deadbeef1dcb1642017-03-29 21:08:16 -07003401 PeerConnectionFactory::Options dtls_12_options;
3402 dtls_12_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
3403 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(dtls_12_options,
3404 dtls_12_options));
3405 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08003406 caller()->AddAudioVideoTracks();
3407 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003408 caller()->CreateAndSetAndSignalOffer();
Qingsi Wang7fc821d2018-07-12 12:54:53 -07003409 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07003410 EXPECT_TRUE_WAIT(rtc::SSLStreamAdapter::IsAcceptableCipher(
deadbeefd8ad7882017-04-18 16:01:17 -07003411 caller()->OldGetStats()->DtlsCipher(), rtc::KT_DEFAULT),
deadbeef1dcb1642017-03-29 21:08:16 -07003412 kDefaultTimeout);
3413 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
deadbeefd8ad7882017-04-18 16:01:17 -07003414 caller()->OldGetStats()->SrtpCipher(), kDefaultTimeout);
Qingsi Wang7fc821d2018-07-12 12:54:53 -07003415 // TODO(bugs.webrtc.org/9456): Fix it.
Ying Wangef3998f2019-12-09 13:06:53 +01003416 EXPECT_METRIC_EQ(1, webrtc::metrics::NumEvents(
3417 "WebRTC.PeerConnection.SrtpCryptoSuite.Audio",
3418 kDefaultSrtpCryptoSuite));
deadbeef1dcb1642017-03-29 21:08:16 -07003419}
3420
3421// Test that DTLS 1.0 can be used if the caller supports DTLS 1.2 and the
3422// callee only supports 1.0.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003423TEST_P(PeerConnectionIntegrationTest, CallerDtls12ToCalleeDtls10) {
deadbeef1dcb1642017-03-29 21:08:16 -07003424 PeerConnectionFactory::Options caller_options;
3425 caller_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
3426 PeerConnectionFactory::Options callee_options;
3427 callee_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
3428 ASSERT_TRUE(
3429 CreatePeerConnectionWrappersWithOptions(caller_options, callee_options));
3430 ConnectFakeSignaling();
3431 // Do normal offer/answer and wait for some frames to be received in each
3432 // direction.
Steve Anton15324772018-01-16 10:26:49 -08003433 caller()->AddAudioVideoTracks();
3434 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003435 caller()->CreateAndSetAndSignalOffer();
3436 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08003437 MediaExpectations media_expectations;
3438 media_expectations.ExpectBidirectionalAudioAndVideo();
3439 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003440}
3441
3442// Test that DTLS 1.0 can be used if the caller only supports DTLS 1.0 and the
3443// callee supports 1.2.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003444TEST_P(PeerConnectionIntegrationTest, CallerDtls10ToCalleeDtls12) {
deadbeef1dcb1642017-03-29 21:08:16 -07003445 PeerConnectionFactory::Options caller_options;
3446 caller_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
3447 PeerConnectionFactory::Options callee_options;
3448 callee_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
3449 ASSERT_TRUE(
3450 CreatePeerConnectionWrappersWithOptions(caller_options, callee_options));
3451 ConnectFakeSignaling();
3452 // Do normal offer/answer and wait for some frames to be received in each
3453 // direction.
Steve Anton15324772018-01-16 10:26:49 -08003454 caller()->AddAudioVideoTracks();
3455 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003456 caller()->CreateAndSetAndSignalOffer();
3457 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08003458 MediaExpectations media_expectations;
3459 media_expectations.ExpectBidirectionalAudioAndVideo();
3460 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003461}
3462
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07003463// The three tests below verify that "enable_aes128_sha1_32_crypto_cipher"
3464// works as expected; the cipher should only be used if enabled by both sides.
3465TEST_P(PeerConnectionIntegrationTest,
3466 Aes128Sha1_32_CipherNotUsedWhenOnlyCallerSupported) {
3467 PeerConnectionFactory::Options caller_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07003468 caller_options.crypto_options.srtp.enable_aes128_sha1_32_crypto_cipher = true;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07003469 PeerConnectionFactory::Options callee_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07003470 callee_options.crypto_options.srtp.enable_aes128_sha1_32_crypto_cipher =
3471 false;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07003472 int expected_cipher_suite = rtc::SRTP_AES128_CM_SHA1_80;
3473 TestNegotiatedCipherSuite(caller_options, callee_options,
3474 expected_cipher_suite);
3475}
3476
3477TEST_P(PeerConnectionIntegrationTest,
3478 Aes128Sha1_32_CipherNotUsedWhenOnlyCalleeSupported) {
3479 PeerConnectionFactory::Options caller_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07003480 caller_options.crypto_options.srtp.enable_aes128_sha1_32_crypto_cipher =
3481 false;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07003482 PeerConnectionFactory::Options callee_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07003483 callee_options.crypto_options.srtp.enable_aes128_sha1_32_crypto_cipher = true;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07003484 int expected_cipher_suite = rtc::SRTP_AES128_CM_SHA1_80;
3485 TestNegotiatedCipherSuite(caller_options, callee_options,
3486 expected_cipher_suite);
3487}
3488
3489TEST_P(PeerConnectionIntegrationTest, Aes128Sha1_32_CipherUsedWhenSupported) {
3490 PeerConnectionFactory::Options caller_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07003491 caller_options.crypto_options.srtp.enable_aes128_sha1_32_crypto_cipher = true;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07003492 PeerConnectionFactory::Options callee_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07003493 callee_options.crypto_options.srtp.enable_aes128_sha1_32_crypto_cipher = true;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07003494 int expected_cipher_suite = rtc::SRTP_AES128_CM_SHA1_32;
3495 TestNegotiatedCipherSuite(caller_options, callee_options,
3496 expected_cipher_suite);
3497}
3498
deadbeef1dcb1642017-03-29 21:08:16 -07003499// Test that a non-GCM cipher is used if both sides only support non-GCM.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003500TEST_P(PeerConnectionIntegrationTest, NonGcmCipherUsedWhenGcmNotSupported) {
deadbeef1dcb1642017-03-29 21:08:16 -07003501 bool local_gcm_enabled = false;
3502 bool remote_gcm_enabled = false;
Philipp Hancke2ebbff82019-10-26 06:12:55 +02003503 bool aes_ctr_enabled = true;
deadbeef1dcb1642017-03-29 21:08:16 -07003504 int expected_cipher_suite = kDefaultSrtpCryptoSuite;
3505 TestGcmNegotiationUsesCipherSuite(local_gcm_enabled, remote_gcm_enabled,
Philipp Hancke2ebbff82019-10-26 06:12:55 +02003506 aes_ctr_enabled, expected_cipher_suite);
deadbeef1dcb1642017-03-29 21:08:16 -07003507}
3508
Philipp Hancke2ebbff82019-10-26 06:12:55 +02003509// Test that a GCM cipher is used if both ends support it and non-GCM is
3510// disabled.
3511TEST_P(PeerConnectionIntegrationTest, GcmCipherUsedWhenOnlyGcmSupported) {
deadbeef1dcb1642017-03-29 21:08:16 -07003512 bool local_gcm_enabled = true;
3513 bool remote_gcm_enabled = true;
Philipp Hancke2ebbff82019-10-26 06:12:55 +02003514 bool aes_ctr_enabled = false;
deadbeef1dcb1642017-03-29 21:08:16 -07003515 int expected_cipher_suite = kDefaultSrtpCryptoSuiteGcm;
3516 TestGcmNegotiationUsesCipherSuite(local_gcm_enabled, remote_gcm_enabled,
Philipp Hancke2ebbff82019-10-26 06:12:55 +02003517 aes_ctr_enabled, expected_cipher_suite);
deadbeef1dcb1642017-03-29 21:08:16 -07003518}
3519
deadbeef7914b8c2017-04-21 03:23:33 -07003520// Verify that media can be transmitted end-to-end when GCM crypto suites are
3521// enabled. Note that the above tests, such as GcmCipherUsedWhenGcmSupported,
3522// only verify that a GCM cipher is negotiated, and not necessarily that SRTP
3523// works with it.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003524TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithGcmCipher) {
deadbeef7914b8c2017-04-21 03:23:33 -07003525 PeerConnectionFactory::Options gcm_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07003526 gcm_options.crypto_options.srtp.enable_gcm_crypto_suites = true;
Philipp Hancke2ebbff82019-10-26 06:12:55 +02003527 gcm_options.crypto_options.srtp.enable_aes128_sha1_80_crypto_cipher = false;
deadbeef7914b8c2017-04-21 03:23:33 -07003528 ASSERT_TRUE(
3529 CreatePeerConnectionWrappersWithOptions(gcm_options, gcm_options));
3530 ConnectFakeSignaling();
3531 // Do normal offer/answer and wait for some frames to be received in each
3532 // direction.
Steve Anton15324772018-01-16 10:26:49 -08003533 caller()->AddAudioVideoTracks();
3534 callee()->AddAudioVideoTracks();
deadbeef7914b8c2017-04-21 03:23:33 -07003535 caller()->CreateAndSetAndSignalOffer();
3536 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08003537 MediaExpectations media_expectations;
3538 media_expectations.ExpectBidirectionalAudioAndVideo();
3539 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef7914b8c2017-04-21 03:23:33 -07003540}
3541
deadbeef1dcb1642017-03-29 21:08:16 -07003542// This test sets up a call between two parties with audio, video and an RTP
3543// data channel.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003544TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithRtpDataChannel) {
Niels Möllerf06f9232018-08-07 12:32:18 +02003545 PeerConnectionInterface::RTCConfiguration rtc_config;
3546 rtc_config.enable_rtp_data_channel = true;
3547 rtc_config.enable_dtls_srtp = false;
3548 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(rtc_config, rtc_config));
deadbeef1dcb1642017-03-29 21:08:16 -07003549 ConnectFakeSignaling();
3550 // Expect that data channel created on caller side will show up for callee as
3551 // well.
3552 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08003553 caller()->AddAudioVideoTracks();
3554 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003555 caller()->CreateAndSetAndSignalOffer();
3556 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3557 // Ensure the existence of the RTP data channel didn't impede audio/video.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003558 MediaExpectations media_expectations;
3559 media_expectations.ExpectBidirectionalAudioAndVideo();
3560 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003561 ASSERT_NE(nullptr, caller()->data_channel());
3562 ASSERT_NE(nullptr, callee()->data_channel());
3563 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3564 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3565
3566 // Ensure data can be sent in both directions.
3567 std::string data = "hello world";
3568 SendRtpDataWithRetries(caller()->data_channel(), data, 5);
3569 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3570 kDefaultTimeout);
3571 SendRtpDataWithRetries(callee()->data_channel(), data, 5);
3572 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3573 kDefaultTimeout);
3574}
3575
Eldar Rellod9ebe012020-03-18 20:41:45 +02003576TEST_P(PeerConnectionIntegrationTest, RtpDataChannelWorksAfterRollback) {
3577 PeerConnectionInterface::RTCConfiguration rtc_config;
3578 rtc_config.enable_rtp_data_channel = true;
3579 rtc_config.enable_dtls_srtp = false;
3580 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(rtc_config, rtc_config));
3581 ConnectFakeSignaling();
3582 auto data_channel = caller()->pc()->CreateDataChannel("label_1", nullptr);
3583 ASSERT_TRUE(data_channel.get() != nullptr);
3584 caller()->CreateAndSetAndSignalOffer();
3585 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3586
3587 caller()->CreateDataChannel("label_2", nullptr);
3588 rtc::scoped_refptr<MockSetSessionDescriptionObserver> observer(
3589 new rtc::RefCountedObject<MockSetSessionDescriptionObserver>());
3590 caller()->pc()->SetLocalDescription(observer,
3591 caller()->CreateOfferAndWait().release());
3592 EXPECT_TRUE_WAIT(observer->called(), kDefaultTimeout);
3593 caller()->Rollback();
3594
3595 std::string data = "hello world";
3596 SendRtpDataWithRetries(data_channel, data, 5);
3597 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3598 kDefaultTimeout);
3599}
3600
deadbeef1dcb1642017-03-29 21:08:16 -07003601// Ensure that an RTP data channel is signaled as closed for the caller when
3602// the callee rejects it in a subsequent offer.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003603TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07003604 RtpDataChannelSignaledClosedInCalleeOffer) {
3605 // Same procedure as above test.
Niels Möllerf06f9232018-08-07 12:32:18 +02003606 PeerConnectionInterface::RTCConfiguration rtc_config;
3607 rtc_config.enable_rtp_data_channel = true;
3608 rtc_config.enable_dtls_srtp = false;
3609 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(rtc_config, rtc_config));
deadbeef1dcb1642017-03-29 21:08:16 -07003610 ConnectFakeSignaling();
3611 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08003612 caller()->AddAudioVideoTracks();
3613 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003614 caller()->CreateAndSetAndSignalOffer();
3615 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3616 ASSERT_NE(nullptr, caller()->data_channel());
3617 ASSERT_NE(nullptr, callee()->data_channel());
3618 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3619 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3620
3621 // Close the data channel on the callee, and do an updated offer/answer.
3622 callee()->data_channel()->Close();
3623 callee()->CreateAndSetAndSignalOffer();
3624 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3625 EXPECT_FALSE(caller()->data_observer()->IsOpen());
3626 EXPECT_FALSE(callee()->data_observer()->IsOpen());
3627}
3628
3629// Tests that data is buffered in an RTP data channel until an observer is
3630// registered for it.
3631//
3632// NOTE: RTP data channels can receive data before the underlying
3633// transport has detected that a channel is writable and thus data can be
3634// received before the data channel state changes to open. That is hard to test
3635// but the same buffering is expected to be used in that case.
Yves Gerey100fe632020-01-17 19:15:53 +01003636//
3637// Use fake clock and simulated network delay so that we predictably can wait
3638// until an SCTP message has been delivered without "sleep()"ing.
3639TEST_P(PeerConnectionIntegrationTestWithFakeClock,
deadbeef1dcb1642017-03-29 21:08:16 -07003640 DataBufferedUntilRtpDataChannelObserverRegistered) {
deadbeef1dcb1642017-03-29 21:08:16 -07003641 virtual_socket_server()->set_delay_mean(5); // 5 ms per hop.
3642 virtual_socket_server()->UpdateDelayDistribution();
3643
Niels Möllerf06f9232018-08-07 12:32:18 +02003644 PeerConnectionInterface::RTCConfiguration rtc_config;
3645 rtc_config.enable_rtp_data_channel = true;
3646 rtc_config.enable_dtls_srtp = false;
3647 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(rtc_config, rtc_config));
deadbeef1dcb1642017-03-29 21:08:16 -07003648 ConnectFakeSignaling();
3649 caller()->CreateDataChannel();
3650 caller()->CreateAndSetAndSignalOffer();
3651 ASSERT_TRUE(caller()->data_channel() != nullptr);
3652 ASSERT_TRUE_SIMULATED_WAIT(callee()->data_channel() != nullptr,
Yves Gerey100fe632020-01-17 19:15:53 +01003653 kDefaultTimeout, FakeClock());
deadbeef1dcb1642017-03-29 21:08:16 -07003654 ASSERT_TRUE_SIMULATED_WAIT(caller()->data_observer()->IsOpen(),
Yves Gerey100fe632020-01-17 19:15:53 +01003655 kDefaultTimeout, FakeClock());
deadbeef1dcb1642017-03-29 21:08:16 -07003656 ASSERT_EQ_SIMULATED_WAIT(DataChannelInterface::kOpen,
3657 callee()->data_channel()->state(), kDefaultTimeout,
Yves Gerey100fe632020-01-17 19:15:53 +01003658 FakeClock());
deadbeef1dcb1642017-03-29 21:08:16 -07003659
3660 // Unregister the observer which is normally automatically registered.
3661 callee()->data_channel()->UnregisterObserver();
3662 // Send data and advance fake clock until it should have been received.
3663 std::string data = "hello world";
3664 caller()->data_channel()->Send(DataBuffer(data));
Yves Gerey100fe632020-01-17 19:15:53 +01003665 SIMULATED_WAIT(false, 50, FakeClock());
deadbeef1dcb1642017-03-29 21:08:16 -07003666
3667 // Attach data channel and expect data to be received immediately. Note that
3668 // EXPECT_EQ_WAIT is used, such that the simulated clock is not advanced any
3669 // further, but data can be received even if the callback is asynchronous.
3670 MockDataChannelObserver new_observer(callee()->data_channel());
3671 EXPECT_EQ_SIMULATED_WAIT(data, new_observer.last_message(), kDefaultTimeout,
Yves Gerey100fe632020-01-17 19:15:53 +01003672 FakeClock());
deadbeef1dcb1642017-03-29 21:08:16 -07003673}
3674
3675// This test sets up a call between two parties with audio, video and but only
3676// the caller client supports RTP data channels.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003677TEST_P(PeerConnectionIntegrationTest, RtpDataChannelsRejectedByCallee) {
Niels Möllerf06f9232018-08-07 12:32:18 +02003678 PeerConnectionInterface::RTCConfiguration rtc_config_1;
3679 rtc_config_1.enable_rtp_data_channel = true;
deadbeef1dcb1642017-03-29 21:08:16 -07003680 // Must disable DTLS to make negotiation succeed.
Niels Möllerf06f9232018-08-07 12:32:18 +02003681 rtc_config_1.enable_dtls_srtp = false;
3682 PeerConnectionInterface::RTCConfiguration rtc_config_2;
3683 rtc_config_2.enable_dtls_srtp = false;
3684 rtc_config_2.enable_dtls_srtp = false;
3685 ASSERT_TRUE(
3686 CreatePeerConnectionWrappersWithConfig(rtc_config_1, rtc_config_2));
deadbeef1dcb1642017-03-29 21:08:16 -07003687 ConnectFakeSignaling();
3688 caller()->CreateDataChannel();
Harald Alvestrandf3736ed2019-04-08 13:09:30 +02003689 ASSERT_TRUE(caller()->data_channel() != nullptr);
Steve Anton15324772018-01-16 10:26:49 -08003690 caller()->AddAudioVideoTracks();
3691 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003692 caller()->CreateAndSetAndSignalOffer();
3693 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3694 // The caller should still have a data channel, but it should be closed, and
3695 // one should ever have been created for the callee.
3696 EXPECT_TRUE(caller()->data_channel() != nullptr);
3697 EXPECT_FALSE(caller()->data_observer()->IsOpen());
3698 EXPECT_EQ(nullptr, callee()->data_channel());
3699}
3700
3701// This test sets up a call between two parties with audio, and video. When
3702// audio and video is setup and flowing, an RTP data channel is negotiated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003703TEST_P(PeerConnectionIntegrationTest, AddRtpDataChannelInSubsequentOffer) {
Niels Möllerf06f9232018-08-07 12:32:18 +02003704 PeerConnectionInterface::RTCConfiguration rtc_config;
3705 rtc_config.enable_rtp_data_channel = true;
3706 rtc_config.enable_dtls_srtp = false;
3707 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(rtc_config, rtc_config));
deadbeef1dcb1642017-03-29 21:08:16 -07003708 ConnectFakeSignaling();
3709 // Do initial offer/answer with audio/video.
Steve Anton15324772018-01-16 10:26:49 -08003710 caller()->AddAudioVideoTracks();
3711 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003712 caller()->CreateAndSetAndSignalOffer();
3713 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3714 // Create data channel and do new offer and answer.
3715 caller()->CreateDataChannel();
3716 caller()->CreateAndSetAndSignalOffer();
3717 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3718 ASSERT_NE(nullptr, caller()->data_channel());
3719 ASSERT_NE(nullptr, callee()->data_channel());
3720 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3721 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3722 // Ensure data can be sent in both directions.
3723 std::string data = "hello world";
3724 SendRtpDataWithRetries(caller()->data_channel(), data, 5);
3725 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3726 kDefaultTimeout);
3727 SendRtpDataWithRetries(callee()->data_channel(), data, 5);
3728 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3729 kDefaultTimeout);
3730}
3731
3732#ifdef HAVE_SCTP
3733
3734// This test sets up a call between two parties with audio, video and an SCTP
3735// data channel.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003736TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithSctpDataChannel) {
deadbeef1dcb1642017-03-29 21:08:16 -07003737 ASSERT_TRUE(CreatePeerConnectionWrappers());
3738 ConnectFakeSignaling();
3739 // Expect that data channel created on caller side will show up for callee as
3740 // well.
3741 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08003742 caller()->AddAudioVideoTracks();
3743 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003744 caller()->CreateAndSetAndSignalOffer();
3745 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3746 // Ensure the existence of the SCTP data channel didn't impede audio/video.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003747 MediaExpectations media_expectations;
3748 media_expectations.ExpectBidirectionalAudioAndVideo();
3749 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003750 // Caller data channel should already exist (it created one). Callee data
3751 // channel may not exist yet, since negotiation happens in-band, not in SDP.
3752 ASSERT_NE(nullptr, caller()->data_channel());
3753 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3754 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3755 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3756
3757 // Ensure data can be sent in both directions.
3758 std::string data = "hello world";
3759 caller()->data_channel()->Send(DataBuffer(data));
3760 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3761 kDefaultTimeout);
3762 callee()->data_channel()->Send(DataBuffer(data));
3763 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3764 kDefaultTimeout);
3765}
3766
3767// Ensure that when the callee closes an SCTP data channel, the closing
3768// procedure results in the data channel being closed for the caller as well.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003769TEST_P(PeerConnectionIntegrationTest, CalleeClosesSctpDataChannel) {
deadbeef1dcb1642017-03-29 21:08:16 -07003770 // Same procedure as above test.
3771 ASSERT_TRUE(CreatePeerConnectionWrappers());
3772 ConnectFakeSignaling();
3773 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08003774 caller()->AddAudioVideoTracks();
3775 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003776 caller()->CreateAndSetAndSignalOffer();
3777 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3778 ASSERT_NE(nullptr, caller()->data_channel());
3779 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3780 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3781 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3782
3783 // Close the data channel on the callee side, and wait for it to reach the
3784 // "closed" state on both sides.
3785 callee()->data_channel()->Close();
3786 EXPECT_TRUE_WAIT(!caller()->data_observer()->IsOpen(), kDefaultTimeout);
3787 EXPECT_TRUE_WAIT(!callee()->data_observer()->IsOpen(), kDefaultTimeout);
3788}
3789
Seth Hampson2f0d7022018-02-20 11:54:42 -08003790TEST_P(PeerConnectionIntegrationTest, SctpDataChannelConfigSentToOtherSide) {
Steve Antonda6c0952017-10-23 11:41:54 -07003791 ASSERT_TRUE(CreatePeerConnectionWrappers());
3792 ConnectFakeSignaling();
3793 webrtc::DataChannelInit init;
3794 init.id = 53;
3795 init.maxRetransmits = 52;
3796 caller()->CreateDataChannel("data-channel", &init);
Steve Anton15324772018-01-16 10:26:49 -08003797 caller()->AddAudioVideoTracks();
3798 callee()->AddAudioVideoTracks();
Steve Antonda6c0952017-10-23 11:41:54 -07003799 caller()->CreateAndSetAndSignalOffer();
3800 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Steve Anton074dece2017-10-24 13:04:12 -07003801 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3802 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
Harald Alvestrand5c4d2ee2019-04-01 12:58:15 +02003803 // Since "negotiated" is false, the "id" parameter should be ignored.
3804 EXPECT_NE(init.id, callee()->data_channel()->id());
Steve Antonda6c0952017-10-23 11:41:54 -07003805 EXPECT_EQ("data-channel", callee()->data_channel()->label());
3806 EXPECT_EQ(init.maxRetransmits, callee()->data_channel()->maxRetransmits());
3807 EXPECT_FALSE(callee()->data_channel()->negotiated());
3808}
3809
deadbeef1dcb1642017-03-29 21:08:16 -07003810// Test usrsctp's ability to process unordered data stream, where data actually
3811// arrives out of order using simulated delays. Previously there have been some
3812// bugs in this area.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003813TEST_P(PeerConnectionIntegrationTest, StressTestUnorderedSctpDataChannel) {
deadbeef1dcb1642017-03-29 21:08:16 -07003814 // Introduce random network delays.
3815 // Otherwise it's not a true "unordered" test.
3816 virtual_socket_server()->set_delay_mean(20);
3817 virtual_socket_server()->set_delay_stddev(5);
3818 virtual_socket_server()->UpdateDelayDistribution();
3819 // Normal procedure, but with unordered data channel config.
3820 ASSERT_TRUE(CreatePeerConnectionWrappers());
3821 ConnectFakeSignaling();
3822 webrtc::DataChannelInit init;
3823 init.ordered = false;
3824 caller()->CreateDataChannel(&init);
3825 caller()->CreateAndSetAndSignalOffer();
3826 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3827 ASSERT_NE(nullptr, caller()->data_channel());
3828 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3829 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3830 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3831
3832 static constexpr int kNumMessages = 100;
3833 // Deliberately chosen to be larger than the MTU so messages get fragmented.
3834 static constexpr size_t kMaxMessageSize = 4096;
3835 // Create and send random messages.
3836 std::vector<std::string> sent_messages;
3837 for (int i = 0; i < kNumMessages; ++i) {
3838 size_t length =
3839 (rand() % kMaxMessageSize) + 1; // NOLINT (rand_r instead of rand)
3840 std::string message;
3841 ASSERT_TRUE(rtc::CreateRandomString(length, &message));
3842 caller()->data_channel()->Send(DataBuffer(message));
3843 callee()->data_channel()->Send(DataBuffer(message));
3844 sent_messages.push_back(message);
3845 }
3846
3847 // Wait for all messages to be received.
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02003848 EXPECT_EQ_WAIT(rtc::checked_cast<size_t>(kNumMessages),
deadbeef1dcb1642017-03-29 21:08:16 -07003849 caller()->data_observer()->received_message_count(),
3850 kDefaultTimeout);
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02003851 EXPECT_EQ_WAIT(rtc::checked_cast<size_t>(kNumMessages),
deadbeef1dcb1642017-03-29 21:08:16 -07003852 callee()->data_observer()->received_message_count(),
3853 kDefaultTimeout);
3854
3855 // Sort and compare to make sure none of the messages were corrupted.
3856 std::vector<std::string> caller_received_messages =
3857 caller()->data_observer()->messages();
3858 std::vector<std::string> callee_received_messages =
3859 callee()->data_observer()->messages();
Steve Anton64b626b2019-01-28 17:25:26 -08003860 absl::c_sort(sent_messages);
3861 absl::c_sort(caller_received_messages);
3862 absl::c_sort(callee_received_messages);
deadbeef1dcb1642017-03-29 21:08:16 -07003863 EXPECT_EQ(sent_messages, caller_received_messages);
3864 EXPECT_EQ(sent_messages, callee_received_messages);
3865}
3866
3867// This test sets up a call between two parties with audio, and video. When
3868// audio and video are setup and flowing, an SCTP data channel is negotiated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003869TEST_P(PeerConnectionIntegrationTest, AddSctpDataChannelInSubsequentOffer) {
deadbeef1dcb1642017-03-29 21:08:16 -07003870 ASSERT_TRUE(CreatePeerConnectionWrappers());
3871 ConnectFakeSignaling();
3872 // Do initial offer/answer with audio/video.
Steve Anton15324772018-01-16 10:26:49 -08003873 caller()->AddAudioVideoTracks();
3874 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003875 caller()->CreateAndSetAndSignalOffer();
3876 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3877 // Create data channel and do new offer and answer.
3878 caller()->CreateDataChannel();
3879 caller()->CreateAndSetAndSignalOffer();
3880 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3881 // Caller data channel should already exist (it created one). Callee data
3882 // channel may not exist yet, since negotiation happens in-band, not in SDP.
3883 ASSERT_NE(nullptr, caller()->data_channel());
3884 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3885 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3886 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3887 // Ensure data can be sent in both directions.
3888 std::string data = "hello world";
3889 caller()->data_channel()->Send(DataBuffer(data));
3890 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3891 kDefaultTimeout);
3892 callee()->data_channel()->Send(DataBuffer(data));
3893 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3894 kDefaultTimeout);
3895}
3896
deadbeef7914b8c2017-04-21 03:23:33 -07003897// Set up a connection initially just using SCTP data channels, later upgrading
3898// to audio/video, ensuring frames are received end-to-end. Effectively the
3899// inverse of the test above.
3900// This was broken in M57; see https://crbug.com/711243
Seth Hampson2f0d7022018-02-20 11:54:42 -08003901TEST_P(PeerConnectionIntegrationTest, SctpDataChannelToAudioVideoUpgrade) {
deadbeef7914b8c2017-04-21 03:23:33 -07003902 ASSERT_TRUE(CreatePeerConnectionWrappers());
3903 ConnectFakeSignaling();
3904 // Do initial offer/answer with just data channel.
3905 caller()->CreateDataChannel();
3906 caller()->CreateAndSetAndSignalOffer();
3907 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3908 // Wait until data can be sent over the data channel.
3909 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3910 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3911 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3912
3913 // Do subsequent offer/answer with two-way audio and video. Audio and video
3914 // should end up bundled on the DTLS/ICE transport already used for data.
Steve Anton15324772018-01-16 10:26:49 -08003915 caller()->AddAudioVideoTracks();
3916 callee()->AddAudioVideoTracks();
deadbeef7914b8c2017-04-21 03:23:33 -07003917 caller()->CreateAndSetAndSignalOffer();
3918 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08003919 MediaExpectations media_expectations;
3920 media_expectations.ExpectBidirectionalAudioAndVideo();
3921 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef7914b8c2017-04-21 03:23:33 -07003922}
3923
deadbeef8b7e9ad2017-05-25 09:38:55 -07003924static void MakeSpecCompliantSctpOffer(cricket::SessionDescription* desc) {
Harald Alvestrand5fc28b12019-05-13 13:36:16 +02003925 cricket::SctpDataContentDescription* dcd_offer =
3926 GetFirstSctpDataContentDescription(desc);
Harald Alvestrand17ea0682019-12-13 11:51:04 +01003927 // See https://crbug.com/webrtc/11211 - this function is a no-op
Steve Antonb1c1de12017-12-21 15:14:30 -08003928 ASSERT_TRUE(dcd_offer);
deadbeef8b7e9ad2017-05-25 09:38:55 -07003929 dcd_offer->set_use_sctpmap(false);
3930 dcd_offer->set_protocol("UDP/DTLS/SCTP");
3931}
3932
3933// Test that the data channel works when a spec-compliant SCTP m= section is
3934// offered (using "a=sctp-port" instead of "a=sctpmap", and using
3935// "UDP/DTLS/SCTP" as the protocol).
Seth Hampson2f0d7022018-02-20 11:54:42 -08003936TEST_P(PeerConnectionIntegrationTest,
deadbeef8b7e9ad2017-05-25 09:38:55 -07003937 DataChannelWorksWhenSpecCompliantSctpOfferReceived) {
3938 ASSERT_TRUE(CreatePeerConnectionWrappers());
3939 ConnectFakeSignaling();
3940 caller()->CreateDataChannel();
3941 caller()->SetGeneratedSdpMunger(MakeSpecCompliantSctpOffer);
3942 caller()->CreateAndSetAndSignalOffer();
3943 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3944 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3945 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3946 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3947
3948 // Ensure data can be sent in both directions.
3949 std::string data = "hello world";
3950 caller()->data_channel()->Send(DataBuffer(data));
3951 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3952 kDefaultTimeout);
3953 callee()->data_channel()->Send(DataBuffer(data));
3954 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3955 kDefaultTimeout);
3956}
3957
deadbeef1dcb1642017-03-29 21:08:16 -07003958#endif // HAVE_SCTP
3959
3960// Test that the ICE connection and gathering states eventually reach
3961// "complete".
Seth Hampson2f0d7022018-02-20 11:54:42 -08003962TEST_P(PeerConnectionIntegrationTest, IceStatesReachCompletion) {
deadbeef1dcb1642017-03-29 21:08:16 -07003963 ASSERT_TRUE(CreatePeerConnectionWrappers());
3964 ConnectFakeSignaling();
3965 // Do normal offer/answer.
Steve Anton15324772018-01-16 10:26:49 -08003966 caller()->AddAudioVideoTracks();
3967 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003968 caller()->CreateAndSetAndSignalOffer();
3969 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3970 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceGatheringComplete,
3971 caller()->ice_gathering_state(), kMaxWaitForFramesMs);
3972 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceGatheringComplete,
3973 callee()->ice_gathering_state(), kMaxWaitForFramesMs);
3974 // After the best candidate pair is selected and all candidates are signaled,
3975 // the ICE connection state should reach "complete".
3976 // TODO(deadbeef): Currently, the ICE "controlled" agent (the
3977 // answerer/"callee" by default) only reaches "connected". When this is
3978 // fixed, this test should be updated.
3979 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
3980 caller()->ice_connection_state(), kDefaultTimeout);
Alex Loiko9289eda2018-11-23 16:18:59 +00003981 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
3982 callee()->ice_connection_state(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07003983}
3984
Qingsi Wang1dac6d82018-12-12 15:28:47 -08003985constexpr int kOnlyLocalPorts = cricket::PORTALLOCATOR_DISABLE_STUN |
3986 cricket::PORTALLOCATOR_DISABLE_RELAY |
3987 cricket::PORTALLOCATOR_DISABLE_TCP;
Zach Stein6fcdc2f2018-08-23 16:25:55 -07003988
Qingsi Wang1dac6d82018-12-12 15:28:47 -08003989// Use a mock resolver to resolve the hostname back to the original IP on both
3990// sides and check that the ICE connection connects.
Zach Stein6fcdc2f2018-08-23 16:25:55 -07003991TEST_P(PeerConnectionIntegrationTest,
3992 IceStatesReachCompletionWithRemoteHostname) {
Qingsi Wang1dac6d82018-12-12 15:28:47 -08003993 auto caller_resolver_factory =
Mirko Bonadei317a1f02019-09-17 17:06:18 +02003994 std::make_unique<NiceMock<webrtc::MockAsyncResolverFactory>>();
Qingsi Wang1dac6d82018-12-12 15:28:47 -08003995 auto callee_resolver_factory =
Mirko Bonadei317a1f02019-09-17 17:06:18 +02003996 std::make_unique<NiceMock<webrtc::MockAsyncResolverFactory>>();
Qingsi Wang1dac6d82018-12-12 15:28:47 -08003997 NiceMock<rtc::MockAsyncResolver> callee_async_resolver;
3998 NiceMock<rtc::MockAsyncResolver> caller_async_resolver;
Zach Stein6fcdc2f2018-08-23 16:25:55 -07003999
4000 // This also verifies that the injected AsyncResolverFactory is used by
4001 // P2PTransportChannel.
Qingsi Wang1dac6d82018-12-12 15:28:47 -08004002 EXPECT_CALL(*caller_resolver_factory, Create())
4003 .WillOnce(Return(&caller_async_resolver));
4004 webrtc::PeerConnectionDependencies caller_deps(nullptr);
4005 caller_deps.async_resolver_factory = std::move(caller_resolver_factory);
4006
4007 EXPECT_CALL(*callee_resolver_factory, Create())
4008 .WillOnce(Return(&callee_async_resolver));
4009 webrtc::PeerConnectionDependencies callee_deps(nullptr);
4010 callee_deps.async_resolver_factory = std::move(callee_resolver_factory);
4011
4012 PeerConnectionInterface::RTCConfiguration config;
4013 config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
4014 config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyRequire;
4015
4016 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndDeps(
4017 config, std::move(caller_deps), config, std::move(callee_deps)));
4018
4019 caller()->SetRemoteAsyncResolver(&callee_async_resolver);
4020 callee()->SetRemoteAsyncResolver(&caller_async_resolver);
4021
4022 // Enable hostname candidates with mDNS names.
Qingsi Wangecd30542019-05-22 14:34:56 -07004023 caller()->SetMdnsResponder(
Mirko Bonadei317a1f02019-09-17 17:06:18 +02004024 std::make_unique<webrtc::FakeMdnsResponder>(network_thread()));
Qingsi Wangecd30542019-05-22 14:34:56 -07004025 callee()->SetMdnsResponder(
Mirko Bonadei317a1f02019-09-17 17:06:18 +02004026 std::make_unique<webrtc::FakeMdnsResponder>(network_thread()));
Qingsi Wang1dac6d82018-12-12 15:28:47 -08004027
4028 SetPortAllocatorFlags(kOnlyLocalPorts, kOnlyLocalPorts);
Zach Stein6fcdc2f2018-08-23 16:25:55 -07004029
4030 ConnectFakeSignaling();
4031 caller()->AddAudioVideoTracks();
4032 callee()->AddAudioVideoTracks();
4033 caller()->CreateAndSetAndSignalOffer();
4034 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4035 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
4036 caller()->ice_connection_state(), kDefaultTimeout);
4037 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
4038 callee()->ice_connection_state(), kDefaultTimeout);
Jeroen de Borst833979f2018-12-13 08:25:54 -08004039
Ying Wangef3998f2019-12-09 13:06:53 +01004040 EXPECT_METRIC_EQ(1, webrtc::metrics::NumEvents(
4041 "WebRTC.PeerConnection.CandidatePairType_UDP",
4042 webrtc::kIceCandidatePairHostNameHostName));
Zach Stein6fcdc2f2018-08-23 16:25:55 -07004043}
4044
Steve Antonede9ca52017-10-16 13:04:27 -07004045// Test that firewalling the ICE connection causes the clients to identify the
4046// disconnected state and then removing the firewall causes them to reconnect.
4047class PeerConnectionIntegrationIceStatesTest
Seth Hampson2f0d7022018-02-20 11:54:42 -08004048 : public PeerConnectionIntegrationBaseTest,
4049 public ::testing::WithParamInterface<
4050 std::tuple<SdpSemantics, std::tuple<std::string, uint32_t>>> {
Steve Antonede9ca52017-10-16 13:04:27 -07004051 protected:
Seth Hampson2f0d7022018-02-20 11:54:42 -08004052 PeerConnectionIntegrationIceStatesTest()
4053 : PeerConnectionIntegrationBaseTest(std::get<0>(GetParam())) {
4054 port_allocator_flags_ = std::get<1>(std::get<1>(GetParam()));
Steve Antonede9ca52017-10-16 13:04:27 -07004055 }
4056
4057 void StartStunServer(const SocketAddress& server_address) {
4058 stun_server_.reset(
4059 cricket::TestStunServer::Create(network_thread(), server_address));
4060 }
4061
4062 bool TestIPv6() {
4063 return (port_allocator_flags_ & cricket::PORTALLOCATOR_ENABLE_IPV6);
4064 }
4065
4066 void SetPortAllocatorFlags() {
Qingsi Wang1dac6d82018-12-12 15:28:47 -08004067 PeerConnectionIntegrationBaseTest::SetPortAllocatorFlags(
4068 port_allocator_flags_, port_allocator_flags_);
Steve Antonede9ca52017-10-16 13:04:27 -07004069 }
4070
4071 std::vector<SocketAddress> CallerAddresses() {
4072 std::vector<SocketAddress> addresses;
4073 addresses.push_back(SocketAddress("1.1.1.1", 0));
4074 if (TestIPv6()) {
4075 addresses.push_back(SocketAddress("1111:0:a:b:c:d:e:f", 0));
4076 }
4077 return addresses;
4078 }
4079
4080 std::vector<SocketAddress> CalleeAddresses() {
4081 std::vector<SocketAddress> addresses;
4082 addresses.push_back(SocketAddress("2.2.2.2", 0));
4083 if (TestIPv6()) {
4084 addresses.push_back(SocketAddress("2222:0:a:b:c:d:e:f", 0));
4085 }
4086 return addresses;
4087 }
4088
4089 void SetUpNetworkInterfaces() {
4090 // Remove the default interfaces added by the test infrastructure.
Qingsi Wangecd30542019-05-22 14:34:56 -07004091 caller()->network_manager()->RemoveInterface(kDefaultLocalAddress);
4092 callee()->network_manager()->RemoveInterface(kDefaultLocalAddress);
Steve Antonede9ca52017-10-16 13:04:27 -07004093
4094 // Add network addresses for test.
4095 for (const auto& caller_address : CallerAddresses()) {
Qingsi Wangecd30542019-05-22 14:34:56 -07004096 caller()->network_manager()->AddInterface(caller_address);
Steve Antonede9ca52017-10-16 13:04:27 -07004097 }
4098 for (const auto& callee_address : CalleeAddresses()) {
Qingsi Wangecd30542019-05-22 14:34:56 -07004099 callee()->network_manager()->AddInterface(callee_address);
Steve Antonede9ca52017-10-16 13:04:27 -07004100 }
4101 }
4102
4103 private:
4104 uint32_t port_allocator_flags_;
4105 std::unique_ptr<cricket::TestStunServer> stun_server_;
4106};
4107
Yves Gerey100fe632020-01-17 19:15:53 +01004108// Ensure FakeClockForTest is constructed first (see class for rationale).
4109class PeerConnectionIntegrationIceStatesTestWithFakeClock
4110 : public FakeClockForTest,
4111 public PeerConnectionIntegrationIceStatesTest {};
4112
Steve Antonede9ca52017-10-16 13:04:27 -07004113// Tests that the PeerConnection goes through all the ICE gathering/connection
4114// states over the duration of the call. This includes Disconnected and Failed
4115// states, induced by putting a firewall between the peers and waiting for them
4116// to time out.
Yves Gerey100fe632020-01-17 19:15:53 +01004117TEST_P(PeerConnectionIntegrationIceStatesTestWithFakeClock, VerifyIceStates) {
Steve Antonede9ca52017-10-16 13:04:27 -07004118 const SocketAddress kStunServerAddress =
4119 SocketAddress("99.99.99.1", cricket::STUN_SERVER_PORT);
4120 StartStunServer(kStunServerAddress);
4121
4122 PeerConnectionInterface::RTCConfiguration config;
4123 PeerConnectionInterface::IceServer ice_stun_server;
4124 ice_stun_server.urls.push_back(
4125 "stun:" + kStunServerAddress.HostAsURIString() + ":" +
4126 kStunServerAddress.PortAsString());
4127 config.servers.push_back(ice_stun_server);
4128
4129 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
4130 ConnectFakeSignaling();
4131 SetPortAllocatorFlags();
4132 SetUpNetworkInterfaces();
Steve Anton15324772018-01-16 10:26:49 -08004133 caller()->AddAudioVideoTracks();
4134 callee()->AddAudioVideoTracks();
Steve Antonede9ca52017-10-16 13:04:27 -07004135
4136 // Initial state before anything happens.
4137 ASSERT_EQ(PeerConnectionInterface::kIceGatheringNew,
4138 caller()->ice_gathering_state());
4139 ASSERT_EQ(PeerConnectionInterface::kIceConnectionNew,
4140 caller()->ice_connection_state());
Jonas Olsson7a6739e2019-01-15 16:31:55 +01004141 ASSERT_EQ(PeerConnectionInterface::kIceConnectionNew,
4142 caller()->standardized_ice_connection_state());
Steve Antonede9ca52017-10-16 13:04:27 -07004143
4144 // Start the call by creating the offer, setting it as the local description,
4145 // then sending it to the peer who will respond with an answer. This happens
4146 // asynchronously so that we can watch the states as it runs in the
4147 // background.
4148 caller()->CreateAndSetAndSignalOffer();
4149
Steve Antona9b67ce2020-01-16 14:00:44 -08004150 ASSERT_EQ_SIMULATED_WAIT(PeerConnectionInterface::kIceConnectionCompleted,
4151 caller()->ice_connection_state(), kDefaultTimeout,
Yves Gerey100fe632020-01-17 19:15:53 +01004152 FakeClock());
Steve Antona9b67ce2020-01-16 14:00:44 -08004153 ASSERT_EQ_SIMULATED_WAIT(PeerConnectionInterface::kIceConnectionCompleted,
4154 caller()->standardized_ice_connection_state(),
Yves Gerey100fe632020-01-17 19:15:53 +01004155 kDefaultTimeout, FakeClock());
Steve Antonede9ca52017-10-16 13:04:27 -07004156
4157 // Verify that the observer was notified of the intermediate transitions.
4158 EXPECT_THAT(caller()->ice_connection_state_history(),
4159 ElementsAre(PeerConnectionInterface::kIceConnectionChecking,
4160 PeerConnectionInterface::kIceConnectionConnected,
4161 PeerConnectionInterface::kIceConnectionCompleted));
Jonas Olssonacd8ae72019-02-25 15:26:24 +01004162 EXPECT_THAT(caller()->standardized_ice_connection_state_history(),
4163 ElementsAre(PeerConnectionInterface::kIceConnectionChecking,
4164 PeerConnectionInterface::kIceConnectionConnected,
4165 PeerConnectionInterface::kIceConnectionCompleted));
Jonas Olsson635474e2018-10-18 15:58:17 +02004166 EXPECT_THAT(
4167 caller()->peer_connection_state_history(),
4168 ElementsAre(PeerConnectionInterface::PeerConnectionState::kConnecting,
Jonas Olsson635474e2018-10-18 15:58:17 +02004169 PeerConnectionInterface::PeerConnectionState::kConnected));
Steve Antonede9ca52017-10-16 13:04:27 -07004170 EXPECT_THAT(caller()->ice_gathering_state_history(),
4171 ElementsAre(PeerConnectionInterface::kIceGatheringGathering,
4172 PeerConnectionInterface::kIceGatheringComplete));
4173
4174 // Block connections to/from the caller and wait for ICE to become
4175 // disconnected.
4176 for (const auto& caller_address : CallerAddresses()) {
4177 firewall()->AddRule(false, rtc::FP_ANY, rtc::FD_ANY, caller_address);
4178 }
Mirko Bonadei675513b2017-11-09 11:09:25 +01004179 RTC_LOG(LS_INFO) << "Firewall rules applied";
Jonas Olssonb75d9e92019-02-22 10:33:29 +01004180 ASSERT_EQ_SIMULATED_WAIT(PeerConnectionInterface::kIceConnectionDisconnected,
4181 caller()->ice_connection_state(), kDefaultTimeout,
Yves Gerey100fe632020-01-17 19:15:53 +01004182 FakeClock());
Jonas Olssonb75d9e92019-02-22 10:33:29 +01004183 ASSERT_EQ_SIMULATED_WAIT(PeerConnectionInterface::kIceConnectionDisconnected,
4184 caller()->standardized_ice_connection_state(),
Yves Gerey100fe632020-01-17 19:15:53 +01004185 kDefaultTimeout, FakeClock());
Steve Antonede9ca52017-10-16 13:04:27 -07004186
4187 // Let ICE re-establish by removing the firewall rules.
4188 firewall()->ClearRules();
Mirko Bonadei675513b2017-11-09 11:09:25 +01004189 RTC_LOG(LS_INFO) << "Firewall rules cleared";
Jonas Olssonb75d9e92019-02-22 10:33:29 +01004190 ASSERT_EQ_SIMULATED_WAIT(PeerConnectionInterface::kIceConnectionCompleted,
4191 caller()->ice_connection_state(), kDefaultTimeout,
Yves Gerey100fe632020-01-17 19:15:53 +01004192 FakeClock());
Jonas Olssonacd8ae72019-02-25 15:26:24 +01004193 ASSERT_EQ_SIMULATED_WAIT(PeerConnectionInterface::kIceConnectionCompleted,
Jonas Olssonb75d9e92019-02-22 10:33:29 +01004194 caller()->standardized_ice_connection_state(),
Yves Gerey100fe632020-01-17 19:15:53 +01004195 kDefaultTimeout, FakeClock());
Steve Antonede9ca52017-10-16 13:04:27 -07004196
4197 // According to RFC7675, if there is no response within 30 seconds then the
4198 // peer should consider the other side to have rejected the connection. This
Steve Anton83119dd2017-11-10 16:19:52 -08004199 // is signaled by the state transitioning to "failed".
Steve Antonede9ca52017-10-16 13:04:27 -07004200 constexpr int kConsentTimeout = 30000;
4201 for (const auto& caller_address : CallerAddresses()) {
4202 firewall()->AddRule(false, rtc::FP_ANY, rtc::FD_ANY, caller_address);
4203 }
Mirko Bonadei675513b2017-11-09 11:09:25 +01004204 RTC_LOG(LS_INFO) << "Firewall rules applied again";
Jonas Olssonb75d9e92019-02-22 10:33:29 +01004205 ASSERT_EQ_SIMULATED_WAIT(PeerConnectionInterface::kIceConnectionFailed,
4206 caller()->ice_connection_state(), kConsentTimeout,
Yves Gerey100fe632020-01-17 19:15:53 +01004207 FakeClock());
Jonas Olssonb75d9e92019-02-22 10:33:29 +01004208 ASSERT_EQ_SIMULATED_WAIT(PeerConnectionInterface::kIceConnectionFailed,
4209 caller()->standardized_ice_connection_state(),
Yves Gerey100fe632020-01-17 19:15:53 +01004210 kConsentTimeout, FakeClock());
Jonas Olssonb75d9e92019-02-22 10:33:29 +01004211}
4212
4213// Tests that if the connection doesn't get set up properly we eventually reach
4214// the "failed" iceConnectionState.
Yves Gerey100fe632020-01-17 19:15:53 +01004215TEST_P(PeerConnectionIntegrationIceStatesTestWithFakeClock,
4216 IceStateSetupFailure) {
Jonas Olssonb75d9e92019-02-22 10:33:29 +01004217 // Block connections to/from the caller and wait for ICE to become
4218 // disconnected.
4219 for (const auto& caller_address : CallerAddresses()) {
4220 firewall()->AddRule(false, rtc::FP_ANY, rtc::FD_ANY, caller_address);
4221 }
4222
4223 ASSERT_TRUE(CreatePeerConnectionWrappers());
4224 ConnectFakeSignaling();
4225 SetPortAllocatorFlags();
4226 SetUpNetworkInterfaces();
4227 caller()->AddAudioVideoTracks();
4228 caller()->CreateAndSetAndSignalOffer();
4229
4230 // According to RFC7675, if there is no response within 30 seconds then the
4231 // peer should consider the other side to have rejected the connection. This
4232 // is signaled by the state transitioning to "failed".
4233 constexpr int kConsentTimeout = 30000;
4234 ASSERT_EQ_SIMULATED_WAIT(PeerConnectionInterface::kIceConnectionFailed,
4235 caller()->standardized_ice_connection_state(),
Yves Gerey100fe632020-01-17 19:15:53 +01004236 kConsentTimeout, FakeClock());
Steve Antonede9ca52017-10-16 13:04:27 -07004237}
4238
4239// Tests that the best connection is set to the appropriate IPv4/IPv6 connection
4240// and that the statistics in the metric observers are updated correctly.
4241TEST_P(PeerConnectionIntegrationIceStatesTest, VerifyBestConnection) {
4242 ASSERT_TRUE(CreatePeerConnectionWrappers());
4243 ConnectFakeSignaling();
4244 SetPortAllocatorFlags();
4245 SetUpNetworkInterfaces();
Steve Anton15324772018-01-16 10:26:49 -08004246 caller()->AddAudioVideoTracks();
4247 callee()->AddAudioVideoTracks();
Steve Antonede9ca52017-10-16 13:04:27 -07004248 caller()->CreateAndSetAndSignalOffer();
4249
4250 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Steve Anton692f3c72020-01-16 14:12:31 -08004251 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
4252 caller()->ice_connection_state(), kDefaultTimeout);
4253 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
4254 callee()->ice_connection_state(), kDefaultTimeout);
Steve Antonede9ca52017-10-16 13:04:27 -07004255
Qingsi Wang7fc821d2018-07-12 12:54:53 -07004256 // TODO(bugs.webrtc.org/9456): Fix it.
4257 const int num_best_ipv4 = webrtc::metrics::NumEvents(
4258 "WebRTC.PeerConnection.IPMetrics", webrtc::kBestConnections_IPv4);
4259 const int num_best_ipv6 = webrtc::metrics::NumEvents(
4260 "WebRTC.PeerConnection.IPMetrics", webrtc::kBestConnections_IPv6);
Steve Antonede9ca52017-10-16 13:04:27 -07004261 if (TestIPv6()) {
4262 // When IPv6 is enabled, we should prefer an IPv6 connection over an IPv4
4263 // connection.
Ying Wangef3998f2019-12-09 13:06:53 +01004264 EXPECT_METRIC_EQ(0, num_best_ipv4);
4265 EXPECT_METRIC_EQ(1, num_best_ipv6);
Steve Antonede9ca52017-10-16 13:04:27 -07004266 } else {
Ying Wangef3998f2019-12-09 13:06:53 +01004267 EXPECT_METRIC_EQ(1, num_best_ipv4);
4268 EXPECT_METRIC_EQ(0, num_best_ipv6);
Steve Antonede9ca52017-10-16 13:04:27 -07004269 }
4270
Ying Wangef3998f2019-12-09 13:06:53 +01004271 EXPECT_METRIC_EQ(0, webrtc::metrics::NumEvents(
4272 "WebRTC.PeerConnection.CandidatePairType_UDP",
4273 webrtc::kIceCandidatePairHostHost));
4274 EXPECT_METRIC_EQ(1, webrtc::metrics::NumEvents(
4275 "WebRTC.PeerConnection.CandidatePairType_UDP",
4276 webrtc::kIceCandidatePairHostPublicHostPublic));
Steve Antonede9ca52017-10-16 13:04:27 -07004277}
4278
4279constexpr uint32_t kFlagsIPv4NoStun = cricket::PORTALLOCATOR_DISABLE_TCP |
4280 cricket::PORTALLOCATOR_DISABLE_STUN |
4281 cricket::PORTALLOCATOR_DISABLE_RELAY;
4282constexpr uint32_t kFlagsIPv6NoStun =
4283 cricket::PORTALLOCATOR_DISABLE_TCP | cricket::PORTALLOCATOR_DISABLE_STUN |
4284 cricket::PORTALLOCATOR_ENABLE_IPV6 | cricket::PORTALLOCATOR_DISABLE_RELAY;
4285constexpr uint32_t kFlagsIPv4Stun =
4286 cricket::PORTALLOCATOR_DISABLE_TCP | cricket::PORTALLOCATOR_DISABLE_RELAY;
4287
Mirko Bonadeic84f6612019-01-31 12:20:57 +01004288INSTANTIATE_TEST_SUITE_P(
Seth Hampson2f0d7022018-02-20 11:54:42 -08004289 PeerConnectionIntegrationTest,
4290 PeerConnectionIntegrationIceStatesTest,
4291 Combine(Values(SdpSemantics::kPlanB, SdpSemantics::kUnifiedPlan),
4292 Values(std::make_pair("IPv4 no STUN", kFlagsIPv4NoStun),
4293 std::make_pair("IPv6 no STUN", kFlagsIPv6NoStun),
4294 std::make_pair("IPv4 with STUN", kFlagsIPv4Stun))));
Steve Antonede9ca52017-10-16 13:04:27 -07004295
Yves Gerey100fe632020-01-17 19:15:53 +01004296INSTANTIATE_TEST_SUITE_P(
4297 PeerConnectionIntegrationTest,
4298 PeerConnectionIntegrationIceStatesTestWithFakeClock,
4299 Combine(Values(SdpSemantics::kPlanB, SdpSemantics::kUnifiedPlan),
4300 Values(std::make_pair("IPv4 no STUN", kFlagsIPv4NoStun),
4301 std::make_pair("IPv6 no STUN", kFlagsIPv6NoStun),
4302 std::make_pair("IPv4 with STUN", kFlagsIPv4Stun))));
4303
deadbeef1dcb1642017-03-29 21:08:16 -07004304// This test sets up a call between two parties with audio and video.
4305// During the call, the caller restarts ICE and the test verifies that
4306// new ICE candidates are generated and audio and video still can flow, and the
4307// ICE state reaches completed again.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004308TEST_P(PeerConnectionIntegrationTest, MediaContinuesFlowingAfterIceRestart) {
deadbeef1dcb1642017-03-29 21:08:16 -07004309 ASSERT_TRUE(CreatePeerConnectionWrappers());
4310 ConnectFakeSignaling();
4311 // Do normal offer/answer and wait for ICE to complete.
Steve Anton15324772018-01-16 10:26:49 -08004312 caller()->AddAudioVideoTracks();
4313 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07004314 caller()->CreateAndSetAndSignalOffer();
4315 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4316 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
4317 caller()->ice_connection_state(), kMaxWaitForFramesMs);
Alex Loiko9289eda2018-11-23 16:18:59 +00004318 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
4319 callee()->ice_connection_state(), kMaxWaitForFramesMs);
deadbeef1dcb1642017-03-29 21:08:16 -07004320
4321 // To verify that the ICE restart actually occurs, get
4322 // ufrag/password/candidates before and after restart.
4323 // Create an SDP string of the first audio candidate for both clients.
4324 const webrtc::IceCandidateCollection* audio_candidates_caller =
4325 caller()->pc()->local_description()->candidates(0);
4326 const webrtc::IceCandidateCollection* audio_candidates_callee =
4327 callee()->pc()->local_description()->candidates(0);
4328 ASSERT_GT(audio_candidates_caller->count(), 0u);
4329 ASSERT_GT(audio_candidates_callee->count(), 0u);
4330 std::string caller_candidate_pre_restart;
4331 ASSERT_TRUE(
4332 audio_candidates_caller->at(0)->ToString(&caller_candidate_pre_restart));
4333 std::string callee_candidate_pre_restart;
4334 ASSERT_TRUE(
4335 audio_candidates_callee->at(0)->ToString(&callee_candidate_pre_restart));
4336 const cricket::SessionDescription* desc =
4337 caller()->pc()->local_description()->description();
4338 std::string caller_ufrag_pre_restart =
4339 desc->transport_infos()[0].description.ice_ufrag;
4340 desc = callee()->pc()->local_description()->description();
4341 std::string callee_ufrag_pre_restart =
4342 desc->transport_infos()[0].description.ice_ufrag;
4343
Alex Drake00c7ecf2019-08-06 10:54:47 -07004344 EXPECT_EQ(caller()->ice_candidate_pair_change_history().size(), 1u);
deadbeef1dcb1642017-03-29 21:08:16 -07004345 // Have the caller initiate an ICE restart.
4346 caller()->SetOfferAnswerOptions(IceRestartOfferAnswerOptions());
4347 caller()->CreateAndSetAndSignalOffer();
4348 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4349 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
4350 caller()->ice_connection_state(), kMaxWaitForFramesMs);
Alex Loiko9289eda2018-11-23 16:18:59 +00004351 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
deadbeef1dcb1642017-03-29 21:08:16 -07004352 callee()->ice_connection_state(), kMaxWaitForFramesMs);
4353
4354 // Grab the ufrags/candidates again.
4355 audio_candidates_caller = caller()->pc()->local_description()->candidates(0);
4356 audio_candidates_callee = callee()->pc()->local_description()->candidates(0);
4357 ASSERT_GT(audio_candidates_caller->count(), 0u);
4358 ASSERT_GT(audio_candidates_callee->count(), 0u);
4359 std::string caller_candidate_post_restart;
4360 ASSERT_TRUE(
4361 audio_candidates_caller->at(0)->ToString(&caller_candidate_post_restart));
4362 std::string callee_candidate_post_restart;
4363 ASSERT_TRUE(
4364 audio_candidates_callee->at(0)->ToString(&callee_candidate_post_restart));
4365 desc = caller()->pc()->local_description()->description();
4366 std::string caller_ufrag_post_restart =
4367 desc->transport_infos()[0].description.ice_ufrag;
4368 desc = callee()->pc()->local_description()->description();
4369 std::string callee_ufrag_post_restart =
4370 desc->transport_infos()[0].description.ice_ufrag;
4371 // Sanity check that an ICE restart was actually negotiated in SDP.
4372 ASSERT_NE(caller_candidate_pre_restart, caller_candidate_post_restart);
4373 ASSERT_NE(callee_candidate_pre_restart, callee_candidate_post_restart);
4374 ASSERT_NE(caller_ufrag_pre_restart, caller_ufrag_post_restart);
4375 ASSERT_NE(callee_ufrag_pre_restart, callee_ufrag_post_restart);
Alex Drake00c7ecf2019-08-06 10:54:47 -07004376 EXPECT_GT(caller()->ice_candidate_pair_change_history().size(), 1u);
deadbeef1dcb1642017-03-29 21:08:16 -07004377
4378 // Ensure that additional frames are received after the ICE restart.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004379 MediaExpectations media_expectations;
4380 media_expectations.ExpectBidirectionalAudioAndVideo();
4381 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07004382}
4383
4384// Verify that audio/video can be received end-to-end when ICE renomination is
4385// enabled.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004386TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithIceRenomination) {
deadbeef1dcb1642017-03-29 21:08:16 -07004387 PeerConnectionInterface::RTCConfiguration config;
4388 config.enable_ice_renomination = true;
4389 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
4390 ConnectFakeSignaling();
4391 // Do normal offer/answer and wait for some frames to be received in each
4392 // direction.
Steve Anton15324772018-01-16 10:26:49 -08004393 caller()->AddAudioVideoTracks();
4394 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07004395 caller()->CreateAndSetAndSignalOffer();
4396 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4397 // Sanity check that ICE renomination was actually negotiated.
4398 const cricket::SessionDescription* desc =
4399 caller()->pc()->local_description()->description();
4400 for (const cricket::TransportInfo& info : desc->transport_infos()) {
Steve Anton64b626b2019-01-28 17:25:26 -08004401 ASSERT_THAT(info.description.transport_options, Contains("renomination"));
deadbeef1dcb1642017-03-29 21:08:16 -07004402 }
4403 desc = callee()->pc()->local_description()->description();
4404 for (const cricket::TransportInfo& info : desc->transport_infos()) {
Steve Anton64b626b2019-01-28 17:25:26 -08004405 ASSERT_THAT(info.description.transport_options, Contains("renomination"));
deadbeef1dcb1642017-03-29 21:08:16 -07004406 }
Seth Hampson2f0d7022018-02-20 11:54:42 -08004407 MediaExpectations media_expectations;
4408 media_expectations.ExpectBidirectionalAudioAndVideo();
4409 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07004410}
4411
Steve Anton6f25b092017-10-23 09:39:20 -07004412// With a max bundle policy and RTCP muxing, adding a new media description to
4413// the connection should not affect ICE at all because the new media will use
4414// the existing connection.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004415TEST_P(PeerConnectionIntegrationTest,
Steve Anton83119dd2017-11-10 16:19:52 -08004416 AddMediaToConnectedBundleDoesNotRestartIce) {
Steve Anton6f25b092017-10-23 09:39:20 -07004417 PeerConnectionInterface::RTCConfiguration config;
4418 config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
4419 config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyRequire;
4420 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(
4421 config, PeerConnectionInterface::RTCConfiguration()));
4422 ConnectFakeSignaling();
4423
Steve Anton15324772018-01-16 10:26:49 -08004424 caller()->AddAudioTrack();
Steve Anton6f25b092017-10-23 09:39:20 -07004425 caller()->CreateAndSetAndSignalOffer();
4426 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Steve Antonff52f1b2017-10-26 12:24:50 -07004427 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceConnectionCompleted,
4428 caller()->ice_connection_state(), kDefaultTimeout);
Steve Anton6f25b092017-10-23 09:39:20 -07004429
4430 caller()->clear_ice_connection_state_history();
4431
Steve Anton15324772018-01-16 10:26:49 -08004432 caller()->AddVideoTrack();
Steve Anton6f25b092017-10-23 09:39:20 -07004433 caller()->CreateAndSetAndSignalOffer();
4434 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4435
4436 EXPECT_EQ(0u, caller()->ice_connection_state_history().size());
4437}
4438
deadbeef1dcb1642017-03-29 21:08:16 -07004439// This test sets up a call between two parties with audio and video. It then
4440// renegotiates setting the video m-line to "port 0", then later renegotiates
4441// again, enabling video.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004442TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07004443 VideoFlowsAfterMediaSectionIsRejectedAndRecycled) {
4444 ASSERT_TRUE(CreatePeerConnectionWrappers());
4445 ConnectFakeSignaling();
4446
4447 // Do initial negotiation, only sending media from the caller. Will result in
4448 // video and audio recvonly "m=" sections.
Steve Anton15324772018-01-16 10:26:49 -08004449 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07004450 caller()->CreateAndSetAndSignalOffer();
4451 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4452
4453 // Negotiate again, disabling the video "m=" section (the callee will set the
4454 // port to 0 due to offer_to_receive_video = 0).
Seth Hampson2f0d7022018-02-20 11:54:42 -08004455 if (sdp_semantics_ == SdpSemantics::kPlanB) {
4456 PeerConnectionInterface::RTCOfferAnswerOptions options;
4457 options.offer_to_receive_video = 0;
4458 callee()->SetOfferAnswerOptions(options);
4459 } else {
4460 callee()->SetRemoteOfferHandler([this] {
Harald Alvestrand6060df52020-08-11 09:54:02 +02004461 callee()
4462 ->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)
4463 ->StopInternal();
Seth Hampson2f0d7022018-02-20 11:54:42 -08004464 });
4465 }
deadbeef1dcb1642017-03-29 21:08:16 -07004466 caller()->CreateAndSetAndSignalOffer();
4467 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4468 // Sanity check that video "m=" section was actually rejected.
4469 const ContentInfo* answer_video_content = cricket::GetFirstVideoContent(
4470 callee()->pc()->local_description()->description());
4471 ASSERT_NE(nullptr, answer_video_content);
4472 ASSERT_TRUE(answer_video_content->rejected);
4473
4474 // Enable video and do negotiation again, making sure video is received
4475 // end-to-end, also adding media stream to callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004476 if (sdp_semantics_ == SdpSemantics::kPlanB) {
4477 PeerConnectionInterface::RTCOfferAnswerOptions options;
4478 options.offer_to_receive_video = 1;
4479 callee()->SetOfferAnswerOptions(options);
4480 } else {
4481 // The caller's transceiver is stopped, so we need to add another track.
4482 auto caller_transceiver =
4483 caller()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO);
Harald Alvestrand6060df52020-08-11 09:54:02 +02004484 EXPECT_EQ(nullptr, caller_transceiver.get());
Seth Hampson2f0d7022018-02-20 11:54:42 -08004485 caller()->AddVideoTrack();
4486 }
4487 callee()->AddVideoTrack();
4488 callee()->SetRemoteOfferHandler(nullptr);
deadbeef1dcb1642017-03-29 21:08:16 -07004489 caller()->CreateAndSetAndSignalOffer();
4490 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08004491
deadbeef1dcb1642017-03-29 21:08:16 -07004492 // Verify the caller receives frames from the newly added stream, and the
4493 // callee receives additional frames from the re-enabled video m= section.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004494 MediaExpectations media_expectations;
4495 media_expectations.CalleeExpectsSomeAudio();
4496 media_expectations.ExpectBidirectionalVideo();
4497 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07004498}
4499
deadbeef1dcb1642017-03-29 21:08:16 -07004500// This tests that if we negotiate after calling CreateSender but before we
4501// have a track, then set a track later, frames from the newly-set track are
4502// received end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004503TEST_F(PeerConnectionIntegrationTestPlanB,
deadbeef1dcb1642017-03-29 21:08:16 -07004504 MediaFlowsAfterEarlyWarmupWithCreateSender) {
4505 ASSERT_TRUE(CreatePeerConnectionWrappers());
4506 ConnectFakeSignaling();
4507 auto caller_audio_sender =
4508 caller()->pc()->CreateSender("audio", "caller_stream");
4509 auto caller_video_sender =
4510 caller()->pc()->CreateSender("video", "caller_stream");
4511 auto callee_audio_sender =
4512 callee()->pc()->CreateSender("audio", "callee_stream");
4513 auto callee_video_sender =
4514 callee()->pc()->CreateSender("video", "callee_stream");
4515 caller()->CreateAndSetAndSignalOffer();
4516 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
4517 // Wait for ICE to complete, without any tracks being set.
4518 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
4519 caller()->ice_connection_state(), kMaxWaitForFramesMs);
4520 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
4521 callee()->ice_connection_state(), kMaxWaitForFramesMs);
4522 // Now set the tracks, and expect frames to immediately start flowing.
4523 EXPECT_TRUE(caller_audio_sender->SetTrack(caller()->CreateLocalAudioTrack()));
4524 EXPECT_TRUE(caller_video_sender->SetTrack(caller()->CreateLocalVideoTrack()));
4525 EXPECT_TRUE(callee_audio_sender->SetTrack(callee()->CreateLocalAudioTrack()));
4526 EXPECT_TRUE(callee_video_sender->SetTrack(callee()->CreateLocalVideoTrack()));
Seth Hampson2f0d7022018-02-20 11:54:42 -08004527 MediaExpectations media_expectations;
4528 media_expectations.ExpectBidirectionalAudioAndVideo();
4529 ASSERT_TRUE(ExpectNewFrames(media_expectations));
4530}
4531
4532// This tests that if we negotiate after calling AddTransceiver but before we
4533// have a track, then set a track later, frames from the newly-set tracks are
4534// received end-to-end.
4535TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
4536 MediaFlowsAfterEarlyWarmupWithAddTransceiver) {
4537 ASSERT_TRUE(CreatePeerConnectionWrappers());
4538 ConnectFakeSignaling();
4539 auto audio_result = caller()->pc()->AddTransceiver(cricket::MEDIA_TYPE_AUDIO);
4540 ASSERT_EQ(RTCErrorType::NONE, audio_result.error().type());
4541 auto caller_audio_sender = audio_result.MoveValue()->sender();
4542 auto video_result = caller()->pc()->AddTransceiver(cricket::MEDIA_TYPE_VIDEO);
4543 ASSERT_EQ(RTCErrorType::NONE, video_result.error().type());
4544 auto caller_video_sender = video_result.MoveValue()->sender();
4545 callee()->SetRemoteOfferHandler([this] {
4546 ASSERT_EQ(2u, callee()->pc()->GetTransceivers().size());
Harald Alvestrand6060df52020-08-11 09:54:02 +02004547 callee()->pc()->GetTransceivers()[0]->SetDirectionWithError(
Seth Hampson2f0d7022018-02-20 11:54:42 -08004548 RtpTransceiverDirection::kSendRecv);
Harald Alvestrand6060df52020-08-11 09:54:02 +02004549 callee()->pc()->GetTransceivers()[1]->SetDirectionWithError(
Seth Hampson2f0d7022018-02-20 11:54:42 -08004550 RtpTransceiverDirection::kSendRecv);
4551 });
4552 caller()->CreateAndSetAndSignalOffer();
4553 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
4554 // Wait for ICE to complete, without any tracks being set.
4555 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
4556 caller()->ice_connection_state(), kMaxWaitForFramesMs);
4557 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
4558 callee()->ice_connection_state(), kMaxWaitForFramesMs);
4559 // Now set the tracks, and expect frames to immediately start flowing.
4560 auto callee_audio_sender = callee()->pc()->GetSenders()[0];
4561 auto callee_video_sender = callee()->pc()->GetSenders()[1];
4562 ASSERT_TRUE(caller_audio_sender->SetTrack(caller()->CreateLocalAudioTrack()));
4563 ASSERT_TRUE(caller_video_sender->SetTrack(caller()->CreateLocalVideoTrack()));
4564 ASSERT_TRUE(callee_audio_sender->SetTrack(callee()->CreateLocalAudioTrack()));
4565 ASSERT_TRUE(callee_video_sender->SetTrack(callee()->CreateLocalVideoTrack()));
4566 MediaExpectations media_expectations;
4567 media_expectations.ExpectBidirectionalAudioAndVideo();
4568 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07004569}
4570
4571// This test verifies that a remote video track can be added via AddStream,
4572// and sent end-to-end. For this particular test, it's simply echoed back
4573// from the caller to the callee, rather than being forwarded to a third
4574// PeerConnection.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004575TEST_F(PeerConnectionIntegrationTestPlanB, CanSendRemoteVideoTrack) {
deadbeef1dcb1642017-03-29 21:08:16 -07004576 ASSERT_TRUE(CreatePeerConnectionWrappers());
4577 ConnectFakeSignaling();
4578 // Just send a video track from the caller.
Steve Anton15324772018-01-16 10:26:49 -08004579 caller()->AddVideoTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07004580 caller()->CreateAndSetAndSignalOffer();
4581 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02004582 ASSERT_EQ(1U, callee()->remote_streams()->count());
deadbeef1dcb1642017-03-29 21:08:16 -07004583
4584 // Echo the stream back, and do a new offer/anwer (initiated by callee this
4585 // time).
4586 callee()->pc()->AddStream(callee()->remote_streams()->at(0));
4587 callee()->CreateAndSetAndSignalOffer();
4588 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
4589
Seth Hampson2f0d7022018-02-20 11:54:42 -08004590 MediaExpectations media_expectations;
4591 media_expectations.ExpectBidirectionalVideo();
4592 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07004593}
4594
4595// Test that we achieve the expected end-to-end connection time, using a
4596// fake clock and simulated latency on the media and signaling paths.
4597// We use a TURN<->TURN connection because this is usually the quickest to
4598// set up initially, especially when we're confident the connection will work
4599// and can start sending media before we get a STUN response.
4600//
4601// With various optimizations enabled, here are the network delays we expect to
4602// be on the critical path:
4603// 1. 2 signaling trips: Signaling offer and offerer's TURN candidate, then
4604// signaling answer (with DTLS fingerprint).
4605// 2. 9 media hops: Rest of the DTLS handshake. 3 hops in each direction when
4606// using TURN<->TURN pair, and DTLS exchange is 4 packets,
4607// the first of which should have arrived before the answer.
Yves Gerey100fe632020-01-17 19:15:53 +01004608TEST_P(PeerConnectionIntegrationTestWithFakeClock,
4609 EndToEndConnectionTimeWithTurnTurnPair) {
deadbeef1dcb1642017-03-29 21:08:16 -07004610 static constexpr int media_hop_delay_ms = 50;
4611 static constexpr int signaling_trip_delay_ms = 500;
4612 // For explanation of these values, see comment above.
4613 static constexpr int required_media_hops = 9;
4614 static constexpr int required_signaling_trips = 2;
4615 // For internal delays (such as posting an event asychronously).
4616 static constexpr int allowed_internal_delay_ms = 20;
4617 static constexpr int total_connection_time_ms =
4618 media_hop_delay_ms * required_media_hops +
4619 signaling_trip_delay_ms * required_signaling_trips +
4620 allowed_internal_delay_ms;
4621
4622 static const rtc::SocketAddress turn_server_1_internal_address{"88.88.88.0",
4623 3478};
4624 static const rtc::SocketAddress turn_server_1_external_address{"88.88.88.1",
4625 0};
4626 static const rtc::SocketAddress turn_server_2_internal_address{"99.99.99.0",
4627 3478};
4628 static const rtc::SocketAddress turn_server_2_external_address{"99.99.99.1",
4629 0};
Seth Hampsonaed71642018-06-11 07:41:32 -07004630 cricket::TestTurnServer* turn_server_1 = CreateTurnServer(
4631 turn_server_1_internal_address, turn_server_1_external_address);
Jonas Orelandbdcee282017-10-10 14:01:40 +02004632
Seth Hampsonaed71642018-06-11 07:41:32 -07004633 cricket::TestTurnServer* turn_server_2 = CreateTurnServer(
4634 turn_server_2_internal_address, turn_server_2_external_address);
deadbeef1dcb1642017-03-29 21:08:16 -07004635 // Bypass permission check on received packets so media can be sent before
4636 // the candidate is signaled.
Seth Hampsonaed71642018-06-11 07:41:32 -07004637 network_thread()->Invoke<void>(RTC_FROM_HERE, [turn_server_1] {
4638 turn_server_1->set_enable_permission_checks(false);
4639 });
4640 network_thread()->Invoke<void>(RTC_FROM_HERE, [turn_server_2] {
4641 turn_server_2->set_enable_permission_checks(false);
4642 });
deadbeef1dcb1642017-03-29 21:08:16 -07004643
4644 PeerConnectionInterface::RTCConfiguration client_1_config;
4645 webrtc::PeerConnectionInterface::IceServer ice_server_1;
4646 ice_server_1.urls.push_back("turn:88.88.88.0:3478");
4647 ice_server_1.username = "test";
4648 ice_server_1.password = "test";
4649 client_1_config.servers.push_back(ice_server_1);
4650 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
4651 client_1_config.presume_writable_when_fully_relayed = true;
4652
4653 PeerConnectionInterface::RTCConfiguration client_2_config;
4654 webrtc::PeerConnectionInterface::IceServer ice_server_2;
4655 ice_server_2.urls.push_back("turn:99.99.99.0:3478");
4656 ice_server_2.username = "test";
4657 ice_server_2.password = "test";
4658 client_2_config.servers.push_back(ice_server_2);
4659 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
4660 client_2_config.presume_writable_when_fully_relayed = true;
4661
4662 ASSERT_TRUE(
4663 CreatePeerConnectionWrappersWithConfig(client_1_config, client_2_config));
4664 // Set up the simulated delays.
4665 SetSignalingDelayMs(signaling_trip_delay_ms);
4666 ConnectFakeSignaling();
4667 virtual_socket_server()->set_delay_mean(media_hop_delay_ms);
4668 virtual_socket_server()->UpdateDelayDistribution();
4669
4670 // Set "offer to receive audio/video" without adding any tracks, so we just
4671 // set up ICE/DTLS with no media.
4672 PeerConnectionInterface::RTCOfferAnswerOptions options;
4673 options.offer_to_receive_audio = 1;
4674 options.offer_to_receive_video = 1;
4675 caller()->SetOfferAnswerOptions(options);
4676 caller()->CreateAndSetAndSignalOffer();
deadbeef71452802017-05-07 17:21:01 -07004677 EXPECT_TRUE_SIMULATED_WAIT(DtlsConnected(), total_connection_time_ms,
Yves Gerey100fe632020-01-17 19:15:53 +01004678 FakeClock());
Seth Hampson1d4a76d2018-06-19 14:31:41 -07004679 // Closing the PeerConnections destroys the ports before the ScopedFakeClock.
4680 // If this is not done a DCHECK can be hit in ports.cc, because a large
4681 // negative number is calculated for the rtt due to the global clock changing.
Steve Antond91969e2019-05-30 12:27:03 -07004682 ClosePeerConnections();
deadbeef1dcb1642017-03-29 21:08:16 -07004683}
4684
Jonas Orelandbdcee282017-10-10 14:01:40 +02004685// Verify that a TurnCustomizer passed in through RTCConfiguration
4686// is actually used by the underlying TURN candidate pair.
4687// Note that turnport_unittest.cc contains more detailed, lower-level tests.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004688TEST_P(PeerConnectionIntegrationTest, TurnCustomizerUsedForTurnConnections) {
Jonas Orelandbdcee282017-10-10 14:01:40 +02004689 static const rtc::SocketAddress turn_server_1_internal_address{"88.88.88.0",
4690 3478};
4691 static const rtc::SocketAddress turn_server_1_external_address{"88.88.88.1",
4692 0};
4693 static const rtc::SocketAddress turn_server_2_internal_address{"99.99.99.0",
4694 3478};
4695 static const rtc::SocketAddress turn_server_2_external_address{"99.99.99.1",
4696 0};
Seth Hampsonaed71642018-06-11 07:41:32 -07004697 CreateTurnServer(turn_server_1_internal_address,
4698 turn_server_1_external_address);
4699 CreateTurnServer(turn_server_2_internal_address,
4700 turn_server_2_external_address);
Jonas Orelandbdcee282017-10-10 14:01:40 +02004701
4702 PeerConnectionInterface::RTCConfiguration client_1_config;
4703 webrtc::PeerConnectionInterface::IceServer ice_server_1;
4704 ice_server_1.urls.push_back("turn:88.88.88.0:3478");
4705 ice_server_1.username = "test";
4706 ice_server_1.password = "test";
4707 client_1_config.servers.push_back(ice_server_1);
4708 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
Seth Hampsonaed71642018-06-11 07:41:32 -07004709 auto* customizer1 = CreateTurnCustomizer();
4710 client_1_config.turn_customizer = customizer1;
Jonas Orelandbdcee282017-10-10 14:01:40 +02004711
4712 PeerConnectionInterface::RTCConfiguration client_2_config;
4713 webrtc::PeerConnectionInterface::IceServer ice_server_2;
4714 ice_server_2.urls.push_back("turn:99.99.99.0:3478");
4715 ice_server_2.username = "test";
4716 ice_server_2.password = "test";
4717 client_2_config.servers.push_back(ice_server_2);
4718 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
Seth Hampsonaed71642018-06-11 07:41:32 -07004719 auto* customizer2 = CreateTurnCustomizer();
4720 client_2_config.turn_customizer = customizer2;
Jonas Orelandbdcee282017-10-10 14:01:40 +02004721
4722 ASSERT_TRUE(
4723 CreatePeerConnectionWrappersWithConfig(client_1_config, client_2_config));
4724 ConnectFakeSignaling();
4725
4726 // Set "offer to receive audio/video" without adding any tracks, so we just
4727 // set up ICE/DTLS with no media.
4728 PeerConnectionInterface::RTCOfferAnswerOptions options;
4729 options.offer_to_receive_audio = 1;
4730 options.offer_to_receive_video = 1;
4731 caller()->SetOfferAnswerOptions(options);
4732 caller()->CreateAndSetAndSignalOffer();
4733 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
4734
Seth Hampsonaed71642018-06-11 07:41:32 -07004735 ExpectTurnCustomizerCountersIncremented(customizer1);
4736 ExpectTurnCustomizerCountersIncremented(customizer2);
Jonas Orelandbdcee282017-10-10 14:01:40 +02004737}
4738
Benjamin Wright2d5f3cb2018-05-22 14:46:06 -07004739// Verifies that you can use TCP instead of UDP to connect to a TURN server and
4740// send media between the caller and the callee.
4741TEST_P(PeerConnectionIntegrationTest, TCPUsedForTurnConnections) {
4742 static const rtc::SocketAddress turn_server_internal_address{"88.88.88.0",
4743 3478};
4744 static const rtc::SocketAddress turn_server_external_address{"88.88.88.1", 0};
4745
4746 // Enable TCP for the fake turn server.
Seth Hampsonaed71642018-06-11 07:41:32 -07004747 CreateTurnServer(turn_server_internal_address, turn_server_external_address,
4748 cricket::PROTO_TCP);
Benjamin Wright2d5f3cb2018-05-22 14:46:06 -07004749
4750 webrtc::PeerConnectionInterface::IceServer ice_server;
4751 ice_server.urls.push_back("turn:88.88.88.0:3478?transport=tcp");
4752 ice_server.username = "test";
4753 ice_server.password = "test";
4754
4755 PeerConnectionInterface::RTCConfiguration client_1_config;
4756 client_1_config.servers.push_back(ice_server);
4757 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
4758
4759 PeerConnectionInterface::RTCConfiguration client_2_config;
4760 client_2_config.servers.push_back(ice_server);
4761 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
4762
4763 ASSERT_TRUE(
4764 CreatePeerConnectionWrappersWithConfig(client_1_config, client_2_config));
4765
4766 // Do normal offer/answer and wait for ICE to complete.
4767 ConnectFakeSignaling();
4768 caller()->AddAudioVideoTracks();
4769 callee()->AddAudioVideoTracks();
4770 caller()->CreateAndSetAndSignalOffer();
4771 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4772 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
4773 callee()->ice_connection_state(), kMaxWaitForFramesMs);
4774
4775 MediaExpectations media_expectations;
4776 media_expectations.ExpectBidirectionalAudioAndVideo();
4777 EXPECT_TRUE(ExpectNewFrames(media_expectations));
4778}
4779
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07004780// Verify that a SSLCertificateVerifier passed in through
4781// PeerConnectionDependencies is actually used by the underlying SSL
4782// implementation to determine whether a certificate presented by the TURN
4783// server is accepted by the client. Note that openssladapter_unittest.cc
4784// contains more detailed, lower-level tests.
4785TEST_P(PeerConnectionIntegrationTest,
4786 SSLCertificateVerifierUsedForTurnConnections) {
4787 static const rtc::SocketAddress turn_server_internal_address{"88.88.88.0",
4788 3478};
4789 static const rtc::SocketAddress turn_server_external_address{"88.88.88.1", 0};
4790
4791 // Enable TCP-TLS for the fake turn server. We need to pass in 88.88.88.0 so
4792 // that host name verification passes on the fake certificate.
Seth Hampsonaed71642018-06-11 07:41:32 -07004793 CreateTurnServer(turn_server_internal_address, turn_server_external_address,
4794 cricket::PROTO_TLS, "88.88.88.0");
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07004795
4796 webrtc::PeerConnectionInterface::IceServer ice_server;
4797 ice_server.urls.push_back("turns:88.88.88.0:3478?transport=tcp");
4798 ice_server.username = "test";
4799 ice_server.password = "test";
4800
4801 PeerConnectionInterface::RTCConfiguration client_1_config;
4802 client_1_config.servers.push_back(ice_server);
4803 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
4804
4805 PeerConnectionInterface::RTCConfiguration client_2_config;
4806 client_2_config.servers.push_back(ice_server);
4807 // Setting the type to kRelay forces the connection to go through a TURN
4808 // server.
4809 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
4810
4811 // Get a copy to the pointer so we can verify calls later.
4812 rtc::TestCertificateVerifier* client_1_cert_verifier =
4813 new rtc::TestCertificateVerifier();
4814 client_1_cert_verifier->verify_certificate_ = true;
4815 rtc::TestCertificateVerifier* client_2_cert_verifier =
4816 new rtc::TestCertificateVerifier();
4817 client_2_cert_verifier->verify_certificate_ = true;
4818
4819 // Create the dependencies with the test certificate verifier.
4820 webrtc::PeerConnectionDependencies client_1_deps(nullptr);
4821 client_1_deps.tls_cert_verifier =
4822 std::unique_ptr<rtc::TestCertificateVerifier>(client_1_cert_verifier);
4823 webrtc::PeerConnectionDependencies client_2_deps(nullptr);
4824 client_2_deps.tls_cert_verifier =
4825 std::unique_ptr<rtc::TestCertificateVerifier>(client_2_cert_verifier);
4826
4827 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndDeps(
4828 client_1_config, std::move(client_1_deps), client_2_config,
4829 std::move(client_2_deps)));
4830 ConnectFakeSignaling();
4831
4832 // Set "offer to receive audio/video" without adding any tracks, so we just
4833 // set up ICE/DTLS with no media.
4834 PeerConnectionInterface::RTCOfferAnswerOptions options;
4835 options.offer_to_receive_audio = 1;
4836 options.offer_to_receive_video = 1;
4837 caller()->SetOfferAnswerOptions(options);
4838 caller()->CreateAndSetAndSignalOffer();
4839 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
4840
4841 EXPECT_GT(client_1_cert_verifier->call_count_, 0u);
4842 EXPECT_GT(client_2_cert_verifier->call_count_, 0u);
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07004843}
4844
4845TEST_P(PeerConnectionIntegrationTest,
4846 SSLCertificateVerifierFailureUsedForTurnConnectionsFailsConnection) {
4847 static const rtc::SocketAddress turn_server_internal_address{"88.88.88.0",
4848 3478};
4849 static const rtc::SocketAddress turn_server_external_address{"88.88.88.1", 0};
4850
4851 // Enable TCP-TLS for the fake turn server. We need to pass in 88.88.88.0 so
4852 // that host name verification passes on the fake certificate.
Seth Hampsonaed71642018-06-11 07:41:32 -07004853 CreateTurnServer(turn_server_internal_address, turn_server_external_address,
4854 cricket::PROTO_TLS, "88.88.88.0");
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07004855
4856 webrtc::PeerConnectionInterface::IceServer ice_server;
4857 ice_server.urls.push_back("turns:88.88.88.0:3478?transport=tcp");
4858 ice_server.username = "test";
4859 ice_server.password = "test";
4860
4861 PeerConnectionInterface::RTCConfiguration client_1_config;
4862 client_1_config.servers.push_back(ice_server);
4863 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
4864
4865 PeerConnectionInterface::RTCConfiguration client_2_config;
4866 client_2_config.servers.push_back(ice_server);
4867 // Setting the type to kRelay forces the connection to go through a TURN
4868 // server.
4869 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
4870
4871 // Get a copy to the pointer so we can verify calls later.
4872 rtc::TestCertificateVerifier* client_1_cert_verifier =
4873 new rtc::TestCertificateVerifier();
4874 client_1_cert_verifier->verify_certificate_ = false;
4875 rtc::TestCertificateVerifier* client_2_cert_verifier =
4876 new rtc::TestCertificateVerifier();
4877 client_2_cert_verifier->verify_certificate_ = false;
4878
4879 // Create the dependencies with the test certificate verifier.
4880 webrtc::PeerConnectionDependencies client_1_deps(nullptr);
4881 client_1_deps.tls_cert_verifier =
4882 std::unique_ptr<rtc::TestCertificateVerifier>(client_1_cert_verifier);
4883 webrtc::PeerConnectionDependencies client_2_deps(nullptr);
4884 client_2_deps.tls_cert_verifier =
4885 std::unique_ptr<rtc::TestCertificateVerifier>(client_2_cert_verifier);
4886
4887 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndDeps(
4888 client_1_config, std::move(client_1_deps), client_2_config,
4889 std::move(client_2_deps)));
4890 ConnectFakeSignaling();
4891
4892 // Set "offer to receive audio/video" without adding any tracks, so we just
4893 // set up ICE/DTLS with no media.
4894 PeerConnectionInterface::RTCOfferAnswerOptions options;
4895 options.offer_to_receive_audio = 1;
4896 options.offer_to_receive_video = 1;
4897 caller()->SetOfferAnswerOptions(options);
4898 caller()->CreateAndSetAndSignalOffer();
4899 bool wait_res = true;
4900 // TODO(bugs.webrtc.org/9219): When IceConnectionState is implemented
4901 // properly, should be able to just wait for a state of "failed" instead of
4902 // waiting a fixed 10 seconds.
4903 WAIT_(DtlsConnected(), kDefaultTimeout, wait_res);
4904 ASSERT_FALSE(wait_res);
4905
4906 EXPECT_GT(client_1_cert_verifier->call_count_, 0u);
4907 EXPECT_GT(client_2_cert_verifier->call_count_, 0u);
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07004908}
4909
Qingsi Wang25ec8882019-11-15 12:33:05 -08004910// Test that the injected ICE transport factory is used to create ICE transports
4911// for WebRTC connections.
4912TEST_P(PeerConnectionIntegrationTest, IceTransportFactoryUsedForConnections) {
4913 PeerConnectionInterface::RTCConfiguration default_config;
4914 PeerConnectionDependencies dependencies(nullptr);
4915 auto ice_transport_factory = std::make_unique<MockIceTransportFactory>();
4916 EXPECT_CALL(*ice_transport_factory, RecordIceTransportCreated()).Times(1);
4917 dependencies.ice_transport_factory = std::move(ice_transport_factory);
Niels Möller2a707032020-06-16 16:39:13 +02004918 auto wrapper = CreatePeerConnectionWrapper("Caller", nullptr, &default_config,
4919 std::move(dependencies), nullptr,
4920 /*reset_encoder_factory=*/false,
4921 /*reset_decoder_factory=*/false);
Qingsi Wang25ec8882019-11-15 12:33:05 -08004922 ASSERT_TRUE(wrapper);
4923 wrapper->CreateDataChannel();
4924 rtc::scoped_refptr<MockSetSessionDescriptionObserver> observer(
4925 new rtc::RefCountedObject<MockSetSessionDescriptionObserver>());
4926 wrapper->pc()->SetLocalDescription(observer,
4927 wrapper->CreateOfferAndWait().release());
4928}
4929
deadbeefc964d0b2017-04-03 10:03:35 -07004930// Test that audio and video flow end-to-end when codec names don't use the
4931// expected casing, given that they're supposed to be case insensitive. To test
4932// this, all but one codec is removed from each media description, and its
4933// casing is changed.
4934//
4935// In the past, this has regressed and caused crashes/black video, due to the
4936// fact that code at some layers was doing case-insensitive comparisons and
4937// code at other layers was not.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004938TEST_P(PeerConnectionIntegrationTest, CodecNamesAreCaseInsensitive) {
deadbeefc964d0b2017-04-03 10:03:35 -07004939 ASSERT_TRUE(CreatePeerConnectionWrappers());
4940 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08004941 caller()->AddAudioVideoTracks();
4942 callee()->AddAudioVideoTracks();
deadbeefc964d0b2017-04-03 10:03:35 -07004943
4944 // Remove all but one audio/video codec (opus and VP8), and change the
4945 // casing of the caller's generated offer.
4946 caller()->SetGeneratedSdpMunger([](cricket::SessionDescription* description) {
4947 cricket::AudioContentDescription* audio =
4948 GetFirstAudioContentDescription(description);
4949 ASSERT_NE(nullptr, audio);
4950 auto audio_codecs = audio->codecs();
4951 audio_codecs.erase(std::remove_if(audio_codecs.begin(), audio_codecs.end(),
4952 [](const cricket::AudioCodec& codec) {
4953 return codec.name != "opus";
4954 }),
4955 audio_codecs.end());
4956 ASSERT_EQ(1u, audio_codecs.size());
4957 audio_codecs[0].name = "OpUs";
4958 audio->set_codecs(audio_codecs);
4959
4960 cricket::VideoContentDescription* video =
4961 GetFirstVideoContentDescription(description);
4962 ASSERT_NE(nullptr, video);
4963 auto video_codecs = video->codecs();
4964 video_codecs.erase(std::remove_if(video_codecs.begin(), video_codecs.end(),
4965 [](const cricket::VideoCodec& codec) {
4966 return codec.name != "VP8";
4967 }),
4968 video_codecs.end());
4969 ASSERT_EQ(1u, video_codecs.size());
4970 video_codecs[0].name = "vP8";
4971 video->set_codecs(video_codecs);
4972 });
4973
4974 caller()->CreateAndSetAndSignalOffer();
4975 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4976
4977 // Verify frames are still received end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004978 MediaExpectations media_expectations;
4979 media_expectations.ExpectBidirectionalAudioAndVideo();
4980 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeefc964d0b2017-04-03 10:03:35 -07004981}
4982
Jonas Oreland49ac5952018-09-26 16:04:32 +02004983TEST_P(PeerConnectionIntegrationTest, GetSourcesAudio) {
hbos8d609f62017-04-10 07:39:05 -07004984 ASSERT_TRUE(CreatePeerConnectionWrappers());
4985 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08004986 caller()->AddAudioTrack();
hbos8d609f62017-04-10 07:39:05 -07004987 caller()->CreateAndSetAndSignalOffer();
4988 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
deadbeefd8ad7882017-04-18 16:01:17 -07004989 // Wait for one audio frame to be received by the callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004990 MediaExpectations media_expectations;
4991 media_expectations.CalleeExpectsSomeAudio(1);
4992 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Jonas Oreland49ac5952018-09-26 16:04:32 +02004993 ASSERT_EQ(callee()->pc()->GetReceivers().size(), 1u);
hbos8d609f62017-04-10 07:39:05 -07004994 auto receiver = callee()->pc()->GetReceivers()[0];
4995 ASSERT_EQ(receiver->media_type(), cricket::MEDIA_TYPE_AUDIO);
Jonas Oreland49ac5952018-09-26 16:04:32 +02004996 auto sources = receiver->GetSources();
hbos8d609f62017-04-10 07:39:05 -07004997 ASSERT_GT(receiver->GetParameters().encodings.size(), 0u);
4998 EXPECT_EQ(receiver->GetParameters().encodings[0].ssrc,
Jonas Oreland49ac5952018-09-26 16:04:32 +02004999 sources[0].source_id());
5000 EXPECT_EQ(webrtc::RtpSourceType::SSRC, sources[0].source_type());
5001}
5002
5003TEST_P(PeerConnectionIntegrationTest, GetSourcesVideo) {
5004 ASSERT_TRUE(CreatePeerConnectionWrappers());
5005 ConnectFakeSignaling();
5006 caller()->AddVideoTrack();
5007 caller()->CreateAndSetAndSignalOffer();
5008 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5009 // Wait for one video frame to be received by the callee.
5010 MediaExpectations media_expectations;
5011 media_expectations.CalleeExpectsSomeVideo(1);
5012 ASSERT_TRUE(ExpectNewFrames(media_expectations));
5013 ASSERT_EQ(callee()->pc()->GetReceivers().size(), 1u);
5014 auto receiver = callee()->pc()->GetReceivers()[0];
5015 ASSERT_EQ(receiver->media_type(), cricket::MEDIA_TYPE_VIDEO);
5016 auto sources = receiver->GetSources();
5017 ASSERT_GT(receiver->GetParameters().encodings.size(), 0u);
Yves Gereyf781bb52019-07-23 19:15:39 +02005018 ASSERT_GT(sources.size(), 0u);
Jonas Oreland49ac5952018-09-26 16:04:32 +02005019 EXPECT_EQ(receiver->GetParameters().encodings[0].ssrc,
5020 sources[0].source_id());
5021 EXPECT_EQ(webrtc::RtpSourceType::SSRC, sources[0].source_type());
hbos8d609f62017-04-10 07:39:05 -07005022}
5023
deadbeef2f425aa2017-04-14 10:41:32 -07005024// Test that if a track is removed and added again with a different stream ID,
5025// the new stream ID is successfully communicated in SDP and media continues to
5026// flow end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08005027// TODO(webrtc.bugs.org/8734): This test does not work for Unified Plan because
5028// it will not reuse a transceiver that has already been sending. After creating
5029// a new transceiver it tries to create an offer with two senders of the same
5030// track ids and it fails.
5031TEST_F(PeerConnectionIntegrationTestPlanB, RemoveAndAddTrackWithNewStreamId) {
deadbeef2f425aa2017-04-14 10:41:32 -07005032 ASSERT_TRUE(CreatePeerConnectionWrappers());
5033 ConnectFakeSignaling();
5034
deadbeef2f425aa2017-04-14 10:41:32 -07005035 // Add track using stream 1, do offer/answer.
5036 rtc::scoped_refptr<webrtc::AudioTrackInterface> track =
5037 caller()->CreateLocalAudioTrack();
5038 rtc::scoped_refptr<webrtc::RtpSenderInterface> sender =
Steve Antond78323f2018-07-11 11:13:44 -07005039 caller()->AddTrack(track, {"stream_1"});
deadbeef2f425aa2017-04-14 10:41:32 -07005040 caller()->CreateAndSetAndSignalOffer();
5041 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08005042 {
5043 MediaExpectations media_expectations;
5044 media_expectations.CalleeExpectsSomeAudio(1);
5045 ASSERT_TRUE(ExpectNewFrames(media_expectations));
5046 }
deadbeef2f425aa2017-04-14 10:41:32 -07005047 // Remove the sender, and create a new one with the new stream.
5048 caller()->pc()->RemoveTrack(sender);
Steve Antond78323f2018-07-11 11:13:44 -07005049 sender = caller()->AddTrack(track, {"stream_2"});
deadbeef2f425aa2017-04-14 10:41:32 -07005050 caller()->CreateAndSetAndSignalOffer();
5051 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5052 // Wait for additional audio frames to be received by the callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08005053 {
5054 MediaExpectations media_expectations;
5055 media_expectations.CalleeExpectsSomeAudio();
5056 ASSERT_TRUE(ExpectNewFrames(media_expectations));
5057 }
deadbeef2f425aa2017-04-14 10:41:32 -07005058}
5059
Seth Hampson2f0d7022018-02-20 11:54:42 -08005060TEST_P(PeerConnectionIntegrationTest, RtcEventLogOutputWriteCalled) {
Elad Alon99c3fe52017-10-13 16:29:40 +02005061 ASSERT_TRUE(CreatePeerConnectionWrappers());
5062 ConnectFakeSignaling();
5063
Mirko Bonadei317a1f02019-09-17 17:06:18 +02005064 auto output = std::make_unique<testing::NiceMock<MockRtcEventLogOutput>>();
Mirko Bonadei6a489f22019-04-09 15:11:12 +02005065 ON_CALL(*output, IsActive()).WillByDefault(::testing::Return(true));
5066 ON_CALL(*output, Write(::testing::_)).WillByDefault(::testing::Return(true));
Elad Alon99c3fe52017-10-13 16:29:40 +02005067 EXPECT_CALL(*output, Write(::testing::_)).Times(::testing::AtLeast(1));
Bjorn Tereliusde939432017-11-20 17:38:14 +01005068 EXPECT_TRUE(caller()->pc()->StartRtcEventLog(
5069 std::move(output), webrtc::RtcEventLog::kImmediateOutput));
Elad Alon99c3fe52017-10-13 16:29:40 +02005070
Steve Anton15324772018-01-16 10:26:49 -08005071 caller()->AddAudioVideoTracks();
Elad Alon99c3fe52017-10-13 16:29:40 +02005072 caller()->CreateAndSetAndSignalOffer();
5073 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5074}
5075
Steve Antonede9ca52017-10-16 13:04:27 -07005076// Test that if candidates are only signaled by applying full session
5077// descriptions (instead of using AddIceCandidate), the peers can connect to
5078// each other and exchange media.
Seth Hampson2f0d7022018-02-20 11:54:42 -08005079TEST_P(PeerConnectionIntegrationTest, MediaFlowsWhenCandidatesSetOnlyInSdp) {
Steve Antonede9ca52017-10-16 13:04:27 -07005080 ASSERT_TRUE(CreatePeerConnectionWrappers());
5081 // Each side will signal the session descriptions but not candidates.
5082 ConnectFakeSignalingForSdpOnly();
5083
5084 // Add audio video track and exchange the initial offer/answer with media
5085 // information only. This will start ICE gathering on each side.
Steve Anton15324772018-01-16 10:26:49 -08005086 caller()->AddAudioVideoTracks();
5087 callee()->AddAudioVideoTracks();
Steve Antonede9ca52017-10-16 13:04:27 -07005088 caller()->CreateAndSetAndSignalOffer();
5089
5090 // Wait for all candidates to be gathered on both the caller and callee.
5091 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceGatheringComplete,
5092 caller()->ice_gathering_state(), kDefaultTimeout);
5093 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceGatheringComplete,
5094 callee()->ice_gathering_state(), kDefaultTimeout);
5095
5096 // The candidates will now be included in the session description, so
5097 // signaling them will start the ICE connection.
5098 caller()->CreateAndSetAndSignalOffer();
5099 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5100
5101 // Ensure that media flows in both directions.
Seth Hampson2f0d7022018-02-20 11:54:42 -08005102 MediaExpectations media_expectations;
5103 media_expectations.ExpectBidirectionalAudioAndVideo();
5104 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Antonede9ca52017-10-16 13:04:27 -07005105}
5106
henrika5f6bf242017-11-01 11:06:56 +01005107// Test that SetAudioPlayout can be used to disable audio playout from the
5108// start, then later enable it. This may be useful, for example, if the caller
5109// needs to play a local ringtone until some event occurs, after which it
5110// switches to playing the received audio.
Seth Hampson2f0d7022018-02-20 11:54:42 -08005111TEST_P(PeerConnectionIntegrationTest, DisableAndEnableAudioPlayout) {
henrika5f6bf242017-11-01 11:06:56 +01005112 ASSERT_TRUE(CreatePeerConnectionWrappers());
5113 ConnectFakeSignaling();
5114
5115 // Set up audio-only call where audio playout is disabled on caller's side.
5116 caller()->pc()->SetAudioPlayout(false);
Steve Anton15324772018-01-16 10:26:49 -08005117 caller()->AddAudioTrack();
5118 callee()->AddAudioTrack();
henrika5f6bf242017-11-01 11:06:56 +01005119 caller()->CreateAndSetAndSignalOffer();
5120 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5121
5122 // Pump messages for a second.
5123 WAIT(false, 1000);
5124 // Since audio playout is disabled, the caller shouldn't have received
5125 // anything (at the playout level, at least).
5126 EXPECT_EQ(0, caller()->audio_frames_received());
5127 // As a sanity check, make sure the callee (for which playout isn't disabled)
5128 // did still see frames on its audio level.
5129 ASSERT_GT(callee()->audio_frames_received(), 0);
5130
5131 // Enable playout again, and ensure audio starts flowing.
5132 caller()->pc()->SetAudioPlayout(true);
Seth Hampson2f0d7022018-02-20 11:54:42 -08005133 MediaExpectations media_expectations;
5134 media_expectations.ExpectBidirectionalAudio();
5135 ASSERT_TRUE(ExpectNewFrames(media_expectations));
henrika5f6bf242017-11-01 11:06:56 +01005136}
5137
5138double GetAudioEnergyStat(PeerConnectionWrapper* pc) {
5139 auto report = pc->NewGetStats();
5140 auto track_stats_list =
5141 report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
5142 const webrtc::RTCMediaStreamTrackStats* remote_track_stats = nullptr;
5143 for (const auto* track_stats : track_stats_list) {
5144 if (track_stats->remote_source.is_defined() &&
5145 *track_stats->remote_source) {
5146 remote_track_stats = track_stats;
5147 break;
5148 }
5149 }
5150
5151 if (!remote_track_stats->total_audio_energy.is_defined()) {
5152 return 0.0;
5153 }
5154 return *remote_track_stats->total_audio_energy;
5155}
5156
5157// Test that if audio playout is disabled via the SetAudioPlayout() method, then
5158// incoming audio is still processed and statistics are generated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08005159TEST_P(PeerConnectionIntegrationTest,
henrika5f6bf242017-11-01 11:06:56 +01005160 DisableAudioPlayoutStillGeneratesAudioStats) {
5161 ASSERT_TRUE(CreatePeerConnectionWrappers());
5162 ConnectFakeSignaling();
5163
5164 // Set up audio-only call where playout is disabled but audio-processing is
5165 // still active.
Steve Anton15324772018-01-16 10:26:49 -08005166 caller()->AddAudioTrack();
5167 callee()->AddAudioTrack();
henrika5f6bf242017-11-01 11:06:56 +01005168 caller()->pc()->SetAudioPlayout(false);
5169
5170 caller()->CreateAndSetAndSignalOffer();
5171 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5172
5173 // Wait for the callee to receive audio stats.
5174 EXPECT_TRUE_WAIT(GetAudioEnergyStat(caller()) > 0, kMaxWaitForFramesMs);
5175}
5176
henrika4f167df2017-11-01 14:45:55 +01005177// Test that SetAudioRecording can be used to disable audio recording from the
5178// start, then later enable it. This may be useful, for example, if the caller
5179// wants to ensure that no audio resources are active before a certain state
5180// is reached.
Seth Hampson2f0d7022018-02-20 11:54:42 -08005181TEST_P(PeerConnectionIntegrationTest, DisableAndEnableAudioRecording) {
henrika4f167df2017-11-01 14:45:55 +01005182 ASSERT_TRUE(CreatePeerConnectionWrappers());
5183 ConnectFakeSignaling();
5184
5185 // Set up audio-only call where audio recording is disabled on caller's side.
5186 caller()->pc()->SetAudioRecording(false);
Steve Anton15324772018-01-16 10:26:49 -08005187 caller()->AddAudioTrack();
5188 callee()->AddAudioTrack();
henrika4f167df2017-11-01 14:45:55 +01005189 caller()->CreateAndSetAndSignalOffer();
5190 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5191
5192 // Pump messages for a second.
5193 WAIT(false, 1000);
5194 // Since caller has disabled audio recording, the callee shouldn't have
5195 // received anything.
5196 EXPECT_EQ(0, callee()->audio_frames_received());
5197 // As a sanity check, make sure the caller did still see frames on its
5198 // audio level since audio recording is enabled on the calle side.
5199 ASSERT_GT(caller()->audio_frames_received(), 0);
5200
5201 // Enable audio recording again, and ensure audio starts flowing.
5202 caller()->pc()->SetAudioRecording(true);
Seth Hampson2f0d7022018-02-20 11:54:42 -08005203 MediaExpectations media_expectations;
5204 media_expectations.ExpectBidirectionalAudio();
5205 ASSERT_TRUE(ExpectNewFrames(media_expectations));
henrika4f167df2017-11-01 14:45:55 +01005206}
5207
Taylor Brandstetter389a97c2018-01-03 16:26:06 -08005208// Test that after closing PeerConnections, they stop sending any packets (ICE,
5209// DTLS, RTP...).
Seth Hampson2f0d7022018-02-20 11:54:42 -08005210TEST_P(PeerConnectionIntegrationTest, ClosingConnectionStopsPacketFlow) {
Taylor Brandstetter389a97c2018-01-03 16:26:06 -08005211 // Set up audio/video/data, wait for some frames to be received.
5212 ASSERT_TRUE(CreatePeerConnectionWrappers());
5213 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08005214 caller()->AddAudioVideoTracks();
Taylor Brandstetter389a97c2018-01-03 16:26:06 -08005215#ifdef HAVE_SCTP
5216 caller()->CreateDataChannel();
5217#endif
5218 caller()->CreateAndSetAndSignalOffer();
5219 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08005220 MediaExpectations media_expectations;
5221 media_expectations.CalleeExpectsSomeAudioAndVideo();
5222 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Taylor Brandstetter389a97c2018-01-03 16:26:06 -08005223 // Close PeerConnections.
Steve Antond91969e2019-05-30 12:27:03 -07005224 ClosePeerConnections();
Taylor Brandstetter389a97c2018-01-03 16:26:06 -08005225 // Pump messages for a second, and ensure no new packets end up sent.
5226 uint32_t sent_packets_a = virtual_socket_server()->sent_packets();
5227 WAIT(false, 1000);
5228 uint32_t sent_packets_b = virtual_socket_server()->sent_packets();
5229 EXPECT_EQ(sent_packets_a, sent_packets_b);
5230}
5231
Steve Anton7eca0932018-03-30 15:18:41 -07005232// Test that transport stats are generated by the RTCStatsCollector for a
5233// connection that only involves data channels. This is a regression test for
5234// crbug.com/826972.
5235#ifdef HAVE_SCTP
5236TEST_P(PeerConnectionIntegrationTest,
5237 TransportStatsReportedForDataChannelOnlyConnection) {
5238 ASSERT_TRUE(CreatePeerConnectionWrappers());
5239 ConnectFakeSignaling();
5240 caller()->CreateDataChannel();
5241
5242 caller()->CreateAndSetAndSignalOffer();
5243 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5244 ASSERT_TRUE_WAIT(callee()->data_channel(), kDefaultTimeout);
5245
5246 auto caller_report = caller()->NewGetStats();
5247 EXPECT_EQ(1u, caller_report->GetStatsOfType<RTCTransportStats>().size());
5248 auto callee_report = callee()->NewGetStats();
5249 EXPECT_EQ(1u, callee_report->GetStatsOfType<RTCTransportStats>().size());
5250}
5251#endif // HAVE_SCTP
5252
Qingsi Wang7685e862018-06-11 20:15:46 -07005253TEST_P(PeerConnectionIntegrationTest,
5254 IceEventsGeneratedAndLoggedInRtcEventLog) {
5255 ASSERT_TRUE(CreatePeerConnectionWrappersWithFakeRtcEventLog());
5256 ConnectFakeSignaling();
5257 PeerConnectionInterface::RTCOfferAnswerOptions options;
5258 options.offer_to_receive_audio = 1;
5259 caller()->SetOfferAnswerOptions(options);
5260 caller()->CreateAndSetAndSignalOffer();
5261 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
5262 ASSERT_NE(nullptr, caller()->event_log_factory());
5263 ASSERT_NE(nullptr, callee()->event_log_factory());
5264 webrtc::FakeRtcEventLog* caller_event_log =
5265 static_cast<webrtc::FakeRtcEventLog*>(
5266 caller()->event_log_factory()->last_log_created());
5267 webrtc::FakeRtcEventLog* callee_event_log =
5268 static_cast<webrtc::FakeRtcEventLog*>(
5269 callee()->event_log_factory()->last_log_created());
5270 ASSERT_NE(nullptr, caller_event_log);
5271 ASSERT_NE(nullptr, callee_event_log);
5272 int caller_ice_config_count = caller_event_log->GetEventCount(
5273 webrtc::RtcEvent::Type::IceCandidatePairConfig);
5274 int caller_ice_event_count = caller_event_log->GetEventCount(
5275 webrtc::RtcEvent::Type::IceCandidatePairEvent);
5276 int callee_ice_config_count = callee_event_log->GetEventCount(
5277 webrtc::RtcEvent::Type::IceCandidatePairConfig);
5278 int callee_ice_event_count = callee_event_log->GetEventCount(
5279 webrtc::RtcEvent::Type::IceCandidatePairEvent);
5280 EXPECT_LT(0, caller_ice_config_count);
5281 EXPECT_LT(0, caller_ice_event_count);
5282 EXPECT_LT(0, callee_ice_config_count);
5283 EXPECT_LT(0, callee_ice_event_count);
5284}
5285
Qingsi Wangc129c352019-04-18 10:41:58 -07005286TEST_P(PeerConnectionIntegrationTest, RegatherAfterChangingIceTransportType) {
Qingsi Wangc129c352019-04-18 10:41:58 -07005287 static const rtc::SocketAddress turn_server_internal_address{"88.88.88.0",
5288 3478};
5289 static const rtc::SocketAddress turn_server_external_address{"88.88.88.1", 0};
5290
5291 CreateTurnServer(turn_server_internal_address, turn_server_external_address);
5292
5293 webrtc::PeerConnectionInterface::IceServer ice_server;
5294 ice_server.urls.push_back("turn:88.88.88.0:3478");
5295 ice_server.username = "test";
5296 ice_server.password = "test";
5297
5298 PeerConnectionInterface::RTCConfiguration caller_config;
5299 caller_config.servers.push_back(ice_server);
5300 caller_config.type = webrtc::PeerConnectionInterface::kRelay;
5301 caller_config.continual_gathering_policy = PeerConnection::GATHER_CONTINUALLY;
Qingsi Wang1fe119f2019-05-31 16:55:33 -07005302 caller_config.surface_ice_candidates_on_ice_transport_type_changed = true;
Qingsi Wangc129c352019-04-18 10:41:58 -07005303
5304 PeerConnectionInterface::RTCConfiguration callee_config;
5305 callee_config.servers.push_back(ice_server);
5306 callee_config.type = webrtc::PeerConnectionInterface::kRelay;
5307 callee_config.continual_gathering_policy = PeerConnection::GATHER_CONTINUALLY;
Qingsi Wang1fe119f2019-05-31 16:55:33 -07005308 callee_config.surface_ice_candidates_on_ice_transport_type_changed = true;
Qingsi Wangc129c352019-04-18 10:41:58 -07005309
5310 ASSERT_TRUE(
5311 CreatePeerConnectionWrappersWithConfig(caller_config, callee_config));
5312
5313 // Do normal offer/answer and wait for ICE to complete.
5314 ConnectFakeSignaling();
5315 caller()->AddAudioVideoTracks();
5316 callee()->AddAudioVideoTracks();
5317 caller()->CreateAndSetAndSignalOffer();
5318 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5319 // Since we are doing continual gathering, the ICE transport does not reach
5320 // kIceGatheringComplete (see
5321 // P2PTransportChannel::OnCandidatesAllocationDone), and consequently not
5322 // kIceConnectionComplete.
5323 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
5324 caller()->ice_connection_state(), kDefaultTimeout);
5325 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
5326 callee()->ice_connection_state(), kDefaultTimeout);
5327 // Note that we cannot use the metric
5328 // |WebRTC.PeerConnection.CandidatePairType_UDP| in this test since this
5329 // metric is only populated when we reach kIceConnectionComplete in the
5330 // current implementation.
5331 EXPECT_EQ(cricket::RELAY_PORT_TYPE,
5332 caller()->last_candidate_gathered().type());
5333 EXPECT_EQ(cricket::RELAY_PORT_TYPE,
5334 callee()->last_candidate_gathered().type());
5335
5336 // Loosen the caller's candidate filter.
5337 caller_config = caller()->pc()->GetConfiguration();
5338 caller_config.type = webrtc::PeerConnectionInterface::kAll;
5339 caller()->pc()->SetConfiguration(caller_config);
5340 // We should have gathered a new host candidate.
5341 EXPECT_EQ_WAIT(cricket::LOCAL_PORT_TYPE,
5342 caller()->last_candidate_gathered().type(), kDefaultTimeout);
5343
5344 // Loosen the callee's candidate filter.
5345 callee_config = callee()->pc()->GetConfiguration();
5346 callee_config.type = webrtc::PeerConnectionInterface::kAll;
5347 callee()->pc()->SetConfiguration(callee_config);
5348 EXPECT_EQ_WAIT(cricket::LOCAL_PORT_TYPE,
5349 callee()->last_candidate_gathered().type(), kDefaultTimeout);
Jonas Orelande3096512020-05-27 09:01:05 +02005350
5351 // Create an offer and verify that it does not contain an ICE restart (i.e new
5352 // ice credentials).
5353 std::string caller_ufrag_pre_offer = caller()
5354 ->pc()
5355 ->local_description()
5356 ->description()
5357 ->transport_infos()[0]
5358 .description.ice_ufrag;
5359 caller()->CreateAndSetAndSignalOffer();
5360 std::string caller_ufrag_post_offer = caller()
5361 ->pc()
5362 ->local_description()
5363 ->description()
5364 ->transport_infos()[0]
5365 .description.ice_ufrag;
5366 EXPECT_EQ(caller_ufrag_pre_offer, caller_ufrag_post_offer);
Qingsi Wangc129c352019-04-18 10:41:58 -07005367}
5368
Eldar Relloda13ea22019-06-01 12:23:43 +03005369TEST_P(PeerConnectionIntegrationTest, OnIceCandidateError) {
Eldar Relloda13ea22019-06-01 12:23:43 +03005370 static const rtc::SocketAddress turn_server_internal_address{"88.88.88.0",
5371 3478};
5372 static const rtc::SocketAddress turn_server_external_address{"88.88.88.1", 0};
5373
5374 CreateTurnServer(turn_server_internal_address, turn_server_external_address);
5375
5376 webrtc::PeerConnectionInterface::IceServer ice_server;
5377 ice_server.urls.push_back("turn:88.88.88.0:3478");
5378 ice_server.username = "test";
5379 ice_server.password = "123";
5380
5381 PeerConnectionInterface::RTCConfiguration caller_config;
5382 caller_config.servers.push_back(ice_server);
5383 caller_config.type = webrtc::PeerConnectionInterface::kRelay;
5384 caller_config.continual_gathering_policy = PeerConnection::GATHER_CONTINUALLY;
5385
5386 PeerConnectionInterface::RTCConfiguration callee_config;
5387 callee_config.servers.push_back(ice_server);
5388 callee_config.type = webrtc::PeerConnectionInterface::kRelay;
5389 callee_config.continual_gathering_policy = PeerConnection::GATHER_CONTINUALLY;
5390
5391 ASSERT_TRUE(
5392 CreatePeerConnectionWrappersWithConfig(caller_config, callee_config));
5393
5394 // Do normal offer/answer and wait for ICE to complete.
5395 ConnectFakeSignaling();
5396 caller()->AddAudioVideoTracks();
5397 callee()->AddAudioVideoTracks();
5398 caller()->CreateAndSetAndSignalOffer();
5399 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5400 EXPECT_EQ_WAIT(401, caller()->error_event().error_code, kDefaultTimeout);
5401 EXPECT_EQ("Unauthorized", caller()->error_event().error_text);
5402 EXPECT_EQ("turn:88.88.88.0:3478?transport=udp", caller()->error_event().url);
Eldar Rello0095d372019-12-02 22:22:07 +02005403 EXPECT_NE(caller()->error_event().address, "");
Eldar Relloda13ea22019-06-01 12:23:43 +03005404}
5405
Eldar Rellofa8019c2020-05-14 11:59:33 +03005406TEST_P(PeerConnectionIntegrationTest, OnIceCandidateErrorWithEmptyAddress) {
5407 webrtc::PeerConnectionInterface::IceServer ice_server;
5408 ice_server.urls.push_back("turn:127.0.0.1:3478?transport=tcp");
5409 ice_server.username = "test";
5410 ice_server.password = "test";
5411
5412 PeerConnectionInterface::RTCConfiguration caller_config;
5413 caller_config.servers.push_back(ice_server);
5414 caller_config.type = webrtc::PeerConnectionInterface::kRelay;
5415 caller_config.continual_gathering_policy = PeerConnection::GATHER_CONTINUALLY;
5416
5417 PeerConnectionInterface::RTCConfiguration callee_config;
5418 callee_config.servers.push_back(ice_server);
5419 callee_config.type = webrtc::PeerConnectionInterface::kRelay;
5420 callee_config.continual_gathering_policy = PeerConnection::GATHER_CONTINUALLY;
5421
5422 ASSERT_TRUE(
5423 CreatePeerConnectionWrappersWithConfig(caller_config, callee_config));
5424
5425 // Do normal offer/answer and wait for ICE to complete.
5426 ConnectFakeSignaling();
5427 caller()->AddAudioVideoTracks();
5428 callee()->AddAudioVideoTracks();
5429 caller()->CreateAndSetAndSignalOffer();
5430 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5431 EXPECT_EQ_WAIT(701, caller()->error_event().error_code, kDefaultTimeout);
5432 EXPECT_EQ(caller()->error_event().address, "");
5433}
5434
Eldar Rello5ab79e62019-10-09 18:29:44 +03005435TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
5436 AudioKeepsFlowingAfterImplicitRollback) {
5437 PeerConnectionInterface::RTCConfiguration config;
5438 config.sdp_semantics = SdpSemantics::kUnifiedPlan;
5439 config.enable_implicit_rollback = true;
5440 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
5441 ConnectFakeSignaling();
5442 caller()->AddAudioTrack();
5443 callee()->AddAudioTrack();
5444 caller()->CreateAndSetAndSignalOffer();
5445 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5446 MediaExpectations media_expectations;
5447 media_expectations.ExpectBidirectionalAudio();
5448 ASSERT_TRUE(ExpectNewFrames(media_expectations));
5449 SetSignalIceCandidates(false); // Workaround candidate outrace sdp.
5450 caller()->AddVideoTrack();
5451 callee()->AddVideoTrack();
5452 rtc::scoped_refptr<MockSetSessionDescriptionObserver> observer(
5453 new rtc::RefCountedObject<MockSetSessionDescriptionObserver>());
5454 callee()->pc()->SetLocalDescription(observer,
5455 callee()->CreateOfferAndWait().release());
5456 EXPECT_TRUE_WAIT(observer->called(), kDefaultTimeout);
5457 caller()->CreateAndSetAndSignalOffer(); // Implicit rollback.
5458 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5459 ASSERT_TRUE(ExpectNewFrames(media_expectations));
5460}
5461
5462TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
5463 ImplicitRollbackVisitsStableState) {
5464 RTCConfiguration config;
5465 config.sdp_semantics = SdpSemantics::kUnifiedPlan;
5466 config.enable_implicit_rollback = true;
5467
5468 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
5469
5470 rtc::scoped_refptr<MockSetSessionDescriptionObserver> sld_observer(
5471 new rtc::RefCountedObject<MockSetSessionDescriptionObserver>());
5472 callee()->pc()->SetLocalDescription(sld_observer,
5473 callee()->CreateOfferAndWait().release());
5474 EXPECT_TRUE_WAIT(sld_observer->called(), kDefaultTimeout);
5475 EXPECT_EQ(sld_observer->error(), "");
5476
5477 rtc::scoped_refptr<MockSetSessionDescriptionObserver> srd_observer(
5478 new rtc::RefCountedObject<MockSetSessionDescriptionObserver>());
5479 callee()->pc()->SetRemoteDescription(
5480 srd_observer, caller()->CreateOfferAndWait().release());
5481 EXPECT_TRUE_WAIT(srd_observer->called(), kDefaultTimeout);
5482 EXPECT_EQ(srd_observer->error(), "");
5483
5484 EXPECT_THAT(callee()->peer_connection_signaling_state_history(),
5485 ElementsAre(PeerConnectionInterface::kHaveLocalOffer,
5486 PeerConnectionInterface::kStable,
5487 PeerConnectionInterface::kHaveRemoteOffer));
5488}
5489
Eldar Rellobd9c33a2020-10-01 17:52:45 +03005490TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
5491 H264FmtpSpsPpsIdrInKeyframeParameterUsage) {
5492 ASSERT_TRUE(CreatePeerConnectionWrappers());
5493 ConnectFakeSignaling();
5494 caller()->AddVideoTrack();
5495 callee()->AddVideoTrack();
5496 auto munger = [](cricket::SessionDescription* desc) {
5497 cricket::VideoContentDescription* video =
5498 GetFirstVideoContentDescription(desc);
5499 auto codecs = video->codecs();
5500 for (auto&& codec : codecs) {
5501 if (codec.name == "H264") {
5502 std::string value;
5503 // The parameter is not supposed to be present in SDP by default.
5504 EXPECT_FALSE(
5505 codec.GetParam(cricket::kH264FmtpSpsPpsIdrInKeyframe, &value));
5506 codec.SetParam(std::string(cricket::kH264FmtpSpsPpsIdrInKeyframe),
5507 std::string(""));
5508 }
5509 }
5510 video->set_codecs(codecs);
5511 };
5512 // Munge local offer for SLD.
5513 caller()->SetGeneratedSdpMunger(munger);
5514 // Munge remote answer for SRD.
5515 caller()->SetReceivedSdpMunger(munger);
5516 caller()->CreateAndSetAndSignalOffer();
5517 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5518 // Observe that after munging the parameter is present in generated SDP.
5519 caller()->SetGeneratedSdpMunger([](cricket::SessionDescription* desc) {
5520 cricket::VideoContentDescription* video =
5521 GetFirstVideoContentDescription(desc);
5522 for (auto&& codec : video->codecs()) {
5523 if (codec.name == "H264") {
5524 std::string value;
5525 EXPECT_TRUE(
5526 codec.GetParam(cricket::kH264FmtpSpsPpsIdrInKeyframe, &value));
5527 }
5528 }
5529 });
5530 caller()->CreateOfferAndWait();
5531}
5532
Mirko Bonadeic84f6612019-01-31 12:20:57 +01005533INSTANTIATE_TEST_SUITE_P(PeerConnectionIntegrationTest,
5534 PeerConnectionIntegrationTest,
5535 Values(SdpSemantics::kPlanB,
5536 SdpSemantics::kUnifiedPlan));
Steve Antond3679212018-01-17 17:41:02 -08005537
Yves Gerey100fe632020-01-17 19:15:53 +01005538INSTANTIATE_TEST_SUITE_P(PeerConnectionIntegrationTest,
5539 PeerConnectionIntegrationTestWithFakeClock,
5540 Values(SdpSemantics::kPlanB,
5541 SdpSemantics::kUnifiedPlan));
5542
Steve Anton74255ff2018-01-24 18:32:57 -08005543// Tests that verify interoperability between Plan B and Unified Plan
5544// PeerConnections.
5545class PeerConnectionIntegrationInteropTest
Seth Hampson2f0d7022018-02-20 11:54:42 -08005546 : public PeerConnectionIntegrationBaseTest,
Steve Anton74255ff2018-01-24 18:32:57 -08005547 public ::testing::WithParamInterface<
5548 std::tuple<SdpSemantics, SdpSemantics>> {
5549 protected:
Seth Hampson2f0d7022018-02-20 11:54:42 -08005550 // Setting the SdpSemantics for the base test to kDefault does not matter
5551 // because we specify not to use the test semantics when creating
5552 // PeerConnectionWrappers.
Steve Anton74255ff2018-01-24 18:32:57 -08005553 PeerConnectionIntegrationInteropTest()
Steve Anton3acffc32018-04-12 17:21:03 -07005554 : PeerConnectionIntegrationBaseTest(SdpSemantics::kPlanB),
Seth Hampson2f0d7022018-02-20 11:54:42 -08005555 caller_semantics_(std::get<0>(GetParam())),
Steve Anton74255ff2018-01-24 18:32:57 -08005556 callee_semantics_(std::get<1>(GetParam())) {}
5557
5558 bool CreatePeerConnectionWrappersWithSemantics() {
Steve Anton3acffc32018-04-12 17:21:03 -07005559 return CreatePeerConnectionWrappersWithSdpSemantics(caller_semantics_,
5560 callee_semantics_);
Steve Anton74255ff2018-01-24 18:32:57 -08005561 }
5562
5563 const SdpSemantics caller_semantics_;
5564 const SdpSemantics callee_semantics_;
5565};
5566
5567TEST_P(PeerConnectionIntegrationInteropTest, NoMediaLocalToNoMediaRemote) {
5568 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
5569 ConnectFakeSignaling();
5570
5571 caller()->CreateAndSetAndSignalOffer();
5572 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5573}
5574
5575TEST_P(PeerConnectionIntegrationInteropTest, OneAudioLocalToNoMediaRemote) {
5576 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
5577 ConnectFakeSignaling();
5578 auto audio_sender = caller()->AddAudioTrack();
5579
5580 caller()->CreateAndSetAndSignalOffer();
5581 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5582
5583 // Verify that one audio receiver has been created on the remote and that it
5584 // has the same track ID as the sending track.
5585 auto receivers = callee()->pc()->GetReceivers();
5586 ASSERT_EQ(1u, receivers.size());
5587 EXPECT_EQ(cricket::MEDIA_TYPE_AUDIO, receivers[0]->media_type());
5588 EXPECT_EQ(receivers[0]->track()->id(), audio_sender->track()->id());
5589
Seth Hampson2f0d7022018-02-20 11:54:42 -08005590 MediaExpectations media_expectations;
5591 media_expectations.CalleeExpectsSomeAudio();
5592 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08005593}
5594
5595TEST_P(PeerConnectionIntegrationInteropTest, OneAudioOneVideoToNoMediaRemote) {
5596 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
5597 ConnectFakeSignaling();
5598 auto video_sender = caller()->AddVideoTrack();
5599 auto audio_sender = caller()->AddAudioTrack();
5600
5601 caller()->CreateAndSetAndSignalOffer();
5602 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5603
5604 // Verify that one audio and one video receiver have been created on the
5605 // remote and that they have the same track IDs as the sending tracks.
5606 auto audio_receivers =
5607 callee()->GetReceiversOfType(cricket::MEDIA_TYPE_AUDIO);
5608 ASSERT_EQ(1u, audio_receivers.size());
5609 EXPECT_EQ(audio_receivers[0]->track()->id(), audio_sender->track()->id());
5610 auto video_receivers =
5611 callee()->GetReceiversOfType(cricket::MEDIA_TYPE_VIDEO);
5612 ASSERT_EQ(1u, video_receivers.size());
5613 EXPECT_EQ(video_receivers[0]->track()->id(), video_sender->track()->id());
5614
Seth Hampson2f0d7022018-02-20 11:54:42 -08005615 MediaExpectations media_expectations;
5616 media_expectations.CalleeExpectsSomeAudioAndVideo();
5617 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08005618}
5619
5620TEST_P(PeerConnectionIntegrationInteropTest,
5621 OneAudioOneVideoLocalToOneAudioOneVideoRemote) {
5622 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
5623 ConnectFakeSignaling();
5624 caller()->AddAudioVideoTracks();
5625 callee()->AddAudioVideoTracks();
5626
5627 caller()->CreateAndSetAndSignalOffer();
5628 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5629
Seth Hampson2f0d7022018-02-20 11:54:42 -08005630 MediaExpectations media_expectations;
5631 media_expectations.ExpectBidirectionalAudioAndVideo();
5632 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08005633}
5634
5635TEST_P(PeerConnectionIntegrationInteropTest,
5636 ReverseRolesOneAudioLocalToOneVideoRemote) {
5637 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
5638 ConnectFakeSignaling();
5639 caller()->AddAudioTrack();
5640 callee()->AddVideoTrack();
5641
5642 caller()->CreateAndSetAndSignalOffer();
5643 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5644
5645 // Verify that only the audio track has been negotiated.
5646 EXPECT_EQ(0u, caller()->GetReceiversOfType(cricket::MEDIA_TYPE_VIDEO).size());
5647 // Might also check that the callee's NegotiationNeeded flag is set.
5648
5649 // Reverse roles.
5650 callee()->CreateAndSetAndSignalOffer();
5651 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5652
Seth Hampson2f0d7022018-02-20 11:54:42 -08005653 MediaExpectations media_expectations;
5654 media_expectations.CallerExpectsSomeVideo();
5655 media_expectations.CalleeExpectsSomeAudio();
5656 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08005657}
5658
Mirko Bonadeic84f6612019-01-31 12:20:57 +01005659INSTANTIATE_TEST_SUITE_P(
Steve Antonba42e992018-04-09 14:10:01 -07005660 PeerConnectionIntegrationTest,
5661 PeerConnectionIntegrationInteropTest,
5662 Values(std::make_tuple(SdpSemantics::kPlanB, SdpSemantics::kUnifiedPlan),
5663 std::make_tuple(SdpSemantics::kUnifiedPlan, SdpSemantics::kPlanB)));
5664
5665// Test that if the Unified Plan side offers two video tracks then the Plan B
5666// side will only see the first one and ignore the second.
5667TEST_F(PeerConnectionIntegrationTestPlanB, TwoVideoUnifiedPlanToNoMediaPlanB) {
Steve Anton3acffc32018-04-12 17:21:03 -07005668 ASSERT_TRUE(CreatePeerConnectionWrappersWithSdpSemantics(
5669 SdpSemantics::kUnifiedPlan, SdpSemantics::kPlanB));
Steve Anton74255ff2018-01-24 18:32:57 -08005670 ConnectFakeSignaling();
5671 auto first_sender = caller()->AddVideoTrack();
5672 caller()->AddVideoTrack();
5673
5674 caller()->CreateAndSetAndSignalOffer();
5675 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5676
5677 // Verify that there is only one receiver and it corresponds to the first
5678 // added track.
5679 auto receivers = callee()->pc()->GetReceivers();
5680 ASSERT_EQ(1u, receivers.size());
5681 EXPECT_TRUE(receivers[0]->track()->enabled());
5682 EXPECT_EQ(first_sender->track()->id(), receivers[0]->track()->id());
5683
Seth Hampson2f0d7022018-02-20 11:54:42 -08005684 MediaExpectations media_expectations;
5685 media_expectations.CalleeExpectsSomeVideo();
5686 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08005687}
5688
Steve Anton2bed3972019-01-04 17:04:30 -08005689// Test that if the initial offer tagged BUNDLE section is rejected due to its
5690// associated RtpTransceiver being stopped and another transceiver is added,
5691// then renegotiation causes the callee to receive the new video track without
5692// error.
5693// This is a regression test for bugs.webrtc.org/9954
5694TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
5695 ReOfferWithStoppedBundleTaggedTransceiver) {
5696 RTCConfiguration config;
5697 config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
5698 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
5699 ConnectFakeSignaling();
5700 auto audio_transceiver_or_error =
5701 caller()->pc()->AddTransceiver(caller()->CreateLocalAudioTrack());
5702 ASSERT_TRUE(audio_transceiver_or_error.ok());
5703 auto audio_transceiver = audio_transceiver_or_error.MoveValue();
5704
5705 caller()->CreateAndSetAndSignalOffer();
5706 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5707 {
5708 MediaExpectations media_expectations;
5709 media_expectations.CalleeExpectsSomeAudio();
5710 ASSERT_TRUE(ExpectNewFrames(media_expectations));
5711 }
5712
Harald Alvestrand6060df52020-08-11 09:54:02 +02005713 audio_transceiver->StopInternal();
Steve Anton2bed3972019-01-04 17:04:30 -08005714 caller()->pc()->AddTransceiver(caller()->CreateLocalVideoTrack());
5715
5716 caller()->CreateAndSetAndSignalOffer();
5717 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5718 {
5719 MediaExpectations media_expectations;
5720 media_expectations.CalleeExpectsSomeVideo();
5721 ASSERT_TRUE(ExpectNewFrames(media_expectations));
5722 }
5723}
5724
Harald Alvestrandbedb6052020-08-20 14:50:10 +02005725TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
5726 StopTransceiverRemovesDtlsTransports) {
5727 RTCConfiguration config;
5728 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
5729 ConnectFakeSignaling();
5730 auto audio_transceiver_or_error =
5731 caller()->pc()->AddTransceiver(caller()->CreateLocalAudioTrack());
5732 ASSERT_TRUE(audio_transceiver_or_error.ok());
5733 auto audio_transceiver = audio_transceiver_or_error.MoveValue();
5734
5735 caller()->CreateAndSetAndSignalOffer();
5736 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5737
5738 audio_transceiver->StopStandard();
5739 caller()->CreateAndSetAndSignalOffer();
5740 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5741 ASSERT_EQ(0U, caller()->pc()->GetTransceivers().size());
5742 EXPECT_EQ(PeerConnectionInterface::kIceGatheringNew,
5743 caller()->pc()->ice_gathering_state());
5744 EXPECT_THAT(caller()->ice_gathering_state_history(),
5745 ElementsAre(PeerConnectionInterface::kIceGatheringGathering,
5746 PeerConnectionInterface::kIceGatheringComplete,
5747 PeerConnectionInterface::kIceGatheringNew));
5748}
5749
Harald Alvestrand1ee33252020-09-24 13:31:15 +00005750TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
Harald Alvestrand45be0a92020-09-30 06:55:23 +00005751 StopTransceiverStopsAndRemovesTransceivers) {
5752 RTCConfiguration config;
5753 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
5754 ConnectFakeSignaling();
5755 auto audio_transceiver_or_error =
5756 caller()->pc()->AddTransceiver(caller()->CreateLocalAudioTrack());
5757 ASSERT_TRUE(audio_transceiver_or_error.ok());
5758 auto caller_transceiver = audio_transceiver_or_error.MoveValue();
5759
5760 caller()->CreateAndSetAndSignalOffer();
5761 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5762 caller_transceiver->StopStandard();
5763
5764 auto callee_transceiver = callee()->pc()->GetTransceivers()[0];
5765 caller()->CreateAndSetAndSignalOffer();
5766 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5767 EXPECT_EQ(0U, caller()->pc()->GetTransceivers().size());
5768 EXPECT_EQ(0U, callee()->pc()->GetTransceivers().size());
5769 EXPECT_EQ(0U, caller()->pc()->GetSenders().size());
5770 EXPECT_EQ(0U, callee()->pc()->GetSenders().size());
5771 EXPECT_EQ(0U, caller()->pc()->GetReceivers().size());
5772 EXPECT_EQ(0U, callee()->pc()->GetReceivers().size());
5773 EXPECT_TRUE(caller_transceiver->stopped());
5774 EXPECT_TRUE(callee_transceiver->stopped());
5775}
5776
5777TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
Harald Alvestrand1ee33252020-09-24 13:31:15 +00005778 StopTransceiverEndsIncomingAudioTrack) {
5779 RTCConfiguration config;
5780 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
5781 ConnectFakeSignaling();
5782 auto audio_transceiver_or_error =
5783 caller()->pc()->AddTransceiver(caller()->CreateLocalAudioTrack());
5784 ASSERT_TRUE(audio_transceiver_or_error.ok());
5785 auto audio_transceiver = audio_transceiver_or_error.MoveValue();
5786
5787 caller()->CreateAndSetAndSignalOffer();
5788 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5789 auto caller_track = audio_transceiver->receiver()->track();
5790 auto callee_track = callee()->pc()->GetReceivers()[0]->track();
5791 audio_transceiver->StopStandard();
5792 EXPECT_EQ(MediaStreamTrackInterface::TrackState::kEnded,
5793 caller_track->state());
5794 caller()->CreateAndSetAndSignalOffer();
5795 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5796 EXPECT_EQ(MediaStreamTrackInterface::TrackState::kEnded,
5797 callee_track->state());
5798}
5799
5800TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
5801 StopTransceiverEndsIncomingVideoTrack) {
5802 RTCConfiguration config;
5803 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
5804 ConnectFakeSignaling();
5805 auto audio_transceiver_or_error =
5806 caller()->pc()->AddTransceiver(caller()->CreateLocalVideoTrack());
5807 ASSERT_TRUE(audio_transceiver_or_error.ok());
5808 auto audio_transceiver = audio_transceiver_or_error.MoveValue();
5809
5810 caller()->CreateAndSetAndSignalOffer();
5811 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5812 auto caller_track = audio_transceiver->receiver()->track();
5813 auto callee_track = callee()->pc()->GetReceivers()[0]->track();
5814 audio_transceiver->StopStandard();
5815 EXPECT_EQ(MediaStreamTrackInterface::TrackState::kEnded,
5816 caller_track->state());
5817 caller()->CreateAndSetAndSignalOffer();
5818 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5819 EXPECT_EQ(MediaStreamTrackInterface::TrackState::kEnded,
5820 callee_track->state());
5821}
5822
Harald Alvestrandd61f2a72019-05-08 20:20:59 +02005823#ifdef HAVE_SCTP
5824
5825TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
5826 EndToEndCallWithBundledSctpDataChannel) {
5827 ASSERT_TRUE(CreatePeerConnectionWrappers());
5828 ConnectFakeSignaling();
5829 caller()->CreateDataChannel();
5830 caller()->AddAudioVideoTracks();
5831 callee()->AddAudioVideoTracks();
Harald Alvestrandd61f2a72019-05-08 20:20:59 +02005832 caller()->CreateAndSetAndSignalOffer();
5833 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Harald Alvestrand17ea0682019-12-13 11:51:04 +01005834 ASSERT_EQ_WAIT(SctpTransportState::kConnected,
5835 caller()->pc()->GetSctpTransport()->Information().state(),
5836 kDefaultTimeout);
5837 ASSERT_TRUE_WAIT(callee()->data_channel(), kDefaultTimeout);
5838 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
5839}
5840
5841TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
5842 EndToEndCallWithDataChannelOnlyConnects) {
5843 ASSERT_TRUE(CreatePeerConnectionWrappers());
5844 ConnectFakeSignaling();
5845 caller()->CreateDataChannel();
5846 caller()->CreateAndSetAndSignalOffer();
5847 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5848 ASSERT_TRUE_WAIT(callee()->data_channel(), kDefaultTimeout);
5849 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
5850 ASSERT_TRUE(caller()->data_observer()->IsOpen());
Harald Alvestrandd61f2a72019-05-08 20:20:59 +02005851}
5852
Harald Alvestrand2697ac12019-12-16 10:37:04 +01005853TEST_F(PeerConnectionIntegrationTestUnifiedPlan, DataChannelClosesWhenClosed) {
5854 ASSERT_TRUE(CreatePeerConnectionWrappers());
5855 ConnectFakeSignaling();
5856 caller()->CreateDataChannel();
5857 caller()->CreateAndSetAndSignalOffer();
5858 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5859 ASSERT_TRUE_WAIT(callee()->data_observer(), kDefaultTimeout);
5860 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
5861 caller()->data_channel()->Close();
5862 ASSERT_TRUE_WAIT(!callee()->data_observer()->IsOpen(), kDefaultTimeout);
5863}
5864
5865TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
5866 DataChannelClosesWhenClosedReverse) {
5867 ASSERT_TRUE(CreatePeerConnectionWrappers());
5868 ConnectFakeSignaling();
5869 caller()->CreateDataChannel();
5870 caller()->CreateAndSetAndSignalOffer();
5871 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5872 ASSERT_TRUE_WAIT(callee()->data_observer(), kDefaultTimeout);
5873 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
5874 callee()->data_channel()->Close();
5875 ASSERT_TRUE_WAIT(!caller()->data_observer()->IsOpen(), kDefaultTimeout);
5876}
5877
5878TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
5879 DataChannelClosesWhenPeerConnectionClosed) {
5880 ASSERT_TRUE(CreatePeerConnectionWrappers());
5881 ConnectFakeSignaling();
5882 caller()->CreateDataChannel();
5883 caller()->CreateAndSetAndSignalOffer();
5884 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5885 ASSERT_TRUE_WAIT(callee()->data_observer(), kDefaultTimeout);
5886 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
5887 caller()->pc()->Close();
5888 ASSERT_TRUE_WAIT(!callee()->data_observer()->IsOpen(), kDefaultTimeout);
5889}
5890
Harald Alvestrandd61f2a72019-05-08 20:20:59 +02005891#endif // HAVE_SCTP
5892
deadbeef1dcb1642017-03-29 21:08:16 -07005893} // namespace
Mirko Bonadeiab64e8a2018-12-12 12:10:18 +01005894} // namespace webrtc
deadbeef1dcb1642017-03-29 21:08:16 -07005895
5896#endif // if !defined(THREAD_SANITIZER)