blob: faf6053adb22449ffbebac8fdd3384c5f9ed74f1 [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;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07001765 int expected_cipher_suite = rtc::SRTP_AES128_CM_SHA1_80;
1766 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;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07001777 int expected_cipher_suite = rtc::SRTP_AES128_CM_SHA1_80;
1778 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;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07001787 int expected_cipher_suite = rtc::SRTP_AES128_CM_SHA1_32;
1788 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.
Zach Stein6fcdc2f2018-08-23 16:25:55 -07001869TEST_P(PeerConnectionIntegrationTest,
1870 IceStatesReachCompletionWithRemoteHostname) {
Qingsi Wang1dac6d82018-12-12 15:28:47 -08001871 auto caller_resolver_factory =
Mirko Bonadei317a1f02019-09-17 17:06:18 +02001872 std::make_unique<NiceMock<webrtc::MockAsyncResolverFactory>>();
Qingsi Wang1dac6d82018-12-12 15:28:47 -08001873 auto callee_resolver_factory =
Mirko Bonadei317a1f02019-09-17 17:06:18 +02001874 std::make_unique<NiceMock<webrtc::MockAsyncResolverFactory>>();
Qingsi Wang1dac6d82018-12-12 15:28:47 -08001875 NiceMock<rtc::MockAsyncResolver> callee_async_resolver;
1876 NiceMock<rtc::MockAsyncResolver> caller_async_resolver;
Zach Stein6fcdc2f2018-08-23 16:25:55 -07001877
1878 // This also verifies that the injected AsyncResolverFactory is used by
1879 // P2PTransportChannel.
Qingsi Wang1dac6d82018-12-12 15:28:47 -08001880 EXPECT_CALL(*caller_resolver_factory, Create())
1881 .WillOnce(Return(&caller_async_resolver));
1882 webrtc::PeerConnectionDependencies caller_deps(nullptr);
1883 caller_deps.async_resolver_factory = std::move(caller_resolver_factory);
1884
1885 EXPECT_CALL(*callee_resolver_factory, Create())
1886 .WillOnce(Return(&callee_async_resolver));
1887 webrtc::PeerConnectionDependencies callee_deps(nullptr);
1888 callee_deps.async_resolver_factory = std::move(callee_resolver_factory);
1889
1890 PeerConnectionInterface::RTCConfiguration config;
1891 config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
1892 config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyRequire;
1893
1894 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndDeps(
1895 config, std::move(caller_deps), config, std::move(callee_deps)));
1896
1897 caller()->SetRemoteAsyncResolver(&callee_async_resolver);
1898 callee()->SetRemoteAsyncResolver(&caller_async_resolver);
1899
1900 // Enable hostname candidates with mDNS names.
Qingsi Wangecd30542019-05-22 14:34:56 -07001901 caller()->SetMdnsResponder(
Mirko Bonadei317a1f02019-09-17 17:06:18 +02001902 std::make_unique<webrtc::FakeMdnsResponder>(network_thread()));
Qingsi Wangecd30542019-05-22 14:34:56 -07001903 callee()->SetMdnsResponder(
Mirko Bonadei317a1f02019-09-17 17:06:18 +02001904 std::make_unique<webrtc::FakeMdnsResponder>(network_thread()));
Qingsi Wang1dac6d82018-12-12 15:28:47 -08001905
1906 SetPortAllocatorFlags(kOnlyLocalPorts, kOnlyLocalPorts);
Zach Stein6fcdc2f2018-08-23 16:25:55 -07001907
1908 ConnectFakeSignaling();
1909 caller()->AddAudioVideoTracks();
1910 callee()->AddAudioVideoTracks();
1911 caller()->CreateAndSetAndSignalOffer();
1912 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1913 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
1914 caller()->ice_connection_state(), kDefaultTimeout);
1915 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
1916 callee()->ice_connection_state(), kDefaultTimeout);
Jeroen de Borst833979f2018-12-13 08:25:54 -08001917
Ying Wangef3998f2019-12-09 13:06:53 +01001918 EXPECT_METRIC_EQ(1, webrtc::metrics::NumEvents(
1919 "WebRTC.PeerConnection.CandidatePairType_UDP",
1920 webrtc::kIceCandidatePairHostNameHostName));
Zach Stein6fcdc2f2018-08-23 16:25:55 -07001921}
1922
Harald Alvestrandec23d6d2021-02-11 10:47:22 +00001923#endif // !defined(THREAD_SANITIZER)
1924
Steve Antonede9ca52017-10-16 13:04:27 -07001925// Test that firewalling the ICE connection causes the clients to identify the
1926// disconnected state and then removing the firewall causes them to reconnect.
1927class PeerConnectionIntegrationIceStatesTest
Seth Hampson2f0d7022018-02-20 11:54:42 -08001928 : public PeerConnectionIntegrationBaseTest,
1929 public ::testing::WithParamInterface<
1930 std::tuple<SdpSemantics, std::tuple<std::string, uint32_t>>> {
Steve Antonede9ca52017-10-16 13:04:27 -07001931 protected:
Seth Hampson2f0d7022018-02-20 11:54:42 -08001932 PeerConnectionIntegrationIceStatesTest()
1933 : PeerConnectionIntegrationBaseTest(std::get<0>(GetParam())) {
1934 port_allocator_flags_ = std::get<1>(std::get<1>(GetParam()));
Steve Antonede9ca52017-10-16 13:04:27 -07001935 }
1936
1937 void StartStunServer(const SocketAddress& server_address) {
1938 stun_server_.reset(
Niels Möller091617d2020-12-02 15:32:08 +01001939 cricket::TestStunServer::Create(firewall(), server_address));
Steve Antonede9ca52017-10-16 13:04:27 -07001940 }
1941
1942 bool TestIPv6() {
1943 return (port_allocator_flags_ & cricket::PORTALLOCATOR_ENABLE_IPV6);
1944 }
1945
1946 void SetPortAllocatorFlags() {
Qingsi Wang1dac6d82018-12-12 15:28:47 -08001947 PeerConnectionIntegrationBaseTest::SetPortAllocatorFlags(
1948 port_allocator_flags_, port_allocator_flags_);
Steve Antonede9ca52017-10-16 13:04:27 -07001949 }
1950
1951 std::vector<SocketAddress> CallerAddresses() {
1952 std::vector<SocketAddress> addresses;
1953 addresses.push_back(SocketAddress("1.1.1.1", 0));
1954 if (TestIPv6()) {
1955 addresses.push_back(SocketAddress("1111:0:a:b:c:d:e:f", 0));
1956 }
1957 return addresses;
1958 }
1959
1960 std::vector<SocketAddress> CalleeAddresses() {
1961 std::vector<SocketAddress> addresses;
1962 addresses.push_back(SocketAddress("2.2.2.2", 0));
1963 if (TestIPv6()) {
1964 addresses.push_back(SocketAddress("2222:0:a:b:c:d:e:f", 0));
1965 }
1966 return addresses;
1967 }
1968
1969 void SetUpNetworkInterfaces() {
1970 // Remove the default interfaces added by the test infrastructure.
Qingsi Wangecd30542019-05-22 14:34:56 -07001971 caller()->network_manager()->RemoveInterface(kDefaultLocalAddress);
1972 callee()->network_manager()->RemoveInterface(kDefaultLocalAddress);
Steve Antonede9ca52017-10-16 13:04:27 -07001973
1974 // Add network addresses for test.
1975 for (const auto& caller_address : CallerAddresses()) {
Qingsi Wangecd30542019-05-22 14:34:56 -07001976 caller()->network_manager()->AddInterface(caller_address);
Steve Antonede9ca52017-10-16 13:04:27 -07001977 }
1978 for (const auto& callee_address : CalleeAddresses()) {
Qingsi Wangecd30542019-05-22 14:34:56 -07001979 callee()->network_manager()->AddInterface(callee_address);
Steve Antonede9ca52017-10-16 13:04:27 -07001980 }
1981 }
1982
1983 private:
1984 uint32_t port_allocator_flags_;
1985 std::unique_ptr<cricket::TestStunServer> stun_server_;
1986};
1987
Yves Gerey100fe632020-01-17 19:15:53 +01001988// Ensure FakeClockForTest is constructed first (see class for rationale).
1989class PeerConnectionIntegrationIceStatesTestWithFakeClock
1990 : public FakeClockForTest,
1991 public PeerConnectionIntegrationIceStatesTest {};
1992
Harald Alvestrandec23d6d2021-02-11 10:47:22 +00001993#if !defined(THREAD_SANITIZER)
1994// This test provokes TSAN errors. bugs.webrtc.org/11282
1995
Steve Antonede9ca52017-10-16 13:04:27 -07001996// Tests that the PeerConnection goes through all the ICE gathering/connection
1997// states over the duration of the call. This includes Disconnected and Failed
1998// states, induced by putting a firewall between the peers and waiting for them
1999// to time out.
Yves Gerey100fe632020-01-17 19:15:53 +01002000TEST_P(PeerConnectionIntegrationIceStatesTestWithFakeClock, VerifyIceStates) {
Steve Antonede9ca52017-10-16 13:04:27 -07002001 const SocketAddress kStunServerAddress =
2002 SocketAddress("99.99.99.1", cricket::STUN_SERVER_PORT);
2003 StartStunServer(kStunServerAddress);
2004
2005 PeerConnectionInterface::RTCConfiguration config;
2006 PeerConnectionInterface::IceServer ice_stun_server;
2007 ice_stun_server.urls.push_back(
2008 "stun:" + kStunServerAddress.HostAsURIString() + ":" +
2009 kStunServerAddress.PortAsString());
2010 config.servers.push_back(ice_stun_server);
2011
2012 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
2013 ConnectFakeSignaling();
2014 SetPortAllocatorFlags();
2015 SetUpNetworkInterfaces();
Steve Anton15324772018-01-16 10:26:49 -08002016 caller()->AddAudioVideoTracks();
2017 callee()->AddAudioVideoTracks();
Steve Antonede9ca52017-10-16 13:04:27 -07002018
2019 // Initial state before anything happens.
2020 ASSERT_EQ(PeerConnectionInterface::kIceGatheringNew,
2021 caller()->ice_gathering_state());
2022 ASSERT_EQ(PeerConnectionInterface::kIceConnectionNew,
2023 caller()->ice_connection_state());
Jonas Olsson7a6739e2019-01-15 16:31:55 +01002024 ASSERT_EQ(PeerConnectionInterface::kIceConnectionNew,
2025 caller()->standardized_ice_connection_state());
Steve Antonede9ca52017-10-16 13:04:27 -07002026
2027 // Start the call by creating the offer, setting it as the local description,
2028 // then sending it to the peer who will respond with an answer. This happens
2029 // asynchronously so that we can watch the states as it runs in the
2030 // background.
2031 caller()->CreateAndSetAndSignalOffer();
2032
Steve Antona9b67ce2020-01-16 14:00:44 -08002033 ASSERT_EQ_SIMULATED_WAIT(PeerConnectionInterface::kIceConnectionCompleted,
2034 caller()->ice_connection_state(), kDefaultTimeout,
Yves Gerey100fe632020-01-17 19:15:53 +01002035 FakeClock());
Steve Antona9b67ce2020-01-16 14:00:44 -08002036 ASSERT_EQ_SIMULATED_WAIT(PeerConnectionInterface::kIceConnectionCompleted,
2037 caller()->standardized_ice_connection_state(),
Yves Gerey100fe632020-01-17 19:15:53 +01002038 kDefaultTimeout, FakeClock());
Steve Antonede9ca52017-10-16 13:04:27 -07002039
2040 // Verify that the observer was notified of the intermediate transitions.
2041 EXPECT_THAT(caller()->ice_connection_state_history(),
2042 ElementsAre(PeerConnectionInterface::kIceConnectionChecking,
2043 PeerConnectionInterface::kIceConnectionConnected,
2044 PeerConnectionInterface::kIceConnectionCompleted));
Jonas Olssonacd8ae72019-02-25 15:26:24 +01002045 EXPECT_THAT(caller()->standardized_ice_connection_state_history(),
2046 ElementsAre(PeerConnectionInterface::kIceConnectionChecking,
2047 PeerConnectionInterface::kIceConnectionConnected,
2048 PeerConnectionInterface::kIceConnectionCompleted));
Jonas Olsson635474e2018-10-18 15:58:17 +02002049 EXPECT_THAT(
2050 caller()->peer_connection_state_history(),
2051 ElementsAre(PeerConnectionInterface::PeerConnectionState::kConnecting,
Jonas Olsson635474e2018-10-18 15:58:17 +02002052 PeerConnectionInterface::PeerConnectionState::kConnected));
Steve Antonede9ca52017-10-16 13:04:27 -07002053 EXPECT_THAT(caller()->ice_gathering_state_history(),
2054 ElementsAre(PeerConnectionInterface::kIceGatheringGathering,
2055 PeerConnectionInterface::kIceGatheringComplete));
2056
2057 // Block connections to/from the caller and wait for ICE to become
2058 // disconnected.
2059 for (const auto& caller_address : CallerAddresses()) {
2060 firewall()->AddRule(false, rtc::FP_ANY, rtc::FD_ANY, caller_address);
2061 }
Mirko Bonadei675513b2017-11-09 11:09:25 +01002062 RTC_LOG(LS_INFO) << "Firewall rules applied";
Jonas Olssonb75d9e92019-02-22 10:33:29 +01002063 ASSERT_EQ_SIMULATED_WAIT(PeerConnectionInterface::kIceConnectionDisconnected,
2064 caller()->ice_connection_state(), kDefaultTimeout,
Yves Gerey100fe632020-01-17 19:15:53 +01002065 FakeClock());
Jonas Olssonb75d9e92019-02-22 10:33:29 +01002066 ASSERT_EQ_SIMULATED_WAIT(PeerConnectionInterface::kIceConnectionDisconnected,
2067 caller()->standardized_ice_connection_state(),
Yves Gerey100fe632020-01-17 19:15:53 +01002068 kDefaultTimeout, FakeClock());
Steve Antonede9ca52017-10-16 13:04:27 -07002069
2070 // Let ICE re-establish by removing the firewall rules.
2071 firewall()->ClearRules();
Mirko Bonadei675513b2017-11-09 11:09:25 +01002072 RTC_LOG(LS_INFO) << "Firewall rules cleared";
Jonas Olssonb75d9e92019-02-22 10:33:29 +01002073 ASSERT_EQ_SIMULATED_WAIT(PeerConnectionInterface::kIceConnectionCompleted,
2074 caller()->ice_connection_state(), kDefaultTimeout,
Yves Gerey100fe632020-01-17 19:15:53 +01002075 FakeClock());
Jonas Olssonacd8ae72019-02-25 15:26:24 +01002076 ASSERT_EQ_SIMULATED_WAIT(PeerConnectionInterface::kIceConnectionCompleted,
Jonas Olssonb75d9e92019-02-22 10:33:29 +01002077 caller()->standardized_ice_connection_state(),
Yves Gerey100fe632020-01-17 19:15:53 +01002078 kDefaultTimeout, FakeClock());
Steve Antonede9ca52017-10-16 13:04:27 -07002079
2080 // According to RFC7675, if there is no response within 30 seconds then the
2081 // peer should consider the other side to have rejected the connection. This
Steve Anton83119dd2017-11-10 16:19:52 -08002082 // is signaled by the state transitioning to "failed".
Steve Antonede9ca52017-10-16 13:04:27 -07002083 constexpr int kConsentTimeout = 30000;
2084 for (const auto& caller_address : CallerAddresses()) {
2085 firewall()->AddRule(false, rtc::FP_ANY, rtc::FD_ANY, caller_address);
2086 }
Mirko Bonadei675513b2017-11-09 11:09:25 +01002087 RTC_LOG(LS_INFO) << "Firewall rules applied again";
Jonas Olssonb75d9e92019-02-22 10:33:29 +01002088 ASSERT_EQ_SIMULATED_WAIT(PeerConnectionInterface::kIceConnectionFailed,
2089 caller()->ice_connection_state(), kConsentTimeout,
Yves Gerey100fe632020-01-17 19:15:53 +01002090 FakeClock());
Jonas Olssonb75d9e92019-02-22 10:33:29 +01002091 ASSERT_EQ_SIMULATED_WAIT(PeerConnectionInterface::kIceConnectionFailed,
2092 caller()->standardized_ice_connection_state(),
Yves Gerey100fe632020-01-17 19:15:53 +01002093 kConsentTimeout, FakeClock());
Jonas Olssonb75d9e92019-02-22 10:33:29 +01002094}
2095
2096// Tests that if the connection doesn't get set up properly we eventually reach
2097// the "failed" iceConnectionState.
Yves Gerey100fe632020-01-17 19:15:53 +01002098TEST_P(PeerConnectionIntegrationIceStatesTestWithFakeClock,
2099 IceStateSetupFailure) {
Jonas Olssonb75d9e92019-02-22 10:33:29 +01002100 // Block connections to/from the caller and wait for ICE to become
2101 // disconnected.
2102 for (const auto& caller_address : CallerAddresses()) {
2103 firewall()->AddRule(false, rtc::FP_ANY, rtc::FD_ANY, caller_address);
2104 }
2105
2106 ASSERT_TRUE(CreatePeerConnectionWrappers());
2107 ConnectFakeSignaling();
2108 SetPortAllocatorFlags();
2109 SetUpNetworkInterfaces();
2110 caller()->AddAudioVideoTracks();
2111 caller()->CreateAndSetAndSignalOffer();
2112
2113 // According to RFC7675, if there is no response within 30 seconds then the
2114 // peer should consider the other side to have rejected the connection. This
2115 // is signaled by the state transitioning to "failed".
2116 constexpr int kConsentTimeout = 30000;
2117 ASSERT_EQ_SIMULATED_WAIT(PeerConnectionInterface::kIceConnectionFailed,
2118 caller()->standardized_ice_connection_state(),
Yves Gerey100fe632020-01-17 19:15:53 +01002119 kConsentTimeout, FakeClock());
Steve Antonede9ca52017-10-16 13:04:27 -07002120}
2121
Harald Alvestrandec23d6d2021-02-11 10:47:22 +00002122#endif // !defined(THREAD_SANITIZER)
2123
Steve Antonede9ca52017-10-16 13:04:27 -07002124// Tests that the best connection is set to the appropriate IPv4/IPv6 connection
2125// and that the statistics in the metric observers are updated correctly.
2126TEST_P(PeerConnectionIntegrationIceStatesTest, VerifyBestConnection) {
2127 ASSERT_TRUE(CreatePeerConnectionWrappers());
2128 ConnectFakeSignaling();
2129 SetPortAllocatorFlags();
2130 SetUpNetworkInterfaces();
Steve Anton15324772018-01-16 10:26:49 -08002131 caller()->AddAudioVideoTracks();
2132 callee()->AddAudioVideoTracks();
Steve Antonede9ca52017-10-16 13:04:27 -07002133 caller()->CreateAndSetAndSignalOffer();
2134
2135 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Steve Anton692f3c72020-01-16 14:12:31 -08002136 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
2137 caller()->ice_connection_state(), kDefaultTimeout);
2138 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
2139 callee()->ice_connection_state(), kDefaultTimeout);
Steve Antonede9ca52017-10-16 13:04:27 -07002140
Qingsi Wang7fc821d2018-07-12 12:54:53 -07002141 // TODO(bugs.webrtc.org/9456): Fix it.
2142 const int num_best_ipv4 = webrtc::metrics::NumEvents(
2143 "WebRTC.PeerConnection.IPMetrics", webrtc::kBestConnections_IPv4);
2144 const int num_best_ipv6 = webrtc::metrics::NumEvents(
2145 "WebRTC.PeerConnection.IPMetrics", webrtc::kBestConnections_IPv6);
Steve Antonede9ca52017-10-16 13:04:27 -07002146 if (TestIPv6()) {
2147 // When IPv6 is enabled, we should prefer an IPv6 connection over an IPv4
2148 // connection.
Ying Wangef3998f2019-12-09 13:06:53 +01002149 EXPECT_METRIC_EQ(0, num_best_ipv4);
2150 EXPECT_METRIC_EQ(1, num_best_ipv6);
Steve Antonede9ca52017-10-16 13:04:27 -07002151 } else {
Ying Wangef3998f2019-12-09 13:06:53 +01002152 EXPECT_METRIC_EQ(1, num_best_ipv4);
2153 EXPECT_METRIC_EQ(0, num_best_ipv6);
Steve Antonede9ca52017-10-16 13:04:27 -07002154 }
2155
Ying Wangef3998f2019-12-09 13:06:53 +01002156 EXPECT_METRIC_EQ(0, webrtc::metrics::NumEvents(
2157 "WebRTC.PeerConnection.CandidatePairType_UDP",
2158 webrtc::kIceCandidatePairHostHost));
2159 EXPECT_METRIC_EQ(1, webrtc::metrics::NumEvents(
2160 "WebRTC.PeerConnection.CandidatePairType_UDP",
2161 webrtc::kIceCandidatePairHostPublicHostPublic));
Steve Antonede9ca52017-10-16 13:04:27 -07002162}
2163
2164constexpr uint32_t kFlagsIPv4NoStun = cricket::PORTALLOCATOR_DISABLE_TCP |
2165 cricket::PORTALLOCATOR_DISABLE_STUN |
2166 cricket::PORTALLOCATOR_DISABLE_RELAY;
2167constexpr uint32_t kFlagsIPv6NoStun =
2168 cricket::PORTALLOCATOR_DISABLE_TCP | cricket::PORTALLOCATOR_DISABLE_STUN |
2169 cricket::PORTALLOCATOR_ENABLE_IPV6 | cricket::PORTALLOCATOR_DISABLE_RELAY;
2170constexpr uint32_t kFlagsIPv4Stun =
2171 cricket::PORTALLOCATOR_DISABLE_TCP | cricket::PORTALLOCATOR_DISABLE_RELAY;
2172
Mirko Bonadeic84f6612019-01-31 12:20:57 +01002173INSTANTIATE_TEST_SUITE_P(
Seth Hampson2f0d7022018-02-20 11:54:42 -08002174 PeerConnectionIntegrationTest,
2175 PeerConnectionIntegrationIceStatesTest,
2176 Combine(Values(SdpSemantics::kPlanB, SdpSemantics::kUnifiedPlan),
2177 Values(std::make_pair("IPv4 no STUN", kFlagsIPv4NoStun),
2178 std::make_pair("IPv6 no STUN", kFlagsIPv6NoStun),
2179 std::make_pair("IPv4 with STUN", kFlagsIPv4Stun))));
Steve Antonede9ca52017-10-16 13:04:27 -07002180
Yves Gerey100fe632020-01-17 19:15:53 +01002181INSTANTIATE_TEST_SUITE_P(
2182 PeerConnectionIntegrationTest,
2183 PeerConnectionIntegrationIceStatesTestWithFakeClock,
2184 Combine(Values(SdpSemantics::kPlanB, SdpSemantics::kUnifiedPlan),
2185 Values(std::make_pair("IPv4 no STUN", kFlagsIPv4NoStun),
2186 std::make_pair("IPv6 no STUN", kFlagsIPv6NoStun),
2187 std::make_pair("IPv4 with STUN", kFlagsIPv4Stun))));
2188
deadbeef1dcb1642017-03-29 21:08:16 -07002189// This test sets up a call between two parties with audio and video.
2190// During the call, the caller restarts ICE and the test verifies that
2191// new ICE candidates are generated and audio and video still can flow, and the
2192// ICE state reaches completed again.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002193TEST_P(PeerConnectionIntegrationTest, MediaContinuesFlowingAfterIceRestart) {
deadbeef1dcb1642017-03-29 21:08:16 -07002194 ASSERT_TRUE(CreatePeerConnectionWrappers());
2195 ConnectFakeSignaling();
2196 // Do normal offer/answer and wait for ICE to complete.
Steve Anton15324772018-01-16 10:26:49 -08002197 caller()->AddAudioVideoTracks();
2198 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002199 caller()->CreateAndSetAndSignalOffer();
2200 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2201 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
2202 caller()->ice_connection_state(), kMaxWaitForFramesMs);
Alex Loiko9289eda2018-11-23 16:18:59 +00002203 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
2204 callee()->ice_connection_state(), kMaxWaitForFramesMs);
deadbeef1dcb1642017-03-29 21:08:16 -07002205
2206 // To verify that the ICE restart actually occurs, get
2207 // ufrag/password/candidates before and after restart.
2208 // Create an SDP string of the first audio candidate for both clients.
2209 const webrtc::IceCandidateCollection* audio_candidates_caller =
2210 caller()->pc()->local_description()->candidates(0);
2211 const webrtc::IceCandidateCollection* audio_candidates_callee =
2212 callee()->pc()->local_description()->candidates(0);
2213 ASSERT_GT(audio_candidates_caller->count(), 0u);
2214 ASSERT_GT(audio_candidates_callee->count(), 0u);
2215 std::string caller_candidate_pre_restart;
2216 ASSERT_TRUE(
2217 audio_candidates_caller->at(0)->ToString(&caller_candidate_pre_restart));
2218 std::string callee_candidate_pre_restart;
2219 ASSERT_TRUE(
2220 audio_candidates_callee->at(0)->ToString(&callee_candidate_pre_restart));
2221 const cricket::SessionDescription* desc =
2222 caller()->pc()->local_description()->description();
2223 std::string caller_ufrag_pre_restart =
2224 desc->transport_infos()[0].description.ice_ufrag;
2225 desc = callee()->pc()->local_description()->description();
2226 std::string callee_ufrag_pre_restart =
2227 desc->transport_infos()[0].description.ice_ufrag;
2228
Alex Drake00c7ecf2019-08-06 10:54:47 -07002229 EXPECT_EQ(caller()->ice_candidate_pair_change_history().size(), 1u);
deadbeef1dcb1642017-03-29 21:08:16 -07002230 // Have the caller initiate an ICE restart.
2231 caller()->SetOfferAnswerOptions(IceRestartOfferAnswerOptions());
2232 caller()->CreateAndSetAndSignalOffer();
2233 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2234 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
2235 caller()->ice_connection_state(), kMaxWaitForFramesMs);
Alex Loiko9289eda2018-11-23 16:18:59 +00002236 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
deadbeef1dcb1642017-03-29 21:08:16 -07002237 callee()->ice_connection_state(), kMaxWaitForFramesMs);
2238
2239 // Grab the ufrags/candidates again.
2240 audio_candidates_caller = caller()->pc()->local_description()->candidates(0);
2241 audio_candidates_callee = callee()->pc()->local_description()->candidates(0);
2242 ASSERT_GT(audio_candidates_caller->count(), 0u);
2243 ASSERT_GT(audio_candidates_callee->count(), 0u);
2244 std::string caller_candidate_post_restart;
2245 ASSERT_TRUE(
2246 audio_candidates_caller->at(0)->ToString(&caller_candidate_post_restart));
2247 std::string callee_candidate_post_restart;
2248 ASSERT_TRUE(
2249 audio_candidates_callee->at(0)->ToString(&callee_candidate_post_restart));
2250 desc = caller()->pc()->local_description()->description();
2251 std::string caller_ufrag_post_restart =
2252 desc->transport_infos()[0].description.ice_ufrag;
2253 desc = callee()->pc()->local_description()->description();
2254 std::string callee_ufrag_post_restart =
2255 desc->transport_infos()[0].description.ice_ufrag;
2256 // Sanity check that an ICE restart was actually negotiated in SDP.
2257 ASSERT_NE(caller_candidate_pre_restart, caller_candidate_post_restart);
2258 ASSERT_NE(callee_candidate_pre_restart, callee_candidate_post_restart);
2259 ASSERT_NE(caller_ufrag_pre_restart, caller_ufrag_post_restart);
2260 ASSERT_NE(callee_ufrag_pre_restart, callee_ufrag_post_restart);
Alex Drake00c7ecf2019-08-06 10:54:47 -07002261 EXPECT_GT(caller()->ice_candidate_pair_change_history().size(), 1u);
deadbeef1dcb1642017-03-29 21:08:16 -07002262
2263 // Ensure that additional frames are received after the ICE restart.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002264 MediaExpectations media_expectations;
2265 media_expectations.ExpectBidirectionalAudioAndVideo();
2266 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002267}
2268
2269// Verify that audio/video can be received end-to-end when ICE renomination is
2270// enabled.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002271TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithIceRenomination) {
deadbeef1dcb1642017-03-29 21:08:16 -07002272 PeerConnectionInterface::RTCConfiguration config;
2273 config.enable_ice_renomination = true;
2274 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
2275 ConnectFakeSignaling();
2276 // Do normal offer/answer and wait for some frames to be received in each
2277 // direction.
Steve Anton15324772018-01-16 10:26:49 -08002278 caller()->AddAudioVideoTracks();
2279 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002280 caller()->CreateAndSetAndSignalOffer();
2281 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2282 // Sanity check that ICE renomination was actually negotiated.
2283 const cricket::SessionDescription* desc =
2284 caller()->pc()->local_description()->description();
2285 for (const cricket::TransportInfo& info : desc->transport_infos()) {
Steve Anton64b626b2019-01-28 17:25:26 -08002286 ASSERT_THAT(info.description.transport_options, Contains("renomination"));
deadbeef1dcb1642017-03-29 21:08:16 -07002287 }
2288 desc = callee()->pc()->local_description()->description();
2289 for (const cricket::TransportInfo& info : desc->transport_infos()) {
Steve Anton64b626b2019-01-28 17:25:26 -08002290 ASSERT_THAT(info.description.transport_options, Contains("renomination"));
deadbeef1dcb1642017-03-29 21:08:16 -07002291 }
Seth Hampson2f0d7022018-02-20 11:54:42 -08002292 MediaExpectations media_expectations;
2293 media_expectations.ExpectBidirectionalAudioAndVideo();
2294 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002295}
2296
Steve Anton6f25b092017-10-23 09:39:20 -07002297// With a max bundle policy and RTCP muxing, adding a new media description to
2298// the connection should not affect ICE at all because the new media will use
2299// the existing connection.
Rasmus Brandt685be142021-03-15 14:03:38 +01002300// TODO(bugs.webrtc.org/12538): Fails on tsan.
2301#if defined(THREAD_SANITIZER)
2302#define MAYBE_AddMediaToConnectedBundleDoesNotRestartIce \
2303 DISABLED_AddMediaToConnectedBundleDoesNotRestartIce
2304#else
2305#define MAYBE_AddMediaToConnectedBundleDoesNotRestartIce \
2306 AddMediaToConnectedBundleDoesNotRestartIce
2307#endif
Seth Hampson2f0d7022018-02-20 11:54:42 -08002308TEST_P(PeerConnectionIntegrationTest,
Rasmus Brandt685be142021-03-15 14:03:38 +01002309 MAYBE_AddMediaToConnectedBundleDoesNotRestartIce) {
Steve Anton6f25b092017-10-23 09:39:20 -07002310 PeerConnectionInterface::RTCConfiguration config;
2311 config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
2312 config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyRequire;
2313 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(
2314 config, PeerConnectionInterface::RTCConfiguration()));
2315 ConnectFakeSignaling();
2316
Steve Anton15324772018-01-16 10:26:49 -08002317 caller()->AddAudioTrack();
Steve Anton6f25b092017-10-23 09:39:20 -07002318 caller()->CreateAndSetAndSignalOffer();
2319 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Steve Antonff52f1b2017-10-26 12:24:50 -07002320 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceConnectionCompleted,
2321 caller()->ice_connection_state(), kDefaultTimeout);
Steve Anton6f25b092017-10-23 09:39:20 -07002322
2323 caller()->clear_ice_connection_state_history();
2324
Steve Anton15324772018-01-16 10:26:49 -08002325 caller()->AddVideoTrack();
Steve Anton6f25b092017-10-23 09:39:20 -07002326 caller()->CreateAndSetAndSignalOffer();
2327 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2328
2329 EXPECT_EQ(0u, caller()->ice_connection_state_history().size());
2330}
2331
deadbeef1dcb1642017-03-29 21:08:16 -07002332// This test sets up a call between two parties with audio and video. It then
2333// renegotiates setting the video m-line to "port 0", then later renegotiates
2334// again, enabling video.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002335TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07002336 VideoFlowsAfterMediaSectionIsRejectedAndRecycled) {
2337 ASSERT_TRUE(CreatePeerConnectionWrappers());
2338 ConnectFakeSignaling();
2339
2340 // Do initial negotiation, only sending media from the caller. Will result in
2341 // video and audio recvonly "m=" sections.
Steve Anton15324772018-01-16 10:26:49 -08002342 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002343 caller()->CreateAndSetAndSignalOffer();
2344 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2345
2346 // Negotiate again, disabling the video "m=" section (the callee will set the
2347 // port to 0 due to offer_to_receive_video = 0).
Seth Hampson2f0d7022018-02-20 11:54:42 -08002348 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2349 PeerConnectionInterface::RTCOfferAnswerOptions options;
2350 options.offer_to_receive_video = 0;
2351 callee()->SetOfferAnswerOptions(options);
2352 } else {
2353 callee()->SetRemoteOfferHandler([this] {
Harald Alvestrand6060df52020-08-11 09:54:02 +02002354 callee()
2355 ->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)
2356 ->StopInternal();
Seth Hampson2f0d7022018-02-20 11:54:42 -08002357 });
2358 }
deadbeef1dcb1642017-03-29 21:08:16 -07002359 caller()->CreateAndSetAndSignalOffer();
2360 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2361 // Sanity check that video "m=" section was actually rejected.
2362 const ContentInfo* answer_video_content = cricket::GetFirstVideoContent(
2363 callee()->pc()->local_description()->description());
2364 ASSERT_NE(nullptr, answer_video_content);
2365 ASSERT_TRUE(answer_video_content->rejected);
2366
2367 // Enable video and do negotiation again, making sure video is received
2368 // end-to-end, also adding media stream to callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002369 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2370 PeerConnectionInterface::RTCOfferAnswerOptions options;
2371 options.offer_to_receive_video = 1;
2372 callee()->SetOfferAnswerOptions(options);
2373 } else {
2374 // The caller's transceiver is stopped, so we need to add another track.
2375 auto caller_transceiver =
2376 caller()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO);
Harald Alvestrand6060df52020-08-11 09:54:02 +02002377 EXPECT_EQ(nullptr, caller_transceiver.get());
Seth Hampson2f0d7022018-02-20 11:54:42 -08002378 caller()->AddVideoTrack();
2379 }
2380 callee()->AddVideoTrack();
2381 callee()->SetRemoteOfferHandler(nullptr);
deadbeef1dcb1642017-03-29 21:08:16 -07002382 caller()->CreateAndSetAndSignalOffer();
2383 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002384
deadbeef1dcb1642017-03-29 21:08:16 -07002385 // Verify the caller receives frames from the newly added stream, and the
2386 // callee receives additional frames from the re-enabled video m= section.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002387 MediaExpectations media_expectations;
2388 media_expectations.CalleeExpectsSomeAudio();
2389 media_expectations.ExpectBidirectionalVideo();
2390 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002391}
2392
deadbeef1dcb1642017-03-29 21:08:16 -07002393// This tests that if we negotiate after calling CreateSender but before we
2394// have a track, then set a track later, frames from the newly-set track are
2395// received end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002396TEST_F(PeerConnectionIntegrationTestPlanB,
deadbeef1dcb1642017-03-29 21:08:16 -07002397 MediaFlowsAfterEarlyWarmupWithCreateSender) {
2398 ASSERT_TRUE(CreatePeerConnectionWrappers());
2399 ConnectFakeSignaling();
2400 auto caller_audio_sender =
2401 caller()->pc()->CreateSender("audio", "caller_stream");
2402 auto caller_video_sender =
2403 caller()->pc()->CreateSender("video", "caller_stream");
2404 auto callee_audio_sender =
2405 callee()->pc()->CreateSender("audio", "callee_stream");
2406 auto callee_video_sender =
2407 callee()->pc()->CreateSender("video", "callee_stream");
2408 caller()->CreateAndSetAndSignalOffer();
2409 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
2410 // Wait for ICE to complete, without any tracks being set.
2411 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
2412 caller()->ice_connection_state(), kMaxWaitForFramesMs);
2413 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
2414 callee()->ice_connection_state(), kMaxWaitForFramesMs);
2415 // Now set the tracks, and expect frames to immediately start flowing.
2416 EXPECT_TRUE(caller_audio_sender->SetTrack(caller()->CreateLocalAudioTrack()));
2417 EXPECT_TRUE(caller_video_sender->SetTrack(caller()->CreateLocalVideoTrack()));
2418 EXPECT_TRUE(callee_audio_sender->SetTrack(callee()->CreateLocalAudioTrack()));
2419 EXPECT_TRUE(callee_video_sender->SetTrack(callee()->CreateLocalVideoTrack()));
Seth Hampson2f0d7022018-02-20 11:54:42 -08002420 MediaExpectations media_expectations;
2421 media_expectations.ExpectBidirectionalAudioAndVideo();
2422 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2423}
2424
2425// This tests that if we negotiate after calling AddTransceiver but before we
2426// have a track, then set a track later, frames from the newly-set tracks are
2427// received end-to-end.
2428TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
2429 MediaFlowsAfterEarlyWarmupWithAddTransceiver) {
2430 ASSERT_TRUE(CreatePeerConnectionWrappers());
2431 ConnectFakeSignaling();
2432 auto audio_result = caller()->pc()->AddTransceiver(cricket::MEDIA_TYPE_AUDIO);
2433 ASSERT_EQ(RTCErrorType::NONE, audio_result.error().type());
2434 auto caller_audio_sender = audio_result.MoveValue()->sender();
2435 auto video_result = caller()->pc()->AddTransceiver(cricket::MEDIA_TYPE_VIDEO);
2436 ASSERT_EQ(RTCErrorType::NONE, video_result.error().type());
2437 auto caller_video_sender = video_result.MoveValue()->sender();
2438 callee()->SetRemoteOfferHandler([this] {
2439 ASSERT_EQ(2u, callee()->pc()->GetTransceivers().size());
Harald Alvestrand6060df52020-08-11 09:54:02 +02002440 callee()->pc()->GetTransceivers()[0]->SetDirectionWithError(
Seth Hampson2f0d7022018-02-20 11:54:42 -08002441 RtpTransceiverDirection::kSendRecv);
Harald Alvestrand6060df52020-08-11 09:54:02 +02002442 callee()->pc()->GetTransceivers()[1]->SetDirectionWithError(
Seth Hampson2f0d7022018-02-20 11:54:42 -08002443 RtpTransceiverDirection::kSendRecv);
2444 });
2445 caller()->CreateAndSetAndSignalOffer();
2446 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
2447 // Wait for ICE to complete, without any tracks being set.
2448 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
2449 caller()->ice_connection_state(), kMaxWaitForFramesMs);
2450 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
2451 callee()->ice_connection_state(), kMaxWaitForFramesMs);
2452 // Now set the tracks, and expect frames to immediately start flowing.
2453 auto callee_audio_sender = callee()->pc()->GetSenders()[0];
2454 auto callee_video_sender = callee()->pc()->GetSenders()[1];
2455 ASSERT_TRUE(caller_audio_sender->SetTrack(caller()->CreateLocalAudioTrack()));
2456 ASSERT_TRUE(caller_video_sender->SetTrack(caller()->CreateLocalVideoTrack()));
2457 ASSERT_TRUE(callee_audio_sender->SetTrack(callee()->CreateLocalAudioTrack()));
2458 ASSERT_TRUE(callee_video_sender->SetTrack(callee()->CreateLocalVideoTrack()));
2459 MediaExpectations media_expectations;
2460 media_expectations.ExpectBidirectionalAudioAndVideo();
2461 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002462}
2463
2464// This test verifies that a remote video track can be added via AddStream,
2465// and sent end-to-end. For this particular test, it's simply echoed back
2466// from the caller to the callee, rather than being forwarded to a third
2467// PeerConnection.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002468TEST_F(PeerConnectionIntegrationTestPlanB, CanSendRemoteVideoTrack) {
deadbeef1dcb1642017-03-29 21:08:16 -07002469 ASSERT_TRUE(CreatePeerConnectionWrappers());
2470 ConnectFakeSignaling();
2471 // Just send a video track from the caller.
Steve Anton15324772018-01-16 10:26:49 -08002472 caller()->AddVideoTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07002473 caller()->CreateAndSetAndSignalOffer();
2474 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02002475 ASSERT_EQ(1U, callee()->remote_streams()->count());
deadbeef1dcb1642017-03-29 21:08:16 -07002476
2477 // Echo the stream back, and do a new offer/anwer (initiated by callee this
2478 // time).
2479 callee()->pc()->AddStream(callee()->remote_streams()->at(0));
2480 callee()->CreateAndSetAndSignalOffer();
2481 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
2482
Seth Hampson2f0d7022018-02-20 11:54:42 -08002483 MediaExpectations media_expectations;
2484 media_expectations.ExpectBidirectionalVideo();
2485 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002486}
2487
Harald Alvestrandec23d6d2021-02-11 10:47:22 +00002488#if !defined(THREAD_SANITIZER)
2489// This test provokes TSAN errors. bugs.webrtc.org/11282
2490
deadbeef1dcb1642017-03-29 21:08:16 -07002491// Test that we achieve the expected end-to-end connection time, using a
2492// fake clock and simulated latency on the media and signaling paths.
2493// We use a TURN<->TURN connection because this is usually the quickest to
2494// set up initially, especially when we're confident the connection will work
2495// and can start sending media before we get a STUN response.
2496//
2497// With various optimizations enabled, here are the network delays we expect to
2498// be on the critical path:
2499// 1. 2 signaling trips: Signaling offer and offerer's TURN candidate, then
2500// signaling answer (with DTLS fingerprint).
2501// 2. 9 media hops: Rest of the DTLS handshake. 3 hops in each direction when
2502// using TURN<->TURN pair, and DTLS exchange is 4 packets,
2503// the first of which should have arrived before the answer.
Yves Gerey100fe632020-01-17 19:15:53 +01002504TEST_P(PeerConnectionIntegrationTestWithFakeClock,
2505 EndToEndConnectionTimeWithTurnTurnPair) {
deadbeef1dcb1642017-03-29 21:08:16 -07002506 static constexpr int media_hop_delay_ms = 50;
2507 static constexpr int signaling_trip_delay_ms = 500;
2508 // For explanation of these values, see comment above.
2509 static constexpr int required_media_hops = 9;
2510 static constexpr int required_signaling_trips = 2;
2511 // For internal delays (such as posting an event asychronously).
2512 static constexpr int allowed_internal_delay_ms = 20;
2513 static constexpr int total_connection_time_ms =
2514 media_hop_delay_ms * required_media_hops +
2515 signaling_trip_delay_ms * required_signaling_trips +
2516 allowed_internal_delay_ms;
2517
2518 static const rtc::SocketAddress turn_server_1_internal_address{"88.88.88.0",
2519 3478};
2520 static const rtc::SocketAddress turn_server_1_external_address{"88.88.88.1",
2521 0};
2522 static const rtc::SocketAddress turn_server_2_internal_address{"99.99.99.0",
2523 3478};
2524 static const rtc::SocketAddress turn_server_2_external_address{"99.99.99.1",
2525 0};
Seth Hampsonaed71642018-06-11 07:41:32 -07002526 cricket::TestTurnServer* turn_server_1 = CreateTurnServer(
2527 turn_server_1_internal_address, turn_server_1_external_address);
Jonas Orelandbdcee282017-10-10 14:01:40 +02002528
Seth Hampsonaed71642018-06-11 07:41:32 -07002529 cricket::TestTurnServer* turn_server_2 = CreateTurnServer(
2530 turn_server_2_internal_address, turn_server_2_external_address);
deadbeef1dcb1642017-03-29 21:08:16 -07002531 // Bypass permission check on received packets so media can be sent before
2532 // the candidate is signaled.
Seth Hampsonaed71642018-06-11 07:41:32 -07002533 network_thread()->Invoke<void>(RTC_FROM_HERE, [turn_server_1] {
2534 turn_server_1->set_enable_permission_checks(false);
2535 });
2536 network_thread()->Invoke<void>(RTC_FROM_HERE, [turn_server_2] {
2537 turn_server_2->set_enable_permission_checks(false);
2538 });
deadbeef1dcb1642017-03-29 21:08:16 -07002539
2540 PeerConnectionInterface::RTCConfiguration client_1_config;
2541 webrtc::PeerConnectionInterface::IceServer ice_server_1;
2542 ice_server_1.urls.push_back("turn:88.88.88.0:3478");
2543 ice_server_1.username = "test";
2544 ice_server_1.password = "test";
2545 client_1_config.servers.push_back(ice_server_1);
2546 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
2547 client_1_config.presume_writable_when_fully_relayed = true;
2548
2549 PeerConnectionInterface::RTCConfiguration client_2_config;
2550 webrtc::PeerConnectionInterface::IceServer ice_server_2;
2551 ice_server_2.urls.push_back("turn:99.99.99.0:3478");
2552 ice_server_2.username = "test";
2553 ice_server_2.password = "test";
2554 client_2_config.servers.push_back(ice_server_2);
2555 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
2556 client_2_config.presume_writable_when_fully_relayed = true;
2557
2558 ASSERT_TRUE(
2559 CreatePeerConnectionWrappersWithConfig(client_1_config, client_2_config));
2560 // Set up the simulated delays.
2561 SetSignalingDelayMs(signaling_trip_delay_ms);
2562 ConnectFakeSignaling();
2563 virtual_socket_server()->set_delay_mean(media_hop_delay_ms);
2564 virtual_socket_server()->UpdateDelayDistribution();
2565
2566 // Set "offer to receive audio/video" without adding any tracks, so we just
2567 // set up ICE/DTLS with no media.
2568 PeerConnectionInterface::RTCOfferAnswerOptions options;
2569 options.offer_to_receive_audio = 1;
2570 options.offer_to_receive_video = 1;
2571 caller()->SetOfferAnswerOptions(options);
2572 caller()->CreateAndSetAndSignalOffer();
deadbeef71452802017-05-07 17:21:01 -07002573 EXPECT_TRUE_SIMULATED_WAIT(DtlsConnected(), total_connection_time_ms,
Yves Gerey100fe632020-01-17 19:15:53 +01002574 FakeClock());
Seth Hampson1d4a76d2018-06-19 14:31:41 -07002575 // Closing the PeerConnections destroys the ports before the ScopedFakeClock.
2576 // If this is not done a DCHECK can be hit in ports.cc, because a large
2577 // negative number is calculated for the rtt due to the global clock changing.
Steve Antond91969e2019-05-30 12:27:03 -07002578 ClosePeerConnections();
deadbeef1dcb1642017-03-29 21:08:16 -07002579}
2580
Harald Alvestrandec23d6d2021-02-11 10:47:22 +00002581#endif // !defined(THREAD_SANITIZER)
2582
Jonas Orelandbdcee282017-10-10 14:01:40 +02002583// Verify that a TurnCustomizer passed in through RTCConfiguration
2584// is actually used by the underlying TURN candidate pair.
2585// Note that turnport_unittest.cc contains more detailed, lower-level tests.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002586TEST_P(PeerConnectionIntegrationTest, TurnCustomizerUsedForTurnConnections) {
Jonas Orelandbdcee282017-10-10 14:01:40 +02002587 static const rtc::SocketAddress turn_server_1_internal_address{"88.88.88.0",
2588 3478};
2589 static const rtc::SocketAddress turn_server_1_external_address{"88.88.88.1",
2590 0};
2591 static const rtc::SocketAddress turn_server_2_internal_address{"99.99.99.0",
2592 3478};
2593 static const rtc::SocketAddress turn_server_2_external_address{"99.99.99.1",
2594 0};
Seth Hampsonaed71642018-06-11 07:41:32 -07002595 CreateTurnServer(turn_server_1_internal_address,
2596 turn_server_1_external_address);
2597 CreateTurnServer(turn_server_2_internal_address,
2598 turn_server_2_external_address);
Jonas Orelandbdcee282017-10-10 14:01:40 +02002599
2600 PeerConnectionInterface::RTCConfiguration client_1_config;
2601 webrtc::PeerConnectionInterface::IceServer ice_server_1;
2602 ice_server_1.urls.push_back("turn:88.88.88.0:3478");
2603 ice_server_1.username = "test";
2604 ice_server_1.password = "test";
2605 client_1_config.servers.push_back(ice_server_1);
2606 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
Seth Hampsonaed71642018-06-11 07:41:32 -07002607 auto* customizer1 = CreateTurnCustomizer();
2608 client_1_config.turn_customizer = customizer1;
Jonas Orelandbdcee282017-10-10 14:01:40 +02002609
2610 PeerConnectionInterface::RTCConfiguration client_2_config;
2611 webrtc::PeerConnectionInterface::IceServer ice_server_2;
2612 ice_server_2.urls.push_back("turn:99.99.99.0:3478");
2613 ice_server_2.username = "test";
2614 ice_server_2.password = "test";
2615 client_2_config.servers.push_back(ice_server_2);
2616 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
Seth Hampsonaed71642018-06-11 07:41:32 -07002617 auto* customizer2 = CreateTurnCustomizer();
2618 client_2_config.turn_customizer = customizer2;
Jonas Orelandbdcee282017-10-10 14:01:40 +02002619
2620 ASSERT_TRUE(
2621 CreatePeerConnectionWrappersWithConfig(client_1_config, client_2_config));
2622 ConnectFakeSignaling();
2623
2624 // Set "offer to receive audio/video" without adding any tracks, so we just
2625 // set up ICE/DTLS with no media.
2626 PeerConnectionInterface::RTCOfferAnswerOptions options;
2627 options.offer_to_receive_audio = 1;
2628 options.offer_to_receive_video = 1;
2629 caller()->SetOfferAnswerOptions(options);
2630 caller()->CreateAndSetAndSignalOffer();
2631 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
2632
Seth Hampsonaed71642018-06-11 07:41:32 -07002633 ExpectTurnCustomizerCountersIncremented(customizer1);
2634 ExpectTurnCustomizerCountersIncremented(customizer2);
Jonas Orelandbdcee282017-10-10 14:01:40 +02002635}
2636
Benjamin Wright2d5f3cb2018-05-22 14:46:06 -07002637// Verifies that you can use TCP instead of UDP to connect to a TURN server and
2638// send media between the caller and the callee.
2639TEST_P(PeerConnectionIntegrationTest, TCPUsedForTurnConnections) {
2640 static const rtc::SocketAddress turn_server_internal_address{"88.88.88.0",
2641 3478};
2642 static const rtc::SocketAddress turn_server_external_address{"88.88.88.1", 0};
2643
2644 // Enable TCP for the fake turn server.
Seth Hampsonaed71642018-06-11 07:41:32 -07002645 CreateTurnServer(turn_server_internal_address, turn_server_external_address,
2646 cricket::PROTO_TCP);
Benjamin Wright2d5f3cb2018-05-22 14:46:06 -07002647
2648 webrtc::PeerConnectionInterface::IceServer ice_server;
2649 ice_server.urls.push_back("turn:88.88.88.0:3478?transport=tcp");
2650 ice_server.username = "test";
2651 ice_server.password = "test";
2652
2653 PeerConnectionInterface::RTCConfiguration client_1_config;
2654 client_1_config.servers.push_back(ice_server);
2655 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
2656
2657 PeerConnectionInterface::RTCConfiguration client_2_config;
2658 client_2_config.servers.push_back(ice_server);
2659 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
2660
2661 ASSERT_TRUE(
2662 CreatePeerConnectionWrappersWithConfig(client_1_config, client_2_config));
2663
2664 // Do normal offer/answer and wait for ICE to complete.
2665 ConnectFakeSignaling();
2666 caller()->AddAudioVideoTracks();
2667 callee()->AddAudioVideoTracks();
2668 caller()->CreateAndSetAndSignalOffer();
2669 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2670 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
2671 callee()->ice_connection_state(), kMaxWaitForFramesMs);
2672
2673 MediaExpectations media_expectations;
2674 media_expectations.ExpectBidirectionalAudioAndVideo();
2675 EXPECT_TRUE(ExpectNewFrames(media_expectations));
2676}
2677
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07002678// Verify that a SSLCertificateVerifier passed in through
2679// PeerConnectionDependencies is actually used by the underlying SSL
2680// implementation to determine whether a certificate presented by the TURN
2681// server is accepted by the client. Note that openssladapter_unittest.cc
2682// contains more detailed, lower-level tests.
2683TEST_P(PeerConnectionIntegrationTest,
2684 SSLCertificateVerifierUsedForTurnConnections) {
2685 static const rtc::SocketAddress turn_server_internal_address{"88.88.88.0",
2686 3478};
2687 static const rtc::SocketAddress turn_server_external_address{"88.88.88.1", 0};
2688
2689 // Enable TCP-TLS for the fake turn server. We need to pass in 88.88.88.0 so
2690 // that host name verification passes on the fake certificate.
Seth Hampsonaed71642018-06-11 07:41:32 -07002691 CreateTurnServer(turn_server_internal_address, turn_server_external_address,
2692 cricket::PROTO_TLS, "88.88.88.0");
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07002693
2694 webrtc::PeerConnectionInterface::IceServer ice_server;
2695 ice_server.urls.push_back("turns:88.88.88.0:3478?transport=tcp");
2696 ice_server.username = "test";
2697 ice_server.password = "test";
2698
2699 PeerConnectionInterface::RTCConfiguration client_1_config;
2700 client_1_config.servers.push_back(ice_server);
2701 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
2702
2703 PeerConnectionInterface::RTCConfiguration client_2_config;
2704 client_2_config.servers.push_back(ice_server);
2705 // Setting the type to kRelay forces the connection to go through a TURN
2706 // server.
2707 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
2708
2709 // Get a copy to the pointer so we can verify calls later.
2710 rtc::TestCertificateVerifier* client_1_cert_verifier =
2711 new rtc::TestCertificateVerifier();
2712 client_1_cert_verifier->verify_certificate_ = true;
2713 rtc::TestCertificateVerifier* client_2_cert_verifier =
2714 new rtc::TestCertificateVerifier();
2715 client_2_cert_verifier->verify_certificate_ = true;
2716
2717 // Create the dependencies with the test certificate verifier.
2718 webrtc::PeerConnectionDependencies client_1_deps(nullptr);
2719 client_1_deps.tls_cert_verifier =
2720 std::unique_ptr<rtc::TestCertificateVerifier>(client_1_cert_verifier);
2721 webrtc::PeerConnectionDependencies client_2_deps(nullptr);
2722 client_2_deps.tls_cert_verifier =
2723 std::unique_ptr<rtc::TestCertificateVerifier>(client_2_cert_verifier);
2724
2725 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndDeps(
2726 client_1_config, std::move(client_1_deps), client_2_config,
2727 std::move(client_2_deps)));
2728 ConnectFakeSignaling();
2729
2730 // Set "offer to receive audio/video" without adding any tracks, so we just
2731 // set up ICE/DTLS with no media.
2732 PeerConnectionInterface::RTCOfferAnswerOptions options;
2733 options.offer_to_receive_audio = 1;
2734 options.offer_to_receive_video = 1;
2735 caller()->SetOfferAnswerOptions(options);
2736 caller()->CreateAndSetAndSignalOffer();
2737 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
2738
2739 EXPECT_GT(client_1_cert_verifier->call_count_, 0u);
2740 EXPECT_GT(client_2_cert_verifier->call_count_, 0u);
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07002741}
2742
2743TEST_P(PeerConnectionIntegrationTest,
2744 SSLCertificateVerifierFailureUsedForTurnConnectionsFailsConnection) {
2745 static const rtc::SocketAddress turn_server_internal_address{"88.88.88.0",
2746 3478};
2747 static const rtc::SocketAddress turn_server_external_address{"88.88.88.1", 0};
2748
2749 // Enable TCP-TLS for the fake turn server. We need to pass in 88.88.88.0 so
2750 // that host name verification passes on the fake certificate.
Seth Hampsonaed71642018-06-11 07:41:32 -07002751 CreateTurnServer(turn_server_internal_address, turn_server_external_address,
2752 cricket::PROTO_TLS, "88.88.88.0");
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07002753
2754 webrtc::PeerConnectionInterface::IceServer ice_server;
2755 ice_server.urls.push_back("turns:88.88.88.0:3478?transport=tcp");
2756 ice_server.username = "test";
2757 ice_server.password = "test";
2758
2759 PeerConnectionInterface::RTCConfiguration client_1_config;
2760 client_1_config.servers.push_back(ice_server);
2761 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
2762
2763 PeerConnectionInterface::RTCConfiguration client_2_config;
2764 client_2_config.servers.push_back(ice_server);
2765 // Setting the type to kRelay forces the connection to go through a TURN
2766 // server.
2767 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
2768
2769 // Get a copy to the pointer so we can verify calls later.
2770 rtc::TestCertificateVerifier* client_1_cert_verifier =
2771 new rtc::TestCertificateVerifier();
2772 client_1_cert_verifier->verify_certificate_ = false;
2773 rtc::TestCertificateVerifier* client_2_cert_verifier =
2774 new rtc::TestCertificateVerifier();
2775 client_2_cert_verifier->verify_certificate_ = false;
2776
2777 // Create the dependencies with the test certificate verifier.
2778 webrtc::PeerConnectionDependencies client_1_deps(nullptr);
2779 client_1_deps.tls_cert_verifier =
2780 std::unique_ptr<rtc::TestCertificateVerifier>(client_1_cert_verifier);
2781 webrtc::PeerConnectionDependencies client_2_deps(nullptr);
2782 client_2_deps.tls_cert_verifier =
2783 std::unique_ptr<rtc::TestCertificateVerifier>(client_2_cert_verifier);
2784
2785 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndDeps(
2786 client_1_config, std::move(client_1_deps), client_2_config,
2787 std::move(client_2_deps)));
2788 ConnectFakeSignaling();
2789
2790 // Set "offer to receive audio/video" without adding any tracks, so we just
2791 // set up ICE/DTLS with no media.
2792 PeerConnectionInterface::RTCOfferAnswerOptions options;
2793 options.offer_to_receive_audio = 1;
2794 options.offer_to_receive_video = 1;
2795 caller()->SetOfferAnswerOptions(options);
2796 caller()->CreateAndSetAndSignalOffer();
2797 bool wait_res = true;
2798 // TODO(bugs.webrtc.org/9219): When IceConnectionState is implemented
2799 // properly, should be able to just wait for a state of "failed" instead of
2800 // waiting a fixed 10 seconds.
2801 WAIT_(DtlsConnected(), kDefaultTimeout, wait_res);
2802 ASSERT_FALSE(wait_res);
2803
2804 EXPECT_GT(client_1_cert_verifier->call_count_, 0u);
2805 EXPECT_GT(client_2_cert_verifier->call_count_, 0u);
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07002806}
2807
Qingsi Wang25ec8882019-11-15 12:33:05 -08002808// Test that the injected ICE transport factory is used to create ICE transports
2809// for WebRTC connections.
2810TEST_P(PeerConnectionIntegrationTest, IceTransportFactoryUsedForConnections) {
2811 PeerConnectionInterface::RTCConfiguration default_config;
2812 PeerConnectionDependencies dependencies(nullptr);
2813 auto ice_transport_factory = std::make_unique<MockIceTransportFactory>();
2814 EXPECT_CALL(*ice_transport_factory, RecordIceTransportCreated()).Times(1);
2815 dependencies.ice_transport_factory = std::move(ice_transport_factory);
Niels Möller2a707032020-06-16 16:39:13 +02002816 auto wrapper = CreatePeerConnectionWrapper("Caller", nullptr, &default_config,
2817 std::move(dependencies), nullptr,
2818 /*reset_encoder_factory=*/false,
2819 /*reset_decoder_factory=*/false);
Qingsi Wang25ec8882019-11-15 12:33:05 -08002820 ASSERT_TRUE(wrapper);
2821 wrapper->CreateDataChannel();
2822 rtc::scoped_refptr<MockSetSessionDescriptionObserver> observer(
2823 new rtc::RefCountedObject<MockSetSessionDescriptionObserver>());
2824 wrapper->pc()->SetLocalDescription(observer,
2825 wrapper->CreateOfferAndWait().release());
2826}
2827
deadbeefc964d0b2017-04-03 10:03:35 -07002828// Test that audio and video flow end-to-end when codec names don't use the
2829// expected casing, given that they're supposed to be case insensitive. To test
2830// this, all but one codec is removed from each media description, and its
2831// casing is changed.
2832//
2833// In the past, this has regressed and caused crashes/black video, due to the
2834// fact that code at some layers was doing case-insensitive comparisons and
2835// code at other layers was not.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002836TEST_P(PeerConnectionIntegrationTest, CodecNamesAreCaseInsensitive) {
deadbeefc964d0b2017-04-03 10:03:35 -07002837 ASSERT_TRUE(CreatePeerConnectionWrappers());
2838 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002839 caller()->AddAudioVideoTracks();
2840 callee()->AddAudioVideoTracks();
deadbeefc964d0b2017-04-03 10:03:35 -07002841
2842 // Remove all but one audio/video codec (opus and VP8), and change the
2843 // casing of the caller's generated offer.
2844 caller()->SetGeneratedSdpMunger([](cricket::SessionDescription* description) {
2845 cricket::AudioContentDescription* audio =
2846 GetFirstAudioContentDescription(description);
2847 ASSERT_NE(nullptr, audio);
2848 auto audio_codecs = audio->codecs();
2849 audio_codecs.erase(std::remove_if(audio_codecs.begin(), audio_codecs.end(),
2850 [](const cricket::AudioCodec& codec) {
2851 return codec.name != "opus";
2852 }),
2853 audio_codecs.end());
2854 ASSERT_EQ(1u, audio_codecs.size());
2855 audio_codecs[0].name = "OpUs";
2856 audio->set_codecs(audio_codecs);
2857
2858 cricket::VideoContentDescription* video =
2859 GetFirstVideoContentDescription(description);
2860 ASSERT_NE(nullptr, video);
2861 auto video_codecs = video->codecs();
2862 video_codecs.erase(std::remove_if(video_codecs.begin(), video_codecs.end(),
2863 [](const cricket::VideoCodec& codec) {
2864 return codec.name != "VP8";
2865 }),
2866 video_codecs.end());
2867 ASSERT_EQ(1u, video_codecs.size());
2868 video_codecs[0].name = "vP8";
2869 video->set_codecs(video_codecs);
2870 });
2871
2872 caller()->CreateAndSetAndSignalOffer();
2873 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2874
2875 // Verify frames are still received end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002876 MediaExpectations media_expectations;
2877 media_expectations.ExpectBidirectionalAudioAndVideo();
2878 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeefc964d0b2017-04-03 10:03:35 -07002879}
2880
Jonas Oreland49ac5952018-09-26 16:04:32 +02002881TEST_P(PeerConnectionIntegrationTest, GetSourcesAudio) {
hbos8d609f62017-04-10 07:39:05 -07002882 ASSERT_TRUE(CreatePeerConnectionWrappers());
2883 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002884 caller()->AddAudioTrack();
hbos8d609f62017-04-10 07:39:05 -07002885 caller()->CreateAndSetAndSignalOffer();
2886 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
deadbeefd8ad7882017-04-18 16:01:17 -07002887 // Wait for one audio frame to be received by the callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002888 MediaExpectations media_expectations;
2889 media_expectations.CalleeExpectsSomeAudio(1);
2890 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Jonas Oreland49ac5952018-09-26 16:04:32 +02002891 ASSERT_EQ(callee()->pc()->GetReceivers().size(), 1u);
hbos8d609f62017-04-10 07:39:05 -07002892 auto receiver = callee()->pc()->GetReceivers()[0];
2893 ASSERT_EQ(receiver->media_type(), cricket::MEDIA_TYPE_AUDIO);
Jonas Oreland49ac5952018-09-26 16:04:32 +02002894 auto sources = receiver->GetSources();
hbos8d609f62017-04-10 07:39:05 -07002895 ASSERT_GT(receiver->GetParameters().encodings.size(), 0u);
2896 EXPECT_EQ(receiver->GetParameters().encodings[0].ssrc,
Jonas Oreland49ac5952018-09-26 16:04:32 +02002897 sources[0].source_id());
2898 EXPECT_EQ(webrtc::RtpSourceType::SSRC, sources[0].source_type());
2899}
2900
2901TEST_P(PeerConnectionIntegrationTest, GetSourcesVideo) {
2902 ASSERT_TRUE(CreatePeerConnectionWrappers());
2903 ConnectFakeSignaling();
2904 caller()->AddVideoTrack();
2905 caller()->CreateAndSetAndSignalOffer();
2906 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2907 // Wait for one video frame to be received by the callee.
2908 MediaExpectations media_expectations;
2909 media_expectations.CalleeExpectsSomeVideo(1);
2910 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2911 ASSERT_EQ(callee()->pc()->GetReceivers().size(), 1u);
2912 auto receiver = callee()->pc()->GetReceivers()[0];
2913 ASSERT_EQ(receiver->media_type(), cricket::MEDIA_TYPE_VIDEO);
2914 auto sources = receiver->GetSources();
2915 ASSERT_GT(receiver->GetParameters().encodings.size(), 0u);
Yves Gereyf781bb52019-07-23 19:15:39 +02002916 ASSERT_GT(sources.size(), 0u);
Jonas Oreland49ac5952018-09-26 16:04:32 +02002917 EXPECT_EQ(receiver->GetParameters().encodings[0].ssrc,
2918 sources[0].source_id());
2919 EXPECT_EQ(webrtc::RtpSourceType::SSRC, sources[0].source_type());
hbos8d609f62017-04-10 07:39:05 -07002920}
2921
deadbeef2f425aa2017-04-14 10:41:32 -07002922// Test that if a track is removed and added again with a different stream ID,
2923// the new stream ID is successfully communicated in SDP and media continues to
2924// flow end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002925// TODO(webrtc.bugs.org/8734): This test does not work for Unified Plan because
2926// it will not reuse a transceiver that has already been sending. After creating
2927// a new transceiver it tries to create an offer with two senders of the same
2928// track ids and it fails.
2929TEST_F(PeerConnectionIntegrationTestPlanB, RemoveAndAddTrackWithNewStreamId) {
deadbeef2f425aa2017-04-14 10:41:32 -07002930 ASSERT_TRUE(CreatePeerConnectionWrappers());
2931 ConnectFakeSignaling();
2932
deadbeef2f425aa2017-04-14 10:41:32 -07002933 // Add track using stream 1, do offer/answer.
2934 rtc::scoped_refptr<webrtc::AudioTrackInterface> track =
2935 caller()->CreateLocalAudioTrack();
2936 rtc::scoped_refptr<webrtc::RtpSenderInterface> sender =
Steve Antond78323f2018-07-11 11:13:44 -07002937 caller()->AddTrack(track, {"stream_1"});
deadbeef2f425aa2017-04-14 10:41:32 -07002938 caller()->CreateAndSetAndSignalOffer();
2939 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002940 {
2941 MediaExpectations media_expectations;
2942 media_expectations.CalleeExpectsSomeAudio(1);
2943 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2944 }
deadbeef2f425aa2017-04-14 10:41:32 -07002945 // Remove the sender, and create a new one with the new stream.
2946 caller()->pc()->RemoveTrack(sender);
Steve Antond78323f2018-07-11 11:13:44 -07002947 sender = caller()->AddTrack(track, {"stream_2"});
deadbeef2f425aa2017-04-14 10:41:32 -07002948 caller()->CreateAndSetAndSignalOffer();
2949 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2950 // Wait for additional audio frames to be received by the callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002951 {
2952 MediaExpectations media_expectations;
2953 media_expectations.CalleeExpectsSomeAudio();
2954 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2955 }
deadbeef2f425aa2017-04-14 10:41:32 -07002956}
2957
Seth Hampson2f0d7022018-02-20 11:54:42 -08002958TEST_P(PeerConnectionIntegrationTest, RtcEventLogOutputWriteCalled) {
Elad Alon99c3fe52017-10-13 16:29:40 +02002959 ASSERT_TRUE(CreatePeerConnectionWrappers());
2960 ConnectFakeSignaling();
2961
Mirko Bonadei317a1f02019-09-17 17:06:18 +02002962 auto output = std::make_unique<testing::NiceMock<MockRtcEventLogOutput>>();
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002963 ON_CALL(*output, IsActive()).WillByDefault(::testing::Return(true));
2964 ON_CALL(*output, Write(::testing::_)).WillByDefault(::testing::Return(true));
Elad Alon99c3fe52017-10-13 16:29:40 +02002965 EXPECT_CALL(*output, Write(::testing::_)).Times(::testing::AtLeast(1));
Bjorn Tereliusde939432017-11-20 17:38:14 +01002966 EXPECT_TRUE(caller()->pc()->StartRtcEventLog(
2967 std::move(output), webrtc::RtcEventLog::kImmediateOutput));
Elad Alon99c3fe52017-10-13 16:29:40 +02002968
Steve Anton15324772018-01-16 10:26:49 -08002969 caller()->AddAudioVideoTracks();
Elad Alon99c3fe52017-10-13 16:29:40 +02002970 caller()->CreateAndSetAndSignalOffer();
2971 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2972}
2973
Steve Antonede9ca52017-10-16 13:04:27 -07002974// Test that if candidates are only signaled by applying full session
2975// descriptions (instead of using AddIceCandidate), the peers can connect to
2976// each other and exchange media.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002977TEST_P(PeerConnectionIntegrationTest, MediaFlowsWhenCandidatesSetOnlyInSdp) {
Steve Antonede9ca52017-10-16 13:04:27 -07002978 ASSERT_TRUE(CreatePeerConnectionWrappers());
2979 // Each side will signal the session descriptions but not candidates.
2980 ConnectFakeSignalingForSdpOnly();
2981
2982 // Add audio video track and exchange the initial offer/answer with media
2983 // information only. This will start ICE gathering on each side.
Steve Anton15324772018-01-16 10:26:49 -08002984 caller()->AddAudioVideoTracks();
2985 callee()->AddAudioVideoTracks();
Steve Antonede9ca52017-10-16 13:04:27 -07002986 caller()->CreateAndSetAndSignalOffer();
2987
2988 // Wait for all candidates to be gathered on both the caller and callee.
2989 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceGatheringComplete,
2990 caller()->ice_gathering_state(), kDefaultTimeout);
2991 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceGatheringComplete,
2992 callee()->ice_gathering_state(), kDefaultTimeout);
2993
2994 // The candidates will now be included in the session description, so
2995 // signaling them will start the ICE connection.
2996 caller()->CreateAndSetAndSignalOffer();
2997 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2998
2999 // Ensure that media flows in both directions.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003000 MediaExpectations media_expectations;
3001 media_expectations.ExpectBidirectionalAudioAndVideo();
3002 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Antonede9ca52017-10-16 13:04:27 -07003003}
3004
Harald Alvestrandec23d6d2021-02-11 10:47:22 +00003005#if !defined(THREAD_SANITIZER)
3006// These tests provokes TSAN errors. See bugs.webrtc.org/11305.
3007
henrika5f6bf242017-11-01 11:06:56 +01003008// Test that SetAudioPlayout can be used to disable audio playout from the
3009// start, then later enable it. This may be useful, for example, if the caller
3010// needs to play a local ringtone until some event occurs, after which it
3011// switches to playing the received audio.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003012TEST_P(PeerConnectionIntegrationTest, DisableAndEnableAudioPlayout) {
henrika5f6bf242017-11-01 11:06:56 +01003013 ASSERT_TRUE(CreatePeerConnectionWrappers());
3014 ConnectFakeSignaling();
3015
3016 // Set up audio-only call where audio playout is disabled on caller's side.
3017 caller()->pc()->SetAudioPlayout(false);
Steve Anton15324772018-01-16 10:26:49 -08003018 caller()->AddAudioTrack();
3019 callee()->AddAudioTrack();
henrika5f6bf242017-11-01 11:06:56 +01003020 caller()->CreateAndSetAndSignalOffer();
3021 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3022
3023 // Pump messages for a second.
3024 WAIT(false, 1000);
3025 // Since audio playout is disabled, the caller shouldn't have received
3026 // anything (at the playout level, at least).
3027 EXPECT_EQ(0, caller()->audio_frames_received());
3028 // As a sanity check, make sure the callee (for which playout isn't disabled)
3029 // did still see frames on its audio level.
3030 ASSERT_GT(callee()->audio_frames_received(), 0);
3031
3032 // Enable playout again, and ensure audio starts flowing.
3033 caller()->pc()->SetAudioPlayout(true);
Seth Hampson2f0d7022018-02-20 11:54:42 -08003034 MediaExpectations media_expectations;
3035 media_expectations.ExpectBidirectionalAudio();
3036 ASSERT_TRUE(ExpectNewFrames(media_expectations));
henrika5f6bf242017-11-01 11:06:56 +01003037}
3038
Harald Alvestrand39993842021-02-17 09:05:31 +00003039double GetAudioEnergyStat(PeerConnectionIntegrationWrapper* pc) {
henrika5f6bf242017-11-01 11:06:56 +01003040 auto report = pc->NewGetStats();
3041 auto track_stats_list =
3042 report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
3043 const webrtc::RTCMediaStreamTrackStats* remote_track_stats = nullptr;
3044 for (const auto* track_stats : track_stats_list) {
3045 if (track_stats->remote_source.is_defined() &&
3046 *track_stats->remote_source) {
3047 remote_track_stats = track_stats;
3048 break;
3049 }
3050 }
3051
3052 if (!remote_track_stats->total_audio_energy.is_defined()) {
3053 return 0.0;
3054 }
3055 return *remote_track_stats->total_audio_energy;
3056}
3057
3058// Test that if audio playout is disabled via the SetAudioPlayout() method, then
3059// incoming audio is still processed and statistics are generated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003060TEST_P(PeerConnectionIntegrationTest,
henrika5f6bf242017-11-01 11:06:56 +01003061 DisableAudioPlayoutStillGeneratesAudioStats) {
3062 ASSERT_TRUE(CreatePeerConnectionWrappers());
3063 ConnectFakeSignaling();
3064
3065 // Set up audio-only call where playout is disabled but audio-processing is
3066 // still active.
Steve Anton15324772018-01-16 10:26:49 -08003067 caller()->AddAudioTrack();
3068 callee()->AddAudioTrack();
henrika5f6bf242017-11-01 11:06:56 +01003069 caller()->pc()->SetAudioPlayout(false);
3070
3071 caller()->CreateAndSetAndSignalOffer();
3072 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3073
3074 // Wait for the callee to receive audio stats.
3075 EXPECT_TRUE_WAIT(GetAudioEnergyStat(caller()) > 0, kMaxWaitForFramesMs);
3076}
3077
Harald Alvestrandec23d6d2021-02-11 10:47:22 +00003078#endif // !defined(THREAD_SANITIZER)
3079
henrika4f167df2017-11-01 14:45:55 +01003080// Test that SetAudioRecording can be used to disable audio recording from the
3081// start, then later enable it. This may be useful, for example, if the caller
3082// wants to ensure that no audio resources are active before a certain state
3083// is reached.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003084TEST_P(PeerConnectionIntegrationTest, DisableAndEnableAudioRecording) {
henrika4f167df2017-11-01 14:45:55 +01003085 ASSERT_TRUE(CreatePeerConnectionWrappers());
3086 ConnectFakeSignaling();
3087
3088 // Set up audio-only call where audio recording is disabled on caller's side.
3089 caller()->pc()->SetAudioRecording(false);
Steve Anton15324772018-01-16 10:26:49 -08003090 caller()->AddAudioTrack();
3091 callee()->AddAudioTrack();
henrika4f167df2017-11-01 14:45:55 +01003092 caller()->CreateAndSetAndSignalOffer();
3093 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3094
3095 // Pump messages for a second.
3096 WAIT(false, 1000);
3097 // Since caller has disabled audio recording, the callee shouldn't have
3098 // received anything.
3099 EXPECT_EQ(0, callee()->audio_frames_received());
3100 // As a sanity check, make sure the caller did still see frames on its
3101 // audio level since audio recording is enabled on the calle side.
3102 ASSERT_GT(caller()->audio_frames_received(), 0);
3103
3104 // Enable audio recording again, and ensure audio starts flowing.
3105 caller()->pc()->SetAudioRecording(true);
Seth Hampson2f0d7022018-02-20 11:54:42 -08003106 MediaExpectations media_expectations;
3107 media_expectations.ExpectBidirectionalAudio();
3108 ASSERT_TRUE(ExpectNewFrames(media_expectations));
henrika4f167df2017-11-01 14:45:55 +01003109}
3110
Qingsi Wang7685e862018-06-11 20:15:46 -07003111TEST_P(PeerConnectionIntegrationTest,
3112 IceEventsGeneratedAndLoggedInRtcEventLog) {
3113 ASSERT_TRUE(CreatePeerConnectionWrappersWithFakeRtcEventLog());
3114 ConnectFakeSignaling();
3115 PeerConnectionInterface::RTCOfferAnswerOptions options;
3116 options.offer_to_receive_audio = 1;
3117 caller()->SetOfferAnswerOptions(options);
3118 caller()->CreateAndSetAndSignalOffer();
3119 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
3120 ASSERT_NE(nullptr, caller()->event_log_factory());
3121 ASSERT_NE(nullptr, callee()->event_log_factory());
3122 webrtc::FakeRtcEventLog* caller_event_log =
Danil Chapovalov4f281f12021-01-18 13:29:00 +01003123 caller()->event_log_factory()->last_log_created();
Qingsi Wang7685e862018-06-11 20:15:46 -07003124 webrtc::FakeRtcEventLog* callee_event_log =
Danil Chapovalov4f281f12021-01-18 13:29:00 +01003125 callee()->event_log_factory()->last_log_created();
Qingsi Wang7685e862018-06-11 20:15:46 -07003126 ASSERT_NE(nullptr, caller_event_log);
3127 ASSERT_NE(nullptr, callee_event_log);
3128 int caller_ice_config_count = caller_event_log->GetEventCount(
3129 webrtc::RtcEvent::Type::IceCandidatePairConfig);
3130 int caller_ice_event_count = caller_event_log->GetEventCount(
3131 webrtc::RtcEvent::Type::IceCandidatePairEvent);
3132 int callee_ice_config_count = callee_event_log->GetEventCount(
3133 webrtc::RtcEvent::Type::IceCandidatePairConfig);
3134 int callee_ice_event_count = callee_event_log->GetEventCount(
3135 webrtc::RtcEvent::Type::IceCandidatePairEvent);
3136 EXPECT_LT(0, caller_ice_config_count);
3137 EXPECT_LT(0, caller_ice_event_count);
3138 EXPECT_LT(0, callee_ice_config_count);
3139 EXPECT_LT(0, callee_ice_event_count);
3140}
3141
Qingsi Wangc129c352019-04-18 10:41:58 -07003142TEST_P(PeerConnectionIntegrationTest, RegatherAfterChangingIceTransportType) {
Qingsi Wangc129c352019-04-18 10:41:58 -07003143 static const rtc::SocketAddress turn_server_internal_address{"88.88.88.0",
3144 3478};
3145 static const rtc::SocketAddress turn_server_external_address{"88.88.88.1", 0};
3146
3147 CreateTurnServer(turn_server_internal_address, turn_server_external_address);
3148
3149 webrtc::PeerConnectionInterface::IceServer ice_server;
3150 ice_server.urls.push_back("turn:88.88.88.0:3478");
3151 ice_server.username = "test";
3152 ice_server.password = "test";
3153
3154 PeerConnectionInterface::RTCConfiguration caller_config;
3155 caller_config.servers.push_back(ice_server);
3156 caller_config.type = webrtc::PeerConnectionInterface::kRelay;
3157 caller_config.continual_gathering_policy = PeerConnection::GATHER_CONTINUALLY;
Qingsi Wang1fe119f2019-05-31 16:55:33 -07003158 caller_config.surface_ice_candidates_on_ice_transport_type_changed = true;
Qingsi Wangc129c352019-04-18 10:41:58 -07003159
3160 PeerConnectionInterface::RTCConfiguration callee_config;
3161 callee_config.servers.push_back(ice_server);
3162 callee_config.type = webrtc::PeerConnectionInterface::kRelay;
3163 callee_config.continual_gathering_policy = PeerConnection::GATHER_CONTINUALLY;
Qingsi Wang1fe119f2019-05-31 16:55:33 -07003164 callee_config.surface_ice_candidates_on_ice_transport_type_changed = true;
Qingsi Wangc129c352019-04-18 10:41:58 -07003165
3166 ASSERT_TRUE(
3167 CreatePeerConnectionWrappersWithConfig(caller_config, callee_config));
3168
3169 // Do normal offer/answer and wait for ICE to complete.
3170 ConnectFakeSignaling();
3171 caller()->AddAudioVideoTracks();
3172 callee()->AddAudioVideoTracks();
3173 caller()->CreateAndSetAndSignalOffer();
3174 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3175 // Since we are doing continual gathering, the ICE transport does not reach
3176 // kIceGatheringComplete (see
3177 // P2PTransportChannel::OnCandidatesAllocationDone), and consequently not
3178 // kIceConnectionComplete.
3179 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
3180 caller()->ice_connection_state(), kDefaultTimeout);
3181 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
3182 callee()->ice_connection_state(), kDefaultTimeout);
3183 // Note that we cannot use the metric
3184 // |WebRTC.PeerConnection.CandidatePairType_UDP| in this test since this
3185 // metric is only populated when we reach kIceConnectionComplete in the
3186 // current implementation.
3187 EXPECT_EQ(cricket::RELAY_PORT_TYPE,
3188 caller()->last_candidate_gathered().type());
3189 EXPECT_EQ(cricket::RELAY_PORT_TYPE,
3190 callee()->last_candidate_gathered().type());
3191
3192 // Loosen the caller's candidate filter.
3193 caller_config = caller()->pc()->GetConfiguration();
3194 caller_config.type = webrtc::PeerConnectionInterface::kAll;
3195 caller()->pc()->SetConfiguration(caller_config);
3196 // We should have gathered a new host candidate.
3197 EXPECT_EQ_WAIT(cricket::LOCAL_PORT_TYPE,
3198 caller()->last_candidate_gathered().type(), kDefaultTimeout);
3199
3200 // Loosen the callee's candidate filter.
3201 callee_config = callee()->pc()->GetConfiguration();
3202 callee_config.type = webrtc::PeerConnectionInterface::kAll;
3203 callee()->pc()->SetConfiguration(callee_config);
3204 EXPECT_EQ_WAIT(cricket::LOCAL_PORT_TYPE,
3205 callee()->last_candidate_gathered().type(), kDefaultTimeout);
Jonas Orelande3096512020-05-27 09:01:05 +02003206
3207 // Create an offer and verify that it does not contain an ICE restart (i.e new
3208 // ice credentials).
3209 std::string caller_ufrag_pre_offer = caller()
3210 ->pc()
3211 ->local_description()
3212 ->description()
3213 ->transport_infos()[0]
3214 .description.ice_ufrag;
3215 caller()->CreateAndSetAndSignalOffer();
3216 std::string caller_ufrag_post_offer = caller()
3217 ->pc()
3218 ->local_description()
3219 ->description()
3220 ->transport_infos()[0]
3221 .description.ice_ufrag;
3222 EXPECT_EQ(caller_ufrag_pre_offer, caller_ufrag_post_offer);
Qingsi Wangc129c352019-04-18 10:41:58 -07003223}
3224
Eldar Relloda13ea22019-06-01 12:23:43 +03003225TEST_P(PeerConnectionIntegrationTest, OnIceCandidateError) {
Eldar Relloda13ea22019-06-01 12:23:43 +03003226 static const rtc::SocketAddress turn_server_internal_address{"88.88.88.0",
3227 3478};
3228 static const rtc::SocketAddress turn_server_external_address{"88.88.88.1", 0};
3229
3230 CreateTurnServer(turn_server_internal_address, turn_server_external_address);
3231
3232 webrtc::PeerConnectionInterface::IceServer ice_server;
3233 ice_server.urls.push_back("turn:88.88.88.0:3478");
3234 ice_server.username = "test";
3235 ice_server.password = "123";
3236
3237 PeerConnectionInterface::RTCConfiguration caller_config;
3238 caller_config.servers.push_back(ice_server);
3239 caller_config.type = webrtc::PeerConnectionInterface::kRelay;
3240 caller_config.continual_gathering_policy = PeerConnection::GATHER_CONTINUALLY;
3241
3242 PeerConnectionInterface::RTCConfiguration callee_config;
3243 callee_config.servers.push_back(ice_server);
3244 callee_config.type = webrtc::PeerConnectionInterface::kRelay;
3245 callee_config.continual_gathering_policy = PeerConnection::GATHER_CONTINUALLY;
3246
3247 ASSERT_TRUE(
3248 CreatePeerConnectionWrappersWithConfig(caller_config, callee_config));
3249
3250 // Do normal offer/answer and wait for ICE to complete.
3251 ConnectFakeSignaling();
3252 caller()->AddAudioVideoTracks();
3253 callee()->AddAudioVideoTracks();
3254 caller()->CreateAndSetAndSignalOffer();
3255 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3256 EXPECT_EQ_WAIT(401, caller()->error_event().error_code, kDefaultTimeout);
3257 EXPECT_EQ("Unauthorized", caller()->error_event().error_text);
3258 EXPECT_EQ("turn:88.88.88.0:3478?transport=udp", caller()->error_event().url);
Eldar Rello0095d372019-12-02 22:22:07 +02003259 EXPECT_NE(caller()->error_event().address, "");
Eldar Relloda13ea22019-06-01 12:23:43 +03003260}
3261
Eldar Rellofa8019c2020-05-14 11:59:33 +03003262TEST_P(PeerConnectionIntegrationTest, OnIceCandidateErrorWithEmptyAddress) {
3263 webrtc::PeerConnectionInterface::IceServer ice_server;
3264 ice_server.urls.push_back("turn:127.0.0.1:3478?transport=tcp");
3265 ice_server.username = "test";
3266 ice_server.password = "test";
3267
3268 PeerConnectionInterface::RTCConfiguration caller_config;
3269 caller_config.servers.push_back(ice_server);
3270 caller_config.type = webrtc::PeerConnectionInterface::kRelay;
3271 caller_config.continual_gathering_policy = PeerConnection::GATHER_CONTINUALLY;
3272
3273 PeerConnectionInterface::RTCConfiguration callee_config;
3274 callee_config.servers.push_back(ice_server);
3275 callee_config.type = webrtc::PeerConnectionInterface::kRelay;
3276 callee_config.continual_gathering_policy = PeerConnection::GATHER_CONTINUALLY;
3277
3278 ASSERT_TRUE(
3279 CreatePeerConnectionWrappersWithConfig(caller_config, callee_config));
3280
3281 // Do normal offer/answer and wait for ICE to complete.
3282 ConnectFakeSignaling();
3283 caller()->AddAudioVideoTracks();
3284 callee()->AddAudioVideoTracks();
3285 caller()->CreateAndSetAndSignalOffer();
3286 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3287 EXPECT_EQ_WAIT(701, caller()->error_event().error_code, kDefaultTimeout);
3288 EXPECT_EQ(caller()->error_event().address, "");
3289}
3290
Eldar Rello5ab79e62019-10-09 18:29:44 +03003291TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
3292 AudioKeepsFlowingAfterImplicitRollback) {
3293 PeerConnectionInterface::RTCConfiguration config;
3294 config.sdp_semantics = SdpSemantics::kUnifiedPlan;
3295 config.enable_implicit_rollback = true;
3296 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
3297 ConnectFakeSignaling();
3298 caller()->AddAudioTrack();
3299 callee()->AddAudioTrack();
3300 caller()->CreateAndSetAndSignalOffer();
3301 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3302 MediaExpectations media_expectations;
3303 media_expectations.ExpectBidirectionalAudio();
3304 ASSERT_TRUE(ExpectNewFrames(media_expectations));
3305 SetSignalIceCandidates(false); // Workaround candidate outrace sdp.
3306 caller()->AddVideoTrack();
3307 callee()->AddVideoTrack();
3308 rtc::scoped_refptr<MockSetSessionDescriptionObserver> observer(
3309 new rtc::RefCountedObject<MockSetSessionDescriptionObserver>());
3310 callee()->pc()->SetLocalDescription(observer,
3311 callee()->CreateOfferAndWait().release());
3312 EXPECT_TRUE_WAIT(observer->called(), kDefaultTimeout);
3313 caller()->CreateAndSetAndSignalOffer(); // Implicit rollback.
3314 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3315 ASSERT_TRUE(ExpectNewFrames(media_expectations));
3316}
3317
3318TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
3319 ImplicitRollbackVisitsStableState) {
3320 RTCConfiguration config;
3321 config.sdp_semantics = SdpSemantics::kUnifiedPlan;
3322 config.enable_implicit_rollback = true;
3323
3324 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
3325
3326 rtc::scoped_refptr<MockSetSessionDescriptionObserver> sld_observer(
3327 new rtc::RefCountedObject<MockSetSessionDescriptionObserver>());
3328 callee()->pc()->SetLocalDescription(sld_observer,
3329 callee()->CreateOfferAndWait().release());
3330 EXPECT_TRUE_WAIT(sld_observer->called(), kDefaultTimeout);
3331 EXPECT_EQ(sld_observer->error(), "");
3332
3333 rtc::scoped_refptr<MockSetSessionDescriptionObserver> srd_observer(
3334 new rtc::RefCountedObject<MockSetSessionDescriptionObserver>());
3335 callee()->pc()->SetRemoteDescription(
3336 srd_observer, caller()->CreateOfferAndWait().release());
3337 EXPECT_TRUE_WAIT(srd_observer->called(), kDefaultTimeout);
3338 EXPECT_EQ(srd_observer->error(), "");
3339
3340 EXPECT_THAT(callee()->peer_connection_signaling_state_history(),
3341 ElementsAre(PeerConnectionInterface::kHaveLocalOffer,
3342 PeerConnectionInterface::kStable,
3343 PeerConnectionInterface::kHaveRemoteOffer));
3344}
3345
Eldar Rellobd9c33a2020-10-01 17:52:45 +03003346TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
3347 H264FmtpSpsPpsIdrInKeyframeParameterUsage) {
3348 ASSERT_TRUE(CreatePeerConnectionWrappers());
3349 ConnectFakeSignaling();
3350 caller()->AddVideoTrack();
3351 callee()->AddVideoTrack();
3352 auto munger = [](cricket::SessionDescription* desc) {
3353 cricket::VideoContentDescription* video =
3354 GetFirstVideoContentDescription(desc);
3355 auto codecs = video->codecs();
3356 for (auto&& codec : codecs) {
3357 if (codec.name == "H264") {
3358 std::string value;
3359 // The parameter is not supposed to be present in SDP by default.
3360 EXPECT_FALSE(
3361 codec.GetParam(cricket::kH264FmtpSpsPpsIdrInKeyframe, &value));
3362 codec.SetParam(std::string(cricket::kH264FmtpSpsPpsIdrInKeyframe),
3363 std::string(""));
3364 }
3365 }
3366 video->set_codecs(codecs);
3367 };
3368 // Munge local offer for SLD.
3369 caller()->SetGeneratedSdpMunger(munger);
3370 // Munge remote answer for SRD.
3371 caller()->SetReceivedSdpMunger(munger);
3372 caller()->CreateAndSetAndSignalOffer();
3373 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3374 // Observe that after munging the parameter is present in generated SDP.
3375 caller()->SetGeneratedSdpMunger([](cricket::SessionDescription* desc) {
3376 cricket::VideoContentDescription* video =
3377 GetFirstVideoContentDescription(desc);
3378 for (auto&& codec : video->codecs()) {
3379 if (codec.name == "H264") {
3380 std::string value;
3381 EXPECT_TRUE(
3382 codec.GetParam(cricket::kH264FmtpSpsPpsIdrInKeyframe, &value));
3383 }
3384 }
3385 });
3386 caller()->CreateOfferAndWait();
3387}
3388
Harald Alvestrand1a9be302020-12-11 14:53:59 +00003389TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
Harald Alvestrand94324f22021-01-13 12:31:53 +00003390 RenegotiateManyAudioTransceivers) {
Harald Alvestrand1a9be302020-12-11 14:53:59 +00003391 PeerConnectionInterface::RTCConfiguration config;
3392 config.sdp_semantics = SdpSemantics::kUnifiedPlan;
3393 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
3394 ConnectFakeSignaling();
3395 caller()->pc()->AddTransceiver(cricket::MEDIA_TYPE_AUDIO);
3396
3397 caller()->CreateAndSetAndSignalOffer();
3398 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3399 int current_size = caller()->pc()->GetTransceivers().size();
3400 // Add more tracks until we get close to having issues.
3401 // Issues have been seen at:
3402 // - 32 tracks on android_arm64_rel and android_arm_dbg bots
Harald Alvestrandcc6ae442021-01-18 08:06:23 +00003403 // - 16 tracks on android_arm_dbg (flaky)
3404 while (current_size < 8) {
Harald Alvestrand1a9be302020-12-11 14:53:59 +00003405 // Double the number of tracks
3406 for (int i = 0; i < current_size; i++) {
3407 caller()->pc()->AddTransceiver(cricket::MEDIA_TYPE_AUDIO);
3408 }
3409 current_size = caller()->pc()->GetTransceivers().size();
3410 RTC_LOG(LS_INFO) << "Renegotiating with " << current_size << " tracks";
3411 auto start_time_ms = rtc::TimeMillis();
3412 caller()->CreateAndSetAndSignalOffer();
3413 // We want to stop when the time exceeds one second.
3414 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3415 auto elapsed_time_ms = rtc::TimeMillis() - start_time_ms;
3416 RTC_LOG(LS_INFO) << "Renegotiating took " << elapsed_time_ms << " ms";
3417 ASSERT_GT(1000, elapsed_time_ms)
3418 << "Audio transceivers: Negotiation took too long after "
3419 << current_size << " tracks added";
3420 }
3421}
3422
3423TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
3424 RenegotiateManyVideoTransceivers) {
3425 PeerConnectionInterface::RTCConfiguration config;
3426 config.sdp_semantics = SdpSemantics::kUnifiedPlan;
3427 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
3428 ConnectFakeSignaling();
3429 caller()->pc()->AddTransceiver(cricket::MEDIA_TYPE_VIDEO);
3430
3431 caller()->CreateAndSetAndSignalOffer();
3432 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3433 int current_size = caller()->pc()->GetTransceivers().size();
3434 // Add more tracks until we get close to having issues.
3435 // Issues have been seen at:
3436 // - 96 on a Linux workstation
3437 // - 64 at win_x86_more_configs and win_x64_msvc_dbg
3438 // - 32 on android_arm64_rel and linux_dbg bots
3439 while (current_size < 16) {
3440 // Double the number of tracks
3441 for (int i = 0; i < current_size; i++) {
3442 caller()->pc()->AddTransceiver(cricket::MEDIA_TYPE_VIDEO);
3443 }
3444 current_size = caller()->pc()->GetTransceivers().size();
3445 RTC_LOG(LS_INFO) << "Renegotiating with " << current_size << " tracks";
3446 auto start_time_ms = rtc::TimeMillis();
3447 caller()->CreateAndSetAndSignalOffer();
3448 // We want to stop when the time exceeds one second.
3449 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3450 auto elapsed_time_ms = rtc::TimeMillis() - start_time_ms;
3451 RTC_LOG(LS_INFO) << "Renegotiating took " << elapsed_time_ms << " ms";
3452 ASSERT_GT(1000, elapsed_time_ms)
3453 << "Video transceivers: Negotiation took too long after "
3454 << current_size << " tracks added";
3455 }
3456}
3457
Harald Alvestrand94324f22021-01-13 12:31:53 +00003458TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
3459 RenegotiateManyVideoTransceiversAndWatchAudioDelay) {
3460 PeerConnectionInterface::RTCConfiguration config;
3461 config.sdp_semantics = SdpSemantics::kUnifiedPlan;
3462 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
3463 ConnectFakeSignaling();
3464 caller()->AddAudioTrack();
Harald Alvestrandcc6ae442021-01-18 08:06:23 +00003465 callee()->AddAudioTrack();
Harald Alvestrand94324f22021-01-13 12:31:53 +00003466 caller()->CreateAndSetAndSignalOffer();
3467 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3468 // Wait until we can see the audio flowing.
3469 MediaExpectations media_expectations;
3470 media_expectations.CalleeExpectsSomeAudio();
3471 ASSERT_TRUE(ExpectNewFrames(media_expectations));
3472
Harald Alvestrandcc6ae442021-01-18 08:06:23 +00003473 // Get the baseline numbers for audio_packets and audio_delay
3474 // in both directions.
3475 caller()->StartWatchingDelayStats();
3476 callee()->StartWatchingDelayStats();
Harald Alvestrand94324f22021-01-13 12:31:53 +00003477
3478 int current_size = caller()->pc()->GetTransceivers().size();
3479 // Add more tracks until we get close to having issues.
3480 // Making this number very large makes the test very slow.
Harald Alvestrandcc6ae442021-01-18 08:06:23 +00003481 while (current_size < 16) {
Harald Alvestrand94324f22021-01-13 12:31:53 +00003482 // Double the number of tracks
3483 for (int i = 0; i < current_size; i++) {
3484 caller()->pc()->AddTransceiver(cricket::MEDIA_TYPE_VIDEO);
3485 }
3486 current_size = caller()->pc()->GetTransceivers().size();
3487 RTC_LOG(LS_INFO) << "Renegotiating with " << current_size << " tracks";
3488 auto start_time_ms = rtc::TimeMillis();
3489 caller()->CreateAndSetAndSignalOffer();
3490 // We want to stop when the time exceeds one second.
3491 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3492 auto elapsed_time_ms = rtc::TimeMillis() - start_time_ms;
3493 RTC_LOG(LS_INFO) << "Renegotiating took " << elapsed_time_ms << " ms";
3494 // This is a guard against the test using excessive amounts of time.
3495 ASSERT_GT(5000, elapsed_time_ms)
3496 << "Video transceivers: Negotiation took too long after "
3497 << current_size << " tracks added";
Harald Alvestrandcc6ae442021-01-18 08:06:23 +00003498 caller()->UpdateDelayStats("caller reception", current_size);
3499 callee()->UpdateDelayStats("callee reception", current_size);
Harald Alvestrand94324f22021-01-13 12:31:53 +00003500 }
3501}
3502
Mirko Bonadeic84f6612019-01-31 12:20:57 +01003503INSTANTIATE_TEST_SUITE_P(PeerConnectionIntegrationTest,
3504 PeerConnectionIntegrationTest,
3505 Values(SdpSemantics::kPlanB,
3506 SdpSemantics::kUnifiedPlan));
Steve Antond3679212018-01-17 17:41:02 -08003507
Yves Gerey100fe632020-01-17 19:15:53 +01003508INSTANTIATE_TEST_SUITE_P(PeerConnectionIntegrationTest,
3509 PeerConnectionIntegrationTestWithFakeClock,
3510 Values(SdpSemantics::kPlanB,
3511 SdpSemantics::kUnifiedPlan));
3512
Steve Anton74255ff2018-01-24 18:32:57 -08003513// Tests that verify interoperability between Plan B and Unified Plan
3514// PeerConnections.
3515class PeerConnectionIntegrationInteropTest
Seth Hampson2f0d7022018-02-20 11:54:42 -08003516 : public PeerConnectionIntegrationBaseTest,
Steve Anton74255ff2018-01-24 18:32:57 -08003517 public ::testing::WithParamInterface<
3518 std::tuple<SdpSemantics, SdpSemantics>> {
3519 protected:
Seth Hampson2f0d7022018-02-20 11:54:42 -08003520 // Setting the SdpSemantics for the base test to kDefault does not matter
3521 // because we specify not to use the test semantics when creating
Harald Alvestrand39993842021-02-17 09:05:31 +00003522 // PeerConnectionIntegrationWrappers.
Steve Anton74255ff2018-01-24 18:32:57 -08003523 PeerConnectionIntegrationInteropTest()
Steve Anton3acffc32018-04-12 17:21:03 -07003524 : PeerConnectionIntegrationBaseTest(SdpSemantics::kPlanB),
Seth Hampson2f0d7022018-02-20 11:54:42 -08003525 caller_semantics_(std::get<0>(GetParam())),
Steve Anton74255ff2018-01-24 18:32:57 -08003526 callee_semantics_(std::get<1>(GetParam())) {}
3527
3528 bool CreatePeerConnectionWrappersWithSemantics() {
Steve Anton3acffc32018-04-12 17:21:03 -07003529 return CreatePeerConnectionWrappersWithSdpSemantics(caller_semantics_,
3530 callee_semantics_);
Steve Anton74255ff2018-01-24 18:32:57 -08003531 }
3532
3533 const SdpSemantics caller_semantics_;
3534 const SdpSemantics callee_semantics_;
3535};
3536
3537TEST_P(PeerConnectionIntegrationInteropTest, NoMediaLocalToNoMediaRemote) {
3538 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
3539 ConnectFakeSignaling();
3540
3541 caller()->CreateAndSetAndSignalOffer();
3542 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3543}
3544
3545TEST_P(PeerConnectionIntegrationInteropTest, OneAudioLocalToNoMediaRemote) {
3546 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
3547 ConnectFakeSignaling();
3548 auto audio_sender = caller()->AddAudioTrack();
3549
3550 caller()->CreateAndSetAndSignalOffer();
3551 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3552
3553 // Verify that one audio receiver has been created on the remote and that it
3554 // has the same track ID as the sending track.
3555 auto receivers = callee()->pc()->GetReceivers();
3556 ASSERT_EQ(1u, receivers.size());
3557 EXPECT_EQ(cricket::MEDIA_TYPE_AUDIO, receivers[0]->media_type());
3558 EXPECT_EQ(receivers[0]->track()->id(), audio_sender->track()->id());
3559
Seth Hampson2f0d7022018-02-20 11:54:42 -08003560 MediaExpectations media_expectations;
3561 media_expectations.CalleeExpectsSomeAudio();
3562 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08003563}
3564
3565TEST_P(PeerConnectionIntegrationInteropTest, OneAudioOneVideoToNoMediaRemote) {
3566 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
3567 ConnectFakeSignaling();
3568 auto video_sender = caller()->AddVideoTrack();
3569 auto audio_sender = caller()->AddAudioTrack();
3570
3571 caller()->CreateAndSetAndSignalOffer();
3572 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3573
3574 // Verify that one audio and one video receiver have been created on the
3575 // remote and that they have the same track IDs as the sending tracks.
3576 auto audio_receivers =
3577 callee()->GetReceiversOfType(cricket::MEDIA_TYPE_AUDIO);
3578 ASSERT_EQ(1u, audio_receivers.size());
3579 EXPECT_EQ(audio_receivers[0]->track()->id(), audio_sender->track()->id());
3580 auto video_receivers =
3581 callee()->GetReceiversOfType(cricket::MEDIA_TYPE_VIDEO);
3582 ASSERT_EQ(1u, video_receivers.size());
3583 EXPECT_EQ(video_receivers[0]->track()->id(), video_sender->track()->id());
3584
Seth Hampson2f0d7022018-02-20 11:54:42 -08003585 MediaExpectations media_expectations;
3586 media_expectations.CalleeExpectsSomeAudioAndVideo();
3587 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08003588}
3589
3590TEST_P(PeerConnectionIntegrationInteropTest,
3591 OneAudioOneVideoLocalToOneAudioOneVideoRemote) {
3592 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
3593 ConnectFakeSignaling();
3594 caller()->AddAudioVideoTracks();
3595 callee()->AddAudioVideoTracks();
3596
3597 caller()->CreateAndSetAndSignalOffer();
3598 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3599
Seth Hampson2f0d7022018-02-20 11:54:42 -08003600 MediaExpectations media_expectations;
3601 media_expectations.ExpectBidirectionalAudioAndVideo();
3602 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08003603}
3604
3605TEST_P(PeerConnectionIntegrationInteropTest,
3606 ReverseRolesOneAudioLocalToOneVideoRemote) {
3607 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
3608 ConnectFakeSignaling();
3609 caller()->AddAudioTrack();
3610 callee()->AddVideoTrack();
3611
3612 caller()->CreateAndSetAndSignalOffer();
3613 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3614
3615 // Verify that only the audio track has been negotiated.
3616 EXPECT_EQ(0u, caller()->GetReceiversOfType(cricket::MEDIA_TYPE_VIDEO).size());
3617 // Might also check that the callee's NegotiationNeeded flag is set.
3618
3619 // Reverse roles.
3620 callee()->CreateAndSetAndSignalOffer();
3621 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3622
Seth Hampson2f0d7022018-02-20 11:54:42 -08003623 MediaExpectations media_expectations;
3624 media_expectations.CallerExpectsSomeVideo();
3625 media_expectations.CalleeExpectsSomeAudio();
3626 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08003627}
3628
Mirko Bonadeic84f6612019-01-31 12:20:57 +01003629INSTANTIATE_TEST_SUITE_P(
Steve Antonba42e992018-04-09 14:10:01 -07003630 PeerConnectionIntegrationTest,
3631 PeerConnectionIntegrationInteropTest,
3632 Values(std::make_tuple(SdpSemantics::kPlanB, SdpSemantics::kUnifiedPlan),
3633 std::make_tuple(SdpSemantics::kUnifiedPlan, SdpSemantics::kPlanB)));
3634
3635// Test that if the Unified Plan side offers two video tracks then the Plan B
3636// side will only see the first one and ignore the second.
3637TEST_F(PeerConnectionIntegrationTestPlanB, TwoVideoUnifiedPlanToNoMediaPlanB) {
Steve Anton3acffc32018-04-12 17:21:03 -07003638 ASSERT_TRUE(CreatePeerConnectionWrappersWithSdpSemantics(
3639 SdpSemantics::kUnifiedPlan, SdpSemantics::kPlanB));
Steve Anton74255ff2018-01-24 18:32:57 -08003640 ConnectFakeSignaling();
3641 auto first_sender = caller()->AddVideoTrack();
3642 caller()->AddVideoTrack();
3643
3644 caller()->CreateAndSetAndSignalOffer();
3645 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3646
3647 // Verify that there is only one receiver and it corresponds to the first
3648 // added track.
3649 auto receivers = callee()->pc()->GetReceivers();
3650 ASSERT_EQ(1u, receivers.size());
3651 EXPECT_TRUE(receivers[0]->track()->enabled());
3652 EXPECT_EQ(first_sender->track()->id(), receivers[0]->track()->id());
3653
Seth Hampson2f0d7022018-02-20 11:54:42 -08003654 MediaExpectations media_expectations;
3655 media_expectations.CalleeExpectsSomeVideo();
3656 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08003657}
3658
Steve Anton2bed3972019-01-04 17:04:30 -08003659// Test that if the initial offer tagged BUNDLE section is rejected due to its
3660// associated RtpTransceiver being stopped and another transceiver is added,
3661// then renegotiation causes the callee to receive the new video track without
3662// error.
3663// This is a regression test for bugs.webrtc.org/9954
3664TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
3665 ReOfferWithStoppedBundleTaggedTransceiver) {
3666 RTCConfiguration config;
3667 config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
3668 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
3669 ConnectFakeSignaling();
3670 auto audio_transceiver_or_error =
3671 caller()->pc()->AddTransceiver(caller()->CreateLocalAudioTrack());
3672 ASSERT_TRUE(audio_transceiver_or_error.ok());
3673 auto audio_transceiver = audio_transceiver_or_error.MoveValue();
3674
3675 caller()->CreateAndSetAndSignalOffer();
3676 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3677 {
3678 MediaExpectations media_expectations;
3679 media_expectations.CalleeExpectsSomeAudio();
3680 ASSERT_TRUE(ExpectNewFrames(media_expectations));
3681 }
3682
Harald Alvestrand6060df52020-08-11 09:54:02 +02003683 audio_transceiver->StopInternal();
Steve Anton2bed3972019-01-04 17:04:30 -08003684 caller()->pc()->AddTransceiver(caller()->CreateLocalVideoTrack());
3685
3686 caller()->CreateAndSetAndSignalOffer();
3687 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3688 {
3689 MediaExpectations media_expectations;
3690 media_expectations.CalleeExpectsSomeVideo();
3691 ASSERT_TRUE(ExpectNewFrames(media_expectations));
3692 }
3693}
3694
Harald Alvestrandbedb6052020-08-20 14:50:10 +02003695TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
3696 StopTransceiverRemovesDtlsTransports) {
3697 RTCConfiguration config;
3698 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
3699 ConnectFakeSignaling();
3700 auto audio_transceiver_or_error =
3701 caller()->pc()->AddTransceiver(caller()->CreateLocalAudioTrack());
3702 ASSERT_TRUE(audio_transceiver_or_error.ok());
3703 auto audio_transceiver = audio_transceiver_or_error.MoveValue();
3704
3705 caller()->CreateAndSetAndSignalOffer();
3706 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3707
3708 audio_transceiver->StopStandard();
3709 caller()->CreateAndSetAndSignalOffer();
3710 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3711 ASSERT_EQ(0U, caller()->pc()->GetTransceivers().size());
3712 EXPECT_EQ(PeerConnectionInterface::kIceGatheringNew,
3713 caller()->pc()->ice_gathering_state());
3714 EXPECT_THAT(caller()->ice_gathering_state_history(),
3715 ElementsAre(PeerConnectionInterface::kIceGatheringGathering,
3716 PeerConnectionInterface::kIceGatheringComplete,
3717 PeerConnectionInterface::kIceGatheringNew));
3718}
3719
Harald Alvestrand1ee33252020-09-24 13:31:15 +00003720TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
Harald Alvestrand45be0a92020-09-30 06:55:23 +00003721 StopTransceiverStopsAndRemovesTransceivers) {
3722 RTCConfiguration config;
3723 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
3724 ConnectFakeSignaling();
3725 auto audio_transceiver_or_error =
3726 caller()->pc()->AddTransceiver(caller()->CreateLocalAudioTrack());
3727 ASSERT_TRUE(audio_transceiver_or_error.ok());
3728 auto caller_transceiver = audio_transceiver_or_error.MoveValue();
3729
3730 caller()->CreateAndSetAndSignalOffer();
3731 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3732 caller_transceiver->StopStandard();
3733
3734 auto callee_transceiver = callee()->pc()->GetTransceivers()[0];
3735 caller()->CreateAndSetAndSignalOffer();
3736 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3737 EXPECT_EQ(0U, caller()->pc()->GetTransceivers().size());
3738 EXPECT_EQ(0U, callee()->pc()->GetTransceivers().size());
3739 EXPECT_EQ(0U, caller()->pc()->GetSenders().size());
3740 EXPECT_EQ(0U, callee()->pc()->GetSenders().size());
3741 EXPECT_EQ(0U, caller()->pc()->GetReceivers().size());
3742 EXPECT_EQ(0U, callee()->pc()->GetReceivers().size());
3743 EXPECT_TRUE(caller_transceiver->stopped());
3744 EXPECT_TRUE(callee_transceiver->stopped());
3745}
3746
3747TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
Harald Alvestrand1ee33252020-09-24 13:31:15 +00003748 StopTransceiverEndsIncomingAudioTrack) {
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 audio_transceiver = audio_transceiver_or_error.MoveValue();
3756
3757 caller()->CreateAndSetAndSignalOffer();
3758 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3759 auto caller_track = audio_transceiver->receiver()->track();
3760 auto callee_track = callee()->pc()->GetReceivers()[0]->track();
3761 audio_transceiver->StopStandard();
3762 EXPECT_EQ(MediaStreamTrackInterface::TrackState::kEnded,
3763 caller_track->state());
3764 caller()->CreateAndSetAndSignalOffer();
3765 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3766 EXPECT_EQ(MediaStreamTrackInterface::TrackState::kEnded,
3767 callee_track->state());
3768}
3769
3770TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
3771 StopTransceiverEndsIncomingVideoTrack) {
3772 RTCConfiguration config;
3773 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
3774 ConnectFakeSignaling();
3775 auto audio_transceiver_or_error =
3776 caller()->pc()->AddTransceiver(caller()->CreateLocalVideoTrack());
3777 ASSERT_TRUE(audio_transceiver_or_error.ok());
3778 auto audio_transceiver = audio_transceiver_or_error.MoveValue();
3779
3780 caller()->CreateAndSetAndSignalOffer();
3781 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3782 auto caller_track = audio_transceiver->receiver()->track();
3783 auto callee_track = callee()->pc()->GetReceivers()[0]->track();
3784 audio_transceiver->StopStandard();
3785 EXPECT_EQ(MediaStreamTrackInterface::TrackState::kEnded,
3786 caller_track->state());
3787 caller()->CreateAndSetAndSignalOffer();
3788 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3789 EXPECT_EQ(MediaStreamTrackInterface::TrackState::kEnded,
3790 callee_track->state());
3791}
3792
Harald Alvestrand89c40e22021-02-17 08:58:35 +00003793} // namespace
Harald Alvestrand39993842021-02-17 09:05:31 +00003794
Mirko Bonadeiab64e8a2018-12-12 12:10:18 +01003795} // namespace webrtc