xref: /aosp_15_r20/external/webrtc/pc/peer_connection_integrationtest.cc (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
2  *  Copyright 2012 The WebRTC project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 // Integration tests for PeerConnection.
12 // These tests exercise a full stack over a simulated network.
13 //
14 // NOTE: If your test takes a while (guideline: more than 5 seconds),
15 // do NOT add it here, but instead add it to the file
16 // slow_peer_connection_integrationtest.cc
17 
18 #include <stdint.h>
19 
20 #include <algorithm>
21 #include <memory>
22 #include <string>
23 #include <tuple>
24 #include <utility>
25 #include <vector>
26 
27 #include "absl/algorithm/container.h"
28 #include "absl/memory/memory.h"
29 #include "absl/strings/string_view.h"
30 #include "absl/types/optional.h"
31 #include "api/async_resolver_factory.h"
32 #include "api/candidate.h"
33 #include "api/crypto/crypto_options.h"
34 #include "api/dtmf_sender_interface.h"
35 #include "api/ice_transport_interface.h"
36 #include "api/jsep.h"
37 #include "api/media_stream_interface.h"
38 #include "api/media_types.h"
39 #include "api/peer_connection_interface.h"
40 #include "api/rtc_error.h"
41 #include "api/rtc_event_log/rtc_event.h"
42 #include "api/rtc_event_log/rtc_event_log.h"
43 #include "api/rtc_event_log_output.h"
44 #include "api/rtp_parameters.h"
45 #include "api/rtp_receiver_interface.h"
46 #include "api/rtp_sender_interface.h"
47 #include "api/rtp_transceiver_direction.h"
48 #include "api/rtp_transceiver_interface.h"
49 #include "api/scoped_refptr.h"
50 #include "api/stats/rtc_stats.h"
51 #include "api/stats/rtc_stats_report.h"
52 #include "api/stats/rtcstats_objects.h"
53 #include "api/test/mock_encoder_selector.h"
54 #include "api/transport/rtp/rtp_source.h"
55 #include "api/uma_metrics.h"
56 #include "api/units/time_delta.h"
57 #include "api/video/video_rotation.h"
58 #include "logging/rtc_event_log/fake_rtc_event_log.h"
59 #include "logging/rtc_event_log/fake_rtc_event_log_factory.h"
60 #include "media/base/codec.h"
61 #include "media/base/media_constants.h"
62 #include "media/base/stream_params.h"
63 #include "p2p/base/mock_async_resolver.h"
64 #include "p2p/base/port.h"
65 #include "p2p/base/port_allocator.h"
66 #include "p2p/base/port_interface.h"
67 #include "p2p/base/test_stun_server.h"
68 #include "p2p/base/test_turn_customizer.h"
69 #include "p2p/base/test_turn_server.h"
70 #include "p2p/base/transport_description.h"
71 #include "p2p/base/transport_info.h"
72 #include "pc/media_session.h"
73 #include "pc/peer_connection.h"
74 #include "pc/peer_connection_factory.h"
75 #include "pc/session_description.h"
76 #include "pc/test/fake_periodic_video_source.h"
77 #include "pc/test/integration_test_helpers.h"
78 #include "pc/test/mock_peer_connection_observers.h"
79 #include "rtc_base/fake_clock.h"
80 #include "rtc_base/fake_mdns_responder.h"
81 #include "rtc_base/fake_network.h"
82 #include "rtc_base/firewall_socket_server.h"
83 #include "rtc_base/gunit.h"
84 #include "rtc_base/helpers.h"
85 #include "rtc_base/logging.h"
86 #include "rtc_base/socket_address.h"
87 #include "rtc_base/ssl_certificate.h"
88 #include "rtc_base/ssl_fingerprint.h"
89 #include "rtc_base/ssl_identity.h"
90 #include "rtc_base/ssl_stream_adapter.h"
91 #include "rtc_base/task_queue_for_test.h"
92 #include "rtc_base/test_certificate_verifier.h"
93 #include "rtc_base/thread.h"
94 #include "rtc_base/time_utils.h"
95 #include "rtc_base/virtual_socket_server.h"
96 #include "system_wrappers/include/metrics.h"
97 #include "test/gmock.h"
98 #include "test/gtest.h"
99 
100 namespace webrtc {
101 
102 namespace {
103 
104 class PeerConnectionIntegrationTest
105     : public PeerConnectionIntegrationBaseTest,
106       public ::testing::WithParamInterface<
107           std::tuple<SdpSemantics, std::string>> {
108  protected:
PeerConnectionIntegrationTest()109   PeerConnectionIntegrationTest()
110       : PeerConnectionIntegrationBaseTest(std::get<0>(GetParam()),
111                                           std::get<1>(GetParam())) {}
112 };
113 
114 // Fake clock must be set before threads are started to prevent race on
115 // Set/GetClockForTesting().
116 // To achieve that, multiple inheritance is used as a mixin pattern
117 // where order of construction is finely controlled.
118 // This also ensures peerconnection is closed before switching back to non-fake
119 // clock, avoiding other races and DCHECK failures such as in rtp_sender.cc.
120 class FakeClockForTest : public rtc::ScopedFakeClock {
121  protected:
FakeClockForTest()122   FakeClockForTest() {
123     // Some things use a time of "0" as a special value, so we need to start out
124     // the fake clock at a nonzero time.
125     // TODO(deadbeef): Fix this.
126     AdvanceTime(webrtc::TimeDelta::Seconds(1));
127   }
128 
129   // Explicit handle.
FakeClock()130   ScopedFakeClock& FakeClock() { return *this; }
131 };
132 
133 // Ensure FakeClockForTest is constructed first (see class for rationale).
134 class PeerConnectionIntegrationTestWithFakeClock
135     : public FakeClockForTest,
136       public PeerConnectionIntegrationTest {};
137 
138 class PeerConnectionIntegrationTestPlanB
139     : public PeerConnectionIntegrationBaseTest {
140  protected:
PeerConnectionIntegrationTestPlanB()141   PeerConnectionIntegrationTestPlanB()
142       : PeerConnectionIntegrationBaseTest(SdpSemantics::kPlanB_DEPRECATED) {}
143 };
144 
145 class PeerConnectionIntegrationTestUnifiedPlan
146     : public PeerConnectionIntegrationBaseTest {
147  protected:
PeerConnectionIntegrationTestUnifiedPlan()148   PeerConnectionIntegrationTestUnifiedPlan()
149       : PeerConnectionIntegrationBaseTest(SdpSemantics::kUnifiedPlan) {}
150 };
151 
152 // Test the OnFirstPacketReceived callback from audio/video RtpReceivers.  This
153 // includes testing that the callback is invoked if an observer is connected
154 // after the first packet has already been received.
TEST_P(PeerConnectionIntegrationTest,RtpReceiverObserverOnFirstPacketReceived)155 TEST_P(PeerConnectionIntegrationTest,
156        RtpReceiverObserverOnFirstPacketReceived) {
157   ASSERT_TRUE(CreatePeerConnectionWrappers());
158   ConnectFakeSignaling();
159   caller()->AddAudioVideoTracks();
160   callee()->AddAudioVideoTracks();
161   // Start offer/answer exchange and wait for it to complete.
162   caller()->CreateAndSetAndSignalOffer();
163   ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
164   // Should be one receiver each for audio/video.
165   EXPECT_EQ(2U, caller()->rtp_receiver_observers().size());
166   EXPECT_EQ(2U, callee()->rtp_receiver_observers().size());
167   // Wait for all "first packet received" callbacks to be fired.
168   EXPECT_TRUE_WAIT(
169       absl::c_all_of(caller()->rtp_receiver_observers(),
170                      [](const std::unique_ptr<MockRtpReceiverObserver>& o) {
171                        return o->first_packet_received();
172                      }),
173       kMaxWaitForFramesMs);
174   EXPECT_TRUE_WAIT(
175       absl::c_all_of(callee()->rtp_receiver_observers(),
176                      [](const std::unique_ptr<MockRtpReceiverObserver>& o) {
177                        return o->first_packet_received();
178                      }),
179       kMaxWaitForFramesMs);
180   // If new observers are set after the first packet was already received, the
181   // callback should still be invoked.
182   caller()->ResetRtpReceiverObservers();
183   callee()->ResetRtpReceiverObservers();
184   EXPECT_EQ(2U, caller()->rtp_receiver_observers().size());
185   EXPECT_EQ(2U, callee()->rtp_receiver_observers().size());
186   EXPECT_TRUE(
187       absl::c_all_of(caller()->rtp_receiver_observers(),
188                      [](const std::unique_ptr<MockRtpReceiverObserver>& o) {
189                        return o->first_packet_received();
190                      }));
191   EXPECT_TRUE(
192       absl::c_all_of(callee()->rtp_receiver_observers(),
193                      [](const std::unique_ptr<MockRtpReceiverObserver>& o) {
194                        return o->first_packet_received();
195                      }));
196 }
197 
198 class DummyDtmfObserver : public DtmfSenderObserverInterface {
199  public:
DummyDtmfObserver()200   DummyDtmfObserver() : completed_(false) {}
201 
202   // Implements DtmfSenderObserverInterface.
OnToneChange(const std::string & tone)203   void OnToneChange(const std::string& tone) override {
204     tones_.push_back(tone);
205     if (tone.empty()) {
206       completed_ = true;
207     }
208   }
209 
tones() const210   const std::vector<std::string>& tones() const { return tones_; }
completed() const211   bool completed() const { return completed_; }
212 
213  private:
214   bool completed_;
215   std::vector<std::string> tones_;
216 };
217 
218 // Assumes `sender` already has an audio track added and the offer/answer
219 // exchange is done.
TestDtmfFromSenderToReceiver(PeerConnectionIntegrationWrapper * sender,PeerConnectionIntegrationWrapper * receiver)220 void TestDtmfFromSenderToReceiver(PeerConnectionIntegrationWrapper* sender,
221                                   PeerConnectionIntegrationWrapper* receiver) {
222   // We should be able to get a DTMF sender from the local sender.
223   rtc::scoped_refptr<DtmfSenderInterface> dtmf_sender =
224       sender->pc()->GetSenders().at(0)->GetDtmfSender();
225   ASSERT_TRUE(dtmf_sender);
226   DummyDtmfObserver observer;
227   dtmf_sender->RegisterObserver(&observer);
228 
229   // Test the DtmfSender object just created.
230   EXPECT_TRUE(dtmf_sender->CanInsertDtmf());
231   EXPECT_TRUE(dtmf_sender->InsertDtmf("1a", 100, 50));
232 
233   EXPECT_TRUE_WAIT(observer.completed(), kDefaultTimeout);
234   std::vector<std::string> tones = {"1", "a", ""};
235   EXPECT_EQ(tones, observer.tones());
236   dtmf_sender->UnregisterObserver();
237   // TODO(deadbeef): Verify the tones were actually received end-to-end.
238 }
239 
240 // Verifies the DtmfSenderObserver callbacks for a DtmfSender (one in each
241 // direction).
TEST_P(PeerConnectionIntegrationTest,DtmfSenderObserver)242 TEST_P(PeerConnectionIntegrationTest, DtmfSenderObserver) {
243   ASSERT_TRUE(CreatePeerConnectionWrappers());
244   ConnectFakeSignaling();
245   // Only need audio for DTMF.
246   caller()->AddAudioTrack();
247   callee()->AddAudioTrack();
248   caller()->CreateAndSetAndSignalOffer();
249   ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
250   // DTLS must finish before the DTMF sender can be used reliably.
251   ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
252   TestDtmfFromSenderToReceiver(caller(), callee());
253   TestDtmfFromSenderToReceiver(callee(), caller());
254 }
255 
256 // Basic end-to-end test, verifying media can be encoded/transmitted/decoded
257 // between two connections, using DTLS-SRTP.
TEST_P(PeerConnectionIntegrationTest,EndToEndCallWithDtls)258 TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithDtls) {
259   ASSERT_TRUE(CreatePeerConnectionWrappers());
260   ConnectFakeSignaling();
261 
262   // Do normal offer/answer and wait for some frames to be received in each
263   // direction.
264   caller()->AddAudioVideoTracks();
265   callee()->AddAudioVideoTracks();
266   caller()->CreateAndSetAndSignalOffer();
267   ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
268   MediaExpectations media_expectations;
269   media_expectations.ExpectBidirectionalAudioAndVideo();
270   ASSERT_TRUE(ExpectNewFrames(media_expectations));
271 }
272 
273 #if defined(WEBRTC_FUCHSIA)
274 // Uses SDES instead of DTLS for key agreement.
TEST_P(PeerConnectionIntegrationTest,EndToEndCallWithSdes)275 TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithSdes) {
276   PeerConnectionInterface::RTCConfiguration sdes_config;
277   sdes_config.enable_dtls_srtp.emplace(false);
278   ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(sdes_config, sdes_config));
279   ConnectFakeSignaling();
280 
281   // Do normal offer/answer and wait for some frames to be received in each
282   // direction.
283   caller()->AddAudioVideoTracks();
284   callee()->AddAudioVideoTracks();
285   caller()->CreateAndSetAndSignalOffer();
286   ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
287   MediaExpectations media_expectations;
288   media_expectations.ExpectBidirectionalAudioAndVideo();
289   ASSERT_TRUE(ExpectNewFrames(media_expectations));
290 }
291 #endif
292 
293 // Basic end-to-end test specifying the `enable_encrypted_rtp_header_extensions`
294 // option to offer encrypted versions of all header extensions alongside the
295 // unencrypted versions.
TEST_P(PeerConnectionIntegrationTest,EndToEndCallWithEncryptedRtpHeaderExtensions)296 TEST_P(PeerConnectionIntegrationTest,
297        EndToEndCallWithEncryptedRtpHeaderExtensions) {
298   CryptoOptions crypto_options;
299   crypto_options.srtp.enable_encrypted_rtp_header_extensions = true;
300   PeerConnectionInterface::RTCConfiguration config;
301   config.crypto_options = crypto_options;
302   // Note: This allows offering >14 RTP header extensions.
303   config.offer_extmap_allow_mixed = true;
304   ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
305   ConnectFakeSignaling();
306 
307   // Do normal offer/answer and wait for some frames to be received in each
308   // direction.
309   caller()->AddAudioVideoTracks();
310   callee()->AddAudioVideoTracks();
311   caller()->CreateAndSetAndSignalOffer();
312   ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
313   MediaExpectations media_expectations;
314   media_expectations.ExpectBidirectionalAudioAndVideo();
315   ASSERT_TRUE(ExpectNewFrames(media_expectations));
316 }
317 
318 // This test sets up a call between two parties with a source resolution of
319 // 1280x720 and verifies that a 16:9 aspect ratio is received.
TEST_P(PeerConnectionIntegrationTest,Send1280By720ResolutionAndReceive16To9AspectRatio)320 TEST_P(PeerConnectionIntegrationTest,
321        Send1280By720ResolutionAndReceive16To9AspectRatio) {
322   ASSERT_TRUE(CreatePeerConnectionWrappers());
323   ConnectFakeSignaling();
324 
325   // Add video tracks with 16:9 aspect ratio, size 1280 x 720.
326   webrtc::FakePeriodicVideoSource::Config config;
327   config.width = 1280;
328   config.height = 720;
329   config.timestamp_offset_ms = rtc::TimeMillis();
330   caller()->AddTrack(caller()->CreateLocalVideoTrackWithConfig(config));
331   callee()->AddTrack(callee()->CreateLocalVideoTrackWithConfig(config));
332 
333   // Do normal offer/answer and wait for at least one frame to be received in
334   // each direction.
335   caller()->CreateAndSetAndSignalOffer();
336   ASSERT_TRUE_WAIT(caller()->min_video_frames_received_per_track() > 0 &&
337                        callee()->min_video_frames_received_per_track() > 0,
338                    kMaxWaitForFramesMs);
339 
340   // Check rendered aspect ratio.
341   EXPECT_EQ(16.0 / 9, caller()->local_rendered_aspect_ratio());
342   EXPECT_EQ(16.0 / 9, caller()->rendered_aspect_ratio());
343   EXPECT_EQ(16.0 / 9, callee()->local_rendered_aspect_ratio());
344   EXPECT_EQ(16.0 / 9, callee()->rendered_aspect_ratio());
345 }
346 
347 // This test sets up an one-way call, with media only from caller to
348 // callee.
TEST_P(PeerConnectionIntegrationTest,OneWayMediaCall)349 TEST_P(PeerConnectionIntegrationTest, OneWayMediaCall) {
350   ASSERT_TRUE(CreatePeerConnectionWrappers());
351   ConnectFakeSignaling();
352   caller()->AddAudioVideoTracks();
353   caller()->CreateAndSetAndSignalOffer();
354   MediaExpectations media_expectations;
355   media_expectations.CalleeExpectsSomeAudioAndVideo();
356   media_expectations.CallerExpectsNoAudio();
357   media_expectations.CallerExpectsNoVideo();
358   ASSERT_TRUE(ExpectNewFrames(media_expectations));
359 }
360 
361 // Tests that send only works without the caller having a decoder factory and
362 // the callee having an encoder factory.
TEST_P(PeerConnectionIntegrationTest,EndToEndCallWithSendOnlyVideo)363 TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithSendOnlyVideo) {
364   ASSERT_TRUE(
365       CreateOneDirectionalPeerConnectionWrappers(/*caller_to_callee=*/true));
366   ConnectFakeSignaling();
367   // Add one-directional video, from caller to callee.
368   rtc::scoped_refptr<webrtc::VideoTrackInterface> caller_track =
369       caller()->CreateLocalVideoTrack();
370   caller()->AddTrack(caller_track);
371   PeerConnectionInterface::RTCOfferAnswerOptions options;
372   options.offer_to_receive_video = 0;
373   caller()->SetOfferAnswerOptions(options);
374   caller()->CreateAndSetAndSignalOffer();
375   ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
376   ASSERT_EQ(callee()->pc()->GetReceivers().size(), 1u);
377 
378   // Expect video to be received in one direction.
379   MediaExpectations media_expectations;
380   media_expectations.CallerExpectsNoVideo();
381   media_expectations.CalleeExpectsSomeVideo();
382 
383   EXPECT_TRUE(ExpectNewFrames(media_expectations));
384 }
385 
386 // Tests that receive only works without the caller having an encoder factory
387 // and the callee having a decoder factory.
TEST_P(PeerConnectionIntegrationTest,EndToEndCallWithReceiveOnlyVideo)388 TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithReceiveOnlyVideo) {
389   ASSERT_TRUE(
390       CreateOneDirectionalPeerConnectionWrappers(/*caller_to_callee=*/false));
391   ConnectFakeSignaling();
392   // Add one-directional video, from callee to caller.
393   rtc::scoped_refptr<webrtc::VideoTrackInterface> callee_track =
394       callee()->CreateLocalVideoTrack();
395   callee()->AddTrack(callee_track);
396   PeerConnectionInterface::RTCOfferAnswerOptions options;
397   options.offer_to_receive_video = 1;
398   caller()->SetOfferAnswerOptions(options);
399   caller()->CreateAndSetAndSignalOffer();
400   ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
401   ASSERT_EQ(caller()->pc()->GetReceivers().size(), 1u);
402 
403   // Expect video to be received in one direction.
404   MediaExpectations media_expectations;
405   media_expectations.CallerExpectsSomeVideo();
406   media_expectations.CalleeExpectsNoVideo();
407 
408   EXPECT_TRUE(ExpectNewFrames(media_expectations));
409 }
410 
TEST_P(PeerConnectionIntegrationTest,EndToEndCallAddReceiveVideoToSendOnlyCall)411 TEST_P(PeerConnectionIntegrationTest,
412        EndToEndCallAddReceiveVideoToSendOnlyCall) {
413   ASSERT_TRUE(CreatePeerConnectionWrappers());
414   ConnectFakeSignaling();
415   // Add one-directional video, from caller to callee.
416   rtc::scoped_refptr<webrtc::VideoTrackInterface> caller_track =
417       caller()->CreateLocalVideoTrack();
418   caller()->AddTrack(caller_track);
419   caller()->CreateAndSetAndSignalOffer();
420   ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
421 
422   // Add receive video.
423   rtc::scoped_refptr<webrtc::VideoTrackInterface> callee_track =
424       callee()->CreateLocalVideoTrack();
425   callee()->AddTrack(callee_track);
426   caller()->CreateAndSetAndSignalOffer();
427   ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
428 
429   // Ensure that video frames are received end-to-end.
430   MediaExpectations media_expectations;
431   media_expectations.ExpectBidirectionalVideo();
432   ASSERT_TRUE(ExpectNewFrames(media_expectations));
433 }
434 
TEST_P(PeerConnectionIntegrationTest,EndToEndCallAddSendVideoToReceiveOnlyCall)435 TEST_P(PeerConnectionIntegrationTest,
436        EndToEndCallAddSendVideoToReceiveOnlyCall) {
437   ASSERT_TRUE(CreatePeerConnectionWrappers());
438   ConnectFakeSignaling();
439   // Add one-directional video, from callee to caller.
440   rtc::scoped_refptr<webrtc::VideoTrackInterface> callee_track =
441       callee()->CreateLocalVideoTrack();
442   callee()->AddTrack(callee_track);
443   caller()->CreateAndSetAndSignalOffer();
444   ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
445 
446   // Add send video.
447   rtc::scoped_refptr<webrtc::VideoTrackInterface> caller_track =
448       caller()->CreateLocalVideoTrack();
449   caller()->AddTrack(caller_track);
450   caller()->CreateAndSetAndSignalOffer();
451   ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
452 
453   // Expect video to be received in one direction.
454   MediaExpectations media_expectations;
455   media_expectations.ExpectBidirectionalVideo();
456   ASSERT_TRUE(ExpectNewFrames(media_expectations));
457 }
458 
TEST_P(PeerConnectionIntegrationTest,EndToEndCallRemoveReceiveVideoFromSendReceiveCall)459 TEST_P(PeerConnectionIntegrationTest,
460        EndToEndCallRemoveReceiveVideoFromSendReceiveCall) {
461   ASSERT_TRUE(CreatePeerConnectionWrappers());
462   ConnectFakeSignaling();
463   // Add send video, from caller to callee.
464   rtc::scoped_refptr<webrtc::VideoTrackInterface> caller_track =
465       caller()->CreateLocalVideoTrack();
466   rtc::scoped_refptr<webrtc::RtpSenderInterface> caller_sender =
467       caller()->AddTrack(caller_track);
468   // Add receive video, from callee to caller.
469   rtc::scoped_refptr<webrtc::VideoTrackInterface> callee_track =
470       callee()->CreateLocalVideoTrack();
471 
472   rtc::scoped_refptr<webrtc::RtpSenderInterface> callee_sender =
473       callee()->AddTrack(callee_track);
474   caller()->CreateAndSetAndSignalOffer();
475   ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
476 
477   // Remove receive video (i.e., callee sender track).
478   callee()->pc()->RemoveTrackOrError(callee_sender);
479 
480   caller()->CreateAndSetAndSignalOffer();
481   ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
482 
483   // Expect one-directional video.
484   MediaExpectations media_expectations;
485   media_expectations.CallerExpectsNoVideo();
486   media_expectations.CalleeExpectsSomeVideo();
487 
488   ASSERT_TRUE(ExpectNewFrames(media_expectations));
489 }
490 
TEST_P(PeerConnectionIntegrationTest,EndToEndCallRemoveSendVideoFromSendReceiveCall)491 TEST_P(PeerConnectionIntegrationTest,
492        EndToEndCallRemoveSendVideoFromSendReceiveCall) {
493   ASSERT_TRUE(CreatePeerConnectionWrappers());
494   ConnectFakeSignaling();
495   // Add send video, from caller to callee.
496   rtc::scoped_refptr<webrtc::VideoTrackInterface> caller_track =
497       caller()->CreateLocalVideoTrack();
498   rtc::scoped_refptr<webrtc::RtpSenderInterface> caller_sender =
499       caller()->AddTrack(caller_track);
500   // Add receive video, from callee to caller.
501   rtc::scoped_refptr<webrtc::VideoTrackInterface> callee_track =
502       callee()->CreateLocalVideoTrack();
503 
504   rtc::scoped_refptr<webrtc::RtpSenderInterface> callee_sender =
505       callee()->AddTrack(callee_track);
506   caller()->CreateAndSetAndSignalOffer();
507   ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
508 
509   // Remove send video (i.e., caller sender track).
510   caller()->pc()->RemoveTrackOrError(caller_sender);
511 
512   caller()->CreateAndSetAndSignalOffer();
513   ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
514 
515   // Expect one-directional video.
516   MediaExpectations media_expectations;
517   media_expectations.CalleeExpectsNoVideo();
518   media_expectations.CallerExpectsSomeVideo();
519 
520   ASSERT_TRUE(ExpectNewFrames(media_expectations));
521 }
522 
523 // This test sets up a audio call initially, with the callee rejecting video
524 // initially. Then later the callee decides to upgrade to audio/video, and
525 // initiates a new offer/answer exchange.
TEST_P(PeerConnectionIntegrationTest,AudioToVideoUpgrade)526 TEST_P(PeerConnectionIntegrationTest, AudioToVideoUpgrade) {
527   ASSERT_TRUE(CreatePeerConnectionWrappers());
528   ConnectFakeSignaling();
529   // Initially, offer an audio/video stream from the caller, but refuse to
530   // send/receive video on the callee side.
531   caller()->AddAudioVideoTracks();
532   callee()->AddAudioTrack();
533   if (sdp_semantics_ == SdpSemantics::kPlanB_DEPRECATED) {
534     PeerConnectionInterface::RTCOfferAnswerOptions options;
535     options.offer_to_receive_video = 0;
536     callee()->SetOfferAnswerOptions(options);
537   } else {
538     callee()->SetRemoteOfferHandler([this] {
539       callee()
540           ->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)
541           ->StopInternal();
542     });
543   }
544   // Do offer/answer and make sure audio is still received end-to-end.
545   caller()->CreateAndSetAndSignalOffer();
546   ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
547   {
548     MediaExpectations media_expectations;
549     media_expectations.ExpectBidirectionalAudio();
550     media_expectations.ExpectNoVideo();
551     ASSERT_TRUE(ExpectNewFrames(media_expectations));
552   }
553   // Sanity check that the callee's description has a rejected video section.
554   ASSERT_NE(nullptr, callee()->pc()->local_description());
555   const ContentInfo* callee_video_content =
556       GetFirstVideoContent(callee()->pc()->local_description()->description());
557   ASSERT_NE(nullptr, callee_video_content);
558   EXPECT_TRUE(callee_video_content->rejected);
559 
560   // Now negotiate with video and ensure negotiation succeeds, with video
561   // frames and additional audio frames being received.
562   callee()->AddVideoTrack();
563   if (sdp_semantics_ == SdpSemantics::kPlanB_DEPRECATED) {
564     PeerConnectionInterface::RTCOfferAnswerOptions options;
565     options.offer_to_receive_video = 1;
566     callee()->SetOfferAnswerOptions(options);
567   } else {
568     callee()->SetRemoteOfferHandler(nullptr);
569     caller()->SetRemoteOfferHandler([this] {
570       // The caller creates a new transceiver to receive video on when receiving
571       // the offer, but by default it is send only.
572       auto transceivers = caller()->pc()->GetTransceivers();
573       ASSERT_EQ(2U, transceivers.size());
574       ASSERT_EQ(cricket::MEDIA_TYPE_VIDEO,
575                 transceivers[1]->receiver()->media_type());
576       transceivers[1]->sender()->SetTrack(
577           caller()->CreateLocalVideoTrack().get());
578       transceivers[1]->SetDirectionWithError(
579           RtpTransceiverDirection::kSendRecv);
580     });
581   }
582   callee()->CreateAndSetAndSignalOffer();
583   ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
584   {
585     // Expect additional audio frames to be received after the upgrade.
586     MediaExpectations media_expectations;
587     media_expectations.ExpectBidirectionalAudioAndVideo();
588     ASSERT_TRUE(ExpectNewFrames(media_expectations));
589   }
590 }
591 
592 // Simpler than the above test; just add an audio track to an established
593 // video-only connection.
TEST_P(PeerConnectionIntegrationTest,AddAudioToVideoOnlyCall)594 TEST_P(PeerConnectionIntegrationTest, AddAudioToVideoOnlyCall) {
595   ASSERT_TRUE(CreatePeerConnectionWrappers());
596   ConnectFakeSignaling();
597   // Do initial offer/answer with just a video track.
598   caller()->AddVideoTrack();
599   callee()->AddVideoTrack();
600   caller()->CreateAndSetAndSignalOffer();
601   ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
602   // Now add an audio track and do another offer/answer.
603   caller()->AddAudioTrack();
604   callee()->AddAudioTrack();
605   caller()->CreateAndSetAndSignalOffer();
606   ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
607   // Ensure both audio and video frames are received end-to-end.
608   MediaExpectations media_expectations;
609   media_expectations.ExpectBidirectionalAudioAndVideo();
610   ASSERT_TRUE(ExpectNewFrames(media_expectations));
611 }
612 
613 // This test sets up a non-bundled call and negotiates bundling at the same
614 // time as starting an ICE restart. When bundling is in effect in the restart,
615 // the DTLS-SRTP context should be successfully reset.
TEST_P(PeerConnectionIntegrationTest,BundlingEnabledWhileIceRestartOccurs)616 TEST_P(PeerConnectionIntegrationTest, BundlingEnabledWhileIceRestartOccurs) {
617   ASSERT_TRUE(CreatePeerConnectionWrappers());
618   ConnectFakeSignaling();
619 
620   caller()->AddAudioVideoTracks();
621   callee()->AddAudioVideoTracks();
622   // Remove the bundle group from the SDP received by the callee.
623   callee()->SetReceivedSdpMunger([](cricket::SessionDescription* desc) {
624     desc->RemoveGroupByName("BUNDLE");
625   });
626   caller()->CreateAndSetAndSignalOffer();
627   ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
628   {
629     MediaExpectations media_expectations;
630     media_expectations.ExpectBidirectionalAudioAndVideo();
631     ASSERT_TRUE(ExpectNewFrames(media_expectations));
632   }
633   // Now stop removing the BUNDLE group, and trigger an ICE restart.
634   callee()->SetReceivedSdpMunger(nullptr);
635   caller()->SetOfferAnswerOptions(IceRestartOfferAnswerOptions());
636   caller()->CreateAndSetAndSignalOffer();
637   ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
638 
639   // Expect additional frames to be received after the ICE restart.
640   {
641     MediaExpectations media_expectations;
642     media_expectations.ExpectBidirectionalAudioAndVideo();
643     ASSERT_TRUE(ExpectNewFrames(media_expectations));
644   }
645 }
646 
647 // Test CVO (Coordination of Video Orientation). If a video source is rotated
648 // and both peers support the CVO RTP header extension, the actual video frames
649 // don't need to be encoded in different resolutions, since the rotation is
650 // communicated through the RTP header extension.
TEST_P(PeerConnectionIntegrationTest,RotatedVideoWithCVOExtension)651 TEST_P(PeerConnectionIntegrationTest, RotatedVideoWithCVOExtension) {
652   ASSERT_TRUE(CreatePeerConnectionWrappers());
653   ConnectFakeSignaling();
654   // Add rotated video tracks.
655   caller()->AddTrack(
656       caller()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_90));
657   callee()->AddTrack(
658       callee()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_270));
659 
660   // Wait for video frames to be received by both sides.
661   caller()->CreateAndSetAndSignalOffer();
662   ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
663   ASSERT_TRUE_WAIT(caller()->min_video_frames_received_per_track() > 0 &&
664                        callee()->min_video_frames_received_per_track() > 0,
665                    kMaxWaitForFramesMs);
666 
667   // Ensure that the aspect ratio is unmodified.
668   // TODO(deadbeef): Where does 4:3 come from? Should be explicit in the test,
669   // not just assumed.
670   EXPECT_EQ(4.0 / 3, caller()->local_rendered_aspect_ratio());
671   EXPECT_EQ(4.0 / 3, caller()->rendered_aspect_ratio());
672   EXPECT_EQ(4.0 / 3, callee()->local_rendered_aspect_ratio());
673   EXPECT_EQ(4.0 / 3, callee()->rendered_aspect_ratio());
674   // Ensure that the CVO bits were surfaced to the renderer.
675   EXPECT_EQ(webrtc::kVideoRotation_270, caller()->rendered_rotation());
676   EXPECT_EQ(webrtc::kVideoRotation_90, callee()->rendered_rotation());
677 }
678 
679 // Test that when the CVO extension isn't supported, video is rotated the
680 // old-fashioned way, by encoding rotated frames.
TEST_P(PeerConnectionIntegrationTest,RotatedVideoWithoutCVOExtension)681 TEST_P(PeerConnectionIntegrationTest, RotatedVideoWithoutCVOExtension) {
682   ASSERT_TRUE(CreatePeerConnectionWrappers());
683   ConnectFakeSignaling();
684   // Add rotated video tracks.
685   caller()->AddTrack(
686       caller()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_90));
687   callee()->AddTrack(
688       callee()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_270));
689 
690   // Remove the CVO extension from the offered SDP.
691   callee()->SetReceivedSdpMunger([](cricket::SessionDescription* desc) {
692     cricket::VideoContentDescription* video =
693         GetFirstVideoContentDescription(desc);
694     video->ClearRtpHeaderExtensions();
695   });
696   // Wait for video frames to be received by both sides.
697   caller()->CreateAndSetAndSignalOffer();
698   ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
699   ASSERT_TRUE_WAIT(caller()->min_video_frames_received_per_track() > 0 &&
700                        callee()->min_video_frames_received_per_track() > 0,
701                    kMaxWaitForFramesMs);
702 
703   // Expect that the aspect ratio is inversed to account for the 90/270 degree
704   // rotation.
705   // TODO(deadbeef): Where does 4:3 come from? Should be explicit in the test,
706   // not just assumed.
707   EXPECT_EQ(3.0 / 4, caller()->local_rendered_aspect_ratio());
708   EXPECT_EQ(3.0 / 4, caller()->rendered_aspect_ratio());
709   EXPECT_EQ(3.0 / 4, callee()->local_rendered_aspect_ratio());
710   EXPECT_EQ(3.0 / 4, callee()->rendered_aspect_ratio());
711   // Expect that each endpoint is unaware of the rotation of the other endpoint.
712   EXPECT_EQ(webrtc::kVideoRotation_0, caller()->rendered_rotation());
713   EXPECT_EQ(webrtc::kVideoRotation_0, callee()->rendered_rotation());
714 }
715 
716 // Test that if the answerer rejects the audio m= section, no audio is sent or
717 // received, but video still can be.
TEST_P(PeerConnectionIntegrationTest,AnswererRejectsAudioSection)718 TEST_P(PeerConnectionIntegrationTest, AnswererRejectsAudioSection) {
719   ASSERT_TRUE(CreatePeerConnectionWrappers());
720   ConnectFakeSignaling();
721   caller()->AddAudioVideoTracks();
722   if (sdp_semantics_ == SdpSemantics::kPlanB_DEPRECATED) {
723     // Only add video track for callee, and set offer_to_receive_audio to 0, so
724     // it will reject the audio m= section completely.
725     PeerConnectionInterface::RTCOfferAnswerOptions options;
726     options.offer_to_receive_audio = 0;
727     callee()->SetOfferAnswerOptions(options);
728   } else {
729     // Stopping the audio RtpTransceiver will cause the media section to be
730     // rejected in the answer.
731     callee()->SetRemoteOfferHandler([this] {
732       callee()
733           ->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_AUDIO)
734           ->StopInternal();
735     });
736   }
737   callee()->AddTrack(callee()->CreateLocalVideoTrack());
738   // Do offer/answer and wait for successful end-to-end video frames.
739   caller()->CreateAndSetAndSignalOffer();
740   ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
741   MediaExpectations media_expectations;
742   media_expectations.ExpectBidirectionalVideo();
743   media_expectations.ExpectNoAudio();
744   ASSERT_TRUE(ExpectNewFrames(media_expectations));
745 
746   // Sanity check that the callee's description has a rejected audio section.
747   ASSERT_NE(nullptr, callee()->pc()->local_description());
748   const ContentInfo* callee_audio_content =
749       GetFirstAudioContent(callee()->pc()->local_description()->description());
750   ASSERT_NE(nullptr, callee_audio_content);
751   EXPECT_TRUE(callee_audio_content->rejected);
752   if (sdp_semantics_ == SdpSemantics::kUnifiedPlan) {
753     // The caller's transceiver should have stopped after receiving the answer,
754     // and thus no longer listed in transceivers.
755     EXPECT_EQ(nullptr,
756               caller()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_AUDIO));
757   }
758 }
759 
760 // Test that if the answerer rejects the video m= section, no video is sent or
761 // received, but audio still can be.
TEST_P(PeerConnectionIntegrationTest,AnswererRejectsVideoSection)762 TEST_P(PeerConnectionIntegrationTest, AnswererRejectsVideoSection) {
763   ASSERT_TRUE(CreatePeerConnectionWrappers());
764   ConnectFakeSignaling();
765   caller()->AddAudioVideoTracks();
766   if (sdp_semantics_ == SdpSemantics::kPlanB_DEPRECATED) {
767     // Only add audio track for callee, and set offer_to_receive_video to 0, so
768     // it will reject the video m= section completely.
769     PeerConnectionInterface::RTCOfferAnswerOptions options;
770     options.offer_to_receive_video = 0;
771     callee()->SetOfferAnswerOptions(options);
772   } else {
773     // Stopping the video RtpTransceiver will cause the media section to be
774     // rejected in the answer.
775     callee()->SetRemoteOfferHandler([this] {
776       callee()
777           ->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)
778           ->StopInternal();
779     });
780   }
781   callee()->AddTrack(callee()->CreateLocalAudioTrack());
782   // Do offer/answer and wait for successful end-to-end audio frames.
783   caller()->CreateAndSetAndSignalOffer();
784   ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
785   MediaExpectations media_expectations;
786   media_expectations.ExpectBidirectionalAudio();
787   media_expectations.ExpectNoVideo();
788   ASSERT_TRUE(ExpectNewFrames(media_expectations));
789 
790   // Sanity check that the callee's description has a rejected video section.
791   ASSERT_NE(nullptr, callee()->pc()->local_description());
792   const ContentInfo* callee_video_content =
793       GetFirstVideoContent(callee()->pc()->local_description()->description());
794   ASSERT_NE(nullptr, callee_video_content);
795   EXPECT_TRUE(callee_video_content->rejected);
796   if (sdp_semantics_ == SdpSemantics::kUnifiedPlan) {
797     // The caller's transceiver should have stopped after receiving the answer,
798     // and thus is no longer present.
799     EXPECT_EQ(nullptr,
800               caller()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO));
801   }
802 }
803 
804 // Test that if the answerer rejects both audio and video m= sections, nothing
805 // bad happens.
806 // TODO(deadbeef): Test that a data channel still works. Currently this doesn't
807 // test anything but the fact that negotiation succeeds, which doesn't mean
808 // much.
TEST_P(PeerConnectionIntegrationTest,AnswererRejectsAudioAndVideoSections)809 TEST_P(PeerConnectionIntegrationTest, AnswererRejectsAudioAndVideoSections) {
810   ASSERT_TRUE(CreatePeerConnectionWrappers());
811   ConnectFakeSignaling();
812   caller()->AddAudioVideoTracks();
813   if (sdp_semantics_ == SdpSemantics::kPlanB_DEPRECATED) {
814     // Don't give the callee any tracks, and set offer_to_receive_X to 0, so it
815     // will reject both audio and video m= sections.
816     PeerConnectionInterface::RTCOfferAnswerOptions options;
817     options.offer_to_receive_audio = 0;
818     options.offer_to_receive_video = 0;
819     callee()->SetOfferAnswerOptions(options);
820   } else {
821     callee()->SetRemoteOfferHandler([this] {
822       // Stopping all transceivers will cause all media sections to be rejected.
823       for (const auto& transceiver : callee()->pc()->GetTransceivers()) {
824         transceiver->StopInternal();
825       }
826     });
827   }
828   // Do offer/answer and wait for stable signaling state.
829   caller()->CreateAndSetAndSignalOffer();
830   ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
831 
832   // Sanity check that the callee's description has rejected m= sections.
833   ASSERT_NE(nullptr, callee()->pc()->local_description());
834   const ContentInfo* callee_audio_content =
835       GetFirstAudioContent(callee()->pc()->local_description()->description());
836   ASSERT_NE(nullptr, callee_audio_content);
837   EXPECT_TRUE(callee_audio_content->rejected);
838   const ContentInfo* callee_video_content =
839       GetFirstVideoContent(callee()->pc()->local_description()->description());
840   ASSERT_NE(nullptr, callee_video_content);
841   EXPECT_TRUE(callee_video_content->rejected);
842 }
843 
844 // This test sets up an audio and video call between two parties. After the
845 // call runs for a while, the caller sends an updated offer with video being
846 // rejected. Once the re-negotiation is done, the video flow should stop and
847 // the audio flow should continue.
TEST_P(PeerConnectionIntegrationTest,VideoRejectedInSubsequentOffer)848 TEST_P(PeerConnectionIntegrationTest, VideoRejectedInSubsequentOffer) {
849   ASSERT_TRUE(CreatePeerConnectionWrappers());
850   ConnectFakeSignaling();
851   caller()->AddAudioVideoTracks();
852   callee()->AddAudioVideoTracks();
853   caller()->CreateAndSetAndSignalOffer();
854   ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
855   {
856     MediaExpectations media_expectations;
857     media_expectations.ExpectBidirectionalAudioAndVideo();
858     ASSERT_TRUE(ExpectNewFrames(media_expectations));
859   }
860   // Renegotiate, rejecting the video m= section.
861   if (sdp_semantics_ == SdpSemantics::kPlanB_DEPRECATED) {
862     caller()->SetGeneratedSdpMunger(
863         [](cricket::SessionDescription* description) {
864           for (cricket::ContentInfo& content : description->contents()) {
865             if (cricket::IsVideoContent(&content)) {
866               content.rejected = true;
867             }
868           }
869         });
870   } else {
871     caller()
872         ->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)
873         ->StopInternal();
874   }
875   caller()->CreateAndSetAndSignalOffer();
876   ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
877 
878   // Sanity check that the caller's description has a rejected video section.
879   ASSERT_NE(nullptr, caller()->pc()->local_description());
880   const ContentInfo* caller_video_content =
881       GetFirstVideoContent(caller()->pc()->local_description()->description());
882   ASSERT_NE(nullptr, caller_video_content);
883   EXPECT_TRUE(caller_video_content->rejected);
884   // Wait for some additional audio frames to be received.
885   {
886     MediaExpectations media_expectations;
887     media_expectations.ExpectBidirectionalAudio();
888     media_expectations.ExpectNoVideo();
889     ASSERT_TRUE(ExpectNewFrames(media_expectations));
890   }
891 }
892 
893 // Do one offer/answer with audio, another that disables it (rejecting the m=
894 // section), and another that re-enables it. Regression test for:
895 // bugs.webrtc.org/6023
TEST_F(PeerConnectionIntegrationTestPlanB,EnableAudioAfterRejecting)896 TEST_F(PeerConnectionIntegrationTestPlanB, EnableAudioAfterRejecting) {
897   ASSERT_TRUE(CreatePeerConnectionWrappers());
898   ConnectFakeSignaling();
899 
900   // Add audio track, do normal offer/answer.
901   rtc::scoped_refptr<webrtc::AudioTrackInterface> track =
902       caller()->CreateLocalAudioTrack();
903   rtc::scoped_refptr<webrtc::RtpSenderInterface> sender =
904       caller()->pc()->AddTrack(track, {"stream"}).MoveValue();
905   caller()->CreateAndSetAndSignalOffer();
906   ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
907 
908   // Remove audio track, and set offer_to_receive_audio to false to cause the
909   // m= section to be completely disabled, not just "recvonly".
910   caller()->pc()->RemoveTrackOrError(sender);
911   PeerConnectionInterface::RTCOfferAnswerOptions options;
912   options.offer_to_receive_audio = 0;
913   caller()->SetOfferAnswerOptions(options);
914   caller()->CreateAndSetAndSignalOffer();
915   ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
916 
917   // Add the audio track again, expecting negotiation to succeed and frames to
918   // flow.
919   sender = caller()->pc()->AddTrack(track, {"stream"}).MoveValue();
920   options.offer_to_receive_audio = 1;
921   caller()->SetOfferAnswerOptions(options);
922   caller()->CreateAndSetAndSignalOffer();
923   ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
924 
925   MediaExpectations media_expectations;
926   media_expectations.CalleeExpectsSomeAudio();
927   EXPECT_TRUE(ExpectNewFrames(media_expectations));
928 }
929 
930 // Basic end-to-end test, but without SSRC/MSID signaling. This functionality
931 // is needed to support legacy endpoints.
932 // TODO(deadbeef): When we support the MID extension and demuxing on MID, also
933 // add a test for an end-to-end test without MID signaling either (basically,
934 // the minimum acceptable SDP).
TEST_P(PeerConnectionIntegrationTest,EndToEndCallWithoutSsrcOrMsidSignaling)935 TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithoutSsrcOrMsidSignaling) {
936   ASSERT_TRUE(CreatePeerConnectionWrappers());
937   ConnectFakeSignaling();
938   // Add audio and video, testing that packets can be demuxed on payload type.
939   caller()->AddAudioVideoTracks();
940   callee()->AddAudioVideoTracks();
941   // Remove SSRCs and MSIDs from the received offer SDP.
942   callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
943   caller()->CreateAndSetAndSignalOffer();
944   ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
945   MediaExpectations media_expectations;
946   media_expectations.ExpectBidirectionalAudioAndVideo();
947   ASSERT_TRUE(ExpectNewFrames(media_expectations));
948 }
949 
950 // Basic end-to-end test, without SSRC signaling. This means that the track
951 // was created properly and frames are delivered when the MSIDs are communicated
952 // with a=msid lines and no a=ssrc lines.
TEST_F(PeerConnectionIntegrationTestUnifiedPlan,EndToEndCallWithoutSsrcSignaling)953 TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
954        EndToEndCallWithoutSsrcSignaling) {
955   const char kStreamId[] = "streamId";
956   ASSERT_TRUE(CreatePeerConnectionWrappers());
957   ConnectFakeSignaling();
958   // Add just audio tracks.
959   caller()->AddTrack(caller()->CreateLocalAudioTrack(), {kStreamId});
960   callee()->AddAudioTrack();
961 
962   // Remove SSRCs from the received offer SDP.
963   callee()->SetReceivedSdpMunger(RemoveSsrcsAndKeepMsids);
964   caller()->CreateAndSetAndSignalOffer();
965   ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
966   MediaExpectations media_expectations;
967   media_expectations.ExpectBidirectionalAudio();
968   ASSERT_TRUE(ExpectNewFrames(media_expectations));
969 }
970 
TEST_F(PeerConnectionIntegrationTestUnifiedPlan,EndToEndCallAddReceiveVideoToSendOnlyCall)971 TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
972        EndToEndCallAddReceiveVideoToSendOnlyCall) {
973   ASSERT_TRUE(CreatePeerConnectionWrappers());
974   ConnectFakeSignaling();
975   // Add one-directional video, from caller to callee.
976   rtc::scoped_refptr<webrtc::VideoTrackInterface> track =
977       caller()->CreateLocalVideoTrack();
978 
979   RtpTransceiverInit video_transceiver_init;
980   video_transceiver_init.stream_ids = {"video1"};
981   video_transceiver_init.direction = RtpTransceiverDirection::kSendOnly;
982   auto video_sender =
983       caller()->pc()->AddTransceiver(track, video_transceiver_init).MoveValue();
984   caller()->CreateAndSetAndSignalOffer();
985   ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
986 
987   // Add receive direction.
988   video_sender->SetDirectionWithError(RtpTransceiverDirection::kSendRecv);
989 
990   rtc::scoped_refptr<webrtc::VideoTrackInterface> callee_track =
991       callee()->CreateLocalVideoTrack();
992 
993   callee()->AddTrack(callee_track);
994   caller()->CreateAndSetAndSignalOffer();
995   ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
996   // Ensure that video frames are received end-to-end.
997   MediaExpectations media_expectations;
998   media_expectations.ExpectBidirectionalVideo();
999   ASSERT_TRUE(ExpectNewFrames(media_expectations));
1000 }
1001 
1002 // Tests that video flows between multiple video tracks when SSRCs are not
1003 // signaled. This exercises the MID RTP header extension which is needed to
1004 // demux the incoming video tracks.
TEST_F(PeerConnectionIntegrationTestUnifiedPlan,EndToEndCallWithTwoVideoTracksAndNoSignaledSsrc)1005 TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
1006        EndToEndCallWithTwoVideoTracksAndNoSignaledSsrc) {
1007   ASSERT_TRUE(CreatePeerConnectionWrappers());
1008   ConnectFakeSignaling();
1009   caller()->AddVideoTrack();
1010   caller()->AddVideoTrack();
1011   callee()->AddVideoTrack();
1012   callee()->AddVideoTrack();
1013 
1014   caller()->SetReceivedSdpMunger(&RemoveSsrcsAndKeepMsids);
1015   callee()->SetReceivedSdpMunger(&RemoveSsrcsAndKeepMsids);
1016   caller()->CreateAndSetAndSignalOffer();
1017   ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1018   ASSERT_EQ(2u, caller()->pc()->GetReceivers().size());
1019   ASSERT_EQ(2u, callee()->pc()->GetReceivers().size());
1020 
1021   // Expect video to be received in both directions on both tracks.
1022   MediaExpectations media_expectations;
1023   media_expectations.ExpectBidirectionalVideo();
1024   EXPECT_TRUE(ExpectNewFrames(media_expectations));
1025 }
1026 
1027 // Used for the test below.
RemoveBundleGroupSsrcsAndMidExtension(cricket::SessionDescription * desc)1028 void RemoveBundleGroupSsrcsAndMidExtension(cricket::SessionDescription* desc) {
1029   RemoveSsrcsAndKeepMsids(desc);
1030   desc->RemoveGroupByName("BUNDLE");
1031   for (ContentInfo& content : desc->contents()) {
1032     cricket::MediaContentDescription* media = content.media_description();
1033     cricket::RtpHeaderExtensions extensions = media->rtp_header_extensions();
1034     extensions.erase(std::remove_if(extensions.begin(), extensions.end(),
1035                                     [](const RtpExtension& extension) {
1036                                       return extension.uri ==
1037                                              RtpExtension::kMidUri;
1038                                     }),
1039                      extensions.end());
1040     media->set_rtp_header_extensions(extensions);
1041   }
1042 }
1043 
1044 // Tests that video flows between multiple video tracks when BUNDLE is not used,
1045 // SSRCs are not signaled and the MID RTP header extension is not used. This
1046 // relies on demuxing by payload type, which normally doesn't work if you have
1047 // multiple media sections using the same payload type, but which should work as
1048 // long as the media sections aren't bundled.
1049 // Regression test for: http://crbug.com/webrtc/12023
TEST_F(PeerConnectionIntegrationTestUnifiedPlan,EndToEndCallWithTwoVideoTracksNoBundleNoSignaledSsrcAndNoMid)1050 TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
1051        EndToEndCallWithTwoVideoTracksNoBundleNoSignaledSsrcAndNoMid) {
1052   ASSERT_TRUE(CreatePeerConnectionWrappers());
1053   ConnectFakeSignaling();
1054   caller()->AddVideoTrack();
1055   caller()->AddVideoTrack();
1056   callee()->AddVideoTrack();
1057   callee()->AddVideoTrack();
1058   caller()->SetReceivedSdpMunger(&RemoveBundleGroupSsrcsAndMidExtension);
1059   callee()->SetReceivedSdpMunger(&RemoveBundleGroupSsrcsAndMidExtension);
1060   caller()->CreateAndSetAndSignalOffer();
1061   ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1062   ASSERT_EQ(2u, caller()->pc()->GetReceivers().size());
1063   ASSERT_EQ(2u, callee()->pc()->GetReceivers().size());
1064   // Make sure we are not bundled.
1065   ASSERT_NE(caller()->pc()->GetSenders()[0]->dtls_transport(),
1066             caller()->pc()->GetSenders()[1]->dtls_transport());
1067 
1068   // Expect video to be received in both directions on both tracks.
1069   MediaExpectations media_expectations;
1070   media_expectations.ExpectBidirectionalVideo();
1071   EXPECT_TRUE(ExpectNewFrames(media_expectations));
1072 }
1073 
1074 // Used for the test below.
ModifyPayloadTypesAndRemoveMidExtension(cricket::SessionDescription * desc)1075 void ModifyPayloadTypesAndRemoveMidExtension(
1076     cricket::SessionDescription* desc) {
1077   int pt = 96;
1078   for (ContentInfo& content : desc->contents()) {
1079     cricket::MediaContentDescription* media = content.media_description();
1080     cricket::RtpHeaderExtensions extensions = media->rtp_header_extensions();
1081     extensions.erase(std::remove_if(extensions.begin(), extensions.end(),
1082                                     [](const RtpExtension& extension) {
1083                                       return extension.uri ==
1084                                              RtpExtension::kMidUri;
1085                                     }),
1086                      extensions.end());
1087     media->set_rtp_header_extensions(extensions);
1088     cricket::VideoContentDescription* video = media->as_video();
1089     ASSERT_TRUE(video != nullptr);
1090     std::vector<cricket::VideoCodec> codecs = {{pt++, "VP8"}};
1091     video->set_codecs(codecs);
1092   }
1093 }
1094 
1095 // Tests that two video tracks can be demultiplexed by payload type alone, by
1096 // using different payload types for the same codec in different m= sections.
1097 // This practice is discouraged but historically has been supported.
1098 // Regression test for: http://crbug.com/webrtc/12029
TEST_F(PeerConnectionIntegrationTestUnifiedPlan,EndToEndCallWithTwoVideoTracksDemultiplexedByPayloadType)1099 TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
1100        EndToEndCallWithTwoVideoTracksDemultiplexedByPayloadType) {
1101   ASSERT_TRUE(CreatePeerConnectionWrappers());
1102   ConnectFakeSignaling();
1103   caller()->AddVideoTrack();
1104   caller()->AddVideoTrack();
1105   callee()->AddVideoTrack();
1106   callee()->AddVideoTrack();
1107   caller()->SetGeneratedSdpMunger(&ModifyPayloadTypesAndRemoveMidExtension);
1108   callee()->SetGeneratedSdpMunger(&ModifyPayloadTypesAndRemoveMidExtension);
1109   // We can't remove SSRCs from the generated SDP because then no send streams
1110   // would be created.
1111   caller()->SetReceivedSdpMunger(&RemoveSsrcsAndKeepMsids);
1112   callee()->SetReceivedSdpMunger(&RemoveSsrcsAndKeepMsids);
1113   caller()->CreateAndSetAndSignalOffer();
1114   ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1115   ASSERT_EQ(2u, caller()->pc()->GetReceivers().size());
1116   ASSERT_EQ(2u, callee()->pc()->GetReceivers().size());
1117   // Make sure we are bundled.
1118   ASSERT_EQ(caller()->pc()->GetSenders()[0]->dtls_transport(),
1119             caller()->pc()->GetSenders()[1]->dtls_transport());
1120 
1121   // Expect video to be received in both directions on both tracks.
1122   MediaExpectations media_expectations;
1123   media_expectations.ExpectBidirectionalVideo();
1124   EXPECT_TRUE(ExpectNewFrames(media_expectations));
1125 }
1126 
TEST_F(PeerConnectionIntegrationTestUnifiedPlan,NoStreamsMsidLinePresent)1127 TEST_F(PeerConnectionIntegrationTestUnifiedPlan, NoStreamsMsidLinePresent) {
1128   ASSERT_TRUE(CreatePeerConnectionWrappers());
1129   ConnectFakeSignaling();
1130   caller()->AddAudioTrack();
1131   caller()->AddVideoTrack();
1132   caller()->CreateAndSetAndSignalOffer();
1133   ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1134   auto callee_receivers = callee()->pc()->GetReceivers();
1135   ASSERT_EQ(2u, callee_receivers.size());
1136   EXPECT_TRUE(callee_receivers[0]->stream_ids().empty());
1137   EXPECT_TRUE(callee_receivers[1]->stream_ids().empty());
1138 }
1139 
TEST_F(PeerConnectionIntegrationTestUnifiedPlan,NoStreamsMsidLineMissing)1140 TEST_F(PeerConnectionIntegrationTestUnifiedPlan, NoStreamsMsidLineMissing) {
1141   ASSERT_TRUE(CreatePeerConnectionWrappers());
1142   ConnectFakeSignaling();
1143   caller()->AddAudioTrack();
1144   caller()->AddVideoTrack();
1145   callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
1146   caller()->CreateAndSetAndSignalOffer();
1147   ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1148   auto callee_receivers = callee()->pc()->GetReceivers();
1149   ASSERT_EQ(2u, callee_receivers.size());
1150   ASSERT_EQ(1u, callee_receivers[0]->stream_ids().size());
1151   ASSERT_EQ(1u, callee_receivers[1]->stream_ids().size());
1152   EXPECT_EQ(callee_receivers[0]->stream_ids()[0],
1153             callee_receivers[1]->stream_ids()[0]);
1154   EXPECT_EQ(callee_receivers[0]->streams()[0],
1155             callee_receivers[1]->streams()[0]);
1156 }
1157 
1158 // Test that if two video tracks are sent (from caller to callee, in this test),
1159 // they're transmitted correctly end-to-end.
TEST_P(PeerConnectionIntegrationTest,EndToEndCallWithTwoVideoTracks)1160 TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithTwoVideoTracks) {
1161   ASSERT_TRUE(CreatePeerConnectionWrappers());
1162   ConnectFakeSignaling();
1163   // Add one audio/video stream, and one video-only stream.
1164   caller()->AddAudioVideoTracks();
1165   caller()->AddVideoTrack();
1166   caller()->CreateAndSetAndSignalOffer();
1167   ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1168   ASSERT_EQ(3u, callee()->pc()->GetReceivers().size());
1169 
1170   MediaExpectations media_expectations;
1171   media_expectations.CalleeExpectsSomeAudioAndVideo();
1172   ASSERT_TRUE(ExpectNewFrames(media_expectations));
1173 }
1174 
MakeSpecCompliantMaxBundleOffer(cricket::SessionDescription * desc)1175 static void MakeSpecCompliantMaxBundleOffer(cricket::SessionDescription* desc) {
1176   bool first = true;
1177   for (cricket::ContentInfo& content : desc->contents()) {
1178     if (first) {
1179       first = false;
1180       continue;
1181     }
1182     content.bundle_only = true;
1183   }
1184   first = true;
1185   for (cricket::TransportInfo& transport : desc->transport_infos()) {
1186     if (first) {
1187       first = false;
1188       continue;
1189     }
1190     transport.description.ice_ufrag.clear();
1191     transport.description.ice_pwd.clear();
1192     transport.description.connection_role = cricket::CONNECTIONROLE_NONE;
1193     transport.description.identity_fingerprint.reset(nullptr);
1194   }
1195 }
1196 
1197 // Test that if applying a true "max bundle" offer, which uses ports of 0,
1198 // "a=bundle-only", omitting "a=fingerprint", "a=setup", "a=ice-ufrag" and
1199 // "a=ice-pwd" for all but the audio "m=" section, negotiation still completes
1200 // successfully and media flows.
1201 // TODO(deadbeef): Update this test to also omit "a=rtcp-mux", once that works.
1202 // TODO(deadbeef): Won't need this test once we start generating actual
1203 // standards-compliant SDP.
TEST_P(PeerConnectionIntegrationTest,EndToEndCallWithSpecCompliantMaxBundleOffer)1204 TEST_P(PeerConnectionIntegrationTest,
1205        EndToEndCallWithSpecCompliantMaxBundleOffer) {
1206   ASSERT_TRUE(CreatePeerConnectionWrappers());
1207   ConnectFakeSignaling();
1208   caller()->AddAudioVideoTracks();
1209   callee()->AddAudioVideoTracks();
1210   // Do the equivalent of setting the port to 0, adding a=bundle-only, and
1211   // removing a=ice-ufrag, a=ice-pwd, a=fingerprint and a=setup from all
1212   // but the first m= section.
1213   callee()->SetReceivedSdpMunger(MakeSpecCompliantMaxBundleOffer);
1214   caller()->CreateAndSetAndSignalOffer();
1215   ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1216   MediaExpectations media_expectations;
1217   media_expectations.ExpectBidirectionalAudioAndVideo();
1218   ASSERT_TRUE(ExpectNewFrames(media_expectations));
1219 }
1220 
1221 // Test that we can receive the audio output level from a remote audio track.
1222 // TODO(deadbeef): Use a fake audio source and verify that the output level is
1223 // exactly what the source on the other side was configured with.
TEST_P(PeerConnectionIntegrationTest,GetAudioOutputLevelStatsWithOldStatsApi)1224 TEST_P(PeerConnectionIntegrationTest, GetAudioOutputLevelStatsWithOldStatsApi) {
1225   ASSERT_TRUE(CreatePeerConnectionWrappers());
1226   ConnectFakeSignaling();
1227   // Just add an audio track.
1228   caller()->AddAudioTrack();
1229   caller()->CreateAndSetAndSignalOffer();
1230   ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1231 
1232   // Get the audio output level stats. Note that the level is not available
1233   // until an RTCP packet has been received.
1234   EXPECT_TRUE_WAIT(callee()->OldGetStats()->AudioOutputLevel() > 0,
1235                    kMaxWaitForFramesMs);
1236 }
1237 
1238 // Test that an audio input level is reported.
1239 // TODO(deadbeef): Use a fake audio source and verify that the input level is
1240 // exactly what the source was configured with.
TEST_P(PeerConnectionIntegrationTest,GetAudioInputLevelStatsWithOldStatsApi)1241 TEST_P(PeerConnectionIntegrationTest, GetAudioInputLevelStatsWithOldStatsApi) {
1242   ASSERT_TRUE(CreatePeerConnectionWrappers());
1243   ConnectFakeSignaling();
1244   // Just add an audio track.
1245   caller()->AddAudioTrack();
1246   caller()->CreateAndSetAndSignalOffer();
1247   ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1248 
1249   // Get the audio input level stats. The level should be available very
1250   // soon after the test starts.
1251   EXPECT_TRUE_WAIT(caller()->OldGetStats()->AudioInputLevel() > 0,
1252                    kMaxWaitForStatsMs);
1253 }
1254 
1255 // Test that we can get incoming byte counts from both audio and video tracks.
TEST_P(PeerConnectionIntegrationTest,GetBytesReceivedStatsWithOldStatsApi)1256 TEST_P(PeerConnectionIntegrationTest, GetBytesReceivedStatsWithOldStatsApi) {
1257   ASSERT_TRUE(CreatePeerConnectionWrappers());
1258   ConnectFakeSignaling();
1259   caller()->AddAudioVideoTracks();
1260   // Do offer/answer, wait for the callee to receive some frames.
1261   caller()->CreateAndSetAndSignalOffer();
1262   ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1263 
1264   MediaExpectations media_expectations;
1265   media_expectations.CalleeExpectsSomeAudioAndVideo();
1266   ASSERT_TRUE(ExpectNewFrames(media_expectations));
1267 
1268   // Get a handle to the remote tracks created, so they can be used as GetStats
1269   // filters.
1270   for (const auto& receiver : callee()->pc()->GetReceivers()) {
1271     // We received frames, so we definitely should have nonzero "received bytes"
1272     // stats at this point.
1273     EXPECT_GT(
1274         callee()->OldGetStatsForTrack(receiver->track().get())->BytesReceived(),
1275         0);
1276   }
1277 }
1278 
1279 // Test that we can get outgoing byte counts from both audio and video tracks.
TEST_P(PeerConnectionIntegrationTest,GetBytesSentStatsWithOldStatsApi)1280 TEST_P(PeerConnectionIntegrationTest, GetBytesSentStatsWithOldStatsApi) {
1281   ASSERT_TRUE(CreatePeerConnectionWrappers());
1282   ConnectFakeSignaling();
1283   auto audio_track = caller()->CreateLocalAudioTrack();
1284   auto video_track = caller()->CreateLocalVideoTrack();
1285   caller()->AddTrack(audio_track);
1286   caller()->AddTrack(video_track);
1287   // Do offer/answer, wait for the callee to receive some frames.
1288   caller()->CreateAndSetAndSignalOffer();
1289   ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1290   MediaExpectations media_expectations;
1291   media_expectations.CalleeExpectsSomeAudioAndVideo();
1292   ASSERT_TRUE(ExpectNewFrames(media_expectations));
1293 
1294   // The callee received frames, so we definitely should have nonzero "sent
1295   // bytes" stats at this point.
1296   EXPECT_GT(caller()->OldGetStatsForTrack(audio_track.get())->BytesSent(), 0);
1297   EXPECT_GT(caller()->OldGetStatsForTrack(video_track.get())->BytesSent(), 0);
1298 }
1299 
1300 // Test that the track ID is associated with all local and remote SSRC stats
1301 // using the old GetStats() and more than 1 audio and more than 1 video track.
1302 // This is a regression test for crbug.com/906988
TEST_F(PeerConnectionIntegrationTestUnifiedPlan,OldGetStatsAssociatesTrackIdForManyMediaSections)1303 TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
1304        OldGetStatsAssociatesTrackIdForManyMediaSections) {
1305   ASSERT_TRUE(CreatePeerConnectionWrappers());
1306   ConnectFakeSignaling();
1307   auto audio_sender_1 = caller()->AddAudioTrack();
1308   auto video_sender_1 = caller()->AddVideoTrack();
1309   auto audio_sender_2 = caller()->AddAudioTrack();
1310   auto video_sender_2 = caller()->AddVideoTrack();
1311   caller()->CreateAndSetAndSignalOffer();
1312   ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1313 
1314   MediaExpectations media_expectations;
1315   media_expectations.CalleeExpectsSomeAudioAndVideo();
1316   ASSERT_TRUE_WAIT(ExpectNewFrames(media_expectations), kDefaultTimeout);
1317 
1318   std::vector<std::string> track_ids = {
1319       audio_sender_1->track()->id(), video_sender_1->track()->id(),
1320       audio_sender_2->track()->id(), video_sender_2->track()->id()};
1321 
1322   auto caller_stats = caller()->OldGetStats();
1323   EXPECT_THAT(caller_stats->TrackIds(), UnorderedElementsAreArray(track_ids));
1324   auto callee_stats = callee()->OldGetStats();
1325   EXPECT_THAT(callee_stats->TrackIds(), UnorderedElementsAreArray(track_ids));
1326 }
1327 
1328 // Test that the new GetStats() returns stats for all outgoing/incoming streams
1329 // with the correct track IDs if there are more than one audio and more than one
1330 // video senders/receivers.
TEST_P(PeerConnectionIntegrationTest,NewGetStatsManyAudioAndManyVideoStreams)1331 TEST_P(PeerConnectionIntegrationTest, NewGetStatsManyAudioAndManyVideoStreams) {
1332   ASSERT_TRUE(CreatePeerConnectionWrappers());
1333   ConnectFakeSignaling();
1334   auto audio_sender_1 = caller()->AddAudioTrack();
1335   auto video_sender_1 = caller()->AddVideoTrack();
1336   auto audio_sender_2 = caller()->AddAudioTrack();
1337   auto video_sender_2 = caller()->AddVideoTrack();
1338   caller()->CreateAndSetAndSignalOffer();
1339   ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1340 
1341   MediaExpectations media_expectations;
1342   media_expectations.CalleeExpectsSomeAudioAndVideo();
1343   ASSERT_TRUE_WAIT(ExpectNewFrames(media_expectations), kDefaultTimeout);
1344 
1345   std::vector<std::string> track_ids = {
1346       audio_sender_1->track()->id(), video_sender_1->track()->id(),
1347       audio_sender_2->track()->id(), video_sender_2->track()->id()};
1348 
1349   rtc::scoped_refptr<const webrtc::RTCStatsReport> caller_report =
1350       caller()->NewGetStats();
1351   ASSERT_TRUE(caller_report);
1352   auto outbound_stream_stats =
1353       caller_report->GetStatsOfType<webrtc::RTCOutboundRTPStreamStats>();
1354   ASSERT_EQ(outbound_stream_stats.size(), 4u);
1355   std::vector<std::string> outbound_track_ids;
1356   for (const auto& stat : outbound_stream_stats) {
1357     ASSERT_TRUE(stat->bytes_sent.is_defined());
1358     EXPECT_LT(0u, *stat->bytes_sent);
1359     if (*stat->kind == "video") {
1360       ASSERT_TRUE(stat->key_frames_encoded.is_defined());
1361       EXPECT_GT(*stat->key_frames_encoded, 0u);
1362       ASSERT_TRUE(stat->frames_encoded.is_defined());
1363       EXPECT_GE(*stat->frames_encoded, *stat->key_frames_encoded);
1364     }
1365     ASSERT_TRUE(stat->track_id.is_defined());
1366     const auto* track_stat =
1367         caller_report->GetAs<webrtc::DEPRECATED_RTCMediaStreamTrackStats>(
1368             *stat->track_id);
1369     ASSERT_TRUE(track_stat);
1370     outbound_track_ids.push_back(*track_stat->track_identifier);
1371   }
1372   EXPECT_THAT(outbound_track_ids, UnorderedElementsAreArray(track_ids));
1373 
1374   rtc::scoped_refptr<const webrtc::RTCStatsReport> callee_report =
1375       callee()->NewGetStats();
1376   ASSERT_TRUE(callee_report);
1377   auto inbound_stream_stats =
1378       callee_report->GetStatsOfType<webrtc::RTCInboundRTPStreamStats>();
1379   ASSERT_EQ(4u, inbound_stream_stats.size());
1380   std::vector<std::string> inbound_track_ids;
1381   for (const auto& stat : inbound_stream_stats) {
1382     ASSERT_TRUE(stat->bytes_received.is_defined());
1383     EXPECT_LT(0u, *stat->bytes_received);
1384     if (*stat->kind == "video") {
1385       ASSERT_TRUE(stat->key_frames_decoded.is_defined());
1386       EXPECT_GT(*stat->key_frames_decoded, 0u);
1387       ASSERT_TRUE(stat->frames_decoded.is_defined());
1388       EXPECT_GE(*stat->frames_decoded, *stat->key_frames_decoded);
1389     }
1390     ASSERT_TRUE(stat->track_id.is_defined());
1391     const auto* track_stat =
1392         callee_report->GetAs<webrtc::DEPRECATED_RTCMediaStreamTrackStats>(
1393             *stat->track_id);
1394     ASSERT_TRUE(track_stat);
1395     inbound_track_ids.push_back(*track_stat->track_identifier);
1396   }
1397   EXPECT_THAT(inbound_track_ids, UnorderedElementsAreArray(track_ids));
1398 }
1399 
1400 // Test that we can get stats (using the new stats implementation) for
1401 // unsignaled streams. Meaning when SSRCs/MSIDs aren't signaled explicitly in
1402 // SDP.
TEST_P(PeerConnectionIntegrationTest,GetStatsForUnsignaledStreamWithNewStatsApi)1403 TEST_P(PeerConnectionIntegrationTest,
1404        GetStatsForUnsignaledStreamWithNewStatsApi) {
1405   ASSERT_TRUE(CreatePeerConnectionWrappers());
1406   ConnectFakeSignaling();
1407   caller()->AddAudioTrack();
1408   // Remove SSRCs and MSIDs from the received offer SDP.
1409   callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
1410   caller()->CreateAndSetAndSignalOffer();
1411   ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1412   MediaExpectations media_expectations;
1413   media_expectations.CalleeExpectsSomeAudio(1);
1414   ASSERT_TRUE(ExpectNewFrames(media_expectations));
1415 
1416   // We received a frame, so we should have nonzero "bytes received" stats for
1417   // the unsignaled stream, if stats are working for it.
1418   rtc::scoped_refptr<const webrtc::RTCStatsReport> report =
1419       callee()->NewGetStats();
1420   ASSERT_NE(nullptr, report);
1421   auto inbound_stream_stats =
1422       report->GetStatsOfType<webrtc::RTCInboundRTPStreamStats>();
1423   ASSERT_EQ(1U, inbound_stream_stats.size());
1424   ASSERT_TRUE(inbound_stream_stats[0]->bytes_received.is_defined());
1425   ASSERT_GT(*inbound_stream_stats[0]->bytes_received, 0U);
1426   ASSERT_TRUE(inbound_stream_stats[0]->track_id.is_defined());
1427 }
1428 
1429 // Same as above but for the legacy stats implementation.
TEST_P(PeerConnectionIntegrationTest,GetStatsForUnsignaledStreamWithOldStatsApi)1430 TEST_P(PeerConnectionIntegrationTest,
1431        GetStatsForUnsignaledStreamWithOldStatsApi) {
1432   ASSERT_TRUE(CreatePeerConnectionWrappers());
1433   ConnectFakeSignaling();
1434   caller()->AddAudioTrack();
1435   // Remove SSRCs and MSIDs from the received offer SDP.
1436   callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
1437   caller()->CreateAndSetAndSignalOffer();
1438   ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1439 
1440   // Note that, since the old stats implementation associates SSRCs with tracks
1441   // using SDP, when SSRCs aren't signaled in SDP these stats won't have an
1442   // associated track ID. So we can't use the track "selector" argument.
1443   //
1444   // Also, we use "EXPECT_TRUE_WAIT" because the stats collector may decide to
1445   // return cached stats if not enough time has passed since the last update.
1446   EXPECT_TRUE_WAIT(callee()->OldGetStats()->BytesReceived() > 0,
1447                    kDefaultTimeout);
1448 }
1449 
1450 // Test that we can successfully get the media related stats (audio level
1451 // etc.) for the unsignaled stream.
TEST_P(PeerConnectionIntegrationTest,GetMediaStatsForUnsignaledStreamWithNewStatsApi)1452 TEST_P(PeerConnectionIntegrationTest,
1453        GetMediaStatsForUnsignaledStreamWithNewStatsApi) {
1454   ASSERT_TRUE(CreatePeerConnectionWrappers());
1455   ConnectFakeSignaling();
1456   caller()->AddAudioVideoTracks();
1457   // Remove SSRCs and MSIDs from the received offer SDP.
1458   callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
1459   caller()->CreateAndSetAndSignalOffer();
1460   ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1461   MediaExpectations media_expectations;
1462   media_expectations.CalleeExpectsSomeAudio(1);
1463   media_expectations.CalleeExpectsSomeVideo(1);
1464   ASSERT_TRUE(ExpectNewFrames(media_expectations));
1465 
1466   rtc::scoped_refptr<const webrtc::RTCStatsReport> report =
1467       callee()->NewGetStats();
1468   ASSERT_NE(nullptr, report);
1469 
1470   auto media_stats =
1471       report->GetStatsOfType<webrtc::DEPRECATED_RTCMediaStreamTrackStats>();
1472   auto audio_index = FindFirstMediaStatsIndexByKind("audio", media_stats);
1473   ASSERT_GE(audio_index, 0);
1474   EXPECT_TRUE(media_stats[audio_index]->audio_level.is_defined());
1475 }
1476 
1477 // Helper for test below.
ModifySsrcs(cricket::SessionDescription * desc)1478 void ModifySsrcs(cricket::SessionDescription* desc) {
1479   for (ContentInfo& content : desc->contents()) {
1480     for (StreamParams& stream :
1481          content.media_description()->mutable_streams()) {
1482       for (uint32_t& ssrc : stream.ssrcs) {
1483         ssrc = rtc::CreateRandomId();
1484       }
1485     }
1486   }
1487 }
1488 
1489 // Test that the "DEPRECATED_RTCMediaStreamTrackStats"  object is updated
1490 // correctly when SSRCs are unsignaled, and the SSRC of the received (audio)
1491 // stream changes. This should result in two "RTCInboundRTPStreamStats", but
1492 // only one "DEPRECATED_RTCMediaStreamTrackStats", whose counters go up
1493 // continuously rather than being reset to 0 once the SSRC change occurs.
1494 //
1495 // Regression test for this bug:
1496 // https://bugs.chromium.org/p/webrtc/issues/detail?id=8158
1497 //
1498 // The bug causes the track stats to only represent one of the two streams:
1499 // whichever one has the higher SSRC. So with this bug, there was a 50% chance
1500 // that the track stat counters would reset to 0 when the new stream is
1501 // received, and a 50% chance that they'll stop updating (while
1502 // "concealed_samples" continues increasing, due to silence being generated for
1503 // the inactive stream).
TEST_P(PeerConnectionIntegrationTest,TrackStatsUpdatedCorrectlyWhenUnsignaledSsrcChanges)1504 TEST_P(PeerConnectionIntegrationTest,
1505        TrackStatsUpdatedCorrectlyWhenUnsignaledSsrcChanges) {
1506   ASSERT_TRUE(CreatePeerConnectionWrappers());
1507   ConnectFakeSignaling();
1508   caller()->AddAudioTrack();
1509   // Remove SSRCs and MSIDs from the received offer SDP, simulating an endpoint
1510   // that doesn't signal SSRCs (from the callee's perspective).
1511   callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
1512   caller()->CreateAndSetAndSignalOffer();
1513   ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1514   // Wait for 50 audio frames (500ms of audio) to be received by the callee.
1515   {
1516     MediaExpectations media_expectations;
1517     media_expectations.CalleeExpectsSomeAudio(50);
1518     ASSERT_TRUE(ExpectNewFrames(media_expectations));
1519   }
1520   // Some audio frames were received, so we should have nonzero "samples
1521   // received" for the track.
1522   rtc::scoped_refptr<const webrtc::RTCStatsReport> report =
1523       callee()->NewGetStats();
1524   ASSERT_NE(nullptr, report);
1525   auto track_stats =
1526       report->GetStatsOfType<webrtc::DEPRECATED_RTCMediaStreamTrackStats>();
1527   ASSERT_EQ(1U, track_stats.size());
1528   ASSERT_TRUE(track_stats[0]->total_samples_received.is_defined());
1529   ASSERT_GT(*track_stats[0]->total_samples_received, 0U);
1530   // uint64_t prev_samples_received = *track_stats[0]->total_samples_received;
1531 
1532   // Create a new offer and munge it to cause the caller to use a new SSRC.
1533   caller()->SetGeneratedSdpMunger(ModifySsrcs);
1534   caller()->CreateAndSetAndSignalOffer();
1535   ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1536   // Wait for 25 more audio frames (250ms of audio) to be received, from the new
1537   // SSRC.
1538   {
1539     MediaExpectations media_expectations;
1540     media_expectations.CalleeExpectsSomeAudio(25);
1541     ASSERT_TRUE(ExpectNewFrames(media_expectations));
1542   }
1543 
1544   report = callee()->NewGetStats();
1545   ASSERT_NE(nullptr, report);
1546   track_stats =
1547       report->GetStatsOfType<webrtc::DEPRECATED_RTCMediaStreamTrackStats>();
1548   ASSERT_EQ(1U, track_stats.size());
1549   ASSERT_TRUE(track_stats[0]->total_samples_received.is_defined());
1550   // The "total samples received" stat should only be greater than it was
1551   // before.
1552   // TODO(deadbeef): Uncomment this assertion once the bug is completely fixed.
1553   // Right now, the new SSRC will cause the counters to reset to 0.
1554   // EXPECT_GT(*track_stats[0]->total_samples_received, prev_samples_received);
1555 
1556   // Additionally, the percentage of concealed samples (samples generated to
1557   // conceal packet loss) should be less than 50%. If it's greater, that's a
1558   // good sign that we're seeing stats from the old stream that's no longer
1559   // receiving packets, and is generating concealed samples of silence.
1560   constexpr double kAcceptableConcealedSamplesPercentage = 0.50;
1561   ASSERT_TRUE(track_stats[0]->concealed_samples.is_defined());
1562   EXPECT_LT(*track_stats[0]->concealed_samples,
1563             *track_stats[0]->total_samples_received *
1564                 kAcceptableConcealedSamplesPercentage);
1565 
1566   // Also ensure that we have two "RTCInboundRTPStreamStats" as expected, as a
1567   // sanity check that the SSRC really changed.
1568   // TODO(deadbeef): This isn't working right now, because we're not returning
1569   // *any* stats for the inactive stream. Uncomment when the bug is completely
1570   // fixed.
1571   // auto inbound_stream_stats =
1572   //     report->GetStatsOfType<webrtc::RTCInboundRTPStreamStats>();
1573   // ASSERT_EQ(2U, inbound_stream_stats.size());
1574 }
1575 
1576 // Test that DTLS 1.0 is used if both sides only support DTLS 1.0.
TEST_P(PeerConnectionIntegrationTest,EndToEndCallWithDtls10)1577 TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithDtls10) {
1578   PeerConnectionFactory::Options dtls_10_options;
1579   dtls_10_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
1580   ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(dtls_10_options,
1581                                                       dtls_10_options));
1582   ConnectFakeSignaling();
1583   // Do normal offer/answer and wait for some frames to be received in each
1584   // direction.
1585   caller()->AddAudioVideoTracks();
1586   callee()->AddAudioVideoTracks();
1587   caller()->CreateAndSetAndSignalOffer();
1588   ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1589   MediaExpectations media_expectations;
1590   media_expectations.ExpectBidirectionalAudioAndVideo();
1591   ASSERT_TRUE(ExpectNewFrames(media_expectations));
1592 }
1593 
1594 // Test getting cipher stats and UMA metrics when DTLS 1.0 is negotiated.
TEST_P(PeerConnectionIntegrationTest,Dtls10CipherStatsAndUmaMetrics)1595 TEST_P(PeerConnectionIntegrationTest, Dtls10CipherStatsAndUmaMetrics) {
1596   PeerConnectionFactory::Options dtls_10_options;
1597   dtls_10_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
1598   ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(dtls_10_options,
1599                                                       dtls_10_options));
1600   ConnectFakeSignaling();
1601   caller()->AddAudioVideoTracks();
1602   callee()->AddAudioVideoTracks();
1603   caller()->CreateAndSetAndSignalOffer();
1604   ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
1605   EXPECT_TRUE_WAIT(rtc::SSLStreamAdapter::IsAcceptableCipher(
1606                        caller()->OldGetStats()->DtlsCipher(), rtc::KT_DEFAULT),
1607                    kDefaultTimeout);
1608   EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
1609                  caller()->OldGetStats()->SrtpCipher(), kDefaultTimeout);
1610   // TODO(bugs.webrtc.org/9456): Fix it.
1611   EXPECT_METRIC_EQ(1, webrtc::metrics::NumEvents(
1612                           "WebRTC.PeerConnection.SrtpCryptoSuite.Audio",
1613                           kDefaultSrtpCryptoSuite));
1614 }
1615 
1616 // Test getting cipher stats and UMA metrics when DTLS 1.2 is negotiated.
TEST_P(PeerConnectionIntegrationTest,Dtls12CipherStatsAndUmaMetrics)1617 TEST_P(PeerConnectionIntegrationTest, Dtls12CipherStatsAndUmaMetrics) {
1618   PeerConnectionFactory::Options dtls_12_options;
1619   dtls_12_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
1620   ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(dtls_12_options,
1621                                                       dtls_12_options));
1622   ConnectFakeSignaling();
1623   caller()->AddAudioVideoTracks();
1624   callee()->AddAudioVideoTracks();
1625   caller()->CreateAndSetAndSignalOffer();
1626   ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
1627   EXPECT_TRUE_WAIT(rtc::SSLStreamAdapter::IsAcceptableCipher(
1628                        caller()->OldGetStats()->DtlsCipher(), rtc::KT_DEFAULT),
1629                    kDefaultTimeout);
1630   EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
1631                  caller()->OldGetStats()->SrtpCipher(), kDefaultTimeout);
1632   // TODO(bugs.webrtc.org/9456): Fix it.
1633   EXPECT_METRIC_EQ(1, webrtc::metrics::NumEvents(
1634                           "WebRTC.PeerConnection.SrtpCryptoSuite.Audio",
1635                           kDefaultSrtpCryptoSuite));
1636 }
1637 
1638 // Test that DTLS 1.0 can be used if the caller supports DTLS 1.2 and the
1639 // callee only supports 1.0.
TEST_P(PeerConnectionIntegrationTest,CallerDtls12ToCalleeDtls10)1640 TEST_P(PeerConnectionIntegrationTest, CallerDtls12ToCalleeDtls10) {
1641   PeerConnectionFactory::Options caller_options;
1642   caller_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
1643   PeerConnectionFactory::Options callee_options;
1644   callee_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
1645   ASSERT_TRUE(
1646       CreatePeerConnectionWrappersWithOptions(caller_options, callee_options));
1647   ConnectFakeSignaling();
1648   // Do normal offer/answer and wait for some frames to be received in each
1649   // direction.
1650   caller()->AddAudioVideoTracks();
1651   callee()->AddAudioVideoTracks();
1652   caller()->CreateAndSetAndSignalOffer();
1653   ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1654   MediaExpectations media_expectations;
1655   media_expectations.ExpectBidirectionalAudioAndVideo();
1656   ASSERT_TRUE(ExpectNewFrames(media_expectations));
1657 }
1658 
1659 // Test that DTLS 1.0 can be used if the caller only supports DTLS 1.0 and the
1660 // callee supports 1.2.
TEST_P(PeerConnectionIntegrationTest,CallerDtls10ToCalleeDtls12)1661 TEST_P(PeerConnectionIntegrationTest, CallerDtls10ToCalleeDtls12) {
1662   PeerConnectionFactory::Options caller_options;
1663   caller_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
1664   PeerConnectionFactory::Options callee_options;
1665   callee_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
1666   ASSERT_TRUE(
1667       CreatePeerConnectionWrappersWithOptions(caller_options, callee_options));
1668   ConnectFakeSignaling();
1669   // Do normal offer/answer and wait for some frames to be received in each
1670   // direction.
1671   caller()->AddAudioVideoTracks();
1672   callee()->AddAudioVideoTracks();
1673   caller()->CreateAndSetAndSignalOffer();
1674   ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1675   MediaExpectations media_expectations;
1676   media_expectations.ExpectBidirectionalAudioAndVideo();
1677   ASSERT_TRUE(ExpectNewFrames(media_expectations));
1678 }
1679 
1680 // The three tests below verify that "enable_aes128_sha1_32_crypto_cipher"
1681 // works as expected; the cipher should only be used if enabled by both sides.
TEST_P(PeerConnectionIntegrationTest,Aes128Sha1_32_CipherNotUsedWhenOnlyCallerSupported)1682 TEST_P(PeerConnectionIntegrationTest,
1683        Aes128Sha1_32_CipherNotUsedWhenOnlyCallerSupported) {
1684   PeerConnectionFactory::Options caller_options;
1685   caller_options.crypto_options.srtp.enable_aes128_sha1_32_crypto_cipher = true;
1686   PeerConnectionFactory::Options callee_options;
1687   callee_options.crypto_options.srtp.enable_aes128_sha1_32_crypto_cipher =
1688       false;
1689   int expected_cipher_suite = rtc::kSrtpAes128CmSha1_80;
1690   TestNegotiatedCipherSuite(caller_options, callee_options,
1691                             expected_cipher_suite);
1692 }
1693 
TEST_P(PeerConnectionIntegrationTest,Aes128Sha1_32_CipherNotUsedWhenOnlyCalleeSupported)1694 TEST_P(PeerConnectionIntegrationTest,
1695        Aes128Sha1_32_CipherNotUsedWhenOnlyCalleeSupported) {
1696   PeerConnectionFactory::Options caller_options;
1697   caller_options.crypto_options.srtp.enable_aes128_sha1_32_crypto_cipher =
1698       false;
1699   PeerConnectionFactory::Options callee_options;
1700   callee_options.crypto_options.srtp.enable_aes128_sha1_32_crypto_cipher = true;
1701   int expected_cipher_suite = rtc::kSrtpAes128CmSha1_80;
1702   TestNegotiatedCipherSuite(caller_options, callee_options,
1703                             expected_cipher_suite);
1704 }
1705 
TEST_P(PeerConnectionIntegrationTest,Aes128Sha1_32_CipherUsedWhenSupported)1706 TEST_P(PeerConnectionIntegrationTest, Aes128Sha1_32_CipherUsedWhenSupported) {
1707   PeerConnectionFactory::Options caller_options;
1708   caller_options.crypto_options.srtp.enable_aes128_sha1_32_crypto_cipher = true;
1709   PeerConnectionFactory::Options callee_options;
1710   callee_options.crypto_options.srtp.enable_aes128_sha1_32_crypto_cipher = true;
1711   int expected_cipher_suite = rtc::kSrtpAes128CmSha1_32;
1712   TestNegotiatedCipherSuite(caller_options, callee_options,
1713                             expected_cipher_suite);
1714 }
1715 
1716 // Test that a non-GCM cipher is used if both sides only support non-GCM.
TEST_P(PeerConnectionIntegrationTest,NonGcmCipherUsedWhenGcmNotSupported)1717 TEST_P(PeerConnectionIntegrationTest, NonGcmCipherUsedWhenGcmNotSupported) {
1718   bool local_gcm_enabled = false;
1719   bool remote_gcm_enabled = false;
1720   bool aes_ctr_enabled = true;
1721   int expected_cipher_suite = kDefaultSrtpCryptoSuite;
1722   TestGcmNegotiationUsesCipherSuite(local_gcm_enabled, remote_gcm_enabled,
1723                                     aes_ctr_enabled, expected_cipher_suite);
1724 }
1725 
1726 // Test that a GCM cipher is used if both ends support it and non-GCM is
1727 // disabled.
TEST_P(PeerConnectionIntegrationTest,GcmCipherUsedWhenOnlyGcmSupported)1728 TEST_P(PeerConnectionIntegrationTest, GcmCipherUsedWhenOnlyGcmSupported) {
1729   bool local_gcm_enabled = true;
1730   bool remote_gcm_enabled = true;
1731   bool aes_ctr_enabled = false;
1732   int expected_cipher_suite = kDefaultSrtpCryptoSuiteGcm;
1733   TestGcmNegotiationUsesCipherSuite(local_gcm_enabled, remote_gcm_enabled,
1734                                     aes_ctr_enabled, expected_cipher_suite);
1735 }
1736 
1737 // Verify that media can be transmitted end-to-end when GCM crypto suites are
1738 // enabled. Note that the above tests, such as GcmCipherUsedWhenGcmSupported,
1739 // only verify that a GCM cipher is negotiated, and not necessarily that SRTP
1740 // works with it.
TEST_P(PeerConnectionIntegrationTest,EndToEndCallWithGcmCipher)1741 TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithGcmCipher) {
1742   PeerConnectionFactory::Options gcm_options;
1743   gcm_options.crypto_options.srtp.enable_gcm_crypto_suites = true;
1744   gcm_options.crypto_options.srtp.enable_aes128_sha1_80_crypto_cipher = false;
1745   ASSERT_TRUE(
1746       CreatePeerConnectionWrappersWithOptions(gcm_options, gcm_options));
1747   ConnectFakeSignaling();
1748   // Do normal offer/answer and wait for some frames to be received in each
1749   // direction.
1750   caller()->AddAudioVideoTracks();
1751   callee()->AddAudioVideoTracks();
1752   caller()->CreateAndSetAndSignalOffer();
1753   ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1754   MediaExpectations media_expectations;
1755   media_expectations.ExpectBidirectionalAudioAndVideo();
1756   ASSERT_TRUE(ExpectNewFrames(media_expectations));
1757 }
1758 
1759 // Test that the ICE connection and gathering states eventually reach
1760 // "complete".
TEST_P(PeerConnectionIntegrationTest,IceStatesReachCompletion)1761 TEST_P(PeerConnectionIntegrationTest, IceStatesReachCompletion) {
1762   ASSERT_TRUE(CreatePeerConnectionWrappers());
1763   ConnectFakeSignaling();
1764   // Do normal offer/answer.
1765   caller()->AddAudioVideoTracks();
1766   callee()->AddAudioVideoTracks();
1767   caller()->CreateAndSetAndSignalOffer();
1768   ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1769   EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceGatheringComplete,
1770                  caller()->ice_gathering_state(), kMaxWaitForFramesMs);
1771   EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceGatheringComplete,
1772                  callee()->ice_gathering_state(), kMaxWaitForFramesMs);
1773   // After the best candidate pair is selected and all candidates are signaled,
1774   // the ICE connection state should reach "complete".
1775   // TODO(deadbeef): Currently, the ICE "controlled" agent (the
1776   // answerer/"callee" by default) only reaches "connected". When this is
1777   // fixed, this test should be updated.
1778   EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
1779                  caller()->ice_connection_state(), kDefaultTimeout);
1780   EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
1781                  callee()->ice_connection_state(), kDefaultTimeout);
1782 }
1783 
1784 constexpr int kOnlyLocalPorts = cricket::PORTALLOCATOR_DISABLE_STUN |
1785                                 cricket::PORTALLOCATOR_DISABLE_RELAY |
1786                                 cricket::PORTALLOCATOR_DISABLE_TCP;
1787 
1788 // Use a mock resolver to resolve the hostname back to the original IP on both
1789 // sides and check that the ICE connection connects.
TEST_P(PeerConnectionIntegrationTest,IceStatesReachCompletionWithRemoteHostname)1790 TEST_P(PeerConnectionIntegrationTest,
1791        IceStatesReachCompletionWithRemoteHostname) {
1792   auto caller_resolver_factory =
1793       std::make_unique<NiceMock<webrtc::MockAsyncResolverFactory>>();
1794   auto callee_resolver_factory =
1795       std::make_unique<NiceMock<webrtc::MockAsyncResolverFactory>>();
1796   NiceMock<rtc::MockAsyncResolver> callee_async_resolver;
1797   NiceMock<rtc::MockAsyncResolver> caller_async_resolver;
1798 
1799   // This also verifies that the injected AsyncResolverFactory is used by
1800   // P2PTransportChannel.
1801   EXPECT_CALL(*caller_resolver_factory, Create())
1802       .WillOnce(Return(&caller_async_resolver));
1803   webrtc::PeerConnectionDependencies caller_deps(nullptr);
1804   caller_deps.async_resolver_factory = std::move(caller_resolver_factory);
1805 
1806   EXPECT_CALL(*callee_resolver_factory, Create())
1807       .WillOnce(Return(&callee_async_resolver));
1808   webrtc::PeerConnectionDependencies callee_deps(nullptr);
1809   callee_deps.async_resolver_factory = std::move(callee_resolver_factory);
1810 
1811   PeerConnectionInterface::RTCConfiguration config;
1812   config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
1813   config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyRequire;
1814 
1815   ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndDeps(
1816       config, std::move(caller_deps), config, std::move(callee_deps)));
1817 
1818   caller()->SetRemoteAsyncResolver(&callee_async_resolver);
1819   callee()->SetRemoteAsyncResolver(&caller_async_resolver);
1820 
1821   // Enable hostname candidates with mDNS names.
1822   caller()->SetMdnsResponder(
1823       std::make_unique<webrtc::FakeMdnsResponder>(network_thread()));
1824   callee()->SetMdnsResponder(
1825       std::make_unique<webrtc::FakeMdnsResponder>(network_thread()));
1826 
1827   SetPortAllocatorFlags(kOnlyLocalPorts, kOnlyLocalPorts);
1828 
1829   ConnectFakeSignaling();
1830   caller()->AddAudioVideoTracks();
1831   callee()->AddAudioVideoTracks();
1832   caller()->CreateAndSetAndSignalOffer();
1833   ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1834   EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
1835                  caller()->ice_connection_state(), kDefaultTimeout);
1836   EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
1837                  callee()->ice_connection_state(), kDefaultTimeout);
1838 
1839   EXPECT_METRIC_EQ(1, webrtc::metrics::NumEvents(
1840                           "WebRTC.PeerConnection.CandidatePairType_UDP",
1841                           webrtc::kIceCandidatePairHostNameHostName));
1842   DestroyPeerConnections();
1843 }
1844 
1845 // Test that firewalling the ICE connection causes the clients to identify the
1846 // disconnected state and then removing the firewall causes them to reconnect.
1847 class PeerConnectionIntegrationIceStatesTest
1848     : public PeerConnectionIntegrationBaseTest,
1849       public ::testing::WithParamInterface<
1850           std::tuple<SdpSemantics, std::tuple<std::string, uint32_t>>> {
1851  protected:
PeerConnectionIntegrationIceStatesTest()1852   PeerConnectionIntegrationIceStatesTest()
1853       : PeerConnectionIntegrationBaseTest(std::get<0>(GetParam())) {
1854     port_allocator_flags_ = std::get<1>(std::get<1>(GetParam()));
1855   }
1856 
StartStunServer(const SocketAddress & server_address)1857   void StartStunServer(const SocketAddress& server_address) {
1858     stun_server_.reset(
1859         cricket::TestStunServer::Create(firewall(), server_address));
1860   }
1861 
TestIPv6()1862   bool TestIPv6() {
1863     return (port_allocator_flags_ & cricket::PORTALLOCATOR_ENABLE_IPV6);
1864   }
1865 
SetPortAllocatorFlags()1866   void SetPortAllocatorFlags() {
1867     PeerConnectionIntegrationBaseTest::SetPortAllocatorFlags(
1868         port_allocator_flags_, port_allocator_flags_);
1869   }
1870 
CallerAddresses()1871   std::vector<SocketAddress> CallerAddresses() {
1872     std::vector<SocketAddress> addresses;
1873     addresses.push_back(SocketAddress("1.1.1.1", 0));
1874     if (TestIPv6()) {
1875       addresses.push_back(SocketAddress("1111:0:a:b:c:d:e:f", 0));
1876     }
1877     return addresses;
1878   }
1879 
CalleeAddresses()1880   std::vector<SocketAddress> CalleeAddresses() {
1881     std::vector<SocketAddress> addresses;
1882     addresses.push_back(SocketAddress("2.2.2.2", 0));
1883     if (TestIPv6()) {
1884       addresses.push_back(SocketAddress("2222:0:a:b:c:d:e:f", 0));
1885     }
1886     return addresses;
1887   }
1888 
SetUpNetworkInterfaces()1889   void SetUpNetworkInterfaces() {
1890     // Remove the default interfaces added by the test infrastructure.
1891     caller()->network_manager()->RemoveInterface(kDefaultLocalAddress);
1892     callee()->network_manager()->RemoveInterface(kDefaultLocalAddress);
1893 
1894     // Add network addresses for test.
1895     for (const auto& caller_address : CallerAddresses()) {
1896       caller()->network_manager()->AddInterface(caller_address);
1897     }
1898     for (const auto& callee_address : CalleeAddresses()) {
1899       callee()->network_manager()->AddInterface(callee_address);
1900     }
1901   }
1902 
1903  private:
1904   uint32_t port_allocator_flags_;
1905   std::unique_ptr<cricket::TestStunServer> stun_server_;
1906 };
1907 
1908 // Ensure FakeClockForTest is constructed first (see class for rationale).
1909 class PeerConnectionIntegrationIceStatesTestWithFakeClock
1910     : public FakeClockForTest,
1911       public PeerConnectionIntegrationIceStatesTest {};
1912 
1913 #if !defined(THREAD_SANITIZER)
1914 // This test provokes TSAN errors. bugs.webrtc.org/11282
1915 
1916 // Tests that if the connection doesn't get set up properly we eventually reach
1917 // the "failed" iceConnectionState.
TEST_P(PeerConnectionIntegrationIceStatesTestWithFakeClock,IceStateSetupFailure)1918 TEST_P(PeerConnectionIntegrationIceStatesTestWithFakeClock,
1919        IceStateSetupFailure) {
1920   // Block connections to/from the caller and wait for ICE to become
1921   // disconnected.
1922   for (const auto& caller_address : CallerAddresses()) {
1923     firewall()->AddRule(false, rtc::FP_ANY, rtc::FD_ANY, caller_address);
1924   }
1925 
1926   ASSERT_TRUE(CreatePeerConnectionWrappers());
1927   ConnectFakeSignaling();
1928   SetPortAllocatorFlags();
1929   SetUpNetworkInterfaces();
1930   caller()->AddAudioVideoTracks();
1931   caller()->CreateAndSetAndSignalOffer();
1932 
1933   // According to RFC7675, if there is no response within 30 seconds then the
1934   // peer should consider the other side to have rejected the connection. This
1935   // is signaled by the state transitioning to "failed".
1936   constexpr int kConsentTimeout = 30000;
1937   ASSERT_EQ_SIMULATED_WAIT(PeerConnectionInterface::kIceConnectionFailed,
1938                            caller()->standardized_ice_connection_state(),
1939                            kConsentTimeout, FakeClock());
1940 }
1941 
1942 #endif  // !defined(THREAD_SANITIZER)
1943 
1944 // Tests that the best connection is set to the appropriate IPv4/IPv6 connection
1945 // and that the statistics in the metric observers are updated correctly.
1946 // TODO(bugs.webrtc.org/12591): Flaky on Windows.
1947 #if defined(WEBRTC_WIN)
1948 #define MAYBE_VerifyBestConnection DISABLED_VerifyBestConnection
1949 #else
1950 #define MAYBE_VerifyBestConnection VerifyBestConnection
1951 #endif
TEST_P(PeerConnectionIntegrationIceStatesTest,MAYBE_VerifyBestConnection)1952 TEST_P(PeerConnectionIntegrationIceStatesTest, MAYBE_VerifyBestConnection) {
1953   ASSERT_TRUE(CreatePeerConnectionWrappers());
1954   ConnectFakeSignaling();
1955   SetPortAllocatorFlags();
1956   SetUpNetworkInterfaces();
1957   caller()->AddAudioVideoTracks();
1958   callee()->AddAudioVideoTracks();
1959   caller()->CreateAndSetAndSignalOffer();
1960 
1961   ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1962   EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
1963                  caller()->ice_connection_state(), kDefaultTimeout);
1964   EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
1965                  callee()->ice_connection_state(), kDefaultTimeout);
1966 
1967   // TODO(bugs.webrtc.org/9456): Fix it.
1968   const int num_best_ipv4 = webrtc::metrics::NumEvents(
1969       "WebRTC.PeerConnection.IPMetrics", webrtc::kBestConnections_IPv4);
1970   const int num_best_ipv6 = webrtc::metrics::NumEvents(
1971       "WebRTC.PeerConnection.IPMetrics", webrtc::kBestConnections_IPv6);
1972   if (TestIPv6()) {
1973     // When IPv6 is enabled, we should prefer an IPv6 connection over an IPv4
1974     // connection.
1975     EXPECT_METRIC_EQ(0, num_best_ipv4);
1976     EXPECT_METRIC_EQ(1, num_best_ipv6);
1977   } else {
1978     EXPECT_METRIC_EQ(1, num_best_ipv4);
1979     EXPECT_METRIC_EQ(0, num_best_ipv6);
1980   }
1981 
1982   EXPECT_METRIC_EQ(0, webrtc::metrics::NumEvents(
1983                           "WebRTC.PeerConnection.CandidatePairType_UDP",
1984                           webrtc::kIceCandidatePairHostHost));
1985   EXPECT_METRIC_EQ(1, webrtc::metrics::NumEvents(
1986                           "WebRTC.PeerConnection.CandidatePairType_UDP",
1987                           webrtc::kIceCandidatePairHostPublicHostPublic));
1988 }
1989 
1990 constexpr uint32_t kFlagsIPv4NoStun = cricket::PORTALLOCATOR_DISABLE_TCP |
1991                                       cricket::PORTALLOCATOR_DISABLE_STUN |
1992                                       cricket::PORTALLOCATOR_DISABLE_RELAY;
1993 constexpr uint32_t kFlagsIPv6NoStun =
1994     cricket::PORTALLOCATOR_DISABLE_TCP | cricket::PORTALLOCATOR_DISABLE_STUN |
1995     cricket::PORTALLOCATOR_ENABLE_IPV6 | cricket::PORTALLOCATOR_DISABLE_RELAY;
1996 constexpr uint32_t kFlagsIPv4Stun =
1997     cricket::PORTALLOCATOR_DISABLE_TCP | cricket::PORTALLOCATOR_DISABLE_RELAY;
1998 
1999 INSTANTIATE_TEST_SUITE_P(
2000     PeerConnectionIntegrationTest,
2001     PeerConnectionIntegrationIceStatesTest,
2002     Combine(Values(SdpSemantics::kPlanB_DEPRECATED, SdpSemantics::kUnifiedPlan),
2003             Values(std::make_pair("IPv4 no STUN", kFlagsIPv4NoStun),
2004                    std::make_pair("IPv6 no STUN", kFlagsIPv6NoStun),
2005                    std::make_pair("IPv4 with STUN", kFlagsIPv4Stun))));
2006 
2007 INSTANTIATE_TEST_SUITE_P(
2008     PeerConnectionIntegrationTest,
2009     PeerConnectionIntegrationIceStatesTestWithFakeClock,
2010     Combine(Values(SdpSemantics::kPlanB_DEPRECATED, SdpSemantics::kUnifiedPlan),
2011             Values(std::make_pair("IPv4 no STUN", kFlagsIPv4NoStun),
2012                    std::make_pair("IPv6 no STUN", kFlagsIPv6NoStun),
2013                    std::make_pair("IPv4 with STUN", kFlagsIPv4Stun))));
2014 
2015 // This test sets up a call between two parties with audio and video.
2016 // During the call, the caller restarts ICE and the test verifies that
2017 // new ICE candidates are generated and audio and video still can flow, and the
2018 // ICE state reaches completed again.
TEST_P(PeerConnectionIntegrationTest,MediaContinuesFlowingAfterIceRestart)2019 TEST_P(PeerConnectionIntegrationTest, MediaContinuesFlowingAfterIceRestart) {
2020   ASSERT_TRUE(CreatePeerConnectionWrappers());
2021   ConnectFakeSignaling();
2022   // Do normal offer/answer and wait for ICE to complete.
2023   caller()->AddAudioVideoTracks();
2024   callee()->AddAudioVideoTracks();
2025   caller()->CreateAndSetAndSignalOffer();
2026   ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2027   EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
2028                  caller()->ice_connection_state(), kMaxWaitForFramesMs);
2029   EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
2030                  callee()->ice_connection_state(), kMaxWaitForFramesMs);
2031 
2032   // To verify that the ICE restart actually occurs, get
2033   // ufrag/password/candidates before and after restart.
2034   // Create an SDP string of the first audio candidate for both clients.
2035   const webrtc::IceCandidateCollection* audio_candidates_caller =
2036       caller()->pc()->local_description()->candidates(0);
2037   const webrtc::IceCandidateCollection* audio_candidates_callee =
2038       callee()->pc()->local_description()->candidates(0);
2039   ASSERT_GT(audio_candidates_caller->count(), 0u);
2040   ASSERT_GT(audio_candidates_callee->count(), 0u);
2041   std::string caller_candidate_pre_restart;
2042   ASSERT_TRUE(
2043       audio_candidates_caller->at(0)->ToString(&caller_candidate_pre_restart));
2044   std::string callee_candidate_pre_restart;
2045   ASSERT_TRUE(
2046       audio_candidates_callee->at(0)->ToString(&callee_candidate_pre_restart));
2047   const cricket::SessionDescription* desc =
2048       caller()->pc()->local_description()->description();
2049   std::string caller_ufrag_pre_restart =
2050       desc->transport_infos()[0].description.ice_ufrag;
2051   desc = callee()->pc()->local_description()->description();
2052   std::string callee_ufrag_pre_restart =
2053       desc->transport_infos()[0].description.ice_ufrag;
2054 
2055   EXPECT_EQ(caller()->ice_candidate_pair_change_history().size(), 1u);
2056   // Have the caller initiate an ICE restart.
2057   caller()->SetOfferAnswerOptions(IceRestartOfferAnswerOptions());
2058   caller()->CreateAndSetAndSignalOffer();
2059   ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2060   EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
2061                  caller()->ice_connection_state(), kMaxWaitForFramesMs);
2062   EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
2063                  callee()->ice_connection_state(), kMaxWaitForFramesMs);
2064 
2065   // Grab the ufrags/candidates again.
2066   audio_candidates_caller = caller()->pc()->local_description()->candidates(0);
2067   audio_candidates_callee = callee()->pc()->local_description()->candidates(0);
2068   ASSERT_GT(audio_candidates_caller->count(), 0u);
2069   ASSERT_GT(audio_candidates_callee->count(), 0u);
2070   std::string caller_candidate_post_restart;
2071   ASSERT_TRUE(
2072       audio_candidates_caller->at(0)->ToString(&caller_candidate_post_restart));
2073   std::string callee_candidate_post_restart;
2074   ASSERT_TRUE(
2075       audio_candidates_callee->at(0)->ToString(&callee_candidate_post_restart));
2076   desc = caller()->pc()->local_description()->description();
2077   std::string caller_ufrag_post_restart =
2078       desc->transport_infos()[0].description.ice_ufrag;
2079   desc = callee()->pc()->local_description()->description();
2080   std::string callee_ufrag_post_restart =
2081       desc->transport_infos()[0].description.ice_ufrag;
2082   // Sanity check that an ICE restart was actually negotiated in SDP.
2083   ASSERT_NE(caller_candidate_pre_restart, caller_candidate_post_restart);
2084   ASSERT_NE(callee_candidate_pre_restart, callee_candidate_post_restart);
2085   ASSERT_NE(caller_ufrag_pre_restart, caller_ufrag_post_restart);
2086   ASSERT_NE(callee_ufrag_pre_restart, callee_ufrag_post_restart);
2087   EXPECT_GT(caller()->ice_candidate_pair_change_history().size(), 1u);
2088 
2089   // Ensure that additional frames are received after the ICE restart.
2090   MediaExpectations media_expectations;
2091   media_expectations.ExpectBidirectionalAudioAndVideo();
2092   ASSERT_TRUE(ExpectNewFrames(media_expectations));
2093 }
2094 
2095 // Verify that audio/video can be received end-to-end when ICE renomination is
2096 // enabled.
TEST_P(PeerConnectionIntegrationTest,EndToEndCallWithIceRenomination)2097 TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithIceRenomination) {
2098   PeerConnectionInterface::RTCConfiguration config;
2099   config.enable_ice_renomination = true;
2100   ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
2101   ConnectFakeSignaling();
2102   // Do normal offer/answer and wait for some frames to be received in each
2103   // direction.
2104   caller()->AddAudioVideoTracks();
2105   callee()->AddAudioVideoTracks();
2106   caller()->CreateAndSetAndSignalOffer();
2107   ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2108   // Sanity check that ICE renomination was actually negotiated.
2109   const cricket::SessionDescription* desc =
2110       caller()->pc()->local_description()->description();
2111   for (const cricket::TransportInfo& info : desc->transport_infos()) {
2112     ASSERT_THAT(info.description.transport_options, Contains("renomination"));
2113   }
2114   desc = callee()->pc()->local_description()->description();
2115   for (const cricket::TransportInfo& info : desc->transport_infos()) {
2116     ASSERT_THAT(info.description.transport_options, Contains("renomination"));
2117   }
2118   MediaExpectations media_expectations;
2119   media_expectations.ExpectBidirectionalAudioAndVideo();
2120   ASSERT_TRUE(ExpectNewFrames(media_expectations));
2121 }
2122 
2123 // With a max bundle policy and RTCP muxing, adding a new media description to
2124 // the connection should not affect ICE at all because the new media will use
2125 // the existing connection.
2126 // TODO(bugs.webrtc.org/12538): Fails on tsan.
2127 #if defined(THREAD_SANITIZER)
2128 #define MAYBE_AddMediaToConnectedBundleDoesNotRestartIce \
2129   DISABLED_AddMediaToConnectedBundleDoesNotRestartIce
2130 #else
2131 #define MAYBE_AddMediaToConnectedBundleDoesNotRestartIce \
2132   AddMediaToConnectedBundleDoesNotRestartIce
2133 #endif
TEST_P(PeerConnectionIntegrationTest,MAYBE_AddMediaToConnectedBundleDoesNotRestartIce)2134 TEST_P(PeerConnectionIntegrationTest,
2135        MAYBE_AddMediaToConnectedBundleDoesNotRestartIce) {
2136   PeerConnectionInterface::RTCConfiguration config;
2137   config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
2138   config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyRequire;
2139   ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(
2140       config, PeerConnectionInterface::RTCConfiguration()));
2141   ConnectFakeSignaling();
2142 
2143   caller()->AddAudioTrack();
2144   caller()->CreateAndSetAndSignalOffer();
2145   ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2146   ASSERT_EQ_WAIT(PeerConnectionInterface::kIceConnectionCompleted,
2147                  caller()->ice_connection_state(), kDefaultTimeout);
2148 
2149   caller()->clear_ice_connection_state_history();
2150 
2151   caller()->AddVideoTrack();
2152   caller()->CreateAndSetAndSignalOffer();
2153   ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2154 
2155   EXPECT_EQ(0u, caller()->ice_connection_state_history().size());
2156 }
2157 
2158 // This test sets up a call between two parties with audio and video. It then
2159 // renegotiates setting the video m-line to "port 0", then later renegotiates
2160 // again, enabling video.
TEST_P(PeerConnectionIntegrationTest,VideoFlowsAfterMediaSectionIsRejectedAndRecycled)2161 TEST_P(PeerConnectionIntegrationTest,
2162        VideoFlowsAfterMediaSectionIsRejectedAndRecycled) {
2163   ASSERT_TRUE(CreatePeerConnectionWrappers());
2164   ConnectFakeSignaling();
2165 
2166   // Do initial negotiation, only sending media from the caller. Will result in
2167   // video and audio recvonly "m=" sections.
2168   caller()->AddAudioVideoTracks();
2169   caller()->CreateAndSetAndSignalOffer();
2170   ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2171 
2172   // Negotiate again, disabling the video "m=" section (the callee will set the
2173   // port to 0 due to offer_to_receive_video = 0).
2174   if (sdp_semantics_ == SdpSemantics::kPlanB_DEPRECATED) {
2175     PeerConnectionInterface::RTCOfferAnswerOptions options;
2176     options.offer_to_receive_video = 0;
2177     callee()->SetOfferAnswerOptions(options);
2178   } else {
2179     callee()->SetRemoteOfferHandler([this] {
2180       callee()
2181           ->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)
2182           ->StopInternal();
2183     });
2184   }
2185   caller()->CreateAndSetAndSignalOffer();
2186   ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2187   // Sanity check that video "m=" section was actually rejected.
2188   const ContentInfo* answer_video_content = cricket::GetFirstVideoContent(
2189       callee()->pc()->local_description()->description());
2190   ASSERT_NE(nullptr, answer_video_content);
2191   ASSERT_TRUE(answer_video_content->rejected);
2192 
2193   // Enable video and do negotiation again, making sure video is received
2194   // end-to-end, also adding media stream to callee.
2195   if (sdp_semantics_ == SdpSemantics::kPlanB_DEPRECATED) {
2196     PeerConnectionInterface::RTCOfferAnswerOptions options;
2197     options.offer_to_receive_video = 1;
2198     callee()->SetOfferAnswerOptions(options);
2199   } else {
2200     // The caller's transceiver is stopped, so we need to add another track.
2201     auto caller_transceiver =
2202         caller()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO);
2203     EXPECT_EQ(nullptr, caller_transceiver.get());
2204     caller()->AddVideoTrack();
2205   }
2206   callee()->AddVideoTrack();
2207   callee()->SetRemoteOfferHandler(nullptr);
2208   caller()->CreateAndSetAndSignalOffer();
2209   ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2210 
2211   // Verify the caller receives frames from the newly added stream, and the
2212   // callee receives additional frames from the re-enabled video m= section.
2213   MediaExpectations media_expectations;
2214   media_expectations.CalleeExpectsSomeAudio();
2215   media_expectations.ExpectBidirectionalVideo();
2216   ASSERT_TRUE(ExpectNewFrames(media_expectations));
2217 }
2218 
2219 // This tests that if we negotiate after calling CreateSender but before we
2220 // have a track, then set a track later, frames from the newly-set track are
2221 // received end-to-end.
TEST_F(PeerConnectionIntegrationTestPlanB,MediaFlowsAfterEarlyWarmupWithCreateSender)2222 TEST_F(PeerConnectionIntegrationTestPlanB,
2223        MediaFlowsAfterEarlyWarmupWithCreateSender) {
2224   ASSERT_TRUE(CreatePeerConnectionWrappers());
2225   ConnectFakeSignaling();
2226   auto caller_audio_sender =
2227       caller()->pc()->CreateSender("audio", "caller_stream");
2228   auto caller_video_sender =
2229       caller()->pc()->CreateSender("video", "caller_stream");
2230   auto callee_audio_sender =
2231       callee()->pc()->CreateSender("audio", "callee_stream");
2232   auto callee_video_sender =
2233       callee()->pc()->CreateSender("video", "callee_stream");
2234   caller()->CreateAndSetAndSignalOffer();
2235   ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
2236   // Wait for ICE to complete, without any tracks being set.
2237   EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
2238                  caller()->ice_connection_state(), kMaxWaitForFramesMs);
2239   EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
2240                  callee()->ice_connection_state(), kMaxWaitForFramesMs);
2241   // Now set the tracks, and expect frames to immediately start flowing.
2242   EXPECT_TRUE(
2243       caller_audio_sender->SetTrack(caller()->CreateLocalAudioTrack().get()));
2244   EXPECT_TRUE(
2245       caller_video_sender->SetTrack(caller()->CreateLocalVideoTrack().get()));
2246   EXPECT_TRUE(
2247       callee_audio_sender->SetTrack(callee()->CreateLocalAudioTrack().get()));
2248   EXPECT_TRUE(
2249       callee_video_sender->SetTrack(callee()->CreateLocalVideoTrack().get()));
2250   MediaExpectations media_expectations;
2251   media_expectations.ExpectBidirectionalAudioAndVideo();
2252   ASSERT_TRUE(ExpectNewFrames(media_expectations));
2253 }
2254 
2255 // This tests that if we negotiate after calling AddTransceiver but before we
2256 // have a track, then set a track later, frames from the newly-set tracks are
2257 // received end-to-end.
TEST_F(PeerConnectionIntegrationTestUnifiedPlan,MediaFlowsAfterEarlyWarmupWithAddTransceiver)2258 TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
2259        MediaFlowsAfterEarlyWarmupWithAddTransceiver) {
2260   ASSERT_TRUE(CreatePeerConnectionWrappers());
2261   ConnectFakeSignaling();
2262   auto audio_result = caller()->pc()->AddTransceiver(cricket::MEDIA_TYPE_AUDIO);
2263   ASSERT_EQ(RTCErrorType::NONE, audio_result.error().type());
2264   auto caller_audio_sender = audio_result.MoveValue()->sender();
2265   auto video_result = caller()->pc()->AddTransceiver(cricket::MEDIA_TYPE_VIDEO);
2266   ASSERT_EQ(RTCErrorType::NONE, video_result.error().type());
2267   auto caller_video_sender = video_result.MoveValue()->sender();
2268   callee()->SetRemoteOfferHandler([this] {
2269     ASSERT_EQ(2u, callee()->pc()->GetTransceivers().size());
2270     callee()->pc()->GetTransceivers()[0]->SetDirectionWithError(
2271         RtpTransceiverDirection::kSendRecv);
2272     callee()->pc()->GetTransceivers()[1]->SetDirectionWithError(
2273         RtpTransceiverDirection::kSendRecv);
2274   });
2275   caller()->CreateAndSetAndSignalOffer();
2276   ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
2277   // Wait for ICE to complete, without any tracks being set.
2278   EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
2279                  caller()->ice_connection_state(), kMaxWaitForFramesMs);
2280   EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
2281                  callee()->ice_connection_state(), kMaxWaitForFramesMs);
2282   // Now set the tracks, and expect frames to immediately start flowing.
2283   auto callee_audio_sender = callee()->pc()->GetSenders()[0];
2284   auto callee_video_sender = callee()->pc()->GetSenders()[1];
2285   ASSERT_TRUE(
2286       caller_audio_sender->SetTrack(caller()->CreateLocalAudioTrack().get()));
2287   ASSERT_TRUE(
2288       caller_video_sender->SetTrack(caller()->CreateLocalVideoTrack().get()));
2289   ASSERT_TRUE(
2290       callee_audio_sender->SetTrack(callee()->CreateLocalAudioTrack().get()));
2291   ASSERT_TRUE(
2292       callee_video_sender->SetTrack(callee()->CreateLocalVideoTrack().get()));
2293   MediaExpectations media_expectations;
2294   media_expectations.ExpectBidirectionalAudioAndVideo();
2295   ASSERT_TRUE(ExpectNewFrames(media_expectations));
2296 }
2297 
2298 // This test verifies that a remote video track can be added via AddStream,
2299 // and sent end-to-end. For this particular test, it's simply echoed back
2300 // from the caller to the callee, rather than being forwarded to a third
2301 // PeerConnection.
TEST_F(PeerConnectionIntegrationTestPlanB,CanSendRemoteVideoTrack)2302 TEST_F(PeerConnectionIntegrationTestPlanB, CanSendRemoteVideoTrack) {
2303   ASSERT_TRUE(CreatePeerConnectionWrappers());
2304   ConnectFakeSignaling();
2305   // Just send a video track from the caller.
2306   caller()->AddVideoTrack();
2307   caller()->CreateAndSetAndSignalOffer();
2308   ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
2309   ASSERT_EQ(1U, callee()->remote_streams()->count());
2310 
2311   // Echo the stream back, and do a new offer/anwer (initiated by callee this
2312   // time).
2313   callee()->pc()->AddStream(callee()->remote_streams()->at(0));
2314   callee()->CreateAndSetAndSignalOffer();
2315   ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
2316 
2317   MediaExpectations media_expectations;
2318   media_expectations.ExpectBidirectionalVideo();
2319   ASSERT_TRUE(ExpectNewFrames(media_expectations));
2320 }
2321 
2322 #if !defined(THREAD_SANITIZER)
2323 // This test provokes TSAN errors. bugs.webrtc.org/11282
2324 
2325 // Test that we achieve the expected end-to-end connection time, using a
2326 // fake clock and simulated latency on the media and signaling paths.
2327 // We use a TURN<->TURN connection because this is usually the quickest to
2328 // set up initially, especially when we're confident the connection will work
2329 // and can start sending media before we get a STUN response.
2330 //
2331 // With various optimizations enabled, here are the network delays we expect to
2332 // be on the critical path:
2333 // 1. 2 signaling trips: Signaling offer and offerer's TURN candidate, then
2334 //                       signaling answer (with DTLS fingerprint).
2335 // 2. 9 media hops: Rest of the DTLS handshake. 3 hops in each direction when
2336 //                  using TURN<->TURN pair, and DTLS exchange is 4 packets,
2337 //                  the first of which should have arrived before the answer.
TEST_P(PeerConnectionIntegrationTestWithFakeClock,EndToEndConnectionTimeWithTurnTurnPair)2338 TEST_P(PeerConnectionIntegrationTestWithFakeClock,
2339        EndToEndConnectionTimeWithTurnTurnPair) {
2340   static constexpr int media_hop_delay_ms = 50;
2341   static constexpr int signaling_trip_delay_ms = 500;
2342   // For explanation of these values, see comment above.
2343   static constexpr int required_media_hops = 9;
2344   static constexpr int required_signaling_trips = 2;
2345   // For internal delays (such as posting an event asychronously).
2346   static constexpr int allowed_internal_delay_ms = 20;
2347   static constexpr int total_connection_time_ms =
2348       media_hop_delay_ms * required_media_hops +
2349       signaling_trip_delay_ms * required_signaling_trips +
2350       allowed_internal_delay_ms;
2351 
2352   static const rtc::SocketAddress turn_server_1_internal_address{"88.88.88.0",
2353                                                                  3478};
2354   static const rtc::SocketAddress turn_server_1_external_address{"88.88.88.1",
2355                                                                  0};
2356   static const rtc::SocketAddress turn_server_2_internal_address{"99.99.99.0",
2357                                                                  3478};
2358   static const rtc::SocketAddress turn_server_2_external_address{"99.99.99.1",
2359                                                                  0};
2360   cricket::TestTurnServer* turn_server_1 = CreateTurnServer(
2361       turn_server_1_internal_address, turn_server_1_external_address);
2362 
2363   cricket::TestTurnServer* turn_server_2 = CreateTurnServer(
2364       turn_server_2_internal_address, turn_server_2_external_address);
2365   // Bypass permission check on received packets so media can be sent before
2366   // the candidate is signaled.
2367   SendTask(network_thread(), [turn_server_1] {
2368     turn_server_1->set_enable_permission_checks(false);
2369   });
2370   SendTask(network_thread(), [turn_server_2] {
2371     turn_server_2->set_enable_permission_checks(false);
2372   });
2373 
2374   PeerConnectionInterface::RTCConfiguration client_1_config;
2375   webrtc::PeerConnectionInterface::IceServer ice_server_1;
2376   ice_server_1.urls.push_back("turn:88.88.88.0:3478");
2377   ice_server_1.username = "test";
2378   ice_server_1.password = "test";
2379   client_1_config.servers.push_back(ice_server_1);
2380   client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
2381   client_1_config.presume_writable_when_fully_relayed = true;
2382 
2383   PeerConnectionInterface::RTCConfiguration client_2_config;
2384   webrtc::PeerConnectionInterface::IceServer ice_server_2;
2385   ice_server_2.urls.push_back("turn:99.99.99.0:3478");
2386   ice_server_2.username = "test";
2387   ice_server_2.password = "test";
2388   client_2_config.servers.push_back(ice_server_2);
2389   client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
2390   client_2_config.presume_writable_when_fully_relayed = true;
2391 
2392   ASSERT_TRUE(
2393       CreatePeerConnectionWrappersWithConfig(client_1_config, client_2_config));
2394   // Set up the simulated delays.
2395   SetSignalingDelayMs(signaling_trip_delay_ms);
2396   ConnectFakeSignaling();
2397   virtual_socket_server()->set_delay_mean(media_hop_delay_ms);
2398   virtual_socket_server()->UpdateDelayDistribution();
2399 
2400   // Set "offer to receive audio/video" without adding any tracks, so we just
2401   // set up ICE/DTLS with no media.
2402   PeerConnectionInterface::RTCOfferAnswerOptions options;
2403   options.offer_to_receive_audio = 1;
2404   options.offer_to_receive_video = 1;
2405   caller()->SetOfferAnswerOptions(options);
2406   caller()->CreateAndSetAndSignalOffer();
2407   EXPECT_TRUE_SIMULATED_WAIT(DtlsConnected(), total_connection_time_ms,
2408                              FakeClock());
2409   // Closing the PeerConnections destroys the ports before the ScopedFakeClock.
2410   // If this is not done a DCHECK can be hit in ports.cc, because a large
2411   // negative number is calculated for the rtt due to the global clock changing.
2412   ClosePeerConnections();
2413 }
2414 
TEST_P(PeerConnectionIntegrationTestWithFakeClock,OnIceCandidateFlushesGetStatsCache)2415 TEST_P(PeerConnectionIntegrationTestWithFakeClock,
2416        OnIceCandidateFlushesGetStatsCache) {
2417   ASSERT_TRUE(CreatePeerConnectionWrappers());
2418   ConnectFakeSignaling();
2419   caller()->AddAudioTrack();
2420 
2421   // Call getStats, assert there are no candidates.
2422   rtc::scoped_refptr<const webrtc::RTCStatsReport> first_report =
2423       caller()->NewGetStats();
2424   ASSERT_TRUE(first_report);
2425   auto first_candidate_stats =
2426       first_report->GetStatsOfType<webrtc::RTCLocalIceCandidateStats>();
2427   ASSERT_EQ(first_candidate_stats.size(), 0u);
2428 
2429   // Create an offer at the caller and set it as remote description on the
2430   // callee.
2431   caller()->CreateAndSetAndSignalOffer();
2432   // Call getStats again, assert there are candidates now.
2433   rtc::scoped_refptr<const webrtc::RTCStatsReport> second_report =
2434       caller()->NewGetStats();
2435   ASSERT_TRUE(second_report);
2436   auto second_candidate_stats =
2437       second_report->GetStatsOfType<webrtc::RTCLocalIceCandidateStats>();
2438   ASSERT_NE(second_candidate_stats.size(), 0u);
2439 
2440   // The fake clock ensures that no time has passed so the cache must have been
2441   // explicitly invalidated.
2442   EXPECT_EQ(first_report->timestamp_us(), second_report->timestamp_us());
2443 }
2444 
TEST_P(PeerConnectionIntegrationTestWithFakeClock,AddIceCandidateFlushesGetStatsCache)2445 TEST_P(PeerConnectionIntegrationTestWithFakeClock,
2446        AddIceCandidateFlushesGetStatsCache) {
2447   ASSERT_TRUE(CreatePeerConnectionWrappers());
2448   ConnectFakeSignalingForSdpOnly();
2449   caller()->AddAudioTrack();
2450 
2451   // Start candidate gathering and wait for it to complete. Candidates are not
2452   // signalled.
2453   caller()->CreateAndSetAndSignalOffer();
2454   ASSERT_TRUE_SIMULATED_WAIT(caller()->IceGatheringStateComplete(),
2455                              kDefaultTimeout, FakeClock());
2456 
2457   // Call getStats, assert there are no candidates.
2458   rtc::scoped_refptr<const webrtc::RTCStatsReport> first_report =
2459       caller()->NewGetStats();
2460   ASSERT_TRUE(first_report);
2461   auto first_candidate_stats =
2462       first_report->GetStatsOfType<webrtc::RTCRemoteIceCandidateStats>();
2463   ASSERT_EQ(first_candidate_stats.size(), 0u);
2464 
2465   // Add a "fake" candidate.
2466   absl::optional<RTCError> result;
2467   caller()->pc()->AddIceCandidate(
2468       absl::WrapUnique(webrtc::CreateIceCandidate(
2469           "", 0,
2470           "candidate:2214029314 1 udp 2122260223 127.0.0.1 49152 typ host",
2471           nullptr)),
2472       [&result](RTCError r) { result = r; });
2473   ASSERT_TRUE_WAIT(result.has_value(), kDefaultTimeout);
2474   ASSERT_TRUE(result.value().ok());
2475 
2476   // Call getStats again, assert there is a remote candidate now.
2477   rtc::scoped_refptr<const webrtc::RTCStatsReport> second_report =
2478       caller()->NewGetStats();
2479   ASSERT_TRUE(second_report);
2480   auto second_candidate_stats =
2481       second_report->GetStatsOfType<webrtc::RTCRemoteIceCandidateStats>();
2482   ASSERT_EQ(second_candidate_stats.size(), 1u);
2483 
2484   // The fake clock ensures that no time has passed so the cache must have been
2485   // explicitly invalidated.
2486   EXPECT_EQ(first_report->timestamp_us(), second_report->timestamp_us());
2487 }
2488 
2489 #endif  // !defined(THREAD_SANITIZER)
2490 
2491 // Verify that a TurnCustomizer passed in through RTCConfiguration
2492 // is actually used by the underlying TURN candidate pair.
2493 // Note that turnport_unittest.cc contains more detailed, lower-level tests.
TEST_P(PeerConnectionIntegrationTest,TurnCustomizerUsedForTurnConnections)2494 TEST_P(PeerConnectionIntegrationTest, TurnCustomizerUsedForTurnConnections) {
2495   static const rtc::SocketAddress turn_server_1_internal_address{"88.88.88.0",
2496                                                                  3478};
2497   static const rtc::SocketAddress turn_server_1_external_address{"88.88.88.1",
2498                                                                  0};
2499   static const rtc::SocketAddress turn_server_2_internal_address{"99.99.99.0",
2500                                                                  3478};
2501   static const rtc::SocketAddress turn_server_2_external_address{"99.99.99.1",
2502                                                                  0};
2503   CreateTurnServer(turn_server_1_internal_address,
2504                    turn_server_1_external_address);
2505   CreateTurnServer(turn_server_2_internal_address,
2506                    turn_server_2_external_address);
2507 
2508   PeerConnectionInterface::RTCConfiguration client_1_config;
2509   webrtc::PeerConnectionInterface::IceServer ice_server_1;
2510   ice_server_1.urls.push_back("turn:88.88.88.0:3478");
2511   ice_server_1.username = "test";
2512   ice_server_1.password = "test";
2513   client_1_config.servers.push_back(ice_server_1);
2514   client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
2515   auto* customizer1 = CreateTurnCustomizer();
2516   client_1_config.turn_customizer = customizer1;
2517 
2518   PeerConnectionInterface::RTCConfiguration client_2_config;
2519   webrtc::PeerConnectionInterface::IceServer ice_server_2;
2520   ice_server_2.urls.push_back("turn:99.99.99.0:3478");
2521   ice_server_2.username = "test";
2522   ice_server_2.password = "test";
2523   client_2_config.servers.push_back(ice_server_2);
2524   client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
2525   auto* customizer2 = CreateTurnCustomizer();
2526   client_2_config.turn_customizer = customizer2;
2527 
2528   ASSERT_TRUE(
2529       CreatePeerConnectionWrappersWithConfig(client_1_config, client_2_config));
2530   ConnectFakeSignaling();
2531 
2532   // Set "offer to receive audio/video" without adding any tracks, so we just
2533   // set up ICE/DTLS with no media.
2534   PeerConnectionInterface::RTCOfferAnswerOptions options;
2535   options.offer_to_receive_audio = 1;
2536   options.offer_to_receive_video = 1;
2537   caller()->SetOfferAnswerOptions(options);
2538   caller()->CreateAndSetAndSignalOffer();
2539   ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
2540 
2541   ExpectTurnCustomizerCountersIncremented(customizer1);
2542   ExpectTurnCustomizerCountersIncremented(customizer2);
2543 }
2544 
2545 // Verifies that you can use TCP instead of UDP to connect to a TURN server and
2546 // send media between the caller and the callee.
TEST_P(PeerConnectionIntegrationTest,TCPUsedForTurnConnections)2547 TEST_P(PeerConnectionIntegrationTest, TCPUsedForTurnConnections) {
2548   static const rtc::SocketAddress turn_server_internal_address{"88.88.88.0",
2549                                                                3478};
2550   static const rtc::SocketAddress turn_server_external_address{"88.88.88.1", 0};
2551 
2552   // Enable TCP for the fake turn server.
2553   CreateTurnServer(turn_server_internal_address, turn_server_external_address,
2554                    cricket::PROTO_TCP);
2555 
2556   webrtc::PeerConnectionInterface::IceServer ice_server;
2557   ice_server.urls.push_back("turn:88.88.88.0:3478?transport=tcp");
2558   ice_server.username = "test";
2559   ice_server.password = "test";
2560 
2561   PeerConnectionInterface::RTCConfiguration client_1_config;
2562   client_1_config.servers.push_back(ice_server);
2563   client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
2564 
2565   PeerConnectionInterface::RTCConfiguration client_2_config;
2566   client_2_config.servers.push_back(ice_server);
2567   client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
2568 
2569   ASSERT_TRUE(
2570       CreatePeerConnectionWrappersWithConfig(client_1_config, client_2_config));
2571 
2572   // Do normal offer/answer and wait for ICE to complete.
2573   ConnectFakeSignaling();
2574   caller()->AddAudioVideoTracks();
2575   callee()->AddAudioVideoTracks();
2576   caller()->CreateAndSetAndSignalOffer();
2577   ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2578   EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
2579                  callee()->ice_connection_state(), kMaxWaitForFramesMs);
2580 
2581   MediaExpectations media_expectations;
2582   media_expectations.ExpectBidirectionalAudioAndVideo();
2583   EXPECT_TRUE(ExpectNewFrames(media_expectations));
2584 }
2585 
2586 // Verify that a SSLCertificateVerifier passed in through
2587 // PeerConnectionDependencies is actually used by the underlying SSL
2588 // implementation to determine whether a certificate presented by the TURN
2589 // server is accepted by the client. Note that openssladapter_unittest.cc
2590 // contains more detailed, lower-level tests.
TEST_P(PeerConnectionIntegrationTest,SSLCertificateVerifierUsedForTurnConnections)2591 TEST_P(PeerConnectionIntegrationTest,
2592        SSLCertificateVerifierUsedForTurnConnections) {
2593   static const rtc::SocketAddress turn_server_internal_address{"88.88.88.0",
2594                                                                3478};
2595   static const rtc::SocketAddress turn_server_external_address{"88.88.88.1", 0};
2596 
2597   // Enable TCP-TLS for the fake turn server. We need to pass in 88.88.88.0 so
2598   // that host name verification passes on the fake certificate.
2599   CreateTurnServer(turn_server_internal_address, turn_server_external_address,
2600                    cricket::PROTO_TLS, "88.88.88.0");
2601 
2602   webrtc::PeerConnectionInterface::IceServer ice_server;
2603   ice_server.urls.push_back("turns:88.88.88.0:3478?transport=tcp");
2604   ice_server.username = "test";
2605   ice_server.password = "test";
2606 
2607   PeerConnectionInterface::RTCConfiguration client_1_config;
2608   client_1_config.servers.push_back(ice_server);
2609   client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
2610 
2611   PeerConnectionInterface::RTCConfiguration client_2_config;
2612   client_2_config.servers.push_back(ice_server);
2613   // Setting the type to kRelay forces the connection to go through a TURN
2614   // server.
2615   client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
2616 
2617   // Get a copy to the pointer so we can verify calls later.
2618   rtc::TestCertificateVerifier* client_1_cert_verifier =
2619       new rtc::TestCertificateVerifier();
2620   client_1_cert_verifier->verify_certificate_ = true;
2621   rtc::TestCertificateVerifier* client_2_cert_verifier =
2622       new rtc::TestCertificateVerifier();
2623   client_2_cert_verifier->verify_certificate_ = true;
2624 
2625   // Create the dependencies with the test certificate verifier.
2626   webrtc::PeerConnectionDependencies client_1_deps(nullptr);
2627   client_1_deps.tls_cert_verifier =
2628       std::unique_ptr<rtc::TestCertificateVerifier>(client_1_cert_verifier);
2629   webrtc::PeerConnectionDependencies client_2_deps(nullptr);
2630   client_2_deps.tls_cert_verifier =
2631       std::unique_ptr<rtc::TestCertificateVerifier>(client_2_cert_verifier);
2632 
2633   ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndDeps(
2634       client_1_config, std::move(client_1_deps), client_2_config,
2635       std::move(client_2_deps)));
2636   ConnectFakeSignaling();
2637 
2638   // Set "offer to receive audio/video" without adding any tracks, so we just
2639   // set up ICE/DTLS with no media.
2640   PeerConnectionInterface::RTCOfferAnswerOptions options;
2641   options.offer_to_receive_audio = 1;
2642   options.offer_to_receive_video = 1;
2643   caller()->SetOfferAnswerOptions(options);
2644   caller()->CreateAndSetAndSignalOffer();
2645   ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
2646 
2647   EXPECT_GT(client_1_cert_verifier->call_count_, 0u);
2648   EXPECT_GT(client_2_cert_verifier->call_count_, 0u);
2649 }
2650 
2651 // Test that the injected ICE transport factory is used to create ICE transports
2652 // for WebRTC connections.
TEST_P(PeerConnectionIntegrationTest,IceTransportFactoryUsedForConnections)2653 TEST_P(PeerConnectionIntegrationTest, IceTransportFactoryUsedForConnections) {
2654   PeerConnectionInterface::RTCConfiguration default_config;
2655   PeerConnectionDependencies dependencies(nullptr);
2656   auto ice_transport_factory = std::make_unique<MockIceTransportFactory>();
2657   EXPECT_CALL(*ice_transport_factory, RecordIceTransportCreated()).Times(1);
2658   dependencies.ice_transport_factory = std::move(ice_transport_factory);
2659   auto wrapper = CreatePeerConnectionWrapper("Caller", nullptr, &default_config,
2660                                              std::move(dependencies), nullptr,
2661                                              /*reset_encoder_factory=*/false,
2662                                              /*reset_decoder_factory=*/false);
2663   ASSERT_TRUE(wrapper);
2664   wrapper->CreateDataChannel();
2665   auto observer = rtc::make_ref_counted<MockSetSessionDescriptionObserver>();
2666   wrapper->pc()->SetLocalDescription(observer.get(),
2667                                      wrapper->CreateOfferAndWait().release());
2668 }
2669 
2670 // Test that audio and video flow end-to-end when codec names don't use the
2671 // expected casing, given that they're supposed to be case insensitive. To test
2672 // this, all but one codec is removed from each media description, and its
2673 // casing is changed.
2674 //
2675 // In the past, this has regressed and caused crashes/black video, due to the
2676 // fact that code at some layers was doing case-insensitive comparisons and
2677 // code at other layers was not.
TEST_P(PeerConnectionIntegrationTest,CodecNamesAreCaseInsensitive)2678 TEST_P(PeerConnectionIntegrationTest, CodecNamesAreCaseInsensitive) {
2679   ASSERT_TRUE(CreatePeerConnectionWrappers());
2680   ConnectFakeSignaling();
2681   caller()->AddAudioVideoTracks();
2682   callee()->AddAudioVideoTracks();
2683 
2684   // Remove all but one audio/video codec (opus and VP8), and change the
2685   // casing of the caller's generated offer.
2686   caller()->SetGeneratedSdpMunger([](cricket::SessionDescription* description) {
2687     cricket::AudioContentDescription* audio =
2688         GetFirstAudioContentDescription(description);
2689     ASSERT_NE(nullptr, audio);
2690     auto audio_codecs = audio->codecs();
2691     audio_codecs.erase(std::remove_if(audio_codecs.begin(), audio_codecs.end(),
2692                                       [](const cricket::AudioCodec& codec) {
2693                                         return codec.name != "opus";
2694                                       }),
2695                        audio_codecs.end());
2696     ASSERT_EQ(1u, audio_codecs.size());
2697     audio_codecs[0].name = "OpUs";
2698     audio->set_codecs(audio_codecs);
2699 
2700     cricket::VideoContentDescription* video =
2701         GetFirstVideoContentDescription(description);
2702     ASSERT_NE(nullptr, video);
2703     auto video_codecs = video->codecs();
2704     video_codecs.erase(std::remove_if(video_codecs.begin(), video_codecs.end(),
2705                                       [](const cricket::VideoCodec& codec) {
2706                                         return codec.name != "VP8";
2707                                       }),
2708                        video_codecs.end());
2709     ASSERT_EQ(1u, video_codecs.size());
2710     video_codecs[0].name = "vP8";
2711     video->set_codecs(video_codecs);
2712   });
2713 
2714   caller()->CreateAndSetAndSignalOffer();
2715   ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2716 
2717   // Verify frames are still received end-to-end.
2718   MediaExpectations media_expectations;
2719   media_expectations.ExpectBidirectionalAudioAndVideo();
2720   ASSERT_TRUE(ExpectNewFrames(media_expectations));
2721 }
2722 
TEST_P(PeerConnectionIntegrationTest,GetSourcesAudio)2723 TEST_P(PeerConnectionIntegrationTest, GetSourcesAudio) {
2724   ASSERT_TRUE(CreatePeerConnectionWrappers());
2725   ConnectFakeSignaling();
2726   caller()->AddAudioTrack();
2727   caller()->CreateAndSetAndSignalOffer();
2728   ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2729   // Wait for one audio frame to be received by the callee.
2730   MediaExpectations media_expectations;
2731   media_expectations.CalleeExpectsSomeAudio(1);
2732   ASSERT_TRUE(ExpectNewFrames(media_expectations));
2733   ASSERT_EQ(callee()->pc()->GetReceivers().size(), 1u);
2734   auto receiver = callee()->pc()->GetReceivers()[0];
2735   ASSERT_EQ(receiver->media_type(), cricket::MEDIA_TYPE_AUDIO);
2736   auto sources = receiver->GetSources();
2737   ASSERT_GT(receiver->GetParameters().encodings.size(), 0u);
2738   EXPECT_EQ(receiver->GetParameters().encodings[0].ssrc,
2739             sources[0].source_id());
2740   EXPECT_EQ(webrtc::RtpSourceType::SSRC, sources[0].source_type());
2741 }
2742 
TEST_P(PeerConnectionIntegrationTest,GetSourcesVideo)2743 TEST_P(PeerConnectionIntegrationTest, GetSourcesVideo) {
2744   ASSERT_TRUE(CreatePeerConnectionWrappers());
2745   ConnectFakeSignaling();
2746   caller()->AddVideoTrack();
2747   caller()->CreateAndSetAndSignalOffer();
2748   ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2749   // Wait for one video frame to be received by the callee.
2750   MediaExpectations media_expectations;
2751   media_expectations.CalleeExpectsSomeVideo(1);
2752   ASSERT_TRUE(ExpectNewFrames(media_expectations));
2753   ASSERT_EQ(callee()->pc()->GetReceivers().size(), 1u);
2754   auto receiver = callee()->pc()->GetReceivers()[0];
2755   ASSERT_EQ(receiver->media_type(), cricket::MEDIA_TYPE_VIDEO);
2756   auto sources = receiver->GetSources();
2757   ASSERT_GT(receiver->GetParameters().encodings.size(), 0u);
2758   ASSERT_GT(sources.size(), 0u);
2759   EXPECT_EQ(receiver->GetParameters().encodings[0].ssrc,
2760             sources[0].source_id());
2761   EXPECT_EQ(webrtc::RtpSourceType::SSRC, sources[0].source_type());
2762 }
2763 
2764 // Test that if a track is removed and added again with a different stream ID,
2765 // the new stream ID is successfully communicated in SDP and media continues to
2766 // flow end-to-end.
2767 // TODO(webrtc.bugs.org/8734): This test does not work for Unified Plan because
2768 // it will not reuse a transceiver that has already been sending. After creating
2769 // a new transceiver it tries to create an offer with two senders of the same
2770 // track ids and it fails.
TEST_F(PeerConnectionIntegrationTestPlanB,RemoveAndAddTrackWithNewStreamId)2771 TEST_F(PeerConnectionIntegrationTestPlanB, RemoveAndAddTrackWithNewStreamId) {
2772   ASSERT_TRUE(CreatePeerConnectionWrappers());
2773   ConnectFakeSignaling();
2774 
2775   // Add track using stream 1, do offer/answer.
2776   rtc::scoped_refptr<webrtc::AudioTrackInterface> track =
2777       caller()->CreateLocalAudioTrack();
2778   rtc::scoped_refptr<webrtc::RtpSenderInterface> sender =
2779       caller()->AddTrack(track, {"stream_1"});
2780   caller()->CreateAndSetAndSignalOffer();
2781   ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2782   {
2783     MediaExpectations media_expectations;
2784     media_expectations.CalleeExpectsSomeAudio(1);
2785     ASSERT_TRUE(ExpectNewFrames(media_expectations));
2786   }
2787   // Remove the sender, and create a new one with the new stream.
2788   caller()->pc()->RemoveTrackOrError(sender);
2789   sender = caller()->AddTrack(track, {"stream_2"});
2790   caller()->CreateAndSetAndSignalOffer();
2791   ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2792   // Wait for additional audio frames to be received by the callee.
2793   {
2794     MediaExpectations media_expectations;
2795     media_expectations.CalleeExpectsSomeAudio();
2796     ASSERT_TRUE(ExpectNewFrames(media_expectations));
2797   }
2798 }
2799 
TEST_P(PeerConnectionIntegrationTest,RtcEventLogOutputWriteCalled)2800 TEST_P(PeerConnectionIntegrationTest, RtcEventLogOutputWriteCalled) {
2801   ASSERT_TRUE(CreatePeerConnectionWrappers());
2802   ConnectFakeSignaling();
2803 
2804   auto output = std::make_unique<testing::NiceMock<MockRtcEventLogOutput>>();
2805   ON_CALL(*output, IsActive()).WillByDefault(::testing::Return(true));
2806   ON_CALL(*output, Write(::testing::A<absl::string_view>()))
2807       .WillByDefault(::testing::Return(true));
2808   EXPECT_CALL(*output, Write(::testing::A<absl::string_view>()))
2809       .Times(::testing::AtLeast(1));
2810   EXPECT_TRUE(caller()->pc()->StartRtcEventLog(
2811       std::move(output), webrtc::RtcEventLog::kImmediateOutput));
2812 
2813   caller()->AddAudioVideoTracks();
2814   caller()->CreateAndSetAndSignalOffer();
2815   ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2816 }
2817 
2818 // Test that if candidates are only signaled by applying full session
2819 // descriptions (instead of using AddIceCandidate), the peers can connect to
2820 // each other and exchange media.
TEST_P(PeerConnectionIntegrationTest,MediaFlowsWhenCandidatesSetOnlyInSdp)2821 TEST_P(PeerConnectionIntegrationTest, MediaFlowsWhenCandidatesSetOnlyInSdp) {
2822   ASSERT_TRUE(CreatePeerConnectionWrappers());
2823   // Each side will signal the session descriptions but not candidates.
2824   ConnectFakeSignalingForSdpOnly();
2825 
2826   // Add audio video track and exchange the initial offer/answer with media
2827   // information only. This will start ICE gathering on each side.
2828   caller()->AddAudioVideoTracks();
2829   callee()->AddAudioVideoTracks();
2830   caller()->CreateAndSetAndSignalOffer();
2831 
2832   // Wait for all candidates to be gathered on both the caller and callee.
2833   ASSERT_EQ_WAIT(PeerConnectionInterface::kIceGatheringComplete,
2834                  caller()->ice_gathering_state(), kDefaultTimeout);
2835   ASSERT_EQ_WAIT(PeerConnectionInterface::kIceGatheringComplete,
2836                  callee()->ice_gathering_state(), kDefaultTimeout);
2837 
2838   // The candidates will now be included in the session description, so
2839   // signaling them will start the ICE connection.
2840   caller()->CreateAndSetAndSignalOffer();
2841   ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2842 
2843   // Ensure that media flows in both directions.
2844   MediaExpectations media_expectations;
2845   media_expectations.ExpectBidirectionalAudioAndVideo();
2846   ASSERT_TRUE(ExpectNewFrames(media_expectations));
2847 }
2848 
2849 #if !defined(THREAD_SANITIZER)
2850 // These tests provokes TSAN errors. See bugs.webrtc.org/11305.
2851 
2852 // Test that SetAudioPlayout can be used to disable audio playout from the
2853 // start, then later enable it. This may be useful, for example, if the caller
2854 // needs to play a local ringtone until some event occurs, after which it
2855 // switches to playing the received audio.
TEST_P(PeerConnectionIntegrationTest,DisableAndEnableAudioPlayout)2856 TEST_P(PeerConnectionIntegrationTest, DisableAndEnableAudioPlayout) {
2857   ASSERT_TRUE(CreatePeerConnectionWrappers());
2858   ConnectFakeSignaling();
2859 
2860   // Set up audio-only call where audio playout is disabled on caller's side.
2861   caller()->pc()->SetAudioPlayout(false);
2862   caller()->AddAudioTrack();
2863   callee()->AddAudioTrack();
2864   caller()->CreateAndSetAndSignalOffer();
2865   ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2866 
2867   // Pump messages for a second.
2868   WAIT(false, 1000);
2869   // Since audio playout is disabled, the caller shouldn't have received
2870   // anything (at the playout level, at least).
2871   EXPECT_EQ(0, caller()->audio_frames_received());
2872   // As a sanity check, make sure the callee (for which playout isn't disabled)
2873   // did still see frames on its audio level.
2874   ASSERT_GT(callee()->audio_frames_received(), 0);
2875 
2876   // Enable playout again, and ensure audio starts flowing.
2877   caller()->pc()->SetAudioPlayout(true);
2878   MediaExpectations media_expectations;
2879   media_expectations.ExpectBidirectionalAudio();
2880   ASSERT_TRUE(ExpectNewFrames(media_expectations));
2881 }
2882 
GetAudioEnergyStat(PeerConnectionIntegrationWrapper * pc)2883 double GetAudioEnergyStat(PeerConnectionIntegrationWrapper* pc) {
2884   auto report = pc->NewGetStats();
2885   auto track_stats_list =
2886       report->GetStatsOfType<webrtc::DEPRECATED_RTCMediaStreamTrackStats>();
2887   const webrtc::DEPRECATED_RTCMediaStreamTrackStats* remote_track_stats =
2888       nullptr;
2889   for (const auto* track_stats : track_stats_list) {
2890     if (track_stats->remote_source.is_defined() &&
2891         *track_stats->remote_source) {
2892       remote_track_stats = track_stats;
2893       break;
2894     }
2895   }
2896 
2897   if (!remote_track_stats->total_audio_energy.is_defined()) {
2898     return 0.0;
2899   }
2900   return *remote_track_stats->total_audio_energy;
2901 }
2902 
2903 // Test that if audio playout is disabled via the SetAudioPlayout() method, then
2904 // incoming audio is still processed and statistics are generated.
TEST_P(PeerConnectionIntegrationTest,DisableAudioPlayoutStillGeneratesAudioStats)2905 TEST_P(PeerConnectionIntegrationTest,
2906        DisableAudioPlayoutStillGeneratesAudioStats) {
2907   ASSERT_TRUE(CreatePeerConnectionWrappers());
2908   ConnectFakeSignaling();
2909 
2910   // Set up audio-only call where playout is disabled but audio-processing is
2911   // still active.
2912   caller()->AddAudioTrack();
2913   callee()->AddAudioTrack();
2914   caller()->pc()->SetAudioPlayout(false);
2915 
2916   caller()->CreateAndSetAndSignalOffer();
2917   ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2918 
2919   // Wait for the callee to receive audio stats.
2920   EXPECT_TRUE_WAIT(GetAudioEnergyStat(caller()) > 0, kMaxWaitForFramesMs);
2921 }
2922 
2923 #endif  // !defined(THREAD_SANITIZER)
2924 
2925 // Test that SetAudioRecording can be used to disable audio recording from the
2926 // start, then later enable it. This may be useful, for example, if the caller
2927 // wants to ensure that no audio resources are active before a certain state
2928 // is reached.
TEST_P(PeerConnectionIntegrationTest,DisableAndEnableAudioRecording)2929 TEST_P(PeerConnectionIntegrationTest, DisableAndEnableAudioRecording) {
2930   ASSERT_TRUE(CreatePeerConnectionWrappers());
2931   ConnectFakeSignaling();
2932 
2933   // Set up audio-only call where audio recording is disabled on caller's side.
2934   caller()->pc()->SetAudioRecording(false);
2935   caller()->AddAudioTrack();
2936   callee()->AddAudioTrack();
2937   caller()->CreateAndSetAndSignalOffer();
2938   ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2939 
2940   // Pump messages for a second.
2941   WAIT(false, 1000);
2942   // Since caller has disabled audio recording, the callee shouldn't have
2943   // received anything.
2944   EXPECT_EQ(0, callee()->audio_frames_received());
2945   // As a sanity check, make sure the caller did still see frames on its
2946   // audio level since audio recording is enabled on the calle side.
2947   ASSERT_GT(caller()->audio_frames_received(), 0);
2948 
2949   // Enable audio recording again, and ensure audio starts flowing.
2950   caller()->pc()->SetAudioRecording(true);
2951   MediaExpectations media_expectations;
2952   media_expectations.ExpectBidirectionalAudio();
2953   ASSERT_TRUE(ExpectNewFrames(media_expectations));
2954 }
2955 
TEST_P(PeerConnectionIntegrationTest,IceEventsGeneratedAndLoggedInRtcEventLog)2956 TEST_P(PeerConnectionIntegrationTest,
2957        IceEventsGeneratedAndLoggedInRtcEventLog) {
2958   ASSERT_TRUE(CreatePeerConnectionWrappersWithFakeRtcEventLog());
2959   ConnectFakeSignaling();
2960   PeerConnectionInterface::RTCOfferAnswerOptions options;
2961   options.offer_to_receive_audio = 1;
2962   caller()->SetOfferAnswerOptions(options);
2963   caller()->CreateAndSetAndSignalOffer();
2964   ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
2965   ASSERT_NE(nullptr, caller()->event_log_factory());
2966   ASSERT_NE(nullptr, callee()->event_log_factory());
2967   webrtc::FakeRtcEventLog* caller_event_log =
2968       caller()->event_log_factory()->last_log_created();
2969   webrtc::FakeRtcEventLog* callee_event_log =
2970       callee()->event_log_factory()->last_log_created();
2971   ASSERT_NE(nullptr, caller_event_log);
2972   ASSERT_NE(nullptr, callee_event_log);
2973   int caller_ice_config_count = caller_event_log->GetEventCount(
2974       webrtc::RtcEvent::Type::IceCandidatePairConfig);
2975   int caller_ice_event_count = caller_event_log->GetEventCount(
2976       webrtc::RtcEvent::Type::IceCandidatePairEvent);
2977   int callee_ice_config_count = callee_event_log->GetEventCount(
2978       webrtc::RtcEvent::Type::IceCandidatePairConfig);
2979   int callee_ice_event_count = callee_event_log->GetEventCount(
2980       webrtc::RtcEvent::Type::IceCandidatePairEvent);
2981   EXPECT_LT(0, caller_ice_config_count);
2982   EXPECT_LT(0, caller_ice_event_count);
2983   EXPECT_LT(0, callee_ice_config_count);
2984   EXPECT_LT(0, callee_ice_event_count);
2985 }
2986 
TEST_P(PeerConnectionIntegrationTest,RegatherAfterChangingIceTransportType)2987 TEST_P(PeerConnectionIntegrationTest, RegatherAfterChangingIceTransportType) {
2988   static const rtc::SocketAddress turn_server_internal_address{"88.88.88.0",
2989                                                                3478};
2990   static const rtc::SocketAddress turn_server_external_address{"88.88.88.1", 0};
2991 
2992   CreateTurnServer(turn_server_internal_address, turn_server_external_address);
2993 
2994   webrtc::PeerConnectionInterface::IceServer ice_server;
2995   ice_server.urls.push_back("turn:88.88.88.0:3478");
2996   ice_server.username = "test";
2997   ice_server.password = "test";
2998 
2999   PeerConnectionInterface::RTCConfiguration caller_config;
3000   caller_config.servers.push_back(ice_server);
3001   caller_config.type = webrtc::PeerConnectionInterface::kRelay;
3002   caller_config.continual_gathering_policy = PeerConnection::GATHER_CONTINUALLY;
3003   caller_config.surface_ice_candidates_on_ice_transport_type_changed = true;
3004 
3005   PeerConnectionInterface::RTCConfiguration callee_config;
3006   callee_config.servers.push_back(ice_server);
3007   callee_config.type = webrtc::PeerConnectionInterface::kRelay;
3008   callee_config.continual_gathering_policy = PeerConnection::GATHER_CONTINUALLY;
3009   callee_config.surface_ice_candidates_on_ice_transport_type_changed = true;
3010 
3011   ASSERT_TRUE(
3012       CreatePeerConnectionWrappersWithConfig(caller_config, callee_config));
3013 
3014   // Do normal offer/answer and wait for ICE to complete.
3015   ConnectFakeSignaling();
3016   caller()->AddAudioVideoTracks();
3017   callee()->AddAudioVideoTracks();
3018   caller()->CreateAndSetAndSignalOffer();
3019   ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3020   // Since we are doing continual gathering, the ICE transport does not reach
3021   // kIceGatheringComplete (see
3022   // P2PTransportChannel::OnCandidatesAllocationDone), and consequently not
3023   // kIceConnectionComplete.
3024   EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
3025                  caller()->ice_connection_state(), kDefaultTimeout);
3026   EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
3027                  callee()->ice_connection_state(), kDefaultTimeout);
3028   // Note that we cannot use the metric
3029   // `WebRTC.PeerConnection.CandidatePairType_UDP` in this test since this
3030   // metric is only populated when we reach kIceConnectionComplete in the
3031   // current implementation.
3032   EXPECT_EQ(cricket::RELAY_PORT_TYPE,
3033             caller()->last_candidate_gathered().type());
3034   EXPECT_EQ(cricket::RELAY_PORT_TYPE,
3035             callee()->last_candidate_gathered().type());
3036 
3037   // Loosen the caller's candidate filter.
3038   caller_config = caller()->pc()->GetConfiguration();
3039   caller_config.type = webrtc::PeerConnectionInterface::kAll;
3040   caller()->pc()->SetConfiguration(caller_config);
3041   // We should have gathered a new host candidate.
3042   EXPECT_EQ_WAIT(cricket::LOCAL_PORT_TYPE,
3043                  caller()->last_candidate_gathered().type(), kDefaultTimeout);
3044 
3045   // Loosen the callee's candidate filter.
3046   callee_config = callee()->pc()->GetConfiguration();
3047   callee_config.type = webrtc::PeerConnectionInterface::kAll;
3048   callee()->pc()->SetConfiguration(callee_config);
3049   EXPECT_EQ_WAIT(cricket::LOCAL_PORT_TYPE,
3050                  callee()->last_candidate_gathered().type(), kDefaultTimeout);
3051 
3052   // Create an offer and verify that it does not contain an ICE restart (i.e new
3053   // ice credentials).
3054   std::string caller_ufrag_pre_offer = caller()
3055                                            ->pc()
3056                                            ->local_description()
3057                                            ->description()
3058                                            ->transport_infos()[0]
3059                                            .description.ice_ufrag;
3060   caller()->CreateAndSetAndSignalOffer();
3061   std::string caller_ufrag_post_offer = caller()
3062                                             ->pc()
3063                                             ->local_description()
3064                                             ->description()
3065                                             ->transport_infos()[0]
3066                                             .description.ice_ufrag;
3067   EXPECT_EQ(caller_ufrag_pre_offer, caller_ufrag_post_offer);
3068 }
3069 
TEST_P(PeerConnectionIntegrationTest,OnIceCandidateError)3070 TEST_P(PeerConnectionIntegrationTest, OnIceCandidateError) {
3071   static const rtc::SocketAddress turn_server_internal_address{"88.88.88.0",
3072                                                                3478};
3073   static const rtc::SocketAddress turn_server_external_address{"88.88.88.1", 0};
3074 
3075   CreateTurnServer(turn_server_internal_address, turn_server_external_address);
3076 
3077   webrtc::PeerConnectionInterface::IceServer ice_server;
3078   ice_server.urls.push_back("turn:88.88.88.0:3478");
3079   ice_server.username = "test";
3080   ice_server.password = "123";
3081 
3082   PeerConnectionInterface::RTCConfiguration caller_config;
3083   caller_config.servers.push_back(ice_server);
3084   caller_config.type = webrtc::PeerConnectionInterface::kRelay;
3085   caller_config.continual_gathering_policy = PeerConnection::GATHER_CONTINUALLY;
3086 
3087   PeerConnectionInterface::RTCConfiguration callee_config;
3088   callee_config.servers.push_back(ice_server);
3089   callee_config.type = webrtc::PeerConnectionInterface::kRelay;
3090   callee_config.continual_gathering_policy = PeerConnection::GATHER_CONTINUALLY;
3091 
3092   ASSERT_TRUE(
3093       CreatePeerConnectionWrappersWithConfig(caller_config, callee_config));
3094 
3095   // Do normal offer/answer and wait for ICE to complete.
3096   ConnectFakeSignaling();
3097   caller()->AddAudioVideoTracks();
3098   callee()->AddAudioVideoTracks();
3099   caller()->CreateAndSetAndSignalOffer();
3100   ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3101   EXPECT_EQ_WAIT(401, caller()->error_event().error_code, kDefaultTimeout);
3102   EXPECT_EQ("Unauthorized", caller()->error_event().error_text);
3103   EXPECT_EQ("turn:88.88.88.0:3478?transport=udp", caller()->error_event().url);
3104   EXPECT_NE(caller()->error_event().address, "");
3105 }
3106 
TEST_P(PeerConnectionIntegrationTest,OnIceCandidateErrorWithEmptyAddress)3107 TEST_P(PeerConnectionIntegrationTest, OnIceCandidateErrorWithEmptyAddress) {
3108   webrtc::PeerConnectionInterface::IceServer ice_server;
3109   ice_server.urls.push_back("turn:127.0.0.1:3478?transport=tcp");
3110   ice_server.username = "test";
3111   ice_server.password = "test";
3112 
3113   PeerConnectionInterface::RTCConfiguration caller_config;
3114   caller_config.servers.push_back(ice_server);
3115   caller_config.type = webrtc::PeerConnectionInterface::kRelay;
3116   caller_config.continual_gathering_policy = PeerConnection::GATHER_CONTINUALLY;
3117 
3118   PeerConnectionInterface::RTCConfiguration callee_config;
3119   callee_config.servers.push_back(ice_server);
3120   callee_config.type = webrtc::PeerConnectionInterface::kRelay;
3121   callee_config.continual_gathering_policy = PeerConnection::GATHER_CONTINUALLY;
3122 
3123   ASSERT_TRUE(
3124       CreatePeerConnectionWrappersWithConfig(caller_config, callee_config));
3125 
3126   // Do normal offer/answer and wait for ICE to complete.
3127   ConnectFakeSignaling();
3128   caller()->AddAudioVideoTracks();
3129   callee()->AddAudioVideoTracks();
3130   caller()->CreateAndSetAndSignalOffer();
3131   ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3132   EXPECT_EQ_WAIT(701, caller()->error_event().error_code, kDefaultTimeout);
3133   EXPECT_EQ(caller()->error_event().address, "");
3134 }
3135 
TEST_F(PeerConnectionIntegrationTestUnifiedPlan,AudioKeepsFlowingAfterImplicitRollback)3136 TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
3137        AudioKeepsFlowingAfterImplicitRollback) {
3138   PeerConnectionInterface::RTCConfiguration config;
3139   config.sdp_semantics = SdpSemantics::kUnifiedPlan;
3140   config.enable_implicit_rollback = true;
3141   ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
3142   ConnectFakeSignaling();
3143   caller()->AddAudioTrack();
3144   callee()->AddAudioTrack();
3145   caller()->CreateAndSetAndSignalOffer();
3146   ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3147   MediaExpectations media_expectations;
3148   media_expectations.ExpectBidirectionalAudio();
3149   ASSERT_TRUE(ExpectNewFrames(media_expectations));
3150   SetSignalIceCandidates(false);  // Workaround candidate outrace sdp.
3151   caller()->AddVideoTrack();
3152   callee()->AddVideoTrack();
3153   auto observer = rtc::make_ref_counted<MockSetSessionDescriptionObserver>();
3154   callee()->pc()->SetLocalDescription(observer.get(),
3155                                       callee()->CreateOfferAndWait().release());
3156   EXPECT_TRUE_WAIT(observer->called(), kDefaultTimeout);
3157   caller()->CreateAndSetAndSignalOffer();  // Implicit rollback.
3158   ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3159   ASSERT_TRUE(ExpectNewFrames(media_expectations));
3160 }
3161 
TEST_F(PeerConnectionIntegrationTestUnifiedPlan,ImplicitRollbackVisitsStableState)3162 TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
3163        ImplicitRollbackVisitsStableState) {
3164   RTCConfiguration config;
3165   config.sdp_semantics = SdpSemantics::kUnifiedPlan;
3166   config.enable_implicit_rollback = true;
3167 
3168   ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
3169 
3170   auto sld_observer =
3171       rtc::make_ref_counted<MockSetSessionDescriptionObserver>();
3172   callee()->pc()->SetLocalDescription(sld_observer.get(),
3173                                       callee()->CreateOfferAndWait().release());
3174   EXPECT_TRUE_WAIT(sld_observer->called(), kDefaultTimeout);
3175   EXPECT_EQ(sld_observer->error(), "");
3176 
3177   auto srd_observer =
3178       rtc::make_ref_counted<MockSetSessionDescriptionObserver>();
3179   callee()->pc()->SetRemoteDescription(
3180       srd_observer.get(), caller()->CreateOfferAndWait().release());
3181   EXPECT_TRUE_WAIT(srd_observer->called(), kDefaultTimeout);
3182   EXPECT_EQ(srd_observer->error(), "");
3183 
3184   EXPECT_THAT(callee()->peer_connection_signaling_state_history(),
3185               ElementsAre(PeerConnectionInterface::kHaveLocalOffer,
3186                           PeerConnectionInterface::kStable,
3187                           PeerConnectionInterface::kHaveRemoteOffer));
3188 }
3189 
TEST_F(PeerConnectionIntegrationTestUnifiedPlan,H264FmtpSpsPpsIdrInKeyframeParameterUsage)3190 TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
3191        H264FmtpSpsPpsIdrInKeyframeParameterUsage) {
3192   ASSERT_TRUE(CreatePeerConnectionWrappers());
3193   ConnectFakeSignaling();
3194   caller()->AddVideoTrack();
3195   callee()->AddVideoTrack();
3196   auto munger = [](cricket::SessionDescription* desc) {
3197     cricket::VideoContentDescription* video =
3198         GetFirstVideoContentDescription(desc);
3199     auto codecs = video->codecs();
3200     for (auto&& codec : codecs) {
3201       if (codec.name == "H264") {
3202         std::string value;
3203         // The parameter is not supposed to be present in SDP by default.
3204         EXPECT_FALSE(
3205             codec.GetParam(cricket::kH264FmtpSpsPpsIdrInKeyframe, &value));
3206         codec.SetParam(std::string(cricket::kH264FmtpSpsPpsIdrInKeyframe),
3207                        std::string(""));
3208       }
3209     }
3210     video->set_codecs(codecs);
3211   };
3212   // Munge local offer for SLD.
3213   caller()->SetGeneratedSdpMunger(munger);
3214   // Munge remote answer for SRD.
3215   caller()->SetReceivedSdpMunger(munger);
3216   caller()->CreateAndSetAndSignalOffer();
3217   ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3218   // Observe that after munging the parameter is present in generated SDP.
3219   caller()->SetGeneratedSdpMunger([](cricket::SessionDescription* desc) {
3220     cricket::VideoContentDescription* video =
3221         GetFirstVideoContentDescription(desc);
3222     for (auto&& codec : video->codecs()) {
3223       if (codec.name == "H264") {
3224         std::string value;
3225         EXPECT_TRUE(
3226             codec.GetParam(cricket::kH264FmtpSpsPpsIdrInKeyframe, &value));
3227       }
3228     }
3229   });
3230   caller()->CreateOfferAndWait();
3231 }
3232 
TEST_F(PeerConnectionIntegrationTestUnifiedPlan,RenegotiateManyAudioTransceivers)3233 TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
3234        RenegotiateManyAudioTransceivers) {
3235   PeerConnectionInterface::RTCConfiguration config;
3236   config.sdp_semantics = SdpSemantics::kUnifiedPlan;
3237   ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
3238   ConnectFakeSignaling();
3239   caller()->pc()->AddTransceiver(cricket::MEDIA_TYPE_AUDIO);
3240 
3241   caller()->CreateAndSetAndSignalOffer();
3242   ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3243   int current_size = caller()->pc()->GetTransceivers().size();
3244   // Add more tracks until we get close to having issues.
3245   // Issues have been seen at:
3246   // - 32 tracks on android_arm64_rel and android_arm_dbg bots
3247   // - 16 tracks on android_arm_dbg (flaky)
3248   while (current_size < 8) {
3249     // Double the number of tracks
3250     for (int i = 0; i < current_size; i++) {
3251       caller()->pc()->AddTransceiver(cricket::MEDIA_TYPE_AUDIO);
3252     }
3253     current_size = caller()->pc()->GetTransceivers().size();
3254     RTC_LOG(LS_INFO) << "Renegotiating with " << current_size << " tracks";
3255     auto start_time_ms = rtc::TimeMillis();
3256     caller()->CreateAndSetAndSignalOffer();
3257     // We want to stop when the time exceeds one second.
3258     ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3259     auto elapsed_time_ms = rtc::TimeMillis() - start_time_ms;
3260     RTC_LOG(LS_INFO) << "Renegotiating took " << elapsed_time_ms << " ms";
3261     ASSERT_GT(1000, elapsed_time_ms)
3262         << "Audio transceivers: Negotiation took too long after "
3263         << current_size << " tracks added";
3264   }
3265 }
3266 
TEST_F(PeerConnectionIntegrationTestUnifiedPlan,RenegotiateManyVideoTransceivers)3267 TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
3268        RenegotiateManyVideoTransceivers) {
3269   PeerConnectionInterface::RTCConfiguration config;
3270   config.sdp_semantics = SdpSemantics::kUnifiedPlan;
3271   ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
3272   ConnectFakeSignaling();
3273   caller()->pc()->AddTransceiver(cricket::MEDIA_TYPE_VIDEO);
3274 
3275   caller()->CreateAndSetAndSignalOffer();
3276   ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3277   int current_size = caller()->pc()->GetTransceivers().size();
3278   // Add more tracks until we get close to having issues.
3279   // Issues have been seen at:
3280   // - 96 on a Linux workstation
3281   // - 64 at win_x86_more_configs and win_x64_msvc_dbg
3282   // - 32 on android_arm64_rel and linux_dbg bots
3283   // - 16 on Android 64 (Nexus 5x)
3284   while (current_size < 8) {
3285     // Double the number of tracks
3286     for (int i = 0; i < current_size; i++) {
3287       caller()->pc()->AddTransceiver(cricket::MEDIA_TYPE_VIDEO);
3288     }
3289     current_size = caller()->pc()->GetTransceivers().size();
3290     RTC_LOG(LS_INFO) << "Renegotiating with " << current_size << " tracks";
3291     auto start_time_ms = rtc::TimeMillis();
3292     caller()->CreateAndSetAndSignalOffer();
3293     // We want to stop when the time exceeds one second.
3294     ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3295     auto elapsed_time_ms = rtc::TimeMillis() - start_time_ms;
3296     RTC_LOG(LS_INFO) << "Renegotiating took " << elapsed_time_ms << " ms";
3297     ASSERT_GT(1000, elapsed_time_ms)
3298         << "Video transceivers: Negotiation took too long after "
3299         << current_size << " tracks added";
3300   }
3301 }
3302 
TEST_F(PeerConnectionIntegrationTestUnifiedPlan,RenegotiateManyVideoTransceiversAndWatchAudioDelay)3303 TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
3304        RenegotiateManyVideoTransceiversAndWatchAudioDelay) {
3305   PeerConnectionInterface::RTCConfiguration config;
3306   config.sdp_semantics = SdpSemantics::kUnifiedPlan;
3307   ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
3308   ConnectFakeSignaling();
3309   caller()->AddAudioTrack();
3310   callee()->AddAudioTrack();
3311   caller()->CreateAndSetAndSignalOffer();
3312   ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3313   // Wait until we can see the audio flowing.
3314   MediaExpectations media_expectations;
3315   media_expectations.CalleeExpectsSomeAudio();
3316   ASSERT_TRUE(ExpectNewFrames(media_expectations));
3317 
3318   // Get the baseline numbers for audio_packets and audio_delay
3319   // in both directions.
3320   caller()->StartWatchingDelayStats();
3321   callee()->StartWatchingDelayStats();
3322 
3323   int current_size = caller()->pc()->GetTransceivers().size();
3324   // Add more tracks until we get close to having issues.
3325   // Making this number very large makes the test very slow.
3326   while (current_size < 16) {
3327     // Double the number of tracks
3328     for (int i = 0; i < current_size; i++) {
3329       caller()->pc()->AddTransceiver(cricket::MEDIA_TYPE_VIDEO);
3330     }
3331     current_size = caller()->pc()->GetTransceivers().size();
3332     RTC_LOG(LS_INFO) << "Renegotiating with " << current_size << " tracks";
3333     auto start_time_ms = rtc::TimeMillis();
3334     caller()->CreateAndSetAndSignalOffer();
3335     // We want to stop when the time exceeds one second.
3336     ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3337     auto elapsed_time_ms = rtc::TimeMillis() - start_time_ms;
3338     RTC_LOG(LS_INFO) << "Renegotiating took " << elapsed_time_ms << " ms";
3339     // This is a guard against the test using excessive amounts of time.
3340     ASSERT_GT(5000, elapsed_time_ms)
3341         << "Video transceivers: Negotiation took too long after "
3342         << current_size << " tracks added";
3343     caller()->UpdateDelayStats("caller reception", current_size);
3344     callee()->UpdateDelayStats("callee reception", current_size);
3345   }
3346 }
3347 
3348 INSTANTIATE_TEST_SUITE_P(
3349     PeerConnectionIntegrationTest,
3350     PeerConnectionIntegrationTest,
3351     Combine(Values(SdpSemantics::kPlanB_DEPRECATED, SdpSemantics::kUnifiedPlan),
3352             Values("WebRTC-FrameBuffer3/arm:FrameBuffer2/",
3353                    "WebRTC-FrameBuffer3/arm:FrameBuffer3/",
3354                    "WebRTC-FrameBuffer3/arm:SyncDecoding/")));
3355 
3356 INSTANTIATE_TEST_SUITE_P(
3357     PeerConnectionIntegrationTest,
3358     PeerConnectionIntegrationTestWithFakeClock,
3359     Combine(Values(SdpSemantics::kPlanB_DEPRECATED, SdpSemantics::kUnifiedPlan),
3360             Values("WebRTC-FrameBuffer3/arm:FrameBuffer2/",
3361                    "WebRTC-FrameBuffer3/arm:FrameBuffer3/",
3362                    "WebRTC-FrameBuffer3/arm:SyncDecoding/")));
3363 
3364 // Tests that verify interoperability between Plan B and Unified Plan
3365 // PeerConnections.
3366 class PeerConnectionIntegrationInteropTest
3367     : public PeerConnectionIntegrationBaseTest,
3368       public ::testing::WithParamInterface<
3369           std::tuple<SdpSemantics, SdpSemantics>> {
3370  protected:
3371   // Setting the SdpSemantics for the base test to kDefault does not matter
3372   // because we specify not to use the test semantics when creating
3373   // PeerConnectionIntegrationWrappers.
PeerConnectionIntegrationInteropTest()3374   PeerConnectionIntegrationInteropTest()
3375       : PeerConnectionIntegrationBaseTest(SdpSemantics::kPlanB_DEPRECATED),
3376         caller_semantics_(std::get<0>(GetParam())),
3377         callee_semantics_(std::get<1>(GetParam())) {}
3378 
CreatePeerConnectionWrappersWithSemantics()3379   bool CreatePeerConnectionWrappersWithSemantics() {
3380     return CreatePeerConnectionWrappersWithSdpSemantics(caller_semantics_,
3381                                                         callee_semantics_);
3382   }
3383 
3384   const SdpSemantics caller_semantics_;
3385   const SdpSemantics callee_semantics_;
3386 };
3387 
TEST_P(PeerConnectionIntegrationInteropTest,NoMediaLocalToNoMediaRemote)3388 TEST_P(PeerConnectionIntegrationInteropTest, NoMediaLocalToNoMediaRemote) {
3389   ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
3390   ConnectFakeSignaling();
3391 
3392   caller()->CreateAndSetAndSignalOffer();
3393   ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3394 }
3395 
TEST_P(PeerConnectionIntegrationInteropTest,OneAudioLocalToNoMediaRemote)3396 TEST_P(PeerConnectionIntegrationInteropTest, OneAudioLocalToNoMediaRemote) {
3397   ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
3398   ConnectFakeSignaling();
3399   auto audio_sender = caller()->AddAudioTrack();
3400 
3401   caller()->CreateAndSetAndSignalOffer();
3402   ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3403 
3404   // Verify that one audio receiver has been created on the remote and that it
3405   // has the same track ID as the sending track.
3406   auto receivers = callee()->pc()->GetReceivers();
3407   ASSERT_EQ(1u, receivers.size());
3408   EXPECT_EQ(cricket::MEDIA_TYPE_AUDIO, receivers[0]->media_type());
3409   EXPECT_EQ(receivers[0]->track()->id(), audio_sender->track()->id());
3410 
3411   MediaExpectations media_expectations;
3412   media_expectations.CalleeExpectsSomeAudio();
3413   ASSERT_TRUE(ExpectNewFrames(media_expectations));
3414 }
3415 
TEST_P(PeerConnectionIntegrationInteropTest,OneAudioOneVideoToNoMediaRemote)3416 TEST_P(PeerConnectionIntegrationInteropTest, OneAudioOneVideoToNoMediaRemote) {
3417   ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
3418   ConnectFakeSignaling();
3419   auto video_sender = caller()->AddVideoTrack();
3420   auto audio_sender = caller()->AddAudioTrack();
3421 
3422   caller()->CreateAndSetAndSignalOffer();
3423   ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3424 
3425   // Verify that one audio and one video receiver have been created on the
3426   // remote and that they have the same track IDs as the sending tracks.
3427   auto audio_receivers =
3428       callee()->GetReceiversOfType(cricket::MEDIA_TYPE_AUDIO);
3429   ASSERT_EQ(1u, audio_receivers.size());
3430   EXPECT_EQ(audio_receivers[0]->track()->id(), audio_sender->track()->id());
3431   auto video_receivers =
3432       callee()->GetReceiversOfType(cricket::MEDIA_TYPE_VIDEO);
3433   ASSERT_EQ(1u, video_receivers.size());
3434   EXPECT_EQ(video_receivers[0]->track()->id(), video_sender->track()->id());
3435 
3436   MediaExpectations media_expectations;
3437   media_expectations.CalleeExpectsSomeAudioAndVideo();
3438   ASSERT_TRUE(ExpectNewFrames(media_expectations));
3439 }
3440 
TEST_P(PeerConnectionIntegrationInteropTest,OneAudioOneVideoLocalToOneAudioOneVideoRemote)3441 TEST_P(PeerConnectionIntegrationInteropTest,
3442        OneAudioOneVideoLocalToOneAudioOneVideoRemote) {
3443   ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
3444   ConnectFakeSignaling();
3445   caller()->AddAudioVideoTracks();
3446   callee()->AddAudioVideoTracks();
3447 
3448   caller()->CreateAndSetAndSignalOffer();
3449   ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3450 
3451   MediaExpectations media_expectations;
3452   media_expectations.ExpectBidirectionalAudioAndVideo();
3453   ASSERT_TRUE(ExpectNewFrames(media_expectations));
3454 }
3455 
TEST_P(PeerConnectionIntegrationInteropTest,ReverseRolesOneAudioLocalToOneVideoRemote)3456 TEST_P(PeerConnectionIntegrationInteropTest,
3457        ReverseRolesOneAudioLocalToOneVideoRemote) {
3458   ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
3459   ConnectFakeSignaling();
3460   caller()->AddAudioTrack();
3461   callee()->AddVideoTrack();
3462 
3463   caller()->CreateAndSetAndSignalOffer();
3464   ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3465 
3466   // Verify that only the audio track has been negotiated.
3467   EXPECT_EQ(0u, caller()->GetReceiversOfType(cricket::MEDIA_TYPE_VIDEO).size());
3468   // Might also check that the callee's NegotiationNeeded flag is set.
3469 
3470   // Reverse roles.
3471   callee()->CreateAndSetAndSignalOffer();
3472   ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3473 
3474   MediaExpectations media_expectations;
3475   media_expectations.CallerExpectsSomeVideo();
3476   media_expectations.CalleeExpectsSomeAudio();
3477   ASSERT_TRUE(ExpectNewFrames(media_expectations));
3478 }
3479 
TEST_P(PeerConnectionIntegrationTest,NewTracksDoNotCauseNewCandidates)3480 TEST_P(PeerConnectionIntegrationTest, NewTracksDoNotCauseNewCandidates) {
3481   ASSERT_TRUE(CreatePeerConnectionWrappers());
3482   ConnectFakeSignaling();
3483   caller()->AddAudioVideoTracks();
3484   caller()->CreateAndSetAndSignalOffer();
3485   ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3486   ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
3487   caller()->ExpectCandidates(0);
3488   callee()->ExpectCandidates(0);
3489   caller()->AddAudioTrack();
3490   caller()->CreateAndSetAndSignalOffer();
3491   ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3492 }
3493 
TEST_P(PeerConnectionIntegrationTest,MediaCallWithoutMediaEngineFails)3494 TEST_P(PeerConnectionIntegrationTest, MediaCallWithoutMediaEngineFails) {
3495   ASSERT_TRUE(CreatePeerConnectionWrappersWithoutMediaEngine());
3496   // AddTrack should fail.
3497   EXPECT_FALSE(
3498       caller()->pc()->AddTrack(caller()->CreateLocalAudioTrack(), {}).ok());
3499 }
3500 
3501 INSTANTIATE_TEST_SUITE_P(
3502     PeerConnectionIntegrationTest,
3503     PeerConnectionIntegrationInteropTest,
3504     Values(std::make_tuple(SdpSemantics::kPlanB_DEPRECATED,
3505                            SdpSemantics::kUnifiedPlan),
3506            std::make_tuple(SdpSemantics::kUnifiedPlan,
3507                            SdpSemantics::kPlanB_DEPRECATED)));
3508 
3509 // Test that if the Unified Plan side offers two video tracks then the Plan B
3510 // side will only see the first one and ignore the second.
TEST_F(PeerConnectionIntegrationTestPlanB,TwoVideoUnifiedPlanToNoMediaPlanB)3511 TEST_F(PeerConnectionIntegrationTestPlanB, TwoVideoUnifiedPlanToNoMediaPlanB) {
3512   ASSERT_TRUE(CreatePeerConnectionWrappersWithSdpSemantics(
3513       SdpSemantics::kUnifiedPlan, SdpSemantics::kPlanB_DEPRECATED));
3514   ConnectFakeSignaling();
3515   auto first_sender = caller()->AddVideoTrack();
3516   caller()->AddVideoTrack();
3517 
3518   caller()->CreateAndSetAndSignalOffer();
3519   ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3520 
3521   // Verify that there is only one receiver and it corresponds to the first
3522   // added track.
3523   auto receivers = callee()->pc()->GetReceivers();
3524   ASSERT_EQ(1u, receivers.size());
3525   EXPECT_TRUE(receivers[0]->track()->enabled());
3526   EXPECT_EQ(first_sender->track()->id(), receivers[0]->track()->id());
3527 
3528   MediaExpectations media_expectations;
3529   media_expectations.CalleeExpectsSomeVideo();
3530   ASSERT_TRUE(ExpectNewFrames(media_expectations));
3531 }
3532 
3533 // Test that if the initial offer tagged BUNDLE section is rejected due to its
3534 // associated RtpTransceiver being stopped and another transceiver is added,
3535 // then renegotiation causes the callee to receive the new video track without
3536 // error.
3537 // This is a regression test for bugs.webrtc.org/9954
TEST_F(PeerConnectionIntegrationTestUnifiedPlan,ReOfferWithStoppedBundleTaggedTransceiver)3538 TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
3539        ReOfferWithStoppedBundleTaggedTransceiver) {
3540   RTCConfiguration config;
3541   config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
3542   ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
3543   ConnectFakeSignaling();
3544   auto audio_transceiver_or_error =
3545       caller()->pc()->AddTransceiver(caller()->CreateLocalAudioTrack());
3546   ASSERT_TRUE(audio_transceiver_or_error.ok());
3547   auto audio_transceiver = audio_transceiver_or_error.MoveValue();
3548 
3549   caller()->CreateAndSetAndSignalOffer();
3550   ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3551   {
3552     MediaExpectations media_expectations;
3553     media_expectations.CalleeExpectsSomeAudio();
3554     ASSERT_TRUE(ExpectNewFrames(media_expectations));
3555   }
3556 
3557   audio_transceiver->StopInternal();
3558   caller()->pc()->AddTransceiver(caller()->CreateLocalVideoTrack());
3559 
3560   caller()->CreateAndSetAndSignalOffer();
3561   ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3562   {
3563     MediaExpectations media_expectations;
3564     media_expectations.CalleeExpectsSomeVideo();
3565     ASSERT_TRUE(ExpectNewFrames(media_expectations));
3566   }
3567 }
3568 
TEST_F(PeerConnectionIntegrationTestUnifiedPlan,StopTransceiverRemovesDtlsTransports)3569 TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
3570        StopTransceiverRemovesDtlsTransports) {
3571   RTCConfiguration config;
3572   ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
3573   ConnectFakeSignaling();
3574   auto audio_transceiver_or_error =
3575       caller()->pc()->AddTransceiver(caller()->CreateLocalAudioTrack());
3576   ASSERT_TRUE(audio_transceiver_or_error.ok());
3577   auto audio_transceiver = audio_transceiver_or_error.MoveValue();
3578 
3579   caller()->CreateAndSetAndSignalOffer();
3580   ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3581 
3582   audio_transceiver->StopStandard();
3583   caller()->CreateAndSetAndSignalOffer();
3584   ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3585   ASSERT_EQ(0U, caller()->pc()->GetTransceivers().size());
3586   EXPECT_EQ(PeerConnectionInterface::kIceGatheringNew,
3587             caller()->pc()->ice_gathering_state());
3588   EXPECT_THAT(caller()->ice_gathering_state_history(),
3589               ElementsAre(PeerConnectionInterface::kIceGatheringGathering,
3590                           PeerConnectionInterface::kIceGatheringComplete,
3591                           PeerConnectionInterface::kIceGatheringNew));
3592 }
3593 
TEST_F(PeerConnectionIntegrationTestUnifiedPlan,StopTransceiverStopsAndRemovesTransceivers)3594 TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
3595        StopTransceiverStopsAndRemovesTransceivers) {
3596   RTCConfiguration config;
3597   ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
3598   ConnectFakeSignaling();
3599   auto audio_transceiver_or_error =
3600       caller()->pc()->AddTransceiver(caller()->CreateLocalAudioTrack());
3601   ASSERT_TRUE(audio_transceiver_or_error.ok());
3602   auto caller_transceiver = audio_transceiver_or_error.MoveValue();
3603 
3604   caller()->CreateAndSetAndSignalOffer();
3605   ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3606   caller_transceiver->StopStandard();
3607 
3608   auto callee_transceiver = callee()->pc()->GetTransceivers()[0];
3609   caller()->CreateAndSetAndSignalOffer();
3610   ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3611   EXPECT_EQ(0U, caller()->pc()->GetTransceivers().size());
3612   EXPECT_EQ(0U, callee()->pc()->GetTransceivers().size());
3613   EXPECT_EQ(0U, caller()->pc()->GetSenders().size());
3614   EXPECT_EQ(0U, callee()->pc()->GetSenders().size());
3615   EXPECT_EQ(0U, caller()->pc()->GetReceivers().size());
3616   EXPECT_EQ(0U, callee()->pc()->GetReceivers().size());
3617   EXPECT_TRUE(caller_transceiver->stopped());
3618   EXPECT_TRUE(callee_transceiver->stopped());
3619 }
3620 
TEST_F(PeerConnectionIntegrationTestUnifiedPlan,StopTransceiverEndsIncomingAudioTrack)3621 TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
3622        StopTransceiverEndsIncomingAudioTrack) {
3623   RTCConfiguration config;
3624   ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
3625   ConnectFakeSignaling();
3626   auto audio_transceiver_or_error =
3627       caller()->pc()->AddTransceiver(caller()->CreateLocalAudioTrack());
3628   ASSERT_TRUE(audio_transceiver_or_error.ok());
3629   auto audio_transceiver = audio_transceiver_or_error.MoveValue();
3630 
3631   caller()->CreateAndSetAndSignalOffer();
3632   ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3633   auto caller_track = audio_transceiver->receiver()->track();
3634   auto callee_track = callee()->pc()->GetReceivers()[0]->track();
3635   audio_transceiver->StopStandard();
3636   EXPECT_EQ(MediaStreamTrackInterface::TrackState::kEnded,
3637             caller_track->state());
3638   caller()->CreateAndSetAndSignalOffer();
3639   ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3640   EXPECT_EQ(MediaStreamTrackInterface::TrackState::kEnded,
3641             callee_track->state());
3642 }
3643 
TEST_F(PeerConnectionIntegrationTestUnifiedPlan,StopTransceiverEndsIncomingVideoTrack)3644 TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
3645        StopTransceiverEndsIncomingVideoTrack) {
3646   RTCConfiguration config;
3647   ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
3648   ConnectFakeSignaling();
3649   auto audio_transceiver_or_error =
3650       caller()->pc()->AddTransceiver(caller()->CreateLocalVideoTrack());
3651   ASSERT_TRUE(audio_transceiver_or_error.ok());
3652   auto audio_transceiver = audio_transceiver_or_error.MoveValue();
3653 
3654   caller()->CreateAndSetAndSignalOffer();
3655   ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3656   auto caller_track = audio_transceiver->receiver()->track();
3657   auto callee_track = callee()->pc()->GetReceivers()[0]->track();
3658   audio_transceiver->StopStandard();
3659   EXPECT_EQ(MediaStreamTrackInterface::TrackState::kEnded,
3660             caller_track->state());
3661   caller()->CreateAndSetAndSignalOffer();
3662   ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3663   EXPECT_EQ(MediaStreamTrackInterface::TrackState::kEnded,
3664             callee_track->state());
3665 }
3666 
TEST_P(PeerConnectionIntegrationTest,EndToEndRtpSenderVideoEncoderSelector)3667 TEST_P(PeerConnectionIntegrationTest, EndToEndRtpSenderVideoEncoderSelector) {
3668   ASSERT_TRUE(
3669       CreateOneDirectionalPeerConnectionWrappers(/*caller_to_callee=*/true));
3670   ConnectFakeSignaling();
3671   // Add one-directional video, from caller to callee.
3672   rtc::scoped_refptr<webrtc::VideoTrackInterface> caller_track =
3673       caller()->CreateLocalVideoTrack();
3674   auto sender = caller()->AddTrack(caller_track);
3675   PeerConnectionInterface::RTCOfferAnswerOptions options;
3676   options.offer_to_receive_video = 0;
3677   caller()->SetOfferAnswerOptions(options);
3678   caller()->CreateAndSetAndSignalOffer();
3679   ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3680   ASSERT_EQ(callee()->pc()->GetReceivers().size(), 1u);
3681 
3682   std::unique_ptr<MockEncoderSelector> encoder_selector =
3683       std::make_unique<MockEncoderSelector>();
3684   EXPECT_CALL(*encoder_selector, OnCurrentEncoder);
3685 
3686   sender->SetEncoderSelector(std::move(encoder_selector));
3687 
3688   // Expect video to be received in one direction.
3689   MediaExpectations media_expectations;
3690   media_expectations.CallerExpectsNoVideo();
3691   media_expectations.CalleeExpectsSomeVideo();
3692 
3693   EXPECT_TRUE(ExpectNewFrames(media_expectations));
3694 }
3695 
3696 }  // namespace
3697 
3698 }  // namespace webrtc
3699