xref: /aosp_15_r20/external/webrtc/pc/test/integration_test_helpers.h (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
2  *  Copyright 2012 The WebRTC project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 #ifndef PC_TEST_INTEGRATION_TEST_HELPERS_H_
12 #define PC_TEST_INTEGRATION_TEST_HELPERS_H_
13 
14 #include <limits.h>
15 #include <stdint.h>
16 #include <stdio.h>
17 
18 #include <algorithm>
19 #include <functional>
20 #include <limits>
21 #include <list>
22 #include <map>
23 #include <memory>
24 #include <set>
25 #include <string>
26 #include <utility>
27 #include <vector>
28 
29 #include "absl/algorithm/container.h"
30 #include "absl/memory/memory.h"
31 #include "absl/strings/string_view.h"
32 #include "absl/types/optional.h"
33 #include "api/audio_options.h"
34 #include "api/call/call_factory_interface.h"
35 #include "api/candidate.h"
36 #include "api/crypto/crypto_options.h"
37 #include "api/data_channel_interface.h"
38 #include "api/field_trials_view.h"
39 #include "api/ice_transport_interface.h"
40 #include "api/jsep.h"
41 #include "api/media_stream_interface.h"
42 #include "api/media_types.h"
43 #include "api/peer_connection_interface.h"
44 #include "api/rtc_error.h"
45 #include "api/rtc_event_log/rtc_event_log_factory.h"
46 #include "api/rtc_event_log/rtc_event_log_factory_interface.h"
47 #include "api/rtc_event_log_output.h"
48 #include "api/rtp_receiver_interface.h"
49 #include "api/rtp_sender_interface.h"
50 #include "api/rtp_transceiver_interface.h"
51 #include "api/scoped_refptr.h"
52 #include "api/stats/rtc_stats.h"
53 #include "api/stats/rtc_stats_report.h"
54 #include "api/stats/rtcstats_objects.h"
55 #include "api/task_queue/default_task_queue_factory.h"
56 #include "api/task_queue/pending_task_safety_flag.h"
57 #include "api/task_queue/task_queue_factory.h"
58 #include "api/transport/field_trial_based_config.h"
59 #include "api/uma_metrics.h"
60 #include "api/units/time_delta.h"
61 #include "api/video/video_rotation.h"
62 #include "api/video_codecs/sdp_video_format.h"
63 #include "api/video_codecs/video_decoder_factory.h"
64 #include "api/video_codecs/video_encoder_factory.h"
65 #include "call/call.h"
66 #include "logging/rtc_event_log/fake_rtc_event_log_factory.h"
67 #include "media/base/media_engine.h"
68 #include "media/base/stream_params.h"
69 #include "media/engine/fake_webrtc_video_engine.h"
70 #include "media/engine/webrtc_media_engine.h"
71 #include "media/engine/webrtc_media_engine_defaults.h"
72 #include "modules/audio_device/include/audio_device.h"
73 #include "modules/audio_processing/include/audio_processing.h"
74 #include "modules/audio_processing/test/audio_processing_builder_for_testing.h"
75 #include "p2p/base/fake_ice_transport.h"
76 #include "p2p/base/ice_transport_internal.h"
77 #include "p2p/base/mock_async_resolver.h"
78 #include "p2p/base/p2p_constants.h"
79 #include "p2p/base/port.h"
80 #include "p2p/base/port_allocator.h"
81 #include "p2p/base/port_interface.h"
82 #include "p2p/base/test_stun_server.h"
83 #include "p2p/base/test_turn_customizer.h"
84 #include "p2p/base/test_turn_server.h"
85 #include "p2p/client/basic_port_allocator.h"
86 #include "pc/dtmf_sender.h"
87 #include "pc/local_audio_source.h"
88 #include "pc/media_session.h"
89 #include "pc/peer_connection.h"
90 #include "pc/peer_connection_factory.h"
91 #include "pc/peer_connection_proxy.h"
92 #include "pc/rtp_media_utils.h"
93 #include "pc/session_description.h"
94 #include "pc/test/fake_audio_capture_module.h"
95 #include "pc/test/fake_periodic_video_source.h"
96 #include "pc/test/fake_periodic_video_track_source.h"
97 #include "pc/test/fake_rtc_certificate_generator.h"
98 #include "pc/test/fake_video_track_renderer.h"
99 #include "pc/test/mock_peer_connection_observers.h"
100 #include "pc/video_track_source.h"
101 #include "rtc_base/checks.h"
102 #include "rtc_base/event.h"
103 #include "rtc_base/fake_clock.h"
104 #include "rtc_base/fake_mdns_responder.h"
105 #include "rtc_base/fake_network.h"
106 #include "rtc_base/firewall_socket_server.h"
107 #include "rtc_base/gunit.h"
108 #include "rtc_base/helpers.h"
109 #include "rtc_base/ip_address.h"
110 #include "rtc_base/logging.h"
111 #include "rtc_base/mdns_responder_interface.h"
112 #include "rtc_base/numerics/safe_conversions.h"
113 #include "rtc_base/rtc_certificate_generator.h"
114 #include "rtc_base/socket_address.h"
115 #include "rtc_base/ssl_stream_adapter.h"
116 #include "rtc_base/task_queue_for_test.h"
117 #include "rtc_base/task_utils/repeating_task.h"
118 #include "rtc_base/test_certificate_verifier.h"
119 #include "rtc_base/thread.h"
120 #include "rtc_base/thread_annotations.h"
121 #include "rtc_base/time_utils.h"
122 #include "rtc_base/virtual_socket_server.h"
123 #include "system_wrappers/include/metrics.h"
124 #include "test/gmock.h"
125 #include "test/scoped_key_value_config.h"
126 
127 namespace webrtc {
128 
129 using ::cricket::ContentInfo;
130 using ::cricket::StreamParams;
131 using ::rtc::SocketAddress;
132 using ::testing::_;
133 using ::testing::Combine;
134 using ::testing::Contains;
135 using ::testing::DoAll;
136 using ::testing::ElementsAre;
137 using ::testing::NiceMock;
138 using ::testing::Return;
139 using ::testing::SetArgPointee;
140 using ::testing::UnorderedElementsAreArray;
141 using ::testing::Values;
142 using RTCConfiguration = PeerConnectionInterface::RTCConfiguration;
143 
144 static const int kDefaultTimeout = 10000;
145 static const int kMaxWaitForStatsMs = 3000;
146 static const int kMaxWaitForActivationMs = 5000;
147 static const int kMaxWaitForFramesMs = 10000;
148 // Default number of audio/video frames to wait for before considering a test
149 // successful.
150 static const int kDefaultExpectedAudioFrameCount = 3;
151 static const int kDefaultExpectedVideoFrameCount = 3;
152 
153 static const char kDataChannelLabel[] = "data_channel";
154 
155 // SRTP cipher name negotiated by the tests. This must be updated if the
156 // default changes.
157 static const int kDefaultSrtpCryptoSuite = rtc::kSrtpAes128CmSha1_80;
158 static const int kDefaultSrtpCryptoSuiteGcm = rtc::kSrtpAeadAes256Gcm;
159 
160 static const SocketAddress kDefaultLocalAddress("192.168.1.1", 0);
161 
162 // Helper function for constructing offer/answer options to initiate an ICE
163 // restart.
164 PeerConnectionInterface::RTCOfferAnswerOptions IceRestartOfferAnswerOptions();
165 
166 // Remove all stream information (SSRCs, track IDs, etc.) and "msid-semantic"
167 // attribute from received SDP, simulating a legacy endpoint.
168 void RemoveSsrcsAndMsids(cricket::SessionDescription* desc);
169 
170 // Removes all stream information besides the stream ids, simulating an
171 // endpoint that only signals a=msid lines to convey stream_ids.
172 void RemoveSsrcsAndKeepMsids(cricket::SessionDescription* desc);
173 
174 // TODO(https://crbug.com/webrtc/14175): Stop depending on "track" stats, the
175 // metrics we're interested in are already available in "inbound-rtp".
176 int FindFirstMediaStatsIndexByKind(
177     const std::string& kind,
178     const std::vector<const webrtc::DEPRECATED_RTCMediaStreamTrackStats*>&
179         media_stats_vec);
180 
181 class TaskQueueMetronome : public webrtc::Metronome {
182  public:
183   explicit TaskQueueMetronome(TimeDelta tick_period);
184   ~TaskQueueMetronome() override;
185 
186   // webrtc::Metronome implementation.
187   void RequestCallOnNextTick(absl::AnyInvocable<void() &&> callback) override;
188   TimeDelta TickPeriod() const override;
189 
190  private:
191   const TimeDelta tick_period_;
192   SequenceChecker sequence_checker_;
193   std::vector<absl::AnyInvocable<void() &&>> callbacks_;
194   ScopedTaskSafetyDetached safety_;
195 };
196 
197 class SignalingMessageReceiver {
198  public:
199   virtual void ReceiveSdpMessage(SdpType type, const std::string& msg) = 0;
200   virtual void ReceiveIceMessage(const std::string& sdp_mid,
201                                  int sdp_mline_index,
202                                  const std::string& msg) = 0;
203 
204  protected:
SignalingMessageReceiver()205   SignalingMessageReceiver() {}
~SignalingMessageReceiver()206   virtual ~SignalingMessageReceiver() {}
207 };
208 
209 class MockRtpReceiverObserver : public webrtc::RtpReceiverObserverInterface {
210  public:
MockRtpReceiverObserver(cricket::MediaType media_type)211   explicit MockRtpReceiverObserver(cricket::MediaType media_type)
212       : expected_media_type_(media_type) {}
213 
OnFirstPacketReceived(cricket::MediaType media_type)214   void OnFirstPacketReceived(cricket::MediaType media_type) override {
215     ASSERT_EQ(expected_media_type_, media_type);
216     first_packet_received_ = true;
217   }
218 
first_packet_received()219   bool first_packet_received() const { return first_packet_received_; }
220 
~MockRtpReceiverObserver()221   virtual ~MockRtpReceiverObserver() {}
222 
223  private:
224   bool first_packet_received_ = false;
225   cricket::MediaType expected_media_type_;
226 };
227 
228 // Helper class that wraps a peer connection, observes it, and can accept
229 // signaling messages from another wrapper.
230 //
231 // Uses a fake network, fake A/V capture, and optionally fake
232 // encoders/decoders, though they aren't used by default since they don't
233 // advertise support of any codecs.
234 // TODO(steveanton): See how this could become a subclass of
235 // PeerConnectionWrapper defined in peerconnectionwrapper.h.
236 class PeerConnectionIntegrationWrapper : public webrtc::PeerConnectionObserver,
237                                          public SignalingMessageReceiver {
238  public:
pc_factory()239   webrtc::PeerConnectionFactoryInterface* pc_factory() const {
240     return peer_connection_factory_.get();
241   }
242 
pc()243   webrtc::PeerConnectionInterface* pc() const { return peer_connection_.get(); }
244 
245   // If a signaling message receiver is set (via ConnectFakeSignaling), this
246   // will set the whole offer/answer exchange in motion. Just need to wait for
247   // the signaling state to reach "stable".
CreateAndSetAndSignalOffer()248   void CreateAndSetAndSignalOffer() {
249     auto offer = CreateOfferAndWait();
250     ASSERT_NE(nullptr, offer);
251     EXPECT_TRUE(SetLocalDescriptionAndSendSdpMessage(std::move(offer)));
252   }
253 
254   // Sets the options to be used when CreateAndSetAndSignalOffer is called, or
255   // when a remote offer is received (via fake signaling) and an answer is
256   // generated. By default, uses default options.
SetOfferAnswerOptions(const PeerConnectionInterface::RTCOfferAnswerOptions & options)257   void SetOfferAnswerOptions(
258       const PeerConnectionInterface::RTCOfferAnswerOptions& options) {
259     offer_answer_options_ = options;
260   }
261 
262   // Set a callback to be invoked when SDP is received via the fake signaling
263   // channel, which provides an opportunity to munge (modify) the SDP. This is
264   // used to test SDP being applied that a PeerConnection would normally not
265   // generate, but a non-JSEP endpoint might.
SetReceivedSdpMunger(std::function<void (cricket::SessionDescription *)> munger)266   void SetReceivedSdpMunger(
267       std::function<void(cricket::SessionDescription*)> munger) {
268     received_sdp_munger_ = std::move(munger);
269   }
270 
271   // Similar to the above, but this is run on SDP immediately after it's
272   // generated.
SetGeneratedSdpMunger(std::function<void (cricket::SessionDescription *)> munger)273   void SetGeneratedSdpMunger(
274       std::function<void(cricket::SessionDescription*)> munger) {
275     generated_sdp_munger_ = std::move(munger);
276   }
277 
278   // Set a callback to be invoked when a remote offer is received via the fake
279   // signaling channel. This provides an opportunity to change the
280   // PeerConnection state before an answer is created and sent to the caller.
SetRemoteOfferHandler(std::function<void ()> handler)281   void SetRemoteOfferHandler(std::function<void()> handler) {
282     remote_offer_handler_ = std::move(handler);
283   }
284 
SetRemoteAsyncResolver(rtc::MockAsyncResolver * resolver)285   void SetRemoteAsyncResolver(rtc::MockAsyncResolver* resolver) {
286     remote_async_resolver_ = resolver;
287   }
288 
289   // Every ICE connection state in order that has been seen by the observer.
290   std::vector<PeerConnectionInterface::IceConnectionState>
ice_connection_state_history()291   ice_connection_state_history() const {
292     return ice_connection_state_history_;
293   }
clear_ice_connection_state_history()294   void clear_ice_connection_state_history() {
295     ice_connection_state_history_.clear();
296   }
297 
298   // Every standardized ICE connection state in order that has been seen by the
299   // observer.
300   std::vector<PeerConnectionInterface::IceConnectionState>
standardized_ice_connection_state_history()301   standardized_ice_connection_state_history() const {
302     return standardized_ice_connection_state_history_;
303   }
304 
305   // Every PeerConnection state in order that has been seen by the observer.
306   std::vector<PeerConnectionInterface::PeerConnectionState>
peer_connection_state_history()307   peer_connection_state_history() const {
308     return peer_connection_state_history_;
309   }
310 
311   // Every ICE gathering state in order that has been seen by the observer.
312   std::vector<PeerConnectionInterface::IceGatheringState>
ice_gathering_state_history()313   ice_gathering_state_history() const {
314     return ice_gathering_state_history_;
315   }
316   std::vector<cricket::CandidatePairChangeEvent>
ice_candidate_pair_change_history()317   ice_candidate_pair_change_history() const {
318     return ice_candidate_pair_change_history_;
319   }
320 
321   // Every PeerConnection signaling state in order that has been seen by the
322   // observer.
323   std::vector<PeerConnectionInterface::SignalingState>
peer_connection_signaling_state_history()324   peer_connection_signaling_state_history() const {
325     return peer_connection_signaling_state_history_;
326   }
327 
AddAudioVideoTracks()328   void AddAudioVideoTracks() {
329     AddAudioTrack();
330     AddVideoTrack();
331   }
332 
AddAudioTrack()333   rtc::scoped_refptr<RtpSenderInterface> AddAudioTrack() {
334     return AddTrack(CreateLocalAudioTrack());
335   }
336 
AddVideoTrack()337   rtc::scoped_refptr<RtpSenderInterface> AddVideoTrack() {
338     return AddTrack(CreateLocalVideoTrack());
339   }
340 
CreateLocalAudioTrack()341   rtc::scoped_refptr<webrtc::AudioTrackInterface> CreateLocalAudioTrack() {
342     cricket::AudioOptions options;
343     // Disable highpass filter so that we can get all the test audio frames.
344     options.highpass_filter = false;
345     rtc::scoped_refptr<webrtc::AudioSourceInterface> source =
346         peer_connection_factory_->CreateAudioSource(options);
347     // TODO(perkj): Test audio source when it is implemented. Currently audio
348     // always use the default input.
349     return peer_connection_factory_->CreateAudioTrack(rtc::CreateRandomUuid(),
350                                                       source.get());
351   }
352 
CreateLocalVideoTrack()353   rtc::scoped_refptr<webrtc::VideoTrackInterface> CreateLocalVideoTrack() {
354     webrtc::FakePeriodicVideoSource::Config config;
355     config.timestamp_offset_ms = rtc::TimeMillis();
356     return CreateLocalVideoTrackInternal(config);
357   }
358 
359   rtc::scoped_refptr<webrtc::VideoTrackInterface>
CreateLocalVideoTrackWithConfig(webrtc::FakePeriodicVideoSource::Config config)360   CreateLocalVideoTrackWithConfig(
361       webrtc::FakePeriodicVideoSource::Config config) {
362     return CreateLocalVideoTrackInternal(config);
363   }
364 
365   rtc::scoped_refptr<webrtc::VideoTrackInterface>
CreateLocalVideoTrackWithRotation(webrtc::VideoRotation rotation)366   CreateLocalVideoTrackWithRotation(webrtc::VideoRotation rotation) {
367     webrtc::FakePeriodicVideoSource::Config config;
368     config.rotation = rotation;
369     config.timestamp_offset_ms = rtc::TimeMillis();
370     return CreateLocalVideoTrackInternal(config);
371   }
372 
373   rtc::scoped_refptr<RtpSenderInterface> AddTrack(
374       rtc::scoped_refptr<MediaStreamTrackInterface> track,
375       const std::vector<std::string>& stream_ids = {}) {
376     EXPECT_TRUE(track);
377     if (!track) {
378       return nullptr;
379     }
380     auto result = pc()->AddTrack(track, stream_ids);
381     EXPECT_EQ(RTCErrorType::NONE, result.error().type());
382     if (result.ok()) {
383       return result.MoveValue();
384     } else {
385       return nullptr;
386     }
387   }
388 
GetReceiversOfType(cricket::MediaType media_type)389   std::vector<rtc::scoped_refptr<RtpReceiverInterface>> GetReceiversOfType(
390       cricket::MediaType media_type) {
391     std::vector<rtc::scoped_refptr<RtpReceiverInterface>> receivers;
392     for (const auto& receiver : pc()->GetReceivers()) {
393       if (receiver->media_type() == media_type) {
394         receivers.push_back(receiver);
395       }
396     }
397     return receivers;
398   }
399 
GetFirstTransceiverOfType(cricket::MediaType media_type)400   rtc::scoped_refptr<RtpTransceiverInterface> GetFirstTransceiverOfType(
401       cricket::MediaType media_type) {
402     for (auto transceiver : pc()->GetTransceivers()) {
403       if (transceiver->receiver()->media_type() == media_type) {
404         return transceiver;
405       }
406     }
407     return nullptr;
408   }
409 
SignalingStateStable()410   bool SignalingStateStable() {
411     return pc()->signaling_state() == webrtc::PeerConnectionInterface::kStable;
412   }
413 
IceGatheringStateComplete()414   bool IceGatheringStateComplete() {
415     return pc()->ice_gathering_state() ==
416            webrtc::PeerConnectionInterface::kIceGatheringComplete;
417   }
418 
CreateDataChannel()419   void CreateDataChannel() { CreateDataChannel(nullptr); }
420 
CreateDataChannel(const webrtc::DataChannelInit * init)421   void CreateDataChannel(const webrtc::DataChannelInit* init) {
422     CreateDataChannel(kDataChannelLabel, init);
423   }
424 
CreateDataChannel(const std::string & label,const webrtc::DataChannelInit * init)425   void CreateDataChannel(const std::string& label,
426                          const webrtc::DataChannelInit* init) {
427     auto data_channel_or_error = pc()->CreateDataChannelOrError(label, init);
428     ASSERT_TRUE(data_channel_or_error.ok());
429     data_channels_.push_back(data_channel_or_error.MoveValue());
430     ASSERT_TRUE(data_channels_.back().get() != nullptr);
431     data_observers_.push_back(
432         std::make_unique<MockDataChannelObserver>(data_channels_.back().get()));
433   }
434 
435   // Return the last observed data channel.
data_channel()436   DataChannelInterface* data_channel() {
437     if (data_channels_.size() == 0) {
438       return nullptr;
439     }
440     return data_channels_.back().get();
441   }
442   // Return all data channels.
data_channels()443   std::vector<rtc::scoped_refptr<DataChannelInterface>>& data_channels() {
444     return data_channels_;
445   }
446 
data_observer()447   const MockDataChannelObserver* data_observer() const {
448     if (data_observers_.size() == 0) {
449       return nullptr;
450     }
451     return data_observers_.back().get();
452   }
453 
data_observers()454   std::vector<std::unique_ptr<MockDataChannelObserver>>& data_observers() {
455     return data_observers_;
456   }
457 
audio_frames_received()458   int audio_frames_received() const {
459     return fake_audio_capture_module_->frames_received();
460   }
461 
462   // Takes minimum of video frames received for each track.
463   //
464   // Can be used like:
465   // EXPECT_GE(expected_frames, min_video_frames_received_per_track());
466   //
467   // To ensure that all video tracks received at least a certain number of
468   // frames.
min_video_frames_received_per_track()469   int min_video_frames_received_per_track() const {
470     int min_frames = INT_MAX;
471     if (fake_video_renderers_.empty()) {
472       return 0;
473     }
474 
475     for (const auto& pair : fake_video_renderers_) {
476       min_frames = std::min(min_frames, pair.second->num_rendered_frames());
477     }
478     return min_frames;
479   }
480 
481   // Returns a MockStatsObserver in a state after stats gathering finished,
482   // which can be used to access the gathered stats.
OldGetStatsForTrack(webrtc::MediaStreamTrackInterface * track)483   rtc::scoped_refptr<MockStatsObserver> OldGetStatsForTrack(
484       webrtc::MediaStreamTrackInterface* track) {
485     auto observer = rtc::make_ref_counted<MockStatsObserver>();
486     EXPECT_TRUE(peer_connection_->GetStats(
487         observer.get(), nullptr,
488         PeerConnectionInterface::kStatsOutputLevelStandard));
489     EXPECT_TRUE_WAIT(observer->called(), kDefaultTimeout);
490     return observer;
491   }
492 
493   // Version that doesn't take a track "filter", and gathers all stats.
OldGetStats()494   rtc::scoped_refptr<MockStatsObserver> OldGetStats() {
495     return OldGetStatsForTrack(nullptr);
496   }
497 
498   // Synchronously gets stats and returns them. If it times out, fails the test
499   // and returns null.
NewGetStats()500   rtc::scoped_refptr<const webrtc::RTCStatsReport> NewGetStats() {
501     auto callback =
502         rtc::make_ref_counted<webrtc::MockRTCStatsCollectorCallback>();
503     peer_connection_->GetStats(callback.get());
504     EXPECT_TRUE_WAIT(callback->called(), kDefaultTimeout);
505     return callback->report();
506   }
507 
rendered_width()508   int rendered_width() {
509     EXPECT_FALSE(fake_video_renderers_.empty());
510     return fake_video_renderers_.empty()
511                ? 0
512                : fake_video_renderers_.begin()->second->width();
513   }
514 
rendered_height()515   int rendered_height() {
516     EXPECT_FALSE(fake_video_renderers_.empty());
517     return fake_video_renderers_.empty()
518                ? 0
519                : fake_video_renderers_.begin()->second->height();
520   }
521 
rendered_aspect_ratio()522   double rendered_aspect_ratio() {
523     if (rendered_height() == 0) {
524       return 0.0;
525     }
526     return static_cast<double>(rendered_width()) / rendered_height();
527   }
528 
rendered_rotation()529   webrtc::VideoRotation rendered_rotation() {
530     EXPECT_FALSE(fake_video_renderers_.empty());
531     return fake_video_renderers_.empty()
532                ? webrtc::kVideoRotation_0
533                : fake_video_renderers_.begin()->second->rotation();
534   }
535 
local_rendered_width()536   int local_rendered_width() {
537     return local_video_renderer_ ? local_video_renderer_->width() : 0;
538   }
539 
local_rendered_height()540   int local_rendered_height() {
541     return local_video_renderer_ ? local_video_renderer_->height() : 0;
542   }
543 
local_rendered_aspect_ratio()544   double local_rendered_aspect_ratio() {
545     if (local_rendered_height() == 0) {
546       return 0.0;
547     }
548     return static_cast<double>(local_rendered_width()) /
549            local_rendered_height();
550   }
551 
number_of_remote_streams()552   size_t number_of_remote_streams() {
553     if (!pc()) {
554       return 0;
555     }
556     return pc()->remote_streams()->count();
557   }
558 
remote_streams()559   StreamCollectionInterface* remote_streams() const {
560     if (!pc()) {
561       ADD_FAILURE();
562       return nullptr;
563     }
564     return pc()->remote_streams().get();
565   }
566 
local_streams()567   StreamCollectionInterface* local_streams() {
568     if (!pc()) {
569       ADD_FAILURE();
570       return nullptr;
571     }
572     return pc()->local_streams().get();
573   }
574 
signaling_state()575   webrtc::PeerConnectionInterface::SignalingState signaling_state() {
576     return pc()->signaling_state();
577   }
578 
ice_connection_state()579   webrtc::PeerConnectionInterface::IceConnectionState ice_connection_state() {
580     return pc()->ice_connection_state();
581   }
582 
583   webrtc::PeerConnectionInterface::IceConnectionState
standardized_ice_connection_state()584   standardized_ice_connection_state() {
585     return pc()->standardized_ice_connection_state();
586   }
587 
ice_gathering_state()588   webrtc::PeerConnectionInterface::IceGatheringState ice_gathering_state() {
589     return pc()->ice_gathering_state();
590   }
591 
592   // Returns a MockRtpReceiverObserver for each RtpReceiver returned by
593   // GetReceivers. They're updated automatically when a remote offer/answer
594   // from the fake signaling channel is applied, or when
595   // ResetRtpReceiverObservers below is called.
596   const std::vector<std::unique_ptr<MockRtpReceiverObserver>>&
rtp_receiver_observers()597   rtp_receiver_observers() {
598     return rtp_receiver_observers_;
599   }
600 
ResetRtpReceiverObservers()601   void ResetRtpReceiverObservers() {
602     rtp_receiver_observers_.clear();
603     for (const rtc::scoped_refptr<RtpReceiverInterface>& receiver :
604          pc()->GetReceivers()) {
605       std::unique_ptr<MockRtpReceiverObserver> observer(
606           new MockRtpReceiverObserver(receiver->media_type()));
607       receiver->SetObserver(observer.get());
608       rtp_receiver_observers_.push_back(std::move(observer));
609     }
610   }
611 
network_manager()612   rtc::FakeNetworkManager* network_manager() const {
613     return fake_network_manager_.get();
614   }
port_allocator()615   cricket::PortAllocator* port_allocator() const { return port_allocator_; }
616 
event_log_factory()617   webrtc::FakeRtcEventLogFactory* event_log_factory() const {
618     return event_log_factory_;
619   }
620 
last_candidate_gathered()621   const cricket::Candidate& last_candidate_gathered() const {
622     return last_candidate_gathered_;
623   }
error_event()624   const cricket::IceCandidateErrorEvent& error_event() const {
625     return error_event_;
626   }
627 
628   // Sets the mDNS responder for the owned fake network manager and keeps a
629   // reference to the responder.
SetMdnsResponder(std::unique_ptr<webrtc::FakeMdnsResponder> mdns_responder)630   void SetMdnsResponder(
631       std::unique_ptr<webrtc::FakeMdnsResponder> mdns_responder) {
632     RTC_DCHECK(mdns_responder != nullptr);
633     mdns_responder_ = mdns_responder.get();
634     network_manager()->set_mdns_responder(std::move(mdns_responder));
635   }
636 
637   // Returns null on failure.
CreateOfferAndWait()638   std::unique_ptr<SessionDescriptionInterface> CreateOfferAndWait() {
639     auto observer =
640         rtc::make_ref_counted<MockCreateSessionDescriptionObserver>();
641     pc()->CreateOffer(observer.get(), offer_answer_options_);
642     return WaitForDescriptionFromObserver(observer.get());
643   }
Rollback()644   bool Rollback() {
645     return SetRemoteDescription(
646         webrtc::CreateSessionDescription(SdpType::kRollback, ""));
647   }
648 
649   // Functions for querying stats.
StartWatchingDelayStats()650   void StartWatchingDelayStats() {
651     // Get the baseline numbers for audio_packets and audio_delay.
652     auto received_stats = NewGetStats();
653     auto rtp_stats =
654         received_stats->GetStatsOfType<webrtc::RTCInboundRTPStreamStats>()[0];
655     ASSERT_TRUE(rtp_stats->relative_packet_arrival_delay.is_defined());
656     ASSERT_TRUE(rtp_stats->packets_received.is_defined());
657     ASSERT_TRUE(rtp_stats->track_id.is_defined());
658     rtp_stats_id_ = rtp_stats->id();
659     audio_packets_stat_ = *rtp_stats->packets_received;
660     audio_delay_stat_ = *rtp_stats->relative_packet_arrival_delay;
661     audio_samples_stat_ = *rtp_stats->total_samples_received;
662     audio_concealed_stat_ = *rtp_stats->concealed_samples;
663   }
664 
UpdateDelayStats(std::string tag,int desc_size)665   void UpdateDelayStats(std::string tag, int desc_size) {
666     auto report = NewGetStats();
667     auto rtp_stats =
668         report->GetAs<webrtc::RTCInboundRTPStreamStats>(rtp_stats_id_);
669     ASSERT_TRUE(rtp_stats);
670     auto delta_packets = *rtp_stats->packets_received - audio_packets_stat_;
671     auto delta_rpad =
672         *rtp_stats->relative_packet_arrival_delay - audio_delay_stat_;
673     auto recent_delay = delta_packets > 0 ? delta_rpad / delta_packets : -1;
674     // The purpose of these checks is to sound the alarm early if we introduce
675     // serious regressions. The numbers are not acceptable for production, but
676     // occur on slow bots.
677     //
678     // An average relative packet arrival delay over the renegotiation of
679     // > 100 ms indicates that something is dramatically wrong, and will impact
680     // quality for sure.
681     // Worst bots:
682     // linux_x86_dbg at 0.206
683 #if !defined(NDEBUG)
684     EXPECT_GT(0.25, recent_delay) << tag << " size " << desc_size;
685 #else
686     EXPECT_GT(0.1, recent_delay) << tag << " size " << desc_size;
687 #endif
688     auto delta_samples =
689         *rtp_stats->total_samples_received - audio_samples_stat_;
690     auto delta_concealed =
691         *rtp_stats->concealed_samples - audio_concealed_stat_;
692     // These limits should be adjusted down as we improve:
693     //
694     // Concealing more than 4000 samples during a renegotiation is unacceptable.
695     // But some bots are slow.
696 
697     // Worst bots:
698     // linux_more_configs bot at conceal count 5184
699     // android_arm_rel at conceal count 9241
700     // linux_x86_dbg at 15174
701 #if !defined(NDEBUG)
702     EXPECT_GT(18000U, delta_concealed) << "Concealed " << delta_concealed
703                                        << " of " << delta_samples << " samples";
704 #else
705     EXPECT_GT(15000U, delta_concealed) << "Concealed " << delta_concealed
706                                        << " of " << delta_samples << " samples";
707 #endif
708     // Concealing more than 20% of samples during a renegotiation is
709     // unacceptable.
710     // Worst bots:
711     // Nondebug: Linux32 Release at conceal rate 0.606597 (CI run)
712     // Debug: linux_x86_dbg bot at conceal rate 0.854
713     if (delta_samples > 0) {
714 #if !defined(NDEBUG)
715       EXPECT_LT(1.0 * delta_concealed / delta_samples, 0.95)
716           << "Concealed " << delta_concealed << " of " << delta_samples
717           << " samples";
718 #else
719       EXPECT_LT(1.0 * delta_concealed / delta_samples, 0.7)
720           << "Concealed " << delta_concealed << " of " << delta_samples
721           << " samples";
722 #endif
723     }
724     // Increment trailing counters
725     audio_packets_stat_ = *rtp_stats->packets_received;
726     audio_delay_stat_ = *rtp_stats->relative_packet_arrival_delay;
727     audio_samples_stat_ = *rtp_stats->total_samples_received;
728     audio_concealed_stat_ = *rtp_stats->concealed_samples;
729   }
730 
731   // Sets number of candidates expected
ExpectCandidates(int candidate_count)732   void ExpectCandidates(int candidate_count) {
733     candidates_expected_ = candidate_count;
734   }
735 
736  private:
737   // Constructor used by friend class PeerConnectionIntegrationBaseTest.
PeerConnectionIntegrationWrapper(const std::string & debug_name)738   explicit PeerConnectionIntegrationWrapper(const std::string& debug_name)
739       : debug_name_(debug_name) {}
740 
Init(const PeerConnectionFactory::Options * options,const PeerConnectionInterface::RTCConfiguration * config,webrtc::PeerConnectionDependencies dependencies,rtc::SocketServer * socket_server,rtc::Thread * network_thread,rtc::Thread * worker_thread,std::unique_ptr<webrtc::FakeRtcEventLogFactory> event_log_factory,bool reset_encoder_factory,bool reset_decoder_factory,bool create_media_engine)741   bool Init(const PeerConnectionFactory::Options* options,
742             const PeerConnectionInterface::RTCConfiguration* config,
743             webrtc::PeerConnectionDependencies dependencies,
744             rtc::SocketServer* socket_server,
745             rtc::Thread* network_thread,
746             rtc::Thread* worker_thread,
747             std::unique_ptr<webrtc::FakeRtcEventLogFactory> event_log_factory,
748             bool reset_encoder_factory,
749             bool reset_decoder_factory,
750             bool create_media_engine) {
751     // There's an error in this test code if Init ends up being called twice.
752     RTC_DCHECK(!peer_connection_);
753     RTC_DCHECK(!peer_connection_factory_);
754 
755     fake_network_manager_.reset(new rtc::FakeNetworkManager());
756     fake_network_manager_->AddInterface(kDefaultLocalAddress);
757 
758     std::unique_ptr<cricket::PortAllocator> port_allocator(
759         new cricket::BasicPortAllocator(
760             fake_network_manager_.get(),
761             std::make_unique<rtc::BasicPacketSocketFactory>(socket_server)));
762     port_allocator_ = port_allocator.get();
763     fake_audio_capture_module_ = FakeAudioCaptureModule::Create();
764     if (!fake_audio_capture_module_) {
765       return false;
766     }
767     rtc::Thread* const signaling_thread = rtc::Thread::Current();
768 
769     webrtc::PeerConnectionFactoryDependencies pc_factory_dependencies;
770     pc_factory_dependencies.network_thread = network_thread;
771     pc_factory_dependencies.worker_thread = worker_thread;
772     pc_factory_dependencies.signaling_thread = signaling_thread;
773     pc_factory_dependencies.task_queue_factory =
774         webrtc::CreateDefaultTaskQueueFactory();
775     pc_factory_dependencies.trials = std::make_unique<FieldTrialBasedConfig>();
776     pc_factory_dependencies.metronome =
777         std::make_unique<TaskQueueMetronome>(TimeDelta::Millis(8));
778     cricket::MediaEngineDependencies media_deps;
779     media_deps.task_queue_factory =
780         pc_factory_dependencies.task_queue_factory.get();
781     media_deps.adm = fake_audio_capture_module_;
782     webrtc::SetMediaEngineDefaults(&media_deps);
783 
784     if (reset_encoder_factory) {
785       media_deps.video_encoder_factory.reset();
786     }
787     if (reset_decoder_factory) {
788       media_deps.video_decoder_factory.reset();
789     }
790 
791     if (!media_deps.audio_processing) {
792       // If the standard Creation method for APM returns a null pointer, instead
793       // use the builder for testing to create an APM object.
794       media_deps.audio_processing = AudioProcessingBuilderForTesting().Create();
795     }
796 
797     media_deps.trials = pc_factory_dependencies.trials.get();
798 
799     if (create_media_engine) {
800       pc_factory_dependencies.media_engine =
801           cricket::CreateMediaEngine(std::move(media_deps));
802     }
803     pc_factory_dependencies.call_factory = webrtc::CreateCallFactory();
804     if (event_log_factory) {
805       event_log_factory_ = event_log_factory.get();
806       pc_factory_dependencies.event_log_factory = std::move(event_log_factory);
807     } else {
808       pc_factory_dependencies.event_log_factory =
809           std::make_unique<webrtc::RtcEventLogFactory>(
810               pc_factory_dependencies.task_queue_factory.get());
811     }
812     peer_connection_factory_ = webrtc::CreateModularPeerConnectionFactory(
813         std::move(pc_factory_dependencies));
814 
815     if (!peer_connection_factory_) {
816       return false;
817     }
818     if (options) {
819       peer_connection_factory_->SetOptions(*options);
820     }
821     if (config) {
822       sdp_semantics_ = config->sdp_semantics;
823     }
824 
825     dependencies.allocator = std::move(port_allocator);
826     peer_connection_ = CreatePeerConnection(config, std::move(dependencies));
827     return peer_connection_.get() != nullptr;
828   }
829 
CreatePeerConnection(const PeerConnectionInterface::RTCConfiguration * config,webrtc::PeerConnectionDependencies dependencies)830   rtc::scoped_refptr<webrtc::PeerConnectionInterface> CreatePeerConnection(
831       const PeerConnectionInterface::RTCConfiguration* config,
832       webrtc::PeerConnectionDependencies dependencies) {
833     PeerConnectionInterface::RTCConfiguration modified_config;
834     modified_config.sdp_semantics = sdp_semantics_;
835     // If `config` is null, this will result in a default configuration being
836     // used.
837     if (config) {
838       modified_config = *config;
839     }
840     // Disable resolution adaptation; we don't want it interfering with the
841     // test results.
842     // TODO(deadbeef): Do something more robust. Since we're testing for aspect
843     // ratios and not specific resolutions, is this even necessary?
844     modified_config.set_cpu_adaptation(false);
845 
846     dependencies.observer = this;
847     auto peer_connection_or_error =
848         peer_connection_factory_->CreatePeerConnectionOrError(
849             modified_config, std::move(dependencies));
850     return peer_connection_or_error.ok() ? peer_connection_or_error.MoveValue()
851                                          : nullptr;
852   }
853 
set_signaling_message_receiver(SignalingMessageReceiver * signaling_message_receiver)854   void set_signaling_message_receiver(
855       SignalingMessageReceiver* signaling_message_receiver) {
856     signaling_message_receiver_ = signaling_message_receiver;
857   }
858 
set_signaling_delay_ms(int delay_ms)859   void set_signaling_delay_ms(int delay_ms) { signaling_delay_ms_ = delay_ms; }
860 
set_signal_ice_candidates(bool signal)861   void set_signal_ice_candidates(bool signal) {
862     signal_ice_candidates_ = signal;
863   }
864 
CreateLocalVideoTrackInternal(webrtc::FakePeriodicVideoSource::Config config)865   rtc::scoped_refptr<webrtc::VideoTrackInterface> CreateLocalVideoTrackInternal(
866       webrtc::FakePeriodicVideoSource::Config config) {
867     // Set max frame rate to 10fps to reduce the risk of test flakiness.
868     // TODO(deadbeef): Do something more robust.
869     config.frame_interval_ms = 100;
870 
871     video_track_sources_.emplace_back(
872         rtc::make_ref_counted<webrtc::FakePeriodicVideoTrackSource>(
873             config, false /* remote */));
874     rtc::scoped_refptr<webrtc::VideoTrackInterface> track(
875         peer_connection_factory_->CreateVideoTrack(
876             rtc::CreateRandomUuid(), video_track_sources_.back().get()));
877     if (!local_video_renderer_) {
878       local_video_renderer_.reset(
879           new webrtc::FakeVideoTrackRenderer(track.get()));
880     }
881     return track;
882   }
883 
HandleIncomingOffer(const std::string & msg)884   void HandleIncomingOffer(const std::string& msg) {
885     RTC_LOG(LS_INFO) << debug_name_ << ": HandleIncomingOffer";
886     std::unique_ptr<SessionDescriptionInterface> desc =
887         webrtc::CreateSessionDescription(SdpType::kOffer, msg);
888     if (received_sdp_munger_) {
889       received_sdp_munger_(desc->description());
890     }
891 
892     EXPECT_TRUE(SetRemoteDescription(std::move(desc)));
893     // Setting a remote description may have changed the number of receivers,
894     // so reset the receiver observers.
895     ResetRtpReceiverObservers();
896     if (remote_offer_handler_) {
897       remote_offer_handler_();
898     }
899     auto answer = CreateAnswer();
900     ASSERT_NE(nullptr, answer);
901     EXPECT_TRUE(SetLocalDescriptionAndSendSdpMessage(std::move(answer)));
902   }
903 
HandleIncomingAnswer(const std::string & msg)904   void HandleIncomingAnswer(const std::string& msg) {
905     RTC_LOG(LS_INFO) << debug_name_ << ": HandleIncomingAnswer";
906     std::unique_ptr<SessionDescriptionInterface> desc =
907         webrtc::CreateSessionDescription(SdpType::kAnswer, msg);
908     if (received_sdp_munger_) {
909       received_sdp_munger_(desc->description());
910     }
911 
912     EXPECT_TRUE(SetRemoteDescription(std::move(desc)));
913     // Set the RtpReceiverObserver after receivers are created.
914     ResetRtpReceiverObservers();
915   }
916 
917   // Returns null on failure.
CreateAnswer()918   std::unique_ptr<SessionDescriptionInterface> CreateAnswer() {
919     auto observer =
920         rtc::make_ref_counted<MockCreateSessionDescriptionObserver>();
921     pc()->CreateAnswer(observer.get(), offer_answer_options_);
922     return WaitForDescriptionFromObserver(observer.get());
923   }
924 
WaitForDescriptionFromObserver(MockCreateSessionDescriptionObserver * observer)925   std::unique_ptr<SessionDescriptionInterface> WaitForDescriptionFromObserver(
926       MockCreateSessionDescriptionObserver* observer) {
927     EXPECT_EQ_WAIT(true, observer->called(), kDefaultTimeout);
928     if (!observer->result()) {
929       return nullptr;
930     }
931     auto description = observer->MoveDescription();
932     if (generated_sdp_munger_) {
933       generated_sdp_munger_(description->description());
934     }
935     return description;
936   }
937 
938   // Setting the local description and sending the SDP message over the fake
939   // signaling channel are combined into the same method because the SDP
940   // message needs to be sent as soon as SetLocalDescription finishes, without
941   // waiting for the observer to be called. This ensures that ICE candidates
942   // don't outrace the description.
SetLocalDescriptionAndSendSdpMessage(std::unique_ptr<SessionDescriptionInterface> desc)943   bool SetLocalDescriptionAndSendSdpMessage(
944       std::unique_ptr<SessionDescriptionInterface> desc) {
945     auto observer = rtc::make_ref_counted<MockSetSessionDescriptionObserver>();
946     RTC_LOG(LS_INFO) << debug_name_ << ": SetLocalDescriptionAndSendSdpMessage";
947     SdpType type = desc->GetType();
948     std::string sdp;
949     EXPECT_TRUE(desc->ToString(&sdp));
950     RTC_LOG(LS_INFO) << debug_name_ << ": local SDP contents=\n" << sdp;
951     pc()->SetLocalDescription(observer.get(), desc.release());
952     RemoveUnusedVideoRenderers();
953     // As mentioned above, we need to send the message immediately after
954     // SetLocalDescription.
955     SendSdpMessage(type, sdp);
956     EXPECT_TRUE_WAIT(observer->called(), kDefaultTimeout);
957     return true;
958   }
959 
SetRemoteDescription(std::unique_ptr<SessionDescriptionInterface> desc)960   bool SetRemoteDescription(std::unique_ptr<SessionDescriptionInterface> desc) {
961     auto observer = rtc::make_ref_counted<MockSetSessionDescriptionObserver>();
962     RTC_LOG(LS_INFO) << debug_name_ << ": SetRemoteDescription";
963     pc()->SetRemoteDescription(observer.get(), desc.release());
964     RemoveUnusedVideoRenderers();
965     EXPECT_TRUE_WAIT(observer->called(), kDefaultTimeout);
966     return observer->result();
967   }
968 
969   // This is a work around to remove unused fake_video_renderers from
970   // transceivers that have either stopped or are no longer receiving.
RemoveUnusedVideoRenderers()971   void RemoveUnusedVideoRenderers() {
972     if (sdp_semantics_ != SdpSemantics::kUnifiedPlan) {
973       return;
974     }
975     auto transceivers = pc()->GetTransceivers();
976     std::set<std::string> active_renderers;
977     for (auto& transceiver : transceivers) {
978       // Note - we don't check for direction here. This function is called
979       // before direction is set, and in that case, we should not remove
980       // the renderer.
981       if (transceiver->receiver()->media_type() == cricket::MEDIA_TYPE_VIDEO) {
982         active_renderers.insert(transceiver->receiver()->track()->id());
983       }
984     }
985     for (auto it = fake_video_renderers_.begin();
986          it != fake_video_renderers_.end();) {
987       // Remove fake video renderers belonging to any non-active transceivers.
988       if (!active_renderers.count(it->first)) {
989         it = fake_video_renderers_.erase(it);
990       } else {
991         it++;
992       }
993     }
994   }
995 
996   // Simulate sending a blob of SDP with delay `signaling_delay_ms_` (0 by
997   // default).
SendSdpMessage(SdpType type,const std::string & msg)998   void SendSdpMessage(SdpType type, const std::string& msg) {
999     if (signaling_delay_ms_ == 0) {
1000       RelaySdpMessageIfReceiverExists(type, msg);
1001     } else {
1002       rtc::Thread::Current()->PostDelayedTask(
1003           SafeTask(task_safety_.flag(),
1004                    [this, type, msg] {
1005                      RelaySdpMessageIfReceiverExists(type, msg);
1006                    }),
1007           TimeDelta::Millis(signaling_delay_ms_));
1008     }
1009   }
1010 
RelaySdpMessageIfReceiverExists(SdpType type,const std::string & msg)1011   void RelaySdpMessageIfReceiverExists(SdpType type, const std::string& msg) {
1012     if (signaling_message_receiver_) {
1013       signaling_message_receiver_->ReceiveSdpMessage(type, msg);
1014     }
1015   }
1016 
1017   // Simulate trickling an ICE candidate with delay `signaling_delay_ms_` (0 by
1018   // default).
SendIceMessage(const std::string & sdp_mid,int sdp_mline_index,const std::string & msg)1019   void SendIceMessage(const std::string& sdp_mid,
1020                       int sdp_mline_index,
1021                       const std::string& msg) {
1022     if (signaling_delay_ms_ == 0) {
1023       RelayIceMessageIfReceiverExists(sdp_mid, sdp_mline_index, msg);
1024     } else {
1025       rtc::Thread::Current()->PostDelayedTask(
1026           SafeTask(task_safety_.flag(),
1027                    [this, sdp_mid, sdp_mline_index, msg] {
1028                      RelayIceMessageIfReceiverExists(sdp_mid, sdp_mline_index,
1029                                                      msg);
1030                    }),
1031           TimeDelta::Millis(signaling_delay_ms_));
1032     }
1033   }
1034 
RelayIceMessageIfReceiverExists(const std::string & sdp_mid,int sdp_mline_index,const std::string & msg)1035   void RelayIceMessageIfReceiverExists(const std::string& sdp_mid,
1036                                        int sdp_mline_index,
1037                                        const std::string& msg) {
1038     if (signaling_message_receiver_) {
1039       signaling_message_receiver_->ReceiveIceMessage(sdp_mid, sdp_mline_index,
1040                                                      msg);
1041     }
1042   }
1043 
1044   // SignalingMessageReceiver callbacks.
ReceiveSdpMessage(SdpType type,const std::string & msg)1045   void ReceiveSdpMessage(SdpType type, const std::string& msg) override {
1046     if (type == SdpType::kOffer) {
1047       HandleIncomingOffer(msg);
1048     } else {
1049       HandleIncomingAnswer(msg);
1050     }
1051   }
1052 
ReceiveIceMessage(const std::string & sdp_mid,int sdp_mline_index,const std::string & msg)1053   void ReceiveIceMessage(const std::string& sdp_mid,
1054                          int sdp_mline_index,
1055                          const std::string& msg) override {
1056     RTC_LOG(LS_INFO) << debug_name_ << ": ReceiveIceMessage";
1057     absl::optional<RTCError> result;
1058     pc()->AddIceCandidate(absl::WrapUnique(webrtc::CreateIceCandidate(
1059                               sdp_mid, sdp_mline_index, msg, nullptr)),
1060                           [&result](RTCError r) { result = r; });
1061     EXPECT_TRUE_WAIT(result.has_value(), kDefaultTimeout);
1062     EXPECT_TRUE(result.value().ok());
1063   }
1064 
1065   // PeerConnectionObserver callbacks.
OnSignalingChange(webrtc::PeerConnectionInterface::SignalingState new_state)1066   void OnSignalingChange(
1067       webrtc::PeerConnectionInterface::SignalingState new_state) override {
1068     EXPECT_EQ(pc()->signaling_state(), new_state);
1069     peer_connection_signaling_state_history_.push_back(new_state);
1070   }
OnAddTrack(rtc::scoped_refptr<RtpReceiverInterface> receiver,const std::vector<rtc::scoped_refptr<MediaStreamInterface>> & streams)1071   void OnAddTrack(rtc::scoped_refptr<RtpReceiverInterface> receiver,
1072                   const std::vector<rtc::scoped_refptr<MediaStreamInterface>>&
1073                       streams) override {
1074     if (receiver->media_type() == cricket::MEDIA_TYPE_VIDEO) {
1075       rtc::scoped_refptr<VideoTrackInterface> video_track(
1076           static_cast<VideoTrackInterface*>(receiver->track().get()));
1077       ASSERT_TRUE(fake_video_renderers_.find(video_track->id()) ==
1078                   fake_video_renderers_.end());
1079       fake_video_renderers_[video_track->id()] =
1080           std::make_unique<FakeVideoTrackRenderer>(video_track.get());
1081     }
1082   }
OnRemoveTrack(rtc::scoped_refptr<RtpReceiverInterface> receiver)1083   void OnRemoveTrack(
1084       rtc::scoped_refptr<RtpReceiverInterface> receiver) override {
1085     if (receiver->media_type() == cricket::MEDIA_TYPE_VIDEO) {
1086       auto it = fake_video_renderers_.find(receiver->track()->id());
1087       if (it != fake_video_renderers_.end()) {
1088         fake_video_renderers_.erase(it);
1089       } else {
1090         RTC_LOG(LS_ERROR) << "OnRemoveTrack called for non-active renderer";
1091       }
1092     }
1093   }
OnRenegotiationNeeded()1094   void OnRenegotiationNeeded() override {}
OnIceConnectionChange(webrtc::PeerConnectionInterface::IceConnectionState new_state)1095   void OnIceConnectionChange(
1096       webrtc::PeerConnectionInterface::IceConnectionState new_state) override {
1097     EXPECT_EQ(pc()->ice_connection_state(), new_state);
1098     ice_connection_state_history_.push_back(new_state);
1099   }
OnStandardizedIceConnectionChange(webrtc::PeerConnectionInterface::IceConnectionState new_state)1100   void OnStandardizedIceConnectionChange(
1101       webrtc::PeerConnectionInterface::IceConnectionState new_state) override {
1102     standardized_ice_connection_state_history_.push_back(new_state);
1103   }
OnConnectionChange(webrtc::PeerConnectionInterface::PeerConnectionState new_state)1104   void OnConnectionChange(
1105       webrtc::PeerConnectionInterface::PeerConnectionState new_state) override {
1106     peer_connection_state_history_.push_back(new_state);
1107   }
1108 
OnIceGatheringChange(webrtc::PeerConnectionInterface::IceGatheringState new_state)1109   void OnIceGatheringChange(
1110       webrtc::PeerConnectionInterface::IceGatheringState new_state) override {
1111     EXPECT_EQ(pc()->ice_gathering_state(), new_state);
1112     ice_gathering_state_history_.push_back(new_state);
1113   }
1114 
OnIceSelectedCandidatePairChanged(const cricket::CandidatePairChangeEvent & event)1115   void OnIceSelectedCandidatePairChanged(
1116       const cricket::CandidatePairChangeEvent& event) {
1117     ice_candidate_pair_change_history_.push_back(event);
1118   }
1119 
OnIceCandidate(const webrtc::IceCandidateInterface * candidate)1120   void OnIceCandidate(const webrtc::IceCandidateInterface* candidate) override {
1121     RTC_LOG(LS_INFO) << debug_name_ << ": OnIceCandidate";
1122 
1123     if (remote_async_resolver_) {
1124       const auto& local_candidate = candidate->candidate();
1125       if (local_candidate.address().IsUnresolvedIP()) {
1126         RTC_DCHECK(local_candidate.type() == cricket::LOCAL_PORT_TYPE);
1127         rtc::SocketAddress resolved_addr(local_candidate.address());
1128         const auto resolved_ip = mdns_responder_->GetMappedAddressForName(
1129             local_candidate.address().hostname());
1130         RTC_DCHECK(!resolved_ip.IsNil());
1131         resolved_addr.SetResolvedIP(resolved_ip);
1132         EXPECT_CALL(*remote_async_resolver_, GetResolvedAddress(_, _))
1133             .WillOnce(DoAll(SetArgPointee<1>(resolved_addr), Return(true)));
1134         EXPECT_CALL(*remote_async_resolver_, Destroy(_));
1135       }
1136     }
1137 
1138     // Check if we expected to have a candidate.
1139     EXPECT_GT(candidates_expected_, 1);
1140     candidates_expected_--;
1141     std::string ice_sdp;
1142     EXPECT_TRUE(candidate->ToString(&ice_sdp));
1143     if (signaling_message_receiver_ == nullptr || !signal_ice_candidates_) {
1144       // Remote party may be deleted.
1145       return;
1146     }
1147     SendIceMessage(candidate->sdp_mid(), candidate->sdp_mline_index(), ice_sdp);
1148     last_candidate_gathered_ = candidate->candidate();
1149   }
OnIceCandidateError(const std::string & address,int port,const std::string & url,int error_code,const std::string & error_text)1150   void OnIceCandidateError(const std::string& address,
1151                            int port,
1152                            const std::string& url,
1153                            int error_code,
1154                            const std::string& error_text) override {
1155     error_event_ = cricket::IceCandidateErrorEvent(address, port, url,
1156                                                    error_code, error_text);
1157   }
OnDataChannel(rtc::scoped_refptr<DataChannelInterface> data_channel)1158   void OnDataChannel(
1159       rtc::scoped_refptr<DataChannelInterface> data_channel) override {
1160     RTC_LOG(LS_INFO) << debug_name_ << ": OnDataChannel";
1161     data_channels_.push_back(data_channel);
1162     data_observers_.push_back(
1163         std::make_unique<MockDataChannelObserver>(data_channel.get()));
1164   }
1165 
1166   std::string debug_name_;
1167 
1168   std::unique_ptr<rtc::FakeNetworkManager> fake_network_manager_;
1169   // Reference to the mDNS responder owned by `fake_network_manager_` after set.
1170   webrtc::FakeMdnsResponder* mdns_responder_ = nullptr;
1171 
1172   rtc::scoped_refptr<webrtc::PeerConnectionInterface> peer_connection_;
1173   rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface>
1174       peer_connection_factory_;
1175 
1176   cricket::PortAllocator* port_allocator_;
1177   // Needed to keep track of number of frames sent.
1178   rtc::scoped_refptr<FakeAudioCaptureModule> fake_audio_capture_module_;
1179   // Needed to keep track of number of frames received.
1180   std::map<std::string, std::unique_ptr<webrtc::FakeVideoTrackRenderer>>
1181       fake_video_renderers_;
1182   // Needed to ensure frames aren't received for removed tracks.
1183   std::vector<std::unique_ptr<webrtc::FakeVideoTrackRenderer>>
1184       removed_fake_video_renderers_;
1185 
1186   // For remote peer communication.
1187   SignalingMessageReceiver* signaling_message_receiver_ = nullptr;
1188   int signaling_delay_ms_ = 0;
1189   bool signal_ice_candidates_ = true;
1190   cricket::Candidate last_candidate_gathered_;
1191   cricket::IceCandidateErrorEvent error_event_;
1192 
1193   // Store references to the video sources we've created, so that we can stop
1194   // them, if required.
1195   std::vector<rtc::scoped_refptr<webrtc::VideoTrackSource>>
1196       video_track_sources_;
1197   // `local_video_renderer_` attached to the first created local video track.
1198   std::unique_ptr<webrtc::FakeVideoTrackRenderer> local_video_renderer_;
1199 
1200   SdpSemantics sdp_semantics_;
1201   PeerConnectionInterface::RTCOfferAnswerOptions offer_answer_options_;
1202   std::function<void(cricket::SessionDescription*)> received_sdp_munger_;
1203   std::function<void(cricket::SessionDescription*)> generated_sdp_munger_;
1204   std::function<void()> remote_offer_handler_;
1205   rtc::MockAsyncResolver* remote_async_resolver_ = nullptr;
1206   // All data channels either created or observed on this peerconnection
1207   std::vector<rtc::scoped_refptr<DataChannelInterface>> data_channels_;
1208   std::vector<std::unique_ptr<MockDataChannelObserver>> data_observers_;
1209 
1210   std::vector<std::unique_ptr<MockRtpReceiverObserver>> rtp_receiver_observers_;
1211 
1212   std::vector<PeerConnectionInterface::IceConnectionState>
1213       ice_connection_state_history_;
1214   std::vector<PeerConnectionInterface::IceConnectionState>
1215       standardized_ice_connection_state_history_;
1216   std::vector<PeerConnectionInterface::PeerConnectionState>
1217       peer_connection_state_history_;
1218   std::vector<PeerConnectionInterface::IceGatheringState>
1219       ice_gathering_state_history_;
1220   std::vector<cricket::CandidatePairChangeEvent>
1221       ice_candidate_pair_change_history_;
1222   std::vector<PeerConnectionInterface::SignalingState>
1223       peer_connection_signaling_state_history_;
1224   webrtc::FakeRtcEventLogFactory* event_log_factory_;
1225 
1226   // Number of ICE candidates expected. The default is no limit.
1227   int candidates_expected_ = std::numeric_limits<int>::max();
1228 
1229   // Variables for tracking delay stats on an audio track
1230   int audio_packets_stat_ = 0;
1231   double audio_delay_stat_ = 0.0;
1232   uint64_t audio_samples_stat_ = 0;
1233   uint64_t audio_concealed_stat_ = 0;
1234   std::string rtp_stats_id_;
1235 
1236   ScopedTaskSafety task_safety_;
1237 
1238   friend class PeerConnectionIntegrationBaseTest;
1239 };
1240 
1241 class MockRtcEventLogOutput : public webrtc::RtcEventLogOutput {
1242  public:
1243   virtual ~MockRtcEventLogOutput() = default;
1244   MOCK_METHOD(bool, IsActive, (), (const, override));
1245   MOCK_METHOD(bool, Write, (absl::string_view), (override));
1246 };
1247 
1248 // This helper object is used for both specifying how many audio/video frames
1249 // are expected to be received for a caller/callee. It provides helper functions
1250 // to specify these expectations. The object initially starts in a state of no
1251 // expectations.
1252 class MediaExpectations {
1253  public:
1254   enum ExpectFrames {
1255     kExpectSomeFrames,
1256     kExpectNoFrames,
1257     kNoExpectation,
1258   };
1259 
ExpectBidirectionalAudioAndVideo()1260   void ExpectBidirectionalAudioAndVideo() {
1261     ExpectBidirectionalAudio();
1262     ExpectBidirectionalVideo();
1263   }
1264 
ExpectBidirectionalAudio()1265   void ExpectBidirectionalAudio() {
1266     CallerExpectsSomeAudio();
1267     CalleeExpectsSomeAudio();
1268   }
1269 
ExpectNoAudio()1270   void ExpectNoAudio() {
1271     CallerExpectsNoAudio();
1272     CalleeExpectsNoAudio();
1273   }
1274 
ExpectBidirectionalVideo()1275   void ExpectBidirectionalVideo() {
1276     CallerExpectsSomeVideo();
1277     CalleeExpectsSomeVideo();
1278   }
1279 
ExpectNoVideo()1280   void ExpectNoVideo() {
1281     CallerExpectsNoVideo();
1282     CalleeExpectsNoVideo();
1283   }
1284 
CallerExpectsSomeAudioAndVideo()1285   void CallerExpectsSomeAudioAndVideo() {
1286     CallerExpectsSomeAudio();
1287     CallerExpectsSomeVideo();
1288   }
1289 
CalleeExpectsSomeAudioAndVideo()1290   void CalleeExpectsSomeAudioAndVideo() {
1291     CalleeExpectsSomeAudio();
1292     CalleeExpectsSomeVideo();
1293   }
1294 
1295   // Caller's audio functions.
1296   void CallerExpectsSomeAudio(
1297       int expected_audio_frames = kDefaultExpectedAudioFrameCount) {
1298     caller_audio_expectation_ = kExpectSomeFrames;
1299     caller_audio_frames_expected_ = expected_audio_frames;
1300   }
1301 
CallerExpectsNoAudio()1302   void CallerExpectsNoAudio() {
1303     caller_audio_expectation_ = kExpectNoFrames;
1304     caller_audio_frames_expected_ = 0;
1305   }
1306 
1307   // Caller's video functions.
1308   void CallerExpectsSomeVideo(
1309       int expected_video_frames = kDefaultExpectedVideoFrameCount) {
1310     caller_video_expectation_ = kExpectSomeFrames;
1311     caller_video_frames_expected_ = expected_video_frames;
1312   }
1313 
CallerExpectsNoVideo()1314   void CallerExpectsNoVideo() {
1315     caller_video_expectation_ = kExpectNoFrames;
1316     caller_video_frames_expected_ = 0;
1317   }
1318 
1319   // Callee's audio functions.
1320   void CalleeExpectsSomeAudio(
1321       int expected_audio_frames = kDefaultExpectedAudioFrameCount) {
1322     callee_audio_expectation_ = kExpectSomeFrames;
1323     callee_audio_frames_expected_ = expected_audio_frames;
1324   }
1325 
CalleeExpectsNoAudio()1326   void CalleeExpectsNoAudio() {
1327     callee_audio_expectation_ = kExpectNoFrames;
1328     callee_audio_frames_expected_ = 0;
1329   }
1330 
1331   // Callee's video functions.
1332   void CalleeExpectsSomeVideo(
1333       int expected_video_frames = kDefaultExpectedVideoFrameCount) {
1334     callee_video_expectation_ = kExpectSomeFrames;
1335     callee_video_frames_expected_ = expected_video_frames;
1336   }
1337 
CalleeExpectsNoVideo()1338   void CalleeExpectsNoVideo() {
1339     callee_video_expectation_ = kExpectNoFrames;
1340     callee_video_frames_expected_ = 0;
1341   }
1342 
1343   ExpectFrames caller_audio_expectation_ = kNoExpectation;
1344   ExpectFrames caller_video_expectation_ = kNoExpectation;
1345   ExpectFrames callee_audio_expectation_ = kNoExpectation;
1346   ExpectFrames callee_video_expectation_ = kNoExpectation;
1347   int caller_audio_frames_expected_ = 0;
1348   int caller_video_frames_expected_ = 0;
1349   int callee_audio_frames_expected_ = 0;
1350   int callee_video_frames_expected_ = 0;
1351 };
1352 
1353 class MockIceTransport : public webrtc::IceTransportInterface {
1354  public:
MockIceTransport(const std::string & name,int component)1355   MockIceTransport(const std::string& name, int component)
1356       : internal_(std::make_unique<cricket::FakeIceTransport>(
1357             name,
1358             component,
1359             nullptr /* network_thread */)) {}
1360   ~MockIceTransport() = default;
internal()1361   cricket::IceTransportInternal* internal() { return internal_.get(); }
1362 
1363  private:
1364   std::unique_ptr<cricket::FakeIceTransport> internal_;
1365 };
1366 
1367 class MockIceTransportFactory : public IceTransportFactory {
1368  public:
1369   ~MockIceTransportFactory() override = default;
CreateIceTransport(const std::string & transport_name,int component,IceTransportInit init)1370   rtc::scoped_refptr<IceTransportInterface> CreateIceTransport(
1371       const std::string& transport_name,
1372       int component,
1373       IceTransportInit init) {
1374     RecordIceTransportCreated();
1375     return rtc::make_ref_counted<MockIceTransport>(transport_name, component);
1376   }
1377   MOCK_METHOD(void, RecordIceTransportCreated, ());
1378 };
1379 
1380 // Tests two PeerConnections connecting to each other end-to-end, using a
1381 // virtual network, fake A/V capture and fake encoder/decoders. The
1382 // PeerConnections share the threads/socket servers, but use separate versions
1383 // of everything else (including "PeerConnectionFactory"s).
1384 class PeerConnectionIntegrationBaseTest : public ::testing::Test {
1385  public:
1386   PeerConnectionIntegrationBaseTest(
1387       SdpSemantics sdp_semantics,
1388       absl::optional<std::string> field_trials = absl::nullopt)
sdp_semantics_(sdp_semantics)1389       : sdp_semantics_(sdp_semantics),
1390         ss_(new rtc::VirtualSocketServer()),
1391         fss_(new rtc::FirewallSocketServer(ss_.get())),
1392         network_thread_(new rtc::Thread(fss_.get())),
1393         worker_thread_(rtc::Thread::Create()),
1394         // TODO(bugs.webrtc.org/10335): Pass optional ScopedKeyValueConfig.
1395         field_trials_(new test::ScopedKeyValueConfig(
1396             field_trials.has_value() ? *field_trials : "")) {
1397     network_thread_->SetName("PCNetworkThread", this);
1398     worker_thread_->SetName("PCWorkerThread", this);
1399     RTC_CHECK(network_thread_->Start());
1400     RTC_CHECK(worker_thread_->Start());
1401     webrtc::metrics::Reset();
1402   }
1403 
~PeerConnectionIntegrationBaseTest()1404   ~PeerConnectionIntegrationBaseTest() {
1405     // The PeerConnections should be deleted before the TurnCustomizers.
1406     // A TurnPort is created with a raw pointer to a TurnCustomizer. The
1407     // TurnPort has the same lifetime as the PeerConnection, so it's expected
1408     // that the TurnCustomizer outlives the life of the PeerConnection or else
1409     // when Send() is called it will hit a seg fault.
1410     if (caller_) {
1411       caller_->set_signaling_message_receiver(nullptr);
1412       caller_->pc()->Close();
1413       delete SetCallerPcWrapperAndReturnCurrent(nullptr);
1414     }
1415     if (callee_) {
1416       callee_->set_signaling_message_receiver(nullptr);
1417       callee_->pc()->Close();
1418       delete SetCalleePcWrapperAndReturnCurrent(nullptr);
1419     }
1420 
1421     // If turn servers were created for the test they need to be destroyed on
1422     // the network thread.
1423     SendTask(network_thread(), [this] {
1424       turn_servers_.clear();
1425       turn_customizers_.clear();
1426     });
1427   }
1428 
SignalingStateStable()1429   bool SignalingStateStable() {
1430     return caller_->SignalingStateStable() && callee_->SignalingStateStable();
1431   }
1432 
DtlsConnected()1433   bool DtlsConnected() {
1434     // TODO(deadbeef): kIceConnectionConnected currently means both ICE and DTLS
1435     // are connected. This is an important distinction. Once we have separate
1436     // ICE and DTLS state, this check needs to use the DTLS state.
1437     return (callee()->ice_connection_state() ==
1438                 webrtc::PeerConnectionInterface::kIceConnectionConnected ||
1439             callee()->ice_connection_state() ==
1440                 webrtc::PeerConnectionInterface::kIceConnectionCompleted) &&
1441            (caller()->ice_connection_state() ==
1442                 webrtc::PeerConnectionInterface::kIceConnectionConnected ||
1443             caller()->ice_connection_state() ==
1444                 webrtc::PeerConnectionInterface::kIceConnectionCompleted);
1445   }
1446 
1447   // When `event_log_factory` is null, the default implementation of the event
1448   // log factory will be used.
1449   std::unique_ptr<PeerConnectionIntegrationWrapper> CreatePeerConnectionWrapper(
1450       const std::string& debug_name,
1451       const PeerConnectionFactory::Options* options,
1452       const RTCConfiguration* config,
1453       webrtc::PeerConnectionDependencies dependencies,
1454       std::unique_ptr<webrtc::FakeRtcEventLogFactory> event_log_factory,
1455       bool reset_encoder_factory,
1456       bool reset_decoder_factory,
1457       bool create_media_engine = true) {
1458     RTCConfiguration modified_config;
1459     if (config) {
1460       modified_config = *config;
1461     }
1462     modified_config.sdp_semantics = sdp_semantics_;
1463     if (!dependencies.cert_generator) {
1464       dependencies.cert_generator =
1465           std::make_unique<FakeRTCCertificateGenerator>();
1466     }
1467     std::unique_ptr<PeerConnectionIntegrationWrapper> client(
1468         new PeerConnectionIntegrationWrapper(debug_name));
1469 
1470     if (!client->Init(options, &modified_config, std::move(dependencies),
1471                       fss_.get(), network_thread_.get(), worker_thread_.get(),
1472                       std::move(event_log_factory), reset_encoder_factory,
1473                       reset_decoder_factory, create_media_engine)) {
1474       return nullptr;
1475     }
1476     return client;
1477   }
1478 
1479   std::unique_ptr<PeerConnectionIntegrationWrapper>
CreatePeerConnectionWrapperWithFakeRtcEventLog(const std::string & debug_name,const PeerConnectionFactory::Options * options,const RTCConfiguration * config,webrtc::PeerConnectionDependencies dependencies)1480   CreatePeerConnectionWrapperWithFakeRtcEventLog(
1481       const std::string& debug_name,
1482       const PeerConnectionFactory::Options* options,
1483       const RTCConfiguration* config,
1484       webrtc::PeerConnectionDependencies dependencies) {
1485     return CreatePeerConnectionWrapper(
1486         debug_name, options, config, std::move(dependencies),
1487         std::make_unique<webrtc::FakeRtcEventLogFactory>(),
1488         /*reset_encoder_factory=*/false,
1489         /*reset_decoder_factory=*/false);
1490   }
1491 
CreatePeerConnectionWrappers()1492   bool CreatePeerConnectionWrappers() {
1493     return CreatePeerConnectionWrappersWithConfig(
1494         PeerConnectionInterface::RTCConfiguration(),
1495         PeerConnectionInterface::RTCConfiguration());
1496   }
1497 
CreatePeerConnectionWrappersWithSdpSemantics(SdpSemantics caller_semantics,SdpSemantics callee_semantics)1498   bool CreatePeerConnectionWrappersWithSdpSemantics(
1499       SdpSemantics caller_semantics,
1500       SdpSemantics callee_semantics) {
1501     // Can't specify the sdp_semantics in the passed-in configuration since it
1502     // will be overwritten by CreatePeerConnectionWrapper with whatever is
1503     // stored in sdp_semantics_. So get around this by modifying the instance
1504     // variable before calling CreatePeerConnectionWrapper for the caller and
1505     // callee PeerConnections.
1506     SdpSemantics original_semantics = sdp_semantics_;
1507     sdp_semantics_ = caller_semantics;
1508     caller_ = CreatePeerConnectionWrapper(
1509         "Caller", nullptr, nullptr, webrtc::PeerConnectionDependencies(nullptr),
1510         nullptr,
1511         /*reset_encoder_factory=*/false,
1512         /*reset_decoder_factory=*/false);
1513     sdp_semantics_ = callee_semantics;
1514     callee_ = CreatePeerConnectionWrapper(
1515         "Callee", nullptr, nullptr, webrtc::PeerConnectionDependencies(nullptr),
1516         nullptr,
1517         /*reset_encoder_factory=*/false,
1518         /*reset_decoder_factory=*/false);
1519     sdp_semantics_ = original_semantics;
1520     return caller_ && callee_;
1521   }
1522 
CreatePeerConnectionWrappersWithConfig(const PeerConnectionInterface::RTCConfiguration & caller_config,const PeerConnectionInterface::RTCConfiguration & callee_config)1523   bool CreatePeerConnectionWrappersWithConfig(
1524       const PeerConnectionInterface::RTCConfiguration& caller_config,
1525       const PeerConnectionInterface::RTCConfiguration& callee_config) {
1526     caller_ = CreatePeerConnectionWrapper(
1527         "Caller", nullptr, &caller_config,
1528         webrtc::PeerConnectionDependencies(nullptr), nullptr,
1529         /*reset_encoder_factory=*/false,
1530         /*reset_decoder_factory=*/false);
1531     callee_ = CreatePeerConnectionWrapper(
1532         "Callee", nullptr, &callee_config,
1533         webrtc::PeerConnectionDependencies(nullptr), nullptr,
1534         /*reset_encoder_factory=*/false,
1535         /*reset_decoder_factory=*/false);
1536     return caller_ && callee_;
1537   }
1538 
CreatePeerConnectionWrappersWithConfigAndDeps(const PeerConnectionInterface::RTCConfiguration & caller_config,webrtc::PeerConnectionDependencies caller_dependencies,const PeerConnectionInterface::RTCConfiguration & callee_config,webrtc::PeerConnectionDependencies callee_dependencies)1539   bool CreatePeerConnectionWrappersWithConfigAndDeps(
1540       const PeerConnectionInterface::RTCConfiguration& caller_config,
1541       webrtc::PeerConnectionDependencies caller_dependencies,
1542       const PeerConnectionInterface::RTCConfiguration& callee_config,
1543       webrtc::PeerConnectionDependencies callee_dependencies) {
1544     caller_ =
1545         CreatePeerConnectionWrapper("Caller", nullptr, &caller_config,
1546                                     std::move(caller_dependencies), nullptr,
1547                                     /*reset_encoder_factory=*/false,
1548                                     /*reset_decoder_factory=*/false);
1549     callee_ =
1550         CreatePeerConnectionWrapper("Callee", nullptr, &callee_config,
1551                                     std::move(callee_dependencies), nullptr,
1552                                     /*reset_encoder_factory=*/false,
1553                                     /*reset_decoder_factory=*/false);
1554     return caller_ && callee_;
1555   }
1556 
CreatePeerConnectionWrappersWithOptions(const PeerConnectionFactory::Options & caller_options,const PeerConnectionFactory::Options & callee_options)1557   bool CreatePeerConnectionWrappersWithOptions(
1558       const PeerConnectionFactory::Options& caller_options,
1559       const PeerConnectionFactory::Options& callee_options) {
1560     caller_ = CreatePeerConnectionWrapper(
1561         "Caller", &caller_options, nullptr,
1562         webrtc::PeerConnectionDependencies(nullptr), nullptr,
1563         /*reset_encoder_factory=*/false,
1564         /*reset_decoder_factory=*/false);
1565     callee_ = CreatePeerConnectionWrapper(
1566         "Callee", &callee_options, nullptr,
1567         webrtc::PeerConnectionDependencies(nullptr), nullptr,
1568         /*reset_encoder_factory=*/false,
1569         /*reset_decoder_factory=*/false);
1570     return caller_ && callee_;
1571   }
1572 
CreatePeerConnectionWrappersWithFakeRtcEventLog()1573   bool CreatePeerConnectionWrappersWithFakeRtcEventLog() {
1574     PeerConnectionInterface::RTCConfiguration default_config;
1575     caller_ = CreatePeerConnectionWrapperWithFakeRtcEventLog(
1576         "Caller", nullptr, &default_config,
1577         webrtc::PeerConnectionDependencies(nullptr));
1578     callee_ = CreatePeerConnectionWrapperWithFakeRtcEventLog(
1579         "Callee", nullptr, &default_config,
1580         webrtc::PeerConnectionDependencies(nullptr));
1581     return caller_ && callee_;
1582   }
1583 
1584   std::unique_ptr<PeerConnectionIntegrationWrapper>
CreatePeerConnectionWrapperWithAlternateKey()1585   CreatePeerConnectionWrapperWithAlternateKey() {
1586     std::unique_ptr<FakeRTCCertificateGenerator> cert_generator(
1587         new FakeRTCCertificateGenerator());
1588     cert_generator->use_alternate_key();
1589 
1590     webrtc::PeerConnectionDependencies dependencies(nullptr);
1591     dependencies.cert_generator = std::move(cert_generator);
1592     return CreatePeerConnectionWrapper("New Peer", nullptr, nullptr,
1593                                        std::move(dependencies), nullptr,
1594                                        /*reset_encoder_factory=*/false,
1595                                        /*reset_decoder_factory=*/false);
1596   }
1597 
CreateOneDirectionalPeerConnectionWrappers(bool caller_to_callee)1598   bool CreateOneDirectionalPeerConnectionWrappers(bool caller_to_callee) {
1599     caller_ = CreatePeerConnectionWrapper(
1600         "Caller", nullptr, nullptr, webrtc::PeerConnectionDependencies(nullptr),
1601         nullptr,
1602         /*reset_encoder_factory=*/!caller_to_callee,
1603         /*reset_decoder_factory=*/caller_to_callee);
1604     callee_ = CreatePeerConnectionWrapper(
1605         "Callee", nullptr, nullptr, webrtc::PeerConnectionDependencies(nullptr),
1606         nullptr,
1607         /*reset_encoder_factory=*/caller_to_callee,
1608         /*reset_decoder_factory=*/!caller_to_callee);
1609     return caller_ && callee_;
1610   }
1611 
CreatePeerConnectionWrappersWithoutMediaEngine()1612   bool CreatePeerConnectionWrappersWithoutMediaEngine() {
1613     caller_ = CreatePeerConnectionWrapper(
1614         "Caller", nullptr, nullptr, webrtc::PeerConnectionDependencies(nullptr),
1615         nullptr,
1616         /*reset_encoder_factory=*/false,
1617         /*reset_decoder_factory=*/false,
1618         /*create_media_engine=*/false);
1619     callee_ = CreatePeerConnectionWrapper(
1620         "Callee", nullptr, nullptr, webrtc::PeerConnectionDependencies(nullptr),
1621         nullptr,
1622         /*reset_encoder_factory=*/false,
1623         /*reset_decoder_factory=*/false,
1624         /*create_media_engine=*/false);
1625     return caller_ && callee_;
1626   }
1627 
1628   cricket::TestTurnServer* CreateTurnServer(
1629       rtc::SocketAddress internal_address,
1630       rtc::SocketAddress external_address,
1631       cricket::ProtocolType type = cricket::ProtocolType::PROTO_UDP,
1632       const std::string& common_name = "test turn server") {
1633     rtc::Thread* thread = network_thread();
1634     rtc::SocketFactory* socket_factory = fss_.get();
1635     std::unique_ptr<cricket::TestTurnServer> turn_server;
1636     SendTask(network_thread(), [&] {
1637       turn_server = std::make_unique<cricket::TestTurnServer>(
1638           thread, socket_factory, internal_address, external_address, type,
1639           /*ignore_bad_certs=*/true, common_name);
1640     });
1641     turn_servers_.push_back(std::move(turn_server));
1642     // Interactions with the turn server should be done on the network thread.
1643     return turn_servers_.back().get();
1644   }
1645 
CreateTurnCustomizer()1646   cricket::TestTurnCustomizer* CreateTurnCustomizer() {
1647     std::unique_ptr<cricket::TestTurnCustomizer> turn_customizer;
1648     SendTask(network_thread(), [&] {
1649       turn_customizer = std::make_unique<cricket::TestTurnCustomizer>();
1650     });
1651     turn_customizers_.push_back(std::move(turn_customizer));
1652     // Interactions with the turn customizer should be done on the network
1653     // thread.
1654     return turn_customizers_.back().get();
1655   }
1656 
1657   // Checks that the function counters for a TestTurnCustomizer are greater than
1658   // 0.
ExpectTurnCustomizerCountersIncremented(cricket::TestTurnCustomizer * turn_customizer)1659   void ExpectTurnCustomizerCountersIncremented(
1660       cricket::TestTurnCustomizer* turn_customizer) {
1661     SendTask(network_thread(), [turn_customizer] {
1662       EXPECT_GT(turn_customizer->allow_channel_data_cnt_, 0u);
1663       EXPECT_GT(turn_customizer->modify_cnt_, 0u);
1664     });
1665   }
1666 
1667   // Once called, SDP blobs and ICE candidates will be automatically signaled
1668   // between PeerConnections.
ConnectFakeSignaling()1669   void ConnectFakeSignaling() {
1670     caller_->set_signaling_message_receiver(callee_.get());
1671     callee_->set_signaling_message_receiver(caller_.get());
1672   }
1673 
1674   // Once called, SDP blobs will be automatically signaled between
1675   // PeerConnections. Note that ICE candidates will not be signaled unless they
1676   // are in the exchanged SDP blobs.
ConnectFakeSignalingForSdpOnly()1677   void ConnectFakeSignalingForSdpOnly() {
1678     ConnectFakeSignaling();
1679     SetSignalIceCandidates(false);
1680   }
1681 
SetSignalingDelayMs(int delay_ms)1682   void SetSignalingDelayMs(int delay_ms) {
1683     caller_->set_signaling_delay_ms(delay_ms);
1684     callee_->set_signaling_delay_ms(delay_ms);
1685   }
1686 
SetSignalIceCandidates(bool signal)1687   void SetSignalIceCandidates(bool signal) {
1688     caller_->set_signal_ice_candidates(signal);
1689     callee_->set_signal_ice_candidates(signal);
1690   }
1691 
1692   // Messages may get lost on the unreliable DataChannel, so we send multiple
1693   // times to avoid test flakiness.
SendRtpDataWithRetries(webrtc::DataChannelInterface * dc,const std::string & data,int retries)1694   void SendRtpDataWithRetries(webrtc::DataChannelInterface* dc,
1695                               const std::string& data,
1696                               int retries) {
1697     for (int i = 0; i < retries; ++i) {
1698       dc->Send(DataBuffer(data));
1699     }
1700   }
1701 
network_thread()1702   rtc::Thread* network_thread() { return network_thread_.get(); }
1703 
virtual_socket_server()1704   rtc::VirtualSocketServer* virtual_socket_server() { return ss_.get(); }
1705 
caller()1706   PeerConnectionIntegrationWrapper* caller() { return caller_.get(); }
1707 
1708   // Destroy peerconnections.
1709   // This can be used to ensure that all pointers to on-stack mocks
1710   // get dropped before exit.
DestroyPeerConnections()1711   void DestroyPeerConnections() {
1712     if (caller_) {
1713       caller_->pc()->Close();
1714     }
1715     if (callee_) {
1716       callee_->pc()->Close();
1717     }
1718     caller_.reset();
1719     callee_.reset();
1720   }
1721 
1722   // Set the `caller_` to the `wrapper` passed in and return the
1723   // original `caller_`.
SetCallerPcWrapperAndReturnCurrent(PeerConnectionIntegrationWrapper * wrapper)1724   PeerConnectionIntegrationWrapper* SetCallerPcWrapperAndReturnCurrent(
1725       PeerConnectionIntegrationWrapper* wrapper) {
1726     PeerConnectionIntegrationWrapper* old = caller_.release();
1727     caller_.reset(wrapper);
1728     return old;
1729   }
1730 
callee()1731   PeerConnectionIntegrationWrapper* callee() { return callee_.get(); }
1732 
1733   // Set the `callee_` to the `wrapper` passed in and return the
1734   // original `callee_`.
SetCalleePcWrapperAndReturnCurrent(PeerConnectionIntegrationWrapper * wrapper)1735   PeerConnectionIntegrationWrapper* SetCalleePcWrapperAndReturnCurrent(
1736       PeerConnectionIntegrationWrapper* wrapper) {
1737     PeerConnectionIntegrationWrapper* old = callee_.release();
1738     callee_.reset(wrapper);
1739     return old;
1740   }
1741 
SetPortAllocatorFlags(uint32_t caller_flags,uint32_t callee_flags)1742   void SetPortAllocatorFlags(uint32_t caller_flags, uint32_t callee_flags) {
1743     SendTask(network_thread(), [this, caller_flags] {
1744       caller()->port_allocator()->set_flags(caller_flags);
1745     });
1746     SendTask(network_thread(), [this, callee_flags] {
1747       callee()->port_allocator()->set_flags(callee_flags);
1748     });
1749   }
1750 
firewall()1751   rtc::FirewallSocketServer* firewall() const { return fss_.get(); }
1752 
1753   // Expects the provided number of new frames to be received within
1754   // kMaxWaitForFramesMs. The new expected frames are specified in
1755   // `media_expectations`. Returns false if any of the expectations were
1756   // not met.
ExpectNewFrames(const MediaExpectations & media_expectations)1757   bool ExpectNewFrames(const MediaExpectations& media_expectations) {
1758     // Make sure there are no bogus tracks confusing the issue.
1759     caller()->RemoveUnusedVideoRenderers();
1760     callee()->RemoveUnusedVideoRenderers();
1761     // First initialize the expected frame counts based upon the current
1762     // frame count.
1763     int total_caller_audio_frames_expected = caller()->audio_frames_received();
1764     if (media_expectations.caller_audio_expectation_ ==
1765         MediaExpectations::kExpectSomeFrames) {
1766       total_caller_audio_frames_expected +=
1767           media_expectations.caller_audio_frames_expected_;
1768     }
1769     int total_caller_video_frames_expected =
1770         caller()->min_video_frames_received_per_track();
1771     if (media_expectations.caller_video_expectation_ ==
1772         MediaExpectations::kExpectSomeFrames) {
1773       total_caller_video_frames_expected +=
1774           media_expectations.caller_video_frames_expected_;
1775     }
1776     int total_callee_audio_frames_expected = callee()->audio_frames_received();
1777     if (media_expectations.callee_audio_expectation_ ==
1778         MediaExpectations::kExpectSomeFrames) {
1779       total_callee_audio_frames_expected +=
1780           media_expectations.callee_audio_frames_expected_;
1781     }
1782     int total_callee_video_frames_expected =
1783         callee()->min_video_frames_received_per_track();
1784     if (media_expectations.callee_video_expectation_ ==
1785         MediaExpectations::kExpectSomeFrames) {
1786       total_callee_video_frames_expected +=
1787           media_expectations.callee_video_frames_expected_;
1788     }
1789 
1790     // Wait for the expected frames.
1791     EXPECT_TRUE_WAIT(caller()->audio_frames_received() >=
1792                              total_caller_audio_frames_expected &&
1793                          caller()->min_video_frames_received_per_track() >=
1794                              total_caller_video_frames_expected &&
1795                          callee()->audio_frames_received() >=
1796                              total_callee_audio_frames_expected &&
1797                          callee()->min_video_frames_received_per_track() >=
1798                              total_callee_video_frames_expected,
1799                      kMaxWaitForFramesMs);
1800     bool expectations_correct =
1801         caller()->audio_frames_received() >=
1802             total_caller_audio_frames_expected &&
1803         caller()->min_video_frames_received_per_track() >=
1804             total_caller_video_frames_expected &&
1805         callee()->audio_frames_received() >=
1806             total_callee_audio_frames_expected &&
1807         callee()->min_video_frames_received_per_track() >=
1808             total_callee_video_frames_expected;
1809 
1810     // After the combined wait, print out a more detailed message upon
1811     // failure.
1812     EXPECT_GE(caller()->audio_frames_received(),
1813               total_caller_audio_frames_expected);
1814     EXPECT_GE(caller()->min_video_frames_received_per_track(),
1815               total_caller_video_frames_expected);
1816     EXPECT_GE(callee()->audio_frames_received(),
1817               total_callee_audio_frames_expected);
1818     EXPECT_GE(callee()->min_video_frames_received_per_track(),
1819               total_callee_video_frames_expected);
1820 
1821     // We want to make sure nothing unexpected was received.
1822     if (media_expectations.caller_audio_expectation_ ==
1823         MediaExpectations::kExpectNoFrames) {
1824       EXPECT_EQ(caller()->audio_frames_received(),
1825                 total_caller_audio_frames_expected);
1826       if (caller()->audio_frames_received() !=
1827           total_caller_audio_frames_expected) {
1828         expectations_correct = false;
1829       }
1830     }
1831     if (media_expectations.caller_video_expectation_ ==
1832         MediaExpectations::kExpectNoFrames) {
1833       EXPECT_EQ(caller()->min_video_frames_received_per_track(),
1834                 total_caller_video_frames_expected);
1835       if (caller()->min_video_frames_received_per_track() !=
1836           total_caller_video_frames_expected) {
1837         expectations_correct = false;
1838       }
1839     }
1840     if (media_expectations.callee_audio_expectation_ ==
1841         MediaExpectations::kExpectNoFrames) {
1842       EXPECT_EQ(callee()->audio_frames_received(),
1843                 total_callee_audio_frames_expected);
1844       if (callee()->audio_frames_received() !=
1845           total_callee_audio_frames_expected) {
1846         expectations_correct = false;
1847       }
1848     }
1849     if (media_expectations.callee_video_expectation_ ==
1850         MediaExpectations::kExpectNoFrames) {
1851       EXPECT_EQ(callee()->min_video_frames_received_per_track(),
1852                 total_callee_video_frames_expected);
1853       if (callee()->min_video_frames_received_per_track() !=
1854           total_callee_video_frames_expected) {
1855         expectations_correct = false;
1856       }
1857     }
1858     return expectations_correct;
1859   }
1860 
ClosePeerConnections()1861   void ClosePeerConnections() {
1862     if (caller())
1863       caller()->pc()->Close();
1864     if (callee())
1865       callee()->pc()->Close();
1866   }
1867 
TestNegotiatedCipherSuite(const PeerConnectionFactory::Options & caller_options,const PeerConnectionFactory::Options & callee_options,int expected_cipher_suite)1868   void TestNegotiatedCipherSuite(
1869       const PeerConnectionFactory::Options& caller_options,
1870       const PeerConnectionFactory::Options& callee_options,
1871       int expected_cipher_suite) {
1872     ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(caller_options,
1873                                                         callee_options));
1874     ConnectFakeSignaling();
1875     caller()->AddAudioVideoTracks();
1876     callee()->AddAudioVideoTracks();
1877     caller()->CreateAndSetAndSignalOffer();
1878     ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
1879     EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(expected_cipher_suite),
1880                    caller()->OldGetStats()->SrtpCipher(), kDefaultTimeout);
1881     // TODO(bugs.webrtc.org/9456): Fix it.
1882     EXPECT_METRIC_EQ(1, webrtc::metrics::NumEvents(
1883                             "WebRTC.PeerConnection.SrtpCryptoSuite.Audio",
1884                             expected_cipher_suite));
1885   }
1886 
TestGcmNegotiationUsesCipherSuite(bool local_gcm_enabled,bool remote_gcm_enabled,bool aes_ctr_enabled,int expected_cipher_suite)1887   void TestGcmNegotiationUsesCipherSuite(bool local_gcm_enabled,
1888                                          bool remote_gcm_enabled,
1889                                          bool aes_ctr_enabled,
1890                                          int expected_cipher_suite) {
1891     PeerConnectionFactory::Options caller_options;
1892     caller_options.crypto_options.srtp.enable_gcm_crypto_suites =
1893         local_gcm_enabled;
1894     caller_options.crypto_options.srtp.enable_aes128_sha1_80_crypto_cipher =
1895         aes_ctr_enabled;
1896     PeerConnectionFactory::Options callee_options;
1897     callee_options.crypto_options.srtp.enable_gcm_crypto_suites =
1898         remote_gcm_enabled;
1899     callee_options.crypto_options.srtp.enable_aes128_sha1_80_crypto_cipher =
1900         aes_ctr_enabled;
1901     TestNegotiatedCipherSuite(caller_options, callee_options,
1902                               expected_cipher_suite);
1903   }
1904 
trials()1905   const FieldTrialsView& trials() const { return *field_trials_.get(); }
1906 
1907  protected:
1908   SdpSemantics sdp_semantics_;
1909 
1910  private:
1911   rtc::AutoThread main_thread_;  // Used as the signal thread by most tests.
1912   // `ss_` is used by `network_thread_` so it must be destroyed later.
1913   std::unique_ptr<rtc::VirtualSocketServer> ss_;
1914   std::unique_ptr<rtc::FirewallSocketServer> fss_;
1915   // `network_thread_` and `worker_thread_` are used by both
1916   // `caller_` and `callee_` so they must be destroyed
1917   // later.
1918   std::unique_ptr<rtc::Thread> network_thread_;
1919   std::unique_ptr<rtc::Thread> worker_thread_;
1920   // The turn servers and turn customizers should be accessed & deleted on the
1921   // network thread to avoid a race with the socket read/write that occurs
1922   // on the network thread.
1923   std::vector<std::unique_ptr<cricket::TestTurnServer>> turn_servers_;
1924   std::vector<std::unique_ptr<cricket::TestTurnCustomizer>> turn_customizers_;
1925   std::unique_ptr<PeerConnectionIntegrationWrapper> caller_;
1926   std::unique_ptr<PeerConnectionIntegrationWrapper> callee_;
1927   std::unique_ptr<FieldTrialsView> field_trials_;
1928 };
1929 
1930 }  // namespace webrtc
1931 
1932 #endif  // PC_TEST_INTEGRATION_TEST_HELPERS_H_
1933