blob: 86b96963f667fe473231329192c3da8882a6ebca [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 Alvestrandcae46562021-02-11 23:26:18 +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 Alvestrandcae46562021-02-11 23:26:18 +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 Alvestrandcae46562021-02-11 23:26:18 +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 Alvestrandcae46562021-02-11 23:26:18 +000029#include "api/media_types.h"
Steve Anton10542f22019-01-11 09:11:00 -080030#include "api/peer_connection_interface.h"
Harald Alvestrandcae46562021-02-11 23:26:18 +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 Alvestrandcae46562021-02-11 23:26:18 +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 Alvestrandcae46562021-02-11 23:26:18 +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 Alvestrandcae46562021-02-11 23:26:18 +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 Alvestrandcae46562021-02-11 23:26:18 +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 Alvestrandcae46562021-02-11 23:26:18 +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 Alvestrandcae46562021-02-11 23:26:18 +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 Alvestrandcae46562021-02-11 23:26:18 +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 Alvestrandcae46562021-02-11 23:26:18 +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 Alvestrandcae46562021-02-11 23:26:18 +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 Alvestrandcae46562021-02-11 23:26:18 +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 Alvestrandcae46562021-02-11 23:26:18 +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 Alvestrandcae46562021-02-11 23:26:18 +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 Alvestrandcae46562021-02-11 23:26:18 +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.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002300TEST_P(PeerConnectionIntegrationTest,
Steve Anton83119dd2017-11-10 16:19:52 -08002301 AddMediaToConnectedBundleDoesNotRestartIce) {
Steve Anton6f25b092017-10-23 09:39:20 -07002302 PeerConnectionInterface::RTCConfiguration config;
2303 config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
2304 config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyRequire;
2305 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(
2306 config, PeerConnectionInterface::RTCConfiguration()));
2307 ConnectFakeSignaling();
2308
Steve Anton15324772018-01-16 10:26:49 -08002309 caller()->AddAudioTrack();
Steve Anton6f25b092017-10-23 09:39:20 -07002310 caller()->CreateAndSetAndSignalOffer();
2311 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Steve Antonff52f1b2017-10-26 12:24:50 -07002312 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceConnectionCompleted,
2313 caller()->ice_connection_state(), kDefaultTimeout);
Steve Anton6f25b092017-10-23 09:39:20 -07002314
2315 caller()->clear_ice_connection_state_history();
2316
Steve Anton15324772018-01-16 10:26:49 -08002317 caller()->AddVideoTrack();
Steve Anton6f25b092017-10-23 09:39:20 -07002318 caller()->CreateAndSetAndSignalOffer();
2319 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2320
2321 EXPECT_EQ(0u, caller()->ice_connection_state_history().size());
2322}
2323
deadbeef1dcb1642017-03-29 21:08:16 -07002324// This test sets up a call between two parties with audio and video. It then
2325// renegotiates setting the video m-line to "port 0", then later renegotiates
2326// again, enabling video.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002327TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07002328 VideoFlowsAfterMediaSectionIsRejectedAndRecycled) {
2329 ASSERT_TRUE(CreatePeerConnectionWrappers());
2330 ConnectFakeSignaling();
2331
2332 // Do initial negotiation, only sending media from the caller. Will result in
2333 // video and audio recvonly "m=" sections.
Steve Anton15324772018-01-16 10:26:49 -08002334 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002335 caller()->CreateAndSetAndSignalOffer();
2336 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2337
2338 // Negotiate again, disabling the video "m=" section (the callee will set the
2339 // port to 0 due to offer_to_receive_video = 0).
Seth Hampson2f0d7022018-02-20 11:54:42 -08002340 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2341 PeerConnectionInterface::RTCOfferAnswerOptions options;
2342 options.offer_to_receive_video = 0;
2343 callee()->SetOfferAnswerOptions(options);
2344 } else {
2345 callee()->SetRemoteOfferHandler([this] {
Harald Alvestrand6060df52020-08-11 09:54:02 +02002346 callee()
2347 ->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)
2348 ->StopInternal();
Seth Hampson2f0d7022018-02-20 11:54:42 -08002349 });
2350 }
deadbeef1dcb1642017-03-29 21:08:16 -07002351 caller()->CreateAndSetAndSignalOffer();
2352 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2353 // Sanity check that video "m=" section was actually rejected.
2354 const ContentInfo* answer_video_content = cricket::GetFirstVideoContent(
2355 callee()->pc()->local_description()->description());
2356 ASSERT_NE(nullptr, answer_video_content);
2357 ASSERT_TRUE(answer_video_content->rejected);
2358
2359 // Enable video and do negotiation again, making sure video is received
2360 // end-to-end, also adding media stream to callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002361 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2362 PeerConnectionInterface::RTCOfferAnswerOptions options;
2363 options.offer_to_receive_video = 1;
2364 callee()->SetOfferAnswerOptions(options);
2365 } else {
2366 // The caller's transceiver is stopped, so we need to add another track.
2367 auto caller_transceiver =
2368 caller()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO);
Harald Alvestrand6060df52020-08-11 09:54:02 +02002369 EXPECT_EQ(nullptr, caller_transceiver.get());
Seth Hampson2f0d7022018-02-20 11:54:42 -08002370 caller()->AddVideoTrack();
2371 }
2372 callee()->AddVideoTrack();
2373 callee()->SetRemoteOfferHandler(nullptr);
deadbeef1dcb1642017-03-29 21:08:16 -07002374 caller()->CreateAndSetAndSignalOffer();
2375 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002376
deadbeef1dcb1642017-03-29 21:08:16 -07002377 // Verify the caller receives frames from the newly added stream, and the
2378 // callee receives additional frames from the re-enabled video m= section.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002379 MediaExpectations media_expectations;
2380 media_expectations.CalleeExpectsSomeAudio();
2381 media_expectations.ExpectBidirectionalVideo();
2382 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002383}
2384
deadbeef1dcb1642017-03-29 21:08:16 -07002385// This tests that if we negotiate after calling CreateSender but before we
2386// have a track, then set a track later, frames from the newly-set track are
2387// received end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002388TEST_F(PeerConnectionIntegrationTestPlanB,
deadbeef1dcb1642017-03-29 21:08:16 -07002389 MediaFlowsAfterEarlyWarmupWithCreateSender) {
2390 ASSERT_TRUE(CreatePeerConnectionWrappers());
2391 ConnectFakeSignaling();
2392 auto caller_audio_sender =
2393 caller()->pc()->CreateSender("audio", "caller_stream");
2394 auto caller_video_sender =
2395 caller()->pc()->CreateSender("video", "caller_stream");
2396 auto callee_audio_sender =
2397 callee()->pc()->CreateSender("audio", "callee_stream");
2398 auto callee_video_sender =
2399 callee()->pc()->CreateSender("video", "callee_stream");
2400 caller()->CreateAndSetAndSignalOffer();
2401 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
2402 // Wait for ICE to complete, without any tracks being set.
2403 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
2404 caller()->ice_connection_state(), kMaxWaitForFramesMs);
2405 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
2406 callee()->ice_connection_state(), kMaxWaitForFramesMs);
2407 // Now set the tracks, and expect frames to immediately start flowing.
2408 EXPECT_TRUE(caller_audio_sender->SetTrack(caller()->CreateLocalAudioTrack()));
2409 EXPECT_TRUE(caller_video_sender->SetTrack(caller()->CreateLocalVideoTrack()));
2410 EXPECT_TRUE(callee_audio_sender->SetTrack(callee()->CreateLocalAudioTrack()));
2411 EXPECT_TRUE(callee_video_sender->SetTrack(callee()->CreateLocalVideoTrack()));
Seth Hampson2f0d7022018-02-20 11:54:42 -08002412 MediaExpectations media_expectations;
2413 media_expectations.ExpectBidirectionalAudioAndVideo();
2414 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2415}
2416
2417// This tests that if we negotiate after calling AddTransceiver but before we
2418// have a track, then set a track later, frames from the newly-set tracks are
2419// received end-to-end.
2420TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
2421 MediaFlowsAfterEarlyWarmupWithAddTransceiver) {
2422 ASSERT_TRUE(CreatePeerConnectionWrappers());
2423 ConnectFakeSignaling();
2424 auto audio_result = caller()->pc()->AddTransceiver(cricket::MEDIA_TYPE_AUDIO);
2425 ASSERT_EQ(RTCErrorType::NONE, audio_result.error().type());
2426 auto caller_audio_sender = audio_result.MoveValue()->sender();
2427 auto video_result = caller()->pc()->AddTransceiver(cricket::MEDIA_TYPE_VIDEO);
2428 ASSERT_EQ(RTCErrorType::NONE, video_result.error().type());
2429 auto caller_video_sender = video_result.MoveValue()->sender();
2430 callee()->SetRemoteOfferHandler([this] {
2431 ASSERT_EQ(2u, callee()->pc()->GetTransceivers().size());
Harald Alvestrand6060df52020-08-11 09:54:02 +02002432 callee()->pc()->GetTransceivers()[0]->SetDirectionWithError(
Seth Hampson2f0d7022018-02-20 11:54:42 -08002433 RtpTransceiverDirection::kSendRecv);
Harald Alvestrand6060df52020-08-11 09:54:02 +02002434 callee()->pc()->GetTransceivers()[1]->SetDirectionWithError(
Seth Hampson2f0d7022018-02-20 11:54:42 -08002435 RtpTransceiverDirection::kSendRecv);
2436 });
2437 caller()->CreateAndSetAndSignalOffer();
2438 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
2439 // Wait for ICE to complete, without any tracks being set.
2440 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
2441 caller()->ice_connection_state(), kMaxWaitForFramesMs);
2442 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
2443 callee()->ice_connection_state(), kMaxWaitForFramesMs);
2444 // Now set the tracks, and expect frames to immediately start flowing.
2445 auto callee_audio_sender = callee()->pc()->GetSenders()[0];
2446 auto callee_video_sender = callee()->pc()->GetSenders()[1];
2447 ASSERT_TRUE(caller_audio_sender->SetTrack(caller()->CreateLocalAudioTrack()));
2448 ASSERT_TRUE(caller_video_sender->SetTrack(caller()->CreateLocalVideoTrack()));
2449 ASSERT_TRUE(callee_audio_sender->SetTrack(callee()->CreateLocalAudioTrack()));
2450 ASSERT_TRUE(callee_video_sender->SetTrack(callee()->CreateLocalVideoTrack()));
2451 MediaExpectations media_expectations;
2452 media_expectations.ExpectBidirectionalAudioAndVideo();
2453 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002454}
2455
2456// This test verifies that a remote video track can be added via AddStream,
2457// and sent end-to-end. For this particular test, it's simply echoed back
2458// from the caller to the callee, rather than being forwarded to a third
2459// PeerConnection.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002460TEST_F(PeerConnectionIntegrationTestPlanB, CanSendRemoteVideoTrack) {
deadbeef1dcb1642017-03-29 21:08:16 -07002461 ASSERT_TRUE(CreatePeerConnectionWrappers());
2462 ConnectFakeSignaling();
2463 // Just send a video track from the caller.
Steve Anton15324772018-01-16 10:26:49 -08002464 caller()->AddVideoTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07002465 caller()->CreateAndSetAndSignalOffer();
2466 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02002467 ASSERT_EQ(1U, callee()->remote_streams()->count());
deadbeef1dcb1642017-03-29 21:08:16 -07002468
2469 // Echo the stream back, and do a new offer/anwer (initiated by callee this
2470 // time).
2471 callee()->pc()->AddStream(callee()->remote_streams()->at(0));
2472 callee()->CreateAndSetAndSignalOffer();
2473 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
2474
Seth Hampson2f0d7022018-02-20 11:54:42 -08002475 MediaExpectations media_expectations;
2476 media_expectations.ExpectBidirectionalVideo();
2477 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002478}
2479
Harald Alvestrandec23d6d2021-02-11 10:47:22 +00002480#if !defined(THREAD_SANITIZER)
2481// This test provokes TSAN errors. bugs.webrtc.org/11282
2482
deadbeef1dcb1642017-03-29 21:08:16 -07002483// Test that we achieve the expected end-to-end connection time, using a
2484// fake clock and simulated latency on the media and signaling paths.
2485// We use a TURN<->TURN connection because this is usually the quickest to
2486// set up initially, especially when we're confident the connection will work
2487// and can start sending media before we get a STUN response.
2488//
2489// With various optimizations enabled, here are the network delays we expect to
2490// be on the critical path:
2491// 1. 2 signaling trips: Signaling offer and offerer's TURN candidate, then
2492// signaling answer (with DTLS fingerprint).
2493// 2. 9 media hops: Rest of the DTLS handshake. 3 hops in each direction when
2494// using TURN<->TURN pair, and DTLS exchange is 4 packets,
2495// the first of which should have arrived before the answer.
Yves Gerey100fe632020-01-17 19:15:53 +01002496TEST_P(PeerConnectionIntegrationTestWithFakeClock,
2497 EndToEndConnectionTimeWithTurnTurnPair) {
deadbeef1dcb1642017-03-29 21:08:16 -07002498 static constexpr int media_hop_delay_ms = 50;
2499 static constexpr int signaling_trip_delay_ms = 500;
2500 // For explanation of these values, see comment above.
2501 static constexpr int required_media_hops = 9;
2502 static constexpr int required_signaling_trips = 2;
2503 // For internal delays (such as posting an event asychronously).
2504 static constexpr int allowed_internal_delay_ms = 20;
2505 static constexpr int total_connection_time_ms =
2506 media_hop_delay_ms * required_media_hops +
2507 signaling_trip_delay_ms * required_signaling_trips +
2508 allowed_internal_delay_ms;
2509
2510 static const rtc::SocketAddress turn_server_1_internal_address{"88.88.88.0",
2511 3478};
2512 static const rtc::SocketAddress turn_server_1_external_address{"88.88.88.1",
2513 0};
2514 static const rtc::SocketAddress turn_server_2_internal_address{"99.99.99.0",
2515 3478};
2516 static const rtc::SocketAddress turn_server_2_external_address{"99.99.99.1",
2517 0};
Seth Hampsonaed71642018-06-11 07:41:32 -07002518 cricket::TestTurnServer* turn_server_1 = CreateTurnServer(
2519 turn_server_1_internal_address, turn_server_1_external_address);
Jonas Orelandbdcee282017-10-10 14:01:40 +02002520
Seth Hampsonaed71642018-06-11 07:41:32 -07002521 cricket::TestTurnServer* turn_server_2 = CreateTurnServer(
2522 turn_server_2_internal_address, turn_server_2_external_address);
deadbeef1dcb1642017-03-29 21:08:16 -07002523 // Bypass permission check on received packets so media can be sent before
2524 // the candidate is signaled.
Seth Hampsonaed71642018-06-11 07:41:32 -07002525 network_thread()->Invoke<void>(RTC_FROM_HERE, [turn_server_1] {
2526 turn_server_1->set_enable_permission_checks(false);
2527 });
2528 network_thread()->Invoke<void>(RTC_FROM_HERE, [turn_server_2] {
2529 turn_server_2->set_enable_permission_checks(false);
2530 });
deadbeef1dcb1642017-03-29 21:08:16 -07002531
2532 PeerConnectionInterface::RTCConfiguration client_1_config;
2533 webrtc::PeerConnectionInterface::IceServer ice_server_1;
2534 ice_server_1.urls.push_back("turn:88.88.88.0:3478");
2535 ice_server_1.username = "test";
2536 ice_server_1.password = "test";
2537 client_1_config.servers.push_back(ice_server_1);
2538 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
2539 client_1_config.presume_writable_when_fully_relayed = true;
2540
2541 PeerConnectionInterface::RTCConfiguration client_2_config;
2542 webrtc::PeerConnectionInterface::IceServer ice_server_2;
2543 ice_server_2.urls.push_back("turn:99.99.99.0:3478");
2544 ice_server_2.username = "test";
2545 ice_server_2.password = "test";
2546 client_2_config.servers.push_back(ice_server_2);
2547 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
2548 client_2_config.presume_writable_when_fully_relayed = true;
2549
2550 ASSERT_TRUE(
2551 CreatePeerConnectionWrappersWithConfig(client_1_config, client_2_config));
2552 // Set up the simulated delays.
2553 SetSignalingDelayMs(signaling_trip_delay_ms);
2554 ConnectFakeSignaling();
2555 virtual_socket_server()->set_delay_mean(media_hop_delay_ms);
2556 virtual_socket_server()->UpdateDelayDistribution();
2557
2558 // Set "offer to receive audio/video" without adding any tracks, so we just
2559 // set up ICE/DTLS with no media.
2560 PeerConnectionInterface::RTCOfferAnswerOptions options;
2561 options.offer_to_receive_audio = 1;
2562 options.offer_to_receive_video = 1;
2563 caller()->SetOfferAnswerOptions(options);
2564 caller()->CreateAndSetAndSignalOffer();
deadbeef71452802017-05-07 17:21:01 -07002565 EXPECT_TRUE_SIMULATED_WAIT(DtlsConnected(), total_connection_time_ms,
Yves Gerey100fe632020-01-17 19:15:53 +01002566 FakeClock());
Seth Hampson1d4a76d2018-06-19 14:31:41 -07002567 // Closing the PeerConnections destroys the ports before the ScopedFakeClock.
2568 // If this is not done a DCHECK can be hit in ports.cc, because a large
2569 // negative number is calculated for the rtt due to the global clock changing.
Steve Antond91969e2019-05-30 12:27:03 -07002570 ClosePeerConnections();
deadbeef1dcb1642017-03-29 21:08:16 -07002571}
2572
Harald Alvestrandec23d6d2021-02-11 10:47:22 +00002573#endif // !defined(THREAD_SANITIZER)
2574
Jonas Orelandbdcee282017-10-10 14:01:40 +02002575// Verify that a TurnCustomizer passed in through RTCConfiguration
2576// is actually used by the underlying TURN candidate pair.
2577// Note that turnport_unittest.cc contains more detailed, lower-level tests.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002578TEST_P(PeerConnectionIntegrationTest, TurnCustomizerUsedForTurnConnections) {
Jonas Orelandbdcee282017-10-10 14:01:40 +02002579 static const rtc::SocketAddress turn_server_1_internal_address{"88.88.88.0",
2580 3478};
2581 static const rtc::SocketAddress turn_server_1_external_address{"88.88.88.1",
2582 0};
2583 static const rtc::SocketAddress turn_server_2_internal_address{"99.99.99.0",
2584 3478};
2585 static const rtc::SocketAddress turn_server_2_external_address{"99.99.99.1",
2586 0};
Seth Hampsonaed71642018-06-11 07:41:32 -07002587 CreateTurnServer(turn_server_1_internal_address,
2588 turn_server_1_external_address);
2589 CreateTurnServer(turn_server_2_internal_address,
2590 turn_server_2_external_address);
Jonas Orelandbdcee282017-10-10 14:01:40 +02002591
2592 PeerConnectionInterface::RTCConfiguration client_1_config;
2593 webrtc::PeerConnectionInterface::IceServer ice_server_1;
2594 ice_server_1.urls.push_back("turn:88.88.88.0:3478");
2595 ice_server_1.username = "test";
2596 ice_server_1.password = "test";
2597 client_1_config.servers.push_back(ice_server_1);
2598 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
Seth Hampsonaed71642018-06-11 07:41:32 -07002599 auto* customizer1 = CreateTurnCustomizer();
2600 client_1_config.turn_customizer = customizer1;
Jonas Orelandbdcee282017-10-10 14:01:40 +02002601
2602 PeerConnectionInterface::RTCConfiguration client_2_config;
2603 webrtc::PeerConnectionInterface::IceServer ice_server_2;
2604 ice_server_2.urls.push_back("turn:99.99.99.0:3478");
2605 ice_server_2.username = "test";
2606 ice_server_2.password = "test";
2607 client_2_config.servers.push_back(ice_server_2);
2608 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
Seth Hampsonaed71642018-06-11 07:41:32 -07002609 auto* customizer2 = CreateTurnCustomizer();
2610 client_2_config.turn_customizer = customizer2;
Jonas Orelandbdcee282017-10-10 14:01:40 +02002611
2612 ASSERT_TRUE(
2613 CreatePeerConnectionWrappersWithConfig(client_1_config, client_2_config));
2614 ConnectFakeSignaling();
2615
2616 // Set "offer to receive audio/video" without adding any tracks, so we just
2617 // set up ICE/DTLS with no media.
2618 PeerConnectionInterface::RTCOfferAnswerOptions options;
2619 options.offer_to_receive_audio = 1;
2620 options.offer_to_receive_video = 1;
2621 caller()->SetOfferAnswerOptions(options);
2622 caller()->CreateAndSetAndSignalOffer();
2623 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
2624
Seth Hampsonaed71642018-06-11 07:41:32 -07002625 ExpectTurnCustomizerCountersIncremented(customizer1);
2626 ExpectTurnCustomizerCountersIncremented(customizer2);
Jonas Orelandbdcee282017-10-10 14:01:40 +02002627}
2628
Benjamin Wright2d5f3cb2018-05-22 14:46:06 -07002629// Verifies that you can use TCP instead of UDP to connect to a TURN server and
2630// send media between the caller and the callee.
2631TEST_P(PeerConnectionIntegrationTest, TCPUsedForTurnConnections) {
2632 static const rtc::SocketAddress turn_server_internal_address{"88.88.88.0",
2633 3478};
2634 static const rtc::SocketAddress turn_server_external_address{"88.88.88.1", 0};
2635
2636 // Enable TCP for the fake turn server.
Seth Hampsonaed71642018-06-11 07:41:32 -07002637 CreateTurnServer(turn_server_internal_address, turn_server_external_address,
2638 cricket::PROTO_TCP);
Benjamin Wright2d5f3cb2018-05-22 14:46:06 -07002639
2640 webrtc::PeerConnectionInterface::IceServer ice_server;
2641 ice_server.urls.push_back("turn:88.88.88.0:3478?transport=tcp");
2642 ice_server.username = "test";
2643 ice_server.password = "test";
2644
2645 PeerConnectionInterface::RTCConfiguration client_1_config;
2646 client_1_config.servers.push_back(ice_server);
2647 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
2648
2649 PeerConnectionInterface::RTCConfiguration client_2_config;
2650 client_2_config.servers.push_back(ice_server);
2651 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
2652
2653 ASSERT_TRUE(
2654 CreatePeerConnectionWrappersWithConfig(client_1_config, client_2_config));
2655
2656 // Do normal offer/answer and wait for ICE to complete.
2657 ConnectFakeSignaling();
2658 caller()->AddAudioVideoTracks();
2659 callee()->AddAudioVideoTracks();
2660 caller()->CreateAndSetAndSignalOffer();
2661 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2662 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
2663 callee()->ice_connection_state(), kMaxWaitForFramesMs);
2664
2665 MediaExpectations media_expectations;
2666 media_expectations.ExpectBidirectionalAudioAndVideo();
2667 EXPECT_TRUE(ExpectNewFrames(media_expectations));
2668}
2669
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07002670// Verify that a SSLCertificateVerifier passed in through
2671// PeerConnectionDependencies is actually used by the underlying SSL
2672// implementation to determine whether a certificate presented by the TURN
2673// server is accepted by the client. Note that openssladapter_unittest.cc
2674// contains more detailed, lower-level tests.
2675TEST_P(PeerConnectionIntegrationTest,
2676 SSLCertificateVerifierUsedForTurnConnections) {
2677 static const rtc::SocketAddress turn_server_internal_address{"88.88.88.0",
2678 3478};
2679 static const rtc::SocketAddress turn_server_external_address{"88.88.88.1", 0};
2680
2681 // Enable TCP-TLS for the fake turn server. We need to pass in 88.88.88.0 so
2682 // that host name verification passes on the fake certificate.
Seth Hampsonaed71642018-06-11 07:41:32 -07002683 CreateTurnServer(turn_server_internal_address, turn_server_external_address,
2684 cricket::PROTO_TLS, "88.88.88.0");
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07002685
2686 webrtc::PeerConnectionInterface::IceServer ice_server;
2687 ice_server.urls.push_back("turns:88.88.88.0:3478?transport=tcp");
2688 ice_server.username = "test";
2689 ice_server.password = "test";
2690
2691 PeerConnectionInterface::RTCConfiguration client_1_config;
2692 client_1_config.servers.push_back(ice_server);
2693 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
2694
2695 PeerConnectionInterface::RTCConfiguration client_2_config;
2696 client_2_config.servers.push_back(ice_server);
2697 // Setting the type to kRelay forces the connection to go through a TURN
2698 // server.
2699 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
2700
2701 // Get a copy to the pointer so we can verify calls later.
2702 rtc::TestCertificateVerifier* client_1_cert_verifier =
2703 new rtc::TestCertificateVerifier();
2704 client_1_cert_verifier->verify_certificate_ = true;
2705 rtc::TestCertificateVerifier* client_2_cert_verifier =
2706 new rtc::TestCertificateVerifier();
2707 client_2_cert_verifier->verify_certificate_ = true;
2708
2709 // Create the dependencies with the test certificate verifier.
2710 webrtc::PeerConnectionDependencies client_1_deps(nullptr);
2711 client_1_deps.tls_cert_verifier =
2712 std::unique_ptr<rtc::TestCertificateVerifier>(client_1_cert_verifier);
2713 webrtc::PeerConnectionDependencies client_2_deps(nullptr);
2714 client_2_deps.tls_cert_verifier =
2715 std::unique_ptr<rtc::TestCertificateVerifier>(client_2_cert_verifier);
2716
2717 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndDeps(
2718 client_1_config, std::move(client_1_deps), client_2_config,
2719 std::move(client_2_deps)));
2720 ConnectFakeSignaling();
2721
2722 // Set "offer to receive audio/video" without adding any tracks, so we just
2723 // set up ICE/DTLS with no media.
2724 PeerConnectionInterface::RTCOfferAnswerOptions options;
2725 options.offer_to_receive_audio = 1;
2726 options.offer_to_receive_video = 1;
2727 caller()->SetOfferAnswerOptions(options);
2728 caller()->CreateAndSetAndSignalOffer();
2729 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
2730
2731 EXPECT_GT(client_1_cert_verifier->call_count_, 0u);
2732 EXPECT_GT(client_2_cert_verifier->call_count_, 0u);
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07002733}
2734
2735TEST_P(PeerConnectionIntegrationTest,
2736 SSLCertificateVerifierFailureUsedForTurnConnectionsFailsConnection) {
2737 static const rtc::SocketAddress turn_server_internal_address{"88.88.88.0",
2738 3478};
2739 static const rtc::SocketAddress turn_server_external_address{"88.88.88.1", 0};
2740
2741 // Enable TCP-TLS for the fake turn server. We need to pass in 88.88.88.0 so
2742 // that host name verification passes on the fake certificate.
Seth Hampsonaed71642018-06-11 07:41:32 -07002743 CreateTurnServer(turn_server_internal_address, turn_server_external_address,
2744 cricket::PROTO_TLS, "88.88.88.0");
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07002745
2746 webrtc::PeerConnectionInterface::IceServer ice_server;
2747 ice_server.urls.push_back("turns:88.88.88.0:3478?transport=tcp");
2748 ice_server.username = "test";
2749 ice_server.password = "test";
2750
2751 PeerConnectionInterface::RTCConfiguration client_1_config;
2752 client_1_config.servers.push_back(ice_server);
2753 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
2754
2755 PeerConnectionInterface::RTCConfiguration client_2_config;
2756 client_2_config.servers.push_back(ice_server);
2757 // Setting the type to kRelay forces the connection to go through a TURN
2758 // server.
2759 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
2760
2761 // Get a copy to the pointer so we can verify calls later.
2762 rtc::TestCertificateVerifier* client_1_cert_verifier =
2763 new rtc::TestCertificateVerifier();
2764 client_1_cert_verifier->verify_certificate_ = false;
2765 rtc::TestCertificateVerifier* client_2_cert_verifier =
2766 new rtc::TestCertificateVerifier();
2767 client_2_cert_verifier->verify_certificate_ = false;
2768
2769 // Create the dependencies with the test certificate verifier.
2770 webrtc::PeerConnectionDependencies client_1_deps(nullptr);
2771 client_1_deps.tls_cert_verifier =
2772 std::unique_ptr<rtc::TestCertificateVerifier>(client_1_cert_verifier);
2773 webrtc::PeerConnectionDependencies client_2_deps(nullptr);
2774 client_2_deps.tls_cert_verifier =
2775 std::unique_ptr<rtc::TestCertificateVerifier>(client_2_cert_verifier);
2776
2777 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndDeps(
2778 client_1_config, std::move(client_1_deps), client_2_config,
2779 std::move(client_2_deps)));
2780 ConnectFakeSignaling();
2781
2782 // Set "offer to receive audio/video" without adding any tracks, so we just
2783 // set up ICE/DTLS with no media.
2784 PeerConnectionInterface::RTCOfferAnswerOptions options;
2785 options.offer_to_receive_audio = 1;
2786 options.offer_to_receive_video = 1;
2787 caller()->SetOfferAnswerOptions(options);
2788 caller()->CreateAndSetAndSignalOffer();
2789 bool wait_res = true;
2790 // TODO(bugs.webrtc.org/9219): When IceConnectionState is implemented
2791 // properly, should be able to just wait for a state of "failed" instead of
2792 // waiting a fixed 10 seconds.
2793 WAIT_(DtlsConnected(), kDefaultTimeout, wait_res);
2794 ASSERT_FALSE(wait_res);
2795
2796 EXPECT_GT(client_1_cert_verifier->call_count_, 0u);
2797 EXPECT_GT(client_2_cert_verifier->call_count_, 0u);
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07002798}
2799
Qingsi Wang25ec8882019-11-15 12:33:05 -08002800// Test that the injected ICE transport factory is used to create ICE transports
2801// for WebRTC connections.
2802TEST_P(PeerConnectionIntegrationTest, IceTransportFactoryUsedForConnections) {
2803 PeerConnectionInterface::RTCConfiguration default_config;
2804 PeerConnectionDependencies dependencies(nullptr);
2805 auto ice_transport_factory = std::make_unique<MockIceTransportFactory>();
2806 EXPECT_CALL(*ice_transport_factory, RecordIceTransportCreated()).Times(1);
2807 dependencies.ice_transport_factory = std::move(ice_transport_factory);
Niels Möller2a707032020-06-16 16:39:13 +02002808 auto wrapper = CreatePeerConnectionWrapper("Caller", nullptr, &default_config,
2809 std::move(dependencies), nullptr,
2810 /*reset_encoder_factory=*/false,
2811 /*reset_decoder_factory=*/false);
Qingsi Wang25ec8882019-11-15 12:33:05 -08002812 ASSERT_TRUE(wrapper);
2813 wrapper->CreateDataChannel();
2814 rtc::scoped_refptr<MockSetSessionDescriptionObserver> observer(
2815 new rtc::RefCountedObject<MockSetSessionDescriptionObserver>());
2816 wrapper->pc()->SetLocalDescription(observer,
2817 wrapper->CreateOfferAndWait().release());
2818}
2819
deadbeefc964d0b2017-04-03 10:03:35 -07002820// Test that audio and video flow end-to-end when codec names don't use the
2821// expected casing, given that they're supposed to be case insensitive. To test
2822// this, all but one codec is removed from each media description, and its
2823// casing is changed.
2824//
2825// In the past, this has regressed and caused crashes/black video, due to the
2826// fact that code at some layers was doing case-insensitive comparisons and
2827// code at other layers was not.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002828TEST_P(PeerConnectionIntegrationTest, CodecNamesAreCaseInsensitive) {
deadbeefc964d0b2017-04-03 10:03:35 -07002829 ASSERT_TRUE(CreatePeerConnectionWrappers());
2830 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002831 caller()->AddAudioVideoTracks();
2832 callee()->AddAudioVideoTracks();
deadbeefc964d0b2017-04-03 10:03:35 -07002833
2834 // Remove all but one audio/video codec (opus and VP8), and change the
2835 // casing of the caller's generated offer.
2836 caller()->SetGeneratedSdpMunger([](cricket::SessionDescription* description) {
2837 cricket::AudioContentDescription* audio =
2838 GetFirstAudioContentDescription(description);
2839 ASSERT_NE(nullptr, audio);
2840 auto audio_codecs = audio->codecs();
2841 audio_codecs.erase(std::remove_if(audio_codecs.begin(), audio_codecs.end(),
2842 [](const cricket::AudioCodec& codec) {
2843 return codec.name != "opus";
2844 }),
2845 audio_codecs.end());
2846 ASSERT_EQ(1u, audio_codecs.size());
2847 audio_codecs[0].name = "OpUs";
2848 audio->set_codecs(audio_codecs);
2849
2850 cricket::VideoContentDescription* video =
2851 GetFirstVideoContentDescription(description);
2852 ASSERT_NE(nullptr, video);
2853 auto video_codecs = video->codecs();
2854 video_codecs.erase(std::remove_if(video_codecs.begin(), video_codecs.end(),
2855 [](const cricket::VideoCodec& codec) {
2856 return codec.name != "VP8";
2857 }),
2858 video_codecs.end());
2859 ASSERT_EQ(1u, video_codecs.size());
2860 video_codecs[0].name = "vP8";
2861 video->set_codecs(video_codecs);
2862 });
2863
2864 caller()->CreateAndSetAndSignalOffer();
2865 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2866
2867 // Verify frames are still received end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002868 MediaExpectations media_expectations;
2869 media_expectations.ExpectBidirectionalAudioAndVideo();
2870 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeefc964d0b2017-04-03 10:03:35 -07002871}
2872
Jonas Oreland49ac5952018-09-26 16:04:32 +02002873TEST_P(PeerConnectionIntegrationTest, GetSourcesAudio) {
hbos8d609f62017-04-10 07:39:05 -07002874 ASSERT_TRUE(CreatePeerConnectionWrappers());
2875 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002876 caller()->AddAudioTrack();
hbos8d609f62017-04-10 07:39:05 -07002877 caller()->CreateAndSetAndSignalOffer();
2878 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
deadbeefd8ad7882017-04-18 16:01:17 -07002879 // Wait for one audio frame to be received by the callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002880 MediaExpectations media_expectations;
2881 media_expectations.CalleeExpectsSomeAudio(1);
2882 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Jonas Oreland49ac5952018-09-26 16:04:32 +02002883 ASSERT_EQ(callee()->pc()->GetReceivers().size(), 1u);
hbos8d609f62017-04-10 07:39:05 -07002884 auto receiver = callee()->pc()->GetReceivers()[0];
2885 ASSERT_EQ(receiver->media_type(), cricket::MEDIA_TYPE_AUDIO);
Jonas Oreland49ac5952018-09-26 16:04:32 +02002886 auto sources = receiver->GetSources();
hbos8d609f62017-04-10 07:39:05 -07002887 ASSERT_GT(receiver->GetParameters().encodings.size(), 0u);
2888 EXPECT_EQ(receiver->GetParameters().encodings[0].ssrc,
Jonas Oreland49ac5952018-09-26 16:04:32 +02002889 sources[0].source_id());
2890 EXPECT_EQ(webrtc::RtpSourceType::SSRC, sources[0].source_type());
2891}
2892
2893TEST_P(PeerConnectionIntegrationTest, GetSourcesVideo) {
2894 ASSERT_TRUE(CreatePeerConnectionWrappers());
2895 ConnectFakeSignaling();
2896 caller()->AddVideoTrack();
2897 caller()->CreateAndSetAndSignalOffer();
2898 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2899 // Wait for one video frame to be received by the callee.
2900 MediaExpectations media_expectations;
2901 media_expectations.CalleeExpectsSomeVideo(1);
2902 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2903 ASSERT_EQ(callee()->pc()->GetReceivers().size(), 1u);
2904 auto receiver = callee()->pc()->GetReceivers()[0];
2905 ASSERT_EQ(receiver->media_type(), cricket::MEDIA_TYPE_VIDEO);
2906 auto sources = receiver->GetSources();
2907 ASSERT_GT(receiver->GetParameters().encodings.size(), 0u);
Yves Gereyf781bb52019-07-23 19:15:39 +02002908 ASSERT_GT(sources.size(), 0u);
Jonas Oreland49ac5952018-09-26 16:04:32 +02002909 EXPECT_EQ(receiver->GetParameters().encodings[0].ssrc,
2910 sources[0].source_id());
2911 EXPECT_EQ(webrtc::RtpSourceType::SSRC, sources[0].source_type());
hbos8d609f62017-04-10 07:39:05 -07002912}
2913
deadbeef2f425aa2017-04-14 10:41:32 -07002914// Test that if a track is removed and added again with a different stream ID,
2915// the new stream ID is successfully communicated in SDP and media continues to
2916// flow end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002917// TODO(webrtc.bugs.org/8734): This test does not work for Unified Plan because
2918// it will not reuse a transceiver that has already been sending. After creating
2919// a new transceiver it tries to create an offer with two senders of the same
2920// track ids and it fails.
2921TEST_F(PeerConnectionIntegrationTestPlanB, RemoveAndAddTrackWithNewStreamId) {
deadbeef2f425aa2017-04-14 10:41:32 -07002922 ASSERT_TRUE(CreatePeerConnectionWrappers());
2923 ConnectFakeSignaling();
2924
deadbeef2f425aa2017-04-14 10:41:32 -07002925 // Add track using stream 1, do offer/answer.
2926 rtc::scoped_refptr<webrtc::AudioTrackInterface> track =
2927 caller()->CreateLocalAudioTrack();
2928 rtc::scoped_refptr<webrtc::RtpSenderInterface> sender =
Steve Antond78323f2018-07-11 11:13:44 -07002929 caller()->AddTrack(track, {"stream_1"});
deadbeef2f425aa2017-04-14 10:41:32 -07002930 caller()->CreateAndSetAndSignalOffer();
2931 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002932 {
2933 MediaExpectations media_expectations;
2934 media_expectations.CalleeExpectsSomeAudio(1);
2935 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2936 }
deadbeef2f425aa2017-04-14 10:41:32 -07002937 // Remove the sender, and create a new one with the new stream.
2938 caller()->pc()->RemoveTrack(sender);
Steve Antond78323f2018-07-11 11:13:44 -07002939 sender = caller()->AddTrack(track, {"stream_2"});
deadbeef2f425aa2017-04-14 10:41:32 -07002940 caller()->CreateAndSetAndSignalOffer();
2941 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2942 // Wait for additional audio frames to be received by the callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002943 {
2944 MediaExpectations media_expectations;
2945 media_expectations.CalleeExpectsSomeAudio();
2946 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2947 }
deadbeef2f425aa2017-04-14 10:41:32 -07002948}
2949
Seth Hampson2f0d7022018-02-20 11:54:42 -08002950TEST_P(PeerConnectionIntegrationTest, RtcEventLogOutputWriteCalled) {
Elad Alon99c3fe52017-10-13 16:29:40 +02002951 ASSERT_TRUE(CreatePeerConnectionWrappers());
2952 ConnectFakeSignaling();
2953
Mirko Bonadei317a1f02019-09-17 17:06:18 +02002954 auto output = std::make_unique<testing::NiceMock<MockRtcEventLogOutput>>();
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002955 ON_CALL(*output, IsActive()).WillByDefault(::testing::Return(true));
2956 ON_CALL(*output, Write(::testing::_)).WillByDefault(::testing::Return(true));
Elad Alon99c3fe52017-10-13 16:29:40 +02002957 EXPECT_CALL(*output, Write(::testing::_)).Times(::testing::AtLeast(1));
Bjorn Tereliusde939432017-11-20 17:38:14 +01002958 EXPECT_TRUE(caller()->pc()->StartRtcEventLog(
2959 std::move(output), webrtc::RtcEventLog::kImmediateOutput));
Elad Alon99c3fe52017-10-13 16:29:40 +02002960
Steve Anton15324772018-01-16 10:26:49 -08002961 caller()->AddAudioVideoTracks();
Elad Alon99c3fe52017-10-13 16:29:40 +02002962 caller()->CreateAndSetAndSignalOffer();
2963 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2964}
2965
Steve Antonede9ca52017-10-16 13:04:27 -07002966// Test that if candidates are only signaled by applying full session
2967// descriptions (instead of using AddIceCandidate), the peers can connect to
2968// each other and exchange media.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002969TEST_P(PeerConnectionIntegrationTest, MediaFlowsWhenCandidatesSetOnlyInSdp) {
Steve Antonede9ca52017-10-16 13:04:27 -07002970 ASSERT_TRUE(CreatePeerConnectionWrappers());
2971 // Each side will signal the session descriptions but not candidates.
2972 ConnectFakeSignalingForSdpOnly();
2973
2974 // Add audio video track and exchange the initial offer/answer with media
2975 // information only. This will start ICE gathering on each side.
Steve Anton15324772018-01-16 10:26:49 -08002976 caller()->AddAudioVideoTracks();
2977 callee()->AddAudioVideoTracks();
Steve Antonede9ca52017-10-16 13:04:27 -07002978 caller()->CreateAndSetAndSignalOffer();
2979
2980 // Wait for all candidates to be gathered on both the caller and callee.
2981 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceGatheringComplete,
2982 caller()->ice_gathering_state(), kDefaultTimeout);
2983 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceGatheringComplete,
2984 callee()->ice_gathering_state(), kDefaultTimeout);
2985
2986 // The candidates will now be included in the session description, so
2987 // signaling them will start the ICE connection.
2988 caller()->CreateAndSetAndSignalOffer();
2989 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2990
2991 // Ensure that media flows in both directions.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002992 MediaExpectations media_expectations;
2993 media_expectations.ExpectBidirectionalAudioAndVideo();
2994 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Antonede9ca52017-10-16 13:04:27 -07002995}
2996
Harald Alvestrandec23d6d2021-02-11 10:47:22 +00002997#if !defined(THREAD_SANITIZER)
2998// These tests provokes TSAN errors. See bugs.webrtc.org/11305.
2999
henrika5f6bf242017-11-01 11:06:56 +01003000// Test that SetAudioPlayout can be used to disable audio playout from the
3001// start, then later enable it. This may be useful, for example, if the caller
3002// needs to play a local ringtone until some event occurs, after which it
3003// switches to playing the received audio.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003004TEST_P(PeerConnectionIntegrationTest, DisableAndEnableAudioPlayout) {
henrika5f6bf242017-11-01 11:06:56 +01003005 ASSERT_TRUE(CreatePeerConnectionWrappers());
3006 ConnectFakeSignaling();
3007
3008 // Set up audio-only call where audio playout is disabled on caller's side.
3009 caller()->pc()->SetAudioPlayout(false);
Steve Anton15324772018-01-16 10:26:49 -08003010 caller()->AddAudioTrack();
3011 callee()->AddAudioTrack();
henrika5f6bf242017-11-01 11:06:56 +01003012 caller()->CreateAndSetAndSignalOffer();
3013 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3014
3015 // Pump messages for a second.
3016 WAIT(false, 1000);
3017 // Since audio playout is disabled, the caller shouldn't have received
3018 // anything (at the playout level, at least).
3019 EXPECT_EQ(0, caller()->audio_frames_received());
3020 // As a sanity check, make sure the callee (for which playout isn't disabled)
3021 // did still see frames on its audio level.
3022 ASSERT_GT(callee()->audio_frames_received(), 0);
3023
3024 // Enable playout again, and ensure audio starts flowing.
3025 caller()->pc()->SetAudioPlayout(true);
Seth Hampson2f0d7022018-02-20 11:54:42 -08003026 MediaExpectations media_expectations;
3027 media_expectations.ExpectBidirectionalAudio();
3028 ASSERT_TRUE(ExpectNewFrames(media_expectations));
henrika5f6bf242017-11-01 11:06:56 +01003029}
3030
Harald Alvestrandcae46562021-02-11 23:26:18 +00003031double GetAudioEnergyStat(PeerConnectionIntegrationWrapper* pc) {
henrika5f6bf242017-11-01 11:06:56 +01003032 auto report = pc->NewGetStats();
3033 auto track_stats_list =
3034 report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
3035 const webrtc::RTCMediaStreamTrackStats* remote_track_stats = nullptr;
3036 for (const auto* track_stats : track_stats_list) {
3037 if (track_stats->remote_source.is_defined() &&
3038 *track_stats->remote_source) {
3039 remote_track_stats = track_stats;
3040 break;
3041 }
3042 }
3043
3044 if (!remote_track_stats->total_audio_energy.is_defined()) {
3045 return 0.0;
3046 }
3047 return *remote_track_stats->total_audio_energy;
3048}
3049
3050// Test that if audio playout is disabled via the SetAudioPlayout() method, then
3051// incoming audio is still processed and statistics are generated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003052TEST_P(PeerConnectionIntegrationTest,
henrika5f6bf242017-11-01 11:06:56 +01003053 DisableAudioPlayoutStillGeneratesAudioStats) {
3054 ASSERT_TRUE(CreatePeerConnectionWrappers());
3055 ConnectFakeSignaling();
3056
3057 // Set up audio-only call where playout is disabled but audio-processing is
3058 // still active.
Steve Anton15324772018-01-16 10:26:49 -08003059 caller()->AddAudioTrack();
3060 callee()->AddAudioTrack();
henrika5f6bf242017-11-01 11:06:56 +01003061 caller()->pc()->SetAudioPlayout(false);
3062
3063 caller()->CreateAndSetAndSignalOffer();
3064 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3065
3066 // Wait for the callee to receive audio stats.
3067 EXPECT_TRUE_WAIT(GetAudioEnergyStat(caller()) > 0, kMaxWaitForFramesMs);
3068}
3069
Harald Alvestrandec23d6d2021-02-11 10:47:22 +00003070#endif // !defined(THREAD_SANITIZER)
3071
henrika4f167df2017-11-01 14:45:55 +01003072// Test that SetAudioRecording can be used to disable audio recording from the
3073// start, then later enable it. This may be useful, for example, if the caller
3074// wants to ensure that no audio resources are active before a certain state
3075// is reached.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003076TEST_P(PeerConnectionIntegrationTest, DisableAndEnableAudioRecording) {
henrika4f167df2017-11-01 14:45:55 +01003077 ASSERT_TRUE(CreatePeerConnectionWrappers());
3078 ConnectFakeSignaling();
3079
3080 // Set up audio-only call where audio recording is disabled on caller's side.
3081 caller()->pc()->SetAudioRecording(false);
Steve Anton15324772018-01-16 10:26:49 -08003082 caller()->AddAudioTrack();
3083 callee()->AddAudioTrack();
henrika4f167df2017-11-01 14:45:55 +01003084 caller()->CreateAndSetAndSignalOffer();
3085 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3086
3087 // Pump messages for a second.
3088 WAIT(false, 1000);
3089 // Since caller has disabled audio recording, the callee shouldn't have
3090 // received anything.
3091 EXPECT_EQ(0, callee()->audio_frames_received());
3092 // As a sanity check, make sure the caller did still see frames on its
3093 // audio level since audio recording is enabled on the calle side.
3094 ASSERT_GT(caller()->audio_frames_received(), 0);
3095
3096 // Enable audio recording again, and ensure audio starts flowing.
3097 caller()->pc()->SetAudioRecording(true);
Seth Hampson2f0d7022018-02-20 11:54:42 -08003098 MediaExpectations media_expectations;
3099 media_expectations.ExpectBidirectionalAudio();
3100 ASSERT_TRUE(ExpectNewFrames(media_expectations));
henrika4f167df2017-11-01 14:45:55 +01003101}
3102
Qingsi Wang7685e862018-06-11 20:15:46 -07003103TEST_P(PeerConnectionIntegrationTest,
3104 IceEventsGeneratedAndLoggedInRtcEventLog) {
3105 ASSERT_TRUE(CreatePeerConnectionWrappersWithFakeRtcEventLog());
3106 ConnectFakeSignaling();
3107 PeerConnectionInterface::RTCOfferAnswerOptions options;
3108 options.offer_to_receive_audio = 1;
3109 caller()->SetOfferAnswerOptions(options);
3110 caller()->CreateAndSetAndSignalOffer();
3111 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
3112 ASSERT_NE(nullptr, caller()->event_log_factory());
3113 ASSERT_NE(nullptr, callee()->event_log_factory());
3114 webrtc::FakeRtcEventLog* caller_event_log =
Danil Chapovalov4f281f12021-01-18 13:29:00 +01003115 caller()->event_log_factory()->last_log_created();
Qingsi Wang7685e862018-06-11 20:15:46 -07003116 webrtc::FakeRtcEventLog* callee_event_log =
Danil Chapovalov4f281f12021-01-18 13:29:00 +01003117 callee()->event_log_factory()->last_log_created();
Qingsi Wang7685e862018-06-11 20:15:46 -07003118 ASSERT_NE(nullptr, caller_event_log);
3119 ASSERT_NE(nullptr, callee_event_log);
3120 int caller_ice_config_count = caller_event_log->GetEventCount(
3121 webrtc::RtcEvent::Type::IceCandidatePairConfig);
3122 int caller_ice_event_count = caller_event_log->GetEventCount(
3123 webrtc::RtcEvent::Type::IceCandidatePairEvent);
3124 int callee_ice_config_count = callee_event_log->GetEventCount(
3125 webrtc::RtcEvent::Type::IceCandidatePairConfig);
3126 int callee_ice_event_count = callee_event_log->GetEventCount(
3127 webrtc::RtcEvent::Type::IceCandidatePairEvent);
3128 EXPECT_LT(0, caller_ice_config_count);
3129 EXPECT_LT(0, caller_ice_event_count);
3130 EXPECT_LT(0, callee_ice_config_count);
3131 EXPECT_LT(0, callee_ice_event_count);
3132}
3133
Qingsi Wangc129c352019-04-18 10:41:58 -07003134TEST_P(PeerConnectionIntegrationTest, RegatherAfterChangingIceTransportType) {
Qingsi Wangc129c352019-04-18 10:41:58 -07003135 static const rtc::SocketAddress turn_server_internal_address{"88.88.88.0",
3136 3478};
3137 static const rtc::SocketAddress turn_server_external_address{"88.88.88.1", 0};
3138
3139 CreateTurnServer(turn_server_internal_address, turn_server_external_address);
3140
3141 webrtc::PeerConnectionInterface::IceServer ice_server;
3142 ice_server.urls.push_back("turn:88.88.88.0:3478");
3143 ice_server.username = "test";
3144 ice_server.password = "test";
3145
3146 PeerConnectionInterface::RTCConfiguration caller_config;
3147 caller_config.servers.push_back(ice_server);
3148 caller_config.type = webrtc::PeerConnectionInterface::kRelay;
3149 caller_config.continual_gathering_policy = PeerConnection::GATHER_CONTINUALLY;
Qingsi Wang1fe119f2019-05-31 16:55:33 -07003150 caller_config.surface_ice_candidates_on_ice_transport_type_changed = true;
Qingsi Wangc129c352019-04-18 10:41:58 -07003151
3152 PeerConnectionInterface::RTCConfiguration callee_config;
3153 callee_config.servers.push_back(ice_server);
3154 callee_config.type = webrtc::PeerConnectionInterface::kRelay;
3155 callee_config.continual_gathering_policy = PeerConnection::GATHER_CONTINUALLY;
Qingsi Wang1fe119f2019-05-31 16:55:33 -07003156 callee_config.surface_ice_candidates_on_ice_transport_type_changed = true;
Qingsi Wangc129c352019-04-18 10:41:58 -07003157
3158 ASSERT_TRUE(
3159 CreatePeerConnectionWrappersWithConfig(caller_config, callee_config));
3160
3161 // Do normal offer/answer and wait for ICE to complete.
3162 ConnectFakeSignaling();
3163 caller()->AddAudioVideoTracks();
3164 callee()->AddAudioVideoTracks();
3165 caller()->CreateAndSetAndSignalOffer();
3166 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3167 // Since we are doing continual gathering, the ICE transport does not reach
3168 // kIceGatheringComplete (see
3169 // P2PTransportChannel::OnCandidatesAllocationDone), and consequently not
3170 // kIceConnectionComplete.
3171 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
3172 caller()->ice_connection_state(), kDefaultTimeout);
3173 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
3174 callee()->ice_connection_state(), kDefaultTimeout);
3175 // Note that we cannot use the metric
3176 // |WebRTC.PeerConnection.CandidatePairType_UDP| in this test since this
3177 // metric is only populated when we reach kIceConnectionComplete in the
3178 // current implementation.
3179 EXPECT_EQ(cricket::RELAY_PORT_TYPE,
3180 caller()->last_candidate_gathered().type());
3181 EXPECT_EQ(cricket::RELAY_PORT_TYPE,
3182 callee()->last_candidate_gathered().type());
3183
3184 // Loosen the caller's candidate filter.
3185 caller_config = caller()->pc()->GetConfiguration();
3186 caller_config.type = webrtc::PeerConnectionInterface::kAll;
3187 caller()->pc()->SetConfiguration(caller_config);
3188 // We should have gathered a new host candidate.
3189 EXPECT_EQ_WAIT(cricket::LOCAL_PORT_TYPE,
3190 caller()->last_candidate_gathered().type(), kDefaultTimeout);
3191
3192 // Loosen the callee's candidate filter.
3193 callee_config = callee()->pc()->GetConfiguration();
3194 callee_config.type = webrtc::PeerConnectionInterface::kAll;
3195 callee()->pc()->SetConfiguration(callee_config);
3196 EXPECT_EQ_WAIT(cricket::LOCAL_PORT_TYPE,
3197 callee()->last_candidate_gathered().type(), kDefaultTimeout);
Jonas Orelande3096512020-05-27 09:01:05 +02003198
3199 // Create an offer and verify that it does not contain an ICE restart (i.e new
3200 // ice credentials).
3201 std::string caller_ufrag_pre_offer = caller()
3202 ->pc()
3203 ->local_description()
3204 ->description()
3205 ->transport_infos()[0]
3206 .description.ice_ufrag;
3207 caller()->CreateAndSetAndSignalOffer();
3208 std::string caller_ufrag_post_offer = caller()
3209 ->pc()
3210 ->local_description()
3211 ->description()
3212 ->transport_infos()[0]
3213 .description.ice_ufrag;
3214 EXPECT_EQ(caller_ufrag_pre_offer, caller_ufrag_post_offer);
Qingsi Wangc129c352019-04-18 10:41:58 -07003215}
3216
Eldar Relloda13ea22019-06-01 12:23:43 +03003217TEST_P(PeerConnectionIntegrationTest, OnIceCandidateError) {
Eldar Relloda13ea22019-06-01 12:23:43 +03003218 static const rtc::SocketAddress turn_server_internal_address{"88.88.88.0",
3219 3478};
3220 static const rtc::SocketAddress turn_server_external_address{"88.88.88.1", 0};
3221
3222 CreateTurnServer(turn_server_internal_address, turn_server_external_address);
3223
3224 webrtc::PeerConnectionInterface::IceServer ice_server;
3225 ice_server.urls.push_back("turn:88.88.88.0:3478");
3226 ice_server.username = "test";
3227 ice_server.password = "123";
3228
3229 PeerConnectionInterface::RTCConfiguration caller_config;
3230 caller_config.servers.push_back(ice_server);
3231 caller_config.type = webrtc::PeerConnectionInterface::kRelay;
3232 caller_config.continual_gathering_policy = PeerConnection::GATHER_CONTINUALLY;
3233
3234 PeerConnectionInterface::RTCConfiguration callee_config;
3235 callee_config.servers.push_back(ice_server);
3236 callee_config.type = webrtc::PeerConnectionInterface::kRelay;
3237 callee_config.continual_gathering_policy = PeerConnection::GATHER_CONTINUALLY;
3238
3239 ASSERT_TRUE(
3240 CreatePeerConnectionWrappersWithConfig(caller_config, callee_config));
3241
3242 // Do normal offer/answer and wait for ICE to complete.
3243 ConnectFakeSignaling();
3244 caller()->AddAudioVideoTracks();
3245 callee()->AddAudioVideoTracks();
3246 caller()->CreateAndSetAndSignalOffer();
3247 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3248 EXPECT_EQ_WAIT(401, caller()->error_event().error_code, kDefaultTimeout);
3249 EXPECT_EQ("Unauthorized", caller()->error_event().error_text);
3250 EXPECT_EQ("turn:88.88.88.0:3478?transport=udp", caller()->error_event().url);
Eldar Rello0095d372019-12-02 22:22:07 +02003251 EXPECT_NE(caller()->error_event().address, "");
Eldar Relloda13ea22019-06-01 12:23:43 +03003252}
3253
Eldar Rellofa8019c2020-05-14 11:59:33 +03003254TEST_P(PeerConnectionIntegrationTest, OnIceCandidateErrorWithEmptyAddress) {
3255 webrtc::PeerConnectionInterface::IceServer ice_server;
3256 ice_server.urls.push_back("turn:127.0.0.1:3478?transport=tcp");
3257 ice_server.username = "test";
3258 ice_server.password = "test";
3259
3260 PeerConnectionInterface::RTCConfiguration caller_config;
3261 caller_config.servers.push_back(ice_server);
3262 caller_config.type = webrtc::PeerConnectionInterface::kRelay;
3263 caller_config.continual_gathering_policy = PeerConnection::GATHER_CONTINUALLY;
3264
3265 PeerConnectionInterface::RTCConfiguration callee_config;
3266 callee_config.servers.push_back(ice_server);
3267 callee_config.type = webrtc::PeerConnectionInterface::kRelay;
3268 callee_config.continual_gathering_policy = PeerConnection::GATHER_CONTINUALLY;
3269
3270 ASSERT_TRUE(
3271 CreatePeerConnectionWrappersWithConfig(caller_config, callee_config));
3272
3273 // Do normal offer/answer and wait for ICE to complete.
3274 ConnectFakeSignaling();
3275 caller()->AddAudioVideoTracks();
3276 callee()->AddAudioVideoTracks();
3277 caller()->CreateAndSetAndSignalOffer();
3278 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3279 EXPECT_EQ_WAIT(701, caller()->error_event().error_code, kDefaultTimeout);
3280 EXPECT_EQ(caller()->error_event().address, "");
3281}
3282
Eldar Rello5ab79e62019-10-09 18:29:44 +03003283TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
3284 AudioKeepsFlowingAfterImplicitRollback) {
3285 PeerConnectionInterface::RTCConfiguration config;
3286 config.sdp_semantics = SdpSemantics::kUnifiedPlan;
3287 config.enable_implicit_rollback = true;
3288 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
3289 ConnectFakeSignaling();
3290 caller()->AddAudioTrack();
3291 callee()->AddAudioTrack();
3292 caller()->CreateAndSetAndSignalOffer();
3293 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3294 MediaExpectations media_expectations;
3295 media_expectations.ExpectBidirectionalAudio();
3296 ASSERT_TRUE(ExpectNewFrames(media_expectations));
3297 SetSignalIceCandidates(false); // Workaround candidate outrace sdp.
3298 caller()->AddVideoTrack();
3299 callee()->AddVideoTrack();
3300 rtc::scoped_refptr<MockSetSessionDescriptionObserver> observer(
3301 new rtc::RefCountedObject<MockSetSessionDescriptionObserver>());
3302 callee()->pc()->SetLocalDescription(observer,
3303 callee()->CreateOfferAndWait().release());
3304 EXPECT_TRUE_WAIT(observer->called(), kDefaultTimeout);
3305 caller()->CreateAndSetAndSignalOffer(); // Implicit rollback.
3306 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3307 ASSERT_TRUE(ExpectNewFrames(media_expectations));
3308}
3309
3310TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
3311 ImplicitRollbackVisitsStableState) {
3312 RTCConfiguration config;
3313 config.sdp_semantics = SdpSemantics::kUnifiedPlan;
3314 config.enable_implicit_rollback = true;
3315
3316 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
3317
3318 rtc::scoped_refptr<MockSetSessionDescriptionObserver> sld_observer(
3319 new rtc::RefCountedObject<MockSetSessionDescriptionObserver>());
3320 callee()->pc()->SetLocalDescription(sld_observer,
3321 callee()->CreateOfferAndWait().release());
3322 EXPECT_TRUE_WAIT(sld_observer->called(), kDefaultTimeout);
3323 EXPECT_EQ(sld_observer->error(), "");
3324
3325 rtc::scoped_refptr<MockSetSessionDescriptionObserver> srd_observer(
3326 new rtc::RefCountedObject<MockSetSessionDescriptionObserver>());
3327 callee()->pc()->SetRemoteDescription(
3328 srd_observer, caller()->CreateOfferAndWait().release());
3329 EXPECT_TRUE_WAIT(srd_observer->called(), kDefaultTimeout);
3330 EXPECT_EQ(srd_observer->error(), "");
3331
3332 EXPECT_THAT(callee()->peer_connection_signaling_state_history(),
3333 ElementsAre(PeerConnectionInterface::kHaveLocalOffer,
3334 PeerConnectionInterface::kStable,
3335 PeerConnectionInterface::kHaveRemoteOffer));
3336}
3337
Eldar Rellobd9c33a2020-10-01 17:52:45 +03003338TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
3339 H264FmtpSpsPpsIdrInKeyframeParameterUsage) {
3340 ASSERT_TRUE(CreatePeerConnectionWrappers());
3341 ConnectFakeSignaling();
3342 caller()->AddVideoTrack();
3343 callee()->AddVideoTrack();
3344 auto munger = [](cricket::SessionDescription* desc) {
3345 cricket::VideoContentDescription* video =
3346 GetFirstVideoContentDescription(desc);
3347 auto codecs = video->codecs();
3348 for (auto&& codec : codecs) {
3349 if (codec.name == "H264") {
3350 std::string value;
3351 // The parameter is not supposed to be present in SDP by default.
3352 EXPECT_FALSE(
3353 codec.GetParam(cricket::kH264FmtpSpsPpsIdrInKeyframe, &value));
3354 codec.SetParam(std::string(cricket::kH264FmtpSpsPpsIdrInKeyframe),
3355 std::string(""));
3356 }
3357 }
3358 video->set_codecs(codecs);
3359 };
3360 // Munge local offer for SLD.
3361 caller()->SetGeneratedSdpMunger(munger);
3362 // Munge remote answer for SRD.
3363 caller()->SetReceivedSdpMunger(munger);
3364 caller()->CreateAndSetAndSignalOffer();
3365 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3366 // Observe that after munging the parameter is present in generated SDP.
3367 caller()->SetGeneratedSdpMunger([](cricket::SessionDescription* desc) {
3368 cricket::VideoContentDescription* video =
3369 GetFirstVideoContentDescription(desc);
3370 for (auto&& codec : video->codecs()) {
3371 if (codec.name == "H264") {
3372 std::string value;
3373 EXPECT_TRUE(
3374 codec.GetParam(cricket::kH264FmtpSpsPpsIdrInKeyframe, &value));
3375 }
3376 }
3377 });
3378 caller()->CreateOfferAndWait();
3379}
3380
Harald Alvestrand1a9be302020-12-11 14:53:59 +00003381TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
Harald Alvestrand94324f22021-01-13 12:31:53 +00003382 RenegotiateManyAudioTransceivers) {
Harald Alvestrand1a9be302020-12-11 14:53:59 +00003383 PeerConnectionInterface::RTCConfiguration config;
3384 config.sdp_semantics = SdpSemantics::kUnifiedPlan;
3385 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
3386 ConnectFakeSignaling();
3387 caller()->pc()->AddTransceiver(cricket::MEDIA_TYPE_AUDIO);
3388
3389 caller()->CreateAndSetAndSignalOffer();
3390 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3391 int current_size = caller()->pc()->GetTransceivers().size();
3392 // Add more tracks until we get close to having issues.
3393 // Issues have been seen at:
3394 // - 32 tracks on android_arm64_rel and android_arm_dbg bots
Harald Alvestrandcc6ae442021-01-18 08:06:23 +00003395 // - 16 tracks on android_arm_dbg (flaky)
3396 while (current_size < 8) {
Harald Alvestrand1a9be302020-12-11 14:53:59 +00003397 // Double the number of tracks
3398 for (int i = 0; i < current_size; i++) {
3399 caller()->pc()->AddTransceiver(cricket::MEDIA_TYPE_AUDIO);
3400 }
3401 current_size = caller()->pc()->GetTransceivers().size();
3402 RTC_LOG(LS_INFO) << "Renegotiating with " << current_size << " tracks";
3403 auto start_time_ms = rtc::TimeMillis();
3404 caller()->CreateAndSetAndSignalOffer();
3405 // We want to stop when the time exceeds one second.
3406 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3407 auto elapsed_time_ms = rtc::TimeMillis() - start_time_ms;
3408 RTC_LOG(LS_INFO) << "Renegotiating took " << elapsed_time_ms << " ms";
3409 ASSERT_GT(1000, elapsed_time_ms)
3410 << "Audio transceivers: Negotiation took too long after "
3411 << current_size << " tracks added";
3412 }
3413}
3414
3415TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
3416 RenegotiateManyVideoTransceivers) {
3417 PeerConnectionInterface::RTCConfiguration config;
3418 config.sdp_semantics = SdpSemantics::kUnifiedPlan;
3419 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
3420 ConnectFakeSignaling();
3421 caller()->pc()->AddTransceiver(cricket::MEDIA_TYPE_VIDEO);
3422
3423 caller()->CreateAndSetAndSignalOffer();
3424 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3425 int current_size = caller()->pc()->GetTransceivers().size();
3426 // Add more tracks until we get close to having issues.
3427 // Issues have been seen at:
3428 // - 96 on a Linux workstation
3429 // - 64 at win_x86_more_configs and win_x64_msvc_dbg
3430 // - 32 on android_arm64_rel and linux_dbg bots
3431 while (current_size < 16) {
3432 // Double the number of tracks
3433 for (int i = 0; i < current_size; i++) {
3434 caller()->pc()->AddTransceiver(cricket::MEDIA_TYPE_VIDEO);
3435 }
3436 current_size = caller()->pc()->GetTransceivers().size();
3437 RTC_LOG(LS_INFO) << "Renegotiating with " << current_size << " tracks";
3438 auto start_time_ms = rtc::TimeMillis();
3439 caller()->CreateAndSetAndSignalOffer();
3440 // We want to stop when the time exceeds one second.
3441 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3442 auto elapsed_time_ms = rtc::TimeMillis() - start_time_ms;
3443 RTC_LOG(LS_INFO) << "Renegotiating took " << elapsed_time_ms << " ms";
3444 ASSERT_GT(1000, elapsed_time_ms)
3445 << "Video transceivers: Negotiation took too long after "
3446 << current_size << " tracks added";
3447 }
3448}
3449
Harald Alvestrand94324f22021-01-13 12:31:53 +00003450TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
3451 RenegotiateManyVideoTransceiversAndWatchAudioDelay) {
3452 PeerConnectionInterface::RTCConfiguration config;
3453 config.sdp_semantics = SdpSemantics::kUnifiedPlan;
3454 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
3455 ConnectFakeSignaling();
3456 caller()->AddAudioTrack();
Harald Alvestrandcc6ae442021-01-18 08:06:23 +00003457 callee()->AddAudioTrack();
Harald Alvestrand94324f22021-01-13 12:31:53 +00003458 caller()->CreateAndSetAndSignalOffer();
3459 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3460 // Wait until we can see the audio flowing.
3461 MediaExpectations media_expectations;
3462 media_expectations.CalleeExpectsSomeAudio();
3463 ASSERT_TRUE(ExpectNewFrames(media_expectations));
3464
Harald Alvestrandcc6ae442021-01-18 08:06:23 +00003465 // Get the baseline numbers for audio_packets and audio_delay
3466 // in both directions.
3467 caller()->StartWatchingDelayStats();
3468 callee()->StartWatchingDelayStats();
Harald Alvestrand94324f22021-01-13 12:31:53 +00003469
3470 int current_size = caller()->pc()->GetTransceivers().size();
3471 // Add more tracks until we get close to having issues.
3472 // Making this number very large makes the test very slow.
Harald Alvestrandcc6ae442021-01-18 08:06:23 +00003473 while (current_size < 16) {
Harald Alvestrand94324f22021-01-13 12:31:53 +00003474 // Double the number of tracks
3475 for (int i = 0; i < current_size; i++) {
3476 caller()->pc()->AddTransceiver(cricket::MEDIA_TYPE_VIDEO);
3477 }
3478 current_size = caller()->pc()->GetTransceivers().size();
3479 RTC_LOG(LS_INFO) << "Renegotiating with " << current_size << " tracks";
3480 auto start_time_ms = rtc::TimeMillis();
3481 caller()->CreateAndSetAndSignalOffer();
3482 // We want to stop when the time exceeds one second.
3483 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3484 auto elapsed_time_ms = rtc::TimeMillis() - start_time_ms;
3485 RTC_LOG(LS_INFO) << "Renegotiating took " << elapsed_time_ms << " ms";
3486 // This is a guard against the test using excessive amounts of time.
3487 ASSERT_GT(5000, elapsed_time_ms)
3488 << "Video transceivers: Negotiation took too long after "
3489 << current_size << " tracks added";
Harald Alvestrandcc6ae442021-01-18 08:06:23 +00003490 caller()->UpdateDelayStats("caller reception", current_size);
3491 callee()->UpdateDelayStats("callee reception", current_size);
Harald Alvestrand94324f22021-01-13 12:31:53 +00003492 }
3493}
3494
Mirko Bonadeic84f6612019-01-31 12:20:57 +01003495INSTANTIATE_TEST_SUITE_P(PeerConnectionIntegrationTest,
3496 PeerConnectionIntegrationTest,
3497 Values(SdpSemantics::kPlanB,
3498 SdpSemantics::kUnifiedPlan));
Steve Antond3679212018-01-17 17:41:02 -08003499
Yves Gerey100fe632020-01-17 19:15:53 +01003500INSTANTIATE_TEST_SUITE_P(PeerConnectionIntegrationTest,
3501 PeerConnectionIntegrationTestWithFakeClock,
3502 Values(SdpSemantics::kPlanB,
3503 SdpSemantics::kUnifiedPlan));
3504
Steve Anton74255ff2018-01-24 18:32:57 -08003505// Tests that verify interoperability between Plan B and Unified Plan
3506// PeerConnections.
3507class PeerConnectionIntegrationInteropTest
Seth Hampson2f0d7022018-02-20 11:54:42 -08003508 : public PeerConnectionIntegrationBaseTest,
Steve Anton74255ff2018-01-24 18:32:57 -08003509 public ::testing::WithParamInterface<
3510 std::tuple<SdpSemantics, SdpSemantics>> {
3511 protected:
Seth Hampson2f0d7022018-02-20 11:54:42 -08003512 // Setting the SdpSemantics for the base test to kDefault does not matter
3513 // because we specify not to use the test semantics when creating
Harald Alvestrandcae46562021-02-11 23:26:18 +00003514 // PeerConnectionIntegrationWrappers.
Steve Anton74255ff2018-01-24 18:32:57 -08003515 PeerConnectionIntegrationInteropTest()
Steve Anton3acffc32018-04-12 17:21:03 -07003516 : PeerConnectionIntegrationBaseTest(SdpSemantics::kPlanB),
Seth Hampson2f0d7022018-02-20 11:54:42 -08003517 caller_semantics_(std::get<0>(GetParam())),
Steve Anton74255ff2018-01-24 18:32:57 -08003518 callee_semantics_(std::get<1>(GetParam())) {}
3519
3520 bool CreatePeerConnectionWrappersWithSemantics() {
Steve Anton3acffc32018-04-12 17:21:03 -07003521 return CreatePeerConnectionWrappersWithSdpSemantics(caller_semantics_,
3522 callee_semantics_);
Steve Anton74255ff2018-01-24 18:32:57 -08003523 }
3524
3525 const SdpSemantics caller_semantics_;
3526 const SdpSemantics callee_semantics_;
3527};
3528
3529TEST_P(PeerConnectionIntegrationInteropTest, NoMediaLocalToNoMediaRemote) {
3530 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
3531 ConnectFakeSignaling();
3532
3533 caller()->CreateAndSetAndSignalOffer();
3534 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3535}
3536
3537TEST_P(PeerConnectionIntegrationInteropTest, OneAudioLocalToNoMediaRemote) {
3538 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
3539 ConnectFakeSignaling();
3540 auto audio_sender = caller()->AddAudioTrack();
3541
3542 caller()->CreateAndSetAndSignalOffer();
3543 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3544
3545 // Verify that one audio receiver has been created on the remote and that it
3546 // has the same track ID as the sending track.
3547 auto receivers = callee()->pc()->GetReceivers();
3548 ASSERT_EQ(1u, receivers.size());
3549 EXPECT_EQ(cricket::MEDIA_TYPE_AUDIO, receivers[0]->media_type());
3550 EXPECT_EQ(receivers[0]->track()->id(), audio_sender->track()->id());
3551
Seth Hampson2f0d7022018-02-20 11:54:42 -08003552 MediaExpectations media_expectations;
3553 media_expectations.CalleeExpectsSomeAudio();
3554 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08003555}
3556
3557TEST_P(PeerConnectionIntegrationInteropTest, OneAudioOneVideoToNoMediaRemote) {
3558 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
3559 ConnectFakeSignaling();
3560 auto video_sender = caller()->AddVideoTrack();
3561 auto audio_sender = caller()->AddAudioTrack();
3562
3563 caller()->CreateAndSetAndSignalOffer();
3564 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3565
3566 // Verify that one audio and one video receiver have been created on the
3567 // remote and that they have the same track IDs as the sending tracks.
3568 auto audio_receivers =
3569 callee()->GetReceiversOfType(cricket::MEDIA_TYPE_AUDIO);
3570 ASSERT_EQ(1u, audio_receivers.size());
3571 EXPECT_EQ(audio_receivers[0]->track()->id(), audio_sender->track()->id());
3572 auto video_receivers =
3573 callee()->GetReceiversOfType(cricket::MEDIA_TYPE_VIDEO);
3574 ASSERT_EQ(1u, video_receivers.size());
3575 EXPECT_EQ(video_receivers[0]->track()->id(), video_sender->track()->id());
3576
Seth Hampson2f0d7022018-02-20 11:54:42 -08003577 MediaExpectations media_expectations;
3578 media_expectations.CalleeExpectsSomeAudioAndVideo();
3579 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08003580}
3581
3582TEST_P(PeerConnectionIntegrationInteropTest,
3583 OneAudioOneVideoLocalToOneAudioOneVideoRemote) {
3584 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
3585 ConnectFakeSignaling();
3586 caller()->AddAudioVideoTracks();
3587 callee()->AddAudioVideoTracks();
3588
3589 caller()->CreateAndSetAndSignalOffer();
3590 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3591
Seth Hampson2f0d7022018-02-20 11:54:42 -08003592 MediaExpectations media_expectations;
3593 media_expectations.ExpectBidirectionalAudioAndVideo();
3594 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08003595}
3596
3597TEST_P(PeerConnectionIntegrationInteropTest,
3598 ReverseRolesOneAudioLocalToOneVideoRemote) {
3599 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
3600 ConnectFakeSignaling();
3601 caller()->AddAudioTrack();
3602 callee()->AddVideoTrack();
3603
3604 caller()->CreateAndSetAndSignalOffer();
3605 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3606
3607 // Verify that only the audio track has been negotiated.
3608 EXPECT_EQ(0u, caller()->GetReceiversOfType(cricket::MEDIA_TYPE_VIDEO).size());
3609 // Might also check that the callee's NegotiationNeeded flag is set.
3610
3611 // Reverse roles.
3612 callee()->CreateAndSetAndSignalOffer();
3613 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3614
Seth Hampson2f0d7022018-02-20 11:54:42 -08003615 MediaExpectations media_expectations;
3616 media_expectations.CallerExpectsSomeVideo();
3617 media_expectations.CalleeExpectsSomeAudio();
3618 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08003619}
3620
Mirko Bonadeic84f6612019-01-31 12:20:57 +01003621INSTANTIATE_TEST_SUITE_P(
Steve Antonba42e992018-04-09 14:10:01 -07003622 PeerConnectionIntegrationTest,
3623 PeerConnectionIntegrationInteropTest,
3624 Values(std::make_tuple(SdpSemantics::kPlanB, SdpSemantics::kUnifiedPlan),
3625 std::make_tuple(SdpSemantics::kUnifiedPlan, SdpSemantics::kPlanB)));
3626
3627// Test that if the Unified Plan side offers two video tracks then the Plan B
3628// side will only see the first one and ignore the second.
3629TEST_F(PeerConnectionIntegrationTestPlanB, TwoVideoUnifiedPlanToNoMediaPlanB) {
Steve Anton3acffc32018-04-12 17:21:03 -07003630 ASSERT_TRUE(CreatePeerConnectionWrappersWithSdpSemantics(
3631 SdpSemantics::kUnifiedPlan, SdpSemantics::kPlanB));
Steve Anton74255ff2018-01-24 18:32:57 -08003632 ConnectFakeSignaling();
3633 auto first_sender = caller()->AddVideoTrack();
3634 caller()->AddVideoTrack();
3635
3636 caller()->CreateAndSetAndSignalOffer();
3637 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3638
3639 // Verify that there is only one receiver and it corresponds to the first
3640 // added track.
3641 auto receivers = callee()->pc()->GetReceivers();
3642 ASSERT_EQ(1u, receivers.size());
3643 EXPECT_TRUE(receivers[0]->track()->enabled());
3644 EXPECT_EQ(first_sender->track()->id(), receivers[0]->track()->id());
3645
Seth Hampson2f0d7022018-02-20 11:54:42 -08003646 MediaExpectations media_expectations;
3647 media_expectations.CalleeExpectsSomeVideo();
3648 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08003649}
3650
Steve Anton2bed3972019-01-04 17:04:30 -08003651// Test that if the initial offer tagged BUNDLE section is rejected due to its
3652// associated RtpTransceiver being stopped and another transceiver is added,
3653// then renegotiation causes the callee to receive the new video track without
3654// error.
3655// This is a regression test for bugs.webrtc.org/9954
3656TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
3657 ReOfferWithStoppedBundleTaggedTransceiver) {
3658 RTCConfiguration config;
3659 config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
3660 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
3661 ConnectFakeSignaling();
3662 auto audio_transceiver_or_error =
3663 caller()->pc()->AddTransceiver(caller()->CreateLocalAudioTrack());
3664 ASSERT_TRUE(audio_transceiver_or_error.ok());
3665 auto audio_transceiver = audio_transceiver_or_error.MoveValue();
3666
3667 caller()->CreateAndSetAndSignalOffer();
3668 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3669 {
3670 MediaExpectations media_expectations;
3671 media_expectations.CalleeExpectsSomeAudio();
3672 ASSERT_TRUE(ExpectNewFrames(media_expectations));
3673 }
3674
Harald Alvestrand6060df52020-08-11 09:54:02 +02003675 audio_transceiver->StopInternal();
Steve Anton2bed3972019-01-04 17:04:30 -08003676 caller()->pc()->AddTransceiver(caller()->CreateLocalVideoTrack());
3677
3678 caller()->CreateAndSetAndSignalOffer();
3679 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3680 {
3681 MediaExpectations media_expectations;
3682 media_expectations.CalleeExpectsSomeVideo();
3683 ASSERT_TRUE(ExpectNewFrames(media_expectations));
3684 }
3685}
3686
Harald Alvestrandbedb6052020-08-20 14:50:10 +02003687TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
3688 StopTransceiverRemovesDtlsTransports) {
3689 RTCConfiguration config;
3690 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
3691 ConnectFakeSignaling();
3692 auto audio_transceiver_or_error =
3693 caller()->pc()->AddTransceiver(caller()->CreateLocalAudioTrack());
3694 ASSERT_TRUE(audio_transceiver_or_error.ok());
3695 auto audio_transceiver = audio_transceiver_or_error.MoveValue();
3696
3697 caller()->CreateAndSetAndSignalOffer();
3698 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3699
3700 audio_transceiver->StopStandard();
3701 caller()->CreateAndSetAndSignalOffer();
3702 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3703 ASSERT_EQ(0U, caller()->pc()->GetTransceivers().size());
3704 EXPECT_EQ(PeerConnectionInterface::kIceGatheringNew,
3705 caller()->pc()->ice_gathering_state());
3706 EXPECT_THAT(caller()->ice_gathering_state_history(),
3707 ElementsAre(PeerConnectionInterface::kIceGatheringGathering,
3708 PeerConnectionInterface::kIceGatheringComplete,
3709 PeerConnectionInterface::kIceGatheringNew));
3710}
3711
Harald Alvestrand1ee33252020-09-24 13:31:15 +00003712TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
Harald Alvestrand45be0a92020-09-30 06:55:23 +00003713 StopTransceiverStopsAndRemovesTransceivers) {
3714 RTCConfiguration config;
3715 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
3716 ConnectFakeSignaling();
3717 auto audio_transceiver_or_error =
3718 caller()->pc()->AddTransceiver(caller()->CreateLocalAudioTrack());
3719 ASSERT_TRUE(audio_transceiver_or_error.ok());
3720 auto caller_transceiver = audio_transceiver_or_error.MoveValue();
3721
3722 caller()->CreateAndSetAndSignalOffer();
3723 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3724 caller_transceiver->StopStandard();
3725
3726 auto callee_transceiver = callee()->pc()->GetTransceivers()[0];
3727 caller()->CreateAndSetAndSignalOffer();
3728 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3729 EXPECT_EQ(0U, caller()->pc()->GetTransceivers().size());
3730 EXPECT_EQ(0U, callee()->pc()->GetTransceivers().size());
3731 EXPECT_EQ(0U, caller()->pc()->GetSenders().size());
3732 EXPECT_EQ(0U, callee()->pc()->GetSenders().size());
3733 EXPECT_EQ(0U, caller()->pc()->GetReceivers().size());
3734 EXPECT_EQ(0U, callee()->pc()->GetReceivers().size());
3735 EXPECT_TRUE(caller_transceiver->stopped());
3736 EXPECT_TRUE(callee_transceiver->stopped());
3737}
3738
3739TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
Harald Alvestrand1ee33252020-09-24 13:31:15 +00003740 StopTransceiverEndsIncomingAudioTrack) {
3741 RTCConfiguration config;
3742 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
3743 ConnectFakeSignaling();
3744 auto audio_transceiver_or_error =
3745 caller()->pc()->AddTransceiver(caller()->CreateLocalAudioTrack());
3746 ASSERT_TRUE(audio_transceiver_or_error.ok());
3747 auto audio_transceiver = audio_transceiver_or_error.MoveValue();
3748
3749 caller()->CreateAndSetAndSignalOffer();
3750 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3751 auto caller_track = audio_transceiver->receiver()->track();
3752 auto callee_track = callee()->pc()->GetReceivers()[0]->track();
3753 audio_transceiver->StopStandard();
3754 EXPECT_EQ(MediaStreamTrackInterface::TrackState::kEnded,
3755 caller_track->state());
3756 caller()->CreateAndSetAndSignalOffer();
3757 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3758 EXPECT_EQ(MediaStreamTrackInterface::TrackState::kEnded,
3759 callee_track->state());
3760}
3761
3762TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
3763 StopTransceiverEndsIncomingVideoTrack) {
3764 RTCConfiguration config;
3765 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
3766 ConnectFakeSignaling();
3767 auto audio_transceiver_or_error =
3768 caller()->pc()->AddTransceiver(caller()->CreateLocalVideoTrack());
3769 ASSERT_TRUE(audio_transceiver_or_error.ok());
3770 auto audio_transceiver = audio_transceiver_or_error.MoveValue();
3771
3772 caller()->CreateAndSetAndSignalOffer();
3773 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3774 auto caller_track = audio_transceiver->receiver()->track();
3775 auto callee_track = callee()->pc()->GetReceivers()[0]->track();
3776 audio_transceiver->StopStandard();
3777 EXPECT_EQ(MediaStreamTrackInterface::TrackState::kEnded,
3778 caller_track->state());
3779 caller()->CreateAndSetAndSignalOffer();
3780 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3781 EXPECT_EQ(MediaStreamTrackInterface::TrackState::kEnded,
3782 callee_track->state());
3783}
3784
deadbeef1dcb1642017-03-29 21:08:16 -07003785} // namespace
Harald Alvestrandcae46562021-02-11 23:26:18 +00003786
Mirko Bonadeiab64e8a2018-12-12 12:10:18 +01003787} // namespace webrtc