blob: 745d1f5dcbb31dbd9b702da4dab9588813cfc060 [file] [log] [blame]
deadbeef1dcb1642017-03-29 21:08:16 -07001/*
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
Harald Alvestrand89c40e22021-02-17 08:58:35 +000011#include <stdio.h>
deadbeef1dcb1642017-03-29 21:08:16 -070012
Taylor Brandstetterd3ef4992020-10-15 18:22:57 -070013#include <algorithm>
Harald Alvestrand89c40e22021-02-17 08:58:35 +000014#include <functional>
15#include <list>
16#include <map>
deadbeef1dcb1642017-03-29 21:08:16 -070017#include <memory>
18#include <utility>
19#include <vector>
20
Steve Anton64b626b2019-01-28 17:25:26 -080021#include "absl/algorithm/container.h"
Steve Anton10542f22019-01-11 09:11:00 -080022#include "api/media_stream_interface.h"
23#include "api/peer_connection_interface.h"
Harald Alvestrand89c40e22021-02-17 08:58:35 +000024#include "api/peer_connection_proxy.h"
25#include "api/rtc_event_log/rtc_event_log_factory.h"
Steve Anton10542f22019-01-11 09:11:00 -080026#include "api/rtp_receiver_interface.h"
Harald Alvestrand89c40e22021-02-17 08:58:35 +000027#include "api/task_queue/default_task_queue_factory.h"
28#include "api/transport/field_trial_based_config.h"
Steve Anton10542f22019-01-11 09:11:00 -080029#include "api/uma_metrics.h"
Harald Alvestrand89c40e22021-02-17 08:58:35 +000030#include "api/video_codecs/sdp_video_format.h"
31#include "call/call.h"
Qingsi Wang7685e862018-06-11 20:15:46 -070032#include "logging/rtc_event_log/fake_rtc_event_log_factory.h"
Harald Alvestrand89c40e22021-02-17 08:58:35 +000033#include "media/engine/fake_webrtc_video_engine.h"
34#include "media/engine/webrtc_media_engine.h"
35#include "media/engine/webrtc_media_engine_defaults.h"
36#include "modules/audio_processing/test/audio_processing_builder_for_testing.h"
37#include "p2p/base/fake_ice_transport.h"
Steve Anton10542f22019-01-11 09:11:00 -080038#include "p2p/base/mock_async_resolver.h"
Harald Alvestrand89c40e22021-02-17 08:58:35 +000039#include "p2p/base/p2p_constants.h"
Steve Anton10542f22019-01-11 09:11:00 -080040#include "p2p/base/port_interface.h"
41#include "p2p/base/test_stun_server.h"
42#include "p2p/base/test_turn_customizer.h"
43#include "p2p/base/test_turn_server.h"
Harald Alvestrand89c40e22021-02-17 08:58:35 +000044#include "p2p/client/basic_port_allocator.h"
45#include "pc/dtmf_sender.h"
46#include "pc/local_audio_source.h"
Steve Anton10542f22019-01-11 09:11:00 -080047#include "pc/media_session.h"
48#include "pc/peer_connection.h"
49#include "pc/peer_connection_factory.h"
Harald Alvestrand89c40e22021-02-17 08:58:35 +000050#include "pc/rtp_media_utils.h"
Steve Anton10542f22019-01-11 09:11:00 -080051#include "pc/session_description.h"
Harald Alvestrand89c40e22021-02-17 08:58:35 +000052#include "pc/test/fake_audio_capture_module.h"
53#include "pc/test/fake_periodic_video_track_source.h"
54#include "pc/test/fake_rtc_certificate_generator.h"
55#include "pc/test/fake_video_track_renderer.h"
Steve Anton10542f22019-01-11 09:11:00 -080056#include "pc/test/mock_peer_connection_observers.h"
Jonas Olssonb75d9e92019-02-22 10:33:29 +010057#include "rtc_base/fake_clock.h"
Qingsi Wangecd30542019-05-22 14:34:56 -070058#include "rtc_base/fake_mdns_responder.h"
Steve Anton10542f22019-01-11 09:11:00 -080059#include "rtc_base/fake_network.h"
60#include "rtc_base/firewall_socket_server.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020061#include "rtc_base/gunit.h"
Harald Alvestrand89c40e22021-02-17 08:58:35 +000062#include "rtc_base/numerics/safe_conversions.h"
Steve Anton10542f22019-01-11 09:11:00 -080063#include "rtc_base/test_certificate_verifier.h"
64#include "rtc_base/time_utils.h"
65#include "rtc_base/virtual_socket_server.h"
Mirko Bonadei17f48782018-09-28 08:51:10 +020066#include "system_wrappers/include/metrics.h"
Harald Alvestrand89c40e22021-02-17 08:58:35 +000067#include "test/field_trial.h"
68#include "test/gmock.h"
deadbeef1dcb1642017-03-29 21:08:16 -070069
Mirko Bonadeiab64e8a2018-12-12 12:10:18 +010070namespace webrtc {
71namespace {
72
Harald Alvestrand89c40e22021-02-17 08:58:35 +000073using ::cricket::ContentInfo;
74using ::cricket::StreamParams;
75using ::rtc::SocketAddress;
76using ::testing::_;
77using ::testing::Combine;
78using ::testing::Contains;
79using ::testing::DoAll;
80using ::testing::ElementsAre;
81using ::testing::NiceMock;
82using ::testing::Return;
83using ::testing::SetArgPointee;
84using ::testing::UnorderedElementsAreArray;
85using ::testing::Values;
86using RTCConfiguration = PeerConnectionInterface::RTCConfiguration;
87
88static const int kDefaultTimeout = 10000;
89static const int kMaxWaitForStatsMs = 3000;
90static const int kMaxWaitForActivationMs = 5000;
91static const int kMaxWaitForFramesMs = 10000;
92// Default number of audio/video frames to wait for before considering a test
93// successful.
94static const int kDefaultExpectedAudioFrameCount = 3;
95static const int kDefaultExpectedVideoFrameCount = 3;
96
97static const char kDataChannelLabel[] = "data_channel";
98
99// SRTP cipher name negotiated by the tests. This must be updated if the
100// default changes.
101static const int kDefaultSrtpCryptoSuite = rtc::SRTP_AES128_CM_SHA1_80;
102static const int kDefaultSrtpCryptoSuiteGcm = rtc::SRTP_AEAD_AES_256_GCM;
103
104static const SocketAddress kDefaultLocalAddress("192.168.1.1", 0);
105
106// Helper function for constructing offer/answer options to initiate an ICE
107// restart.
108PeerConnectionInterface::RTCOfferAnswerOptions IceRestartOfferAnswerOptions() {
109 PeerConnectionInterface::RTCOfferAnswerOptions options;
110 options.ice_restart = true;
111 return options;
112}
113
114// Remove all stream information (SSRCs, track IDs, etc.) and "msid-semantic"
115// attribute from received SDP, simulating a legacy endpoint.
116void RemoveSsrcsAndMsids(cricket::SessionDescription* desc) {
117 for (ContentInfo& content : desc->contents()) {
118 content.media_description()->mutable_streams().clear();
119 }
120 desc->set_msid_supported(false);
121 desc->set_msid_signaling(0);
122}
123
124// Removes all stream information besides the stream ids, simulating an
125// endpoint that only signals a=msid lines to convey stream_ids.
126void RemoveSsrcsAndKeepMsids(cricket::SessionDescription* desc) {
127 for (ContentInfo& content : desc->contents()) {
128 std::string track_id;
129 std::vector<std::string> stream_ids;
130 if (!content.media_description()->streams().empty()) {
131 const StreamParams& first_stream =
132 content.media_description()->streams()[0];
133 track_id = first_stream.id;
134 stream_ids = first_stream.stream_ids();
135 }
136 content.media_description()->mutable_streams().clear();
137 StreamParams new_stream;
138 new_stream.id = track_id;
139 new_stream.set_stream_ids(stream_ids);
140 content.media_description()->AddStream(new_stream);
141 }
142}
143
144int FindFirstMediaStatsIndexByKind(
145 const std::string& kind,
146 const std::vector<const webrtc::RTCMediaStreamTrackStats*>&
147 media_stats_vec) {
148 for (size_t i = 0; i < media_stats_vec.size(); i++) {
149 if (media_stats_vec[i]->kind.ValueToString() == kind) {
150 return i;
151 }
152 }
153 return -1;
154}
155
156class SignalingMessageReceiver {
157 public:
158 virtual void ReceiveSdpMessage(SdpType type, const std::string& msg) = 0;
159 virtual void ReceiveIceMessage(const std::string& sdp_mid,
160 int sdp_mline_index,
161 const std::string& msg) = 0;
162
163 protected:
164 SignalingMessageReceiver() {}
165 virtual ~SignalingMessageReceiver() {}
166};
167
168class MockRtpReceiverObserver : public webrtc::RtpReceiverObserverInterface {
169 public:
170 explicit MockRtpReceiverObserver(cricket::MediaType media_type)
171 : expected_media_type_(media_type) {}
172
173 void OnFirstPacketReceived(cricket::MediaType media_type) override {
174 ASSERT_EQ(expected_media_type_, media_type);
175 first_packet_received_ = true;
176 }
177
178 bool first_packet_received() const { return first_packet_received_; }
179
180 virtual ~MockRtpReceiverObserver() {}
181
182 private:
183 bool first_packet_received_ = false;
184 cricket::MediaType expected_media_type_;
185};
186
187// Helper class that wraps a peer connection, observes it, and can accept
188// signaling messages from another wrapper.
189//
190// Uses a fake network, fake A/V capture, and optionally fake
191// encoders/decoders, though they aren't used by default since they don't
192// advertise support of any codecs.
193// TODO(steveanton): See how this could become a subclass of
194// PeerConnectionWrapper defined in peerconnectionwrapper.h.
195class PeerConnectionWrapper : public webrtc::PeerConnectionObserver,
196 public SignalingMessageReceiver {
197 public:
198 // Different factory methods for convenience.
199 // TODO(deadbeef): Could use the pattern of:
200 //
201 // PeerConnectionWrapper =
202 // WrapperBuilder.WithConfig(...).WithOptions(...).build();
203 //
204 // To reduce some code duplication.
205 static PeerConnectionWrapper* CreateWithDtlsIdentityStore(
206 const std::string& debug_name,
207 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator,
208 rtc::Thread* network_thread,
209 rtc::Thread* worker_thread) {
210 PeerConnectionWrapper* client(new PeerConnectionWrapper(debug_name));
211 webrtc::PeerConnectionDependencies dependencies(nullptr);
212 dependencies.cert_generator = std::move(cert_generator);
213 if (!client->Init(nullptr, nullptr, std::move(dependencies), network_thread,
214 worker_thread, nullptr,
215 /*reset_encoder_factory=*/false,
216 /*reset_decoder_factory=*/false)) {
217 delete client;
218 return nullptr;
219 }
220 return client;
221 }
222
223 webrtc::PeerConnectionFactoryInterface* pc_factory() const {
224 return peer_connection_factory_.get();
225 }
226
227 webrtc::PeerConnectionInterface* pc() const { return peer_connection_.get(); }
228
229 // If a signaling message receiver is set (via ConnectFakeSignaling), this
230 // will set the whole offer/answer exchange in motion. Just need to wait for
231 // the signaling state to reach "stable".
232 void CreateAndSetAndSignalOffer() {
233 auto offer = CreateOfferAndWait();
234 ASSERT_NE(nullptr, offer);
235 EXPECT_TRUE(SetLocalDescriptionAndSendSdpMessage(std::move(offer)));
236 }
237
238 // Sets the options to be used when CreateAndSetAndSignalOffer is called, or
239 // when a remote offer is received (via fake signaling) and an answer is
240 // generated. By default, uses default options.
241 void SetOfferAnswerOptions(
242 const PeerConnectionInterface::RTCOfferAnswerOptions& options) {
243 offer_answer_options_ = options;
244 }
245
246 // Set a callback to be invoked when SDP is received via the fake signaling
247 // channel, which provides an opportunity to munge (modify) the SDP. This is
248 // used to test SDP being applied that a PeerConnection would normally not
249 // generate, but a non-JSEP endpoint might.
250 void SetReceivedSdpMunger(
251 std::function<void(cricket::SessionDescription*)> munger) {
252 received_sdp_munger_ = std::move(munger);
253 }
254
255 // Similar to the above, but this is run on SDP immediately after it's
256 // generated.
257 void SetGeneratedSdpMunger(
258 std::function<void(cricket::SessionDescription*)> munger) {
259 generated_sdp_munger_ = std::move(munger);
260 }
261
262 // Set a callback to be invoked when a remote offer is received via the fake
263 // signaling channel. This provides an opportunity to change the
264 // PeerConnection state before an answer is created and sent to the caller.
265 void SetRemoteOfferHandler(std::function<void()> handler) {
266 remote_offer_handler_ = std::move(handler);
267 }
268
269 void SetRemoteAsyncResolver(rtc::MockAsyncResolver* resolver) {
270 remote_async_resolver_ = resolver;
271 }
272
273 // Every ICE connection state in order that has been seen by the observer.
274 std::vector<PeerConnectionInterface::IceConnectionState>
275 ice_connection_state_history() const {
276 return ice_connection_state_history_;
277 }
278 void clear_ice_connection_state_history() {
279 ice_connection_state_history_.clear();
280 }
281
282 // Every standardized ICE connection state in order that has been seen by the
283 // observer.
284 std::vector<PeerConnectionInterface::IceConnectionState>
285 standardized_ice_connection_state_history() const {
286 return standardized_ice_connection_state_history_;
287 }
288
289 // Every PeerConnection state in order that has been seen by the observer.
290 std::vector<PeerConnectionInterface::PeerConnectionState>
291 peer_connection_state_history() const {
292 return peer_connection_state_history_;
293 }
294
295 // Every ICE gathering state in order that has been seen by the observer.
296 std::vector<PeerConnectionInterface::IceGatheringState>
297 ice_gathering_state_history() const {
298 return ice_gathering_state_history_;
299 }
300 std::vector<cricket::CandidatePairChangeEvent>
301 ice_candidate_pair_change_history() const {
302 return ice_candidate_pair_change_history_;
303 }
304
305 // Every PeerConnection signaling state in order that has been seen by the
306 // observer.
307 std::vector<PeerConnectionInterface::SignalingState>
308 peer_connection_signaling_state_history() const {
309 return peer_connection_signaling_state_history_;
310 }
311
312 void AddAudioVideoTracks() {
313 AddAudioTrack();
314 AddVideoTrack();
315 }
316
317 rtc::scoped_refptr<RtpSenderInterface> AddAudioTrack() {
318 return AddTrack(CreateLocalAudioTrack());
319 }
320
321 rtc::scoped_refptr<RtpSenderInterface> AddVideoTrack() {
322 return AddTrack(CreateLocalVideoTrack());
323 }
324
325 rtc::scoped_refptr<webrtc::AudioTrackInterface> CreateLocalAudioTrack() {
326 cricket::AudioOptions options;
327 // Disable highpass filter so that we can get all the test audio frames.
328 options.highpass_filter = false;
329 rtc::scoped_refptr<webrtc::AudioSourceInterface> source =
330 peer_connection_factory_->CreateAudioSource(options);
331 // TODO(perkj): Test audio source when it is implemented. Currently audio
332 // always use the default input.
333 return peer_connection_factory_->CreateAudioTrack(rtc::CreateRandomUuid(),
334 source);
335 }
336
337 rtc::scoped_refptr<webrtc::VideoTrackInterface> CreateLocalVideoTrack() {
338 webrtc::FakePeriodicVideoSource::Config config;
339 config.timestamp_offset_ms = rtc::TimeMillis();
340 return CreateLocalVideoTrackInternal(config);
341 }
342
343 rtc::scoped_refptr<webrtc::VideoTrackInterface>
344 CreateLocalVideoTrackWithConfig(
345 webrtc::FakePeriodicVideoSource::Config config) {
346 return CreateLocalVideoTrackInternal(config);
347 }
348
349 rtc::scoped_refptr<webrtc::VideoTrackInterface>
350 CreateLocalVideoTrackWithRotation(webrtc::VideoRotation rotation) {
351 webrtc::FakePeriodicVideoSource::Config config;
352 config.rotation = rotation;
353 config.timestamp_offset_ms = rtc::TimeMillis();
354 return CreateLocalVideoTrackInternal(config);
355 }
356
357 rtc::scoped_refptr<RtpSenderInterface> AddTrack(
358 rtc::scoped_refptr<MediaStreamTrackInterface> track,
359 const std::vector<std::string>& stream_ids = {}) {
360 auto result = pc()->AddTrack(track, stream_ids);
361 EXPECT_EQ(RTCErrorType::NONE, result.error().type());
362 return result.MoveValue();
363 }
364
365 std::vector<rtc::scoped_refptr<RtpReceiverInterface>> GetReceiversOfType(
366 cricket::MediaType media_type) {
367 std::vector<rtc::scoped_refptr<RtpReceiverInterface>> receivers;
368 for (const auto& receiver : pc()->GetReceivers()) {
369 if (receiver->media_type() == media_type) {
370 receivers.push_back(receiver);
371 }
372 }
373 return receivers;
374 }
375
376 rtc::scoped_refptr<RtpTransceiverInterface> GetFirstTransceiverOfType(
377 cricket::MediaType media_type) {
378 for (auto transceiver : pc()->GetTransceivers()) {
379 if (transceiver->receiver()->media_type() == media_type) {
380 return transceiver;
381 }
382 }
383 return nullptr;
384 }
385
386 bool SignalingStateStable() {
387 return pc()->signaling_state() == webrtc::PeerConnectionInterface::kStable;
388 }
389
390 void CreateDataChannel() { CreateDataChannel(nullptr); }
391
392 void CreateDataChannel(const webrtc::DataChannelInit* init) {
393 CreateDataChannel(kDataChannelLabel, init);
394 }
395
396 void CreateDataChannel(const std::string& label,
397 const webrtc::DataChannelInit* init) {
398 data_channel_ = pc()->CreateDataChannel(label, init);
399 ASSERT_TRUE(data_channel_.get() != nullptr);
400 data_observer_.reset(new MockDataChannelObserver(data_channel_));
401 }
402
403 DataChannelInterface* data_channel() { return data_channel_; }
404 const MockDataChannelObserver* data_observer() const {
405 return data_observer_.get();
406 }
407
408 int audio_frames_received() const {
409 return fake_audio_capture_module_->frames_received();
410 }
411
412 // Takes minimum of video frames received for each track.
413 //
414 // Can be used like:
415 // EXPECT_GE(expected_frames, min_video_frames_received_per_track());
416 //
417 // To ensure that all video tracks received at least a certain number of
418 // frames.
419 int min_video_frames_received_per_track() const {
420 int min_frames = INT_MAX;
421 if (fake_video_renderers_.empty()) {
422 return 0;
423 }
424
425 for (const auto& pair : fake_video_renderers_) {
426 min_frames = std::min(min_frames, pair.second->num_rendered_frames());
427 }
428 return min_frames;
429 }
430
431 // Returns a MockStatsObserver in a state after stats gathering finished,
432 // which can be used to access the gathered stats.
433 rtc::scoped_refptr<MockStatsObserver> OldGetStatsForTrack(
434 webrtc::MediaStreamTrackInterface* track) {
435 rtc::scoped_refptr<MockStatsObserver> observer(
436 new rtc::RefCountedObject<MockStatsObserver>());
437 EXPECT_TRUE(peer_connection_->GetStats(
438 observer, nullptr, PeerConnectionInterface::kStatsOutputLevelStandard));
439 EXPECT_TRUE_WAIT(observer->called(), kDefaultTimeout);
440 return observer;
441 }
442
443 // Version that doesn't take a track "filter", and gathers all stats.
444 rtc::scoped_refptr<MockStatsObserver> OldGetStats() {
445 return OldGetStatsForTrack(nullptr);
446 }
447
448 // Synchronously gets stats and returns them. If it times out, fails the test
449 // and returns null.
450 rtc::scoped_refptr<const webrtc::RTCStatsReport> NewGetStats() {
451 rtc::scoped_refptr<webrtc::MockRTCStatsCollectorCallback> callback(
452 new rtc::RefCountedObject<webrtc::MockRTCStatsCollectorCallback>());
453 peer_connection_->GetStats(callback);
454 EXPECT_TRUE_WAIT(callback->called(), kDefaultTimeout);
455 return callback->report();
456 }
457
458 int rendered_width() {
459 EXPECT_FALSE(fake_video_renderers_.empty());
460 return fake_video_renderers_.empty()
461 ? 0
462 : fake_video_renderers_.begin()->second->width();
463 }
464
465 int rendered_height() {
466 EXPECT_FALSE(fake_video_renderers_.empty());
467 return fake_video_renderers_.empty()
468 ? 0
469 : fake_video_renderers_.begin()->second->height();
470 }
471
472 double rendered_aspect_ratio() {
473 if (rendered_height() == 0) {
474 return 0.0;
475 }
476 return static_cast<double>(rendered_width()) / rendered_height();
477 }
478
479 webrtc::VideoRotation rendered_rotation() {
480 EXPECT_FALSE(fake_video_renderers_.empty());
481 return fake_video_renderers_.empty()
482 ? webrtc::kVideoRotation_0
483 : fake_video_renderers_.begin()->second->rotation();
484 }
485
486 int local_rendered_width() {
487 return local_video_renderer_ ? local_video_renderer_->width() : 0;
488 }
489
490 int local_rendered_height() {
491 return local_video_renderer_ ? local_video_renderer_->height() : 0;
492 }
493
494 double local_rendered_aspect_ratio() {
495 if (local_rendered_height() == 0) {
496 return 0.0;
497 }
498 return static_cast<double>(local_rendered_width()) /
499 local_rendered_height();
500 }
501
502 size_t number_of_remote_streams() {
503 if (!pc()) {
504 return 0;
505 }
506 return pc()->remote_streams()->count();
507 }
508
509 StreamCollectionInterface* remote_streams() const {
510 if (!pc()) {
511 ADD_FAILURE();
512 return nullptr;
513 }
514 return pc()->remote_streams();
515 }
516
517 StreamCollectionInterface* local_streams() {
518 if (!pc()) {
519 ADD_FAILURE();
520 return nullptr;
521 }
522 return pc()->local_streams();
523 }
524
525 webrtc::PeerConnectionInterface::SignalingState signaling_state() {
526 return pc()->signaling_state();
527 }
528
529 webrtc::PeerConnectionInterface::IceConnectionState ice_connection_state() {
530 return pc()->ice_connection_state();
531 }
532
533 webrtc::PeerConnectionInterface::IceConnectionState
534 standardized_ice_connection_state() {
535 return pc()->standardized_ice_connection_state();
536 }
537
538 webrtc::PeerConnectionInterface::IceGatheringState ice_gathering_state() {
539 return pc()->ice_gathering_state();
540 }
541
542 // Returns a MockRtpReceiverObserver for each RtpReceiver returned by
543 // GetReceivers. They're updated automatically when a remote offer/answer
544 // from the fake signaling channel is applied, or when
545 // ResetRtpReceiverObservers below is called.
546 const std::vector<std::unique_ptr<MockRtpReceiverObserver>>&
547 rtp_receiver_observers() {
548 return rtp_receiver_observers_;
549 }
550
551 void ResetRtpReceiverObservers() {
552 rtp_receiver_observers_.clear();
553 for (const rtc::scoped_refptr<RtpReceiverInterface>& receiver :
554 pc()->GetReceivers()) {
555 std::unique_ptr<MockRtpReceiverObserver> observer(
556 new MockRtpReceiverObserver(receiver->media_type()));
557 receiver->SetObserver(observer.get());
558 rtp_receiver_observers_.push_back(std::move(observer));
559 }
560 }
561
562 rtc::FakeNetworkManager* network_manager() const {
563 return fake_network_manager_.get();
564 }
565 cricket::PortAllocator* port_allocator() const { return port_allocator_; }
566
567 webrtc::FakeRtcEventLogFactory* event_log_factory() const {
568 return event_log_factory_;
569 }
570
571 const cricket::Candidate& last_candidate_gathered() const {
572 return last_candidate_gathered_;
573 }
574 const cricket::IceCandidateErrorEvent& error_event() const {
575 return error_event_;
576 }
577
578 // Sets the mDNS responder for the owned fake network manager and keeps a
579 // reference to the responder.
580 void SetMdnsResponder(
581 std::unique_ptr<webrtc::FakeMdnsResponder> mdns_responder) {
582 RTC_DCHECK(mdns_responder != nullptr);
583 mdns_responder_ = mdns_responder.get();
584 network_manager()->set_mdns_responder(std::move(mdns_responder));
585 }
586
587 // Returns null on failure.
588 std::unique_ptr<SessionDescriptionInterface> CreateOfferAndWait() {
589 rtc::scoped_refptr<MockCreateSessionDescriptionObserver> observer(
590 new rtc::RefCountedObject<MockCreateSessionDescriptionObserver>());
591 pc()->CreateOffer(observer, offer_answer_options_);
592 return WaitForDescriptionFromObserver(observer);
593 }
594 bool Rollback() {
595 return SetRemoteDescription(
596 webrtc::CreateSessionDescription(SdpType::kRollback, ""));
597 }
598
599 // Functions for querying stats.
600 void StartWatchingDelayStats() {
601 // Get the baseline numbers for audio_packets and audio_delay.
602 auto received_stats = NewGetStats();
603 auto track_stats =
604 received_stats->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>()[0];
605 ASSERT_TRUE(track_stats->relative_packet_arrival_delay.is_defined());
606 auto rtp_stats =
607 received_stats->GetStatsOfType<webrtc::RTCInboundRTPStreamStats>()[0];
608 ASSERT_TRUE(rtp_stats->packets_received.is_defined());
609 ASSERT_TRUE(rtp_stats->track_id.is_defined());
610 audio_track_stats_id_ = track_stats->id();
611 ASSERT_TRUE(received_stats->Get(audio_track_stats_id_));
612 rtp_stats_id_ = rtp_stats->id();
613 ASSERT_EQ(audio_track_stats_id_, *rtp_stats->track_id);
614 audio_packets_stat_ = *rtp_stats->packets_received;
615 audio_delay_stat_ = *track_stats->relative_packet_arrival_delay;
616 audio_samples_stat_ = *track_stats->total_samples_received;
617 audio_concealed_stat_ = *track_stats->concealed_samples;
618 }
619
620 void UpdateDelayStats(std::string tag, int desc_size) {
621 auto report = NewGetStats();
622 auto track_stats =
623 report->GetAs<webrtc::RTCMediaStreamTrackStats>(audio_track_stats_id_);
624 ASSERT_TRUE(track_stats);
625 auto rtp_stats =
626 report->GetAs<webrtc::RTCInboundRTPStreamStats>(rtp_stats_id_);
627 ASSERT_TRUE(rtp_stats);
628 auto delta_packets = *rtp_stats->packets_received - audio_packets_stat_;
629 auto delta_rpad =
630 *track_stats->relative_packet_arrival_delay - audio_delay_stat_;
631 auto recent_delay = delta_packets > 0 ? delta_rpad / delta_packets : -1;
632 // The purpose of these checks is to sound the alarm early if we introduce
633 // serious regressions. The numbers are not acceptable for production, but
634 // occur on slow bots.
635 //
636 // An average relative packet arrival delay over the renegotiation of
637 // > 100 ms indicates that something is dramatically wrong, and will impact
638 // quality for sure.
639 // Worst bots:
640 // linux_x86_dbg at 0.206
641#if !defined(NDEBUG)
642 EXPECT_GT(0.25, recent_delay) << tag << " size " << desc_size;
643#else
644 EXPECT_GT(0.1, recent_delay) << tag << " size " << desc_size;
645#endif
646 auto delta_samples =
647 *track_stats->total_samples_received - audio_samples_stat_;
648 auto delta_concealed =
649 *track_stats->concealed_samples - audio_concealed_stat_;
650 // These limits should be adjusted down as we improve:
651 //
652 // Concealing more than 4000 samples during a renegotiation is unacceptable.
653 // But some bots are slow.
654
655 // Worst bots:
656 // linux_more_configs bot at conceal count 5184
657 // android_arm_rel at conceal count 9241
658 // linux_x86_dbg at 15174
659#if !defined(NDEBUG)
660 EXPECT_GT(18000U, delta_concealed) << "Concealed " << delta_concealed
661 << " of " << delta_samples << " samples";
662#else
663 EXPECT_GT(15000U, delta_concealed) << "Concealed " << delta_concealed
664 << " of " << delta_samples << " samples";
665#endif
666 // Concealing more than 20% of samples during a renegotiation is
667 // unacceptable.
668 // Worst bots:
669 // linux_more_configs bot at conceal rate 0.516
670 // linux_x86_dbg bot at conceal rate 0.854
671 if (delta_samples > 0) {
672#if !defined(NDEBUG)
673 EXPECT_GT(0.95, 1.0 * delta_concealed / delta_samples)
674 << "Concealed " << delta_concealed << " of " << delta_samples
675 << " samples";
676#else
677 EXPECT_GT(0.6, 1.0 * delta_concealed / delta_samples)
678 << "Concealed " << delta_concealed << " of " << delta_samples
679 << " samples";
680#endif
681 }
682 // Increment trailing counters
683 audio_packets_stat_ = *rtp_stats->packets_received;
684 audio_delay_stat_ = *track_stats->relative_packet_arrival_delay;
685 audio_samples_stat_ = *track_stats->total_samples_received;
686 audio_concealed_stat_ = *track_stats->concealed_samples;
687 }
688
689 private:
690 explicit PeerConnectionWrapper(const std::string& debug_name)
691 : debug_name_(debug_name) {}
692
693 bool Init(
694 const PeerConnectionFactory::Options* options,
695 const PeerConnectionInterface::RTCConfiguration* config,
696 webrtc::PeerConnectionDependencies dependencies,
697 rtc::Thread* network_thread,
698 rtc::Thread* worker_thread,
699 std::unique_ptr<webrtc::FakeRtcEventLogFactory> event_log_factory,
700 bool reset_encoder_factory,
701 bool reset_decoder_factory) {
702 // There's an error in this test code if Init ends up being called twice.
703 RTC_DCHECK(!peer_connection_);
704 RTC_DCHECK(!peer_connection_factory_);
705
706 fake_network_manager_.reset(new rtc::FakeNetworkManager());
707 fake_network_manager_->AddInterface(kDefaultLocalAddress);
708
709 std::unique_ptr<cricket::PortAllocator> port_allocator(
710 new cricket::BasicPortAllocator(fake_network_manager_.get()));
711 port_allocator_ = port_allocator.get();
712 fake_audio_capture_module_ = FakeAudioCaptureModule::Create();
713 if (!fake_audio_capture_module_) {
714 return false;
715 }
716 rtc::Thread* const signaling_thread = rtc::Thread::Current();
717
718 webrtc::PeerConnectionFactoryDependencies pc_factory_dependencies;
719 pc_factory_dependencies.network_thread = network_thread;
720 pc_factory_dependencies.worker_thread = worker_thread;
721 pc_factory_dependencies.signaling_thread = signaling_thread;
722 pc_factory_dependencies.task_queue_factory =
723 webrtc::CreateDefaultTaskQueueFactory();
724 pc_factory_dependencies.trials = std::make_unique<FieldTrialBasedConfig>();
725 cricket::MediaEngineDependencies media_deps;
726 media_deps.task_queue_factory =
727 pc_factory_dependencies.task_queue_factory.get();
728 media_deps.adm = fake_audio_capture_module_;
729 webrtc::SetMediaEngineDefaults(&media_deps);
730
731 if (reset_encoder_factory) {
732 media_deps.video_encoder_factory.reset();
733 }
734 if (reset_decoder_factory) {
735 media_deps.video_decoder_factory.reset();
736 }
737
738 if (!media_deps.audio_processing) {
739 // If the standard Creation method for APM returns a null pointer, instead
740 // use the builder for testing to create an APM object.
741 media_deps.audio_processing = AudioProcessingBuilderForTesting().Create();
742 }
743
744 media_deps.trials = pc_factory_dependencies.trials.get();
745
746 pc_factory_dependencies.media_engine =
747 cricket::CreateMediaEngine(std::move(media_deps));
748 pc_factory_dependencies.call_factory = webrtc::CreateCallFactory();
749 if (event_log_factory) {
750 event_log_factory_ = event_log_factory.get();
751 pc_factory_dependencies.event_log_factory = std::move(event_log_factory);
752 } else {
753 pc_factory_dependencies.event_log_factory =
754 std::make_unique<webrtc::RtcEventLogFactory>(
755 pc_factory_dependencies.task_queue_factory.get());
756 }
757 peer_connection_factory_ = webrtc::CreateModularPeerConnectionFactory(
758 std::move(pc_factory_dependencies));
759
760 if (!peer_connection_factory_) {
761 return false;
762 }
763 if (options) {
764 peer_connection_factory_->SetOptions(*options);
765 }
766 if (config) {
767 sdp_semantics_ = config->sdp_semantics;
768 }
769
770 dependencies.allocator = std::move(port_allocator);
771 peer_connection_ = CreatePeerConnection(config, std::move(dependencies));
772 return peer_connection_.get() != nullptr;
773 }
774
775 rtc::scoped_refptr<webrtc::PeerConnectionInterface> CreatePeerConnection(
776 const PeerConnectionInterface::RTCConfiguration* config,
777 webrtc::PeerConnectionDependencies dependencies) {
778 PeerConnectionInterface::RTCConfiguration modified_config;
779 // If |config| is null, this will result in a default configuration being
780 // used.
781 if (config) {
782 modified_config = *config;
783 }
784 // Disable resolution adaptation; we don't want it interfering with the
785 // test results.
786 // TODO(deadbeef): Do something more robust. Since we're testing for aspect
787 // ratios and not specific resolutions, is this even necessary?
788 modified_config.set_cpu_adaptation(false);
789
790 dependencies.observer = this;
791 return peer_connection_factory_->CreatePeerConnection(
792 modified_config, std::move(dependencies));
793 }
794
795 void set_signaling_message_receiver(
796 SignalingMessageReceiver* signaling_message_receiver) {
797 signaling_message_receiver_ = signaling_message_receiver;
798 }
799
800 void set_signaling_delay_ms(int delay_ms) { signaling_delay_ms_ = delay_ms; }
801
802 void set_signal_ice_candidates(bool signal) {
803 signal_ice_candidates_ = signal;
804 }
805
806 rtc::scoped_refptr<webrtc::VideoTrackInterface> CreateLocalVideoTrackInternal(
807 webrtc::FakePeriodicVideoSource::Config config) {
808 // Set max frame rate to 10fps to reduce the risk of test flakiness.
809 // TODO(deadbeef): Do something more robust.
810 config.frame_interval_ms = 100;
811
812 video_track_sources_.emplace_back(
813 new rtc::RefCountedObject<webrtc::FakePeriodicVideoTrackSource>(
814 config, false /* remote */));
815 rtc::scoped_refptr<webrtc::VideoTrackInterface> track(
816 peer_connection_factory_->CreateVideoTrack(
817 rtc::CreateRandomUuid(), video_track_sources_.back()));
818 if (!local_video_renderer_) {
819 local_video_renderer_.reset(new webrtc::FakeVideoTrackRenderer(track));
820 }
821 return track;
822 }
823
824 void HandleIncomingOffer(const std::string& msg) {
825 RTC_LOG(LS_INFO) << debug_name_ << ": HandleIncomingOffer";
826 std::unique_ptr<SessionDescriptionInterface> desc =
827 webrtc::CreateSessionDescription(SdpType::kOffer, msg);
828 if (received_sdp_munger_) {
829 received_sdp_munger_(desc->description());
830 }
831
832 EXPECT_TRUE(SetRemoteDescription(std::move(desc)));
833 // Setting a remote description may have changed the number of receivers,
834 // so reset the receiver observers.
835 ResetRtpReceiverObservers();
836 if (remote_offer_handler_) {
837 remote_offer_handler_();
838 }
839 auto answer = CreateAnswer();
840 ASSERT_NE(nullptr, answer);
841 EXPECT_TRUE(SetLocalDescriptionAndSendSdpMessage(std::move(answer)));
842 }
843
844 void HandleIncomingAnswer(const std::string& msg) {
845 RTC_LOG(LS_INFO) << debug_name_ << ": HandleIncomingAnswer";
846 std::unique_ptr<SessionDescriptionInterface> desc =
847 webrtc::CreateSessionDescription(SdpType::kAnswer, msg);
848 if (received_sdp_munger_) {
849 received_sdp_munger_(desc->description());
850 }
851
852 EXPECT_TRUE(SetRemoteDescription(std::move(desc)));
853 // Set the RtpReceiverObserver after receivers are created.
854 ResetRtpReceiverObservers();
855 }
856
857 // Returns null on failure.
858 std::unique_ptr<SessionDescriptionInterface> CreateAnswer() {
859 rtc::scoped_refptr<MockCreateSessionDescriptionObserver> observer(
860 new rtc::RefCountedObject<MockCreateSessionDescriptionObserver>());
861 pc()->CreateAnswer(observer, offer_answer_options_);
862 return WaitForDescriptionFromObserver(observer);
863 }
864
865 std::unique_ptr<SessionDescriptionInterface> WaitForDescriptionFromObserver(
866 MockCreateSessionDescriptionObserver* observer) {
867 EXPECT_EQ_WAIT(true, observer->called(), kDefaultTimeout);
868 if (!observer->result()) {
869 return nullptr;
870 }
871 auto description = observer->MoveDescription();
872 if (generated_sdp_munger_) {
873 generated_sdp_munger_(description->description());
874 }
875 return description;
876 }
877
878 // Setting the local description and sending the SDP message over the fake
879 // signaling channel are combined into the same method because the SDP
880 // message needs to be sent as soon as SetLocalDescription finishes, without
881 // waiting for the observer to be called. This ensures that ICE candidates
882 // don't outrace the description.
883 bool SetLocalDescriptionAndSendSdpMessage(
884 std::unique_ptr<SessionDescriptionInterface> desc) {
885 rtc::scoped_refptr<MockSetSessionDescriptionObserver> observer(
886 new rtc::RefCountedObject<MockSetSessionDescriptionObserver>());
887 RTC_LOG(LS_INFO) << debug_name_ << ": SetLocalDescriptionAndSendSdpMessage";
888 SdpType type = desc->GetType();
889 std::string sdp;
890 EXPECT_TRUE(desc->ToString(&sdp));
891 RTC_LOG(LS_INFO) << debug_name_ << ": local SDP contents=\n" << sdp;
892 pc()->SetLocalDescription(observer, desc.release());
893 RemoveUnusedVideoRenderers();
894 // As mentioned above, we need to send the message immediately after
895 // SetLocalDescription.
896 SendSdpMessage(type, sdp);
897 EXPECT_TRUE_WAIT(observer->called(), kDefaultTimeout);
898 return true;
899 }
900
901 bool SetRemoteDescription(std::unique_ptr<SessionDescriptionInterface> desc) {
902 rtc::scoped_refptr<MockSetSessionDescriptionObserver> observer(
903 new rtc::RefCountedObject<MockSetSessionDescriptionObserver>());
904 RTC_LOG(LS_INFO) << debug_name_ << ": SetRemoteDescription";
905 pc()->SetRemoteDescription(observer, desc.release());
906 RemoveUnusedVideoRenderers();
907 EXPECT_TRUE_WAIT(observer->called(), kDefaultTimeout);
908 return observer->result();
909 }
910
911 // This is a work around to remove unused fake_video_renderers from
912 // transceivers that have either stopped or are no longer receiving.
913 void RemoveUnusedVideoRenderers() {
914 if (sdp_semantics_ != SdpSemantics::kUnifiedPlan) {
915 return;
916 }
917 auto transceivers = pc()->GetTransceivers();
918 std::set<std::string> active_renderers;
919 for (auto& transceiver : transceivers) {
920 // Note - we don't check for direction here. This function is called
921 // before direction is set, and in that case, we should not remove
922 // the renderer.
923 if (transceiver->receiver()->media_type() == cricket::MEDIA_TYPE_VIDEO) {
924 active_renderers.insert(transceiver->receiver()->track()->id());
925 }
926 }
927 for (auto it = fake_video_renderers_.begin();
928 it != fake_video_renderers_.end();) {
929 // Remove fake video renderers belonging to any non-active transceivers.
930 if (!active_renderers.count(it->first)) {
931 it = fake_video_renderers_.erase(it);
932 } else {
933 it++;
934 }
935 }
936 }
937
938 // Simulate sending a blob of SDP with delay |signaling_delay_ms_| (0 by
939 // default).
940 void SendSdpMessage(SdpType type, const std::string& msg) {
941 if (signaling_delay_ms_ == 0) {
942 RelaySdpMessageIfReceiverExists(type, msg);
943 } else {
944 invoker_.AsyncInvokeDelayed<void>(
945 RTC_FROM_HERE, rtc::Thread::Current(),
946 [this, type, msg] { RelaySdpMessageIfReceiverExists(type, msg); },
947 signaling_delay_ms_);
948 }
949 }
950
951 void RelaySdpMessageIfReceiverExists(SdpType type, const std::string& msg) {
952 if (signaling_message_receiver_) {
953 signaling_message_receiver_->ReceiveSdpMessage(type, msg);
954 }
955 }
956
957 // Simulate trickling an ICE candidate with delay |signaling_delay_ms_| (0 by
958 // default).
959 void SendIceMessage(const std::string& sdp_mid,
960 int sdp_mline_index,
961 const std::string& msg) {
962 if (signaling_delay_ms_ == 0) {
963 RelayIceMessageIfReceiverExists(sdp_mid, sdp_mline_index, msg);
964 } else {
965 invoker_.AsyncInvokeDelayed<void>(
966 RTC_FROM_HERE, rtc::Thread::Current(),
967 [this, sdp_mid, sdp_mline_index, msg] {
968 RelayIceMessageIfReceiverExists(sdp_mid, sdp_mline_index, msg);
969 },
970 signaling_delay_ms_);
971 }
972 }
973
974 void RelayIceMessageIfReceiverExists(const std::string& sdp_mid,
975 int sdp_mline_index,
976 const std::string& msg) {
977 if (signaling_message_receiver_) {
978 signaling_message_receiver_->ReceiveIceMessage(sdp_mid, sdp_mline_index,
979 msg);
980 }
981 }
982
983 // SignalingMessageReceiver callbacks.
984 void ReceiveSdpMessage(SdpType type, const std::string& msg) override {
985 if (type == SdpType::kOffer) {
986 HandleIncomingOffer(msg);
987 } else {
988 HandleIncomingAnswer(msg);
989 }
990 }
991
992 void ReceiveIceMessage(const std::string& sdp_mid,
993 int sdp_mline_index,
994 const std::string& msg) override {
995 RTC_LOG(LS_INFO) << debug_name_ << ": ReceiveIceMessage";
996 std::unique_ptr<webrtc::IceCandidateInterface> candidate(
997 webrtc::CreateIceCandidate(sdp_mid, sdp_mline_index, msg, nullptr));
998 EXPECT_TRUE(pc()->AddIceCandidate(candidate.get()));
999 }
1000
1001 // PeerConnectionObserver callbacks.
1002 void OnSignalingChange(
1003 webrtc::PeerConnectionInterface::SignalingState new_state) override {
1004 EXPECT_EQ(pc()->signaling_state(), new_state);
1005 peer_connection_signaling_state_history_.push_back(new_state);
1006 }
1007 void OnAddTrack(rtc::scoped_refptr<RtpReceiverInterface> receiver,
1008 const std::vector<rtc::scoped_refptr<MediaStreamInterface>>&
1009 streams) override {
1010 if (receiver->media_type() == cricket::MEDIA_TYPE_VIDEO) {
1011 rtc::scoped_refptr<VideoTrackInterface> video_track(
1012 static_cast<VideoTrackInterface*>(receiver->track().get()));
1013 ASSERT_TRUE(fake_video_renderers_.find(video_track->id()) ==
1014 fake_video_renderers_.end());
1015 fake_video_renderers_[video_track->id()] =
1016 std::make_unique<FakeVideoTrackRenderer>(video_track);
1017 }
1018 }
1019 void OnRemoveTrack(
1020 rtc::scoped_refptr<RtpReceiverInterface> receiver) override {
1021 if (receiver->media_type() == cricket::MEDIA_TYPE_VIDEO) {
1022 auto it = fake_video_renderers_.find(receiver->track()->id());
1023 if (it != fake_video_renderers_.end()) {
1024 fake_video_renderers_.erase(it);
1025 } else {
1026 RTC_LOG(LS_ERROR) << "OnRemoveTrack called for non-active renderer";
1027 }
1028 }
1029 }
1030 void OnRenegotiationNeeded() override {}
1031 void OnIceConnectionChange(
1032 webrtc::PeerConnectionInterface::IceConnectionState new_state) override {
1033 EXPECT_EQ(pc()->ice_connection_state(), new_state);
1034 ice_connection_state_history_.push_back(new_state);
1035 }
1036 void OnStandardizedIceConnectionChange(
1037 webrtc::PeerConnectionInterface::IceConnectionState new_state) override {
1038 standardized_ice_connection_state_history_.push_back(new_state);
1039 }
1040 void OnConnectionChange(
1041 webrtc::PeerConnectionInterface::PeerConnectionState new_state) override {
1042 peer_connection_state_history_.push_back(new_state);
1043 }
1044
1045 void OnIceGatheringChange(
1046 webrtc::PeerConnectionInterface::IceGatheringState new_state) override {
1047 EXPECT_EQ(pc()->ice_gathering_state(), new_state);
1048 ice_gathering_state_history_.push_back(new_state);
1049 }
1050
1051 void OnIceSelectedCandidatePairChanged(
1052 const cricket::CandidatePairChangeEvent& event) {
1053 ice_candidate_pair_change_history_.push_back(event);
1054 }
1055
1056 void OnIceCandidate(const webrtc::IceCandidateInterface* candidate) override {
1057 RTC_LOG(LS_INFO) << debug_name_ << ": OnIceCandidate";
1058
1059 if (remote_async_resolver_) {
1060 const auto& local_candidate = candidate->candidate();
1061 if (local_candidate.address().IsUnresolvedIP()) {
1062 RTC_DCHECK(local_candidate.type() == cricket::LOCAL_PORT_TYPE);
1063 rtc::SocketAddress resolved_addr(local_candidate.address());
1064 const auto resolved_ip = mdns_responder_->GetMappedAddressForName(
1065 local_candidate.address().hostname());
1066 RTC_DCHECK(!resolved_ip.IsNil());
1067 resolved_addr.SetResolvedIP(resolved_ip);
1068 EXPECT_CALL(*remote_async_resolver_, GetResolvedAddress(_, _))
1069 .WillOnce(DoAll(SetArgPointee<1>(resolved_addr), Return(true)));
1070 EXPECT_CALL(*remote_async_resolver_, Destroy(_));
1071 }
1072 }
1073
1074 std::string ice_sdp;
1075 EXPECT_TRUE(candidate->ToString(&ice_sdp));
1076 if (signaling_message_receiver_ == nullptr || !signal_ice_candidates_) {
1077 // Remote party may be deleted.
1078 return;
1079 }
1080 SendIceMessage(candidate->sdp_mid(), candidate->sdp_mline_index(), ice_sdp);
1081 last_candidate_gathered_ = candidate->candidate();
1082 }
1083 void OnIceCandidateError(const std::string& address,
1084 int port,
1085 const std::string& url,
1086 int error_code,
1087 const std::string& error_text) override {
1088 error_event_ = cricket::IceCandidateErrorEvent(address, port, url,
1089 error_code, error_text);
1090 }
1091 void OnDataChannel(
1092 rtc::scoped_refptr<DataChannelInterface> data_channel) override {
1093 RTC_LOG(LS_INFO) << debug_name_ << ": OnDataChannel";
1094 data_channel_ = data_channel;
1095 data_observer_.reset(new MockDataChannelObserver(data_channel));
1096 }
1097
1098 std::string debug_name_;
1099
1100 std::unique_ptr<rtc::FakeNetworkManager> fake_network_manager_;
1101 // Reference to the mDNS responder owned by |fake_network_manager_| after set.
1102 webrtc::FakeMdnsResponder* mdns_responder_ = nullptr;
1103
1104 rtc::scoped_refptr<webrtc::PeerConnectionInterface> peer_connection_;
1105 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface>
1106 peer_connection_factory_;
1107
1108 cricket::PortAllocator* port_allocator_;
1109 // Needed to keep track of number of frames sent.
1110 rtc::scoped_refptr<FakeAudioCaptureModule> fake_audio_capture_module_;
1111 // Needed to keep track of number of frames received.
1112 std::map<std::string, std::unique_ptr<webrtc::FakeVideoTrackRenderer>>
1113 fake_video_renderers_;
1114 // Needed to ensure frames aren't received for removed tracks.
1115 std::vector<std::unique_ptr<webrtc::FakeVideoTrackRenderer>>
1116 removed_fake_video_renderers_;
1117
1118 // For remote peer communication.
1119 SignalingMessageReceiver* signaling_message_receiver_ = nullptr;
1120 int signaling_delay_ms_ = 0;
1121 bool signal_ice_candidates_ = true;
1122 cricket::Candidate last_candidate_gathered_;
1123 cricket::IceCandidateErrorEvent error_event_;
1124
1125 // Store references to the video sources we've created, so that we can stop
1126 // them, if required.
1127 std::vector<rtc::scoped_refptr<webrtc::VideoTrackSource>>
1128 video_track_sources_;
1129 // |local_video_renderer_| attached to the first created local video track.
1130 std::unique_ptr<webrtc::FakeVideoTrackRenderer> local_video_renderer_;
1131
1132 SdpSemantics sdp_semantics_;
1133 PeerConnectionInterface::RTCOfferAnswerOptions offer_answer_options_;
1134 std::function<void(cricket::SessionDescription*)> received_sdp_munger_;
1135 std::function<void(cricket::SessionDescription*)> generated_sdp_munger_;
1136 std::function<void()> remote_offer_handler_;
1137 rtc::MockAsyncResolver* remote_async_resolver_ = nullptr;
1138 rtc::scoped_refptr<DataChannelInterface> data_channel_;
1139 std::unique_ptr<MockDataChannelObserver> data_observer_;
1140
1141 std::vector<std::unique_ptr<MockRtpReceiverObserver>> rtp_receiver_observers_;
1142
1143 std::vector<PeerConnectionInterface::IceConnectionState>
1144 ice_connection_state_history_;
1145 std::vector<PeerConnectionInterface::IceConnectionState>
1146 standardized_ice_connection_state_history_;
1147 std::vector<PeerConnectionInterface::PeerConnectionState>
1148 peer_connection_state_history_;
1149 std::vector<PeerConnectionInterface::IceGatheringState>
1150 ice_gathering_state_history_;
1151 std::vector<cricket::CandidatePairChangeEvent>
1152 ice_candidate_pair_change_history_;
1153 std::vector<PeerConnectionInterface::SignalingState>
1154 peer_connection_signaling_state_history_;
1155 webrtc::FakeRtcEventLogFactory* event_log_factory_;
1156
1157 // Variables for tracking delay stats on an audio track
1158 int audio_packets_stat_ = 0;
1159 double audio_delay_stat_ = 0.0;
1160 uint64_t audio_samples_stat_ = 0;
1161 uint64_t audio_concealed_stat_ = 0;
1162 std::string rtp_stats_id_;
1163 std::string audio_track_stats_id_;
1164
1165 rtc::AsyncInvoker invoker_;
1166
1167 friend class PeerConnectionIntegrationBaseTest;
1168};
1169
1170class MockRtcEventLogOutput : public webrtc::RtcEventLogOutput {
1171 public:
1172 virtual ~MockRtcEventLogOutput() = default;
1173 MOCK_METHOD(bool, IsActive, (), (const, override));
1174 MOCK_METHOD(bool, Write, (const std::string&), (override));
1175};
1176
1177// This helper object is used for both specifying how many audio/video frames
1178// are expected to be received for a caller/callee. It provides helper functions
1179// to specify these expectations. The object initially starts in a state of no
1180// expectations.
1181class MediaExpectations {
1182 public:
1183 enum ExpectFrames {
1184 kExpectSomeFrames,
1185 kExpectNoFrames,
1186 kNoExpectation,
1187 };
1188
1189 void ExpectBidirectionalAudioAndVideo() {
1190 ExpectBidirectionalAudio();
1191 ExpectBidirectionalVideo();
1192 }
1193
1194 void ExpectBidirectionalAudio() {
1195 CallerExpectsSomeAudio();
1196 CalleeExpectsSomeAudio();
1197 }
1198
1199 void ExpectNoAudio() {
1200 CallerExpectsNoAudio();
1201 CalleeExpectsNoAudio();
1202 }
1203
1204 void ExpectBidirectionalVideo() {
1205 CallerExpectsSomeVideo();
1206 CalleeExpectsSomeVideo();
1207 }
1208
1209 void ExpectNoVideo() {
1210 CallerExpectsNoVideo();
1211 CalleeExpectsNoVideo();
1212 }
1213
1214 void CallerExpectsSomeAudioAndVideo() {
1215 CallerExpectsSomeAudio();
1216 CallerExpectsSomeVideo();
1217 }
1218
1219 void CalleeExpectsSomeAudioAndVideo() {
1220 CalleeExpectsSomeAudio();
1221 CalleeExpectsSomeVideo();
1222 }
1223
1224 // Caller's audio functions.
1225 void CallerExpectsSomeAudio(
1226 int expected_audio_frames = kDefaultExpectedAudioFrameCount) {
1227 caller_audio_expectation_ = kExpectSomeFrames;
1228 caller_audio_frames_expected_ = expected_audio_frames;
1229 }
1230
1231 void CallerExpectsNoAudio() {
1232 caller_audio_expectation_ = kExpectNoFrames;
1233 caller_audio_frames_expected_ = 0;
1234 }
1235
1236 // Caller's video functions.
1237 void CallerExpectsSomeVideo(
1238 int expected_video_frames = kDefaultExpectedVideoFrameCount) {
1239 caller_video_expectation_ = kExpectSomeFrames;
1240 caller_video_frames_expected_ = expected_video_frames;
1241 }
1242
1243 void CallerExpectsNoVideo() {
1244 caller_video_expectation_ = kExpectNoFrames;
1245 caller_video_frames_expected_ = 0;
1246 }
1247
1248 // Callee's audio functions.
1249 void CalleeExpectsSomeAudio(
1250 int expected_audio_frames = kDefaultExpectedAudioFrameCount) {
1251 callee_audio_expectation_ = kExpectSomeFrames;
1252 callee_audio_frames_expected_ = expected_audio_frames;
1253 }
1254
1255 void CalleeExpectsNoAudio() {
1256 callee_audio_expectation_ = kExpectNoFrames;
1257 callee_audio_frames_expected_ = 0;
1258 }
1259
1260 // Callee's video functions.
1261 void CalleeExpectsSomeVideo(
1262 int expected_video_frames = kDefaultExpectedVideoFrameCount) {
1263 callee_video_expectation_ = kExpectSomeFrames;
1264 callee_video_frames_expected_ = expected_video_frames;
1265 }
1266
1267 void CalleeExpectsNoVideo() {
1268 callee_video_expectation_ = kExpectNoFrames;
1269 callee_video_frames_expected_ = 0;
1270 }
1271
1272 ExpectFrames caller_audio_expectation_ = kNoExpectation;
1273 ExpectFrames caller_video_expectation_ = kNoExpectation;
1274 ExpectFrames callee_audio_expectation_ = kNoExpectation;
1275 ExpectFrames callee_video_expectation_ = kNoExpectation;
1276 int caller_audio_frames_expected_ = 0;
1277 int caller_video_frames_expected_ = 0;
1278 int callee_audio_frames_expected_ = 0;
1279 int callee_video_frames_expected_ = 0;
1280};
1281
1282class MockIceTransport : public webrtc::IceTransportInterface {
1283 public:
1284 MockIceTransport(const std::string& name, int component)
1285 : internal_(std::make_unique<cricket::FakeIceTransport>(
1286 name,
1287 component,
1288 nullptr /* network_thread */)) {}
1289 ~MockIceTransport() = default;
1290 cricket::IceTransportInternal* internal() { return internal_.get(); }
1291
1292 private:
1293 std::unique_ptr<cricket::FakeIceTransport> internal_;
1294};
1295
1296class MockIceTransportFactory : public IceTransportFactory {
1297 public:
1298 ~MockIceTransportFactory() override = default;
1299 rtc::scoped_refptr<IceTransportInterface> CreateIceTransport(
1300 const std::string& transport_name,
1301 int component,
1302 IceTransportInit init) {
1303 RecordIceTransportCreated();
1304 return new rtc::RefCountedObject<MockIceTransport>(transport_name,
1305 component);
1306 }
1307 MOCK_METHOD(void, RecordIceTransportCreated, ());
1308};
1309
1310// Tests two PeerConnections connecting to each other end-to-end, using a
1311// virtual network, fake A/V capture and fake encoder/decoders. The
1312// PeerConnections share the threads/socket servers, but use separate versions
1313// of everything else (including "PeerConnectionFactory"s).
1314class PeerConnectionIntegrationBaseTest : public ::testing::Test {
1315 public:
1316 explicit PeerConnectionIntegrationBaseTest(SdpSemantics sdp_semantics)
1317 : sdp_semantics_(sdp_semantics),
1318 ss_(new rtc::VirtualSocketServer()),
1319 fss_(new rtc::FirewallSocketServer(ss_.get())),
1320 network_thread_(new rtc::Thread(fss_.get())),
1321 worker_thread_(rtc::Thread::Create()) {
1322 network_thread_->SetName("PCNetworkThread", this);
1323 worker_thread_->SetName("PCWorkerThread", this);
1324 RTC_CHECK(network_thread_->Start());
1325 RTC_CHECK(worker_thread_->Start());
1326 webrtc::metrics::Reset();
1327 }
1328
1329 ~PeerConnectionIntegrationBaseTest() {
1330 // The PeerConnections should be deleted before the TurnCustomizers.
1331 // A TurnPort is created with a raw pointer to a TurnCustomizer. The
1332 // TurnPort has the same lifetime as the PeerConnection, so it's expected
1333 // that the TurnCustomizer outlives the life of the PeerConnection or else
1334 // when Send() is called it will hit a seg fault.
1335 if (caller_) {
1336 caller_->set_signaling_message_receiver(nullptr);
1337 delete SetCallerPcWrapperAndReturnCurrent(nullptr);
1338 }
1339 if (callee_) {
1340 callee_->set_signaling_message_receiver(nullptr);
1341 delete SetCalleePcWrapperAndReturnCurrent(nullptr);
1342 }
1343
1344 // If turn servers were created for the test they need to be destroyed on
1345 // the network thread.
1346 network_thread()->Invoke<void>(RTC_FROM_HERE, [this] {
1347 turn_servers_.clear();
1348 turn_customizers_.clear();
1349 });
1350 }
1351
1352 bool SignalingStateStable() {
1353 return caller_->SignalingStateStable() && callee_->SignalingStateStable();
1354 }
1355
1356 bool DtlsConnected() {
1357 // TODO(deadbeef): kIceConnectionConnected currently means both ICE and DTLS
1358 // are connected. This is an important distinction. Once we have separate
1359 // ICE and DTLS state, this check needs to use the DTLS state.
1360 return (callee()->ice_connection_state() ==
1361 webrtc::PeerConnectionInterface::kIceConnectionConnected ||
1362 callee()->ice_connection_state() ==
1363 webrtc::PeerConnectionInterface::kIceConnectionCompleted) &&
1364 (caller()->ice_connection_state() ==
1365 webrtc::PeerConnectionInterface::kIceConnectionConnected ||
1366 caller()->ice_connection_state() ==
1367 webrtc::PeerConnectionInterface::kIceConnectionCompleted);
1368 }
1369
1370 // When |event_log_factory| is null, the default implementation of the event
1371 // log factory will be used.
1372 std::unique_ptr<PeerConnectionWrapper> CreatePeerConnectionWrapper(
1373 const std::string& debug_name,
1374 const PeerConnectionFactory::Options* options,
1375 const RTCConfiguration* config,
1376 webrtc::PeerConnectionDependencies dependencies,
1377 std::unique_ptr<webrtc::FakeRtcEventLogFactory> event_log_factory,
1378 bool reset_encoder_factory,
1379 bool reset_decoder_factory) {
1380 RTCConfiguration modified_config;
1381 if (config) {
1382 modified_config = *config;
1383 }
1384 modified_config.sdp_semantics = sdp_semantics_;
1385 if (!dependencies.cert_generator) {
1386 dependencies.cert_generator =
1387 std::make_unique<FakeRTCCertificateGenerator>();
1388 }
1389 std::unique_ptr<PeerConnectionWrapper> client(
1390 new PeerConnectionWrapper(debug_name));
1391
1392 if (!client->Init(options, &modified_config, std::move(dependencies),
1393 network_thread_.get(), worker_thread_.get(),
1394 std::move(event_log_factory), reset_encoder_factory,
1395 reset_decoder_factory)) {
1396 return nullptr;
1397 }
1398 return client;
1399 }
1400
1401 std::unique_ptr<PeerConnectionWrapper>
1402 CreatePeerConnectionWrapperWithFakeRtcEventLog(
1403 const std::string& debug_name,
1404 const PeerConnectionFactory::Options* options,
1405 const RTCConfiguration* config,
1406 webrtc::PeerConnectionDependencies dependencies) {
1407 return CreatePeerConnectionWrapper(
1408 debug_name, options, config, std::move(dependencies),
1409 std::make_unique<webrtc::FakeRtcEventLogFactory>(),
1410 /*reset_encoder_factory=*/false,
1411 /*reset_decoder_factory=*/false);
1412 }
1413
1414 bool CreatePeerConnectionWrappers() {
1415 return CreatePeerConnectionWrappersWithConfig(
1416 PeerConnectionInterface::RTCConfiguration(),
1417 PeerConnectionInterface::RTCConfiguration());
1418 }
1419
1420 bool CreatePeerConnectionWrappersWithSdpSemantics(
1421 SdpSemantics caller_semantics,
1422 SdpSemantics callee_semantics) {
1423 // Can't specify the sdp_semantics in the passed-in configuration since it
1424 // will be overwritten by CreatePeerConnectionWrapper with whatever is
1425 // stored in sdp_semantics_. So get around this by modifying the instance
1426 // variable before calling CreatePeerConnectionWrapper for the caller and
1427 // callee PeerConnections.
1428 SdpSemantics original_semantics = sdp_semantics_;
1429 sdp_semantics_ = caller_semantics;
1430 caller_ = CreatePeerConnectionWrapper(
1431 "Caller", nullptr, nullptr, webrtc::PeerConnectionDependencies(nullptr),
1432 nullptr,
1433 /*reset_encoder_factory=*/false,
1434 /*reset_decoder_factory=*/false);
1435 sdp_semantics_ = callee_semantics;
1436 callee_ = CreatePeerConnectionWrapper(
1437 "Callee", nullptr, nullptr, webrtc::PeerConnectionDependencies(nullptr),
1438 nullptr,
1439 /*reset_encoder_factory=*/false,
1440 /*reset_decoder_factory=*/false);
1441 sdp_semantics_ = original_semantics;
1442 return caller_ && callee_;
1443 }
1444
1445 bool CreatePeerConnectionWrappersWithConfig(
1446 const PeerConnectionInterface::RTCConfiguration& caller_config,
1447 const PeerConnectionInterface::RTCConfiguration& callee_config) {
1448 caller_ = CreatePeerConnectionWrapper(
1449 "Caller", nullptr, &caller_config,
1450 webrtc::PeerConnectionDependencies(nullptr), nullptr,
1451 /*reset_encoder_factory=*/false,
1452 /*reset_decoder_factory=*/false);
1453 callee_ = CreatePeerConnectionWrapper(
1454 "Callee", nullptr, &callee_config,
1455 webrtc::PeerConnectionDependencies(nullptr), nullptr,
1456 /*reset_encoder_factory=*/false,
1457 /*reset_decoder_factory=*/false);
1458 return caller_ && callee_;
1459 }
1460
1461 bool CreatePeerConnectionWrappersWithConfigAndDeps(
1462 const PeerConnectionInterface::RTCConfiguration& caller_config,
1463 webrtc::PeerConnectionDependencies caller_dependencies,
1464 const PeerConnectionInterface::RTCConfiguration& callee_config,
1465 webrtc::PeerConnectionDependencies callee_dependencies) {
1466 caller_ =
1467 CreatePeerConnectionWrapper("Caller", nullptr, &caller_config,
1468 std::move(caller_dependencies), nullptr,
1469 /*reset_encoder_factory=*/false,
1470 /*reset_decoder_factory=*/false);
1471 callee_ =
1472 CreatePeerConnectionWrapper("Callee", nullptr, &callee_config,
1473 std::move(callee_dependencies), nullptr,
1474 /*reset_encoder_factory=*/false,
1475 /*reset_decoder_factory=*/false);
1476 return caller_ && callee_;
1477 }
1478
1479 bool CreatePeerConnectionWrappersWithOptions(
1480 const PeerConnectionFactory::Options& caller_options,
1481 const PeerConnectionFactory::Options& callee_options) {
1482 caller_ = CreatePeerConnectionWrapper(
1483 "Caller", &caller_options, nullptr,
1484 webrtc::PeerConnectionDependencies(nullptr), nullptr,
1485 /*reset_encoder_factory=*/false,
1486 /*reset_decoder_factory=*/false);
1487 callee_ = CreatePeerConnectionWrapper(
1488 "Callee", &callee_options, nullptr,
1489 webrtc::PeerConnectionDependencies(nullptr), nullptr,
1490 /*reset_encoder_factory=*/false,
1491 /*reset_decoder_factory=*/false);
1492 return caller_ && callee_;
1493 }
1494
1495 bool CreatePeerConnectionWrappersWithFakeRtcEventLog() {
1496 PeerConnectionInterface::RTCConfiguration default_config;
1497 caller_ = CreatePeerConnectionWrapperWithFakeRtcEventLog(
1498 "Caller", nullptr, &default_config,
1499 webrtc::PeerConnectionDependencies(nullptr));
1500 callee_ = CreatePeerConnectionWrapperWithFakeRtcEventLog(
1501 "Callee", nullptr, &default_config,
1502 webrtc::PeerConnectionDependencies(nullptr));
1503 return caller_ && callee_;
1504 }
1505
1506 std::unique_ptr<PeerConnectionWrapper>
1507 CreatePeerConnectionWrapperWithAlternateKey() {
1508 std::unique_ptr<FakeRTCCertificateGenerator> cert_generator(
1509 new FakeRTCCertificateGenerator());
1510 cert_generator->use_alternate_key();
1511
1512 webrtc::PeerConnectionDependencies dependencies(nullptr);
1513 dependencies.cert_generator = std::move(cert_generator);
1514 return CreatePeerConnectionWrapper("New Peer", nullptr, nullptr,
1515 std::move(dependencies), nullptr,
1516 /*reset_encoder_factory=*/false,
1517 /*reset_decoder_factory=*/false);
1518 }
1519
1520 bool CreateOneDirectionalPeerConnectionWrappers(bool caller_to_callee) {
1521 caller_ = CreatePeerConnectionWrapper(
1522 "Caller", nullptr, nullptr, webrtc::PeerConnectionDependencies(nullptr),
1523 nullptr,
1524 /*reset_encoder_factory=*/!caller_to_callee,
1525 /*reset_decoder_factory=*/caller_to_callee);
1526 callee_ = CreatePeerConnectionWrapper(
1527 "Callee", nullptr, nullptr, webrtc::PeerConnectionDependencies(nullptr),
1528 nullptr,
1529 /*reset_encoder_factory=*/caller_to_callee,
1530 /*reset_decoder_factory=*/!caller_to_callee);
1531 return caller_ && callee_;
1532 }
1533
1534 cricket::TestTurnServer* CreateTurnServer(
1535 rtc::SocketAddress internal_address,
1536 rtc::SocketAddress external_address,
1537 cricket::ProtocolType type = cricket::ProtocolType::PROTO_UDP,
1538 const std::string& common_name = "test turn server") {
1539 rtc::Thread* thread = network_thread();
1540 std::unique_ptr<cricket::TestTurnServer> turn_server =
1541 network_thread()->Invoke<std::unique_ptr<cricket::TestTurnServer>>(
1542 RTC_FROM_HERE,
1543 [thread, internal_address, external_address, type, common_name] {
1544 return std::make_unique<cricket::TestTurnServer>(
1545 thread, internal_address, external_address, type,
1546 /*ignore_bad_certs=*/true, common_name);
1547 });
1548 turn_servers_.push_back(std::move(turn_server));
1549 // Interactions with the turn server should be done on the network thread.
1550 return turn_servers_.back().get();
1551 }
1552
1553 cricket::TestTurnCustomizer* CreateTurnCustomizer() {
1554 std::unique_ptr<cricket::TestTurnCustomizer> turn_customizer =
1555 network_thread()->Invoke<std::unique_ptr<cricket::TestTurnCustomizer>>(
1556 RTC_FROM_HERE,
1557 [] { return std::make_unique<cricket::TestTurnCustomizer>(); });
1558 turn_customizers_.push_back(std::move(turn_customizer));
1559 // Interactions with the turn customizer should be done on the network
1560 // thread.
1561 return turn_customizers_.back().get();
1562 }
1563
1564 // Checks that the function counters for a TestTurnCustomizer are greater than
1565 // 0.
1566 void ExpectTurnCustomizerCountersIncremented(
1567 cricket::TestTurnCustomizer* turn_customizer) {
1568 unsigned int allow_channel_data_counter =
1569 network_thread()->Invoke<unsigned int>(
1570 RTC_FROM_HERE, [turn_customizer] {
1571 return turn_customizer->allow_channel_data_cnt_;
1572 });
1573 EXPECT_GT(allow_channel_data_counter, 0u);
1574 unsigned int modify_counter = network_thread()->Invoke<unsigned int>(
1575 RTC_FROM_HERE,
1576 [turn_customizer] { return turn_customizer->modify_cnt_; });
1577 EXPECT_GT(modify_counter, 0u);
1578 }
1579
1580 // Once called, SDP blobs and ICE candidates will be automatically signaled
1581 // between PeerConnections.
1582 void ConnectFakeSignaling() {
1583 caller_->set_signaling_message_receiver(callee_.get());
1584 callee_->set_signaling_message_receiver(caller_.get());
1585 }
1586
1587 // Once called, SDP blobs will be automatically signaled between
1588 // PeerConnections. Note that ICE candidates will not be signaled unless they
1589 // are in the exchanged SDP blobs.
1590 void ConnectFakeSignalingForSdpOnly() {
1591 ConnectFakeSignaling();
1592 SetSignalIceCandidates(false);
1593 }
1594
1595 void SetSignalingDelayMs(int delay_ms) {
1596 caller_->set_signaling_delay_ms(delay_ms);
1597 callee_->set_signaling_delay_ms(delay_ms);
1598 }
1599
1600 void SetSignalIceCandidates(bool signal) {
1601 caller_->set_signal_ice_candidates(signal);
1602 callee_->set_signal_ice_candidates(signal);
1603 }
1604
1605 // Messages may get lost on the unreliable DataChannel, so we send multiple
1606 // times to avoid test flakiness.
1607 void SendRtpDataWithRetries(webrtc::DataChannelInterface* dc,
1608 const std::string& data,
1609 int retries) {
1610 for (int i = 0; i < retries; ++i) {
1611 dc->Send(DataBuffer(data));
1612 }
1613 }
1614
1615 rtc::Thread* network_thread() { return network_thread_.get(); }
1616
1617 rtc::VirtualSocketServer* virtual_socket_server() { return ss_.get(); }
1618
1619 PeerConnectionWrapper* caller() { return caller_.get(); }
1620
1621 // Set the |caller_| to the |wrapper| passed in and return the
1622 // original |caller_|.
1623 PeerConnectionWrapper* SetCallerPcWrapperAndReturnCurrent(
1624 PeerConnectionWrapper* wrapper) {
1625 PeerConnectionWrapper* old = caller_.release();
1626 caller_.reset(wrapper);
1627 return old;
1628 }
1629
1630 PeerConnectionWrapper* callee() { return callee_.get(); }
1631
1632 // Set the |callee_| to the |wrapper| passed in and return the
1633 // original |callee_|.
1634 PeerConnectionWrapper* SetCalleePcWrapperAndReturnCurrent(
1635 PeerConnectionWrapper* wrapper) {
1636 PeerConnectionWrapper* old = callee_.release();
1637 callee_.reset(wrapper);
1638 return old;
1639 }
1640
1641 void SetPortAllocatorFlags(uint32_t caller_flags, uint32_t callee_flags) {
1642 network_thread()->Invoke<void>(RTC_FROM_HERE, [this, caller_flags] {
1643 caller()->port_allocator()->set_flags(caller_flags);
1644 });
1645 network_thread()->Invoke<void>(RTC_FROM_HERE, [this, callee_flags] {
1646 callee()->port_allocator()->set_flags(callee_flags);
1647 });
1648 }
1649
1650 rtc::FirewallSocketServer* firewall() const { return fss_.get(); }
1651
1652 // Expects the provided number of new frames to be received within
1653 // kMaxWaitForFramesMs. The new expected frames are specified in
1654 // |media_expectations|. Returns false if any of the expectations were
1655 // not met.
1656 bool ExpectNewFrames(const MediaExpectations& media_expectations) {
1657 // Make sure there are no bogus tracks confusing the issue.
1658 caller()->RemoveUnusedVideoRenderers();
1659 callee()->RemoveUnusedVideoRenderers();
1660 // First initialize the expected frame counts based upon the current
1661 // frame count.
1662 int total_caller_audio_frames_expected = caller()->audio_frames_received();
1663 if (media_expectations.caller_audio_expectation_ ==
1664 MediaExpectations::kExpectSomeFrames) {
1665 total_caller_audio_frames_expected +=
1666 media_expectations.caller_audio_frames_expected_;
1667 }
1668 int total_caller_video_frames_expected =
1669 caller()->min_video_frames_received_per_track();
1670 if (media_expectations.caller_video_expectation_ ==
1671 MediaExpectations::kExpectSomeFrames) {
1672 total_caller_video_frames_expected +=
1673 media_expectations.caller_video_frames_expected_;
1674 }
1675 int total_callee_audio_frames_expected = callee()->audio_frames_received();
1676 if (media_expectations.callee_audio_expectation_ ==
1677 MediaExpectations::kExpectSomeFrames) {
1678 total_callee_audio_frames_expected +=
1679 media_expectations.callee_audio_frames_expected_;
1680 }
1681 int total_callee_video_frames_expected =
1682 callee()->min_video_frames_received_per_track();
1683 if (media_expectations.callee_video_expectation_ ==
1684 MediaExpectations::kExpectSomeFrames) {
1685 total_callee_video_frames_expected +=
1686 media_expectations.callee_video_frames_expected_;
1687 }
1688
1689 // Wait for the expected frames.
1690 EXPECT_TRUE_WAIT(caller()->audio_frames_received() >=
1691 total_caller_audio_frames_expected &&
1692 caller()->min_video_frames_received_per_track() >=
1693 total_caller_video_frames_expected &&
1694 callee()->audio_frames_received() >=
1695 total_callee_audio_frames_expected &&
1696 callee()->min_video_frames_received_per_track() >=
1697 total_callee_video_frames_expected,
1698 kMaxWaitForFramesMs);
1699 bool expectations_correct =
1700 caller()->audio_frames_received() >=
1701 total_caller_audio_frames_expected &&
1702 caller()->min_video_frames_received_per_track() >=
1703 total_caller_video_frames_expected &&
1704 callee()->audio_frames_received() >=
1705 total_callee_audio_frames_expected &&
1706 callee()->min_video_frames_received_per_track() >=
1707 total_callee_video_frames_expected;
1708
1709 // After the combined wait, print out a more detailed message upon
1710 // failure.
1711 EXPECT_GE(caller()->audio_frames_received(),
1712 total_caller_audio_frames_expected);
1713 EXPECT_GE(caller()->min_video_frames_received_per_track(),
1714 total_caller_video_frames_expected);
1715 EXPECT_GE(callee()->audio_frames_received(),
1716 total_callee_audio_frames_expected);
1717 EXPECT_GE(callee()->min_video_frames_received_per_track(),
1718 total_callee_video_frames_expected);
1719
1720 // We want to make sure nothing unexpected was received.
1721 if (media_expectations.caller_audio_expectation_ ==
1722 MediaExpectations::kExpectNoFrames) {
1723 EXPECT_EQ(caller()->audio_frames_received(),
1724 total_caller_audio_frames_expected);
1725 if (caller()->audio_frames_received() !=
1726 total_caller_audio_frames_expected) {
1727 expectations_correct = false;
1728 }
1729 }
1730 if (media_expectations.caller_video_expectation_ ==
1731 MediaExpectations::kExpectNoFrames) {
1732 EXPECT_EQ(caller()->min_video_frames_received_per_track(),
1733 total_caller_video_frames_expected);
1734 if (caller()->min_video_frames_received_per_track() !=
1735 total_caller_video_frames_expected) {
1736 expectations_correct = false;
1737 }
1738 }
1739 if (media_expectations.callee_audio_expectation_ ==
1740 MediaExpectations::kExpectNoFrames) {
1741 EXPECT_EQ(callee()->audio_frames_received(),
1742 total_callee_audio_frames_expected);
1743 if (callee()->audio_frames_received() !=
1744 total_callee_audio_frames_expected) {
1745 expectations_correct = false;
1746 }
1747 }
1748 if (media_expectations.callee_video_expectation_ ==
1749 MediaExpectations::kExpectNoFrames) {
1750 EXPECT_EQ(callee()->min_video_frames_received_per_track(),
1751 total_callee_video_frames_expected);
1752 if (callee()->min_video_frames_received_per_track() !=
1753 total_callee_video_frames_expected) {
1754 expectations_correct = false;
1755 }
1756 }
1757 return expectations_correct;
1758 }
1759
1760 void ClosePeerConnections() {
1761 caller()->pc()->Close();
1762 callee()->pc()->Close();
1763 }
1764
1765 void TestNegotiatedCipherSuite(
1766 const PeerConnectionFactory::Options& caller_options,
1767 const PeerConnectionFactory::Options& callee_options,
1768 int expected_cipher_suite) {
1769 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(caller_options,
1770 callee_options));
1771 ConnectFakeSignaling();
1772 caller()->AddAudioVideoTracks();
1773 callee()->AddAudioVideoTracks();
1774 caller()->CreateAndSetAndSignalOffer();
1775 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
1776 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(expected_cipher_suite),
1777 caller()->OldGetStats()->SrtpCipher(), kDefaultTimeout);
1778 // TODO(bugs.webrtc.org/9456): Fix it.
1779 EXPECT_METRIC_EQ(1, webrtc::metrics::NumEvents(
1780 "WebRTC.PeerConnection.SrtpCryptoSuite.Audio",
1781 expected_cipher_suite));
1782 }
1783
1784 void TestGcmNegotiationUsesCipherSuite(bool local_gcm_enabled,
1785 bool remote_gcm_enabled,
1786 bool aes_ctr_enabled,
1787 int expected_cipher_suite) {
1788 PeerConnectionFactory::Options caller_options;
1789 caller_options.crypto_options.srtp.enable_gcm_crypto_suites =
1790 local_gcm_enabled;
1791 caller_options.crypto_options.srtp.enable_aes128_sha1_80_crypto_cipher =
1792 aes_ctr_enabled;
1793 PeerConnectionFactory::Options callee_options;
1794 callee_options.crypto_options.srtp.enable_gcm_crypto_suites =
1795 remote_gcm_enabled;
1796 callee_options.crypto_options.srtp.enable_aes128_sha1_80_crypto_cipher =
1797 aes_ctr_enabled;
1798 TestNegotiatedCipherSuite(caller_options, callee_options,
1799 expected_cipher_suite);
1800 }
1801
1802 protected:
1803 SdpSemantics sdp_semantics_;
1804
1805 private:
1806 // |ss_| is used by |network_thread_| so it must be destroyed later.
1807 std::unique_ptr<rtc::VirtualSocketServer> ss_;
1808 std::unique_ptr<rtc::FirewallSocketServer> fss_;
1809 // |network_thread_| and |worker_thread_| are used by both
1810 // |caller_| and |callee_| so they must be destroyed
1811 // later.
1812 std::unique_ptr<rtc::Thread> network_thread_;
1813 std::unique_ptr<rtc::Thread> worker_thread_;
1814 // The turn servers and turn customizers should be accessed & deleted on the
1815 // network thread to avoid a race with the socket read/write that occurs
1816 // on the network thread.
1817 std::vector<std::unique_ptr<cricket::TestTurnServer>> turn_servers_;
1818 std::vector<std::unique_ptr<cricket::TestTurnCustomizer>> turn_customizers_;
1819 std::unique_ptr<PeerConnectionWrapper> caller_;
1820 std::unique_ptr<PeerConnectionWrapper> callee_;
1821};
1822
Seth Hampson2f0d7022018-02-20 11:54:42 -08001823class PeerConnectionIntegrationTest
1824 : public PeerConnectionIntegrationBaseTest,
1825 public ::testing::WithParamInterface<SdpSemantics> {
1826 protected:
1827 PeerConnectionIntegrationTest()
1828 : PeerConnectionIntegrationBaseTest(GetParam()) {}
1829};
1830
Yves Gerey100fe632020-01-17 19:15:53 +01001831// Fake clock must be set before threads are started to prevent race on
1832// Set/GetClockForTesting().
1833// To achieve that, multiple inheritance is used as a mixin pattern
1834// where order of construction is finely controlled.
1835// This also ensures peerconnection is closed before switching back to non-fake
1836// clock, avoiding other races and DCHECK failures such as in rtp_sender.cc.
1837class FakeClockForTest : public rtc::ScopedFakeClock {
1838 protected:
1839 FakeClockForTest() {
1840 // Some things use a time of "0" as a special value, so we need to start out
1841 // the fake clock at a nonzero time.
1842 // TODO(deadbeef): Fix this.
Danil Chapovalov0c626af2020-02-10 11:16:00 +01001843 AdvanceTime(webrtc::TimeDelta::Seconds(1));
Yves Gerey100fe632020-01-17 19:15:53 +01001844 }
1845
1846 // Explicit handle.
1847 ScopedFakeClock& FakeClock() { return *this; }
1848};
1849
1850// Ensure FakeClockForTest is constructed first (see class for rationale).
1851class PeerConnectionIntegrationTestWithFakeClock
1852 : public FakeClockForTest,
1853 public PeerConnectionIntegrationTest {};
1854
Seth Hampson2f0d7022018-02-20 11:54:42 -08001855class PeerConnectionIntegrationTestPlanB
1856 : public PeerConnectionIntegrationBaseTest {
1857 protected:
1858 PeerConnectionIntegrationTestPlanB()
1859 : PeerConnectionIntegrationBaseTest(SdpSemantics::kPlanB) {}
1860};
1861
1862class PeerConnectionIntegrationTestUnifiedPlan
1863 : public PeerConnectionIntegrationBaseTest {
1864 protected:
1865 PeerConnectionIntegrationTestUnifiedPlan()
1866 : PeerConnectionIntegrationBaseTest(SdpSemantics::kUnifiedPlan) {}
1867};
1868
deadbeef1dcb1642017-03-29 21:08:16 -07001869// Test the OnFirstPacketReceived callback from audio/video RtpReceivers. This
1870// includes testing that the callback is invoked if an observer is connected
1871// after the first packet has already been received.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001872TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07001873 RtpReceiverObserverOnFirstPacketReceived) {
1874 ASSERT_TRUE(CreatePeerConnectionWrappers());
1875 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001876 caller()->AddAudioVideoTracks();
1877 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001878 // Start offer/answer exchange and wait for it to complete.
1879 caller()->CreateAndSetAndSignalOffer();
1880 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1881 // Should be one receiver each for audio/video.
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02001882 EXPECT_EQ(2U, caller()->rtp_receiver_observers().size());
1883 EXPECT_EQ(2U, callee()->rtp_receiver_observers().size());
deadbeef1dcb1642017-03-29 21:08:16 -07001884 // Wait for all "first packet received" callbacks to be fired.
1885 EXPECT_TRUE_WAIT(
Steve Anton64b626b2019-01-28 17:25:26 -08001886 absl::c_all_of(caller()->rtp_receiver_observers(),
1887 [](const std::unique_ptr<MockRtpReceiverObserver>& o) {
1888 return o->first_packet_received();
1889 }),
deadbeef1dcb1642017-03-29 21:08:16 -07001890 kMaxWaitForFramesMs);
1891 EXPECT_TRUE_WAIT(
Steve Anton64b626b2019-01-28 17:25:26 -08001892 absl::c_all_of(callee()->rtp_receiver_observers(),
1893 [](const std::unique_ptr<MockRtpReceiverObserver>& o) {
1894 return o->first_packet_received();
1895 }),
deadbeef1dcb1642017-03-29 21:08:16 -07001896 kMaxWaitForFramesMs);
1897 // If new observers are set after the first packet was already received, the
1898 // callback should still be invoked.
1899 caller()->ResetRtpReceiverObservers();
1900 callee()->ResetRtpReceiverObservers();
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02001901 EXPECT_EQ(2U, caller()->rtp_receiver_observers().size());
1902 EXPECT_EQ(2U, callee()->rtp_receiver_observers().size());
deadbeef1dcb1642017-03-29 21:08:16 -07001903 EXPECT_TRUE(
Steve Anton64b626b2019-01-28 17:25:26 -08001904 absl::c_all_of(caller()->rtp_receiver_observers(),
1905 [](const std::unique_ptr<MockRtpReceiverObserver>& o) {
1906 return o->first_packet_received();
1907 }));
deadbeef1dcb1642017-03-29 21:08:16 -07001908 EXPECT_TRUE(
Steve Anton64b626b2019-01-28 17:25:26 -08001909 absl::c_all_of(callee()->rtp_receiver_observers(),
1910 [](const std::unique_ptr<MockRtpReceiverObserver>& o) {
1911 return o->first_packet_received();
1912 }));
deadbeef1dcb1642017-03-29 21:08:16 -07001913}
1914
1915class DummyDtmfObserver : public DtmfSenderObserverInterface {
1916 public:
1917 DummyDtmfObserver() : completed_(false) {}
1918
1919 // Implements DtmfSenderObserverInterface.
1920 void OnToneChange(const std::string& tone) override {
1921 tones_.push_back(tone);
1922 if (tone.empty()) {
1923 completed_ = true;
1924 }
1925 }
1926
1927 const std::vector<std::string>& tones() const { return tones_; }
1928 bool completed() const { return completed_; }
1929
1930 private:
1931 bool completed_;
1932 std::vector<std::string> tones_;
1933};
1934
1935// Assumes |sender| already has an audio track added and the offer/answer
1936// exchange is done.
Harald Alvestrand89c40e22021-02-17 08:58:35 +00001937void TestDtmfFromSenderToReceiver(PeerConnectionWrapper* sender,
1938 PeerConnectionWrapper* receiver) {
Steve Anton15324772018-01-16 10:26:49 -08001939 // We should be able to get a DTMF sender from the local sender.
1940 rtc::scoped_refptr<DtmfSenderInterface> dtmf_sender =
1941 sender->pc()->GetSenders().at(0)->GetDtmfSender();
1942 ASSERT_TRUE(dtmf_sender);
deadbeef1dcb1642017-03-29 21:08:16 -07001943 DummyDtmfObserver observer;
deadbeef1dcb1642017-03-29 21:08:16 -07001944 dtmf_sender->RegisterObserver(&observer);
1945
1946 // Test the DtmfSender object just created.
1947 EXPECT_TRUE(dtmf_sender->CanInsertDtmf());
1948 EXPECT_TRUE(dtmf_sender->InsertDtmf("1a", 100, 50));
1949
1950 EXPECT_TRUE_WAIT(observer.completed(), kDefaultTimeout);
1951 std::vector<std::string> tones = {"1", "a", ""};
1952 EXPECT_EQ(tones, observer.tones());
1953 dtmf_sender->UnregisterObserver();
1954 // TODO(deadbeef): Verify the tones were actually received end-to-end.
1955}
1956
1957// Verifies the DtmfSenderObserver callbacks for a DtmfSender (one in each
1958// direction).
Seth Hampson2f0d7022018-02-20 11:54:42 -08001959TEST_P(PeerConnectionIntegrationTest, DtmfSenderObserver) {
deadbeef1dcb1642017-03-29 21:08:16 -07001960 ASSERT_TRUE(CreatePeerConnectionWrappers());
1961 ConnectFakeSignaling();
1962 // Only need audio for DTMF.
Steve Anton15324772018-01-16 10:26:49 -08001963 caller()->AddAudioTrack();
1964 callee()->AddAudioTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07001965 caller()->CreateAndSetAndSignalOffer();
1966 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
deadbeef71452802017-05-07 17:21:01 -07001967 // DTLS must finish before the DTMF sender can be used reliably.
1968 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07001969 TestDtmfFromSenderToReceiver(caller(), callee());
1970 TestDtmfFromSenderToReceiver(callee(), caller());
1971}
1972
1973// Basic end-to-end test, verifying media can be encoded/transmitted/decoded
1974// between two connections, using DTLS-SRTP.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001975TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithDtls) {
deadbeef1dcb1642017-03-29 21:08:16 -07001976 ASSERT_TRUE(CreatePeerConnectionWrappers());
1977 ConnectFakeSignaling();
Harald Alvestrand194939b2018-01-24 16:04:13 +01001978
deadbeef1dcb1642017-03-29 21:08:16 -07001979 // Do normal offer/answer and wait for some frames to be received in each
1980 // direction.
Steve Anton15324772018-01-16 10:26:49 -08001981 caller()->AddAudioVideoTracks();
1982 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001983 caller()->CreateAndSetAndSignalOffer();
1984 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001985 MediaExpectations media_expectations;
1986 media_expectations.ExpectBidirectionalAudioAndVideo();
1987 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Ying Wangef3998f2019-12-09 13:06:53 +01001988 EXPECT_METRIC_LE(
1989 2, webrtc::metrics::NumEvents("WebRTC.PeerConnection.KeyProtocol",
1990 webrtc::kEnumCounterKeyProtocolDtls));
1991 EXPECT_METRIC_EQ(
1992 0, webrtc::metrics::NumEvents("WebRTC.PeerConnection.KeyProtocol",
1993 webrtc::kEnumCounterKeyProtocolSdes));
deadbeef1dcb1642017-03-29 21:08:16 -07001994}
1995
1996// Uses SDES instead of DTLS for key agreement.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001997TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithSdes) {
deadbeef1dcb1642017-03-29 21:08:16 -07001998 PeerConnectionInterface::RTCConfiguration sdes_config;
1999 sdes_config.enable_dtls_srtp.emplace(false);
2000 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(sdes_config, sdes_config));
2001 ConnectFakeSignaling();
2002
2003 // Do normal offer/answer and wait for some frames to be received in each
2004 // direction.
Steve Anton15324772018-01-16 10:26:49 -08002005 caller()->AddAudioVideoTracks();
2006 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002007 caller()->CreateAndSetAndSignalOffer();
2008 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002009 MediaExpectations media_expectations;
2010 media_expectations.ExpectBidirectionalAudioAndVideo();
2011 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Ying Wangef3998f2019-12-09 13:06:53 +01002012 EXPECT_METRIC_LE(
2013 2, webrtc::metrics::NumEvents("WebRTC.PeerConnection.KeyProtocol",
2014 webrtc::kEnumCounterKeyProtocolSdes));
2015 EXPECT_METRIC_EQ(
2016 0, webrtc::metrics::NumEvents("WebRTC.PeerConnection.KeyProtocol",
2017 webrtc::kEnumCounterKeyProtocolDtls));
deadbeef1dcb1642017-03-29 21:08:16 -07002018}
2019
Steve Anton9a44b2d2019-07-12 12:58:30 -07002020// Basic end-to-end test specifying the |enable_encrypted_rtp_header_extensions|
2021// option to offer encrypted versions of all header extensions alongside the
2022// unencrypted versions.
2023TEST_P(PeerConnectionIntegrationTest,
2024 EndToEndCallWithEncryptedRtpHeaderExtensions) {
2025 CryptoOptions crypto_options;
2026 crypto_options.srtp.enable_encrypted_rtp_header_extensions = true;
2027 PeerConnectionInterface::RTCConfiguration config;
2028 config.crypto_options = crypto_options;
2029 // Note: This allows offering >14 RTP header extensions.
2030 config.offer_extmap_allow_mixed = true;
2031 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
2032 ConnectFakeSignaling();
2033
2034 // Do normal offer/answer and wait for some frames to be received in each
2035 // direction.
2036 caller()->AddAudioVideoTracks();
2037 callee()->AddAudioVideoTracks();
2038 caller()->CreateAndSetAndSignalOffer();
2039 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2040 MediaExpectations media_expectations;
2041 media_expectations.ExpectBidirectionalAudioAndVideo();
2042 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2043}
2044
deadbeef1dcb1642017-03-29 21:08:16 -07002045// This test sets up a call between two parties with a source resolution of
2046// 1280x720 and verifies that a 16:9 aspect ratio is received.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002047TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07002048 Send1280By720ResolutionAndReceive16To9AspectRatio) {
2049 ASSERT_TRUE(CreatePeerConnectionWrappers());
2050 ConnectFakeSignaling();
2051
Niels Möller5c7efe72018-05-11 10:34:46 +02002052 // Add video tracks with 16:9 aspect ratio, size 1280 x 720.
2053 webrtc::FakePeriodicVideoSource::Config config;
2054 config.width = 1280;
2055 config.height = 720;
Johannes Kron965e7942018-09-13 15:36:20 +02002056 config.timestamp_offset_ms = rtc::TimeMillis();
Niels Möller5c7efe72018-05-11 10:34:46 +02002057 caller()->AddTrack(caller()->CreateLocalVideoTrackWithConfig(config));
2058 callee()->AddTrack(callee()->CreateLocalVideoTrackWithConfig(config));
deadbeef1dcb1642017-03-29 21:08:16 -07002059
2060 // Do normal offer/answer and wait for at least one frame to be received in
2061 // each direction.
2062 caller()->CreateAndSetAndSignalOffer();
2063 ASSERT_TRUE_WAIT(caller()->min_video_frames_received_per_track() > 0 &&
2064 callee()->min_video_frames_received_per_track() > 0,
2065 kMaxWaitForFramesMs);
2066
2067 // Check rendered aspect ratio.
2068 EXPECT_EQ(16.0 / 9, caller()->local_rendered_aspect_ratio());
2069 EXPECT_EQ(16.0 / 9, caller()->rendered_aspect_ratio());
2070 EXPECT_EQ(16.0 / 9, callee()->local_rendered_aspect_ratio());
2071 EXPECT_EQ(16.0 / 9, callee()->rendered_aspect_ratio());
2072}
2073
2074// This test sets up an one-way call, with media only from caller to
2075// callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002076TEST_P(PeerConnectionIntegrationTest, OneWayMediaCall) {
deadbeef1dcb1642017-03-29 21:08:16 -07002077 ASSERT_TRUE(CreatePeerConnectionWrappers());
2078 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002079 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002080 caller()->CreateAndSetAndSignalOffer();
Seth Hampson2f0d7022018-02-20 11:54:42 -08002081 MediaExpectations media_expectations;
2082 media_expectations.CalleeExpectsSomeAudioAndVideo();
2083 media_expectations.CallerExpectsNoAudio();
2084 media_expectations.CallerExpectsNoVideo();
2085 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002086}
2087
Johannes Kron3e983682020-03-29 22:17:00 +02002088// Tests that send only works without the caller having a decoder factory and
2089// the callee having an encoder factory.
2090TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithSendOnlyVideo) {
2091 ASSERT_TRUE(
2092 CreateOneDirectionalPeerConnectionWrappers(/*caller_to_callee=*/true));
2093 ConnectFakeSignaling();
2094 // Add one-directional video, from caller to callee.
2095 rtc::scoped_refptr<webrtc::VideoTrackInterface> caller_track =
2096 caller()->CreateLocalVideoTrack();
2097 caller()->AddTrack(caller_track);
2098 PeerConnectionInterface::RTCOfferAnswerOptions options;
2099 options.offer_to_receive_video = 0;
2100 caller()->SetOfferAnswerOptions(options);
2101 caller()->CreateAndSetAndSignalOffer();
2102 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2103 ASSERT_EQ(callee()->pc()->GetReceivers().size(), 1u);
2104
2105 // Expect video to be received in one direction.
2106 MediaExpectations media_expectations;
2107 media_expectations.CallerExpectsNoVideo();
2108 media_expectations.CalleeExpectsSomeVideo();
2109
2110 EXPECT_TRUE(ExpectNewFrames(media_expectations));
2111}
2112
2113// Tests that receive only works without the caller having an encoder factory
2114// and the callee having a decoder factory.
2115TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithReceiveOnlyVideo) {
2116 ASSERT_TRUE(
2117 CreateOneDirectionalPeerConnectionWrappers(/*caller_to_callee=*/false));
2118 ConnectFakeSignaling();
2119 // Add one-directional video, from callee to caller.
2120 rtc::scoped_refptr<webrtc::VideoTrackInterface> callee_track =
2121 callee()->CreateLocalVideoTrack();
2122 callee()->AddTrack(callee_track);
2123 PeerConnectionInterface::RTCOfferAnswerOptions options;
2124 options.offer_to_receive_video = 1;
2125 caller()->SetOfferAnswerOptions(options);
2126 caller()->CreateAndSetAndSignalOffer();
2127 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2128 ASSERT_EQ(caller()->pc()->GetReceivers().size(), 1u);
2129
2130 // Expect video to be received in one direction.
2131 MediaExpectations media_expectations;
2132 media_expectations.CallerExpectsSomeVideo();
2133 media_expectations.CalleeExpectsNoVideo();
2134
2135 EXPECT_TRUE(ExpectNewFrames(media_expectations));
2136}
2137
2138TEST_P(PeerConnectionIntegrationTest,
2139 EndToEndCallAddReceiveVideoToSendOnlyCall) {
2140 ASSERT_TRUE(CreatePeerConnectionWrappers());
2141 ConnectFakeSignaling();
2142 // Add one-directional video, from caller to callee.
2143 rtc::scoped_refptr<webrtc::VideoTrackInterface> caller_track =
2144 caller()->CreateLocalVideoTrack();
2145 caller()->AddTrack(caller_track);
2146 caller()->CreateAndSetAndSignalOffer();
2147 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2148
2149 // Add receive video.
2150 rtc::scoped_refptr<webrtc::VideoTrackInterface> callee_track =
2151 callee()->CreateLocalVideoTrack();
2152 callee()->AddTrack(callee_track);
2153 caller()->CreateAndSetAndSignalOffer();
2154 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2155
2156 // Ensure that video frames are received end-to-end.
2157 MediaExpectations media_expectations;
2158 media_expectations.ExpectBidirectionalVideo();
2159 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2160}
2161
2162TEST_P(PeerConnectionIntegrationTest,
2163 EndToEndCallAddSendVideoToReceiveOnlyCall) {
2164 ASSERT_TRUE(CreatePeerConnectionWrappers());
2165 ConnectFakeSignaling();
2166 // Add one-directional video, from callee to caller.
2167 rtc::scoped_refptr<webrtc::VideoTrackInterface> callee_track =
2168 callee()->CreateLocalVideoTrack();
2169 callee()->AddTrack(callee_track);
2170 caller()->CreateAndSetAndSignalOffer();
2171 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2172
2173 // Add send video.
2174 rtc::scoped_refptr<webrtc::VideoTrackInterface> caller_track =
2175 caller()->CreateLocalVideoTrack();
2176 caller()->AddTrack(caller_track);
2177 caller()->CreateAndSetAndSignalOffer();
2178 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2179
2180 // Expect video to be received in one direction.
2181 MediaExpectations media_expectations;
2182 media_expectations.ExpectBidirectionalVideo();
2183 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2184}
2185
2186TEST_P(PeerConnectionIntegrationTest,
2187 EndToEndCallRemoveReceiveVideoFromSendReceiveCall) {
2188 ASSERT_TRUE(CreatePeerConnectionWrappers());
2189 ConnectFakeSignaling();
2190 // Add send video, from caller to callee.
2191 rtc::scoped_refptr<webrtc::VideoTrackInterface> caller_track =
2192 caller()->CreateLocalVideoTrack();
2193 rtc::scoped_refptr<webrtc::RtpSenderInterface> caller_sender =
2194 caller()->AddTrack(caller_track);
2195 // Add receive video, from callee to caller.
2196 rtc::scoped_refptr<webrtc::VideoTrackInterface> callee_track =
2197 callee()->CreateLocalVideoTrack();
2198
2199 rtc::scoped_refptr<webrtc::RtpSenderInterface> callee_sender =
2200 callee()->AddTrack(callee_track);
2201 caller()->CreateAndSetAndSignalOffer();
2202 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2203
2204 // Remove receive video (i.e., callee sender track).
2205 callee()->pc()->RemoveTrack(callee_sender);
2206
2207 caller()->CreateAndSetAndSignalOffer();
2208 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2209
2210 // Expect one-directional video.
2211 MediaExpectations media_expectations;
2212 media_expectations.CallerExpectsNoVideo();
2213 media_expectations.CalleeExpectsSomeVideo();
2214
2215 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2216}
2217
2218TEST_P(PeerConnectionIntegrationTest,
2219 EndToEndCallRemoveSendVideoFromSendReceiveCall) {
2220 ASSERT_TRUE(CreatePeerConnectionWrappers());
2221 ConnectFakeSignaling();
2222 // Add send video, from caller to callee.
2223 rtc::scoped_refptr<webrtc::VideoTrackInterface> caller_track =
2224 caller()->CreateLocalVideoTrack();
2225 rtc::scoped_refptr<webrtc::RtpSenderInterface> caller_sender =
2226 caller()->AddTrack(caller_track);
2227 // Add receive video, from callee to caller.
2228 rtc::scoped_refptr<webrtc::VideoTrackInterface> callee_track =
2229 callee()->CreateLocalVideoTrack();
2230
2231 rtc::scoped_refptr<webrtc::RtpSenderInterface> callee_sender =
2232 callee()->AddTrack(callee_track);
2233 caller()->CreateAndSetAndSignalOffer();
2234 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2235
2236 // Remove send video (i.e., caller sender track).
2237 caller()->pc()->RemoveTrack(caller_sender);
2238
2239 caller()->CreateAndSetAndSignalOffer();
2240 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2241
2242 // Expect one-directional video.
2243 MediaExpectations media_expectations;
2244 media_expectations.CalleeExpectsNoVideo();
2245 media_expectations.CallerExpectsSomeVideo();
2246
2247 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2248}
2249
deadbeef1dcb1642017-03-29 21:08:16 -07002250// This test sets up a audio call initially, with the callee rejecting video
2251// initially. Then later the callee decides to upgrade to audio/video, and
2252// initiates a new offer/answer exchange.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002253TEST_P(PeerConnectionIntegrationTest, AudioToVideoUpgrade) {
deadbeef1dcb1642017-03-29 21:08:16 -07002254 ASSERT_TRUE(CreatePeerConnectionWrappers());
2255 ConnectFakeSignaling();
2256 // Initially, offer an audio/video stream from the caller, but refuse to
2257 // send/receive video on the callee side.
Steve Anton15324772018-01-16 10:26:49 -08002258 caller()->AddAudioVideoTracks();
2259 callee()->AddAudioTrack();
Seth Hampson2f0d7022018-02-20 11:54:42 -08002260 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2261 PeerConnectionInterface::RTCOfferAnswerOptions options;
2262 options.offer_to_receive_video = 0;
2263 callee()->SetOfferAnswerOptions(options);
2264 } else {
2265 callee()->SetRemoteOfferHandler([this] {
Harald Alvestrand6060df52020-08-11 09:54:02 +02002266 callee()
2267 ->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)
2268 ->StopInternal();
Seth Hampson2f0d7022018-02-20 11:54:42 -08002269 });
2270 }
deadbeef1dcb1642017-03-29 21:08:16 -07002271 // Do offer/answer and make sure audio is still received end-to-end.
2272 caller()->CreateAndSetAndSignalOffer();
2273 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002274 {
2275 MediaExpectations media_expectations;
2276 media_expectations.ExpectBidirectionalAudio();
2277 media_expectations.ExpectNoVideo();
2278 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2279 }
deadbeef1dcb1642017-03-29 21:08:16 -07002280 // Sanity check that the callee's description has a rejected video section.
2281 ASSERT_NE(nullptr, callee()->pc()->local_description());
2282 const ContentInfo* callee_video_content =
2283 GetFirstVideoContent(callee()->pc()->local_description()->description());
2284 ASSERT_NE(nullptr, callee_video_content);
2285 EXPECT_TRUE(callee_video_content->rejected);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002286
deadbeef1dcb1642017-03-29 21:08:16 -07002287 // Now negotiate with video and ensure negotiation succeeds, with video
2288 // frames and additional audio frames being received.
Steve Anton15324772018-01-16 10:26:49 -08002289 callee()->AddVideoTrack();
Seth Hampson2f0d7022018-02-20 11:54:42 -08002290 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2291 PeerConnectionInterface::RTCOfferAnswerOptions options;
2292 options.offer_to_receive_video = 1;
2293 callee()->SetOfferAnswerOptions(options);
2294 } else {
2295 callee()->SetRemoteOfferHandler(nullptr);
2296 caller()->SetRemoteOfferHandler([this] {
2297 // The caller creates a new transceiver to receive video on when receiving
2298 // the offer, but by default it is send only.
2299 auto transceivers = caller()->pc()->GetTransceivers();
Harald Alvestrand6060df52020-08-11 09:54:02 +02002300 ASSERT_EQ(2U, transceivers.size());
Seth Hampson2f0d7022018-02-20 11:54:42 -08002301 ASSERT_EQ(cricket::MEDIA_TYPE_VIDEO,
Harald Alvestrand6060df52020-08-11 09:54:02 +02002302 transceivers[1]->receiver()->media_type());
2303 transceivers[1]->sender()->SetTrack(caller()->CreateLocalVideoTrack());
2304 transceivers[1]->SetDirectionWithError(
2305 RtpTransceiverDirection::kSendRecv);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002306 });
2307 }
deadbeef1dcb1642017-03-29 21:08:16 -07002308 callee()->CreateAndSetAndSignalOffer();
2309 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002310 {
2311 // Expect additional audio frames to be received after the upgrade.
2312 MediaExpectations media_expectations;
2313 media_expectations.ExpectBidirectionalAudioAndVideo();
2314 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2315 }
deadbeef1dcb1642017-03-29 21:08:16 -07002316}
2317
deadbeef4389b4d2017-09-07 09:07:36 -07002318// Simpler than the above test; just add an audio track to an established
2319// video-only connection.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002320TEST_P(PeerConnectionIntegrationTest, AddAudioToVideoOnlyCall) {
deadbeef4389b4d2017-09-07 09:07:36 -07002321 ASSERT_TRUE(CreatePeerConnectionWrappers());
2322 ConnectFakeSignaling();
2323 // Do initial offer/answer with just a video track.
Steve Anton15324772018-01-16 10:26:49 -08002324 caller()->AddVideoTrack();
2325 callee()->AddVideoTrack();
deadbeef4389b4d2017-09-07 09:07:36 -07002326 caller()->CreateAndSetAndSignalOffer();
2327 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2328 // Now add an audio track and do another offer/answer.
Steve Anton15324772018-01-16 10:26:49 -08002329 caller()->AddAudioTrack();
2330 callee()->AddAudioTrack();
deadbeef4389b4d2017-09-07 09:07:36 -07002331 caller()->CreateAndSetAndSignalOffer();
2332 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2333 // Ensure both audio and video frames are received end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002334 MediaExpectations media_expectations;
2335 media_expectations.ExpectBidirectionalAudioAndVideo();
2336 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef4389b4d2017-09-07 09:07:36 -07002337}
2338
deadbeef1dcb1642017-03-29 21:08:16 -07002339// This test sets up a call that's transferred to a new caller with a different
2340// DTLS fingerprint.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002341TEST_P(PeerConnectionIntegrationTest, CallTransferredForCallee) {
deadbeef1dcb1642017-03-29 21:08:16 -07002342 ASSERT_TRUE(CreatePeerConnectionWrappers());
2343 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002344 caller()->AddAudioVideoTracks();
2345 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002346 caller()->CreateAndSetAndSignalOffer();
2347 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2348
2349 // Keep the original peer around which will still send packets to the
2350 // receiving client. These SRTP packets will be dropped.
Harald Alvestrand89c40e22021-02-17 08:58:35 +00002351 std::unique_ptr<PeerConnectionWrapper> original_peer(
deadbeef1dcb1642017-03-29 21:08:16 -07002352 SetCallerPcWrapperAndReturnCurrent(
Seth Hampson2f0d7022018-02-20 11:54:42 -08002353 CreatePeerConnectionWrapperWithAlternateKey().release()));
deadbeef1dcb1642017-03-29 21:08:16 -07002354 // TODO(deadbeef): Why do we call Close here? That goes against the comment
2355 // directly above.
2356 original_peer->pc()->Close();
2357
2358 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002359 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002360 caller()->CreateAndSetAndSignalOffer();
2361 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2362 // Wait for some additional frames to be transmitted end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002363 MediaExpectations media_expectations;
2364 media_expectations.ExpectBidirectionalAudioAndVideo();
2365 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002366}
2367
2368// This test sets up a call that's transferred to a new callee with a different
2369// DTLS fingerprint.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002370TEST_P(PeerConnectionIntegrationTest, CallTransferredForCaller) {
deadbeef1dcb1642017-03-29 21:08:16 -07002371 ASSERT_TRUE(CreatePeerConnectionWrappers());
2372 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002373 caller()->AddAudioVideoTracks();
2374 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002375 caller()->CreateAndSetAndSignalOffer();
2376 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2377
2378 // Keep the original peer around which will still send packets to the
2379 // receiving client. These SRTP packets will be dropped.
Harald Alvestrand89c40e22021-02-17 08:58:35 +00002380 std::unique_ptr<PeerConnectionWrapper> original_peer(
deadbeef1dcb1642017-03-29 21:08:16 -07002381 SetCalleePcWrapperAndReturnCurrent(
Seth Hampson2f0d7022018-02-20 11:54:42 -08002382 CreatePeerConnectionWrapperWithAlternateKey().release()));
deadbeef1dcb1642017-03-29 21:08:16 -07002383 // TODO(deadbeef): Why do we call Close here? That goes against the comment
2384 // directly above.
2385 original_peer->pc()->Close();
2386
2387 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002388 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002389 caller()->SetOfferAnswerOptions(IceRestartOfferAnswerOptions());
2390 caller()->CreateAndSetAndSignalOffer();
2391 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2392 // Wait for some additional frames to be transmitted end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002393 MediaExpectations media_expectations;
2394 media_expectations.ExpectBidirectionalAudioAndVideo();
2395 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002396}
2397
Harald Alvestrand89c40e22021-02-17 08:58:35 +00002398#ifdef WEBRTC_HAVE_SCTP
2399
2400// This test causes a PeerConnection to enter Disconnected state, and
2401// sends data on a DataChannel while disconnected.
2402// The data should be surfaced when the connection reestablishes.
2403TEST_P(PeerConnectionIntegrationTest, DataChannelWhileDisconnected) {
2404 CreatePeerConnectionWrappers();
2405 ConnectFakeSignaling();
2406 caller()->CreateDataChannel();
2407 caller()->CreateAndSetAndSignalOffer();
2408 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2409 ASSERT_TRUE_WAIT(callee()->data_observer(), kDefaultTimeout);
2410 std::string data1 = "hello first";
2411 caller()->data_channel()->Send(DataBuffer(data1));
2412 EXPECT_EQ_WAIT(data1, callee()->data_observer()->last_message(),
2413 kDefaultTimeout);
2414 // Cause a network outage
2415 virtual_socket_server()->set_drop_probability(1.0);
2416 EXPECT_EQ_WAIT(PeerConnectionInterface::kIceConnectionDisconnected,
2417 caller()->standardized_ice_connection_state(),
2418 kDefaultTimeout);
2419 std::string data2 = "hello second";
2420 caller()->data_channel()->Send(DataBuffer(data2));
2421 // Remove the network outage. The connection should reestablish.
2422 virtual_socket_server()->set_drop_probability(0.0);
2423 EXPECT_EQ_WAIT(data2, callee()->data_observer()->last_message(),
2424 kDefaultTimeout);
2425}
2426
2427// This test causes a PeerConnection to enter Disconnected state,
2428// sends data on a DataChannel while disconnected, and then triggers
2429// an ICE restart.
2430// The data should be surfaced when the connection reestablishes.
2431TEST_P(PeerConnectionIntegrationTest, DataChannelWhileDisconnectedIceRestart) {
2432 CreatePeerConnectionWrappers();
2433 ConnectFakeSignaling();
2434 caller()->CreateDataChannel();
2435 caller()->CreateAndSetAndSignalOffer();
2436 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2437 ASSERT_TRUE_WAIT(callee()->data_observer(), kDefaultTimeout);
2438 std::string data1 = "hello first";
2439 caller()->data_channel()->Send(DataBuffer(data1));
2440 EXPECT_EQ_WAIT(data1, callee()->data_observer()->last_message(),
2441 kDefaultTimeout);
2442 // Cause a network outage
2443 virtual_socket_server()->set_drop_probability(1.0);
2444 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceConnectionDisconnected,
2445 caller()->standardized_ice_connection_state(),
2446 kDefaultTimeout);
2447 std::string data2 = "hello second";
2448 caller()->data_channel()->Send(DataBuffer(data2));
2449
2450 // Trigger an ICE restart. The signaling channel is not affected by
2451 // the network outage.
2452 caller()->SetOfferAnswerOptions(IceRestartOfferAnswerOptions());
2453 caller()->CreateAndSetAndSignalOffer();
2454 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2455 // Remove the network outage. The connection should reestablish.
2456 virtual_socket_server()->set_drop_probability(0.0);
2457 EXPECT_EQ_WAIT(data2, callee()->data_observer()->last_message(),
2458 kDefaultTimeout);
2459}
2460
2461#endif // WEBRTC_HAVE_SCTP
2462
deadbeef1dcb1642017-03-29 21:08:16 -07002463// This test sets up a non-bundled call and negotiates bundling at the same
2464// time as starting an ICE restart. When bundling is in effect in the restart,
2465// the DTLS-SRTP context should be successfully reset.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002466TEST_P(PeerConnectionIntegrationTest, BundlingEnabledWhileIceRestartOccurs) {
deadbeef1dcb1642017-03-29 21:08:16 -07002467 ASSERT_TRUE(CreatePeerConnectionWrappers());
2468 ConnectFakeSignaling();
2469
Steve Anton15324772018-01-16 10:26:49 -08002470 caller()->AddAudioVideoTracks();
2471 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002472 // Remove the bundle group from the SDP received by the callee.
2473 callee()->SetReceivedSdpMunger([](cricket::SessionDescription* desc) {
2474 desc->RemoveGroupByName("BUNDLE");
2475 });
2476 caller()->CreateAndSetAndSignalOffer();
2477 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002478 {
2479 MediaExpectations media_expectations;
2480 media_expectations.ExpectBidirectionalAudioAndVideo();
2481 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2482 }
deadbeef1dcb1642017-03-29 21:08:16 -07002483 // Now stop removing the BUNDLE group, and trigger an ICE restart.
2484 callee()->SetReceivedSdpMunger(nullptr);
2485 caller()->SetOfferAnswerOptions(IceRestartOfferAnswerOptions());
2486 caller()->CreateAndSetAndSignalOffer();
2487 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2488
2489 // Expect additional frames to be received after the ICE restart.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002490 {
2491 MediaExpectations media_expectations;
2492 media_expectations.ExpectBidirectionalAudioAndVideo();
2493 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2494 }
deadbeef1dcb1642017-03-29 21:08:16 -07002495}
2496
2497// Test CVO (Coordination of Video Orientation). If a video source is rotated
2498// and both peers support the CVO RTP header extension, the actual video frames
2499// don't need to be encoded in different resolutions, since the rotation is
2500// communicated through the RTP header extension.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002501TEST_P(PeerConnectionIntegrationTest, RotatedVideoWithCVOExtension) {
deadbeef1dcb1642017-03-29 21:08:16 -07002502 ASSERT_TRUE(CreatePeerConnectionWrappers());
2503 ConnectFakeSignaling();
2504 // Add rotated video tracks.
Steve Anton15324772018-01-16 10:26:49 -08002505 caller()->AddTrack(
deadbeef1dcb1642017-03-29 21:08:16 -07002506 caller()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_90));
Steve Anton15324772018-01-16 10:26:49 -08002507 callee()->AddTrack(
deadbeef1dcb1642017-03-29 21:08:16 -07002508 callee()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_270));
2509
2510 // Wait for video frames to be received by both sides.
2511 caller()->CreateAndSetAndSignalOffer();
2512 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2513 ASSERT_TRUE_WAIT(caller()->min_video_frames_received_per_track() > 0 &&
2514 callee()->min_video_frames_received_per_track() > 0,
2515 kMaxWaitForFramesMs);
2516
2517 // Ensure that the aspect ratio is unmodified.
2518 // TODO(deadbeef): Where does 4:3 come from? Should be explicit in the test,
2519 // not just assumed.
2520 EXPECT_EQ(4.0 / 3, caller()->local_rendered_aspect_ratio());
2521 EXPECT_EQ(4.0 / 3, caller()->rendered_aspect_ratio());
2522 EXPECT_EQ(4.0 / 3, callee()->local_rendered_aspect_ratio());
2523 EXPECT_EQ(4.0 / 3, callee()->rendered_aspect_ratio());
2524 // Ensure that the CVO bits were surfaced to the renderer.
2525 EXPECT_EQ(webrtc::kVideoRotation_270, caller()->rendered_rotation());
2526 EXPECT_EQ(webrtc::kVideoRotation_90, callee()->rendered_rotation());
2527}
2528
2529// Test that when the CVO extension isn't supported, video is rotated the
2530// old-fashioned way, by encoding rotated frames.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002531TEST_P(PeerConnectionIntegrationTest, RotatedVideoWithoutCVOExtension) {
deadbeef1dcb1642017-03-29 21:08:16 -07002532 ASSERT_TRUE(CreatePeerConnectionWrappers());
2533 ConnectFakeSignaling();
2534 // Add rotated video tracks.
Steve Anton15324772018-01-16 10:26:49 -08002535 caller()->AddTrack(
deadbeef1dcb1642017-03-29 21:08:16 -07002536 caller()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_90));
Steve Anton15324772018-01-16 10:26:49 -08002537 callee()->AddTrack(
deadbeef1dcb1642017-03-29 21:08:16 -07002538 callee()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_270));
2539
2540 // Remove the CVO extension from the offered SDP.
2541 callee()->SetReceivedSdpMunger([](cricket::SessionDescription* desc) {
2542 cricket::VideoContentDescription* video =
2543 GetFirstVideoContentDescription(desc);
2544 video->ClearRtpHeaderExtensions();
2545 });
2546 // Wait for video frames to be received by both sides.
2547 caller()->CreateAndSetAndSignalOffer();
2548 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2549 ASSERT_TRUE_WAIT(caller()->min_video_frames_received_per_track() > 0 &&
2550 callee()->min_video_frames_received_per_track() > 0,
2551 kMaxWaitForFramesMs);
2552
2553 // Expect that the aspect ratio is inversed to account for the 90/270 degree
2554 // rotation.
2555 // TODO(deadbeef): Where does 4:3 come from? Should be explicit in the test,
2556 // not just assumed.
2557 EXPECT_EQ(3.0 / 4, caller()->local_rendered_aspect_ratio());
2558 EXPECT_EQ(3.0 / 4, caller()->rendered_aspect_ratio());
2559 EXPECT_EQ(3.0 / 4, callee()->local_rendered_aspect_ratio());
2560 EXPECT_EQ(3.0 / 4, callee()->rendered_aspect_ratio());
2561 // Expect that each endpoint is unaware of the rotation of the other endpoint.
2562 EXPECT_EQ(webrtc::kVideoRotation_0, caller()->rendered_rotation());
2563 EXPECT_EQ(webrtc::kVideoRotation_0, callee()->rendered_rotation());
2564}
2565
deadbeef1dcb1642017-03-29 21:08:16 -07002566// Test that if the answerer rejects the audio m= section, no audio is sent or
2567// received, but video still can be.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002568TEST_P(PeerConnectionIntegrationTest, AnswererRejectsAudioSection) {
deadbeef1dcb1642017-03-29 21:08:16 -07002569 ASSERT_TRUE(CreatePeerConnectionWrappers());
2570 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002571 caller()->AddAudioVideoTracks();
Seth Hampson2f0d7022018-02-20 11:54:42 -08002572 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2573 // Only add video track for callee, and set offer_to_receive_audio to 0, so
2574 // it will reject the audio m= section completely.
2575 PeerConnectionInterface::RTCOfferAnswerOptions options;
2576 options.offer_to_receive_audio = 0;
2577 callee()->SetOfferAnswerOptions(options);
2578 } else {
2579 // Stopping the audio RtpTransceiver will cause the media section to be
2580 // rejected in the answer.
2581 callee()->SetRemoteOfferHandler([this] {
Harald Alvestrand6060df52020-08-11 09:54:02 +02002582 callee()
2583 ->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_AUDIO)
2584 ->StopInternal();
Seth Hampson2f0d7022018-02-20 11:54:42 -08002585 });
2586 }
Steve Anton15324772018-01-16 10:26:49 -08002587 callee()->AddTrack(callee()->CreateLocalVideoTrack());
deadbeef1dcb1642017-03-29 21:08:16 -07002588 // Do offer/answer and wait for successful end-to-end video frames.
2589 caller()->CreateAndSetAndSignalOffer();
2590 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002591 MediaExpectations media_expectations;
2592 media_expectations.ExpectBidirectionalVideo();
2593 media_expectations.ExpectNoAudio();
2594 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2595
deadbeef1dcb1642017-03-29 21:08:16 -07002596 // Sanity check that the callee's description has a rejected audio section.
2597 ASSERT_NE(nullptr, callee()->pc()->local_description());
2598 const ContentInfo* callee_audio_content =
2599 GetFirstAudioContent(callee()->pc()->local_description()->description());
2600 ASSERT_NE(nullptr, callee_audio_content);
2601 EXPECT_TRUE(callee_audio_content->rejected);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002602 if (sdp_semantics_ == SdpSemantics::kUnifiedPlan) {
Harald Alvestrand6060df52020-08-11 09:54:02 +02002603 // The caller's transceiver should have stopped after receiving the answer,
2604 // and thus no longer listed in transceivers.
2605 EXPECT_EQ(nullptr,
2606 caller()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_AUDIO));
Seth Hampson2f0d7022018-02-20 11:54:42 -08002607 }
deadbeef1dcb1642017-03-29 21:08:16 -07002608}
2609
2610// Test that if the answerer rejects the video m= section, no video is sent or
2611// received, but audio still can be.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002612TEST_P(PeerConnectionIntegrationTest, AnswererRejectsVideoSection) {
deadbeef1dcb1642017-03-29 21:08:16 -07002613 ASSERT_TRUE(CreatePeerConnectionWrappers());
2614 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002615 caller()->AddAudioVideoTracks();
Seth Hampson2f0d7022018-02-20 11:54:42 -08002616 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2617 // Only add audio track for callee, and set offer_to_receive_video to 0, so
2618 // it will reject the video m= section completely.
2619 PeerConnectionInterface::RTCOfferAnswerOptions options;
2620 options.offer_to_receive_video = 0;
2621 callee()->SetOfferAnswerOptions(options);
2622 } else {
2623 // Stopping the video RtpTransceiver will cause the media section to be
2624 // rejected in the answer.
2625 callee()->SetRemoteOfferHandler([this] {
Harald Alvestrand6060df52020-08-11 09:54:02 +02002626 callee()
2627 ->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)
2628 ->StopInternal();
Seth Hampson2f0d7022018-02-20 11:54:42 -08002629 });
2630 }
Steve Anton15324772018-01-16 10:26:49 -08002631 callee()->AddTrack(callee()->CreateLocalAudioTrack());
deadbeef1dcb1642017-03-29 21:08:16 -07002632 // Do offer/answer and wait for successful end-to-end audio frames.
2633 caller()->CreateAndSetAndSignalOffer();
2634 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002635 MediaExpectations media_expectations;
2636 media_expectations.ExpectBidirectionalAudio();
2637 media_expectations.ExpectNoVideo();
2638 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2639
deadbeef1dcb1642017-03-29 21:08:16 -07002640 // Sanity check that the callee's description has a rejected video section.
2641 ASSERT_NE(nullptr, callee()->pc()->local_description());
2642 const ContentInfo* callee_video_content =
2643 GetFirstVideoContent(callee()->pc()->local_description()->description());
2644 ASSERT_NE(nullptr, callee_video_content);
2645 EXPECT_TRUE(callee_video_content->rejected);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002646 if (sdp_semantics_ == SdpSemantics::kUnifiedPlan) {
Harald Alvestrand6060df52020-08-11 09:54:02 +02002647 // The caller's transceiver should have stopped after receiving the answer,
2648 // and thus is no longer present.
2649 EXPECT_EQ(nullptr,
2650 caller()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO));
Seth Hampson2f0d7022018-02-20 11:54:42 -08002651 }
deadbeef1dcb1642017-03-29 21:08:16 -07002652}
2653
2654// Test that if the answerer rejects both audio and video m= sections, nothing
2655// bad happens.
2656// TODO(deadbeef): Test that a data channel still works. Currently this doesn't
2657// test anything but the fact that negotiation succeeds, which doesn't mean
2658// much.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002659TEST_P(PeerConnectionIntegrationTest, AnswererRejectsAudioAndVideoSections) {
deadbeef1dcb1642017-03-29 21:08:16 -07002660 ASSERT_TRUE(CreatePeerConnectionWrappers());
2661 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002662 caller()->AddAudioVideoTracks();
Seth Hampson2f0d7022018-02-20 11:54:42 -08002663 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2664 // Don't give the callee any tracks, and set offer_to_receive_X to 0, so it
2665 // will reject both audio and video m= sections.
2666 PeerConnectionInterface::RTCOfferAnswerOptions options;
2667 options.offer_to_receive_audio = 0;
2668 options.offer_to_receive_video = 0;
2669 callee()->SetOfferAnswerOptions(options);
2670 } else {
2671 callee()->SetRemoteOfferHandler([this] {
2672 // Stopping all transceivers will cause all media sections to be rejected.
Mirko Bonadei739baf02019-01-27 17:29:42 +01002673 for (const auto& transceiver : callee()->pc()->GetTransceivers()) {
Harald Alvestrand6060df52020-08-11 09:54:02 +02002674 transceiver->StopInternal();
Seth Hampson2f0d7022018-02-20 11:54:42 -08002675 }
2676 });
2677 }
deadbeef1dcb1642017-03-29 21:08:16 -07002678 // Do offer/answer and wait for stable signaling state.
2679 caller()->CreateAndSetAndSignalOffer();
2680 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002681
deadbeef1dcb1642017-03-29 21:08:16 -07002682 // Sanity check that the callee's description has rejected m= sections.
2683 ASSERT_NE(nullptr, callee()->pc()->local_description());
2684 const ContentInfo* callee_audio_content =
2685 GetFirstAudioContent(callee()->pc()->local_description()->description());
2686 ASSERT_NE(nullptr, callee_audio_content);
2687 EXPECT_TRUE(callee_audio_content->rejected);
2688 const ContentInfo* callee_video_content =
2689 GetFirstVideoContent(callee()->pc()->local_description()->description());
2690 ASSERT_NE(nullptr, callee_video_content);
2691 EXPECT_TRUE(callee_video_content->rejected);
2692}
2693
2694// This test sets up an audio and video call between two parties. After the
2695// call runs for a while, the caller sends an updated offer with video being
2696// rejected. Once the re-negotiation is done, the video flow should stop and
2697// the audio flow should continue.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002698TEST_P(PeerConnectionIntegrationTest, VideoRejectedInSubsequentOffer) {
deadbeef1dcb1642017-03-29 21:08:16 -07002699 ASSERT_TRUE(CreatePeerConnectionWrappers());
2700 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002701 caller()->AddAudioVideoTracks();
2702 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002703 caller()->CreateAndSetAndSignalOffer();
2704 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002705 {
2706 MediaExpectations media_expectations;
2707 media_expectations.ExpectBidirectionalAudioAndVideo();
2708 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2709 }
deadbeef1dcb1642017-03-29 21:08:16 -07002710 // Renegotiate, rejecting the video m= section.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002711 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2712 caller()->SetGeneratedSdpMunger(
2713 [](cricket::SessionDescription* description) {
2714 for (cricket::ContentInfo& content : description->contents()) {
2715 if (cricket::IsVideoContent(&content)) {
2716 content.rejected = true;
2717 }
2718 }
2719 });
2720 } else {
Harald Alvestrand6060df52020-08-11 09:54:02 +02002721 caller()
2722 ->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)
2723 ->StopInternal();
Seth Hampson2f0d7022018-02-20 11:54:42 -08002724 }
deadbeef1dcb1642017-03-29 21:08:16 -07002725 caller()->CreateAndSetAndSignalOffer();
2726 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
2727
2728 // Sanity check that the caller's description has a rejected video section.
2729 ASSERT_NE(nullptr, caller()->pc()->local_description());
2730 const ContentInfo* caller_video_content =
2731 GetFirstVideoContent(caller()->pc()->local_description()->description());
2732 ASSERT_NE(nullptr, caller_video_content);
2733 EXPECT_TRUE(caller_video_content->rejected);
deadbeef1dcb1642017-03-29 21:08:16 -07002734 // Wait for some additional audio frames to be received.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002735 {
2736 MediaExpectations media_expectations;
2737 media_expectations.ExpectBidirectionalAudio();
2738 media_expectations.ExpectNoVideo();
2739 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2740 }
deadbeef1dcb1642017-03-29 21:08:16 -07002741}
2742
Taylor Brandstetter60c8dc82018-04-11 15:20:27 -07002743// Do one offer/answer with audio, another that disables it (rejecting the m=
2744// section), and another that re-enables it. Regression test for:
2745// bugs.webrtc.org/6023
2746TEST_F(PeerConnectionIntegrationTestPlanB, EnableAudioAfterRejecting) {
2747 ASSERT_TRUE(CreatePeerConnectionWrappers());
2748 ConnectFakeSignaling();
2749
2750 // Add audio track, do normal offer/answer.
2751 rtc::scoped_refptr<webrtc::AudioTrackInterface> track =
2752 caller()->CreateLocalAudioTrack();
2753 rtc::scoped_refptr<webrtc::RtpSenderInterface> sender =
2754 caller()->pc()->AddTrack(track, {"stream"}).MoveValue();
2755 caller()->CreateAndSetAndSignalOffer();
2756 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2757
2758 // Remove audio track, and set offer_to_receive_audio to false to cause the
2759 // m= section to be completely disabled, not just "recvonly".
2760 caller()->pc()->RemoveTrack(sender);
2761 PeerConnectionInterface::RTCOfferAnswerOptions options;
2762 options.offer_to_receive_audio = 0;
2763 caller()->SetOfferAnswerOptions(options);
2764 caller()->CreateAndSetAndSignalOffer();
2765 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2766
2767 // Add the audio track again, expecting negotiation to succeed and frames to
2768 // flow.
2769 sender = caller()->pc()->AddTrack(track, {"stream"}).MoveValue();
2770 options.offer_to_receive_audio = 1;
2771 caller()->SetOfferAnswerOptions(options);
2772 caller()->CreateAndSetAndSignalOffer();
2773 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2774
2775 MediaExpectations media_expectations;
2776 media_expectations.CalleeExpectsSomeAudio();
2777 EXPECT_TRUE(ExpectNewFrames(media_expectations));
2778}
2779
deadbeef1dcb1642017-03-29 21:08:16 -07002780// Basic end-to-end test, but without SSRC/MSID signaling. This functionality
2781// is needed to support legacy endpoints.
2782// TODO(deadbeef): When we support the MID extension and demuxing on MID, also
2783// add a test for an end-to-end test without MID signaling either (basically,
2784// the minimum acceptable SDP).
Seth Hampson2f0d7022018-02-20 11:54:42 -08002785TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithoutSsrcOrMsidSignaling) {
deadbeef1dcb1642017-03-29 21:08:16 -07002786 ASSERT_TRUE(CreatePeerConnectionWrappers());
2787 ConnectFakeSignaling();
2788 // Add audio and video, testing that packets can be demuxed on payload type.
Steve Anton15324772018-01-16 10:26:49 -08002789 caller()->AddAudioVideoTracks();
2790 callee()->AddAudioVideoTracks();
deadbeefd8ad7882017-04-18 16:01:17 -07002791 // Remove SSRCs and MSIDs from the received offer SDP.
2792 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
deadbeef1dcb1642017-03-29 21:08:16 -07002793 caller()->CreateAndSetAndSignalOffer();
2794 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002795 MediaExpectations media_expectations;
2796 media_expectations.ExpectBidirectionalAudioAndVideo();
2797 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002798}
2799
Seth Hampson5897a6e2018-04-03 11:16:33 -07002800// Basic end-to-end test, without SSRC signaling. This means that the track
2801// was created properly and frames are delivered when the MSIDs are communicated
2802// with a=msid lines and no a=ssrc lines.
2803TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
2804 EndToEndCallWithoutSsrcSignaling) {
2805 const char kStreamId[] = "streamId";
2806 ASSERT_TRUE(CreatePeerConnectionWrappers());
2807 ConnectFakeSignaling();
2808 // Add just audio tracks.
2809 caller()->AddTrack(caller()->CreateLocalAudioTrack(), {kStreamId});
2810 callee()->AddAudioTrack();
2811
2812 // Remove SSRCs from the received offer SDP.
2813 callee()->SetReceivedSdpMunger(RemoveSsrcsAndKeepMsids);
2814 caller()->CreateAndSetAndSignalOffer();
2815 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2816 MediaExpectations media_expectations;
2817 media_expectations.ExpectBidirectionalAudio();
2818 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2819}
2820
Johannes Kron3e983682020-03-29 22:17:00 +02002821TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
2822 EndToEndCallAddReceiveVideoToSendOnlyCall) {
2823 ASSERT_TRUE(CreatePeerConnectionWrappers());
2824 ConnectFakeSignaling();
2825 // Add one-directional video, from caller to callee.
2826 rtc::scoped_refptr<webrtc::VideoTrackInterface> track =
2827 caller()->CreateLocalVideoTrack();
2828
2829 RtpTransceiverInit video_transceiver_init;
2830 video_transceiver_init.stream_ids = {"video1"};
2831 video_transceiver_init.direction = RtpTransceiverDirection::kSendOnly;
2832 auto video_sender =
2833 caller()->pc()->AddTransceiver(track, video_transceiver_init).MoveValue();
2834 caller()->CreateAndSetAndSignalOffer();
2835 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2836
2837 // Add receive direction.
Harald Alvestrand6060df52020-08-11 09:54:02 +02002838 video_sender->SetDirectionWithError(RtpTransceiverDirection::kSendRecv);
Johannes Kron3e983682020-03-29 22:17:00 +02002839
2840 rtc::scoped_refptr<webrtc::VideoTrackInterface> callee_track =
2841 callee()->CreateLocalVideoTrack();
2842
2843 callee()->AddTrack(callee_track);
2844 caller()->CreateAndSetAndSignalOffer();
2845 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2846 // Ensure that video frames are received end-to-end.
2847 MediaExpectations media_expectations;
2848 media_expectations.ExpectBidirectionalVideo();
2849 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2850}
2851
Steve Antondf527fd2018-04-27 15:52:03 -07002852// Tests that video flows between multiple video tracks when SSRCs are not
2853// signaled. This exercises the MID RTP header extension which is needed to
2854// demux the incoming video tracks.
2855TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
2856 EndToEndCallWithTwoVideoTracksAndNoSignaledSsrc) {
2857 ASSERT_TRUE(CreatePeerConnectionWrappers());
2858 ConnectFakeSignaling();
2859 caller()->AddVideoTrack();
2860 caller()->AddVideoTrack();
2861 callee()->AddVideoTrack();
2862 callee()->AddVideoTrack();
2863
2864 caller()->SetReceivedSdpMunger(&RemoveSsrcsAndKeepMsids);
2865 callee()->SetReceivedSdpMunger(&RemoveSsrcsAndKeepMsids);
2866 caller()->CreateAndSetAndSignalOffer();
2867 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2868 ASSERT_EQ(2u, caller()->pc()->GetReceivers().size());
2869 ASSERT_EQ(2u, callee()->pc()->GetReceivers().size());
2870
2871 // Expect video to be received in both directions on both tracks.
2872 MediaExpectations media_expectations;
2873 media_expectations.ExpectBidirectionalVideo();
2874 EXPECT_TRUE(ExpectNewFrames(media_expectations));
2875}
2876
Taylor Brandstetterd3ef4992020-10-15 18:22:57 -07002877// Used for the test below.
2878void RemoveBundleGroupSsrcsAndMidExtension(cricket::SessionDescription* desc) {
2879 RemoveSsrcsAndKeepMsids(desc);
2880 desc->RemoveGroupByName("BUNDLE");
2881 for (ContentInfo& content : desc->contents()) {
2882 cricket::MediaContentDescription* media = content.media_description();
2883 cricket::RtpHeaderExtensions extensions = media->rtp_header_extensions();
2884 extensions.erase(std::remove_if(extensions.begin(), extensions.end(),
2885 [](const RtpExtension& extension) {
2886 return extension.uri ==
2887 RtpExtension::kMidUri;
2888 }),
2889 extensions.end());
2890 media->set_rtp_header_extensions(extensions);
2891 }
2892}
2893
2894// Tests that video flows between multiple video tracks when BUNDLE is not used,
2895// SSRCs are not signaled and the MID RTP header extension is not used. This
2896// relies on demuxing by payload type, which normally doesn't work if you have
2897// multiple media sections using the same payload type, but which should work as
2898// long as the media sections aren't bundled.
2899// Regression test for: http://crbug.com/webrtc/12023
2900TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
2901 EndToEndCallWithTwoVideoTracksNoBundleNoSignaledSsrcAndNoMid) {
2902 ASSERT_TRUE(CreatePeerConnectionWrappers());
2903 ConnectFakeSignaling();
2904 caller()->AddVideoTrack();
2905 caller()->AddVideoTrack();
2906 callee()->AddVideoTrack();
2907 callee()->AddVideoTrack();
2908 caller()->SetReceivedSdpMunger(&RemoveBundleGroupSsrcsAndMidExtension);
2909 callee()->SetReceivedSdpMunger(&RemoveBundleGroupSsrcsAndMidExtension);
2910 caller()->CreateAndSetAndSignalOffer();
2911 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2912 ASSERT_EQ(2u, caller()->pc()->GetReceivers().size());
2913 ASSERT_EQ(2u, callee()->pc()->GetReceivers().size());
2914 // Make sure we are not bundled.
2915 ASSERT_NE(caller()->pc()->GetSenders()[0]->dtls_transport(),
2916 caller()->pc()->GetSenders()[1]->dtls_transport());
2917
2918 // Expect video to be received in both directions on both tracks.
2919 MediaExpectations media_expectations;
2920 media_expectations.ExpectBidirectionalVideo();
2921 EXPECT_TRUE(ExpectNewFrames(media_expectations));
2922}
2923
2924// Used for the test below.
2925void ModifyPayloadTypesAndRemoveMidExtension(
2926 cricket::SessionDescription* desc) {
2927 int pt = 96;
2928 for (ContentInfo& content : desc->contents()) {
2929 cricket::MediaContentDescription* media = content.media_description();
2930 cricket::RtpHeaderExtensions extensions = media->rtp_header_extensions();
2931 extensions.erase(std::remove_if(extensions.begin(), extensions.end(),
2932 [](const RtpExtension& extension) {
2933 return extension.uri ==
2934 RtpExtension::kMidUri;
2935 }),
2936 extensions.end());
2937 media->set_rtp_header_extensions(extensions);
2938 cricket::VideoContentDescription* video = media->as_video();
2939 ASSERT_TRUE(video != nullptr);
2940 std::vector<cricket::VideoCodec> codecs = {{pt++, "VP8"}};
2941 video->set_codecs(codecs);
2942 }
2943}
2944
2945// Tests that two video tracks can be demultiplexed by payload type alone, by
2946// using different payload types for the same codec in different m= sections.
2947// This practice is discouraged but historically has been supported.
2948// Regression test for: http://crbug.com/webrtc/12029
2949TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
2950 EndToEndCallWithTwoVideoTracksDemultiplexedByPayloadType) {
2951 ASSERT_TRUE(CreatePeerConnectionWrappers());
2952 ConnectFakeSignaling();
2953 caller()->AddVideoTrack();
2954 caller()->AddVideoTrack();
2955 callee()->AddVideoTrack();
2956 callee()->AddVideoTrack();
2957 caller()->SetGeneratedSdpMunger(&ModifyPayloadTypesAndRemoveMidExtension);
2958 callee()->SetGeneratedSdpMunger(&ModifyPayloadTypesAndRemoveMidExtension);
2959 // We can't remove SSRCs from the generated SDP because then no send streams
2960 // would be created.
2961 caller()->SetReceivedSdpMunger(&RemoveSsrcsAndKeepMsids);
2962 callee()->SetReceivedSdpMunger(&RemoveSsrcsAndKeepMsids);
2963 caller()->CreateAndSetAndSignalOffer();
2964 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2965 ASSERT_EQ(2u, caller()->pc()->GetReceivers().size());
2966 ASSERT_EQ(2u, callee()->pc()->GetReceivers().size());
2967 // Make sure we are bundled.
2968 ASSERT_EQ(caller()->pc()->GetSenders()[0]->dtls_transport(),
2969 caller()->pc()->GetSenders()[1]->dtls_transport());
2970
2971 // Expect video to be received in both directions on both tracks.
2972 MediaExpectations media_expectations;
2973 media_expectations.ExpectBidirectionalVideo();
2974 EXPECT_TRUE(ExpectNewFrames(media_expectations));
2975}
2976
Henrik Boström5b147782018-12-04 11:25:05 +01002977TEST_F(PeerConnectionIntegrationTestUnifiedPlan, NoStreamsMsidLinePresent) {
2978 ASSERT_TRUE(CreatePeerConnectionWrappers());
2979 ConnectFakeSignaling();
2980 caller()->AddAudioTrack();
2981 caller()->AddVideoTrack();
2982 caller()->CreateAndSetAndSignalOffer();
2983 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2984 auto callee_receivers = callee()->pc()->GetReceivers();
2985 ASSERT_EQ(2u, callee_receivers.size());
2986 EXPECT_TRUE(callee_receivers[0]->stream_ids().empty());
2987 EXPECT_TRUE(callee_receivers[1]->stream_ids().empty());
2988}
2989
2990TEST_F(PeerConnectionIntegrationTestUnifiedPlan, NoStreamsMsidLineMissing) {
2991 ASSERT_TRUE(CreatePeerConnectionWrappers());
2992 ConnectFakeSignaling();
2993 caller()->AddAudioTrack();
2994 caller()->AddVideoTrack();
2995 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
2996 caller()->CreateAndSetAndSignalOffer();
2997 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2998 auto callee_receivers = callee()->pc()->GetReceivers();
2999 ASSERT_EQ(2u, callee_receivers.size());
3000 ASSERT_EQ(1u, callee_receivers[0]->stream_ids().size());
3001 ASSERT_EQ(1u, callee_receivers[1]->stream_ids().size());
3002 EXPECT_EQ(callee_receivers[0]->stream_ids()[0],
3003 callee_receivers[1]->stream_ids()[0]);
3004 EXPECT_EQ(callee_receivers[0]->streams()[0],
3005 callee_receivers[1]->streams()[0]);
3006}
3007
deadbeef1dcb1642017-03-29 21:08:16 -07003008// Test that if two video tracks are sent (from caller to callee, in this test),
3009// they're transmitted correctly end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003010TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithTwoVideoTracks) {
deadbeef1dcb1642017-03-29 21:08:16 -07003011 ASSERT_TRUE(CreatePeerConnectionWrappers());
3012 ConnectFakeSignaling();
3013 // Add one audio/video stream, and one video-only stream.
Steve Anton15324772018-01-16 10:26:49 -08003014 caller()->AddAudioVideoTracks();
3015 caller()->AddVideoTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07003016 caller()->CreateAndSetAndSignalOffer();
3017 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Steve Anton15324772018-01-16 10:26:49 -08003018 ASSERT_EQ(3u, callee()->pc()->GetReceivers().size());
Seth Hampson2f0d7022018-02-20 11:54:42 -08003019
3020 MediaExpectations media_expectations;
3021 media_expectations.CalleeExpectsSomeAudioAndVideo();
3022 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003023}
3024
3025static void MakeSpecCompliantMaxBundleOffer(cricket::SessionDescription* desc) {
3026 bool first = true;
3027 for (cricket::ContentInfo& content : desc->contents()) {
3028 if (first) {
3029 first = false;
3030 continue;
3031 }
3032 content.bundle_only = true;
3033 }
3034 first = true;
3035 for (cricket::TransportInfo& transport : desc->transport_infos()) {
3036 if (first) {
3037 first = false;
3038 continue;
3039 }
3040 transport.description.ice_ufrag.clear();
3041 transport.description.ice_pwd.clear();
3042 transport.description.connection_role = cricket::CONNECTIONROLE_NONE;
3043 transport.description.identity_fingerprint.reset(nullptr);
3044 }
3045}
3046
3047// Test that if applying a true "max bundle" offer, which uses ports of 0,
3048// "a=bundle-only", omitting "a=fingerprint", "a=setup", "a=ice-ufrag" and
3049// "a=ice-pwd" for all but the audio "m=" section, negotiation still completes
3050// successfully and media flows.
3051// TODO(deadbeef): Update this test to also omit "a=rtcp-mux", once that works.
3052// TODO(deadbeef): Won't need this test once we start generating actual
3053// standards-compliant SDP.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003054TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07003055 EndToEndCallWithSpecCompliantMaxBundleOffer) {
3056 ASSERT_TRUE(CreatePeerConnectionWrappers());
3057 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08003058 caller()->AddAudioVideoTracks();
3059 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003060 // Do the equivalent of setting the port to 0, adding a=bundle-only, and
3061 // removing a=ice-ufrag, a=ice-pwd, a=fingerprint and a=setup from all
3062 // but the first m= section.
3063 callee()->SetReceivedSdpMunger(MakeSpecCompliantMaxBundleOffer);
3064 caller()->CreateAndSetAndSignalOffer();
3065 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08003066 MediaExpectations media_expectations;
3067 media_expectations.ExpectBidirectionalAudioAndVideo();
3068 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003069}
3070
3071// Test that we can receive the audio output level from a remote audio track.
3072// TODO(deadbeef): Use a fake audio source and verify that the output level is
3073// exactly what the source on the other side was configured with.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003074TEST_P(PeerConnectionIntegrationTest, GetAudioOutputLevelStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-29 21:08:16 -07003075 ASSERT_TRUE(CreatePeerConnectionWrappers());
3076 ConnectFakeSignaling();
3077 // Just add an audio track.
Steve Anton15324772018-01-16 10:26:49 -08003078 caller()->AddAudioTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07003079 caller()->CreateAndSetAndSignalOffer();
3080 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3081
3082 // Get the audio output level stats. Note that the level is not available
3083 // until an RTCP packet has been received.
deadbeefd8ad7882017-04-18 16:01:17 -07003084 EXPECT_TRUE_WAIT(callee()->OldGetStats()->AudioOutputLevel() > 0,
deadbeef1dcb1642017-03-29 21:08:16 -07003085 kMaxWaitForFramesMs);
3086}
3087
3088// Test that an audio input level is reported.
3089// TODO(deadbeef): Use a fake audio source and verify that the input level is
3090// exactly what the source was configured with.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003091TEST_P(PeerConnectionIntegrationTest, GetAudioInputLevelStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-29 21:08:16 -07003092 ASSERT_TRUE(CreatePeerConnectionWrappers());
3093 ConnectFakeSignaling();
3094 // Just add an audio track.
Steve Anton15324772018-01-16 10:26:49 -08003095 caller()->AddAudioTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07003096 caller()->CreateAndSetAndSignalOffer();
3097 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3098
3099 // Get the audio input level stats. The level should be available very
3100 // soon after the test starts.
deadbeefd8ad7882017-04-18 16:01:17 -07003101 EXPECT_TRUE_WAIT(caller()->OldGetStats()->AudioInputLevel() > 0,
deadbeef1dcb1642017-03-29 21:08:16 -07003102 kMaxWaitForStatsMs);
3103}
3104
3105// Test that we can get incoming byte counts from both audio and video tracks.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003106TEST_P(PeerConnectionIntegrationTest, GetBytesReceivedStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-29 21:08:16 -07003107 ASSERT_TRUE(CreatePeerConnectionWrappers());
3108 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08003109 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003110 // Do offer/answer, wait for the callee to receive some frames.
3111 caller()->CreateAndSetAndSignalOffer();
3112 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08003113
3114 MediaExpectations media_expectations;
3115 media_expectations.CalleeExpectsSomeAudioAndVideo();
3116 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003117
3118 // Get a handle to the remote tracks created, so they can be used as GetStats
3119 // filters.
Mirko Bonadei739baf02019-01-27 17:29:42 +01003120 for (const auto& receiver : callee()->pc()->GetReceivers()) {
Steve Anton15324772018-01-16 10:26:49 -08003121 // We received frames, so we definitely should have nonzero "received bytes"
3122 // stats at this point.
3123 EXPECT_GT(callee()->OldGetStatsForTrack(receiver->track())->BytesReceived(),
3124 0);
3125 }
deadbeef1dcb1642017-03-29 21:08:16 -07003126}
3127
3128// Test that we can get outgoing byte counts from both audio and video tracks.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003129TEST_P(PeerConnectionIntegrationTest, GetBytesSentStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-29 21:08:16 -07003130 ASSERT_TRUE(CreatePeerConnectionWrappers());
3131 ConnectFakeSignaling();
3132 auto audio_track = caller()->CreateLocalAudioTrack();
3133 auto video_track = caller()->CreateLocalVideoTrack();
Steve Anton15324772018-01-16 10:26:49 -08003134 caller()->AddTrack(audio_track);
3135 caller()->AddTrack(video_track);
deadbeef1dcb1642017-03-29 21:08:16 -07003136 // Do offer/answer, wait for the callee to receive some frames.
3137 caller()->CreateAndSetAndSignalOffer();
3138 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08003139 MediaExpectations media_expectations;
3140 media_expectations.CalleeExpectsSomeAudioAndVideo();
3141 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003142
3143 // The callee received frames, so we definitely should have nonzero "sent
3144 // bytes" stats at this point.
deadbeefd8ad7882017-04-18 16:01:17 -07003145 EXPECT_GT(caller()->OldGetStatsForTrack(audio_track)->BytesSent(), 0);
3146 EXPECT_GT(caller()->OldGetStatsForTrack(video_track)->BytesSent(), 0);
3147}
3148
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02003149// Test that we can get capture start ntp time.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003150TEST_P(PeerConnectionIntegrationTest, GetCaptureStartNtpTimeWithOldStatsApi) {
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02003151 ASSERT_TRUE(CreatePeerConnectionWrappers());
3152 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08003153 caller()->AddAudioTrack();
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02003154
Steve Anton15324772018-01-16 10:26:49 -08003155 callee()->AddAudioTrack();
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02003156
3157 // Do offer/answer, wait for the callee to receive some frames.
3158 caller()->CreateAndSetAndSignalOffer();
3159 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3160
3161 // Get the remote audio track created on the receiver, so they can be used as
3162 // GetStats filters.
Steve Antonfc853712018-03-01 13:48:58 -08003163 auto receivers = callee()->pc()->GetReceivers();
3164 ASSERT_EQ(1u, receivers.size());
3165 auto remote_audio_track = receivers[0]->track();
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02003166
3167 // Get the audio output level stats. Note that the level is not available
3168 // until an RTCP packet has been received.
Zhi Huange830e682018-03-30 10:48:35 -07003169 EXPECT_TRUE_WAIT(
3170 callee()->OldGetStatsForTrack(remote_audio_track)->CaptureStartNtpTime() >
3171 0,
3172 2 * kMaxWaitForFramesMs);
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02003173}
3174
Steve Antona41959e2018-11-28 11:15:33 -08003175// Test that the track ID is associated with all local and remote SSRC stats
3176// using the old GetStats() and more than 1 audio and more than 1 video track.
3177// This is a regression test for crbug.com/906988
3178TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
3179 OldGetStatsAssociatesTrackIdForManyMediaSections) {
3180 ASSERT_TRUE(CreatePeerConnectionWrappers());
3181 ConnectFakeSignaling();
3182 auto audio_sender_1 = caller()->AddAudioTrack();
3183 auto video_sender_1 = caller()->AddVideoTrack();
3184 auto audio_sender_2 = caller()->AddAudioTrack();
3185 auto video_sender_2 = caller()->AddVideoTrack();
3186 caller()->CreateAndSetAndSignalOffer();
3187 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3188
3189 MediaExpectations media_expectations;
3190 media_expectations.CalleeExpectsSomeAudioAndVideo();
3191 ASSERT_TRUE_WAIT(ExpectNewFrames(media_expectations), kDefaultTimeout);
3192
3193 std::vector<std::string> track_ids = {
3194 audio_sender_1->track()->id(), video_sender_1->track()->id(),
3195 audio_sender_2->track()->id(), video_sender_2->track()->id()};
3196
3197 auto caller_stats = caller()->OldGetStats();
3198 EXPECT_THAT(caller_stats->TrackIds(), UnorderedElementsAreArray(track_ids));
3199 auto callee_stats = callee()->OldGetStats();
3200 EXPECT_THAT(callee_stats->TrackIds(), UnorderedElementsAreArray(track_ids));
3201}
3202
Steve Antonffa6ce42018-11-30 09:26:08 -08003203// Test that the new GetStats() returns stats for all outgoing/incoming streams
3204// with the correct track IDs if there are more than one audio and more than one
3205// video senders/receivers.
3206TEST_P(PeerConnectionIntegrationTest, NewGetStatsManyAudioAndManyVideoStreams) {
3207 ASSERT_TRUE(CreatePeerConnectionWrappers());
3208 ConnectFakeSignaling();
3209 auto audio_sender_1 = caller()->AddAudioTrack();
3210 auto video_sender_1 = caller()->AddVideoTrack();
3211 auto audio_sender_2 = caller()->AddAudioTrack();
3212 auto video_sender_2 = caller()->AddVideoTrack();
3213 caller()->CreateAndSetAndSignalOffer();
3214 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3215
3216 MediaExpectations media_expectations;
3217 media_expectations.CalleeExpectsSomeAudioAndVideo();
3218 ASSERT_TRUE_WAIT(ExpectNewFrames(media_expectations), kDefaultTimeout);
3219
3220 std::vector<std::string> track_ids = {
3221 audio_sender_1->track()->id(), video_sender_1->track()->id(),
3222 audio_sender_2->track()->id(), video_sender_2->track()->id()};
3223
3224 rtc::scoped_refptr<const webrtc::RTCStatsReport> caller_report =
3225 caller()->NewGetStats();
3226 ASSERT_TRUE(caller_report);
3227 auto outbound_stream_stats =
3228 caller_report->GetStatsOfType<webrtc::RTCOutboundRTPStreamStats>();
Henrik Boströma0ff50c2020-05-05 15:54:46 +02003229 ASSERT_EQ(outbound_stream_stats.size(), 4u);
Steve Antonffa6ce42018-11-30 09:26:08 -08003230 std::vector<std::string> outbound_track_ids;
3231 for (const auto& stat : outbound_stream_stats) {
3232 ASSERT_TRUE(stat->bytes_sent.is_defined());
3233 EXPECT_LT(0u, *stat->bytes_sent);
Rasmus Brandt2efae772019-06-27 14:29:34 +02003234 if (*stat->kind == "video") {
3235 ASSERT_TRUE(stat->key_frames_encoded.is_defined());
3236 EXPECT_GT(*stat->key_frames_encoded, 0u);
3237 ASSERT_TRUE(stat->frames_encoded.is_defined());
3238 EXPECT_GE(*stat->frames_encoded, *stat->key_frames_encoded);
3239 }
Steve Antonffa6ce42018-11-30 09:26:08 -08003240 ASSERT_TRUE(stat->track_id.is_defined());
3241 const auto* track_stat =
3242 caller_report->GetAs<webrtc::RTCMediaStreamTrackStats>(*stat->track_id);
3243 ASSERT_TRUE(track_stat);
3244 outbound_track_ids.push_back(*track_stat->track_identifier);
3245 }
3246 EXPECT_THAT(outbound_track_ids, UnorderedElementsAreArray(track_ids));
3247
3248 rtc::scoped_refptr<const webrtc::RTCStatsReport> callee_report =
3249 callee()->NewGetStats();
3250 ASSERT_TRUE(callee_report);
3251 auto inbound_stream_stats =
3252 callee_report->GetStatsOfType<webrtc::RTCInboundRTPStreamStats>();
3253 ASSERT_EQ(4u, inbound_stream_stats.size());
3254 std::vector<std::string> inbound_track_ids;
3255 for (const auto& stat : inbound_stream_stats) {
3256 ASSERT_TRUE(stat->bytes_received.is_defined());
3257 EXPECT_LT(0u, *stat->bytes_received);
Rasmus Brandt2efae772019-06-27 14:29:34 +02003258 if (*stat->kind == "video") {
3259 ASSERT_TRUE(stat->key_frames_decoded.is_defined());
3260 EXPECT_GT(*stat->key_frames_decoded, 0u);
3261 ASSERT_TRUE(stat->frames_decoded.is_defined());
3262 EXPECT_GE(*stat->frames_decoded, *stat->key_frames_decoded);
3263 }
Steve Antonffa6ce42018-11-30 09:26:08 -08003264 ASSERT_TRUE(stat->track_id.is_defined());
3265 const auto* track_stat =
3266 callee_report->GetAs<webrtc::RTCMediaStreamTrackStats>(*stat->track_id);
3267 ASSERT_TRUE(track_stat);
3268 inbound_track_ids.push_back(*track_stat->track_identifier);
3269 }
3270 EXPECT_THAT(inbound_track_ids, UnorderedElementsAreArray(track_ids));
3271}
3272
3273// Test that we can get stats (using the new stats implementation) for
deadbeefd8ad7882017-04-18 16:01:17 -07003274// unsignaled streams. Meaning when SSRCs/MSIDs aren't signaled explicitly in
3275// SDP.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003276TEST_P(PeerConnectionIntegrationTest,
deadbeefd8ad7882017-04-18 16:01:17 -07003277 GetStatsForUnsignaledStreamWithNewStatsApi) {
3278 ASSERT_TRUE(CreatePeerConnectionWrappers());
3279 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08003280 caller()->AddAudioTrack();
deadbeefd8ad7882017-04-18 16:01:17 -07003281 // Remove SSRCs and MSIDs from the received offer SDP.
3282 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
3283 caller()->CreateAndSetAndSignalOffer();
3284 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08003285 MediaExpectations media_expectations;
3286 media_expectations.CalleeExpectsSomeAudio(1);
3287 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeefd8ad7882017-04-18 16:01:17 -07003288
3289 // We received a frame, so we should have nonzero "bytes received" stats for
3290 // the unsignaled stream, if stats are working for it.
3291 rtc::scoped_refptr<const webrtc::RTCStatsReport> report =
3292 callee()->NewGetStats();
3293 ASSERT_NE(nullptr, report);
3294 auto inbound_stream_stats =
3295 report->GetStatsOfType<webrtc::RTCInboundRTPStreamStats>();
3296 ASSERT_EQ(1U, inbound_stream_stats.size());
3297 ASSERT_TRUE(inbound_stream_stats[0]->bytes_received.is_defined());
3298 ASSERT_GT(*inbound_stream_stats[0]->bytes_received, 0U);
zhihuangf8164932017-05-19 13:09:47 -07003299 ASSERT_TRUE(inbound_stream_stats[0]->track_id.is_defined());
3300}
3301
Taylor Brandstettera4653442018-06-19 09:44:26 -07003302// Same as above but for the legacy stats implementation.
3303TEST_P(PeerConnectionIntegrationTest,
3304 GetStatsForUnsignaledStreamWithOldStatsApi) {
3305 ASSERT_TRUE(CreatePeerConnectionWrappers());
3306 ConnectFakeSignaling();
3307 caller()->AddAudioTrack();
3308 // Remove SSRCs and MSIDs from the received offer SDP.
3309 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
3310 caller()->CreateAndSetAndSignalOffer();
3311 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3312
3313 // Note that, since the old stats implementation associates SSRCs with tracks
3314 // using SDP, when SSRCs aren't signaled in SDP these stats won't have an
3315 // associated track ID. So we can't use the track "selector" argument.
3316 //
3317 // Also, we use "EXPECT_TRUE_WAIT" because the stats collector may decide to
3318 // return cached stats if not enough time has passed since the last update.
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02003319 EXPECT_TRUE_WAIT(callee()->OldGetStats()->BytesReceived() > 0,
Taylor Brandstettera4653442018-06-19 09:44:26 -07003320 kDefaultTimeout);
3321}
3322
zhihuangf8164932017-05-19 13:09:47 -07003323// Test that we can successfully get the media related stats (audio level
3324// etc.) for the unsignaled stream.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003325TEST_P(PeerConnectionIntegrationTest,
zhihuangf8164932017-05-19 13:09:47 -07003326 GetMediaStatsForUnsignaledStreamWithNewStatsApi) {
3327 ASSERT_TRUE(CreatePeerConnectionWrappers());
3328 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08003329 caller()->AddAudioVideoTracks();
zhihuangf8164932017-05-19 13:09:47 -07003330 // Remove SSRCs and MSIDs from the received offer SDP.
3331 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
3332 caller()->CreateAndSetAndSignalOffer();
3333 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08003334 MediaExpectations media_expectations;
3335 media_expectations.CalleeExpectsSomeAudio(1);
3336 media_expectations.CalleeExpectsSomeVideo(1);
3337 ASSERT_TRUE(ExpectNewFrames(media_expectations));
zhihuangf8164932017-05-19 13:09:47 -07003338
3339 rtc::scoped_refptr<const webrtc::RTCStatsReport> report =
3340 callee()->NewGetStats();
3341 ASSERT_NE(nullptr, report);
3342
3343 auto media_stats = report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
3344 auto audio_index = FindFirstMediaStatsIndexByKind("audio", media_stats);
3345 ASSERT_GE(audio_index, 0);
3346 EXPECT_TRUE(media_stats[audio_index]->audio_level.is_defined());
deadbeef1dcb1642017-03-29 21:08:16 -07003347}
3348
deadbeef4e2deab2017-09-20 13:56:21 -07003349// Helper for test below.
3350void ModifySsrcs(cricket::SessionDescription* desc) {
3351 for (ContentInfo& content : desc->contents()) {
Steve Antondf527fd2018-04-27 15:52:03 -07003352 for (StreamParams& stream :
Steve Antonb1c1de12017-12-21 15:14:30 -08003353 content.media_description()->mutable_streams()) {
deadbeef4e2deab2017-09-20 13:56:21 -07003354 for (uint32_t& ssrc : stream.ssrcs) {
3355 ssrc = rtc::CreateRandomId();
3356 }
3357 }
3358 }
3359}
3360
3361// Test that the "RTCMediaSteamTrackStats" object is updated correctly when
3362// SSRCs are unsignaled, and the SSRC of the received (audio) stream changes.
3363// This should result in two "RTCInboundRTPStreamStats", but only one
3364// "RTCMediaStreamTrackStats", whose counters go up continuously rather than
3365// being reset to 0 once the SSRC change occurs.
3366//
3367// Regression test for this bug:
3368// https://bugs.chromium.org/p/webrtc/issues/detail?id=8158
3369//
3370// The bug causes the track stats to only represent one of the two streams:
3371// whichever one has the higher SSRC. So with this bug, there was a 50% chance
3372// that the track stat counters would reset to 0 when the new stream is
3373// received, and a 50% chance that they'll stop updating (while
3374// "concealed_samples" continues increasing, due to silence being generated for
3375// the inactive stream).
Seth Hampson2f0d7022018-02-20 11:54:42 -08003376TEST_P(PeerConnectionIntegrationTest,
Steve Anton83119dd2017-11-10 16:19:52 -08003377 TrackStatsUpdatedCorrectlyWhenUnsignaledSsrcChanges) {
deadbeef4e2deab2017-09-20 13:56:21 -07003378 ASSERT_TRUE(CreatePeerConnectionWrappers());
3379 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08003380 caller()->AddAudioTrack();
deadbeef4e2deab2017-09-20 13:56:21 -07003381 // Remove SSRCs and MSIDs from the received offer SDP, simulating an endpoint
3382 // that doesn't signal SSRCs (from the callee's perspective).
3383 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
3384 caller()->CreateAndSetAndSignalOffer();
3385 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3386 // Wait for 50 audio frames (500ms of audio) to be received by the callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003387 {
3388 MediaExpectations media_expectations;
3389 media_expectations.CalleeExpectsSomeAudio(50);
3390 ASSERT_TRUE(ExpectNewFrames(media_expectations));
3391 }
deadbeef4e2deab2017-09-20 13:56:21 -07003392 // Some audio frames were received, so we should have nonzero "samples
3393 // received" for the track.
3394 rtc::scoped_refptr<const webrtc::RTCStatsReport> report =
3395 callee()->NewGetStats();
3396 ASSERT_NE(nullptr, report);
3397 auto track_stats = report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
3398 ASSERT_EQ(1U, track_stats.size());
3399 ASSERT_TRUE(track_stats[0]->total_samples_received.is_defined());
3400 ASSERT_GT(*track_stats[0]->total_samples_received, 0U);
3401 // uint64_t prev_samples_received = *track_stats[0]->total_samples_received;
3402
3403 // Create a new offer and munge it to cause the caller to use a new SSRC.
3404 caller()->SetGeneratedSdpMunger(ModifySsrcs);
3405 caller()->CreateAndSetAndSignalOffer();
3406 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3407 // Wait for 25 more audio frames (250ms of audio) to be received, from the new
3408 // SSRC.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003409 {
3410 MediaExpectations media_expectations;
3411 media_expectations.CalleeExpectsSomeAudio(25);
3412 ASSERT_TRUE(ExpectNewFrames(media_expectations));
3413 }
deadbeef4e2deab2017-09-20 13:56:21 -07003414
3415 report = callee()->NewGetStats();
3416 ASSERT_NE(nullptr, report);
3417 track_stats = report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
3418 ASSERT_EQ(1U, track_stats.size());
3419 ASSERT_TRUE(track_stats[0]->total_samples_received.is_defined());
3420 // The "total samples received" stat should only be greater than it was
3421 // before.
3422 // TODO(deadbeef): Uncomment this assertion once the bug is completely fixed.
3423 // Right now, the new SSRC will cause the counters to reset to 0.
3424 // EXPECT_GT(*track_stats[0]->total_samples_received, prev_samples_received);
3425
3426 // Additionally, the percentage of concealed samples (samples generated to
Steve Anton83119dd2017-11-10 16:19:52 -08003427 // conceal packet loss) should be less than 50%. If it's greater, that's a
deadbeef4e2deab2017-09-20 13:56:21 -07003428 // good sign that we're seeing stats from the old stream that's no longer
3429 // receiving packets, and is generating concealed samples of silence.
Steve Anton83119dd2017-11-10 16:19:52 -08003430 constexpr double kAcceptableConcealedSamplesPercentage = 0.50;
deadbeef4e2deab2017-09-20 13:56:21 -07003431 ASSERT_TRUE(track_stats[0]->concealed_samples.is_defined());
3432 EXPECT_LT(*track_stats[0]->concealed_samples,
3433 *track_stats[0]->total_samples_received *
3434 kAcceptableConcealedSamplesPercentage);
3435
3436 // Also ensure that we have two "RTCInboundRTPStreamStats" as expected, as a
3437 // sanity check that the SSRC really changed.
3438 // TODO(deadbeef): This isn't working right now, because we're not returning
3439 // *any* stats for the inactive stream. Uncomment when the bug is completely
3440 // fixed.
3441 // auto inbound_stream_stats =
3442 // report->GetStatsOfType<webrtc::RTCInboundRTPStreamStats>();
3443 // ASSERT_EQ(2U, inbound_stream_stats.size());
3444}
3445
deadbeef1dcb1642017-03-29 21:08:16 -07003446// Test that DTLS 1.0 is used if both sides only support DTLS 1.0.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003447TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithDtls10) {
deadbeef1dcb1642017-03-29 21:08:16 -07003448 PeerConnectionFactory::Options dtls_10_options;
3449 dtls_10_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
3450 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(dtls_10_options,
3451 dtls_10_options));
3452 ConnectFakeSignaling();
3453 // Do normal offer/answer and wait for some frames to be received in each
3454 // direction.
Steve Anton15324772018-01-16 10:26:49 -08003455 caller()->AddAudioVideoTracks();
3456 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003457 caller()->CreateAndSetAndSignalOffer();
3458 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08003459 MediaExpectations media_expectations;
3460 media_expectations.ExpectBidirectionalAudioAndVideo();
3461 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003462}
3463
3464// Test getting cipher stats and UMA metrics when DTLS 1.0 is negotiated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003465TEST_P(PeerConnectionIntegrationTest, Dtls10CipherStatsAndUmaMetrics) {
deadbeef1dcb1642017-03-29 21:08:16 -07003466 PeerConnectionFactory::Options dtls_10_options;
3467 dtls_10_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
3468 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(dtls_10_options,
3469 dtls_10_options));
3470 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08003471 caller()->AddAudioVideoTracks();
3472 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003473 caller()->CreateAndSetAndSignalOffer();
Qingsi Wang7fc821d2018-07-12 12:54:53 -07003474 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07003475 EXPECT_TRUE_WAIT(rtc::SSLStreamAdapter::IsAcceptableCipher(
deadbeefd8ad7882017-04-18 16:01:17 -07003476 caller()->OldGetStats()->DtlsCipher(), rtc::KT_DEFAULT),
deadbeef1dcb1642017-03-29 21:08:16 -07003477 kDefaultTimeout);
3478 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
deadbeefd8ad7882017-04-18 16:01:17 -07003479 caller()->OldGetStats()->SrtpCipher(), kDefaultTimeout);
Qingsi Wang7fc821d2018-07-12 12:54:53 -07003480 // TODO(bugs.webrtc.org/9456): Fix it.
Ying Wangef3998f2019-12-09 13:06:53 +01003481 EXPECT_METRIC_EQ(1, webrtc::metrics::NumEvents(
3482 "WebRTC.PeerConnection.SrtpCryptoSuite.Audio",
3483 kDefaultSrtpCryptoSuite));
deadbeef1dcb1642017-03-29 21:08:16 -07003484}
3485
3486// Test getting cipher stats and UMA metrics when DTLS 1.2 is negotiated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003487TEST_P(PeerConnectionIntegrationTest, Dtls12CipherStatsAndUmaMetrics) {
deadbeef1dcb1642017-03-29 21:08:16 -07003488 PeerConnectionFactory::Options dtls_12_options;
3489 dtls_12_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
3490 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(dtls_12_options,
3491 dtls_12_options));
3492 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08003493 caller()->AddAudioVideoTracks();
3494 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003495 caller()->CreateAndSetAndSignalOffer();
Qingsi Wang7fc821d2018-07-12 12:54:53 -07003496 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07003497 EXPECT_TRUE_WAIT(rtc::SSLStreamAdapter::IsAcceptableCipher(
deadbeefd8ad7882017-04-18 16:01:17 -07003498 caller()->OldGetStats()->DtlsCipher(), rtc::KT_DEFAULT),
deadbeef1dcb1642017-03-29 21:08:16 -07003499 kDefaultTimeout);
3500 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
deadbeefd8ad7882017-04-18 16:01:17 -07003501 caller()->OldGetStats()->SrtpCipher(), kDefaultTimeout);
Qingsi Wang7fc821d2018-07-12 12:54:53 -07003502 // TODO(bugs.webrtc.org/9456): Fix it.
Ying Wangef3998f2019-12-09 13:06:53 +01003503 EXPECT_METRIC_EQ(1, webrtc::metrics::NumEvents(
3504 "WebRTC.PeerConnection.SrtpCryptoSuite.Audio",
3505 kDefaultSrtpCryptoSuite));
deadbeef1dcb1642017-03-29 21:08:16 -07003506}
3507
3508// Test that DTLS 1.0 can be used if the caller supports DTLS 1.2 and the
3509// callee only supports 1.0.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003510TEST_P(PeerConnectionIntegrationTest, CallerDtls12ToCalleeDtls10) {
deadbeef1dcb1642017-03-29 21:08:16 -07003511 PeerConnectionFactory::Options caller_options;
3512 caller_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
3513 PeerConnectionFactory::Options callee_options;
3514 callee_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
3515 ASSERT_TRUE(
3516 CreatePeerConnectionWrappersWithOptions(caller_options, callee_options));
3517 ConnectFakeSignaling();
3518 // Do normal offer/answer and wait for some frames to be received in each
3519 // direction.
Steve Anton15324772018-01-16 10:26:49 -08003520 caller()->AddAudioVideoTracks();
3521 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003522 caller()->CreateAndSetAndSignalOffer();
3523 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08003524 MediaExpectations media_expectations;
3525 media_expectations.ExpectBidirectionalAudioAndVideo();
3526 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003527}
3528
3529// Test that DTLS 1.0 can be used if the caller only supports DTLS 1.0 and the
3530// callee supports 1.2.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003531TEST_P(PeerConnectionIntegrationTest, CallerDtls10ToCalleeDtls12) {
deadbeef1dcb1642017-03-29 21:08:16 -07003532 PeerConnectionFactory::Options caller_options;
3533 caller_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
3534 PeerConnectionFactory::Options callee_options;
3535 callee_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
3536 ASSERT_TRUE(
3537 CreatePeerConnectionWrappersWithOptions(caller_options, callee_options));
3538 ConnectFakeSignaling();
3539 // Do normal offer/answer and wait for some frames to be received in each
3540 // direction.
Steve Anton15324772018-01-16 10:26:49 -08003541 caller()->AddAudioVideoTracks();
3542 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003543 caller()->CreateAndSetAndSignalOffer();
3544 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08003545 MediaExpectations media_expectations;
3546 media_expectations.ExpectBidirectionalAudioAndVideo();
3547 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003548}
3549
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07003550// The three tests below verify that "enable_aes128_sha1_32_crypto_cipher"
3551// works as expected; the cipher should only be used if enabled by both sides.
3552TEST_P(PeerConnectionIntegrationTest,
3553 Aes128Sha1_32_CipherNotUsedWhenOnlyCallerSupported) {
3554 PeerConnectionFactory::Options caller_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07003555 caller_options.crypto_options.srtp.enable_aes128_sha1_32_crypto_cipher = true;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07003556 PeerConnectionFactory::Options callee_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07003557 callee_options.crypto_options.srtp.enable_aes128_sha1_32_crypto_cipher =
3558 false;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07003559 int expected_cipher_suite = rtc::SRTP_AES128_CM_SHA1_80;
3560 TestNegotiatedCipherSuite(caller_options, callee_options,
3561 expected_cipher_suite);
3562}
3563
3564TEST_P(PeerConnectionIntegrationTest,
3565 Aes128Sha1_32_CipherNotUsedWhenOnlyCalleeSupported) {
3566 PeerConnectionFactory::Options caller_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07003567 caller_options.crypto_options.srtp.enable_aes128_sha1_32_crypto_cipher =
3568 false;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07003569 PeerConnectionFactory::Options callee_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07003570 callee_options.crypto_options.srtp.enable_aes128_sha1_32_crypto_cipher = true;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07003571 int expected_cipher_suite = rtc::SRTP_AES128_CM_SHA1_80;
3572 TestNegotiatedCipherSuite(caller_options, callee_options,
3573 expected_cipher_suite);
3574}
3575
3576TEST_P(PeerConnectionIntegrationTest, Aes128Sha1_32_CipherUsedWhenSupported) {
3577 PeerConnectionFactory::Options caller_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07003578 caller_options.crypto_options.srtp.enable_aes128_sha1_32_crypto_cipher = true;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07003579 PeerConnectionFactory::Options callee_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07003580 callee_options.crypto_options.srtp.enable_aes128_sha1_32_crypto_cipher = true;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07003581 int expected_cipher_suite = rtc::SRTP_AES128_CM_SHA1_32;
3582 TestNegotiatedCipherSuite(caller_options, callee_options,
3583 expected_cipher_suite);
3584}
3585
deadbeef1dcb1642017-03-29 21:08:16 -07003586// Test that a non-GCM cipher is used if both sides only support non-GCM.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003587TEST_P(PeerConnectionIntegrationTest, NonGcmCipherUsedWhenGcmNotSupported) {
deadbeef1dcb1642017-03-29 21:08:16 -07003588 bool local_gcm_enabled = false;
3589 bool remote_gcm_enabled = false;
Philipp Hancke2ebbff82019-10-26 06:12:55 +02003590 bool aes_ctr_enabled = true;
deadbeef1dcb1642017-03-29 21:08:16 -07003591 int expected_cipher_suite = kDefaultSrtpCryptoSuite;
3592 TestGcmNegotiationUsesCipherSuite(local_gcm_enabled, remote_gcm_enabled,
Philipp Hancke2ebbff82019-10-26 06:12:55 +02003593 aes_ctr_enabled, expected_cipher_suite);
deadbeef1dcb1642017-03-29 21:08:16 -07003594}
3595
Philipp Hancke2ebbff82019-10-26 06:12:55 +02003596// Test that a GCM cipher is used if both ends support it and non-GCM is
3597// disabled.
3598TEST_P(PeerConnectionIntegrationTest, GcmCipherUsedWhenOnlyGcmSupported) {
deadbeef1dcb1642017-03-29 21:08:16 -07003599 bool local_gcm_enabled = true;
3600 bool remote_gcm_enabled = true;
Philipp Hancke2ebbff82019-10-26 06:12:55 +02003601 bool aes_ctr_enabled = false;
deadbeef1dcb1642017-03-29 21:08:16 -07003602 int expected_cipher_suite = kDefaultSrtpCryptoSuiteGcm;
3603 TestGcmNegotiationUsesCipherSuite(local_gcm_enabled, remote_gcm_enabled,
Philipp Hancke2ebbff82019-10-26 06:12:55 +02003604 aes_ctr_enabled, expected_cipher_suite);
deadbeef1dcb1642017-03-29 21:08:16 -07003605}
3606
deadbeef7914b8c2017-04-21 03:23:33 -07003607// Verify that media can be transmitted end-to-end when GCM crypto suites are
3608// enabled. Note that the above tests, such as GcmCipherUsedWhenGcmSupported,
3609// only verify that a GCM cipher is negotiated, and not necessarily that SRTP
3610// works with it.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003611TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithGcmCipher) {
deadbeef7914b8c2017-04-21 03:23:33 -07003612 PeerConnectionFactory::Options gcm_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07003613 gcm_options.crypto_options.srtp.enable_gcm_crypto_suites = true;
Philipp Hancke2ebbff82019-10-26 06:12:55 +02003614 gcm_options.crypto_options.srtp.enable_aes128_sha1_80_crypto_cipher = false;
deadbeef7914b8c2017-04-21 03:23:33 -07003615 ASSERT_TRUE(
3616 CreatePeerConnectionWrappersWithOptions(gcm_options, gcm_options));
3617 ConnectFakeSignaling();
3618 // Do normal offer/answer and wait for some frames to be received in each
3619 // direction.
Steve Anton15324772018-01-16 10:26:49 -08003620 caller()->AddAudioVideoTracks();
3621 callee()->AddAudioVideoTracks();
deadbeef7914b8c2017-04-21 03:23:33 -07003622 caller()->CreateAndSetAndSignalOffer();
3623 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08003624 MediaExpectations media_expectations;
3625 media_expectations.ExpectBidirectionalAudioAndVideo();
3626 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef7914b8c2017-04-21 03:23:33 -07003627}
3628
Harald Alvestrand89c40e22021-02-17 08:58:35 +00003629// This test sets up a call between two parties with audio, video and an RTP
3630// data channel.
3631TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithRtpDataChannel) {
3632 PeerConnectionInterface::RTCConfiguration rtc_config;
3633 rtc_config.enable_rtp_data_channel = true;
3634 rtc_config.enable_dtls_srtp = false;
3635 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(rtc_config, rtc_config));
3636 ConnectFakeSignaling();
3637 // Expect that data channel created on caller side will show up for callee as
3638 // well.
3639 caller()->CreateDataChannel();
3640 caller()->AddAudioVideoTracks();
3641 callee()->AddAudioVideoTracks();
3642 caller()->CreateAndSetAndSignalOffer();
3643 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3644 // Ensure the existence of the RTP data channel didn't impede audio/video.
3645 MediaExpectations media_expectations;
3646 media_expectations.ExpectBidirectionalAudioAndVideo();
3647 ASSERT_TRUE(ExpectNewFrames(media_expectations));
3648 ASSERT_NE(nullptr, caller()->data_channel());
3649 ASSERT_NE(nullptr, callee()->data_channel());
3650 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3651 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3652
3653 // Ensure data can be sent in both directions.
3654 std::string data = "hello world";
3655 SendRtpDataWithRetries(caller()->data_channel(), data, 5);
3656 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3657 kDefaultTimeout);
3658 SendRtpDataWithRetries(callee()->data_channel(), data, 5);
3659 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3660 kDefaultTimeout);
3661}
3662
3663TEST_P(PeerConnectionIntegrationTest, RtpDataChannelWorksAfterRollback) {
3664 PeerConnectionInterface::RTCConfiguration rtc_config;
3665 rtc_config.enable_rtp_data_channel = true;
3666 rtc_config.enable_dtls_srtp = false;
3667 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(rtc_config, rtc_config));
3668 ConnectFakeSignaling();
3669 auto data_channel = caller()->pc()->CreateDataChannel("label_1", nullptr);
3670 ASSERT_TRUE(data_channel.get() != nullptr);
3671 caller()->CreateAndSetAndSignalOffer();
3672 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3673
3674 caller()->CreateDataChannel("label_2", nullptr);
3675 rtc::scoped_refptr<MockSetSessionDescriptionObserver> observer(
3676 new rtc::RefCountedObject<MockSetSessionDescriptionObserver>());
3677 caller()->pc()->SetLocalDescription(observer,
3678 caller()->CreateOfferAndWait().release());
3679 EXPECT_TRUE_WAIT(observer->called(), kDefaultTimeout);
3680 caller()->Rollback();
3681
3682 std::string data = "hello world";
3683 SendRtpDataWithRetries(data_channel, data, 5);
3684 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3685 kDefaultTimeout);
3686}
3687
3688// Ensure that an RTP data channel is signaled as closed for the caller when
3689// the callee rejects it in a subsequent offer.
3690TEST_P(PeerConnectionIntegrationTest,
3691 RtpDataChannelSignaledClosedInCalleeOffer) {
3692 // Same procedure as above test.
3693 PeerConnectionInterface::RTCConfiguration rtc_config;
3694 rtc_config.enable_rtp_data_channel = true;
3695 rtc_config.enable_dtls_srtp = false;
3696 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(rtc_config, rtc_config));
3697 ConnectFakeSignaling();
3698 caller()->CreateDataChannel();
3699 caller()->AddAudioVideoTracks();
3700 callee()->AddAudioVideoTracks();
3701 caller()->CreateAndSetAndSignalOffer();
3702 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3703 ASSERT_NE(nullptr, caller()->data_channel());
3704 ASSERT_NE(nullptr, callee()->data_channel());
3705 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3706 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3707
3708 // Close the data channel on the callee, and do an updated offer/answer.
3709 callee()->data_channel()->Close();
3710 callee()->CreateAndSetAndSignalOffer();
3711 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3712 EXPECT_FALSE(caller()->data_observer()->IsOpen());
3713 EXPECT_FALSE(callee()->data_observer()->IsOpen());
3714}
3715
3716#if !defined(THREAD_SANITIZER)
3717// This test provokes TSAN errors. See bugs.webrtc.org/11282
3718
3719// Tests that data is buffered in an RTP data channel until an observer is
3720// registered for it.
3721//
3722// NOTE: RTP data channels can receive data before the underlying
3723// transport has detected that a channel is writable and thus data can be
3724// received before the data channel state changes to open. That is hard to test
3725// but the same buffering is expected to be used in that case.
3726//
3727// Use fake clock and simulated network delay so that we predictably can wait
3728// until an SCTP message has been delivered without "sleep()"ing.
3729TEST_P(PeerConnectionIntegrationTestWithFakeClock,
3730 DataBufferedUntilRtpDataChannelObserverRegistered) {
3731 virtual_socket_server()->set_delay_mean(5); // 5 ms per hop.
3732 virtual_socket_server()->UpdateDelayDistribution();
3733
3734 PeerConnectionInterface::RTCConfiguration rtc_config;
3735 rtc_config.enable_rtp_data_channel = true;
3736 rtc_config.enable_dtls_srtp = false;
3737 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(rtc_config, rtc_config));
3738 ConnectFakeSignaling();
3739 caller()->CreateDataChannel();
3740 caller()->CreateAndSetAndSignalOffer();
3741 ASSERT_TRUE(caller()->data_channel() != nullptr);
3742 ASSERT_TRUE_SIMULATED_WAIT(callee()->data_channel() != nullptr,
3743 kDefaultTimeout, FakeClock());
3744 ASSERT_TRUE_SIMULATED_WAIT(caller()->data_observer()->IsOpen(),
3745 kDefaultTimeout, FakeClock());
3746 ASSERT_EQ_SIMULATED_WAIT(DataChannelInterface::kOpen,
3747 callee()->data_channel()->state(), kDefaultTimeout,
3748 FakeClock());
3749
3750 // Unregister the observer which is normally automatically registered.
3751 callee()->data_channel()->UnregisterObserver();
3752 // Send data and advance fake clock until it should have been received.
3753 std::string data = "hello world";
3754 caller()->data_channel()->Send(DataBuffer(data));
3755 SIMULATED_WAIT(false, 50, FakeClock());
3756
3757 // Attach data channel and expect data to be received immediately. Note that
3758 // EXPECT_EQ_WAIT is used, such that the simulated clock is not advanced any
3759 // further, but data can be received even if the callback is asynchronous.
3760 MockDataChannelObserver new_observer(callee()->data_channel());
3761 EXPECT_EQ_SIMULATED_WAIT(data, new_observer.last_message(), kDefaultTimeout,
3762 FakeClock());
3763}
3764
3765#endif // !defined(THREAD_SANITIZER)
3766
3767// This test sets up a call between two parties with audio, video and but only
3768// the caller client supports RTP data channels.
3769TEST_P(PeerConnectionIntegrationTest, RtpDataChannelsRejectedByCallee) {
3770 PeerConnectionInterface::RTCConfiguration rtc_config_1;
3771 rtc_config_1.enable_rtp_data_channel = true;
3772 // Must disable DTLS to make negotiation succeed.
3773 rtc_config_1.enable_dtls_srtp = false;
3774 PeerConnectionInterface::RTCConfiguration rtc_config_2;
3775 rtc_config_2.enable_dtls_srtp = false;
3776 rtc_config_2.enable_dtls_srtp = false;
3777 ASSERT_TRUE(
3778 CreatePeerConnectionWrappersWithConfig(rtc_config_1, rtc_config_2));
3779 ConnectFakeSignaling();
3780 caller()->CreateDataChannel();
3781 ASSERT_TRUE(caller()->data_channel() != nullptr);
3782 caller()->AddAudioVideoTracks();
3783 callee()->AddAudioVideoTracks();
3784 caller()->CreateAndSetAndSignalOffer();
3785 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3786 // The caller should still have a data channel, but it should be closed, and
3787 // one should ever have been created for the callee.
3788 EXPECT_TRUE(caller()->data_channel() != nullptr);
3789 EXPECT_FALSE(caller()->data_observer()->IsOpen());
3790 EXPECT_EQ(nullptr, callee()->data_channel());
3791}
3792
3793// This test sets up a call between two parties with audio, and video. When
3794// audio and video is setup and flowing, an RTP data channel is negotiated.
3795TEST_P(PeerConnectionIntegrationTest, AddRtpDataChannelInSubsequentOffer) {
3796 PeerConnectionInterface::RTCConfiguration rtc_config;
3797 rtc_config.enable_rtp_data_channel = true;
3798 rtc_config.enable_dtls_srtp = false;
3799 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(rtc_config, rtc_config));
3800 ConnectFakeSignaling();
3801 // Do initial offer/answer with audio/video.
3802 caller()->AddAudioVideoTracks();
3803 callee()->AddAudioVideoTracks();
3804 caller()->CreateAndSetAndSignalOffer();
3805 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3806 // Create data channel and do new offer and answer.
3807 caller()->CreateDataChannel();
3808 caller()->CreateAndSetAndSignalOffer();
3809 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3810 ASSERT_NE(nullptr, caller()->data_channel());
3811 ASSERT_NE(nullptr, callee()->data_channel());
3812 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3813 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3814 // Ensure data can be sent in both directions.
3815 std::string data = "hello world";
3816 SendRtpDataWithRetries(caller()->data_channel(), data, 5);
3817 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3818 kDefaultTimeout);
3819 SendRtpDataWithRetries(callee()->data_channel(), data, 5);
3820 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3821 kDefaultTimeout);
3822}
3823
3824#ifdef WEBRTC_HAVE_SCTP
3825
3826// This test sets up a call between two parties with audio, video and an SCTP
3827// data channel.
3828TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithSctpDataChannel) {
3829 ASSERT_TRUE(CreatePeerConnectionWrappers());
3830 ConnectFakeSignaling();
3831 // Expect that data channel created on caller side will show up for callee as
3832 // well.
3833 caller()->CreateDataChannel();
3834 caller()->AddAudioVideoTracks();
3835 callee()->AddAudioVideoTracks();
3836 caller()->CreateAndSetAndSignalOffer();
3837 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3838 // Ensure the existence of the SCTP data channel didn't impede audio/video.
3839 MediaExpectations media_expectations;
3840 media_expectations.ExpectBidirectionalAudioAndVideo();
3841 ASSERT_TRUE(ExpectNewFrames(media_expectations));
3842 // Caller data channel should already exist (it created one). Callee data
3843 // channel may not exist yet, since negotiation happens in-band, not in SDP.
3844 ASSERT_NE(nullptr, caller()->data_channel());
3845 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3846 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3847 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3848
3849 // Ensure data can be sent in both directions.
3850 std::string data = "hello world";
3851 caller()->data_channel()->Send(DataBuffer(data));
3852 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3853 kDefaultTimeout);
3854 callee()->data_channel()->Send(DataBuffer(data));
3855 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3856 kDefaultTimeout);
3857}
3858
3859// Ensure that when the callee closes an SCTP data channel, the closing
3860// procedure results in the data channel being closed for the caller as well.
3861TEST_P(PeerConnectionIntegrationTest, CalleeClosesSctpDataChannel) {
3862 // Same procedure as above test.
3863 ASSERT_TRUE(CreatePeerConnectionWrappers());
3864 ConnectFakeSignaling();
3865 caller()->CreateDataChannel();
3866 caller()->AddAudioVideoTracks();
3867 callee()->AddAudioVideoTracks();
3868 caller()->CreateAndSetAndSignalOffer();
3869 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3870 ASSERT_NE(nullptr, caller()->data_channel());
3871 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3872 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3873 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3874
3875 // Close the data channel on the callee side, and wait for it to reach the
3876 // "closed" state on both sides.
3877 callee()->data_channel()->Close();
3878 EXPECT_TRUE_WAIT(!caller()->data_observer()->IsOpen(), kDefaultTimeout);
3879 EXPECT_TRUE_WAIT(!callee()->data_observer()->IsOpen(), kDefaultTimeout);
3880}
3881
3882TEST_P(PeerConnectionIntegrationTest, SctpDataChannelConfigSentToOtherSide) {
3883 ASSERT_TRUE(CreatePeerConnectionWrappers());
3884 ConnectFakeSignaling();
3885 webrtc::DataChannelInit init;
3886 init.id = 53;
3887 init.maxRetransmits = 52;
3888 caller()->CreateDataChannel("data-channel", &init);
3889 caller()->AddAudioVideoTracks();
3890 callee()->AddAudioVideoTracks();
3891 caller()->CreateAndSetAndSignalOffer();
3892 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3893 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3894 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3895 // Since "negotiated" is false, the "id" parameter should be ignored.
3896 EXPECT_NE(init.id, callee()->data_channel()->id());
3897 EXPECT_EQ("data-channel", callee()->data_channel()->label());
3898 EXPECT_EQ(init.maxRetransmits, callee()->data_channel()->maxRetransmits());
3899 EXPECT_FALSE(callee()->data_channel()->negotiated());
3900}
3901
3902// Test usrsctp's ability to process unordered data stream, where data actually
3903// arrives out of order using simulated delays. Previously there have been some
3904// bugs in this area.
3905TEST_P(PeerConnectionIntegrationTest, StressTestUnorderedSctpDataChannel) {
3906 // Introduce random network delays.
3907 // Otherwise it's not a true "unordered" test.
3908 virtual_socket_server()->set_delay_mean(20);
3909 virtual_socket_server()->set_delay_stddev(5);
3910 virtual_socket_server()->UpdateDelayDistribution();
3911 // Normal procedure, but with unordered data channel config.
3912 ASSERT_TRUE(CreatePeerConnectionWrappers());
3913 ConnectFakeSignaling();
3914 webrtc::DataChannelInit init;
3915 init.ordered = false;
3916 caller()->CreateDataChannel(&init);
3917 caller()->CreateAndSetAndSignalOffer();
3918 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3919 ASSERT_NE(nullptr, caller()->data_channel());
3920 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3921 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3922 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3923
3924 static constexpr int kNumMessages = 100;
3925 // Deliberately chosen to be larger than the MTU so messages get fragmented.
3926 static constexpr size_t kMaxMessageSize = 4096;
3927 // Create and send random messages.
3928 std::vector<std::string> sent_messages;
3929 for (int i = 0; i < kNumMessages; ++i) {
3930 size_t length =
3931 (rand() % kMaxMessageSize) + 1; // NOLINT (rand_r instead of rand)
3932 std::string message;
3933 ASSERT_TRUE(rtc::CreateRandomString(length, &message));
3934 caller()->data_channel()->Send(DataBuffer(message));
3935 callee()->data_channel()->Send(DataBuffer(message));
3936 sent_messages.push_back(message);
3937 }
3938
3939 // Wait for all messages to be received.
3940 EXPECT_EQ_WAIT(rtc::checked_cast<size_t>(kNumMessages),
3941 caller()->data_observer()->received_message_count(),
3942 kDefaultTimeout);
3943 EXPECT_EQ_WAIT(rtc::checked_cast<size_t>(kNumMessages),
3944 callee()->data_observer()->received_message_count(),
3945 kDefaultTimeout);
3946
3947 // Sort and compare to make sure none of the messages were corrupted.
3948 std::vector<std::string> caller_received_messages =
3949 caller()->data_observer()->messages();
3950 std::vector<std::string> callee_received_messages =
3951 callee()->data_observer()->messages();
3952 absl::c_sort(sent_messages);
3953 absl::c_sort(caller_received_messages);
3954 absl::c_sort(callee_received_messages);
3955 EXPECT_EQ(sent_messages, caller_received_messages);
3956 EXPECT_EQ(sent_messages, callee_received_messages);
3957}
3958
3959// This test sets up a call between two parties with audio, and video. When
3960// audio and video are setup and flowing, an SCTP data channel is negotiated.
3961TEST_P(PeerConnectionIntegrationTest, AddSctpDataChannelInSubsequentOffer) {
3962 ASSERT_TRUE(CreatePeerConnectionWrappers());
3963 ConnectFakeSignaling();
3964 // Do initial offer/answer with audio/video.
3965 caller()->AddAudioVideoTracks();
3966 callee()->AddAudioVideoTracks();
3967 caller()->CreateAndSetAndSignalOffer();
3968 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3969 // Create data channel and do new offer and answer.
3970 caller()->CreateDataChannel();
3971 caller()->CreateAndSetAndSignalOffer();
3972 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3973 // Caller data channel should already exist (it created one). Callee data
3974 // channel may not exist yet, since negotiation happens in-band, not in SDP.
3975 ASSERT_NE(nullptr, caller()->data_channel());
3976 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3977 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3978 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3979 // Ensure data can be sent in both directions.
3980 std::string data = "hello world";
3981 caller()->data_channel()->Send(DataBuffer(data));
3982 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3983 kDefaultTimeout);
3984 callee()->data_channel()->Send(DataBuffer(data));
3985 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3986 kDefaultTimeout);
3987}
3988
3989// Set up a connection initially just using SCTP data channels, later upgrading
3990// to audio/video, ensuring frames are received end-to-end. Effectively the
3991// inverse of the test above.
3992// This was broken in M57; see https://crbug.com/711243
3993TEST_P(PeerConnectionIntegrationTest, SctpDataChannelToAudioVideoUpgrade) {
3994 ASSERT_TRUE(CreatePeerConnectionWrappers());
3995 ConnectFakeSignaling();
3996 // Do initial offer/answer with just data channel.
3997 caller()->CreateDataChannel();
3998 caller()->CreateAndSetAndSignalOffer();
3999 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4000 // Wait until data can be sent over the data channel.
4001 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
4002 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
4003 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
4004
4005 // Do subsequent offer/answer with two-way audio and video. Audio and video
4006 // should end up bundled on the DTLS/ICE transport already used for data.
4007 caller()->AddAudioVideoTracks();
4008 callee()->AddAudioVideoTracks();
4009 caller()->CreateAndSetAndSignalOffer();
4010 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4011 MediaExpectations media_expectations;
4012 media_expectations.ExpectBidirectionalAudioAndVideo();
4013 ASSERT_TRUE(ExpectNewFrames(media_expectations));
4014}
4015
4016static void MakeSpecCompliantSctpOffer(cricket::SessionDescription* desc) {
4017 cricket::SctpDataContentDescription* dcd_offer =
4018 GetFirstSctpDataContentDescription(desc);
4019 // See https://crbug.com/webrtc/11211 - this function is a no-op
4020 ASSERT_TRUE(dcd_offer);
4021 dcd_offer->set_use_sctpmap(false);
4022 dcd_offer->set_protocol("UDP/DTLS/SCTP");
4023}
4024
4025// Test that the data channel works when a spec-compliant SCTP m= section is
4026// offered (using "a=sctp-port" instead of "a=sctpmap", and using
4027// "UDP/DTLS/SCTP" as the protocol).
4028TEST_P(PeerConnectionIntegrationTest,
4029 DataChannelWorksWhenSpecCompliantSctpOfferReceived) {
4030 ASSERT_TRUE(CreatePeerConnectionWrappers());
4031 ConnectFakeSignaling();
4032 caller()->CreateDataChannel();
4033 caller()->SetGeneratedSdpMunger(MakeSpecCompliantSctpOffer);
4034 caller()->CreateAndSetAndSignalOffer();
4035 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4036 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
4037 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
4038 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
4039
4040 // Ensure data can be sent in both directions.
4041 std::string data = "hello world";
4042 caller()->data_channel()->Send(DataBuffer(data));
4043 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
4044 kDefaultTimeout);
4045 callee()->data_channel()->Send(DataBuffer(data));
4046 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
4047 kDefaultTimeout);
4048}
4049
4050#endif // WEBRTC_HAVE_SCTP
4051
deadbeef1dcb1642017-03-29 21:08:16 -07004052// Test that the ICE connection and gathering states eventually reach
4053// "complete".
Seth Hampson2f0d7022018-02-20 11:54:42 -08004054TEST_P(PeerConnectionIntegrationTest, IceStatesReachCompletion) {
deadbeef1dcb1642017-03-29 21:08:16 -07004055 ASSERT_TRUE(CreatePeerConnectionWrappers());
4056 ConnectFakeSignaling();
4057 // Do normal offer/answer.
Steve Anton15324772018-01-16 10:26:49 -08004058 caller()->AddAudioVideoTracks();
4059 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07004060 caller()->CreateAndSetAndSignalOffer();
4061 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4062 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceGatheringComplete,
4063 caller()->ice_gathering_state(), kMaxWaitForFramesMs);
4064 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceGatheringComplete,
4065 callee()->ice_gathering_state(), kMaxWaitForFramesMs);
4066 // After the best candidate pair is selected and all candidates are signaled,
4067 // the ICE connection state should reach "complete".
4068 // TODO(deadbeef): Currently, the ICE "controlled" agent (the
4069 // answerer/"callee" by default) only reaches "connected". When this is
4070 // fixed, this test should be updated.
4071 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
4072 caller()->ice_connection_state(), kDefaultTimeout);
Alex Loiko9289eda2018-11-23 16:18:59 +00004073 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
4074 callee()->ice_connection_state(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07004075}
4076
Harald Alvestrandec23d6d2021-02-11 10:47:22 +00004077#if !defined(THREAD_SANITIZER)
4078// This test provokes TSAN errors. See bugs.webrtc.org/3608
4079
Qingsi Wang1dac6d82018-12-12 15:28:47 -08004080constexpr int kOnlyLocalPorts = cricket::PORTALLOCATOR_DISABLE_STUN |
4081 cricket::PORTALLOCATOR_DISABLE_RELAY |
4082 cricket::PORTALLOCATOR_DISABLE_TCP;
Zach Stein6fcdc2f2018-08-23 16:25:55 -07004083
Qingsi Wang1dac6d82018-12-12 15:28:47 -08004084// Use a mock resolver to resolve the hostname back to the original IP on both
4085// sides and check that the ICE connection connects.
Zach Stein6fcdc2f2018-08-23 16:25:55 -07004086TEST_P(PeerConnectionIntegrationTest,
4087 IceStatesReachCompletionWithRemoteHostname) {
Qingsi Wang1dac6d82018-12-12 15:28:47 -08004088 auto caller_resolver_factory =
Mirko Bonadei317a1f02019-09-17 17:06:18 +02004089 std::make_unique<NiceMock<webrtc::MockAsyncResolverFactory>>();
Qingsi Wang1dac6d82018-12-12 15:28:47 -08004090 auto callee_resolver_factory =
Mirko Bonadei317a1f02019-09-17 17:06:18 +02004091 std::make_unique<NiceMock<webrtc::MockAsyncResolverFactory>>();
Qingsi Wang1dac6d82018-12-12 15:28:47 -08004092 NiceMock<rtc::MockAsyncResolver> callee_async_resolver;
4093 NiceMock<rtc::MockAsyncResolver> caller_async_resolver;
Zach Stein6fcdc2f2018-08-23 16:25:55 -07004094
4095 // This also verifies that the injected AsyncResolverFactory is used by
4096 // P2PTransportChannel.
Qingsi Wang1dac6d82018-12-12 15:28:47 -08004097 EXPECT_CALL(*caller_resolver_factory, Create())
4098 .WillOnce(Return(&caller_async_resolver));
4099 webrtc::PeerConnectionDependencies caller_deps(nullptr);
4100 caller_deps.async_resolver_factory = std::move(caller_resolver_factory);
4101
4102 EXPECT_CALL(*callee_resolver_factory, Create())
4103 .WillOnce(Return(&callee_async_resolver));
4104 webrtc::PeerConnectionDependencies callee_deps(nullptr);
4105 callee_deps.async_resolver_factory = std::move(callee_resolver_factory);
4106
4107 PeerConnectionInterface::RTCConfiguration config;
4108 config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
4109 config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyRequire;
4110
4111 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndDeps(
4112 config, std::move(caller_deps), config, std::move(callee_deps)));
4113
4114 caller()->SetRemoteAsyncResolver(&callee_async_resolver);
4115 callee()->SetRemoteAsyncResolver(&caller_async_resolver);
4116
4117 // Enable hostname candidates with mDNS names.
Qingsi Wangecd30542019-05-22 14:34:56 -07004118 caller()->SetMdnsResponder(
Mirko Bonadei317a1f02019-09-17 17:06:18 +02004119 std::make_unique<webrtc::FakeMdnsResponder>(network_thread()));
Qingsi Wangecd30542019-05-22 14:34:56 -07004120 callee()->SetMdnsResponder(
Mirko Bonadei317a1f02019-09-17 17:06:18 +02004121 std::make_unique<webrtc::FakeMdnsResponder>(network_thread()));
Qingsi Wang1dac6d82018-12-12 15:28:47 -08004122
4123 SetPortAllocatorFlags(kOnlyLocalPorts, kOnlyLocalPorts);
Zach Stein6fcdc2f2018-08-23 16:25:55 -07004124
4125 ConnectFakeSignaling();
4126 caller()->AddAudioVideoTracks();
4127 callee()->AddAudioVideoTracks();
4128 caller()->CreateAndSetAndSignalOffer();
4129 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4130 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
4131 caller()->ice_connection_state(), kDefaultTimeout);
4132 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
4133 callee()->ice_connection_state(), kDefaultTimeout);
Jeroen de Borst833979f2018-12-13 08:25:54 -08004134
Ying Wangef3998f2019-12-09 13:06:53 +01004135 EXPECT_METRIC_EQ(1, webrtc::metrics::NumEvents(
4136 "WebRTC.PeerConnection.CandidatePairType_UDP",
4137 webrtc::kIceCandidatePairHostNameHostName));
Zach Stein6fcdc2f2018-08-23 16:25:55 -07004138}
4139
Harald Alvestrandec23d6d2021-02-11 10:47:22 +00004140#endif // !defined(THREAD_SANITIZER)
4141
Steve Antonede9ca52017-10-16 13:04:27 -07004142// Test that firewalling the ICE connection causes the clients to identify the
4143// disconnected state and then removing the firewall causes them to reconnect.
4144class PeerConnectionIntegrationIceStatesTest
Seth Hampson2f0d7022018-02-20 11:54:42 -08004145 : public PeerConnectionIntegrationBaseTest,
4146 public ::testing::WithParamInterface<
4147 std::tuple<SdpSemantics, std::tuple<std::string, uint32_t>>> {
Steve Antonede9ca52017-10-16 13:04:27 -07004148 protected:
Seth Hampson2f0d7022018-02-20 11:54:42 -08004149 PeerConnectionIntegrationIceStatesTest()
4150 : PeerConnectionIntegrationBaseTest(std::get<0>(GetParam())) {
4151 port_allocator_flags_ = std::get<1>(std::get<1>(GetParam()));
Steve Antonede9ca52017-10-16 13:04:27 -07004152 }
4153
4154 void StartStunServer(const SocketAddress& server_address) {
4155 stun_server_.reset(
Niels Möller091617d2020-12-02 15:32:08 +01004156 cricket::TestStunServer::Create(firewall(), server_address));
Steve Antonede9ca52017-10-16 13:04:27 -07004157 }
4158
4159 bool TestIPv6() {
4160 return (port_allocator_flags_ & cricket::PORTALLOCATOR_ENABLE_IPV6);
4161 }
4162
4163 void SetPortAllocatorFlags() {
Qingsi Wang1dac6d82018-12-12 15:28:47 -08004164 PeerConnectionIntegrationBaseTest::SetPortAllocatorFlags(
4165 port_allocator_flags_, port_allocator_flags_);
Steve Antonede9ca52017-10-16 13:04:27 -07004166 }
4167
4168 std::vector<SocketAddress> CallerAddresses() {
4169 std::vector<SocketAddress> addresses;
4170 addresses.push_back(SocketAddress("1.1.1.1", 0));
4171 if (TestIPv6()) {
4172 addresses.push_back(SocketAddress("1111:0:a:b:c:d:e:f", 0));
4173 }
4174 return addresses;
4175 }
4176
4177 std::vector<SocketAddress> CalleeAddresses() {
4178 std::vector<SocketAddress> addresses;
4179 addresses.push_back(SocketAddress("2.2.2.2", 0));
4180 if (TestIPv6()) {
4181 addresses.push_back(SocketAddress("2222:0:a:b:c:d:e:f", 0));
4182 }
4183 return addresses;
4184 }
4185
4186 void SetUpNetworkInterfaces() {
4187 // Remove the default interfaces added by the test infrastructure.
Qingsi Wangecd30542019-05-22 14:34:56 -07004188 caller()->network_manager()->RemoveInterface(kDefaultLocalAddress);
4189 callee()->network_manager()->RemoveInterface(kDefaultLocalAddress);
Steve Antonede9ca52017-10-16 13:04:27 -07004190
4191 // Add network addresses for test.
4192 for (const auto& caller_address : CallerAddresses()) {
Qingsi Wangecd30542019-05-22 14:34:56 -07004193 caller()->network_manager()->AddInterface(caller_address);
Steve Antonede9ca52017-10-16 13:04:27 -07004194 }
4195 for (const auto& callee_address : CalleeAddresses()) {
Qingsi Wangecd30542019-05-22 14:34:56 -07004196 callee()->network_manager()->AddInterface(callee_address);
Steve Antonede9ca52017-10-16 13:04:27 -07004197 }
4198 }
4199
4200 private:
4201 uint32_t port_allocator_flags_;
4202 std::unique_ptr<cricket::TestStunServer> stun_server_;
4203};
4204
Yves Gerey100fe632020-01-17 19:15:53 +01004205// Ensure FakeClockForTest is constructed first (see class for rationale).
4206class PeerConnectionIntegrationIceStatesTestWithFakeClock
4207 : public FakeClockForTest,
4208 public PeerConnectionIntegrationIceStatesTest {};
4209
Harald Alvestrandec23d6d2021-02-11 10:47:22 +00004210#if !defined(THREAD_SANITIZER)
4211// This test provokes TSAN errors. bugs.webrtc.org/11282
4212
Steve Antonede9ca52017-10-16 13:04:27 -07004213// Tests that the PeerConnection goes through all the ICE gathering/connection
4214// states over the duration of the call. This includes Disconnected and Failed
4215// states, induced by putting a firewall between the peers and waiting for them
4216// to time out.
Yves Gerey100fe632020-01-17 19:15:53 +01004217TEST_P(PeerConnectionIntegrationIceStatesTestWithFakeClock, VerifyIceStates) {
Steve Antonede9ca52017-10-16 13:04:27 -07004218 const SocketAddress kStunServerAddress =
4219 SocketAddress("99.99.99.1", cricket::STUN_SERVER_PORT);
4220 StartStunServer(kStunServerAddress);
4221
4222 PeerConnectionInterface::RTCConfiguration config;
4223 PeerConnectionInterface::IceServer ice_stun_server;
4224 ice_stun_server.urls.push_back(
4225 "stun:" + kStunServerAddress.HostAsURIString() + ":" +
4226 kStunServerAddress.PortAsString());
4227 config.servers.push_back(ice_stun_server);
4228
4229 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
4230 ConnectFakeSignaling();
4231 SetPortAllocatorFlags();
4232 SetUpNetworkInterfaces();
Steve Anton15324772018-01-16 10:26:49 -08004233 caller()->AddAudioVideoTracks();
4234 callee()->AddAudioVideoTracks();
Steve Antonede9ca52017-10-16 13:04:27 -07004235
4236 // Initial state before anything happens.
4237 ASSERT_EQ(PeerConnectionInterface::kIceGatheringNew,
4238 caller()->ice_gathering_state());
4239 ASSERT_EQ(PeerConnectionInterface::kIceConnectionNew,
4240 caller()->ice_connection_state());
Jonas Olsson7a6739e2019-01-15 16:31:55 +01004241 ASSERT_EQ(PeerConnectionInterface::kIceConnectionNew,
4242 caller()->standardized_ice_connection_state());
Steve Antonede9ca52017-10-16 13:04:27 -07004243
4244 // Start the call by creating the offer, setting it as the local description,
4245 // then sending it to the peer who will respond with an answer. This happens
4246 // asynchronously so that we can watch the states as it runs in the
4247 // background.
4248 caller()->CreateAndSetAndSignalOffer();
4249
Steve Antona9b67ce2020-01-16 14:00:44 -08004250 ASSERT_EQ_SIMULATED_WAIT(PeerConnectionInterface::kIceConnectionCompleted,
4251 caller()->ice_connection_state(), kDefaultTimeout,
Yves Gerey100fe632020-01-17 19:15:53 +01004252 FakeClock());
Steve Antona9b67ce2020-01-16 14:00:44 -08004253 ASSERT_EQ_SIMULATED_WAIT(PeerConnectionInterface::kIceConnectionCompleted,
4254 caller()->standardized_ice_connection_state(),
Yves Gerey100fe632020-01-17 19:15:53 +01004255 kDefaultTimeout, FakeClock());
Steve Antonede9ca52017-10-16 13:04:27 -07004256
4257 // Verify that the observer was notified of the intermediate transitions.
4258 EXPECT_THAT(caller()->ice_connection_state_history(),
4259 ElementsAre(PeerConnectionInterface::kIceConnectionChecking,
4260 PeerConnectionInterface::kIceConnectionConnected,
4261 PeerConnectionInterface::kIceConnectionCompleted));
Jonas Olssonacd8ae72019-02-25 15:26:24 +01004262 EXPECT_THAT(caller()->standardized_ice_connection_state_history(),
4263 ElementsAre(PeerConnectionInterface::kIceConnectionChecking,
4264 PeerConnectionInterface::kIceConnectionConnected,
4265 PeerConnectionInterface::kIceConnectionCompleted));
Jonas Olsson635474e2018-10-18 15:58:17 +02004266 EXPECT_THAT(
4267 caller()->peer_connection_state_history(),
4268 ElementsAre(PeerConnectionInterface::PeerConnectionState::kConnecting,
Jonas Olsson635474e2018-10-18 15:58:17 +02004269 PeerConnectionInterface::PeerConnectionState::kConnected));
Steve Antonede9ca52017-10-16 13:04:27 -07004270 EXPECT_THAT(caller()->ice_gathering_state_history(),
4271 ElementsAre(PeerConnectionInterface::kIceGatheringGathering,
4272 PeerConnectionInterface::kIceGatheringComplete));
4273
4274 // Block connections to/from the caller and wait for ICE to become
4275 // disconnected.
4276 for (const auto& caller_address : CallerAddresses()) {
4277 firewall()->AddRule(false, rtc::FP_ANY, rtc::FD_ANY, caller_address);
4278 }
Mirko Bonadei675513b2017-11-09 11:09:25 +01004279 RTC_LOG(LS_INFO) << "Firewall rules applied";
Jonas Olssonb75d9e92019-02-22 10:33:29 +01004280 ASSERT_EQ_SIMULATED_WAIT(PeerConnectionInterface::kIceConnectionDisconnected,
4281 caller()->ice_connection_state(), kDefaultTimeout,
Yves Gerey100fe632020-01-17 19:15:53 +01004282 FakeClock());
Jonas Olssonb75d9e92019-02-22 10:33:29 +01004283 ASSERT_EQ_SIMULATED_WAIT(PeerConnectionInterface::kIceConnectionDisconnected,
4284 caller()->standardized_ice_connection_state(),
Yves Gerey100fe632020-01-17 19:15:53 +01004285 kDefaultTimeout, FakeClock());
Steve Antonede9ca52017-10-16 13:04:27 -07004286
4287 // Let ICE re-establish by removing the firewall rules.
4288 firewall()->ClearRules();
Mirko Bonadei675513b2017-11-09 11:09:25 +01004289 RTC_LOG(LS_INFO) << "Firewall rules cleared";
Jonas Olssonb75d9e92019-02-22 10:33:29 +01004290 ASSERT_EQ_SIMULATED_WAIT(PeerConnectionInterface::kIceConnectionCompleted,
4291 caller()->ice_connection_state(), kDefaultTimeout,
Yves Gerey100fe632020-01-17 19:15:53 +01004292 FakeClock());
Jonas Olssonacd8ae72019-02-25 15:26:24 +01004293 ASSERT_EQ_SIMULATED_WAIT(PeerConnectionInterface::kIceConnectionCompleted,
Jonas Olssonb75d9e92019-02-22 10:33:29 +01004294 caller()->standardized_ice_connection_state(),
Yves Gerey100fe632020-01-17 19:15:53 +01004295 kDefaultTimeout, FakeClock());
Steve Antonede9ca52017-10-16 13:04:27 -07004296
4297 // According to RFC7675, if there is no response within 30 seconds then the
4298 // peer should consider the other side to have rejected the connection. This
Steve Anton83119dd2017-11-10 16:19:52 -08004299 // is signaled by the state transitioning to "failed".
Steve Antonede9ca52017-10-16 13:04:27 -07004300 constexpr int kConsentTimeout = 30000;
4301 for (const auto& caller_address : CallerAddresses()) {
4302 firewall()->AddRule(false, rtc::FP_ANY, rtc::FD_ANY, caller_address);
4303 }
Mirko Bonadei675513b2017-11-09 11:09:25 +01004304 RTC_LOG(LS_INFO) << "Firewall rules applied again";
Jonas Olssonb75d9e92019-02-22 10:33:29 +01004305 ASSERT_EQ_SIMULATED_WAIT(PeerConnectionInterface::kIceConnectionFailed,
4306 caller()->ice_connection_state(), kConsentTimeout,
Yves Gerey100fe632020-01-17 19:15:53 +01004307 FakeClock());
Jonas Olssonb75d9e92019-02-22 10:33:29 +01004308 ASSERT_EQ_SIMULATED_WAIT(PeerConnectionInterface::kIceConnectionFailed,
4309 caller()->standardized_ice_connection_state(),
Yves Gerey100fe632020-01-17 19:15:53 +01004310 kConsentTimeout, FakeClock());
Jonas Olssonb75d9e92019-02-22 10:33:29 +01004311}
4312
4313// Tests that if the connection doesn't get set up properly we eventually reach
4314// the "failed" iceConnectionState.
Yves Gerey100fe632020-01-17 19:15:53 +01004315TEST_P(PeerConnectionIntegrationIceStatesTestWithFakeClock,
4316 IceStateSetupFailure) {
Jonas Olssonb75d9e92019-02-22 10:33:29 +01004317 // Block connections to/from the caller and wait for ICE to become
4318 // disconnected.
4319 for (const auto& caller_address : CallerAddresses()) {
4320 firewall()->AddRule(false, rtc::FP_ANY, rtc::FD_ANY, caller_address);
4321 }
4322
4323 ASSERT_TRUE(CreatePeerConnectionWrappers());
4324 ConnectFakeSignaling();
4325 SetPortAllocatorFlags();
4326 SetUpNetworkInterfaces();
4327 caller()->AddAudioVideoTracks();
4328 caller()->CreateAndSetAndSignalOffer();
4329
4330 // According to RFC7675, if there is no response within 30 seconds then the
4331 // peer should consider the other side to have rejected the connection. This
4332 // is signaled by the state transitioning to "failed".
4333 constexpr int kConsentTimeout = 30000;
4334 ASSERT_EQ_SIMULATED_WAIT(PeerConnectionInterface::kIceConnectionFailed,
4335 caller()->standardized_ice_connection_state(),
Yves Gerey100fe632020-01-17 19:15:53 +01004336 kConsentTimeout, FakeClock());
Steve Antonede9ca52017-10-16 13:04:27 -07004337}
4338
Harald Alvestrandec23d6d2021-02-11 10:47:22 +00004339#endif // !defined(THREAD_SANITIZER)
4340
Steve Antonede9ca52017-10-16 13:04:27 -07004341// Tests that the best connection is set to the appropriate IPv4/IPv6 connection
4342// and that the statistics in the metric observers are updated correctly.
4343TEST_P(PeerConnectionIntegrationIceStatesTest, VerifyBestConnection) {
4344 ASSERT_TRUE(CreatePeerConnectionWrappers());
4345 ConnectFakeSignaling();
4346 SetPortAllocatorFlags();
4347 SetUpNetworkInterfaces();
Steve Anton15324772018-01-16 10:26:49 -08004348 caller()->AddAudioVideoTracks();
4349 callee()->AddAudioVideoTracks();
Steve Antonede9ca52017-10-16 13:04:27 -07004350 caller()->CreateAndSetAndSignalOffer();
4351
4352 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Steve Anton692f3c72020-01-16 14:12:31 -08004353 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
4354 caller()->ice_connection_state(), kDefaultTimeout);
4355 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
4356 callee()->ice_connection_state(), kDefaultTimeout);
Steve Antonede9ca52017-10-16 13:04:27 -07004357
Qingsi Wang7fc821d2018-07-12 12:54:53 -07004358 // TODO(bugs.webrtc.org/9456): Fix it.
4359 const int num_best_ipv4 = webrtc::metrics::NumEvents(
4360 "WebRTC.PeerConnection.IPMetrics", webrtc::kBestConnections_IPv4);
4361 const int num_best_ipv6 = webrtc::metrics::NumEvents(
4362 "WebRTC.PeerConnection.IPMetrics", webrtc::kBestConnections_IPv6);
Steve Antonede9ca52017-10-16 13:04:27 -07004363 if (TestIPv6()) {
4364 // When IPv6 is enabled, we should prefer an IPv6 connection over an IPv4
4365 // connection.
Ying Wangef3998f2019-12-09 13:06:53 +01004366 EXPECT_METRIC_EQ(0, num_best_ipv4);
4367 EXPECT_METRIC_EQ(1, num_best_ipv6);
Steve Antonede9ca52017-10-16 13:04:27 -07004368 } else {
Ying Wangef3998f2019-12-09 13:06:53 +01004369 EXPECT_METRIC_EQ(1, num_best_ipv4);
4370 EXPECT_METRIC_EQ(0, num_best_ipv6);
Steve Antonede9ca52017-10-16 13:04:27 -07004371 }
4372
Ying Wangef3998f2019-12-09 13:06:53 +01004373 EXPECT_METRIC_EQ(0, webrtc::metrics::NumEvents(
4374 "WebRTC.PeerConnection.CandidatePairType_UDP",
4375 webrtc::kIceCandidatePairHostHost));
4376 EXPECT_METRIC_EQ(1, webrtc::metrics::NumEvents(
4377 "WebRTC.PeerConnection.CandidatePairType_UDP",
4378 webrtc::kIceCandidatePairHostPublicHostPublic));
Steve Antonede9ca52017-10-16 13:04:27 -07004379}
4380
4381constexpr uint32_t kFlagsIPv4NoStun = cricket::PORTALLOCATOR_DISABLE_TCP |
4382 cricket::PORTALLOCATOR_DISABLE_STUN |
4383 cricket::PORTALLOCATOR_DISABLE_RELAY;
4384constexpr uint32_t kFlagsIPv6NoStun =
4385 cricket::PORTALLOCATOR_DISABLE_TCP | cricket::PORTALLOCATOR_DISABLE_STUN |
4386 cricket::PORTALLOCATOR_ENABLE_IPV6 | cricket::PORTALLOCATOR_DISABLE_RELAY;
4387constexpr uint32_t kFlagsIPv4Stun =
4388 cricket::PORTALLOCATOR_DISABLE_TCP | cricket::PORTALLOCATOR_DISABLE_RELAY;
4389
Mirko Bonadeic84f6612019-01-31 12:20:57 +01004390INSTANTIATE_TEST_SUITE_P(
Seth Hampson2f0d7022018-02-20 11:54:42 -08004391 PeerConnectionIntegrationTest,
4392 PeerConnectionIntegrationIceStatesTest,
4393 Combine(Values(SdpSemantics::kPlanB, SdpSemantics::kUnifiedPlan),
4394 Values(std::make_pair("IPv4 no STUN", kFlagsIPv4NoStun),
4395 std::make_pair("IPv6 no STUN", kFlagsIPv6NoStun),
4396 std::make_pair("IPv4 with STUN", kFlagsIPv4Stun))));
Steve Antonede9ca52017-10-16 13:04:27 -07004397
Yves Gerey100fe632020-01-17 19:15:53 +01004398INSTANTIATE_TEST_SUITE_P(
4399 PeerConnectionIntegrationTest,
4400 PeerConnectionIntegrationIceStatesTestWithFakeClock,
4401 Combine(Values(SdpSemantics::kPlanB, SdpSemantics::kUnifiedPlan),
4402 Values(std::make_pair("IPv4 no STUN", kFlagsIPv4NoStun),
4403 std::make_pair("IPv6 no STUN", kFlagsIPv6NoStun),
4404 std::make_pair("IPv4 with STUN", kFlagsIPv4Stun))));
4405
deadbeef1dcb1642017-03-29 21:08:16 -07004406// This test sets up a call between two parties with audio and video.
4407// During the call, the caller restarts ICE and the test verifies that
4408// new ICE candidates are generated and audio and video still can flow, and the
4409// ICE state reaches completed again.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004410TEST_P(PeerConnectionIntegrationTest, MediaContinuesFlowingAfterIceRestart) {
deadbeef1dcb1642017-03-29 21:08:16 -07004411 ASSERT_TRUE(CreatePeerConnectionWrappers());
4412 ConnectFakeSignaling();
4413 // Do normal offer/answer and wait for ICE to complete.
Steve Anton15324772018-01-16 10:26:49 -08004414 caller()->AddAudioVideoTracks();
4415 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07004416 caller()->CreateAndSetAndSignalOffer();
4417 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4418 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
4419 caller()->ice_connection_state(), kMaxWaitForFramesMs);
Alex Loiko9289eda2018-11-23 16:18:59 +00004420 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
4421 callee()->ice_connection_state(), kMaxWaitForFramesMs);
deadbeef1dcb1642017-03-29 21:08:16 -07004422
4423 // To verify that the ICE restart actually occurs, get
4424 // ufrag/password/candidates before and after restart.
4425 // Create an SDP string of the first audio candidate for both clients.
4426 const webrtc::IceCandidateCollection* audio_candidates_caller =
4427 caller()->pc()->local_description()->candidates(0);
4428 const webrtc::IceCandidateCollection* audio_candidates_callee =
4429 callee()->pc()->local_description()->candidates(0);
4430 ASSERT_GT(audio_candidates_caller->count(), 0u);
4431 ASSERT_GT(audio_candidates_callee->count(), 0u);
4432 std::string caller_candidate_pre_restart;
4433 ASSERT_TRUE(
4434 audio_candidates_caller->at(0)->ToString(&caller_candidate_pre_restart));
4435 std::string callee_candidate_pre_restart;
4436 ASSERT_TRUE(
4437 audio_candidates_callee->at(0)->ToString(&callee_candidate_pre_restart));
4438 const cricket::SessionDescription* desc =
4439 caller()->pc()->local_description()->description();
4440 std::string caller_ufrag_pre_restart =
4441 desc->transport_infos()[0].description.ice_ufrag;
4442 desc = callee()->pc()->local_description()->description();
4443 std::string callee_ufrag_pre_restart =
4444 desc->transport_infos()[0].description.ice_ufrag;
4445
Alex Drake00c7ecf2019-08-06 10:54:47 -07004446 EXPECT_EQ(caller()->ice_candidate_pair_change_history().size(), 1u);
deadbeef1dcb1642017-03-29 21:08:16 -07004447 // Have the caller initiate an ICE restart.
4448 caller()->SetOfferAnswerOptions(IceRestartOfferAnswerOptions());
4449 caller()->CreateAndSetAndSignalOffer();
4450 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4451 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
4452 caller()->ice_connection_state(), kMaxWaitForFramesMs);
Alex Loiko9289eda2018-11-23 16:18:59 +00004453 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
deadbeef1dcb1642017-03-29 21:08:16 -07004454 callee()->ice_connection_state(), kMaxWaitForFramesMs);
4455
4456 // Grab the ufrags/candidates again.
4457 audio_candidates_caller = caller()->pc()->local_description()->candidates(0);
4458 audio_candidates_callee = callee()->pc()->local_description()->candidates(0);
4459 ASSERT_GT(audio_candidates_caller->count(), 0u);
4460 ASSERT_GT(audio_candidates_callee->count(), 0u);
4461 std::string caller_candidate_post_restart;
4462 ASSERT_TRUE(
4463 audio_candidates_caller->at(0)->ToString(&caller_candidate_post_restart));
4464 std::string callee_candidate_post_restart;
4465 ASSERT_TRUE(
4466 audio_candidates_callee->at(0)->ToString(&callee_candidate_post_restart));
4467 desc = caller()->pc()->local_description()->description();
4468 std::string caller_ufrag_post_restart =
4469 desc->transport_infos()[0].description.ice_ufrag;
4470 desc = callee()->pc()->local_description()->description();
4471 std::string callee_ufrag_post_restart =
4472 desc->transport_infos()[0].description.ice_ufrag;
4473 // Sanity check that an ICE restart was actually negotiated in SDP.
4474 ASSERT_NE(caller_candidate_pre_restart, caller_candidate_post_restart);
4475 ASSERT_NE(callee_candidate_pre_restart, callee_candidate_post_restart);
4476 ASSERT_NE(caller_ufrag_pre_restart, caller_ufrag_post_restart);
4477 ASSERT_NE(callee_ufrag_pre_restart, callee_ufrag_post_restart);
Alex Drake00c7ecf2019-08-06 10:54:47 -07004478 EXPECT_GT(caller()->ice_candidate_pair_change_history().size(), 1u);
deadbeef1dcb1642017-03-29 21:08:16 -07004479
4480 // Ensure that additional frames are received after the ICE restart.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004481 MediaExpectations media_expectations;
4482 media_expectations.ExpectBidirectionalAudioAndVideo();
4483 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07004484}
4485
4486// Verify that audio/video can be received end-to-end when ICE renomination is
4487// enabled.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004488TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithIceRenomination) {
deadbeef1dcb1642017-03-29 21:08:16 -07004489 PeerConnectionInterface::RTCConfiguration config;
4490 config.enable_ice_renomination = true;
4491 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
4492 ConnectFakeSignaling();
4493 // Do normal offer/answer and wait for some frames to be received in each
4494 // direction.
Steve Anton15324772018-01-16 10:26:49 -08004495 caller()->AddAudioVideoTracks();
4496 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07004497 caller()->CreateAndSetAndSignalOffer();
4498 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4499 // Sanity check that ICE renomination was actually negotiated.
4500 const cricket::SessionDescription* desc =
4501 caller()->pc()->local_description()->description();
4502 for (const cricket::TransportInfo& info : desc->transport_infos()) {
Steve Anton64b626b2019-01-28 17:25:26 -08004503 ASSERT_THAT(info.description.transport_options, Contains("renomination"));
deadbeef1dcb1642017-03-29 21:08:16 -07004504 }
4505 desc = callee()->pc()->local_description()->description();
4506 for (const cricket::TransportInfo& info : desc->transport_infos()) {
Steve Anton64b626b2019-01-28 17:25:26 -08004507 ASSERT_THAT(info.description.transport_options, Contains("renomination"));
deadbeef1dcb1642017-03-29 21:08:16 -07004508 }
Seth Hampson2f0d7022018-02-20 11:54:42 -08004509 MediaExpectations media_expectations;
4510 media_expectations.ExpectBidirectionalAudioAndVideo();
4511 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07004512}
4513
Steve Anton6f25b092017-10-23 09:39:20 -07004514// With a max bundle policy and RTCP muxing, adding a new media description to
4515// the connection should not affect ICE at all because the new media will use
4516// the existing connection.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004517TEST_P(PeerConnectionIntegrationTest,
Steve Anton83119dd2017-11-10 16:19:52 -08004518 AddMediaToConnectedBundleDoesNotRestartIce) {
Steve Anton6f25b092017-10-23 09:39:20 -07004519 PeerConnectionInterface::RTCConfiguration config;
4520 config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
4521 config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyRequire;
4522 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(
4523 config, PeerConnectionInterface::RTCConfiguration()));
4524 ConnectFakeSignaling();
4525
Steve Anton15324772018-01-16 10:26:49 -08004526 caller()->AddAudioTrack();
Steve Anton6f25b092017-10-23 09:39:20 -07004527 caller()->CreateAndSetAndSignalOffer();
4528 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Steve Antonff52f1b2017-10-26 12:24:50 -07004529 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceConnectionCompleted,
4530 caller()->ice_connection_state(), kDefaultTimeout);
Steve Anton6f25b092017-10-23 09:39:20 -07004531
4532 caller()->clear_ice_connection_state_history();
4533
Steve Anton15324772018-01-16 10:26:49 -08004534 caller()->AddVideoTrack();
Steve Anton6f25b092017-10-23 09:39:20 -07004535 caller()->CreateAndSetAndSignalOffer();
4536 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4537
4538 EXPECT_EQ(0u, caller()->ice_connection_state_history().size());
4539}
4540
deadbeef1dcb1642017-03-29 21:08:16 -07004541// This test sets up a call between two parties with audio and video. It then
4542// renegotiates setting the video m-line to "port 0", then later renegotiates
4543// again, enabling video.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004544TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07004545 VideoFlowsAfterMediaSectionIsRejectedAndRecycled) {
4546 ASSERT_TRUE(CreatePeerConnectionWrappers());
4547 ConnectFakeSignaling();
4548
4549 // Do initial negotiation, only sending media from the caller. Will result in
4550 // video and audio recvonly "m=" sections.
Steve Anton15324772018-01-16 10:26:49 -08004551 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07004552 caller()->CreateAndSetAndSignalOffer();
4553 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4554
4555 // Negotiate again, disabling the video "m=" section (the callee will set the
4556 // port to 0 due to offer_to_receive_video = 0).
Seth Hampson2f0d7022018-02-20 11:54:42 -08004557 if (sdp_semantics_ == SdpSemantics::kPlanB) {
4558 PeerConnectionInterface::RTCOfferAnswerOptions options;
4559 options.offer_to_receive_video = 0;
4560 callee()->SetOfferAnswerOptions(options);
4561 } else {
4562 callee()->SetRemoteOfferHandler([this] {
Harald Alvestrand6060df52020-08-11 09:54:02 +02004563 callee()
4564 ->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)
4565 ->StopInternal();
Seth Hampson2f0d7022018-02-20 11:54:42 -08004566 });
4567 }
deadbeef1dcb1642017-03-29 21:08:16 -07004568 caller()->CreateAndSetAndSignalOffer();
4569 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4570 // Sanity check that video "m=" section was actually rejected.
4571 const ContentInfo* answer_video_content = cricket::GetFirstVideoContent(
4572 callee()->pc()->local_description()->description());
4573 ASSERT_NE(nullptr, answer_video_content);
4574 ASSERT_TRUE(answer_video_content->rejected);
4575
4576 // Enable video and do negotiation again, making sure video is received
4577 // end-to-end, also adding media stream to callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004578 if (sdp_semantics_ == SdpSemantics::kPlanB) {
4579 PeerConnectionInterface::RTCOfferAnswerOptions options;
4580 options.offer_to_receive_video = 1;
4581 callee()->SetOfferAnswerOptions(options);
4582 } else {
4583 // The caller's transceiver is stopped, so we need to add another track.
4584 auto caller_transceiver =
4585 caller()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO);
Harald Alvestrand6060df52020-08-11 09:54:02 +02004586 EXPECT_EQ(nullptr, caller_transceiver.get());
Seth Hampson2f0d7022018-02-20 11:54:42 -08004587 caller()->AddVideoTrack();
4588 }
4589 callee()->AddVideoTrack();
4590 callee()->SetRemoteOfferHandler(nullptr);
deadbeef1dcb1642017-03-29 21:08:16 -07004591 caller()->CreateAndSetAndSignalOffer();
4592 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08004593
deadbeef1dcb1642017-03-29 21:08:16 -07004594 // Verify the caller receives frames from the newly added stream, and the
4595 // callee receives additional frames from the re-enabled video m= section.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004596 MediaExpectations media_expectations;
4597 media_expectations.CalleeExpectsSomeAudio();
4598 media_expectations.ExpectBidirectionalVideo();
4599 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07004600}
4601
deadbeef1dcb1642017-03-29 21:08:16 -07004602// This tests that if we negotiate after calling CreateSender but before we
4603// have a track, then set a track later, frames from the newly-set track are
4604// received end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004605TEST_F(PeerConnectionIntegrationTestPlanB,
deadbeef1dcb1642017-03-29 21:08:16 -07004606 MediaFlowsAfterEarlyWarmupWithCreateSender) {
4607 ASSERT_TRUE(CreatePeerConnectionWrappers());
4608 ConnectFakeSignaling();
4609 auto caller_audio_sender =
4610 caller()->pc()->CreateSender("audio", "caller_stream");
4611 auto caller_video_sender =
4612 caller()->pc()->CreateSender("video", "caller_stream");
4613 auto callee_audio_sender =
4614 callee()->pc()->CreateSender("audio", "callee_stream");
4615 auto callee_video_sender =
4616 callee()->pc()->CreateSender("video", "callee_stream");
4617 caller()->CreateAndSetAndSignalOffer();
4618 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
4619 // Wait for ICE to complete, without any tracks being set.
4620 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
4621 caller()->ice_connection_state(), kMaxWaitForFramesMs);
4622 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
4623 callee()->ice_connection_state(), kMaxWaitForFramesMs);
4624 // Now set the tracks, and expect frames to immediately start flowing.
4625 EXPECT_TRUE(caller_audio_sender->SetTrack(caller()->CreateLocalAudioTrack()));
4626 EXPECT_TRUE(caller_video_sender->SetTrack(caller()->CreateLocalVideoTrack()));
4627 EXPECT_TRUE(callee_audio_sender->SetTrack(callee()->CreateLocalAudioTrack()));
4628 EXPECT_TRUE(callee_video_sender->SetTrack(callee()->CreateLocalVideoTrack()));
Seth Hampson2f0d7022018-02-20 11:54:42 -08004629 MediaExpectations media_expectations;
4630 media_expectations.ExpectBidirectionalAudioAndVideo();
4631 ASSERT_TRUE(ExpectNewFrames(media_expectations));
4632}
4633
4634// This tests that if we negotiate after calling AddTransceiver but before we
4635// have a track, then set a track later, frames from the newly-set tracks are
4636// received end-to-end.
4637TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
4638 MediaFlowsAfterEarlyWarmupWithAddTransceiver) {
4639 ASSERT_TRUE(CreatePeerConnectionWrappers());
4640 ConnectFakeSignaling();
4641 auto audio_result = caller()->pc()->AddTransceiver(cricket::MEDIA_TYPE_AUDIO);
4642 ASSERT_EQ(RTCErrorType::NONE, audio_result.error().type());
4643 auto caller_audio_sender = audio_result.MoveValue()->sender();
4644 auto video_result = caller()->pc()->AddTransceiver(cricket::MEDIA_TYPE_VIDEO);
4645 ASSERT_EQ(RTCErrorType::NONE, video_result.error().type());
4646 auto caller_video_sender = video_result.MoveValue()->sender();
4647 callee()->SetRemoteOfferHandler([this] {
4648 ASSERT_EQ(2u, callee()->pc()->GetTransceivers().size());
Harald Alvestrand6060df52020-08-11 09:54:02 +02004649 callee()->pc()->GetTransceivers()[0]->SetDirectionWithError(
Seth Hampson2f0d7022018-02-20 11:54:42 -08004650 RtpTransceiverDirection::kSendRecv);
Harald Alvestrand6060df52020-08-11 09:54:02 +02004651 callee()->pc()->GetTransceivers()[1]->SetDirectionWithError(
Seth Hampson2f0d7022018-02-20 11:54:42 -08004652 RtpTransceiverDirection::kSendRecv);
4653 });
4654 caller()->CreateAndSetAndSignalOffer();
4655 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
4656 // Wait for ICE to complete, without any tracks being set.
4657 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
4658 caller()->ice_connection_state(), kMaxWaitForFramesMs);
4659 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
4660 callee()->ice_connection_state(), kMaxWaitForFramesMs);
4661 // Now set the tracks, and expect frames to immediately start flowing.
4662 auto callee_audio_sender = callee()->pc()->GetSenders()[0];
4663 auto callee_video_sender = callee()->pc()->GetSenders()[1];
4664 ASSERT_TRUE(caller_audio_sender->SetTrack(caller()->CreateLocalAudioTrack()));
4665 ASSERT_TRUE(caller_video_sender->SetTrack(caller()->CreateLocalVideoTrack()));
4666 ASSERT_TRUE(callee_audio_sender->SetTrack(callee()->CreateLocalAudioTrack()));
4667 ASSERT_TRUE(callee_video_sender->SetTrack(callee()->CreateLocalVideoTrack()));
4668 MediaExpectations media_expectations;
4669 media_expectations.ExpectBidirectionalAudioAndVideo();
4670 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07004671}
4672
4673// This test verifies that a remote video track can be added via AddStream,
4674// and sent end-to-end. For this particular test, it's simply echoed back
4675// from the caller to the callee, rather than being forwarded to a third
4676// PeerConnection.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004677TEST_F(PeerConnectionIntegrationTestPlanB, CanSendRemoteVideoTrack) {
deadbeef1dcb1642017-03-29 21:08:16 -07004678 ASSERT_TRUE(CreatePeerConnectionWrappers());
4679 ConnectFakeSignaling();
4680 // Just send a video track from the caller.
Steve Anton15324772018-01-16 10:26:49 -08004681 caller()->AddVideoTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07004682 caller()->CreateAndSetAndSignalOffer();
4683 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02004684 ASSERT_EQ(1U, callee()->remote_streams()->count());
deadbeef1dcb1642017-03-29 21:08:16 -07004685
4686 // Echo the stream back, and do a new offer/anwer (initiated by callee this
4687 // time).
4688 callee()->pc()->AddStream(callee()->remote_streams()->at(0));
4689 callee()->CreateAndSetAndSignalOffer();
4690 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
4691
Seth Hampson2f0d7022018-02-20 11:54:42 -08004692 MediaExpectations media_expectations;
4693 media_expectations.ExpectBidirectionalVideo();
4694 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07004695}
4696
Harald Alvestrandec23d6d2021-02-11 10:47:22 +00004697#if !defined(THREAD_SANITIZER)
4698// This test provokes TSAN errors. bugs.webrtc.org/11282
4699
deadbeef1dcb1642017-03-29 21:08:16 -07004700// Test that we achieve the expected end-to-end connection time, using a
4701// fake clock and simulated latency on the media and signaling paths.
4702// We use a TURN<->TURN connection because this is usually the quickest to
4703// set up initially, especially when we're confident the connection will work
4704// and can start sending media before we get a STUN response.
4705//
4706// With various optimizations enabled, here are the network delays we expect to
4707// be on the critical path:
4708// 1. 2 signaling trips: Signaling offer and offerer's TURN candidate, then
4709// signaling answer (with DTLS fingerprint).
4710// 2. 9 media hops: Rest of the DTLS handshake. 3 hops in each direction when
4711// using TURN<->TURN pair, and DTLS exchange is 4 packets,
4712// the first of which should have arrived before the answer.
Yves Gerey100fe632020-01-17 19:15:53 +01004713TEST_P(PeerConnectionIntegrationTestWithFakeClock,
4714 EndToEndConnectionTimeWithTurnTurnPair) {
deadbeef1dcb1642017-03-29 21:08:16 -07004715 static constexpr int media_hop_delay_ms = 50;
4716 static constexpr int signaling_trip_delay_ms = 500;
4717 // For explanation of these values, see comment above.
4718 static constexpr int required_media_hops = 9;
4719 static constexpr int required_signaling_trips = 2;
4720 // For internal delays (such as posting an event asychronously).
4721 static constexpr int allowed_internal_delay_ms = 20;
4722 static constexpr int total_connection_time_ms =
4723 media_hop_delay_ms * required_media_hops +
4724 signaling_trip_delay_ms * required_signaling_trips +
4725 allowed_internal_delay_ms;
4726
4727 static const rtc::SocketAddress turn_server_1_internal_address{"88.88.88.0",
4728 3478};
4729 static const rtc::SocketAddress turn_server_1_external_address{"88.88.88.1",
4730 0};
4731 static const rtc::SocketAddress turn_server_2_internal_address{"99.99.99.0",
4732 3478};
4733 static const rtc::SocketAddress turn_server_2_external_address{"99.99.99.1",
4734 0};
Seth Hampsonaed71642018-06-11 07:41:32 -07004735 cricket::TestTurnServer* turn_server_1 = CreateTurnServer(
4736 turn_server_1_internal_address, turn_server_1_external_address);
Jonas Orelandbdcee282017-10-10 14:01:40 +02004737
Seth Hampsonaed71642018-06-11 07:41:32 -07004738 cricket::TestTurnServer* turn_server_2 = CreateTurnServer(
4739 turn_server_2_internal_address, turn_server_2_external_address);
deadbeef1dcb1642017-03-29 21:08:16 -07004740 // Bypass permission check on received packets so media can be sent before
4741 // the candidate is signaled.
Seth Hampsonaed71642018-06-11 07:41:32 -07004742 network_thread()->Invoke<void>(RTC_FROM_HERE, [turn_server_1] {
4743 turn_server_1->set_enable_permission_checks(false);
4744 });
4745 network_thread()->Invoke<void>(RTC_FROM_HERE, [turn_server_2] {
4746 turn_server_2->set_enable_permission_checks(false);
4747 });
deadbeef1dcb1642017-03-29 21:08:16 -07004748
4749 PeerConnectionInterface::RTCConfiguration client_1_config;
4750 webrtc::PeerConnectionInterface::IceServer ice_server_1;
4751 ice_server_1.urls.push_back("turn:88.88.88.0:3478");
4752 ice_server_1.username = "test";
4753 ice_server_1.password = "test";
4754 client_1_config.servers.push_back(ice_server_1);
4755 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
4756 client_1_config.presume_writable_when_fully_relayed = true;
4757
4758 PeerConnectionInterface::RTCConfiguration client_2_config;
4759 webrtc::PeerConnectionInterface::IceServer ice_server_2;
4760 ice_server_2.urls.push_back("turn:99.99.99.0:3478");
4761 ice_server_2.username = "test";
4762 ice_server_2.password = "test";
4763 client_2_config.servers.push_back(ice_server_2);
4764 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
4765 client_2_config.presume_writable_when_fully_relayed = true;
4766
4767 ASSERT_TRUE(
4768 CreatePeerConnectionWrappersWithConfig(client_1_config, client_2_config));
4769 // Set up the simulated delays.
4770 SetSignalingDelayMs(signaling_trip_delay_ms);
4771 ConnectFakeSignaling();
4772 virtual_socket_server()->set_delay_mean(media_hop_delay_ms);
4773 virtual_socket_server()->UpdateDelayDistribution();
4774
4775 // Set "offer to receive audio/video" without adding any tracks, so we just
4776 // set up ICE/DTLS with no media.
4777 PeerConnectionInterface::RTCOfferAnswerOptions options;
4778 options.offer_to_receive_audio = 1;
4779 options.offer_to_receive_video = 1;
4780 caller()->SetOfferAnswerOptions(options);
4781 caller()->CreateAndSetAndSignalOffer();
deadbeef71452802017-05-07 17:21:01 -07004782 EXPECT_TRUE_SIMULATED_WAIT(DtlsConnected(), total_connection_time_ms,
Yves Gerey100fe632020-01-17 19:15:53 +01004783 FakeClock());
Seth Hampson1d4a76d2018-06-19 14:31:41 -07004784 // Closing the PeerConnections destroys the ports before the ScopedFakeClock.
4785 // If this is not done a DCHECK can be hit in ports.cc, because a large
4786 // negative number is calculated for the rtt due to the global clock changing.
Steve Antond91969e2019-05-30 12:27:03 -07004787 ClosePeerConnections();
deadbeef1dcb1642017-03-29 21:08:16 -07004788}
4789
Harald Alvestrandec23d6d2021-02-11 10:47:22 +00004790#endif // !defined(THREAD_SANITIZER)
4791
Jonas Orelandbdcee282017-10-10 14:01:40 +02004792// Verify that a TurnCustomizer passed in through RTCConfiguration
4793// is actually used by the underlying TURN candidate pair.
4794// Note that turnport_unittest.cc contains more detailed, lower-level tests.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004795TEST_P(PeerConnectionIntegrationTest, TurnCustomizerUsedForTurnConnections) {
Jonas Orelandbdcee282017-10-10 14:01:40 +02004796 static const rtc::SocketAddress turn_server_1_internal_address{"88.88.88.0",
4797 3478};
4798 static const rtc::SocketAddress turn_server_1_external_address{"88.88.88.1",
4799 0};
4800 static const rtc::SocketAddress turn_server_2_internal_address{"99.99.99.0",
4801 3478};
4802 static const rtc::SocketAddress turn_server_2_external_address{"99.99.99.1",
4803 0};
Seth Hampsonaed71642018-06-11 07:41:32 -07004804 CreateTurnServer(turn_server_1_internal_address,
4805 turn_server_1_external_address);
4806 CreateTurnServer(turn_server_2_internal_address,
4807 turn_server_2_external_address);
Jonas Orelandbdcee282017-10-10 14:01:40 +02004808
4809 PeerConnectionInterface::RTCConfiguration client_1_config;
4810 webrtc::PeerConnectionInterface::IceServer ice_server_1;
4811 ice_server_1.urls.push_back("turn:88.88.88.0:3478");
4812 ice_server_1.username = "test";
4813 ice_server_1.password = "test";
4814 client_1_config.servers.push_back(ice_server_1);
4815 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
Seth Hampsonaed71642018-06-11 07:41:32 -07004816 auto* customizer1 = CreateTurnCustomizer();
4817 client_1_config.turn_customizer = customizer1;
Jonas Orelandbdcee282017-10-10 14:01:40 +02004818
4819 PeerConnectionInterface::RTCConfiguration client_2_config;
4820 webrtc::PeerConnectionInterface::IceServer ice_server_2;
4821 ice_server_2.urls.push_back("turn:99.99.99.0:3478");
4822 ice_server_2.username = "test";
4823 ice_server_2.password = "test";
4824 client_2_config.servers.push_back(ice_server_2);
4825 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
Seth Hampsonaed71642018-06-11 07:41:32 -07004826 auto* customizer2 = CreateTurnCustomizer();
4827 client_2_config.turn_customizer = customizer2;
Jonas Orelandbdcee282017-10-10 14:01:40 +02004828
4829 ASSERT_TRUE(
4830 CreatePeerConnectionWrappersWithConfig(client_1_config, client_2_config));
4831 ConnectFakeSignaling();
4832
4833 // Set "offer to receive audio/video" without adding any tracks, so we just
4834 // set up ICE/DTLS with no media.
4835 PeerConnectionInterface::RTCOfferAnswerOptions options;
4836 options.offer_to_receive_audio = 1;
4837 options.offer_to_receive_video = 1;
4838 caller()->SetOfferAnswerOptions(options);
4839 caller()->CreateAndSetAndSignalOffer();
4840 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
4841
Seth Hampsonaed71642018-06-11 07:41:32 -07004842 ExpectTurnCustomizerCountersIncremented(customizer1);
4843 ExpectTurnCustomizerCountersIncremented(customizer2);
Jonas Orelandbdcee282017-10-10 14:01:40 +02004844}
4845
Benjamin Wright2d5f3cb2018-05-22 14:46:06 -07004846// Verifies that you can use TCP instead of UDP to connect to a TURN server and
4847// send media between the caller and the callee.
4848TEST_P(PeerConnectionIntegrationTest, TCPUsedForTurnConnections) {
4849 static const rtc::SocketAddress turn_server_internal_address{"88.88.88.0",
4850 3478};
4851 static const rtc::SocketAddress turn_server_external_address{"88.88.88.1", 0};
4852
4853 // Enable TCP for the fake turn server.
Seth Hampsonaed71642018-06-11 07:41:32 -07004854 CreateTurnServer(turn_server_internal_address, turn_server_external_address,
4855 cricket::PROTO_TCP);
Benjamin Wright2d5f3cb2018-05-22 14:46:06 -07004856
4857 webrtc::PeerConnectionInterface::IceServer ice_server;
4858 ice_server.urls.push_back("turn:88.88.88.0:3478?transport=tcp");
4859 ice_server.username = "test";
4860 ice_server.password = "test";
4861
4862 PeerConnectionInterface::RTCConfiguration client_1_config;
4863 client_1_config.servers.push_back(ice_server);
4864 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
4865
4866 PeerConnectionInterface::RTCConfiguration client_2_config;
4867 client_2_config.servers.push_back(ice_server);
4868 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
4869
4870 ASSERT_TRUE(
4871 CreatePeerConnectionWrappersWithConfig(client_1_config, client_2_config));
4872
4873 // Do normal offer/answer and wait for ICE to complete.
4874 ConnectFakeSignaling();
4875 caller()->AddAudioVideoTracks();
4876 callee()->AddAudioVideoTracks();
4877 caller()->CreateAndSetAndSignalOffer();
4878 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4879 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
4880 callee()->ice_connection_state(), kMaxWaitForFramesMs);
4881
4882 MediaExpectations media_expectations;
4883 media_expectations.ExpectBidirectionalAudioAndVideo();
4884 EXPECT_TRUE(ExpectNewFrames(media_expectations));
4885}
4886
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07004887// Verify that a SSLCertificateVerifier passed in through
4888// PeerConnectionDependencies is actually used by the underlying SSL
4889// implementation to determine whether a certificate presented by the TURN
4890// server is accepted by the client. Note that openssladapter_unittest.cc
4891// contains more detailed, lower-level tests.
4892TEST_P(PeerConnectionIntegrationTest,
4893 SSLCertificateVerifierUsedForTurnConnections) {
4894 static const rtc::SocketAddress turn_server_internal_address{"88.88.88.0",
4895 3478};
4896 static const rtc::SocketAddress turn_server_external_address{"88.88.88.1", 0};
4897
4898 // Enable TCP-TLS for the fake turn server. We need to pass in 88.88.88.0 so
4899 // that host name verification passes on the fake certificate.
Seth Hampsonaed71642018-06-11 07:41:32 -07004900 CreateTurnServer(turn_server_internal_address, turn_server_external_address,
4901 cricket::PROTO_TLS, "88.88.88.0");
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07004902
4903 webrtc::PeerConnectionInterface::IceServer ice_server;
4904 ice_server.urls.push_back("turns:88.88.88.0:3478?transport=tcp");
4905 ice_server.username = "test";
4906 ice_server.password = "test";
4907
4908 PeerConnectionInterface::RTCConfiguration client_1_config;
4909 client_1_config.servers.push_back(ice_server);
4910 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
4911
4912 PeerConnectionInterface::RTCConfiguration client_2_config;
4913 client_2_config.servers.push_back(ice_server);
4914 // Setting the type to kRelay forces the connection to go through a TURN
4915 // server.
4916 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
4917
4918 // Get a copy to the pointer so we can verify calls later.
4919 rtc::TestCertificateVerifier* client_1_cert_verifier =
4920 new rtc::TestCertificateVerifier();
4921 client_1_cert_verifier->verify_certificate_ = true;
4922 rtc::TestCertificateVerifier* client_2_cert_verifier =
4923 new rtc::TestCertificateVerifier();
4924 client_2_cert_verifier->verify_certificate_ = true;
4925
4926 // Create the dependencies with the test certificate verifier.
4927 webrtc::PeerConnectionDependencies client_1_deps(nullptr);
4928 client_1_deps.tls_cert_verifier =
4929 std::unique_ptr<rtc::TestCertificateVerifier>(client_1_cert_verifier);
4930 webrtc::PeerConnectionDependencies client_2_deps(nullptr);
4931 client_2_deps.tls_cert_verifier =
4932 std::unique_ptr<rtc::TestCertificateVerifier>(client_2_cert_verifier);
4933
4934 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndDeps(
4935 client_1_config, std::move(client_1_deps), client_2_config,
4936 std::move(client_2_deps)));
4937 ConnectFakeSignaling();
4938
4939 // Set "offer to receive audio/video" without adding any tracks, so we just
4940 // set up ICE/DTLS with no media.
4941 PeerConnectionInterface::RTCOfferAnswerOptions options;
4942 options.offer_to_receive_audio = 1;
4943 options.offer_to_receive_video = 1;
4944 caller()->SetOfferAnswerOptions(options);
4945 caller()->CreateAndSetAndSignalOffer();
4946 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
4947
4948 EXPECT_GT(client_1_cert_verifier->call_count_, 0u);
4949 EXPECT_GT(client_2_cert_verifier->call_count_, 0u);
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07004950}
4951
4952TEST_P(PeerConnectionIntegrationTest,
4953 SSLCertificateVerifierFailureUsedForTurnConnectionsFailsConnection) {
4954 static const rtc::SocketAddress turn_server_internal_address{"88.88.88.0",
4955 3478};
4956 static const rtc::SocketAddress turn_server_external_address{"88.88.88.1", 0};
4957
4958 // Enable TCP-TLS for the fake turn server. We need to pass in 88.88.88.0 so
4959 // that host name verification passes on the fake certificate.
Seth Hampsonaed71642018-06-11 07:41:32 -07004960 CreateTurnServer(turn_server_internal_address, turn_server_external_address,
4961 cricket::PROTO_TLS, "88.88.88.0");
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07004962
4963 webrtc::PeerConnectionInterface::IceServer ice_server;
4964 ice_server.urls.push_back("turns:88.88.88.0:3478?transport=tcp");
4965 ice_server.username = "test";
4966 ice_server.password = "test";
4967
4968 PeerConnectionInterface::RTCConfiguration client_1_config;
4969 client_1_config.servers.push_back(ice_server);
4970 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
4971
4972 PeerConnectionInterface::RTCConfiguration client_2_config;
4973 client_2_config.servers.push_back(ice_server);
4974 // Setting the type to kRelay forces the connection to go through a TURN
4975 // server.
4976 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
4977
4978 // Get a copy to the pointer so we can verify calls later.
4979 rtc::TestCertificateVerifier* client_1_cert_verifier =
4980 new rtc::TestCertificateVerifier();
4981 client_1_cert_verifier->verify_certificate_ = false;
4982 rtc::TestCertificateVerifier* client_2_cert_verifier =
4983 new rtc::TestCertificateVerifier();
4984 client_2_cert_verifier->verify_certificate_ = false;
4985
4986 // Create the dependencies with the test certificate verifier.
4987 webrtc::PeerConnectionDependencies client_1_deps(nullptr);
4988 client_1_deps.tls_cert_verifier =
4989 std::unique_ptr<rtc::TestCertificateVerifier>(client_1_cert_verifier);
4990 webrtc::PeerConnectionDependencies client_2_deps(nullptr);
4991 client_2_deps.tls_cert_verifier =
4992 std::unique_ptr<rtc::TestCertificateVerifier>(client_2_cert_verifier);
4993
4994 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndDeps(
4995 client_1_config, std::move(client_1_deps), client_2_config,
4996 std::move(client_2_deps)));
4997 ConnectFakeSignaling();
4998
4999 // Set "offer to receive audio/video" without adding any tracks, so we just
5000 // set up ICE/DTLS with no media.
5001 PeerConnectionInterface::RTCOfferAnswerOptions options;
5002 options.offer_to_receive_audio = 1;
5003 options.offer_to_receive_video = 1;
5004 caller()->SetOfferAnswerOptions(options);
5005 caller()->CreateAndSetAndSignalOffer();
5006 bool wait_res = true;
5007 // TODO(bugs.webrtc.org/9219): When IceConnectionState is implemented
5008 // properly, should be able to just wait for a state of "failed" instead of
5009 // waiting a fixed 10 seconds.
5010 WAIT_(DtlsConnected(), kDefaultTimeout, wait_res);
5011 ASSERT_FALSE(wait_res);
5012
5013 EXPECT_GT(client_1_cert_verifier->call_count_, 0u);
5014 EXPECT_GT(client_2_cert_verifier->call_count_, 0u);
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07005015}
5016
Qingsi Wang25ec8882019-11-15 12:33:05 -08005017// Test that the injected ICE transport factory is used to create ICE transports
5018// for WebRTC connections.
5019TEST_P(PeerConnectionIntegrationTest, IceTransportFactoryUsedForConnections) {
5020 PeerConnectionInterface::RTCConfiguration default_config;
5021 PeerConnectionDependencies dependencies(nullptr);
5022 auto ice_transport_factory = std::make_unique<MockIceTransportFactory>();
5023 EXPECT_CALL(*ice_transport_factory, RecordIceTransportCreated()).Times(1);
5024 dependencies.ice_transport_factory = std::move(ice_transport_factory);
Niels Möller2a707032020-06-16 16:39:13 +02005025 auto wrapper = CreatePeerConnectionWrapper("Caller", nullptr, &default_config,
5026 std::move(dependencies), nullptr,
5027 /*reset_encoder_factory=*/false,
5028 /*reset_decoder_factory=*/false);
Qingsi Wang25ec8882019-11-15 12:33:05 -08005029 ASSERT_TRUE(wrapper);
5030 wrapper->CreateDataChannel();
5031 rtc::scoped_refptr<MockSetSessionDescriptionObserver> observer(
5032 new rtc::RefCountedObject<MockSetSessionDescriptionObserver>());
5033 wrapper->pc()->SetLocalDescription(observer,
5034 wrapper->CreateOfferAndWait().release());
5035}
5036
deadbeefc964d0b2017-04-03 10:03:35 -07005037// Test that audio and video flow end-to-end when codec names don't use the
5038// expected casing, given that they're supposed to be case insensitive. To test
5039// this, all but one codec is removed from each media description, and its
5040// casing is changed.
5041//
5042// In the past, this has regressed and caused crashes/black video, due to the
5043// fact that code at some layers was doing case-insensitive comparisons and
5044// code at other layers was not.
Seth Hampson2f0d7022018-02-20 11:54:42 -08005045TEST_P(PeerConnectionIntegrationTest, CodecNamesAreCaseInsensitive) {
deadbeefc964d0b2017-04-03 10:03:35 -07005046 ASSERT_TRUE(CreatePeerConnectionWrappers());
5047 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08005048 caller()->AddAudioVideoTracks();
5049 callee()->AddAudioVideoTracks();
deadbeefc964d0b2017-04-03 10:03:35 -07005050
5051 // Remove all but one audio/video codec (opus and VP8), and change the
5052 // casing of the caller's generated offer.
5053 caller()->SetGeneratedSdpMunger([](cricket::SessionDescription* description) {
5054 cricket::AudioContentDescription* audio =
5055 GetFirstAudioContentDescription(description);
5056 ASSERT_NE(nullptr, audio);
5057 auto audio_codecs = audio->codecs();
5058 audio_codecs.erase(std::remove_if(audio_codecs.begin(), audio_codecs.end(),
5059 [](const cricket::AudioCodec& codec) {
5060 return codec.name != "opus";
5061 }),
5062 audio_codecs.end());
5063 ASSERT_EQ(1u, audio_codecs.size());
5064 audio_codecs[0].name = "OpUs";
5065 audio->set_codecs(audio_codecs);
5066
5067 cricket::VideoContentDescription* video =
5068 GetFirstVideoContentDescription(description);
5069 ASSERT_NE(nullptr, video);
5070 auto video_codecs = video->codecs();
5071 video_codecs.erase(std::remove_if(video_codecs.begin(), video_codecs.end(),
5072 [](const cricket::VideoCodec& codec) {
5073 return codec.name != "VP8";
5074 }),
5075 video_codecs.end());
5076 ASSERT_EQ(1u, video_codecs.size());
5077 video_codecs[0].name = "vP8";
5078 video->set_codecs(video_codecs);
5079 });
5080
5081 caller()->CreateAndSetAndSignalOffer();
5082 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5083
5084 // Verify frames are still received end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08005085 MediaExpectations media_expectations;
5086 media_expectations.ExpectBidirectionalAudioAndVideo();
5087 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeefc964d0b2017-04-03 10:03:35 -07005088}
5089
Jonas Oreland49ac5952018-09-26 16:04:32 +02005090TEST_P(PeerConnectionIntegrationTest, GetSourcesAudio) {
hbos8d609f62017-04-10 07:39:05 -07005091 ASSERT_TRUE(CreatePeerConnectionWrappers());
5092 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08005093 caller()->AddAudioTrack();
hbos8d609f62017-04-10 07:39:05 -07005094 caller()->CreateAndSetAndSignalOffer();
5095 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
deadbeefd8ad7882017-04-18 16:01:17 -07005096 // Wait for one audio frame to be received by the callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08005097 MediaExpectations media_expectations;
5098 media_expectations.CalleeExpectsSomeAudio(1);
5099 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Jonas Oreland49ac5952018-09-26 16:04:32 +02005100 ASSERT_EQ(callee()->pc()->GetReceivers().size(), 1u);
hbos8d609f62017-04-10 07:39:05 -07005101 auto receiver = callee()->pc()->GetReceivers()[0];
5102 ASSERT_EQ(receiver->media_type(), cricket::MEDIA_TYPE_AUDIO);
Jonas Oreland49ac5952018-09-26 16:04:32 +02005103 auto sources = receiver->GetSources();
hbos8d609f62017-04-10 07:39:05 -07005104 ASSERT_GT(receiver->GetParameters().encodings.size(), 0u);
5105 EXPECT_EQ(receiver->GetParameters().encodings[0].ssrc,
Jonas Oreland49ac5952018-09-26 16:04:32 +02005106 sources[0].source_id());
5107 EXPECT_EQ(webrtc::RtpSourceType::SSRC, sources[0].source_type());
5108}
5109
5110TEST_P(PeerConnectionIntegrationTest, GetSourcesVideo) {
5111 ASSERT_TRUE(CreatePeerConnectionWrappers());
5112 ConnectFakeSignaling();
5113 caller()->AddVideoTrack();
5114 caller()->CreateAndSetAndSignalOffer();
5115 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5116 // Wait for one video frame to be received by the callee.
5117 MediaExpectations media_expectations;
5118 media_expectations.CalleeExpectsSomeVideo(1);
5119 ASSERT_TRUE(ExpectNewFrames(media_expectations));
5120 ASSERT_EQ(callee()->pc()->GetReceivers().size(), 1u);
5121 auto receiver = callee()->pc()->GetReceivers()[0];
5122 ASSERT_EQ(receiver->media_type(), cricket::MEDIA_TYPE_VIDEO);
5123 auto sources = receiver->GetSources();
5124 ASSERT_GT(receiver->GetParameters().encodings.size(), 0u);
Yves Gereyf781bb52019-07-23 19:15:39 +02005125 ASSERT_GT(sources.size(), 0u);
Jonas Oreland49ac5952018-09-26 16:04:32 +02005126 EXPECT_EQ(receiver->GetParameters().encodings[0].ssrc,
5127 sources[0].source_id());
5128 EXPECT_EQ(webrtc::RtpSourceType::SSRC, sources[0].source_type());
hbos8d609f62017-04-10 07:39:05 -07005129}
5130
deadbeef2f425aa2017-04-14 10:41:32 -07005131// Test that if a track is removed and added again with a different stream ID,
5132// the new stream ID is successfully communicated in SDP and media continues to
5133// flow end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08005134// TODO(webrtc.bugs.org/8734): This test does not work for Unified Plan because
5135// it will not reuse a transceiver that has already been sending. After creating
5136// a new transceiver it tries to create an offer with two senders of the same
5137// track ids and it fails.
5138TEST_F(PeerConnectionIntegrationTestPlanB, RemoveAndAddTrackWithNewStreamId) {
deadbeef2f425aa2017-04-14 10:41:32 -07005139 ASSERT_TRUE(CreatePeerConnectionWrappers());
5140 ConnectFakeSignaling();
5141
deadbeef2f425aa2017-04-14 10:41:32 -07005142 // Add track using stream 1, do offer/answer.
5143 rtc::scoped_refptr<webrtc::AudioTrackInterface> track =
5144 caller()->CreateLocalAudioTrack();
5145 rtc::scoped_refptr<webrtc::RtpSenderInterface> sender =
Steve Antond78323f2018-07-11 11:13:44 -07005146 caller()->AddTrack(track, {"stream_1"});
deadbeef2f425aa2017-04-14 10:41:32 -07005147 caller()->CreateAndSetAndSignalOffer();
5148 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08005149 {
5150 MediaExpectations media_expectations;
5151 media_expectations.CalleeExpectsSomeAudio(1);
5152 ASSERT_TRUE(ExpectNewFrames(media_expectations));
5153 }
deadbeef2f425aa2017-04-14 10:41:32 -07005154 // Remove the sender, and create a new one with the new stream.
5155 caller()->pc()->RemoveTrack(sender);
Steve Antond78323f2018-07-11 11:13:44 -07005156 sender = caller()->AddTrack(track, {"stream_2"});
deadbeef2f425aa2017-04-14 10:41:32 -07005157 caller()->CreateAndSetAndSignalOffer();
5158 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5159 // Wait for additional audio frames to be received by the callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08005160 {
5161 MediaExpectations media_expectations;
5162 media_expectations.CalleeExpectsSomeAudio();
5163 ASSERT_TRUE(ExpectNewFrames(media_expectations));
5164 }
deadbeef2f425aa2017-04-14 10:41:32 -07005165}
5166
Seth Hampson2f0d7022018-02-20 11:54:42 -08005167TEST_P(PeerConnectionIntegrationTest, RtcEventLogOutputWriteCalled) {
Elad Alon99c3fe52017-10-13 16:29:40 +02005168 ASSERT_TRUE(CreatePeerConnectionWrappers());
5169 ConnectFakeSignaling();
5170
Mirko Bonadei317a1f02019-09-17 17:06:18 +02005171 auto output = std::make_unique<testing::NiceMock<MockRtcEventLogOutput>>();
Mirko Bonadei6a489f22019-04-09 15:11:12 +02005172 ON_CALL(*output, IsActive()).WillByDefault(::testing::Return(true));
5173 ON_CALL(*output, Write(::testing::_)).WillByDefault(::testing::Return(true));
Elad Alon99c3fe52017-10-13 16:29:40 +02005174 EXPECT_CALL(*output, Write(::testing::_)).Times(::testing::AtLeast(1));
Bjorn Tereliusde939432017-11-20 17:38:14 +01005175 EXPECT_TRUE(caller()->pc()->StartRtcEventLog(
5176 std::move(output), webrtc::RtcEventLog::kImmediateOutput));
Elad Alon99c3fe52017-10-13 16:29:40 +02005177
Steve Anton15324772018-01-16 10:26:49 -08005178 caller()->AddAudioVideoTracks();
Elad Alon99c3fe52017-10-13 16:29:40 +02005179 caller()->CreateAndSetAndSignalOffer();
5180 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5181}
5182
Steve Antonede9ca52017-10-16 13:04:27 -07005183// Test that if candidates are only signaled by applying full session
5184// descriptions (instead of using AddIceCandidate), the peers can connect to
5185// each other and exchange media.
Seth Hampson2f0d7022018-02-20 11:54:42 -08005186TEST_P(PeerConnectionIntegrationTest, MediaFlowsWhenCandidatesSetOnlyInSdp) {
Steve Antonede9ca52017-10-16 13:04:27 -07005187 ASSERT_TRUE(CreatePeerConnectionWrappers());
5188 // Each side will signal the session descriptions but not candidates.
5189 ConnectFakeSignalingForSdpOnly();
5190
5191 // Add audio video track and exchange the initial offer/answer with media
5192 // information only. This will start ICE gathering on each side.
Steve Anton15324772018-01-16 10:26:49 -08005193 caller()->AddAudioVideoTracks();
5194 callee()->AddAudioVideoTracks();
Steve Antonede9ca52017-10-16 13:04:27 -07005195 caller()->CreateAndSetAndSignalOffer();
5196
5197 // Wait for all candidates to be gathered on both the caller and callee.
5198 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceGatheringComplete,
5199 caller()->ice_gathering_state(), kDefaultTimeout);
5200 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceGatheringComplete,
5201 callee()->ice_gathering_state(), kDefaultTimeout);
5202
5203 // The candidates will now be included in the session description, so
5204 // signaling them will start the ICE connection.
5205 caller()->CreateAndSetAndSignalOffer();
5206 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5207
5208 // Ensure that media flows in both directions.
Seth Hampson2f0d7022018-02-20 11:54:42 -08005209 MediaExpectations media_expectations;
5210 media_expectations.ExpectBidirectionalAudioAndVideo();
5211 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Antonede9ca52017-10-16 13:04:27 -07005212}
5213
Harald Alvestrandec23d6d2021-02-11 10:47:22 +00005214#if !defined(THREAD_SANITIZER)
5215// These tests provokes TSAN errors. See bugs.webrtc.org/11305.
5216
henrika5f6bf242017-11-01 11:06:56 +01005217// Test that SetAudioPlayout can be used to disable audio playout from the
5218// start, then later enable it. This may be useful, for example, if the caller
5219// needs to play a local ringtone until some event occurs, after which it
5220// switches to playing the received audio.
Seth Hampson2f0d7022018-02-20 11:54:42 -08005221TEST_P(PeerConnectionIntegrationTest, DisableAndEnableAudioPlayout) {
henrika5f6bf242017-11-01 11:06:56 +01005222 ASSERT_TRUE(CreatePeerConnectionWrappers());
5223 ConnectFakeSignaling();
5224
5225 // Set up audio-only call where audio playout is disabled on caller's side.
5226 caller()->pc()->SetAudioPlayout(false);
Steve Anton15324772018-01-16 10:26:49 -08005227 caller()->AddAudioTrack();
5228 callee()->AddAudioTrack();
henrika5f6bf242017-11-01 11:06:56 +01005229 caller()->CreateAndSetAndSignalOffer();
5230 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5231
5232 // Pump messages for a second.
5233 WAIT(false, 1000);
5234 // Since audio playout is disabled, the caller shouldn't have received
5235 // anything (at the playout level, at least).
5236 EXPECT_EQ(0, caller()->audio_frames_received());
5237 // As a sanity check, make sure the callee (for which playout isn't disabled)
5238 // did still see frames on its audio level.
5239 ASSERT_GT(callee()->audio_frames_received(), 0);
5240
5241 // Enable playout again, and ensure audio starts flowing.
5242 caller()->pc()->SetAudioPlayout(true);
Seth Hampson2f0d7022018-02-20 11:54:42 -08005243 MediaExpectations media_expectations;
5244 media_expectations.ExpectBidirectionalAudio();
5245 ASSERT_TRUE(ExpectNewFrames(media_expectations));
henrika5f6bf242017-11-01 11:06:56 +01005246}
5247
Harald Alvestrand89c40e22021-02-17 08:58:35 +00005248double GetAudioEnergyStat(PeerConnectionWrapper* pc) {
henrika5f6bf242017-11-01 11:06:56 +01005249 auto report = pc->NewGetStats();
5250 auto track_stats_list =
5251 report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
5252 const webrtc::RTCMediaStreamTrackStats* remote_track_stats = nullptr;
5253 for (const auto* track_stats : track_stats_list) {
5254 if (track_stats->remote_source.is_defined() &&
5255 *track_stats->remote_source) {
5256 remote_track_stats = track_stats;
5257 break;
5258 }
5259 }
5260
5261 if (!remote_track_stats->total_audio_energy.is_defined()) {
5262 return 0.0;
5263 }
5264 return *remote_track_stats->total_audio_energy;
5265}
5266
5267// Test that if audio playout is disabled via the SetAudioPlayout() method, then
5268// incoming audio is still processed and statistics are generated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08005269TEST_P(PeerConnectionIntegrationTest,
henrika5f6bf242017-11-01 11:06:56 +01005270 DisableAudioPlayoutStillGeneratesAudioStats) {
5271 ASSERT_TRUE(CreatePeerConnectionWrappers());
5272 ConnectFakeSignaling();
5273
5274 // Set up audio-only call where playout is disabled but audio-processing is
5275 // still active.
Steve Anton15324772018-01-16 10:26:49 -08005276 caller()->AddAudioTrack();
5277 callee()->AddAudioTrack();
henrika5f6bf242017-11-01 11:06:56 +01005278 caller()->pc()->SetAudioPlayout(false);
5279
5280 caller()->CreateAndSetAndSignalOffer();
5281 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5282
5283 // Wait for the callee to receive audio stats.
5284 EXPECT_TRUE_WAIT(GetAudioEnergyStat(caller()) > 0, kMaxWaitForFramesMs);
5285}
5286
Harald Alvestrandec23d6d2021-02-11 10:47:22 +00005287#endif // !defined(THREAD_SANITIZER)
5288
henrika4f167df2017-11-01 14:45:55 +01005289// Test that SetAudioRecording can be used to disable audio recording from the
5290// start, then later enable it. This may be useful, for example, if the caller
5291// wants to ensure that no audio resources are active before a certain state
5292// is reached.
Seth Hampson2f0d7022018-02-20 11:54:42 -08005293TEST_P(PeerConnectionIntegrationTest, DisableAndEnableAudioRecording) {
henrika4f167df2017-11-01 14:45:55 +01005294 ASSERT_TRUE(CreatePeerConnectionWrappers());
5295 ConnectFakeSignaling();
5296
5297 // Set up audio-only call where audio recording is disabled on caller's side.
5298 caller()->pc()->SetAudioRecording(false);
Steve Anton15324772018-01-16 10:26:49 -08005299 caller()->AddAudioTrack();
5300 callee()->AddAudioTrack();
henrika4f167df2017-11-01 14:45:55 +01005301 caller()->CreateAndSetAndSignalOffer();
5302 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5303
5304 // Pump messages for a second.
5305 WAIT(false, 1000);
5306 // Since caller has disabled audio recording, the callee shouldn't have
5307 // received anything.
5308 EXPECT_EQ(0, callee()->audio_frames_received());
5309 // As a sanity check, make sure the caller did still see frames on its
5310 // audio level since audio recording is enabled on the calle side.
5311 ASSERT_GT(caller()->audio_frames_received(), 0);
5312
5313 // Enable audio recording again, and ensure audio starts flowing.
5314 caller()->pc()->SetAudioRecording(true);
Seth Hampson2f0d7022018-02-20 11:54:42 -08005315 MediaExpectations media_expectations;
5316 media_expectations.ExpectBidirectionalAudio();
5317 ASSERT_TRUE(ExpectNewFrames(media_expectations));
henrika4f167df2017-11-01 14:45:55 +01005318}
5319
Harald Alvestrand89c40e22021-02-17 08:58:35 +00005320// Test that after closing PeerConnections, they stop sending any packets (ICE,
5321// DTLS, RTP...).
5322TEST_P(PeerConnectionIntegrationTest, ClosingConnectionStopsPacketFlow) {
5323 // Set up audio/video/data, wait for some frames to be received.
5324 ASSERT_TRUE(CreatePeerConnectionWrappers());
5325 ConnectFakeSignaling();
5326 caller()->AddAudioVideoTracks();
5327#ifdef WEBRTC_HAVE_SCTP
5328 caller()->CreateDataChannel();
5329#endif
5330 caller()->CreateAndSetAndSignalOffer();
5331 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5332 MediaExpectations media_expectations;
5333 media_expectations.CalleeExpectsSomeAudioAndVideo();
5334 ASSERT_TRUE(ExpectNewFrames(media_expectations));
5335 // Close PeerConnections.
5336 ClosePeerConnections();
5337 // Pump messages for a second, and ensure no new packets end up sent.
5338 uint32_t sent_packets_a = virtual_socket_server()->sent_packets();
5339 WAIT(false, 1000);
5340 uint32_t sent_packets_b = virtual_socket_server()->sent_packets();
5341 EXPECT_EQ(sent_packets_a, sent_packets_b);
5342}
5343
5344// Test that transport stats are generated by the RTCStatsCollector for a
5345// connection that only involves data channels. This is a regression test for
5346// crbug.com/826972.
5347#ifdef WEBRTC_HAVE_SCTP
5348TEST_P(PeerConnectionIntegrationTest,
5349 TransportStatsReportedForDataChannelOnlyConnection) {
5350 ASSERT_TRUE(CreatePeerConnectionWrappers());
5351 ConnectFakeSignaling();
5352 caller()->CreateDataChannel();
5353
5354 caller()->CreateAndSetAndSignalOffer();
5355 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5356 ASSERT_TRUE_WAIT(callee()->data_channel(), kDefaultTimeout);
5357
5358 auto caller_report = caller()->NewGetStats();
5359 EXPECT_EQ(1u, caller_report->GetStatsOfType<RTCTransportStats>().size());
5360 auto callee_report = callee()->NewGetStats();
5361 EXPECT_EQ(1u, callee_report->GetStatsOfType<RTCTransportStats>().size());
5362}
5363#endif // WEBRTC_HAVE_SCTP
5364
Qingsi Wang7685e862018-06-11 20:15:46 -07005365TEST_P(PeerConnectionIntegrationTest,
5366 IceEventsGeneratedAndLoggedInRtcEventLog) {
5367 ASSERT_TRUE(CreatePeerConnectionWrappersWithFakeRtcEventLog());
5368 ConnectFakeSignaling();
5369 PeerConnectionInterface::RTCOfferAnswerOptions options;
5370 options.offer_to_receive_audio = 1;
5371 caller()->SetOfferAnswerOptions(options);
5372 caller()->CreateAndSetAndSignalOffer();
5373 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
5374 ASSERT_NE(nullptr, caller()->event_log_factory());
5375 ASSERT_NE(nullptr, callee()->event_log_factory());
5376 webrtc::FakeRtcEventLog* caller_event_log =
Danil Chapovalov4f281f12021-01-18 13:29:00 +01005377 caller()->event_log_factory()->last_log_created();
Qingsi Wang7685e862018-06-11 20:15:46 -07005378 webrtc::FakeRtcEventLog* callee_event_log =
Danil Chapovalov4f281f12021-01-18 13:29:00 +01005379 callee()->event_log_factory()->last_log_created();
Qingsi Wang7685e862018-06-11 20:15:46 -07005380 ASSERT_NE(nullptr, caller_event_log);
5381 ASSERT_NE(nullptr, callee_event_log);
5382 int caller_ice_config_count = caller_event_log->GetEventCount(
5383 webrtc::RtcEvent::Type::IceCandidatePairConfig);
5384 int caller_ice_event_count = caller_event_log->GetEventCount(
5385 webrtc::RtcEvent::Type::IceCandidatePairEvent);
5386 int callee_ice_config_count = callee_event_log->GetEventCount(
5387 webrtc::RtcEvent::Type::IceCandidatePairConfig);
5388 int callee_ice_event_count = callee_event_log->GetEventCount(
5389 webrtc::RtcEvent::Type::IceCandidatePairEvent);
5390 EXPECT_LT(0, caller_ice_config_count);
5391 EXPECT_LT(0, caller_ice_event_count);
5392 EXPECT_LT(0, callee_ice_config_count);
5393 EXPECT_LT(0, callee_ice_event_count);
5394}
5395
Qingsi Wangc129c352019-04-18 10:41:58 -07005396TEST_P(PeerConnectionIntegrationTest, RegatherAfterChangingIceTransportType) {
Qingsi Wangc129c352019-04-18 10:41:58 -07005397 static const rtc::SocketAddress turn_server_internal_address{"88.88.88.0",
5398 3478};
5399 static const rtc::SocketAddress turn_server_external_address{"88.88.88.1", 0};
5400
5401 CreateTurnServer(turn_server_internal_address, turn_server_external_address);
5402
5403 webrtc::PeerConnectionInterface::IceServer ice_server;
5404 ice_server.urls.push_back("turn:88.88.88.0:3478");
5405 ice_server.username = "test";
5406 ice_server.password = "test";
5407
5408 PeerConnectionInterface::RTCConfiguration caller_config;
5409 caller_config.servers.push_back(ice_server);
5410 caller_config.type = webrtc::PeerConnectionInterface::kRelay;
5411 caller_config.continual_gathering_policy = PeerConnection::GATHER_CONTINUALLY;
Qingsi Wang1fe119f2019-05-31 16:55:33 -07005412 caller_config.surface_ice_candidates_on_ice_transport_type_changed = true;
Qingsi Wangc129c352019-04-18 10:41:58 -07005413
5414 PeerConnectionInterface::RTCConfiguration callee_config;
5415 callee_config.servers.push_back(ice_server);
5416 callee_config.type = webrtc::PeerConnectionInterface::kRelay;
5417 callee_config.continual_gathering_policy = PeerConnection::GATHER_CONTINUALLY;
Qingsi Wang1fe119f2019-05-31 16:55:33 -07005418 callee_config.surface_ice_candidates_on_ice_transport_type_changed = true;
Qingsi Wangc129c352019-04-18 10:41:58 -07005419
5420 ASSERT_TRUE(
5421 CreatePeerConnectionWrappersWithConfig(caller_config, callee_config));
5422
5423 // Do normal offer/answer and wait for ICE to complete.
5424 ConnectFakeSignaling();
5425 caller()->AddAudioVideoTracks();
5426 callee()->AddAudioVideoTracks();
5427 caller()->CreateAndSetAndSignalOffer();
5428 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5429 // Since we are doing continual gathering, the ICE transport does not reach
5430 // kIceGatheringComplete (see
5431 // P2PTransportChannel::OnCandidatesAllocationDone), and consequently not
5432 // kIceConnectionComplete.
5433 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
5434 caller()->ice_connection_state(), kDefaultTimeout);
5435 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
5436 callee()->ice_connection_state(), kDefaultTimeout);
5437 // Note that we cannot use the metric
5438 // |WebRTC.PeerConnection.CandidatePairType_UDP| in this test since this
5439 // metric is only populated when we reach kIceConnectionComplete in the
5440 // current implementation.
5441 EXPECT_EQ(cricket::RELAY_PORT_TYPE,
5442 caller()->last_candidate_gathered().type());
5443 EXPECT_EQ(cricket::RELAY_PORT_TYPE,
5444 callee()->last_candidate_gathered().type());
5445
5446 // Loosen the caller's candidate filter.
5447 caller_config = caller()->pc()->GetConfiguration();
5448 caller_config.type = webrtc::PeerConnectionInterface::kAll;
5449 caller()->pc()->SetConfiguration(caller_config);
5450 // We should have gathered a new host candidate.
5451 EXPECT_EQ_WAIT(cricket::LOCAL_PORT_TYPE,
5452 caller()->last_candidate_gathered().type(), kDefaultTimeout);
5453
5454 // Loosen the callee's candidate filter.
5455 callee_config = callee()->pc()->GetConfiguration();
5456 callee_config.type = webrtc::PeerConnectionInterface::kAll;
5457 callee()->pc()->SetConfiguration(callee_config);
5458 EXPECT_EQ_WAIT(cricket::LOCAL_PORT_TYPE,
5459 callee()->last_candidate_gathered().type(), kDefaultTimeout);
Jonas Orelande3096512020-05-27 09:01:05 +02005460
5461 // Create an offer and verify that it does not contain an ICE restart (i.e new
5462 // ice credentials).
5463 std::string caller_ufrag_pre_offer = caller()
5464 ->pc()
5465 ->local_description()
5466 ->description()
5467 ->transport_infos()[0]
5468 .description.ice_ufrag;
5469 caller()->CreateAndSetAndSignalOffer();
5470 std::string caller_ufrag_post_offer = caller()
5471 ->pc()
5472 ->local_description()
5473 ->description()
5474 ->transport_infos()[0]
5475 .description.ice_ufrag;
5476 EXPECT_EQ(caller_ufrag_pre_offer, caller_ufrag_post_offer);
Qingsi Wangc129c352019-04-18 10:41:58 -07005477}
5478
Eldar Relloda13ea22019-06-01 12:23:43 +03005479TEST_P(PeerConnectionIntegrationTest, OnIceCandidateError) {
Eldar Relloda13ea22019-06-01 12:23:43 +03005480 static const rtc::SocketAddress turn_server_internal_address{"88.88.88.0",
5481 3478};
5482 static const rtc::SocketAddress turn_server_external_address{"88.88.88.1", 0};
5483
5484 CreateTurnServer(turn_server_internal_address, turn_server_external_address);
5485
5486 webrtc::PeerConnectionInterface::IceServer ice_server;
5487 ice_server.urls.push_back("turn:88.88.88.0:3478");
5488 ice_server.username = "test";
5489 ice_server.password = "123";
5490
5491 PeerConnectionInterface::RTCConfiguration caller_config;
5492 caller_config.servers.push_back(ice_server);
5493 caller_config.type = webrtc::PeerConnectionInterface::kRelay;
5494 caller_config.continual_gathering_policy = PeerConnection::GATHER_CONTINUALLY;
5495
5496 PeerConnectionInterface::RTCConfiguration callee_config;
5497 callee_config.servers.push_back(ice_server);
5498 callee_config.type = webrtc::PeerConnectionInterface::kRelay;
5499 callee_config.continual_gathering_policy = PeerConnection::GATHER_CONTINUALLY;
5500
5501 ASSERT_TRUE(
5502 CreatePeerConnectionWrappersWithConfig(caller_config, callee_config));
5503
5504 // Do normal offer/answer and wait for ICE to complete.
5505 ConnectFakeSignaling();
5506 caller()->AddAudioVideoTracks();
5507 callee()->AddAudioVideoTracks();
5508 caller()->CreateAndSetAndSignalOffer();
5509 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5510 EXPECT_EQ_WAIT(401, caller()->error_event().error_code, kDefaultTimeout);
5511 EXPECT_EQ("Unauthorized", caller()->error_event().error_text);
5512 EXPECT_EQ("turn:88.88.88.0:3478?transport=udp", caller()->error_event().url);
Eldar Rello0095d372019-12-02 22:22:07 +02005513 EXPECT_NE(caller()->error_event().address, "");
Eldar Relloda13ea22019-06-01 12:23:43 +03005514}
5515
Eldar Rellofa8019c2020-05-14 11:59:33 +03005516TEST_P(PeerConnectionIntegrationTest, OnIceCandidateErrorWithEmptyAddress) {
5517 webrtc::PeerConnectionInterface::IceServer ice_server;
5518 ice_server.urls.push_back("turn:127.0.0.1:3478?transport=tcp");
5519 ice_server.username = "test";
5520 ice_server.password = "test";
5521
5522 PeerConnectionInterface::RTCConfiguration caller_config;
5523 caller_config.servers.push_back(ice_server);
5524 caller_config.type = webrtc::PeerConnectionInterface::kRelay;
5525 caller_config.continual_gathering_policy = PeerConnection::GATHER_CONTINUALLY;
5526
5527 PeerConnectionInterface::RTCConfiguration callee_config;
5528 callee_config.servers.push_back(ice_server);
5529 callee_config.type = webrtc::PeerConnectionInterface::kRelay;
5530 callee_config.continual_gathering_policy = PeerConnection::GATHER_CONTINUALLY;
5531
5532 ASSERT_TRUE(
5533 CreatePeerConnectionWrappersWithConfig(caller_config, callee_config));
5534
5535 // Do normal offer/answer and wait for ICE to complete.
5536 ConnectFakeSignaling();
5537 caller()->AddAudioVideoTracks();
5538 callee()->AddAudioVideoTracks();
5539 caller()->CreateAndSetAndSignalOffer();
5540 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5541 EXPECT_EQ_WAIT(701, caller()->error_event().error_code, kDefaultTimeout);
5542 EXPECT_EQ(caller()->error_event().address, "");
5543}
5544
Eldar Rello5ab79e62019-10-09 18:29:44 +03005545TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
5546 AudioKeepsFlowingAfterImplicitRollback) {
5547 PeerConnectionInterface::RTCConfiguration config;
5548 config.sdp_semantics = SdpSemantics::kUnifiedPlan;
5549 config.enable_implicit_rollback = true;
5550 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
5551 ConnectFakeSignaling();
5552 caller()->AddAudioTrack();
5553 callee()->AddAudioTrack();
5554 caller()->CreateAndSetAndSignalOffer();
5555 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5556 MediaExpectations media_expectations;
5557 media_expectations.ExpectBidirectionalAudio();
5558 ASSERT_TRUE(ExpectNewFrames(media_expectations));
5559 SetSignalIceCandidates(false); // Workaround candidate outrace sdp.
5560 caller()->AddVideoTrack();
5561 callee()->AddVideoTrack();
5562 rtc::scoped_refptr<MockSetSessionDescriptionObserver> observer(
5563 new rtc::RefCountedObject<MockSetSessionDescriptionObserver>());
5564 callee()->pc()->SetLocalDescription(observer,
5565 callee()->CreateOfferAndWait().release());
5566 EXPECT_TRUE_WAIT(observer->called(), kDefaultTimeout);
5567 caller()->CreateAndSetAndSignalOffer(); // Implicit rollback.
5568 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5569 ASSERT_TRUE(ExpectNewFrames(media_expectations));
5570}
5571
5572TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
5573 ImplicitRollbackVisitsStableState) {
5574 RTCConfiguration config;
5575 config.sdp_semantics = SdpSemantics::kUnifiedPlan;
5576 config.enable_implicit_rollback = true;
5577
5578 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
5579
5580 rtc::scoped_refptr<MockSetSessionDescriptionObserver> sld_observer(
5581 new rtc::RefCountedObject<MockSetSessionDescriptionObserver>());
5582 callee()->pc()->SetLocalDescription(sld_observer,
5583 callee()->CreateOfferAndWait().release());
5584 EXPECT_TRUE_WAIT(sld_observer->called(), kDefaultTimeout);
5585 EXPECT_EQ(sld_observer->error(), "");
5586
5587 rtc::scoped_refptr<MockSetSessionDescriptionObserver> srd_observer(
5588 new rtc::RefCountedObject<MockSetSessionDescriptionObserver>());
5589 callee()->pc()->SetRemoteDescription(
5590 srd_observer, caller()->CreateOfferAndWait().release());
5591 EXPECT_TRUE_WAIT(srd_observer->called(), kDefaultTimeout);
5592 EXPECT_EQ(srd_observer->error(), "");
5593
5594 EXPECT_THAT(callee()->peer_connection_signaling_state_history(),
5595 ElementsAre(PeerConnectionInterface::kHaveLocalOffer,
5596 PeerConnectionInterface::kStable,
5597 PeerConnectionInterface::kHaveRemoteOffer));
5598}
5599
Eldar Rellobd9c33a2020-10-01 17:52:45 +03005600TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
5601 H264FmtpSpsPpsIdrInKeyframeParameterUsage) {
5602 ASSERT_TRUE(CreatePeerConnectionWrappers());
5603 ConnectFakeSignaling();
5604 caller()->AddVideoTrack();
5605 callee()->AddVideoTrack();
5606 auto munger = [](cricket::SessionDescription* desc) {
5607 cricket::VideoContentDescription* video =
5608 GetFirstVideoContentDescription(desc);
5609 auto codecs = video->codecs();
5610 for (auto&& codec : codecs) {
5611 if (codec.name == "H264") {
5612 std::string value;
5613 // The parameter is not supposed to be present in SDP by default.
5614 EXPECT_FALSE(
5615 codec.GetParam(cricket::kH264FmtpSpsPpsIdrInKeyframe, &value));
5616 codec.SetParam(std::string(cricket::kH264FmtpSpsPpsIdrInKeyframe),
5617 std::string(""));
5618 }
5619 }
5620 video->set_codecs(codecs);
5621 };
5622 // Munge local offer for SLD.
5623 caller()->SetGeneratedSdpMunger(munger);
5624 // Munge remote answer for SRD.
5625 caller()->SetReceivedSdpMunger(munger);
5626 caller()->CreateAndSetAndSignalOffer();
5627 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5628 // Observe that after munging the parameter is present in generated SDP.
5629 caller()->SetGeneratedSdpMunger([](cricket::SessionDescription* desc) {
5630 cricket::VideoContentDescription* video =
5631 GetFirstVideoContentDescription(desc);
5632 for (auto&& codec : video->codecs()) {
5633 if (codec.name == "H264") {
5634 std::string value;
5635 EXPECT_TRUE(
5636 codec.GetParam(cricket::kH264FmtpSpsPpsIdrInKeyframe, &value));
5637 }
5638 }
5639 });
5640 caller()->CreateOfferAndWait();
5641}
5642
Harald Alvestrand1a9be302020-12-11 14:53:59 +00005643TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
Harald Alvestrand94324f22021-01-13 12:31:53 +00005644 RenegotiateManyAudioTransceivers) {
Harald Alvestrand1a9be302020-12-11 14:53:59 +00005645 PeerConnectionInterface::RTCConfiguration config;
5646 config.sdp_semantics = SdpSemantics::kUnifiedPlan;
5647 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
5648 ConnectFakeSignaling();
5649 caller()->pc()->AddTransceiver(cricket::MEDIA_TYPE_AUDIO);
5650
5651 caller()->CreateAndSetAndSignalOffer();
5652 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5653 int current_size = caller()->pc()->GetTransceivers().size();
5654 // Add more tracks until we get close to having issues.
5655 // Issues have been seen at:
5656 // - 32 tracks on android_arm64_rel and android_arm_dbg bots
Harald Alvestrandcc6ae442021-01-18 08:06:23 +00005657 // - 16 tracks on android_arm_dbg (flaky)
5658 while (current_size < 8) {
Harald Alvestrand1a9be302020-12-11 14:53:59 +00005659 // Double the number of tracks
5660 for (int i = 0; i < current_size; i++) {
5661 caller()->pc()->AddTransceiver(cricket::MEDIA_TYPE_AUDIO);
5662 }
5663 current_size = caller()->pc()->GetTransceivers().size();
5664 RTC_LOG(LS_INFO) << "Renegotiating with " << current_size << " tracks";
5665 auto start_time_ms = rtc::TimeMillis();
5666 caller()->CreateAndSetAndSignalOffer();
5667 // We want to stop when the time exceeds one second.
5668 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5669 auto elapsed_time_ms = rtc::TimeMillis() - start_time_ms;
5670 RTC_LOG(LS_INFO) << "Renegotiating took " << elapsed_time_ms << " ms";
5671 ASSERT_GT(1000, elapsed_time_ms)
5672 << "Audio transceivers: Negotiation took too long after "
5673 << current_size << " tracks added";
5674 }
5675}
5676
5677TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
5678 RenegotiateManyVideoTransceivers) {
5679 PeerConnectionInterface::RTCConfiguration config;
5680 config.sdp_semantics = SdpSemantics::kUnifiedPlan;
5681 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
5682 ConnectFakeSignaling();
5683 caller()->pc()->AddTransceiver(cricket::MEDIA_TYPE_VIDEO);
5684
5685 caller()->CreateAndSetAndSignalOffer();
5686 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5687 int current_size = caller()->pc()->GetTransceivers().size();
5688 // Add more tracks until we get close to having issues.
5689 // Issues have been seen at:
5690 // - 96 on a Linux workstation
5691 // - 64 at win_x86_more_configs and win_x64_msvc_dbg
5692 // - 32 on android_arm64_rel and linux_dbg bots
5693 while (current_size < 16) {
5694 // Double the number of tracks
5695 for (int i = 0; i < current_size; i++) {
5696 caller()->pc()->AddTransceiver(cricket::MEDIA_TYPE_VIDEO);
5697 }
5698 current_size = caller()->pc()->GetTransceivers().size();
5699 RTC_LOG(LS_INFO) << "Renegotiating with " << current_size << " tracks";
5700 auto start_time_ms = rtc::TimeMillis();
5701 caller()->CreateAndSetAndSignalOffer();
5702 // We want to stop when the time exceeds one second.
5703 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5704 auto elapsed_time_ms = rtc::TimeMillis() - start_time_ms;
5705 RTC_LOG(LS_INFO) << "Renegotiating took " << elapsed_time_ms << " ms";
5706 ASSERT_GT(1000, elapsed_time_ms)
5707 << "Video transceivers: Negotiation took too long after "
5708 << current_size << " tracks added";
5709 }
5710}
5711
Harald Alvestrand94324f22021-01-13 12:31:53 +00005712TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
5713 RenegotiateManyVideoTransceiversAndWatchAudioDelay) {
5714 PeerConnectionInterface::RTCConfiguration config;
5715 config.sdp_semantics = SdpSemantics::kUnifiedPlan;
5716 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
5717 ConnectFakeSignaling();
5718 caller()->AddAudioTrack();
Harald Alvestrandcc6ae442021-01-18 08:06:23 +00005719 callee()->AddAudioTrack();
Harald Alvestrand94324f22021-01-13 12:31:53 +00005720 caller()->CreateAndSetAndSignalOffer();
5721 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5722 // Wait until we can see the audio flowing.
5723 MediaExpectations media_expectations;
5724 media_expectations.CalleeExpectsSomeAudio();
5725 ASSERT_TRUE(ExpectNewFrames(media_expectations));
5726
Harald Alvestrandcc6ae442021-01-18 08:06:23 +00005727 // Get the baseline numbers for audio_packets and audio_delay
5728 // in both directions.
5729 caller()->StartWatchingDelayStats();
5730 callee()->StartWatchingDelayStats();
Harald Alvestrand94324f22021-01-13 12:31:53 +00005731
5732 int current_size = caller()->pc()->GetTransceivers().size();
5733 // Add more tracks until we get close to having issues.
5734 // Making this number very large makes the test very slow.
Harald Alvestrandcc6ae442021-01-18 08:06:23 +00005735 while (current_size < 16) {
Harald Alvestrand94324f22021-01-13 12:31:53 +00005736 // Double the number of tracks
5737 for (int i = 0; i < current_size; i++) {
5738 caller()->pc()->AddTransceiver(cricket::MEDIA_TYPE_VIDEO);
5739 }
5740 current_size = caller()->pc()->GetTransceivers().size();
5741 RTC_LOG(LS_INFO) << "Renegotiating with " << current_size << " tracks";
5742 auto start_time_ms = rtc::TimeMillis();
5743 caller()->CreateAndSetAndSignalOffer();
5744 // We want to stop when the time exceeds one second.
5745 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5746 auto elapsed_time_ms = rtc::TimeMillis() - start_time_ms;
5747 RTC_LOG(LS_INFO) << "Renegotiating took " << elapsed_time_ms << " ms";
5748 // This is a guard against the test using excessive amounts of time.
5749 ASSERT_GT(5000, elapsed_time_ms)
5750 << "Video transceivers: Negotiation took too long after "
5751 << current_size << " tracks added";
Harald Alvestrandcc6ae442021-01-18 08:06:23 +00005752 caller()->UpdateDelayStats("caller reception", current_size);
5753 callee()->UpdateDelayStats("callee reception", current_size);
Harald Alvestrand94324f22021-01-13 12:31:53 +00005754 }
5755}
5756
Mirko Bonadeic84f6612019-01-31 12:20:57 +01005757INSTANTIATE_TEST_SUITE_P(PeerConnectionIntegrationTest,
5758 PeerConnectionIntegrationTest,
5759 Values(SdpSemantics::kPlanB,
5760 SdpSemantics::kUnifiedPlan));
Steve Antond3679212018-01-17 17:41:02 -08005761
Yves Gerey100fe632020-01-17 19:15:53 +01005762INSTANTIATE_TEST_SUITE_P(PeerConnectionIntegrationTest,
5763 PeerConnectionIntegrationTestWithFakeClock,
5764 Values(SdpSemantics::kPlanB,
5765 SdpSemantics::kUnifiedPlan));
5766
Steve Anton74255ff2018-01-24 18:32:57 -08005767// Tests that verify interoperability between Plan B and Unified Plan
5768// PeerConnections.
5769class PeerConnectionIntegrationInteropTest
Seth Hampson2f0d7022018-02-20 11:54:42 -08005770 : public PeerConnectionIntegrationBaseTest,
Steve Anton74255ff2018-01-24 18:32:57 -08005771 public ::testing::WithParamInterface<
5772 std::tuple<SdpSemantics, SdpSemantics>> {
5773 protected:
Seth Hampson2f0d7022018-02-20 11:54:42 -08005774 // Setting the SdpSemantics for the base test to kDefault does not matter
5775 // because we specify not to use the test semantics when creating
Harald Alvestrand89c40e22021-02-17 08:58:35 +00005776 // PeerConnectionWrappers.
Steve Anton74255ff2018-01-24 18:32:57 -08005777 PeerConnectionIntegrationInteropTest()
Steve Anton3acffc32018-04-12 17:21:03 -07005778 : PeerConnectionIntegrationBaseTest(SdpSemantics::kPlanB),
Seth Hampson2f0d7022018-02-20 11:54:42 -08005779 caller_semantics_(std::get<0>(GetParam())),
Steve Anton74255ff2018-01-24 18:32:57 -08005780 callee_semantics_(std::get<1>(GetParam())) {}
5781
5782 bool CreatePeerConnectionWrappersWithSemantics() {
Steve Anton3acffc32018-04-12 17:21:03 -07005783 return CreatePeerConnectionWrappersWithSdpSemantics(caller_semantics_,
5784 callee_semantics_);
Steve Anton74255ff2018-01-24 18:32:57 -08005785 }
5786
5787 const SdpSemantics caller_semantics_;
5788 const SdpSemantics callee_semantics_;
5789};
5790
5791TEST_P(PeerConnectionIntegrationInteropTest, NoMediaLocalToNoMediaRemote) {
5792 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
5793 ConnectFakeSignaling();
5794
5795 caller()->CreateAndSetAndSignalOffer();
5796 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5797}
5798
5799TEST_P(PeerConnectionIntegrationInteropTest, OneAudioLocalToNoMediaRemote) {
5800 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
5801 ConnectFakeSignaling();
5802 auto audio_sender = caller()->AddAudioTrack();
5803
5804 caller()->CreateAndSetAndSignalOffer();
5805 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5806
5807 // Verify that one audio receiver has been created on the remote and that it
5808 // has the same track ID as the sending track.
5809 auto receivers = callee()->pc()->GetReceivers();
5810 ASSERT_EQ(1u, receivers.size());
5811 EXPECT_EQ(cricket::MEDIA_TYPE_AUDIO, receivers[0]->media_type());
5812 EXPECT_EQ(receivers[0]->track()->id(), audio_sender->track()->id());
5813
Seth Hampson2f0d7022018-02-20 11:54:42 -08005814 MediaExpectations media_expectations;
5815 media_expectations.CalleeExpectsSomeAudio();
5816 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08005817}
5818
5819TEST_P(PeerConnectionIntegrationInteropTest, OneAudioOneVideoToNoMediaRemote) {
5820 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
5821 ConnectFakeSignaling();
5822 auto video_sender = caller()->AddVideoTrack();
5823 auto audio_sender = caller()->AddAudioTrack();
5824
5825 caller()->CreateAndSetAndSignalOffer();
5826 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5827
5828 // Verify that one audio and one video receiver have been created on the
5829 // remote and that they have the same track IDs as the sending tracks.
5830 auto audio_receivers =
5831 callee()->GetReceiversOfType(cricket::MEDIA_TYPE_AUDIO);
5832 ASSERT_EQ(1u, audio_receivers.size());
5833 EXPECT_EQ(audio_receivers[0]->track()->id(), audio_sender->track()->id());
5834 auto video_receivers =
5835 callee()->GetReceiversOfType(cricket::MEDIA_TYPE_VIDEO);
5836 ASSERT_EQ(1u, video_receivers.size());
5837 EXPECT_EQ(video_receivers[0]->track()->id(), video_sender->track()->id());
5838
Seth Hampson2f0d7022018-02-20 11:54:42 -08005839 MediaExpectations media_expectations;
5840 media_expectations.CalleeExpectsSomeAudioAndVideo();
5841 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08005842}
5843
5844TEST_P(PeerConnectionIntegrationInteropTest,
5845 OneAudioOneVideoLocalToOneAudioOneVideoRemote) {
5846 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
5847 ConnectFakeSignaling();
5848 caller()->AddAudioVideoTracks();
5849 callee()->AddAudioVideoTracks();
5850
5851 caller()->CreateAndSetAndSignalOffer();
5852 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5853
Seth Hampson2f0d7022018-02-20 11:54:42 -08005854 MediaExpectations media_expectations;
5855 media_expectations.ExpectBidirectionalAudioAndVideo();
5856 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08005857}
5858
5859TEST_P(PeerConnectionIntegrationInteropTest,
5860 ReverseRolesOneAudioLocalToOneVideoRemote) {
5861 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
5862 ConnectFakeSignaling();
5863 caller()->AddAudioTrack();
5864 callee()->AddVideoTrack();
5865
5866 caller()->CreateAndSetAndSignalOffer();
5867 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5868
5869 // Verify that only the audio track has been negotiated.
5870 EXPECT_EQ(0u, caller()->GetReceiversOfType(cricket::MEDIA_TYPE_VIDEO).size());
5871 // Might also check that the callee's NegotiationNeeded flag is set.
5872
5873 // Reverse roles.
5874 callee()->CreateAndSetAndSignalOffer();
5875 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5876
Seth Hampson2f0d7022018-02-20 11:54:42 -08005877 MediaExpectations media_expectations;
5878 media_expectations.CallerExpectsSomeVideo();
5879 media_expectations.CalleeExpectsSomeAudio();
5880 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08005881}
5882
Mirko Bonadeic84f6612019-01-31 12:20:57 +01005883INSTANTIATE_TEST_SUITE_P(
Steve Antonba42e992018-04-09 14:10:01 -07005884 PeerConnectionIntegrationTest,
5885 PeerConnectionIntegrationInteropTest,
5886 Values(std::make_tuple(SdpSemantics::kPlanB, SdpSemantics::kUnifiedPlan),
5887 std::make_tuple(SdpSemantics::kUnifiedPlan, SdpSemantics::kPlanB)));
5888
5889// Test that if the Unified Plan side offers two video tracks then the Plan B
5890// side will only see the first one and ignore the second.
5891TEST_F(PeerConnectionIntegrationTestPlanB, TwoVideoUnifiedPlanToNoMediaPlanB) {
Steve Anton3acffc32018-04-12 17:21:03 -07005892 ASSERT_TRUE(CreatePeerConnectionWrappersWithSdpSemantics(
5893 SdpSemantics::kUnifiedPlan, SdpSemantics::kPlanB));
Steve Anton74255ff2018-01-24 18:32:57 -08005894 ConnectFakeSignaling();
5895 auto first_sender = caller()->AddVideoTrack();
5896 caller()->AddVideoTrack();
5897
5898 caller()->CreateAndSetAndSignalOffer();
5899 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5900
5901 // Verify that there is only one receiver and it corresponds to the first
5902 // added track.
5903 auto receivers = callee()->pc()->GetReceivers();
5904 ASSERT_EQ(1u, receivers.size());
5905 EXPECT_TRUE(receivers[0]->track()->enabled());
5906 EXPECT_EQ(first_sender->track()->id(), receivers[0]->track()->id());
5907
Seth Hampson2f0d7022018-02-20 11:54:42 -08005908 MediaExpectations media_expectations;
5909 media_expectations.CalleeExpectsSomeVideo();
5910 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08005911}
5912
Steve Anton2bed3972019-01-04 17:04:30 -08005913// Test that if the initial offer tagged BUNDLE section is rejected due to its
5914// associated RtpTransceiver being stopped and another transceiver is added,
5915// then renegotiation causes the callee to receive the new video track without
5916// error.
5917// This is a regression test for bugs.webrtc.org/9954
5918TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
5919 ReOfferWithStoppedBundleTaggedTransceiver) {
5920 RTCConfiguration config;
5921 config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
5922 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
5923 ConnectFakeSignaling();
5924 auto audio_transceiver_or_error =
5925 caller()->pc()->AddTransceiver(caller()->CreateLocalAudioTrack());
5926 ASSERT_TRUE(audio_transceiver_or_error.ok());
5927 auto audio_transceiver = audio_transceiver_or_error.MoveValue();
5928
5929 caller()->CreateAndSetAndSignalOffer();
5930 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5931 {
5932 MediaExpectations media_expectations;
5933 media_expectations.CalleeExpectsSomeAudio();
5934 ASSERT_TRUE(ExpectNewFrames(media_expectations));
5935 }
5936
Harald Alvestrand6060df52020-08-11 09:54:02 +02005937 audio_transceiver->StopInternal();
Steve Anton2bed3972019-01-04 17:04:30 -08005938 caller()->pc()->AddTransceiver(caller()->CreateLocalVideoTrack());
5939
5940 caller()->CreateAndSetAndSignalOffer();
5941 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5942 {
5943 MediaExpectations media_expectations;
5944 media_expectations.CalleeExpectsSomeVideo();
5945 ASSERT_TRUE(ExpectNewFrames(media_expectations));
5946 }
5947}
5948
Harald Alvestrandbedb6052020-08-20 14:50:10 +02005949TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
5950 StopTransceiverRemovesDtlsTransports) {
5951 RTCConfiguration config;
5952 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
5953 ConnectFakeSignaling();
5954 auto audio_transceiver_or_error =
5955 caller()->pc()->AddTransceiver(caller()->CreateLocalAudioTrack());
5956 ASSERT_TRUE(audio_transceiver_or_error.ok());
5957 auto audio_transceiver = audio_transceiver_or_error.MoveValue();
5958
5959 caller()->CreateAndSetAndSignalOffer();
5960 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5961
5962 audio_transceiver->StopStandard();
5963 caller()->CreateAndSetAndSignalOffer();
5964 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5965 ASSERT_EQ(0U, caller()->pc()->GetTransceivers().size());
5966 EXPECT_EQ(PeerConnectionInterface::kIceGatheringNew,
5967 caller()->pc()->ice_gathering_state());
5968 EXPECT_THAT(caller()->ice_gathering_state_history(),
5969 ElementsAre(PeerConnectionInterface::kIceGatheringGathering,
5970 PeerConnectionInterface::kIceGatheringComplete,
5971 PeerConnectionInterface::kIceGatheringNew));
5972}
5973
Harald Alvestrand1ee33252020-09-24 13:31:15 +00005974TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
Harald Alvestrand45be0a92020-09-30 06:55:23 +00005975 StopTransceiverStopsAndRemovesTransceivers) {
5976 RTCConfiguration config;
5977 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
5978 ConnectFakeSignaling();
5979 auto audio_transceiver_or_error =
5980 caller()->pc()->AddTransceiver(caller()->CreateLocalAudioTrack());
5981 ASSERT_TRUE(audio_transceiver_or_error.ok());
5982 auto caller_transceiver = audio_transceiver_or_error.MoveValue();
5983
5984 caller()->CreateAndSetAndSignalOffer();
5985 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5986 caller_transceiver->StopStandard();
5987
5988 auto callee_transceiver = callee()->pc()->GetTransceivers()[0];
5989 caller()->CreateAndSetAndSignalOffer();
5990 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5991 EXPECT_EQ(0U, caller()->pc()->GetTransceivers().size());
5992 EXPECT_EQ(0U, callee()->pc()->GetTransceivers().size());
5993 EXPECT_EQ(0U, caller()->pc()->GetSenders().size());
5994 EXPECT_EQ(0U, callee()->pc()->GetSenders().size());
5995 EXPECT_EQ(0U, caller()->pc()->GetReceivers().size());
5996 EXPECT_EQ(0U, callee()->pc()->GetReceivers().size());
5997 EXPECT_TRUE(caller_transceiver->stopped());
5998 EXPECT_TRUE(callee_transceiver->stopped());
5999}
6000
6001TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
Harald Alvestrand1ee33252020-09-24 13:31:15 +00006002 StopTransceiverEndsIncomingAudioTrack) {
6003 RTCConfiguration config;
6004 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
6005 ConnectFakeSignaling();
6006 auto audio_transceiver_or_error =
6007 caller()->pc()->AddTransceiver(caller()->CreateLocalAudioTrack());
6008 ASSERT_TRUE(audio_transceiver_or_error.ok());
6009 auto audio_transceiver = audio_transceiver_or_error.MoveValue();
6010
6011 caller()->CreateAndSetAndSignalOffer();
6012 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
6013 auto caller_track = audio_transceiver->receiver()->track();
6014 auto callee_track = callee()->pc()->GetReceivers()[0]->track();
6015 audio_transceiver->StopStandard();
6016 EXPECT_EQ(MediaStreamTrackInterface::TrackState::kEnded,
6017 caller_track->state());
6018 caller()->CreateAndSetAndSignalOffer();
6019 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
6020 EXPECT_EQ(MediaStreamTrackInterface::TrackState::kEnded,
6021 callee_track->state());
6022}
6023
6024TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
6025 StopTransceiverEndsIncomingVideoTrack) {
6026 RTCConfiguration config;
6027 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
6028 ConnectFakeSignaling();
6029 auto audio_transceiver_or_error =
6030 caller()->pc()->AddTransceiver(caller()->CreateLocalVideoTrack());
6031 ASSERT_TRUE(audio_transceiver_or_error.ok());
6032 auto audio_transceiver = audio_transceiver_or_error.MoveValue();
6033
6034 caller()->CreateAndSetAndSignalOffer();
6035 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
6036 auto caller_track = audio_transceiver->receiver()->track();
6037 auto callee_track = callee()->pc()->GetReceivers()[0]->track();
6038 audio_transceiver->StopStandard();
6039 EXPECT_EQ(MediaStreamTrackInterface::TrackState::kEnded,
6040 caller_track->state());
6041 caller()->CreateAndSetAndSignalOffer();
6042 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
6043 EXPECT_EQ(MediaStreamTrackInterface::TrackState::kEnded,
6044 callee_track->state());
6045}
6046
Harald Alvestrand89c40e22021-02-17 08:58:35 +00006047#ifdef WEBRTC_HAVE_SCTP
Harald Alvestrand772066b2021-02-16 15:29:32 +00006048
Harald Alvestrand89c40e22021-02-17 08:58:35 +00006049TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
6050 EndToEndCallWithBundledSctpDataChannel) {
6051 ASSERT_TRUE(CreatePeerConnectionWrappers());
6052 ConnectFakeSignaling();
6053 caller()->CreateDataChannel();
6054 caller()->AddAudioVideoTracks();
6055 callee()->AddAudioVideoTracks();
6056 caller()->CreateAndSetAndSignalOffer();
6057 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
6058 network_thread()->Invoke<void>(RTC_FROM_HERE, [this] {
6059 ASSERT_EQ_WAIT(SctpTransportState::kConnected,
6060 caller()->pc()->GetSctpTransport()->Information().state(),
6061 kDefaultTimeout);
6062 });
6063 ASSERT_TRUE_WAIT(callee()->data_channel(), kDefaultTimeout);
6064 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
6065}
6066
6067TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
6068 EndToEndCallWithDataChannelOnlyConnects) {
6069 ASSERT_TRUE(CreatePeerConnectionWrappers());
6070 ConnectFakeSignaling();
6071 caller()->CreateDataChannel();
6072 caller()->CreateAndSetAndSignalOffer();
6073 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
6074 ASSERT_TRUE_WAIT(callee()->data_channel(), kDefaultTimeout);
6075 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
6076 ASSERT_TRUE(caller()->data_observer()->IsOpen());
6077}
6078
6079TEST_F(PeerConnectionIntegrationTestUnifiedPlan, DataChannelClosesWhenClosed) {
6080 ASSERT_TRUE(CreatePeerConnectionWrappers());
6081 ConnectFakeSignaling();
6082 caller()->CreateDataChannel();
6083 caller()->CreateAndSetAndSignalOffer();
6084 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
6085 ASSERT_TRUE_WAIT(callee()->data_observer(), kDefaultTimeout);
6086 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
6087 caller()->data_channel()->Close();
6088 ASSERT_TRUE_WAIT(!callee()->data_observer()->IsOpen(), kDefaultTimeout);
6089}
6090
6091TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
6092 DataChannelClosesWhenClosedReverse) {
6093 ASSERT_TRUE(CreatePeerConnectionWrappers());
6094 ConnectFakeSignaling();
6095 caller()->CreateDataChannel();
6096 caller()->CreateAndSetAndSignalOffer();
6097 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
6098 ASSERT_TRUE_WAIT(callee()->data_observer(), kDefaultTimeout);
6099 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
6100 callee()->data_channel()->Close();
6101 ASSERT_TRUE_WAIT(!caller()->data_observer()->IsOpen(), kDefaultTimeout);
6102}
6103
6104TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
6105 DataChannelClosesWhenPeerConnectionClosed) {
6106 ASSERT_TRUE(CreatePeerConnectionWrappers());
6107 ConnectFakeSignaling();
6108 caller()->CreateDataChannel();
6109 caller()->CreateAndSetAndSignalOffer();
6110 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
6111 ASSERT_TRUE_WAIT(callee()->data_observer(), kDefaultTimeout);
6112 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
6113 caller()->pc()->Close();
6114 ASSERT_TRUE_WAIT(!callee()->data_observer()->IsOpen(), kDefaultTimeout);
6115}
6116
6117#endif // WEBRTC_HAVE_SCTP
6118
6119} // namespace
Mirko Bonadeiab64e8a2018-12-12 12:10:18 +01006120} // namespace webrtc