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