blob: 4ec86b3af3986d78da894a1fd34c1e37746c6fb6 [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 Alvestrand39993842021-02-17 09:05:31 +000011#include <stdint.h>
deadbeef1dcb1642017-03-29 21:08:16 -070012
Taylor Brandstetterd3ef4992020-10-15 18:22:57 -070013#include <algorithm>
deadbeef1dcb1642017-03-29 21:08:16 -070014#include <memory>
Harald Alvestrand39993842021-02-17 09:05:31 +000015#include <string>
16#include <tuple>
deadbeef1dcb1642017-03-29 21:08:16 -070017#include <utility>
18#include <vector>
19
Steve Anton64b626b2019-01-28 17:25:26 -080020#include "absl/algorithm/container.h"
Harald Alvestrand39993842021-02-17 09:05:31 +000021#include "absl/types/optional.h"
22#include "api/async_resolver_factory.h"
23#include "api/candidate.h"
24#include "api/crypto/crypto_options.h"
25#include "api/dtmf_sender_interface.h"
26#include "api/ice_transport_interface.h"
27#include "api/jsep.h"
Steve Anton10542f22019-01-11 09:11:00 -080028#include "api/media_stream_interface.h"
Harald Alvestrand39993842021-02-17 09:05:31 +000029#include "api/media_types.h"
Steve Anton10542f22019-01-11 09:11:00 -080030#include "api/peer_connection_interface.h"
Harald Alvestrand39993842021-02-17 09:05:31 +000031#include "api/rtc_error.h"
32#include "api/rtc_event_log/rtc_event.h"
33#include "api/rtc_event_log/rtc_event_log.h"
34#include "api/rtc_event_log_output.h"
35#include "api/rtp_parameters.h"
Steve Anton10542f22019-01-11 09:11:00 -080036#include "api/rtp_receiver_interface.h"
Harald Alvestrand39993842021-02-17 09:05:31 +000037#include "api/rtp_sender_interface.h"
38#include "api/rtp_transceiver_direction.h"
39#include "api/rtp_transceiver_interface.h"
40#include "api/scoped_refptr.h"
41#include "api/stats/rtc_stats.h"
42#include "api/stats/rtc_stats_report.h"
43#include "api/stats/rtcstats_objects.h"
44#include "api/transport/rtp/rtp_source.h"
Steve Anton10542f22019-01-11 09:11:00 -080045#include "api/uma_metrics.h"
Harald Alvestrand39993842021-02-17 09:05:31 +000046#include "api/units/time_delta.h"
47#include "api/video/video_rotation.h"
48#include "logging/rtc_event_log/fake_rtc_event_log.h"
Qingsi Wang7685e862018-06-11 20:15:46 -070049#include "logging/rtc_event_log/fake_rtc_event_log_factory.h"
Harald Alvestrand39993842021-02-17 09:05:31 +000050#include "media/base/codec.h"
51#include "media/base/media_constants.h"
52#include "media/base/stream_params.h"
Steve Anton10542f22019-01-11 09:11:00 -080053#include "p2p/base/mock_async_resolver.h"
Harald Alvestrand39993842021-02-17 09:05:31 +000054#include "p2p/base/port.h"
55#include "p2p/base/port_allocator.h"
Steve Anton10542f22019-01-11 09:11:00 -080056#include "p2p/base/port_interface.h"
Harald Alvestrand39993842021-02-17 09:05:31 +000057#include "p2p/base/stun_server.h"
Steve Anton10542f22019-01-11 09:11:00 -080058#include "p2p/base/test_stun_server.h"
59#include "p2p/base/test_turn_customizer.h"
60#include "p2p/base/test_turn_server.h"
Harald Alvestrand39993842021-02-17 09:05:31 +000061#include "p2p/base/transport_description.h"
62#include "p2p/base/transport_info.h"
Steve Anton10542f22019-01-11 09:11:00 -080063#include "pc/media_session.h"
64#include "pc/peer_connection.h"
65#include "pc/peer_connection_factory.h"
Steve Anton10542f22019-01-11 09:11:00 -080066#include "pc/session_description.h"
Harald Alvestrand39993842021-02-17 09:05:31 +000067#include "pc/test/fake_periodic_video_source.h"
68#include "pc/test/integration_test_helpers.h"
Steve Anton10542f22019-01-11 09:11:00 -080069#include "pc/test/mock_peer_connection_observers.h"
Jonas Olssonb75d9e92019-02-22 10:33:29 +010070#include "rtc_base/fake_clock.h"
Qingsi Wangecd30542019-05-22 14:34:56 -070071#include "rtc_base/fake_mdns_responder.h"
Steve Anton10542f22019-01-11 09:11:00 -080072#include "rtc_base/fake_network.h"
73#include "rtc_base/firewall_socket_server.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020074#include "rtc_base/gunit.h"
Harald Alvestrand39993842021-02-17 09:05:31 +000075#include "rtc_base/helpers.h"
76#include "rtc_base/location.h"
77#include "rtc_base/logging.h"
78#include "rtc_base/ref_counted_object.h"
79#include "rtc_base/socket_address.h"
80#include "rtc_base/ssl_certificate.h"
81#include "rtc_base/ssl_fingerprint.h"
82#include "rtc_base/ssl_identity.h"
83#include "rtc_base/ssl_stream_adapter.h"
Steve Anton10542f22019-01-11 09:11:00 -080084#include "rtc_base/test_certificate_verifier.h"
Harald Alvestrand39993842021-02-17 09:05:31 +000085#include "rtc_base/thread.h"
Steve Anton10542f22019-01-11 09:11:00 -080086#include "rtc_base/time_utils.h"
87#include "rtc_base/virtual_socket_server.h"
Mirko Bonadei17f48782018-09-28 08:51:10 +020088#include "system_wrappers/include/metrics.h"
deadbeef1dcb1642017-03-29 21:08:16 -070089
Mirko Bonadeiab64e8a2018-12-12 12:10:18 +010090namespace webrtc {
Harald Alvestrand39993842021-02-17 09:05:31 +000091
Mirko Bonadeiab64e8a2018-12-12 12:10:18 +010092namespace {
93
Seth Hampson2f0d7022018-02-20 11:54:42 -080094class PeerConnectionIntegrationTest
95 : public PeerConnectionIntegrationBaseTest,
96 public ::testing::WithParamInterface<SdpSemantics> {
97 protected:
98 PeerConnectionIntegrationTest()
99 : PeerConnectionIntegrationBaseTest(GetParam()) {}
100};
101
Yves Gerey100fe632020-01-17 19:15:53 +0100102// Fake clock must be set before threads are started to prevent race on
103// Set/GetClockForTesting().
104// To achieve that, multiple inheritance is used as a mixin pattern
105// where order of construction is finely controlled.
106// This also ensures peerconnection is closed before switching back to non-fake
107// clock, avoiding other races and DCHECK failures such as in rtp_sender.cc.
108class FakeClockForTest : public rtc::ScopedFakeClock {
109 protected:
110 FakeClockForTest() {
111 // Some things use a time of "0" as a special value, so we need to start out
112 // the fake clock at a nonzero time.
113 // TODO(deadbeef): Fix this.
Danil Chapovalov0c626af2020-02-10 11:16:00 +0100114 AdvanceTime(webrtc::TimeDelta::Seconds(1));
Yves Gerey100fe632020-01-17 19:15:53 +0100115 }
116
117 // Explicit handle.
118 ScopedFakeClock& FakeClock() { return *this; }
119};
120
121// Ensure FakeClockForTest is constructed first (see class for rationale).
122class PeerConnectionIntegrationTestWithFakeClock
123 : public FakeClockForTest,
124 public PeerConnectionIntegrationTest {};
125
Seth Hampson2f0d7022018-02-20 11:54:42 -0800126class PeerConnectionIntegrationTestPlanB
127 : public PeerConnectionIntegrationBaseTest {
128 protected:
129 PeerConnectionIntegrationTestPlanB()
130 : PeerConnectionIntegrationBaseTest(SdpSemantics::kPlanB) {}
131};
132
133class PeerConnectionIntegrationTestUnifiedPlan
134 : public PeerConnectionIntegrationBaseTest {
135 protected:
136 PeerConnectionIntegrationTestUnifiedPlan()
137 : PeerConnectionIntegrationBaseTest(SdpSemantics::kUnifiedPlan) {}
138};
139
deadbeef1dcb1642017-03-29 21:08:16 -0700140// Test the OnFirstPacketReceived callback from audio/video RtpReceivers. This
141// includes testing that the callback is invoked if an observer is connected
142// after the first packet has already been received.
Seth Hampson2f0d7022018-02-20 11:54:42 -0800143TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -0700144 RtpReceiverObserverOnFirstPacketReceived) {
145 ASSERT_TRUE(CreatePeerConnectionWrappers());
146 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -0800147 caller()->AddAudioVideoTracks();
148 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -0700149 // Start offer/answer exchange and wait for it to complete.
150 caller()->CreateAndSetAndSignalOffer();
151 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
152 // Should be one receiver each for audio/video.
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +0200153 EXPECT_EQ(2U, caller()->rtp_receiver_observers().size());
154 EXPECT_EQ(2U, callee()->rtp_receiver_observers().size());
deadbeef1dcb1642017-03-29 21:08:16 -0700155 // Wait for all "first packet received" callbacks to be fired.
156 EXPECT_TRUE_WAIT(
Steve Anton64b626b2019-01-28 17:25:26 -0800157 absl::c_all_of(caller()->rtp_receiver_observers(),
158 [](const std::unique_ptr<MockRtpReceiverObserver>& o) {
159 return o->first_packet_received();
160 }),
deadbeef1dcb1642017-03-29 21:08:16 -0700161 kMaxWaitForFramesMs);
162 EXPECT_TRUE_WAIT(
Steve Anton64b626b2019-01-28 17:25:26 -0800163 absl::c_all_of(callee()->rtp_receiver_observers(),
164 [](const std::unique_ptr<MockRtpReceiverObserver>& o) {
165 return o->first_packet_received();
166 }),
deadbeef1dcb1642017-03-29 21:08:16 -0700167 kMaxWaitForFramesMs);
168 // If new observers are set after the first packet was already received, the
169 // callback should still be invoked.
170 caller()->ResetRtpReceiverObservers();
171 callee()->ResetRtpReceiverObservers();
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +0200172 EXPECT_EQ(2U, caller()->rtp_receiver_observers().size());
173 EXPECT_EQ(2U, callee()->rtp_receiver_observers().size());
deadbeef1dcb1642017-03-29 21:08:16 -0700174 EXPECT_TRUE(
Steve Anton64b626b2019-01-28 17:25:26 -0800175 absl::c_all_of(caller()->rtp_receiver_observers(),
176 [](const std::unique_ptr<MockRtpReceiverObserver>& o) {
177 return o->first_packet_received();
178 }));
deadbeef1dcb1642017-03-29 21:08:16 -0700179 EXPECT_TRUE(
Steve Anton64b626b2019-01-28 17:25:26 -0800180 absl::c_all_of(callee()->rtp_receiver_observers(),
181 [](const std::unique_ptr<MockRtpReceiverObserver>& o) {
182 return o->first_packet_received();
183 }));
deadbeef1dcb1642017-03-29 21:08:16 -0700184}
185
186class DummyDtmfObserver : public DtmfSenderObserverInterface {
187 public:
188 DummyDtmfObserver() : completed_(false) {}
189
190 // Implements DtmfSenderObserverInterface.
191 void OnToneChange(const std::string& tone) override {
192 tones_.push_back(tone);
193 if (tone.empty()) {
194 completed_ = true;
195 }
196 }
197
198 const std::vector<std::string>& tones() const { return tones_; }
199 bool completed() const { return completed_; }
200
201 private:
202 bool completed_;
203 std::vector<std::string> tones_;
204};
205
206// Assumes |sender| already has an audio track added and the offer/answer
207// exchange is done.
Harald Alvestrand39993842021-02-17 09:05:31 +0000208void TestDtmfFromSenderToReceiver(PeerConnectionIntegrationWrapper* sender,
209 PeerConnectionIntegrationWrapper* receiver) {
Steve Anton15324772018-01-16 10:26:49 -0800210 // We should be able to get a DTMF sender from the local sender.
211 rtc::scoped_refptr<DtmfSenderInterface> dtmf_sender =
212 sender->pc()->GetSenders().at(0)->GetDtmfSender();
213 ASSERT_TRUE(dtmf_sender);
deadbeef1dcb1642017-03-29 21:08:16 -0700214 DummyDtmfObserver observer;
deadbeef1dcb1642017-03-29 21:08:16 -0700215 dtmf_sender->RegisterObserver(&observer);
216
217 // Test the DtmfSender object just created.
218 EXPECT_TRUE(dtmf_sender->CanInsertDtmf());
219 EXPECT_TRUE(dtmf_sender->InsertDtmf("1a", 100, 50));
220
221 EXPECT_TRUE_WAIT(observer.completed(), kDefaultTimeout);
222 std::vector<std::string> tones = {"1", "a", ""};
223 EXPECT_EQ(tones, observer.tones());
224 dtmf_sender->UnregisterObserver();
225 // TODO(deadbeef): Verify the tones were actually received end-to-end.
226}
227
228// Verifies the DtmfSenderObserver callbacks for a DtmfSender (one in each
229// direction).
Seth Hampson2f0d7022018-02-20 11:54:42 -0800230TEST_P(PeerConnectionIntegrationTest, DtmfSenderObserver) {
deadbeef1dcb1642017-03-29 21:08:16 -0700231 ASSERT_TRUE(CreatePeerConnectionWrappers());
232 ConnectFakeSignaling();
233 // Only need audio for DTMF.
Steve Anton15324772018-01-16 10:26:49 -0800234 caller()->AddAudioTrack();
235 callee()->AddAudioTrack();
deadbeef1dcb1642017-03-29 21:08:16 -0700236 caller()->CreateAndSetAndSignalOffer();
237 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
deadbeef71452802017-05-07 17:21:01 -0700238 // DTLS must finish before the DTMF sender can be used reliably.
239 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -0700240 TestDtmfFromSenderToReceiver(caller(), callee());
241 TestDtmfFromSenderToReceiver(callee(), caller());
242}
243
244// Basic end-to-end test, verifying media can be encoded/transmitted/decoded
245// between two connections, using DTLS-SRTP.
Seth Hampson2f0d7022018-02-20 11:54:42 -0800246TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithDtls) {
deadbeef1dcb1642017-03-29 21:08:16 -0700247 ASSERT_TRUE(CreatePeerConnectionWrappers());
248 ConnectFakeSignaling();
Harald Alvestrand194939b2018-01-24 16:04:13 +0100249
deadbeef1dcb1642017-03-29 21:08:16 -0700250 // Do normal offer/answer and wait for some frames to be received in each
251 // direction.
Steve Anton15324772018-01-16 10:26:49 -0800252 caller()->AddAudioVideoTracks();
253 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -0700254 caller()->CreateAndSetAndSignalOffer();
255 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -0800256 MediaExpectations media_expectations;
257 media_expectations.ExpectBidirectionalAudioAndVideo();
258 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Ying Wangef3998f2019-12-09 13:06:53 +0100259 EXPECT_METRIC_LE(
260 2, webrtc::metrics::NumEvents("WebRTC.PeerConnection.KeyProtocol",
261 webrtc::kEnumCounterKeyProtocolDtls));
262 EXPECT_METRIC_EQ(
263 0, webrtc::metrics::NumEvents("WebRTC.PeerConnection.KeyProtocol",
264 webrtc::kEnumCounterKeyProtocolSdes));
deadbeef1dcb1642017-03-29 21:08:16 -0700265}
266
267// Uses SDES instead of DTLS for key agreement.
Seth Hampson2f0d7022018-02-20 11:54:42 -0800268TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithSdes) {
deadbeef1dcb1642017-03-29 21:08:16 -0700269 PeerConnectionInterface::RTCConfiguration sdes_config;
270 sdes_config.enable_dtls_srtp.emplace(false);
271 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(sdes_config, sdes_config));
272 ConnectFakeSignaling();
273
274 // Do normal offer/answer and wait for some frames to be received in each
275 // direction.
Steve Anton15324772018-01-16 10:26:49 -0800276 caller()->AddAudioVideoTracks();
277 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -0700278 caller()->CreateAndSetAndSignalOffer();
279 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -0800280 MediaExpectations media_expectations;
281 media_expectations.ExpectBidirectionalAudioAndVideo();
282 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Ying Wangef3998f2019-12-09 13:06:53 +0100283 EXPECT_METRIC_LE(
284 2, webrtc::metrics::NumEvents("WebRTC.PeerConnection.KeyProtocol",
285 webrtc::kEnumCounterKeyProtocolSdes));
286 EXPECT_METRIC_EQ(
287 0, webrtc::metrics::NumEvents("WebRTC.PeerConnection.KeyProtocol",
288 webrtc::kEnumCounterKeyProtocolDtls));
deadbeef1dcb1642017-03-29 21:08:16 -0700289}
290
Steve Anton9a44b2d2019-07-12 12:58:30 -0700291// Basic end-to-end test specifying the |enable_encrypted_rtp_header_extensions|
292// option to offer encrypted versions of all header extensions alongside the
293// unencrypted versions.
294TEST_P(PeerConnectionIntegrationTest,
295 EndToEndCallWithEncryptedRtpHeaderExtensions) {
296 CryptoOptions crypto_options;
297 crypto_options.srtp.enable_encrypted_rtp_header_extensions = true;
298 PeerConnectionInterface::RTCConfiguration config;
299 config.crypto_options = crypto_options;
300 // Note: This allows offering >14 RTP header extensions.
301 config.offer_extmap_allow_mixed = true;
302 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
303 ConnectFakeSignaling();
304
305 // Do normal offer/answer and wait for some frames to be received in each
306 // direction.
307 caller()->AddAudioVideoTracks();
308 callee()->AddAudioVideoTracks();
309 caller()->CreateAndSetAndSignalOffer();
310 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
311 MediaExpectations media_expectations;
312 media_expectations.ExpectBidirectionalAudioAndVideo();
313 ASSERT_TRUE(ExpectNewFrames(media_expectations));
314}
315
deadbeef1dcb1642017-03-29 21:08:16 -0700316// This test sets up a call between two parties with a source resolution of
317// 1280x720 and verifies that a 16:9 aspect ratio is received.
Seth Hampson2f0d7022018-02-20 11:54:42 -0800318TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -0700319 Send1280By720ResolutionAndReceive16To9AspectRatio) {
320 ASSERT_TRUE(CreatePeerConnectionWrappers());
321 ConnectFakeSignaling();
322
Niels Möller5c7efe72018-05-11 10:34:46 +0200323 // Add video tracks with 16:9 aspect ratio, size 1280 x 720.
324 webrtc::FakePeriodicVideoSource::Config config;
325 config.width = 1280;
326 config.height = 720;
Johannes Kron965e7942018-09-13 15:36:20 +0200327 config.timestamp_offset_ms = rtc::TimeMillis();
Niels Möller5c7efe72018-05-11 10:34:46 +0200328 caller()->AddTrack(caller()->CreateLocalVideoTrackWithConfig(config));
329 callee()->AddTrack(callee()->CreateLocalVideoTrackWithConfig(config));
deadbeef1dcb1642017-03-29 21:08:16 -0700330
331 // Do normal offer/answer and wait for at least one frame to be received in
332 // each direction.
333 caller()->CreateAndSetAndSignalOffer();
334 ASSERT_TRUE_WAIT(caller()->min_video_frames_received_per_track() > 0 &&
335 callee()->min_video_frames_received_per_track() > 0,
336 kMaxWaitForFramesMs);
337
338 // Check rendered aspect ratio.
339 EXPECT_EQ(16.0 / 9, caller()->local_rendered_aspect_ratio());
340 EXPECT_EQ(16.0 / 9, caller()->rendered_aspect_ratio());
341 EXPECT_EQ(16.0 / 9, callee()->local_rendered_aspect_ratio());
342 EXPECT_EQ(16.0 / 9, callee()->rendered_aspect_ratio());
343}
344
345// This test sets up an one-way call, with media only from caller to
346// callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -0800347TEST_P(PeerConnectionIntegrationTest, OneWayMediaCall) {
deadbeef1dcb1642017-03-29 21:08:16 -0700348 ASSERT_TRUE(CreatePeerConnectionWrappers());
349 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -0800350 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -0700351 caller()->CreateAndSetAndSignalOffer();
Seth Hampson2f0d7022018-02-20 11:54:42 -0800352 MediaExpectations media_expectations;
353 media_expectations.CalleeExpectsSomeAudioAndVideo();
354 media_expectations.CallerExpectsNoAudio();
355 media_expectations.CallerExpectsNoVideo();
356 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -0700357}
358
Johannes Kron3e983682020-03-29 22:17:00 +0200359// Tests that send only works without the caller having a decoder factory and
360// the callee having an encoder factory.
361TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithSendOnlyVideo) {
362 ASSERT_TRUE(
363 CreateOneDirectionalPeerConnectionWrappers(/*caller_to_callee=*/true));
364 ConnectFakeSignaling();
365 // Add one-directional video, from caller to callee.
366 rtc::scoped_refptr<webrtc::VideoTrackInterface> caller_track =
367 caller()->CreateLocalVideoTrack();
368 caller()->AddTrack(caller_track);
369 PeerConnectionInterface::RTCOfferAnswerOptions options;
370 options.offer_to_receive_video = 0;
371 caller()->SetOfferAnswerOptions(options);
372 caller()->CreateAndSetAndSignalOffer();
373 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
374 ASSERT_EQ(callee()->pc()->GetReceivers().size(), 1u);
375
376 // Expect video to be received in one direction.
377 MediaExpectations media_expectations;
378 media_expectations.CallerExpectsNoVideo();
379 media_expectations.CalleeExpectsSomeVideo();
380
381 EXPECT_TRUE(ExpectNewFrames(media_expectations));
382}
383
384// Tests that receive only works without the caller having an encoder factory
385// and the callee having a decoder factory.
386TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithReceiveOnlyVideo) {
387 ASSERT_TRUE(
388 CreateOneDirectionalPeerConnectionWrappers(/*caller_to_callee=*/false));
389 ConnectFakeSignaling();
390 // Add one-directional video, from callee to caller.
391 rtc::scoped_refptr<webrtc::VideoTrackInterface> callee_track =
392 callee()->CreateLocalVideoTrack();
393 callee()->AddTrack(callee_track);
394 PeerConnectionInterface::RTCOfferAnswerOptions options;
395 options.offer_to_receive_video = 1;
396 caller()->SetOfferAnswerOptions(options);
397 caller()->CreateAndSetAndSignalOffer();
398 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
399 ASSERT_EQ(caller()->pc()->GetReceivers().size(), 1u);
400
401 // Expect video to be received in one direction.
402 MediaExpectations media_expectations;
403 media_expectations.CallerExpectsSomeVideo();
404 media_expectations.CalleeExpectsNoVideo();
405
406 EXPECT_TRUE(ExpectNewFrames(media_expectations));
407}
408
409TEST_P(PeerConnectionIntegrationTest,
410 EndToEndCallAddReceiveVideoToSendOnlyCall) {
411 ASSERT_TRUE(CreatePeerConnectionWrappers());
412 ConnectFakeSignaling();
413 // Add one-directional video, from caller to callee.
414 rtc::scoped_refptr<webrtc::VideoTrackInterface> caller_track =
415 caller()->CreateLocalVideoTrack();
416 caller()->AddTrack(caller_track);
417 caller()->CreateAndSetAndSignalOffer();
418 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
419
420 // Add receive video.
421 rtc::scoped_refptr<webrtc::VideoTrackInterface> callee_track =
422 callee()->CreateLocalVideoTrack();
423 callee()->AddTrack(callee_track);
424 caller()->CreateAndSetAndSignalOffer();
425 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
426
427 // Ensure that video frames are received end-to-end.
428 MediaExpectations media_expectations;
429 media_expectations.ExpectBidirectionalVideo();
430 ASSERT_TRUE(ExpectNewFrames(media_expectations));
431}
432
433TEST_P(PeerConnectionIntegrationTest,
434 EndToEndCallAddSendVideoToReceiveOnlyCall) {
435 ASSERT_TRUE(CreatePeerConnectionWrappers());
436 ConnectFakeSignaling();
437 // Add one-directional video, from callee to caller.
438 rtc::scoped_refptr<webrtc::VideoTrackInterface> callee_track =
439 callee()->CreateLocalVideoTrack();
440 callee()->AddTrack(callee_track);
441 caller()->CreateAndSetAndSignalOffer();
442 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
443
444 // Add send video.
445 rtc::scoped_refptr<webrtc::VideoTrackInterface> caller_track =
446 caller()->CreateLocalVideoTrack();
447 caller()->AddTrack(caller_track);
448 caller()->CreateAndSetAndSignalOffer();
449 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
450
451 // Expect video to be received in one direction.
452 MediaExpectations media_expectations;
453 media_expectations.ExpectBidirectionalVideo();
454 ASSERT_TRUE(ExpectNewFrames(media_expectations));
455}
456
457TEST_P(PeerConnectionIntegrationTest,
458 EndToEndCallRemoveReceiveVideoFromSendReceiveCall) {
459 ASSERT_TRUE(CreatePeerConnectionWrappers());
460 ConnectFakeSignaling();
461 // Add send video, from caller to callee.
462 rtc::scoped_refptr<webrtc::VideoTrackInterface> caller_track =
463 caller()->CreateLocalVideoTrack();
464 rtc::scoped_refptr<webrtc::RtpSenderInterface> caller_sender =
465 caller()->AddTrack(caller_track);
466 // Add receive video, from callee to caller.
467 rtc::scoped_refptr<webrtc::VideoTrackInterface> callee_track =
468 callee()->CreateLocalVideoTrack();
469
470 rtc::scoped_refptr<webrtc::RtpSenderInterface> callee_sender =
471 callee()->AddTrack(callee_track);
472 caller()->CreateAndSetAndSignalOffer();
473 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
474
475 // Remove receive video (i.e., callee sender track).
476 callee()->pc()->RemoveTrack(callee_sender);
477
478 caller()->CreateAndSetAndSignalOffer();
479 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
480
481 // Expect one-directional video.
482 MediaExpectations media_expectations;
483 media_expectations.CallerExpectsNoVideo();
484 media_expectations.CalleeExpectsSomeVideo();
485
486 ASSERT_TRUE(ExpectNewFrames(media_expectations));
487}
488
489TEST_P(PeerConnectionIntegrationTest,
490 EndToEndCallRemoveSendVideoFromSendReceiveCall) {
491 ASSERT_TRUE(CreatePeerConnectionWrappers());
492 ConnectFakeSignaling();
493 // Add send video, from caller to callee.
494 rtc::scoped_refptr<webrtc::VideoTrackInterface> caller_track =
495 caller()->CreateLocalVideoTrack();
496 rtc::scoped_refptr<webrtc::RtpSenderInterface> caller_sender =
497 caller()->AddTrack(caller_track);
498 // Add receive video, from callee to caller.
499 rtc::scoped_refptr<webrtc::VideoTrackInterface> callee_track =
500 callee()->CreateLocalVideoTrack();
501
502 rtc::scoped_refptr<webrtc::RtpSenderInterface> callee_sender =
503 callee()->AddTrack(callee_track);
504 caller()->CreateAndSetAndSignalOffer();
505 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
506
507 // Remove send video (i.e., caller sender track).
508 caller()->pc()->RemoveTrack(caller_sender);
509
510 caller()->CreateAndSetAndSignalOffer();
511 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
512
513 // Expect one-directional video.
514 MediaExpectations media_expectations;
515 media_expectations.CalleeExpectsNoVideo();
516 media_expectations.CallerExpectsSomeVideo();
517
518 ASSERT_TRUE(ExpectNewFrames(media_expectations));
519}
520
deadbeef1dcb1642017-03-29 21:08:16 -0700521// This test sets up a audio call initially, with the callee rejecting video
522// initially. Then later the callee decides to upgrade to audio/video, and
523// initiates a new offer/answer exchange.
Seth Hampson2f0d7022018-02-20 11:54:42 -0800524TEST_P(PeerConnectionIntegrationTest, AudioToVideoUpgrade) {
deadbeef1dcb1642017-03-29 21:08:16 -0700525 ASSERT_TRUE(CreatePeerConnectionWrappers());
526 ConnectFakeSignaling();
527 // Initially, offer an audio/video stream from the caller, but refuse to
528 // send/receive video on the callee side.
Steve Anton15324772018-01-16 10:26:49 -0800529 caller()->AddAudioVideoTracks();
530 callee()->AddAudioTrack();
Seth Hampson2f0d7022018-02-20 11:54:42 -0800531 if (sdp_semantics_ == SdpSemantics::kPlanB) {
532 PeerConnectionInterface::RTCOfferAnswerOptions options;
533 options.offer_to_receive_video = 0;
534 callee()->SetOfferAnswerOptions(options);
535 } else {
536 callee()->SetRemoteOfferHandler([this] {
Harald Alvestrand6060df52020-08-11 09:54:02 +0200537 callee()
538 ->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)
539 ->StopInternal();
Seth Hampson2f0d7022018-02-20 11:54:42 -0800540 });
541 }
deadbeef1dcb1642017-03-29 21:08:16 -0700542 // Do offer/answer and make sure audio is still received end-to-end.
543 caller()->CreateAndSetAndSignalOffer();
544 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -0800545 {
546 MediaExpectations media_expectations;
547 media_expectations.ExpectBidirectionalAudio();
548 media_expectations.ExpectNoVideo();
549 ASSERT_TRUE(ExpectNewFrames(media_expectations));
550 }
deadbeef1dcb1642017-03-29 21:08:16 -0700551 // Sanity check that the callee's description has a rejected video section.
552 ASSERT_NE(nullptr, callee()->pc()->local_description());
553 const ContentInfo* callee_video_content =
554 GetFirstVideoContent(callee()->pc()->local_description()->description());
555 ASSERT_NE(nullptr, callee_video_content);
556 EXPECT_TRUE(callee_video_content->rejected);
Seth Hampson2f0d7022018-02-20 11:54:42 -0800557
deadbeef1dcb1642017-03-29 21:08:16 -0700558 // Now negotiate with video and ensure negotiation succeeds, with video
559 // frames and additional audio frames being received.
Steve Anton15324772018-01-16 10:26:49 -0800560 callee()->AddVideoTrack();
Seth Hampson2f0d7022018-02-20 11:54:42 -0800561 if (sdp_semantics_ == SdpSemantics::kPlanB) {
562 PeerConnectionInterface::RTCOfferAnswerOptions options;
563 options.offer_to_receive_video = 1;
564 callee()->SetOfferAnswerOptions(options);
565 } else {
566 callee()->SetRemoteOfferHandler(nullptr);
567 caller()->SetRemoteOfferHandler([this] {
568 // The caller creates a new transceiver to receive video on when receiving
569 // the offer, but by default it is send only.
570 auto transceivers = caller()->pc()->GetTransceivers();
Harald Alvestrand6060df52020-08-11 09:54:02 +0200571 ASSERT_EQ(2U, transceivers.size());
Seth Hampson2f0d7022018-02-20 11:54:42 -0800572 ASSERT_EQ(cricket::MEDIA_TYPE_VIDEO,
Harald Alvestrand6060df52020-08-11 09:54:02 +0200573 transceivers[1]->receiver()->media_type());
574 transceivers[1]->sender()->SetTrack(caller()->CreateLocalVideoTrack());
575 transceivers[1]->SetDirectionWithError(
576 RtpTransceiverDirection::kSendRecv);
Seth Hampson2f0d7022018-02-20 11:54:42 -0800577 });
578 }
deadbeef1dcb1642017-03-29 21:08:16 -0700579 callee()->CreateAndSetAndSignalOffer();
580 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -0800581 {
582 // Expect additional audio frames to be received after the upgrade.
583 MediaExpectations media_expectations;
584 media_expectations.ExpectBidirectionalAudioAndVideo();
585 ASSERT_TRUE(ExpectNewFrames(media_expectations));
586 }
deadbeef1dcb1642017-03-29 21:08:16 -0700587}
588
deadbeef4389b4d2017-09-07 09:07:36 -0700589// Simpler than the above test; just add an audio track to an established
590// video-only connection.
Seth Hampson2f0d7022018-02-20 11:54:42 -0800591TEST_P(PeerConnectionIntegrationTest, AddAudioToVideoOnlyCall) {
deadbeef4389b4d2017-09-07 09:07:36 -0700592 ASSERT_TRUE(CreatePeerConnectionWrappers());
593 ConnectFakeSignaling();
594 // Do initial offer/answer with just a video track.
Steve Anton15324772018-01-16 10:26:49 -0800595 caller()->AddVideoTrack();
596 callee()->AddVideoTrack();
deadbeef4389b4d2017-09-07 09:07:36 -0700597 caller()->CreateAndSetAndSignalOffer();
598 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
599 // Now add an audio track and do another offer/answer.
Steve Anton15324772018-01-16 10:26:49 -0800600 caller()->AddAudioTrack();
601 callee()->AddAudioTrack();
deadbeef4389b4d2017-09-07 09:07:36 -0700602 caller()->CreateAndSetAndSignalOffer();
603 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
604 // Ensure both audio and video frames are received end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -0800605 MediaExpectations media_expectations;
606 media_expectations.ExpectBidirectionalAudioAndVideo();
607 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef4389b4d2017-09-07 09:07:36 -0700608}
609
deadbeef1dcb1642017-03-29 21:08:16 -0700610// This test sets up a call that's transferred to a new caller with a different
611// DTLS fingerprint.
Seth Hampson2f0d7022018-02-20 11:54:42 -0800612TEST_P(PeerConnectionIntegrationTest, CallTransferredForCallee) {
deadbeef1dcb1642017-03-29 21:08:16 -0700613 ASSERT_TRUE(CreatePeerConnectionWrappers());
614 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -0800615 caller()->AddAudioVideoTracks();
616 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -0700617 caller()->CreateAndSetAndSignalOffer();
618 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
619
620 // Keep the original peer around which will still send packets to the
621 // receiving client. These SRTP packets will be dropped.
Harald Alvestrand39993842021-02-17 09:05:31 +0000622 std::unique_ptr<PeerConnectionIntegrationWrapper> original_peer(
deadbeef1dcb1642017-03-29 21:08:16 -0700623 SetCallerPcWrapperAndReturnCurrent(
Seth Hampson2f0d7022018-02-20 11:54:42 -0800624 CreatePeerConnectionWrapperWithAlternateKey().release()));
deadbeef1dcb1642017-03-29 21:08:16 -0700625 // TODO(deadbeef): Why do we call Close here? That goes against the comment
626 // directly above.
627 original_peer->pc()->Close();
628
629 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -0800630 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -0700631 caller()->CreateAndSetAndSignalOffer();
632 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
633 // Wait for some additional frames to be transmitted end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -0800634 MediaExpectations media_expectations;
635 media_expectations.ExpectBidirectionalAudioAndVideo();
636 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -0700637}
638
639// This test sets up a call that's transferred to a new callee with a different
640// DTLS fingerprint.
Seth Hampson2f0d7022018-02-20 11:54:42 -0800641TEST_P(PeerConnectionIntegrationTest, CallTransferredForCaller) {
deadbeef1dcb1642017-03-29 21:08:16 -0700642 ASSERT_TRUE(CreatePeerConnectionWrappers());
643 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -0800644 caller()->AddAudioVideoTracks();
645 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -0700646 caller()->CreateAndSetAndSignalOffer();
647 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
648
649 // Keep the original peer around which will still send packets to the
650 // receiving client. These SRTP packets will be dropped.
Harald Alvestrand39993842021-02-17 09:05:31 +0000651 std::unique_ptr<PeerConnectionIntegrationWrapper> original_peer(
deadbeef1dcb1642017-03-29 21:08:16 -0700652 SetCalleePcWrapperAndReturnCurrent(
Seth Hampson2f0d7022018-02-20 11:54:42 -0800653 CreatePeerConnectionWrapperWithAlternateKey().release()));
deadbeef1dcb1642017-03-29 21:08:16 -0700654 // TODO(deadbeef): Why do we call Close here? That goes against the comment
655 // directly above.
656 original_peer->pc()->Close();
657
658 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -0800659 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -0700660 caller()->SetOfferAnswerOptions(IceRestartOfferAnswerOptions());
661 caller()->CreateAndSetAndSignalOffer();
662 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
663 // Wait for some additional frames to be transmitted end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -0800664 MediaExpectations media_expectations;
665 media_expectations.ExpectBidirectionalAudioAndVideo();
666 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -0700667}
668
669// This test sets up a non-bundled call and negotiates bundling at the same
670// time as starting an ICE restart. When bundling is in effect in the restart,
671// the DTLS-SRTP context should be successfully reset.
Seth Hampson2f0d7022018-02-20 11:54:42 -0800672TEST_P(PeerConnectionIntegrationTest, BundlingEnabledWhileIceRestartOccurs) {
deadbeef1dcb1642017-03-29 21:08:16 -0700673 ASSERT_TRUE(CreatePeerConnectionWrappers());
674 ConnectFakeSignaling();
675
Steve Anton15324772018-01-16 10:26:49 -0800676 caller()->AddAudioVideoTracks();
677 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -0700678 // Remove the bundle group from the SDP received by the callee.
679 callee()->SetReceivedSdpMunger([](cricket::SessionDescription* desc) {
680 desc->RemoveGroupByName("BUNDLE");
681 });
682 caller()->CreateAndSetAndSignalOffer();
683 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -0800684 {
685 MediaExpectations media_expectations;
686 media_expectations.ExpectBidirectionalAudioAndVideo();
687 ASSERT_TRUE(ExpectNewFrames(media_expectations));
688 }
deadbeef1dcb1642017-03-29 21:08:16 -0700689 // Now stop removing the BUNDLE group, and trigger an ICE restart.
690 callee()->SetReceivedSdpMunger(nullptr);
691 caller()->SetOfferAnswerOptions(IceRestartOfferAnswerOptions());
692 caller()->CreateAndSetAndSignalOffer();
693 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
694
695 // Expect additional frames to be received after the ICE restart.
Seth Hampson2f0d7022018-02-20 11:54:42 -0800696 {
697 MediaExpectations media_expectations;
698 media_expectations.ExpectBidirectionalAudioAndVideo();
699 ASSERT_TRUE(ExpectNewFrames(media_expectations));
700 }
deadbeef1dcb1642017-03-29 21:08:16 -0700701}
702
703// Test CVO (Coordination of Video Orientation). If a video source is rotated
704// and both peers support the CVO RTP header extension, the actual video frames
705// don't need to be encoded in different resolutions, since the rotation is
706// communicated through the RTP header extension.
Seth Hampson2f0d7022018-02-20 11:54:42 -0800707TEST_P(PeerConnectionIntegrationTest, RotatedVideoWithCVOExtension) {
deadbeef1dcb1642017-03-29 21:08:16 -0700708 ASSERT_TRUE(CreatePeerConnectionWrappers());
709 ConnectFakeSignaling();
710 // Add rotated video tracks.
Steve Anton15324772018-01-16 10:26:49 -0800711 caller()->AddTrack(
deadbeef1dcb1642017-03-29 21:08:16 -0700712 caller()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_90));
Steve Anton15324772018-01-16 10:26:49 -0800713 callee()->AddTrack(
deadbeef1dcb1642017-03-29 21:08:16 -0700714 callee()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_270));
715
716 // Wait for video frames to be received by both sides.
717 caller()->CreateAndSetAndSignalOffer();
718 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
719 ASSERT_TRUE_WAIT(caller()->min_video_frames_received_per_track() > 0 &&
720 callee()->min_video_frames_received_per_track() > 0,
721 kMaxWaitForFramesMs);
722
723 // Ensure that the aspect ratio is unmodified.
724 // TODO(deadbeef): Where does 4:3 come from? Should be explicit in the test,
725 // not just assumed.
726 EXPECT_EQ(4.0 / 3, caller()->local_rendered_aspect_ratio());
727 EXPECT_EQ(4.0 / 3, caller()->rendered_aspect_ratio());
728 EXPECT_EQ(4.0 / 3, callee()->local_rendered_aspect_ratio());
729 EXPECT_EQ(4.0 / 3, callee()->rendered_aspect_ratio());
730 // Ensure that the CVO bits were surfaced to the renderer.
731 EXPECT_EQ(webrtc::kVideoRotation_270, caller()->rendered_rotation());
732 EXPECT_EQ(webrtc::kVideoRotation_90, callee()->rendered_rotation());
733}
734
735// Test that when the CVO extension isn't supported, video is rotated the
736// old-fashioned way, by encoding rotated frames.
Seth Hampson2f0d7022018-02-20 11:54:42 -0800737TEST_P(PeerConnectionIntegrationTest, RotatedVideoWithoutCVOExtension) {
deadbeef1dcb1642017-03-29 21:08:16 -0700738 ASSERT_TRUE(CreatePeerConnectionWrappers());
739 ConnectFakeSignaling();
740 // Add rotated video tracks.
Steve Anton15324772018-01-16 10:26:49 -0800741 caller()->AddTrack(
deadbeef1dcb1642017-03-29 21:08:16 -0700742 caller()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_90));
Steve Anton15324772018-01-16 10:26:49 -0800743 callee()->AddTrack(
deadbeef1dcb1642017-03-29 21:08:16 -0700744 callee()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_270));
745
746 // Remove the CVO extension from the offered SDP.
747 callee()->SetReceivedSdpMunger([](cricket::SessionDescription* desc) {
748 cricket::VideoContentDescription* video =
749 GetFirstVideoContentDescription(desc);
750 video->ClearRtpHeaderExtensions();
751 });
752 // Wait for video frames to be received by both sides.
753 caller()->CreateAndSetAndSignalOffer();
754 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
755 ASSERT_TRUE_WAIT(caller()->min_video_frames_received_per_track() > 0 &&
756 callee()->min_video_frames_received_per_track() > 0,
757 kMaxWaitForFramesMs);
758
759 // Expect that the aspect ratio is inversed to account for the 90/270 degree
760 // rotation.
761 // TODO(deadbeef): Where does 4:3 come from? Should be explicit in the test,
762 // not just assumed.
763 EXPECT_EQ(3.0 / 4, caller()->local_rendered_aspect_ratio());
764 EXPECT_EQ(3.0 / 4, caller()->rendered_aspect_ratio());
765 EXPECT_EQ(3.0 / 4, callee()->local_rendered_aspect_ratio());
766 EXPECT_EQ(3.0 / 4, callee()->rendered_aspect_ratio());
767 // Expect that each endpoint is unaware of the rotation of the other endpoint.
768 EXPECT_EQ(webrtc::kVideoRotation_0, caller()->rendered_rotation());
769 EXPECT_EQ(webrtc::kVideoRotation_0, callee()->rendered_rotation());
770}
771
deadbeef1dcb1642017-03-29 21:08:16 -0700772// Test that if the answerer rejects the audio m= section, no audio is sent or
773// received, but video still can be.
Seth Hampson2f0d7022018-02-20 11:54:42 -0800774TEST_P(PeerConnectionIntegrationTest, AnswererRejectsAudioSection) {
deadbeef1dcb1642017-03-29 21:08:16 -0700775 ASSERT_TRUE(CreatePeerConnectionWrappers());
776 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -0800777 caller()->AddAudioVideoTracks();
Seth Hampson2f0d7022018-02-20 11:54:42 -0800778 if (sdp_semantics_ == SdpSemantics::kPlanB) {
779 // Only add video track for callee, and set offer_to_receive_audio to 0, so
780 // it will reject the audio m= section completely.
781 PeerConnectionInterface::RTCOfferAnswerOptions options;
782 options.offer_to_receive_audio = 0;
783 callee()->SetOfferAnswerOptions(options);
784 } else {
785 // Stopping the audio 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_AUDIO)
790 ->StopInternal();
Seth Hampson2f0d7022018-02-20 11:54:42 -0800791 });
792 }
Steve Anton15324772018-01-16 10:26:49 -0800793 callee()->AddTrack(callee()->CreateLocalVideoTrack());
deadbeef1dcb1642017-03-29 21:08:16 -0700794 // Do offer/answer and wait for successful end-to-end video frames.
795 caller()->CreateAndSetAndSignalOffer();
796 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -0800797 MediaExpectations media_expectations;
798 media_expectations.ExpectBidirectionalVideo();
799 media_expectations.ExpectNoAudio();
800 ASSERT_TRUE(ExpectNewFrames(media_expectations));
801
deadbeef1dcb1642017-03-29 21:08:16 -0700802 // Sanity check that the callee's description has a rejected audio section.
803 ASSERT_NE(nullptr, callee()->pc()->local_description());
804 const ContentInfo* callee_audio_content =
805 GetFirstAudioContent(callee()->pc()->local_description()->description());
806 ASSERT_NE(nullptr, callee_audio_content);
807 EXPECT_TRUE(callee_audio_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 no longer listed in transceivers.
811 EXPECT_EQ(nullptr,
812 caller()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_AUDIO));
Seth Hampson2f0d7022018-02-20 11:54:42 -0800813 }
deadbeef1dcb1642017-03-29 21:08:16 -0700814}
815
816// Test that if the answerer rejects the video m= section, no video is sent or
817// received, but audio still can be.
Seth Hampson2f0d7022018-02-20 11:54:42 -0800818TEST_P(PeerConnectionIntegrationTest, AnswererRejectsVideoSection) {
deadbeef1dcb1642017-03-29 21:08:16 -0700819 ASSERT_TRUE(CreatePeerConnectionWrappers());
820 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -0800821 caller()->AddAudioVideoTracks();
Seth Hampson2f0d7022018-02-20 11:54:42 -0800822 if (sdp_semantics_ == SdpSemantics::kPlanB) {
823 // Only add audio track for callee, and set offer_to_receive_video to 0, so
824 // it will reject the video m= section completely.
825 PeerConnectionInterface::RTCOfferAnswerOptions options;
826 options.offer_to_receive_video = 0;
827 callee()->SetOfferAnswerOptions(options);
828 } else {
829 // Stopping the video RtpTransceiver will cause the media section to be
830 // rejected in the answer.
831 callee()->SetRemoteOfferHandler([this] {
Harald Alvestrand6060df52020-08-11 09:54:02 +0200832 callee()
833 ->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)
834 ->StopInternal();
Seth Hampson2f0d7022018-02-20 11:54:42 -0800835 });
836 }
Steve Anton15324772018-01-16 10:26:49 -0800837 callee()->AddTrack(callee()->CreateLocalAudioTrack());
deadbeef1dcb1642017-03-29 21:08:16 -0700838 // Do offer/answer and wait for successful end-to-end audio frames.
839 caller()->CreateAndSetAndSignalOffer();
840 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -0800841 MediaExpectations media_expectations;
842 media_expectations.ExpectBidirectionalAudio();
843 media_expectations.ExpectNoVideo();
844 ASSERT_TRUE(ExpectNewFrames(media_expectations));
845
deadbeef1dcb1642017-03-29 21:08:16 -0700846 // Sanity check that the callee's description has a rejected video section.
847 ASSERT_NE(nullptr, callee()->pc()->local_description());
848 const ContentInfo* callee_video_content =
849 GetFirstVideoContent(callee()->pc()->local_description()->description());
850 ASSERT_NE(nullptr, callee_video_content);
851 EXPECT_TRUE(callee_video_content->rejected);
Seth Hampson2f0d7022018-02-20 11:54:42 -0800852 if (sdp_semantics_ == SdpSemantics::kUnifiedPlan) {
Harald Alvestrand6060df52020-08-11 09:54:02 +0200853 // The caller's transceiver should have stopped after receiving the answer,
854 // and thus is no longer present.
855 EXPECT_EQ(nullptr,
856 caller()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO));
Seth Hampson2f0d7022018-02-20 11:54:42 -0800857 }
deadbeef1dcb1642017-03-29 21:08:16 -0700858}
859
860// Test that if the answerer rejects both audio and video m= sections, nothing
861// bad happens.
862// TODO(deadbeef): Test that a data channel still works. Currently this doesn't
863// test anything but the fact that negotiation succeeds, which doesn't mean
864// much.
Seth Hampson2f0d7022018-02-20 11:54:42 -0800865TEST_P(PeerConnectionIntegrationTest, AnswererRejectsAudioAndVideoSections) {
deadbeef1dcb1642017-03-29 21:08:16 -0700866 ASSERT_TRUE(CreatePeerConnectionWrappers());
867 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -0800868 caller()->AddAudioVideoTracks();
Seth Hampson2f0d7022018-02-20 11:54:42 -0800869 if (sdp_semantics_ == SdpSemantics::kPlanB) {
870 // Don't give the callee any tracks, and set offer_to_receive_X to 0, so it
871 // will reject both audio and video m= sections.
872 PeerConnectionInterface::RTCOfferAnswerOptions options;
873 options.offer_to_receive_audio = 0;
874 options.offer_to_receive_video = 0;
875 callee()->SetOfferAnswerOptions(options);
876 } else {
877 callee()->SetRemoteOfferHandler([this] {
878 // Stopping all transceivers will cause all media sections to be rejected.
Mirko Bonadei739baf02019-01-27 17:29:42 +0100879 for (const auto& transceiver : callee()->pc()->GetTransceivers()) {
Harald Alvestrand6060df52020-08-11 09:54:02 +0200880 transceiver->StopInternal();
Seth Hampson2f0d7022018-02-20 11:54:42 -0800881 }
882 });
883 }
deadbeef1dcb1642017-03-29 21:08:16 -0700884 // Do offer/answer and wait for stable signaling state.
885 caller()->CreateAndSetAndSignalOffer();
886 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -0800887
deadbeef1dcb1642017-03-29 21:08:16 -0700888 // Sanity check that the callee's description has rejected m= sections.
889 ASSERT_NE(nullptr, callee()->pc()->local_description());
890 const ContentInfo* callee_audio_content =
891 GetFirstAudioContent(callee()->pc()->local_description()->description());
892 ASSERT_NE(nullptr, callee_audio_content);
893 EXPECT_TRUE(callee_audio_content->rejected);
894 const ContentInfo* callee_video_content =
895 GetFirstVideoContent(callee()->pc()->local_description()->description());
896 ASSERT_NE(nullptr, callee_video_content);
897 EXPECT_TRUE(callee_video_content->rejected);
898}
899
900// This test sets up an audio and video call between two parties. After the
901// call runs for a while, the caller sends an updated offer with video being
902// rejected. Once the re-negotiation is done, the video flow should stop and
903// the audio flow should continue.
Seth Hampson2f0d7022018-02-20 11:54:42 -0800904TEST_P(PeerConnectionIntegrationTest, VideoRejectedInSubsequentOffer) {
deadbeef1dcb1642017-03-29 21:08:16 -0700905 ASSERT_TRUE(CreatePeerConnectionWrappers());
906 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -0800907 caller()->AddAudioVideoTracks();
908 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -0700909 caller()->CreateAndSetAndSignalOffer();
910 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -0800911 {
912 MediaExpectations media_expectations;
913 media_expectations.ExpectBidirectionalAudioAndVideo();
914 ASSERT_TRUE(ExpectNewFrames(media_expectations));
915 }
deadbeef1dcb1642017-03-29 21:08:16 -0700916 // Renegotiate, rejecting the video m= section.
Seth Hampson2f0d7022018-02-20 11:54:42 -0800917 if (sdp_semantics_ == SdpSemantics::kPlanB) {
918 caller()->SetGeneratedSdpMunger(
919 [](cricket::SessionDescription* description) {
920 for (cricket::ContentInfo& content : description->contents()) {
921 if (cricket::IsVideoContent(&content)) {
922 content.rejected = true;
923 }
924 }
925 });
926 } else {
Harald Alvestrand6060df52020-08-11 09:54:02 +0200927 caller()
928 ->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)
929 ->StopInternal();
Seth Hampson2f0d7022018-02-20 11:54:42 -0800930 }
deadbeef1dcb1642017-03-29 21:08:16 -0700931 caller()->CreateAndSetAndSignalOffer();
932 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
933
934 // Sanity check that the caller's description has a rejected video section.
935 ASSERT_NE(nullptr, caller()->pc()->local_description());
936 const ContentInfo* caller_video_content =
937 GetFirstVideoContent(caller()->pc()->local_description()->description());
938 ASSERT_NE(nullptr, caller_video_content);
939 EXPECT_TRUE(caller_video_content->rejected);
deadbeef1dcb1642017-03-29 21:08:16 -0700940 // Wait for some additional audio frames to be received.
Seth Hampson2f0d7022018-02-20 11:54:42 -0800941 {
942 MediaExpectations media_expectations;
943 media_expectations.ExpectBidirectionalAudio();
944 media_expectations.ExpectNoVideo();
945 ASSERT_TRUE(ExpectNewFrames(media_expectations));
946 }
deadbeef1dcb1642017-03-29 21:08:16 -0700947}
948
Taylor Brandstetter60c8dc82018-04-11 15:20:27 -0700949// Do one offer/answer with audio, another that disables it (rejecting the m=
950// section), and another that re-enables it. Regression test for:
951// bugs.webrtc.org/6023
952TEST_F(PeerConnectionIntegrationTestPlanB, EnableAudioAfterRejecting) {
953 ASSERT_TRUE(CreatePeerConnectionWrappers());
954 ConnectFakeSignaling();
955
956 // Add audio track, do normal offer/answer.
957 rtc::scoped_refptr<webrtc::AudioTrackInterface> track =
958 caller()->CreateLocalAudioTrack();
959 rtc::scoped_refptr<webrtc::RtpSenderInterface> sender =
960 caller()->pc()->AddTrack(track, {"stream"}).MoveValue();
961 caller()->CreateAndSetAndSignalOffer();
962 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
963
964 // Remove audio track, and set offer_to_receive_audio to false to cause the
965 // m= section to be completely disabled, not just "recvonly".
966 caller()->pc()->RemoveTrack(sender);
967 PeerConnectionInterface::RTCOfferAnswerOptions options;
968 options.offer_to_receive_audio = 0;
969 caller()->SetOfferAnswerOptions(options);
970 caller()->CreateAndSetAndSignalOffer();
971 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
972
973 // Add the audio track again, expecting negotiation to succeed and frames to
974 // flow.
975 sender = caller()->pc()->AddTrack(track, {"stream"}).MoveValue();
976 options.offer_to_receive_audio = 1;
977 caller()->SetOfferAnswerOptions(options);
978 caller()->CreateAndSetAndSignalOffer();
979 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
980
981 MediaExpectations media_expectations;
982 media_expectations.CalleeExpectsSomeAudio();
983 EXPECT_TRUE(ExpectNewFrames(media_expectations));
984}
985
deadbeef1dcb1642017-03-29 21:08:16 -0700986// Basic end-to-end test, but without SSRC/MSID signaling. This functionality
987// is needed to support legacy endpoints.
988// TODO(deadbeef): When we support the MID extension and demuxing on MID, also
989// add a test for an end-to-end test without MID signaling either (basically,
990// the minimum acceptable SDP).
Seth Hampson2f0d7022018-02-20 11:54:42 -0800991TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithoutSsrcOrMsidSignaling) {
deadbeef1dcb1642017-03-29 21:08:16 -0700992 ASSERT_TRUE(CreatePeerConnectionWrappers());
993 ConnectFakeSignaling();
994 // Add audio and video, testing that packets can be demuxed on payload type.
Steve Anton15324772018-01-16 10:26:49 -0800995 caller()->AddAudioVideoTracks();
996 callee()->AddAudioVideoTracks();
deadbeefd8ad7882017-04-18 16:01:17 -0700997 // Remove SSRCs and MSIDs from the received offer SDP.
998 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
deadbeef1dcb1642017-03-29 21:08:16 -0700999 caller()->CreateAndSetAndSignalOffer();
1000 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001001 MediaExpectations media_expectations;
1002 media_expectations.ExpectBidirectionalAudioAndVideo();
1003 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07001004}
1005
Seth Hampson5897a6e2018-04-03 11:16:33 -07001006// Basic end-to-end test, without SSRC signaling. This means that the track
1007// was created properly and frames are delivered when the MSIDs are communicated
1008// with a=msid lines and no a=ssrc lines.
1009TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
1010 EndToEndCallWithoutSsrcSignaling) {
1011 const char kStreamId[] = "streamId";
1012 ASSERT_TRUE(CreatePeerConnectionWrappers());
1013 ConnectFakeSignaling();
1014 // Add just audio tracks.
1015 caller()->AddTrack(caller()->CreateLocalAudioTrack(), {kStreamId});
1016 callee()->AddAudioTrack();
1017
1018 // Remove SSRCs from the received offer SDP.
1019 callee()->SetReceivedSdpMunger(RemoveSsrcsAndKeepMsids);
1020 caller()->CreateAndSetAndSignalOffer();
1021 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1022 MediaExpectations media_expectations;
1023 media_expectations.ExpectBidirectionalAudio();
1024 ASSERT_TRUE(ExpectNewFrames(media_expectations));
1025}
1026
Johannes Kron3e983682020-03-29 22:17:00 +02001027TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
1028 EndToEndCallAddReceiveVideoToSendOnlyCall) {
1029 ASSERT_TRUE(CreatePeerConnectionWrappers());
1030 ConnectFakeSignaling();
1031 // Add one-directional video, from caller to callee.
1032 rtc::scoped_refptr<webrtc::VideoTrackInterface> track =
1033 caller()->CreateLocalVideoTrack();
1034
1035 RtpTransceiverInit video_transceiver_init;
1036 video_transceiver_init.stream_ids = {"video1"};
1037 video_transceiver_init.direction = RtpTransceiverDirection::kSendOnly;
1038 auto video_sender =
1039 caller()->pc()->AddTransceiver(track, video_transceiver_init).MoveValue();
1040 caller()->CreateAndSetAndSignalOffer();
1041 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1042
1043 // Add receive direction.
Harald Alvestrand6060df52020-08-11 09:54:02 +02001044 video_sender->SetDirectionWithError(RtpTransceiverDirection::kSendRecv);
Johannes Kron3e983682020-03-29 22:17:00 +02001045
1046 rtc::scoped_refptr<webrtc::VideoTrackInterface> callee_track =
1047 callee()->CreateLocalVideoTrack();
1048
1049 callee()->AddTrack(callee_track);
1050 caller()->CreateAndSetAndSignalOffer();
1051 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1052 // Ensure that video frames are received end-to-end.
1053 MediaExpectations media_expectations;
1054 media_expectations.ExpectBidirectionalVideo();
1055 ASSERT_TRUE(ExpectNewFrames(media_expectations));
1056}
1057
Steve Antondf527fd2018-04-27 15:52:03 -07001058// Tests that video flows between multiple video tracks when SSRCs are not
1059// signaled. This exercises the MID RTP header extension which is needed to
1060// demux the incoming video tracks.
1061TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
1062 EndToEndCallWithTwoVideoTracksAndNoSignaledSsrc) {
1063 ASSERT_TRUE(CreatePeerConnectionWrappers());
1064 ConnectFakeSignaling();
1065 caller()->AddVideoTrack();
1066 caller()->AddVideoTrack();
1067 callee()->AddVideoTrack();
1068 callee()->AddVideoTrack();
1069
1070 caller()->SetReceivedSdpMunger(&RemoveSsrcsAndKeepMsids);
1071 callee()->SetReceivedSdpMunger(&RemoveSsrcsAndKeepMsids);
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
1077 // Expect video to be received in both directions on both tracks.
1078 MediaExpectations media_expectations;
1079 media_expectations.ExpectBidirectionalVideo();
1080 EXPECT_TRUE(ExpectNewFrames(media_expectations));
1081}
1082
Taylor Brandstetterd3ef4992020-10-15 18:22:57 -07001083// Used for the test below.
1084void RemoveBundleGroupSsrcsAndMidExtension(cricket::SessionDescription* desc) {
1085 RemoveSsrcsAndKeepMsids(desc);
1086 desc->RemoveGroupByName("BUNDLE");
1087 for (ContentInfo& content : desc->contents()) {
1088 cricket::MediaContentDescription* media = content.media_description();
1089 cricket::RtpHeaderExtensions extensions = media->rtp_header_extensions();
1090 extensions.erase(std::remove_if(extensions.begin(), extensions.end(),
1091 [](const RtpExtension& extension) {
1092 return extension.uri ==
1093 RtpExtension::kMidUri;
1094 }),
1095 extensions.end());
1096 media->set_rtp_header_extensions(extensions);
1097 }
1098}
1099
1100// Tests that video flows between multiple video tracks when BUNDLE is not used,
1101// SSRCs are not signaled and the MID RTP header extension is not used. This
1102// relies on demuxing by payload type, which normally doesn't work if you have
1103// multiple media sections using the same payload type, but which should work as
1104// long as the media sections aren't bundled.
1105// Regression test for: http://crbug.com/webrtc/12023
1106TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
1107 EndToEndCallWithTwoVideoTracksNoBundleNoSignaledSsrcAndNoMid) {
1108 ASSERT_TRUE(CreatePeerConnectionWrappers());
1109 ConnectFakeSignaling();
1110 caller()->AddVideoTrack();
1111 caller()->AddVideoTrack();
1112 callee()->AddVideoTrack();
1113 callee()->AddVideoTrack();
1114 caller()->SetReceivedSdpMunger(&RemoveBundleGroupSsrcsAndMidExtension);
1115 callee()->SetReceivedSdpMunger(&RemoveBundleGroupSsrcsAndMidExtension);
1116 caller()->CreateAndSetAndSignalOffer();
1117 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1118 ASSERT_EQ(2u, caller()->pc()->GetReceivers().size());
1119 ASSERT_EQ(2u, callee()->pc()->GetReceivers().size());
1120 // Make sure we are not bundled.
1121 ASSERT_NE(caller()->pc()->GetSenders()[0]->dtls_transport(),
1122 caller()->pc()->GetSenders()[1]->dtls_transport());
1123
1124 // Expect video to be received in both directions on both tracks.
1125 MediaExpectations media_expectations;
1126 media_expectations.ExpectBidirectionalVideo();
1127 EXPECT_TRUE(ExpectNewFrames(media_expectations));
1128}
1129
1130// Used for the test below.
1131void ModifyPayloadTypesAndRemoveMidExtension(
1132 cricket::SessionDescription* desc) {
1133 int pt = 96;
1134 for (ContentInfo& content : desc->contents()) {
1135 cricket::MediaContentDescription* media = content.media_description();
1136 cricket::RtpHeaderExtensions extensions = media->rtp_header_extensions();
1137 extensions.erase(std::remove_if(extensions.begin(), extensions.end(),
1138 [](const RtpExtension& extension) {
1139 return extension.uri ==
1140 RtpExtension::kMidUri;
1141 }),
1142 extensions.end());
1143 media->set_rtp_header_extensions(extensions);
1144 cricket::VideoContentDescription* video = media->as_video();
1145 ASSERT_TRUE(video != nullptr);
1146 std::vector<cricket::VideoCodec> codecs = {{pt++, "VP8"}};
1147 video->set_codecs(codecs);
1148 }
1149}
1150
1151// Tests that two video tracks can be demultiplexed by payload type alone, by
1152// using different payload types for the same codec in different m= sections.
1153// This practice is discouraged but historically has been supported.
1154// Regression test for: http://crbug.com/webrtc/12029
1155TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
1156 EndToEndCallWithTwoVideoTracksDemultiplexedByPayloadType) {
1157 ASSERT_TRUE(CreatePeerConnectionWrappers());
1158 ConnectFakeSignaling();
1159 caller()->AddVideoTrack();
1160 caller()->AddVideoTrack();
1161 callee()->AddVideoTrack();
1162 callee()->AddVideoTrack();
1163 caller()->SetGeneratedSdpMunger(&ModifyPayloadTypesAndRemoveMidExtension);
1164 callee()->SetGeneratedSdpMunger(&ModifyPayloadTypesAndRemoveMidExtension);
1165 // We can't remove SSRCs from the generated SDP because then no send streams
1166 // would be created.
1167 caller()->SetReceivedSdpMunger(&RemoveSsrcsAndKeepMsids);
1168 callee()->SetReceivedSdpMunger(&RemoveSsrcsAndKeepMsids);
1169 caller()->CreateAndSetAndSignalOffer();
1170 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1171 ASSERT_EQ(2u, caller()->pc()->GetReceivers().size());
1172 ASSERT_EQ(2u, callee()->pc()->GetReceivers().size());
1173 // Make sure we are bundled.
1174 ASSERT_EQ(caller()->pc()->GetSenders()[0]->dtls_transport(),
1175 caller()->pc()->GetSenders()[1]->dtls_transport());
1176
1177 // Expect video to be received in both directions on both tracks.
1178 MediaExpectations media_expectations;
1179 media_expectations.ExpectBidirectionalVideo();
1180 EXPECT_TRUE(ExpectNewFrames(media_expectations));
1181}
1182
Henrik Boström5b147782018-12-04 11:25:05 +01001183TEST_F(PeerConnectionIntegrationTestUnifiedPlan, NoStreamsMsidLinePresent) {
1184 ASSERT_TRUE(CreatePeerConnectionWrappers());
1185 ConnectFakeSignaling();
1186 caller()->AddAudioTrack();
1187 caller()->AddVideoTrack();
1188 caller()->CreateAndSetAndSignalOffer();
1189 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1190 auto callee_receivers = callee()->pc()->GetReceivers();
1191 ASSERT_EQ(2u, callee_receivers.size());
1192 EXPECT_TRUE(callee_receivers[0]->stream_ids().empty());
1193 EXPECT_TRUE(callee_receivers[1]->stream_ids().empty());
1194}
1195
1196TEST_F(PeerConnectionIntegrationTestUnifiedPlan, NoStreamsMsidLineMissing) {
1197 ASSERT_TRUE(CreatePeerConnectionWrappers());
1198 ConnectFakeSignaling();
1199 caller()->AddAudioTrack();
1200 caller()->AddVideoTrack();
1201 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
1202 caller()->CreateAndSetAndSignalOffer();
1203 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1204 auto callee_receivers = callee()->pc()->GetReceivers();
1205 ASSERT_EQ(2u, callee_receivers.size());
1206 ASSERT_EQ(1u, callee_receivers[0]->stream_ids().size());
1207 ASSERT_EQ(1u, callee_receivers[1]->stream_ids().size());
1208 EXPECT_EQ(callee_receivers[0]->stream_ids()[0],
1209 callee_receivers[1]->stream_ids()[0]);
1210 EXPECT_EQ(callee_receivers[0]->streams()[0],
1211 callee_receivers[1]->streams()[0]);
1212}
1213
deadbeef1dcb1642017-03-29 21:08:16 -07001214// Test that if two video tracks are sent (from caller to callee, in this test),
1215// they're transmitted correctly end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001216TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithTwoVideoTracks) {
deadbeef1dcb1642017-03-29 21:08:16 -07001217 ASSERT_TRUE(CreatePeerConnectionWrappers());
1218 ConnectFakeSignaling();
1219 // Add one audio/video stream, and one video-only stream.
Steve Anton15324772018-01-16 10:26:49 -08001220 caller()->AddAudioVideoTracks();
1221 caller()->AddVideoTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07001222 caller()->CreateAndSetAndSignalOffer();
1223 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Steve Anton15324772018-01-16 10:26:49 -08001224 ASSERT_EQ(3u, callee()->pc()->GetReceivers().size());
Seth Hampson2f0d7022018-02-20 11:54:42 -08001225
1226 MediaExpectations media_expectations;
1227 media_expectations.CalleeExpectsSomeAudioAndVideo();
1228 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07001229}
1230
1231static void MakeSpecCompliantMaxBundleOffer(cricket::SessionDescription* desc) {
1232 bool first = true;
1233 for (cricket::ContentInfo& content : desc->contents()) {
1234 if (first) {
1235 first = false;
1236 continue;
1237 }
1238 content.bundle_only = true;
1239 }
1240 first = true;
1241 for (cricket::TransportInfo& transport : desc->transport_infos()) {
1242 if (first) {
1243 first = false;
1244 continue;
1245 }
1246 transport.description.ice_ufrag.clear();
1247 transport.description.ice_pwd.clear();
1248 transport.description.connection_role = cricket::CONNECTIONROLE_NONE;
1249 transport.description.identity_fingerprint.reset(nullptr);
1250 }
1251}
1252
1253// Test that if applying a true "max bundle" offer, which uses ports of 0,
1254// "a=bundle-only", omitting "a=fingerprint", "a=setup", "a=ice-ufrag" and
1255// "a=ice-pwd" for all but the audio "m=" section, negotiation still completes
1256// successfully and media flows.
1257// TODO(deadbeef): Update this test to also omit "a=rtcp-mux", once that works.
1258// TODO(deadbeef): Won't need this test once we start generating actual
1259// standards-compliant SDP.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001260TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07001261 EndToEndCallWithSpecCompliantMaxBundleOffer) {
1262 ASSERT_TRUE(CreatePeerConnectionWrappers());
1263 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001264 caller()->AddAudioVideoTracks();
1265 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001266 // Do the equivalent of setting the port to 0, adding a=bundle-only, and
1267 // removing a=ice-ufrag, a=ice-pwd, a=fingerprint and a=setup from all
1268 // but the first m= section.
1269 callee()->SetReceivedSdpMunger(MakeSpecCompliantMaxBundleOffer);
1270 caller()->CreateAndSetAndSignalOffer();
1271 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001272 MediaExpectations media_expectations;
1273 media_expectations.ExpectBidirectionalAudioAndVideo();
1274 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07001275}
1276
1277// Test that we can receive the audio output level from a remote audio track.
1278// TODO(deadbeef): Use a fake audio source and verify that the output level is
1279// exactly what the source on the other side was configured with.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001280TEST_P(PeerConnectionIntegrationTest, GetAudioOutputLevelStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-29 21:08:16 -07001281 ASSERT_TRUE(CreatePeerConnectionWrappers());
1282 ConnectFakeSignaling();
1283 // Just add an audio track.
Steve Anton15324772018-01-16 10:26:49 -08001284 caller()->AddAudioTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07001285 caller()->CreateAndSetAndSignalOffer();
1286 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1287
1288 // Get the audio output level stats. Note that the level is not available
1289 // until an RTCP packet has been received.
deadbeefd8ad7882017-04-18 16:01:17 -07001290 EXPECT_TRUE_WAIT(callee()->OldGetStats()->AudioOutputLevel() > 0,
deadbeef1dcb1642017-03-29 21:08:16 -07001291 kMaxWaitForFramesMs);
1292}
1293
1294// Test that an audio input level is reported.
1295// TODO(deadbeef): Use a fake audio source and verify that the input level is
1296// exactly what the source was configured with.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001297TEST_P(PeerConnectionIntegrationTest, GetAudioInputLevelStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-29 21:08:16 -07001298 ASSERT_TRUE(CreatePeerConnectionWrappers());
1299 ConnectFakeSignaling();
1300 // Just add an audio track.
Steve Anton15324772018-01-16 10:26:49 -08001301 caller()->AddAudioTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07001302 caller()->CreateAndSetAndSignalOffer();
1303 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1304
1305 // Get the audio input level stats. The level should be available very
1306 // soon after the test starts.
deadbeefd8ad7882017-04-18 16:01:17 -07001307 EXPECT_TRUE_WAIT(caller()->OldGetStats()->AudioInputLevel() > 0,
deadbeef1dcb1642017-03-29 21:08:16 -07001308 kMaxWaitForStatsMs);
1309}
1310
1311// Test that we can get incoming byte counts from both audio and video tracks.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001312TEST_P(PeerConnectionIntegrationTest, GetBytesReceivedStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-29 21:08:16 -07001313 ASSERT_TRUE(CreatePeerConnectionWrappers());
1314 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001315 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001316 // Do offer/answer, wait for the callee to receive some frames.
1317 caller()->CreateAndSetAndSignalOffer();
1318 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001319
1320 MediaExpectations media_expectations;
1321 media_expectations.CalleeExpectsSomeAudioAndVideo();
1322 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07001323
1324 // Get a handle to the remote tracks created, so they can be used as GetStats
1325 // filters.
Mirko Bonadei739baf02019-01-27 17:29:42 +01001326 for (const auto& receiver : callee()->pc()->GetReceivers()) {
Steve Anton15324772018-01-16 10:26:49 -08001327 // We received frames, so we definitely should have nonzero "received bytes"
1328 // stats at this point.
1329 EXPECT_GT(callee()->OldGetStatsForTrack(receiver->track())->BytesReceived(),
1330 0);
1331 }
deadbeef1dcb1642017-03-29 21:08:16 -07001332}
1333
1334// Test that we can get outgoing byte counts from both audio and video tracks.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001335TEST_P(PeerConnectionIntegrationTest, GetBytesSentStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-29 21:08:16 -07001336 ASSERT_TRUE(CreatePeerConnectionWrappers());
1337 ConnectFakeSignaling();
1338 auto audio_track = caller()->CreateLocalAudioTrack();
1339 auto video_track = caller()->CreateLocalVideoTrack();
Steve Anton15324772018-01-16 10:26:49 -08001340 caller()->AddTrack(audio_track);
1341 caller()->AddTrack(video_track);
deadbeef1dcb1642017-03-29 21:08:16 -07001342 // Do offer/answer, wait for the callee to receive some frames.
1343 caller()->CreateAndSetAndSignalOffer();
1344 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001345 MediaExpectations media_expectations;
1346 media_expectations.CalleeExpectsSomeAudioAndVideo();
1347 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07001348
1349 // The callee received frames, so we definitely should have nonzero "sent
1350 // bytes" stats at this point.
deadbeefd8ad7882017-04-18 16:01:17 -07001351 EXPECT_GT(caller()->OldGetStatsForTrack(audio_track)->BytesSent(), 0);
1352 EXPECT_GT(caller()->OldGetStatsForTrack(video_track)->BytesSent(), 0);
1353}
1354
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02001355// Test that we can get capture start ntp time.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001356TEST_P(PeerConnectionIntegrationTest, GetCaptureStartNtpTimeWithOldStatsApi) {
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02001357 ASSERT_TRUE(CreatePeerConnectionWrappers());
1358 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001359 caller()->AddAudioTrack();
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02001360
Steve Anton15324772018-01-16 10:26:49 -08001361 callee()->AddAudioTrack();
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02001362
1363 // Do offer/answer, wait for the callee to receive some frames.
1364 caller()->CreateAndSetAndSignalOffer();
1365 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1366
1367 // Get the remote audio track created on the receiver, so they can be used as
1368 // GetStats filters.
Steve Antonfc853712018-03-01 13:48:58 -08001369 auto receivers = callee()->pc()->GetReceivers();
1370 ASSERT_EQ(1u, receivers.size());
1371 auto remote_audio_track = receivers[0]->track();
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02001372
1373 // Get the audio output level stats. Note that the level is not available
1374 // until an RTCP packet has been received.
Zhi Huange830e682018-03-30 10:48:35 -07001375 EXPECT_TRUE_WAIT(
1376 callee()->OldGetStatsForTrack(remote_audio_track)->CaptureStartNtpTime() >
1377 0,
1378 2 * kMaxWaitForFramesMs);
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02001379}
1380
Steve Antona41959e2018-11-28 11:15:33 -08001381// Test that the track ID is associated with all local and remote SSRC stats
1382// using the old GetStats() and more than 1 audio and more than 1 video track.
1383// This is a regression test for crbug.com/906988
1384TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
1385 OldGetStatsAssociatesTrackIdForManyMediaSections) {
1386 ASSERT_TRUE(CreatePeerConnectionWrappers());
1387 ConnectFakeSignaling();
1388 auto audio_sender_1 = caller()->AddAudioTrack();
1389 auto video_sender_1 = caller()->AddVideoTrack();
1390 auto audio_sender_2 = caller()->AddAudioTrack();
1391 auto video_sender_2 = caller()->AddVideoTrack();
1392 caller()->CreateAndSetAndSignalOffer();
1393 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1394
1395 MediaExpectations media_expectations;
1396 media_expectations.CalleeExpectsSomeAudioAndVideo();
1397 ASSERT_TRUE_WAIT(ExpectNewFrames(media_expectations), kDefaultTimeout);
1398
1399 std::vector<std::string> track_ids = {
1400 audio_sender_1->track()->id(), video_sender_1->track()->id(),
1401 audio_sender_2->track()->id(), video_sender_2->track()->id()};
1402
1403 auto caller_stats = caller()->OldGetStats();
1404 EXPECT_THAT(caller_stats->TrackIds(), UnorderedElementsAreArray(track_ids));
1405 auto callee_stats = callee()->OldGetStats();
1406 EXPECT_THAT(callee_stats->TrackIds(), UnorderedElementsAreArray(track_ids));
1407}
1408
Steve Antonffa6ce42018-11-30 09:26:08 -08001409// Test that the new GetStats() returns stats for all outgoing/incoming streams
1410// with the correct track IDs if there are more than one audio and more than one
1411// video senders/receivers.
1412TEST_P(PeerConnectionIntegrationTest, NewGetStatsManyAudioAndManyVideoStreams) {
1413 ASSERT_TRUE(CreatePeerConnectionWrappers());
1414 ConnectFakeSignaling();
1415 auto audio_sender_1 = caller()->AddAudioTrack();
1416 auto video_sender_1 = caller()->AddVideoTrack();
1417 auto audio_sender_2 = caller()->AddAudioTrack();
1418 auto video_sender_2 = caller()->AddVideoTrack();
1419 caller()->CreateAndSetAndSignalOffer();
1420 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1421
1422 MediaExpectations media_expectations;
1423 media_expectations.CalleeExpectsSomeAudioAndVideo();
1424 ASSERT_TRUE_WAIT(ExpectNewFrames(media_expectations), kDefaultTimeout);
1425
1426 std::vector<std::string> track_ids = {
1427 audio_sender_1->track()->id(), video_sender_1->track()->id(),
1428 audio_sender_2->track()->id(), video_sender_2->track()->id()};
1429
1430 rtc::scoped_refptr<const webrtc::RTCStatsReport> caller_report =
1431 caller()->NewGetStats();
1432 ASSERT_TRUE(caller_report);
1433 auto outbound_stream_stats =
1434 caller_report->GetStatsOfType<webrtc::RTCOutboundRTPStreamStats>();
Henrik Boströma0ff50c2020-05-05 15:54:46 +02001435 ASSERT_EQ(outbound_stream_stats.size(), 4u);
Steve Antonffa6ce42018-11-30 09:26:08 -08001436 std::vector<std::string> outbound_track_ids;
1437 for (const auto& stat : outbound_stream_stats) {
1438 ASSERT_TRUE(stat->bytes_sent.is_defined());
1439 EXPECT_LT(0u, *stat->bytes_sent);
Rasmus Brandt2efae772019-06-27 14:29:34 +02001440 if (*stat->kind == "video") {
1441 ASSERT_TRUE(stat->key_frames_encoded.is_defined());
1442 EXPECT_GT(*stat->key_frames_encoded, 0u);
1443 ASSERT_TRUE(stat->frames_encoded.is_defined());
1444 EXPECT_GE(*stat->frames_encoded, *stat->key_frames_encoded);
1445 }
Steve Antonffa6ce42018-11-30 09:26:08 -08001446 ASSERT_TRUE(stat->track_id.is_defined());
1447 const auto* track_stat =
1448 caller_report->GetAs<webrtc::RTCMediaStreamTrackStats>(*stat->track_id);
1449 ASSERT_TRUE(track_stat);
1450 outbound_track_ids.push_back(*track_stat->track_identifier);
1451 }
1452 EXPECT_THAT(outbound_track_ids, UnorderedElementsAreArray(track_ids));
1453
1454 rtc::scoped_refptr<const webrtc::RTCStatsReport> callee_report =
1455 callee()->NewGetStats();
1456 ASSERT_TRUE(callee_report);
1457 auto inbound_stream_stats =
1458 callee_report->GetStatsOfType<webrtc::RTCInboundRTPStreamStats>();
1459 ASSERT_EQ(4u, inbound_stream_stats.size());
1460 std::vector<std::string> inbound_track_ids;
1461 for (const auto& stat : inbound_stream_stats) {
1462 ASSERT_TRUE(stat->bytes_received.is_defined());
1463 EXPECT_LT(0u, *stat->bytes_received);
Rasmus Brandt2efae772019-06-27 14:29:34 +02001464 if (*stat->kind == "video") {
1465 ASSERT_TRUE(stat->key_frames_decoded.is_defined());
1466 EXPECT_GT(*stat->key_frames_decoded, 0u);
1467 ASSERT_TRUE(stat->frames_decoded.is_defined());
1468 EXPECT_GE(*stat->frames_decoded, *stat->key_frames_decoded);
1469 }
Steve Antonffa6ce42018-11-30 09:26:08 -08001470 ASSERT_TRUE(stat->track_id.is_defined());
1471 const auto* track_stat =
1472 callee_report->GetAs<webrtc::RTCMediaStreamTrackStats>(*stat->track_id);
1473 ASSERT_TRUE(track_stat);
1474 inbound_track_ids.push_back(*track_stat->track_identifier);
1475 }
1476 EXPECT_THAT(inbound_track_ids, UnorderedElementsAreArray(track_ids));
1477}
1478
1479// Test that we can get stats (using the new stats implementation) for
deadbeefd8ad7882017-04-18 16:01:17 -07001480// unsignaled streams. Meaning when SSRCs/MSIDs aren't signaled explicitly in
1481// SDP.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001482TEST_P(PeerConnectionIntegrationTest,
deadbeefd8ad7882017-04-18 16:01:17 -07001483 GetStatsForUnsignaledStreamWithNewStatsApi) {
1484 ASSERT_TRUE(CreatePeerConnectionWrappers());
1485 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001486 caller()->AddAudioTrack();
deadbeefd8ad7882017-04-18 16:01:17 -07001487 // Remove SSRCs and MSIDs from the received offer SDP.
1488 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
1489 caller()->CreateAndSetAndSignalOffer();
1490 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001491 MediaExpectations media_expectations;
1492 media_expectations.CalleeExpectsSomeAudio(1);
1493 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeefd8ad7882017-04-18 16:01:17 -07001494
1495 // We received a frame, so we should have nonzero "bytes received" stats for
1496 // the unsignaled stream, if stats are working for it.
1497 rtc::scoped_refptr<const webrtc::RTCStatsReport> report =
1498 callee()->NewGetStats();
1499 ASSERT_NE(nullptr, report);
1500 auto inbound_stream_stats =
1501 report->GetStatsOfType<webrtc::RTCInboundRTPStreamStats>();
1502 ASSERT_EQ(1U, inbound_stream_stats.size());
1503 ASSERT_TRUE(inbound_stream_stats[0]->bytes_received.is_defined());
1504 ASSERT_GT(*inbound_stream_stats[0]->bytes_received, 0U);
zhihuangf8164932017-05-19 13:09:47 -07001505 ASSERT_TRUE(inbound_stream_stats[0]->track_id.is_defined());
1506}
1507
Taylor Brandstettera4653442018-06-19 09:44:26 -07001508// Same as above but for the legacy stats implementation.
1509TEST_P(PeerConnectionIntegrationTest,
1510 GetStatsForUnsignaledStreamWithOldStatsApi) {
1511 ASSERT_TRUE(CreatePeerConnectionWrappers());
1512 ConnectFakeSignaling();
1513 caller()->AddAudioTrack();
1514 // Remove SSRCs and MSIDs from the received offer SDP.
1515 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
1516 caller()->CreateAndSetAndSignalOffer();
1517 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1518
1519 // Note that, since the old stats implementation associates SSRCs with tracks
1520 // using SDP, when SSRCs aren't signaled in SDP these stats won't have an
1521 // associated track ID. So we can't use the track "selector" argument.
1522 //
1523 // Also, we use "EXPECT_TRUE_WAIT" because the stats collector may decide to
1524 // return cached stats if not enough time has passed since the last update.
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02001525 EXPECT_TRUE_WAIT(callee()->OldGetStats()->BytesReceived() > 0,
Taylor Brandstettera4653442018-06-19 09:44:26 -07001526 kDefaultTimeout);
1527}
1528
zhihuangf8164932017-05-19 13:09:47 -07001529// Test that we can successfully get the media related stats (audio level
1530// etc.) for the unsignaled stream.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001531TEST_P(PeerConnectionIntegrationTest,
zhihuangf8164932017-05-19 13:09:47 -07001532 GetMediaStatsForUnsignaledStreamWithNewStatsApi) {
1533 ASSERT_TRUE(CreatePeerConnectionWrappers());
1534 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001535 caller()->AddAudioVideoTracks();
zhihuangf8164932017-05-19 13:09:47 -07001536 // Remove SSRCs and MSIDs from the received offer SDP.
1537 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
1538 caller()->CreateAndSetAndSignalOffer();
1539 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001540 MediaExpectations media_expectations;
1541 media_expectations.CalleeExpectsSomeAudio(1);
1542 media_expectations.CalleeExpectsSomeVideo(1);
1543 ASSERT_TRUE(ExpectNewFrames(media_expectations));
zhihuangf8164932017-05-19 13:09:47 -07001544
1545 rtc::scoped_refptr<const webrtc::RTCStatsReport> report =
1546 callee()->NewGetStats();
1547 ASSERT_NE(nullptr, report);
1548
1549 auto media_stats = report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
1550 auto audio_index = FindFirstMediaStatsIndexByKind("audio", media_stats);
1551 ASSERT_GE(audio_index, 0);
1552 EXPECT_TRUE(media_stats[audio_index]->audio_level.is_defined());
deadbeef1dcb1642017-03-29 21:08:16 -07001553}
1554
deadbeef4e2deab2017-09-20 13:56:21 -07001555// Helper for test below.
1556void ModifySsrcs(cricket::SessionDescription* desc) {
1557 for (ContentInfo& content : desc->contents()) {
Steve Antondf527fd2018-04-27 15:52:03 -07001558 for (StreamParams& stream :
Steve Antonb1c1de12017-12-21 15:14:30 -08001559 content.media_description()->mutable_streams()) {
deadbeef4e2deab2017-09-20 13:56:21 -07001560 for (uint32_t& ssrc : stream.ssrcs) {
1561 ssrc = rtc::CreateRandomId();
1562 }
1563 }
1564 }
1565}
1566
1567// Test that the "RTCMediaSteamTrackStats" object is updated correctly when
1568// SSRCs are unsignaled, and the SSRC of the received (audio) stream changes.
1569// This should result in two "RTCInboundRTPStreamStats", but only one
1570// "RTCMediaStreamTrackStats", whose counters go up continuously rather than
1571// being reset to 0 once the SSRC change occurs.
1572//
1573// Regression test for this bug:
1574// https://bugs.chromium.org/p/webrtc/issues/detail?id=8158
1575//
1576// The bug causes the track stats to only represent one of the two streams:
1577// whichever one has the higher SSRC. So with this bug, there was a 50% chance
1578// that the track stat counters would reset to 0 when the new stream is
1579// received, and a 50% chance that they'll stop updating (while
1580// "concealed_samples" continues increasing, due to silence being generated for
1581// the inactive stream).
Seth Hampson2f0d7022018-02-20 11:54:42 -08001582TEST_P(PeerConnectionIntegrationTest,
Steve Anton83119dd2017-11-10 16:19:52 -08001583 TrackStatsUpdatedCorrectlyWhenUnsignaledSsrcChanges) {
deadbeef4e2deab2017-09-20 13:56:21 -07001584 ASSERT_TRUE(CreatePeerConnectionWrappers());
1585 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001586 caller()->AddAudioTrack();
deadbeef4e2deab2017-09-20 13:56:21 -07001587 // Remove SSRCs and MSIDs from the received offer SDP, simulating an endpoint
1588 // that doesn't signal SSRCs (from the callee's perspective).
1589 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
1590 caller()->CreateAndSetAndSignalOffer();
1591 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1592 // Wait for 50 audio frames (500ms of audio) to be received by the callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001593 {
1594 MediaExpectations media_expectations;
1595 media_expectations.CalleeExpectsSomeAudio(50);
1596 ASSERT_TRUE(ExpectNewFrames(media_expectations));
1597 }
deadbeef4e2deab2017-09-20 13:56:21 -07001598 // Some audio frames were received, so we should have nonzero "samples
1599 // received" for the track.
1600 rtc::scoped_refptr<const webrtc::RTCStatsReport> report =
1601 callee()->NewGetStats();
1602 ASSERT_NE(nullptr, report);
1603 auto track_stats = report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
1604 ASSERT_EQ(1U, track_stats.size());
1605 ASSERT_TRUE(track_stats[0]->total_samples_received.is_defined());
1606 ASSERT_GT(*track_stats[0]->total_samples_received, 0U);
1607 // uint64_t prev_samples_received = *track_stats[0]->total_samples_received;
1608
1609 // Create a new offer and munge it to cause the caller to use a new SSRC.
1610 caller()->SetGeneratedSdpMunger(ModifySsrcs);
1611 caller()->CreateAndSetAndSignalOffer();
1612 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1613 // Wait for 25 more audio frames (250ms of audio) to be received, from the new
1614 // SSRC.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001615 {
1616 MediaExpectations media_expectations;
1617 media_expectations.CalleeExpectsSomeAudio(25);
1618 ASSERT_TRUE(ExpectNewFrames(media_expectations));
1619 }
deadbeef4e2deab2017-09-20 13:56:21 -07001620
1621 report = callee()->NewGetStats();
1622 ASSERT_NE(nullptr, report);
1623 track_stats = report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
1624 ASSERT_EQ(1U, track_stats.size());
1625 ASSERT_TRUE(track_stats[0]->total_samples_received.is_defined());
1626 // The "total samples received" stat should only be greater than it was
1627 // before.
1628 // TODO(deadbeef): Uncomment this assertion once the bug is completely fixed.
1629 // Right now, the new SSRC will cause the counters to reset to 0.
1630 // EXPECT_GT(*track_stats[0]->total_samples_received, prev_samples_received);
1631
1632 // Additionally, the percentage of concealed samples (samples generated to
Steve Anton83119dd2017-11-10 16:19:52 -08001633 // conceal packet loss) should be less than 50%. If it's greater, that's a
deadbeef4e2deab2017-09-20 13:56:21 -07001634 // good sign that we're seeing stats from the old stream that's no longer
1635 // receiving packets, and is generating concealed samples of silence.
Steve Anton83119dd2017-11-10 16:19:52 -08001636 constexpr double kAcceptableConcealedSamplesPercentage = 0.50;
deadbeef4e2deab2017-09-20 13:56:21 -07001637 ASSERT_TRUE(track_stats[0]->concealed_samples.is_defined());
1638 EXPECT_LT(*track_stats[0]->concealed_samples,
1639 *track_stats[0]->total_samples_received *
1640 kAcceptableConcealedSamplesPercentage);
1641
1642 // Also ensure that we have two "RTCInboundRTPStreamStats" as expected, as a
1643 // sanity check that the SSRC really changed.
1644 // TODO(deadbeef): This isn't working right now, because we're not returning
1645 // *any* stats for the inactive stream. Uncomment when the bug is completely
1646 // fixed.
1647 // auto inbound_stream_stats =
1648 // report->GetStatsOfType<webrtc::RTCInboundRTPStreamStats>();
1649 // ASSERT_EQ(2U, inbound_stream_stats.size());
1650}
1651
deadbeef1dcb1642017-03-29 21:08:16 -07001652// Test that DTLS 1.0 is used if both sides only support DTLS 1.0.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001653TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithDtls10) {
deadbeef1dcb1642017-03-29 21:08:16 -07001654 PeerConnectionFactory::Options dtls_10_options;
1655 dtls_10_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
1656 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(dtls_10_options,
1657 dtls_10_options));
1658 ConnectFakeSignaling();
1659 // Do normal offer/answer and wait for some frames to be received in each
1660 // direction.
Steve Anton15324772018-01-16 10:26:49 -08001661 caller()->AddAudioVideoTracks();
1662 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001663 caller()->CreateAndSetAndSignalOffer();
1664 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001665 MediaExpectations media_expectations;
1666 media_expectations.ExpectBidirectionalAudioAndVideo();
1667 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07001668}
1669
1670// Test getting cipher stats and UMA metrics when DTLS 1.0 is negotiated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001671TEST_P(PeerConnectionIntegrationTest, Dtls10CipherStatsAndUmaMetrics) {
deadbeef1dcb1642017-03-29 21:08:16 -07001672 PeerConnectionFactory::Options dtls_10_options;
1673 dtls_10_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
1674 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(dtls_10_options,
1675 dtls_10_options));
1676 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001677 caller()->AddAudioVideoTracks();
1678 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001679 caller()->CreateAndSetAndSignalOffer();
Qingsi Wang7fc821d2018-07-12 12:54:53 -07001680 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07001681 EXPECT_TRUE_WAIT(rtc::SSLStreamAdapter::IsAcceptableCipher(
deadbeefd8ad7882017-04-18 16:01:17 -07001682 caller()->OldGetStats()->DtlsCipher(), rtc::KT_DEFAULT),
deadbeef1dcb1642017-03-29 21:08:16 -07001683 kDefaultTimeout);
1684 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
deadbeefd8ad7882017-04-18 16:01:17 -07001685 caller()->OldGetStats()->SrtpCipher(), kDefaultTimeout);
Qingsi Wang7fc821d2018-07-12 12:54:53 -07001686 // TODO(bugs.webrtc.org/9456): Fix it.
Ying Wangef3998f2019-12-09 13:06:53 +01001687 EXPECT_METRIC_EQ(1, webrtc::metrics::NumEvents(
1688 "WebRTC.PeerConnection.SrtpCryptoSuite.Audio",
1689 kDefaultSrtpCryptoSuite));
deadbeef1dcb1642017-03-29 21:08:16 -07001690}
1691
1692// Test getting cipher stats and UMA metrics when DTLS 1.2 is negotiated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001693TEST_P(PeerConnectionIntegrationTest, Dtls12CipherStatsAndUmaMetrics) {
deadbeef1dcb1642017-03-29 21:08:16 -07001694 PeerConnectionFactory::Options dtls_12_options;
1695 dtls_12_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
1696 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(dtls_12_options,
1697 dtls_12_options));
1698 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001699 caller()->AddAudioVideoTracks();
1700 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001701 caller()->CreateAndSetAndSignalOffer();
Qingsi Wang7fc821d2018-07-12 12:54:53 -07001702 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07001703 EXPECT_TRUE_WAIT(rtc::SSLStreamAdapter::IsAcceptableCipher(
deadbeefd8ad7882017-04-18 16:01:17 -07001704 caller()->OldGetStats()->DtlsCipher(), rtc::KT_DEFAULT),
deadbeef1dcb1642017-03-29 21:08:16 -07001705 kDefaultTimeout);
1706 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
deadbeefd8ad7882017-04-18 16:01:17 -07001707 caller()->OldGetStats()->SrtpCipher(), kDefaultTimeout);
Qingsi Wang7fc821d2018-07-12 12:54:53 -07001708 // TODO(bugs.webrtc.org/9456): Fix it.
Ying Wangef3998f2019-12-09 13:06:53 +01001709 EXPECT_METRIC_EQ(1, webrtc::metrics::NumEvents(
1710 "WebRTC.PeerConnection.SrtpCryptoSuite.Audio",
1711 kDefaultSrtpCryptoSuite));
deadbeef1dcb1642017-03-29 21:08:16 -07001712}
1713
1714// Test that DTLS 1.0 can be used if the caller supports DTLS 1.2 and the
1715// callee only supports 1.0.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001716TEST_P(PeerConnectionIntegrationTest, CallerDtls12ToCalleeDtls10) {
deadbeef1dcb1642017-03-29 21:08:16 -07001717 PeerConnectionFactory::Options caller_options;
1718 caller_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
1719 PeerConnectionFactory::Options callee_options;
1720 callee_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
1721 ASSERT_TRUE(
1722 CreatePeerConnectionWrappersWithOptions(caller_options, callee_options));
1723 ConnectFakeSignaling();
1724 // Do normal offer/answer and wait for some frames to be received in each
1725 // direction.
Steve Anton15324772018-01-16 10:26:49 -08001726 caller()->AddAudioVideoTracks();
1727 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001728 caller()->CreateAndSetAndSignalOffer();
1729 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001730 MediaExpectations media_expectations;
1731 media_expectations.ExpectBidirectionalAudioAndVideo();
1732 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07001733}
1734
1735// Test that DTLS 1.0 can be used if the caller only supports DTLS 1.0 and the
1736// callee supports 1.2.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001737TEST_P(PeerConnectionIntegrationTest, CallerDtls10ToCalleeDtls12) {
deadbeef1dcb1642017-03-29 21:08:16 -07001738 PeerConnectionFactory::Options caller_options;
1739 caller_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
1740 PeerConnectionFactory::Options callee_options;
1741 callee_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
1742 ASSERT_TRUE(
1743 CreatePeerConnectionWrappersWithOptions(caller_options, callee_options));
1744 ConnectFakeSignaling();
1745 // Do normal offer/answer and wait for some frames to be received in each
1746 // direction.
Steve Anton15324772018-01-16 10:26:49 -08001747 caller()->AddAudioVideoTracks();
1748 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001749 caller()->CreateAndSetAndSignalOffer();
1750 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001751 MediaExpectations media_expectations;
1752 media_expectations.ExpectBidirectionalAudioAndVideo();
1753 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07001754}
1755
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07001756// The three tests below verify that "enable_aes128_sha1_32_crypto_cipher"
1757// works as expected; the cipher should only be used if enabled by both sides.
1758TEST_P(PeerConnectionIntegrationTest,
1759 Aes128Sha1_32_CipherNotUsedWhenOnlyCallerSupported) {
1760 PeerConnectionFactory::Options caller_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07001761 caller_options.crypto_options.srtp.enable_aes128_sha1_32_crypto_cipher = true;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07001762 PeerConnectionFactory::Options callee_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07001763 callee_options.crypto_options.srtp.enable_aes128_sha1_32_crypto_cipher =
1764 false;
Mirko Bonadei7750d802021-07-26 17:27:42 +02001765 int expected_cipher_suite = rtc::kSrtpAes128CmSha1_80;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07001766 TestNegotiatedCipherSuite(caller_options, callee_options,
1767 expected_cipher_suite);
1768}
1769
1770TEST_P(PeerConnectionIntegrationTest,
1771 Aes128Sha1_32_CipherNotUsedWhenOnlyCalleeSupported) {
1772 PeerConnectionFactory::Options caller_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07001773 caller_options.crypto_options.srtp.enable_aes128_sha1_32_crypto_cipher =
1774 false;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07001775 PeerConnectionFactory::Options callee_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07001776 callee_options.crypto_options.srtp.enable_aes128_sha1_32_crypto_cipher = true;
Mirko Bonadei7750d802021-07-26 17:27:42 +02001777 int expected_cipher_suite = rtc::kSrtpAes128CmSha1_80;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07001778 TestNegotiatedCipherSuite(caller_options, callee_options,
1779 expected_cipher_suite);
1780}
1781
1782TEST_P(PeerConnectionIntegrationTest, Aes128Sha1_32_CipherUsedWhenSupported) {
1783 PeerConnectionFactory::Options caller_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07001784 caller_options.crypto_options.srtp.enable_aes128_sha1_32_crypto_cipher = true;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07001785 PeerConnectionFactory::Options callee_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07001786 callee_options.crypto_options.srtp.enable_aes128_sha1_32_crypto_cipher = true;
Mirko Bonadei7750d802021-07-26 17:27:42 +02001787 int expected_cipher_suite = rtc::kSrtpAes128CmSha1_32;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07001788 TestNegotiatedCipherSuite(caller_options, callee_options,
1789 expected_cipher_suite);
1790}
1791
deadbeef1dcb1642017-03-29 21:08:16 -07001792// Test that a non-GCM cipher is used if both sides only support non-GCM.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001793TEST_P(PeerConnectionIntegrationTest, NonGcmCipherUsedWhenGcmNotSupported) {
deadbeef1dcb1642017-03-29 21:08:16 -07001794 bool local_gcm_enabled = false;
1795 bool remote_gcm_enabled = false;
Philipp Hancke2ebbff82019-10-26 06:12:55 +02001796 bool aes_ctr_enabled = true;
deadbeef1dcb1642017-03-29 21:08:16 -07001797 int expected_cipher_suite = kDefaultSrtpCryptoSuite;
1798 TestGcmNegotiationUsesCipherSuite(local_gcm_enabled, remote_gcm_enabled,
Philipp Hancke2ebbff82019-10-26 06:12:55 +02001799 aes_ctr_enabled, expected_cipher_suite);
deadbeef1dcb1642017-03-29 21:08:16 -07001800}
1801
Philipp Hancke2ebbff82019-10-26 06:12:55 +02001802// Test that a GCM cipher is used if both ends support it and non-GCM is
1803// disabled.
1804TEST_P(PeerConnectionIntegrationTest, GcmCipherUsedWhenOnlyGcmSupported) {
deadbeef1dcb1642017-03-29 21:08:16 -07001805 bool local_gcm_enabled = true;
1806 bool remote_gcm_enabled = true;
Philipp Hancke2ebbff82019-10-26 06:12:55 +02001807 bool aes_ctr_enabled = false;
deadbeef1dcb1642017-03-29 21:08:16 -07001808 int expected_cipher_suite = kDefaultSrtpCryptoSuiteGcm;
1809 TestGcmNegotiationUsesCipherSuite(local_gcm_enabled, remote_gcm_enabled,
Philipp Hancke2ebbff82019-10-26 06:12:55 +02001810 aes_ctr_enabled, expected_cipher_suite);
deadbeef1dcb1642017-03-29 21:08:16 -07001811}
1812
deadbeef7914b8c2017-04-21 03:23:33 -07001813// Verify that media can be transmitted end-to-end when GCM crypto suites are
1814// enabled. Note that the above tests, such as GcmCipherUsedWhenGcmSupported,
1815// only verify that a GCM cipher is negotiated, and not necessarily that SRTP
1816// works with it.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001817TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithGcmCipher) {
deadbeef7914b8c2017-04-21 03:23:33 -07001818 PeerConnectionFactory::Options gcm_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07001819 gcm_options.crypto_options.srtp.enable_gcm_crypto_suites = true;
Philipp Hancke2ebbff82019-10-26 06:12:55 +02001820 gcm_options.crypto_options.srtp.enable_aes128_sha1_80_crypto_cipher = false;
deadbeef7914b8c2017-04-21 03:23:33 -07001821 ASSERT_TRUE(
1822 CreatePeerConnectionWrappersWithOptions(gcm_options, gcm_options));
1823 ConnectFakeSignaling();
1824 // Do normal offer/answer and wait for some frames to be received in each
1825 // direction.
Steve Anton15324772018-01-16 10:26:49 -08001826 caller()->AddAudioVideoTracks();
1827 callee()->AddAudioVideoTracks();
deadbeef7914b8c2017-04-21 03:23:33 -07001828 caller()->CreateAndSetAndSignalOffer();
1829 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001830 MediaExpectations media_expectations;
1831 media_expectations.ExpectBidirectionalAudioAndVideo();
1832 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef7914b8c2017-04-21 03:23:33 -07001833}
1834
deadbeef1dcb1642017-03-29 21:08:16 -07001835// Test that the ICE connection and gathering states eventually reach
1836// "complete".
Seth Hampson2f0d7022018-02-20 11:54:42 -08001837TEST_P(PeerConnectionIntegrationTest, IceStatesReachCompletion) {
deadbeef1dcb1642017-03-29 21:08:16 -07001838 ASSERT_TRUE(CreatePeerConnectionWrappers());
1839 ConnectFakeSignaling();
1840 // Do normal offer/answer.
Steve Anton15324772018-01-16 10:26:49 -08001841 caller()->AddAudioVideoTracks();
1842 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001843 caller()->CreateAndSetAndSignalOffer();
1844 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1845 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceGatheringComplete,
1846 caller()->ice_gathering_state(), kMaxWaitForFramesMs);
1847 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceGatheringComplete,
1848 callee()->ice_gathering_state(), kMaxWaitForFramesMs);
1849 // After the best candidate pair is selected and all candidates are signaled,
1850 // the ICE connection state should reach "complete".
1851 // TODO(deadbeef): Currently, the ICE "controlled" agent (the
1852 // answerer/"callee" by default) only reaches "connected". When this is
1853 // fixed, this test should be updated.
1854 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
1855 caller()->ice_connection_state(), kDefaultTimeout);
Alex Loiko9289eda2018-11-23 16:18:59 +00001856 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
1857 callee()->ice_connection_state(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07001858}
1859
Harald Alvestrandec23d6d2021-02-11 10:47:22 +00001860#if !defined(THREAD_SANITIZER)
1861// This test provokes TSAN errors. See bugs.webrtc.org/3608
1862
Qingsi Wang1dac6d82018-12-12 15:28:47 -08001863constexpr int kOnlyLocalPorts = cricket::PORTALLOCATOR_DISABLE_STUN |
1864 cricket::PORTALLOCATOR_DISABLE_RELAY |
1865 cricket::PORTALLOCATOR_DISABLE_TCP;
Zach Stein6fcdc2f2018-08-23 16:25:55 -07001866
Qingsi Wang1dac6d82018-12-12 15:28:47 -08001867// Use a mock resolver to resolve the hostname back to the original IP on both
1868// sides and check that the ICE connection connects.
Markus Handell56910532021-04-10 11:23:14 +00001869// TODO(bugs.webrtc.org/12590): Flaky on Windows and on Linux MSAN.
1870#if defined(WEBRTC_WIN) || defined(WEBRTC_LINUX)
Rasmus Brandt32af25b2021-03-17 13:40:21 +01001871#define MAYBE_IceStatesReachCompletionWithRemoteHostname \
1872 DISABLED_IceStatesReachCompletionWithRemoteHostname
1873#else
1874#define MAYBE_IceStatesReachCompletionWithRemoteHostname \
1875 IceStatesReachCompletionWithRemoteHostname
1876#endif
Zach Stein6fcdc2f2018-08-23 16:25:55 -07001877TEST_P(PeerConnectionIntegrationTest,
Rasmus Brandt32af25b2021-03-17 13:40:21 +01001878 MAYBE_IceStatesReachCompletionWithRemoteHostname) {
Qingsi Wang1dac6d82018-12-12 15:28:47 -08001879 auto caller_resolver_factory =
Mirko Bonadei317a1f02019-09-17 17:06:18 +02001880 std::make_unique<NiceMock<webrtc::MockAsyncResolverFactory>>();
Qingsi Wang1dac6d82018-12-12 15:28:47 -08001881 auto callee_resolver_factory =
Mirko Bonadei317a1f02019-09-17 17:06:18 +02001882 std::make_unique<NiceMock<webrtc::MockAsyncResolverFactory>>();
Qingsi Wang1dac6d82018-12-12 15:28:47 -08001883 NiceMock<rtc::MockAsyncResolver> callee_async_resolver;
1884 NiceMock<rtc::MockAsyncResolver> caller_async_resolver;
Zach Stein6fcdc2f2018-08-23 16:25:55 -07001885
1886 // This also verifies that the injected AsyncResolverFactory is used by
1887 // P2PTransportChannel.
Qingsi Wang1dac6d82018-12-12 15:28:47 -08001888 EXPECT_CALL(*caller_resolver_factory, Create())
1889 .WillOnce(Return(&caller_async_resolver));
1890 webrtc::PeerConnectionDependencies caller_deps(nullptr);
1891 caller_deps.async_resolver_factory = std::move(caller_resolver_factory);
1892
1893 EXPECT_CALL(*callee_resolver_factory, Create())
1894 .WillOnce(Return(&callee_async_resolver));
1895 webrtc::PeerConnectionDependencies callee_deps(nullptr);
1896 callee_deps.async_resolver_factory = std::move(callee_resolver_factory);
1897
1898 PeerConnectionInterface::RTCConfiguration config;
1899 config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
1900 config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyRequire;
1901
1902 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndDeps(
1903 config, std::move(caller_deps), config, std::move(callee_deps)));
1904
1905 caller()->SetRemoteAsyncResolver(&callee_async_resolver);
1906 callee()->SetRemoteAsyncResolver(&caller_async_resolver);
1907
1908 // Enable hostname candidates with mDNS names.
Qingsi Wangecd30542019-05-22 14:34:56 -07001909 caller()->SetMdnsResponder(
Mirko Bonadei317a1f02019-09-17 17:06:18 +02001910 std::make_unique<webrtc::FakeMdnsResponder>(network_thread()));
Qingsi Wangecd30542019-05-22 14:34:56 -07001911 callee()->SetMdnsResponder(
Mirko Bonadei317a1f02019-09-17 17:06:18 +02001912 std::make_unique<webrtc::FakeMdnsResponder>(network_thread()));
Qingsi Wang1dac6d82018-12-12 15:28:47 -08001913
1914 SetPortAllocatorFlags(kOnlyLocalPorts, kOnlyLocalPorts);
Zach Stein6fcdc2f2018-08-23 16:25:55 -07001915
1916 ConnectFakeSignaling();
1917 caller()->AddAudioVideoTracks();
1918 callee()->AddAudioVideoTracks();
1919 caller()->CreateAndSetAndSignalOffer();
1920 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1921 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
1922 caller()->ice_connection_state(), kDefaultTimeout);
1923 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
1924 callee()->ice_connection_state(), kDefaultTimeout);
Jeroen de Borst833979f2018-12-13 08:25:54 -08001925
Ying Wangef3998f2019-12-09 13:06:53 +01001926 EXPECT_METRIC_EQ(1, webrtc::metrics::NumEvents(
1927 "WebRTC.PeerConnection.CandidatePairType_UDP",
1928 webrtc::kIceCandidatePairHostNameHostName));
Zach Stein6fcdc2f2018-08-23 16:25:55 -07001929}
1930
Harald Alvestrandec23d6d2021-02-11 10:47:22 +00001931#endif // !defined(THREAD_SANITIZER)
1932
Steve Antonede9ca52017-10-16 13:04:27 -07001933// Test that firewalling the ICE connection causes the clients to identify the
1934// disconnected state and then removing the firewall causes them to reconnect.
1935class PeerConnectionIntegrationIceStatesTest
Seth Hampson2f0d7022018-02-20 11:54:42 -08001936 : public PeerConnectionIntegrationBaseTest,
1937 public ::testing::WithParamInterface<
1938 std::tuple<SdpSemantics, std::tuple<std::string, uint32_t>>> {
Steve Antonede9ca52017-10-16 13:04:27 -07001939 protected:
Seth Hampson2f0d7022018-02-20 11:54:42 -08001940 PeerConnectionIntegrationIceStatesTest()
1941 : PeerConnectionIntegrationBaseTest(std::get<0>(GetParam())) {
1942 port_allocator_flags_ = std::get<1>(std::get<1>(GetParam()));
Steve Antonede9ca52017-10-16 13:04:27 -07001943 }
1944
1945 void StartStunServer(const SocketAddress& server_address) {
1946 stun_server_.reset(
Niels Möller091617d2020-12-02 15:32:08 +01001947 cricket::TestStunServer::Create(firewall(), server_address));
Steve Antonede9ca52017-10-16 13:04:27 -07001948 }
1949
1950 bool TestIPv6() {
1951 return (port_allocator_flags_ & cricket::PORTALLOCATOR_ENABLE_IPV6);
1952 }
1953
1954 void SetPortAllocatorFlags() {
Qingsi Wang1dac6d82018-12-12 15:28:47 -08001955 PeerConnectionIntegrationBaseTest::SetPortAllocatorFlags(
1956 port_allocator_flags_, port_allocator_flags_);
Steve Antonede9ca52017-10-16 13:04:27 -07001957 }
1958
1959 std::vector<SocketAddress> CallerAddresses() {
1960 std::vector<SocketAddress> addresses;
1961 addresses.push_back(SocketAddress("1.1.1.1", 0));
1962 if (TestIPv6()) {
1963 addresses.push_back(SocketAddress("1111:0:a:b:c:d:e:f", 0));
1964 }
1965 return addresses;
1966 }
1967
1968 std::vector<SocketAddress> CalleeAddresses() {
1969 std::vector<SocketAddress> addresses;
1970 addresses.push_back(SocketAddress("2.2.2.2", 0));
1971 if (TestIPv6()) {
1972 addresses.push_back(SocketAddress("2222:0:a:b:c:d:e:f", 0));
1973 }
1974 return addresses;
1975 }
1976
1977 void SetUpNetworkInterfaces() {
1978 // Remove the default interfaces added by the test infrastructure.
Qingsi Wangecd30542019-05-22 14:34:56 -07001979 caller()->network_manager()->RemoveInterface(kDefaultLocalAddress);
1980 callee()->network_manager()->RemoveInterface(kDefaultLocalAddress);
Steve Antonede9ca52017-10-16 13:04:27 -07001981
1982 // Add network addresses for test.
1983 for (const auto& caller_address : CallerAddresses()) {
Qingsi Wangecd30542019-05-22 14:34:56 -07001984 caller()->network_manager()->AddInterface(caller_address);
Steve Antonede9ca52017-10-16 13:04:27 -07001985 }
1986 for (const auto& callee_address : CalleeAddresses()) {
Qingsi Wangecd30542019-05-22 14:34:56 -07001987 callee()->network_manager()->AddInterface(callee_address);
Steve Antonede9ca52017-10-16 13:04:27 -07001988 }
1989 }
1990
1991 private:
1992 uint32_t port_allocator_flags_;
1993 std::unique_ptr<cricket::TestStunServer> stun_server_;
1994};
1995
Yves Gerey100fe632020-01-17 19:15:53 +01001996// Ensure FakeClockForTest is constructed first (see class for rationale).
1997class PeerConnectionIntegrationIceStatesTestWithFakeClock
1998 : public FakeClockForTest,
1999 public PeerConnectionIntegrationIceStatesTest {};
2000
Harald Alvestrandec23d6d2021-02-11 10:47:22 +00002001#if !defined(THREAD_SANITIZER)
2002// This test provokes TSAN errors. bugs.webrtc.org/11282
2003
Steve Antonede9ca52017-10-16 13:04:27 -07002004// Tests that the PeerConnection goes through all the ICE gathering/connection
2005// states over the duration of the call. This includes Disconnected and Failed
2006// states, induced by putting a firewall between the peers and waiting for them
2007// to time out.
Yves Gerey100fe632020-01-17 19:15:53 +01002008TEST_P(PeerConnectionIntegrationIceStatesTestWithFakeClock, VerifyIceStates) {
Steve Antonede9ca52017-10-16 13:04:27 -07002009 const SocketAddress kStunServerAddress =
2010 SocketAddress("99.99.99.1", cricket::STUN_SERVER_PORT);
2011 StartStunServer(kStunServerAddress);
2012
2013 PeerConnectionInterface::RTCConfiguration config;
2014 PeerConnectionInterface::IceServer ice_stun_server;
2015 ice_stun_server.urls.push_back(
2016 "stun:" + kStunServerAddress.HostAsURIString() + ":" +
2017 kStunServerAddress.PortAsString());
2018 config.servers.push_back(ice_stun_server);
2019
2020 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
2021 ConnectFakeSignaling();
2022 SetPortAllocatorFlags();
2023 SetUpNetworkInterfaces();
Steve Anton15324772018-01-16 10:26:49 -08002024 caller()->AddAudioVideoTracks();
2025 callee()->AddAudioVideoTracks();
Steve Antonede9ca52017-10-16 13:04:27 -07002026
2027 // Initial state before anything happens.
2028 ASSERT_EQ(PeerConnectionInterface::kIceGatheringNew,
2029 caller()->ice_gathering_state());
2030 ASSERT_EQ(PeerConnectionInterface::kIceConnectionNew,
2031 caller()->ice_connection_state());
Jonas Olsson7a6739e2019-01-15 16:31:55 +01002032 ASSERT_EQ(PeerConnectionInterface::kIceConnectionNew,
2033 caller()->standardized_ice_connection_state());
Steve Antonede9ca52017-10-16 13:04:27 -07002034
2035 // Start the call by creating the offer, setting it as the local description,
2036 // then sending it to the peer who will respond with an answer. This happens
2037 // asynchronously so that we can watch the states as it runs in the
2038 // background.
2039 caller()->CreateAndSetAndSignalOffer();
2040
Steve Antona9b67ce2020-01-16 14:00:44 -08002041 ASSERT_EQ_SIMULATED_WAIT(PeerConnectionInterface::kIceConnectionCompleted,
2042 caller()->ice_connection_state(), kDefaultTimeout,
Yves Gerey100fe632020-01-17 19:15:53 +01002043 FakeClock());
Steve Antona9b67ce2020-01-16 14:00:44 -08002044 ASSERT_EQ_SIMULATED_WAIT(PeerConnectionInterface::kIceConnectionCompleted,
2045 caller()->standardized_ice_connection_state(),
Yves Gerey100fe632020-01-17 19:15:53 +01002046 kDefaultTimeout, FakeClock());
Steve Antonede9ca52017-10-16 13:04:27 -07002047
2048 // Verify that the observer was notified of the intermediate transitions.
2049 EXPECT_THAT(caller()->ice_connection_state_history(),
2050 ElementsAre(PeerConnectionInterface::kIceConnectionChecking,
2051 PeerConnectionInterface::kIceConnectionConnected,
2052 PeerConnectionInterface::kIceConnectionCompleted));
Jonas Olssonacd8ae72019-02-25 15:26:24 +01002053 EXPECT_THAT(caller()->standardized_ice_connection_state_history(),
2054 ElementsAre(PeerConnectionInterface::kIceConnectionChecking,
2055 PeerConnectionInterface::kIceConnectionConnected,
2056 PeerConnectionInterface::kIceConnectionCompleted));
Jonas Olsson635474e2018-10-18 15:58:17 +02002057 EXPECT_THAT(
2058 caller()->peer_connection_state_history(),
2059 ElementsAre(PeerConnectionInterface::PeerConnectionState::kConnecting,
Jonas Olsson635474e2018-10-18 15:58:17 +02002060 PeerConnectionInterface::PeerConnectionState::kConnected));
Steve Antonede9ca52017-10-16 13:04:27 -07002061 EXPECT_THAT(caller()->ice_gathering_state_history(),
2062 ElementsAre(PeerConnectionInterface::kIceGatheringGathering,
2063 PeerConnectionInterface::kIceGatheringComplete));
2064
2065 // Block connections to/from the caller and wait for ICE to become
2066 // disconnected.
2067 for (const auto& caller_address : CallerAddresses()) {
2068 firewall()->AddRule(false, rtc::FP_ANY, rtc::FD_ANY, caller_address);
2069 }
Mirko Bonadei675513b2017-11-09 11:09:25 +01002070 RTC_LOG(LS_INFO) << "Firewall rules applied";
Jonas Olssonb75d9e92019-02-22 10:33:29 +01002071 ASSERT_EQ_SIMULATED_WAIT(PeerConnectionInterface::kIceConnectionDisconnected,
2072 caller()->ice_connection_state(), kDefaultTimeout,
Yves Gerey100fe632020-01-17 19:15:53 +01002073 FakeClock());
Jonas Olssonb75d9e92019-02-22 10:33:29 +01002074 ASSERT_EQ_SIMULATED_WAIT(PeerConnectionInterface::kIceConnectionDisconnected,
2075 caller()->standardized_ice_connection_state(),
Yves Gerey100fe632020-01-17 19:15:53 +01002076 kDefaultTimeout, FakeClock());
Steve Antonede9ca52017-10-16 13:04:27 -07002077
2078 // Let ICE re-establish by removing the firewall rules.
2079 firewall()->ClearRules();
Mirko Bonadei675513b2017-11-09 11:09:25 +01002080 RTC_LOG(LS_INFO) << "Firewall rules cleared";
Jonas Olssonb75d9e92019-02-22 10:33:29 +01002081 ASSERT_EQ_SIMULATED_WAIT(PeerConnectionInterface::kIceConnectionCompleted,
2082 caller()->ice_connection_state(), kDefaultTimeout,
Yves Gerey100fe632020-01-17 19:15:53 +01002083 FakeClock());
Jonas Olssonacd8ae72019-02-25 15:26:24 +01002084 ASSERT_EQ_SIMULATED_WAIT(PeerConnectionInterface::kIceConnectionCompleted,
Jonas Olssonb75d9e92019-02-22 10:33:29 +01002085 caller()->standardized_ice_connection_state(),
Yves Gerey100fe632020-01-17 19:15:53 +01002086 kDefaultTimeout, FakeClock());
Steve Antonede9ca52017-10-16 13:04:27 -07002087
2088 // According to RFC7675, if there is no response within 30 seconds then the
2089 // peer should consider the other side to have rejected the connection. This
Steve Anton83119dd2017-11-10 16:19:52 -08002090 // is signaled by the state transitioning to "failed".
Steve Antonede9ca52017-10-16 13:04:27 -07002091 constexpr int kConsentTimeout = 30000;
2092 for (const auto& caller_address : CallerAddresses()) {
2093 firewall()->AddRule(false, rtc::FP_ANY, rtc::FD_ANY, caller_address);
2094 }
Mirko Bonadei675513b2017-11-09 11:09:25 +01002095 RTC_LOG(LS_INFO) << "Firewall rules applied again";
Jonas Olssonb75d9e92019-02-22 10:33:29 +01002096 ASSERT_EQ_SIMULATED_WAIT(PeerConnectionInterface::kIceConnectionFailed,
2097 caller()->ice_connection_state(), kConsentTimeout,
Yves Gerey100fe632020-01-17 19:15:53 +01002098 FakeClock());
Jonas Olssonb75d9e92019-02-22 10:33:29 +01002099 ASSERT_EQ_SIMULATED_WAIT(PeerConnectionInterface::kIceConnectionFailed,
2100 caller()->standardized_ice_connection_state(),
Yves Gerey100fe632020-01-17 19:15:53 +01002101 kConsentTimeout, FakeClock());
Jonas Olssonb75d9e92019-02-22 10:33:29 +01002102}
2103
2104// Tests that if the connection doesn't get set up properly we eventually reach
2105// the "failed" iceConnectionState.
Yves Gerey100fe632020-01-17 19:15:53 +01002106TEST_P(PeerConnectionIntegrationIceStatesTestWithFakeClock,
2107 IceStateSetupFailure) {
Jonas Olssonb75d9e92019-02-22 10:33:29 +01002108 // Block connections to/from the caller and wait for ICE to become
2109 // disconnected.
2110 for (const auto& caller_address : CallerAddresses()) {
2111 firewall()->AddRule(false, rtc::FP_ANY, rtc::FD_ANY, caller_address);
2112 }
2113
2114 ASSERT_TRUE(CreatePeerConnectionWrappers());
2115 ConnectFakeSignaling();
2116 SetPortAllocatorFlags();
2117 SetUpNetworkInterfaces();
2118 caller()->AddAudioVideoTracks();
2119 caller()->CreateAndSetAndSignalOffer();
2120
2121 // According to RFC7675, if there is no response within 30 seconds then the
2122 // peer should consider the other side to have rejected the connection. This
2123 // is signaled by the state transitioning to "failed".
2124 constexpr int kConsentTimeout = 30000;
2125 ASSERT_EQ_SIMULATED_WAIT(PeerConnectionInterface::kIceConnectionFailed,
2126 caller()->standardized_ice_connection_state(),
Yves Gerey100fe632020-01-17 19:15:53 +01002127 kConsentTimeout, FakeClock());
Steve Antonede9ca52017-10-16 13:04:27 -07002128}
2129
Harald Alvestrandec23d6d2021-02-11 10:47:22 +00002130#endif // !defined(THREAD_SANITIZER)
2131
Steve Antonede9ca52017-10-16 13:04:27 -07002132// Tests that the best connection is set to the appropriate IPv4/IPv6 connection
2133// and that the statistics in the metric observers are updated correctly.
Rasmus Brandt32af25b2021-03-17 13:40:21 +01002134// TODO(bugs.webrtc.org/12591): Flaky on Windows.
2135#if defined(WEBRTC_WIN)
2136#define MAYBE_VerifyBestConnection DISABLED_VerifyBestConnection
2137#else
2138#define MAYBE_VerifyBestConnection VerifyBestConnection
2139#endif
2140TEST_P(PeerConnectionIntegrationIceStatesTest, MAYBE_VerifyBestConnection) {
Steve Antonede9ca52017-10-16 13:04:27 -07002141 ASSERT_TRUE(CreatePeerConnectionWrappers());
2142 ConnectFakeSignaling();
2143 SetPortAllocatorFlags();
2144 SetUpNetworkInterfaces();
Steve Anton15324772018-01-16 10:26:49 -08002145 caller()->AddAudioVideoTracks();
2146 callee()->AddAudioVideoTracks();
Steve Antonede9ca52017-10-16 13:04:27 -07002147 caller()->CreateAndSetAndSignalOffer();
2148
2149 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Steve Anton692f3c72020-01-16 14:12:31 -08002150 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
2151 caller()->ice_connection_state(), kDefaultTimeout);
2152 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
2153 callee()->ice_connection_state(), kDefaultTimeout);
Steve Antonede9ca52017-10-16 13:04:27 -07002154
Qingsi Wang7fc821d2018-07-12 12:54:53 -07002155 // TODO(bugs.webrtc.org/9456): Fix it.
2156 const int num_best_ipv4 = webrtc::metrics::NumEvents(
2157 "WebRTC.PeerConnection.IPMetrics", webrtc::kBestConnections_IPv4);
2158 const int num_best_ipv6 = webrtc::metrics::NumEvents(
2159 "WebRTC.PeerConnection.IPMetrics", webrtc::kBestConnections_IPv6);
Steve Antonede9ca52017-10-16 13:04:27 -07002160 if (TestIPv6()) {
2161 // When IPv6 is enabled, we should prefer an IPv6 connection over an IPv4
2162 // connection.
Ying Wangef3998f2019-12-09 13:06:53 +01002163 EXPECT_METRIC_EQ(0, num_best_ipv4);
2164 EXPECT_METRIC_EQ(1, num_best_ipv6);
Steve Antonede9ca52017-10-16 13:04:27 -07002165 } else {
Ying Wangef3998f2019-12-09 13:06:53 +01002166 EXPECT_METRIC_EQ(1, num_best_ipv4);
2167 EXPECT_METRIC_EQ(0, num_best_ipv6);
Steve Antonede9ca52017-10-16 13:04:27 -07002168 }
2169
Ying Wangef3998f2019-12-09 13:06:53 +01002170 EXPECT_METRIC_EQ(0, webrtc::metrics::NumEvents(
2171 "WebRTC.PeerConnection.CandidatePairType_UDP",
2172 webrtc::kIceCandidatePairHostHost));
2173 EXPECT_METRIC_EQ(1, webrtc::metrics::NumEvents(
2174 "WebRTC.PeerConnection.CandidatePairType_UDP",
2175 webrtc::kIceCandidatePairHostPublicHostPublic));
Steve Antonede9ca52017-10-16 13:04:27 -07002176}
2177
2178constexpr uint32_t kFlagsIPv4NoStun = cricket::PORTALLOCATOR_DISABLE_TCP |
2179 cricket::PORTALLOCATOR_DISABLE_STUN |
2180 cricket::PORTALLOCATOR_DISABLE_RELAY;
2181constexpr uint32_t kFlagsIPv6NoStun =
2182 cricket::PORTALLOCATOR_DISABLE_TCP | cricket::PORTALLOCATOR_DISABLE_STUN |
2183 cricket::PORTALLOCATOR_ENABLE_IPV6 | cricket::PORTALLOCATOR_DISABLE_RELAY;
2184constexpr uint32_t kFlagsIPv4Stun =
2185 cricket::PORTALLOCATOR_DISABLE_TCP | cricket::PORTALLOCATOR_DISABLE_RELAY;
2186
Mirko Bonadeic84f6612019-01-31 12:20:57 +01002187INSTANTIATE_TEST_SUITE_P(
Seth Hampson2f0d7022018-02-20 11:54:42 -08002188 PeerConnectionIntegrationTest,
2189 PeerConnectionIntegrationIceStatesTest,
2190 Combine(Values(SdpSemantics::kPlanB, SdpSemantics::kUnifiedPlan),
2191 Values(std::make_pair("IPv4 no STUN", kFlagsIPv4NoStun),
2192 std::make_pair("IPv6 no STUN", kFlagsIPv6NoStun),
2193 std::make_pair("IPv4 with STUN", kFlagsIPv4Stun))));
Steve Antonede9ca52017-10-16 13:04:27 -07002194
Yves Gerey100fe632020-01-17 19:15:53 +01002195INSTANTIATE_TEST_SUITE_P(
2196 PeerConnectionIntegrationTest,
2197 PeerConnectionIntegrationIceStatesTestWithFakeClock,
2198 Combine(Values(SdpSemantics::kPlanB, SdpSemantics::kUnifiedPlan),
2199 Values(std::make_pair("IPv4 no STUN", kFlagsIPv4NoStun),
2200 std::make_pair("IPv6 no STUN", kFlagsIPv6NoStun),
2201 std::make_pair("IPv4 with STUN", kFlagsIPv4Stun))));
2202
deadbeef1dcb1642017-03-29 21:08:16 -07002203// This test sets up a call between two parties with audio and video.
2204// During the call, the caller restarts ICE and the test verifies that
2205// new ICE candidates are generated and audio and video still can flow, and the
2206// ICE state reaches completed again.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002207TEST_P(PeerConnectionIntegrationTest, MediaContinuesFlowingAfterIceRestart) {
deadbeef1dcb1642017-03-29 21:08:16 -07002208 ASSERT_TRUE(CreatePeerConnectionWrappers());
2209 ConnectFakeSignaling();
2210 // Do normal offer/answer and wait for ICE to complete.
Steve Anton15324772018-01-16 10:26:49 -08002211 caller()->AddAudioVideoTracks();
2212 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002213 caller()->CreateAndSetAndSignalOffer();
2214 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2215 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
2216 caller()->ice_connection_state(), kMaxWaitForFramesMs);
Alex Loiko9289eda2018-11-23 16:18:59 +00002217 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
2218 callee()->ice_connection_state(), kMaxWaitForFramesMs);
deadbeef1dcb1642017-03-29 21:08:16 -07002219
2220 // To verify that the ICE restart actually occurs, get
2221 // ufrag/password/candidates before and after restart.
2222 // Create an SDP string of the first audio candidate for both clients.
2223 const webrtc::IceCandidateCollection* audio_candidates_caller =
2224 caller()->pc()->local_description()->candidates(0);
2225 const webrtc::IceCandidateCollection* audio_candidates_callee =
2226 callee()->pc()->local_description()->candidates(0);
2227 ASSERT_GT(audio_candidates_caller->count(), 0u);
2228 ASSERT_GT(audio_candidates_callee->count(), 0u);
2229 std::string caller_candidate_pre_restart;
2230 ASSERT_TRUE(
2231 audio_candidates_caller->at(0)->ToString(&caller_candidate_pre_restart));
2232 std::string callee_candidate_pre_restart;
2233 ASSERT_TRUE(
2234 audio_candidates_callee->at(0)->ToString(&callee_candidate_pre_restart));
2235 const cricket::SessionDescription* desc =
2236 caller()->pc()->local_description()->description();
2237 std::string caller_ufrag_pre_restart =
2238 desc->transport_infos()[0].description.ice_ufrag;
2239 desc = callee()->pc()->local_description()->description();
2240 std::string callee_ufrag_pre_restart =
2241 desc->transport_infos()[0].description.ice_ufrag;
2242
Alex Drake00c7ecf2019-08-06 10:54:47 -07002243 EXPECT_EQ(caller()->ice_candidate_pair_change_history().size(), 1u);
deadbeef1dcb1642017-03-29 21:08:16 -07002244 // Have the caller initiate an ICE restart.
2245 caller()->SetOfferAnswerOptions(IceRestartOfferAnswerOptions());
2246 caller()->CreateAndSetAndSignalOffer();
2247 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2248 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
2249 caller()->ice_connection_state(), kMaxWaitForFramesMs);
Alex Loiko9289eda2018-11-23 16:18:59 +00002250 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
deadbeef1dcb1642017-03-29 21:08:16 -07002251 callee()->ice_connection_state(), kMaxWaitForFramesMs);
2252
2253 // Grab the ufrags/candidates again.
2254 audio_candidates_caller = caller()->pc()->local_description()->candidates(0);
2255 audio_candidates_callee = callee()->pc()->local_description()->candidates(0);
2256 ASSERT_GT(audio_candidates_caller->count(), 0u);
2257 ASSERT_GT(audio_candidates_callee->count(), 0u);
2258 std::string caller_candidate_post_restart;
2259 ASSERT_TRUE(
2260 audio_candidates_caller->at(0)->ToString(&caller_candidate_post_restart));
2261 std::string callee_candidate_post_restart;
2262 ASSERT_TRUE(
2263 audio_candidates_callee->at(0)->ToString(&callee_candidate_post_restart));
2264 desc = caller()->pc()->local_description()->description();
2265 std::string caller_ufrag_post_restart =
2266 desc->transport_infos()[0].description.ice_ufrag;
2267 desc = callee()->pc()->local_description()->description();
2268 std::string callee_ufrag_post_restart =
2269 desc->transport_infos()[0].description.ice_ufrag;
2270 // Sanity check that an ICE restart was actually negotiated in SDP.
2271 ASSERT_NE(caller_candidate_pre_restart, caller_candidate_post_restart);
2272 ASSERT_NE(callee_candidate_pre_restart, callee_candidate_post_restart);
2273 ASSERT_NE(caller_ufrag_pre_restart, caller_ufrag_post_restart);
2274 ASSERT_NE(callee_ufrag_pre_restart, callee_ufrag_post_restart);
Alex Drake00c7ecf2019-08-06 10:54:47 -07002275 EXPECT_GT(caller()->ice_candidate_pair_change_history().size(), 1u);
deadbeef1dcb1642017-03-29 21:08:16 -07002276
2277 // Ensure that additional frames are received after the ICE restart.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002278 MediaExpectations media_expectations;
2279 media_expectations.ExpectBidirectionalAudioAndVideo();
2280 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002281}
2282
2283// Verify that audio/video can be received end-to-end when ICE renomination is
2284// enabled.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002285TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithIceRenomination) {
deadbeef1dcb1642017-03-29 21:08:16 -07002286 PeerConnectionInterface::RTCConfiguration config;
2287 config.enable_ice_renomination = true;
2288 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
2289 ConnectFakeSignaling();
2290 // Do normal offer/answer and wait for some frames to be received in each
2291 // direction.
Steve Anton15324772018-01-16 10:26:49 -08002292 caller()->AddAudioVideoTracks();
2293 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002294 caller()->CreateAndSetAndSignalOffer();
2295 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2296 // Sanity check that ICE renomination was actually negotiated.
2297 const cricket::SessionDescription* desc =
2298 caller()->pc()->local_description()->description();
2299 for (const cricket::TransportInfo& info : desc->transport_infos()) {
Steve Anton64b626b2019-01-28 17:25:26 -08002300 ASSERT_THAT(info.description.transport_options, Contains("renomination"));
deadbeef1dcb1642017-03-29 21:08:16 -07002301 }
2302 desc = callee()->pc()->local_description()->description();
2303 for (const cricket::TransportInfo& info : desc->transport_infos()) {
Steve Anton64b626b2019-01-28 17:25:26 -08002304 ASSERT_THAT(info.description.transport_options, Contains("renomination"));
deadbeef1dcb1642017-03-29 21:08:16 -07002305 }
Seth Hampson2f0d7022018-02-20 11:54:42 -08002306 MediaExpectations media_expectations;
2307 media_expectations.ExpectBidirectionalAudioAndVideo();
2308 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002309}
2310
Steve Anton6f25b092017-10-23 09:39:20 -07002311// With a max bundle policy and RTCP muxing, adding a new media description to
2312// the connection should not affect ICE at all because the new media will use
2313// the existing connection.
Rasmus Brandt685be142021-03-15 14:03:38 +01002314// TODO(bugs.webrtc.org/12538): Fails on tsan.
2315#if defined(THREAD_SANITIZER)
2316#define MAYBE_AddMediaToConnectedBundleDoesNotRestartIce \
2317 DISABLED_AddMediaToConnectedBundleDoesNotRestartIce
2318#else
2319#define MAYBE_AddMediaToConnectedBundleDoesNotRestartIce \
2320 AddMediaToConnectedBundleDoesNotRestartIce
2321#endif
Seth Hampson2f0d7022018-02-20 11:54:42 -08002322TEST_P(PeerConnectionIntegrationTest,
Rasmus Brandt685be142021-03-15 14:03:38 +01002323 MAYBE_AddMediaToConnectedBundleDoesNotRestartIce) {
Steve Anton6f25b092017-10-23 09:39:20 -07002324 PeerConnectionInterface::RTCConfiguration config;
2325 config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
2326 config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyRequire;
2327 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(
2328 config, PeerConnectionInterface::RTCConfiguration()));
2329 ConnectFakeSignaling();
2330
Steve Anton15324772018-01-16 10:26:49 -08002331 caller()->AddAudioTrack();
Steve Anton6f25b092017-10-23 09:39:20 -07002332 caller()->CreateAndSetAndSignalOffer();
2333 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Steve Antonff52f1b2017-10-26 12:24:50 -07002334 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceConnectionCompleted,
2335 caller()->ice_connection_state(), kDefaultTimeout);
Steve Anton6f25b092017-10-23 09:39:20 -07002336
2337 caller()->clear_ice_connection_state_history();
2338
Steve Anton15324772018-01-16 10:26:49 -08002339 caller()->AddVideoTrack();
Steve Anton6f25b092017-10-23 09:39:20 -07002340 caller()->CreateAndSetAndSignalOffer();
2341 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2342
2343 EXPECT_EQ(0u, caller()->ice_connection_state_history().size());
2344}
2345
deadbeef1dcb1642017-03-29 21:08:16 -07002346// This test sets up a call between two parties with audio and video. It then
2347// renegotiates setting the video m-line to "port 0", then later renegotiates
2348// again, enabling video.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002349TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07002350 VideoFlowsAfterMediaSectionIsRejectedAndRecycled) {
2351 ASSERT_TRUE(CreatePeerConnectionWrappers());
2352 ConnectFakeSignaling();
2353
2354 // Do initial negotiation, only sending media from the caller. Will result in
2355 // video and audio recvonly "m=" sections.
Steve Anton15324772018-01-16 10:26:49 -08002356 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002357 caller()->CreateAndSetAndSignalOffer();
2358 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2359
2360 // Negotiate again, disabling the video "m=" section (the callee will set the
2361 // port to 0 due to offer_to_receive_video = 0).
Seth Hampson2f0d7022018-02-20 11:54:42 -08002362 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2363 PeerConnectionInterface::RTCOfferAnswerOptions options;
2364 options.offer_to_receive_video = 0;
2365 callee()->SetOfferAnswerOptions(options);
2366 } else {
2367 callee()->SetRemoteOfferHandler([this] {
Harald Alvestrand6060df52020-08-11 09:54:02 +02002368 callee()
2369 ->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)
2370 ->StopInternal();
Seth Hampson2f0d7022018-02-20 11:54:42 -08002371 });
2372 }
deadbeef1dcb1642017-03-29 21:08:16 -07002373 caller()->CreateAndSetAndSignalOffer();
2374 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2375 // Sanity check that video "m=" section was actually rejected.
2376 const ContentInfo* answer_video_content = cricket::GetFirstVideoContent(
2377 callee()->pc()->local_description()->description());
2378 ASSERT_NE(nullptr, answer_video_content);
2379 ASSERT_TRUE(answer_video_content->rejected);
2380
2381 // Enable video and do negotiation again, making sure video is received
2382 // end-to-end, also adding media stream to callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002383 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2384 PeerConnectionInterface::RTCOfferAnswerOptions options;
2385 options.offer_to_receive_video = 1;
2386 callee()->SetOfferAnswerOptions(options);
2387 } else {
2388 // The caller's transceiver is stopped, so we need to add another track.
2389 auto caller_transceiver =
2390 caller()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO);
Harald Alvestrand6060df52020-08-11 09:54:02 +02002391 EXPECT_EQ(nullptr, caller_transceiver.get());
Seth Hampson2f0d7022018-02-20 11:54:42 -08002392 caller()->AddVideoTrack();
2393 }
2394 callee()->AddVideoTrack();
2395 callee()->SetRemoteOfferHandler(nullptr);
deadbeef1dcb1642017-03-29 21:08:16 -07002396 caller()->CreateAndSetAndSignalOffer();
2397 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002398
deadbeef1dcb1642017-03-29 21:08:16 -07002399 // Verify the caller receives frames from the newly added stream, and the
2400 // callee receives additional frames from the re-enabled video m= section.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002401 MediaExpectations media_expectations;
2402 media_expectations.CalleeExpectsSomeAudio();
2403 media_expectations.ExpectBidirectionalVideo();
2404 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002405}
2406
deadbeef1dcb1642017-03-29 21:08:16 -07002407// This tests that if we negotiate after calling CreateSender but before we
2408// have a track, then set a track later, frames from the newly-set track are
2409// received end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002410TEST_F(PeerConnectionIntegrationTestPlanB,
deadbeef1dcb1642017-03-29 21:08:16 -07002411 MediaFlowsAfterEarlyWarmupWithCreateSender) {
2412 ASSERT_TRUE(CreatePeerConnectionWrappers());
2413 ConnectFakeSignaling();
2414 auto caller_audio_sender =
2415 caller()->pc()->CreateSender("audio", "caller_stream");
2416 auto caller_video_sender =
2417 caller()->pc()->CreateSender("video", "caller_stream");
2418 auto callee_audio_sender =
2419 callee()->pc()->CreateSender("audio", "callee_stream");
2420 auto callee_video_sender =
2421 callee()->pc()->CreateSender("video", "callee_stream");
2422 caller()->CreateAndSetAndSignalOffer();
2423 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
2424 // Wait for ICE to complete, without any tracks being set.
2425 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
2426 caller()->ice_connection_state(), kMaxWaitForFramesMs);
2427 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
2428 callee()->ice_connection_state(), kMaxWaitForFramesMs);
2429 // Now set the tracks, and expect frames to immediately start flowing.
2430 EXPECT_TRUE(caller_audio_sender->SetTrack(caller()->CreateLocalAudioTrack()));
2431 EXPECT_TRUE(caller_video_sender->SetTrack(caller()->CreateLocalVideoTrack()));
2432 EXPECT_TRUE(callee_audio_sender->SetTrack(callee()->CreateLocalAudioTrack()));
2433 EXPECT_TRUE(callee_video_sender->SetTrack(callee()->CreateLocalVideoTrack()));
Seth Hampson2f0d7022018-02-20 11:54:42 -08002434 MediaExpectations media_expectations;
2435 media_expectations.ExpectBidirectionalAudioAndVideo();
2436 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2437}
2438
2439// This tests that if we negotiate after calling AddTransceiver but before we
2440// have a track, then set a track later, frames from the newly-set tracks are
2441// received end-to-end.
2442TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
2443 MediaFlowsAfterEarlyWarmupWithAddTransceiver) {
2444 ASSERT_TRUE(CreatePeerConnectionWrappers());
2445 ConnectFakeSignaling();
2446 auto audio_result = caller()->pc()->AddTransceiver(cricket::MEDIA_TYPE_AUDIO);
2447 ASSERT_EQ(RTCErrorType::NONE, audio_result.error().type());
2448 auto caller_audio_sender = audio_result.MoveValue()->sender();
2449 auto video_result = caller()->pc()->AddTransceiver(cricket::MEDIA_TYPE_VIDEO);
2450 ASSERT_EQ(RTCErrorType::NONE, video_result.error().type());
2451 auto caller_video_sender = video_result.MoveValue()->sender();
2452 callee()->SetRemoteOfferHandler([this] {
2453 ASSERT_EQ(2u, callee()->pc()->GetTransceivers().size());
Harald Alvestrand6060df52020-08-11 09:54:02 +02002454 callee()->pc()->GetTransceivers()[0]->SetDirectionWithError(
Seth Hampson2f0d7022018-02-20 11:54:42 -08002455 RtpTransceiverDirection::kSendRecv);
Harald Alvestrand6060df52020-08-11 09:54:02 +02002456 callee()->pc()->GetTransceivers()[1]->SetDirectionWithError(
Seth Hampson2f0d7022018-02-20 11:54:42 -08002457 RtpTransceiverDirection::kSendRecv);
2458 });
2459 caller()->CreateAndSetAndSignalOffer();
2460 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
2461 // Wait for ICE to complete, without any tracks being set.
2462 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
2463 caller()->ice_connection_state(), kMaxWaitForFramesMs);
2464 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
2465 callee()->ice_connection_state(), kMaxWaitForFramesMs);
2466 // Now set the tracks, and expect frames to immediately start flowing.
2467 auto callee_audio_sender = callee()->pc()->GetSenders()[0];
2468 auto callee_video_sender = callee()->pc()->GetSenders()[1];
2469 ASSERT_TRUE(caller_audio_sender->SetTrack(caller()->CreateLocalAudioTrack()));
2470 ASSERT_TRUE(caller_video_sender->SetTrack(caller()->CreateLocalVideoTrack()));
2471 ASSERT_TRUE(callee_audio_sender->SetTrack(callee()->CreateLocalAudioTrack()));
2472 ASSERT_TRUE(callee_video_sender->SetTrack(callee()->CreateLocalVideoTrack()));
2473 MediaExpectations media_expectations;
2474 media_expectations.ExpectBidirectionalAudioAndVideo();
2475 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002476}
2477
2478// This test verifies that a remote video track can be added via AddStream,
2479// and sent end-to-end. For this particular test, it's simply echoed back
2480// from the caller to the callee, rather than being forwarded to a third
2481// PeerConnection.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002482TEST_F(PeerConnectionIntegrationTestPlanB, CanSendRemoteVideoTrack) {
deadbeef1dcb1642017-03-29 21:08:16 -07002483 ASSERT_TRUE(CreatePeerConnectionWrappers());
2484 ConnectFakeSignaling();
2485 // Just send a video track from the caller.
Steve Anton15324772018-01-16 10:26:49 -08002486 caller()->AddVideoTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07002487 caller()->CreateAndSetAndSignalOffer();
2488 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02002489 ASSERT_EQ(1U, callee()->remote_streams()->count());
deadbeef1dcb1642017-03-29 21:08:16 -07002490
2491 // Echo the stream back, and do a new offer/anwer (initiated by callee this
2492 // time).
2493 callee()->pc()->AddStream(callee()->remote_streams()->at(0));
2494 callee()->CreateAndSetAndSignalOffer();
2495 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
2496
Seth Hampson2f0d7022018-02-20 11:54:42 -08002497 MediaExpectations media_expectations;
2498 media_expectations.ExpectBidirectionalVideo();
2499 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002500}
2501
Harald Alvestrandec23d6d2021-02-11 10:47:22 +00002502#if !defined(THREAD_SANITIZER)
2503// This test provokes TSAN errors. bugs.webrtc.org/11282
2504
deadbeef1dcb1642017-03-29 21:08:16 -07002505// Test that we achieve the expected end-to-end connection time, using a
2506// fake clock and simulated latency on the media and signaling paths.
2507// We use a TURN<->TURN connection because this is usually the quickest to
2508// set up initially, especially when we're confident the connection will work
2509// and can start sending media before we get a STUN response.
2510//
2511// With various optimizations enabled, here are the network delays we expect to
2512// be on the critical path:
2513// 1. 2 signaling trips: Signaling offer and offerer's TURN candidate, then
2514// signaling answer (with DTLS fingerprint).
2515// 2. 9 media hops: Rest of the DTLS handshake. 3 hops in each direction when
2516// using TURN<->TURN pair, and DTLS exchange is 4 packets,
2517// the first of which should have arrived before the answer.
Yves Gerey100fe632020-01-17 19:15:53 +01002518TEST_P(PeerConnectionIntegrationTestWithFakeClock,
2519 EndToEndConnectionTimeWithTurnTurnPair) {
deadbeef1dcb1642017-03-29 21:08:16 -07002520 static constexpr int media_hop_delay_ms = 50;
2521 static constexpr int signaling_trip_delay_ms = 500;
2522 // For explanation of these values, see comment above.
2523 static constexpr int required_media_hops = 9;
2524 static constexpr int required_signaling_trips = 2;
2525 // For internal delays (such as posting an event asychronously).
2526 static constexpr int allowed_internal_delay_ms = 20;
2527 static constexpr int total_connection_time_ms =
2528 media_hop_delay_ms * required_media_hops +
2529 signaling_trip_delay_ms * required_signaling_trips +
2530 allowed_internal_delay_ms;
2531
2532 static const rtc::SocketAddress turn_server_1_internal_address{"88.88.88.0",
2533 3478};
2534 static const rtc::SocketAddress turn_server_1_external_address{"88.88.88.1",
2535 0};
2536 static const rtc::SocketAddress turn_server_2_internal_address{"99.99.99.0",
2537 3478};
2538 static const rtc::SocketAddress turn_server_2_external_address{"99.99.99.1",
2539 0};
Seth Hampsonaed71642018-06-11 07:41:32 -07002540 cricket::TestTurnServer* turn_server_1 = CreateTurnServer(
2541 turn_server_1_internal_address, turn_server_1_external_address);
Jonas Orelandbdcee282017-10-10 14:01:40 +02002542
Seth Hampsonaed71642018-06-11 07:41:32 -07002543 cricket::TestTurnServer* turn_server_2 = CreateTurnServer(
2544 turn_server_2_internal_address, turn_server_2_external_address);
deadbeef1dcb1642017-03-29 21:08:16 -07002545 // Bypass permission check on received packets so media can be sent before
2546 // the candidate is signaled.
Seth Hampsonaed71642018-06-11 07:41:32 -07002547 network_thread()->Invoke<void>(RTC_FROM_HERE, [turn_server_1] {
2548 turn_server_1->set_enable_permission_checks(false);
2549 });
2550 network_thread()->Invoke<void>(RTC_FROM_HERE, [turn_server_2] {
2551 turn_server_2->set_enable_permission_checks(false);
2552 });
deadbeef1dcb1642017-03-29 21:08:16 -07002553
2554 PeerConnectionInterface::RTCConfiguration client_1_config;
2555 webrtc::PeerConnectionInterface::IceServer ice_server_1;
2556 ice_server_1.urls.push_back("turn:88.88.88.0:3478");
2557 ice_server_1.username = "test";
2558 ice_server_1.password = "test";
2559 client_1_config.servers.push_back(ice_server_1);
2560 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
2561 client_1_config.presume_writable_when_fully_relayed = true;
2562
2563 PeerConnectionInterface::RTCConfiguration client_2_config;
2564 webrtc::PeerConnectionInterface::IceServer ice_server_2;
2565 ice_server_2.urls.push_back("turn:99.99.99.0:3478");
2566 ice_server_2.username = "test";
2567 ice_server_2.password = "test";
2568 client_2_config.servers.push_back(ice_server_2);
2569 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
2570 client_2_config.presume_writable_when_fully_relayed = true;
2571
2572 ASSERT_TRUE(
2573 CreatePeerConnectionWrappersWithConfig(client_1_config, client_2_config));
2574 // Set up the simulated delays.
2575 SetSignalingDelayMs(signaling_trip_delay_ms);
2576 ConnectFakeSignaling();
2577 virtual_socket_server()->set_delay_mean(media_hop_delay_ms);
2578 virtual_socket_server()->UpdateDelayDistribution();
2579
2580 // Set "offer to receive audio/video" without adding any tracks, so we just
2581 // set up ICE/DTLS with no media.
2582 PeerConnectionInterface::RTCOfferAnswerOptions options;
2583 options.offer_to_receive_audio = 1;
2584 options.offer_to_receive_video = 1;
2585 caller()->SetOfferAnswerOptions(options);
2586 caller()->CreateAndSetAndSignalOffer();
deadbeef71452802017-05-07 17:21:01 -07002587 EXPECT_TRUE_SIMULATED_WAIT(DtlsConnected(), total_connection_time_ms,
Yves Gerey100fe632020-01-17 19:15:53 +01002588 FakeClock());
Seth Hampson1d4a76d2018-06-19 14:31:41 -07002589 // Closing the PeerConnections destroys the ports before the ScopedFakeClock.
2590 // If this is not done a DCHECK can be hit in ports.cc, because a large
2591 // negative number is calculated for the rtt due to the global clock changing.
Steve Antond91969e2019-05-30 12:27:03 -07002592 ClosePeerConnections();
deadbeef1dcb1642017-03-29 21:08:16 -07002593}
2594
Harald Alvestrandec23d6d2021-02-11 10:47:22 +00002595#endif // !defined(THREAD_SANITIZER)
2596
Jonas Orelandbdcee282017-10-10 14:01:40 +02002597// Verify that a TurnCustomizer passed in through RTCConfiguration
2598// is actually used by the underlying TURN candidate pair.
2599// Note that turnport_unittest.cc contains more detailed, lower-level tests.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002600TEST_P(PeerConnectionIntegrationTest, TurnCustomizerUsedForTurnConnections) {
Jonas Orelandbdcee282017-10-10 14:01:40 +02002601 static const rtc::SocketAddress turn_server_1_internal_address{"88.88.88.0",
2602 3478};
2603 static const rtc::SocketAddress turn_server_1_external_address{"88.88.88.1",
2604 0};
2605 static const rtc::SocketAddress turn_server_2_internal_address{"99.99.99.0",
2606 3478};
2607 static const rtc::SocketAddress turn_server_2_external_address{"99.99.99.1",
2608 0};
Seth Hampsonaed71642018-06-11 07:41:32 -07002609 CreateTurnServer(turn_server_1_internal_address,
2610 turn_server_1_external_address);
2611 CreateTurnServer(turn_server_2_internal_address,
2612 turn_server_2_external_address);
Jonas Orelandbdcee282017-10-10 14:01:40 +02002613
2614 PeerConnectionInterface::RTCConfiguration client_1_config;
2615 webrtc::PeerConnectionInterface::IceServer ice_server_1;
2616 ice_server_1.urls.push_back("turn:88.88.88.0:3478");
2617 ice_server_1.username = "test";
2618 ice_server_1.password = "test";
2619 client_1_config.servers.push_back(ice_server_1);
2620 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
Seth Hampsonaed71642018-06-11 07:41:32 -07002621 auto* customizer1 = CreateTurnCustomizer();
2622 client_1_config.turn_customizer = customizer1;
Jonas Orelandbdcee282017-10-10 14:01:40 +02002623
2624 PeerConnectionInterface::RTCConfiguration client_2_config;
2625 webrtc::PeerConnectionInterface::IceServer ice_server_2;
2626 ice_server_2.urls.push_back("turn:99.99.99.0:3478");
2627 ice_server_2.username = "test";
2628 ice_server_2.password = "test";
2629 client_2_config.servers.push_back(ice_server_2);
2630 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
Seth Hampsonaed71642018-06-11 07:41:32 -07002631 auto* customizer2 = CreateTurnCustomizer();
2632 client_2_config.turn_customizer = customizer2;
Jonas Orelandbdcee282017-10-10 14:01:40 +02002633
2634 ASSERT_TRUE(
2635 CreatePeerConnectionWrappersWithConfig(client_1_config, client_2_config));
2636 ConnectFakeSignaling();
2637
2638 // Set "offer to receive audio/video" without adding any tracks, so we just
2639 // set up ICE/DTLS with no media.
2640 PeerConnectionInterface::RTCOfferAnswerOptions options;
2641 options.offer_to_receive_audio = 1;
2642 options.offer_to_receive_video = 1;
2643 caller()->SetOfferAnswerOptions(options);
2644 caller()->CreateAndSetAndSignalOffer();
2645 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
2646
Seth Hampsonaed71642018-06-11 07:41:32 -07002647 ExpectTurnCustomizerCountersIncremented(customizer1);
2648 ExpectTurnCustomizerCountersIncremented(customizer2);
Jonas Orelandbdcee282017-10-10 14:01:40 +02002649}
2650
Benjamin Wright2d5f3cb2018-05-22 14:46:06 -07002651// Verifies that you can use TCP instead of UDP to connect to a TURN server and
2652// send media between the caller and the callee.
2653TEST_P(PeerConnectionIntegrationTest, TCPUsedForTurnConnections) {
2654 static const rtc::SocketAddress turn_server_internal_address{"88.88.88.0",
2655 3478};
2656 static const rtc::SocketAddress turn_server_external_address{"88.88.88.1", 0};
2657
2658 // Enable TCP for the fake turn server.
Seth Hampsonaed71642018-06-11 07:41:32 -07002659 CreateTurnServer(turn_server_internal_address, turn_server_external_address,
2660 cricket::PROTO_TCP);
Benjamin Wright2d5f3cb2018-05-22 14:46:06 -07002661
2662 webrtc::PeerConnectionInterface::IceServer ice_server;
2663 ice_server.urls.push_back("turn:88.88.88.0:3478?transport=tcp");
2664 ice_server.username = "test";
2665 ice_server.password = "test";
2666
2667 PeerConnectionInterface::RTCConfiguration client_1_config;
2668 client_1_config.servers.push_back(ice_server);
2669 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
2670
2671 PeerConnectionInterface::RTCConfiguration client_2_config;
2672 client_2_config.servers.push_back(ice_server);
2673 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
2674
2675 ASSERT_TRUE(
2676 CreatePeerConnectionWrappersWithConfig(client_1_config, client_2_config));
2677
2678 // Do normal offer/answer and wait for ICE to complete.
2679 ConnectFakeSignaling();
2680 caller()->AddAudioVideoTracks();
2681 callee()->AddAudioVideoTracks();
2682 caller()->CreateAndSetAndSignalOffer();
2683 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2684 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
2685 callee()->ice_connection_state(), kMaxWaitForFramesMs);
2686
2687 MediaExpectations media_expectations;
2688 media_expectations.ExpectBidirectionalAudioAndVideo();
2689 EXPECT_TRUE(ExpectNewFrames(media_expectations));
2690}
2691
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07002692// Verify that a SSLCertificateVerifier passed in through
2693// PeerConnectionDependencies is actually used by the underlying SSL
2694// implementation to determine whether a certificate presented by the TURN
2695// server is accepted by the client. Note that openssladapter_unittest.cc
2696// contains more detailed, lower-level tests.
2697TEST_P(PeerConnectionIntegrationTest,
2698 SSLCertificateVerifierUsedForTurnConnections) {
2699 static const rtc::SocketAddress turn_server_internal_address{"88.88.88.0",
2700 3478};
2701 static const rtc::SocketAddress turn_server_external_address{"88.88.88.1", 0};
2702
2703 // Enable TCP-TLS for the fake turn server. We need to pass in 88.88.88.0 so
2704 // that host name verification passes on the fake certificate.
Seth Hampsonaed71642018-06-11 07:41:32 -07002705 CreateTurnServer(turn_server_internal_address, turn_server_external_address,
2706 cricket::PROTO_TLS, "88.88.88.0");
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07002707
2708 webrtc::PeerConnectionInterface::IceServer ice_server;
2709 ice_server.urls.push_back("turns:88.88.88.0:3478?transport=tcp");
2710 ice_server.username = "test";
2711 ice_server.password = "test";
2712
2713 PeerConnectionInterface::RTCConfiguration client_1_config;
2714 client_1_config.servers.push_back(ice_server);
2715 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
2716
2717 PeerConnectionInterface::RTCConfiguration client_2_config;
2718 client_2_config.servers.push_back(ice_server);
2719 // Setting the type to kRelay forces the connection to go through a TURN
2720 // server.
2721 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
2722
2723 // Get a copy to the pointer so we can verify calls later.
2724 rtc::TestCertificateVerifier* client_1_cert_verifier =
2725 new rtc::TestCertificateVerifier();
2726 client_1_cert_verifier->verify_certificate_ = true;
2727 rtc::TestCertificateVerifier* client_2_cert_verifier =
2728 new rtc::TestCertificateVerifier();
2729 client_2_cert_verifier->verify_certificate_ = true;
2730
2731 // Create the dependencies with the test certificate verifier.
2732 webrtc::PeerConnectionDependencies client_1_deps(nullptr);
2733 client_1_deps.tls_cert_verifier =
2734 std::unique_ptr<rtc::TestCertificateVerifier>(client_1_cert_verifier);
2735 webrtc::PeerConnectionDependencies client_2_deps(nullptr);
2736 client_2_deps.tls_cert_verifier =
2737 std::unique_ptr<rtc::TestCertificateVerifier>(client_2_cert_verifier);
2738
2739 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndDeps(
2740 client_1_config, std::move(client_1_deps), client_2_config,
2741 std::move(client_2_deps)));
2742 ConnectFakeSignaling();
2743
2744 // Set "offer to receive audio/video" without adding any tracks, so we just
2745 // set up ICE/DTLS with no media.
2746 PeerConnectionInterface::RTCOfferAnswerOptions options;
2747 options.offer_to_receive_audio = 1;
2748 options.offer_to_receive_video = 1;
2749 caller()->SetOfferAnswerOptions(options);
2750 caller()->CreateAndSetAndSignalOffer();
2751 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
2752
2753 EXPECT_GT(client_1_cert_verifier->call_count_, 0u);
2754 EXPECT_GT(client_2_cert_verifier->call_count_, 0u);
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07002755}
2756
2757TEST_P(PeerConnectionIntegrationTest,
2758 SSLCertificateVerifierFailureUsedForTurnConnectionsFailsConnection) {
2759 static const rtc::SocketAddress turn_server_internal_address{"88.88.88.0",
2760 3478};
2761 static const rtc::SocketAddress turn_server_external_address{"88.88.88.1", 0};
2762
2763 // Enable TCP-TLS for the fake turn server. We need to pass in 88.88.88.0 so
2764 // that host name verification passes on the fake certificate.
Seth Hampsonaed71642018-06-11 07:41:32 -07002765 CreateTurnServer(turn_server_internal_address, turn_server_external_address,
2766 cricket::PROTO_TLS, "88.88.88.0");
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07002767
2768 webrtc::PeerConnectionInterface::IceServer ice_server;
2769 ice_server.urls.push_back("turns:88.88.88.0:3478?transport=tcp");
2770 ice_server.username = "test";
2771 ice_server.password = "test";
2772
2773 PeerConnectionInterface::RTCConfiguration client_1_config;
2774 client_1_config.servers.push_back(ice_server);
2775 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
2776
2777 PeerConnectionInterface::RTCConfiguration client_2_config;
2778 client_2_config.servers.push_back(ice_server);
2779 // Setting the type to kRelay forces the connection to go through a TURN
2780 // server.
2781 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
2782
2783 // Get a copy to the pointer so we can verify calls later.
2784 rtc::TestCertificateVerifier* client_1_cert_verifier =
2785 new rtc::TestCertificateVerifier();
2786 client_1_cert_verifier->verify_certificate_ = false;
2787 rtc::TestCertificateVerifier* client_2_cert_verifier =
2788 new rtc::TestCertificateVerifier();
2789 client_2_cert_verifier->verify_certificate_ = false;
2790
2791 // Create the dependencies with the test certificate verifier.
2792 webrtc::PeerConnectionDependencies client_1_deps(nullptr);
2793 client_1_deps.tls_cert_verifier =
2794 std::unique_ptr<rtc::TestCertificateVerifier>(client_1_cert_verifier);
2795 webrtc::PeerConnectionDependencies client_2_deps(nullptr);
2796 client_2_deps.tls_cert_verifier =
2797 std::unique_ptr<rtc::TestCertificateVerifier>(client_2_cert_verifier);
2798
2799 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndDeps(
2800 client_1_config, std::move(client_1_deps), client_2_config,
2801 std::move(client_2_deps)));
2802 ConnectFakeSignaling();
2803
2804 // Set "offer to receive audio/video" without adding any tracks, so we just
2805 // set up ICE/DTLS with no media.
2806 PeerConnectionInterface::RTCOfferAnswerOptions options;
2807 options.offer_to_receive_audio = 1;
2808 options.offer_to_receive_video = 1;
2809 caller()->SetOfferAnswerOptions(options);
2810 caller()->CreateAndSetAndSignalOffer();
2811 bool wait_res = true;
2812 // TODO(bugs.webrtc.org/9219): When IceConnectionState is implemented
2813 // properly, should be able to just wait for a state of "failed" instead of
2814 // waiting a fixed 10 seconds.
2815 WAIT_(DtlsConnected(), kDefaultTimeout, wait_res);
2816 ASSERT_FALSE(wait_res);
2817
2818 EXPECT_GT(client_1_cert_verifier->call_count_, 0u);
2819 EXPECT_GT(client_2_cert_verifier->call_count_, 0u);
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07002820}
2821
Qingsi Wang25ec8882019-11-15 12:33:05 -08002822// Test that the injected ICE transport factory is used to create ICE transports
2823// for WebRTC connections.
2824TEST_P(PeerConnectionIntegrationTest, IceTransportFactoryUsedForConnections) {
2825 PeerConnectionInterface::RTCConfiguration default_config;
2826 PeerConnectionDependencies dependencies(nullptr);
2827 auto ice_transport_factory = std::make_unique<MockIceTransportFactory>();
2828 EXPECT_CALL(*ice_transport_factory, RecordIceTransportCreated()).Times(1);
2829 dependencies.ice_transport_factory = std::move(ice_transport_factory);
Niels Möller2a707032020-06-16 16:39:13 +02002830 auto wrapper = CreatePeerConnectionWrapper("Caller", nullptr, &default_config,
2831 std::move(dependencies), nullptr,
2832 /*reset_encoder_factory=*/false,
2833 /*reset_decoder_factory=*/false);
Qingsi Wang25ec8882019-11-15 12:33:05 -08002834 ASSERT_TRUE(wrapper);
2835 wrapper->CreateDataChannel();
Tommi87f70902021-04-27 14:43:08 +02002836 auto observer = rtc::make_ref_counted<MockSetSessionDescriptionObserver>();
Qingsi Wang25ec8882019-11-15 12:33:05 -08002837 wrapper->pc()->SetLocalDescription(observer,
2838 wrapper->CreateOfferAndWait().release());
2839}
2840
deadbeefc964d0b2017-04-03 10:03:35 -07002841// Test that audio and video flow end-to-end when codec names don't use the
2842// expected casing, given that they're supposed to be case insensitive. To test
2843// this, all but one codec is removed from each media description, and its
2844// casing is changed.
2845//
2846// In the past, this has regressed and caused crashes/black video, due to the
2847// fact that code at some layers was doing case-insensitive comparisons and
2848// code at other layers was not.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002849TEST_P(PeerConnectionIntegrationTest, CodecNamesAreCaseInsensitive) {
deadbeefc964d0b2017-04-03 10:03:35 -07002850 ASSERT_TRUE(CreatePeerConnectionWrappers());
2851 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002852 caller()->AddAudioVideoTracks();
2853 callee()->AddAudioVideoTracks();
deadbeefc964d0b2017-04-03 10:03:35 -07002854
2855 // Remove all but one audio/video codec (opus and VP8), and change the
2856 // casing of the caller's generated offer.
2857 caller()->SetGeneratedSdpMunger([](cricket::SessionDescription* description) {
2858 cricket::AudioContentDescription* audio =
2859 GetFirstAudioContentDescription(description);
2860 ASSERT_NE(nullptr, audio);
2861 auto audio_codecs = audio->codecs();
2862 audio_codecs.erase(std::remove_if(audio_codecs.begin(), audio_codecs.end(),
2863 [](const cricket::AudioCodec& codec) {
2864 return codec.name != "opus";
2865 }),
2866 audio_codecs.end());
2867 ASSERT_EQ(1u, audio_codecs.size());
2868 audio_codecs[0].name = "OpUs";
2869 audio->set_codecs(audio_codecs);
2870
2871 cricket::VideoContentDescription* video =
2872 GetFirstVideoContentDescription(description);
2873 ASSERT_NE(nullptr, video);
2874 auto video_codecs = video->codecs();
2875 video_codecs.erase(std::remove_if(video_codecs.begin(), video_codecs.end(),
2876 [](const cricket::VideoCodec& codec) {
2877 return codec.name != "VP8";
2878 }),
2879 video_codecs.end());
2880 ASSERT_EQ(1u, video_codecs.size());
2881 video_codecs[0].name = "vP8";
2882 video->set_codecs(video_codecs);
2883 });
2884
2885 caller()->CreateAndSetAndSignalOffer();
2886 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2887
2888 // Verify frames are still received end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002889 MediaExpectations media_expectations;
2890 media_expectations.ExpectBidirectionalAudioAndVideo();
2891 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeefc964d0b2017-04-03 10:03:35 -07002892}
2893
Jonas Oreland49ac5952018-09-26 16:04:32 +02002894TEST_P(PeerConnectionIntegrationTest, GetSourcesAudio) {
hbos8d609f62017-04-10 07:39:05 -07002895 ASSERT_TRUE(CreatePeerConnectionWrappers());
2896 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002897 caller()->AddAudioTrack();
hbos8d609f62017-04-10 07:39:05 -07002898 caller()->CreateAndSetAndSignalOffer();
2899 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
deadbeefd8ad7882017-04-18 16:01:17 -07002900 // Wait for one audio frame to be received by the callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002901 MediaExpectations media_expectations;
2902 media_expectations.CalleeExpectsSomeAudio(1);
2903 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Jonas Oreland49ac5952018-09-26 16:04:32 +02002904 ASSERT_EQ(callee()->pc()->GetReceivers().size(), 1u);
hbos8d609f62017-04-10 07:39:05 -07002905 auto receiver = callee()->pc()->GetReceivers()[0];
2906 ASSERT_EQ(receiver->media_type(), cricket::MEDIA_TYPE_AUDIO);
Jonas Oreland49ac5952018-09-26 16:04:32 +02002907 auto sources = receiver->GetSources();
hbos8d609f62017-04-10 07:39:05 -07002908 ASSERT_GT(receiver->GetParameters().encodings.size(), 0u);
2909 EXPECT_EQ(receiver->GetParameters().encodings[0].ssrc,
Jonas Oreland49ac5952018-09-26 16:04:32 +02002910 sources[0].source_id());
2911 EXPECT_EQ(webrtc::RtpSourceType::SSRC, sources[0].source_type());
2912}
2913
2914TEST_P(PeerConnectionIntegrationTest, GetSourcesVideo) {
2915 ASSERT_TRUE(CreatePeerConnectionWrappers());
2916 ConnectFakeSignaling();
2917 caller()->AddVideoTrack();
2918 caller()->CreateAndSetAndSignalOffer();
2919 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2920 // Wait for one video frame to be received by the callee.
2921 MediaExpectations media_expectations;
2922 media_expectations.CalleeExpectsSomeVideo(1);
2923 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2924 ASSERT_EQ(callee()->pc()->GetReceivers().size(), 1u);
2925 auto receiver = callee()->pc()->GetReceivers()[0];
2926 ASSERT_EQ(receiver->media_type(), cricket::MEDIA_TYPE_VIDEO);
2927 auto sources = receiver->GetSources();
2928 ASSERT_GT(receiver->GetParameters().encodings.size(), 0u);
Yves Gereyf781bb52019-07-23 19:15:39 +02002929 ASSERT_GT(sources.size(), 0u);
Jonas Oreland49ac5952018-09-26 16:04:32 +02002930 EXPECT_EQ(receiver->GetParameters().encodings[0].ssrc,
2931 sources[0].source_id());
2932 EXPECT_EQ(webrtc::RtpSourceType::SSRC, sources[0].source_type());
hbos8d609f62017-04-10 07:39:05 -07002933}
2934
deadbeef2f425aa2017-04-14 10:41:32 -07002935// Test that if a track is removed and added again with a different stream ID,
2936// the new stream ID is successfully communicated in SDP and media continues to
2937// flow end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002938// TODO(webrtc.bugs.org/8734): This test does not work for Unified Plan because
2939// it will not reuse a transceiver that has already been sending. After creating
2940// a new transceiver it tries to create an offer with two senders of the same
2941// track ids and it fails.
2942TEST_F(PeerConnectionIntegrationTestPlanB, RemoveAndAddTrackWithNewStreamId) {
deadbeef2f425aa2017-04-14 10:41:32 -07002943 ASSERT_TRUE(CreatePeerConnectionWrappers());
2944 ConnectFakeSignaling();
2945
deadbeef2f425aa2017-04-14 10:41:32 -07002946 // Add track using stream 1, do offer/answer.
2947 rtc::scoped_refptr<webrtc::AudioTrackInterface> track =
2948 caller()->CreateLocalAudioTrack();
2949 rtc::scoped_refptr<webrtc::RtpSenderInterface> sender =
Steve Antond78323f2018-07-11 11:13:44 -07002950 caller()->AddTrack(track, {"stream_1"});
deadbeef2f425aa2017-04-14 10:41:32 -07002951 caller()->CreateAndSetAndSignalOffer();
2952 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002953 {
2954 MediaExpectations media_expectations;
2955 media_expectations.CalleeExpectsSomeAudio(1);
2956 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2957 }
deadbeef2f425aa2017-04-14 10:41:32 -07002958 // Remove the sender, and create a new one with the new stream.
2959 caller()->pc()->RemoveTrack(sender);
Steve Antond78323f2018-07-11 11:13:44 -07002960 sender = caller()->AddTrack(track, {"stream_2"});
deadbeef2f425aa2017-04-14 10:41:32 -07002961 caller()->CreateAndSetAndSignalOffer();
2962 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2963 // Wait for additional audio frames to be received by the callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002964 {
2965 MediaExpectations media_expectations;
2966 media_expectations.CalleeExpectsSomeAudio();
2967 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2968 }
deadbeef2f425aa2017-04-14 10:41:32 -07002969}
2970
Seth Hampson2f0d7022018-02-20 11:54:42 -08002971TEST_P(PeerConnectionIntegrationTest, RtcEventLogOutputWriteCalled) {
Elad Alon99c3fe52017-10-13 16:29:40 +02002972 ASSERT_TRUE(CreatePeerConnectionWrappers());
2973 ConnectFakeSignaling();
2974
Mirko Bonadei317a1f02019-09-17 17:06:18 +02002975 auto output = std::make_unique<testing::NiceMock<MockRtcEventLogOutput>>();
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002976 ON_CALL(*output, IsActive()).WillByDefault(::testing::Return(true));
2977 ON_CALL(*output, Write(::testing::_)).WillByDefault(::testing::Return(true));
Elad Alon99c3fe52017-10-13 16:29:40 +02002978 EXPECT_CALL(*output, Write(::testing::_)).Times(::testing::AtLeast(1));
Bjorn Tereliusde939432017-11-20 17:38:14 +01002979 EXPECT_TRUE(caller()->pc()->StartRtcEventLog(
2980 std::move(output), webrtc::RtcEventLog::kImmediateOutput));
Elad Alon99c3fe52017-10-13 16:29:40 +02002981
Steve Anton15324772018-01-16 10:26:49 -08002982 caller()->AddAudioVideoTracks();
Elad Alon99c3fe52017-10-13 16:29:40 +02002983 caller()->CreateAndSetAndSignalOffer();
2984 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2985}
2986
Steve Antonede9ca52017-10-16 13:04:27 -07002987// Test that if candidates are only signaled by applying full session
2988// descriptions (instead of using AddIceCandidate), the peers can connect to
2989// each other and exchange media.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002990TEST_P(PeerConnectionIntegrationTest, MediaFlowsWhenCandidatesSetOnlyInSdp) {
Steve Antonede9ca52017-10-16 13:04:27 -07002991 ASSERT_TRUE(CreatePeerConnectionWrappers());
2992 // Each side will signal the session descriptions but not candidates.
2993 ConnectFakeSignalingForSdpOnly();
2994
2995 // Add audio video track and exchange the initial offer/answer with media
2996 // information only. This will start ICE gathering on each side.
Steve Anton15324772018-01-16 10:26:49 -08002997 caller()->AddAudioVideoTracks();
2998 callee()->AddAudioVideoTracks();
Steve Antonede9ca52017-10-16 13:04:27 -07002999 caller()->CreateAndSetAndSignalOffer();
3000
3001 // Wait for all candidates to be gathered on both the caller and callee.
3002 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceGatheringComplete,
3003 caller()->ice_gathering_state(), kDefaultTimeout);
3004 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceGatheringComplete,
3005 callee()->ice_gathering_state(), kDefaultTimeout);
3006
3007 // The candidates will now be included in the session description, so
3008 // signaling them will start the ICE connection.
3009 caller()->CreateAndSetAndSignalOffer();
3010 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3011
3012 // Ensure that media flows in both directions.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003013 MediaExpectations media_expectations;
3014 media_expectations.ExpectBidirectionalAudioAndVideo();
3015 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Antonede9ca52017-10-16 13:04:27 -07003016}
3017
Harald Alvestrandec23d6d2021-02-11 10:47:22 +00003018#if !defined(THREAD_SANITIZER)
3019// These tests provokes TSAN errors. See bugs.webrtc.org/11305.
3020
henrika5f6bf242017-11-01 11:06:56 +01003021// Test that SetAudioPlayout can be used to disable audio playout from the
3022// start, then later enable it. This may be useful, for example, if the caller
3023// needs to play a local ringtone until some event occurs, after which it
3024// switches to playing the received audio.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003025TEST_P(PeerConnectionIntegrationTest, DisableAndEnableAudioPlayout) {
henrika5f6bf242017-11-01 11:06:56 +01003026 ASSERT_TRUE(CreatePeerConnectionWrappers());
3027 ConnectFakeSignaling();
3028
3029 // Set up audio-only call where audio playout is disabled on caller's side.
3030 caller()->pc()->SetAudioPlayout(false);
Steve Anton15324772018-01-16 10:26:49 -08003031 caller()->AddAudioTrack();
3032 callee()->AddAudioTrack();
henrika5f6bf242017-11-01 11:06:56 +01003033 caller()->CreateAndSetAndSignalOffer();
3034 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3035
3036 // Pump messages for a second.
3037 WAIT(false, 1000);
3038 // Since audio playout is disabled, the caller shouldn't have received
3039 // anything (at the playout level, at least).
3040 EXPECT_EQ(0, caller()->audio_frames_received());
3041 // As a sanity check, make sure the callee (for which playout isn't disabled)
3042 // did still see frames on its audio level.
3043 ASSERT_GT(callee()->audio_frames_received(), 0);
3044
3045 // Enable playout again, and ensure audio starts flowing.
3046 caller()->pc()->SetAudioPlayout(true);
Seth Hampson2f0d7022018-02-20 11:54:42 -08003047 MediaExpectations media_expectations;
3048 media_expectations.ExpectBidirectionalAudio();
3049 ASSERT_TRUE(ExpectNewFrames(media_expectations));
henrika5f6bf242017-11-01 11:06:56 +01003050}
3051
Harald Alvestrand39993842021-02-17 09:05:31 +00003052double GetAudioEnergyStat(PeerConnectionIntegrationWrapper* pc) {
henrika5f6bf242017-11-01 11:06:56 +01003053 auto report = pc->NewGetStats();
3054 auto track_stats_list =
3055 report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
3056 const webrtc::RTCMediaStreamTrackStats* remote_track_stats = nullptr;
3057 for (const auto* track_stats : track_stats_list) {
3058 if (track_stats->remote_source.is_defined() &&
3059 *track_stats->remote_source) {
3060 remote_track_stats = track_stats;
3061 break;
3062 }
3063 }
3064
3065 if (!remote_track_stats->total_audio_energy.is_defined()) {
3066 return 0.0;
3067 }
3068 return *remote_track_stats->total_audio_energy;
3069}
3070
3071// Test that if audio playout is disabled via the SetAudioPlayout() method, then
3072// incoming audio is still processed and statistics are generated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003073TEST_P(PeerConnectionIntegrationTest,
henrika5f6bf242017-11-01 11:06:56 +01003074 DisableAudioPlayoutStillGeneratesAudioStats) {
3075 ASSERT_TRUE(CreatePeerConnectionWrappers());
3076 ConnectFakeSignaling();
3077
3078 // Set up audio-only call where playout is disabled but audio-processing is
3079 // still active.
Steve Anton15324772018-01-16 10:26:49 -08003080 caller()->AddAudioTrack();
3081 callee()->AddAudioTrack();
henrika5f6bf242017-11-01 11:06:56 +01003082 caller()->pc()->SetAudioPlayout(false);
3083
3084 caller()->CreateAndSetAndSignalOffer();
3085 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3086
3087 // Wait for the callee to receive audio stats.
3088 EXPECT_TRUE_WAIT(GetAudioEnergyStat(caller()) > 0, kMaxWaitForFramesMs);
3089}
3090
Harald Alvestrandec23d6d2021-02-11 10:47:22 +00003091#endif // !defined(THREAD_SANITIZER)
3092
henrika4f167df2017-11-01 14:45:55 +01003093// Test that SetAudioRecording can be used to disable audio recording from the
3094// start, then later enable it. This may be useful, for example, if the caller
3095// wants to ensure that no audio resources are active before a certain state
3096// is reached.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003097TEST_P(PeerConnectionIntegrationTest, DisableAndEnableAudioRecording) {
henrika4f167df2017-11-01 14:45:55 +01003098 ASSERT_TRUE(CreatePeerConnectionWrappers());
3099 ConnectFakeSignaling();
3100
3101 // Set up audio-only call where audio recording is disabled on caller's side.
3102 caller()->pc()->SetAudioRecording(false);
Steve Anton15324772018-01-16 10:26:49 -08003103 caller()->AddAudioTrack();
3104 callee()->AddAudioTrack();
henrika4f167df2017-11-01 14:45:55 +01003105 caller()->CreateAndSetAndSignalOffer();
3106 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3107
3108 // Pump messages for a second.
3109 WAIT(false, 1000);
3110 // Since caller has disabled audio recording, the callee shouldn't have
3111 // received anything.
3112 EXPECT_EQ(0, callee()->audio_frames_received());
3113 // As a sanity check, make sure the caller did still see frames on its
3114 // audio level since audio recording is enabled on the calle side.
3115 ASSERT_GT(caller()->audio_frames_received(), 0);
3116
3117 // Enable audio recording again, and ensure audio starts flowing.
3118 caller()->pc()->SetAudioRecording(true);
Seth Hampson2f0d7022018-02-20 11:54:42 -08003119 MediaExpectations media_expectations;
3120 media_expectations.ExpectBidirectionalAudio();
3121 ASSERT_TRUE(ExpectNewFrames(media_expectations));
henrika4f167df2017-11-01 14:45:55 +01003122}
3123
Qingsi Wang7685e862018-06-11 20:15:46 -07003124TEST_P(PeerConnectionIntegrationTest,
3125 IceEventsGeneratedAndLoggedInRtcEventLog) {
3126 ASSERT_TRUE(CreatePeerConnectionWrappersWithFakeRtcEventLog());
3127 ConnectFakeSignaling();
3128 PeerConnectionInterface::RTCOfferAnswerOptions options;
3129 options.offer_to_receive_audio = 1;
3130 caller()->SetOfferAnswerOptions(options);
3131 caller()->CreateAndSetAndSignalOffer();
3132 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
3133 ASSERT_NE(nullptr, caller()->event_log_factory());
3134 ASSERT_NE(nullptr, callee()->event_log_factory());
3135 webrtc::FakeRtcEventLog* caller_event_log =
Danil Chapovalov4f281f12021-01-18 13:29:00 +01003136 caller()->event_log_factory()->last_log_created();
Qingsi Wang7685e862018-06-11 20:15:46 -07003137 webrtc::FakeRtcEventLog* callee_event_log =
Danil Chapovalov4f281f12021-01-18 13:29:00 +01003138 callee()->event_log_factory()->last_log_created();
Qingsi Wang7685e862018-06-11 20:15:46 -07003139 ASSERT_NE(nullptr, caller_event_log);
3140 ASSERT_NE(nullptr, callee_event_log);
3141 int caller_ice_config_count = caller_event_log->GetEventCount(
3142 webrtc::RtcEvent::Type::IceCandidatePairConfig);
3143 int caller_ice_event_count = caller_event_log->GetEventCount(
3144 webrtc::RtcEvent::Type::IceCandidatePairEvent);
3145 int callee_ice_config_count = callee_event_log->GetEventCount(
3146 webrtc::RtcEvent::Type::IceCandidatePairConfig);
3147 int callee_ice_event_count = callee_event_log->GetEventCount(
3148 webrtc::RtcEvent::Type::IceCandidatePairEvent);
3149 EXPECT_LT(0, caller_ice_config_count);
3150 EXPECT_LT(0, caller_ice_event_count);
3151 EXPECT_LT(0, callee_ice_config_count);
3152 EXPECT_LT(0, callee_ice_event_count);
3153}
3154
Qingsi Wangc129c352019-04-18 10:41:58 -07003155TEST_P(PeerConnectionIntegrationTest, RegatherAfterChangingIceTransportType) {
Qingsi Wangc129c352019-04-18 10:41:58 -07003156 static const rtc::SocketAddress turn_server_internal_address{"88.88.88.0",
3157 3478};
3158 static const rtc::SocketAddress turn_server_external_address{"88.88.88.1", 0};
3159
3160 CreateTurnServer(turn_server_internal_address, turn_server_external_address);
3161
3162 webrtc::PeerConnectionInterface::IceServer ice_server;
3163 ice_server.urls.push_back("turn:88.88.88.0:3478");
3164 ice_server.username = "test";
3165 ice_server.password = "test";
3166
3167 PeerConnectionInterface::RTCConfiguration caller_config;
3168 caller_config.servers.push_back(ice_server);
3169 caller_config.type = webrtc::PeerConnectionInterface::kRelay;
3170 caller_config.continual_gathering_policy = PeerConnection::GATHER_CONTINUALLY;
Qingsi Wang1fe119f2019-05-31 16:55:33 -07003171 caller_config.surface_ice_candidates_on_ice_transport_type_changed = true;
Qingsi Wangc129c352019-04-18 10:41:58 -07003172
3173 PeerConnectionInterface::RTCConfiguration callee_config;
3174 callee_config.servers.push_back(ice_server);
3175 callee_config.type = webrtc::PeerConnectionInterface::kRelay;
3176 callee_config.continual_gathering_policy = PeerConnection::GATHER_CONTINUALLY;
Qingsi Wang1fe119f2019-05-31 16:55:33 -07003177 callee_config.surface_ice_candidates_on_ice_transport_type_changed = true;
Qingsi Wangc129c352019-04-18 10:41:58 -07003178
3179 ASSERT_TRUE(
3180 CreatePeerConnectionWrappersWithConfig(caller_config, callee_config));
3181
3182 // Do normal offer/answer and wait for ICE to complete.
3183 ConnectFakeSignaling();
3184 caller()->AddAudioVideoTracks();
3185 callee()->AddAudioVideoTracks();
3186 caller()->CreateAndSetAndSignalOffer();
3187 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3188 // Since we are doing continual gathering, the ICE transport does not reach
3189 // kIceGatheringComplete (see
3190 // P2PTransportChannel::OnCandidatesAllocationDone), and consequently not
3191 // kIceConnectionComplete.
3192 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
3193 caller()->ice_connection_state(), kDefaultTimeout);
3194 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
3195 callee()->ice_connection_state(), kDefaultTimeout);
3196 // Note that we cannot use the metric
3197 // |WebRTC.PeerConnection.CandidatePairType_UDP| in this test since this
3198 // metric is only populated when we reach kIceConnectionComplete in the
3199 // current implementation.
3200 EXPECT_EQ(cricket::RELAY_PORT_TYPE,
3201 caller()->last_candidate_gathered().type());
3202 EXPECT_EQ(cricket::RELAY_PORT_TYPE,
3203 callee()->last_candidate_gathered().type());
3204
3205 // Loosen the caller's candidate filter.
3206 caller_config = caller()->pc()->GetConfiguration();
3207 caller_config.type = webrtc::PeerConnectionInterface::kAll;
3208 caller()->pc()->SetConfiguration(caller_config);
3209 // We should have gathered a new host candidate.
3210 EXPECT_EQ_WAIT(cricket::LOCAL_PORT_TYPE,
3211 caller()->last_candidate_gathered().type(), kDefaultTimeout);
3212
3213 // Loosen the callee's candidate filter.
3214 callee_config = callee()->pc()->GetConfiguration();
3215 callee_config.type = webrtc::PeerConnectionInterface::kAll;
3216 callee()->pc()->SetConfiguration(callee_config);
3217 EXPECT_EQ_WAIT(cricket::LOCAL_PORT_TYPE,
3218 callee()->last_candidate_gathered().type(), kDefaultTimeout);
Jonas Orelande3096512020-05-27 09:01:05 +02003219
3220 // Create an offer and verify that it does not contain an ICE restart (i.e new
3221 // ice credentials).
3222 std::string caller_ufrag_pre_offer = caller()
3223 ->pc()
3224 ->local_description()
3225 ->description()
3226 ->transport_infos()[0]
3227 .description.ice_ufrag;
3228 caller()->CreateAndSetAndSignalOffer();
3229 std::string caller_ufrag_post_offer = caller()
3230 ->pc()
3231 ->local_description()
3232 ->description()
3233 ->transport_infos()[0]
3234 .description.ice_ufrag;
3235 EXPECT_EQ(caller_ufrag_pre_offer, caller_ufrag_post_offer);
Qingsi Wangc129c352019-04-18 10:41:58 -07003236}
3237
Eldar Relloda13ea22019-06-01 12:23:43 +03003238TEST_P(PeerConnectionIntegrationTest, OnIceCandidateError) {
Eldar Relloda13ea22019-06-01 12:23:43 +03003239 static const rtc::SocketAddress turn_server_internal_address{"88.88.88.0",
3240 3478};
3241 static const rtc::SocketAddress turn_server_external_address{"88.88.88.1", 0};
3242
3243 CreateTurnServer(turn_server_internal_address, turn_server_external_address);
3244
3245 webrtc::PeerConnectionInterface::IceServer ice_server;
3246 ice_server.urls.push_back("turn:88.88.88.0:3478");
3247 ice_server.username = "test";
3248 ice_server.password = "123";
3249
3250 PeerConnectionInterface::RTCConfiguration caller_config;
3251 caller_config.servers.push_back(ice_server);
3252 caller_config.type = webrtc::PeerConnectionInterface::kRelay;
3253 caller_config.continual_gathering_policy = PeerConnection::GATHER_CONTINUALLY;
3254
3255 PeerConnectionInterface::RTCConfiguration callee_config;
3256 callee_config.servers.push_back(ice_server);
3257 callee_config.type = webrtc::PeerConnectionInterface::kRelay;
3258 callee_config.continual_gathering_policy = PeerConnection::GATHER_CONTINUALLY;
3259
3260 ASSERT_TRUE(
3261 CreatePeerConnectionWrappersWithConfig(caller_config, callee_config));
3262
3263 // Do normal offer/answer and wait for ICE to complete.
3264 ConnectFakeSignaling();
3265 caller()->AddAudioVideoTracks();
3266 callee()->AddAudioVideoTracks();
3267 caller()->CreateAndSetAndSignalOffer();
3268 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3269 EXPECT_EQ_WAIT(401, caller()->error_event().error_code, kDefaultTimeout);
3270 EXPECT_EQ("Unauthorized", caller()->error_event().error_text);
3271 EXPECT_EQ("turn:88.88.88.0:3478?transport=udp", caller()->error_event().url);
Eldar Rello0095d372019-12-02 22:22:07 +02003272 EXPECT_NE(caller()->error_event().address, "");
Eldar Relloda13ea22019-06-01 12:23:43 +03003273}
3274
Eldar Rellofa8019c2020-05-14 11:59:33 +03003275TEST_P(PeerConnectionIntegrationTest, OnIceCandidateErrorWithEmptyAddress) {
3276 webrtc::PeerConnectionInterface::IceServer ice_server;
3277 ice_server.urls.push_back("turn:127.0.0.1:3478?transport=tcp");
3278 ice_server.username = "test";
3279 ice_server.password = "test";
3280
3281 PeerConnectionInterface::RTCConfiguration caller_config;
3282 caller_config.servers.push_back(ice_server);
3283 caller_config.type = webrtc::PeerConnectionInterface::kRelay;
3284 caller_config.continual_gathering_policy = PeerConnection::GATHER_CONTINUALLY;
3285
3286 PeerConnectionInterface::RTCConfiguration callee_config;
3287 callee_config.servers.push_back(ice_server);
3288 callee_config.type = webrtc::PeerConnectionInterface::kRelay;
3289 callee_config.continual_gathering_policy = PeerConnection::GATHER_CONTINUALLY;
3290
3291 ASSERT_TRUE(
3292 CreatePeerConnectionWrappersWithConfig(caller_config, callee_config));
3293
3294 // Do normal offer/answer and wait for ICE to complete.
3295 ConnectFakeSignaling();
3296 caller()->AddAudioVideoTracks();
3297 callee()->AddAudioVideoTracks();
3298 caller()->CreateAndSetAndSignalOffer();
3299 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3300 EXPECT_EQ_WAIT(701, caller()->error_event().error_code, kDefaultTimeout);
3301 EXPECT_EQ(caller()->error_event().address, "");
3302}
3303
Eldar Rello5ab79e62019-10-09 18:29:44 +03003304TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
3305 AudioKeepsFlowingAfterImplicitRollback) {
3306 PeerConnectionInterface::RTCConfiguration config;
3307 config.sdp_semantics = SdpSemantics::kUnifiedPlan;
3308 config.enable_implicit_rollback = true;
3309 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
3310 ConnectFakeSignaling();
3311 caller()->AddAudioTrack();
3312 callee()->AddAudioTrack();
3313 caller()->CreateAndSetAndSignalOffer();
3314 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3315 MediaExpectations media_expectations;
3316 media_expectations.ExpectBidirectionalAudio();
3317 ASSERT_TRUE(ExpectNewFrames(media_expectations));
3318 SetSignalIceCandidates(false); // Workaround candidate outrace sdp.
3319 caller()->AddVideoTrack();
3320 callee()->AddVideoTrack();
Tommi87f70902021-04-27 14:43:08 +02003321 auto observer = rtc::make_ref_counted<MockSetSessionDescriptionObserver>();
Eldar Rello5ab79e62019-10-09 18:29:44 +03003322 callee()->pc()->SetLocalDescription(observer,
3323 callee()->CreateOfferAndWait().release());
3324 EXPECT_TRUE_WAIT(observer->called(), kDefaultTimeout);
3325 caller()->CreateAndSetAndSignalOffer(); // Implicit rollback.
3326 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3327 ASSERT_TRUE(ExpectNewFrames(media_expectations));
3328}
3329
3330TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
3331 ImplicitRollbackVisitsStableState) {
3332 RTCConfiguration config;
3333 config.sdp_semantics = SdpSemantics::kUnifiedPlan;
3334 config.enable_implicit_rollback = true;
3335
3336 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
3337
Tommi87f70902021-04-27 14:43:08 +02003338 auto sld_observer =
3339 rtc::make_ref_counted<MockSetSessionDescriptionObserver>();
Eldar Rello5ab79e62019-10-09 18:29:44 +03003340 callee()->pc()->SetLocalDescription(sld_observer,
3341 callee()->CreateOfferAndWait().release());
3342 EXPECT_TRUE_WAIT(sld_observer->called(), kDefaultTimeout);
3343 EXPECT_EQ(sld_observer->error(), "");
3344
Tommi87f70902021-04-27 14:43:08 +02003345 auto srd_observer =
3346 rtc::make_ref_counted<MockSetSessionDescriptionObserver>();
Eldar Rello5ab79e62019-10-09 18:29:44 +03003347 callee()->pc()->SetRemoteDescription(
3348 srd_observer, caller()->CreateOfferAndWait().release());
3349 EXPECT_TRUE_WAIT(srd_observer->called(), kDefaultTimeout);
3350 EXPECT_EQ(srd_observer->error(), "");
3351
3352 EXPECT_THAT(callee()->peer_connection_signaling_state_history(),
3353 ElementsAre(PeerConnectionInterface::kHaveLocalOffer,
3354 PeerConnectionInterface::kStable,
3355 PeerConnectionInterface::kHaveRemoteOffer));
3356}
3357
Eldar Rellobd9c33a2020-10-01 17:52:45 +03003358TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
3359 H264FmtpSpsPpsIdrInKeyframeParameterUsage) {
3360 ASSERT_TRUE(CreatePeerConnectionWrappers());
3361 ConnectFakeSignaling();
3362 caller()->AddVideoTrack();
3363 callee()->AddVideoTrack();
3364 auto munger = [](cricket::SessionDescription* desc) {
3365 cricket::VideoContentDescription* video =
3366 GetFirstVideoContentDescription(desc);
3367 auto codecs = video->codecs();
3368 for (auto&& codec : codecs) {
3369 if (codec.name == "H264") {
3370 std::string value;
3371 // The parameter is not supposed to be present in SDP by default.
3372 EXPECT_FALSE(
3373 codec.GetParam(cricket::kH264FmtpSpsPpsIdrInKeyframe, &value));
3374 codec.SetParam(std::string(cricket::kH264FmtpSpsPpsIdrInKeyframe),
3375 std::string(""));
3376 }
3377 }
3378 video->set_codecs(codecs);
3379 };
3380 // Munge local offer for SLD.
3381 caller()->SetGeneratedSdpMunger(munger);
3382 // Munge remote answer for SRD.
3383 caller()->SetReceivedSdpMunger(munger);
3384 caller()->CreateAndSetAndSignalOffer();
3385 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3386 // Observe that after munging the parameter is present in generated SDP.
3387 caller()->SetGeneratedSdpMunger([](cricket::SessionDescription* desc) {
3388 cricket::VideoContentDescription* video =
3389 GetFirstVideoContentDescription(desc);
3390 for (auto&& codec : video->codecs()) {
3391 if (codec.name == "H264") {
3392 std::string value;
3393 EXPECT_TRUE(
3394 codec.GetParam(cricket::kH264FmtpSpsPpsIdrInKeyframe, &value));
3395 }
3396 }
3397 });
3398 caller()->CreateOfferAndWait();
3399}
3400
Harald Alvestrand1a9be302020-12-11 14:53:59 +00003401TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
Harald Alvestrand94324f22021-01-13 12:31:53 +00003402 RenegotiateManyAudioTransceivers) {
Harald Alvestrand1a9be302020-12-11 14:53:59 +00003403 PeerConnectionInterface::RTCConfiguration config;
3404 config.sdp_semantics = SdpSemantics::kUnifiedPlan;
3405 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
3406 ConnectFakeSignaling();
3407 caller()->pc()->AddTransceiver(cricket::MEDIA_TYPE_AUDIO);
3408
3409 caller()->CreateAndSetAndSignalOffer();
3410 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3411 int current_size = caller()->pc()->GetTransceivers().size();
3412 // Add more tracks until we get close to having issues.
3413 // Issues have been seen at:
3414 // - 32 tracks on android_arm64_rel and android_arm_dbg bots
Harald Alvestrandcc6ae442021-01-18 08:06:23 +00003415 // - 16 tracks on android_arm_dbg (flaky)
3416 while (current_size < 8) {
Harald Alvestrand1a9be302020-12-11 14:53:59 +00003417 // Double the number of tracks
3418 for (int i = 0; i < current_size; i++) {
3419 caller()->pc()->AddTransceiver(cricket::MEDIA_TYPE_AUDIO);
3420 }
3421 current_size = caller()->pc()->GetTransceivers().size();
3422 RTC_LOG(LS_INFO) << "Renegotiating with " << current_size << " tracks";
3423 auto start_time_ms = rtc::TimeMillis();
3424 caller()->CreateAndSetAndSignalOffer();
3425 // We want to stop when the time exceeds one second.
3426 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3427 auto elapsed_time_ms = rtc::TimeMillis() - start_time_ms;
3428 RTC_LOG(LS_INFO) << "Renegotiating took " << elapsed_time_ms << " ms";
3429 ASSERT_GT(1000, elapsed_time_ms)
3430 << "Audio transceivers: Negotiation took too long after "
3431 << current_size << " tracks added";
3432 }
3433}
3434
3435TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
3436 RenegotiateManyVideoTransceivers) {
3437 PeerConnectionInterface::RTCConfiguration config;
3438 config.sdp_semantics = SdpSemantics::kUnifiedPlan;
3439 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
3440 ConnectFakeSignaling();
3441 caller()->pc()->AddTransceiver(cricket::MEDIA_TYPE_VIDEO);
3442
3443 caller()->CreateAndSetAndSignalOffer();
3444 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3445 int current_size = caller()->pc()->GetTransceivers().size();
3446 // Add more tracks until we get close to having issues.
3447 // Issues have been seen at:
3448 // - 96 on a Linux workstation
3449 // - 64 at win_x86_more_configs and win_x64_msvc_dbg
3450 // - 32 on android_arm64_rel and linux_dbg bots
Harald Alvestrand785e23b2021-03-15 21:26:27 +00003451 // - 16 on Android 64 (Nexus 5x)
3452 while (current_size < 8) {
Harald Alvestrand1a9be302020-12-11 14:53:59 +00003453 // Double the number of tracks
3454 for (int i = 0; i < current_size; i++) {
3455 caller()->pc()->AddTransceiver(cricket::MEDIA_TYPE_VIDEO);
3456 }
3457 current_size = caller()->pc()->GetTransceivers().size();
3458 RTC_LOG(LS_INFO) << "Renegotiating with " << current_size << " tracks";
3459 auto start_time_ms = rtc::TimeMillis();
3460 caller()->CreateAndSetAndSignalOffer();
3461 // We want to stop when the time exceeds one second.
3462 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3463 auto elapsed_time_ms = rtc::TimeMillis() - start_time_ms;
3464 RTC_LOG(LS_INFO) << "Renegotiating took " << elapsed_time_ms << " ms";
3465 ASSERT_GT(1000, elapsed_time_ms)
3466 << "Video transceivers: Negotiation took too long after "
3467 << current_size << " tracks added";
3468 }
3469}
3470
Harald Alvestrand94324f22021-01-13 12:31:53 +00003471TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
3472 RenegotiateManyVideoTransceiversAndWatchAudioDelay) {
3473 PeerConnectionInterface::RTCConfiguration config;
3474 config.sdp_semantics = SdpSemantics::kUnifiedPlan;
3475 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
3476 ConnectFakeSignaling();
3477 caller()->AddAudioTrack();
Harald Alvestrandcc6ae442021-01-18 08:06:23 +00003478 callee()->AddAudioTrack();
Harald Alvestrand94324f22021-01-13 12:31:53 +00003479 caller()->CreateAndSetAndSignalOffer();
3480 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3481 // Wait until we can see the audio flowing.
3482 MediaExpectations media_expectations;
3483 media_expectations.CalleeExpectsSomeAudio();
3484 ASSERT_TRUE(ExpectNewFrames(media_expectations));
3485
Harald Alvestrandcc6ae442021-01-18 08:06:23 +00003486 // Get the baseline numbers for audio_packets and audio_delay
3487 // in both directions.
3488 caller()->StartWatchingDelayStats();
3489 callee()->StartWatchingDelayStats();
Harald Alvestrand94324f22021-01-13 12:31:53 +00003490
3491 int current_size = caller()->pc()->GetTransceivers().size();
3492 // Add more tracks until we get close to having issues.
3493 // Making this number very large makes the test very slow.
Harald Alvestrandcc6ae442021-01-18 08:06:23 +00003494 while (current_size < 16) {
Harald Alvestrand94324f22021-01-13 12:31:53 +00003495 // Double the number of tracks
3496 for (int i = 0; i < current_size; i++) {
3497 caller()->pc()->AddTransceiver(cricket::MEDIA_TYPE_VIDEO);
3498 }
3499 current_size = caller()->pc()->GetTransceivers().size();
3500 RTC_LOG(LS_INFO) << "Renegotiating with " << current_size << " tracks";
3501 auto start_time_ms = rtc::TimeMillis();
3502 caller()->CreateAndSetAndSignalOffer();
3503 // We want to stop when the time exceeds one second.
3504 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3505 auto elapsed_time_ms = rtc::TimeMillis() - start_time_ms;
3506 RTC_LOG(LS_INFO) << "Renegotiating took " << elapsed_time_ms << " ms";
3507 // This is a guard against the test using excessive amounts of time.
3508 ASSERT_GT(5000, elapsed_time_ms)
3509 << "Video transceivers: Negotiation took too long after "
3510 << current_size << " tracks added";
Harald Alvestrandcc6ae442021-01-18 08:06:23 +00003511 caller()->UpdateDelayStats("caller reception", current_size);
3512 callee()->UpdateDelayStats("callee reception", current_size);
Harald Alvestrand94324f22021-01-13 12:31:53 +00003513 }
3514}
3515
Mirko Bonadeic84f6612019-01-31 12:20:57 +01003516INSTANTIATE_TEST_SUITE_P(PeerConnectionIntegrationTest,
3517 PeerConnectionIntegrationTest,
3518 Values(SdpSemantics::kPlanB,
3519 SdpSemantics::kUnifiedPlan));
Steve Antond3679212018-01-17 17:41:02 -08003520
Yves Gerey100fe632020-01-17 19:15:53 +01003521INSTANTIATE_TEST_SUITE_P(PeerConnectionIntegrationTest,
3522 PeerConnectionIntegrationTestWithFakeClock,
3523 Values(SdpSemantics::kPlanB,
3524 SdpSemantics::kUnifiedPlan));
3525
Steve Anton74255ff2018-01-24 18:32:57 -08003526// Tests that verify interoperability between Plan B and Unified Plan
3527// PeerConnections.
3528class PeerConnectionIntegrationInteropTest
Seth Hampson2f0d7022018-02-20 11:54:42 -08003529 : public PeerConnectionIntegrationBaseTest,
Steve Anton74255ff2018-01-24 18:32:57 -08003530 public ::testing::WithParamInterface<
3531 std::tuple<SdpSemantics, SdpSemantics>> {
3532 protected:
Seth Hampson2f0d7022018-02-20 11:54:42 -08003533 // Setting the SdpSemantics for the base test to kDefault does not matter
3534 // because we specify not to use the test semantics when creating
Harald Alvestrand39993842021-02-17 09:05:31 +00003535 // PeerConnectionIntegrationWrappers.
Steve Anton74255ff2018-01-24 18:32:57 -08003536 PeerConnectionIntegrationInteropTest()
Steve Anton3acffc32018-04-12 17:21:03 -07003537 : PeerConnectionIntegrationBaseTest(SdpSemantics::kPlanB),
Seth Hampson2f0d7022018-02-20 11:54:42 -08003538 caller_semantics_(std::get<0>(GetParam())),
Steve Anton74255ff2018-01-24 18:32:57 -08003539 callee_semantics_(std::get<1>(GetParam())) {}
3540
3541 bool CreatePeerConnectionWrappersWithSemantics() {
Steve Anton3acffc32018-04-12 17:21:03 -07003542 return CreatePeerConnectionWrappersWithSdpSemantics(caller_semantics_,
3543 callee_semantics_);
Steve Anton74255ff2018-01-24 18:32:57 -08003544 }
3545
3546 const SdpSemantics caller_semantics_;
3547 const SdpSemantics callee_semantics_;
3548};
3549
3550TEST_P(PeerConnectionIntegrationInteropTest, NoMediaLocalToNoMediaRemote) {
3551 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
3552 ConnectFakeSignaling();
3553
3554 caller()->CreateAndSetAndSignalOffer();
3555 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3556}
3557
3558TEST_P(PeerConnectionIntegrationInteropTest, OneAudioLocalToNoMediaRemote) {
3559 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
3560 ConnectFakeSignaling();
3561 auto audio_sender = caller()->AddAudioTrack();
3562
3563 caller()->CreateAndSetAndSignalOffer();
3564 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3565
3566 // Verify that one audio receiver has been created on the remote and that it
3567 // has the same track ID as the sending track.
3568 auto receivers = callee()->pc()->GetReceivers();
3569 ASSERT_EQ(1u, receivers.size());
3570 EXPECT_EQ(cricket::MEDIA_TYPE_AUDIO, receivers[0]->media_type());
3571 EXPECT_EQ(receivers[0]->track()->id(), audio_sender->track()->id());
3572
Seth Hampson2f0d7022018-02-20 11:54:42 -08003573 MediaExpectations media_expectations;
3574 media_expectations.CalleeExpectsSomeAudio();
3575 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08003576}
3577
3578TEST_P(PeerConnectionIntegrationInteropTest, OneAudioOneVideoToNoMediaRemote) {
3579 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
3580 ConnectFakeSignaling();
3581 auto video_sender = caller()->AddVideoTrack();
3582 auto audio_sender = caller()->AddAudioTrack();
3583
3584 caller()->CreateAndSetAndSignalOffer();
3585 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3586
3587 // Verify that one audio and one video receiver have been created on the
3588 // remote and that they have the same track IDs as the sending tracks.
3589 auto audio_receivers =
3590 callee()->GetReceiversOfType(cricket::MEDIA_TYPE_AUDIO);
3591 ASSERT_EQ(1u, audio_receivers.size());
3592 EXPECT_EQ(audio_receivers[0]->track()->id(), audio_sender->track()->id());
3593 auto video_receivers =
3594 callee()->GetReceiversOfType(cricket::MEDIA_TYPE_VIDEO);
3595 ASSERT_EQ(1u, video_receivers.size());
3596 EXPECT_EQ(video_receivers[0]->track()->id(), video_sender->track()->id());
3597
Seth Hampson2f0d7022018-02-20 11:54:42 -08003598 MediaExpectations media_expectations;
3599 media_expectations.CalleeExpectsSomeAudioAndVideo();
3600 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08003601}
3602
3603TEST_P(PeerConnectionIntegrationInteropTest,
3604 OneAudioOneVideoLocalToOneAudioOneVideoRemote) {
3605 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
3606 ConnectFakeSignaling();
3607 caller()->AddAudioVideoTracks();
3608 callee()->AddAudioVideoTracks();
3609
3610 caller()->CreateAndSetAndSignalOffer();
3611 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3612
Seth Hampson2f0d7022018-02-20 11:54:42 -08003613 MediaExpectations media_expectations;
3614 media_expectations.ExpectBidirectionalAudioAndVideo();
3615 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08003616}
3617
3618TEST_P(PeerConnectionIntegrationInteropTest,
3619 ReverseRolesOneAudioLocalToOneVideoRemote) {
3620 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
3621 ConnectFakeSignaling();
3622 caller()->AddAudioTrack();
3623 callee()->AddVideoTrack();
3624
3625 caller()->CreateAndSetAndSignalOffer();
3626 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3627
3628 // Verify that only the audio track has been negotiated.
3629 EXPECT_EQ(0u, caller()->GetReceiversOfType(cricket::MEDIA_TYPE_VIDEO).size());
3630 // Might also check that the callee's NegotiationNeeded flag is set.
3631
3632 // Reverse roles.
3633 callee()->CreateAndSetAndSignalOffer();
3634 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3635
Seth Hampson2f0d7022018-02-20 11:54:42 -08003636 MediaExpectations media_expectations;
3637 media_expectations.CallerExpectsSomeVideo();
3638 media_expectations.CalleeExpectsSomeAudio();
3639 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08003640}
3641
Harald Alvestrand7a2db8a2021-06-14 15:41:30 +00003642TEST_P(PeerConnectionIntegrationTest, NewTracksDoNotCauseNewCandidates) {
3643 ASSERT_TRUE(CreatePeerConnectionWrappers());
3644 ConnectFakeSignaling();
3645 caller()->AddAudioVideoTracks();
3646 caller()->CreateAndSetAndSignalOffer();
3647 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3648 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
3649 caller()->ExpectCandidates(0);
3650 callee()->ExpectCandidates(0);
3651 caller()->AddAudioTrack();
3652 caller()->CreateAndSetAndSignalOffer();
3653 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3654}
3655
Mirko Bonadeic84f6612019-01-31 12:20:57 +01003656INSTANTIATE_TEST_SUITE_P(
Steve Antonba42e992018-04-09 14:10:01 -07003657 PeerConnectionIntegrationTest,
3658 PeerConnectionIntegrationInteropTest,
3659 Values(std::make_tuple(SdpSemantics::kPlanB, SdpSemantics::kUnifiedPlan),
3660 std::make_tuple(SdpSemantics::kUnifiedPlan, SdpSemantics::kPlanB)));
3661
3662// Test that if the Unified Plan side offers two video tracks then the Plan B
3663// side will only see the first one and ignore the second.
3664TEST_F(PeerConnectionIntegrationTestPlanB, TwoVideoUnifiedPlanToNoMediaPlanB) {
Steve Anton3acffc32018-04-12 17:21:03 -07003665 ASSERT_TRUE(CreatePeerConnectionWrappersWithSdpSemantics(
3666 SdpSemantics::kUnifiedPlan, SdpSemantics::kPlanB));
Steve Anton74255ff2018-01-24 18:32:57 -08003667 ConnectFakeSignaling();
3668 auto first_sender = caller()->AddVideoTrack();
3669 caller()->AddVideoTrack();
3670
3671 caller()->CreateAndSetAndSignalOffer();
3672 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3673
3674 // Verify that there is only one receiver and it corresponds to the first
3675 // added track.
3676 auto receivers = callee()->pc()->GetReceivers();
3677 ASSERT_EQ(1u, receivers.size());
3678 EXPECT_TRUE(receivers[0]->track()->enabled());
3679 EXPECT_EQ(first_sender->track()->id(), receivers[0]->track()->id());
3680
Seth Hampson2f0d7022018-02-20 11:54:42 -08003681 MediaExpectations media_expectations;
3682 media_expectations.CalleeExpectsSomeVideo();
3683 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08003684}
3685
Steve Anton2bed3972019-01-04 17:04:30 -08003686// Test that if the initial offer tagged BUNDLE section is rejected due to its
3687// associated RtpTransceiver being stopped and another transceiver is added,
3688// then renegotiation causes the callee to receive the new video track without
3689// error.
3690// This is a regression test for bugs.webrtc.org/9954
3691TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
3692 ReOfferWithStoppedBundleTaggedTransceiver) {
3693 RTCConfiguration config;
3694 config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
3695 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
3696 ConnectFakeSignaling();
3697 auto audio_transceiver_or_error =
3698 caller()->pc()->AddTransceiver(caller()->CreateLocalAudioTrack());
3699 ASSERT_TRUE(audio_transceiver_or_error.ok());
3700 auto audio_transceiver = audio_transceiver_or_error.MoveValue();
3701
3702 caller()->CreateAndSetAndSignalOffer();
3703 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3704 {
3705 MediaExpectations media_expectations;
3706 media_expectations.CalleeExpectsSomeAudio();
3707 ASSERT_TRUE(ExpectNewFrames(media_expectations));
3708 }
3709
Harald Alvestrand6060df52020-08-11 09:54:02 +02003710 audio_transceiver->StopInternal();
Steve Anton2bed3972019-01-04 17:04:30 -08003711 caller()->pc()->AddTransceiver(caller()->CreateLocalVideoTrack());
3712
3713 caller()->CreateAndSetAndSignalOffer();
3714 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3715 {
3716 MediaExpectations media_expectations;
3717 media_expectations.CalleeExpectsSomeVideo();
3718 ASSERT_TRUE(ExpectNewFrames(media_expectations));
3719 }
3720}
3721
Harald Alvestrandbedb6052020-08-20 14:50:10 +02003722TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
3723 StopTransceiverRemovesDtlsTransports) {
3724 RTCConfiguration config;
3725 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
3726 ConnectFakeSignaling();
3727 auto audio_transceiver_or_error =
3728 caller()->pc()->AddTransceiver(caller()->CreateLocalAudioTrack());
3729 ASSERT_TRUE(audio_transceiver_or_error.ok());
3730 auto audio_transceiver = audio_transceiver_or_error.MoveValue();
3731
3732 caller()->CreateAndSetAndSignalOffer();
3733 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3734
3735 audio_transceiver->StopStandard();
3736 caller()->CreateAndSetAndSignalOffer();
3737 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3738 ASSERT_EQ(0U, caller()->pc()->GetTransceivers().size());
3739 EXPECT_EQ(PeerConnectionInterface::kIceGatheringNew,
3740 caller()->pc()->ice_gathering_state());
3741 EXPECT_THAT(caller()->ice_gathering_state_history(),
3742 ElementsAre(PeerConnectionInterface::kIceGatheringGathering,
3743 PeerConnectionInterface::kIceGatheringComplete,
3744 PeerConnectionInterface::kIceGatheringNew));
3745}
3746
Harald Alvestrand1ee33252020-09-24 13:31:15 +00003747TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
Harald Alvestrand45be0a92020-09-30 06:55:23 +00003748 StopTransceiverStopsAndRemovesTransceivers) {
3749 RTCConfiguration config;
3750 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
3751 ConnectFakeSignaling();
3752 auto audio_transceiver_or_error =
3753 caller()->pc()->AddTransceiver(caller()->CreateLocalAudioTrack());
3754 ASSERT_TRUE(audio_transceiver_or_error.ok());
3755 auto caller_transceiver = audio_transceiver_or_error.MoveValue();
3756
3757 caller()->CreateAndSetAndSignalOffer();
3758 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3759 caller_transceiver->StopStandard();
3760
3761 auto callee_transceiver = callee()->pc()->GetTransceivers()[0];
3762 caller()->CreateAndSetAndSignalOffer();
3763 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3764 EXPECT_EQ(0U, caller()->pc()->GetTransceivers().size());
3765 EXPECT_EQ(0U, callee()->pc()->GetTransceivers().size());
3766 EXPECT_EQ(0U, caller()->pc()->GetSenders().size());
3767 EXPECT_EQ(0U, callee()->pc()->GetSenders().size());
3768 EXPECT_EQ(0U, caller()->pc()->GetReceivers().size());
3769 EXPECT_EQ(0U, callee()->pc()->GetReceivers().size());
3770 EXPECT_TRUE(caller_transceiver->stopped());
3771 EXPECT_TRUE(callee_transceiver->stopped());
3772}
3773
3774TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
Harald Alvestrand1ee33252020-09-24 13:31:15 +00003775 StopTransceiverEndsIncomingAudioTrack) {
3776 RTCConfiguration config;
3777 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
3778 ConnectFakeSignaling();
3779 auto audio_transceiver_or_error =
3780 caller()->pc()->AddTransceiver(caller()->CreateLocalAudioTrack());
3781 ASSERT_TRUE(audio_transceiver_or_error.ok());
3782 auto audio_transceiver = audio_transceiver_or_error.MoveValue();
3783
3784 caller()->CreateAndSetAndSignalOffer();
3785 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3786 auto caller_track = audio_transceiver->receiver()->track();
3787 auto callee_track = callee()->pc()->GetReceivers()[0]->track();
3788 audio_transceiver->StopStandard();
3789 EXPECT_EQ(MediaStreamTrackInterface::TrackState::kEnded,
3790 caller_track->state());
3791 caller()->CreateAndSetAndSignalOffer();
3792 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3793 EXPECT_EQ(MediaStreamTrackInterface::TrackState::kEnded,
3794 callee_track->state());
3795}
3796
3797TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
3798 StopTransceiverEndsIncomingVideoTrack) {
3799 RTCConfiguration config;
3800 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
3801 ConnectFakeSignaling();
3802 auto audio_transceiver_or_error =
3803 caller()->pc()->AddTransceiver(caller()->CreateLocalVideoTrack());
3804 ASSERT_TRUE(audio_transceiver_or_error.ok());
3805 auto audio_transceiver = audio_transceiver_or_error.MoveValue();
3806
3807 caller()->CreateAndSetAndSignalOffer();
3808 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3809 auto caller_track = audio_transceiver->receiver()->track();
3810 auto callee_track = callee()->pc()->GetReceivers()[0]->track();
3811 audio_transceiver->StopStandard();
3812 EXPECT_EQ(MediaStreamTrackInterface::TrackState::kEnded,
3813 caller_track->state());
3814 caller()->CreateAndSetAndSignalOffer();
3815 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3816 EXPECT_EQ(MediaStreamTrackInterface::TrackState::kEnded,
3817 callee_track->state());
3818}
3819
Harald Alvestrand89c40e22021-02-17 08:58:35 +00003820} // namespace
Harald Alvestrand39993842021-02-17 09:05:31 +00003821
Mirko Bonadeiab64e8a2018-12-12 12:10:18 +01003822} // namespace webrtc