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