blob: 6358bce074c8ec2d8273d8b8a99d6d5dcef0b8b9 [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
Harald Alvestrandf8f7b702022-05-05 13:21:19 +000011// Integration tests for PeerConnection.
12// These tests exercise a full stack over a simulated network.
13//
14// NOTE: If your test takes a while (guideline: more than 5 seconds),
15// do NOT add it here, but instead add it to the file
16// slow_peer_connection_integrationtest.cc
17
Harald Alvestrand39993842021-02-17 09:05:31 +000018#include <stdint.h>
deadbeef1dcb1642017-03-29 21:08:16 -070019
Taylor Brandstetterd3ef4992020-10-15 18:22:57 -070020#include <algorithm>
deadbeef1dcb1642017-03-29 21:08:16 -070021#include <memory>
Harald Alvestrand39993842021-02-17 09:05:31 +000022#include <string>
23#include <tuple>
deadbeef1dcb1642017-03-29 21:08:16 -070024#include <utility>
25#include <vector>
26
Steve Anton64b626b2019-01-28 17:25:26 -080027#include "absl/algorithm/container.h"
Harald Alvestrandf8f7b702022-05-05 13:21:19 +000028#include "absl/strings/string_view.h"
Harald Alvestrand39993842021-02-17 09:05:31 +000029#include "absl/types/optional.h"
30#include "api/async_resolver_factory.h"
31#include "api/candidate.h"
32#include "api/crypto/crypto_options.h"
33#include "api/dtmf_sender_interface.h"
34#include "api/ice_transport_interface.h"
35#include "api/jsep.h"
Steve Anton10542f22019-01-11 09:11:00 -080036#include "api/media_stream_interface.h"
Harald Alvestrand39993842021-02-17 09:05:31 +000037#include "api/media_types.h"
Steve Anton10542f22019-01-11 09:11:00 -080038#include "api/peer_connection_interface.h"
Harald Alvestrand39993842021-02-17 09:05:31 +000039#include "api/rtc_error.h"
40#include "api/rtc_event_log/rtc_event.h"
41#include "api/rtc_event_log/rtc_event_log.h"
42#include "api/rtc_event_log_output.h"
43#include "api/rtp_parameters.h"
Steve Anton10542f22019-01-11 09:11:00 -080044#include "api/rtp_receiver_interface.h"
Harald Alvestrand39993842021-02-17 09:05:31 +000045#include "api/rtp_sender_interface.h"
46#include "api/rtp_transceiver_direction.h"
47#include "api/rtp_transceiver_interface.h"
48#include "api/scoped_refptr.h"
49#include "api/stats/rtc_stats.h"
50#include "api/stats/rtc_stats_report.h"
51#include "api/stats/rtcstats_objects.h"
Jonas Oreland65455162022-06-08 11:25:46 +020052#include "api/test/mock_encoder_selector.h"
Harald Alvestrand39993842021-02-17 09:05:31 +000053#include "api/transport/rtp/rtp_source.h"
Steve Anton10542f22019-01-11 09:11:00 -080054#include "api/uma_metrics.h"
Harald Alvestrand39993842021-02-17 09:05:31 +000055#include "api/units/time_delta.h"
56#include "api/video/video_rotation.h"
57#include "logging/rtc_event_log/fake_rtc_event_log.h"
Qingsi Wang7685e862018-06-11 20:15:46 -070058#include "logging/rtc_event_log/fake_rtc_event_log_factory.h"
Harald Alvestrand39993842021-02-17 09:05:31 +000059#include "media/base/codec.h"
60#include "media/base/media_constants.h"
61#include "media/base/stream_params.h"
Steve Anton10542f22019-01-11 09:11:00 -080062#include "p2p/base/mock_async_resolver.h"
Harald Alvestrand39993842021-02-17 09:05:31 +000063#include "p2p/base/port.h"
64#include "p2p/base/port_allocator.h"
Steve Anton10542f22019-01-11 09:11:00 -080065#include "p2p/base/port_interface.h"
66#include "p2p/base/test_stun_server.h"
67#include "p2p/base/test_turn_customizer.h"
68#include "p2p/base/test_turn_server.h"
Harald Alvestrand39993842021-02-17 09:05:31 +000069#include "p2p/base/transport_description.h"
70#include "p2p/base/transport_info.h"
Steve Anton10542f22019-01-11 09:11:00 -080071#include "pc/media_session.h"
72#include "pc/peer_connection.h"
73#include "pc/peer_connection_factory.h"
Steve Anton10542f22019-01-11 09:11:00 -080074#include "pc/session_description.h"
Harald Alvestrand39993842021-02-17 09:05:31 +000075#include "pc/test/fake_periodic_video_source.h"
76#include "pc/test/integration_test_helpers.h"
Steve Anton10542f22019-01-11 09:11:00 -080077#include "pc/test/mock_peer_connection_observers.h"
Jonas Olssonb75d9e92019-02-22 10:33:29 +010078#include "rtc_base/fake_clock.h"
Qingsi Wangecd30542019-05-22 14:34:56 -070079#include "rtc_base/fake_mdns_responder.h"
Steve Anton10542f22019-01-11 09:11:00 -080080#include "rtc_base/fake_network.h"
81#include "rtc_base/firewall_socket_server.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020082#include "rtc_base/gunit.h"
Harald Alvestrand39993842021-02-17 09:05:31 +000083#include "rtc_base/helpers.h"
84#include "rtc_base/location.h"
85#include "rtc_base/logging.h"
86#include "rtc_base/ref_counted_object.h"
87#include "rtc_base/socket_address.h"
88#include "rtc_base/ssl_certificate.h"
89#include "rtc_base/ssl_fingerprint.h"
90#include "rtc_base/ssl_identity.h"
91#include "rtc_base/ssl_stream_adapter.h"
Steve Anton10542f22019-01-11 09:11:00 -080092#include "rtc_base/test_certificate_verifier.h"
Harald Alvestrand39993842021-02-17 09:05:31 +000093#include "rtc_base/thread.h"
Steve Anton10542f22019-01-11 09:11:00 -080094#include "rtc_base/time_utils.h"
95#include "rtc_base/virtual_socket_server.h"
Mirko Bonadei17f48782018-09-28 08:51:10 +020096#include "system_wrappers/include/metrics.h"
Harald Alvestrandc24a2182022-02-23 13:44:59 +000097#include "test/gmock.h"
98#include "test/gtest.h"
deadbeef1dcb1642017-03-29 21:08:16 -070099
Mirko Bonadeiab64e8a2018-12-12 12:10:18 +0100100namespace webrtc {
Harald Alvestrand39993842021-02-17 09:05:31 +0000101
Mirko Bonadeiab64e8a2018-12-12 12:10:18 +0100102namespace {
103
Seth Hampson2f0d7022018-02-20 11:54:42 -0800104class PeerConnectionIntegrationTest
105 : public PeerConnectionIntegrationBaseTest,
Evan Shrubsole7619b7c2022-03-01 10:42:44 +0100106 public ::testing::WithParamInterface<
107 std::tuple<SdpSemantics, std::string>> {
Seth Hampson2f0d7022018-02-20 11:54:42 -0800108 protected:
109 PeerConnectionIntegrationTest()
Evan Shrubsole7619b7c2022-03-01 10:42:44 +0100110 : PeerConnectionIntegrationBaseTest(std::get<0>(GetParam()),
111 std::get<1>(GetParam())) {}
Seth Hampson2f0d7022018-02-20 11:54:42 -0800112};
113
Yves Gerey100fe632020-01-17 19:15:53 +0100114// Fake clock must be set before threads are started to prevent race on
115// Set/GetClockForTesting().
116// To achieve that, multiple inheritance is used as a mixin pattern
117// where order of construction is finely controlled.
118// This also ensures peerconnection is closed before switching back to non-fake
119// clock, avoiding other races and DCHECK failures such as in rtp_sender.cc.
120class FakeClockForTest : public rtc::ScopedFakeClock {
121 protected:
122 FakeClockForTest() {
123 // Some things use a time of "0" as a special value, so we need to start out
124 // the fake clock at a nonzero time.
125 // TODO(deadbeef): Fix this.
Danil Chapovalov0c626af2020-02-10 11:16:00 +0100126 AdvanceTime(webrtc::TimeDelta::Seconds(1));
Yves Gerey100fe632020-01-17 19:15:53 +0100127 }
128
129 // Explicit handle.
130 ScopedFakeClock& FakeClock() { return *this; }
131};
132
133// Ensure FakeClockForTest is constructed first (see class for rationale).
134class PeerConnectionIntegrationTestWithFakeClock
135 : public FakeClockForTest,
136 public PeerConnectionIntegrationTest {};
137
Seth Hampson2f0d7022018-02-20 11:54:42 -0800138class PeerConnectionIntegrationTestPlanB
139 : public PeerConnectionIntegrationBaseTest {
140 protected:
141 PeerConnectionIntegrationTestPlanB()
Florent Castelli15a38de2022-04-06 00:38:21 +0200142 : PeerConnectionIntegrationBaseTest(SdpSemantics::kPlanB_DEPRECATED) {}
Seth Hampson2f0d7022018-02-20 11:54:42 -0800143};
144
145class PeerConnectionIntegrationTestUnifiedPlan
146 : public PeerConnectionIntegrationBaseTest {
147 protected:
148 PeerConnectionIntegrationTestUnifiedPlan()
149 : PeerConnectionIntegrationBaseTest(SdpSemantics::kUnifiedPlan) {}
150};
151
deadbeef1dcb1642017-03-29 21:08:16 -0700152// Test the OnFirstPacketReceived callback from audio/video RtpReceivers. This
153// includes testing that the callback is invoked if an observer is connected
154// after the first packet has already been received.
Seth Hampson2f0d7022018-02-20 11:54:42 -0800155TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -0700156 RtpReceiverObserverOnFirstPacketReceived) {
157 ASSERT_TRUE(CreatePeerConnectionWrappers());
158 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -0800159 caller()->AddAudioVideoTracks();
160 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -0700161 // Start offer/answer exchange and wait for it to complete.
162 caller()->CreateAndSetAndSignalOffer();
163 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
164 // Should be one receiver each for audio/video.
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +0200165 EXPECT_EQ(2U, caller()->rtp_receiver_observers().size());
166 EXPECT_EQ(2U, callee()->rtp_receiver_observers().size());
deadbeef1dcb1642017-03-29 21:08:16 -0700167 // Wait for all "first packet received" callbacks to be fired.
168 EXPECT_TRUE_WAIT(
Steve Anton64b626b2019-01-28 17:25:26 -0800169 absl::c_all_of(caller()->rtp_receiver_observers(),
170 [](const std::unique_ptr<MockRtpReceiverObserver>& o) {
171 return o->first_packet_received();
172 }),
deadbeef1dcb1642017-03-29 21:08:16 -0700173 kMaxWaitForFramesMs);
174 EXPECT_TRUE_WAIT(
Steve Anton64b626b2019-01-28 17:25:26 -0800175 absl::c_all_of(callee()->rtp_receiver_observers(),
176 [](const std::unique_ptr<MockRtpReceiverObserver>& o) {
177 return o->first_packet_received();
178 }),
deadbeef1dcb1642017-03-29 21:08:16 -0700179 kMaxWaitForFramesMs);
180 // If new observers are set after the first packet was already received, the
181 // callback should still be invoked.
182 caller()->ResetRtpReceiverObservers();
183 callee()->ResetRtpReceiverObservers();
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +0200184 EXPECT_EQ(2U, caller()->rtp_receiver_observers().size());
185 EXPECT_EQ(2U, callee()->rtp_receiver_observers().size());
deadbeef1dcb1642017-03-29 21:08:16 -0700186 EXPECT_TRUE(
Steve Anton64b626b2019-01-28 17:25:26 -0800187 absl::c_all_of(caller()->rtp_receiver_observers(),
188 [](const std::unique_ptr<MockRtpReceiverObserver>& o) {
189 return o->first_packet_received();
190 }));
deadbeef1dcb1642017-03-29 21:08:16 -0700191 EXPECT_TRUE(
Steve Anton64b626b2019-01-28 17:25:26 -0800192 absl::c_all_of(callee()->rtp_receiver_observers(),
193 [](const std::unique_ptr<MockRtpReceiverObserver>& o) {
194 return o->first_packet_received();
195 }));
deadbeef1dcb1642017-03-29 21:08:16 -0700196}
197
198class DummyDtmfObserver : public DtmfSenderObserverInterface {
199 public:
200 DummyDtmfObserver() : completed_(false) {}
201
202 // Implements DtmfSenderObserverInterface.
203 void OnToneChange(const std::string& tone) override {
204 tones_.push_back(tone);
205 if (tone.empty()) {
206 completed_ = true;
207 }
208 }
209
210 const std::vector<std::string>& tones() const { return tones_; }
211 bool completed() const { return completed_; }
212
213 private:
214 bool completed_;
215 std::vector<std::string> tones_;
216};
217
Artem Titov880fa812021-07-30 22:30:23 +0200218// Assumes `sender` already has an audio track added and the offer/answer
deadbeef1dcb1642017-03-29 21:08:16 -0700219// exchange is done.
Harald Alvestrand39993842021-02-17 09:05:31 +0000220void TestDtmfFromSenderToReceiver(PeerConnectionIntegrationWrapper* sender,
221 PeerConnectionIntegrationWrapper* receiver) {
Steve Anton15324772018-01-16 10:26:49 -0800222 // We should be able to get a DTMF sender from the local sender.
223 rtc::scoped_refptr<DtmfSenderInterface> dtmf_sender =
224 sender->pc()->GetSenders().at(0)->GetDtmfSender();
225 ASSERT_TRUE(dtmf_sender);
deadbeef1dcb1642017-03-29 21:08:16 -0700226 DummyDtmfObserver observer;
deadbeef1dcb1642017-03-29 21:08:16 -0700227 dtmf_sender->RegisterObserver(&observer);
228
229 // Test the DtmfSender object just created.
230 EXPECT_TRUE(dtmf_sender->CanInsertDtmf());
231 EXPECT_TRUE(dtmf_sender->InsertDtmf("1a", 100, 50));
232
233 EXPECT_TRUE_WAIT(observer.completed(), kDefaultTimeout);
234 std::vector<std::string> tones = {"1", "a", ""};
235 EXPECT_EQ(tones, observer.tones());
236 dtmf_sender->UnregisterObserver();
237 // TODO(deadbeef): Verify the tones were actually received end-to-end.
238}
239
240// Verifies the DtmfSenderObserver callbacks for a DtmfSender (one in each
241// direction).
Seth Hampson2f0d7022018-02-20 11:54:42 -0800242TEST_P(PeerConnectionIntegrationTest, DtmfSenderObserver) {
deadbeef1dcb1642017-03-29 21:08:16 -0700243 ASSERT_TRUE(CreatePeerConnectionWrappers());
244 ConnectFakeSignaling();
245 // Only need audio for DTMF.
Steve Anton15324772018-01-16 10:26:49 -0800246 caller()->AddAudioTrack();
247 callee()->AddAudioTrack();
deadbeef1dcb1642017-03-29 21:08:16 -0700248 caller()->CreateAndSetAndSignalOffer();
249 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
deadbeef71452802017-05-07 17:21:01 -0700250 // DTLS must finish before the DTMF sender can be used reliably.
251 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -0700252 TestDtmfFromSenderToReceiver(caller(), callee());
253 TestDtmfFromSenderToReceiver(callee(), caller());
254}
255
256// Basic end-to-end test, verifying media can be encoded/transmitted/decoded
257// between two connections, using DTLS-SRTP.
Seth Hampson2f0d7022018-02-20 11:54:42 -0800258TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithDtls) {
deadbeef1dcb1642017-03-29 21:08:16 -0700259 ASSERT_TRUE(CreatePeerConnectionWrappers());
260 ConnectFakeSignaling();
Harald Alvestrand194939b2018-01-24 16:04:13 +0100261
deadbeef1dcb1642017-03-29 21:08:16 -0700262 // Do normal offer/answer and wait for some frames to be received in each
263 // direction.
Steve Anton15324772018-01-16 10:26:49 -0800264 caller()->AddAudioVideoTracks();
265 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -0700266 caller()->CreateAndSetAndSignalOffer();
267 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -0800268 MediaExpectations media_expectations;
269 media_expectations.ExpectBidirectionalAudioAndVideo();
270 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Ying Wangef3998f2019-12-09 13:06:53 +0100271 EXPECT_METRIC_LE(
272 2, webrtc::metrics::NumEvents("WebRTC.PeerConnection.KeyProtocol",
273 webrtc::kEnumCounterKeyProtocolDtls));
274 EXPECT_METRIC_EQ(
275 0, webrtc::metrics::NumEvents("WebRTC.PeerConnection.KeyProtocol",
276 webrtc::kEnumCounterKeyProtocolSdes));
deadbeef1dcb1642017-03-29 21:08:16 -0700277}
278
Harald Alvestrandca327932022-04-04 15:37:31 +0000279#if defined(WEBRTC_FUCHSIA)
Harald Alvestrand50b95522021-11-18 10:01:06 +0000280// Uses SDES instead of DTLS for key agreement.
281TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithSdes) {
282 PeerConnectionInterface::RTCConfiguration sdes_config;
283 sdes_config.enable_dtls_srtp.emplace(false);
284 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(sdes_config, sdes_config));
285 ConnectFakeSignaling();
286
287 // Do normal offer/answer and wait for some frames to be received in each
288 // direction.
289 caller()->AddAudioVideoTracks();
290 callee()->AddAudioVideoTracks();
291 caller()->CreateAndSetAndSignalOffer();
292 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
293 MediaExpectations media_expectations;
294 media_expectations.ExpectBidirectionalAudioAndVideo();
295 ASSERT_TRUE(ExpectNewFrames(media_expectations));
296 EXPECT_METRIC_LE(
297 2, webrtc::metrics::NumEvents("WebRTC.PeerConnection.KeyProtocol",
298 webrtc::kEnumCounterKeyProtocolSdes));
299 EXPECT_METRIC_EQ(
300 0, webrtc::metrics::NumEvents("WebRTC.PeerConnection.KeyProtocol",
301 webrtc::kEnumCounterKeyProtocolDtls));
302}
Harald Alvestrandca327932022-04-04 15:37:31 +0000303#endif
Harald Alvestrand50b95522021-11-18 10:01:06 +0000304
Artem Titov880fa812021-07-30 22:30:23 +0200305// Basic end-to-end test specifying the `enable_encrypted_rtp_header_extensions`
Steve Anton9a44b2d2019-07-12 12:58:30 -0700306// option to offer encrypted versions of all header extensions alongside the
307// unencrypted versions.
308TEST_P(PeerConnectionIntegrationTest,
309 EndToEndCallWithEncryptedRtpHeaderExtensions) {
310 CryptoOptions crypto_options;
311 crypto_options.srtp.enable_encrypted_rtp_header_extensions = true;
312 PeerConnectionInterface::RTCConfiguration config;
313 config.crypto_options = crypto_options;
314 // Note: This allows offering >14 RTP header extensions.
315 config.offer_extmap_allow_mixed = true;
316 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
317 ConnectFakeSignaling();
318
319 // Do normal offer/answer and wait for some frames to be received in each
320 // direction.
321 caller()->AddAudioVideoTracks();
322 callee()->AddAudioVideoTracks();
323 caller()->CreateAndSetAndSignalOffer();
324 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
325 MediaExpectations media_expectations;
326 media_expectations.ExpectBidirectionalAudioAndVideo();
327 ASSERT_TRUE(ExpectNewFrames(media_expectations));
328}
329
deadbeef1dcb1642017-03-29 21:08:16 -0700330// This test sets up a call between two parties with a source resolution of
331// 1280x720 and verifies that a 16:9 aspect ratio is received.
Seth Hampson2f0d7022018-02-20 11:54:42 -0800332TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -0700333 Send1280By720ResolutionAndReceive16To9AspectRatio) {
334 ASSERT_TRUE(CreatePeerConnectionWrappers());
335 ConnectFakeSignaling();
336
Niels Möller5c7efe72018-05-11 10:34:46 +0200337 // Add video tracks with 16:9 aspect ratio, size 1280 x 720.
338 webrtc::FakePeriodicVideoSource::Config config;
339 config.width = 1280;
340 config.height = 720;
Johannes Kron965e7942018-09-13 15:36:20 +0200341 config.timestamp_offset_ms = rtc::TimeMillis();
Niels Möller5c7efe72018-05-11 10:34:46 +0200342 caller()->AddTrack(caller()->CreateLocalVideoTrackWithConfig(config));
343 callee()->AddTrack(callee()->CreateLocalVideoTrackWithConfig(config));
deadbeef1dcb1642017-03-29 21:08:16 -0700344
345 // Do normal offer/answer and wait for at least one frame to be received in
346 // each direction.
347 caller()->CreateAndSetAndSignalOffer();
348 ASSERT_TRUE_WAIT(caller()->min_video_frames_received_per_track() > 0 &&
349 callee()->min_video_frames_received_per_track() > 0,
350 kMaxWaitForFramesMs);
351
352 // Check rendered aspect ratio.
353 EXPECT_EQ(16.0 / 9, caller()->local_rendered_aspect_ratio());
354 EXPECT_EQ(16.0 / 9, caller()->rendered_aspect_ratio());
355 EXPECT_EQ(16.0 / 9, callee()->local_rendered_aspect_ratio());
356 EXPECT_EQ(16.0 / 9, callee()->rendered_aspect_ratio());
357}
358
359// This test sets up an one-way call, with media only from caller to
360// callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -0800361TEST_P(PeerConnectionIntegrationTest, OneWayMediaCall) {
deadbeef1dcb1642017-03-29 21:08:16 -0700362 ASSERT_TRUE(CreatePeerConnectionWrappers());
363 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -0800364 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -0700365 caller()->CreateAndSetAndSignalOffer();
Seth Hampson2f0d7022018-02-20 11:54:42 -0800366 MediaExpectations media_expectations;
367 media_expectations.CalleeExpectsSomeAudioAndVideo();
368 media_expectations.CallerExpectsNoAudio();
369 media_expectations.CallerExpectsNoVideo();
370 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -0700371}
372
Johannes Kron3e983682020-03-29 22:17:00 +0200373// Tests that send only works without the caller having a decoder factory and
374// the callee having an encoder factory.
375TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithSendOnlyVideo) {
376 ASSERT_TRUE(
377 CreateOneDirectionalPeerConnectionWrappers(/*caller_to_callee=*/true));
378 ConnectFakeSignaling();
379 // Add one-directional video, from caller to callee.
380 rtc::scoped_refptr<webrtc::VideoTrackInterface> caller_track =
381 caller()->CreateLocalVideoTrack();
382 caller()->AddTrack(caller_track);
383 PeerConnectionInterface::RTCOfferAnswerOptions options;
384 options.offer_to_receive_video = 0;
385 caller()->SetOfferAnswerOptions(options);
386 caller()->CreateAndSetAndSignalOffer();
387 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
388 ASSERT_EQ(callee()->pc()->GetReceivers().size(), 1u);
389
390 // Expect video to be received in one direction.
391 MediaExpectations media_expectations;
392 media_expectations.CallerExpectsNoVideo();
393 media_expectations.CalleeExpectsSomeVideo();
394
395 EXPECT_TRUE(ExpectNewFrames(media_expectations));
396}
397
398// Tests that receive only works without the caller having an encoder factory
399// and the callee having a decoder factory.
400TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithReceiveOnlyVideo) {
401 ASSERT_TRUE(
402 CreateOneDirectionalPeerConnectionWrappers(/*caller_to_callee=*/false));
403 ConnectFakeSignaling();
404 // Add one-directional video, from callee to caller.
405 rtc::scoped_refptr<webrtc::VideoTrackInterface> callee_track =
406 callee()->CreateLocalVideoTrack();
407 callee()->AddTrack(callee_track);
408 PeerConnectionInterface::RTCOfferAnswerOptions options;
409 options.offer_to_receive_video = 1;
410 caller()->SetOfferAnswerOptions(options);
411 caller()->CreateAndSetAndSignalOffer();
412 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
413 ASSERT_EQ(caller()->pc()->GetReceivers().size(), 1u);
414
415 // Expect video to be received in one direction.
416 MediaExpectations media_expectations;
417 media_expectations.CallerExpectsSomeVideo();
418 media_expectations.CalleeExpectsNoVideo();
419
420 EXPECT_TRUE(ExpectNewFrames(media_expectations));
421}
422
423TEST_P(PeerConnectionIntegrationTest,
424 EndToEndCallAddReceiveVideoToSendOnlyCall) {
425 ASSERT_TRUE(CreatePeerConnectionWrappers());
426 ConnectFakeSignaling();
427 // Add one-directional video, from caller to callee.
428 rtc::scoped_refptr<webrtc::VideoTrackInterface> caller_track =
429 caller()->CreateLocalVideoTrack();
430 caller()->AddTrack(caller_track);
431 caller()->CreateAndSetAndSignalOffer();
432 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
433
434 // Add receive video.
435 rtc::scoped_refptr<webrtc::VideoTrackInterface> callee_track =
436 callee()->CreateLocalVideoTrack();
437 callee()->AddTrack(callee_track);
438 caller()->CreateAndSetAndSignalOffer();
439 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
440
441 // Ensure that video frames are received end-to-end.
442 MediaExpectations media_expectations;
443 media_expectations.ExpectBidirectionalVideo();
444 ASSERT_TRUE(ExpectNewFrames(media_expectations));
445}
446
447TEST_P(PeerConnectionIntegrationTest,
448 EndToEndCallAddSendVideoToReceiveOnlyCall) {
449 ASSERT_TRUE(CreatePeerConnectionWrappers());
450 ConnectFakeSignaling();
451 // Add one-directional video, from callee to caller.
452 rtc::scoped_refptr<webrtc::VideoTrackInterface> callee_track =
453 callee()->CreateLocalVideoTrack();
454 callee()->AddTrack(callee_track);
455 caller()->CreateAndSetAndSignalOffer();
456 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
457
458 // Add send video.
459 rtc::scoped_refptr<webrtc::VideoTrackInterface> caller_track =
460 caller()->CreateLocalVideoTrack();
461 caller()->AddTrack(caller_track);
462 caller()->CreateAndSetAndSignalOffer();
463 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
464
465 // Expect video to be received in one direction.
466 MediaExpectations media_expectations;
467 media_expectations.ExpectBidirectionalVideo();
468 ASSERT_TRUE(ExpectNewFrames(media_expectations));
469}
470
471TEST_P(PeerConnectionIntegrationTest,
472 EndToEndCallRemoveReceiveVideoFromSendReceiveCall) {
473 ASSERT_TRUE(CreatePeerConnectionWrappers());
474 ConnectFakeSignaling();
475 // Add send video, from caller to callee.
476 rtc::scoped_refptr<webrtc::VideoTrackInterface> caller_track =
477 caller()->CreateLocalVideoTrack();
478 rtc::scoped_refptr<webrtc::RtpSenderInterface> caller_sender =
479 caller()->AddTrack(caller_track);
480 // Add receive video, from callee to caller.
481 rtc::scoped_refptr<webrtc::VideoTrackInterface> callee_track =
482 callee()->CreateLocalVideoTrack();
483
484 rtc::scoped_refptr<webrtc::RtpSenderInterface> callee_sender =
485 callee()->AddTrack(callee_track);
486 caller()->CreateAndSetAndSignalOffer();
487 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
488
489 // Remove receive video (i.e., callee sender track).
Harald Alvestrand93dd7632022-01-19 12:28:45 +0000490 callee()->pc()->RemoveTrackOrError(callee_sender);
Johannes Kron3e983682020-03-29 22:17:00 +0200491
492 caller()->CreateAndSetAndSignalOffer();
493 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
494
495 // Expect one-directional video.
496 MediaExpectations media_expectations;
497 media_expectations.CallerExpectsNoVideo();
498 media_expectations.CalleeExpectsSomeVideo();
499
500 ASSERT_TRUE(ExpectNewFrames(media_expectations));
501}
502
503TEST_P(PeerConnectionIntegrationTest,
504 EndToEndCallRemoveSendVideoFromSendReceiveCall) {
505 ASSERT_TRUE(CreatePeerConnectionWrappers());
506 ConnectFakeSignaling();
507 // Add send video, from caller to callee.
508 rtc::scoped_refptr<webrtc::VideoTrackInterface> caller_track =
509 caller()->CreateLocalVideoTrack();
510 rtc::scoped_refptr<webrtc::RtpSenderInterface> caller_sender =
511 caller()->AddTrack(caller_track);
512 // Add receive video, from callee to caller.
513 rtc::scoped_refptr<webrtc::VideoTrackInterface> callee_track =
514 callee()->CreateLocalVideoTrack();
515
516 rtc::scoped_refptr<webrtc::RtpSenderInterface> callee_sender =
517 callee()->AddTrack(callee_track);
518 caller()->CreateAndSetAndSignalOffer();
519 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
520
521 // Remove send video (i.e., caller sender track).
Harald Alvestrand93dd7632022-01-19 12:28:45 +0000522 caller()->pc()->RemoveTrackOrError(caller_sender);
Johannes Kron3e983682020-03-29 22:17:00 +0200523
524 caller()->CreateAndSetAndSignalOffer();
525 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
526
527 // Expect one-directional video.
528 MediaExpectations media_expectations;
529 media_expectations.CalleeExpectsNoVideo();
530 media_expectations.CallerExpectsSomeVideo();
531
532 ASSERT_TRUE(ExpectNewFrames(media_expectations));
533}
534
deadbeef1dcb1642017-03-29 21:08:16 -0700535// This test sets up a audio call initially, with the callee rejecting video
536// initially. Then later the callee decides to upgrade to audio/video, and
537// initiates a new offer/answer exchange.
Seth Hampson2f0d7022018-02-20 11:54:42 -0800538TEST_P(PeerConnectionIntegrationTest, AudioToVideoUpgrade) {
deadbeef1dcb1642017-03-29 21:08:16 -0700539 ASSERT_TRUE(CreatePeerConnectionWrappers());
540 ConnectFakeSignaling();
541 // Initially, offer an audio/video stream from the caller, but refuse to
542 // send/receive video on the callee side.
Steve Anton15324772018-01-16 10:26:49 -0800543 caller()->AddAudioVideoTracks();
544 callee()->AddAudioTrack();
Florent Castelli15a38de2022-04-06 00:38:21 +0200545 if (sdp_semantics_ == SdpSemantics::kPlanB_DEPRECATED) {
Seth Hampson2f0d7022018-02-20 11:54:42 -0800546 PeerConnectionInterface::RTCOfferAnswerOptions options;
547 options.offer_to_receive_video = 0;
548 callee()->SetOfferAnswerOptions(options);
549 } else {
550 callee()->SetRemoteOfferHandler([this] {
Harald Alvestrand6060df52020-08-11 09:54:02 +0200551 callee()
552 ->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)
553 ->StopInternal();
Seth Hampson2f0d7022018-02-20 11:54:42 -0800554 });
555 }
deadbeef1dcb1642017-03-29 21:08:16 -0700556 // Do offer/answer and make sure audio is still received end-to-end.
557 caller()->CreateAndSetAndSignalOffer();
558 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -0800559 {
560 MediaExpectations media_expectations;
561 media_expectations.ExpectBidirectionalAudio();
562 media_expectations.ExpectNoVideo();
563 ASSERT_TRUE(ExpectNewFrames(media_expectations));
564 }
deadbeef1dcb1642017-03-29 21:08:16 -0700565 // Sanity check that the callee's description has a rejected video section.
566 ASSERT_NE(nullptr, callee()->pc()->local_description());
567 const ContentInfo* callee_video_content =
568 GetFirstVideoContent(callee()->pc()->local_description()->description());
569 ASSERT_NE(nullptr, callee_video_content);
570 EXPECT_TRUE(callee_video_content->rejected);
Seth Hampson2f0d7022018-02-20 11:54:42 -0800571
deadbeef1dcb1642017-03-29 21:08:16 -0700572 // Now negotiate with video and ensure negotiation succeeds, with video
573 // frames and additional audio frames being received.
Steve Anton15324772018-01-16 10:26:49 -0800574 callee()->AddVideoTrack();
Florent Castelli15a38de2022-04-06 00:38:21 +0200575 if (sdp_semantics_ == SdpSemantics::kPlanB_DEPRECATED) {
Seth Hampson2f0d7022018-02-20 11:54:42 -0800576 PeerConnectionInterface::RTCOfferAnswerOptions options;
577 options.offer_to_receive_video = 1;
578 callee()->SetOfferAnswerOptions(options);
579 } else {
580 callee()->SetRemoteOfferHandler(nullptr);
581 caller()->SetRemoteOfferHandler([this] {
582 // The caller creates a new transceiver to receive video on when receiving
583 // the offer, but by default it is send only.
584 auto transceivers = caller()->pc()->GetTransceivers();
Harald Alvestrand6060df52020-08-11 09:54:02 +0200585 ASSERT_EQ(2U, transceivers.size());
Seth Hampson2f0d7022018-02-20 11:54:42 -0800586 ASSERT_EQ(cricket::MEDIA_TYPE_VIDEO,
Harald Alvestrand6060df52020-08-11 09:54:02 +0200587 transceivers[1]->receiver()->media_type());
Niels Möllerafb246b2022-04-20 14:26:50 +0200588 transceivers[1]->sender()->SetTrack(
589 caller()->CreateLocalVideoTrack().get());
Harald Alvestrand6060df52020-08-11 09:54:02 +0200590 transceivers[1]->SetDirectionWithError(
591 RtpTransceiverDirection::kSendRecv);
Seth Hampson2f0d7022018-02-20 11:54:42 -0800592 });
593 }
deadbeef1dcb1642017-03-29 21:08:16 -0700594 callee()->CreateAndSetAndSignalOffer();
595 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -0800596 {
597 // Expect additional audio frames to be received after the upgrade.
598 MediaExpectations media_expectations;
599 media_expectations.ExpectBidirectionalAudioAndVideo();
600 ASSERT_TRUE(ExpectNewFrames(media_expectations));
601 }
deadbeef1dcb1642017-03-29 21:08:16 -0700602}
603
deadbeef4389b4d2017-09-07 09:07:36 -0700604// Simpler than the above test; just add an audio track to an established
605// video-only connection.
Seth Hampson2f0d7022018-02-20 11:54:42 -0800606TEST_P(PeerConnectionIntegrationTest, AddAudioToVideoOnlyCall) {
deadbeef4389b4d2017-09-07 09:07:36 -0700607 ASSERT_TRUE(CreatePeerConnectionWrappers());
608 ConnectFakeSignaling();
609 // Do initial offer/answer with just a video track.
Steve Anton15324772018-01-16 10:26:49 -0800610 caller()->AddVideoTrack();
611 callee()->AddVideoTrack();
deadbeef4389b4d2017-09-07 09:07:36 -0700612 caller()->CreateAndSetAndSignalOffer();
613 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
614 // Now add an audio track and do another offer/answer.
Steve Anton15324772018-01-16 10:26:49 -0800615 caller()->AddAudioTrack();
616 callee()->AddAudioTrack();
deadbeef4389b4d2017-09-07 09:07:36 -0700617 caller()->CreateAndSetAndSignalOffer();
618 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
619 // Ensure both audio and video frames are received end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -0800620 MediaExpectations media_expectations;
621 media_expectations.ExpectBidirectionalAudioAndVideo();
622 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef4389b4d2017-09-07 09:07:36 -0700623}
624
deadbeef1dcb1642017-03-29 21:08:16 -0700625// This test sets up a non-bundled call and negotiates bundling at the same
626// time as starting an ICE restart. When bundling is in effect in the restart,
627// the DTLS-SRTP context should be successfully reset.
Seth Hampson2f0d7022018-02-20 11:54:42 -0800628TEST_P(PeerConnectionIntegrationTest, BundlingEnabledWhileIceRestartOccurs) {
deadbeef1dcb1642017-03-29 21:08:16 -0700629 ASSERT_TRUE(CreatePeerConnectionWrappers());
630 ConnectFakeSignaling();
631
Steve Anton15324772018-01-16 10:26:49 -0800632 caller()->AddAudioVideoTracks();
633 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -0700634 // Remove the bundle group from the SDP received by the callee.
635 callee()->SetReceivedSdpMunger([](cricket::SessionDescription* desc) {
636 desc->RemoveGroupByName("BUNDLE");
637 });
638 caller()->CreateAndSetAndSignalOffer();
639 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -0800640 {
641 MediaExpectations media_expectations;
642 media_expectations.ExpectBidirectionalAudioAndVideo();
643 ASSERT_TRUE(ExpectNewFrames(media_expectations));
644 }
deadbeef1dcb1642017-03-29 21:08:16 -0700645 // Now stop removing the BUNDLE group, and trigger an ICE restart.
646 callee()->SetReceivedSdpMunger(nullptr);
647 caller()->SetOfferAnswerOptions(IceRestartOfferAnswerOptions());
648 caller()->CreateAndSetAndSignalOffer();
649 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
650
651 // Expect additional frames to be received after the ICE restart.
Seth Hampson2f0d7022018-02-20 11:54:42 -0800652 {
653 MediaExpectations media_expectations;
654 media_expectations.ExpectBidirectionalAudioAndVideo();
655 ASSERT_TRUE(ExpectNewFrames(media_expectations));
656 }
deadbeef1dcb1642017-03-29 21:08:16 -0700657}
658
659// Test CVO (Coordination of Video Orientation). If a video source is rotated
660// and both peers support the CVO RTP header extension, the actual video frames
661// don't need to be encoded in different resolutions, since the rotation is
662// communicated through the RTP header extension.
Seth Hampson2f0d7022018-02-20 11:54:42 -0800663TEST_P(PeerConnectionIntegrationTest, RotatedVideoWithCVOExtension) {
deadbeef1dcb1642017-03-29 21:08:16 -0700664 ASSERT_TRUE(CreatePeerConnectionWrappers());
665 ConnectFakeSignaling();
666 // Add rotated video tracks.
Steve Anton15324772018-01-16 10:26:49 -0800667 caller()->AddTrack(
deadbeef1dcb1642017-03-29 21:08:16 -0700668 caller()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_90));
Steve Anton15324772018-01-16 10:26:49 -0800669 callee()->AddTrack(
deadbeef1dcb1642017-03-29 21:08:16 -0700670 callee()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_270));
671
672 // Wait for video frames to be received by both sides.
673 caller()->CreateAndSetAndSignalOffer();
674 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
675 ASSERT_TRUE_WAIT(caller()->min_video_frames_received_per_track() > 0 &&
676 callee()->min_video_frames_received_per_track() > 0,
677 kMaxWaitForFramesMs);
678
679 // Ensure that the aspect ratio is unmodified.
680 // TODO(deadbeef): Where does 4:3 come from? Should be explicit in the test,
681 // not just assumed.
682 EXPECT_EQ(4.0 / 3, caller()->local_rendered_aspect_ratio());
683 EXPECT_EQ(4.0 / 3, caller()->rendered_aspect_ratio());
684 EXPECT_EQ(4.0 / 3, callee()->local_rendered_aspect_ratio());
685 EXPECT_EQ(4.0 / 3, callee()->rendered_aspect_ratio());
686 // Ensure that the CVO bits were surfaced to the renderer.
687 EXPECT_EQ(webrtc::kVideoRotation_270, caller()->rendered_rotation());
688 EXPECT_EQ(webrtc::kVideoRotation_90, callee()->rendered_rotation());
689}
690
691// Test that when the CVO extension isn't supported, video is rotated the
692// old-fashioned way, by encoding rotated frames.
Seth Hampson2f0d7022018-02-20 11:54:42 -0800693TEST_P(PeerConnectionIntegrationTest, RotatedVideoWithoutCVOExtension) {
deadbeef1dcb1642017-03-29 21:08:16 -0700694 ASSERT_TRUE(CreatePeerConnectionWrappers());
695 ConnectFakeSignaling();
696 // Add rotated video tracks.
Steve Anton15324772018-01-16 10:26:49 -0800697 caller()->AddTrack(
deadbeef1dcb1642017-03-29 21:08:16 -0700698 caller()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_90));
Steve Anton15324772018-01-16 10:26:49 -0800699 callee()->AddTrack(
deadbeef1dcb1642017-03-29 21:08:16 -0700700 callee()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_270));
701
702 // Remove the CVO extension from the offered SDP.
703 callee()->SetReceivedSdpMunger([](cricket::SessionDescription* desc) {
704 cricket::VideoContentDescription* video =
705 GetFirstVideoContentDescription(desc);
706 video->ClearRtpHeaderExtensions();
707 });
708 // Wait for video frames to be received by both sides.
709 caller()->CreateAndSetAndSignalOffer();
710 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
711 ASSERT_TRUE_WAIT(caller()->min_video_frames_received_per_track() > 0 &&
712 callee()->min_video_frames_received_per_track() > 0,
713 kMaxWaitForFramesMs);
714
715 // Expect that the aspect ratio is inversed to account for the 90/270 degree
716 // rotation.
717 // TODO(deadbeef): Where does 4:3 come from? Should be explicit in the test,
718 // not just assumed.
719 EXPECT_EQ(3.0 / 4, caller()->local_rendered_aspect_ratio());
720 EXPECT_EQ(3.0 / 4, caller()->rendered_aspect_ratio());
721 EXPECT_EQ(3.0 / 4, callee()->local_rendered_aspect_ratio());
722 EXPECT_EQ(3.0 / 4, callee()->rendered_aspect_ratio());
723 // Expect that each endpoint is unaware of the rotation of the other endpoint.
724 EXPECT_EQ(webrtc::kVideoRotation_0, caller()->rendered_rotation());
725 EXPECT_EQ(webrtc::kVideoRotation_0, callee()->rendered_rotation());
726}
727
deadbeef1dcb1642017-03-29 21:08:16 -0700728// Test that if the answerer rejects the audio m= section, no audio is sent or
729// received, but video still can be.
Seth Hampson2f0d7022018-02-20 11:54:42 -0800730TEST_P(PeerConnectionIntegrationTest, AnswererRejectsAudioSection) {
deadbeef1dcb1642017-03-29 21:08:16 -0700731 ASSERT_TRUE(CreatePeerConnectionWrappers());
732 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -0800733 caller()->AddAudioVideoTracks();
Florent Castelli15a38de2022-04-06 00:38:21 +0200734 if (sdp_semantics_ == SdpSemantics::kPlanB_DEPRECATED) {
Seth Hampson2f0d7022018-02-20 11:54:42 -0800735 // Only add video track for callee, and set offer_to_receive_audio to 0, so
736 // it will reject the audio m= section completely.
737 PeerConnectionInterface::RTCOfferAnswerOptions options;
738 options.offer_to_receive_audio = 0;
739 callee()->SetOfferAnswerOptions(options);
740 } else {
741 // Stopping the audio RtpTransceiver will cause the media section to be
742 // rejected in the answer.
743 callee()->SetRemoteOfferHandler([this] {
Harald Alvestrand6060df52020-08-11 09:54:02 +0200744 callee()
745 ->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_AUDIO)
746 ->StopInternal();
Seth Hampson2f0d7022018-02-20 11:54:42 -0800747 });
748 }
Steve Anton15324772018-01-16 10:26:49 -0800749 callee()->AddTrack(callee()->CreateLocalVideoTrack());
deadbeef1dcb1642017-03-29 21:08:16 -0700750 // Do offer/answer and wait for successful end-to-end video frames.
751 caller()->CreateAndSetAndSignalOffer();
752 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -0800753 MediaExpectations media_expectations;
754 media_expectations.ExpectBidirectionalVideo();
755 media_expectations.ExpectNoAudio();
756 ASSERT_TRUE(ExpectNewFrames(media_expectations));
757
deadbeef1dcb1642017-03-29 21:08:16 -0700758 // Sanity check that the callee's description has a rejected audio section.
759 ASSERT_NE(nullptr, callee()->pc()->local_description());
760 const ContentInfo* callee_audio_content =
761 GetFirstAudioContent(callee()->pc()->local_description()->description());
762 ASSERT_NE(nullptr, callee_audio_content);
763 EXPECT_TRUE(callee_audio_content->rejected);
Seth Hampson2f0d7022018-02-20 11:54:42 -0800764 if (sdp_semantics_ == SdpSemantics::kUnifiedPlan) {
Harald Alvestrand6060df52020-08-11 09:54:02 +0200765 // The caller's transceiver should have stopped after receiving the answer,
766 // and thus no longer listed in transceivers.
767 EXPECT_EQ(nullptr,
768 caller()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_AUDIO));
Seth Hampson2f0d7022018-02-20 11:54:42 -0800769 }
deadbeef1dcb1642017-03-29 21:08:16 -0700770}
771
772// Test that if the answerer rejects the video m= section, no video is sent or
773// received, but audio still can be.
Seth Hampson2f0d7022018-02-20 11:54:42 -0800774TEST_P(PeerConnectionIntegrationTest, AnswererRejectsVideoSection) {
deadbeef1dcb1642017-03-29 21:08:16 -0700775 ASSERT_TRUE(CreatePeerConnectionWrappers());
776 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -0800777 caller()->AddAudioVideoTracks();
Florent Castelli15a38de2022-04-06 00:38:21 +0200778 if (sdp_semantics_ == SdpSemantics::kPlanB_DEPRECATED) {
Seth Hampson2f0d7022018-02-20 11:54:42 -0800779 // Only add audio track for callee, and set offer_to_receive_video to 0, so
780 // it will reject the video m= section completely.
781 PeerConnectionInterface::RTCOfferAnswerOptions options;
782 options.offer_to_receive_video = 0;
783 callee()->SetOfferAnswerOptions(options);
784 } else {
785 // Stopping the video RtpTransceiver will cause the media section to be
786 // rejected in the answer.
787 callee()->SetRemoteOfferHandler([this] {
Harald Alvestrand6060df52020-08-11 09:54:02 +0200788 callee()
789 ->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)
790 ->StopInternal();
Seth Hampson2f0d7022018-02-20 11:54:42 -0800791 });
792 }
Steve Anton15324772018-01-16 10:26:49 -0800793 callee()->AddTrack(callee()->CreateLocalAudioTrack());
deadbeef1dcb1642017-03-29 21:08:16 -0700794 // Do offer/answer and wait for successful end-to-end audio frames.
795 caller()->CreateAndSetAndSignalOffer();
796 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -0800797 MediaExpectations media_expectations;
798 media_expectations.ExpectBidirectionalAudio();
799 media_expectations.ExpectNoVideo();
800 ASSERT_TRUE(ExpectNewFrames(media_expectations));
801
deadbeef1dcb1642017-03-29 21:08:16 -0700802 // Sanity check that the callee's description has a rejected video section.
803 ASSERT_NE(nullptr, callee()->pc()->local_description());
804 const ContentInfo* callee_video_content =
805 GetFirstVideoContent(callee()->pc()->local_description()->description());
806 ASSERT_NE(nullptr, callee_video_content);
807 EXPECT_TRUE(callee_video_content->rejected);
Seth Hampson2f0d7022018-02-20 11:54:42 -0800808 if (sdp_semantics_ == SdpSemantics::kUnifiedPlan) {
Harald Alvestrand6060df52020-08-11 09:54:02 +0200809 // The caller's transceiver should have stopped after receiving the answer,
810 // and thus is no longer present.
811 EXPECT_EQ(nullptr,
812 caller()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO));
Seth Hampson2f0d7022018-02-20 11:54:42 -0800813 }
deadbeef1dcb1642017-03-29 21:08:16 -0700814}
815
816// Test that if the answerer rejects both audio and video m= sections, nothing
817// bad happens.
818// TODO(deadbeef): Test that a data channel still works. Currently this doesn't
819// test anything but the fact that negotiation succeeds, which doesn't mean
820// much.
Seth Hampson2f0d7022018-02-20 11:54:42 -0800821TEST_P(PeerConnectionIntegrationTest, AnswererRejectsAudioAndVideoSections) {
deadbeef1dcb1642017-03-29 21:08:16 -0700822 ASSERT_TRUE(CreatePeerConnectionWrappers());
823 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -0800824 caller()->AddAudioVideoTracks();
Florent Castelli15a38de2022-04-06 00:38:21 +0200825 if (sdp_semantics_ == SdpSemantics::kPlanB_DEPRECATED) {
Seth Hampson2f0d7022018-02-20 11:54:42 -0800826 // Don't give the callee any tracks, and set offer_to_receive_X to 0, so it
827 // will reject both audio and video m= sections.
828 PeerConnectionInterface::RTCOfferAnswerOptions options;
829 options.offer_to_receive_audio = 0;
830 options.offer_to_receive_video = 0;
831 callee()->SetOfferAnswerOptions(options);
832 } else {
833 callee()->SetRemoteOfferHandler([this] {
834 // Stopping all transceivers will cause all media sections to be rejected.
Mirko Bonadei739baf02019-01-27 17:29:42 +0100835 for (const auto& transceiver : callee()->pc()->GetTransceivers()) {
Harald Alvestrand6060df52020-08-11 09:54:02 +0200836 transceiver->StopInternal();
Seth Hampson2f0d7022018-02-20 11:54:42 -0800837 }
838 });
839 }
deadbeef1dcb1642017-03-29 21:08:16 -0700840 // Do offer/answer and wait for stable signaling state.
841 caller()->CreateAndSetAndSignalOffer();
842 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -0800843
deadbeef1dcb1642017-03-29 21:08:16 -0700844 // Sanity check that the callee's description has rejected m= sections.
845 ASSERT_NE(nullptr, callee()->pc()->local_description());
846 const ContentInfo* callee_audio_content =
847 GetFirstAudioContent(callee()->pc()->local_description()->description());
848 ASSERT_NE(nullptr, callee_audio_content);
849 EXPECT_TRUE(callee_audio_content->rejected);
850 const ContentInfo* callee_video_content =
851 GetFirstVideoContent(callee()->pc()->local_description()->description());
852 ASSERT_NE(nullptr, callee_video_content);
853 EXPECT_TRUE(callee_video_content->rejected);
854}
855
856// This test sets up an audio and video call between two parties. After the
857// call runs for a while, the caller sends an updated offer with video being
858// rejected. Once the re-negotiation is done, the video flow should stop and
859// the audio flow should continue.
Seth Hampson2f0d7022018-02-20 11:54:42 -0800860TEST_P(PeerConnectionIntegrationTest, VideoRejectedInSubsequentOffer) {
deadbeef1dcb1642017-03-29 21:08:16 -0700861 ASSERT_TRUE(CreatePeerConnectionWrappers());
862 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -0800863 caller()->AddAudioVideoTracks();
864 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -0700865 caller()->CreateAndSetAndSignalOffer();
866 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -0800867 {
868 MediaExpectations media_expectations;
869 media_expectations.ExpectBidirectionalAudioAndVideo();
870 ASSERT_TRUE(ExpectNewFrames(media_expectations));
871 }
deadbeef1dcb1642017-03-29 21:08:16 -0700872 // Renegotiate, rejecting the video m= section.
Florent Castelli15a38de2022-04-06 00:38:21 +0200873 if (sdp_semantics_ == SdpSemantics::kPlanB_DEPRECATED) {
Seth Hampson2f0d7022018-02-20 11:54:42 -0800874 caller()->SetGeneratedSdpMunger(
875 [](cricket::SessionDescription* description) {
876 for (cricket::ContentInfo& content : description->contents()) {
877 if (cricket::IsVideoContent(&content)) {
878 content.rejected = true;
879 }
880 }
881 });
882 } else {
Harald Alvestrand6060df52020-08-11 09:54:02 +0200883 caller()
884 ->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)
885 ->StopInternal();
Seth Hampson2f0d7022018-02-20 11:54:42 -0800886 }
deadbeef1dcb1642017-03-29 21:08:16 -0700887 caller()->CreateAndSetAndSignalOffer();
888 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
889
890 // Sanity check that the caller's description has a rejected video section.
891 ASSERT_NE(nullptr, caller()->pc()->local_description());
892 const ContentInfo* caller_video_content =
893 GetFirstVideoContent(caller()->pc()->local_description()->description());
894 ASSERT_NE(nullptr, caller_video_content);
895 EXPECT_TRUE(caller_video_content->rejected);
deadbeef1dcb1642017-03-29 21:08:16 -0700896 // Wait for some additional audio frames to be received.
Seth Hampson2f0d7022018-02-20 11:54:42 -0800897 {
898 MediaExpectations media_expectations;
899 media_expectations.ExpectBidirectionalAudio();
900 media_expectations.ExpectNoVideo();
901 ASSERT_TRUE(ExpectNewFrames(media_expectations));
902 }
deadbeef1dcb1642017-03-29 21:08:16 -0700903}
904
Taylor Brandstetter60c8dc82018-04-11 15:20:27 -0700905// Do one offer/answer with audio, another that disables it (rejecting the m=
906// section), and another that re-enables it. Regression test for:
907// bugs.webrtc.org/6023
908TEST_F(PeerConnectionIntegrationTestPlanB, EnableAudioAfterRejecting) {
909 ASSERT_TRUE(CreatePeerConnectionWrappers());
910 ConnectFakeSignaling();
911
912 // Add audio track, do normal offer/answer.
913 rtc::scoped_refptr<webrtc::AudioTrackInterface> track =
914 caller()->CreateLocalAudioTrack();
915 rtc::scoped_refptr<webrtc::RtpSenderInterface> sender =
916 caller()->pc()->AddTrack(track, {"stream"}).MoveValue();
917 caller()->CreateAndSetAndSignalOffer();
918 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
919
920 // Remove audio track, and set offer_to_receive_audio to false to cause the
921 // m= section to be completely disabled, not just "recvonly".
Harald Alvestrand93dd7632022-01-19 12:28:45 +0000922 caller()->pc()->RemoveTrackOrError(sender);
Taylor Brandstetter60c8dc82018-04-11 15:20:27 -0700923 PeerConnectionInterface::RTCOfferAnswerOptions options;
924 options.offer_to_receive_audio = 0;
925 caller()->SetOfferAnswerOptions(options);
926 caller()->CreateAndSetAndSignalOffer();
927 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
928
929 // Add the audio track again, expecting negotiation to succeed and frames to
930 // flow.
931 sender = caller()->pc()->AddTrack(track, {"stream"}).MoveValue();
932 options.offer_to_receive_audio = 1;
933 caller()->SetOfferAnswerOptions(options);
934 caller()->CreateAndSetAndSignalOffer();
935 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
936
937 MediaExpectations media_expectations;
938 media_expectations.CalleeExpectsSomeAudio();
939 EXPECT_TRUE(ExpectNewFrames(media_expectations));
940}
941
deadbeef1dcb1642017-03-29 21:08:16 -0700942// Basic end-to-end test, but without SSRC/MSID signaling. This functionality
943// is needed to support legacy endpoints.
944// TODO(deadbeef): When we support the MID extension and demuxing on MID, also
945// add a test for an end-to-end test without MID signaling either (basically,
946// the minimum acceptable SDP).
Seth Hampson2f0d7022018-02-20 11:54:42 -0800947TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithoutSsrcOrMsidSignaling) {
deadbeef1dcb1642017-03-29 21:08:16 -0700948 ASSERT_TRUE(CreatePeerConnectionWrappers());
949 ConnectFakeSignaling();
950 // Add audio and video, testing that packets can be demuxed on payload type.
Steve Anton15324772018-01-16 10:26:49 -0800951 caller()->AddAudioVideoTracks();
952 callee()->AddAudioVideoTracks();
deadbeefd8ad7882017-04-18 16:01:17 -0700953 // Remove SSRCs and MSIDs from the received offer SDP.
954 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
deadbeef1dcb1642017-03-29 21:08:16 -0700955 caller()->CreateAndSetAndSignalOffer();
956 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -0800957 MediaExpectations media_expectations;
958 media_expectations.ExpectBidirectionalAudioAndVideo();
959 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -0700960}
961
Seth Hampson5897a6e2018-04-03 11:16:33 -0700962// Basic end-to-end test, without SSRC signaling. This means that the track
963// was created properly and frames are delivered when the MSIDs are communicated
964// with a=msid lines and no a=ssrc lines.
965TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
966 EndToEndCallWithoutSsrcSignaling) {
967 const char kStreamId[] = "streamId";
968 ASSERT_TRUE(CreatePeerConnectionWrappers());
969 ConnectFakeSignaling();
970 // Add just audio tracks.
971 caller()->AddTrack(caller()->CreateLocalAudioTrack(), {kStreamId});
972 callee()->AddAudioTrack();
973
974 // Remove SSRCs from the received offer SDP.
975 callee()->SetReceivedSdpMunger(RemoveSsrcsAndKeepMsids);
976 caller()->CreateAndSetAndSignalOffer();
977 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
978 MediaExpectations media_expectations;
979 media_expectations.ExpectBidirectionalAudio();
980 ASSERT_TRUE(ExpectNewFrames(media_expectations));
981}
982
Johannes Kron3e983682020-03-29 22:17:00 +0200983TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
984 EndToEndCallAddReceiveVideoToSendOnlyCall) {
985 ASSERT_TRUE(CreatePeerConnectionWrappers());
986 ConnectFakeSignaling();
987 // Add one-directional video, from caller to callee.
988 rtc::scoped_refptr<webrtc::VideoTrackInterface> track =
989 caller()->CreateLocalVideoTrack();
990
991 RtpTransceiverInit video_transceiver_init;
992 video_transceiver_init.stream_ids = {"video1"};
993 video_transceiver_init.direction = RtpTransceiverDirection::kSendOnly;
994 auto video_sender =
995 caller()->pc()->AddTransceiver(track, video_transceiver_init).MoveValue();
996 caller()->CreateAndSetAndSignalOffer();
997 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
998
999 // Add receive direction.
Harald Alvestrand6060df52020-08-11 09:54:02 +02001000 video_sender->SetDirectionWithError(RtpTransceiverDirection::kSendRecv);
Johannes Kron3e983682020-03-29 22:17:00 +02001001
1002 rtc::scoped_refptr<webrtc::VideoTrackInterface> callee_track =
1003 callee()->CreateLocalVideoTrack();
1004
1005 callee()->AddTrack(callee_track);
1006 caller()->CreateAndSetAndSignalOffer();
1007 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1008 // Ensure that video frames are received end-to-end.
1009 MediaExpectations media_expectations;
1010 media_expectations.ExpectBidirectionalVideo();
1011 ASSERT_TRUE(ExpectNewFrames(media_expectations));
1012}
1013
Steve Antondf527fd2018-04-27 15:52:03 -07001014// Tests that video flows between multiple video tracks when SSRCs are not
1015// signaled. This exercises the MID RTP header extension which is needed to
1016// demux the incoming video tracks.
1017TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
1018 EndToEndCallWithTwoVideoTracksAndNoSignaledSsrc) {
1019 ASSERT_TRUE(CreatePeerConnectionWrappers());
1020 ConnectFakeSignaling();
1021 caller()->AddVideoTrack();
1022 caller()->AddVideoTrack();
1023 callee()->AddVideoTrack();
1024 callee()->AddVideoTrack();
1025
1026 caller()->SetReceivedSdpMunger(&RemoveSsrcsAndKeepMsids);
1027 callee()->SetReceivedSdpMunger(&RemoveSsrcsAndKeepMsids);
1028 caller()->CreateAndSetAndSignalOffer();
1029 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1030 ASSERT_EQ(2u, caller()->pc()->GetReceivers().size());
1031 ASSERT_EQ(2u, callee()->pc()->GetReceivers().size());
1032
1033 // Expect video to be received in both directions on both tracks.
1034 MediaExpectations media_expectations;
1035 media_expectations.ExpectBidirectionalVideo();
1036 EXPECT_TRUE(ExpectNewFrames(media_expectations));
1037}
1038
Taylor Brandstetterd3ef4992020-10-15 18:22:57 -07001039// Used for the test below.
1040void RemoveBundleGroupSsrcsAndMidExtension(cricket::SessionDescription* desc) {
1041 RemoveSsrcsAndKeepMsids(desc);
1042 desc->RemoveGroupByName("BUNDLE");
1043 for (ContentInfo& content : desc->contents()) {
1044 cricket::MediaContentDescription* media = content.media_description();
1045 cricket::RtpHeaderExtensions extensions = media->rtp_header_extensions();
1046 extensions.erase(std::remove_if(extensions.begin(), extensions.end(),
1047 [](const RtpExtension& extension) {
1048 return extension.uri ==
1049 RtpExtension::kMidUri;
1050 }),
1051 extensions.end());
1052 media->set_rtp_header_extensions(extensions);
1053 }
1054}
1055
1056// Tests that video flows between multiple video tracks when BUNDLE is not used,
1057// SSRCs are not signaled and the MID RTP header extension is not used. This
1058// relies on demuxing by payload type, which normally doesn't work if you have
1059// multiple media sections using the same payload type, but which should work as
1060// long as the media sections aren't bundled.
1061// Regression test for: http://crbug.com/webrtc/12023
1062TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
1063 EndToEndCallWithTwoVideoTracksNoBundleNoSignaledSsrcAndNoMid) {
1064 ASSERT_TRUE(CreatePeerConnectionWrappers());
1065 ConnectFakeSignaling();
1066 caller()->AddVideoTrack();
1067 caller()->AddVideoTrack();
1068 callee()->AddVideoTrack();
1069 callee()->AddVideoTrack();
1070 caller()->SetReceivedSdpMunger(&RemoveBundleGroupSsrcsAndMidExtension);
1071 callee()->SetReceivedSdpMunger(&RemoveBundleGroupSsrcsAndMidExtension);
1072 caller()->CreateAndSetAndSignalOffer();
1073 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1074 ASSERT_EQ(2u, caller()->pc()->GetReceivers().size());
1075 ASSERT_EQ(2u, callee()->pc()->GetReceivers().size());
1076 // Make sure we are not bundled.
1077 ASSERT_NE(caller()->pc()->GetSenders()[0]->dtls_transport(),
1078 caller()->pc()->GetSenders()[1]->dtls_transport());
1079
1080 // Expect video to be received in both directions on both tracks.
1081 MediaExpectations media_expectations;
1082 media_expectations.ExpectBidirectionalVideo();
1083 EXPECT_TRUE(ExpectNewFrames(media_expectations));
1084}
1085
1086// Used for the test below.
1087void ModifyPayloadTypesAndRemoveMidExtension(
1088 cricket::SessionDescription* desc) {
1089 int pt = 96;
1090 for (ContentInfo& content : desc->contents()) {
1091 cricket::MediaContentDescription* media = content.media_description();
1092 cricket::RtpHeaderExtensions extensions = media->rtp_header_extensions();
1093 extensions.erase(std::remove_if(extensions.begin(), extensions.end(),
1094 [](const RtpExtension& extension) {
1095 return extension.uri ==
1096 RtpExtension::kMidUri;
1097 }),
1098 extensions.end());
1099 media->set_rtp_header_extensions(extensions);
1100 cricket::VideoContentDescription* video = media->as_video();
1101 ASSERT_TRUE(video != nullptr);
1102 std::vector<cricket::VideoCodec> codecs = {{pt++, "VP8"}};
1103 video->set_codecs(codecs);
1104 }
1105}
1106
1107// Tests that two video tracks can be demultiplexed by payload type alone, by
1108// using different payload types for the same codec in different m= sections.
1109// This practice is discouraged but historically has been supported.
1110// Regression test for: http://crbug.com/webrtc/12029
1111TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
1112 EndToEndCallWithTwoVideoTracksDemultiplexedByPayloadType) {
1113 ASSERT_TRUE(CreatePeerConnectionWrappers());
1114 ConnectFakeSignaling();
1115 caller()->AddVideoTrack();
1116 caller()->AddVideoTrack();
1117 callee()->AddVideoTrack();
1118 callee()->AddVideoTrack();
1119 caller()->SetGeneratedSdpMunger(&ModifyPayloadTypesAndRemoveMidExtension);
1120 callee()->SetGeneratedSdpMunger(&ModifyPayloadTypesAndRemoveMidExtension);
1121 // We can't remove SSRCs from the generated SDP because then no send streams
1122 // would be created.
1123 caller()->SetReceivedSdpMunger(&RemoveSsrcsAndKeepMsids);
1124 callee()->SetReceivedSdpMunger(&RemoveSsrcsAndKeepMsids);
1125 caller()->CreateAndSetAndSignalOffer();
1126 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1127 ASSERT_EQ(2u, caller()->pc()->GetReceivers().size());
1128 ASSERT_EQ(2u, callee()->pc()->GetReceivers().size());
1129 // Make sure we are bundled.
1130 ASSERT_EQ(caller()->pc()->GetSenders()[0]->dtls_transport(),
1131 caller()->pc()->GetSenders()[1]->dtls_transport());
1132
1133 // Expect video to be received in both directions on both tracks.
1134 MediaExpectations media_expectations;
1135 media_expectations.ExpectBidirectionalVideo();
1136 EXPECT_TRUE(ExpectNewFrames(media_expectations));
1137}
1138
Henrik Boström5b147782018-12-04 11:25:05 +01001139TEST_F(PeerConnectionIntegrationTestUnifiedPlan, NoStreamsMsidLinePresent) {
1140 ASSERT_TRUE(CreatePeerConnectionWrappers());
1141 ConnectFakeSignaling();
1142 caller()->AddAudioTrack();
1143 caller()->AddVideoTrack();
1144 caller()->CreateAndSetAndSignalOffer();
1145 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1146 auto callee_receivers = callee()->pc()->GetReceivers();
1147 ASSERT_EQ(2u, callee_receivers.size());
1148 EXPECT_TRUE(callee_receivers[0]->stream_ids().empty());
1149 EXPECT_TRUE(callee_receivers[1]->stream_ids().empty());
1150}
1151
1152TEST_F(PeerConnectionIntegrationTestUnifiedPlan, NoStreamsMsidLineMissing) {
1153 ASSERT_TRUE(CreatePeerConnectionWrappers());
1154 ConnectFakeSignaling();
1155 caller()->AddAudioTrack();
1156 caller()->AddVideoTrack();
1157 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
1158 caller()->CreateAndSetAndSignalOffer();
1159 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1160 auto callee_receivers = callee()->pc()->GetReceivers();
1161 ASSERT_EQ(2u, callee_receivers.size());
1162 ASSERT_EQ(1u, callee_receivers[0]->stream_ids().size());
1163 ASSERT_EQ(1u, callee_receivers[1]->stream_ids().size());
1164 EXPECT_EQ(callee_receivers[0]->stream_ids()[0],
1165 callee_receivers[1]->stream_ids()[0]);
1166 EXPECT_EQ(callee_receivers[0]->streams()[0],
1167 callee_receivers[1]->streams()[0]);
1168}
1169
deadbeef1dcb1642017-03-29 21:08:16 -07001170// Test that if two video tracks are sent (from caller to callee, in this test),
1171// they're transmitted correctly end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001172TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithTwoVideoTracks) {
deadbeef1dcb1642017-03-29 21:08:16 -07001173 ASSERT_TRUE(CreatePeerConnectionWrappers());
1174 ConnectFakeSignaling();
1175 // Add one audio/video stream, and one video-only stream.
Steve Anton15324772018-01-16 10:26:49 -08001176 caller()->AddAudioVideoTracks();
1177 caller()->AddVideoTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07001178 caller()->CreateAndSetAndSignalOffer();
1179 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Steve Anton15324772018-01-16 10:26:49 -08001180 ASSERT_EQ(3u, callee()->pc()->GetReceivers().size());
Seth Hampson2f0d7022018-02-20 11:54:42 -08001181
1182 MediaExpectations media_expectations;
1183 media_expectations.CalleeExpectsSomeAudioAndVideo();
1184 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07001185}
1186
1187static void MakeSpecCompliantMaxBundleOffer(cricket::SessionDescription* desc) {
1188 bool first = true;
1189 for (cricket::ContentInfo& content : desc->contents()) {
1190 if (first) {
1191 first = false;
1192 continue;
1193 }
1194 content.bundle_only = true;
1195 }
1196 first = true;
1197 for (cricket::TransportInfo& transport : desc->transport_infos()) {
1198 if (first) {
1199 first = false;
1200 continue;
1201 }
1202 transport.description.ice_ufrag.clear();
1203 transport.description.ice_pwd.clear();
1204 transport.description.connection_role = cricket::CONNECTIONROLE_NONE;
1205 transport.description.identity_fingerprint.reset(nullptr);
1206 }
1207}
1208
1209// Test that if applying a true "max bundle" offer, which uses ports of 0,
1210// "a=bundle-only", omitting "a=fingerprint", "a=setup", "a=ice-ufrag" and
1211// "a=ice-pwd" for all but the audio "m=" section, negotiation still completes
1212// successfully and media flows.
1213// TODO(deadbeef): Update this test to also omit "a=rtcp-mux", once that works.
1214// TODO(deadbeef): Won't need this test once we start generating actual
1215// standards-compliant SDP.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001216TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07001217 EndToEndCallWithSpecCompliantMaxBundleOffer) {
1218 ASSERT_TRUE(CreatePeerConnectionWrappers());
1219 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001220 caller()->AddAudioVideoTracks();
1221 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001222 // Do the equivalent of setting the port to 0, adding a=bundle-only, and
1223 // removing a=ice-ufrag, a=ice-pwd, a=fingerprint and a=setup from all
1224 // but the first m= section.
1225 callee()->SetReceivedSdpMunger(MakeSpecCompliantMaxBundleOffer);
1226 caller()->CreateAndSetAndSignalOffer();
1227 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001228 MediaExpectations media_expectations;
1229 media_expectations.ExpectBidirectionalAudioAndVideo();
1230 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07001231}
1232
1233// Test that we can receive the audio output level from a remote audio track.
1234// TODO(deadbeef): Use a fake audio source and verify that the output level is
1235// exactly what the source on the other side was configured with.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001236TEST_P(PeerConnectionIntegrationTest, GetAudioOutputLevelStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-29 21:08:16 -07001237 ASSERT_TRUE(CreatePeerConnectionWrappers());
1238 ConnectFakeSignaling();
1239 // Just add an audio track.
Steve Anton15324772018-01-16 10:26:49 -08001240 caller()->AddAudioTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07001241 caller()->CreateAndSetAndSignalOffer();
1242 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1243
1244 // Get the audio output level stats. Note that the level is not available
1245 // until an RTCP packet has been received.
deadbeefd8ad7882017-04-18 16:01:17 -07001246 EXPECT_TRUE_WAIT(callee()->OldGetStats()->AudioOutputLevel() > 0,
deadbeef1dcb1642017-03-29 21:08:16 -07001247 kMaxWaitForFramesMs);
1248}
1249
1250// Test that an audio input level is reported.
1251// TODO(deadbeef): Use a fake audio source and verify that the input level is
1252// exactly what the source was configured with.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001253TEST_P(PeerConnectionIntegrationTest, GetAudioInputLevelStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-29 21:08:16 -07001254 ASSERT_TRUE(CreatePeerConnectionWrappers());
1255 ConnectFakeSignaling();
1256 // Just add an audio track.
Steve Anton15324772018-01-16 10:26:49 -08001257 caller()->AddAudioTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07001258 caller()->CreateAndSetAndSignalOffer();
1259 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1260
1261 // Get the audio input level stats. The level should be available very
1262 // soon after the test starts.
deadbeefd8ad7882017-04-18 16:01:17 -07001263 EXPECT_TRUE_WAIT(caller()->OldGetStats()->AudioInputLevel() > 0,
deadbeef1dcb1642017-03-29 21:08:16 -07001264 kMaxWaitForStatsMs);
1265}
1266
1267// Test that we can get incoming byte counts from both audio and video tracks.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001268TEST_P(PeerConnectionIntegrationTest, GetBytesReceivedStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-29 21:08:16 -07001269 ASSERT_TRUE(CreatePeerConnectionWrappers());
1270 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001271 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001272 // Do offer/answer, wait for the callee to receive some frames.
1273 caller()->CreateAndSetAndSignalOffer();
1274 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001275
1276 MediaExpectations media_expectations;
1277 media_expectations.CalleeExpectsSomeAudioAndVideo();
1278 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07001279
1280 // Get a handle to the remote tracks created, so they can be used as GetStats
1281 // filters.
Mirko Bonadei739baf02019-01-27 17:29:42 +01001282 for (const auto& receiver : callee()->pc()->GetReceivers()) {
Steve Anton15324772018-01-16 10:26:49 -08001283 // We received frames, so we definitely should have nonzero "received bytes"
1284 // stats at this point.
Niels Möllerafb246b2022-04-20 14:26:50 +02001285 EXPECT_GT(
1286 callee()->OldGetStatsForTrack(receiver->track().get())->BytesReceived(),
1287 0);
Steve Anton15324772018-01-16 10:26:49 -08001288 }
deadbeef1dcb1642017-03-29 21:08:16 -07001289}
1290
1291// Test that we can get outgoing byte counts from both audio and video tracks.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001292TEST_P(PeerConnectionIntegrationTest, GetBytesSentStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-29 21:08:16 -07001293 ASSERT_TRUE(CreatePeerConnectionWrappers());
1294 ConnectFakeSignaling();
1295 auto audio_track = caller()->CreateLocalAudioTrack();
1296 auto video_track = caller()->CreateLocalVideoTrack();
Steve Anton15324772018-01-16 10:26:49 -08001297 caller()->AddTrack(audio_track);
1298 caller()->AddTrack(video_track);
deadbeef1dcb1642017-03-29 21:08:16 -07001299 // Do offer/answer, wait for the callee to receive some frames.
1300 caller()->CreateAndSetAndSignalOffer();
1301 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001302 MediaExpectations media_expectations;
1303 media_expectations.CalleeExpectsSomeAudioAndVideo();
1304 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07001305
1306 // The callee received frames, so we definitely should have nonzero "sent
1307 // bytes" stats at this point.
Niels Möllerafb246b2022-04-20 14:26:50 +02001308 EXPECT_GT(caller()->OldGetStatsForTrack(audio_track.get())->BytesSent(), 0);
1309 EXPECT_GT(caller()->OldGetStatsForTrack(video_track.get())->BytesSent(), 0);
deadbeefd8ad7882017-04-18 16:01:17 -07001310}
1311
Steve Antona41959e2018-11-28 11:15:33 -08001312// Test that the track ID is associated with all local and remote SSRC stats
1313// using the old GetStats() and more than 1 audio and more than 1 video track.
1314// This is a regression test for crbug.com/906988
1315TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
1316 OldGetStatsAssociatesTrackIdForManyMediaSections) {
1317 ASSERT_TRUE(CreatePeerConnectionWrappers());
1318 ConnectFakeSignaling();
1319 auto audio_sender_1 = caller()->AddAudioTrack();
1320 auto video_sender_1 = caller()->AddVideoTrack();
1321 auto audio_sender_2 = caller()->AddAudioTrack();
1322 auto video_sender_2 = caller()->AddVideoTrack();
1323 caller()->CreateAndSetAndSignalOffer();
1324 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1325
1326 MediaExpectations media_expectations;
1327 media_expectations.CalleeExpectsSomeAudioAndVideo();
1328 ASSERT_TRUE_WAIT(ExpectNewFrames(media_expectations), kDefaultTimeout);
1329
1330 std::vector<std::string> track_ids = {
1331 audio_sender_1->track()->id(), video_sender_1->track()->id(),
1332 audio_sender_2->track()->id(), video_sender_2->track()->id()};
1333
1334 auto caller_stats = caller()->OldGetStats();
1335 EXPECT_THAT(caller_stats->TrackIds(), UnorderedElementsAreArray(track_ids));
1336 auto callee_stats = callee()->OldGetStats();
1337 EXPECT_THAT(callee_stats->TrackIds(), UnorderedElementsAreArray(track_ids));
1338}
1339
Steve Antonffa6ce42018-11-30 09:26:08 -08001340// Test that the new GetStats() returns stats for all outgoing/incoming streams
1341// with the correct track IDs if there are more than one audio and more than one
1342// video senders/receivers.
1343TEST_P(PeerConnectionIntegrationTest, NewGetStatsManyAudioAndManyVideoStreams) {
1344 ASSERT_TRUE(CreatePeerConnectionWrappers());
1345 ConnectFakeSignaling();
1346 auto audio_sender_1 = caller()->AddAudioTrack();
1347 auto video_sender_1 = caller()->AddVideoTrack();
1348 auto audio_sender_2 = caller()->AddAudioTrack();
1349 auto video_sender_2 = caller()->AddVideoTrack();
1350 caller()->CreateAndSetAndSignalOffer();
1351 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1352
1353 MediaExpectations media_expectations;
1354 media_expectations.CalleeExpectsSomeAudioAndVideo();
1355 ASSERT_TRUE_WAIT(ExpectNewFrames(media_expectations), kDefaultTimeout);
1356
1357 std::vector<std::string> track_ids = {
1358 audio_sender_1->track()->id(), video_sender_1->track()->id(),
1359 audio_sender_2->track()->id(), video_sender_2->track()->id()};
1360
1361 rtc::scoped_refptr<const webrtc::RTCStatsReport> caller_report =
1362 caller()->NewGetStats();
1363 ASSERT_TRUE(caller_report);
1364 auto outbound_stream_stats =
1365 caller_report->GetStatsOfType<webrtc::RTCOutboundRTPStreamStats>();
Henrik Boströma0ff50c2020-05-05 15:54:46 +02001366 ASSERT_EQ(outbound_stream_stats.size(), 4u);
Steve Antonffa6ce42018-11-30 09:26:08 -08001367 std::vector<std::string> outbound_track_ids;
1368 for (const auto& stat : outbound_stream_stats) {
1369 ASSERT_TRUE(stat->bytes_sent.is_defined());
1370 EXPECT_LT(0u, *stat->bytes_sent);
Rasmus Brandt2efae772019-06-27 14:29:34 +02001371 if (*stat->kind == "video") {
1372 ASSERT_TRUE(stat->key_frames_encoded.is_defined());
1373 EXPECT_GT(*stat->key_frames_encoded, 0u);
1374 ASSERT_TRUE(stat->frames_encoded.is_defined());
1375 EXPECT_GE(*stat->frames_encoded, *stat->key_frames_encoded);
1376 }
Steve Antonffa6ce42018-11-30 09:26:08 -08001377 ASSERT_TRUE(stat->track_id.is_defined());
1378 const auto* track_stat =
1379 caller_report->GetAs<webrtc::RTCMediaStreamTrackStats>(*stat->track_id);
1380 ASSERT_TRUE(track_stat);
1381 outbound_track_ids.push_back(*track_stat->track_identifier);
1382 }
1383 EXPECT_THAT(outbound_track_ids, UnorderedElementsAreArray(track_ids));
1384
1385 rtc::scoped_refptr<const webrtc::RTCStatsReport> callee_report =
1386 callee()->NewGetStats();
1387 ASSERT_TRUE(callee_report);
1388 auto inbound_stream_stats =
1389 callee_report->GetStatsOfType<webrtc::RTCInboundRTPStreamStats>();
1390 ASSERT_EQ(4u, inbound_stream_stats.size());
1391 std::vector<std::string> inbound_track_ids;
1392 for (const auto& stat : inbound_stream_stats) {
1393 ASSERT_TRUE(stat->bytes_received.is_defined());
1394 EXPECT_LT(0u, *stat->bytes_received);
Rasmus Brandt2efae772019-06-27 14:29:34 +02001395 if (*stat->kind == "video") {
1396 ASSERT_TRUE(stat->key_frames_decoded.is_defined());
1397 EXPECT_GT(*stat->key_frames_decoded, 0u);
1398 ASSERT_TRUE(stat->frames_decoded.is_defined());
1399 EXPECT_GE(*stat->frames_decoded, *stat->key_frames_decoded);
1400 }
Steve Antonffa6ce42018-11-30 09:26:08 -08001401 ASSERT_TRUE(stat->track_id.is_defined());
1402 const auto* track_stat =
1403 callee_report->GetAs<webrtc::RTCMediaStreamTrackStats>(*stat->track_id);
1404 ASSERT_TRUE(track_stat);
1405 inbound_track_ids.push_back(*track_stat->track_identifier);
1406 }
1407 EXPECT_THAT(inbound_track_ids, UnorderedElementsAreArray(track_ids));
1408}
1409
1410// Test that we can get stats (using the new stats implementation) for
deadbeefd8ad7882017-04-18 16:01:17 -07001411// unsignaled streams. Meaning when SSRCs/MSIDs aren't signaled explicitly in
1412// SDP.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001413TEST_P(PeerConnectionIntegrationTest,
deadbeefd8ad7882017-04-18 16:01:17 -07001414 GetStatsForUnsignaledStreamWithNewStatsApi) {
1415 ASSERT_TRUE(CreatePeerConnectionWrappers());
1416 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001417 caller()->AddAudioTrack();
deadbeefd8ad7882017-04-18 16:01:17 -07001418 // Remove SSRCs and MSIDs from the received offer SDP.
1419 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
1420 caller()->CreateAndSetAndSignalOffer();
1421 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001422 MediaExpectations media_expectations;
1423 media_expectations.CalleeExpectsSomeAudio(1);
1424 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeefd8ad7882017-04-18 16:01:17 -07001425
1426 // We received a frame, so we should have nonzero "bytes received" stats for
1427 // the unsignaled stream, if stats are working for it.
1428 rtc::scoped_refptr<const webrtc::RTCStatsReport> report =
1429 callee()->NewGetStats();
1430 ASSERT_NE(nullptr, report);
1431 auto inbound_stream_stats =
1432 report->GetStatsOfType<webrtc::RTCInboundRTPStreamStats>();
1433 ASSERT_EQ(1U, inbound_stream_stats.size());
1434 ASSERT_TRUE(inbound_stream_stats[0]->bytes_received.is_defined());
1435 ASSERT_GT(*inbound_stream_stats[0]->bytes_received, 0U);
zhihuangf8164932017-05-19 13:09:47 -07001436 ASSERT_TRUE(inbound_stream_stats[0]->track_id.is_defined());
1437}
1438
Taylor Brandstettera4653442018-06-19 09:44:26 -07001439// Same as above but for the legacy stats implementation.
1440TEST_P(PeerConnectionIntegrationTest,
1441 GetStatsForUnsignaledStreamWithOldStatsApi) {
1442 ASSERT_TRUE(CreatePeerConnectionWrappers());
1443 ConnectFakeSignaling();
1444 caller()->AddAudioTrack();
1445 // Remove SSRCs and MSIDs from the received offer SDP.
1446 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
1447 caller()->CreateAndSetAndSignalOffer();
1448 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1449
1450 // Note that, since the old stats implementation associates SSRCs with tracks
1451 // using SDP, when SSRCs aren't signaled in SDP these stats won't have an
1452 // associated track ID. So we can't use the track "selector" argument.
1453 //
1454 // Also, we use "EXPECT_TRUE_WAIT" because the stats collector may decide to
1455 // return cached stats if not enough time has passed since the last update.
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02001456 EXPECT_TRUE_WAIT(callee()->OldGetStats()->BytesReceived() > 0,
Taylor Brandstettera4653442018-06-19 09:44:26 -07001457 kDefaultTimeout);
1458}
1459
zhihuangf8164932017-05-19 13:09:47 -07001460// Test that we can successfully get the media related stats (audio level
1461// etc.) for the unsignaled stream.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001462TEST_P(PeerConnectionIntegrationTest,
zhihuangf8164932017-05-19 13:09:47 -07001463 GetMediaStatsForUnsignaledStreamWithNewStatsApi) {
1464 ASSERT_TRUE(CreatePeerConnectionWrappers());
1465 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001466 caller()->AddAudioVideoTracks();
zhihuangf8164932017-05-19 13:09:47 -07001467 // Remove SSRCs and MSIDs from the received offer SDP.
1468 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
1469 caller()->CreateAndSetAndSignalOffer();
1470 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001471 MediaExpectations media_expectations;
1472 media_expectations.CalleeExpectsSomeAudio(1);
1473 media_expectations.CalleeExpectsSomeVideo(1);
1474 ASSERT_TRUE(ExpectNewFrames(media_expectations));
zhihuangf8164932017-05-19 13:09:47 -07001475
1476 rtc::scoped_refptr<const webrtc::RTCStatsReport> report =
1477 callee()->NewGetStats();
1478 ASSERT_NE(nullptr, report);
1479
1480 auto media_stats = report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
1481 auto audio_index = FindFirstMediaStatsIndexByKind("audio", media_stats);
1482 ASSERT_GE(audio_index, 0);
1483 EXPECT_TRUE(media_stats[audio_index]->audio_level.is_defined());
deadbeef1dcb1642017-03-29 21:08:16 -07001484}
1485
deadbeef4e2deab2017-09-20 13:56:21 -07001486// Helper for test below.
1487void ModifySsrcs(cricket::SessionDescription* desc) {
1488 for (ContentInfo& content : desc->contents()) {
Steve Antondf527fd2018-04-27 15:52:03 -07001489 for (StreamParams& stream :
Steve Antonb1c1de12017-12-21 15:14:30 -08001490 content.media_description()->mutable_streams()) {
deadbeef4e2deab2017-09-20 13:56:21 -07001491 for (uint32_t& ssrc : stream.ssrcs) {
1492 ssrc = rtc::CreateRandomId();
1493 }
1494 }
1495 }
1496}
1497
1498// Test that the "RTCMediaSteamTrackStats" object is updated correctly when
1499// SSRCs are unsignaled, and the SSRC of the received (audio) stream changes.
1500// This should result in two "RTCInboundRTPStreamStats", but only one
1501// "RTCMediaStreamTrackStats", whose counters go up continuously rather than
1502// being reset to 0 once the SSRC change occurs.
1503//
1504// Regression test for this bug:
1505// https://bugs.chromium.org/p/webrtc/issues/detail?id=8158
1506//
1507// The bug causes the track stats to only represent one of the two streams:
1508// whichever one has the higher SSRC. So with this bug, there was a 50% chance
1509// that the track stat counters would reset to 0 when the new stream is
1510// received, and a 50% chance that they'll stop updating (while
1511// "concealed_samples" continues increasing, due to silence being generated for
1512// the inactive stream).
Seth Hampson2f0d7022018-02-20 11:54:42 -08001513TEST_P(PeerConnectionIntegrationTest,
Steve Anton83119dd2017-11-10 16:19:52 -08001514 TrackStatsUpdatedCorrectlyWhenUnsignaledSsrcChanges) {
deadbeef4e2deab2017-09-20 13:56:21 -07001515 ASSERT_TRUE(CreatePeerConnectionWrappers());
1516 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001517 caller()->AddAudioTrack();
deadbeef4e2deab2017-09-20 13:56:21 -07001518 // Remove SSRCs and MSIDs from the received offer SDP, simulating an endpoint
1519 // that doesn't signal SSRCs (from the callee's perspective).
1520 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
1521 caller()->CreateAndSetAndSignalOffer();
1522 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1523 // Wait for 50 audio frames (500ms of audio) to be received by the callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001524 {
1525 MediaExpectations media_expectations;
1526 media_expectations.CalleeExpectsSomeAudio(50);
1527 ASSERT_TRUE(ExpectNewFrames(media_expectations));
1528 }
deadbeef4e2deab2017-09-20 13:56:21 -07001529 // Some audio frames were received, so we should have nonzero "samples
1530 // received" for the track.
1531 rtc::scoped_refptr<const webrtc::RTCStatsReport> report =
1532 callee()->NewGetStats();
1533 ASSERT_NE(nullptr, report);
1534 auto track_stats = report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
1535 ASSERT_EQ(1U, track_stats.size());
1536 ASSERT_TRUE(track_stats[0]->total_samples_received.is_defined());
1537 ASSERT_GT(*track_stats[0]->total_samples_received, 0U);
1538 // uint64_t prev_samples_received = *track_stats[0]->total_samples_received;
1539
1540 // Create a new offer and munge it to cause the caller to use a new SSRC.
1541 caller()->SetGeneratedSdpMunger(ModifySsrcs);
1542 caller()->CreateAndSetAndSignalOffer();
1543 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1544 // Wait for 25 more audio frames (250ms of audio) to be received, from the new
1545 // SSRC.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001546 {
1547 MediaExpectations media_expectations;
1548 media_expectations.CalleeExpectsSomeAudio(25);
1549 ASSERT_TRUE(ExpectNewFrames(media_expectations));
1550 }
deadbeef4e2deab2017-09-20 13:56:21 -07001551
1552 report = callee()->NewGetStats();
1553 ASSERT_NE(nullptr, report);
1554 track_stats = report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
1555 ASSERT_EQ(1U, track_stats.size());
1556 ASSERT_TRUE(track_stats[0]->total_samples_received.is_defined());
1557 // The "total samples received" stat should only be greater than it was
1558 // before.
1559 // TODO(deadbeef): Uncomment this assertion once the bug is completely fixed.
1560 // Right now, the new SSRC will cause the counters to reset to 0.
1561 // EXPECT_GT(*track_stats[0]->total_samples_received, prev_samples_received);
1562
1563 // Additionally, the percentage of concealed samples (samples generated to
Steve Anton83119dd2017-11-10 16:19:52 -08001564 // conceal packet loss) should be less than 50%. If it's greater, that's a
deadbeef4e2deab2017-09-20 13:56:21 -07001565 // good sign that we're seeing stats from the old stream that's no longer
1566 // receiving packets, and is generating concealed samples of silence.
Steve Anton83119dd2017-11-10 16:19:52 -08001567 constexpr double kAcceptableConcealedSamplesPercentage = 0.50;
deadbeef4e2deab2017-09-20 13:56:21 -07001568 ASSERT_TRUE(track_stats[0]->concealed_samples.is_defined());
1569 EXPECT_LT(*track_stats[0]->concealed_samples,
1570 *track_stats[0]->total_samples_received *
1571 kAcceptableConcealedSamplesPercentage);
1572
1573 // Also ensure that we have two "RTCInboundRTPStreamStats" as expected, as a
1574 // sanity check that the SSRC really changed.
1575 // TODO(deadbeef): This isn't working right now, because we're not returning
1576 // *any* stats for the inactive stream. Uncomment when the bug is completely
1577 // fixed.
1578 // auto inbound_stream_stats =
1579 // report->GetStatsOfType<webrtc::RTCInboundRTPStreamStats>();
1580 // ASSERT_EQ(2U, inbound_stream_stats.size());
1581}
1582
deadbeef1dcb1642017-03-29 21:08:16 -07001583// Test that DTLS 1.0 is used if both sides only support DTLS 1.0.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001584TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithDtls10) {
deadbeef1dcb1642017-03-29 21:08:16 -07001585 PeerConnectionFactory::Options dtls_10_options;
1586 dtls_10_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
1587 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(dtls_10_options,
1588 dtls_10_options));
1589 ConnectFakeSignaling();
1590 // Do normal offer/answer and wait for some frames to be received in each
1591 // direction.
Steve Anton15324772018-01-16 10:26:49 -08001592 caller()->AddAudioVideoTracks();
1593 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001594 caller()->CreateAndSetAndSignalOffer();
1595 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001596 MediaExpectations media_expectations;
1597 media_expectations.ExpectBidirectionalAudioAndVideo();
1598 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07001599}
1600
1601// Test getting cipher stats and UMA metrics when DTLS 1.0 is negotiated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001602TEST_P(PeerConnectionIntegrationTest, Dtls10CipherStatsAndUmaMetrics) {
deadbeef1dcb1642017-03-29 21:08:16 -07001603 PeerConnectionFactory::Options dtls_10_options;
1604 dtls_10_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
1605 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(dtls_10_options,
1606 dtls_10_options));
1607 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001608 caller()->AddAudioVideoTracks();
1609 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001610 caller()->CreateAndSetAndSignalOffer();
Qingsi Wang7fc821d2018-07-12 12:54:53 -07001611 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07001612 EXPECT_TRUE_WAIT(rtc::SSLStreamAdapter::IsAcceptableCipher(
deadbeefd8ad7882017-04-18 16:01:17 -07001613 caller()->OldGetStats()->DtlsCipher(), rtc::KT_DEFAULT),
deadbeef1dcb1642017-03-29 21:08:16 -07001614 kDefaultTimeout);
1615 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
deadbeefd8ad7882017-04-18 16:01:17 -07001616 caller()->OldGetStats()->SrtpCipher(), kDefaultTimeout);
Qingsi Wang7fc821d2018-07-12 12:54:53 -07001617 // TODO(bugs.webrtc.org/9456): Fix it.
Ying Wangef3998f2019-12-09 13:06:53 +01001618 EXPECT_METRIC_EQ(1, webrtc::metrics::NumEvents(
1619 "WebRTC.PeerConnection.SrtpCryptoSuite.Audio",
1620 kDefaultSrtpCryptoSuite));
deadbeef1dcb1642017-03-29 21:08:16 -07001621}
1622
1623// Test getting cipher stats and UMA metrics when DTLS 1.2 is negotiated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001624TEST_P(PeerConnectionIntegrationTest, Dtls12CipherStatsAndUmaMetrics) {
deadbeef1dcb1642017-03-29 21:08:16 -07001625 PeerConnectionFactory::Options dtls_12_options;
1626 dtls_12_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
1627 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(dtls_12_options,
1628 dtls_12_options));
1629 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001630 caller()->AddAudioVideoTracks();
1631 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001632 caller()->CreateAndSetAndSignalOffer();
Qingsi Wang7fc821d2018-07-12 12:54:53 -07001633 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07001634 EXPECT_TRUE_WAIT(rtc::SSLStreamAdapter::IsAcceptableCipher(
deadbeefd8ad7882017-04-18 16:01:17 -07001635 caller()->OldGetStats()->DtlsCipher(), rtc::KT_DEFAULT),
deadbeef1dcb1642017-03-29 21:08:16 -07001636 kDefaultTimeout);
1637 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
deadbeefd8ad7882017-04-18 16:01:17 -07001638 caller()->OldGetStats()->SrtpCipher(), kDefaultTimeout);
Qingsi Wang7fc821d2018-07-12 12:54:53 -07001639 // TODO(bugs.webrtc.org/9456): Fix it.
Ying Wangef3998f2019-12-09 13:06:53 +01001640 EXPECT_METRIC_EQ(1, webrtc::metrics::NumEvents(
1641 "WebRTC.PeerConnection.SrtpCryptoSuite.Audio",
1642 kDefaultSrtpCryptoSuite));
deadbeef1dcb1642017-03-29 21:08:16 -07001643}
1644
1645// Test that DTLS 1.0 can be used if the caller supports DTLS 1.2 and the
1646// callee only supports 1.0.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001647TEST_P(PeerConnectionIntegrationTest, CallerDtls12ToCalleeDtls10) {
deadbeef1dcb1642017-03-29 21:08:16 -07001648 PeerConnectionFactory::Options caller_options;
1649 caller_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
1650 PeerConnectionFactory::Options callee_options;
1651 callee_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
1652 ASSERT_TRUE(
1653 CreatePeerConnectionWrappersWithOptions(caller_options, callee_options));
1654 ConnectFakeSignaling();
1655 // Do normal offer/answer and wait for some frames to be received in each
1656 // direction.
Steve Anton15324772018-01-16 10:26:49 -08001657 caller()->AddAudioVideoTracks();
1658 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001659 caller()->CreateAndSetAndSignalOffer();
1660 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001661 MediaExpectations media_expectations;
1662 media_expectations.ExpectBidirectionalAudioAndVideo();
1663 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07001664}
1665
1666// Test that DTLS 1.0 can be used if the caller only supports DTLS 1.0 and the
1667// callee supports 1.2.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001668TEST_P(PeerConnectionIntegrationTest, CallerDtls10ToCalleeDtls12) {
deadbeef1dcb1642017-03-29 21:08:16 -07001669 PeerConnectionFactory::Options caller_options;
1670 caller_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
1671 PeerConnectionFactory::Options callee_options;
1672 callee_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
1673 ASSERT_TRUE(
1674 CreatePeerConnectionWrappersWithOptions(caller_options, callee_options));
1675 ConnectFakeSignaling();
1676 // Do normal offer/answer and wait for some frames to be received in each
1677 // direction.
Steve Anton15324772018-01-16 10:26:49 -08001678 caller()->AddAudioVideoTracks();
1679 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001680 caller()->CreateAndSetAndSignalOffer();
1681 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001682 MediaExpectations media_expectations;
1683 media_expectations.ExpectBidirectionalAudioAndVideo();
1684 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07001685}
1686
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07001687// The three tests below verify that "enable_aes128_sha1_32_crypto_cipher"
1688// works as expected; the cipher should only be used if enabled by both sides.
1689TEST_P(PeerConnectionIntegrationTest,
1690 Aes128Sha1_32_CipherNotUsedWhenOnlyCallerSupported) {
1691 PeerConnectionFactory::Options caller_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07001692 caller_options.crypto_options.srtp.enable_aes128_sha1_32_crypto_cipher = true;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07001693 PeerConnectionFactory::Options callee_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07001694 callee_options.crypto_options.srtp.enable_aes128_sha1_32_crypto_cipher =
1695 false;
Mirko Bonadei7750d802021-07-26 17:27:42 +02001696 int expected_cipher_suite = rtc::kSrtpAes128CmSha1_80;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07001697 TestNegotiatedCipherSuite(caller_options, callee_options,
1698 expected_cipher_suite);
1699}
1700
1701TEST_P(PeerConnectionIntegrationTest,
1702 Aes128Sha1_32_CipherNotUsedWhenOnlyCalleeSupported) {
1703 PeerConnectionFactory::Options caller_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07001704 caller_options.crypto_options.srtp.enable_aes128_sha1_32_crypto_cipher =
1705 false;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07001706 PeerConnectionFactory::Options callee_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07001707 callee_options.crypto_options.srtp.enable_aes128_sha1_32_crypto_cipher = true;
Mirko Bonadei7750d802021-07-26 17:27:42 +02001708 int expected_cipher_suite = rtc::kSrtpAes128CmSha1_80;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07001709 TestNegotiatedCipherSuite(caller_options, callee_options,
1710 expected_cipher_suite);
1711}
1712
1713TEST_P(PeerConnectionIntegrationTest, Aes128Sha1_32_CipherUsedWhenSupported) {
1714 PeerConnectionFactory::Options caller_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07001715 caller_options.crypto_options.srtp.enable_aes128_sha1_32_crypto_cipher = true;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07001716 PeerConnectionFactory::Options callee_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07001717 callee_options.crypto_options.srtp.enable_aes128_sha1_32_crypto_cipher = true;
Mirko Bonadei7750d802021-07-26 17:27:42 +02001718 int expected_cipher_suite = rtc::kSrtpAes128CmSha1_32;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07001719 TestNegotiatedCipherSuite(caller_options, callee_options,
1720 expected_cipher_suite);
1721}
1722
deadbeef1dcb1642017-03-29 21:08:16 -07001723// Test that a non-GCM cipher is used if both sides only support non-GCM.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001724TEST_P(PeerConnectionIntegrationTest, NonGcmCipherUsedWhenGcmNotSupported) {
deadbeef1dcb1642017-03-29 21:08:16 -07001725 bool local_gcm_enabled = false;
1726 bool remote_gcm_enabled = false;
Philipp Hancke2ebbff82019-10-26 06:12:55 +02001727 bool aes_ctr_enabled = true;
deadbeef1dcb1642017-03-29 21:08:16 -07001728 int expected_cipher_suite = kDefaultSrtpCryptoSuite;
1729 TestGcmNegotiationUsesCipherSuite(local_gcm_enabled, remote_gcm_enabled,
Philipp Hancke2ebbff82019-10-26 06:12:55 +02001730 aes_ctr_enabled, expected_cipher_suite);
deadbeef1dcb1642017-03-29 21:08:16 -07001731}
1732
Philipp Hancke2ebbff82019-10-26 06:12:55 +02001733// Test that a GCM cipher is used if both ends support it and non-GCM is
1734// disabled.
1735TEST_P(PeerConnectionIntegrationTest, GcmCipherUsedWhenOnlyGcmSupported) {
deadbeef1dcb1642017-03-29 21:08:16 -07001736 bool local_gcm_enabled = true;
1737 bool remote_gcm_enabled = true;
Philipp Hancke2ebbff82019-10-26 06:12:55 +02001738 bool aes_ctr_enabled = false;
deadbeef1dcb1642017-03-29 21:08:16 -07001739 int expected_cipher_suite = kDefaultSrtpCryptoSuiteGcm;
1740 TestGcmNegotiationUsesCipherSuite(local_gcm_enabled, remote_gcm_enabled,
Philipp Hancke2ebbff82019-10-26 06:12:55 +02001741 aes_ctr_enabled, expected_cipher_suite);
deadbeef1dcb1642017-03-29 21:08:16 -07001742}
1743
deadbeef7914b8c2017-04-21 03:23:33 -07001744// Verify that media can be transmitted end-to-end when GCM crypto suites are
1745// enabled. Note that the above tests, such as GcmCipherUsedWhenGcmSupported,
1746// only verify that a GCM cipher is negotiated, and not necessarily that SRTP
1747// works with it.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001748TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithGcmCipher) {
deadbeef7914b8c2017-04-21 03:23:33 -07001749 PeerConnectionFactory::Options gcm_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07001750 gcm_options.crypto_options.srtp.enable_gcm_crypto_suites = true;
Philipp Hancke2ebbff82019-10-26 06:12:55 +02001751 gcm_options.crypto_options.srtp.enable_aes128_sha1_80_crypto_cipher = false;
deadbeef7914b8c2017-04-21 03:23:33 -07001752 ASSERT_TRUE(
1753 CreatePeerConnectionWrappersWithOptions(gcm_options, gcm_options));
1754 ConnectFakeSignaling();
1755 // Do normal offer/answer and wait for some frames to be received in each
1756 // direction.
Steve Anton15324772018-01-16 10:26:49 -08001757 caller()->AddAudioVideoTracks();
1758 callee()->AddAudioVideoTracks();
deadbeef7914b8c2017-04-21 03:23:33 -07001759 caller()->CreateAndSetAndSignalOffer();
1760 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001761 MediaExpectations media_expectations;
1762 media_expectations.ExpectBidirectionalAudioAndVideo();
1763 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef7914b8c2017-04-21 03:23:33 -07001764}
1765
deadbeef1dcb1642017-03-29 21:08:16 -07001766// Test that the ICE connection and gathering states eventually reach
1767// "complete".
Seth Hampson2f0d7022018-02-20 11:54:42 -08001768TEST_P(PeerConnectionIntegrationTest, IceStatesReachCompletion) {
deadbeef1dcb1642017-03-29 21:08:16 -07001769 ASSERT_TRUE(CreatePeerConnectionWrappers());
1770 ConnectFakeSignaling();
1771 // Do normal offer/answer.
Steve Anton15324772018-01-16 10:26:49 -08001772 caller()->AddAudioVideoTracks();
1773 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001774 caller()->CreateAndSetAndSignalOffer();
1775 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1776 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceGatheringComplete,
1777 caller()->ice_gathering_state(), kMaxWaitForFramesMs);
1778 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceGatheringComplete,
1779 callee()->ice_gathering_state(), kMaxWaitForFramesMs);
1780 // After the best candidate pair is selected and all candidates are signaled,
1781 // the ICE connection state should reach "complete".
1782 // TODO(deadbeef): Currently, the ICE "controlled" agent (the
1783 // answerer/"callee" by default) only reaches "connected". When this is
1784 // fixed, this test should be updated.
1785 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
1786 caller()->ice_connection_state(), kDefaultTimeout);
Alex Loiko9289eda2018-11-23 16:18:59 +00001787 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
1788 callee()->ice_connection_state(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07001789}
1790
Qingsi Wang1dac6d82018-12-12 15:28:47 -08001791constexpr int kOnlyLocalPorts = cricket::PORTALLOCATOR_DISABLE_STUN |
1792 cricket::PORTALLOCATOR_DISABLE_RELAY |
1793 cricket::PORTALLOCATOR_DISABLE_TCP;
Zach Stein6fcdc2f2018-08-23 16:25:55 -07001794
Qingsi Wang1dac6d82018-12-12 15:28:47 -08001795// Use a mock resolver to resolve the hostname back to the original IP on both
1796// sides and check that the ICE connection connects.
Zach Stein6fcdc2f2018-08-23 16:25:55 -07001797TEST_P(PeerConnectionIntegrationTest,
Harald Alvestrand099ff622022-06-02 13:24:32 +00001798 IceStatesReachCompletionWithRemoteHostname) {
Qingsi Wang1dac6d82018-12-12 15:28:47 -08001799 auto caller_resolver_factory =
Mirko Bonadei317a1f02019-09-17 17:06:18 +02001800 std::make_unique<NiceMock<webrtc::MockAsyncResolverFactory>>();
Qingsi Wang1dac6d82018-12-12 15:28:47 -08001801 auto callee_resolver_factory =
Mirko Bonadei317a1f02019-09-17 17:06:18 +02001802 std::make_unique<NiceMock<webrtc::MockAsyncResolverFactory>>();
Qingsi Wang1dac6d82018-12-12 15:28:47 -08001803 NiceMock<rtc::MockAsyncResolver> callee_async_resolver;
1804 NiceMock<rtc::MockAsyncResolver> caller_async_resolver;
Zach Stein6fcdc2f2018-08-23 16:25:55 -07001805
1806 // This also verifies that the injected AsyncResolverFactory is used by
1807 // P2PTransportChannel.
Qingsi Wang1dac6d82018-12-12 15:28:47 -08001808 EXPECT_CALL(*caller_resolver_factory, Create())
1809 .WillOnce(Return(&caller_async_resolver));
1810 webrtc::PeerConnectionDependencies caller_deps(nullptr);
1811 caller_deps.async_resolver_factory = std::move(caller_resolver_factory);
1812
1813 EXPECT_CALL(*callee_resolver_factory, Create())
1814 .WillOnce(Return(&callee_async_resolver));
1815 webrtc::PeerConnectionDependencies callee_deps(nullptr);
1816 callee_deps.async_resolver_factory = std::move(callee_resolver_factory);
1817
1818 PeerConnectionInterface::RTCConfiguration config;
1819 config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
1820 config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyRequire;
1821
1822 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndDeps(
1823 config, std::move(caller_deps), config, std::move(callee_deps)));
1824
1825 caller()->SetRemoteAsyncResolver(&callee_async_resolver);
1826 callee()->SetRemoteAsyncResolver(&caller_async_resolver);
1827
1828 // Enable hostname candidates with mDNS names.
Qingsi Wangecd30542019-05-22 14:34:56 -07001829 caller()->SetMdnsResponder(
Mirko Bonadei317a1f02019-09-17 17:06:18 +02001830 std::make_unique<webrtc::FakeMdnsResponder>(network_thread()));
Qingsi Wangecd30542019-05-22 14:34:56 -07001831 callee()->SetMdnsResponder(
Mirko Bonadei317a1f02019-09-17 17:06:18 +02001832 std::make_unique<webrtc::FakeMdnsResponder>(network_thread()));
Qingsi Wang1dac6d82018-12-12 15:28:47 -08001833
1834 SetPortAllocatorFlags(kOnlyLocalPorts, kOnlyLocalPorts);
Zach Stein6fcdc2f2018-08-23 16:25:55 -07001835
1836 ConnectFakeSignaling();
1837 caller()->AddAudioVideoTracks();
1838 callee()->AddAudioVideoTracks();
1839 caller()->CreateAndSetAndSignalOffer();
1840 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1841 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
1842 caller()->ice_connection_state(), kDefaultTimeout);
1843 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
1844 callee()->ice_connection_state(), kDefaultTimeout);
Jeroen de Borst833979f2018-12-13 08:25:54 -08001845
Ying Wangef3998f2019-12-09 13:06:53 +01001846 EXPECT_METRIC_EQ(1, webrtc::metrics::NumEvents(
1847 "WebRTC.PeerConnection.CandidatePairType_UDP",
1848 webrtc::kIceCandidatePairHostNameHostName));
Harald Alvestrand4f7486a2022-06-02 11:35:49 +00001849 DestroyPeerConnections();
Zach Stein6fcdc2f2018-08-23 16:25:55 -07001850}
1851
Steve Antonede9ca52017-10-16 13:04:27 -07001852// Test that firewalling the ICE connection causes the clients to identify the
1853// disconnected state and then removing the firewall causes them to reconnect.
1854class PeerConnectionIntegrationIceStatesTest
Seth Hampson2f0d7022018-02-20 11:54:42 -08001855 : public PeerConnectionIntegrationBaseTest,
1856 public ::testing::WithParamInterface<
1857 std::tuple<SdpSemantics, std::tuple<std::string, uint32_t>>> {
Steve Antonede9ca52017-10-16 13:04:27 -07001858 protected:
Seth Hampson2f0d7022018-02-20 11:54:42 -08001859 PeerConnectionIntegrationIceStatesTest()
1860 : PeerConnectionIntegrationBaseTest(std::get<0>(GetParam())) {
1861 port_allocator_flags_ = std::get<1>(std::get<1>(GetParam()));
Steve Antonede9ca52017-10-16 13:04:27 -07001862 }
1863
1864 void StartStunServer(const SocketAddress& server_address) {
1865 stun_server_.reset(
Niels Möller091617d2020-12-02 15:32:08 +01001866 cricket::TestStunServer::Create(firewall(), server_address));
Steve Antonede9ca52017-10-16 13:04:27 -07001867 }
1868
1869 bool TestIPv6() {
1870 return (port_allocator_flags_ & cricket::PORTALLOCATOR_ENABLE_IPV6);
1871 }
1872
1873 void SetPortAllocatorFlags() {
Qingsi Wang1dac6d82018-12-12 15:28:47 -08001874 PeerConnectionIntegrationBaseTest::SetPortAllocatorFlags(
1875 port_allocator_flags_, port_allocator_flags_);
Steve Antonede9ca52017-10-16 13:04:27 -07001876 }
1877
1878 std::vector<SocketAddress> CallerAddresses() {
1879 std::vector<SocketAddress> addresses;
1880 addresses.push_back(SocketAddress("1.1.1.1", 0));
1881 if (TestIPv6()) {
1882 addresses.push_back(SocketAddress("1111:0:a:b:c:d:e:f", 0));
1883 }
1884 return addresses;
1885 }
1886
1887 std::vector<SocketAddress> CalleeAddresses() {
1888 std::vector<SocketAddress> addresses;
1889 addresses.push_back(SocketAddress("2.2.2.2", 0));
1890 if (TestIPv6()) {
1891 addresses.push_back(SocketAddress("2222:0:a:b:c:d:e:f", 0));
1892 }
1893 return addresses;
1894 }
1895
1896 void SetUpNetworkInterfaces() {
1897 // Remove the default interfaces added by the test infrastructure.
Qingsi Wangecd30542019-05-22 14:34:56 -07001898 caller()->network_manager()->RemoveInterface(kDefaultLocalAddress);
1899 callee()->network_manager()->RemoveInterface(kDefaultLocalAddress);
Steve Antonede9ca52017-10-16 13:04:27 -07001900
1901 // Add network addresses for test.
1902 for (const auto& caller_address : CallerAddresses()) {
Qingsi Wangecd30542019-05-22 14:34:56 -07001903 caller()->network_manager()->AddInterface(caller_address);
Steve Antonede9ca52017-10-16 13:04:27 -07001904 }
1905 for (const auto& callee_address : CalleeAddresses()) {
Qingsi Wangecd30542019-05-22 14:34:56 -07001906 callee()->network_manager()->AddInterface(callee_address);
Steve Antonede9ca52017-10-16 13:04:27 -07001907 }
1908 }
1909
1910 private:
1911 uint32_t port_allocator_flags_;
1912 std::unique_ptr<cricket::TestStunServer> stun_server_;
1913};
1914
Yves Gerey100fe632020-01-17 19:15:53 +01001915// Ensure FakeClockForTest is constructed first (see class for rationale).
1916class PeerConnectionIntegrationIceStatesTestWithFakeClock
1917 : public FakeClockForTest,
1918 public PeerConnectionIntegrationIceStatesTest {};
1919
Harald Alvestrandec23d6d2021-02-11 10:47:22 +00001920#if !defined(THREAD_SANITIZER)
1921// This test provokes TSAN errors. bugs.webrtc.org/11282
1922
Jonas Olssonb75d9e92019-02-22 10:33:29 +01001923// Tests that if the connection doesn't get set up properly we eventually reach
1924// the "failed" iceConnectionState.
Yves Gerey100fe632020-01-17 19:15:53 +01001925TEST_P(PeerConnectionIntegrationIceStatesTestWithFakeClock,
1926 IceStateSetupFailure) {
Jonas Olssonb75d9e92019-02-22 10:33:29 +01001927 // Block connections to/from the caller and wait for ICE to become
1928 // disconnected.
1929 for (const auto& caller_address : CallerAddresses()) {
1930 firewall()->AddRule(false, rtc::FP_ANY, rtc::FD_ANY, caller_address);
1931 }
1932
1933 ASSERT_TRUE(CreatePeerConnectionWrappers());
1934 ConnectFakeSignaling();
1935 SetPortAllocatorFlags();
1936 SetUpNetworkInterfaces();
1937 caller()->AddAudioVideoTracks();
1938 caller()->CreateAndSetAndSignalOffer();
1939
1940 // According to RFC7675, if there is no response within 30 seconds then the
1941 // peer should consider the other side to have rejected the connection. This
1942 // is signaled by the state transitioning to "failed".
1943 constexpr int kConsentTimeout = 30000;
1944 ASSERT_EQ_SIMULATED_WAIT(PeerConnectionInterface::kIceConnectionFailed,
1945 caller()->standardized_ice_connection_state(),
Yves Gerey100fe632020-01-17 19:15:53 +01001946 kConsentTimeout, FakeClock());
Steve Antonede9ca52017-10-16 13:04:27 -07001947}
1948
Harald Alvestrandec23d6d2021-02-11 10:47:22 +00001949#endif // !defined(THREAD_SANITIZER)
1950
Steve Antonede9ca52017-10-16 13:04:27 -07001951// Tests that the best connection is set to the appropriate IPv4/IPv6 connection
1952// and that the statistics in the metric observers are updated correctly.
Rasmus Brandt32af25b2021-03-17 13:40:21 +01001953// TODO(bugs.webrtc.org/12591): Flaky on Windows.
1954#if defined(WEBRTC_WIN)
1955#define MAYBE_VerifyBestConnection DISABLED_VerifyBestConnection
1956#else
1957#define MAYBE_VerifyBestConnection VerifyBestConnection
1958#endif
1959TEST_P(PeerConnectionIntegrationIceStatesTest, MAYBE_VerifyBestConnection) {
Steve Antonede9ca52017-10-16 13:04:27 -07001960 ASSERT_TRUE(CreatePeerConnectionWrappers());
1961 ConnectFakeSignaling();
1962 SetPortAllocatorFlags();
1963 SetUpNetworkInterfaces();
Steve Anton15324772018-01-16 10:26:49 -08001964 caller()->AddAudioVideoTracks();
1965 callee()->AddAudioVideoTracks();
Steve Antonede9ca52017-10-16 13:04:27 -07001966 caller()->CreateAndSetAndSignalOffer();
1967
1968 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Steve Anton692f3c72020-01-16 14:12:31 -08001969 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
1970 caller()->ice_connection_state(), kDefaultTimeout);
1971 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
1972 callee()->ice_connection_state(), kDefaultTimeout);
Steve Antonede9ca52017-10-16 13:04:27 -07001973
Qingsi Wang7fc821d2018-07-12 12:54:53 -07001974 // TODO(bugs.webrtc.org/9456): Fix it.
1975 const int num_best_ipv4 = webrtc::metrics::NumEvents(
1976 "WebRTC.PeerConnection.IPMetrics", webrtc::kBestConnections_IPv4);
1977 const int num_best_ipv6 = webrtc::metrics::NumEvents(
1978 "WebRTC.PeerConnection.IPMetrics", webrtc::kBestConnections_IPv6);
Steve Antonede9ca52017-10-16 13:04:27 -07001979 if (TestIPv6()) {
1980 // When IPv6 is enabled, we should prefer an IPv6 connection over an IPv4
1981 // connection.
Ying Wangef3998f2019-12-09 13:06:53 +01001982 EXPECT_METRIC_EQ(0, num_best_ipv4);
1983 EXPECT_METRIC_EQ(1, num_best_ipv6);
Steve Antonede9ca52017-10-16 13:04:27 -07001984 } else {
Ying Wangef3998f2019-12-09 13:06:53 +01001985 EXPECT_METRIC_EQ(1, num_best_ipv4);
1986 EXPECT_METRIC_EQ(0, num_best_ipv6);
Steve Antonede9ca52017-10-16 13:04:27 -07001987 }
1988
Ying Wangef3998f2019-12-09 13:06:53 +01001989 EXPECT_METRIC_EQ(0, webrtc::metrics::NumEvents(
1990 "WebRTC.PeerConnection.CandidatePairType_UDP",
1991 webrtc::kIceCandidatePairHostHost));
1992 EXPECT_METRIC_EQ(1, webrtc::metrics::NumEvents(
1993 "WebRTC.PeerConnection.CandidatePairType_UDP",
1994 webrtc::kIceCandidatePairHostPublicHostPublic));
Steve Antonede9ca52017-10-16 13:04:27 -07001995}
1996
1997constexpr uint32_t kFlagsIPv4NoStun = cricket::PORTALLOCATOR_DISABLE_TCP |
1998 cricket::PORTALLOCATOR_DISABLE_STUN |
1999 cricket::PORTALLOCATOR_DISABLE_RELAY;
2000constexpr uint32_t kFlagsIPv6NoStun =
2001 cricket::PORTALLOCATOR_DISABLE_TCP | cricket::PORTALLOCATOR_DISABLE_STUN |
2002 cricket::PORTALLOCATOR_ENABLE_IPV6 | cricket::PORTALLOCATOR_DISABLE_RELAY;
2003constexpr uint32_t kFlagsIPv4Stun =
2004 cricket::PORTALLOCATOR_DISABLE_TCP | cricket::PORTALLOCATOR_DISABLE_RELAY;
2005
Mirko Bonadeic84f6612019-01-31 12:20:57 +01002006INSTANTIATE_TEST_SUITE_P(
Seth Hampson2f0d7022018-02-20 11:54:42 -08002007 PeerConnectionIntegrationTest,
2008 PeerConnectionIntegrationIceStatesTest,
Florent Castelli15a38de2022-04-06 00:38:21 +02002009 Combine(Values(SdpSemantics::kPlanB_DEPRECATED, SdpSemantics::kUnifiedPlan),
Seth Hampson2f0d7022018-02-20 11:54:42 -08002010 Values(std::make_pair("IPv4 no STUN", kFlagsIPv4NoStun),
2011 std::make_pair("IPv6 no STUN", kFlagsIPv6NoStun),
2012 std::make_pair("IPv4 with STUN", kFlagsIPv4Stun))));
Steve Antonede9ca52017-10-16 13:04:27 -07002013
Yves Gerey100fe632020-01-17 19:15:53 +01002014INSTANTIATE_TEST_SUITE_P(
2015 PeerConnectionIntegrationTest,
2016 PeerConnectionIntegrationIceStatesTestWithFakeClock,
Florent Castelli15a38de2022-04-06 00:38:21 +02002017 Combine(Values(SdpSemantics::kPlanB_DEPRECATED, SdpSemantics::kUnifiedPlan),
Yves Gerey100fe632020-01-17 19:15:53 +01002018 Values(std::make_pair("IPv4 no STUN", kFlagsIPv4NoStun),
2019 std::make_pair("IPv6 no STUN", kFlagsIPv6NoStun),
2020 std::make_pair("IPv4 with STUN", kFlagsIPv4Stun))));
2021
deadbeef1dcb1642017-03-29 21:08:16 -07002022// This test sets up a call between two parties with audio and video.
2023// During the call, the caller restarts ICE and the test verifies that
2024// new ICE candidates are generated and audio and video still can flow, and the
2025// ICE state reaches completed again.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002026TEST_P(PeerConnectionIntegrationTest, MediaContinuesFlowingAfterIceRestart) {
deadbeef1dcb1642017-03-29 21:08:16 -07002027 ASSERT_TRUE(CreatePeerConnectionWrappers());
2028 ConnectFakeSignaling();
2029 // Do normal offer/answer and wait for ICE to complete.
Steve Anton15324772018-01-16 10:26:49 -08002030 caller()->AddAudioVideoTracks();
2031 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002032 caller()->CreateAndSetAndSignalOffer();
2033 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2034 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
2035 caller()->ice_connection_state(), kMaxWaitForFramesMs);
Alex Loiko9289eda2018-11-23 16:18:59 +00002036 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
2037 callee()->ice_connection_state(), kMaxWaitForFramesMs);
deadbeef1dcb1642017-03-29 21:08:16 -07002038
2039 // To verify that the ICE restart actually occurs, get
2040 // ufrag/password/candidates before and after restart.
2041 // Create an SDP string of the first audio candidate for both clients.
2042 const webrtc::IceCandidateCollection* audio_candidates_caller =
2043 caller()->pc()->local_description()->candidates(0);
2044 const webrtc::IceCandidateCollection* audio_candidates_callee =
2045 callee()->pc()->local_description()->candidates(0);
2046 ASSERT_GT(audio_candidates_caller->count(), 0u);
2047 ASSERT_GT(audio_candidates_callee->count(), 0u);
2048 std::string caller_candidate_pre_restart;
2049 ASSERT_TRUE(
2050 audio_candidates_caller->at(0)->ToString(&caller_candidate_pre_restart));
2051 std::string callee_candidate_pre_restart;
2052 ASSERT_TRUE(
2053 audio_candidates_callee->at(0)->ToString(&callee_candidate_pre_restart));
2054 const cricket::SessionDescription* desc =
2055 caller()->pc()->local_description()->description();
2056 std::string caller_ufrag_pre_restart =
2057 desc->transport_infos()[0].description.ice_ufrag;
2058 desc = callee()->pc()->local_description()->description();
2059 std::string callee_ufrag_pre_restart =
2060 desc->transport_infos()[0].description.ice_ufrag;
2061
Alex Drake00c7ecf2019-08-06 10:54:47 -07002062 EXPECT_EQ(caller()->ice_candidate_pair_change_history().size(), 1u);
deadbeef1dcb1642017-03-29 21:08:16 -07002063 // Have the caller initiate an ICE restart.
2064 caller()->SetOfferAnswerOptions(IceRestartOfferAnswerOptions());
2065 caller()->CreateAndSetAndSignalOffer();
2066 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2067 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
2068 caller()->ice_connection_state(), kMaxWaitForFramesMs);
Alex Loiko9289eda2018-11-23 16:18:59 +00002069 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
deadbeef1dcb1642017-03-29 21:08:16 -07002070 callee()->ice_connection_state(), kMaxWaitForFramesMs);
2071
2072 // Grab the ufrags/candidates again.
2073 audio_candidates_caller = caller()->pc()->local_description()->candidates(0);
2074 audio_candidates_callee = callee()->pc()->local_description()->candidates(0);
2075 ASSERT_GT(audio_candidates_caller->count(), 0u);
2076 ASSERT_GT(audio_candidates_callee->count(), 0u);
2077 std::string caller_candidate_post_restart;
2078 ASSERT_TRUE(
2079 audio_candidates_caller->at(0)->ToString(&caller_candidate_post_restart));
2080 std::string callee_candidate_post_restart;
2081 ASSERT_TRUE(
2082 audio_candidates_callee->at(0)->ToString(&callee_candidate_post_restart));
2083 desc = caller()->pc()->local_description()->description();
2084 std::string caller_ufrag_post_restart =
2085 desc->transport_infos()[0].description.ice_ufrag;
2086 desc = callee()->pc()->local_description()->description();
2087 std::string callee_ufrag_post_restart =
2088 desc->transport_infos()[0].description.ice_ufrag;
2089 // Sanity check that an ICE restart was actually negotiated in SDP.
2090 ASSERT_NE(caller_candidate_pre_restart, caller_candidate_post_restart);
2091 ASSERT_NE(callee_candidate_pre_restart, callee_candidate_post_restart);
2092 ASSERT_NE(caller_ufrag_pre_restart, caller_ufrag_post_restart);
2093 ASSERT_NE(callee_ufrag_pre_restart, callee_ufrag_post_restart);
Alex Drake00c7ecf2019-08-06 10:54:47 -07002094 EXPECT_GT(caller()->ice_candidate_pair_change_history().size(), 1u);
deadbeef1dcb1642017-03-29 21:08:16 -07002095
2096 // Ensure that additional frames are received after the ICE restart.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002097 MediaExpectations media_expectations;
2098 media_expectations.ExpectBidirectionalAudioAndVideo();
2099 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002100}
2101
2102// Verify that audio/video can be received end-to-end when ICE renomination is
2103// enabled.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002104TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithIceRenomination) {
deadbeef1dcb1642017-03-29 21:08:16 -07002105 PeerConnectionInterface::RTCConfiguration config;
2106 config.enable_ice_renomination = true;
2107 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
2108 ConnectFakeSignaling();
2109 // Do normal offer/answer and wait for some frames to be received in each
2110 // direction.
Steve Anton15324772018-01-16 10:26:49 -08002111 caller()->AddAudioVideoTracks();
2112 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002113 caller()->CreateAndSetAndSignalOffer();
2114 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2115 // Sanity check that ICE renomination was actually negotiated.
2116 const cricket::SessionDescription* desc =
2117 caller()->pc()->local_description()->description();
2118 for (const cricket::TransportInfo& info : desc->transport_infos()) {
Steve Anton64b626b2019-01-28 17:25:26 -08002119 ASSERT_THAT(info.description.transport_options, Contains("renomination"));
deadbeef1dcb1642017-03-29 21:08:16 -07002120 }
2121 desc = callee()->pc()->local_description()->description();
2122 for (const cricket::TransportInfo& info : desc->transport_infos()) {
Steve Anton64b626b2019-01-28 17:25:26 -08002123 ASSERT_THAT(info.description.transport_options, Contains("renomination"));
deadbeef1dcb1642017-03-29 21:08:16 -07002124 }
Seth Hampson2f0d7022018-02-20 11:54:42 -08002125 MediaExpectations media_expectations;
2126 media_expectations.ExpectBidirectionalAudioAndVideo();
2127 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002128}
2129
Steve Anton6f25b092017-10-23 09:39:20 -07002130// With a max bundle policy and RTCP muxing, adding a new media description to
2131// the connection should not affect ICE at all because the new media will use
2132// the existing connection.
Rasmus Brandt685be142021-03-15 14:03:38 +01002133// TODO(bugs.webrtc.org/12538): Fails on tsan.
2134#if defined(THREAD_SANITIZER)
2135#define MAYBE_AddMediaToConnectedBundleDoesNotRestartIce \
2136 DISABLED_AddMediaToConnectedBundleDoesNotRestartIce
2137#else
2138#define MAYBE_AddMediaToConnectedBundleDoesNotRestartIce \
2139 AddMediaToConnectedBundleDoesNotRestartIce
2140#endif
Seth Hampson2f0d7022018-02-20 11:54:42 -08002141TEST_P(PeerConnectionIntegrationTest,
Rasmus Brandt685be142021-03-15 14:03:38 +01002142 MAYBE_AddMediaToConnectedBundleDoesNotRestartIce) {
Steve Anton6f25b092017-10-23 09:39:20 -07002143 PeerConnectionInterface::RTCConfiguration config;
2144 config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
2145 config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyRequire;
2146 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(
2147 config, PeerConnectionInterface::RTCConfiguration()));
2148 ConnectFakeSignaling();
2149
Steve Anton15324772018-01-16 10:26:49 -08002150 caller()->AddAudioTrack();
Steve Anton6f25b092017-10-23 09:39:20 -07002151 caller()->CreateAndSetAndSignalOffer();
2152 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Steve Antonff52f1b2017-10-26 12:24:50 -07002153 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceConnectionCompleted,
2154 caller()->ice_connection_state(), kDefaultTimeout);
Steve Anton6f25b092017-10-23 09:39:20 -07002155
2156 caller()->clear_ice_connection_state_history();
2157
Steve Anton15324772018-01-16 10:26:49 -08002158 caller()->AddVideoTrack();
Steve Anton6f25b092017-10-23 09:39:20 -07002159 caller()->CreateAndSetAndSignalOffer();
2160 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2161
2162 EXPECT_EQ(0u, caller()->ice_connection_state_history().size());
2163}
2164
deadbeef1dcb1642017-03-29 21:08:16 -07002165// This test sets up a call between two parties with audio and video. It then
2166// renegotiates setting the video m-line to "port 0", then later renegotiates
2167// again, enabling video.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002168TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07002169 VideoFlowsAfterMediaSectionIsRejectedAndRecycled) {
2170 ASSERT_TRUE(CreatePeerConnectionWrappers());
2171 ConnectFakeSignaling();
2172
2173 // Do initial negotiation, only sending media from the caller. Will result in
2174 // video and audio recvonly "m=" sections.
Steve Anton15324772018-01-16 10:26:49 -08002175 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002176 caller()->CreateAndSetAndSignalOffer();
2177 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2178
2179 // Negotiate again, disabling the video "m=" section (the callee will set the
2180 // port to 0 due to offer_to_receive_video = 0).
Florent Castelli15a38de2022-04-06 00:38:21 +02002181 if (sdp_semantics_ == SdpSemantics::kPlanB_DEPRECATED) {
Seth Hampson2f0d7022018-02-20 11:54:42 -08002182 PeerConnectionInterface::RTCOfferAnswerOptions options;
2183 options.offer_to_receive_video = 0;
2184 callee()->SetOfferAnswerOptions(options);
2185 } else {
2186 callee()->SetRemoteOfferHandler([this] {
Harald Alvestrand6060df52020-08-11 09:54:02 +02002187 callee()
2188 ->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)
2189 ->StopInternal();
Seth Hampson2f0d7022018-02-20 11:54:42 -08002190 });
2191 }
deadbeef1dcb1642017-03-29 21:08:16 -07002192 caller()->CreateAndSetAndSignalOffer();
2193 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2194 // Sanity check that video "m=" section was actually rejected.
2195 const ContentInfo* answer_video_content = cricket::GetFirstVideoContent(
2196 callee()->pc()->local_description()->description());
2197 ASSERT_NE(nullptr, answer_video_content);
2198 ASSERT_TRUE(answer_video_content->rejected);
2199
2200 // Enable video and do negotiation again, making sure video is received
2201 // end-to-end, also adding media stream to callee.
Florent Castelli15a38de2022-04-06 00:38:21 +02002202 if (sdp_semantics_ == SdpSemantics::kPlanB_DEPRECATED) {
Seth Hampson2f0d7022018-02-20 11:54:42 -08002203 PeerConnectionInterface::RTCOfferAnswerOptions options;
2204 options.offer_to_receive_video = 1;
2205 callee()->SetOfferAnswerOptions(options);
2206 } else {
2207 // The caller's transceiver is stopped, so we need to add another track.
2208 auto caller_transceiver =
2209 caller()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO);
Harald Alvestrand6060df52020-08-11 09:54:02 +02002210 EXPECT_EQ(nullptr, caller_transceiver.get());
Seth Hampson2f0d7022018-02-20 11:54:42 -08002211 caller()->AddVideoTrack();
2212 }
2213 callee()->AddVideoTrack();
2214 callee()->SetRemoteOfferHandler(nullptr);
deadbeef1dcb1642017-03-29 21:08:16 -07002215 caller()->CreateAndSetAndSignalOffer();
2216 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002217
deadbeef1dcb1642017-03-29 21:08:16 -07002218 // Verify the caller receives frames from the newly added stream, and the
2219 // callee receives additional frames from the re-enabled video m= section.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002220 MediaExpectations media_expectations;
2221 media_expectations.CalleeExpectsSomeAudio();
2222 media_expectations.ExpectBidirectionalVideo();
2223 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002224}
2225
deadbeef1dcb1642017-03-29 21:08:16 -07002226// This tests that if we negotiate after calling CreateSender but before we
2227// have a track, then set a track later, frames from the newly-set track are
2228// received end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002229TEST_F(PeerConnectionIntegrationTestPlanB,
deadbeef1dcb1642017-03-29 21:08:16 -07002230 MediaFlowsAfterEarlyWarmupWithCreateSender) {
2231 ASSERT_TRUE(CreatePeerConnectionWrappers());
2232 ConnectFakeSignaling();
2233 auto caller_audio_sender =
2234 caller()->pc()->CreateSender("audio", "caller_stream");
2235 auto caller_video_sender =
2236 caller()->pc()->CreateSender("video", "caller_stream");
2237 auto callee_audio_sender =
2238 callee()->pc()->CreateSender("audio", "callee_stream");
2239 auto callee_video_sender =
2240 callee()->pc()->CreateSender("video", "callee_stream");
2241 caller()->CreateAndSetAndSignalOffer();
2242 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
2243 // Wait for ICE to complete, without any tracks being set.
2244 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
2245 caller()->ice_connection_state(), kMaxWaitForFramesMs);
2246 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
2247 callee()->ice_connection_state(), kMaxWaitForFramesMs);
2248 // Now set the tracks, and expect frames to immediately start flowing.
Niels Möllerafb246b2022-04-20 14:26:50 +02002249 EXPECT_TRUE(
2250 caller_audio_sender->SetTrack(caller()->CreateLocalAudioTrack().get()));
2251 EXPECT_TRUE(
2252 caller_video_sender->SetTrack(caller()->CreateLocalVideoTrack().get()));
2253 EXPECT_TRUE(
2254 callee_audio_sender->SetTrack(callee()->CreateLocalAudioTrack().get()));
2255 EXPECT_TRUE(
2256 callee_video_sender->SetTrack(callee()->CreateLocalVideoTrack().get()));
Seth Hampson2f0d7022018-02-20 11:54:42 -08002257 MediaExpectations media_expectations;
2258 media_expectations.ExpectBidirectionalAudioAndVideo();
2259 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2260}
2261
2262// This tests that if we negotiate after calling AddTransceiver but before we
2263// have a track, then set a track later, frames from the newly-set tracks are
2264// received end-to-end.
2265TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
2266 MediaFlowsAfterEarlyWarmupWithAddTransceiver) {
2267 ASSERT_TRUE(CreatePeerConnectionWrappers());
2268 ConnectFakeSignaling();
2269 auto audio_result = caller()->pc()->AddTransceiver(cricket::MEDIA_TYPE_AUDIO);
2270 ASSERT_EQ(RTCErrorType::NONE, audio_result.error().type());
2271 auto caller_audio_sender = audio_result.MoveValue()->sender();
2272 auto video_result = caller()->pc()->AddTransceiver(cricket::MEDIA_TYPE_VIDEO);
2273 ASSERT_EQ(RTCErrorType::NONE, video_result.error().type());
2274 auto caller_video_sender = video_result.MoveValue()->sender();
2275 callee()->SetRemoteOfferHandler([this] {
2276 ASSERT_EQ(2u, callee()->pc()->GetTransceivers().size());
Harald Alvestrand6060df52020-08-11 09:54:02 +02002277 callee()->pc()->GetTransceivers()[0]->SetDirectionWithError(
Seth Hampson2f0d7022018-02-20 11:54:42 -08002278 RtpTransceiverDirection::kSendRecv);
Harald Alvestrand6060df52020-08-11 09:54:02 +02002279 callee()->pc()->GetTransceivers()[1]->SetDirectionWithError(
Seth Hampson2f0d7022018-02-20 11:54:42 -08002280 RtpTransceiverDirection::kSendRecv);
2281 });
2282 caller()->CreateAndSetAndSignalOffer();
2283 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
2284 // Wait for ICE to complete, without any tracks being set.
2285 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
2286 caller()->ice_connection_state(), kMaxWaitForFramesMs);
2287 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
2288 callee()->ice_connection_state(), kMaxWaitForFramesMs);
2289 // Now set the tracks, and expect frames to immediately start flowing.
2290 auto callee_audio_sender = callee()->pc()->GetSenders()[0];
2291 auto callee_video_sender = callee()->pc()->GetSenders()[1];
Niels Möllerafb246b2022-04-20 14:26:50 +02002292 ASSERT_TRUE(
2293 caller_audio_sender->SetTrack(caller()->CreateLocalAudioTrack().get()));
2294 ASSERT_TRUE(
2295 caller_video_sender->SetTrack(caller()->CreateLocalVideoTrack().get()));
2296 ASSERT_TRUE(
2297 callee_audio_sender->SetTrack(callee()->CreateLocalAudioTrack().get()));
2298 ASSERT_TRUE(
2299 callee_video_sender->SetTrack(callee()->CreateLocalVideoTrack().get()));
Seth Hampson2f0d7022018-02-20 11:54:42 -08002300 MediaExpectations media_expectations;
2301 media_expectations.ExpectBidirectionalAudioAndVideo();
2302 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002303}
2304
2305// This test verifies that a remote video track can be added via AddStream,
2306// and sent end-to-end. For this particular test, it's simply echoed back
2307// from the caller to the callee, rather than being forwarded to a third
2308// PeerConnection.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002309TEST_F(PeerConnectionIntegrationTestPlanB, CanSendRemoteVideoTrack) {
deadbeef1dcb1642017-03-29 21:08:16 -07002310 ASSERT_TRUE(CreatePeerConnectionWrappers());
2311 ConnectFakeSignaling();
2312 // Just send a video track from the caller.
Steve Anton15324772018-01-16 10:26:49 -08002313 caller()->AddVideoTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07002314 caller()->CreateAndSetAndSignalOffer();
2315 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02002316 ASSERT_EQ(1U, callee()->remote_streams()->count());
deadbeef1dcb1642017-03-29 21:08:16 -07002317
2318 // Echo the stream back, and do a new offer/anwer (initiated by callee this
2319 // time).
2320 callee()->pc()->AddStream(callee()->remote_streams()->at(0));
2321 callee()->CreateAndSetAndSignalOffer();
2322 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
2323
Seth Hampson2f0d7022018-02-20 11:54:42 -08002324 MediaExpectations media_expectations;
2325 media_expectations.ExpectBidirectionalVideo();
2326 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002327}
2328
Harald Alvestrandec23d6d2021-02-11 10:47:22 +00002329#if !defined(THREAD_SANITIZER)
2330// This test provokes TSAN errors. bugs.webrtc.org/11282
2331
deadbeef1dcb1642017-03-29 21:08:16 -07002332// Test that we achieve the expected end-to-end connection time, using a
2333// fake clock and simulated latency on the media and signaling paths.
2334// We use a TURN<->TURN connection because this is usually the quickest to
2335// set up initially, especially when we're confident the connection will work
2336// and can start sending media before we get a STUN response.
2337//
2338// With various optimizations enabled, here are the network delays we expect to
2339// be on the critical path:
2340// 1. 2 signaling trips: Signaling offer and offerer's TURN candidate, then
2341// signaling answer (with DTLS fingerprint).
2342// 2. 9 media hops: Rest of the DTLS handshake. 3 hops in each direction when
2343// using TURN<->TURN pair, and DTLS exchange is 4 packets,
2344// the first of which should have arrived before the answer.
Yves Gerey100fe632020-01-17 19:15:53 +01002345TEST_P(PeerConnectionIntegrationTestWithFakeClock,
2346 EndToEndConnectionTimeWithTurnTurnPair) {
deadbeef1dcb1642017-03-29 21:08:16 -07002347 static constexpr int media_hop_delay_ms = 50;
2348 static constexpr int signaling_trip_delay_ms = 500;
2349 // For explanation of these values, see comment above.
2350 static constexpr int required_media_hops = 9;
2351 static constexpr int required_signaling_trips = 2;
2352 // For internal delays (such as posting an event asychronously).
2353 static constexpr int allowed_internal_delay_ms = 20;
2354 static constexpr int total_connection_time_ms =
2355 media_hop_delay_ms * required_media_hops +
2356 signaling_trip_delay_ms * required_signaling_trips +
2357 allowed_internal_delay_ms;
2358
2359 static const rtc::SocketAddress turn_server_1_internal_address{"88.88.88.0",
2360 3478};
2361 static const rtc::SocketAddress turn_server_1_external_address{"88.88.88.1",
2362 0};
2363 static const rtc::SocketAddress turn_server_2_internal_address{"99.99.99.0",
2364 3478};
2365 static const rtc::SocketAddress turn_server_2_external_address{"99.99.99.1",
2366 0};
Seth Hampsonaed71642018-06-11 07:41:32 -07002367 cricket::TestTurnServer* turn_server_1 = CreateTurnServer(
2368 turn_server_1_internal_address, turn_server_1_external_address);
Jonas Orelandbdcee282017-10-10 14:01:40 +02002369
Seth Hampsonaed71642018-06-11 07:41:32 -07002370 cricket::TestTurnServer* turn_server_2 = CreateTurnServer(
2371 turn_server_2_internal_address, turn_server_2_external_address);
deadbeef1dcb1642017-03-29 21:08:16 -07002372 // Bypass permission check on received packets so media can be sent before
2373 // the candidate is signaled.
Seth Hampsonaed71642018-06-11 07:41:32 -07002374 network_thread()->Invoke<void>(RTC_FROM_HERE, [turn_server_1] {
2375 turn_server_1->set_enable_permission_checks(false);
2376 });
2377 network_thread()->Invoke<void>(RTC_FROM_HERE, [turn_server_2] {
2378 turn_server_2->set_enable_permission_checks(false);
2379 });
deadbeef1dcb1642017-03-29 21:08:16 -07002380
2381 PeerConnectionInterface::RTCConfiguration client_1_config;
2382 webrtc::PeerConnectionInterface::IceServer ice_server_1;
2383 ice_server_1.urls.push_back("turn:88.88.88.0:3478");
2384 ice_server_1.username = "test";
2385 ice_server_1.password = "test";
2386 client_1_config.servers.push_back(ice_server_1);
2387 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
2388 client_1_config.presume_writable_when_fully_relayed = true;
2389
2390 PeerConnectionInterface::RTCConfiguration client_2_config;
2391 webrtc::PeerConnectionInterface::IceServer ice_server_2;
2392 ice_server_2.urls.push_back("turn:99.99.99.0:3478");
2393 ice_server_2.username = "test";
2394 ice_server_2.password = "test";
2395 client_2_config.servers.push_back(ice_server_2);
2396 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
2397 client_2_config.presume_writable_when_fully_relayed = true;
2398
2399 ASSERT_TRUE(
2400 CreatePeerConnectionWrappersWithConfig(client_1_config, client_2_config));
2401 // Set up the simulated delays.
2402 SetSignalingDelayMs(signaling_trip_delay_ms);
2403 ConnectFakeSignaling();
2404 virtual_socket_server()->set_delay_mean(media_hop_delay_ms);
2405 virtual_socket_server()->UpdateDelayDistribution();
2406
2407 // Set "offer to receive audio/video" without adding any tracks, so we just
2408 // set up ICE/DTLS with no media.
2409 PeerConnectionInterface::RTCOfferAnswerOptions options;
2410 options.offer_to_receive_audio = 1;
2411 options.offer_to_receive_video = 1;
2412 caller()->SetOfferAnswerOptions(options);
2413 caller()->CreateAndSetAndSignalOffer();
deadbeef71452802017-05-07 17:21:01 -07002414 EXPECT_TRUE_SIMULATED_WAIT(DtlsConnected(), total_connection_time_ms,
Yves Gerey100fe632020-01-17 19:15:53 +01002415 FakeClock());
Seth Hampson1d4a76d2018-06-19 14:31:41 -07002416 // Closing the PeerConnections destroys the ports before the ScopedFakeClock.
2417 // If this is not done a DCHECK can be hit in ports.cc, because a large
2418 // negative number is calculated for the rtt due to the global clock changing.
Steve Antond91969e2019-05-30 12:27:03 -07002419 ClosePeerConnections();
deadbeef1dcb1642017-03-29 21:08:16 -07002420}
2421
Harald Alvestrandec23d6d2021-02-11 10:47:22 +00002422#endif // !defined(THREAD_SANITIZER)
2423
Jonas Orelandbdcee282017-10-10 14:01:40 +02002424// Verify that a TurnCustomizer passed in through RTCConfiguration
2425// is actually used by the underlying TURN candidate pair.
2426// Note that turnport_unittest.cc contains more detailed, lower-level tests.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002427TEST_P(PeerConnectionIntegrationTest, TurnCustomizerUsedForTurnConnections) {
Jonas Orelandbdcee282017-10-10 14:01:40 +02002428 static const rtc::SocketAddress turn_server_1_internal_address{"88.88.88.0",
2429 3478};
2430 static const rtc::SocketAddress turn_server_1_external_address{"88.88.88.1",
2431 0};
2432 static const rtc::SocketAddress turn_server_2_internal_address{"99.99.99.0",
2433 3478};
2434 static const rtc::SocketAddress turn_server_2_external_address{"99.99.99.1",
2435 0};
Seth Hampsonaed71642018-06-11 07:41:32 -07002436 CreateTurnServer(turn_server_1_internal_address,
2437 turn_server_1_external_address);
2438 CreateTurnServer(turn_server_2_internal_address,
2439 turn_server_2_external_address);
Jonas Orelandbdcee282017-10-10 14:01:40 +02002440
2441 PeerConnectionInterface::RTCConfiguration client_1_config;
2442 webrtc::PeerConnectionInterface::IceServer ice_server_1;
2443 ice_server_1.urls.push_back("turn:88.88.88.0:3478");
2444 ice_server_1.username = "test";
2445 ice_server_1.password = "test";
2446 client_1_config.servers.push_back(ice_server_1);
2447 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
Seth Hampsonaed71642018-06-11 07:41:32 -07002448 auto* customizer1 = CreateTurnCustomizer();
2449 client_1_config.turn_customizer = customizer1;
Jonas Orelandbdcee282017-10-10 14:01:40 +02002450
2451 PeerConnectionInterface::RTCConfiguration client_2_config;
2452 webrtc::PeerConnectionInterface::IceServer ice_server_2;
2453 ice_server_2.urls.push_back("turn:99.99.99.0:3478");
2454 ice_server_2.username = "test";
2455 ice_server_2.password = "test";
2456 client_2_config.servers.push_back(ice_server_2);
2457 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
Seth Hampsonaed71642018-06-11 07:41:32 -07002458 auto* customizer2 = CreateTurnCustomizer();
2459 client_2_config.turn_customizer = customizer2;
Jonas Orelandbdcee282017-10-10 14:01:40 +02002460
2461 ASSERT_TRUE(
2462 CreatePeerConnectionWrappersWithConfig(client_1_config, client_2_config));
2463 ConnectFakeSignaling();
2464
2465 // Set "offer to receive audio/video" without adding any tracks, so we just
2466 // set up ICE/DTLS with no media.
2467 PeerConnectionInterface::RTCOfferAnswerOptions options;
2468 options.offer_to_receive_audio = 1;
2469 options.offer_to_receive_video = 1;
2470 caller()->SetOfferAnswerOptions(options);
2471 caller()->CreateAndSetAndSignalOffer();
2472 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
2473
Seth Hampsonaed71642018-06-11 07:41:32 -07002474 ExpectTurnCustomizerCountersIncremented(customizer1);
2475 ExpectTurnCustomizerCountersIncremented(customizer2);
Jonas Orelandbdcee282017-10-10 14:01:40 +02002476}
2477
Benjamin Wright2d5f3cb2018-05-22 14:46:06 -07002478// Verifies that you can use TCP instead of UDP to connect to a TURN server and
2479// send media between the caller and the callee.
2480TEST_P(PeerConnectionIntegrationTest, TCPUsedForTurnConnections) {
2481 static const rtc::SocketAddress turn_server_internal_address{"88.88.88.0",
2482 3478};
2483 static const rtc::SocketAddress turn_server_external_address{"88.88.88.1", 0};
2484
2485 // Enable TCP for the fake turn server.
Seth Hampsonaed71642018-06-11 07:41:32 -07002486 CreateTurnServer(turn_server_internal_address, turn_server_external_address,
2487 cricket::PROTO_TCP);
Benjamin Wright2d5f3cb2018-05-22 14:46:06 -07002488
2489 webrtc::PeerConnectionInterface::IceServer ice_server;
2490 ice_server.urls.push_back("turn:88.88.88.0:3478?transport=tcp");
2491 ice_server.username = "test";
2492 ice_server.password = "test";
2493
2494 PeerConnectionInterface::RTCConfiguration client_1_config;
2495 client_1_config.servers.push_back(ice_server);
2496 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
2497
2498 PeerConnectionInterface::RTCConfiguration client_2_config;
2499 client_2_config.servers.push_back(ice_server);
2500 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
2501
2502 ASSERT_TRUE(
2503 CreatePeerConnectionWrappersWithConfig(client_1_config, client_2_config));
2504
2505 // Do normal offer/answer and wait for ICE to complete.
2506 ConnectFakeSignaling();
2507 caller()->AddAudioVideoTracks();
2508 callee()->AddAudioVideoTracks();
2509 caller()->CreateAndSetAndSignalOffer();
2510 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2511 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
2512 callee()->ice_connection_state(), kMaxWaitForFramesMs);
2513
2514 MediaExpectations media_expectations;
2515 media_expectations.ExpectBidirectionalAudioAndVideo();
2516 EXPECT_TRUE(ExpectNewFrames(media_expectations));
2517}
2518
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07002519// Verify that a SSLCertificateVerifier passed in through
2520// PeerConnectionDependencies is actually used by the underlying SSL
2521// implementation to determine whether a certificate presented by the TURN
2522// server is accepted by the client. Note that openssladapter_unittest.cc
2523// contains more detailed, lower-level tests.
2524TEST_P(PeerConnectionIntegrationTest,
2525 SSLCertificateVerifierUsedForTurnConnections) {
2526 static const rtc::SocketAddress turn_server_internal_address{"88.88.88.0",
2527 3478};
2528 static const rtc::SocketAddress turn_server_external_address{"88.88.88.1", 0};
2529
2530 // Enable TCP-TLS for the fake turn server. We need to pass in 88.88.88.0 so
2531 // that host name verification passes on the fake certificate.
Seth Hampsonaed71642018-06-11 07:41:32 -07002532 CreateTurnServer(turn_server_internal_address, turn_server_external_address,
2533 cricket::PROTO_TLS, "88.88.88.0");
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07002534
2535 webrtc::PeerConnectionInterface::IceServer ice_server;
2536 ice_server.urls.push_back("turns:88.88.88.0:3478?transport=tcp");
2537 ice_server.username = "test";
2538 ice_server.password = "test";
2539
2540 PeerConnectionInterface::RTCConfiguration client_1_config;
2541 client_1_config.servers.push_back(ice_server);
2542 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
2543
2544 PeerConnectionInterface::RTCConfiguration client_2_config;
2545 client_2_config.servers.push_back(ice_server);
2546 // Setting the type to kRelay forces the connection to go through a TURN
2547 // server.
2548 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
2549
2550 // Get a copy to the pointer so we can verify calls later.
2551 rtc::TestCertificateVerifier* client_1_cert_verifier =
2552 new rtc::TestCertificateVerifier();
2553 client_1_cert_verifier->verify_certificate_ = true;
2554 rtc::TestCertificateVerifier* client_2_cert_verifier =
2555 new rtc::TestCertificateVerifier();
2556 client_2_cert_verifier->verify_certificate_ = true;
2557
2558 // Create the dependencies with the test certificate verifier.
2559 webrtc::PeerConnectionDependencies client_1_deps(nullptr);
2560 client_1_deps.tls_cert_verifier =
2561 std::unique_ptr<rtc::TestCertificateVerifier>(client_1_cert_verifier);
2562 webrtc::PeerConnectionDependencies client_2_deps(nullptr);
2563 client_2_deps.tls_cert_verifier =
2564 std::unique_ptr<rtc::TestCertificateVerifier>(client_2_cert_verifier);
2565
2566 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndDeps(
2567 client_1_config, std::move(client_1_deps), client_2_config,
2568 std::move(client_2_deps)));
2569 ConnectFakeSignaling();
2570
2571 // Set "offer to receive audio/video" without adding any tracks, so we just
2572 // set up ICE/DTLS with no media.
2573 PeerConnectionInterface::RTCOfferAnswerOptions options;
2574 options.offer_to_receive_audio = 1;
2575 options.offer_to_receive_video = 1;
2576 caller()->SetOfferAnswerOptions(options);
2577 caller()->CreateAndSetAndSignalOffer();
2578 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
2579
2580 EXPECT_GT(client_1_cert_verifier->call_count_, 0u);
2581 EXPECT_GT(client_2_cert_verifier->call_count_, 0u);
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07002582}
2583
Qingsi Wang25ec8882019-11-15 12:33:05 -08002584// Test that the injected ICE transport factory is used to create ICE transports
2585// for WebRTC connections.
2586TEST_P(PeerConnectionIntegrationTest, IceTransportFactoryUsedForConnections) {
2587 PeerConnectionInterface::RTCConfiguration default_config;
2588 PeerConnectionDependencies dependencies(nullptr);
2589 auto ice_transport_factory = std::make_unique<MockIceTransportFactory>();
2590 EXPECT_CALL(*ice_transport_factory, RecordIceTransportCreated()).Times(1);
2591 dependencies.ice_transport_factory = std::move(ice_transport_factory);
Niels Möller2a707032020-06-16 16:39:13 +02002592 auto wrapper = CreatePeerConnectionWrapper("Caller", nullptr, &default_config,
2593 std::move(dependencies), nullptr,
2594 /*reset_encoder_factory=*/false,
2595 /*reset_decoder_factory=*/false);
Qingsi Wang25ec8882019-11-15 12:33:05 -08002596 ASSERT_TRUE(wrapper);
2597 wrapper->CreateDataChannel();
Tommi87f70902021-04-27 14:43:08 +02002598 auto observer = rtc::make_ref_counted<MockSetSessionDescriptionObserver>();
Niels Möllerafb246b2022-04-20 14:26:50 +02002599 wrapper->pc()->SetLocalDescription(observer.get(),
Qingsi Wang25ec8882019-11-15 12:33:05 -08002600 wrapper->CreateOfferAndWait().release());
2601}
2602
deadbeefc964d0b2017-04-03 10:03:35 -07002603// Test that audio and video flow end-to-end when codec names don't use the
2604// expected casing, given that they're supposed to be case insensitive. To test
2605// this, all but one codec is removed from each media description, and its
2606// casing is changed.
2607//
2608// In the past, this has regressed and caused crashes/black video, due to the
2609// fact that code at some layers was doing case-insensitive comparisons and
2610// code at other layers was not.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002611TEST_P(PeerConnectionIntegrationTest, CodecNamesAreCaseInsensitive) {
deadbeefc964d0b2017-04-03 10:03:35 -07002612 ASSERT_TRUE(CreatePeerConnectionWrappers());
2613 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002614 caller()->AddAudioVideoTracks();
2615 callee()->AddAudioVideoTracks();
deadbeefc964d0b2017-04-03 10:03:35 -07002616
2617 // Remove all but one audio/video codec (opus and VP8), and change the
2618 // casing of the caller's generated offer.
2619 caller()->SetGeneratedSdpMunger([](cricket::SessionDescription* description) {
2620 cricket::AudioContentDescription* audio =
2621 GetFirstAudioContentDescription(description);
2622 ASSERT_NE(nullptr, audio);
2623 auto audio_codecs = audio->codecs();
2624 audio_codecs.erase(std::remove_if(audio_codecs.begin(), audio_codecs.end(),
2625 [](const cricket::AudioCodec& codec) {
2626 return codec.name != "opus";
2627 }),
2628 audio_codecs.end());
2629 ASSERT_EQ(1u, audio_codecs.size());
2630 audio_codecs[0].name = "OpUs";
2631 audio->set_codecs(audio_codecs);
2632
2633 cricket::VideoContentDescription* video =
2634 GetFirstVideoContentDescription(description);
2635 ASSERT_NE(nullptr, video);
2636 auto video_codecs = video->codecs();
2637 video_codecs.erase(std::remove_if(video_codecs.begin(), video_codecs.end(),
2638 [](const cricket::VideoCodec& codec) {
2639 return codec.name != "VP8";
2640 }),
2641 video_codecs.end());
2642 ASSERT_EQ(1u, video_codecs.size());
2643 video_codecs[0].name = "vP8";
2644 video->set_codecs(video_codecs);
2645 });
2646
2647 caller()->CreateAndSetAndSignalOffer();
2648 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2649
2650 // Verify frames are still received end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002651 MediaExpectations media_expectations;
2652 media_expectations.ExpectBidirectionalAudioAndVideo();
2653 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeefc964d0b2017-04-03 10:03:35 -07002654}
2655
Jonas Oreland49ac5952018-09-26 16:04:32 +02002656TEST_P(PeerConnectionIntegrationTest, GetSourcesAudio) {
hbos8d609f62017-04-10 07:39:05 -07002657 ASSERT_TRUE(CreatePeerConnectionWrappers());
2658 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002659 caller()->AddAudioTrack();
hbos8d609f62017-04-10 07:39:05 -07002660 caller()->CreateAndSetAndSignalOffer();
2661 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
deadbeefd8ad7882017-04-18 16:01:17 -07002662 // Wait for one audio frame to be received by the callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002663 MediaExpectations media_expectations;
2664 media_expectations.CalleeExpectsSomeAudio(1);
2665 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Jonas Oreland49ac5952018-09-26 16:04:32 +02002666 ASSERT_EQ(callee()->pc()->GetReceivers().size(), 1u);
hbos8d609f62017-04-10 07:39:05 -07002667 auto receiver = callee()->pc()->GetReceivers()[0];
2668 ASSERT_EQ(receiver->media_type(), cricket::MEDIA_TYPE_AUDIO);
Jonas Oreland49ac5952018-09-26 16:04:32 +02002669 auto sources = receiver->GetSources();
hbos8d609f62017-04-10 07:39:05 -07002670 ASSERT_GT(receiver->GetParameters().encodings.size(), 0u);
2671 EXPECT_EQ(receiver->GetParameters().encodings[0].ssrc,
Jonas Oreland49ac5952018-09-26 16:04:32 +02002672 sources[0].source_id());
2673 EXPECT_EQ(webrtc::RtpSourceType::SSRC, sources[0].source_type());
2674}
2675
2676TEST_P(PeerConnectionIntegrationTest, GetSourcesVideo) {
2677 ASSERT_TRUE(CreatePeerConnectionWrappers());
2678 ConnectFakeSignaling();
2679 caller()->AddVideoTrack();
2680 caller()->CreateAndSetAndSignalOffer();
2681 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2682 // Wait for one video frame to be received by the callee.
2683 MediaExpectations media_expectations;
2684 media_expectations.CalleeExpectsSomeVideo(1);
2685 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2686 ASSERT_EQ(callee()->pc()->GetReceivers().size(), 1u);
2687 auto receiver = callee()->pc()->GetReceivers()[0];
2688 ASSERT_EQ(receiver->media_type(), cricket::MEDIA_TYPE_VIDEO);
2689 auto sources = receiver->GetSources();
2690 ASSERT_GT(receiver->GetParameters().encodings.size(), 0u);
Yves Gereyf781bb52019-07-23 19:15:39 +02002691 ASSERT_GT(sources.size(), 0u);
Jonas Oreland49ac5952018-09-26 16:04:32 +02002692 EXPECT_EQ(receiver->GetParameters().encodings[0].ssrc,
2693 sources[0].source_id());
2694 EXPECT_EQ(webrtc::RtpSourceType::SSRC, sources[0].source_type());
hbos8d609f62017-04-10 07:39:05 -07002695}
2696
deadbeef2f425aa2017-04-14 10:41:32 -07002697// Test that if a track is removed and added again with a different stream ID,
2698// the new stream ID is successfully communicated in SDP and media continues to
2699// flow end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002700// TODO(webrtc.bugs.org/8734): This test does not work for Unified Plan because
2701// it will not reuse a transceiver that has already been sending. After creating
2702// a new transceiver it tries to create an offer with two senders of the same
2703// track ids and it fails.
2704TEST_F(PeerConnectionIntegrationTestPlanB, RemoveAndAddTrackWithNewStreamId) {
deadbeef2f425aa2017-04-14 10:41:32 -07002705 ASSERT_TRUE(CreatePeerConnectionWrappers());
2706 ConnectFakeSignaling();
2707
deadbeef2f425aa2017-04-14 10:41:32 -07002708 // Add track using stream 1, do offer/answer.
2709 rtc::scoped_refptr<webrtc::AudioTrackInterface> track =
2710 caller()->CreateLocalAudioTrack();
2711 rtc::scoped_refptr<webrtc::RtpSenderInterface> sender =
Steve Antond78323f2018-07-11 11:13:44 -07002712 caller()->AddTrack(track, {"stream_1"});
deadbeef2f425aa2017-04-14 10:41:32 -07002713 caller()->CreateAndSetAndSignalOffer();
2714 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002715 {
2716 MediaExpectations media_expectations;
2717 media_expectations.CalleeExpectsSomeAudio(1);
2718 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2719 }
deadbeef2f425aa2017-04-14 10:41:32 -07002720 // Remove the sender, and create a new one with the new stream.
Harald Alvestrand93dd7632022-01-19 12:28:45 +00002721 caller()->pc()->RemoveTrackOrError(sender);
Steve Antond78323f2018-07-11 11:13:44 -07002722 sender = caller()->AddTrack(track, {"stream_2"});
deadbeef2f425aa2017-04-14 10:41:32 -07002723 caller()->CreateAndSetAndSignalOffer();
2724 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2725 // Wait for additional audio frames to be received by the callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002726 {
2727 MediaExpectations media_expectations;
2728 media_expectations.CalleeExpectsSomeAudio();
2729 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2730 }
deadbeef2f425aa2017-04-14 10:41:32 -07002731}
2732
Seth Hampson2f0d7022018-02-20 11:54:42 -08002733TEST_P(PeerConnectionIntegrationTest, RtcEventLogOutputWriteCalled) {
Elad Alon99c3fe52017-10-13 16:29:40 +02002734 ASSERT_TRUE(CreatePeerConnectionWrappers());
2735 ConnectFakeSignaling();
2736
Mirko Bonadei317a1f02019-09-17 17:06:18 +02002737 auto output = std::make_unique<testing::NiceMock<MockRtcEventLogOutput>>();
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002738 ON_CALL(*output, IsActive()).WillByDefault(::testing::Return(true));
2739 ON_CALL(*output, Write(::testing::_)).WillByDefault(::testing::Return(true));
Elad Alon99c3fe52017-10-13 16:29:40 +02002740 EXPECT_CALL(*output, Write(::testing::_)).Times(::testing::AtLeast(1));
Bjorn Tereliusde939432017-11-20 17:38:14 +01002741 EXPECT_TRUE(caller()->pc()->StartRtcEventLog(
2742 std::move(output), webrtc::RtcEventLog::kImmediateOutput));
Elad Alon99c3fe52017-10-13 16:29:40 +02002743
Steve Anton15324772018-01-16 10:26:49 -08002744 caller()->AddAudioVideoTracks();
Elad Alon99c3fe52017-10-13 16:29:40 +02002745 caller()->CreateAndSetAndSignalOffer();
2746 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2747}
2748
Steve Antonede9ca52017-10-16 13:04:27 -07002749// Test that if candidates are only signaled by applying full session
2750// descriptions (instead of using AddIceCandidate), the peers can connect to
2751// each other and exchange media.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002752TEST_P(PeerConnectionIntegrationTest, MediaFlowsWhenCandidatesSetOnlyInSdp) {
Steve Antonede9ca52017-10-16 13:04:27 -07002753 ASSERT_TRUE(CreatePeerConnectionWrappers());
2754 // Each side will signal the session descriptions but not candidates.
2755 ConnectFakeSignalingForSdpOnly();
2756
2757 // Add audio video track and exchange the initial offer/answer with media
2758 // information only. This will start ICE gathering on each side.
Steve Anton15324772018-01-16 10:26:49 -08002759 caller()->AddAudioVideoTracks();
2760 callee()->AddAudioVideoTracks();
Steve Antonede9ca52017-10-16 13:04:27 -07002761 caller()->CreateAndSetAndSignalOffer();
2762
2763 // Wait for all candidates to be gathered on both the caller and callee.
2764 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceGatheringComplete,
2765 caller()->ice_gathering_state(), kDefaultTimeout);
2766 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceGatheringComplete,
2767 callee()->ice_gathering_state(), kDefaultTimeout);
2768
2769 // The candidates will now be included in the session description, so
2770 // signaling them will start the ICE connection.
2771 caller()->CreateAndSetAndSignalOffer();
2772 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2773
2774 // Ensure that media flows in both directions.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002775 MediaExpectations media_expectations;
2776 media_expectations.ExpectBidirectionalAudioAndVideo();
2777 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Antonede9ca52017-10-16 13:04:27 -07002778}
2779
Harald Alvestrandec23d6d2021-02-11 10:47:22 +00002780#if !defined(THREAD_SANITIZER)
2781// These tests provokes TSAN errors. See bugs.webrtc.org/11305.
2782
henrika5f6bf242017-11-01 11:06:56 +01002783// Test that SetAudioPlayout can be used to disable audio playout from the
2784// start, then later enable it. This may be useful, for example, if the caller
2785// needs to play a local ringtone until some event occurs, after which it
2786// switches to playing the received audio.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002787TEST_P(PeerConnectionIntegrationTest, DisableAndEnableAudioPlayout) {
henrika5f6bf242017-11-01 11:06:56 +01002788 ASSERT_TRUE(CreatePeerConnectionWrappers());
2789 ConnectFakeSignaling();
2790
2791 // Set up audio-only call where audio playout is disabled on caller's side.
2792 caller()->pc()->SetAudioPlayout(false);
Steve Anton15324772018-01-16 10:26:49 -08002793 caller()->AddAudioTrack();
2794 callee()->AddAudioTrack();
henrika5f6bf242017-11-01 11:06:56 +01002795 caller()->CreateAndSetAndSignalOffer();
2796 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2797
2798 // Pump messages for a second.
2799 WAIT(false, 1000);
2800 // Since audio playout is disabled, the caller shouldn't have received
2801 // anything (at the playout level, at least).
2802 EXPECT_EQ(0, caller()->audio_frames_received());
2803 // As a sanity check, make sure the callee (for which playout isn't disabled)
2804 // did still see frames on its audio level.
2805 ASSERT_GT(callee()->audio_frames_received(), 0);
2806
2807 // Enable playout again, and ensure audio starts flowing.
2808 caller()->pc()->SetAudioPlayout(true);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002809 MediaExpectations media_expectations;
2810 media_expectations.ExpectBidirectionalAudio();
2811 ASSERT_TRUE(ExpectNewFrames(media_expectations));
henrika5f6bf242017-11-01 11:06:56 +01002812}
2813
Harald Alvestrand39993842021-02-17 09:05:31 +00002814double GetAudioEnergyStat(PeerConnectionIntegrationWrapper* pc) {
henrika5f6bf242017-11-01 11:06:56 +01002815 auto report = pc->NewGetStats();
2816 auto track_stats_list =
2817 report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
2818 const webrtc::RTCMediaStreamTrackStats* remote_track_stats = nullptr;
2819 for (const auto* track_stats : track_stats_list) {
2820 if (track_stats->remote_source.is_defined() &&
2821 *track_stats->remote_source) {
2822 remote_track_stats = track_stats;
2823 break;
2824 }
2825 }
2826
2827 if (!remote_track_stats->total_audio_energy.is_defined()) {
2828 return 0.0;
2829 }
2830 return *remote_track_stats->total_audio_energy;
2831}
2832
2833// Test that if audio playout is disabled via the SetAudioPlayout() method, then
2834// incoming audio is still processed and statistics are generated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002835TEST_P(PeerConnectionIntegrationTest,
henrika5f6bf242017-11-01 11:06:56 +01002836 DisableAudioPlayoutStillGeneratesAudioStats) {
2837 ASSERT_TRUE(CreatePeerConnectionWrappers());
2838 ConnectFakeSignaling();
2839
2840 // Set up audio-only call where playout is disabled but audio-processing is
2841 // still active.
Steve Anton15324772018-01-16 10:26:49 -08002842 caller()->AddAudioTrack();
2843 callee()->AddAudioTrack();
henrika5f6bf242017-11-01 11:06:56 +01002844 caller()->pc()->SetAudioPlayout(false);
2845
2846 caller()->CreateAndSetAndSignalOffer();
2847 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2848
2849 // Wait for the callee to receive audio stats.
2850 EXPECT_TRUE_WAIT(GetAudioEnergyStat(caller()) > 0, kMaxWaitForFramesMs);
2851}
2852
Harald Alvestrandec23d6d2021-02-11 10:47:22 +00002853#endif // !defined(THREAD_SANITIZER)
2854
henrika4f167df2017-11-01 14:45:55 +01002855// Test that SetAudioRecording can be used to disable audio recording from the
2856// start, then later enable it. This may be useful, for example, if the caller
2857// wants to ensure that no audio resources are active before a certain state
2858// is reached.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002859TEST_P(PeerConnectionIntegrationTest, DisableAndEnableAudioRecording) {
henrika4f167df2017-11-01 14:45:55 +01002860 ASSERT_TRUE(CreatePeerConnectionWrappers());
2861 ConnectFakeSignaling();
2862
2863 // Set up audio-only call where audio recording is disabled on caller's side.
2864 caller()->pc()->SetAudioRecording(false);
Steve Anton15324772018-01-16 10:26:49 -08002865 caller()->AddAudioTrack();
2866 callee()->AddAudioTrack();
henrika4f167df2017-11-01 14:45:55 +01002867 caller()->CreateAndSetAndSignalOffer();
2868 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2869
2870 // Pump messages for a second.
2871 WAIT(false, 1000);
2872 // Since caller has disabled audio recording, the callee shouldn't have
2873 // received anything.
2874 EXPECT_EQ(0, callee()->audio_frames_received());
2875 // As a sanity check, make sure the caller did still see frames on its
2876 // audio level since audio recording is enabled on the calle side.
2877 ASSERT_GT(caller()->audio_frames_received(), 0);
2878
2879 // Enable audio recording again, and ensure audio starts flowing.
2880 caller()->pc()->SetAudioRecording(true);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002881 MediaExpectations media_expectations;
2882 media_expectations.ExpectBidirectionalAudio();
2883 ASSERT_TRUE(ExpectNewFrames(media_expectations));
henrika4f167df2017-11-01 14:45:55 +01002884}
2885
Qingsi Wang7685e862018-06-11 20:15:46 -07002886TEST_P(PeerConnectionIntegrationTest,
2887 IceEventsGeneratedAndLoggedInRtcEventLog) {
2888 ASSERT_TRUE(CreatePeerConnectionWrappersWithFakeRtcEventLog());
2889 ConnectFakeSignaling();
2890 PeerConnectionInterface::RTCOfferAnswerOptions options;
2891 options.offer_to_receive_audio = 1;
2892 caller()->SetOfferAnswerOptions(options);
2893 caller()->CreateAndSetAndSignalOffer();
2894 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
2895 ASSERT_NE(nullptr, caller()->event_log_factory());
2896 ASSERT_NE(nullptr, callee()->event_log_factory());
2897 webrtc::FakeRtcEventLog* caller_event_log =
Danil Chapovalov4f281f12021-01-18 13:29:00 +01002898 caller()->event_log_factory()->last_log_created();
Qingsi Wang7685e862018-06-11 20:15:46 -07002899 webrtc::FakeRtcEventLog* callee_event_log =
Danil Chapovalov4f281f12021-01-18 13:29:00 +01002900 callee()->event_log_factory()->last_log_created();
Qingsi Wang7685e862018-06-11 20:15:46 -07002901 ASSERT_NE(nullptr, caller_event_log);
2902 ASSERT_NE(nullptr, callee_event_log);
2903 int caller_ice_config_count = caller_event_log->GetEventCount(
2904 webrtc::RtcEvent::Type::IceCandidatePairConfig);
2905 int caller_ice_event_count = caller_event_log->GetEventCount(
2906 webrtc::RtcEvent::Type::IceCandidatePairEvent);
2907 int callee_ice_config_count = callee_event_log->GetEventCount(
2908 webrtc::RtcEvent::Type::IceCandidatePairConfig);
2909 int callee_ice_event_count = callee_event_log->GetEventCount(
2910 webrtc::RtcEvent::Type::IceCandidatePairEvent);
2911 EXPECT_LT(0, caller_ice_config_count);
2912 EXPECT_LT(0, caller_ice_event_count);
2913 EXPECT_LT(0, callee_ice_config_count);
2914 EXPECT_LT(0, callee_ice_event_count);
2915}
2916
Qingsi Wangc129c352019-04-18 10:41:58 -07002917TEST_P(PeerConnectionIntegrationTest, RegatherAfterChangingIceTransportType) {
Qingsi Wangc129c352019-04-18 10:41:58 -07002918 static const rtc::SocketAddress turn_server_internal_address{"88.88.88.0",
2919 3478};
2920 static const rtc::SocketAddress turn_server_external_address{"88.88.88.1", 0};
2921
2922 CreateTurnServer(turn_server_internal_address, turn_server_external_address);
2923
2924 webrtc::PeerConnectionInterface::IceServer ice_server;
2925 ice_server.urls.push_back("turn:88.88.88.0:3478");
2926 ice_server.username = "test";
2927 ice_server.password = "test";
2928
2929 PeerConnectionInterface::RTCConfiguration caller_config;
2930 caller_config.servers.push_back(ice_server);
2931 caller_config.type = webrtc::PeerConnectionInterface::kRelay;
2932 caller_config.continual_gathering_policy = PeerConnection::GATHER_CONTINUALLY;
Qingsi Wang1fe119f2019-05-31 16:55:33 -07002933 caller_config.surface_ice_candidates_on_ice_transport_type_changed = true;
Qingsi Wangc129c352019-04-18 10:41:58 -07002934
2935 PeerConnectionInterface::RTCConfiguration callee_config;
2936 callee_config.servers.push_back(ice_server);
2937 callee_config.type = webrtc::PeerConnectionInterface::kRelay;
2938 callee_config.continual_gathering_policy = PeerConnection::GATHER_CONTINUALLY;
Qingsi Wang1fe119f2019-05-31 16:55:33 -07002939 callee_config.surface_ice_candidates_on_ice_transport_type_changed = true;
Qingsi Wangc129c352019-04-18 10:41:58 -07002940
2941 ASSERT_TRUE(
2942 CreatePeerConnectionWrappersWithConfig(caller_config, callee_config));
2943
2944 // Do normal offer/answer and wait for ICE to complete.
2945 ConnectFakeSignaling();
2946 caller()->AddAudioVideoTracks();
2947 callee()->AddAudioVideoTracks();
2948 caller()->CreateAndSetAndSignalOffer();
2949 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2950 // Since we are doing continual gathering, the ICE transport does not reach
2951 // kIceGatheringComplete (see
2952 // P2PTransportChannel::OnCandidatesAllocationDone), and consequently not
2953 // kIceConnectionComplete.
2954 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
2955 caller()->ice_connection_state(), kDefaultTimeout);
2956 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
2957 callee()->ice_connection_state(), kDefaultTimeout);
2958 // Note that we cannot use the metric
Artem Titovcfea2182021-08-10 01:22:31 +02002959 // `WebRTC.PeerConnection.CandidatePairType_UDP` in this test since this
Qingsi Wangc129c352019-04-18 10:41:58 -07002960 // metric is only populated when we reach kIceConnectionComplete in the
2961 // current implementation.
2962 EXPECT_EQ(cricket::RELAY_PORT_TYPE,
2963 caller()->last_candidate_gathered().type());
2964 EXPECT_EQ(cricket::RELAY_PORT_TYPE,
2965 callee()->last_candidate_gathered().type());
2966
2967 // Loosen the caller's candidate filter.
2968 caller_config = caller()->pc()->GetConfiguration();
2969 caller_config.type = webrtc::PeerConnectionInterface::kAll;
2970 caller()->pc()->SetConfiguration(caller_config);
2971 // We should have gathered a new host candidate.
2972 EXPECT_EQ_WAIT(cricket::LOCAL_PORT_TYPE,
2973 caller()->last_candidate_gathered().type(), kDefaultTimeout);
2974
2975 // Loosen the callee's candidate filter.
2976 callee_config = callee()->pc()->GetConfiguration();
2977 callee_config.type = webrtc::PeerConnectionInterface::kAll;
2978 callee()->pc()->SetConfiguration(callee_config);
2979 EXPECT_EQ_WAIT(cricket::LOCAL_PORT_TYPE,
2980 callee()->last_candidate_gathered().type(), kDefaultTimeout);
Jonas Orelande3096512020-05-27 09:01:05 +02002981
2982 // Create an offer and verify that it does not contain an ICE restart (i.e new
2983 // ice credentials).
2984 std::string caller_ufrag_pre_offer = caller()
2985 ->pc()
2986 ->local_description()
2987 ->description()
2988 ->transport_infos()[0]
2989 .description.ice_ufrag;
2990 caller()->CreateAndSetAndSignalOffer();
2991 std::string caller_ufrag_post_offer = caller()
2992 ->pc()
2993 ->local_description()
2994 ->description()
2995 ->transport_infos()[0]
2996 .description.ice_ufrag;
2997 EXPECT_EQ(caller_ufrag_pre_offer, caller_ufrag_post_offer);
Qingsi Wangc129c352019-04-18 10:41:58 -07002998}
2999
Eldar Relloda13ea22019-06-01 12:23:43 +03003000TEST_P(PeerConnectionIntegrationTest, OnIceCandidateError) {
Eldar Relloda13ea22019-06-01 12:23:43 +03003001 static const rtc::SocketAddress turn_server_internal_address{"88.88.88.0",
3002 3478};
3003 static const rtc::SocketAddress turn_server_external_address{"88.88.88.1", 0};
3004
3005 CreateTurnServer(turn_server_internal_address, turn_server_external_address);
3006
3007 webrtc::PeerConnectionInterface::IceServer ice_server;
3008 ice_server.urls.push_back("turn:88.88.88.0:3478");
3009 ice_server.username = "test";
3010 ice_server.password = "123";
3011
3012 PeerConnectionInterface::RTCConfiguration caller_config;
3013 caller_config.servers.push_back(ice_server);
3014 caller_config.type = webrtc::PeerConnectionInterface::kRelay;
3015 caller_config.continual_gathering_policy = PeerConnection::GATHER_CONTINUALLY;
3016
3017 PeerConnectionInterface::RTCConfiguration callee_config;
3018 callee_config.servers.push_back(ice_server);
3019 callee_config.type = webrtc::PeerConnectionInterface::kRelay;
3020 callee_config.continual_gathering_policy = PeerConnection::GATHER_CONTINUALLY;
3021
3022 ASSERT_TRUE(
3023 CreatePeerConnectionWrappersWithConfig(caller_config, callee_config));
3024
3025 // Do normal offer/answer and wait for ICE to complete.
3026 ConnectFakeSignaling();
3027 caller()->AddAudioVideoTracks();
3028 callee()->AddAudioVideoTracks();
3029 caller()->CreateAndSetAndSignalOffer();
3030 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3031 EXPECT_EQ_WAIT(401, caller()->error_event().error_code, kDefaultTimeout);
3032 EXPECT_EQ("Unauthorized", caller()->error_event().error_text);
3033 EXPECT_EQ("turn:88.88.88.0:3478?transport=udp", caller()->error_event().url);
Eldar Rello0095d372019-12-02 22:22:07 +02003034 EXPECT_NE(caller()->error_event().address, "");
Eldar Relloda13ea22019-06-01 12:23:43 +03003035}
3036
Eldar Rellofa8019c2020-05-14 11:59:33 +03003037TEST_P(PeerConnectionIntegrationTest, OnIceCandidateErrorWithEmptyAddress) {
3038 webrtc::PeerConnectionInterface::IceServer ice_server;
3039 ice_server.urls.push_back("turn:127.0.0.1:3478?transport=tcp");
3040 ice_server.username = "test";
3041 ice_server.password = "test";
3042
3043 PeerConnectionInterface::RTCConfiguration caller_config;
3044 caller_config.servers.push_back(ice_server);
3045 caller_config.type = webrtc::PeerConnectionInterface::kRelay;
3046 caller_config.continual_gathering_policy = PeerConnection::GATHER_CONTINUALLY;
3047
3048 PeerConnectionInterface::RTCConfiguration callee_config;
3049 callee_config.servers.push_back(ice_server);
3050 callee_config.type = webrtc::PeerConnectionInterface::kRelay;
3051 callee_config.continual_gathering_policy = PeerConnection::GATHER_CONTINUALLY;
3052
3053 ASSERT_TRUE(
3054 CreatePeerConnectionWrappersWithConfig(caller_config, callee_config));
3055
3056 // Do normal offer/answer and wait for ICE to complete.
3057 ConnectFakeSignaling();
3058 caller()->AddAudioVideoTracks();
3059 callee()->AddAudioVideoTracks();
3060 caller()->CreateAndSetAndSignalOffer();
3061 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3062 EXPECT_EQ_WAIT(701, caller()->error_event().error_code, kDefaultTimeout);
3063 EXPECT_EQ(caller()->error_event().address, "");
3064}
3065
Eldar Rello5ab79e62019-10-09 18:29:44 +03003066TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
3067 AudioKeepsFlowingAfterImplicitRollback) {
3068 PeerConnectionInterface::RTCConfiguration config;
3069 config.sdp_semantics = SdpSemantics::kUnifiedPlan;
3070 config.enable_implicit_rollback = true;
3071 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
3072 ConnectFakeSignaling();
3073 caller()->AddAudioTrack();
3074 callee()->AddAudioTrack();
3075 caller()->CreateAndSetAndSignalOffer();
3076 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3077 MediaExpectations media_expectations;
3078 media_expectations.ExpectBidirectionalAudio();
3079 ASSERT_TRUE(ExpectNewFrames(media_expectations));
3080 SetSignalIceCandidates(false); // Workaround candidate outrace sdp.
3081 caller()->AddVideoTrack();
3082 callee()->AddVideoTrack();
Tommi87f70902021-04-27 14:43:08 +02003083 auto observer = rtc::make_ref_counted<MockSetSessionDescriptionObserver>();
Niels Möllerafb246b2022-04-20 14:26:50 +02003084 callee()->pc()->SetLocalDescription(observer.get(),
Eldar Rello5ab79e62019-10-09 18:29:44 +03003085 callee()->CreateOfferAndWait().release());
3086 EXPECT_TRUE_WAIT(observer->called(), kDefaultTimeout);
3087 caller()->CreateAndSetAndSignalOffer(); // Implicit rollback.
3088 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3089 ASSERT_TRUE(ExpectNewFrames(media_expectations));
3090}
3091
3092TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
3093 ImplicitRollbackVisitsStableState) {
3094 RTCConfiguration config;
3095 config.sdp_semantics = SdpSemantics::kUnifiedPlan;
3096 config.enable_implicit_rollback = true;
3097
3098 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
3099
Tommi87f70902021-04-27 14:43:08 +02003100 auto sld_observer =
3101 rtc::make_ref_counted<MockSetSessionDescriptionObserver>();
Niels Möllerafb246b2022-04-20 14:26:50 +02003102 callee()->pc()->SetLocalDescription(sld_observer.get(),
Eldar Rello5ab79e62019-10-09 18:29:44 +03003103 callee()->CreateOfferAndWait().release());
3104 EXPECT_TRUE_WAIT(sld_observer->called(), kDefaultTimeout);
3105 EXPECT_EQ(sld_observer->error(), "");
3106
Tommi87f70902021-04-27 14:43:08 +02003107 auto srd_observer =
3108 rtc::make_ref_counted<MockSetSessionDescriptionObserver>();
Eldar Rello5ab79e62019-10-09 18:29:44 +03003109 callee()->pc()->SetRemoteDescription(
Niels Möllerafb246b2022-04-20 14:26:50 +02003110 srd_observer.get(), caller()->CreateOfferAndWait().release());
Eldar Rello5ab79e62019-10-09 18:29:44 +03003111 EXPECT_TRUE_WAIT(srd_observer->called(), kDefaultTimeout);
3112 EXPECT_EQ(srd_observer->error(), "");
3113
3114 EXPECT_THAT(callee()->peer_connection_signaling_state_history(),
3115 ElementsAre(PeerConnectionInterface::kHaveLocalOffer,
3116 PeerConnectionInterface::kStable,
3117 PeerConnectionInterface::kHaveRemoteOffer));
3118}
3119
Eldar Rellobd9c33a2020-10-01 17:52:45 +03003120TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
3121 H264FmtpSpsPpsIdrInKeyframeParameterUsage) {
3122 ASSERT_TRUE(CreatePeerConnectionWrappers());
3123 ConnectFakeSignaling();
3124 caller()->AddVideoTrack();
3125 callee()->AddVideoTrack();
3126 auto munger = [](cricket::SessionDescription* desc) {
3127 cricket::VideoContentDescription* video =
3128 GetFirstVideoContentDescription(desc);
3129 auto codecs = video->codecs();
3130 for (auto&& codec : codecs) {
3131 if (codec.name == "H264") {
3132 std::string value;
3133 // The parameter is not supposed to be present in SDP by default.
3134 EXPECT_FALSE(
3135 codec.GetParam(cricket::kH264FmtpSpsPpsIdrInKeyframe, &value));
3136 codec.SetParam(std::string(cricket::kH264FmtpSpsPpsIdrInKeyframe),
3137 std::string(""));
3138 }
3139 }
3140 video->set_codecs(codecs);
3141 };
3142 // Munge local offer for SLD.
3143 caller()->SetGeneratedSdpMunger(munger);
3144 // Munge remote answer for SRD.
3145 caller()->SetReceivedSdpMunger(munger);
3146 caller()->CreateAndSetAndSignalOffer();
3147 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3148 // Observe that after munging the parameter is present in generated SDP.
3149 caller()->SetGeneratedSdpMunger([](cricket::SessionDescription* desc) {
3150 cricket::VideoContentDescription* video =
3151 GetFirstVideoContentDescription(desc);
3152 for (auto&& codec : video->codecs()) {
3153 if (codec.name == "H264") {
3154 std::string value;
3155 EXPECT_TRUE(
3156 codec.GetParam(cricket::kH264FmtpSpsPpsIdrInKeyframe, &value));
3157 }
3158 }
3159 });
3160 caller()->CreateOfferAndWait();
3161}
3162
Harald Alvestrand1a9be302020-12-11 14:53:59 +00003163TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
Harald Alvestrand94324f22021-01-13 12:31:53 +00003164 RenegotiateManyAudioTransceivers) {
Harald Alvestrand1a9be302020-12-11 14:53:59 +00003165 PeerConnectionInterface::RTCConfiguration config;
3166 config.sdp_semantics = SdpSemantics::kUnifiedPlan;
3167 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
3168 ConnectFakeSignaling();
3169 caller()->pc()->AddTransceiver(cricket::MEDIA_TYPE_AUDIO);
3170
3171 caller()->CreateAndSetAndSignalOffer();
3172 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3173 int current_size = caller()->pc()->GetTransceivers().size();
3174 // Add more tracks until we get close to having issues.
3175 // Issues have been seen at:
3176 // - 32 tracks on android_arm64_rel and android_arm_dbg bots
Harald Alvestrandcc6ae442021-01-18 08:06:23 +00003177 // - 16 tracks on android_arm_dbg (flaky)
3178 while (current_size < 8) {
Harald Alvestrand1a9be302020-12-11 14:53:59 +00003179 // Double the number of tracks
3180 for (int i = 0; i < current_size; i++) {
3181 caller()->pc()->AddTransceiver(cricket::MEDIA_TYPE_AUDIO);
3182 }
3183 current_size = caller()->pc()->GetTransceivers().size();
3184 RTC_LOG(LS_INFO) << "Renegotiating with " << current_size << " tracks";
3185 auto start_time_ms = rtc::TimeMillis();
3186 caller()->CreateAndSetAndSignalOffer();
3187 // We want to stop when the time exceeds one second.
3188 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3189 auto elapsed_time_ms = rtc::TimeMillis() - start_time_ms;
3190 RTC_LOG(LS_INFO) << "Renegotiating took " << elapsed_time_ms << " ms";
3191 ASSERT_GT(1000, elapsed_time_ms)
3192 << "Audio transceivers: Negotiation took too long after "
3193 << current_size << " tracks added";
3194 }
3195}
3196
3197TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
3198 RenegotiateManyVideoTransceivers) {
3199 PeerConnectionInterface::RTCConfiguration config;
3200 config.sdp_semantics = SdpSemantics::kUnifiedPlan;
3201 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
3202 ConnectFakeSignaling();
3203 caller()->pc()->AddTransceiver(cricket::MEDIA_TYPE_VIDEO);
3204
3205 caller()->CreateAndSetAndSignalOffer();
3206 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3207 int current_size = caller()->pc()->GetTransceivers().size();
3208 // Add more tracks until we get close to having issues.
3209 // Issues have been seen at:
3210 // - 96 on a Linux workstation
3211 // - 64 at win_x86_more_configs and win_x64_msvc_dbg
3212 // - 32 on android_arm64_rel and linux_dbg bots
Harald Alvestrand785e23b2021-03-15 21:26:27 +00003213 // - 16 on Android 64 (Nexus 5x)
3214 while (current_size < 8) {
Harald Alvestrand1a9be302020-12-11 14:53:59 +00003215 // Double the number of tracks
3216 for (int i = 0; i < current_size; i++) {
3217 caller()->pc()->AddTransceiver(cricket::MEDIA_TYPE_VIDEO);
3218 }
3219 current_size = caller()->pc()->GetTransceivers().size();
3220 RTC_LOG(LS_INFO) << "Renegotiating with " << current_size << " tracks";
3221 auto start_time_ms = rtc::TimeMillis();
3222 caller()->CreateAndSetAndSignalOffer();
3223 // We want to stop when the time exceeds one second.
3224 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3225 auto elapsed_time_ms = rtc::TimeMillis() - start_time_ms;
3226 RTC_LOG(LS_INFO) << "Renegotiating took " << elapsed_time_ms << " ms";
3227 ASSERT_GT(1000, elapsed_time_ms)
3228 << "Video transceivers: Negotiation took too long after "
3229 << current_size << " tracks added";
3230 }
3231}
3232
Harald Alvestrand94324f22021-01-13 12:31:53 +00003233TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
3234 RenegotiateManyVideoTransceiversAndWatchAudioDelay) {
3235 PeerConnectionInterface::RTCConfiguration config;
3236 config.sdp_semantics = SdpSemantics::kUnifiedPlan;
3237 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
3238 ConnectFakeSignaling();
3239 caller()->AddAudioTrack();
Harald Alvestrandcc6ae442021-01-18 08:06:23 +00003240 callee()->AddAudioTrack();
Harald Alvestrand94324f22021-01-13 12:31:53 +00003241 caller()->CreateAndSetAndSignalOffer();
3242 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3243 // Wait until we can see the audio flowing.
3244 MediaExpectations media_expectations;
3245 media_expectations.CalleeExpectsSomeAudio();
3246 ASSERT_TRUE(ExpectNewFrames(media_expectations));
3247
Harald Alvestrandcc6ae442021-01-18 08:06:23 +00003248 // Get the baseline numbers for audio_packets and audio_delay
3249 // in both directions.
3250 caller()->StartWatchingDelayStats();
3251 callee()->StartWatchingDelayStats();
Harald Alvestrand94324f22021-01-13 12:31:53 +00003252
3253 int current_size = caller()->pc()->GetTransceivers().size();
3254 // Add more tracks until we get close to having issues.
3255 // Making this number very large makes the test very slow.
Harald Alvestrandcc6ae442021-01-18 08:06:23 +00003256 while (current_size < 16) {
Harald Alvestrand94324f22021-01-13 12:31:53 +00003257 // Double the number of tracks
3258 for (int i = 0; i < current_size; i++) {
3259 caller()->pc()->AddTransceiver(cricket::MEDIA_TYPE_VIDEO);
3260 }
3261 current_size = caller()->pc()->GetTransceivers().size();
3262 RTC_LOG(LS_INFO) << "Renegotiating with " << current_size << " tracks";
3263 auto start_time_ms = rtc::TimeMillis();
3264 caller()->CreateAndSetAndSignalOffer();
3265 // We want to stop when the time exceeds one second.
3266 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3267 auto elapsed_time_ms = rtc::TimeMillis() - start_time_ms;
3268 RTC_LOG(LS_INFO) << "Renegotiating took " << elapsed_time_ms << " ms";
3269 // This is a guard against the test using excessive amounts of time.
3270 ASSERT_GT(5000, elapsed_time_ms)
3271 << "Video transceivers: Negotiation took too long after "
3272 << current_size << " tracks added";
Harald Alvestrandcc6ae442021-01-18 08:06:23 +00003273 caller()->UpdateDelayStats("caller reception", current_size);
3274 callee()->UpdateDelayStats("callee reception", current_size);
Harald Alvestrand94324f22021-01-13 12:31:53 +00003275 }
3276}
3277
Evan Shrubsole7619b7c2022-03-01 10:42:44 +01003278INSTANTIATE_TEST_SUITE_P(
3279 PeerConnectionIntegrationTest,
3280 PeerConnectionIntegrationTest,
Florent Castelli15a38de2022-04-06 00:38:21 +02003281 Combine(Values(SdpSemantics::kPlanB_DEPRECATED, SdpSemantics::kUnifiedPlan),
Evan Shrubsole7619b7c2022-03-01 10:42:44 +01003282 Values("WebRTC-FrameBuffer3/arm:FrameBuffer2/",
3283 "WebRTC-FrameBuffer3/arm:FrameBuffer3/",
3284 "WebRTC-FrameBuffer3/arm:SyncDecoding/")));
Steve Antond3679212018-01-17 17:41:02 -08003285
Evan Shrubsole7619b7c2022-03-01 10:42:44 +01003286INSTANTIATE_TEST_SUITE_P(
3287 PeerConnectionIntegrationTest,
3288 PeerConnectionIntegrationTestWithFakeClock,
Florent Castelli15a38de2022-04-06 00:38:21 +02003289 Combine(Values(SdpSemantics::kPlanB_DEPRECATED, SdpSemantics::kUnifiedPlan),
Evan Shrubsole7619b7c2022-03-01 10:42:44 +01003290 Values("WebRTC-FrameBuffer3/arm:FrameBuffer2/",
3291 "WebRTC-FrameBuffer3/arm:FrameBuffer3/",
3292 "WebRTC-FrameBuffer3/arm:SyncDecoding/")));
Yves Gerey100fe632020-01-17 19:15:53 +01003293
Steve Anton74255ff2018-01-24 18:32:57 -08003294// Tests that verify interoperability between Plan B and Unified Plan
3295// PeerConnections.
3296class PeerConnectionIntegrationInteropTest
Seth Hampson2f0d7022018-02-20 11:54:42 -08003297 : public PeerConnectionIntegrationBaseTest,
Steve Anton74255ff2018-01-24 18:32:57 -08003298 public ::testing::WithParamInterface<
3299 std::tuple<SdpSemantics, SdpSemantics>> {
3300 protected:
Seth Hampson2f0d7022018-02-20 11:54:42 -08003301 // Setting the SdpSemantics for the base test to kDefault does not matter
3302 // because we specify not to use the test semantics when creating
Harald Alvestrand39993842021-02-17 09:05:31 +00003303 // PeerConnectionIntegrationWrappers.
Steve Anton74255ff2018-01-24 18:32:57 -08003304 PeerConnectionIntegrationInteropTest()
Florent Castelli15a38de2022-04-06 00:38:21 +02003305 : PeerConnectionIntegrationBaseTest(SdpSemantics::kPlanB_DEPRECATED),
Seth Hampson2f0d7022018-02-20 11:54:42 -08003306 caller_semantics_(std::get<0>(GetParam())),
Steve Anton74255ff2018-01-24 18:32:57 -08003307 callee_semantics_(std::get<1>(GetParam())) {}
3308
3309 bool CreatePeerConnectionWrappersWithSemantics() {
Steve Anton3acffc32018-04-12 17:21:03 -07003310 return CreatePeerConnectionWrappersWithSdpSemantics(caller_semantics_,
3311 callee_semantics_);
Steve Anton74255ff2018-01-24 18:32:57 -08003312 }
3313
3314 const SdpSemantics caller_semantics_;
3315 const SdpSemantics callee_semantics_;
3316};
3317
3318TEST_P(PeerConnectionIntegrationInteropTest, NoMediaLocalToNoMediaRemote) {
3319 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
3320 ConnectFakeSignaling();
3321
3322 caller()->CreateAndSetAndSignalOffer();
3323 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3324}
3325
3326TEST_P(PeerConnectionIntegrationInteropTest, OneAudioLocalToNoMediaRemote) {
3327 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
3328 ConnectFakeSignaling();
3329 auto audio_sender = caller()->AddAudioTrack();
3330
3331 caller()->CreateAndSetAndSignalOffer();
3332 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3333
3334 // Verify that one audio receiver has been created on the remote and that it
3335 // has the same track ID as the sending track.
3336 auto receivers = callee()->pc()->GetReceivers();
3337 ASSERT_EQ(1u, receivers.size());
3338 EXPECT_EQ(cricket::MEDIA_TYPE_AUDIO, receivers[0]->media_type());
3339 EXPECT_EQ(receivers[0]->track()->id(), audio_sender->track()->id());
3340
Seth Hampson2f0d7022018-02-20 11:54:42 -08003341 MediaExpectations media_expectations;
3342 media_expectations.CalleeExpectsSomeAudio();
3343 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08003344}
3345
3346TEST_P(PeerConnectionIntegrationInteropTest, OneAudioOneVideoToNoMediaRemote) {
3347 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
3348 ConnectFakeSignaling();
3349 auto video_sender = caller()->AddVideoTrack();
3350 auto audio_sender = caller()->AddAudioTrack();
3351
3352 caller()->CreateAndSetAndSignalOffer();
3353 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3354
3355 // Verify that one audio and one video receiver have been created on the
3356 // remote and that they have the same track IDs as the sending tracks.
3357 auto audio_receivers =
3358 callee()->GetReceiversOfType(cricket::MEDIA_TYPE_AUDIO);
3359 ASSERT_EQ(1u, audio_receivers.size());
3360 EXPECT_EQ(audio_receivers[0]->track()->id(), audio_sender->track()->id());
3361 auto video_receivers =
3362 callee()->GetReceiversOfType(cricket::MEDIA_TYPE_VIDEO);
3363 ASSERT_EQ(1u, video_receivers.size());
3364 EXPECT_EQ(video_receivers[0]->track()->id(), video_sender->track()->id());
3365
Seth Hampson2f0d7022018-02-20 11:54:42 -08003366 MediaExpectations media_expectations;
3367 media_expectations.CalleeExpectsSomeAudioAndVideo();
3368 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08003369}
3370
3371TEST_P(PeerConnectionIntegrationInteropTest,
3372 OneAudioOneVideoLocalToOneAudioOneVideoRemote) {
3373 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
3374 ConnectFakeSignaling();
3375 caller()->AddAudioVideoTracks();
3376 callee()->AddAudioVideoTracks();
3377
3378 caller()->CreateAndSetAndSignalOffer();
3379 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3380
Seth Hampson2f0d7022018-02-20 11:54:42 -08003381 MediaExpectations media_expectations;
3382 media_expectations.ExpectBidirectionalAudioAndVideo();
3383 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08003384}
3385
3386TEST_P(PeerConnectionIntegrationInteropTest,
3387 ReverseRolesOneAudioLocalToOneVideoRemote) {
3388 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
3389 ConnectFakeSignaling();
3390 caller()->AddAudioTrack();
3391 callee()->AddVideoTrack();
3392
3393 caller()->CreateAndSetAndSignalOffer();
3394 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3395
3396 // Verify that only the audio track has been negotiated.
3397 EXPECT_EQ(0u, caller()->GetReceiversOfType(cricket::MEDIA_TYPE_VIDEO).size());
3398 // Might also check that the callee's NegotiationNeeded flag is set.
3399
3400 // Reverse roles.
3401 callee()->CreateAndSetAndSignalOffer();
3402 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3403
Seth Hampson2f0d7022018-02-20 11:54:42 -08003404 MediaExpectations media_expectations;
3405 media_expectations.CallerExpectsSomeVideo();
3406 media_expectations.CalleeExpectsSomeAudio();
3407 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08003408}
3409
Taylor Brandstetter1c7ecef2021-08-11 12:38:35 -07003410TEST_P(PeerConnectionIntegrationTest, NewTracksDoNotCauseNewCandidates) {
3411 ASSERT_TRUE(CreatePeerConnectionWrappers());
3412 ConnectFakeSignaling();
3413 caller()->AddAudioVideoTracks();
3414 caller()->CreateAndSetAndSignalOffer();
3415 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3416 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
3417 caller()->ExpectCandidates(0);
3418 callee()->ExpectCandidates(0);
3419 caller()->AddAudioTrack();
3420 caller()->CreateAndSetAndSignalOffer();
3421 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3422}
3423
Harald Alvestrand35ba0c52022-05-05 07:37:41 +00003424TEST_P(PeerConnectionIntegrationTest, MediaCallWithoutMediaEngineFails) {
3425 ASSERT_TRUE(CreatePeerConnectionWrappersWithoutMediaEngine());
3426 // AddTrack should fail.
3427 EXPECT_FALSE(
3428 caller()->pc()->AddTrack(caller()->CreateLocalAudioTrack(), {}).ok());
3429}
3430
Mirko Bonadeic84f6612019-01-31 12:20:57 +01003431INSTANTIATE_TEST_SUITE_P(
Steve Antonba42e992018-04-09 14:10:01 -07003432 PeerConnectionIntegrationTest,
3433 PeerConnectionIntegrationInteropTest,
Florent Castelli15a38de2022-04-06 00:38:21 +02003434 Values(std::make_tuple(SdpSemantics::kPlanB_DEPRECATED,
3435 SdpSemantics::kUnifiedPlan),
3436 std::make_tuple(SdpSemantics::kUnifiedPlan,
3437 SdpSemantics::kPlanB_DEPRECATED)));
Steve Antonba42e992018-04-09 14:10:01 -07003438
3439// Test that if the Unified Plan side offers two video tracks then the Plan B
3440// side will only see the first one and ignore the second.
3441TEST_F(PeerConnectionIntegrationTestPlanB, TwoVideoUnifiedPlanToNoMediaPlanB) {
Steve Anton3acffc32018-04-12 17:21:03 -07003442 ASSERT_TRUE(CreatePeerConnectionWrappersWithSdpSemantics(
Florent Castelli15a38de2022-04-06 00:38:21 +02003443 SdpSemantics::kUnifiedPlan, SdpSemantics::kPlanB_DEPRECATED));
Steve Anton74255ff2018-01-24 18:32:57 -08003444 ConnectFakeSignaling();
3445 auto first_sender = caller()->AddVideoTrack();
3446 caller()->AddVideoTrack();
3447
3448 caller()->CreateAndSetAndSignalOffer();
3449 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3450
3451 // Verify that there is only one receiver and it corresponds to the first
3452 // added track.
3453 auto receivers = callee()->pc()->GetReceivers();
3454 ASSERT_EQ(1u, receivers.size());
3455 EXPECT_TRUE(receivers[0]->track()->enabled());
3456 EXPECT_EQ(first_sender->track()->id(), receivers[0]->track()->id());
3457
Seth Hampson2f0d7022018-02-20 11:54:42 -08003458 MediaExpectations media_expectations;
3459 media_expectations.CalleeExpectsSomeVideo();
3460 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08003461}
3462
Steve Anton2bed3972019-01-04 17:04:30 -08003463// Test that if the initial offer tagged BUNDLE section is rejected due to its
3464// associated RtpTransceiver being stopped and another transceiver is added,
3465// then renegotiation causes the callee to receive the new video track without
3466// error.
3467// This is a regression test for bugs.webrtc.org/9954
3468TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
3469 ReOfferWithStoppedBundleTaggedTransceiver) {
3470 RTCConfiguration config;
3471 config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
3472 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
3473 ConnectFakeSignaling();
3474 auto audio_transceiver_or_error =
3475 caller()->pc()->AddTransceiver(caller()->CreateLocalAudioTrack());
3476 ASSERT_TRUE(audio_transceiver_or_error.ok());
3477 auto audio_transceiver = audio_transceiver_or_error.MoveValue();
3478
3479 caller()->CreateAndSetAndSignalOffer();
3480 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3481 {
3482 MediaExpectations media_expectations;
3483 media_expectations.CalleeExpectsSomeAudio();
3484 ASSERT_TRUE(ExpectNewFrames(media_expectations));
3485 }
3486
Harald Alvestrand6060df52020-08-11 09:54:02 +02003487 audio_transceiver->StopInternal();
Steve Anton2bed3972019-01-04 17:04:30 -08003488 caller()->pc()->AddTransceiver(caller()->CreateLocalVideoTrack());
3489
3490 caller()->CreateAndSetAndSignalOffer();
3491 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3492 {
3493 MediaExpectations media_expectations;
3494 media_expectations.CalleeExpectsSomeVideo();
3495 ASSERT_TRUE(ExpectNewFrames(media_expectations));
3496 }
3497}
3498
Harald Alvestrandbedb6052020-08-20 14:50:10 +02003499TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
3500 StopTransceiverRemovesDtlsTransports) {
3501 RTCConfiguration config;
3502 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
3503 ConnectFakeSignaling();
3504 auto audio_transceiver_or_error =
3505 caller()->pc()->AddTransceiver(caller()->CreateLocalAudioTrack());
3506 ASSERT_TRUE(audio_transceiver_or_error.ok());
3507 auto audio_transceiver = audio_transceiver_or_error.MoveValue();
3508
3509 caller()->CreateAndSetAndSignalOffer();
3510 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3511
3512 audio_transceiver->StopStandard();
3513 caller()->CreateAndSetAndSignalOffer();
3514 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3515 ASSERT_EQ(0U, caller()->pc()->GetTransceivers().size());
3516 EXPECT_EQ(PeerConnectionInterface::kIceGatheringNew,
3517 caller()->pc()->ice_gathering_state());
3518 EXPECT_THAT(caller()->ice_gathering_state_history(),
3519 ElementsAre(PeerConnectionInterface::kIceGatheringGathering,
3520 PeerConnectionInterface::kIceGatheringComplete,
3521 PeerConnectionInterface::kIceGatheringNew));
3522}
3523
Harald Alvestrand1ee33252020-09-24 13:31:15 +00003524TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
Harald Alvestrand45be0a92020-09-30 06:55:23 +00003525 StopTransceiverStopsAndRemovesTransceivers) {
3526 RTCConfiguration config;
3527 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
3528 ConnectFakeSignaling();
3529 auto audio_transceiver_or_error =
3530 caller()->pc()->AddTransceiver(caller()->CreateLocalAudioTrack());
3531 ASSERT_TRUE(audio_transceiver_or_error.ok());
3532 auto caller_transceiver = audio_transceiver_or_error.MoveValue();
3533
3534 caller()->CreateAndSetAndSignalOffer();
3535 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3536 caller_transceiver->StopStandard();
3537
3538 auto callee_transceiver = callee()->pc()->GetTransceivers()[0];
3539 caller()->CreateAndSetAndSignalOffer();
3540 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3541 EXPECT_EQ(0U, caller()->pc()->GetTransceivers().size());
3542 EXPECT_EQ(0U, callee()->pc()->GetTransceivers().size());
3543 EXPECT_EQ(0U, caller()->pc()->GetSenders().size());
3544 EXPECT_EQ(0U, callee()->pc()->GetSenders().size());
3545 EXPECT_EQ(0U, caller()->pc()->GetReceivers().size());
3546 EXPECT_EQ(0U, callee()->pc()->GetReceivers().size());
3547 EXPECT_TRUE(caller_transceiver->stopped());
3548 EXPECT_TRUE(callee_transceiver->stopped());
3549}
3550
3551TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
Harald Alvestrand1ee33252020-09-24 13:31:15 +00003552 StopTransceiverEndsIncomingAudioTrack) {
3553 RTCConfiguration config;
3554 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
3555 ConnectFakeSignaling();
3556 auto audio_transceiver_or_error =
3557 caller()->pc()->AddTransceiver(caller()->CreateLocalAudioTrack());
3558 ASSERT_TRUE(audio_transceiver_or_error.ok());
3559 auto audio_transceiver = audio_transceiver_or_error.MoveValue();
3560
3561 caller()->CreateAndSetAndSignalOffer();
3562 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3563 auto caller_track = audio_transceiver->receiver()->track();
3564 auto callee_track = callee()->pc()->GetReceivers()[0]->track();
3565 audio_transceiver->StopStandard();
3566 EXPECT_EQ(MediaStreamTrackInterface::TrackState::kEnded,
3567 caller_track->state());
3568 caller()->CreateAndSetAndSignalOffer();
3569 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3570 EXPECT_EQ(MediaStreamTrackInterface::TrackState::kEnded,
3571 callee_track->state());
3572}
3573
3574TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
3575 StopTransceiverEndsIncomingVideoTrack) {
3576 RTCConfiguration config;
3577 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
3578 ConnectFakeSignaling();
3579 auto audio_transceiver_or_error =
3580 caller()->pc()->AddTransceiver(caller()->CreateLocalVideoTrack());
3581 ASSERT_TRUE(audio_transceiver_or_error.ok());
3582 auto audio_transceiver = audio_transceiver_or_error.MoveValue();
3583
3584 caller()->CreateAndSetAndSignalOffer();
3585 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3586 auto caller_track = audio_transceiver->receiver()->track();
3587 auto callee_track = callee()->pc()->GetReceivers()[0]->track();
3588 audio_transceiver->StopStandard();
3589 EXPECT_EQ(MediaStreamTrackInterface::TrackState::kEnded,
3590 caller_track->state());
3591 caller()->CreateAndSetAndSignalOffer();
3592 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3593 EXPECT_EQ(MediaStreamTrackInterface::TrackState::kEnded,
3594 callee_track->state());
3595}
3596
Jonas Oreland65455162022-06-08 11:25:46 +02003597TEST_P(PeerConnectionIntegrationTest, EndToEndRtpSenderVideoEncoderSelector) {
3598 ASSERT_TRUE(
3599 CreateOneDirectionalPeerConnectionWrappers(/*caller_to_callee=*/true));
3600 ConnectFakeSignaling();
3601 // Add one-directional video, from caller to callee.
3602 rtc::scoped_refptr<webrtc::VideoTrackInterface> caller_track =
3603 caller()->CreateLocalVideoTrack();
3604 auto sender = caller()->AddTrack(caller_track);
3605 PeerConnectionInterface::RTCOfferAnswerOptions options;
3606 options.offer_to_receive_video = 0;
3607 caller()->SetOfferAnswerOptions(options);
3608 caller()->CreateAndSetAndSignalOffer();
3609 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3610 ASSERT_EQ(callee()->pc()->GetReceivers().size(), 1u);
3611
3612 std::unique_ptr<MockEncoderSelector> encoder_selector =
3613 std::make_unique<MockEncoderSelector>();
3614 EXPECT_CALL(*encoder_selector, OnCurrentEncoder);
3615
3616 sender->SetEncoderSelector(std::move(encoder_selector));
3617
3618 // Expect video to be received in one direction.
3619 MediaExpectations media_expectations;
3620 media_expectations.CallerExpectsNoVideo();
3621 media_expectations.CalleeExpectsSomeVideo();
3622
3623 EXPECT_TRUE(ExpectNewFrames(media_expectations));
3624}
3625
Harald Alvestrand89c40e22021-02-17 08:58:35 +00003626} // namespace
Harald Alvestrand39993842021-02-17 09:05:31 +00003627
Mirko Bonadeiab64e8a2018-12-12 12:10:18 +01003628} // namespace webrtc