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