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