blob: b06091b3d918b70f1f2e8987eb8a7fb4487701fd [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
11// Disable for TSan v2, see
12// https://code.google.com/p/webrtc/issues/detail?id=1205 for details.
13#if !defined(THREAD_SANITIZER)
14
15#include <stdio.h>
16
deadbeef1dcb1642017-03-29 21:08:16 -070017#include <functional>
18#include <list>
19#include <map>
20#include <memory>
21#include <utility>
22#include <vector>
23
Steve Anton64b626b2019-01-28 17:25:26 -080024#include "absl/algorithm/container.h"
Steve Anton10542f22019-01-11 09:11:00 -080025#include "api/media_stream_interface.h"
26#include "api/peer_connection_interface.h"
27#include "api/peer_connection_proxy.h"
Danil Chapovalov9da25bd2019-06-20 10:19:42 +020028#include "api/rtc_event_log/rtc_event_log_factory.h"
Steve Anton10542f22019-01-11 09:11:00 -080029#include "api/rtp_receiver_interface.h"
Danil Chapovalov9da25bd2019-06-20 10:19:42 +020030#include "api/task_queue/default_task_queue_factory.h"
Bjorn Mellem175aa2e2018-11-08 11:23:22 -080031#include "api/test/loopback_media_transport.h"
Steve Anton10542f22019-01-11 09:11:00 -080032#include "api/uma_metrics.h"
Anders Carlsson67537952018-05-03 11:28:29 +020033#include "api/video_codecs/sdp_video_format.h"
Qingsi Wang7685e862018-06-11 20:15:46 -070034#include "call/call.h"
35#include "logging/rtc_event_log/fake_rtc_event_log_factory.h"
Steve Anton10542f22019-01-11 09:11:00 -080036#include "media/engine/fake_webrtc_video_engine.h"
37#include "media/engine/webrtc_media_engine.h"
Danil Chapovalov9da25bd2019-06-20 10:19:42 +020038#include "media/engine/webrtc_media_engine_defaults.h"
Steve Anton10542f22019-01-11 09:11:00 -080039#include "p2p/base/mock_async_resolver.h"
40#include "p2p/base/p2p_constants.h"
41#include "p2p/base/port_interface.h"
42#include "p2p/base/test_stun_server.h"
43#include "p2p/base/test_turn_customizer.h"
44#include "p2p/base/test_turn_server.h"
45#include "p2p/client/basic_port_allocator.h"
46#include "pc/dtmf_sender.h"
47#include "pc/local_audio_source.h"
48#include "pc/media_session.h"
49#include "pc/peer_connection.h"
50#include "pc/peer_connection_factory.h"
51#include "pc/rtp_media_utils.h"
52#include "pc/session_description.h"
53#include "pc/test/fake_audio_capture_module.h"
54#include "pc/test/fake_periodic_video_track_source.h"
55#include "pc/test/fake_rtc_certificate_generator.h"
56#include "pc/test/fake_video_track_renderer.h"
57#include "pc/test/mock_peer_connection_observers.h"
Jonas Olssonb75d9e92019-02-22 10:33:29 +010058#include "rtc_base/fake_clock.h"
Qingsi Wangecd30542019-05-22 14:34:56 -070059#include "rtc_base/fake_mdns_responder.h"
Steve Anton10542f22019-01-11 09:11:00 -080060#include "rtc_base/fake_network.h"
61#include "rtc_base/firewall_socket_server.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020062#include "rtc_base/gunit.h"
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +020063#include "rtc_base/numerics/safe_conversions.h"
Steve Anton10542f22019-01-11 09:11:00 -080064#include "rtc_base/test_certificate_verifier.h"
65#include "rtc_base/time_utils.h"
66#include "rtc_base/virtual_socket_server.h"
Mirko Bonadei17f48782018-09-28 08:51:10 +020067#include "system_wrappers/include/metrics.h"
Qingsi Wangc129c352019-04-18 10:41:58 -070068#include "test/field_trial.h"
Elad Alon99c3fe52017-10-13 16:29:40 +020069#include "test/gmock.h"
deadbeef1dcb1642017-03-29 21:08:16 -070070
Mirko Bonadeiab64e8a2018-12-12 12:10:18 +010071namespace webrtc {
72namespace {
73
74using ::cricket::ContentInfo;
75using ::cricket::StreamParams;
76using ::rtc::SocketAddress;
77using ::testing::_;
Seth Hampson2f0d7022018-02-20 11:54:42 -080078using ::testing::Combine;
Steve Anton64b626b2019-01-28 17:25:26 -080079using ::testing::Contains;
Mirko Bonadeie46f5db2019-03-26 20:14:46 +010080using ::testing::DoAll;
Steve Antonede9ca52017-10-16 13:04:27 -070081using ::testing::ElementsAre;
Qingsi Wang1dac6d82018-12-12 15:28:47 -080082using ::testing::NiceMock;
Steve Anton64b626b2019-01-28 17:25:26 -080083using ::testing::Return;
Zach Stein6fcdc2f2018-08-23 16:25:55 -070084using ::testing::SetArgPointee;
Steve Antonffa6ce42018-11-30 09:26:08 -080085using ::testing::UnorderedElementsAreArray;
Mirko Bonadeiab64e8a2018-12-12 12:10:18 +010086using ::testing::Values;
Steve Anton74255ff2018-01-24 18:32:57 -080087using RTCConfiguration = PeerConnectionInterface::RTCConfiguration;
deadbeef1dcb1642017-03-29 21:08:16 -070088
89static const int kDefaultTimeout = 10000;
90static const int kMaxWaitForStatsMs = 3000;
91static const int kMaxWaitForActivationMs = 5000;
92static const int kMaxWaitForFramesMs = 10000;
93// Default number of audio/video frames to wait for before considering a test
94// successful.
95static const int kDefaultExpectedAudioFrameCount = 3;
96static const int kDefaultExpectedVideoFrameCount = 3;
97
deadbeef1dcb1642017-03-29 21:08:16 -070098static const char kDataChannelLabel[] = "data_channel";
99
100// SRTP cipher name negotiated by the tests. This must be updated if the
101// default changes.
Taylor Brandstetterfd350d72018-04-03 16:29:26 -0700102static const int kDefaultSrtpCryptoSuite = rtc::SRTP_AES128_CM_SHA1_80;
deadbeef1dcb1642017-03-29 21:08:16 -0700103static const int kDefaultSrtpCryptoSuiteGcm = rtc::SRTP_AEAD_AES_256_GCM;
104
Steve Antonede9ca52017-10-16 13:04:27 -0700105static const SocketAddress kDefaultLocalAddress("192.168.1.1", 0);
106
deadbeef1dcb1642017-03-29 21:08:16 -0700107// Helper function for constructing offer/answer options to initiate an ICE
108// restart.
109PeerConnectionInterface::RTCOfferAnswerOptions IceRestartOfferAnswerOptions() {
110 PeerConnectionInterface::RTCOfferAnswerOptions options;
111 options.ice_restart = true;
112 return options;
113}
114
deadbeefd8ad7882017-04-18 16:01:17 -0700115// Remove all stream information (SSRCs, track IDs, etc.) and "msid-semantic"
116// attribute from received SDP, simulating a legacy endpoint.
117void RemoveSsrcsAndMsids(cricket::SessionDescription* desc) {
118 for (ContentInfo& content : desc->contents()) {
Steve Antonb1c1de12017-12-21 15:14:30 -0800119 content.media_description()->mutable_streams().clear();
deadbeefd8ad7882017-04-18 16:01:17 -0700120 }
121 desc->set_msid_supported(false);
Henrik Boström5b147782018-12-04 11:25:05 +0100122 desc->set_msid_signaling(0);
deadbeefd8ad7882017-04-18 16:01:17 -0700123}
124
Seth Hampson5897a6e2018-04-03 11:16:33 -0700125// Removes all stream information besides the stream ids, simulating an
126// endpoint that only signals a=msid lines to convey stream_ids.
127void RemoveSsrcsAndKeepMsids(cricket::SessionDescription* desc) {
128 for (ContentInfo& content : desc->contents()) {
Steve Antondf527fd2018-04-27 15:52:03 -0700129 std::string track_id;
Seth Hampson5897a6e2018-04-03 11:16:33 -0700130 std::vector<std::string> stream_ids;
131 if (!content.media_description()->streams().empty()) {
Steve Antondf527fd2018-04-27 15:52:03 -0700132 const StreamParams& first_stream =
133 content.media_description()->streams()[0];
134 track_id = first_stream.id;
135 stream_ids = first_stream.stream_ids();
Seth Hampson5897a6e2018-04-03 11:16:33 -0700136 }
137 content.media_description()->mutable_streams().clear();
Steve Antondf527fd2018-04-27 15:52:03 -0700138 StreamParams new_stream;
139 new_stream.id = track_id;
Seth Hampson5897a6e2018-04-03 11:16:33 -0700140 new_stream.set_stream_ids(stream_ids);
141 content.media_description()->AddStream(new_stream);
142 }
143}
144
zhihuangf8164932017-05-19 13:09:47 -0700145int FindFirstMediaStatsIndexByKind(
146 const std::string& kind,
147 const std::vector<const webrtc::RTCMediaStreamTrackStats*>&
148 media_stats_vec) {
149 for (size_t i = 0; i < media_stats_vec.size(); i++) {
150 if (media_stats_vec[i]->kind.ValueToString() == kind) {
151 return i;
152 }
153 }
154 return -1;
155}
156
deadbeef1dcb1642017-03-29 21:08:16 -0700157class SignalingMessageReceiver {
158 public:
Steve Antona3a92c22017-12-07 10:27:41 -0800159 virtual void ReceiveSdpMessage(SdpType type, const std::string& msg) = 0;
deadbeef1dcb1642017-03-29 21:08:16 -0700160 virtual void ReceiveIceMessage(const std::string& sdp_mid,
161 int sdp_mline_index,
162 const std::string& msg) = 0;
163
164 protected:
165 SignalingMessageReceiver() {}
166 virtual ~SignalingMessageReceiver() {}
167};
168
169class MockRtpReceiverObserver : public webrtc::RtpReceiverObserverInterface {
170 public:
171 explicit MockRtpReceiverObserver(cricket::MediaType media_type)
172 : expected_media_type_(media_type) {}
173
174 void OnFirstPacketReceived(cricket::MediaType media_type) override {
175 ASSERT_EQ(expected_media_type_, media_type);
176 first_packet_received_ = true;
177 }
178
179 bool first_packet_received() const { return first_packet_received_; }
180
181 virtual ~MockRtpReceiverObserver() {}
182
183 private:
184 bool first_packet_received_ = false;
185 cricket::MediaType expected_media_type_;
186};
187
188// Helper class that wraps a peer connection, observes it, and can accept
189// signaling messages from another wrapper.
190//
191// Uses a fake network, fake A/V capture, and optionally fake
192// encoders/decoders, though they aren't used by default since they don't
193// advertise support of any codecs.
Steve Anton94286cb2017-09-26 16:20:19 -0700194// TODO(steveanton): See how this could become a subclass of
Seth Hampson2f0d7022018-02-20 11:54:42 -0800195// PeerConnectionWrapper defined in peerconnectionwrapper.h.
deadbeef1dcb1642017-03-29 21:08:16 -0700196class PeerConnectionWrapper : public webrtc::PeerConnectionObserver,
Steve Anton15324772018-01-16 10:26:49 -0800197 public SignalingMessageReceiver {
deadbeef1dcb1642017-03-29 21:08:16 -0700198 public:
199 // Different factory methods for convenience.
200 // TODO(deadbeef): Could use the pattern of:
201 //
202 // PeerConnectionWrapper =
203 // WrapperBuilder.WithConfig(...).WithOptions(...).build();
204 //
205 // To reduce some code duplication.
206 static PeerConnectionWrapper* CreateWithDtlsIdentityStore(
207 const std::string& debug_name,
208 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator,
209 rtc::Thread* network_thread,
210 rtc::Thread* worker_thread) {
211 PeerConnectionWrapper* client(new PeerConnectionWrapper(debug_name));
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700212 webrtc::PeerConnectionDependencies dependencies(nullptr);
213 dependencies.cert_generator = std::move(cert_generator);
Niels Möllerf06f9232018-08-07 12:32:18 +0200214 if (!client->Init(nullptr, nullptr, std::move(dependencies), network_thread,
Bjorn Mellem175aa2e2018-11-08 11:23:22 -0800215 worker_thread, nullptr,
216 /*media_transport_factory=*/nullptr)) {
deadbeef1dcb1642017-03-29 21:08:16 -0700217 delete client;
218 return nullptr;
219 }
220 return client;
221 }
222
deadbeef2f425aa2017-04-14 10:41:32 -0700223 webrtc::PeerConnectionFactoryInterface* pc_factory() const {
224 return peer_connection_factory_.get();
225 }
226
deadbeef1dcb1642017-03-29 21:08:16 -0700227 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() {
Eldar Rello16d4c4d2019-10-09 18:29:44 +0300233 auto offer = CreateOfferAndWait();
deadbeef1dcb1642017-03-29 21:08:16 -0700234 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) {
Mirko Bonadeic61ce0d2017-11-21 17:04:20 +0100252 received_sdp_munger_ = std::move(munger);
deadbeef1dcb1642017-03-29 21:08:16 -0700253 }
254
deadbeefc964d0b2017-04-03 10:03:35 -0700255 // Similar to the above, but this is run on SDP immediately after it's
deadbeef1dcb1642017-03-29 21:08:16 -0700256 // generated.
257 void SetGeneratedSdpMunger(
258 std::function<void(cricket::SessionDescription*)> munger) {
Mirko Bonadeic61ce0d2017-11-21 17:04:20 +0100259 generated_sdp_munger_ = std::move(munger);
deadbeef1dcb1642017-03-29 21:08:16 -0700260 }
261
Seth Hampson2f0d7022018-02-20 11:54:42 -0800262 // 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
Qingsi Wang1dac6d82018-12-12 15:28:47 -0800269 void SetRemoteAsyncResolver(rtc::MockAsyncResolver* resolver) {
270 remote_async_resolver_ = resolver;
Zach Stein6fcdc2f2018-08-23 16:25:55 -0700271 }
272
Steve Antonede9ca52017-10-16 13:04:27 -0700273 // 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 }
Steve Anton6f25b092017-10-23 09:39:20 -0700278 void clear_ice_connection_state_history() {
279 ice_connection_state_history_.clear();
280 }
Steve Antonede9ca52017-10-16 13:04:27 -0700281
Jonas Olssonacd8ae72019-02-25 15:26:24 +0100282 // 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
Jonas Olsson635474e2018-10-18 15:58:17 +0200289 // 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
Steve Antonede9ca52017-10-16 13:04:27 -0700295 // 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_;
deadbeef1dcb1642017-03-29 21:08:16 -0700299 }
Alex Drake00c7ecf2019-08-06 10:54:47 -0700300 std::vector<cricket::CandidatePairChangeEvent>
301 ice_candidate_pair_change_history() const {
302 return ice_candidate_pair_change_history_;
303 }
deadbeef1dcb1642017-03-29 21:08:16 -0700304
Eldar Rello16d4c4d2019-10-09 18:29:44 +0300305 // 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
Steve Anton15324772018-01-16 10:26:49 -0800312 void AddAudioVideoTracks() {
313 AddAudioTrack();
314 AddVideoTrack();
deadbeef1dcb1642017-03-29 21:08:16 -0700315 }
316
Steve Anton74255ff2018-01-24 18:32:57 -0800317 rtc::scoped_refptr<RtpSenderInterface> AddAudioTrack() {
318 return AddTrack(CreateLocalAudioTrack());
319 }
deadbeef1dcb1642017-03-29 21:08:16 -0700320
Steve Anton74255ff2018-01-24 18:32:57 -0800321 rtc::scoped_refptr<RtpSenderInterface> AddVideoTrack() {
322 return AddTrack(CreateLocalVideoTrack());
323 }
deadbeef1dcb1642017-03-29 21:08:16 -0700324
325 rtc::scoped_refptr<webrtc::AudioTrackInterface> CreateLocalAudioTrack() {
Niels Möller2d02e082018-05-21 11:23:35 +0200326 cricket::AudioOptions options;
deadbeef1dcb1642017-03-29 21:08:16 -0700327 // Disable highpass filter so that we can get all the test audio frames.
Niels Möller2d02e082018-05-21 11:23:35 +0200328 options.highpass_filter = false;
deadbeef1dcb1642017-03-29 21:08:16 -0700329 rtc::scoped_refptr<webrtc::AudioSourceInterface> source =
Niels Möller2d02e082018-05-21 11:23:35 +0200330 peer_connection_factory_->CreateAudioSource(options);
deadbeef1dcb1642017-03-29 21:08:16 -0700331 // TODO(perkj): Test audio source when it is implemented. Currently audio
332 // always use the default input.
deadbeefb1a15d72017-09-07 14:12:05 -0700333 return peer_connection_factory_->CreateAudioTrack(rtc::CreateRandomUuid(),
deadbeef1dcb1642017-03-29 21:08:16 -0700334 source);
335 }
336
337 rtc::scoped_refptr<webrtc::VideoTrackInterface> CreateLocalVideoTrack() {
Johannes Kron965e7942018-09-13 15:36:20 +0200338 webrtc::FakePeriodicVideoSource::Config config;
339 config.timestamp_offset_ms = rtc::TimeMillis();
340 return CreateLocalVideoTrackInternal(config);
deadbeef1dcb1642017-03-29 21:08:16 -0700341 }
342
343 rtc::scoped_refptr<webrtc::VideoTrackInterface>
Niels Möller5c7efe72018-05-11 10:34:46 +0200344 CreateLocalVideoTrackWithConfig(
345 webrtc::FakePeriodicVideoSource::Config config) {
346 return CreateLocalVideoTrackInternal(config);
deadbeef1dcb1642017-03-29 21:08:16 -0700347 }
348
349 rtc::scoped_refptr<webrtc::VideoTrackInterface>
350 CreateLocalVideoTrackWithRotation(webrtc::VideoRotation rotation) {
Niels Möller5c7efe72018-05-11 10:34:46 +0200351 webrtc::FakePeriodicVideoSource::Config config;
352 config.rotation = rotation;
Johannes Kron965e7942018-09-13 15:36:20 +0200353 config.timestamp_offset_ms = rtc::TimeMillis();
Niels Möller5c7efe72018-05-11 10:34:46 +0200354 return CreateLocalVideoTrackInternal(config);
deadbeef1dcb1642017-03-29 21:08:16 -0700355 }
356
Steve Anton74255ff2018-01-24 18:32:57 -0800357 rtc::scoped_refptr<RtpSenderInterface> AddTrack(
358 rtc::scoped_refptr<MediaStreamTrackInterface> track,
Seth Hampson845e8782018-03-02 11:34:10 -0800359 const std::vector<std::string>& stream_ids = {}) {
360 auto result = pc()->AddTrack(track, stream_ids);
Steve Anton15324772018-01-16 10:26:49 -0800361 EXPECT_EQ(RTCErrorType::NONE, result.error().type());
Steve Anton74255ff2018-01-24 18:32:57 -0800362 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;
Mirko Bonadei739baf02019-01-27 17:29:42 +0100368 for (const auto& receiver : pc()->GetReceivers()) {
Steve Anton74255ff2018-01-24 18:32:57 -0800369 if (receiver->media_type() == media_type) {
370 receivers.push_back(receiver);
371 }
372 }
373 return receivers;
deadbeef1dcb1642017-03-29 21:08:16 -0700374 }
375
Seth Hampson2f0d7022018-02-20 11:54:42 -0800376 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
deadbeef1dcb1642017-03-29 21:08:16 -0700386 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) {
Steve Antonda6c0952017-10-23 11:41:54 -0700393 CreateDataChannel(kDataChannelLabel, init);
394 }
395
396 void CreateDataChannel(const std::string& label,
397 const webrtc::DataChannelInit* init) {
398 data_channel_ = pc()->CreateDataChannel(label, init);
deadbeef1dcb1642017-03-29 21:08:16 -0700399 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;
Anders Carlsson5f2bb622018-05-14 09:48:06 +0200421 if (fake_video_renderers_.empty()) {
422 return 0;
deadbeef1dcb1642017-03-29 21:08:16 -0700423 }
deadbeef1dcb1642017-03-29 21:08:16 -0700424
Anders Carlsson5f2bb622018-05-14 09:48:06 +0200425 for (const auto& pair : fake_video_renderers_) {
426 min_frames = std::min(min_frames, pair.second->num_rendered_frames());
deadbeef1dcb1642017-03-29 21:08:16 -0700427 }
Anders Carlsson5f2bb622018-05-14 09:48:06 +0200428 return min_frames;
deadbeef1dcb1642017-03-29 21:08:16 -0700429 }
430
431 // Returns a MockStatsObserver in a state after stats gathering finished,
432 // which can be used to access the gathered stats.
deadbeefd8ad7882017-04-18 16:01:17 -0700433 rtc::scoped_refptr<MockStatsObserver> OldGetStatsForTrack(
deadbeef1dcb1642017-03-29 21:08:16 -0700434 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.
deadbeefd8ad7882017-04-18 16:01:17 -0700444 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();
deadbeef1dcb1642017-03-29 21:08:16 -0700456 }
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
Jonas Olsson7a6739e2019-01-15 16:31:55 +0100533 webrtc::PeerConnectionInterface::IceConnectionState
534 standardized_ice_connection_state() {
535 return pc()->standardized_ice_connection_state();
536 }
537
deadbeef1dcb1642017-03-29 21:08:16 -0700538 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();
Mirko Bonadeic61ce0d2017-11-21 17:04:20 +0100553 for (const rtc::scoped_refptr<RtpReceiverInterface>& receiver :
554 pc()->GetReceivers()) {
deadbeef1dcb1642017-03-29 21:08:16 -0700555 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
Qingsi Wangecd30542019-05-22 14:34:56 -0700562 rtc::FakeNetworkManager* network_manager() const {
Steve Antonede9ca52017-10-16 13:04:27 -0700563 return fake_network_manager_.get();
564 }
565 cricket::PortAllocator* port_allocator() const { return port_allocator_; }
566
Qingsi Wang7685e862018-06-11 20:15:46 -0700567 webrtc::FakeRtcEventLogFactory* event_log_factory() const {
568 return event_log_factory_;
569 }
570
Qingsi Wangc129c352019-04-18 10:41:58 -0700571 const cricket::Candidate& last_candidate_gathered() const {
572 return last_candidate_gathered_;
573 }
Eldar Relloda13ea22019-06-01 12:23:43 +0300574 const cricket::IceCandidateErrorEvent& error_event() const {
575 return error_event_;
576 }
Qingsi Wangc129c352019-04-18 10:41:58 -0700577
Qingsi Wangecd30542019-05-22 14:34:56 -0700578 // 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
Eldar Rello16d4c4d2019-10-09 18:29:44 +0300587 // 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
deadbeef1dcb1642017-03-29 21:08:16 -0700595 private:
596 explicit PeerConnectionWrapper(const std::string& debug_name)
597 : debug_name_(debug_name) {}
598
Bjorn Mellem175aa2e2018-11-08 11:23:22 -0800599 bool Init(
600 const PeerConnectionFactory::Options* options,
601 const PeerConnectionInterface::RTCConfiguration* config,
602 webrtc::PeerConnectionDependencies dependencies,
603 rtc::Thread* network_thread,
604 rtc::Thread* worker_thread,
605 std::unique_ptr<webrtc::FakeRtcEventLogFactory> event_log_factory,
606 std::unique_ptr<webrtc::MediaTransportFactory> media_transport_factory) {
deadbeef1dcb1642017-03-29 21:08:16 -0700607 // There's an error in this test code if Init ends up being called twice.
608 RTC_DCHECK(!peer_connection_);
609 RTC_DCHECK(!peer_connection_factory_);
610
611 fake_network_manager_.reset(new rtc::FakeNetworkManager());
Steve Antonede9ca52017-10-16 13:04:27 -0700612 fake_network_manager_->AddInterface(kDefaultLocalAddress);
deadbeef1dcb1642017-03-29 21:08:16 -0700613
614 std::unique_ptr<cricket::PortAllocator> port_allocator(
615 new cricket::BasicPortAllocator(fake_network_manager_.get()));
Steve Antonede9ca52017-10-16 13:04:27 -0700616 port_allocator_ = port_allocator.get();
deadbeef1dcb1642017-03-29 21:08:16 -0700617 fake_audio_capture_module_ = FakeAudioCaptureModule::Create();
618 if (!fake_audio_capture_module_) {
619 return false;
620 }
deadbeef1dcb1642017-03-29 21:08:16 -0700621 rtc::Thread* const signaling_thread = rtc::Thread::Current();
Qingsi Wang7685e862018-06-11 20:15:46 -0700622
623 webrtc::PeerConnectionFactoryDependencies pc_factory_dependencies;
624 pc_factory_dependencies.network_thread = network_thread;
625 pc_factory_dependencies.worker_thread = worker_thread;
626 pc_factory_dependencies.signaling_thread = signaling_thread;
Danil Chapovalov9da25bd2019-06-20 10:19:42 +0200627 pc_factory_dependencies.task_queue_factory =
628 webrtc::CreateDefaultTaskQueueFactory();
629 cricket::MediaEngineDependencies media_deps;
630 media_deps.task_queue_factory =
631 pc_factory_dependencies.task_queue_factory.get();
632 media_deps.adm = fake_audio_capture_module_;
633 webrtc::SetMediaEngineDefaults(&media_deps);
Qingsi Wang7685e862018-06-11 20:15:46 -0700634 pc_factory_dependencies.media_engine =
Danil Chapovalov9da25bd2019-06-20 10:19:42 +0200635 cricket::CreateMediaEngine(std::move(media_deps));
Qingsi Wang7685e862018-06-11 20:15:46 -0700636 pc_factory_dependencies.call_factory = webrtc::CreateCallFactory();
637 if (event_log_factory) {
638 event_log_factory_ = event_log_factory.get();
639 pc_factory_dependencies.event_log_factory = std::move(event_log_factory);
640 } else {
641 pc_factory_dependencies.event_log_factory =
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200642 std::make_unique<webrtc::RtcEventLogFactory>(
Danil Chapovalov9da25bd2019-06-20 10:19:42 +0200643 pc_factory_dependencies.task_queue_factory.get());
Qingsi Wang7685e862018-06-11 20:15:46 -0700644 }
Bjorn Mellem175aa2e2018-11-08 11:23:22 -0800645 if (media_transport_factory) {
646 pc_factory_dependencies.media_transport_factory =
647 std::move(media_transport_factory);
648 }
Qingsi Wang7685e862018-06-11 20:15:46 -0700649 peer_connection_factory_ = webrtc::CreateModularPeerConnectionFactory(
650 std::move(pc_factory_dependencies));
651
deadbeef1dcb1642017-03-29 21:08:16 -0700652 if (!peer_connection_factory_) {
653 return false;
654 }
655 if (options) {
656 peer_connection_factory_->SetOptions(*options);
657 }
Seth Hampson2f0d7022018-02-20 11:54:42 -0800658 if (config) {
659 sdp_semantics_ = config->sdp_semantics;
660 }
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700661
662 dependencies.allocator = std::move(port_allocator);
Niels Möllerf06f9232018-08-07 12:32:18 +0200663 peer_connection_ = CreatePeerConnection(config, std::move(dependencies));
deadbeef1dcb1642017-03-29 21:08:16 -0700664 return peer_connection_.get() != nullptr;
665 }
666
667 rtc::scoped_refptr<webrtc::PeerConnectionInterface> CreatePeerConnection(
deadbeef1dcb1642017-03-29 21:08:16 -0700668 const PeerConnectionInterface::RTCConfiguration* config,
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700669 webrtc::PeerConnectionDependencies dependencies) {
deadbeef1dcb1642017-03-29 21:08:16 -0700670 PeerConnectionInterface::RTCConfiguration modified_config;
671 // If |config| is null, this will result in a default configuration being
672 // used.
673 if (config) {
674 modified_config = *config;
675 }
676 // Disable resolution adaptation; we don't want it interfering with the
677 // test results.
678 // TODO(deadbeef): Do something more robust. Since we're testing for aspect
679 // ratios and not specific resolutions, is this even necessary?
680 modified_config.set_cpu_adaptation(false);
681
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700682 dependencies.observer = this;
deadbeef1dcb1642017-03-29 21:08:16 -0700683 return peer_connection_factory_->CreatePeerConnection(
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700684 modified_config, std::move(dependencies));
deadbeef1dcb1642017-03-29 21:08:16 -0700685 }
686
687 void set_signaling_message_receiver(
688 SignalingMessageReceiver* signaling_message_receiver) {
689 signaling_message_receiver_ = signaling_message_receiver;
690 }
691
692 void set_signaling_delay_ms(int delay_ms) { signaling_delay_ms_ = delay_ms; }
693
Steve Antonede9ca52017-10-16 13:04:27 -0700694 void set_signal_ice_candidates(bool signal) {
695 signal_ice_candidates_ = signal;
696 }
697
deadbeef1dcb1642017-03-29 21:08:16 -0700698 rtc::scoped_refptr<webrtc::VideoTrackInterface> CreateLocalVideoTrackInternal(
Niels Möller5c7efe72018-05-11 10:34:46 +0200699 webrtc::FakePeriodicVideoSource::Config config) {
deadbeef1dcb1642017-03-29 21:08:16 -0700700 // Set max frame rate to 10fps to reduce the risk of test flakiness.
701 // TODO(deadbeef): Do something more robust.
Niels Möller5c7efe72018-05-11 10:34:46 +0200702 config.frame_interval_ms = 100;
deadbeef1dcb1642017-03-29 21:08:16 -0700703
Niels Möller5c7efe72018-05-11 10:34:46 +0200704 video_track_sources_.emplace_back(
Niels Möller0f405822018-05-17 09:16:41 +0200705 new rtc::RefCountedObject<webrtc::FakePeriodicVideoTrackSource>(
706 config, false /* remote */));
deadbeef1dcb1642017-03-29 21:08:16 -0700707 rtc::scoped_refptr<webrtc::VideoTrackInterface> track(
Niels Möller5c7efe72018-05-11 10:34:46 +0200708 peer_connection_factory_->CreateVideoTrack(
709 rtc::CreateRandomUuid(), video_track_sources_.back()));
deadbeef1dcb1642017-03-29 21:08:16 -0700710 if (!local_video_renderer_) {
711 local_video_renderer_.reset(new webrtc::FakeVideoTrackRenderer(track));
712 }
713 return track;
714 }
715
716 void HandleIncomingOffer(const std::string& msg) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100717 RTC_LOG(LS_INFO) << debug_name_ << ": HandleIncomingOffer";
Steve Antona3a92c22017-12-07 10:27:41 -0800718 std::unique_ptr<SessionDescriptionInterface> desc =
719 webrtc::CreateSessionDescription(SdpType::kOffer, msg);
deadbeef1dcb1642017-03-29 21:08:16 -0700720 if (received_sdp_munger_) {
721 received_sdp_munger_(desc->description());
722 }
723
724 EXPECT_TRUE(SetRemoteDescription(std::move(desc)));
725 // Setting a remote description may have changed the number of receivers,
726 // so reset the receiver observers.
727 ResetRtpReceiverObservers();
Seth Hampson2f0d7022018-02-20 11:54:42 -0800728 if (remote_offer_handler_) {
729 remote_offer_handler_();
730 }
deadbeef1dcb1642017-03-29 21:08:16 -0700731 auto answer = CreateAnswer();
732 ASSERT_NE(nullptr, answer);
733 EXPECT_TRUE(SetLocalDescriptionAndSendSdpMessage(std::move(answer)));
734 }
735
736 void HandleIncomingAnswer(const std::string& msg) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100737 RTC_LOG(LS_INFO) << debug_name_ << ": HandleIncomingAnswer";
Steve Antona3a92c22017-12-07 10:27:41 -0800738 std::unique_ptr<SessionDescriptionInterface> desc =
739 webrtc::CreateSessionDescription(SdpType::kAnswer, msg);
deadbeef1dcb1642017-03-29 21:08:16 -0700740 if (received_sdp_munger_) {
741 received_sdp_munger_(desc->description());
742 }
743
744 EXPECT_TRUE(SetRemoteDescription(std::move(desc)));
745 // Set the RtpReceiverObserver after receivers are created.
746 ResetRtpReceiverObservers();
747 }
748
749 // Returns null on failure.
deadbeef1dcb1642017-03-29 21:08:16 -0700750 std::unique_ptr<SessionDescriptionInterface> CreateAnswer() {
751 rtc::scoped_refptr<MockCreateSessionDescriptionObserver> observer(
752 new rtc::RefCountedObject<MockCreateSessionDescriptionObserver>());
753 pc()->CreateAnswer(observer, offer_answer_options_);
754 return WaitForDescriptionFromObserver(observer);
755 }
756
757 std::unique_ptr<SessionDescriptionInterface> WaitForDescriptionFromObserver(
Mirko Bonadeic61ce0d2017-11-21 17:04:20 +0100758 MockCreateSessionDescriptionObserver* observer) {
deadbeef1dcb1642017-03-29 21:08:16 -0700759 EXPECT_EQ_WAIT(true, observer->called(), kDefaultTimeout);
760 if (!observer->result()) {
761 return nullptr;
762 }
763 auto description = observer->MoveDescription();
764 if (generated_sdp_munger_) {
765 generated_sdp_munger_(description->description());
766 }
767 return description;
768 }
769
770 // Setting the local description and sending the SDP message over the fake
771 // signaling channel are combined into the same method because the SDP
772 // message needs to be sent as soon as SetLocalDescription finishes, without
773 // waiting for the observer to be called. This ensures that ICE candidates
774 // don't outrace the description.
775 bool SetLocalDescriptionAndSendSdpMessage(
776 std::unique_ptr<SessionDescriptionInterface> desc) {
777 rtc::scoped_refptr<MockSetSessionDescriptionObserver> observer(
778 new rtc::RefCountedObject<MockSetSessionDescriptionObserver>());
Mirko Bonadei675513b2017-11-09 11:09:25 +0100779 RTC_LOG(LS_INFO) << debug_name_ << ": SetLocalDescriptionAndSendSdpMessage";
Steve Antona3a92c22017-12-07 10:27:41 -0800780 SdpType type = desc->GetType();
deadbeef1dcb1642017-03-29 21:08:16 -0700781 std::string sdp;
782 EXPECT_TRUE(desc->ToString(&sdp));
Bjorn A Mellemb689af42019-08-21 10:44:59 -0700783 RTC_LOG(LS_INFO) << debug_name_ << ": local SDP contents=\n" << sdp;
deadbeef1dcb1642017-03-29 21:08:16 -0700784 pc()->SetLocalDescription(observer, desc.release());
Seth Hampson2f0d7022018-02-20 11:54:42 -0800785 if (sdp_semantics_ == SdpSemantics::kUnifiedPlan) {
786 RemoveUnusedVideoRenderers();
787 }
deadbeef1dcb1642017-03-29 21:08:16 -0700788 // As mentioned above, we need to send the message immediately after
789 // SetLocalDescription.
790 SendSdpMessage(type, sdp);
791 EXPECT_TRUE_WAIT(observer->called(), kDefaultTimeout);
792 return true;
793 }
794
795 bool SetRemoteDescription(std::unique_ptr<SessionDescriptionInterface> desc) {
796 rtc::scoped_refptr<MockSetSessionDescriptionObserver> observer(
797 new rtc::RefCountedObject<MockSetSessionDescriptionObserver>());
Mirko Bonadei675513b2017-11-09 11:09:25 +0100798 RTC_LOG(LS_INFO) << debug_name_ << ": SetRemoteDescription";
deadbeef1dcb1642017-03-29 21:08:16 -0700799 pc()->SetRemoteDescription(observer, desc.release());
Seth Hampson2f0d7022018-02-20 11:54:42 -0800800 if (sdp_semantics_ == SdpSemantics::kUnifiedPlan) {
801 RemoveUnusedVideoRenderers();
802 }
deadbeef1dcb1642017-03-29 21:08:16 -0700803 EXPECT_TRUE_WAIT(observer->called(), kDefaultTimeout);
804 return observer->result();
805 }
806
Seth Hampson2f0d7022018-02-20 11:54:42 -0800807 // This is a work around to remove unused fake_video_renderers from
808 // transceivers that have either stopped or are no longer receiving.
809 void RemoveUnusedVideoRenderers() {
810 auto transceivers = pc()->GetTransceivers();
811 for (auto& transceiver : transceivers) {
812 if (transceiver->receiver()->media_type() != cricket::MEDIA_TYPE_VIDEO) {
813 continue;
814 }
815 // Remove fake video renderers from any stopped transceivers.
816 if (transceiver->stopped()) {
817 auto it =
818 fake_video_renderers_.find(transceiver->receiver()->track()->id());
819 if (it != fake_video_renderers_.end()) {
820 fake_video_renderers_.erase(it);
821 }
822 }
823 // Remove fake video renderers from any transceivers that are no longer
824 // receiving.
825 if ((transceiver->current_direction() &&
826 !webrtc::RtpTransceiverDirectionHasRecv(
827 *transceiver->current_direction()))) {
828 auto it =
829 fake_video_renderers_.find(transceiver->receiver()->track()->id());
830 if (it != fake_video_renderers_.end()) {
831 fake_video_renderers_.erase(it);
832 }
833 }
834 }
835 }
836
deadbeef1dcb1642017-03-29 21:08:16 -0700837 // Simulate sending a blob of SDP with delay |signaling_delay_ms_| (0 by
838 // default).
Steve Antona3a92c22017-12-07 10:27:41 -0800839 void SendSdpMessage(SdpType type, const std::string& msg) {
deadbeef1dcb1642017-03-29 21:08:16 -0700840 if (signaling_delay_ms_ == 0) {
841 RelaySdpMessageIfReceiverExists(type, msg);
842 } else {
843 invoker_.AsyncInvokeDelayed<void>(
844 RTC_FROM_HERE, rtc::Thread::Current(),
845 rtc::Bind(&PeerConnectionWrapper::RelaySdpMessageIfReceiverExists,
846 this, type, msg),
847 signaling_delay_ms_);
848 }
849 }
850
Steve Antona3a92c22017-12-07 10:27:41 -0800851 void RelaySdpMessageIfReceiverExists(SdpType type, const std::string& msg) {
deadbeef1dcb1642017-03-29 21:08:16 -0700852 if (signaling_message_receiver_) {
853 signaling_message_receiver_->ReceiveSdpMessage(type, msg);
854 }
855 }
856
857 // Simulate trickling an ICE candidate with delay |signaling_delay_ms_| (0 by
858 // default).
859 void SendIceMessage(const std::string& sdp_mid,
860 int sdp_mline_index,
861 const std::string& msg) {
862 if (signaling_delay_ms_ == 0) {
863 RelayIceMessageIfReceiverExists(sdp_mid, sdp_mline_index, msg);
864 } else {
865 invoker_.AsyncInvokeDelayed<void>(
866 RTC_FROM_HERE, rtc::Thread::Current(),
867 rtc::Bind(&PeerConnectionWrapper::RelayIceMessageIfReceiverExists,
868 this, sdp_mid, sdp_mline_index, msg),
869 signaling_delay_ms_);
870 }
871 }
872
873 void RelayIceMessageIfReceiverExists(const std::string& sdp_mid,
874 int sdp_mline_index,
875 const std::string& msg) {
876 if (signaling_message_receiver_) {
877 signaling_message_receiver_->ReceiveIceMessage(sdp_mid, sdp_mline_index,
878 msg);
879 }
880 }
881
882 // SignalingMessageReceiver callbacks.
Steve Antona3a92c22017-12-07 10:27:41 -0800883 void ReceiveSdpMessage(SdpType type, const std::string& msg) override {
884 if (type == SdpType::kOffer) {
deadbeef1dcb1642017-03-29 21:08:16 -0700885 HandleIncomingOffer(msg);
886 } else {
887 HandleIncomingAnswer(msg);
888 }
889 }
890
891 void ReceiveIceMessage(const std::string& sdp_mid,
892 int sdp_mline_index,
893 const std::string& msg) override {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100894 RTC_LOG(LS_INFO) << debug_name_ << ": ReceiveIceMessage";
deadbeef1dcb1642017-03-29 21:08:16 -0700895 std::unique_ptr<webrtc::IceCandidateInterface> candidate(
896 webrtc::CreateIceCandidate(sdp_mid, sdp_mline_index, msg, nullptr));
897 EXPECT_TRUE(pc()->AddIceCandidate(candidate.get()));
898 }
899
900 // PeerConnectionObserver callbacks.
901 void OnSignalingChange(
902 webrtc::PeerConnectionInterface::SignalingState new_state) override {
903 EXPECT_EQ(pc()->signaling_state(), new_state);
Eldar Rello16d4c4d2019-10-09 18:29:44 +0300904 peer_connection_signaling_state_history_.push_back(new_state);
deadbeef1dcb1642017-03-29 21:08:16 -0700905 }
Steve Anton15324772018-01-16 10:26:49 -0800906 void OnAddTrack(rtc::scoped_refptr<RtpReceiverInterface> receiver,
907 const std::vector<rtc::scoped_refptr<MediaStreamInterface>>&
908 streams) override {
909 if (receiver->media_type() == cricket::MEDIA_TYPE_VIDEO) {
910 rtc::scoped_refptr<VideoTrackInterface> video_track(
911 static_cast<VideoTrackInterface*>(receiver->track().get()));
912 ASSERT_TRUE(fake_video_renderers_.find(video_track->id()) ==
deadbeef1dcb1642017-03-29 21:08:16 -0700913 fake_video_renderers_.end());
Steve Anton15324772018-01-16 10:26:49 -0800914 fake_video_renderers_[video_track->id()] =
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200915 std::make_unique<FakeVideoTrackRenderer>(video_track);
deadbeef1dcb1642017-03-29 21:08:16 -0700916 }
917 }
Steve Anton15324772018-01-16 10:26:49 -0800918 void OnRemoveTrack(
919 rtc::scoped_refptr<RtpReceiverInterface> receiver) override {
920 if (receiver->media_type() == cricket::MEDIA_TYPE_VIDEO) {
921 auto it = fake_video_renderers_.find(receiver->track()->id());
922 RTC_DCHECK(it != fake_video_renderers_.end());
923 fake_video_renderers_.erase(it);
924 }
925 }
deadbeef1dcb1642017-03-29 21:08:16 -0700926 void OnRenegotiationNeeded() override {}
927 void OnIceConnectionChange(
928 webrtc::PeerConnectionInterface::IceConnectionState new_state) override {
929 EXPECT_EQ(pc()->ice_connection_state(), new_state);
Steve Antonede9ca52017-10-16 13:04:27 -0700930 ice_connection_state_history_.push_back(new_state);
deadbeef1dcb1642017-03-29 21:08:16 -0700931 }
Jonas Olssonacd8ae72019-02-25 15:26:24 +0100932 void OnStandardizedIceConnectionChange(
933 webrtc::PeerConnectionInterface::IceConnectionState new_state) override {
934 standardized_ice_connection_state_history_.push_back(new_state);
935 }
Jonas Olsson635474e2018-10-18 15:58:17 +0200936 void OnConnectionChange(
937 webrtc::PeerConnectionInterface::PeerConnectionState new_state) override {
938 peer_connection_state_history_.push_back(new_state);
939 }
940
deadbeef1dcb1642017-03-29 21:08:16 -0700941 void OnIceGatheringChange(
942 webrtc::PeerConnectionInterface::IceGatheringState new_state) override {
deadbeef1dcb1642017-03-29 21:08:16 -0700943 EXPECT_EQ(pc()->ice_gathering_state(), new_state);
Steve Antonede9ca52017-10-16 13:04:27 -0700944 ice_gathering_state_history_.push_back(new_state);
deadbeef1dcb1642017-03-29 21:08:16 -0700945 }
Alex Drake00c7ecf2019-08-06 10:54:47 -0700946
947 void OnIceSelectedCandidatePairChanged(
948 const cricket::CandidatePairChangeEvent& event) {
949 ice_candidate_pair_change_history_.push_back(event);
950 }
Alex Drake43faee02019-08-12 16:27:34 -0700951
deadbeef1dcb1642017-03-29 21:08:16 -0700952 void OnIceCandidate(const webrtc::IceCandidateInterface* candidate) override {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100953 RTC_LOG(LS_INFO) << debug_name_ << ": OnIceCandidate";
deadbeef1dcb1642017-03-29 21:08:16 -0700954
Qingsi Wang1dac6d82018-12-12 15:28:47 -0800955 if (remote_async_resolver_) {
956 const auto& local_candidate = candidate->candidate();
Qingsi Wang1dac6d82018-12-12 15:28:47 -0800957 if (local_candidate.address().IsUnresolvedIP()) {
958 RTC_DCHECK(local_candidate.type() == cricket::LOCAL_PORT_TYPE);
959 rtc::SocketAddress resolved_addr(local_candidate.address());
Qingsi Wangecd30542019-05-22 14:34:56 -0700960 const auto resolved_ip = mdns_responder_->GetMappedAddressForName(
Qingsi Wang1dac6d82018-12-12 15:28:47 -0800961 local_candidate.address().hostname());
962 RTC_DCHECK(!resolved_ip.IsNil());
963 resolved_addr.SetResolvedIP(resolved_ip);
964 EXPECT_CALL(*remote_async_resolver_, GetResolvedAddress(_, _))
965 .WillOnce(DoAll(SetArgPointee<1>(resolved_addr), Return(true)));
966 EXPECT_CALL(*remote_async_resolver_, Destroy(_));
Zach Stein6fcdc2f2018-08-23 16:25:55 -0700967 }
Zach Stein6fcdc2f2018-08-23 16:25:55 -0700968 }
969
deadbeef1dcb1642017-03-29 21:08:16 -0700970 std::string ice_sdp;
Qingsi Wang1dac6d82018-12-12 15:28:47 -0800971 EXPECT_TRUE(candidate->ToString(&ice_sdp));
Steve Antonede9ca52017-10-16 13:04:27 -0700972 if (signaling_message_receiver_ == nullptr || !signal_ice_candidates_) {
deadbeef1dcb1642017-03-29 21:08:16 -0700973 // Remote party may be deleted.
974 return;
975 }
Qingsi Wang1dac6d82018-12-12 15:28:47 -0800976 SendIceMessage(candidate->sdp_mid(), candidate->sdp_mline_index(), ice_sdp);
Qingsi Wangc129c352019-04-18 10:41:58 -0700977 last_candidate_gathered_ = candidate->candidate();
deadbeef1dcb1642017-03-29 21:08:16 -0700978 }
Eldar Relloda13ea22019-06-01 12:23:43 +0300979 void OnIceCandidateError(const std::string& host_candidate,
980 const std::string& url,
981 int error_code,
982 const std::string& error_text) override {
983 error_event_ = cricket::IceCandidateErrorEvent(host_candidate, url,
984 error_code, error_text);
985 }
deadbeef1dcb1642017-03-29 21:08:16 -0700986 void OnDataChannel(
987 rtc::scoped_refptr<DataChannelInterface> data_channel) override {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100988 RTC_LOG(LS_INFO) << debug_name_ << ": OnDataChannel";
deadbeef1dcb1642017-03-29 21:08:16 -0700989 data_channel_ = data_channel;
990 data_observer_.reset(new MockDataChannelObserver(data_channel));
991 }
992
deadbeef1dcb1642017-03-29 21:08:16 -0700993 std::string debug_name_;
994
995 std::unique_ptr<rtc::FakeNetworkManager> fake_network_manager_;
Qingsi Wangecd30542019-05-22 14:34:56 -0700996 // Reference to the mDNS responder owned by |fake_network_manager_| after set.
997 webrtc::FakeMdnsResponder* mdns_responder_ = nullptr;
deadbeef1dcb1642017-03-29 21:08:16 -0700998
999 rtc::scoped_refptr<webrtc::PeerConnectionInterface> peer_connection_;
1000 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface>
1001 peer_connection_factory_;
1002
Steve Antonede9ca52017-10-16 13:04:27 -07001003 cricket::PortAllocator* port_allocator_;
deadbeef1dcb1642017-03-29 21:08:16 -07001004 // Needed to keep track of number of frames sent.
1005 rtc::scoped_refptr<FakeAudioCaptureModule> fake_audio_capture_module_;
1006 // Needed to keep track of number of frames received.
1007 std::map<std::string, std::unique_ptr<webrtc::FakeVideoTrackRenderer>>
1008 fake_video_renderers_;
1009 // Needed to ensure frames aren't received for removed tracks.
1010 std::vector<std::unique_ptr<webrtc::FakeVideoTrackRenderer>>
1011 removed_fake_video_renderers_;
deadbeef1dcb1642017-03-29 21:08:16 -07001012
1013 // For remote peer communication.
1014 SignalingMessageReceiver* signaling_message_receiver_ = nullptr;
1015 int signaling_delay_ms_ = 0;
Steve Antonede9ca52017-10-16 13:04:27 -07001016 bool signal_ice_candidates_ = true;
Qingsi Wangc129c352019-04-18 10:41:58 -07001017 cricket::Candidate last_candidate_gathered_;
Eldar Relloda13ea22019-06-01 12:23:43 +03001018 cricket::IceCandidateErrorEvent error_event_;
deadbeef1dcb1642017-03-29 21:08:16 -07001019
Niels Möller5c7efe72018-05-11 10:34:46 +02001020 // Store references to the video sources we've created, so that we can stop
deadbeef1dcb1642017-03-29 21:08:16 -07001021 // them, if required.
Niels Möller5c7efe72018-05-11 10:34:46 +02001022 std::vector<rtc::scoped_refptr<webrtc::VideoTrackSource>>
1023 video_track_sources_;
deadbeef1dcb1642017-03-29 21:08:16 -07001024 // |local_video_renderer_| attached to the first created local video track.
1025 std::unique_ptr<webrtc::FakeVideoTrackRenderer> local_video_renderer_;
1026
Seth Hampson2f0d7022018-02-20 11:54:42 -08001027 SdpSemantics sdp_semantics_;
deadbeef1dcb1642017-03-29 21:08:16 -07001028 PeerConnectionInterface::RTCOfferAnswerOptions offer_answer_options_;
1029 std::function<void(cricket::SessionDescription*)> received_sdp_munger_;
1030 std::function<void(cricket::SessionDescription*)> generated_sdp_munger_;
Seth Hampson2f0d7022018-02-20 11:54:42 -08001031 std::function<void()> remote_offer_handler_;
Qingsi Wang1dac6d82018-12-12 15:28:47 -08001032 rtc::MockAsyncResolver* remote_async_resolver_ = nullptr;
deadbeef1dcb1642017-03-29 21:08:16 -07001033 rtc::scoped_refptr<DataChannelInterface> data_channel_;
1034 std::unique_ptr<MockDataChannelObserver> data_observer_;
1035
1036 std::vector<std::unique_ptr<MockRtpReceiverObserver>> rtp_receiver_observers_;
1037
Steve Antonede9ca52017-10-16 13:04:27 -07001038 std::vector<PeerConnectionInterface::IceConnectionState>
1039 ice_connection_state_history_;
Jonas Olssonacd8ae72019-02-25 15:26:24 +01001040 std::vector<PeerConnectionInterface::IceConnectionState>
1041 standardized_ice_connection_state_history_;
Jonas Olsson635474e2018-10-18 15:58:17 +02001042 std::vector<PeerConnectionInterface::PeerConnectionState>
1043 peer_connection_state_history_;
Steve Antonede9ca52017-10-16 13:04:27 -07001044 std::vector<PeerConnectionInterface::IceGatheringState>
1045 ice_gathering_state_history_;
Alex Drake00c7ecf2019-08-06 10:54:47 -07001046 std::vector<cricket::CandidatePairChangeEvent>
1047 ice_candidate_pair_change_history_;
Eldar Rello16d4c4d2019-10-09 18:29:44 +03001048 std::vector<PeerConnectionInterface::SignalingState>
1049 peer_connection_signaling_state_history_;
Qingsi Wang7685e862018-06-11 20:15:46 -07001050 webrtc::FakeRtcEventLogFactory* event_log_factory_;
1051
deadbeef1dcb1642017-03-29 21:08:16 -07001052 rtc::AsyncInvoker invoker_;
1053
Seth Hampson2f0d7022018-02-20 11:54:42 -08001054 friend class PeerConnectionIntegrationBaseTest;
deadbeef1dcb1642017-03-29 21:08:16 -07001055};
1056
Elad Alon99c3fe52017-10-13 16:29:40 +02001057class MockRtcEventLogOutput : public webrtc::RtcEventLogOutput {
1058 public:
1059 virtual ~MockRtcEventLogOutput() = default;
1060 MOCK_CONST_METHOD0(IsActive, bool());
1061 MOCK_METHOD1(Write, bool(const std::string&));
1062};
1063
Seth Hampson2f0d7022018-02-20 11:54:42 -08001064// This helper object is used for both specifying how many audio/video frames
1065// are expected to be received for a caller/callee. It provides helper functions
1066// to specify these expectations. The object initially starts in a state of no
1067// expectations.
1068class MediaExpectations {
1069 public:
1070 enum ExpectFrames {
1071 kExpectSomeFrames,
1072 kExpectNoFrames,
1073 kNoExpectation,
1074 };
1075
1076 void ExpectBidirectionalAudioAndVideo() {
1077 ExpectBidirectionalAudio();
1078 ExpectBidirectionalVideo();
1079 }
1080
1081 void ExpectBidirectionalAudio() {
1082 CallerExpectsSomeAudio();
1083 CalleeExpectsSomeAudio();
1084 }
1085
1086 void ExpectNoAudio() {
1087 CallerExpectsNoAudio();
1088 CalleeExpectsNoAudio();
1089 }
1090
1091 void ExpectBidirectionalVideo() {
1092 CallerExpectsSomeVideo();
1093 CalleeExpectsSomeVideo();
1094 }
1095
1096 void ExpectNoVideo() {
1097 CallerExpectsNoVideo();
1098 CalleeExpectsNoVideo();
1099 }
1100
1101 void CallerExpectsSomeAudioAndVideo() {
1102 CallerExpectsSomeAudio();
1103 CallerExpectsSomeVideo();
1104 }
1105
1106 void CalleeExpectsSomeAudioAndVideo() {
1107 CalleeExpectsSomeAudio();
1108 CalleeExpectsSomeVideo();
1109 }
1110
1111 // Caller's audio functions.
1112 void CallerExpectsSomeAudio(
1113 int expected_audio_frames = kDefaultExpectedAudioFrameCount) {
1114 caller_audio_expectation_ = kExpectSomeFrames;
1115 caller_audio_frames_expected_ = expected_audio_frames;
1116 }
1117
1118 void CallerExpectsNoAudio() {
1119 caller_audio_expectation_ = kExpectNoFrames;
1120 caller_audio_frames_expected_ = 0;
1121 }
1122
1123 // Caller's video functions.
1124 void CallerExpectsSomeVideo(
1125 int expected_video_frames = kDefaultExpectedVideoFrameCount) {
1126 caller_video_expectation_ = kExpectSomeFrames;
1127 caller_video_frames_expected_ = expected_video_frames;
1128 }
1129
1130 void CallerExpectsNoVideo() {
1131 caller_video_expectation_ = kExpectNoFrames;
1132 caller_video_frames_expected_ = 0;
1133 }
1134
1135 // Callee's audio functions.
1136 void CalleeExpectsSomeAudio(
1137 int expected_audio_frames = kDefaultExpectedAudioFrameCount) {
1138 callee_audio_expectation_ = kExpectSomeFrames;
1139 callee_audio_frames_expected_ = expected_audio_frames;
1140 }
1141
1142 void CalleeExpectsNoAudio() {
1143 callee_audio_expectation_ = kExpectNoFrames;
1144 callee_audio_frames_expected_ = 0;
1145 }
1146
1147 // Callee's video functions.
1148 void CalleeExpectsSomeVideo(
1149 int expected_video_frames = kDefaultExpectedVideoFrameCount) {
1150 callee_video_expectation_ = kExpectSomeFrames;
1151 callee_video_frames_expected_ = expected_video_frames;
1152 }
1153
1154 void CalleeExpectsNoVideo() {
1155 callee_video_expectation_ = kExpectNoFrames;
1156 callee_video_frames_expected_ = 0;
1157 }
1158
1159 ExpectFrames caller_audio_expectation_ = kNoExpectation;
1160 ExpectFrames caller_video_expectation_ = kNoExpectation;
1161 ExpectFrames callee_audio_expectation_ = kNoExpectation;
1162 ExpectFrames callee_video_expectation_ = kNoExpectation;
1163 int caller_audio_frames_expected_ = 0;
1164 int caller_video_frames_expected_ = 0;
1165 int callee_audio_frames_expected_ = 0;
1166 int callee_video_frames_expected_ = 0;
1167};
1168
deadbeef1dcb1642017-03-29 21:08:16 -07001169// Tests two PeerConnections connecting to each other end-to-end, using a
1170// virtual network, fake A/V capture and fake encoder/decoders. The
1171// PeerConnections share the threads/socket servers, but use separate versions
1172// of everything else (including "PeerConnectionFactory"s).
Mirko Bonadei6a489f22019-04-09 15:11:12 +02001173class PeerConnectionIntegrationBaseTest : public ::testing::Test {
deadbeef1dcb1642017-03-29 21:08:16 -07001174 public:
Seth Hampson2f0d7022018-02-20 11:54:42 -08001175 explicit PeerConnectionIntegrationBaseTest(SdpSemantics sdp_semantics)
1176 : sdp_semantics_(sdp_semantics),
1177 ss_(new rtc::VirtualSocketServer()),
Steve Antonede9ca52017-10-16 13:04:27 -07001178 fss_(new rtc::FirewallSocketServer(ss_.get())),
1179 network_thread_(new rtc::Thread(fss_.get())),
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001180 worker_thread_(rtc::Thread::Create()),
1181 loopback_media_transports_(network_thread_.get()) {
Sebastian Jansson8a793a02018-03-13 15:21:48 +01001182 network_thread_->SetName("PCNetworkThread", this);
1183 worker_thread_->SetName("PCWorkerThread", this);
deadbeef1dcb1642017-03-29 21:08:16 -07001184 RTC_CHECK(network_thread_->Start());
1185 RTC_CHECK(worker_thread_->Start());
Qingsi Wang7fc821d2018-07-12 12:54:53 -07001186 webrtc::metrics::Reset();
deadbeef1dcb1642017-03-29 21:08:16 -07001187 }
1188
Seth Hampson2f0d7022018-02-20 11:54:42 -08001189 ~PeerConnectionIntegrationBaseTest() {
Seth Hampsonaed71642018-06-11 07:41:32 -07001190 // The PeerConnections should deleted before the TurnCustomizers.
1191 // A TurnPort is created with a raw pointer to a TurnCustomizer. The
1192 // TurnPort has the same lifetime as the PeerConnection, so it's expected
1193 // that the TurnCustomizer outlives the life of the PeerConnection or else
1194 // when Send() is called it will hit a seg fault.
deadbeef1dcb1642017-03-29 21:08:16 -07001195 if (caller_) {
1196 caller_->set_signaling_message_receiver(nullptr);
Seth Hampsonaed71642018-06-11 07:41:32 -07001197 delete SetCallerPcWrapperAndReturnCurrent(nullptr);
deadbeef1dcb1642017-03-29 21:08:16 -07001198 }
1199 if (callee_) {
1200 callee_->set_signaling_message_receiver(nullptr);
Seth Hampsonaed71642018-06-11 07:41:32 -07001201 delete SetCalleePcWrapperAndReturnCurrent(nullptr);
deadbeef1dcb1642017-03-29 21:08:16 -07001202 }
Seth Hampsonaed71642018-06-11 07:41:32 -07001203
1204 // If turn servers were created for the test they need to be destroyed on
1205 // the network thread.
1206 network_thread()->Invoke<void>(RTC_FROM_HERE, [this] {
1207 turn_servers_.clear();
1208 turn_customizers_.clear();
1209 });
deadbeef1dcb1642017-03-29 21:08:16 -07001210 }
1211
1212 bool SignalingStateStable() {
1213 return caller_->SignalingStateStable() && callee_->SignalingStateStable();
1214 }
1215
deadbeef71452802017-05-07 17:21:01 -07001216 bool DtlsConnected() {
Alex Loiko9289eda2018-11-23 16:18:59 +00001217 // TODO(deadbeef): kIceConnectionConnected currently means both ICE and DTLS
1218 // are connected. This is an important distinction. Once we have separate
1219 // ICE and DTLS state, this check needs to use the DTLS state.
1220 return (callee()->ice_connection_state() ==
1221 webrtc::PeerConnectionInterface::kIceConnectionConnected ||
1222 callee()->ice_connection_state() ==
1223 webrtc::PeerConnectionInterface::kIceConnectionCompleted) &&
1224 (caller()->ice_connection_state() ==
1225 webrtc::PeerConnectionInterface::kIceConnectionConnected ||
1226 caller()->ice_connection_state() ==
1227 webrtc::PeerConnectionInterface::kIceConnectionCompleted);
deadbeef71452802017-05-07 17:21:01 -07001228 }
1229
Qingsi Wang7685e862018-06-11 20:15:46 -07001230 // When |event_log_factory| is null, the default implementation of the event
1231 // log factory will be used.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001232 std::unique_ptr<PeerConnectionWrapper> CreatePeerConnectionWrapper(
1233 const std::string& debug_name,
Seth Hampson2f0d7022018-02-20 11:54:42 -08001234 const PeerConnectionFactory::Options* options,
1235 const RTCConfiguration* config,
Qingsi Wang7685e862018-06-11 20:15:46 -07001236 webrtc::PeerConnectionDependencies dependencies,
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001237 std::unique_ptr<webrtc::FakeRtcEventLogFactory> event_log_factory,
1238 std::unique_ptr<webrtc::MediaTransportFactory> media_transport_factory) {
Seth Hampson2f0d7022018-02-20 11:54:42 -08001239 RTCConfiguration modified_config;
1240 if (config) {
1241 modified_config = *config;
1242 }
Steve Anton3acffc32018-04-12 17:21:03 -07001243 modified_config.sdp_semantics = sdp_semantics_;
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001244 if (!dependencies.cert_generator) {
1245 dependencies.cert_generator =
Mirko Bonadei317a1f02019-09-17 17:06:18 +02001246 std::make_unique<FakeRTCCertificateGenerator>();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001247 }
1248 std::unique_ptr<PeerConnectionWrapper> client(
1249 new PeerConnectionWrapper(debug_name));
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001250
Niels Möllerf06f9232018-08-07 12:32:18 +02001251 if (!client->Init(options, &modified_config, std::move(dependencies),
1252 network_thread_.get(), worker_thread_.get(),
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001253 std::move(event_log_factory),
1254 std::move(media_transport_factory))) {
Seth Hampson2f0d7022018-02-20 11:54:42 -08001255 return nullptr;
1256 }
1257 return client;
1258 }
1259
Qingsi Wang7685e862018-06-11 20:15:46 -07001260 std::unique_ptr<PeerConnectionWrapper>
1261 CreatePeerConnectionWrapperWithFakeRtcEventLog(
1262 const std::string& debug_name,
Qingsi Wang7685e862018-06-11 20:15:46 -07001263 const PeerConnectionFactory::Options* options,
1264 const RTCConfiguration* config,
1265 webrtc::PeerConnectionDependencies dependencies) {
1266 std::unique_ptr<webrtc::FakeRtcEventLogFactory> event_log_factory(
1267 new webrtc::FakeRtcEventLogFactory(rtc::Thread::Current()));
Niels Möllerf06f9232018-08-07 12:32:18 +02001268 return CreatePeerConnectionWrapper(debug_name, options, config,
Qingsi Wang7685e862018-06-11 20:15:46 -07001269 std::move(dependencies),
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001270 std::move(event_log_factory),
1271 /*media_transport_factory=*/nullptr);
Qingsi Wang7685e862018-06-11 20:15:46 -07001272 }
1273
deadbeef1dcb1642017-03-29 21:08:16 -07001274 bool CreatePeerConnectionWrappers() {
1275 return CreatePeerConnectionWrappersWithConfig(
1276 PeerConnectionInterface::RTCConfiguration(),
1277 PeerConnectionInterface::RTCConfiguration());
1278 }
1279
Steve Anton3acffc32018-04-12 17:21:03 -07001280 bool CreatePeerConnectionWrappersWithSdpSemantics(
1281 SdpSemantics caller_semantics,
1282 SdpSemantics callee_semantics) {
1283 // Can't specify the sdp_semantics in the passed-in configuration since it
1284 // will be overwritten by CreatePeerConnectionWrapper with whatever is
1285 // stored in sdp_semantics_. So get around this by modifying the instance
1286 // variable before calling CreatePeerConnectionWrapper for the caller and
1287 // callee PeerConnections.
1288 SdpSemantics original_semantics = sdp_semantics_;
1289 sdp_semantics_ = caller_semantics;
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001290 caller_ = CreatePeerConnectionWrapper(
Niels Möllerf06f9232018-08-07 12:32:18 +02001291 "Caller", nullptr, nullptr, webrtc::PeerConnectionDependencies(nullptr),
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001292 nullptr, /*media_transport_factory=*/nullptr);
Steve Anton3acffc32018-04-12 17:21:03 -07001293 sdp_semantics_ = callee_semantics;
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001294 callee_ = CreatePeerConnectionWrapper(
Niels Möllerf06f9232018-08-07 12:32:18 +02001295 "Callee", nullptr, nullptr, webrtc::PeerConnectionDependencies(nullptr),
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001296 nullptr, /*media_transport_factory=*/nullptr);
Steve Anton3acffc32018-04-12 17:21:03 -07001297 sdp_semantics_ = original_semantics;
1298 return caller_ && callee_;
1299 }
1300
deadbeef1dcb1642017-03-29 21:08:16 -07001301 bool CreatePeerConnectionWrappersWithConfig(
1302 const PeerConnectionInterface::RTCConfiguration& caller_config,
1303 const PeerConnectionInterface::RTCConfiguration& callee_config) {
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001304 caller_ = CreatePeerConnectionWrapper(
Niels Möllerf06f9232018-08-07 12:32:18 +02001305 "Caller", nullptr, &caller_config,
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001306 webrtc::PeerConnectionDependencies(nullptr), nullptr,
1307 /*media_transport_factory=*/nullptr);
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001308 callee_ = CreatePeerConnectionWrapper(
Niels Möllerf06f9232018-08-07 12:32:18 +02001309 "Callee", nullptr, &callee_config,
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001310 webrtc::PeerConnectionDependencies(nullptr), nullptr,
1311 /*media_transport_factory=*/nullptr);
1312 return caller_ && callee_;
1313 }
1314
1315 bool CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
1316 const PeerConnectionInterface::RTCConfiguration& caller_config,
1317 const PeerConnectionInterface::RTCConfiguration& callee_config,
1318 std::unique_ptr<webrtc::MediaTransportFactory> caller_factory,
1319 std::unique_ptr<webrtc::MediaTransportFactory> callee_factory) {
1320 caller_ =
1321 CreatePeerConnectionWrapper("Caller", nullptr, &caller_config,
1322 webrtc::PeerConnectionDependencies(nullptr),
1323 nullptr, std::move(caller_factory));
1324 callee_ =
1325 CreatePeerConnectionWrapper("Callee", nullptr, &callee_config,
1326 webrtc::PeerConnectionDependencies(nullptr),
1327 nullptr, std::move(callee_factory));
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001328 return caller_ && callee_;
1329 }
1330
1331 bool CreatePeerConnectionWrappersWithConfigAndDeps(
1332 const PeerConnectionInterface::RTCConfiguration& caller_config,
1333 webrtc::PeerConnectionDependencies caller_dependencies,
1334 const PeerConnectionInterface::RTCConfiguration& callee_config,
1335 webrtc::PeerConnectionDependencies callee_dependencies) {
1336 caller_ =
Niels Möllerf06f9232018-08-07 12:32:18 +02001337 CreatePeerConnectionWrapper("Caller", nullptr, &caller_config,
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001338 std::move(caller_dependencies), nullptr,
1339 /*media_transport_factory=*/nullptr);
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001340 callee_ =
Niels Möllerf06f9232018-08-07 12:32:18 +02001341 CreatePeerConnectionWrapper("Callee", nullptr, &callee_config,
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001342 std::move(callee_dependencies), nullptr,
1343 /*media_transport_factory=*/nullptr);
deadbeef1dcb1642017-03-29 21:08:16 -07001344 return caller_ && callee_;
1345 }
1346
1347 bool CreatePeerConnectionWrappersWithOptions(
1348 const PeerConnectionFactory::Options& caller_options,
1349 const PeerConnectionFactory::Options& callee_options) {
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001350 caller_ = CreatePeerConnectionWrapper(
Niels Möllerf06f9232018-08-07 12:32:18 +02001351 "Caller", &caller_options, nullptr,
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001352 webrtc::PeerConnectionDependencies(nullptr), nullptr,
1353 /*media_transport_factory=*/nullptr);
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001354 callee_ = CreatePeerConnectionWrapper(
Niels Möllerf06f9232018-08-07 12:32:18 +02001355 "Callee", &callee_options, nullptr,
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001356 webrtc::PeerConnectionDependencies(nullptr), nullptr,
1357 /*media_transport_factory=*/nullptr);
Qingsi Wang7685e862018-06-11 20:15:46 -07001358 return caller_ && callee_;
1359 }
1360
1361 bool CreatePeerConnectionWrappersWithFakeRtcEventLog() {
1362 PeerConnectionInterface::RTCConfiguration default_config;
1363 caller_ = CreatePeerConnectionWrapperWithFakeRtcEventLog(
Niels Möllerf06f9232018-08-07 12:32:18 +02001364 "Caller", nullptr, &default_config,
Qingsi Wang7685e862018-06-11 20:15:46 -07001365 webrtc::PeerConnectionDependencies(nullptr));
1366 callee_ = CreatePeerConnectionWrapperWithFakeRtcEventLog(
Niels Möllerf06f9232018-08-07 12:32:18 +02001367 "Callee", nullptr, &default_config,
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001368 webrtc::PeerConnectionDependencies(nullptr));
deadbeef1dcb1642017-03-29 21:08:16 -07001369 return caller_ && callee_;
1370 }
1371
Seth Hampson2f0d7022018-02-20 11:54:42 -08001372 std::unique_ptr<PeerConnectionWrapper>
1373 CreatePeerConnectionWrapperWithAlternateKey() {
deadbeef1dcb1642017-03-29 21:08:16 -07001374 std::unique_ptr<FakeRTCCertificateGenerator> cert_generator(
1375 new FakeRTCCertificateGenerator());
1376 cert_generator->use_alternate_key();
1377
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001378 webrtc::PeerConnectionDependencies dependencies(nullptr);
1379 dependencies.cert_generator = std::move(cert_generator);
Niels Möllerf06f9232018-08-07 12:32:18 +02001380 return CreatePeerConnectionWrapper("New Peer", nullptr, nullptr,
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001381 std::move(dependencies), nullptr,
1382 /*media_transport_factory=*/nullptr);
deadbeef1dcb1642017-03-29 21:08:16 -07001383 }
1384
Seth Hampsonaed71642018-06-11 07:41:32 -07001385 cricket::TestTurnServer* CreateTurnServer(
1386 rtc::SocketAddress internal_address,
1387 rtc::SocketAddress external_address,
1388 cricket::ProtocolType type = cricket::ProtocolType::PROTO_UDP,
1389 const std::string& common_name = "test turn server") {
1390 rtc::Thread* thread = network_thread();
1391 std::unique_ptr<cricket::TestTurnServer> turn_server =
1392 network_thread()->Invoke<std::unique_ptr<cricket::TestTurnServer>>(
1393 RTC_FROM_HERE,
1394 [thread, internal_address, external_address, type, common_name] {
Mirko Bonadei317a1f02019-09-17 17:06:18 +02001395 return std::make_unique<cricket::TestTurnServer>(
Seth Hampsonaed71642018-06-11 07:41:32 -07001396 thread, internal_address, external_address, type,
1397 /*ignore_bad_certs=*/true, common_name);
1398 });
1399 turn_servers_.push_back(std::move(turn_server));
1400 // Interactions with the turn server should be done on the network thread.
1401 return turn_servers_.back().get();
1402 }
1403
1404 cricket::TestTurnCustomizer* CreateTurnCustomizer() {
1405 std::unique_ptr<cricket::TestTurnCustomizer> turn_customizer =
1406 network_thread()->Invoke<std::unique_ptr<cricket::TestTurnCustomizer>>(
1407 RTC_FROM_HERE,
Mirko Bonadei317a1f02019-09-17 17:06:18 +02001408 [] { return std::make_unique<cricket::TestTurnCustomizer>(); });
Seth Hampsonaed71642018-06-11 07:41:32 -07001409 turn_customizers_.push_back(std::move(turn_customizer));
1410 // Interactions with the turn customizer should be done on the network
1411 // thread.
1412 return turn_customizers_.back().get();
1413 }
1414
1415 // Checks that the function counters for a TestTurnCustomizer are greater than
1416 // 0.
1417 void ExpectTurnCustomizerCountersIncremented(
1418 cricket::TestTurnCustomizer* turn_customizer) {
1419 unsigned int allow_channel_data_counter =
1420 network_thread()->Invoke<unsigned int>(
1421 RTC_FROM_HERE, [turn_customizer] {
1422 return turn_customizer->allow_channel_data_cnt_;
1423 });
1424 EXPECT_GT(allow_channel_data_counter, 0u);
1425 unsigned int modify_counter = network_thread()->Invoke<unsigned int>(
1426 RTC_FROM_HERE,
1427 [turn_customizer] { return turn_customizer->modify_cnt_; });
1428 EXPECT_GT(modify_counter, 0u);
1429 }
1430
deadbeef1dcb1642017-03-29 21:08:16 -07001431 // Once called, SDP blobs and ICE candidates will be automatically signaled
1432 // between PeerConnections.
1433 void ConnectFakeSignaling() {
1434 caller_->set_signaling_message_receiver(callee_.get());
1435 callee_->set_signaling_message_receiver(caller_.get());
1436 }
1437
Steve Antonede9ca52017-10-16 13:04:27 -07001438 // Once called, SDP blobs will be automatically signaled between
1439 // PeerConnections. Note that ICE candidates will not be signaled unless they
1440 // are in the exchanged SDP blobs.
1441 void ConnectFakeSignalingForSdpOnly() {
1442 ConnectFakeSignaling();
1443 SetSignalIceCandidates(false);
1444 }
1445
deadbeef1dcb1642017-03-29 21:08:16 -07001446 void SetSignalingDelayMs(int delay_ms) {
1447 caller_->set_signaling_delay_ms(delay_ms);
1448 callee_->set_signaling_delay_ms(delay_ms);
1449 }
1450
Steve Antonede9ca52017-10-16 13:04:27 -07001451 void SetSignalIceCandidates(bool signal) {
1452 caller_->set_signal_ice_candidates(signal);
1453 callee_->set_signal_ice_candidates(signal);
1454 }
1455
deadbeef1dcb1642017-03-29 21:08:16 -07001456 // Messages may get lost on the unreliable DataChannel, so we send multiple
1457 // times to avoid test flakiness.
1458 void SendRtpDataWithRetries(webrtc::DataChannelInterface* dc,
1459 const std::string& data,
1460 int retries) {
1461 for (int i = 0; i < retries; ++i) {
1462 dc->Send(DataBuffer(data));
1463 }
1464 }
1465
1466 rtc::Thread* network_thread() { return network_thread_.get(); }
1467
1468 rtc::VirtualSocketServer* virtual_socket_server() { return ss_.get(); }
1469
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001470 webrtc::MediaTransportPair* loopback_media_transports() {
1471 return &loopback_media_transports_;
1472 }
1473
deadbeef1dcb1642017-03-29 21:08:16 -07001474 PeerConnectionWrapper* caller() { return caller_.get(); }
1475
1476 // Set the |caller_| to the |wrapper| passed in and return the
1477 // original |caller_|.
1478 PeerConnectionWrapper* SetCallerPcWrapperAndReturnCurrent(
1479 PeerConnectionWrapper* wrapper) {
1480 PeerConnectionWrapper* old = caller_.release();
1481 caller_.reset(wrapper);
1482 return old;
1483 }
1484
1485 PeerConnectionWrapper* callee() { return callee_.get(); }
1486
1487 // Set the |callee_| to the |wrapper| passed in and return the
1488 // original |callee_|.
1489 PeerConnectionWrapper* SetCalleePcWrapperAndReturnCurrent(
1490 PeerConnectionWrapper* wrapper) {
1491 PeerConnectionWrapper* old = callee_.release();
1492 callee_.reset(wrapper);
1493 return old;
1494 }
1495
Qingsi Wang1dac6d82018-12-12 15:28:47 -08001496 void SetPortAllocatorFlags(uint32_t caller_flags, uint32_t callee_flags) {
1497 network_thread()->Invoke<void>(
1498 RTC_FROM_HERE, rtc::Bind(&cricket::PortAllocator::set_flags,
1499 caller()->port_allocator(), caller_flags));
1500 network_thread()->Invoke<void>(
1501 RTC_FROM_HERE, rtc::Bind(&cricket::PortAllocator::set_flags,
1502 callee()->port_allocator(), callee_flags));
1503 }
1504
Steve Antonede9ca52017-10-16 13:04:27 -07001505 rtc::FirewallSocketServer* firewall() const { return fss_.get(); }
1506
Seth Hampson2f0d7022018-02-20 11:54:42 -08001507 // Expects the provided number of new frames to be received within
1508 // kMaxWaitForFramesMs. The new expected frames are specified in
1509 // |media_expectations|. Returns false if any of the expectations were
1510 // not met.
1511 bool ExpectNewFrames(const MediaExpectations& media_expectations) {
1512 // First initialize the expected frame counts based upon the current
1513 // frame count.
1514 int total_caller_audio_frames_expected = caller()->audio_frames_received();
1515 if (media_expectations.caller_audio_expectation_ ==
1516 MediaExpectations::kExpectSomeFrames) {
1517 total_caller_audio_frames_expected +=
1518 media_expectations.caller_audio_frames_expected_;
1519 }
1520 int total_caller_video_frames_expected =
deadbeef1dcb1642017-03-29 21:08:16 -07001521 caller()->min_video_frames_received_per_track();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001522 if (media_expectations.caller_video_expectation_ ==
1523 MediaExpectations::kExpectSomeFrames) {
1524 total_caller_video_frames_expected +=
1525 media_expectations.caller_video_frames_expected_;
1526 }
1527 int total_callee_audio_frames_expected = callee()->audio_frames_received();
1528 if (media_expectations.callee_audio_expectation_ ==
1529 MediaExpectations::kExpectSomeFrames) {
1530 total_callee_audio_frames_expected +=
1531 media_expectations.callee_audio_frames_expected_;
1532 }
1533 int total_callee_video_frames_expected =
deadbeef1dcb1642017-03-29 21:08:16 -07001534 callee()->min_video_frames_received_per_track();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001535 if (media_expectations.callee_video_expectation_ ==
1536 MediaExpectations::kExpectSomeFrames) {
1537 total_callee_video_frames_expected +=
1538 media_expectations.callee_video_frames_expected_;
1539 }
deadbeef1dcb1642017-03-29 21:08:16 -07001540
Seth Hampson2f0d7022018-02-20 11:54:42 -08001541 // Wait for the expected frames.
deadbeef1dcb1642017-03-29 21:08:16 -07001542 EXPECT_TRUE_WAIT(caller()->audio_frames_received() >=
Seth Hampson2f0d7022018-02-20 11:54:42 -08001543 total_caller_audio_frames_expected &&
deadbeef1dcb1642017-03-29 21:08:16 -07001544 caller()->min_video_frames_received_per_track() >=
Seth Hampson2f0d7022018-02-20 11:54:42 -08001545 total_caller_video_frames_expected &&
deadbeef1dcb1642017-03-29 21:08:16 -07001546 callee()->audio_frames_received() >=
Seth Hampson2f0d7022018-02-20 11:54:42 -08001547 total_callee_audio_frames_expected &&
deadbeef1dcb1642017-03-29 21:08:16 -07001548 callee()->min_video_frames_received_per_track() >=
Seth Hampson2f0d7022018-02-20 11:54:42 -08001549 total_callee_video_frames_expected,
1550 kMaxWaitForFramesMs);
1551 bool expectations_correct =
1552 caller()->audio_frames_received() >=
1553 total_caller_audio_frames_expected &&
1554 caller()->min_video_frames_received_per_track() >=
1555 total_caller_video_frames_expected &&
1556 callee()->audio_frames_received() >=
1557 total_callee_audio_frames_expected &&
1558 callee()->min_video_frames_received_per_track() >=
1559 total_callee_video_frames_expected;
deadbeef1dcb1642017-03-29 21:08:16 -07001560
Seth Hampson2f0d7022018-02-20 11:54:42 -08001561 // After the combined wait, print out a more detailed message upon
1562 // failure.
deadbeef1dcb1642017-03-29 21:08:16 -07001563 EXPECT_GE(caller()->audio_frames_received(),
Seth Hampson2f0d7022018-02-20 11:54:42 -08001564 total_caller_audio_frames_expected);
deadbeef1dcb1642017-03-29 21:08:16 -07001565 EXPECT_GE(caller()->min_video_frames_received_per_track(),
Seth Hampson2f0d7022018-02-20 11:54:42 -08001566 total_caller_video_frames_expected);
deadbeef1dcb1642017-03-29 21:08:16 -07001567 EXPECT_GE(callee()->audio_frames_received(),
Seth Hampson2f0d7022018-02-20 11:54:42 -08001568 total_callee_audio_frames_expected);
deadbeef1dcb1642017-03-29 21:08:16 -07001569 EXPECT_GE(callee()->min_video_frames_received_per_track(),
Seth Hampson2f0d7022018-02-20 11:54:42 -08001570 total_callee_video_frames_expected);
1571
1572 // We want to make sure nothing unexpected was received.
1573 if (media_expectations.caller_audio_expectation_ ==
1574 MediaExpectations::kExpectNoFrames) {
1575 EXPECT_EQ(caller()->audio_frames_received(),
1576 total_caller_audio_frames_expected);
1577 if (caller()->audio_frames_received() !=
1578 total_caller_audio_frames_expected) {
1579 expectations_correct = false;
1580 }
1581 }
1582 if (media_expectations.caller_video_expectation_ ==
1583 MediaExpectations::kExpectNoFrames) {
1584 EXPECT_EQ(caller()->min_video_frames_received_per_track(),
1585 total_caller_video_frames_expected);
1586 if (caller()->min_video_frames_received_per_track() !=
1587 total_caller_video_frames_expected) {
1588 expectations_correct = false;
1589 }
1590 }
1591 if (media_expectations.callee_audio_expectation_ ==
1592 MediaExpectations::kExpectNoFrames) {
1593 EXPECT_EQ(callee()->audio_frames_received(),
1594 total_callee_audio_frames_expected);
1595 if (callee()->audio_frames_received() !=
1596 total_callee_audio_frames_expected) {
1597 expectations_correct = false;
1598 }
1599 }
1600 if (media_expectations.callee_video_expectation_ ==
1601 MediaExpectations::kExpectNoFrames) {
1602 EXPECT_EQ(callee()->min_video_frames_received_per_track(),
1603 total_callee_video_frames_expected);
1604 if (callee()->min_video_frames_received_per_track() !=
1605 total_callee_video_frames_expected) {
1606 expectations_correct = false;
1607 }
1608 }
1609 return expectations_correct;
deadbeef1dcb1642017-03-29 21:08:16 -07001610 }
1611
Steve Antond91969e2019-05-30 12:27:03 -07001612 void ClosePeerConnections() {
1613 caller()->pc()->Close();
1614 callee()->pc()->Close();
1615 }
1616
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07001617 void TestNegotiatedCipherSuite(
1618 const PeerConnectionFactory::Options& caller_options,
1619 const PeerConnectionFactory::Options& callee_options,
1620 int expected_cipher_suite) {
deadbeef1dcb1642017-03-29 21:08:16 -07001621 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(caller_options,
1622 callee_options));
deadbeef1dcb1642017-03-29 21:08:16 -07001623 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001624 caller()->AddAudioVideoTracks();
1625 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001626 caller()->CreateAndSetAndSignalOffer();
Qingsi Wang7fc821d2018-07-12 12:54:53 -07001627 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07001628 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(expected_cipher_suite),
deadbeefd8ad7882017-04-18 16:01:17 -07001629 caller()->OldGetStats()->SrtpCipher(), kDefaultTimeout);
Qingsi Wang7fc821d2018-07-12 12:54:53 -07001630 // TODO(bugs.webrtc.org/9456): Fix it.
Alex Loiko9289eda2018-11-23 16:18:59 +00001631 EXPECT_EQ(1, webrtc::metrics::NumEvents(
Qingsi Wang7fc821d2018-07-12 12:54:53 -07001632 "WebRTC.PeerConnection.SrtpCryptoSuite.Audio",
1633 expected_cipher_suite));
deadbeef1dcb1642017-03-29 21:08:16 -07001634 }
1635
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07001636 void TestGcmNegotiationUsesCipherSuite(bool local_gcm_enabled,
1637 bool remote_gcm_enabled,
1638 int expected_cipher_suite) {
1639 PeerConnectionFactory::Options caller_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07001640 caller_options.crypto_options.srtp.enable_gcm_crypto_suites =
1641 local_gcm_enabled;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07001642 PeerConnectionFactory::Options callee_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07001643 callee_options.crypto_options.srtp.enable_gcm_crypto_suites =
1644 remote_gcm_enabled;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07001645 TestNegotiatedCipherSuite(caller_options, callee_options,
1646 expected_cipher_suite);
1647 }
1648
Seth Hampson2f0d7022018-02-20 11:54:42 -08001649 protected:
Steve Anton3acffc32018-04-12 17:21:03 -07001650 SdpSemantics sdp_semantics_;
Seth Hampson2f0d7022018-02-20 11:54:42 -08001651
deadbeef1dcb1642017-03-29 21:08:16 -07001652 private:
1653 // |ss_| is used by |network_thread_| so it must be destroyed later.
deadbeef1dcb1642017-03-29 21:08:16 -07001654 std::unique_ptr<rtc::VirtualSocketServer> ss_;
Steve Antonede9ca52017-10-16 13:04:27 -07001655 std::unique_ptr<rtc::FirewallSocketServer> fss_;
deadbeef1dcb1642017-03-29 21:08:16 -07001656 // |network_thread_| and |worker_thread_| are used by both
1657 // |caller_| and |callee_| so they must be destroyed
1658 // later.
1659 std::unique_ptr<rtc::Thread> network_thread_;
1660 std::unique_ptr<rtc::Thread> worker_thread_;
Seth Hampsonaed71642018-06-11 07:41:32 -07001661 // The turn servers and turn customizers should be accessed & deleted on the
1662 // network thread to avoid a race with the socket read/write that occurs
1663 // on the network thread.
1664 std::vector<std::unique_ptr<cricket::TestTurnServer>> turn_servers_;
1665 std::vector<std::unique_ptr<cricket::TestTurnCustomizer>> turn_customizers_;
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001666 webrtc::MediaTransportPair loopback_media_transports_;
deadbeef1dcb1642017-03-29 21:08:16 -07001667 std::unique_ptr<PeerConnectionWrapper> caller_;
1668 std::unique_ptr<PeerConnectionWrapper> callee_;
1669};
1670
Seth Hampson2f0d7022018-02-20 11:54:42 -08001671class PeerConnectionIntegrationTest
1672 : public PeerConnectionIntegrationBaseTest,
1673 public ::testing::WithParamInterface<SdpSemantics> {
1674 protected:
1675 PeerConnectionIntegrationTest()
1676 : PeerConnectionIntegrationBaseTest(GetParam()) {}
1677};
1678
1679class PeerConnectionIntegrationTestPlanB
1680 : public PeerConnectionIntegrationBaseTest {
1681 protected:
1682 PeerConnectionIntegrationTestPlanB()
1683 : PeerConnectionIntegrationBaseTest(SdpSemantics::kPlanB) {}
1684};
1685
1686class PeerConnectionIntegrationTestUnifiedPlan
1687 : public PeerConnectionIntegrationBaseTest {
1688 protected:
1689 PeerConnectionIntegrationTestUnifiedPlan()
1690 : PeerConnectionIntegrationBaseTest(SdpSemantics::kUnifiedPlan) {}
1691};
1692
deadbeef1dcb1642017-03-29 21:08:16 -07001693// Test the OnFirstPacketReceived callback from audio/video RtpReceivers. This
1694// includes testing that the callback is invoked if an observer is connected
1695// after the first packet has already been received.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001696TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07001697 RtpReceiverObserverOnFirstPacketReceived) {
1698 ASSERT_TRUE(CreatePeerConnectionWrappers());
1699 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001700 caller()->AddAudioVideoTracks();
1701 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001702 // Start offer/answer exchange and wait for it to complete.
1703 caller()->CreateAndSetAndSignalOffer();
1704 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1705 // Should be one receiver each for audio/video.
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02001706 EXPECT_EQ(2U, caller()->rtp_receiver_observers().size());
1707 EXPECT_EQ(2U, callee()->rtp_receiver_observers().size());
deadbeef1dcb1642017-03-29 21:08:16 -07001708 // Wait for all "first packet received" callbacks to be fired.
1709 EXPECT_TRUE_WAIT(
Steve Anton64b626b2019-01-28 17:25:26 -08001710 absl::c_all_of(caller()->rtp_receiver_observers(),
1711 [](const std::unique_ptr<MockRtpReceiverObserver>& o) {
1712 return o->first_packet_received();
1713 }),
deadbeef1dcb1642017-03-29 21:08:16 -07001714 kMaxWaitForFramesMs);
1715 EXPECT_TRUE_WAIT(
Steve Anton64b626b2019-01-28 17:25:26 -08001716 absl::c_all_of(callee()->rtp_receiver_observers(),
1717 [](const std::unique_ptr<MockRtpReceiverObserver>& o) {
1718 return o->first_packet_received();
1719 }),
deadbeef1dcb1642017-03-29 21:08:16 -07001720 kMaxWaitForFramesMs);
1721 // If new observers are set after the first packet was already received, the
1722 // callback should still be invoked.
1723 caller()->ResetRtpReceiverObservers();
1724 callee()->ResetRtpReceiverObservers();
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02001725 EXPECT_EQ(2U, caller()->rtp_receiver_observers().size());
1726 EXPECT_EQ(2U, callee()->rtp_receiver_observers().size());
deadbeef1dcb1642017-03-29 21:08:16 -07001727 EXPECT_TRUE(
Steve Anton64b626b2019-01-28 17:25:26 -08001728 absl::c_all_of(caller()->rtp_receiver_observers(),
1729 [](const std::unique_ptr<MockRtpReceiverObserver>& o) {
1730 return o->first_packet_received();
1731 }));
deadbeef1dcb1642017-03-29 21:08:16 -07001732 EXPECT_TRUE(
Steve Anton64b626b2019-01-28 17:25:26 -08001733 absl::c_all_of(callee()->rtp_receiver_observers(),
1734 [](const std::unique_ptr<MockRtpReceiverObserver>& o) {
1735 return o->first_packet_received();
1736 }));
deadbeef1dcb1642017-03-29 21:08:16 -07001737}
1738
1739class DummyDtmfObserver : public DtmfSenderObserverInterface {
1740 public:
1741 DummyDtmfObserver() : completed_(false) {}
1742
1743 // Implements DtmfSenderObserverInterface.
1744 void OnToneChange(const std::string& tone) override {
1745 tones_.push_back(tone);
1746 if (tone.empty()) {
1747 completed_ = true;
1748 }
1749 }
1750
1751 const std::vector<std::string>& tones() const { return tones_; }
1752 bool completed() const { return completed_; }
1753
1754 private:
1755 bool completed_;
1756 std::vector<std::string> tones_;
1757};
1758
1759// Assumes |sender| already has an audio track added and the offer/answer
1760// exchange is done.
1761void TestDtmfFromSenderToReceiver(PeerConnectionWrapper* sender,
1762 PeerConnectionWrapper* receiver) {
Steve Anton15324772018-01-16 10:26:49 -08001763 // We should be able to get a DTMF sender from the local sender.
1764 rtc::scoped_refptr<DtmfSenderInterface> dtmf_sender =
1765 sender->pc()->GetSenders().at(0)->GetDtmfSender();
1766 ASSERT_TRUE(dtmf_sender);
deadbeef1dcb1642017-03-29 21:08:16 -07001767 DummyDtmfObserver observer;
deadbeef1dcb1642017-03-29 21:08:16 -07001768 dtmf_sender->RegisterObserver(&observer);
1769
1770 // Test the DtmfSender object just created.
1771 EXPECT_TRUE(dtmf_sender->CanInsertDtmf());
1772 EXPECT_TRUE(dtmf_sender->InsertDtmf("1a", 100, 50));
1773
1774 EXPECT_TRUE_WAIT(observer.completed(), kDefaultTimeout);
1775 std::vector<std::string> tones = {"1", "a", ""};
1776 EXPECT_EQ(tones, observer.tones());
1777 dtmf_sender->UnregisterObserver();
1778 // TODO(deadbeef): Verify the tones were actually received end-to-end.
1779}
1780
1781// Verifies the DtmfSenderObserver callbacks for a DtmfSender (one in each
1782// direction).
Seth Hampson2f0d7022018-02-20 11:54:42 -08001783TEST_P(PeerConnectionIntegrationTest, DtmfSenderObserver) {
deadbeef1dcb1642017-03-29 21:08:16 -07001784 ASSERT_TRUE(CreatePeerConnectionWrappers());
1785 ConnectFakeSignaling();
1786 // Only need audio for DTMF.
Steve Anton15324772018-01-16 10:26:49 -08001787 caller()->AddAudioTrack();
1788 callee()->AddAudioTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07001789 caller()->CreateAndSetAndSignalOffer();
1790 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
deadbeef71452802017-05-07 17:21:01 -07001791 // DTLS must finish before the DTMF sender can be used reliably.
1792 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07001793 TestDtmfFromSenderToReceiver(caller(), callee());
1794 TestDtmfFromSenderToReceiver(callee(), caller());
1795}
1796
1797// Basic end-to-end test, verifying media can be encoded/transmitted/decoded
1798// between two connections, using DTLS-SRTP.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001799TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithDtls) {
deadbeef1dcb1642017-03-29 21:08:16 -07001800 ASSERT_TRUE(CreatePeerConnectionWrappers());
1801 ConnectFakeSignaling();
Harald Alvestrand194939b2018-01-24 16:04:13 +01001802
deadbeef1dcb1642017-03-29 21:08:16 -07001803 // Do normal offer/answer and wait for some frames to be received in each
1804 // direction.
Steve Anton15324772018-01-16 10:26:49 -08001805 caller()->AddAudioVideoTracks();
1806 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001807 caller()->CreateAndSetAndSignalOffer();
1808 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001809 MediaExpectations media_expectations;
1810 media_expectations.ExpectBidirectionalAudioAndVideo();
1811 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Qingsi Wang7fc821d2018-07-12 12:54:53 -07001812 EXPECT_LE(2, webrtc::metrics::NumEvents("WebRTC.PeerConnection.KeyProtocol",
1813 webrtc::kEnumCounterKeyProtocolDtls));
1814 EXPECT_EQ(0, webrtc::metrics::NumEvents("WebRTC.PeerConnection.KeyProtocol",
1815 webrtc::kEnumCounterKeyProtocolSdes));
deadbeef1dcb1642017-03-29 21:08:16 -07001816}
1817
1818// Uses SDES instead of DTLS for key agreement.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001819TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithSdes) {
deadbeef1dcb1642017-03-29 21:08:16 -07001820 PeerConnectionInterface::RTCConfiguration sdes_config;
1821 sdes_config.enable_dtls_srtp.emplace(false);
1822 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(sdes_config, sdes_config));
1823 ConnectFakeSignaling();
1824
1825 // Do normal offer/answer and wait for some frames to be received in each
1826 // direction.
Steve Anton15324772018-01-16 10:26:49 -08001827 caller()->AddAudioVideoTracks();
1828 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001829 caller()->CreateAndSetAndSignalOffer();
1830 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001831 MediaExpectations media_expectations;
1832 media_expectations.ExpectBidirectionalAudioAndVideo();
1833 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Qingsi Wang7fc821d2018-07-12 12:54:53 -07001834 EXPECT_LE(2, webrtc::metrics::NumEvents("WebRTC.PeerConnection.KeyProtocol",
1835 webrtc::kEnumCounterKeyProtocolSdes));
1836 EXPECT_EQ(0, webrtc::metrics::NumEvents("WebRTC.PeerConnection.KeyProtocol",
1837 webrtc::kEnumCounterKeyProtocolDtls));
deadbeef1dcb1642017-03-29 21:08:16 -07001838}
1839
Steve Anton9a44b2d2019-07-12 12:58:30 -07001840// Basic end-to-end test specifying the |enable_encrypted_rtp_header_extensions|
1841// option to offer encrypted versions of all header extensions alongside the
1842// unencrypted versions.
1843TEST_P(PeerConnectionIntegrationTest,
1844 EndToEndCallWithEncryptedRtpHeaderExtensions) {
1845 CryptoOptions crypto_options;
1846 crypto_options.srtp.enable_encrypted_rtp_header_extensions = true;
1847 PeerConnectionInterface::RTCConfiguration config;
1848 config.crypto_options = crypto_options;
1849 // Note: This allows offering >14 RTP header extensions.
1850 config.offer_extmap_allow_mixed = true;
1851 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
1852 ConnectFakeSignaling();
1853
1854 // Do normal offer/answer and wait for some frames to be received in each
1855 // direction.
1856 caller()->AddAudioVideoTracks();
1857 callee()->AddAudioVideoTracks();
1858 caller()->CreateAndSetAndSignalOffer();
1859 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1860 MediaExpectations media_expectations;
1861 media_expectations.ExpectBidirectionalAudioAndVideo();
1862 ASSERT_TRUE(ExpectNewFrames(media_expectations));
1863}
1864
Steve Anton8c0f7a72017-10-03 10:03:10 -07001865// Tests that the GetRemoteAudioSSLCertificate method returns the remote DTLS
1866// certificate once the DTLS handshake has finished.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001867TEST_P(PeerConnectionIntegrationTest,
Steve Anton8c0f7a72017-10-03 10:03:10 -07001868 GetRemoteAudioSSLCertificateReturnsExchangedCertificate) {
1869 auto GetRemoteAudioSSLCertificate = [](PeerConnectionWrapper* wrapper) {
1870 auto pci = reinterpret_cast<PeerConnectionProxy*>(wrapper->pc());
1871 auto pc = reinterpret_cast<PeerConnection*>(pci->internal());
1872 return pc->GetRemoteAudioSSLCertificate();
1873 };
Zhi Huang70b820f2018-01-27 14:16:15 -08001874 auto GetRemoteAudioSSLCertChain = [](PeerConnectionWrapper* wrapper) {
1875 auto pci = reinterpret_cast<PeerConnectionProxy*>(wrapper->pc());
1876 auto pc = reinterpret_cast<PeerConnection*>(pci->internal());
1877 return pc->GetRemoteAudioSSLCertChain();
1878 };
Steve Anton8c0f7a72017-10-03 10:03:10 -07001879
1880 auto caller_cert = rtc::RTCCertificate::FromPEM(kRsaPems[0]);
1881 auto callee_cert = rtc::RTCCertificate::FromPEM(kRsaPems[1]);
1882
1883 // Configure each side with a known certificate so they can be compared later.
1884 PeerConnectionInterface::RTCConfiguration caller_config;
1885 caller_config.enable_dtls_srtp.emplace(true);
1886 caller_config.certificates.push_back(caller_cert);
1887 PeerConnectionInterface::RTCConfiguration callee_config;
1888 callee_config.enable_dtls_srtp.emplace(true);
1889 callee_config.certificates.push_back(callee_cert);
1890 ASSERT_TRUE(
1891 CreatePeerConnectionWrappersWithConfig(caller_config, callee_config));
1892 ConnectFakeSignaling();
1893
1894 // When first initialized, there should not be a remote SSL certificate (and
1895 // calling this method should not crash).
1896 EXPECT_EQ(nullptr, GetRemoteAudioSSLCertificate(caller()));
1897 EXPECT_EQ(nullptr, GetRemoteAudioSSLCertificate(callee()));
Zhi Huang70b820f2018-01-27 14:16:15 -08001898 EXPECT_EQ(nullptr, GetRemoteAudioSSLCertChain(caller()));
1899 EXPECT_EQ(nullptr, GetRemoteAudioSSLCertChain(callee()));
Steve Anton8c0f7a72017-10-03 10:03:10 -07001900
Steve Anton15324772018-01-16 10:26:49 -08001901 caller()->AddAudioTrack();
1902 callee()->AddAudioTrack();
Steve Anton8c0f7a72017-10-03 10:03:10 -07001903 caller()->CreateAndSetAndSignalOffer();
1904 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1905 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
1906
1907 // Once DTLS has been connected, each side should return the other's SSL
1908 // certificate when calling GetRemoteAudioSSLCertificate.
1909
1910 auto caller_remote_cert = GetRemoteAudioSSLCertificate(caller());
1911 ASSERT_TRUE(caller_remote_cert);
Benjamin Wright6c6c9df2018-10-25 01:16:26 -07001912 EXPECT_EQ(callee_cert->GetSSLCertificate().ToPEMString(),
Steve Anton8c0f7a72017-10-03 10:03:10 -07001913 caller_remote_cert->ToPEMString());
1914
1915 auto callee_remote_cert = GetRemoteAudioSSLCertificate(callee());
1916 ASSERT_TRUE(callee_remote_cert);
Benjamin Wright6c6c9df2018-10-25 01:16:26 -07001917 EXPECT_EQ(caller_cert->GetSSLCertificate().ToPEMString(),
Steve Anton8c0f7a72017-10-03 10:03:10 -07001918 callee_remote_cert->ToPEMString());
Zhi Huang70b820f2018-01-27 14:16:15 -08001919
1920 auto caller_remote_cert_chain = GetRemoteAudioSSLCertChain(caller());
1921 ASSERT_TRUE(caller_remote_cert_chain);
1922 ASSERT_EQ(1U, caller_remote_cert_chain->GetSize());
1923 auto remote_cert = &caller_remote_cert_chain->Get(0);
Benjamin Wright6c6c9df2018-10-25 01:16:26 -07001924 EXPECT_EQ(callee_cert->GetSSLCertificate().ToPEMString(),
Zhi Huang70b820f2018-01-27 14:16:15 -08001925 remote_cert->ToPEMString());
1926
1927 auto callee_remote_cert_chain = GetRemoteAudioSSLCertChain(callee());
1928 ASSERT_TRUE(callee_remote_cert_chain);
1929 ASSERT_EQ(1U, callee_remote_cert_chain->GetSize());
1930 remote_cert = &callee_remote_cert_chain->Get(0);
Benjamin Wright6c6c9df2018-10-25 01:16:26 -07001931 EXPECT_EQ(caller_cert->GetSSLCertificate().ToPEMString(),
Zhi Huang70b820f2018-01-27 14:16:15 -08001932 remote_cert->ToPEMString());
Steve Anton8c0f7a72017-10-03 10:03:10 -07001933}
1934
deadbeef1dcb1642017-03-29 21:08:16 -07001935// This test sets up a call between two parties with a source resolution of
1936// 1280x720 and verifies that a 16:9 aspect ratio is received.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001937TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07001938 Send1280By720ResolutionAndReceive16To9AspectRatio) {
1939 ASSERT_TRUE(CreatePeerConnectionWrappers());
1940 ConnectFakeSignaling();
1941
Niels Möller5c7efe72018-05-11 10:34:46 +02001942 // Add video tracks with 16:9 aspect ratio, size 1280 x 720.
1943 webrtc::FakePeriodicVideoSource::Config config;
1944 config.width = 1280;
1945 config.height = 720;
Johannes Kron965e7942018-09-13 15:36:20 +02001946 config.timestamp_offset_ms = rtc::TimeMillis();
Niels Möller5c7efe72018-05-11 10:34:46 +02001947 caller()->AddTrack(caller()->CreateLocalVideoTrackWithConfig(config));
1948 callee()->AddTrack(callee()->CreateLocalVideoTrackWithConfig(config));
deadbeef1dcb1642017-03-29 21:08:16 -07001949
1950 // Do normal offer/answer and wait for at least one frame to be received in
1951 // each direction.
1952 caller()->CreateAndSetAndSignalOffer();
1953 ASSERT_TRUE_WAIT(caller()->min_video_frames_received_per_track() > 0 &&
1954 callee()->min_video_frames_received_per_track() > 0,
1955 kMaxWaitForFramesMs);
1956
1957 // Check rendered aspect ratio.
1958 EXPECT_EQ(16.0 / 9, caller()->local_rendered_aspect_ratio());
1959 EXPECT_EQ(16.0 / 9, caller()->rendered_aspect_ratio());
1960 EXPECT_EQ(16.0 / 9, callee()->local_rendered_aspect_ratio());
1961 EXPECT_EQ(16.0 / 9, callee()->rendered_aspect_ratio());
1962}
1963
1964// This test sets up an one-way call, with media only from caller to
1965// callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001966TEST_P(PeerConnectionIntegrationTest, OneWayMediaCall) {
deadbeef1dcb1642017-03-29 21:08:16 -07001967 ASSERT_TRUE(CreatePeerConnectionWrappers());
1968 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001969 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001970 caller()->CreateAndSetAndSignalOffer();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001971 MediaExpectations media_expectations;
1972 media_expectations.CalleeExpectsSomeAudioAndVideo();
1973 media_expectations.CallerExpectsNoAudio();
1974 media_expectations.CallerExpectsNoVideo();
1975 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07001976}
1977
1978// This test sets up a audio call initially, with the callee rejecting video
1979// initially. Then later the callee decides to upgrade to audio/video, and
1980// initiates a new offer/answer exchange.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001981TEST_P(PeerConnectionIntegrationTest, AudioToVideoUpgrade) {
deadbeef1dcb1642017-03-29 21:08:16 -07001982 ASSERT_TRUE(CreatePeerConnectionWrappers());
1983 ConnectFakeSignaling();
1984 // Initially, offer an audio/video stream from the caller, but refuse to
1985 // send/receive video on the callee side.
Steve Anton15324772018-01-16 10:26:49 -08001986 caller()->AddAudioVideoTracks();
1987 callee()->AddAudioTrack();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001988 if (sdp_semantics_ == SdpSemantics::kPlanB) {
1989 PeerConnectionInterface::RTCOfferAnswerOptions options;
1990 options.offer_to_receive_video = 0;
1991 callee()->SetOfferAnswerOptions(options);
1992 } else {
1993 callee()->SetRemoteOfferHandler([this] {
1994 callee()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)->Stop();
1995 });
1996 }
deadbeef1dcb1642017-03-29 21:08:16 -07001997 // Do offer/answer and make sure audio is still received end-to-end.
1998 caller()->CreateAndSetAndSignalOffer();
1999 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002000 {
2001 MediaExpectations media_expectations;
2002 media_expectations.ExpectBidirectionalAudio();
2003 media_expectations.ExpectNoVideo();
2004 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2005 }
deadbeef1dcb1642017-03-29 21:08:16 -07002006 // Sanity check that the callee's description has a rejected video section.
2007 ASSERT_NE(nullptr, callee()->pc()->local_description());
2008 const ContentInfo* callee_video_content =
2009 GetFirstVideoContent(callee()->pc()->local_description()->description());
2010 ASSERT_NE(nullptr, callee_video_content);
2011 EXPECT_TRUE(callee_video_content->rejected);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002012
deadbeef1dcb1642017-03-29 21:08:16 -07002013 // Now negotiate with video and ensure negotiation succeeds, with video
2014 // frames and additional audio frames being received.
Steve Anton15324772018-01-16 10:26:49 -08002015 callee()->AddVideoTrack();
Seth Hampson2f0d7022018-02-20 11:54:42 -08002016 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2017 PeerConnectionInterface::RTCOfferAnswerOptions options;
2018 options.offer_to_receive_video = 1;
2019 callee()->SetOfferAnswerOptions(options);
2020 } else {
2021 callee()->SetRemoteOfferHandler(nullptr);
2022 caller()->SetRemoteOfferHandler([this] {
2023 // The caller creates a new transceiver to receive video on when receiving
2024 // the offer, but by default it is send only.
2025 auto transceivers = caller()->pc()->GetTransceivers();
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02002026 ASSERT_EQ(3U, transceivers.size());
Seth Hampson2f0d7022018-02-20 11:54:42 -08002027 ASSERT_EQ(cricket::MEDIA_TYPE_VIDEO,
2028 transceivers[2]->receiver()->media_type());
2029 transceivers[2]->sender()->SetTrack(caller()->CreateLocalVideoTrack());
2030 transceivers[2]->SetDirection(RtpTransceiverDirection::kSendRecv);
2031 });
2032 }
deadbeef1dcb1642017-03-29 21:08:16 -07002033 callee()->CreateAndSetAndSignalOffer();
2034 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002035 {
2036 // Expect additional audio frames to be received after the upgrade.
2037 MediaExpectations media_expectations;
2038 media_expectations.ExpectBidirectionalAudioAndVideo();
2039 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2040 }
deadbeef1dcb1642017-03-29 21:08:16 -07002041}
2042
deadbeef4389b4d2017-09-07 09:07:36 -07002043// Simpler than the above test; just add an audio track to an established
2044// video-only connection.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002045TEST_P(PeerConnectionIntegrationTest, AddAudioToVideoOnlyCall) {
deadbeef4389b4d2017-09-07 09:07:36 -07002046 ASSERT_TRUE(CreatePeerConnectionWrappers());
2047 ConnectFakeSignaling();
2048 // Do initial offer/answer with just a video track.
Steve Anton15324772018-01-16 10:26:49 -08002049 caller()->AddVideoTrack();
2050 callee()->AddVideoTrack();
deadbeef4389b4d2017-09-07 09:07:36 -07002051 caller()->CreateAndSetAndSignalOffer();
2052 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2053 // Now add an audio track and do another offer/answer.
Steve Anton15324772018-01-16 10:26:49 -08002054 caller()->AddAudioTrack();
2055 callee()->AddAudioTrack();
deadbeef4389b4d2017-09-07 09:07:36 -07002056 caller()->CreateAndSetAndSignalOffer();
2057 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2058 // Ensure both audio and video frames are received end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002059 MediaExpectations media_expectations;
2060 media_expectations.ExpectBidirectionalAudioAndVideo();
2061 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef4389b4d2017-09-07 09:07:36 -07002062}
2063
deadbeef1dcb1642017-03-29 21:08:16 -07002064// This test sets up a call that's transferred to a new caller with a different
2065// DTLS fingerprint.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002066TEST_P(PeerConnectionIntegrationTest, CallTransferredForCallee) {
deadbeef1dcb1642017-03-29 21:08:16 -07002067 ASSERT_TRUE(CreatePeerConnectionWrappers());
2068 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002069 caller()->AddAudioVideoTracks();
2070 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002071 caller()->CreateAndSetAndSignalOffer();
2072 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2073
2074 // Keep the original peer around which will still send packets to the
2075 // receiving client. These SRTP packets will be dropped.
2076 std::unique_ptr<PeerConnectionWrapper> original_peer(
2077 SetCallerPcWrapperAndReturnCurrent(
Seth Hampson2f0d7022018-02-20 11:54:42 -08002078 CreatePeerConnectionWrapperWithAlternateKey().release()));
deadbeef1dcb1642017-03-29 21:08:16 -07002079 // TODO(deadbeef): Why do we call Close here? That goes against the comment
2080 // directly above.
2081 original_peer->pc()->Close();
2082
2083 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002084 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002085 caller()->CreateAndSetAndSignalOffer();
2086 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2087 // Wait for some additional frames to be transmitted end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002088 MediaExpectations media_expectations;
2089 media_expectations.ExpectBidirectionalAudioAndVideo();
2090 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002091}
2092
2093// This test sets up a call that's transferred to a new callee with a different
2094// DTLS fingerprint.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002095TEST_P(PeerConnectionIntegrationTest, CallTransferredForCaller) {
deadbeef1dcb1642017-03-29 21:08:16 -07002096 ASSERT_TRUE(CreatePeerConnectionWrappers());
2097 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002098 caller()->AddAudioVideoTracks();
2099 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002100 caller()->CreateAndSetAndSignalOffer();
2101 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2102
2103 // Keep the original peer around which will still send packets to the
2104 // receiving client. These SRTP packets will be dropped.
2105 std::unique_ptr<PeerConnectionWrapper> original_peer(
2106 SetCalleePcWrapperAndReturnCurrent(
Seth Hampson2f0d7022018-02-20 11:54:42 -08002107 CreatePeerConnectionWrapperWithAlternateKey().release()));
deadbeef1dcb1642017-03-29 21:08:16 -07002108 // TODO(deadbeef): Why do we call Close here? That goes against the comment
2109 // directly above.
2110 original_peer->pc()->Close();
2111
2112 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002113 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002114 caller()->SetOfferAnswerOptions(IceRestartOfferAnswerOptions());
2115 caller()->CreateAndSetAndSignalOffer();
2116 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2117 // Wait for some additional frames to be transmitted end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002118 MediaExpectations media_expectations;
2119 media_expectations.ExpectBidirectionalAudioAndVideo();
2120 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002121}
2122
2123// This test sets up a non-bundled call and negotiates bundling at the same
2124// time as starting an ICE restart. When bundling is in effect in the restart,
2125// the DTLS-SRTP context should be successfully reset.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002126TEST_P(PeerConnectionIntegrationTest, BundlingEnabledWhileIceRestartOccurs) {
deadbeef1dcb1642017-03-29 21:08:16 -07002127 ASSERT_TRUE(CreatePeerConnectionWrappers());
2128 ConnectFakeSignaling();
2129
Steve Anton15324772018-01-16 10:26:49 -08002130 caller()->AddAudioVideoTracks();
2131 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002132 // Remove the bundle group from the SDP received by the callee.
2133 callee()->SetReceivedSdpMunger([](cricket::SessionDescription* desc) {
2134 desc->RemoveGroupByName("BUNDLE");
2135 });
2136 caller()->CreateAndSetAndSignalOffer();
2137 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002138 {
2139 MediaExpectations media_expectations;
2140 media_expectations.ExpectBidirectionalAudioAndVideo();
2141 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2142 }
deadbeef1dcb1642017-03-29 21:08:16 -07002143 // Now stop removing the BUNDLE group, and trigger an ICE restart.
2144 callee()->SetReceivedSdpMunger(nullptr);
2145 caller()->SetOfferAnswerOptions(IceRestartOfferAnswerOptions());
2146 caller()->CreateAndSetAndSignalOffer();
2147 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2148
2149 // Expect additional frames to be received after the ICE restart.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002150 {
2151 MediaExpectations media_expectations;
2152 media_expectations.ExpectBidirectionalAudioAndVideo();
2153 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2154 }
deadbeef1dcb1642017-03-29 21:08:16 -07002155}
2156
2157// Test CVO (Coordination of Video Orientation). If a video source is rotated
2158// and both peers support the CVO RTP header extension, the actual video frames
2159// don't need to be encoded in different resolutions, since the rotation is
2160// communicated through the RTP header extension.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002161TEST_P(PeerConnectionIntegrationTest, RotatedVideoWithCVOExtension) {
deadbeef1dcb1642017-03-29 21:08:16 -07002162 ASSERT_TRUE(CreatePeerConnectionWrappers());
2163 ConnectFakeSignaling();
2164 // Add rotated video tracks.
Steve Anton15324772018-01-16 10:26:49 -08002165 caller()->AddTrack(
deadbeef1dcb1642017-03-29 21:08:16 -07002166 caller()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_90));
Steve Anton15324772018-01-16 10:26:49 -08002167 callee()->AddTrack(
deadbeef1dcb1642017-03-29 21:08:16 -07002168 callee()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_270));
2169
2170 // Wait for video frames to be received by both sides.
2171 caller()->CreateAndSetAndSignalOffer();
2172 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2173 ASSERT_TRUE_WAIT(caller()->min_video_frames_received_per_track() > 0 &&
2174 callee()->min_video_frames_received_per_track() > 0,
2175 kMaxWaitForFramesMs);
2176
2177 // Ensure that the aspect ratio is unmodified.
2178 // TODO(deadbeef): Where does 4:3 come from? Should be explicit in the test,
2179 // not just assumed.
2180 EXPECT_EQ(4.0 / 3, caller()->local_rendered_aspect_ratio());
2181 EXPECT_EQ(4.0 / 3, caller()->rendered_aspect_ratio());
2182 EXPECT_EQ(4.0 / 3, callee()->local_rendered_aspect_ratio());
2183 EXPECT_EQ(4.0 / 3, callee()->rendered_aspect_ratio());
2184 // Ensure that the CVO bits were surfaced to the renderer.
2185 EXPECT_EQ(webrtc::kVideoRotation_270, caller()->rendered_rotation());
2186 EXPECT_EQ(webrtc::kVideoRotation_90, callee()->rendered_rotation());
2187}
2188
2189// Test that when the CVO extension isn't supported, video is rotated the
2190// old-fashioned way, by encoding rotated frames.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002191TEST_P(PeerConnectionIntegrationTest, RotatedVideoWithoutCVOExtension) {
deadbeef1dcb1642017-03-29 21:08:16 -07002192 ASSERT_TRUE(CreatePeerConnectionWrappers());
2193 ConnectFakeSignaling();
2194 // Add rotated video tracks.
Steve Anton15324772018-01-16 10:26:49 -08002195 caller()->AddTrack(
deadbeef1dcb1642017-03-29 21:08:16 -07002196 caller()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_90));
Steve Anton15324772018-01-16 10:26:49 -08002197 callee()->AddTrack(
deadbeef1dcb1642017-03-29 21:08:16 -07002198 callee()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_270));
2199
2200 // Remove the CVO extension from the offered SDP.
2201 callee()->SetReceivedSdpMunger([](cricket::SessionDescription* desc) {
2202 cricket::VideoContentDescription* video =
2203 GetFirstVideoContentDescription(desc);
2204 video->ClearRtpHeaderExtensions();
2205 });
2206 // Wait for video frames to be received by both sides.
2207 caller()->CreateAndSetAndSignalOffer();
2208 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2209 ASSERT_TRUE_WAIT(caller()->min_video_frames_received_per_track() > 0 &&
2210 callee()->min_video_frames_received_per_track() > 0,
2211 kMaxWaitForFramesMs);
2212
2213 // Expect that the aspect ratio is inversed to account for the 90/270 degree
2214 // rotation.
2215 // TODO(deadbeef): Where does 4:3 come from? Should be explicit in the test,
2216 // not just assumed.
2217 EXPECT_EQ(3.0 / 4, caller()->local_rendered_aspect_ratio());
2218 EXPECT_EQ(3.0 / 4, caller()->rendered_aspect_ratio());
2219 EXPECT_EQ(3.0 / 4, callee()->local_rendered_aspect_ratio());
2220 EXPECT_EQ(3.0 / 4, callee()->rendered_aspect_ratio());
2221 // Expect that each endpoint is unaware of the rotation of the other endpoint.
2222 EXPECT_EQ(webrtc::kVideoRotation_0, caller()->rendered_rotation());
2223 EXPECT_EQ(webrtc::kVideoRotation_0, callee()->rendered_rotation());
2224}
2225
deadbeef1dcb1642017-03-29 21:08:16 -07002226// Test that if the answerer rejects the audio m= section, no audio is sent or
2227// received, but video still can be.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002228TEST_P(PeerConnectionIntegrationTest, AnswererRejectsAudioSection) {
deadbeef1dcb1642017-03-29 21:08:16 -07002229 ASSERT_TRUE(CreatePeerConnectionWrappers());
2230 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002231 caller()->AddAudioVideoTracks();
Seth Hampson2f0d7022018-02-20 11:54:42 -08002232 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2233 // Only add video track for callee, and set offer_to_receive_audio to 0, so
2234 // it will reject the audio m= section completely.
2235 PeerConnectionInterface::RTCOfferAnswerOptions options;
2236 options.offer_to_receive_audio = 0;
2237 callee()->SetOfferAnswerOptions(options);
2238 } else {
2239 // Stopping the audio RtpTransceiver will cause the media section to be
2240 // rejected in the answer.
2241 callee()->SetRemoteOfferHandler([this] {
2242 callee()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_AUDIO)->Stop();
2243 });
2244 }
Steve Anton15324772018-01-16 10:26:49 -08002245 callee()->AddTrack(callee()->CreateLocalVideoTrack());
deadbeef1dcb1642017-03-29 21:08:16 -07002246 // Do offer/answer and wait for successful end-to-end video frames.
2247 caller()->CreateAndSetAndSignalOffer();
2248 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002249 MediaExpectations media_expectations;
2250 media_expectations.ExpectBidirectionalVideo();
2251 media_expectations.ExpectNoAudio();
2252 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2253
deadbeef1dcb1642017-03-29 21:08:16 -07002254 // Sanity check that the callee's description has a rejected audio section.
2255 ASSERT_NE(nullptr, callee()->pc()->local_description());
2256 const ContentInfo* callee_audio_content =
2257 GetFirstAudioContent(callee()->pc()->local_description()->description());
2258 ASSERT_NE(nullptr, callee_audio_content);
2259 EXPECT_TRUE(callee_audio_content->rejected);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002260 if (sdp_semantics_ == SdpSemantics::kUnifiedPlan) {
2261 // The caller's transceiver should have stopped after receiving the answer.
2262 EXPECT_TRUE(caller()
2263 ->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_AUDIO)
2264 ->stopped());
2265 }
deadbeef1dcb1642017-03-29 21:08:16 -07002266}
2267
2268// Test that if the answerer rejects the video m= section, no video is sent or
2269// received, but audio still can be.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002270TEST_P(PeerConnectionIntegrationTest, AnswererRejectsVideoSection) {
deadbeef1dcb1642017-03-29 21:08:16 -07002271 ASSERT_TRUE(CreatePeerConnectionWrappers());
2272 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002273 caller()->AddAudioVideoTracks();
Seth Hampson2f0d7022018-02-20 11:54:42 -08002274 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2275 // Only add audio track for callee, and set offer_to_receive_video to 0, so
2276 // it will reject the video m= section completely.
2277 PeerConnectionInterface::RTCOfferAnswerOptions options;
2278 options.offer_to_receive_video = 0;
2279 callee()->SetOfferAnswerOptions(options);
2280 } else {
2281 // Stopping the video RtpTransceiver will cause the media section to be
2282 // rejected in the answer.
2283 callee()->SetRemoteOfferHandler([this] {
2284 callee()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)->Stop();
2285 });
2286 }
Steve Anton15324772018-01-16 10:26:49 -08002287 callee()->AddTrack(callee()->CreateLocalAudioTrack());
deadbeef1dcb1642017-03-29 21:08:16 -07002288 // Do offer/answer and wait for successful end-to-end audio frames.
2289 caller()->CreateAndSetAndSignalOffer();
2290 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002291 MediaExpectations media_expectations;
2292 media_expectations.ExpectBidirectionalAudio();
2293 media_expectations.ExpectNoVideo();
2294 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2295
deadbeef1dcb1642017-03-29 21:08:16 -07002296 // Sanity check that the callee's description has a rejected video section.
2297 ASSERT_NE(nullptr, callee()->pc()->local_description());
2298 const ContentInfo* callee_video_content =
2299 GetFirstVideoContent(callee()->pc()->local_description()->description());
2300 ASSERT_NE(nullptr, callee_video_content);
2301 EXPECT_TRUE(callee_video_content->rejected);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002302 if (sdp_semantics_ == SdpSemantics::kUnifiedPlan) {
2303 // The caller's transceiver should have stopped after receiving the answer.
2304 EXPECT_TRUE(caller()
2305 ->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)
2306 ->stopped());
2307 }
deadbeef1dcb1642017-03-29 21:08:16 -07002308}
2309
2310// Test that if the answerer rejects both audio and video m= sections, nothing
2311// bad happens.
2312// TODO(deadbeef): Test that a data channel still works. Currently this doesn't
2313// test anything but the fact that negotiation succeeds, which doesn't mean
2314// much.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002315TEST_P(PeerConnectionIntegrationTest, AnswererRejectsAudioAndVideoSections) {
deadbeef1dcb1642017-03-29 21:08:16 -07002316 ASSERT_TRUE(CreatePeerConnectionWrappers());
2317 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002318 caller()->AddAudioVideoTracks();
Seth Hampson2f0d7022018-02-20 11:54:42 -08002319 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2320 // Don't give the callee any tracks, and set offer_to_receive_X to 0, so it
2321 // will reject both audio and video m= sections.
2322 PeerConnectionInterface::RTCOfferAnswerOptions options;
2323 options.offer_to_receive_audio = 0;
2324 options.offer_to_receive_video = 0;
2325 callee()->SetOfferAnswerOptions(options);
2326 } else {
2327 callee()->SetRemoteOfferHandler([this] {
2328 // Stopping all transceivers will cause all media sections to be rejected.
Mirko Bonadei739baf02019-01-27 17:29:42 +01002329 for (const auto& transceiver : callee()->pc()->GetTransceivers()) {
Seth Hampson2f0d7022018-02-20 11:54:42 -08002330 transceiver->Stop();
2331 }
2332 });
2333 }
deadbeef1dcb1642017-03-29 21:08:16 -07002334 // Do offer/answer and wait for stable signaling state.
2335 caller()->CreateAndSetAndSignalOffer();
2336 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002337
deadbeef1dcb1642017-03-29 21:08:16 -07002338 // Sanity check that the callee's description has rejected m= sections.
2339 ASSERT_NE(nullptr, callee()->pc()->local_description());
2340 const ContentInfo* callee_audio_content =
2341 GetFirstAudioContent(callee()->pc()->local_description()->description());
2342 ASSERT_NE(nullptr, callee_audio_content);
2343 EXPECT_TRUE(callee_audio_content->rejected);
2344 const ContentInfo* callee_video_content =
2345 GetFirstVideoContent(callee()->pc()->local_description()->description());
2346 ASSERT_NE(nullptr, callee_video_content);
2347 EXPECT_TRUE(callee_video_content->rejected);
2348}
2349
2350// This test sets up an audio and video call between two parties. After the
2351// call runs for a while, the caller sends an updated offer with video being
2352// rejected. Once the re-negotiation is done, the video flow should stop and
2353// the audio flow should continue.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002354TEST_P(PeerConnectionIntegrationTest, VideoRejectedInSubsequentOffer) {
deadbeef1dcb1642017-03-29 21:08:16 -07002355 ASSERT_TRUE(CreatePeerConnectionWrappers());
2356 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002357 caller()->AddAudioVideoTracks();
2358 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002359 caller()->CreateAndSetAndSignalOffer();
2360 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002361 {
2362 MediaExpectations media_expectations;
2363 media_expectations.ExpectBidirectionalAudioAndVideo();
2364 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2365 }
deadbeef1dcb1642017-03-29 21:08:16 -07002366 // Renegotiate, rejecting the video m= section.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002367 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2368 caller()->SetGeneratedSdpMunger(
2369 [](cricket::SessionDescription* description) {
2370 for (cricket::ContentInfo& content : description->contents()) {
2371 if (cricket::IsVideoContent(&content)) {
2372 content.rejected = true;
2373 }
2374 }
2375 });
2376 } else {
2377 caller()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)->Stop();
2378 }
deadbeef1dcb1642017-03-29 21:08:16 -07002379 caller()->CreateAndSetAndSignalOffer();
2380 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
2381
2382 // Sanity check that the caller's description has a rejected video section.
2383 ASSERT_NE(nullptr, caller()->pc()->local_description());
2384 const ContentInfo* caller_video_content =
2385 GetFirstVideoContent(caller()->pc()->local_description()->description());
2386 ASSERT_NE(nullptr, caller_video_content);
2387 EXPECT_TRUE(caller_video_content->rejected);
deadbeef1dcb1642017-03-29 21:08:16 -07002388 // Wait for some additional audio frames to be received.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002389 {
2390 MediaExpectations media_expectations;
2391 media_expectations.ExpectBidirectionalAudio();
2392 media_expectations.ExpectNoVideo();
2393 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2394 }
deadbeef1dcb1642017-03-29 21:08:16 -07002395}
2396
Taylor Brandstetter60c8dc82018-04-11 15:20:27 -07002397// Do one offer/answer with audio, another that disables it (rejecting the m=
2398// section), and another that re-enables it. Regression test for:
2399// bugs.webrtc.org/6023
2400TEST_F(PeerConnectionIntegrationTestPlanB, EnableAudioAfterRejecting) {
2401 ASSERT_TRUE(CreatePeerConnectionWrappers());
2402 ConnectFakeSignaling();
2403
2404 // Add audio track, do normal offer/answer.
2405 rtc::scoped_refptr<webrtc::AudioTrackInterface> track =
2406 caller()->CreateLocalAudioTrack();
2407 rtc::scoped_refptr<webrtc::RtpSenderInterface> sender =
2408 caller()->pc()->AddTrack(track, {"stream"}).MoveValue();
2409 caller()->CreateAndSetAndSignalOffer();
2410 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2411
2412 // Remove audio track, and set offer_to_receive_audio to false to cause the
2413 // m= section to be completely disabled, not just "recvonly".
2414 caller()->pc()->RemoveTrack(sender);
2415 PeerConnectionInterface::RTCOfferAnswerOptions options;
2416 options.offer_to_receive_audio = 0;
2417 caller()->SetOfferAnswerOptions(options);
2418 caller()->CreateAndSetAndSignalOffer();
2419 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2420
2421 // Add the audio track again, expecting negotiation to succeed and frames to
2422 // flow.
2423 sender = caller()->pc()->AddTrack(track, {"stream"}).MoveValue();
2424 options.offer_to_receive_audio = 1;
2425 caller()->SetOfferAnswerOptions(options);
2426 caller()->CreateAndSetAndSignalOffer();
2427 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2428
2429 MediaExpectations media_expectations;
2430 media_expectations.CalleeExpectsSomeAudio();
2431 EXPECT_TRUE(ExpectNewFrames(media_expectations));
2432}
2433
deadbeef1dcb1642017-03-29 21:08:16 -07002434// Basic end-to-end test, but without SSRC/MSID signaling. This functionality
2435// is needed to support legacy endpoints.
2436// TODO(deadbeef): When we support the MID extension and demuxing on MID, also
2437// add a test for an end-to-end test without MID signaling either (basically,
2438// the minimum acceptable SDP).
Seth Hampson2f0d7022018-02-20 11:54:42 -08002439TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithoutSsrcOrMsidSignaling) {
deadbeef1dcb1642017-03-29 21:08:16 -07002440 ASSERT_TRUE(CreatePeerConnectionWrappers());
2441 ConnectFakeSignaling();
2442 // Add audio and video, testing that packets can be demuxed on payload type.
Steve Anton15324772018-01-16 10:26:49 -08002443 caller()->AddAudioVideoTracks();
2444 callee()->AddAudioVideoTracks();
deadbeefd8ad7882017-04-18 16:01:17 -07002445 // Remove SSRCs and MSIDs from the received offer SDP.
2446 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
deadbeef1dcb1642017-03-29 21:08:16 -07002447 caller()->CreateAndSetAndSignalOffer();
2448 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002449 MediaExpectations media_expectations;
2450 media_expectations.ExpectBidirectionalAudioAndVideo();
2451 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002452}
2453
Seth Hampson5897a6e2018-04-03 11:16:33 -07002454// Basic end-to-end test, without SSRC signaling. This means that the track
2455// was created properly and frames are delivered when the MSIDs are communicated
2456// with a=msid lines and no a=ssrc lines.
2457TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
2458 EndToEndCallWithoutSsrcSignaling) {
2459 const char kStreamId[] = "streamId";
2460 ASSERT_TRUE(CreatePeerConnectionWrappers());
2461 ConnectFakeSignaling();
2462 // Add just audio tracks.
2463 caller()->AddTrack(caller()->CreateLocalAudioTrack(), {kStreamId});
2464 callee()->AddAudioTrack();
2465
2466 // Remove SSRCs from the received offer SDP.
2467 callee()->SetReceivedSdpMunger(RemoveSsrcsAndKeepMsids);
2468 caller()->CreateAndSetAndSignalOffer();
2469 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2470 MediaExpectations media_expectations;
2471 media_expectations.ExpectBidirectionalAudio();
2472 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2473}
2474
Steve Antondf527fd2018-04-27 15:52:03 -07002475// Tests that video flows between multiple video tracks when SSRCs are not
2476// signaled. This exercises the MID RTP header extension which is needed to
2477// demux the incoming video tracks.
2478TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
2479 EndToEndCallWithTwoVideoTracksAndNoSignaledSsrc) {
2480 ASSERT_TRUE(CreatePeerConnectionWrappers());
2481 ConnectFakeSignaling();
2482 caller()->AddVideoTrack();
2483 caller()->AddVideoTrack();
2484 callee()->AddVideoTrack();
2485 callee()->AddVideoTrack();
2486
2487 caller()->SetReceivedSdpMunger(&RemoveSsrcsAndKeepMsids);
2488 callee()->SetReceivedSdpMunger(&RemoveSsrcsAndKeepMsids);
2489 caller()->CreateAndSetAndSignalOffer();
2490 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2491 ASSERT_EQ(2u, caller()->pc()->GetReceivers().size());
2492 ASSERT_EQ(2u, callee()->pc()->GetReceivers().size());
2493
2494 // Expect video to be received in both directions on both tracks.
2495 MediaExpectations media_expectations;
2496 media_expectations.ExpectBidirectionalVideo();
2497 EXPECT_TRUE(ExpectNewFrames(media_expectations));
2498}
2499
Henrik Boström5b147782018-12-04 11:25:05 +01002500TEST_F(PeerConnectionIntegrationTestUnifiedPlan, NoStreamsMsidLinePresent) {
2501 ASSERT_TRUE(CreatePeerConnectionWrappers());
2502 ConnectFakeSignaling();
2503 caller()->AddAudioTrack();
2504 caller()->AddVideoTrack();
2505 caller()->CreateAndSetAndSignalOffer();
2506 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2507 auto callee_receivers = callee()->pc()->GetReceivers();
2508 ASSERT_EQ(2u, callee_receivers.size());
2509 EXPECT_TRUE(callee_receivers[0]->stream_ids().empty());
2510 EXPECT_TRUE(callee_receivers[1]->stream_ids().empty());
2511}
2512
2513TEST_F(PeerConnectionIntegrationTestUnifiedPlan, NoStreamsMsidLineMissing) {
2514 ASSERT_TRUE(CreatePeerConnectionWrappers());
2515 ConnectFakeSignaling();
2516 caller()->AddAudioTrack();
2517 caller()->AddVideoTrack();
2518 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
2519 caller()->CreateAndSetAndSignalOffer();
2520 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2521 auto callee_receivers = callee()->pc()->GetReceivers();
2522 ASSERT_EQ(2u, callee_receivers.size());
2523 ASSERT_EQ(1u, callee_receivers[0]->stream_ids().size());
2524 ASSERT_EQ(1u, callee_receivers[1]->stream_ids().size());
2525 EXPECT_EQ(callee_receivers[0]->stream_ids()[0],
2526 callee_receivers[1]->stream_ids()[0]);
2527 EXPECT_EQ(callee_receivers[0]->streams()[0],
2528 callee_receivers[1]->streams()[0]);
2529}
2530
deadbeef1dcb1642017-03-29 21:08:16 -07002531// Test that if two video tracks are sent (from caller to callee, in this test),
2532// they're transmitted correctly end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002533TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithTwoVideoTracks) {
deadbeef1dcb1642017-03-29 21:08:16 -07002534 ASSERT_TRUE(CreatePeerConnectionWrappers());
2535 ConnectFakeSignaling();
2536 // Add one audio/video stream, and one video-only stream.
Steve Anton15324772018-01-16 10:26:49 -08002537 caller()->AddAudioVideoTracks();
2538 caller()->AddVideoTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07002539 caller()->CreateAndSetAndSignalOffer();
2540 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Steve Anton15324772018-01-16 10:26:49 -08002541 ASSERT_EQ(3u, callee()->pc()->GetReceivers().size());
Seth Hampson2f0d7022018-02-20 11:54:42 -08002542
2543 MediaExpectations media_expectations;
2544 media_expectations.CalleeExpectsSomeAudioAndVideo();
2545 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002546}
2547
2548static void MakeSpecCompliantMaxBundleOffer(cricket::SessionDescription* desc) {
2549 bool first = true;
2550 for (cricket::ContentInfo& content : desc->contents()) {
2551 if (first) {
2552 first = false;
2553 continue;
2554 }
2555 content.bundle_only = true;
2556 }
2557 first = true;
2558 for (cricket::TransportInfo& transport : desc->transport_infos()) {
2559 if (first) {
2560 first = false;
2561 continue;
2562 }
2563 transport.description.ice_ufrag.clear();
2564 transport.description.ice_pwd.clear();
2565 transport.description.connection_role = cricket::CONNECTIONROLE_NONE;
2566 transport.description.identity_fingerprint.reset(nullptr);
2567 }
2568}
2569
2570// Test that if applying a true "max bundle" offer, which uses ports of 0,
2571// "a=bundle-only", omitting "a=fingerprint", "a=setup", "a=ice-ufrag" and
2572// "a=ice-pwd" for all but the audio "m=" section, negotiation still completes
2573// successfully and media flows.
2574// TODO(deadbeef): Update this test to also omit "a=rtcp-mux", once that works.
2575// TODO(deadbeef): Won't need this test once we start generating actual
2576// standards-compliant SDP.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002577TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07002578 EndToEndCallWithSpecCompliantMaxBundleOffer) {
2579 ASSERT_TRUE(CreatePeerConnectionWrappers());
2580 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002581 caller()->AddAudioVideoTracks();
2582 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002583 // Do the equivalent of setting the port to 0, adding a=bundle-only, and
2584 // removing a=ice-ufrag, a=ice-pwd, a=fingerprint and a=setup from all
2585 // but the first m= section.
2586 callee()->SetReceivedSdpMunger(MakeSpecCompliantMaxBundleOffer);
2587 caller()->CreateAndSetAndSignalOffer();
2588 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002589 MediaExpectations media_expectations;
2590 media_expectations.ExpectBidirectionalAudioAndVideo();
2591 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002592}
2593
2594// Test that we can receive the audio output level from a remote audio track.
2595// TODO(deadbeef): Use a fake audio source and verify that the output level is
2596// exactly what the source on the other side was configured with.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002597TEST_P(PeerConnectionIntegrationTest, GetAudioOutputLevelStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-29 21:08:16 -07002598 ASSERT_TRUE(CreatePeerConnectionWrappers());
2599 ConnectFakeSignaling();
2600 // Just add an audio track.
Steve Anton15324772018-01-16 10:26:49 -08002601 caller()->AddAudioTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07002602 caller()->CreateAndSetAndSignalOffer();
2603 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2604
2605 // Get the audio output level stats. Note that the level is not available
2606 // until an RTCP packet has been received.
deadbeefd8ad7882017-04-18 16:01:17 -07002607 EXPECT_TRUE_WAIT(callee()->OldGetStats()->AudioOutputLevel() > 0,
deadbeef1dcb1642017-03-29 21:08:16 -07002608 kMaxWaitForFramesMs);
2609}
2610
2611// Test that an audio input level is reported.
2612// TODO(deadbeef): Use a fake audio source and verify that the input level is
2613// exactly what the source was configured with.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002614TEST_P(PeerConnectionIntegrationTest, GetAudioInputLevelStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-29 21:08:16 -07002615 ASSERT_TRUE(CreatePeerConnectionWrappers());
2616 ConnectFakeSignaling();
2617 // Just add an audio track.
Steve Anton15324772018-01-16 10:26:49 -08002618 caller()->AddAudioTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07002619 caller()->CreateAndSetAndSignalOffer();
2620 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2621
2622 // Get the audio input level stats. The level should be available very
2623 // soon after the test starts.
deadbeefd8ad7882017-04-18 16:01:17 -07002624 EXPECT_TRUE_WAIT(caller()->OldGetStats()->AudioInputLevel() > 0,
deadbeef1dcb1642017-03-29 21:08:16 -07002625 kMaxWaitForStatsMs);
2626}
2627
2628// Test that we can get incoming byte counts from both audio and video tracks.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002629TEST_P(PeerConnectionIntegrationTest, GetBytesReceivedStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-29 21:08:16 -07002630 ASSERT_TRUE(CreatePeerConnectionWrappers());
2631 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002632 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002633 // Do offer/answer, wait for the callee to receive some frames.
2634 caller()->CreateAndSetAndSignalOffer();
2635 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002636
2637 MediaExpectations media_expectations;
2638 media_expectations.CalleeExpectsSomeAudioAndVideo();
2639 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002640
2641 // Get a handle to the remote tracks created, so they can be used as GetStats
2642 // filters.
Mirko Bonadei739baf02019-01-27 17:29:42 +01002643 for (const auto& receiver : callee()->pc()->GetReceivers()) {
Steve Anton15324772018-01-16 10:26:49 -08002644 // We received frames, so we definitely should have nonzero "received bytes"
2645 // stats at this point.
2646 EXPECT_GT(callee()->OldGetStatsForTrack(receiver->track())->BytesReceived(),
2647 0);
2648 }
deadbeef1dcb1642017-03-29 21:08:16 -07002649}
2650
2651// Test that we can get outgoing byte counts from both audio and video tracks.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002652TEST_P(PeerConnectionIntegrationTest, GetBytesSentStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-29 21:08:16 -07002653 ASSERT_TRUE(CreatePeerConnectionWrappers());
2654 ConnectFakeSignaling();
2655 auto audio_track = caller()->CreateLocalAudioTrack();
2656 auto video_track = caller()->CreateLocalVideoTrack();
Steve Anton15324772018-01-16 10:26:49 -08002657 caller()->AddTrack(audio_track);
2658 caller()->AddTrack(video_track);
deadbeef1dcb1642017-03-29 21:08:16 -07002659 // Do offer/answer, wait for the callee to receive some frames.
2660 caller()->CreateAndSetAndSignalOffer();
2661 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002662 MediaExpectations media_expectations;
2663 media_expectations.CalleeExpectsSomeAudioAndVideo();
2664 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002665
2666 // The callee received frames, so we definitely should have nonzero "sent
2667 // bytes" stats at this point.
deadbeefd8ad7882017-04-18 16:01:17 -07002668 EXPECT_GT(caller()->OldGetStatsForTrack(audio_track)->BytesSent(), 0);
2669 EXPECT_GT(caller()->OldGetStatsForTrack(video_track)->BytesSent(), 0);
2670}
2671
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002672// Test that we can get capture start ntp time.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002673TEST_P(PeerConnectionIntegrationTest, GetCaptureStartNtpTimeWithOldStatsApi) {
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002674 ASSERT_TRUE(CreatePeerConnectionWrappers());
2675 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002676 caller()->AddAudioTrack();
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002677
Steve Anton15324772018-01-16 10:26:49 -08002678 callee()->AddAudioTrack();
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002679
2680 // Do offer/answer, wait for the callee to receive some frames.
2681 caller()->CreateAndSetAndSignalOffer();
2682 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2683
2684 // Get the remote audio track created on the receiver, so they can be used as
2685 // GetStats filters.
Steve Antonfc853712018-03-01 13:48:58 -08002686 auto receivers = callee()->pc()->GetReceivers();
2687 ASSERT_EQ(1u, receivers.size());
2688 auto remote_audio_track = receivers[0]->track();
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002689
2690 // Get the audio output level stats. Note that the level is not available
2691 // until an RTCP packet has been received.
Zhi Huange830e682018-03-30 10:48:35 -07002692 EXPECT_TRUE_WAIT(
2693 callee()->OldGetStatsForTrack(remote_audio_track)->CaptureStartNtpTime() >
2694 0,
2695 2 * kMaxWaitForFramesMs);
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002696}
2697
Steve Antona41959e2018-11-28 11:15:33 -08002698// Test that the track ID is associated with all local and remote SSRC stats
2699// using the old GetStats() and more than 1 audio and more than 1 video track.
2700// This is a regression test for crbug.com/906988
2701TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
2702 OldGetStatsAssociatesTrackIdForManyMediaSections) {
2703 ASSERT_TRUE(CreatePeerConnectionWrappers());
2704 ConnectFakeSignaling();
2705 auto audio_sender_1 = caller()->AddAudioTrack();
2706 auto video_sender_1 = caller()->AddVideoTrack();
2707 auto audio_sender_2 = caller()->AddAudioTrack();
2708 auto video_sender_2 = caller()->AddVideoTrack();
2709 caller()->CreateAndSetAndSignalOffer();
2710 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2711
2712 MediaExpectations media_expectations;
2713 media_expectations.CalleeExpectsSomeAudioAndVideo();
2714 ASSERT_TRUE_WAIT(ExpectNewFrames(media_expectations), kDefaultTimeout);
2715
2716 std::vector<std::string> track_ids = {
2717 audio_sender_1->track()->id(), video_sender_1->track()->id(),
2718 audio_sender_2->track()->id(), video_sender_2->track()->id()};
2719
2720 auto caller_stats = caller()->OldGetStats();
2721 EXPECT_THAT(caller_stats->TrackIds(), UnorderedElementsAreArray(track_ids));
2722 auto callee_stats = callee()->OldGetStats();
2723 EXPECT_THAT(callee_stats->TrackIds(), UnorderedElementsAreArray(track_ids));
2724}
2725
Steve Antonffa6ce42018-11-30 09:26:08 -08002726// Test that the new GetStats() returns stats for all outgoing/incoming streams
2727// with the correct track IDs if there are more than one audio and more than one
2728// video senders/receivers.
2729TEST_P(PeerConnectionIntegrationTest, NewGetStatsManyAudioAndManyVideoStreams) {
2730 ASSERT_TRUE(CreatePeerConnectionWrappers());
2731 ConnectFakeSignaling();
2732 auto audio_sender_1 = caller()->AddAudioTrack();
2733 auto video_sender_1 = caller()->AddVideoTrack();
2734 auto audio_sender_2 = caller()->AddAudioTrack();
2735 auto video_sender_2 = caller()->AddVideoTrack();
2736 caller()->CreateAndSetAndSignalOffer();
2737 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2738
2739 MediaExpectations media_expectations;
2740 media_expectations.CalleeExpectsSomeAudioAndVideo();
2741 ASSERT_TRUE_WAIT(ExpectNewFrames(media_expectations), kDefaultTimeout);
2742
2743 std::vector<std::string> track_ids = {
2744 audio_sender_1->track()->id(), video_sender_1->track()->id(),
2745 audio_sender_2->track()->id(), video_sender_2->track()->id()};
2746
2747 rtc::scoped_refptr<const webrtc::RTCStatsReport> caller_report =
2748 caller()->NewGetStats();
2749 ASSERT_TRUE(caller_report);
2750 auto outbound_stream_stats =
2751 caller_report->GetStatsOfType<webrtc::RTCOutboundRTPStreamStats>();
2752 ASSERT_EQ(4u, outbound_stream_stats.size());
2753 std::vector<std::string> outbound_track_ids;
2754 for (const auto& stat : outbound_stream_stats) {
2755 ASSERT_TRUE(stat->bytes_sent.is_defined());
2756 EXPECT_LT(0u, *stat->bytes_sent);
Rasmus Brandt2efae772019-06-27 14:29:34 +02002757 if (*stat->kind == "video") {
2758 ASSERT_TRUE(stat->key_frames_encoded.is_defined());
2759 EXPECT_GT(*stat->key_frames_encoded, 0u);
2760 ASSERT_TRUE(stat->frames_encoded.is_defined());
2761 EXPECT_GE(*stat->frames_encoded, *stat->key_frames_encoded);
2762 }
Steve Antonffa6ce42018-11-30 09:26:08 -08002763 ASSERT_TRUE(stat->track_id.is_defined());
2764 const auto* track_stat =
2765 caller_report->GetAs<webrtc::RTCMediaStreamTrackStats>(*stat->track_id);
2766 ASSERT_TRUE(track_stat);
2767 outbound_track_ids.push_back(*track_stat->track_identifier);
2768 }
2769 EXPECT_THAT(outbound_track_ids, UnorderedElementsAreArray(track_ids));
2770
2771 rtc::scoped_refptr<const webrtc::RTCStatsReport> callee_report =
2772 callee()->NewGetStats();
2773 ASSERT_TRUE(callee_report);
2774 auto inbound_stream_stats =
2775 callee_report->GetStatsOfType<webrtc::RTCInboundRTPStreamStats>();
2776 ASSERT_EQ(4u, inbound_stream_stats.size());
2777 std::vector<std::string> inbound_track_ids;
2778 for (const auto& stat : inbound_stream_stats) {
2779 ASSERT_TRUE(stat->bytes_received.is_defined());
2780 EXPECT_LT(0u, *stat->bytes_received);
Rasmus Brandt2efae772019-06-27 14:29:34 +02002781 if (*stat->kind == "video") {
2782 ASSERT_TRUE(stat->key_frames_decoded.is_defined());
2783 EXPECT_GT(*stat->key_frames_decoded, 0u);
2784 ASSERT_TRUE(stat->frames_decoded.is_defined());
2785 EXPECT_GE(*stat->frames_decoded, *stat->key_frames_decoded);
2786 }
Steve Antonffa6ce42018-11-30 09:26:08 -08002787 ASSERT_TRUE(stat->track_id.is_defined());
2788 const auto* track_stat =
2789 callee_report->GetAs<webrtc::RTCMediaStreamTrackStats>(*stat->track_id);
2790 ASSERT_TRUE(track_stat);
2791 inbound_track_ids.push_back(*track_stat->track_identifier);
2792 }
2793 EXPECT_THAT(inbound_track_ids, UnorderedElementsAreArray(track_ids));
2794}
2795
2796// Test that we can get stats (using the new stats implementation) for
deadbeefd8ad7882017-04-18 16:01:17 -07002797// unsignaled streams. Meaning when SSRCs/MSIDs aren't signaled explicitly in
2798// SDP.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002799TEST_P(PeerConnectionIntegrationTest,
deadbeefd8ad7882017-04-18 16:01:17 -07002800 GetStatsForUnsignaledStreamWithNewStatsApi) {
2801 ASSERT_TRUE(CreatePeerConnectionWrappers());
2802 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002803 caller()->AddAudioTrack();
deadbeefd8ad7882017-04-18 16:01:17 -07002804 // Remove SSRCs and MSIDs from the received offer SDP.
2805 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
2806 caller()->CreateAndSetAndSignalOffer();
2807 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002808 MediaExpectations media_expectations;
2809 media_expectations.CalleeExpectsSomeAudio(1);
2810 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeefd8ad7882017-04-18 16:01:17 -07002811
2812 // We received a frame, so we should have nonzero "bytes received" stats for
2813 // the unsignaled stream, if stats are working for it.
2814 rtc::scoped_refptr<const webrtc::RTCStatsReport> report =
2815 callee()->NewGetStats();
2816 ASSERT_NE(nullptr, report);
2817 auto inbound_stream_stats =
2818 report->GetStatsOfType<webrtc::RTCInboundRTPStreamStats>();
2819 ASSERT_EQ(1U, inbound_stream_stats.size());
2820 ASSERT_TRUE(inbound_stream_stats[0]->bytes_received.is_defined());
2821 ASSERT_GT(*inbound_stream_stats[0]->bytes_received, 0U);
zhihuangf8164932017-05-19 13:09:47 -07002822 ASSERT_TRUE(inbound_stream_stats[0]->track_id.is_defined());
2823}
2824
Taylor Brandstettera4653442018-06-19 09:44:26 -07002825// Same as above but for the legacy stats implementation.
2826TEST_P(PeerConnectionIntegrationTest,
2827 GetStatsForUnsignaledStreamWithOldStatsApi) {
2828 ASSERT_TRUE(CreatePeerConnectionWrappers());
2829 ConnectFakeSignaling();
2830 caller()->AddAudioTrack();
2831 // Remove SSRCs and MSIDs from the received offer SDP.
2832 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
2833 caller()->CreateAndSetAndSignalOffer();
2834 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2835
2836 // Note that, since the old stats implementation associates SSRCs with tracks
2837 // using SDP, when SSRCs aren't signaled in SDP these stats won't have an
2838 // associated track ID. So we can't use the track "selector" argument.
2839 //
2840 // Also, we use "EXPECT_TRUE_WAIT" because the stats collector may decide to
2841 // return cached stats if not enough time has passed since the last update.
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02002842 EXPECT_TRUE_WAIT(callee()->OldGetStats()->BytesReceived() > 0,
Taylor Brandstettera4653442018-06-19 09:44:26 -07002843 kDefaultTimeout);
2844}
2845
zhihuangf8164932017-05-19 13:09:47 -07002846// Test that we can successfully get the media related stats (audio level
2847// etc.) for the unsignaled stream.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002848TEST_P(PeerConnectionIntegrationTest,
zhihuangf8164932017-05-19 13:09:47 -07002849 GetMediaStatsForUnsignaledStreamWithNewStatsApi) {
2850 ASSERT_TRUE(CreatePeerConnectionWrappers());
2851 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002852 caller()->AddAudioVideoTracks();
zhihuangf8164932017-05-19 13:09:47 -07002853 // Remove SSRCs and MSIDs from the received offer SDP.
2854 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
2855 caller()->CreateAndSetAndSignalOffer();
2856 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002857 MediaExpectations media_expectations;
2858 media_expectations.CalleeExpectsSomeAudio(1);
2859 media_expectations.CalleeExpectsSomeVideo(1);
2860 ASSERT_TRUE(ExpectNewFrames(media_expectations));
zhihuangf8164932017-05-19 13:09:47 -07002861
2862 rtc::scoped_refptr<const webrtc::RTCStatsReport> report =
2863 callee()->NewGetStats();
2864 ASSERT_NE(nullptr, report);
2865
2866 auto media_stats = report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
2867 auto audio_index = FindFirstMediaStatsIndexByKind("audio", media_stats);
2868 ASSERT_GE(audio_index, 0);
2869 EXPECT_TRUE(media_stats[audio_index]->audio_level.is_defined());
deadbeef1dcb1642017-03-29 21:08:16 -07002870}
2871
deadbeef4e2deab2017-09-20 13:56:21 -07002872// Helper for test below.
2873void ModifySsrcs(cricket::SessionDescription* desc) {
2874 for (ContentInfo& content : desc->contents()) {
Steve Antondf527fd2018-04-27 15:52:03 -07002875 for (StreamParams& stream :
Steve Antonb1c1de12017-12-21 15:14:30 -08002876 content.media_description()->mutable_streams()) {
deadbeef4e2deab2017-09-20 13:56:21 -07002877 for (uint32_t& ssrc : stream.ssrcs) {
2878 ssrc = rtc::CreateRandomId();
2879 }
2880 }
2881 }
2882}
2883
2884// Test that the "RTCMediaSteamTrackStats" object is updated correctly when
2885// SSRCs are unsignaled, and the SSRC of the received (audio) stream changes.
2886// This should result in two "RTCInboundRTPStreamStats", but only one
2887// "RTCMediaStreamTrackStats", whose counters go up continuously rather than
2888// being reset to 0 once the SSRC change occurs.
2889//
2890// Regression test for this bug:
2891// https://bugs.chromium.org/p/webrtc/issues/detail?id=8158
2892//
2893// The bug causes the track stats to only represent one of the two streams:
2894// whichever one has the higher SSRC. So with this bug, there was a 50% chance
2895// that the track stat counters would reset to 0 when the new stream is
2896// received, and a 50% chance that they'll stop updating (while
2897// "concealed_samples" continues increasing, due to silence being generated for
2898// the inactive stream).
Seth Hampson2f0d7022018-02-20 11:54:42 -08002899TEST_P(PeerConnectionIntegrationTest,
Steve Anton83119dd2017-11-10 16:19:52 -08002900 TrackStatsUpdatedCorrectlyWhenUnsignaledSsrcChanges) {
deadbeef4e2deab2017-09-20 13:56:21 -07002901 ASSERT_TRUE(CreatePeerConnectionWrappers());
2902 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002903 caller()->AddAudioTrack();
deadbeef4e2deab2017-09-20 13:56:21 -07002904 // Remove SSRCs and MSIDs from the received offer SDP, simulating an endpoint
2905 // that doesn't signal SSRCs (from the callee's perspective).
2906 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
2907 caller()->CreateAndSetAndSignalOffer();
2908 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2909 // Wait for 50 audio frames (500ms of audio) to be received by the callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002910 {
2911 MediaExpectations media_expectations;
2912 media_expectations.CalleeExpectsSomeAudio(50);
2913 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2914 }
deadbeef4e2deab2017-09-20 13:56:21 -07002915 // Some audio frames were received, so we should have nonzero "samples
2916 // received" for the track.
2917 rtc::scoped_refptr<const webrtc::RTCStatsReport> report =
2918 callee()->NewGetStats();
2919 ASSERT_NE(nullptr, report);
2920 auto track_stats = report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
2921 ASSERT_EQ(1U, track_stats.size());
2922 ASSERT_TRUE(track_stats[0]->total_samples_received.is_defined());
2923 ASSERT_GT(*track_stats[0]->total_samples_received, 0U);
2924 // uint64_t prev_samples_received = *track_stats[0]->total_samples_received;
2925
2926 // Create a new offer and munge it to cause the caller to use a new SSRC.
2927 caller()->SetGeneratedSdpMunger(ModifySsrcs);
2928 caller()->CreateAndSetAndSignalOffer();
2929 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2930 // Wait for 25 more audio frames (250ms of audio) to be received, from the new
2931 // SSRC.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002932 {
2933 MediaExpectations media_expectations;
2934 media_expectations.CalleeExpectsSomeAudio(25);
2935 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2936 }
deadbeef4e2deab2017-09-20 13:56:21 -07002937
2938 report = callee()->NewGetStats();
2939 ASSERT_NE(nullptr, report);
2940 track_stats = report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
2941 ASSERT_EQ(1U, track_stats.size());
2942 ASSERT_TRUE(track_stats[0]->total_samples_received.is_defined());
2943 // The "total samples received" stat should only be greater than it was
2944 // before.
2945 // TODO(deadbeef): Uncomment this assertion once the bug is completely fixed.
2946 // Right now, the new SSRC will cause the counters to reset to 0.
2947 // EXPECT_GT(*track_stats[0]->total_samples_received, prev_samples_received);
2948
2949 // Additionally, the percentage of concealed samples (samples generated to
Steve Anton83119dd2017-11-10 16:19:52 -08002950 // conceal packet loss) should be less than 50%. If it's greater, that's a
deadbeef4e2deab2017-09-20 13:56:21 -07002951 // good sign that we're seeing stats from the old stream that's no longer
2952 // receiving packets, and is generating concealed samples of silence.
Steve Anton83119dd2017-11-10 16:19:52 -08002953 constexpr double kAcceptableConcealedSamplesPercentage = 0.50;
deadbeef4e2deab2017-09-20 13:56:21 -07002954 ASSERT_TRUE(track_stats[0]->concealed_samples.is_defined());
2955 EXPECT_LT(*track_stats[0]->concealed_samples,
2956 *track_stats[0]->total_samples_received *
2957 kAcceptableConcealedSamplesPercentage);
2958
2959 // Also ensure that we have two "RTCInboundRTPStreamStats" as expected, as a
2960 // sanity check that the SSRC really changed.
2961 // TODO(deadbeef): This isn't working right now, because we're not returning
2962 // *any* stats for the inactive stream. Uncomment when the bug is completely
2963 // fixed.
2964 // auto inbound_stream_stats =
2965 // report->GetStatsOfType<webrtc::RTCInboundRTPStreamStats>();
2966 // ASSERT_EQ(2U, inbound_stream_stats.size());
2967}
2968
deadbeef1dcb1642017-03-29 21:08:16 -07002969// Test that DTLS 1.0 is used if both sides only support DTLS 1.0.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002970TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithDtls10) {
deadbeef1dcb1642017-03-29 21:08:16 -07002971 PeerConnectionFactory::Options dtls_10_options;
2972 dtls_10_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
2973 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(dtls_10_options,
2974 dtls_10_options));
2975 ConnectFakeSignaling();
2976 // Do normal offer/answer and wait for some frames to be received in each
2977 // direction.
Steve Anton15324772018-01-16 10:26:49 -08002978 caller()->AddAudioVideoTracks();
2979 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002980 caller()->CreateAndSetAndSignalOffer();
2981 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002982 MediaExpectations media_expectations;
2983 media_expectations.ExpectBidirectionalAudioAndVideo();
2984 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002985}
2986
2987// Test getting cipher stats and UMA metrics when DTLS 1.0 is negotiated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002988TEST_P(PeerConnectionIntegrationTest, Dtls10CipherStatsAndUmaMetrics) {
deadbeef1dcb1642017-03-29 21:08:16 -07002989 PeerConnectionFactory::Options dtls_10_options;
2990 dtls_10_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
2991 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(dtls_10_options,
2992 dtls_10_options));
2993 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002994 caller()->AddAudioVideoTracks();
2995 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002996 caller()->CreateAndSetAndSignalOffer();
Qingsi Wang7fc821d2018-07-12 12:54:53 -07002997 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07002998 EXPECT_TRUE_WAIT(rtc::SSLStreamAdapter::IsAcceptableCipher(
deadbeefd8ad7882017-04-18 16:01:17 -07002999 caller()->OldGetStats()->DtlsCipher(), rtc::KT_DEFAULT),
deadbeef1dcb1642017-03-29 21:08:16 -07003000 kDefaultTimeout);
3001 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
deadbeefd8ad7882017-04-18 16:01:17 -07003002 caller()->OldGetStats()->SrtpCipher(), kDefaultTimeout);
Qingsi Wang7fc821d2018-07-12 12:54:53 -07003003 // TODO(bugs.webrtc.org/9456): Fix it.
Alex Loiko9289eda2018-11-23 16:18:59 +00003004 EXPECT_EQ(1, webrtc::metrics::NumEvents(
Qingsi Wang7fc821d2018-07-12 12:54:53 -07003005 "WebRTC.PeerConnection.SrtpCryptoSuite.Audio",
3006 kDefaultSrtpCryptoSuite));
deadbeef1dcb1642017-03-29 21:08:16 -07003007}
3008
3009// Test getting cipher stats and UMA metrics when DTLS 1.2 is negotiated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003010TEST_P(PeerConnectionIntegrationTest, Dtls12CipherStatsAndUmaMetrics) {
deadbeef1dcb1642017-03-29 21:08:16 -07003011 PeerConnectionFactory::Options dtls_12_options;
3012 dtls_12_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
3013 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(dtls_12_options,
3014 dtls_12_options));
3015 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08003016 caller()->AddAudioVideoTracks();
3017 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003018 caller()->CreateAndSetAndSignalOffer();
Qingsi Wang7fc821d2018-07-12 12:54:53 -07003019 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07003020 EXPECT_TRUE_WAIT(rtc::SSLStreamAdapter::IsAcceptableCipher(
deadbeefd8ad7882017-04-18 16:01:17 -07003021 caller()->OldGetStats()->DtlsCipher(), rtc::KT_DEFAULT),
deadbeef1dcb1642017-03-29 21:08:16 -07003022 kDefaultTimeout);
3023 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
deadbeefd8ad7882017-04-18 16:01:17 -07003024 caller()->OldGetStats()->SrtpCipher(), kDefaultTimeout);
Qingsi Wang7fc821d2018-07-12 12:54:53 -07003025 // TODO(bugs.webrtc.org/9456): Fix it.
Alex Loiko9289eda2018-11-23 16:18:59 +00003026 EXPECT_EQ(1, webrtc::metrics::NumEvents(
Qingsi Wang7fc821d2018-07-12 12:54:53 -07003027 "WebRTC.PeerConnection.SrtpCryptoSuite.Audio",
3028 kDefaultSrtpCryptoSuite));
deadbeef1dcb1642017-03-29 21:08:16 -07003029}
3030
3031// Test that DTLS 1.0 can be used if the caller supports DTLS 1.2 and the
3032// callee only supports 1.0.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003033TEST_P(PeerConnectionIntegrationTest, CallerDtls12ToCalleeDtls10) {
deadbeef1dcb1642017-03-29 21:08:16 -07003034 PeerConnectionFactory::Options caller_options;
3035 caller_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
3036 PeerConnectionFactory::Options callee_options;
3037 callee_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
3038 ASSERT_TRUE(
3039 CreatePeerConnectionWrappersWithOptions(caller_options, callee_options));
3040 ConnectFakeSignaling();
3041 // Do normal offer/answer and wait for some frames to be received in each
3042 // direction.
Steve Anton15324772018-01-16 10:26:49 -08003043 caller()->AddAudioVideoTracks();
3044 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003045 caller()->CreateAndSetAndSignalOffer();
3046 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08003047 MediaExpectations media_expectations;
3048 media_expectations.ExpectBidirectionalAudioAndVideo();
3049 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003050}
3051
3052// Test that DTLS 1.0 can be used if the caller only supports DTLS 1.0 and the
3053// callee supports 1.2.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003054TEST_P(PeerConnectionIntegrationTest, CallerDtls10ToCalleeDtls12) {
deadbeef1dcb1642017-03-29 21:08:16 -07003055 PeerConnectionFactory::Options caller_options;
3056 caller_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
3057 PeerConnectionFactory::Options callee_options;
3058 callee_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
3059 ASSERT_TRUE(
3060 CreatePeerConnectionWrappersWithOptions(caller_options, callee_options));
3061 ConnectFakeSignaling();
3062 // Do normal offer/answer and wait for some frames to be received in each
3063 // direction.
Steve Anton15324772018-01-16 10:26:49 -08003064 caller()->AddAudioVideoTracks();
3065 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003066 caller()->CreateAndSetAndSignalOffer();
3067 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08003068 MediaExpectations media_expectations;
3069 media_expectations.ExpectBidirectionalAudioAndVideo();
3070 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003071}
3072
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07003073// The three tests below verify that "enable_aes128_sha1_32_crypto_cipher"
3074// works as expected; the cipher should only be used if enabled by both sides.
3075TEST_P(PeerConnectionIntegrationTest,
3076 Aes128Sha1_32_CipherNotUsedWhenOnlyCallerSupported) {
3077 PeerConnectionFactory::Options caller_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07003078 caller_options.crypto_options.srtp.enable_aes128_sha1_32_crypto_cipher = true;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07003079 PeerConnectionFactory::Options callee_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07003080 callee_options.crypto_options.srtp.enable_aes128_sha1_32_crypto_cipher =
3081 false;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07003082 int expected_cipher_suite = rtc::SRTP_AES128_CM_SHA1_80;
3083 TestNegotiatedCipherSuite(caller_options, callee_options,
3084 expected_cipher_suite);
3085}
3086
3087TEST_P(PeerConnectionIntegrationTest,
3088 Aes128Sha1_32_CipherNotUsedWhenOnlyCalleeSupported) {
3089 PeerConnectionFactory::Options caller_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07003090 caller_options.crypto_options.srtp.enable_aes128_sha1_32_crypto_cipher =
3091 false;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07003092 PeerConnectionFactory::Options callee_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07003093 callee_options.crypto_options.srtp.enable_aes128_sha1_32_crypto_cipher = true;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07003094 int expected_cipher_suite = rtc::SRTP_AES128_CM_SHA1_80;
3095 TestNegotiatedCipherSuite(caller_options, callee_options,
3096 expected_cipher_suite);
3097}
3098
3099TEST_P(PeerConnectionIntegrationTest, Aes128Sha1_32_CipherUsedWhenSupported) {
3100 PeerConnectionFactory::Options caller_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07003101 caller_options.crypto_options.srtp.enable_aes128_sha1_32_crypto_cipher = true;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07003102 PeerConnectionFactory::Options callee_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07003103 callee_options.crypto_options.srtp.enable_aes128_sha1_32_crypto_cipher = true;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07003104 int expected_cipher_suite = rtc::SRTP_AES128_CM_SHA1_32;
3105 TestNegotiatedCipherSuite(caller_options, callee_options,
3106 expected_cipher_suite);
3107}
3108
deadbeef1dcb1642017-03-29 21:08:16 -07003109// Test that a non-GCM cipher is used if both sides only support non-GCM.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003110TEST_P(PeerConnectionIntegrationTest, NonGcmCipherUsedWhenGcmNotSupported) {
deadbeef1dcb1642017-03-29 21:08:16 -07003111 bool local_gcm_enabled = false;
3112 bool remote_gcm_enabled = false;
3113 int expected_cipher_suite = kDefaultSrtpCryptoSuite;
3114 TestGcmNegotiationUsesCipherSuite(local_gcm_enabled, remote_gcm_enabled,
3115 expected_cipher_suite);
3116}
3117
3118// Test that a GCM cipher is used if both ends support it.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003119TEST_P(PeerConnectionIntegrationTest, GcmCipherUsedWhenGcmSupported) {
deadbeef1dcb1642017-03-29 21:08:16 -07003120 bool local_gcm_enabled = true;
3121 bool remote_gcm_enabled = true;
3122 int expected_cipher_suite = kDefaultSrtpCryptoSuiteGcm;
3123 TestGcmNegotiationUsesCipherSuite(local_gcm_enabled, remote_gcm_enabled,
3124 expected_cipher_suite);
3125}
3126
3127// Test that GCM isn't used if only the offerer supports it.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003128TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07003129 NonGcmCipherUsedWhenOnlyCallerSupportsGcm) {
3130 bool local_gcm_enabled = true;
3131 bool remote_gcm_enabled = false;
3132 int expected_cipher_suite = kDefaultSrtpCryptoSuite;
3133 TestGcmNegotiationUsesCipherSuite(local_gcm_enabled, remote_gcm_enabled,
3134 expected_cipher_suite);
3135}
3136
3137// Test that GCM isn't used if only the answerer supports it.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003138TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07003139 NonGcmCipherUsedWhenOnlyCalleeSupportsGcm) {
3140 bool local_gcm_enabled = false;
3141 bool remote_gcm_enabled = true;
3142 int expected_cipher_suite = kDefaultSrtpCryptoSuite;
3143 TestGcmNegotiationUsesCipherSuite(local_gcm_enabled, remote_gcm_enabled,
3144 expected_cipher_suite);
3145}
3146
deadbeef7914b8c2017-04-21 03:23:33 -07003147// Verify that media can be transmitted end-to-end when GCM crypto suites are
3148// enabled. Note that the above tests, such as GcmCipherUsedWhenGcmSupported,
3149// only verify that a GCM cipher is negotiated, and not necessarily that SRTP
3150// works with it.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003151TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithGcmCipher) {
deadbeef7914b8c2017-04-21 03:23:33 -07003152 PeerConnectionFactory::Options gcm_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07003153 gcm_options.crypto_options.srtp.enable_gcm_crypto_suites = true;
deadbeef7914b8c2017-04-21 03:23:33 -07003154 ASSERT_TRUE(
3155 CreatePeerConnectionWrappersWithOptions(gcm_options, gcm_options));
3156 ConnectFakeSignaling();
3157 // Do normal offer/answer and wait for some frames to be received in each
3158 // direction.
Steve Anton15324772018-01-16 10:26:49 -08003159 caller()->AddAudioVideoTracks();
3160 callee()->AddAudioVideoTracks();
deadbeef7914b8c2017-04-21 03:23:33 -07003161 caller()->CreateAndSetAndSignalOffer();
3162 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08003163 MediaExpectations media_expectations;
3164 media_expectations.ExpectBidirectionalAudioAndVideo();
3165 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef7914b8c2017-04-21 03:23:33 -07003166}
3167
deadbeef1dcb1642017-03-29 21:08:16 -07003168// This test sets up a call between two parties with audio, video and an RTP
3169// data channel.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003170TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithRtpDataChannel) {
Niels Möllerf06f9232018-08-07 12:32:18 +02003171 PeerConnectionInterface::RTCConfiguration rtc_config;
3172 rtc_config.enable_rtp_data_channel = true;
3173 rtc_config.enable_dtls_srtp = false;
3174 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(rtc_config, rtc_config));
deadbeef1dcb1642017-03-29 21:08:16 -07003175 ConnectFakeSignaling();
3176 // Expect that data channel created on caller side will show up for callee as
3177 // well.
3178 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08003179 caller()->AddAudioVideoTracks();
3180 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003181 caller()->CreateAndSetAndSignalOffer();
3182 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3183 // Ensure the existence of the RTP data channel didn't impede audio/video.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003184 MediaExpectations media_expectations;
3185 media_expectations.ExpectBidirectionalAudioAndVideo();
3186 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003187 ASSERT_NE(nullptr, caller()->data_channel());
3188 ASSERT_NE(nullptr, callee()->data_channel());
3189 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3190 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3191
3192 // Ensure data can be sent in both directions.
3193 std::string data = "hello world";
3194 SendRtpDataWithRetries(caller()->data_channel(), data, 5);
3195 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3196 kDefaultTimeout);
3197 SendRtpDataWithRetries(callee()->data_channel(), data, 5);
3198 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3199 kDefaultTimeout);
3200}
3201
3202// Ensure that an RTP data channel is signaled as closed for the caller when
3203// the callee rejects it in a subsequent offer.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003204TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07003205 RtpDataChannelSignaledClosedInCalleeOffer) {
3206 // Same procedure as above test.
Niels Möllerf06f9232018-08-07 12:32:18 +02003207 PeerConnectionInterface::RTCConfiguration rtc_config;
3208 rtc_config.enable_rtp_data_channel = true;
3209 rtc_config.enable_dtls_srtp = false;
3210 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(rtc_config, rtc_config));
deadbeef1dcb1642017-03-29 21:08:16 -07003211 ConnectFakeSignaling();
3212 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08003213 caller()->AddAudioVideoTracks();
3214 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003215 caller()->CreateAndSetAndSignalOffer();
3216 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3217 ASSERT_NE(nullptr, caller()->data_channel());
3218 ASSERT_NE(nullptr, callee()->data_channel());
3219 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3220 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3221
3222 // Close the data channel on the callee, and do an updated offer/answer.
3223 callee()->data_channel()->Close();
3224 callee()->CreateAndSetAndSignalOffer();
3225 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3226 EXPECT_FALSE(caller()->data_observer()->IsOpen());
3227 EXPECT_FALSE(callee()->data_observer()->IsOpen());
3228}
3229
3230// Tests that data is buffered in an RTP data channel until an observer is
3231// registered for it.
3232//
3233// NOTE: RTP data channels can receive data before the underlying
3234// transport has detected that a channel is writable and thus data can be
3235// received before the data channel state changes to open. That is hard to test
3236// but the same buffering is expected to be used in that case.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003237TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07003238 DataBufferedUntilRtpDataChannelObserverRegistered) {
3239 // Use fake clock and simulated network delay so that we predictably can wait
3240 // until an SCTP message has been delivered without "sleep()"ing.
3241 rtc::ScopedFakeClock fake_clock;
3242 // Some things use a time of "0" as a special value, so we need to start out
3243 // the fake clock at a nonzero time.
3244 // TODO(deadbeef): Fix this.
Sebastian Jansson5f83cf02018-05-08 14:52:22 +02003245 fake_clock.AdvanceTime(webrtc::TimeDelta::seconds(1));
deadbeef1dcb1642017-03-29 21:08:16 -07003246 virtual_socket_server()->set_delay_mean(5); // 5 ms per hop.
3247 virtual_socket_server()->UpdateDelayDistribution();
3248
Niels Möllerf06f9232018-08-07 12:32:18 +02003249 PeerConnectionInterface::RTCConfiguration rtc_config;
3250 rtc_config.enable_rtp_data_channel = true;
3251 rtc_config.enable_dtls_srtp = false;
3252 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(rtc_config, rtc_config));
deadbeef1dcb1642017-03-29 21:08:16 -07003253 ConnectFakeSignaling();
3254 caller()->CreateDataChannel();
3255 caller()->CreateAndSetAndSignalOffer();
3256 ASSERT_TRUE(caller()->data_channel() != nullptr);
3257 ASSERT_TRUE_SIMULATED_WAIT(callee()->data_channel() != nullptr,
3258 kDefaultTimeout, fake_clock);
3259 ASSERT_TRUE_SIMULATED_WAIT(caller()->data_observer()->IsOpen(),
3260 kDefaultTimeout, fake_clock);
3261 ASSERT_EQ_SIMULATED_WAIT(DataChannelInterface::kOpen,
3262 callee()->data_channel()->state(), kDefaultTimeout,
3263 fake_clock);
3264
3265 // Unregister the observer which is normally automatically registered.
3266 callee()->data_channel()->UnregisterObserver();
3267 // Send data and advance fake clock until it should have been received.
3268 std::string data = "hello world";
3269 caller()->data_channel()->Send(DataBuffer(data));
3270 SIMULATED_WAIT(false, 50, fake_clock);
3271
3272 // Attach data channel and expect data to be received immediately. Note that
3273 // EXPECT_EQ_WAIT is used, such that the simulated clock is not advanced any
3274 // further, but data can be received even if the callback is asynchronous.
3275 MockDataChannelObserver new_observer(callee()->data_channel());
3276 EXPECT_EQ_SIMULATED_WAIT(data, new_observer.last_message(), kDefaultTimeout,
3277 fake_clock);
Seth Hampson1d4a76d2018-06-19 14:31:41 -07003278 // Closing the PeerConnections destroys the ports before the ScopedFakeClock.
3279 // If this is not done a DCHECK can be hit in ports.cc, because a large
3280 // negative number is calculated for the rtt due to the global clock changing.
Steve Antond91969e2019-05-30 12:27:03 -07003281 ClosePeerConnections();
deadbeef1dcb1642017-03-29 21:08:16 -07003282}
3283
3284// This test sets up a call between two parties with audio, video and but only
3285// the caller client supports RTP data channels.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003286TEST_P(PeerConnectionIntegrationTest, RtpDataChannelsRejectedByCallee) {
Niels Möllerf06f9232018-08-07 12:32:18 +02003287 PeerConnectionInterface::RTCConfiguration rtc_config_1;
3288 rtc_config_1.enable_rtp_data_channel = true;
deadbeef1dcb1642017-03-29 21:08:16 -07003289 // Must disable DTLS to make negotiation succeed.
Niels Möllerf06f9232018-08-07 12:32:18 +02003290 rtc_config_1.enable_dtls_srtp = false;
3291 PeerConnectionInterface::RTCConfiguration rtc_config_2;
3292 rtc_config_2.enable_dtls_srtp = false;
3293 rtc_config_2.enable_dtls_srtp = false;
3294 ASSERT_TRUE(
3295 CreatePeerConnectionWrappersWithConfig(rtc_config_1, rtc_config_2));
deadbeef1dcb1642017-03-29 21:08:16 -07003296 ConnectFakeSignaling();
3297 caller()->CreateDataChannel();
Harald Alvestrandf3736ed2019-04-08 13:09:30 +02003298 ASSERT_TRUE(caller()->data_channel() != nullptr);
Steve Anton15324772018-01-16 10:26:49 -08003299 caller()->AddAudioVideoTracks();
3300 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003301 caller()->CreateAndSetAndSignalOffer();
3302 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3303 // The caller should still have a data channel, but it should be closed, and
3304 // one should ever have been created for the callee.
3305 EXPECT_TRUE(caller()->data_channel() != nullptr);
3306 EXPECT_FALSE(caller()->data_observer()->IsOpen());
3307 EXPECT_EQ(nullptr, callee()->data_channel());
3308}
3309
3310// This test sets up a call between two parties with audio, and video. When
3311// audio and video is setup and flowing, an RTP data channel is negotiated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003312TEST_P(PeerConnectionIntegrationTest, AddRtpDataChannelInSubsequentOffer) {
Niels Möllerf06f9232018-08-07 12:32:18 +02003313 PeerConnectionInterface::RTCConfiguration rtc_config;
3314 rtc_config.enable_rtp_data_channel = true;
3315 rtc_config.enable_dtls_srtp = false;
3316 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(rtc_config, rtc_config));
deadbeef1dcb1642017-03-29 21:08:16 -07003317 ConnectFakeSignaling();
3318 // Do initial offer/answer with audio/video.
Steve Anton15324772018-01-16 10:26:49 -08003319 caller()->AddAudioVideoTracks();
3320 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003321 caller()->CreateAndSetAndSignalOffer();
3322 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3323 // Create data channel and do new offer and answer.
3324 caller()->CreateDataChannel();
3325 caller()->CreateAndSetAndSignalOffer();
3326 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3327 ASSERT_NE(nullptr, caller()->data_channel());
3328 ASSERT_NE(nullptr, callee()->data_channel());
3329 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3330 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3331 // Ensure data can be sent in both directions.
3332 std::string data = "hello world";
3333 SendRtpDataWithRetries(caller()->data_channel(), data, 5);
3334 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3335 kDefaultTimeout);
3336 SendRtpDataWithRetries(callee()->data_channel(), data, 5);
3337 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3338 kDefaultTimeout);
3339}
3340
3341#ifdef HAVE_SCTP
3342
3343// This test sets up a call between two parties with audio, video and an SCTP
3344// data channel.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003345TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithSctpDataChannel) {
deadbeef1dcb1642017-03-29 21:08:16 -07003346 ASSERT_TRUE(CreatePeerConnectionWrappers());
3347 ConnectFakeSignaling();
3348 // Expect that data channel created on caller side will show up for callee as
3349 // well.
3350 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08003351 caller()->AddAudioVideoTracks();
3352 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003353 caller()->CreateAndSetAndSignalOffer();
3354 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3355 // Ensure the existence of the SCTP data channel didn't impede audio/video.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003356 MediaExpectations media_expectations;
3357 media_expectations.ExpectBidirectionalAudioAndVideo();
3358 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003359 // Caller data channel should already exist (it created one). Callee data
3360 // channel may not exist yet, since negotiation happens in-band, not in SDP.
3361 ASSERT_NE(nullptr, caller()->data_channel());
3362 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3363 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3364 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3365
3366 // Ensure data can be sent in both directions.
3367 std::string data = "hello world";
3368 caller()->data_channel()->Send(DataBuffer(data));
3369 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3370 kDefaultTimeout);
3371 callee()->data_channel()->Send(DataBuffer(data));
3372 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3373 kDefaultTimeout);
3374}
3375
3376// Ensure that when the callee closes an SCTP data channel, the closing
3377// procedure results in the data channel being closed for the caller as well.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003378TEST_P(PeerConnectionIntegrationTest, CalleeClosesSctpDataChannel) {
deadbeef1dcb1642017-03-29 21:08:16 -07003379 // Same procedure as above test.
3380 ASSERT_TRUE(CreatePeerConnectionWrappers());
3381 ConnectFakeSignaling();
3382 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08003383 caller()->AddAudioVideoTracks();
3384 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003385 caller()->CreateAndSetAndSignalOffer();
3386 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3387 ASSERT_NE(nullptr, caller()->data_channel());
3388 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3389 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3390 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3391
3392 // Close the data channel on the callee side, and wait for it to reach the
3393 // "closed" state on both sides.
3394 callee()->data_channel()->Close();
3395 EXPECT_TRUE_WAIT(!caller()->data_observer()->IsOpen(), kDefaultTimeout);
3396 EXPECT_TRUE_WAIT(!callee()->data_observer()->IsOpen(), kDefaultTimeout);
3397}
3398
Seth Hampson2f0d7022018-02-20 11:54:42 -08003399TEST_P(PeerConnectionIntegrationTest, SctpDataChannelConfigSentToOtherSide) {
Steve Antonda6c0952017-10-23 11:41:54 -07003400 ASSERT_TRUE(CreatePeerConnectionWrappers());
3401 ConnectFakeSignaling();
3402 webrtc::DataChannelInit init;
3403 init.id = 53;
3404 init.maxRetransmits = 52;
3405 caller()->CreateDataChannel("data-channel", &init);
Steve Anton15324772018-01-16 10:26:49 -08003406 caller()->AddAudioVideoTracks();
3407 callee()->AddAudioVideoTracks();
Steve Antonda6c0952017-10-23 11:41:54 -07003408 caller()->CreateAndSetAndSignalOffer();
3409 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Steve Anton074dece2017-10-24 13:04:12 -07003410 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3411 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
Harald Alvestrand5c4d2ee2019-04-01 12:58:15 +02003412 // Since "negotiated" is false, the "id" parameter should be ignored.
3413 EXPECT_NE(init.id, callee()->data_channel()->id());
Steve Antonda6c0952017-10-23 11:41:54 -07003414 EXPECT_EQ("data-channel", callee()->data_channel()->label());
3415 EXPECT_EQ(init.maxRetransmits, callee()->data_channel()->maxRetransmits());
3416 EXPECT_FALSE(callee()->data_channel()->negotiated());
3417}
3418
deadbeef1dcb1642017-03-29 21:08:16 -07003419// Test usrsctp's ability to process unordered data stream, where data actually
3420// arrives out of order using simulated delays. Previously there have been some
3421// bugs in this area.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003422TEST_P(PeerConnectionIntegrationTest, StressTestUnorderedSctpDataChannel) {
deadbeef1dcb1642017-03-29 21:08:16 -07003423 // Introduce random network delays.
3424 // Otherwise it's not a true "unordered" test.
3425 virtual_socket_server()->set_delay_mean(20);
3426 virtual_socket_server()->set_delay_stddev(5);
3427 virtual_socket_server()->UpdateDelayDistribution();
3428 // Normal procedure, but with unordered data channel config.
3429 ASSERT_TRUE(CreatePeerConnectionWrappers());
3430 ConnectFakeSignaling();
3431 webrtc::DataChannelInit init;
3432 init.ordered = false;
3433 caller()->CreateDataChannel(&init);
3434 caller()->CreateAndSetAndSignalOffer();
3435 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3436 ASSERT_NE(nullptr, caller()->data_channel());
3437 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3438 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3439 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3440
3441 static constexpr int kNumMessages = 100;
3442 // Deliberately chosen to be larger than the MTU so messages get fragmented.
3443 static constexpr size_t kMaxMessageSize = 4096;
3444 // Create and send random messages.
3445 std::vector<std::string> sent_messages;
3446 for (int i = 0; i < kNumMessages; ++i) {
3447 size_t length =
3448 (rand() % kMaxMessageSize) + 1; // NOLINT (rand_r instead of rand)
3449 std::string message;
3450 ASSERT_TRUE(rtc::CreateRandomString(length, &message));
3451 caller()->data_channel()->Send(DataBuffer(message));
3452 callee()->data_channel()->Send(DataBuffer(message));
3453 sent_messages.push_back(message);
3454 }
3455
3456 // Wait for all messages to be received.
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02003457 EXPECT_EQ_WAIT(rtc::checked_cast<size_t>(kNumMessages),
deadbeef1dcb1642017-03-29 21:08:16 -07003458 caller()->data_observer()->received_message_count(),
3459 kDefaultTimeout);
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02003460 EXPECT_EQ_WAIT(rtc::checked_cast<size_t>(kNumMessages),
deadbeef1dcb1642017-03-29 21:08:16 -07003461 callee()->data_observer()->received_message_count(),
3462 kDefaultTimeout);
3463
3464 // Sort and compare to make sure none of the messages were corrupted.
3465 std::vector<std::string> caller_received_messages =
3466 caller()->data_observer()->messages();
3467 std::vector<std::string> callee_received_messages =
3468 callee()->data_observer()->messages();
Steve Anton64b626b2019-01-28 17:25:26 -08003469 absl::c_sort(sent_messages);
3470 absl::c_sort(caller_received_messages);
3471 absl::c_sort(callee_received_messages);
deadbeef1dcb1642017-03-29 21:08:16 -07003472 EXPECT_EQ(sent_messages, caller_received_messages);
3473 EXPECT_EQ(sent_messages, callee_received_messages);
3474}
3475
3476// This test sets up a call between two parties with audio, and video. When
3477// audio and video are setup and flowing, an SCTP data channel is negotiated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003478TEST_P(PeerConnectionIntegrationTest, AddSctpDataChannelInSubsequentOffer) {
deadbeef1dcb1642017-03-29 21:08:16 -07003479 ASSERT_TRUE(CreatePeerConnectionWrappers());
3480 ConnectFakeSignaling();
3481 // Do initial offer/answer with audio/video.
Steve Anton15324772018-01-16 10:26:49 -08003482 caller()->AddAudioVideoTracks();
3483 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003484 caller()->CreateAndSetAndSignalOffer();
3485 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3486 // Create data channel and do new offer and answer.
3487 caller()->CreateDataChannel();
3488 caller()->CreateAndSetAndSignalOffer();
3489 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3490 // Caller data channel should already exist (it created one). Callee data
3491 // channel may not exist yet, since negotiation happens in-band, not in SDP.
3492 ASSERT_NE(nullptr, caller()->data_channel());
3493 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3494 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3495 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3496 // Ensure data can be sent in both directions.
3497 std::string data = "hello world";
3498 caller()->data_channel()->Send(DataBuffer(data));
3499 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3500 kDefaultTimeout);
3501 callee()->data_channel()->Send(DataBuffer(data));
3502 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3503 kDefaultTimeout);
3504}
3505
deadbeef7914b8c2017-04-21 03:23:33 -07003506// Set up a connection initially just using SCTP data channels, later upgrading
3507// to audio/video, ensuring frames are received end-to-end. Effectively the
3508// inverse of the test above.
3509// This was broken in M57; see https://crbug.com/711243
Seth Hampson2f0d7022018-02-20 11:54:42 -08003510TEST_P(PeerConnectionIntegrationTest, SctpDataChannelToAudioVideoUpgrade) {
deadbeef7914b8c2017-04-21 03:23:33 -07003511 ASSERT_TRUE(CreatePeerConnectionWrappers());
3512 ConnectFakeSignaling();
3513 // Do initial offer/answer with just data channel.
3514 caller()->CreateDataChannel();
3515 caller()->CreateAndSetAndSignalOffer();
3516 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3517 // Wait until data can be sent over the data channel.
3518 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3519 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3520 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3521
3522 // Do subsequent offer/answer with two-way audio and video. Audio and video
3523 // should end up bundled on the DTLS/ICE transport already used for data.
Steve Anton15324772018-01-16 10:26:49 -08003524 caller()->AddAudioVideoTracks();
3525 callee()->AddAudioVideoTracks();
deadbeef7914b8c2017-04-21 03:23:33 -07003526 caller()->CreateAndSetAndSignalOffer();
3527 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08003528 MediaExpectations media_expectations;
3529 media_expectations.ExpectBidirectionalAudioAndVideo();
3530 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef7914b8c2017-04-21 03:23:33 -07003531}
3532
deadbeef8b7e9ad2017-05-25 09:38:55 -07003533static void MakeSpecCompliantSctpOffer(cricket::SessionDescription* desc) {
Harald Alvestrand5fc28b12019-05-13 13:36:16 +02003534 cricket::SctpDataContentDescription* dcd_offer =
3535 GetFirstSctpDataContentDescription(desc);
Steve Antonb1c1de12017-12-21 15:14:30 -08003536 ASSERT_TRUE(dcd_offer);
deadbeef8b7e9ad2017-05-25 09:38:55 -07003537 dcd_offer->set_use_sctpmap(false);
3538 dcd_offer->set_protocol("UDP/DTLS/SCTP");
3539}
3540
3541// Test that the data channel works when a spec-compliant SCTP m= section is
3542// offered (using "a=sctp-port" instead of "a=sctpmap", and using
3543// "UDP/DTLS/SCTP" as the protocol).
Seth Hampson2f0d7022018-02-20 11:54:42 -08003544TEST_P(PeerConnectionIntegrationTest,
deadbeef8b7e9ad2017-05-25 09:38:55 -07003545 DataChannelWorksWhenSpecCompliantSctpOfferReceived) {
3546 ASSERT_TRUE(CreatePeerConnectionWrappers());
3547 ConnectFakeSignaling();
3548 caller()->CreateDataChannel();
3549 caller()->SetGeneratedSdpMunger(MakeSpecCompliantSctpOffer);
3550 caller()->CreateAndSetAndSignalOffer();
3551 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3552 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3553 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3554 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3555
3556 // Ensure data can be sent in both directions.
3557 std::string data = "hello world";
3558 caller()->data_channel()->Send(DataBuffer(data));
3559 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3560 kDefaultTimeout);
3561 callee()->data_channel()->Send(DataBuffer(data));
3562 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3563 kDefaultTimeout);
3564}
3565
Bjorn A Mellemb689af42019-08-21 10:44:59 -07003566// Tests that the datagram transport to SCTP fallback works correctly when
3567// datagram transport negotiation fails.
3568TEST_P(PeerConnectionIntegrationTest,
3569 DatagramTransportDataChannelFallbackToSctp) {
3570 PeerConnectionInterface::RTCConfiguration rtc_config;
3571 rtc_config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyRequire;
3572 rtc_config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
3573 rtc_config.use_datagram_transport_for_data_channels = true;
3574
3575 // Configure one endpoint to use datagram transport for data channels while
3576 // the other does not.
3577 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
3578 rtc_config, RTCConfiguration(),
3579 loopback_media_transports()->first_factory(), nullptr));
3580 ConnectFakeSignaling();
3581
3582 // The caller offers a data channel using either datagram transport or SCTP.
3583 caller()->CreateDataChannel();
3584 caller()->AddAudioVideoTracks();
3585 callee()->AddAudioVideoTracks();
3586 caller()->CreateAndSetAndSignalOffer();
3587 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3588
3589 // Negotiation should fallback to SCTP, allowing the data channel to be
3590 // established.
3591 ASSERT_NE(nullptr, caller()->data_channel());
3592 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3593 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3594 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3595
3596 // Ensure data can be sent in both directions.
3597 std::string data = "hello world";
3598 caller()->data_channel()->Send(DataBuffer(data));
3599 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3600 kDefaultTimeout);
3601 callee()->data_channel()->Send(DataBuffer(data));
3602 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3603 kDefaultTimeout);
3604
3605 // Ensure that failure of the datagram negotiation doesn't impede media flow.
3606 MediaExpectations media_expectations;
3607 media_expectations.ExpectBidirectionalAudioAndVideo();
3608 ASSERT_TRUE(ExpectNewFrames(media_expectations));
3609}
3610
Bjorn A Mellemfc604aa2019-09-24 14:59:21 -07003611// Tests that the data channel transport works correctly when datagram transport
3612// negotiation succeeds and does not fall back to SCTP.
3613TEST_P(PeerConnectionIntegrationTest,
3614 DatagramTransportDataChannelDoesNotFallbackToSctp) {
3615 PeerConnectionInterface::RTCConfiguration rtc_config;
3616 rtc_config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyRequire;
3617 rtc_config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
3618 rtc_config.use_datagram_transport_for_data_channels = true;
3619
3620 // Configure one endpoint to use datagram transport for data channels while
3621 // the other does not.
3622 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
3623 rtc_config, rtc_config, loopback_media_transports()->first_factory(),
3624 loopback_media_transports()->second_factory()));
3625 ConnectFakeSignaling();
3626
3627 // The caller offers a data channel using either datagram transport or SCTP.
3628 caller()->CreateDataChannel();
3629 caller()->AddAudioVideoTracks();
3630 callee()->AddAudioVideoTracks();
3631 caller()->CreateAndSetAndSignalOffer();
3632 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3633
3634 // Ensure that the data channel transport is ready.
3635 loopback_media_transports()->SetState(webrtc::MediaTransportState::kWritable);
3636 loopback_media_transports()->FlushAsyncInvokes();
3637
3638 // Negotiation should succeed, allowing the data channel to be established.
3639 ASSERT_NE(nullptr, caller()->data_channel());
3640 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3641 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3642 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3643
3644 // Ensure data can be sent in both directions.
3645 std::string data = "hello world";
3646 caller()->data_channel()->Send(DataBuffer(data));
3647 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3648 kDefaultTimeout);
3649 callee()->data_channel()->Send(DataBuffer(data));
3650 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3651 kDefaultTimeout);
3652
3653 // Ensure that failure of the datagram negotiation doesn't impede media flow.
3654 MediaExpectations media_expectations;
3655 media_expectations.ExpectBidirectionalAudioAndVideo();
3656 ASSERT_TRUE(ExpectNewFrames(media_expectations));
3657}
3658
Bjorn A Mellem8e1343a2019-09-30 15:12:47 -07003659TEST_P(PeerConnectionIntegrationTest,
3660 DatagramTransportDataChannelWithMediaOnCaller) {
3661 // Configure the caller to attempt use of datagram transport for media and
3662 // data channels.
3663 PeerConnectionInterface::RTCConfiguration offerer_config;
3664 offerer_config.rtcp_mux_policy =
3665 PeerConnectionInterface::kRtcpMuxPolicyRequire;
3666 offerer_config.bundle_policy =
3667 PeerConnectionInterface::kBundlePolicyMaxBundle;
3668 offerer_config.use_datagram_transport_for_data_channels = true;
3669 offerer_config.use_datagram_transport = true;
3670
3671 // Configure the callee to only use datagram transport for data channels.
3672 PeerConnectionInterface::RTCConfiguration answerer_config;
3673 answerer_config.rtcp_mux_policy =
3674 PeerConnectionInterface::kRtcpMuxPolicyRequire;
3675 answerer_config.bundle_policy =
3676 PeerConnectionInterface::kBundlePolicyMaxBundle;
3677 answerer_config.use_datagram_transport_for_data_channels = true;
3678
3679 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
3680 offerer_config, answerer_config,
3681 loopback_media_transports()->first_factory(),
3682 loopback_media_transports()->second_factory()));
3683 ConnectFakeSignaling();
3684
3685 // Offer both media and data.
3686 caller()->AddAudioVideoTracks();
3687 callee()->AddAudioVideoTracks();
3688 caller()->CreateDataChannel();
3689 caller()->CreateAndSetAndSignalOffer();
3690 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3691
3692 // Ensure that the data channel transport is ready.
3693 loopback_media_transports()->SetState(webrtc::MediaTransportState::kWritable);
3694 loopback_media_transports()->FlushAsyncInvokes();
3695
3696 ASSERT_NE(nullptr, caller()->data_channel());
3697 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3698 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3699 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3700
3701 // Both endpoints should agree to use datagram transport for data channels.
3702 EXPECT_EQ(nullptr, caller()->pc()->GetSctpTransport());
3703 EXPECT_EQ(nullptr, callee()->pc()->GetSctpTransport());
3704
3705 // Ensure data can be sent in both directions.
3706 std::string data = "hello world";
3707 caller()->data_channel()->Send(DataBuffer(data));
3708 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3709 kDefaultTimeout);
3710 callee()->data_channel()->Send(DataBuffer(data));
3711 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3712 kDefaultTimeout);
3713
3714 // Media flow should not be impacted.
3715 MediaExpectations media_expectations;
3716 media_expectations.ExpectBidirectionalAudioAndVideo();
3717 ASSERT_TRUE(ExpectNewFrames(media_expectations));
3718}
3719
3720TEST_P(PeerConnectionIntegrationTest,
3721 DatagramTransportMediaWithDataChannelOnCaller) {
3722 // Configure the caller to attempt use of datagram transport for media and
3723 // data channels.
3724 PeerConnectionInterface::RTCConfiguration offerer_config;
3725 offerer_config.rtcp_mux_policy =
3726 PeerConnectionInterface::kRtcpMuxPolicyRequire;
3727 offerer_config.bundle_policy =
3728 PeerConnectionInterface::kBundlePolicyMaxBundle;
3729 offerer_config.use_datagram_transport_for_data_channels = true;
3730 offerer_config.use_datagram_transport = true;
3731
3732 // Configure the callee to only use datagram transport for media.
3733 PeerConnectionInterface::RTCConfiguration answerer_config;
3734 answerer_config.rtcp_mux_policy =
3735 PeerConnectionInterface::kRtcpMuxPolicyRequire;
3736 answerer_config.bundle_policy =
3737 PeerConnectionInterface::kBundlePolicyMaxBundle;
3738 answerer_config.use_datagram_transport = true;
3739
3740 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
3741 offerer_config, answerer_config,
3742 loopback_media_transports()->first_factory(),
3743 loopback_media_transports()->second_factory()));
3744 ConnectFakeSignaling();
3745
3746 // Offer both media and data.
3747 caller()->AddAudioVideoTracks();
3748 callee()->AddAudioVideoTracks();
3749 caller()->CreateDataChannel();
3750 caller()->CreateAndSetAndSignalOffer();
3751 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3752
3753 // Ensure that the data channel transport is ready.
3754 loopback_media_transports()->SetState(webrtc::MediaTransportState::kWritable);
3755 loopback_media_transports()->FlushAsyncInvokes();
3756
3757 ASSERT_NE(nullptr, caller()->data_channel());
3758 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3759 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3760 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3761
3762 // Both endpoints should agree to use SCTP for data channels.
3763 EXPECT_NE(nullptr, caller()->pc()->GetSctpTransport());
3764 EXPECT_NE(nullptr, callee()->pc()->GetSctpTransport());
3765
3766 // Ensure data can be sent in both directions.
3767 std::string data = "hello world";
3768 caller()->data_channel()->Send(DataBuffer(data));
3769 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3770 kDefaultTimeout);
3771 callee()->data_channel()->Send(DataBuffer(data));
3772 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3773 kDefaultTimeout);
3774
3775 // Media flow should not be impacted.
3776 MediaExpectations media_expectations;
3777 media_expectations.ExpectBidirectionalAudioAndVideo();
3778 ASSERT_TRUE(ExpectNewFrames(media_expectations));
3779}
3780
3781TEST_P(PeerConnectionIntegrationTest,
3782 DatagramTransportDataChannelWithMediaOnCallee) {
3783 // Configure the caller to attempt use of datagram transport for data
3784 // channels.
3785 PeerConnectionInterface::RTCConfiguration offerer_config;
3786 offerer_config.rtcp_mux_policy =
3787 PeerConnectionInterface::kRtcpMuxPolicyRequire;
3788 offerer_config.bundle_policy =
3789 PeerConnectionInterface::kBundlePolicyMaxBundle;
3790 offerer_config.use_datagram_transport_for_data_channels = true;
3791
3792 // Configure the callee to use datagram transport for data channels and media.
3793 PeerConnectionInterface::RTCConfiguration answerer_config;
3794 answerer_config.rtcp_mux_policy =
3795 PeerConnectionInterface::kRtcpMuxPolicyRequire;
3796 answerer_config.bundle_policy =
3797 PeerConnectionInterface::kBundlePolicyMaxBundle;
3798 answerer_config.use_datagram_transport_for_data_channels = true;
3799 answerer_config.use_datagram_transport = true;
3800
3801 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
3802 offerer_config, answerer_config,
3803 loopback_media_transports()->first_factory(),
3804 loopback_media_transports()->second_factory()));
3805 ConnectFakeSignaling();
3806
3807 // Offer both media and data.
3808 caller()->AddAudioVideoTracks();
3809 callee()->AddAudioVideoTracks();
3810 caller()->CreateDataChannel();
3811 caller()->CreateAndSetAndSignalOffer();
3812 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3813
3814 // Ensure that the data channel transport is ready.
3815 loopback_media_transports()->SetState(webrtc::MediaTransportState::kWritable);
3816 loopback_media_transports()->FlushAsyncInvokes();
3817
3818 ASSERT_NE(nullptr, caller()->data_channel());
3819 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3820 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3821 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3822
3823 // Both endpoints should agree to use datagram transport for data channels.
3824 EXPECT_EQ(nullptr, caller()->pc()->GetSctpTransport());
3825 EXPECT_EQ(nullptr, callee()->pc()->GetSctpTransport());
3826
3827 // Ensure data can be sent in both directions.
3828 std::string data = "hello world";
3829 caller()->data_channel()->Send(DataBuffer(data));
3830 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3831 kDefaultTimeout);
3832 callee()->data_channel()->Send(DataBuffer(data));
3833 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3834 kDefaultTimeout);
3835
3836 // Media flow should not be impacted.
3837 MediaExpectations media_expectations;
3838 media_expectations.ExpectBidirectionalAudioAndVideo();
3839 ASSERT_TRUE(ExpectNewFrames(media_expectations));
3840}
3841
3842TEST_P(PeerConnectionIntegrationTest,
3843 DatagramTransportMediaWithDataChannelOnCallee) {
3844 // Configure the caller to attempt use of datagram transport for media.
3845 PeerConnectionInterface::RTCConfiguration offerer_config;
3846 offerer_config.rtcp_mux_policy =
3847 PeerConnectionInterface::kRtcpMuxPolicyRequire;
3848 offerer_config.bundle_policy =
3849 PeerConnectionInterface::kBundlePolicyMaxBundle;
3850 offerer_config.use_datagram_transport = true;
3851
3852 // Configure the callee to only use datagram transport for media and data
3853 // channels.
3854 PeerConnectionInterface::RTCConfiguration answerer_config;
3855 answerer_config.rtcp_mux_policy =
3856 PeerConnectionInterface::kRtcpMuxPolicyRequire;
3857 answerer_config.bundle_policy =
3858 PeerConnectionInterface::kBundlePolicyMaxBundle;
3859 answerer_config.use_datagram_transport = true;
3860 answerer_config.use_datagram_transport_for_data_channels = true;
3861
3862 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
3863 offerer_config, answerer_config,
3864 loopback_media_transports()->first_factory(),
3865 loopback_media_transports()->second_factory()));
3866 ConnectFakeSignaling();
3867
3868 // Offer both media and data.
3869 caller()->AddAudioVideoTracks();
3870 callee()->AddAudioVideoTracks();
3871 caller()->CreateDataChannel();
3872 caller()->CreateAndSetAndSignalOffer();
3873 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3874
3875 // Ensure that the data channel transport is ready.
3876 loopback_media_transports()->SetState(webrtc::MediaTransportState::kWritable);
3877 loopback_media_transports()->FlushAsyncInvokes();
3878
3879 ASSERT_NE(nullptr, caller()->data_channel());
3880 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3881 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3882 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3883
3884 // Both endpoints should agree to use SCTP for data channels.
3885 EXPECT_NE(nullptr, caller()->pc()->GetSctpTransport());
3886 EXPECT_NE(nullptr, callee()->pc()->GetSctpTransport());
3887
3888 // Ensure data can be sent in both directions.
3889 std::string data = "hello world";
3890 caller()->data_channel()->Send(DataBuffer(data));
3891 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3892 kDefaultTimeout);
3893 callee()->data_channel()->Send(DataBuffer(data));
3894 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3895 kDefaultTimeout);
3896
3897 // Media flow should not be impacted.
3898 MediaExpectations media_expectations;
3899 media_expectations.ExpectBidirectionalAudioAndVideo();
3900 ASSERT_TRUE(ExpectNewFrames(media_expectations));
3901}
3902
3903TEST_P(PeerConnectionIntegrationTest, DatagramTransportDataChannelAndMedia) {
3904 // Configure the caller to use datagram transport for data channels and media.
3905 PeerConnectionInterface::RTCConfiguration offerer_config;
3906 offerer_config.rtcp_mux_policy =
3907 PeerConnectionInterface::kRtcpMuxPolicyRequire;
3908 offerer_config.bundle_policy =
3909 PeerConnectionInterface::kBundlePolicyMaxBundle;
3910 offerer_config.use_datagram_transport_for_data_channels = true;
3911 offerer_config.use_datagram_transport = true;
3912
3913 // Configure the callee to use datagram transport for data channels and media.
3914 PeerConnectionInterface::RTCConfiguration answerer_config;
3915 answerer_config.rtcp_mux_policy =
3916 PeerConnectionInterface::kRtcpMuxPolicyRequire;
3917 answerer_config.bundle_policy =
3918 PeerConnectionInterface::kBundlePolicyMaxBundle;
3919 answerer_config.use_datagram_transport_for_data_channels = true;
3920 answerer_config.use_datagram_transport = true;
3921
3922 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
3923 offerer_config, answerer_config,
3924 loopback_media_transports()->first_factory(),
3925 loopback_media_transports()->second_factory()));
3926 ConnectFakeSignaling();
3927
3928 // Offer both media and data.
3929 caller()->AddAudioVideoTracks();
3930 callee()->AddAudioVideoTracks();
3931 caller()->CreateDataChannel();
3932 caller()->CreateAndSetAndSignalOffer();
3933 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3934
3935 // Ensure that the data channel transport is ready.
3936 loopback_media_transports()->SetState(webrtc::MediaTransportState::kWritable);
3937 loopback_media_transports()->FlushAsyncInvokes();
3938
3939 ASSERT_NE(nullptr, caller()->data_channel());
3940 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3941 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3942 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3943
3944 // Both endpoints should agree to use datagram transport for data channels.
3945 EXPECT_EQ(nullptr, caller()->pc()->GetSctpTransport());
3946 EXPECT_EQ(nullptr, callee()->pc()->GetSctpTransport());
3947
3948 // Ensure data can be sent in both directions.
3949 std::string data = "hello world";
3950 caller()->data_channel()->Send(DataBuffer(data));
3951 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3952 kDefaultTimeout);
3953 callee()->data_channel()->Send(DataBuffer(data));
3954 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3955 kDefaultTimeout);
3956
3957 // Media flow should not be impacted.
3958 MediaExpectations media_expectations;
3959 media_expectations.ExpectBidirectionalAudioAndVideo();
3960 ASSERT_TRUE(ExpectNewFrames(media_expectations));
3961}
3962
Bjorn A Mellem7da4e562019-09-26 11:02:11 -07003963// Tests that data channels use SCTP instead of datagram transport if datagram
3964// transport is configured in receive-only mode on the caller.
3965TEST_P(PeerConnectionIntegrationTest,
3966 DatagramTransportDataChannelReceiveOnlyOnCallerUsesSctp) {
3967 PeerConnectionInterface::RTCConfiguration rtc_config;
3968 rtc_config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyRequire;
3969 rtc_config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
3970 rtc_config.use_datagram_transport_for_data_channels = true;
3971 rtc_config.use_datagram_transport_for_data_channels_receive_only = true;
3972
3973 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
3974 rtc_config, rtc_config, loopback_media_transports()->first_factory(),
3975 loopback_media_transports()->second_factory()));
3976 ConnectFakeSignaling();
3977
3978 // The caller should offer a data channel using SCTP.
3979 caller()->CreateDataChannel();
3980 caller()->AddAudioVideoTracks();
3981 callee()->AddAudioVideoTracks();
3982 caller()->CreateAndSetAndSignalOffer();
3983 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3984
3985 ASSERT_NE(nullptr, caller()->data_channel());
3986 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3987 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3988 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3989
3990 // SCTP transports should be present, since they are in use.
3991 EXPECT_NE(caller()->pc()->GetSctpTransport(), nullptr);
3992 EXPECT_NE(callee()->pc()->GetSctpTransport(), nullptr);
3993
3994 // Ensure data can be sent in both directions.
3995 std::string data = "hello world";
3996 caller()->data_channel()->Send(DataBuffer(data));
3997 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3998 kDefaultTimeout);
3999 callee()->data_channel()->Send(DataBuffer(data));
4000 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
4001 kDefaultTimeout);
4002}
4003
deadbeef1dcb1642017-03-29 21:08:16 -07004004#endif // HAVE_SCTP
4005
Bjorn A Mellem7da4e562019-09-26 11:02:11 -07004006// Tests that a callee configured for receive-only use of datagram transport
4007// data channels accepts them on incoming calls.
4008TEST_P(PeerConnectionIntegrationTest,
4009 DatagramTransportDataChannelReceiveOnlyOnCallee) {
4010 PeerConnectionInterface::RTCConfiguration offerer_config;
4011 offerer_config.rtcp_mux_policy =
4012 PeerConnectionInterface::kRtcpMuxPolicyRequire;
4013 offerer_config.bundle_policy =
4014 PeerConnectionInterface::kBundlePolicyMaxBundle;
4015 offerer_config.use_datagram_transport_for_data_channels = true;
4016
4017 PeerConnectionInterface::RTCConfiguration answerer_config;
4018 answerer_config.rtcp_mux_policy =
4019 PeerConnectionInterface::kRtcpMuxPolicyRequire;
4020 answerer_config.bundle_policy =
4021 PeerConnectionInterface::kBundlePolicyMaxBundle;
4022 answerer_config.use_datagram_transport_for_data_channels = true;
4023 answerer_config.use_datagram_transport_for_data_channels_receive_only = true;
4024
4025 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
4026 offerer_config, answerer_config,
4027 loopback_media_transports()->first_factory(),
4028 loopback_media_transports()->second_factory()));
4029 ConnectFakeSignaling();
4030
4031 caller()->CreateDataChannel();
4032 caller()->CreateAndSetAndSignalOffer();
4033 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4034
4035 // Ensure that the data channel transport is ready.
4036 loopback_media_transports()->SetState(webrtc::MediaTransportState::kWritable);
4037 loopback_media_transports()->FlushAsyncInvokes();
4038
4039 ASSERT_NE(nullptr, caller()->data_channel());
4040 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
4041 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
4042 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
4043
4044 // SCTP transports should not be present, since datagram transport is used.
4045 EXPECT_EQ(caller()->pc()->GetSctpTransport(), nullptr);
4046 EXPECT_EQ(callee()->pc()->GetSctpTransport(), nullptr);
4047
4048 // Ensure data can be sent in both directions.
4049 std::string data = "hello world";
4050 caller()->data_channel()->Send(DataBuffer(data));
4051 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
4052 kDefaultTimeout);
4053 callee()->data_channel()->Send(DataBuffer(data));
4054 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
4055 kDefaultTimeout);
4056}
4057
Bjorn A Mellemb689af42019-08-21 10:44:59 -07004058// This test sets up a call between two parties with a datagram transport data
4059// channel.
4060TEST_P(PeerConnectionIntegrationTest, DatagramTransportDataChannelEndToEnd) {
4061 PeerConnectionInterface::RTCConfiguration rtc_config;
4062 rtc_config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyRequire;
4063 rtc_config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
4064 rtc_config.use_datagram_transport_for_data_channels = true;
4065 rtc_config.enable_dtls_srtp = false;
4066 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
4067 rtc_config, rtc_config, loopback_media_transports()->first_factory(),
4068 loopback_media_transports()->second_factory()));
4069 ConnectFakeSignaling();
4070
4071 // Expect that data channel created on caller side will show up for callee as
4072 // well.
4073 caller()->CreateDataChannel();
4074 caller()->CreateAndSetAndSignalOffer();
4075 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4076
Bjorn A Mellemfc604aa2019-09-24 14:59:21 -07004077 // Ensure that the data channel transport is ready.
Bjorn A Mellemb689af42019-08-21 10:44:59 -07004078 loopback_media_transports()->SetState(webrtc::MediaTransportState::kWritable);
4079 loopback_media_transports()->FlushAsyncInvokes();
4080
4081 // Caller data channel should already exist (it created one). Callee data
4082 // channel may not exist yet, since negotiation happens in-band, not in SDP.
4083 ASSERT_NE(nullptr, caller()->data_channel());
4084 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
4085 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
4086 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
4087
4088 // Ensure data can be sent in both directions.
4089 std::string data = "hello world";
4090 caller()->data_channel()->Send(DataBuffer(data));
4091 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
4092 kDefaultTimeout);
4093 callee()->data_channel()->Send(DataBuffer(data));
4094 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
4095 kDefaultTimeout);
4096}
4097
Bjorn A Mellembc3eebc2019-09-23 14:53:54 -07004098// Tests that 'zero-rtt' data channel transports (which are ready-to-send as
4099// soon as they're created) work correctly.
4100TEST_P(PeerConnectionIntegrationTest, DatagramTransportDataChannelZeroRtt) {
4101 PeerConnectionInterface::RTCConfiguration rtc_config;
4102 rtc_config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyRequire;
4103 rtc_config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
4104 rtc_config.use_datagram_transport_for_data_channels = true;
4105 rtc_config.enable_dtls_srtp = false; // SDES is required for media transport.
4106 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
4107 rtc_config, rtc_config, loopback_media_transports()->first_factory(),
4108 loopback_media_transports()->second_factory()));
4109 ConnectFakeSignaling();
4110
4111 // Ensure that the callee's media transport is ready-to-send immediately.
4112 // Note that only the callee can become writable in zero RTTs. The caller
4113 // must wait for the callee's answer.
4114 loopback_media_transports()->SetSecondStateAfterConnect(
4115 webrtc::MediaTransportState::kWritable);
4116 loopback_media_transports()->FlushAsyncInvokes();
4117
4118 // Expect that data channel created on caller side will show up for callee as
4119 // well.
4120 caller()->CreateDataChannel();
4121 caller()->CreateAndSetAndSignalOffer();
4122 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4123
4124 loopback_media_transports()->SetFirstState(
4125 webrtc::MediaTransportState::kWritable);
4126 loopback_media_transports()->FlushAsyncInvokes();
4127
4128 // Caller data channel should already exist (it created one). Callee data
4129 // channel may not exist yet, since negotiation happens in-band, not in SDP.
4130 ASSERT_NE(nullptr, caller()->data_channel());
4131 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
4132 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
4133 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
4134
4135 // Ensure data can be sent in both directions.
4136 std::string data = "hello world";
4137 caller()->data_channel()->Send(DataBuffer(data));
4138 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
4139 kDefaultTimeout);
4140 callee()->data_channel()->Send(DataBuffer(data));
4141 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
4142 kDefaultTimeout);
4143}
4144
Bjorn A Mellemb689af42019-08-21 10:44:59 -07004145// Ensures that when the callee closes a datagram transport data channel, the
4146// closing procedure results in the data channel being closed for the caller
4147// as well.
4148TEST_P(PeerConnectionIntegrationTest,
4149 DatagramTransportDataChannelCalleeCloses) {
4150 PeerConnectionInterface::RTCConfiguration rtc_config;
4151 rtc_config.use_datagram_transport_for_data_channels = true;
4152 rtc_config.enable_dtls_srtp = false;
4153 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
4154 rtc_config, rtc_config, loopback_media_transports()->first_factory(),
4155 loopback_media_transports()->second_factory()));
4156 ConnectFakeSignaling();
4157
4158 // Create a data channel on the caller and signal it to the callee.
4159 caller()->CreateDataChannel();
4160 caller()->CreateAndSetAndSignalOffer();
4161 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4162
Bjorn A Mellemfc604aa2019-09-24 14:59:21 -07004163 // Ensure that the data channel transport is ready.
Bjorn A Mellemb689af42019-08-21 10:44:59 -07004164 loopback_media_transports()->SetState(webrtc::MediaTransportState::kWritable);
4165 loopback_media_transports()->FlushAsyncInvokes();
4166
4167 // Data channels exist and open on both ends of the connection.
4168 ASSERT_NE(nullptr, caller()->data_channel());
4169 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
4170 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
4171 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
4172
4173 // Close the data channel on the callee side, and wait for it to reach the
4174 // "closed" state on both sides.
4175 callee()->data_channel()->Close();
4176 EXPECT_TRUE_WAIT(!caller()->data_observer()->IsOpen(), kDefaultTimeout);
4177 EXPECT_TRUE_WAIT(!callee()->data_observer()->IsOpen(), kDefaultTimeout);
4178}
4179
4180// Tests that datagram transport data channels can do in-band negotiation.
4181TEST_P(PeerConnectionIntegrationTest,
4182 DatagramTransportDataChannelConfigSentToOtherSide) {
4183 PeerConnectionInterface::RTCConfiguration rtc_config;
4184 rtc_config.use_datagram_transport_for_data_channels = true;
4185 rtc_config.enable_dtls_srtp = false;
4186 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
4187 rtc_config, rtc_config, loopback_media_transports()->first_factory(),
4188 loopback_media_transports()->second_factory()));
4189 ConnectFakeSignaling();
4190
4191 // Create a data channel with a non-default configuration and signal it to the
4192 // callee.
4193 webrtc::DataChannelInit init;
4194 init.id = 53;
4195 init.maxRetransmits = 52;
4196 caller()->CreateDataChannel("data-channel", &init);
4197 caller()->CreateAndSetAndSignalOffer();
4198 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4199
Bjorn A Mellemfc604aa2019-09-24 14:59:21 -07004200 // Ensure that the data channel transport is ready.
Bjorn A Mellemb689af42019-08-21 10:44:59 -07004201 loopback_media_transports()->SetState(webrtc::MediaTransportState::kWritable);
4202 loopback_media_transports()->FlushAsyncInvokes();
4203
4204 // Ensure that the data channel exists on the callee with the correct
4205 // configuration.
4206 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
4207 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
4208 // Since "negotiate" is false, the "id" parameter is ignored.
4209 EXPECT_NE(init.id, callee()->data_channel()->id());
4210 EXPECT_EQ("data-channel", callee()->data_channel()->label());
4211 EXPECT_EQ(init.maxRetransmits, callee()->data_channel()->maxRetransmits());
4212 EXPECT_FALSE(callee()->data_channel()->negotiated());
4213}
4214
4215TEST_P(PeerConnectionIntegrationTest,
4216 DatagramTransportDataChannelRejectedWithNoFallback) {
4217 PeerConnectionInterface::RTCConfiguration offerer_config;
4218 offerer_config.rtcp_mux_policy =
4219 PeerConnectionInterface::kRtcpMuxPolicyRequire;
4220 offerer_config.bundle_policy =
4221 PeerConnectionInterface::kBundlePolicyMaxBundle;
4222 offerer_config.use_datagram_transport_for_data_channels = true;
4223 // Disabling DTLS precludes a fallback to SCTP.
4224 offerer_config.enable_dtls_srtp = false;
4225
4226 PeerConnectionInterface::RTCConfiguration answerer_config;
4227 answerer_config.rtcp_mux_policy =
4228 PeerConnectionInterface::kRtcpMuxPolicyRequire;
4229 answerer_config.bundle_policy =
4230 PeerConnectionInterface::kBundlePolicyMaxBundle;
4231 // Both endpoints must disable DTLS or SetRemoteDescription will fail.
4232 answerer_config.enable_dtls_srtp = false;
4233
4234 // Configure one endpoint to use datagram transport for data channels while
4235 // the other does not.
4236 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
4237 offerer_config, answerer_config,
4238 loopback_media_transports()->first_factory(), nullptr));
4239 ConnectFakeSignaling();
4240
4241 // The caller offers a data channel using either datagram transport or SCTP.
4242 caller()->CreateDataChannel();
4243 caller()->AddAudioVideoTracks();
4244 callee()->AddAudioVideoTracks();
4245 caller()->CreateAndSetAndSignalOffer();
4246 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4247
4248 // Caller data channel should already exist (it created one). Callee data
4249 // channel should not exist, since negotiation happens in-band, not in SDP.
4250 EXPECT_NE(nullptr, caller()->data_channel());
4251 EXPECT_EQ(nullptr, callee()->data_channel());
4252
4253 // The caller's data channel should close when the datagram transport is
4254 // rejected.
4255 EXPECT_FALSE(caller()->data_observer()->IsOpen());
4256
4257 // Media flow should not be impacted by the failed data channel.
4258 MediaExpectations media_expectations;
4259 media_expectations.ExpectBidirectionalAudioAndVideo();
4260 ASSERT_TRUE(ExpectNewFrames(media_expectations));
4261}
4262
Bjorn Mellema2eb0a72018-11-09 10:13:51 -08004263// This test sets up a call between two parties with a media transport data
4264// channel.
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08004265TEST_P(PeerConnectionIntegrationTest, MediaTransportDataChannelEndToEnd) {
4266 PeerConnectionInterface::RTCConfiguration rtc_config;
Piotr (Peter) Slatalab1ae10b2019-03-01 11:14:05 -08004267 rtc_config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyRequire;
4268 rtc_config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08004269 rtc_config.use_media_transport_for_data_channels = true;
4270 rtc_config.enable_dtls_srtp = false; // SDES is required for media transport.
4271 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
4272 rtc_config, rtc_config, loopback_media_transports()->first_factory(),
4273 loopback_media_transports()->second_factory()));
4274 ConnectFakeSignaling();
4275
4276 // Expect that data channel created on caller side will show up for callee as
4277 // well.
4278 caller()->CreateDataChannel();
4279 caller()->CreateAndSetAndSignalOffer();
4280 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4281
4282 // Ensure that the media transport is ready.
4283 loopback_media_transports()->SetState(webrtc::MediaTransportState::kWritable);
4284 loopback_media_transports()->FlushAsyncInvokes();
4285
4286 // Caller data channel should already exist (it created one). Callee data
4287 // channel may not exist yet, since negotiation happens in-band, not in SDP.
4288 ASSERT_NE(nullptr, caller()->data_channel());
4289 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
4290 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
4291 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
4292
4293 // Ensure data can be sent in both directions.
4294 std::string data = "hello world";
4295 caller()->data_channel()->Send(DataBuffer(data));
4296 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
4297 kDefaultTimeout);
4298 callee()->data_channel()->Send(DataBuffer(data));
4299 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
4300 kDefaultTimeout);
4301}
4302
Bjorn A Mellembc3eebc2019-09-23 14:53:54 -07004303// Tests that 'zero-rtt' data channel transports (which are ready-to-send as
4304// soon as they're created) work correctly.
4305TEST_P(PeerConnectionIntegrationTest, MediaTransportDataChannelZeroRtt) {
4306 PeerConnectionInterface::RTCConfiguration rtc_config;
4307 rtc_config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyRequire;
4308 rtc_config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
4309 rtc_config.use_media_transport_for_data_channels = true;
4310 rtc_config.enable_dtls_srtp = false; // SDES is required for media transport.
4311 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
4312 rtc_config, rtc_config, loopback_media_transports()->first_factory(),
4313 loopback_media_transports()->second_factory()));
4314 ConnectFakeSignaling();
4315
4316 // Ensure that the callee's media transport is ready-to-send immediately.
4317 // Note that only the callee can become writable in zero RTTs. The caller
4318 // must wait for the callee's answer.
4319 loopback_media_transports()->SetSecondStateAfterConnect(
4320 webrtc::MediaTransportState::kWritable);
4321 loopback_media_transports()->FlushAsyncInvokes();
4322
4323 // Expect that data channel created on caller side will show up for callee as
4324 // well.
4325 caller()->CreateDataChannel();
4326 caller()->CreateAndSetAndSignalOffer();
4327 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4328
4329 loopback_media_transports()->SetFirstState(
4330 webrtc::MediaTransportState::kWritable);
4331 loopback_media_transports()->FlushAsyncInvokes();
4332
4333 // Caller data channel should already exist (it created one). Callee data
4334 // channel may not exist yet, since negotiation happens in-band, not in SDP.
4335 ASSERT_NE(nullptr, caller()->data_channel());
4336 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
4337 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
4338 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
4339
4340 // Ensure data can be sent in both directions.
4341 std::string data = "hello world";
4342 caller()->data_channel()->Send(DataBuffer(data));
4343 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
4344 kDefaultTimeout);
4345 callee()->data_channel()->Send(DataBuffer(data));
4346 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
4347 kDefaultTimeout);
4348}
4349
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08004350// Ensure that when the callee closes a media transport data channel, the
4351// closing procedure results in the data channel being closed for the caller
4352// as well.
4353TEST_P(PeerConnectionIntegrationTest, MediaTransportDataChannelCalleeCloses) {
4354 PeerConnectionInterface::RTCConfiguration rtc_config;
4355 rtc_config.use_media_transport_for_data_channels = true;
4356 rtc_config.enable_dtls_srtp = false; // SDES is required for media transport.
4357 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
4358 rtc_config, rtc_config, loopback_media_transports()->first_factory(),
4359 loopback_media_transports()->second_factory()));
4360 ConnectFakeSignaling();
4361
4362 // Create a data channel on the caller and signal it to the callee.
4363 caller()->CreateDataChannel();
4364 caller()->CreateAndSetAndSignalOffer();
4365 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4366
4367 // Ensure that the media transport is ready.
4368 loopback_media_transports()->SetState(webrtc::MediaTransportState::kWritable);
4369 loopback_media_transports()->FlushAsyncInvokes();
4370
4371 // Data channels exist and open on both ends of the connection.
4372 ASSERT_NE(nullptr, caller()->data_channel());
4373 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
4374 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
4375 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
4376
4377 // Close the data channel on the callee side, and wait for it to reach the
4378 // "closed" state on both sides.
4379 callee()->data_channel()->Close();
4380 EXPECT_TRUE_WAIT(!caller()->data_observer()->IsOpen(), kDefaultTimeout);
4381 EXPECT_TRUE_WAIT(!callee()->data_observer()->IsOpen(), kDefaultTimeout);
4382}
4383
4384TEST_P(PeerConnectionIntegrationTest,
4385 MediaTransportDataChannelConfigSentToOtherSide) {
4386 PeerConnectionInterface::RTCConfiguration rtc_config;
4387 rtc_config.use_media_transport_for_data_channels = true;
4388 rtc_config.enable_dtls_srtp = false; // SDES is required for media transport.
4389 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
4390 rtc_config, rtc_config, loopback_media_transports()->first_factory(),
4391 loopback_media_transports()->second_factory()));
4392 ConnectFakeSignaling();
4393
4394 // Create a data channel with a non-default configuration and signal it to the
4395 // callee.
4396 webrtc::DataChannelInit init;
4397 init.id = 53;
4398 init.maxRetransmits = 52;
4399 caller()->CreateDataChannel("data-channel", &init);
4400 caller()->CreateAndSetAndSignalOffer();
4401 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4402
4403 // Ensure that the media transport is ready.
4404 loopback_media_transports()->SetState(webrtc::MediaTransportState::kWritable);
4405 loopback_media_transports()->FlushAsyncInvokes();
4406
4407 // Ensure that the data channel exists on the callee with the correct
4408 // configuration.
4409 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
4410 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
Harald Alvestrand5c4d2ee2019-04-01 12:58:15 +02004411 // Since "negotiate" is false, the "id" parameter is ignored.
4412 EXPECT_NE(init.id, callee()->data_channel()->id());
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08004413 EXPECT_EQ("data-channel", callee()->data_channel()->label());
4414 EXPECT_EQ(init.maxRetransmits, callee()->data_channel()->maxRetransmits());
4415 EXPECT_FALSE(callee()->data_channel()->negotiated());
4416}
4417
Piotr (Peter) Slatalab1ae10b2019-03-01 11:14:05 -08004418TEST_P(PeerConnectionIntegrationTest, MediaTransportOfferUpgrade) {
4419 PeerConnectionInterface::RTCConfiguration rtc_config;
4420 rtc_config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyRequire;
4421 rtc_config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
4422 rtc_config.use_media_transport = true;
4423 rtc_config.enable_dtls_srtp = false; // SDES is required for media transport.
4424 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
4425 rtc_config, rtc_config, loopback_media_transports()->first_factory(),
4426 loopback_media_transports()->second_factory()));
4427 ConnectFakeSignaling();
4428
4429 // Do initial offer/answer with just a video track.
4430 caller()->AddVideoTrack();
4431 callee()->AddVideoTrack();
4432 caller()->CreateAndSetAndSignalOffer();
4433 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4434
4435 // Ensure that the media transport is ready.
4436 loopback_media_transports()->SetState(webrtc::MediaTransportState::kWritable);
4437 loopback_media_transports()->FlushAsyncInvokes();
4438
4439 // Now add an audio track and do another offer/answer.
4440 caller()->AddAudioTrack();
4441 callee()->AddAudioTrack();
4442 caller()->CreateAndSetAndSignalOffer();
4443 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4444
4445 // Ensure both audio and video frames are received end-to-end.
4446 MediaExpectations media_expectations;
4447 media_expectations.ExpectBidirectionalAudioAndVideo();
4448 ASSERT_TRUE(ExpectNewFrames(media_expectations));
4449
4450 // The second offer should not have generated another media transport.
4451 // Media transport was kept alive, and was not recreated.
4452 EXPECT_EQ(1, loopback_media_transports()->first_factory_transport_count());
4453 EXPECT_EQ(1, loopback_media_transports()->second_factory_transport_count());
4454}
4455
4456TEST_P(PeerConnectionIntegrationTest, MediaTransportOfferUpgradeOnTheCallee) {
4457 PeerConnectionInterface::RTCConfiguration rtc_config;
4458 rtc_config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyRequire;
4459 rtc_config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
4460 rtc_config.use_media_transport = true;
4461 rtc_config.enable_dtls_srtp = false; // SDES is required for media transport.
4462 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
4463 rtc_config, rtc_config, loopback_media_transports()->first_factory(),
4464 loopback_media_transports()->second_factory()));
4465 ConnectFakeSignaling();
4466
4467 // Do initial offer/answer with just a video track.
4468 caller()->AddVideoTrack();
4469 callee()->AddVideoTrack();
4470 caller()->CreateAndSetAndSignalOffer();
4471 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4472
4473 // Ensure that the media transport is ready.
4474 loopback_media_transports()->SetState(webrtc::MediaTransportState::kWritable);
4475 loopback_media_transports()->FlushAsyncInvokes();
4476
4477 // Now add an audio track and do another offer/answer.
4478 caller()->AddAudioTrack();
4479 callee()->AddAudioTrack();
4480 callee()->CreateAndSetAndSignalOffer();
4481 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4482
4483 // Ensure both audio and video frames are received end-to-end.
4484 MediaExpectations media_expectations;
4485 media_expectations.ExpectBidirectionalAudioAndVideo();
4486 ASSERT_TRUE(ExpectNewFrames(media_expectations));
4487
4488 // The second offer should not have generated another media transport.
4489 // Media transport was kept alive, and was not recreated.
4490 EXPECT_EQ(1, loopback_media_transports()->first_factory_transport_count());
4491 EXPECT_EQ(1, loopback_media_transports()->second_factory_transport_count());
4492}
4493
Niels Möllerc68d2822018-11-20 14:52:05 +01004494TEST_P(PeerConnectionIntegrationTest, MediaTransportBidirectionalAudio) {
4495 PeerConnectionInterface::RTCConfiguration rtc_config;
Piotr (Peter) Slatalab1ae10b2019-03-01 11:14:05 -08004496 rtc_config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyRequire;
4497 rtc_config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
Niels Möllerc68d2822018-11-20 14:52:05 +01004498 rtc_config.use_media_transport = true;
4499 rtc_config.enable_dtls_srtp = false; // SDES is required for media transport.
4500 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
4501 rtc_config, rtc_config, loopback_media_transports()->first_factory(),
4502 loopback_media_transports()->second_factory()));
4503 ConnectFakeSignaling();
4504
4505 caller()->AddAudioTrack();
4506 callee()->AddAudioTrack();
4507 // Start offer/answer exchange and wait for it to complete.
4508 caller()->CreateAndSetAndSignalOffer();
4509 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4510
4511 // Ensure that the media transport is ready.
4512 loopback_media_transports()->SetState(webrtc::MediaTransportState::kWritable);
4513 loopback_media_transports()->FlushAsyncInvokes();
4514
4515 MediaExpectations media_expectations;
4516 media_expectations.ExpectBidirectionalAudio();
4517 ASSERT_TRUE(ExpectNewFrames(media_expectations));
4518
4519 webrtc::MediaTransportPair::Stats first_stats =
4520 loopback_media_transports()->FirstStats();
4521 webrtc::MediaTransportPair::Stats second_stats =
4522 loopback_media_transports()->SecondStats();
4523
4524 EXPECT_GT(first_stats.received_audio_frames, 0);
4525 EXPECT_GE(second_stats.sent_audio_frames, first_stats.received_audio_frames);
4526
4527 EXPECT_GT(second_stats.received_audio_frames, 0);
4528 EXPECT_GE(first_stats.sent_audio_frames, second_stats.received_audio_frames);
4529}
4530
Niels Möller46879152019-01-07 15:54:47 +01004531TEST_P(PeerConnectionIntegrationTest, MediaTransportBidirectionalVideo) {
4532 PeerConnectionInterface::RTCConfiguration rtc_config;
4533 rtc_config.use_media_transport = true;
4534 rtc_config.enable_dtls_srtp = false; // SDES is required for media transport.
4535 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
4536 rtc_config, rtc_config, loopback_media_transports()->first_factory(),
4537 loopback_media_transports()->second_factory()));
4538 ConnectFakeSignaling();
4539
4540 caller()->AddVideoTrack();
4541 callee()->AddVideoTrack();
4542 // Start offer/answer exchange and wait for it to complete.
4543 caller()->CreateAndSetAndSignalOffer();
4544 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4545
4546 // Ensure that the media transport is ready.
4547 loopback_media_transports()->SetState(webrtc::MediaTransportState::kWritable);
4548 loopback_media_transports()->FlushAsyncInvokes();
4549
4550 MediaExpectations media_expectations;
4551 media_expectations.ExpectBidirectionalVideo();
4552 ASSERT_TRUE(ExpectNewFrames(media_expectations));
4553
4554 webrtc::MediaTransportPair::Stats first_stats =
4555 loopback_media_transports()->FirstStats();
4556 webrtc::MediaTransportPair::Stats second_stats =
4557 loopback_media_transports()->SecondStats();
4558
4559 EXPECT_GT(first_stats.received_video_frames, 0);
4560 EXPECT_GE(second_stats.sent_video_frames, first_stats.received_video_frames);
4561
4562 EXPECT_GT(second_stats.received_video_frames, 0);
4563 EXPECT_GE(first_stats.sent_video_frames, second_stats.received_video_frames);
4564}
4565
Piotr (Peter) Slatala55b91b92019-01-25 13:31:15 -08004566TEST_P(PeerConnectionIntegrationTest,
4567 MediaTransportDataChannelUsesRtpBidirectionalVideo) {
4568 PeerConnectionInterface::RTCConfiguration rtc_config;
4569 rtc_config.use_media_transport = false;
4570 rtc_config.use_media_transport_for_data_channels = true;
4571 rtc_config.enable_dtls_srtp = false; // SDES is required for media transport.
4572 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
4573 rtc_config, rtc_config, loopback_media_transports()->first_factory(),
4574 loopback_media_transports()->second_factory()));
4575 ConnectFakeSignaling();
4576
4577 caller()->AddVideoTrack();
4578 callee()->AddVideoTrack();
4579 // Start offer/answer exchange and wait for it to complete.
4580 caller()->CreateAndSetAndSignalOffer();
4581 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4582
4583 MediaExpectations media_expectations;
4584 media_expectations.ExpectBidirectionalVideo();
4585 ASSERT_TRUE(ExpectNewFrames(media_expectations));
4586}
4587
deadbeef1dcb1642017-03-29 21:08:16 -07004588// Test that the ICE connection and gathering states eventually reach
4589// "complete".
Seth Hampson2f0d7022018-02-20 11:54:42 -08004590TEST_P(PeerConnectionIntegrationTest, IceStatesReachCompletion) {
deadbeef1dcb1642017-03-29 21:08:16 -07004591 ASSERT_TRUE(CreatePeerConnectionWrappers());
4592 ConnectFakeSignaling();
4593 // Do normal offer/answer.
Steve Anton15324772018-01-16 10:26:49 -08004594 caller()->AddAudioVideoTracks();
4595 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07004596 caller()->CreateAndSetAndSignalOffer();
4597 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4598 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceGatheringComplete,
4599 caller()->ice_gathering_state(), kMaxWaitForFramesMs);
4600 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceGatheringComplete,
4601 callee()->ice_gathering_state(), kMaxWaitForFramesMs);
4602 // After the best candidate pair is selected and all candidates are signaled,
4603 // the ICE connection state should reach "complete".
4604 // TODO(deadbeef): Currently, the ICE "controlled" agent (the
4605 // answerer/"callee" by default) only reaches "connected". When this is
4606 // fixed, this test should be updated.
4607 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
4608 caller()->ice_connection_state(), kDefaultTimeout);
Alex Loiko9289eda2018-11-23 16:18:59 +00004609 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
4610 callee()->ice_connection_state(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07004611}
4612
Qingsi Wang1dac6d82018-12-12 15:28:47 -08004613constexpr int kOnlyLocalPorts = cricket::PORTALLOCATOR_DISABLE_STUN |
4614 cricket::PORTALLOCATOR_DISABLE_RELAY |
4615 cricket::PORTALLOCATOR_DISABLE_TCP;
Zach Stein6fcdc2f2018-08-23 16:25:55 -07004616
Qingsi Wang1dac6d82018-12-12 15:28:47 -08004617// Use a mock resolver to resolve the hostname back to the original IP on both
4618// sides and check that the ICE connection connects.
Zach Stein6fcdc2f2018-08-23 16:25:55 -07004619TEST_P(PeerConnectionIntegrationTest,
4620 IceStatesReachCompletionWithRemoteHostname) {
Qingsi Wang1dac6d82018-12-12 15:28:47 -08004621 auto caller_resolver_factory =
Mirko Bonadei317a1f02019-09-17 17:06:18 +02004622 std::make_unique<NiceMock<webrtc::MockAsyncResolverFactory>>();
Qingsi Wang1dac6d82018-12-12 15:28:47 -08004623 auto callee_resolver_factory =
Mirko Bonadei317a1f02019-09-17 17:06:18 +02004624 std::make_unique<NiceMock<webrtc::MockAsyncResolverFactory>>();
Qingsi Wang1dac6d82018-12-12 15:28:47 -08004625 NiceMock<rtc::MockAsyncResolver> callee_async_resolver;
4626 NiceMock<rtc::MockAsyncResolver> caller_async_resolver;
Zach Stein6fcdc2f2018-08-23 16:25:55 -07004627
4628 // This also verifies that the injected AsyncResolverFactory is used by
4629 // P2PTransportChannel.
Qingsi Wang1dac6d82018-12-12 15:28:47 -08004630 EXPECT_CALL(*caller_resolver_factory, Create())
4631 .WillOnce(Return(&caller_async_resolver));
4632 webrtc::PeerConnectionDependencies caller_deps(nullptr);
4633 caller_deps.async_resolver_factory = std::move(caller_resolver_factory);
4634
4635 EXPECT_CALL(*callee_resolver_factory, Create())
4636 .WillOnce(Return(&callee_async_resolver));
4637 webrtc::PeerConnectionDependencies callee_deps(nullptr);
4638 callee_deps.async_resolver_factory = std::move(callee_resolver_factory);
4639
4640 PeerConnectionInterface::RTCConfiguration config;
4641 config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
4642 config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyRequire;
4643
4644 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndDeps(
4645 config, std::move(caller_deps), config, std::move(callee_deps)));
4646
4647 caller()->SetRemoteAsyncResolver(&callee_async_resolver);
4648 callee()->SetRemoteAsyncResolver(&caller_async_resolver);
4649
4650 // Enable hostname candidates with mDNS names.
Qingsi Wangecd30542019-05-22 14:34:56 -07004651 caller()->SetMdnsResponder(
Mirko Bonadei317a1f02019-09-17 17:06:18 +02004652 std::make_unique<webrtc::FakeMdnsResponder>(network_thread()));
Qingsi Wangecd30542019-05-22 14:34:56 -07004653 callee()->SetMdnsResponder(
Mirko Bonadei317a1f02019-09-17 17:06:18 +02004654 std::make_unique<webrtc::FakeMdnsResponder>(network_thread()));
Qingsi Wang1dac6d82018-12-12 15:28:47 -08004655
4656 SetPortAllocatorFlags(kOnlyLocalPorts, kOnlyLocalPorts);
Zach Stein6fcdc2f2018-08-23 16:25:55 -07004657
4658 ConnectFakeSignaling();
4659 caller()->AddAudioVideoTracks();
4660 callee()->AddAudioVideoTracks();
4661 caller()->CreateAndSetAndSignalOffer();
4662 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4663 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
4664 caller()->ice_connection_state(), kDefaultTimeout);
4665 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
4666 callee()->ice_connection_state(), kDefaultTimeout);
Jeroen de Borst833979f2018-12-13 08:25:54 -08004667
4668 EXPECT_EQ(1, webrtc::metrics::NumEvents(
4669 "WebRTC.PeerConnection.CandidatePairType_UDP",
4670 webrtc::kIceCandidatePairHostNameHostName));
Zach Stein6fcdc2f2018-08-23 16:25:55 -07004671}
4672
Steve Antonede9ca52017-10-16 13:04:27 -07004673// Test that firewalling the ICE connection causes the clients to identify the
4674// disconnected state and then removing the firewall causes them to reconnect.
4675class PeerConnectionIntegrationIceStatesTest
Seth Hampson2f0d7022018-02-20 11:54:42 -08004676 : public PeerConnectionIntegrationBaseTest,
4677 public ::testing::WithParamInterface<
4678 std::tuple<SdpSemantics, std::tuple<std::string, uint32_t>>> {
Steve Antonede9ca52017-10-16 13:04:27 -07004679 protected:
Seth Hampson2f0d7022018-02-20 11:54:42 -08004680 PeerConnectionIntegrationIceStatesTest()
4681 : PeerConnectionIntegrationBaseTest(std::get<0>(GetParam())) {
4682 port_allocator_flags_ = std::get<1>(std::get<1>(GetParam()));
Steve Antonede9ca52017-10-16 13:04:27 -07004683 }
4684
4685 void StartStunServer(const SocketAddress& server_address) {
4686 stun_server_.reset(
4687 cricket::TestStunServer::Create(network_thread(), server_address));
4688 }
4689
4690 bool TestIPv6() {
4691 return (port_allocator_flags_ & cricket::PORTALLOCATOR_ENABLE_IPV6);
4692 }
4693
4694 void SetPortAllocatorFlags() {
Qingsi Wang1dac6d82018-12-12 15:28:47 -08004695 PeerConnectionIntegrationBaseTest::SetPortAllocatorFlags(
4696 port_allocator_flags_, port_allocator_flags_);
Steve Antonede9ca52017-10-16 13:04:27 -07004697 }
4698
4699 std::vector<SocketAddress> CallerAddresses() {
4700 std::vector<SocketAddress> addresses;
4701 addresses.push_back(SocketAddress("1.1.1.1", 0));
4702 if (TestIPv6()) {
4703 addresses.push_back(SocketAddress("1111:0:a:b:c:d:e:f", 0));
4704 }
4705 return addresses;
4706 }
4707
4708 std::vector<SocketAddress> CalleeAddresses() {
4709 std::vector<SocketAddress> addresses;
4710 addresses.push_back(SocketAddress("2.2.2.2", 0));
4711 if (TestIPv6()) {
4712 addresses.push_back(SocketAddress("2222:0:a:b:c:d:e:f", 0));
4713 }
4714 return addresses;
4715 }
4716
4717 void SetUpNetworkInterfaces() {
4718 // Remove the default interfaces added by the test infrastructure.
Qingsi Wangecd30542019-05-22 14:34:56 -07004719 caller()->network_manager()->RemoveInterface(kDefaultLocalAddress);
4720 callee()->network_manager()->RemoveInterface(kDefaultLocalAddress);
Steve Antonede9ca52017-10-16 13:04:27 -07004721
4722 // Add network addresses for test.
4723 for (const auto& caller_address : CallerAddresses()) {
Qingsi Wangecd30542019-05-22 14:34:56 -07004724 caller()->network_manager()->AddInterface(caller_address);
Steve Antonede9ca52017-10-16 13:04:27 -07004725 }
4726 for (const auto& callee_address : CalleeAddresses()) {
Qingsi Wangecd30542019-05-22 14:34:56 -07004727 callee()->network_manager()->AddInterface(callee_address);
Steve Antonede9ca52017-10-16 13:04:27 -07004728 }
4729 }
4730
4731 private:
4732 uint32_t port_allocator_flags_;
4733 std::unique_ptr<cricket::TestStunServer> stun_server_;
4734};
4735
4736// Tests that the PeerConnection goes through all the ICE gathering/connection
4737// states over the duration of the call. This includes Disconnected and Failed
4738// states, induced by putting a firewall between the peers and waiting for them
4739// to time out.
Steve Anton83119dd2017-11-10 16:19:52 -08004740TEST_P(PeerConnectionIntegrationIceStatesTest, VerifyIceStates) {
Jonas Olssonb75d9e92019-02-22 10:33:29 +01004741 rtc::ScopedFakeClock fake_clock;
4742 // Some things use a time of "0" as a special value, so we need to start out
4743 // the fake clock at a nonzero time.
4744 fake_clock.AdvanceTime(TimeDelta::seconds(1));
Steve Antonede9ca52017-10-16 13:04:27 -07004745
4746 const SocketAddress kStunServerAddress =
4747 SocketAddress("99.99.99.1", cricket::STUN_SERVER_PORT);
4748 StartStunServer(kStunServerAddress);
4749
4750 PeerConnectionInterface::RTCConfiguration config;
4751 PeerConnectionInterface::IceServer ice_stun_server;
4752 ice_stun_server.urls.push_back(
4753 "stun:" + kStunServerAddress.HostAsURIString() + ":" +
4754 kStunServerAddress.PortAsString());
4755 config.servers.push_back(ice_stun_server);
4756
4757 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
4758 ConnectFakeSignaling();
4759 SetPortAllocatorFlags();
4760 SetUpNetworkInterfaces();
Steve Anton15324772018-01-16 10:26:49 -08004761 caller()->AddAudioVideoTracks();
4762 callee()->AddAudioVideoTracks();
Steve Antonede9ca52017-10-16 13:04:27 -07004763
4764 // Initial state before anything happens.
4765 ASSERT_EQ(PeerConnectionInterface::kIceGatheringNew,
4766 caller()->ice_gathering_state());
4767 ASSERT_EQ(PeerConnectionInterface::kIceConnectionNew,
4768 caller()->ice_connection_state());
Jonas Olsson7a6739e2019-01-15 16:31:55 +01004769 ASSERT_EQ(PeerConnectionInterface::kIceConnectionNew,
4770 caller()->standardized_ice_connection_state());
Steve Antonede9ca52017-10-16 13:04:27 -07004771
4772 // Start the call by creating the offer, setting it as the local description,
4773 // then sending it to the peer who will respond with an answer. This happens
4774 // asynchronously so that we can watch the states as it runs in the
4775 // background.
4776 caller()->CreateAndSetAndSignalOffer();
4777
Jonas Olsson7a6739e2019-01-15 16:31:55 +01004778 ASSERT_EQ(PeerConnectionInterface::kIceConnectionCompleted,
4779 caller()->ice_connection_state());
Jonas Olssonacd8ae72019-02-25 15:26:24 +01004780 ASSERT_EQ(PeerConnectionInterface::kIceConnectionCompleted,
Jonas Olsson7a6739e2019-01-15 16:31:55 +01004781 caller()->standardized_ice_connection_state());
Steve Antonede9ca52017-10-16 13:04:27 -07004782
4783 // Verify that the observer was notified of the intermediate transitions.
4784 EXPECT_THAT(caller()->ice_connection_state_history(),
4785 ElementsAre(PeerConnectionInterface::kIceConnectionChecking,
4786 PeerConnectionInterface::kIceConnectionConnected,
4787 PeerConnectionInterface::kIceConnectionCompleted));
Jonas Olssonacd8ae72019-02-25 15:26:24 +01004788 EXPECT_THAT(caller()->standardized_ice_connection_state_history(),
4789 ElementsAre(PeerConnectionInterface::kIceConnectionChecking,
4790 PeerConnectionInterface::kIceConnectionConnected,
4791 PeerConnectionInterface::kIceConnectionCompleted));
Jonas Olsson635474e2018-10-18 15:58:17 +02004792 EXPECT_THAT(
4793 caller()->peer_connection_state_history(),
4794 ElementsAre(PeerConnectionInterface::PeerConnectionState::kConnecting,
Jonas Olsson635474e2018-10-18 15:58:17 +02004795 PeerConnectionInterface::PeerConnectionState::kConnected));
Steve Antonede9ca52017-10-16 13:04:27 -07004796 EXPECT_THAT(caller()->ice_gathering_state_history(),
4797 ElementsAre(PeerConnectionInterface::kIceGatheringGathering,
4798 PeerConnectionInterface::kIceGatheringComplete));
4799
4800 // Block connections to/from the caller and wait for ICE to become
4801 // disconnected.
4802 for (const auto& caller_address : CallerAddresses()) {
4803 firewall()->AddRule(false, rtc::FP_ANY, rtc::FD_ANY, caller_address);
4804 }
Mirko Bonadei675513b2017-11-09 11:09:25 +01004805 RTC_LOG(LS_INFO) << "Firewall rules applied";
Jonas Olssonb75d9e92019-02-22 10:33:29 +01004806 ASSERT_EQ_SIMULATED_WAIT(PeerConnectionInterface::kIceConnectionDisconnected,
4807 caller()->ice_connection_state(), kDefaultTimeout,
4808 fake_clock);
4809 ASSERT_EQ_SIMULATED_WAIT(PeerConnectionInterface::kIceConnectionDisconnected,
4810 caller()->standardized_ice_connection_state(),
4811 kDefaultTimeout, fake_clock);
Steve Antonede9ca52017-10-16 13:04:27 -07004812
4813 // Let ICE re-establish by removing the firewall rules.
4814 firewall()->ClearRules();
Mirko Bonadei675513b2017-11-09 11:09:25 +01004815 RTC_LOG(LS_INFO) << "Firewall rules cleared";
Jonas Olssonb75d9e92019-02-22 10:33:29 +01004816 ASSERT_EQ_SIMULATED_WAIT(PeerConnectionInterface::kIceConnectionCompleted,
4817 caller()->ice_connection_state(), kDefaultTimeout,
4818 fake_clock);
Jonas Olssonacd8ae72019-02-25 15:26:24 +01004819 ASSERT_EQ_SIMULATED_WAIT(PeerConnectionInterface::kIceConnectionCompleted,
Jonas Olssonb75d9e92019-02-22 10:33:29 +01004820 caller()->standardized_ice_connection_state(),
4821 kDefaultTimeout, fake_clock);
Steve Antonede9ca52017-10-16 13:04:27 -07004822
4823 // According to RFC7675, if there is no response within 30 seconds then the
4824 // peer should consider the other side to have rejected the connection. This
Steve Anton83119dd2017-11-10 16:19:52 -08004825 // is signaled by the state transitioning to "failed".
Steve Antonede9ca52017-10-16 13:04:27 -07004826 constexpr int kConsentTimeout = 30000;
4827 for (const auto& caller_address : CallerAddresses()) {
4828 firewall()->AddRule(false, rtc::FP_ANY, rtc::FD_ANY, caller_address);
4829 }
Mirko Bonadei675513b2017-11-09 11:09:25 +01004830 RTC_LOG(LS_INFO) << "Firewall rules applied again";
Jonas Olssonb75d9e92019-02-22 10:33:29 +01004831 ASSERT_EQ_SIMULATED_WAIT(PeerConnectionInterface::kIceConnectionFailed,
4832 caller()->ice_connection_state(), kConsentTimeout,
4833 fake_clock);
4834 ASSERT_EQ_SIMULATED_WAIT(PeerConnectionInterface::kIceConnectionFailed,
4835 caller()->standardized_ice_connection_state(),
4836 kConsentTimeout, fake_clock);
4837
4838 // We need to manually close the peerconnections before the fake clock goes
4839 // out of scope, or we trigger a DCHECK in rtp_sender.cc when we briefly
4840 // return to using non-faked time.
4841 delete SetCallerPcWrapperAndReturnCurrent(nullptr);
4842 delete SetCalleePcWrapperAndReturnCurrent(nullptr);
4843}
4844
4845// Tests that if the connection doesn't get set up properly we eventually reach
4846// the "failed" iceConnectionState.
4847TEST_P(PeerConnectionIntegrationIceStatesTest, IceStateSetupFailure) {
4848 rtc::ScopedFakeClock fake_clock;
4849 // Some things use a time of "0" as a special value, so we need to start out
4850 // the fake clock at a nonzero time.
4851 fake_clock.AdvanceTime(TimeDelta::seconds(1));
4852
4853 // Block connections to/from the caller and wait for ICE to become
4854 // disconnected.
4855 for (const auto& caller_address : CallerAddresses()) {
4856 firewall()->AddRule(false, rtc::FP_ANY, rtc::FD_ANY, caller_address);
4857 }
4858
4859 ASSERT_TRUE(CreatePeerConnectionWrappers());
4860 ConnectFakeSignaling();
4861 SetPortAllocatorFlags();
4862 SetUpNetworkInterfaces();
4863 caller()->AddAudioVideoTracks();
4864 caller()->CreateAndSetAndSignalOffer();
4865
4866 // According to RFC7675, if there is no response within 30 seconds then the
4867 // peer should consider the other side to have rejected the connection. This
4868 // is signaled by the state transitioning to "failed".
4869 constexpr int kConsentTimeout = 30000;
4870 ASSERT_EQ_SIMULATED_WAIT(PeerConnectionInterface::kIceConnectionFailed,
4871 caller()->standardized_ice_connection_state(),
4872 kConsentTimeout, fake_clock);
4873
4874 // We need to manually close the peerconnections before the fake clock goes
4875 // out of scope, or we trigger a DCHECK in rtp_sender.cc when we briefly
4876 // return to using non-faked time.
4877 delete SetCallerPcWrapperAndReturnCurrent(nullptr);
4878 delete SetCalleePcWrapperAndReturnCurrent(nullptr);
Steve Antonede9ca52017-10-16 13:04:27 -07004879}
4880
4881// Tests that the best connection is set to the appropriate IPv4/IPv6 connection
4882// and that the statistics in the metric observers are updated correctly.
4883TEST_P(PeerConnectionIntegrationIceStatesTest, VerifyBestConnection) {
4884 ASSERT_TRUE(CreatePeerConnectionWrappers());
4885 ConnectFakeSignaling();
4886 SetPortAllocatorFlags();
4887 SetUpNetworkInterfaces();
Steve Anton15324772018-01-16 10:26:49 -08004888 caller()->AddAudioVideoTracks();
4889 callee()->AddAudioVideoTracks();
Steve Antonede9ca52017-10-16 13:04:27 -07004890 caller()->CreateAndSetAndSignalOffer();
4891
4892 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4893
Qingsi Wang7fc821d2018-07-12 12:54:53 -07004894 // TODO(bugs.webrtc.org/9456): Fix it.
4895 const int num_best_ipv4 = webrtc::metrics::NumEvents(
4896 "WebRTC.PeerConnection.IPMetrics", webrtc::kBestConnections_IPv4);
4897 const int num_best_ipv6 = webrtc::metrics::NumEvents(
4898 "WebRTC.PeerConnection.IPMetrics", webrtc::kBestConnections_IPv6);
Steve Antonede9ca52017-10-16 13:04:27 -07004899 if (TestIPv6()) {
4900 // When IPv6 is enabled, we should prefer an IPv6 connection over an IPv4
4901 // connection.
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02004902 EXPECT_EQ(0, num_best_ipv4);
4903 EXPECT_EQ(1, num_best_ipv6);
Steve Antonede9ca52017-10-16 13:04:27 -07004904 } else {
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02004905 EXPECT_EQ(1, num_best_ipv4);
4906 EXPECT_EQ(0, num_best_ipv6);
Steve Antonede9ca52017-10-16 13:04:27 -07004907 }
4908
Qingsi Wang7fc821d2018-07-12 12:54:53 -07004909 EXPECT_EQ(0, webrtc::metrics::NumEvents(
4910 "WebRTC.PeerConnection.CandidatePairType_UDP",
4911 webrtc::kIceCandidatePairHostHost));
4912 EXPECT_EQ(1, webrtc::metrics::NumEvents(
4913 "WebRTC.PeerConnection.CandidatePairType_UDP",
4914 webrtc::kIceCandidatePairHostPublicHostPublic));
Steve Antonede9ca52017-10-16 13:04:27 -07004915}
4916
4917constexpr uint32_t kFlagsIPv4NoStun = cricket::PORTALLOCATOR_DISABLE_TCP |
4918 cricket::PORTALLOCATOR_DISABLE_STUN |
4919 cricket::PORTALLOCATOR_DISABLE_RELAY;
4920constexpr uint32_t kFlagsIPv6NoStun =
4921 cricket::PORTALLOCATOR_DISABLE_TCP | cricket::PORTALLOCATOR_DISABLE_STUN |
4922 cricket::PORTALLOCATOR_ENABLE_IPV6 | cricket::PORTALLOCATOR_DISABLE_RELAY;
4923constexpr uint32_t kFlagsIPv4Stun =
4924 cricket::PORTALLOCATOR_DISABLE_TCP | cricket::PORTALLOCATOR_DISABLE_RELAY;
4925
Mirko Bonadeic84f6612019-01-31 12:20:57 +01004926INSTANTIATE_TEST_SUITE_P(
Seth Hampson2f0d7022018-02-20 11:54:42 -08004927 PeerConnectionIntegrationTest,
4928 PeerConnectionIntegrationIceStatesTest,
4929 Combine(Values(SdpSemantics::kPlanB, SdpSemantics::kUnifiedPlan),
4930 Values(std::make_pair("IPv4 no STUN", kFlagsIPv4NoStun),
4931 std::make_pair("IPv6 no STUN", kFlagsIPv6NoStun),
4932 std::make_pair("IPv4 with STUN", kFlagsIPv4Stun))));
Steve Antonede9ca52017-10-16 13:04:27 -07004933
deadbeef1dcb1642017-03-29 21:08:16 -07004934// This test sets up a call between two parties with audio and video.
4935// During the call, the caller restarts ICE and the test verifies that
4936// new ICE candidates are generated and audio and video still can flow, and the
4937// ICE state reaches completed again.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004938TEST_P(PeerConnectionIntegrationTest, MediaContinuesFlowingAfterIceRestart) {
deadbeef1dcb1642017-03-29 21:08:16 -07004939 ASSERT_TRUE(CreatePeerConnectionWrappers());
4940 ConnectFakeSignaling();
4941 // Do normal offer/answer and wait for ICE to complete.
Steve Anton15324772018-01-16 10:26:49 -08004942 caller()->AddAudioVideoTracks();
4943 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07004944 caller()->CreateAndSetAndSignalOffer();
4945 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4946 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
4947 caller()->ice_connection_state(), kMaxWaitForFramesMs);
Alex Loiko9289eda2018-11-23 16:18:59 +00004948 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
4949 callee()->ice_connection_state(), kMaxWaitForFramesMs);
deadbeef1dcb1642017-03-29 21:08:16 -07004950
4951 // To verify that the ICE restart actually occurs, get
4952 // ufrag/password/candidates before and after restart.
4953 // Create an SDP string of the first audio candidate for both clients.
4954 const webrtc::IceCandidateCollection* audio_candidates_caller =
4955 caller()->pc()->local_description()->candidates(0);
4956 const webrtc::IceCandidateCollection* audio_candidates_callee =
4957 callee()->pc()->local_description()->candidates(0);
4958 ASSERT_GT(audio_candidates_caller->count(), 0u);
4959 ASSERT_GT(audio_candidates_callee->count(), 0u);
4960 std::string caller_candidate_pre_restart;
4961 ASSERT_TRUE(
4962 audio_candidates_caller->at(0)->ToString(&caller_candidate_pre_restart));
4963 std::string callee_candidate_pre_restart;
4964 ASSERT_TRUE(
4965 audio_candidates_callee->at(0)->ToString(&callee_candidate_pre_restart));
4966 const cricket::SessionDescription* desc =
4967 caller()->pc()->local_description()->description();
4968 std::string caller_ufrag_pre_restart =
4969 desc->transport_infos()[0].description.ice_ufrag;
4970 desc = callee()->pc()->local_description()->description();
4971 std::string callee_ufrag_pre_restart =
4972 desc->transport_infos()[0].description.ice_ufrag;
4973
Alex Drake00c7ecf2019-08-06 10:54:47 -07004974 EXPECT_EQ(caller()->ice_candidate_pair_change_history().size(), 1u);
deadbeef1dcb1642017-03-29 21:08:16 -07004975 // Have the caller initiate an ICE restart.
4976 caller()->SetOfferAnswerOptions(IceRestartOfferAnswerOptions());
4977 caller()->CreateAndSetAndSignalOffer();
4978 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4979 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
4980 caller()->ice_connection_state(), kMaxWaitForFramesMs);
Alex Loiko9289eda2018-11-23 16:18:59 +00004981 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
deadbeef1dcb1642017-03-29 21:08:16 -07004982 callee()->ice_connection_state(), kMaxWaitForFramesMs);
4983
4984 // Grab the ufrags/candidates again.
4985 audio_candidates_caller = caller()->pc()->local_description()->candidates(0);
4986 audio_candidates_callee = callee()->pc()->local_description()->candidates(0);
4987 ASSERT_GT(audio_candidates_caller->count(), 0u);
4988 ASSERT_GT(audio_candidates_callee->count(), 0u);
4989 std::string caller_candidate_post_restart;
4990 ASSERT_TRUE(
4991 audio_candidates_caller->at(0)->ToString(&caller_candidate_post_restart));
4992 std::string callee_candidate_post_restart;
4993 ASSERT_TRUE(
4994 audio_candidates_callee->at(0)->ToString(&callee_candidate_post_restart));
4995 desc = caller()->pc()->local_description()->description();
4996 std::string caller_ufrag_post_restart =
4997 desc->transport_infos()[0].description.ice_ufrag;
4998 desc = callee()->pc()->local_description()->description();
4999 std::string callee_ufrag_post_restart =
5000 desc->transport_infos()[0].description.ice_ufrag;
5001 // Sanity check that an ICE restart was actually negotiated in SDP.
5002 ASSERT_NE(caller_candidate_pre_restart, caller_candidate_post_restart);
5003 ASSERT_NE(callee_candidate_pre_restart, callee_candidate_post_restart);
5004 ASSERT_NE(caller_ufrag_pre_restart, caller_ufrag_post_restart);
5005 ASSERT_NE(callee_ufrag_pre_restart, callee_ufrag_post_restart);
Alex Drake00c7ecf2019-08-06 10:54:47 -07005006 EXPECT_GT(caller()->ice_candidate_pair_change_history().size(), 1u);
deadbeef1dcb1642017-03-29 21:08:16 -07005007
5008 // Ensure that additional frames are received after the ICE restart.
Seth Hampson2f0d7022018-02-20 11:54:42 -08005009 MediaExpectations media_expectations;
5010 media_expectations.ExpectBidirectionalAudioAndVideo();
5011 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07005012}
5013
5014// Verify that audio/video can be received end-to-end when ICE renomination is
5015// enabled.
Seth Hampson2f0d7022018-02-20 11:54:42 -08005016TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithIceRenomination) {
deadbeef1dcb1642017-03-29 21:08:16 -07005017 PeerConnectionInterface::RTCConfiguration config;
5018 config.enable_ice_renomination = true;
5019 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
5020 ConnectFakeSignaling();
5021 // Do normal offer/answer and wait for some frames to be received in each
5022 // direction.
Steve Anton15324772018-01-16 10:26:49 -08005023 caller()->AddAudioVideoTracks();
5024 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07005025 caller()->CreateAndSetAndSignalOffer();
5026 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5027 // Sanity check that ICE renomination was actually negotiated.
5028 const cricket::SessionDescription* desc =
5029 caller()->pc()->local_description()->description();
5030 for (const cricket::TransportInfo& info : desc->transport_infos()) {
Steve Anton64b626b2019-01-28 17:25:26 -08005031 ASSERT_THAT(info.description.transport_options, Contains("renomination"));
deadbeef1dcb1642017-03-29 21:08:16 -07005032 }
5033 desc = callee()->pc()->local_description()->description();
5034 for (const cricket::TransportInfo& info : desc->transport_infos()) {
Steve Anton64b626b2019-01-28 17:25:26 -08005035 ASSERT_THAT(info.description.transport_options, Contains("renomination"));
deadbeef1dcb1642017-03-29 21:08:16 -07005036 }
Seth Hampson2f0d7022018-02-20 11:54:42 -08005037 MediaExpectations media_expectations;
5038 media_expectations.ExpectBidirectionalAudioAndVideo();
5039 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07005040}
5041
Steve Anton6f25b092017-10-23 09:39:20 -07005042// With a max bundle policy and RTCP muxing, adding a new media description to
5043// the connection should not affect ICE at all because the new media will use
5044// the existing connection.
Seth Hampson2f0d7022018-02-20 11:54:42 -08005045TEST_P(PeerConnectionIntegrationTest,
Steve Anton83119dd2017-11-10 16:19:52 -08005046 AddMediaToConnectedBundleDoesNotRestartIce) {
Steve Anton6f25b092017-10-23 09:39:20 -07005047 PeerConnectionInterface::RTCConfiguration config;
5048 config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
5049 config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyRequire;
5050 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(
5051 config, PeerConnectionInterface::RTCConfiguration()));
5052 ConnectFakeSignaling();
5053
Steve Anton15324772018-01-16 10:26:49 -08005054 caller()->AddAudioTrack();
Steve Anton6f25b092017-10-23 09:39:20 -07005055 caller()->CreateAndSetAndSignalOffer();
5056 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Steve Antonff52f1b2017-10-26 12:24:50 -07005057 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceConnectionCompleted,
5058 caller()->ice_connection_state(), kDefaultTimeout);
Steve Anton6f25b092017-10-23 09:39:20 -07005059
5060 caller()->clear_ice_connection_state_history();
5061
Steve Anton15324772018-01-16 10:26:49 -08005062 caller()->AddVideoTrack();
Steve Anton6f25b092017-10-23 09:39:20 -07005063 caller()->CreateAndSetAndSignalOffer();
5064 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5065
5066 EXPECT_EQ(0u, caller()->ice_connection_state_history().size());
5067}
5068
deadbeef1dcb1642017-03-29 21:08:16 -07005069// This test sets up a call between two parties with audio and video. It then
5070// renegotiates setting the video m-line to "port 0", then later renegotiates
5071// again, enabling video.
Seth Hampson2f0d7022018-02-20 11:54:42 -08005072TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07005073 VideoFlowsAfterMediaSectionIsRejectedAndRecycled) {
5074 ASSERT_TRUE(CreatePeerConnectionWrappers());
5075 ConnectFakeSignaling();
5076
5077 // Do initial negotiation, only sending media from the caller. Will result in
5078 // video and audio recvonly "m=" sections.
Steve Anton15324772018-01-16 10:26:49 -08005079 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07005080 caller()->CreateAndSetAndSignalOffer();
5081 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5082
5083 // Negotiate again, disabling the video "m=" section (the callee will set the
5084 // port to 0 due to offer_to_receive_video = 0).
Seth Hampson2f0d7022018-02-20 11:54:42 -08005085 if (sdp_semantics_ == SdpSemantics::kPlanB) {
5086 PeerConnectionInterface::RTCOfferAnswerOptions options;
5087 options.offer_to_receive_video = 0;
5088 callee()->SetOfferAnswerOptions(options);
5089 } else {
5090 callee()->SetRemoteOfferHandler([this] {
5091 callee()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)->Stop();
5092 });
5093 }
deadbeef1dcb1642017-03-29 21:08:16 -07005094 caller()->CreateAndSetAndSignalOffer();
5095 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5096 // Sanity check that video "m=" section was actually rejected.
5097 const ContentInfo* answer_video_content = cricket::GetFirstVideoContent(
5098 callee()->pc()->local_description()->description());
5099 ASSERT_NE(nullptr, answer_video_content);
5100 ASSERT_TRUE(answer_video_content->rejected);
5101
5102 // Enable video and do negotiation again, making sure video is received
5103 // end-to-end, also adding media stream to callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08005104 if (sdp_semantics_ == SdpSemantics::kPlanB) {
5105 PeerConnectionInterface::RTCOfferAnswerOptions options;
5106 options.offer_to_receive_video = 1;
5107 callee()->SetOfferAnswerOptions(options);
5108 } else {
5109 // The caller's transceiver is stopped, so we need to add another track.
5110 auto caller_transceiver =
5111 caller()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO);
5112 EXPECT_TRUE(caller_transceiver->stopped());
5113 caller()->AddVideoTrack();
5114 }
5115 callee()->AddVideoTrack();
5116 callee()->SetRemoteOfferHandler(nullptr);
deadbeef1dcb1642017-03-29 21:08:16 -07005117 caller()->CreateAndSetAndSignalOffer();
5118 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08005119
deadbeef1dcb1642017-03-29 21:08:16 -07005120 // Verify the caller receives frames from the newly added stream, and the
5121 // callee receives additional frames from the re-enabled video m= section.
Seth Hampson2f0d7022018-02-20 11:54:42 -08005122 MediaExpectations media_expectations;
5123 media_expectations.CalleeExpectsSomeAudio();
5124 media_expectations.ExpectBidirectionalVideo();
5125 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07005126}
5127
deadbeef1dcb1642017-03-29 21:08:16 -07005128// This tests that if we negotiate after calling CreateSender but before we
5129// have a track, then set a track later, frames from the newly-set track are
5130// received end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08005131TEST_F(PeerConnectionIntegrationTestPlanB,
deadbeef1dcb1642017-03-29 21:08:16 -07005132 MediaFlowsAfterEarlyWarmupWithCreateSender) {
5133 ASSERT_TRUE(CreatePeerConnectionWrappers());
5134 ConnectFakeSignaling();
5135 auto caller_audio_sender =
5136 caller()->pc()->CreateSender("audio", "caller_stream");
5137 auto caller_video_sender =
5138 caller()->pc()->CreateSender("video", "caller_stream");
5139 auto callee_audio_sender =
5140 callee()->pc()->CreateSender("audio", "callee_stream");
5141 auto callee_video_sender =
5142 callee()->pc()->CreateSender("video", "callee_stream");
5143 caller()->CreateAndSetAndSignalOffer();
5144 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
5145 // Wait for ICE to complete, without any tracks being set.
5146 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
5147 caller()->ice_connection_state(), kMaxWaitForFramesMs);
5148 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
5149 callee()->ice_connection_state(), kMaxWaitForFramesMs);
5150 // Now set the tracks, and expect frames to immediately start flowing.
5151 EXPECT_TRUE(caller_audio_sender->SetTrack(caller()->CreateLocalAudioTrack()));
5152 EXPECT_TRUE(caller_video_sender->SetTrack(caller()->CreateLocalVideoTrack()));
5153 EXPECT_TRUE(callee_audio_sender->SetTrack(callee()->CreateLocalAudioTrack()));
5154 EXPECT_TRUE(callee_video_sender->SetTrack(callee()->CreateLocalVideoTrack()));
Seth Hampson2f0d7022018-02-20 11:54:42 -08005155 MediaExpectations media_expectations;
5156 media_expectations.ExpectBidirectionalAudioAndVideo();
5157 ASSERT_TRUE(ExpectNewFrames(media_expectations));
5158}
5159
5160// This tests that if we negotiate after calling AddTransceiver but before we
5161// have a track, then set a track later, frames from the newly-set tracks are
5162// received end-to-end.
5163TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
5164 MediaFlowsAfterEarlyWarmupWithAddTransceiver) {
5165 ASSERT_TRUE(CreatePeerConnectionWrappers());
5166 ConnectFakeSignaling();
5167 auto audio_result = caller()->pc()->AddTransceiver(cricket::MEDIA_TYPE_AUDIO);
5168 ASSERT_EQ(RTCErrorType::NONE, audio_result.error().type());
5169 auto caller_audio_sender = audio_result.MoveValue()->sender();
5170 auto video_result = caller()->pc()->AddTransceiver(cricket::MEDIA_TYPE_VIDEO);
5171 ASSERT_EQ(RTCErrorType::NONE, video_result.error().type());
5172 auto caller_video_sender = video_result.MoveValue()->sender();
5173 callee()->SetRemoteOfferHandler([this] {
5174 ASSERT_EQ(2u, callee()->pc()->GetTransceivers().size());
5175 callee()->pc()->GetTransceivers()[0]->SetDirection(
5176 RtpTransceiverDirection::kSendRecv);
5177 callee()->pc()->GetTransceivers()[1]->SetDirection(
5178 RtpTransceiverDirection::kSendRecv);
5179 });
5180 caller()->CreateAndSetAndSignalOffer();
5181 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
5182 // Wait for ICE to complete, without any tracks being set.
5183 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
5184 caller()->ice_connection_state(), kMaxWaitForFramesMs);
5185 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
5186 callee()->ice_connection_state(), kMaxWaitForFramesMs);
5187 // Now set the tracks, and expect frames to immediately start flowing.
5188 auto callee_audio_sender = callee()->pc()->GetSenders()[0];
5189 auto callee_video_sender = callee()->pc()->GetSenders()[1];
5190 ASSERT_TRUE(caller_audio_sender->SetTrack(caller()->CreateLocalAudioTrack()));
5191 ASSERT_TRUE(caller_video_sender->SetTrack(caller()->CreateLocalVideoTrack()));
5192 ASSERT_TRUE(callee_audio_sender->SetTrack(callee()->CreateLocalAudioTrack()));
5193 ASSERT_TRUE(callee_video_sender->SetTrack(callee()->CreateLocalVideoTrack()));
5194 MediaExpectations media_expectations;
5195 media_expectations.ExpectBidirectionalAudioAndVideo();
5196 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07005197}
5198
5199// This test verifies that a remote video track can be added via AddStream,
5200// and sent end-to-end. For this particular test, it's simply echoed back
5201// from the caller to the callee, rather than being forwarded to a third
5202// PeerConnection.
Seth Hampson2f0d7022018-02-20 11:54:42 -08005203TEST_F(PeerConnectionIntegrationTestPlanB, CanSendRemoteVideoTrack) {
deadbeef1dcb1642017-03-29 21:08:16 -07005204 ASSERT_TRUE(CreatePeerConnectionWrappers());
5205 ConnectFakeSignaling();
5206 // Just send a video track from the caller.
Steve Anton15324772018-01-16 10:26:49 -08005207 caller()->AddVideoTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07005208 caller()->CreateAndSetAndSignalOffer();
5209 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02005210 ASSERT_EQ(1U, callee()->remote_streams()->count());
deadbeef1dcb1642017-03-29 21:08:16 -07005211
5212 // Echo the stream back, and do a new offer/anwer (initiated by callee this
5213 // time).
5214 callee()->pc()->AddStream(callee()->remote_streams()->at(0));
5215 callee()->CreateAndSetAndSignalOffer();
5216 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
5217
Seth Hampson2f0d7022018-02-20 11:54:42 -08005218 MediaExpectations media_expectations;
5219 media_expectations.ExpectBidirectionalVideo();
5220 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07005221}
5222
5223// Test that we achieve the expected end-to-end connection time, using a
5224// fake clock and simulated latency on the media and signaling paths.
5225// We use a TURN<->TURN connection because this is usually the quickest to
5226// set up initially, especially when we're confident the connection will work
5227// and can start sending media before we get a STUN response.
5228//
5229// With various optimizations enabled, here are the network delays we expect to
5230// be on the critical path:
5231// 1. 2 signaling trips: Signaling offer and offerer's TURN candidate, then
5232// signaling answer (with DTLS fingerprint).
5233// 2. 9 media hops: Rest of the DTLS handshake. 3 hops in each direction when
5234// using TURN<->TURN pair, and DTLS exchange is 4 packets,
5235// the first of which should have arrived before the answer.
Seth Hampson2f0d7022018-02-20 11:54:42 -08005236TEST_P(PeerConnectionIntegrationTest, EndToEndConnectionTimeWithTurnTurnPair) {
deadbeef1dcb1642017-03-29 21:08:16 -07005237 rtc::ScopedFakeClock fake_clock;
5238 // Some things use a time of "0" as a special value, so we need to start out
5239 // the fake clock at a nonzero time.
5240 // TODO(deadbeef): Fix this.
Sebastian Jansson5f83cf02018-05-08 14:52:22 +02005241 fake_clock.AdvanceTime(webrtc::TimeDelta::seconds(1));
deadbeef1dcb1642017-03-29 21:08:16 -07005242
5243 static constexpr int media_hop_delay_ms = 50;
5244 static constexpr int signaling_trip_delay_ms = 500;
5245 // For explanation of these values, see comment above.
5246 static constexpr int required_media_hops = 9;
5247 static constexpr int required_signaling_trips = 2;
5248 // For internal delays (such as posting an event asychronously).
5249 static constexpr int allowed_internal_delay_ms = 20;
5250 static constexpr int total_connection_time_ms =
5251 media_hop_delay_ms * required_media_hops +
5252 signaling_trip_delay_ms * required_signaling_trips +
5253 allowed_internal_delay_ms;
5254
5255 static const rtc::SocketAddress turn_server_1_internal_address{"88.88.88.0",
5256 3478};
5257 static const rtc::SocketAddress turn_server_1_external_address{"88.88.88.1",
5258 0};
5259 static const rtc::SocketAddress turn_server_2_internal_address{"99.99.99.0",
5260 3478};
5261 static const rtc::SocketAddress turn_server_2_external_address{"99.99.99.1",
5262 0};
Seth Hampsonaed71642018-06-11 07:41:32 -07005263 cricket::TestTurnServer* turn_server_1 = CreateTurnServer(
5264 turn_server_1_internal_address, turn_server_1_external_address);
Jonas Orelandbdcee282017-10-10 14:01:40 +02005265
Seth Hampsonaed71642018-06-11 07:41:32 -07005266 cricket::TestTurnServer* turn_server_2 = CreateTurnServer(
5267 turn_server_2_internal_address, turn_server_2_external_address);
deadbeef1dcb1642017-03-29 21:08:16 -07005268 // Bypass permission check on received packets so media can be sent before
5269 // the candidate is signaled.
Seth Hampsonaed71642018-06-11 07:41:32 -07005270 network_thread()->Invoke<void>(RTC_FROM_HERE, [turn_server_1] {
5271 turn_server_1->set_enable_permission_checks(false);
5272 });
5273 network_thread()->Invoke<void>(RTC_FROM_HERE, [turn_server_2] {
5274 turn_server_2->set_enable_permission_checks(false);
5275 });
deadbeef1dcb1642017-03-29 21:08:16 -07005276
5277 PeerConnectionInterface::RTCConfiguration client_1_config;
5278 webrtc::PeerConnectionInterface::IceServer ice_server_1;
5279 ice_server_1.urls.push_back("turn:88.88.88.0:3478");
5280 ice_server_1.username = "test";
5281 ice_server_1.password = "test";
5282 client_1_config.servers.push_back(ice_server_1);
5283 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
5284 client_1_config.presume_writable_when_fully_relayed = true;
5285
5286 PeerConnectionInterface::RTCConfiguration client_2_config;
5287 webrtc::PeerConnectionInterface::IceServer ice_server_2;
5288 ice_server_2.urls.push_back("turn:99.99.99.0:3478");
5289 ice_server_2.username = "test";
5290 ice_server_2.password = "test";
5291 client_2_config.servers.push_back(ice_server_2);
5292 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
5293 client_2_config.presume_writable_when_fully_relayed = true;
5294
5295 ASSERT_TRUE(
5296 CreatePeerConnectionWrappersWithConfig(client_1_config, client_2_config));
5297 // Set up the simulated delays.
5298 SetSignalingDelayMs(signaling_trip_delay_ms);
5299 ConnectFakeSignaling();
5300 virtual_socket_server()->set_delay_mean(media_hop_delay_ms);
5301 virtual_socket_server()->UpdateDelayDistribution();
5302
5303 // Set "offer to receive audio/video" without adding any tracks, so we just
5304 // set up ICE/DTLS with no media.
5305 PeerConnectionInterface::RTCOfferAnswerOptions options;
5306 options.offer_to_receive_audio = 1;
5307 options.offer_to_receive_video = 1;
5308 caller()->SetOfferAnswerOptions(options);
5309 caller()->CreateAndSetAndSignalOffer();
deadbeef71452802017-05-07 17:21:01 -07005310 EXPECT_TRUE_SIMULATED_WAIT(DtlsConnected(), total_connection_time_ms,
5311 fake_clock);
Seth Hampson1d4a76d2018-06-19 14:31:41 -07005312 // Closing the PeerConnections destroys the ports before the ScopedFakeClock.
5313 // If this is not done a DCHECK can be hit in ports.cc, because a large
5314 // negative number is calculated for the rtt due to the global clock changing.
Steve Antond91969e2019-05-30 12:27:03 -07005315 ClosePeerConnections();
deadbeef1dcb1642017-03-29 21:08:16 -07005316}
5317
Jonas Orelandbdcee282017-10-10 14:01:40 +02005318// Verify that a TurnCustomizer passed in through RTCConfiguration
5319// is actually used by the underlying TURN candidate pair.
5320// Note that turnport_unittest.cc contains more detailed, lower-level tests.
Seth Hampson2f0d7022018-02-20 11:54:42 -08005321TEST_P(PeerConnectionIntegrationTest, TurnCustomizerUsedForTurnConnections) {
Jonas Orelandbdcee282017-10-10 14:01:40 +02005322 static const rtc::SocketAddress turn_server_1_internal_address{"88.88.88.0",
5323 3478};
5324 static const rtc::SocketAddress turn_server_1_external_address{"88.88.88.1",
5325 0};
5326 static const rtc::SocketAddress turn_server_2_internal_address{"99.99.99.0",
5327 3478};
5328 static const rtc::SocketAddress turn_server_2_external_address{"99.99.99.1",
5329 0};
Seth Hampsonaed71642018-06-11 07:41:32 -07005330 CreateTurnServer(turn_server_1_internal_address,
5331 turn_server_1_external_address);
5332 CreateTurnServer(turn_server_2_internal_address,
5333 turn_server_2_external_address);
Jonas Orelandbdcee282017-10-10 14:01:40 +02005334
5335 PeerConnectionInterface::RTCConfiguration client_1_config;
5336 webrtc::PeerConnectionInterface::IceServer ice_server_1;
5337 ice_server_1.urls.push_back("turn:88.88.88.0:3478");
5338 ice_server_1.username = "test";
5339 ice_server_1.password = "test";
5340 client_1_config.servers.push_back(ice_server_1);
5341 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
Seth Hampsonaed71642018-06-11 07:41:32 -07005342 auto* customizer1 = CreateTurnCustomizer();
5343 client_1_config.turn_customizer = customizer1;
Jonas Orelandbdcee282017-10-10 14:01:40 +02005344
5345 PeerConnectionInterface::RTCConfiguration client_2_config;
5346 webrtc::PeerConnectionInterface::IceServer ice_server_2;
5347 ice_server_2.urls.push_back("turn:99.99.99.0:3478");
5348 ice_server_2.username = "test";
5349 ice_server_2.password = "test";
5350 client_2_config.servers.push_back(ice_server_2);
5351 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
Seth Hampsonaed71642018-06-11 07:41:32 -07005352 auto* customizer2 = CreateTurnCustomizer();
5353 client_2_config.turn_customizer = customizer2;
Jonas Orelandbdcee282017-10-10 14:01:40 +02005354
5355 ASSERT_TRUE(
5356 CreatePeerConnectionWrappersWithConfig(client_1_config, client_2_config));
5357 ConnectFakeSignaling();
5358
5359 // Set "offer to receive audio/video" without adding any tracks, so we just
5360 // set up ICE/DTLS with no media.
5361 PeerConnectionInterface::RTCOfferAnswerOptions options;
5362 options.offer_to_receive_audio = 1;
5363 options.offer_to_receive_video = 1;
5364 caller()->SetOfferAnswerOptions(options);
5365 caller()->CreateAndSetAndSignalOffer();
5366 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
5367
Seth Hampsonaed71642018-06-11 07:41:32 -07005368 ExpectTurnCustomizerCountersIncremented(customizer1);
5369 ExpectTurnCustomizerCountersIncremented(customizer2);
Jonas Orelandbdcee282017-10-10 14:01:40 +02005370}
5371
Benjamin Wright2d5f3cb2018-05-22 14:46:06 -07005372// Verifies that you can use TCP instead of UDP to connect to a TURN server and
5373// send media between the caller and the callee.
5374TEST_P(PeerConnectionIntegrationTest, TCPUsedForTurnConnections) {
5375 static const rtc::SocketAddress turn_server_internal_address{"88.88.88.0",
5376 3478};
5377 static const rtc::SocketAddress turn_server_external_address{"88.88.88.1", 0};
5378
5379 // Enable TCP for the fake turn server.
Seth Hampsonaed71642018-06-11 07:41:32 -07005380 CreateTurnServer(turn_server_internal_address, turn_server_external_address,
5381 cricket::PROTO_TCP);
Benjamin Wright2d5f3cb2018-05-22 14:46:06 -07005382
5383 webrtc::PeerConnectionInterface::IceServer ice_server;
5384 ice_server.urls.push_back("turn:88.88.88.0:3478?transport=tcp");
5385 ice_server.username = "test";
5386 ice_server.password = "test";
5387
5388 PeerConnectionInterface::RTCConfiguration client_1_config;
5389 client_1_config.servers.push_back(ice_server);
5390 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
5391
5392 PeerConnectionInterface::RTCConfiguration client_2_config;
5393 client_2_config.servers.push_back(ice_server);
5394 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
5395
5396 ASSERT_TRUE(
5397 CreatePeerConnectionWrappersWithConfig(client_1_config, client_2_config));
5398
5399 // Do normal offer/answer and wait for ICE to complete.
5400 ConnectFakeSignaling();
5401 caller()->AddAudioVideoTracks();
5402 callee()->AddAudioVideoTracks();
5403 caller()->CreateAndSetAndSignalOffer();
5404 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5405 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
5406 callee()->ice_connection_state(), kMaxWaitForFramesMs);
5407
5408 MediaExpectations media_expectations;
5409 media_expectations.ExpectBidirectionalAudioAndVideo();
5410 EXPECT_TRUE(ExpectNewFrames(media_expectations));
5411}
5412
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07005413// Verify that a SSLCertificateVerifier passed in through
5414// PeerConnectionDependencies is actually used by the underlying SSL
5415// implementation to determine whether a certificate presented by the TURN
5416// server is accepted by the client. Note that openssladapter_unittest.cc
5417// contains more detailed, lower-level tests.
5418TEST_P(PeerConnectionIntegrationTest,
5419 SSLCertificateVerifierUsedForTurnConnections) {
5420 static const rtc::SocketAddress turn_server_internal_address{"88.88.88.0",
5421 3478};
5422 static const rtc::SocketAddress turn_server_external_address{"88.88.88.1", 0};
5423
5424 // Enable TCP-TLS for the fake turn server. We need to pass in 88.88.88.0 so
5425 // that host name verification passes on the fake certificate.
Seth Hampsonaed71642018-06-11 07:41:32 -07005426 CreateTurnServer(turn_server_internal_address, turn_server_external_address,
5427 cricket::PROTO_TLS, "88.88.88.0");
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07005428
5429 webrtc::PeerConnectionInterface::IceServer ice_server;
5430 ice_server.urls.push_back("turns:88.88.88.0:3478?transport=tcp");
5431 ice_server.username = "test";
5432 ice_server.password = "test";
5433
5434 PeerConnectionInterface::RTCConfiguration client_1_config;
5435 client_1_config.servers.push_back(ice_server);
5436 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
5437
5438 PeerConnectionInterface::RTCConfiguration client_2_config;
5439 client_2_config.servers.push_back(ice_server);
5440 // Setting the type to kRelay forces the connection to go through a TURN
5441 // server.
5442 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
5443
5444 // Get a copy to the pointer so we can verify calls later.
5445 rtc::TestCertificateVerifier* client_1_cert_verifier =
5446 new rtc::TestCertificateVerifier();
5447 client_1_cert_verifier->verify_certificate_ = true;
5448 rtc::TestCertificateVerifier* client_2_cert_verifier =
5449 new rtc::TestCertificateVerifier();
5450 client_2_cert_verifier->verify_certificate_ = true;
5451
5452 // Create the dependencies with the test certificate verifier.
5453 webrtc::PeerConnectionDependencies client_1_deps(nullptr);
5454 client_1_deps.tls_cert_verifier =
5455 std::unique_ptr<rtc::TestCertificateVerifier>(client_1_cert_verifier);
5456 webrtc::PeerConnectionDependencies client_2_deps(nullptr);
5457 client_2_deps.tls_cert_verifier =
5458 std::unique_ptr<rtc::TestCertificateVerifier>(client_2_cert_verifier);
5459
5460 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndDeps(
5461 client_1_config, std::move(client_1_deps), client_2_config,
5462 std::move(client_2_deps)));
5463 ConnectFakeSignaling();
5464
5465 // Set "offer to receive audio/video" without adding any tracks, so we just
5466 // set up ICE/DTLS with no media.
5467 PeerConnectionInterface::RTCOfferAnswerOptions options;
5468 options.offer_to_receive_audio = 1;
5469 options.offer_to_receive_video = 1;
5470 caller()->SetOfferAnswerOptions(options);
5471 caller()->CreateAndSetAndSignalOffer();
5472 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
5473
5474 EXPECT_GT(client_1_cert_verifier->call_count_, 0u);
5475 EXPECT_GT(client_2_cert_verifier->call_count_, 0u);
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07005476}
5477
5478TEST_P(PeerConnectionIntegrationTest,
5479 SSLCertificateVerifierFailureUsedForTurnConnectionsFailsConnection) {
5480 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 // Enable TCP-TLS for the fake turn server. We need to pass in 88.88.88.0 so
5485 // that host name verification passes on the fake certificate.
Seth Hampsonaed71642018-06-11 07:41:32 -07005486 CreateTurnServer(turn_server_internal_address, turn_server_external_address,
5487 cricket::PROTO_TLS, "88.88.88.0");
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07005488
5489 webrtc::PeerConnectionInterface::IceServer ice_server;
5490 ice_server.urls.push_back("turns:88.88.88.0:3478?transport=tcp");
5491 ice_server.username = "test";
5492 ice_server.password = "test";
5493
5494 PeerConnectionInterface::RTCConfiguration client_1_config;
5495 client_1_config.servers.push_back(ice_server);
5496 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
5497
5498 PeerConnectionInterface::RTCConfiguration client_2_config;
5499 client_2_config.servers.push_back(ice_server);
5500 // Setting the type to kRelay forces the connection to go through a TURN
5501 // server.
5502 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
5503
5504 // Get a copy to the pointer so we can verify calls later.
5505 rtc::TestCertificateVerifier* client_1_cert_verifier =
5506 new rtc::TestCertificateVerifier();
5507 client_1_cert_verifier->verify_certificate_ = false;
5508 rtc::TestCertificateVerifier* client_2_cert_verifier =
5509 new rtc::TestCertificateVerifier();
5510 client_2_cert_verifier->verify_certificate_ = false;
5511
5512 // Create the dependencies with the test certificate verifier.
5513 webrtc::PeerConnectionDependencies client_1_deps(nullptr);
5514 client_1_deps.tls_cert_verifier =
5515 std::unique_ptr<rtc::TestCertificateVerifier>(client_1_cert_verifier);
5516 webrtc::PeerConnectionDependencies client_2_deps(nullptr);
5517 client_2_deps.tls_cert_verifier =
5518 std::unique_ptr<rtc::TestCertificateVerifier>(client_2_cert_verifier);
5519
5520 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndDeps(
5521 client_1_config, std::move(client_1_deps), client_2_config,
5522 std::move(client_2_deps)));
5523 ConnectFakeSignaling();
5524
5525 // Set "offer to receive audio/video" without adding any tracks, so we just
5526 // set up ICE/DTLS with no media.
5527 PeerConnectionInterface::RTCOfferAnswerOptions options;
5528 options.offer_to_receive_audio = 1;
5529 options.offer_to_receive_video = 1;
5530 caller()->SetOfferAnswerOptions(options);
5531 caller()->CreateAndSetAndSignalOffer();
5532 bool wait_res = true;
5533 // TODO(bugs.webrtc.org/9219): When IceConnectionState is implemented
5534 // properly, should be able to just wait for a state of "failed" instead of
5535 // waiting a fixed 10 seconds.
5536 WAIT_(DtlsConnected(), kDefaultTimeout, wait_res);
5537 ASSERT_FALSE(wait_res);
5538
5539 EXPECT_GT(client_1_cert_verifier->call_count_, 0u);
5540 EXPECT_GT(client_2_cert_verifier->call_count_, 0u);
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07005541}
5542
deadbeefc964d0b2017-04-03 10:03:35 -07005543// Test that audio and video flow end-to-end when codec names don't use the
5544// expected casing, given that they're supposed to be case insensitive. To test
5545// this, all but one codec is removed from each media description, and its
5546// casing is changed.
5547//
5548// In the past, this has regressed and caused crashes/black video, due to the
5549// fact that code at some layers was doing case-insensitive comparisons and
5550// code at other layers was not.
Seth Hampson2f0d7022018-02-20 11:54:42 -08005551TEST_P(PeerConnectionIntegrationTest, CodecNamesAreCaseInsensitive) {
deadbeefc964d0b2017-04-03 10:03:35 -07005552 ASSERT_TRUE(CreatePeerConnectionWrappers());
5553 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08005554 caller()->AddAudioVideoTracks();
5555 callee()->AddAudioVideoTracks();
deadbeefc964d0b2017-04-03 10:03:35 -07005556
5557 // Remove all but one audio/video codec (opus and VP8), and change the
5558 // casing of the caller's generated offer.
5559 caller()->SetGeneratedSdpMunger([](cricket::SessionDescription* description) {
5560 cricket::AudioContentDescription* audio =
5561 GetFirstAudioContentDescription(description);
5562 ASSERT_NE(nullptr, audio);
5563 auto audio_codecs = audio->codecs();
5564 audio_codecs.erase(std::remove_if(audio_codecs.begin(), audio_codecs.end(),
5565 [](const cricket::AudioCodec& codec) {
5566 return codec.name != "opus";
5567 }),
5568 audio_codecs.end());
5569 ASSERT_EQ(1u, audio_codecs.size());
5570 audio_codecs[0].name = "OpUs";
5571 audio->set_codecs(audio_codecs);
5572
5573 cricket::VideoContentDescription* video =
5574 GetFirstVideoContentDescription(description);
5575 ASSERT_NE(nullptr, video);
5576 auto video_codecs = video->codecs();
5577 video_codecs.erase(std::remove_if(video_codecs.begin(), video_codecs.end(),
5578 [](const cricket::VideoCodec& codec) {
5579 return codec.name != "VP8";
5580 }),
5581 video_codecs.end());
5582 ASSERT_EQ(1u, video_codecs.size());
5583 video_codecs[0].name = "vP8";
5584 video->set_codecs(video_codecs);
5585 });
5586
5587 caller()->CreateAndSetAndSignalOffer();
5588 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5589
5590 // Verify frames are still received end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08005591 MediaExpectations media_expectations;
5592 media_expectations.ExpectBidirectionalAudioAndVideo();
5593 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeefc964d0b2017-04-03 10:03:35 -07005594}
5595
Jonas Oreland49ac5952018-09-26 16:04:32 +02005596TEST_P(PeerConnectionIntegrationTest, GetSourcesAudio) {
hbos8d609f62017-04-10 07:39:05 -07005597 ASSERT_TRUE(CreatePeerConnectionWrappers());
5598 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08005599 caller()->AddAudioTrack();
hbos8d609f62017-04-10 07:39:05 -07005600 caller()->CreateAndSetAndSignalOffer();
5601 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
deadbeefd8ad7882017-04-18 16:01:17 -07005602 // Wait for one audio frame to be received by the callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08005603 MediaExpectations media_expectations;
5604 media_expectations.CalleeExpectsSomeAudio(1);
5605 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Jonas Oreland49ac5952018-09-26 16:04:32 +02005606 ASSERT_EQ(callee()->pc()->GetReceivers().size(), 1u);
hbos8d609f62017-04-10 07:39:05 -07005607 auto receiver = callee()->pc()->GetReceivers()[0];
5608 ASSERT_EQ(receiver->media_type(), cricket::MEDIA_TYPE_AUDIO);
Jonas Oreland49ac5952018-09-26 16:04:32 +02005609 auto sources = receiver->GetSources();
hbos8d609f62017-04-10 07:39:05 -07005610 ASSERT_GT(receiver->GetParameters().encodings.size(), 0u);
5611 EXPECT_EQ(receiver->GetParameters().encodings[0].ssrc,
Jonas Oreland49ac5952018-09-26 16:04:32 +02005612 sources[0].source_id());
5613 EXPECT_EQ(webrtc::RtpSourceType::SSRC, sources[0].source_type());
5614}
5615
5616TEST_P(PeerConnectionIntegrationTest, GetSourcesVideo) {
5617 ASSERT_TRUE(CreatePeerConnectionWrappers());
5618 ConnectFakeSignaling();
5619 caller()->AddVideoTrack();
5620 caller()->CreateAndSetAndSignalOffer();
5621 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5622 // Wait for one video frame to be received by the callee.
5623 MediaExpectations media_expectations;
5624 media_expectations.CalleeExpectsSomeVideo(1);
5625 ASSERT_TRUE(ExpectNewFrames(media_expectations));
5626 ASSERT_EQ(callee()->pc()->GetReceivers().size(), 1u);
5627 auto receiver = callee()->pc()->GetReceivers()[0];
5628 ASSERT_EQ(receiver->media_type(), cricket::MEDIA_TYPE_VIDEO);
5629 auto sources = receiver->GetSources();
5630 ASSERT_GT(receiver->GetParameters().encodings.size(), 0u);
Yves Gereyf781bb52019-07-23 19:15:39 +02005631 ASSERT_GT(sources.size(), 0u);
Jonas Oreland49ac5952018-09-26 16:04:32 +02005632 EXPECT_EQ(receiver->GetParameters().encodings[0].ssrc,
5633 sources[0].source_id());
5634 EXPECT_EQ(webrtc::RtpSourceType::SSRC, sources[0].source_type());
hbos8d609f62017-04-10 07:39:05 -07005635}
5636
deadbeef2f425aa2017-04-14 10:41:32 -07005637// Test that if a track is removed and added again with a different stream ID,
5638// the new stream ID is successfully communicated in SDP and media continues to
5639// flow end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08005640// TODO(webrtc.bugs.org/8734): This test does not work for Unified Plan because
5641// it will not reuse a transceiver that has already been sending. After creating
5642// a new transceiver it tries to create an offer with two senders of the same
5643// track ids and it fails.
5644TEST_F(PeerConnectionIntegrationTestPlanB, RemoveAndAddTrackWithNewStreamId) {
deadbeef2f425aa2017-04-14 10:41:32 -07005645 ASSERT_TRUE(CreatePeerConnectionWrappers());
5646 ConnectFakeSignaling();
5647
deadbeef2f425aa2017-04-14 10:41:32 -07005648 // Add track using stream 1, do offer/answer.
5649 rtc::scoped_refptr<webrtc::AudioTrackInterface> track =
5650 caller()->CreateLocalAudioTrack();
5651 rtc::scoped_refptr<webrtc::RtpSenderInterface> sender =
Steve Antond78323f2018-07-11 11:13:44 -07005652 caller()->AddTrack(track, {"stream_1"});
deadbeef2f425aa2017-04-14 10:41:32 -07005653 caller()->CreateAndSetAndSignalOffer();
5654 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08005655 {
5656 MediaExpectations media_expectations;
5657 media_expectations.CalleeExpectsSomeAudio(1);
5658 ASSERT_TRUE(ExpectNewFrames(media_expectations));
5659 }
deadbeef2f425aa2017-04-14 10:41:32 -07005660 // Remove the sender, and create a new one with the new stream.
5661 caller()->pc()->RemoveTrack(sender);
Steve Antond78323f2018-07-11 11:13:44 -07005662 sender = caller()->AddTrack(track, {"stream_2"});
deadbeef2f425aa2017-04-14 10:41:32 -07005663 caller()->CreateAndSetAndSignalOffer();
5664 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5665 // Wait for additional audio frames to be received by the callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08005666 {
5667 MediaExpectations media_expectations;
5668 media_expectations.CalleeExpectsSomeAudio();
5669 ASSERT_TRUE(ExpectNewFrames(media_expectations));
5670 }
deadbeef2f425aa2017-04-14 10:41:32 -07005671}
5672
Seth Hampson2f0d7022018-02-20 11:54:42 -08005673TEST_P(PeerConnectionIntegrationTest, RtcEventLogOutputWriteCalled) {
Elad Alon99c3fe52017-10-13 16:29:40 +02005674 ASSERT_TRUE(CreatePeerConnectionWrappers());
5675 ConnectFakeSignaling();
5676
Mirko Bonadei317a1f02019-09-17 17:06:18 +02005677 auto output = std::make_unique<testing::NiceMock<MockRtcEventLogOutput>>();
Mirko Bonadei6a489f22019-04-09 15:11:12 +02005678 ON_CALL(*output, IsActive()).WillByDefault(::testing::Return(true));
5679 ON_CALL(*output, Write(::testing::_)).WillByDefault(::testing::Return(true));
Elad Alon99c3fe52017-10-13 16:29:40 +02005680 EXPECT_CALL(*output, Write(::testing::_)).Times(::testing::AtLeast(1));
Bjorn Tereliusde939432017-11-20 17:38:14 +01005681 EXPECT_TRUE(caller()->pc()->StartRtcEventLog(
5682 std::move(output), webrtc::RtcEventLog::kImmediateOutput));
Elad Alon99c3fe52017-10-13 16:29:40 +02005683
Steve Anton15324772018-01-16 10:26:49 -08005684 caller()->AddAudioVideoTracks();
Elad Alon99c3fe52017-10-13 16:29:40 +02005685 caller()->CreateAndSetAndSignalOffer();
5686 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5687}
5688
Steve Antonede9ca52017-10-16 13:04:27 -07005689// Test that if candidates are only signaled by applying full session
5690// descriptions (instead of using AddIceCandidate), the peers can connect to
5691// each other and exchange media.
Seth Hampson2f0d7022018-02-20 11:54:42 -08005692TEST_P(PeerConnectionIntegrationTest, MediaFlowsWhenCandidatesSetOnlyInSdp) {
Steve Antonede9ca52017-10-16 13:04:27 -07005693 ASSERT_TRUE(CreatePeerConnectionWrappers());
5694 // Each side will signal the session descriptions but not candidates.
5695 ConnectFakeSignalingForSdpOnly();
5696
5697 // Add audio video track and exchange the initial offer/answer with media
5698 // information only. This will start ICE gathering on each side.
Steve Anton15324772018-01-16 10:26:49 -08005699 caller()->AddAudioVideoTracks();
5700 callee()->AddAudioVideoTracks();
Steve Antonede9ca52017-10-16 13:04:27 -07005701 caller()->CreateAndSetAndSignalOffer();
5702
5703 // Wait for all candidates to be gathered on both the caller and callee.
5704 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceGatheringComplete,
5705 caller()->ice_gathering_state(), kDefaultTimeout);
5706 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceGatheringComplete,
5707 callee()->ice_gathering_state(), kDefaultTimeout);
5708
5709 // The candidates will now be included in the session description, so
5710 // signaling them will start the ICE connection.
5711 caller()->CreateAndSetAndSignalOffer();
5712 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5713
5714 // Ensure that media flows in both directions.
Seth Hampson2f0d7022018-02-20 11:54:42 -08005715 MediaExpectations media_expectations;
5716 media_expectations.ExpectBidirectionalAudioAndVideo();
5717 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Antonede9ca52017-10-16 13:04:27 -07005718}
5719
henrika5f6bf242017-11-01 11:06:56 +01005720// Test that SetAudioPlayout can be used to disable audio playout from the
5721// start, then later enable it. This may be useful, for example, if the caller
5722// needs to play a local ringtone until some event occurs, after which it
5723// switches to playing the received audio.
Seth Hampson2f0d7022018-02-20 11:54:42 -08005724TEST_P(PeerConnectionIntegrationTest, DisableAndEnableAudioPlayout) {
henrika5f6bf242017-11-01 11:06:56 +01005725 ASSERT_TRUE(CreatePeerConnectionWrappers());
5726 ConnectFakeSignaling();
5727
5728 // Set up audio-only call where audio playout is disabled on caller's side.
5729 caller()->pc()->SetAudioPlayout(false);
Steve Anton15324772018-01-16 10:26:49 -08005730 caller()->AddAudioTrack();
5731 callee()->AddAudioTrack();
henrika5f6bf242017-11-01 11:06:56 +01005732 caller()->CreateAndSetAndSignalOffer();
5733 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5734
5735 // Pump messages for a second.
5736 WAIT(false, 1000);
5737 // Since audio playout is disabled, the caller shouldn't have received
5738 // anything (at the playout level, at least).
5739 EXPECT_EQ(0, caller()->audio_frames_received());
5740 // As a sanity check, make sure the callee (for which playout isn't disabled)
5741 // did still see frames on its audio level.
5742 ASSERT_GT(callee()->audio_frames_received(), 0);
5743
5744 // Enable playout again, and ensure audio starts flowing.
5745 caller()->pc()->SetAudioPlayout(true);
Seth Hampson2f0d7022018-02-20 11:54:42 -08005746 MediaExpectations media_expectations;
5747 media_expectations.ExpectBidirectionalAudio();
5748 ASSERT_TRUE(ExpectNewFrames(media_expectations));
henrika5f6bf242017-11-01 11:06:56 +01005749}
5750
5751double GetAudioEnergyStat(PeerConnectionWrapper* pc) {
5752 auto report = pc->NewGetStats();
5753 auto track_stats_list =
5754 report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
5755 const webrtc::RTCMediaStreamTrackStats* remote_track_stats = nullptr;
5756 for (const auto* track_stats : track_stats_list) {
5757 if (track_stats->remote_source.is_defined() &&
5758 *track_stats->remote_source) {
5759 remote_track_stats = track_stats;
5760 break;
5761 }
5762 }
5763
5764 if (!remote_track_stats->total_audio_energy.is_defined()) {
5765 return 0.0;
5766 }
5767 return *remote_track_stats->total_audio_energy;
5768}
5769
5770// Test that if audio playout is disabled via the SetAudioPlayout() method, then
5771// incoming audio is still processed and statistics are generated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08005772TEST_P(PeerConnectionIntegrationTest,
henrika5f6bf242017-11-01 11:06:56 +01005773 DisableAudioPlayoutStillGeneratesAudioStats) {
5774 ASSERT_TRUE(CreatePeerConnectionWrappers());
5775 ConnectFakeSignaling();
5776
5777 // Set up audio-only call where playout is disabled but audio-processing is
5778 // still active.
Steve Anton15324772018-01-16 10:26:49 -08005779 caller()->AddAudioTrack();
5780 callee()->AddAudioTrack();
henrika5f6bf242017-11-01 11:06:56 +01005781 caller()->pc()->SetAudioPlayout(false);
5782
5783 caller()->CreateAndSetAndSignalOffer();
5784 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5785
5786 // Wait for the callee to receive audio stats.
5787 EXPECT_TRUE_WAIT(GetAudioEnergyStat(caller()) > 0, kMaxWaitForFramesMs);
5788}
5789
henrika4f167df2017-11-01 14:45:55 +01005790// Test that SetAudioRecording can be used to disable audio recording from the
5791// start, then later enable it. This may be useful, for example, if the caller
5792// wants to ensure that no audio resources are active before a certain state
5793// is reached.
Seth Hampson2f0d7022018-02-20 11:54:42 -08005794TEST_P(PeerConnectionIntegrationTest, DisableAndEnableAudioRecording) {
henrika4f167df2017-11-01 14:45:55 +01005795 ASSERT_TRUE(CreatePeerConnectionWrappers());
5796 ConnectFakeSignaling();
5797
5798 // Set up audio-only call where audio recording is disabled on caller's side.
5799 caller()->pc()->SetAudioRecording(false);
Steve Anton15324772018-01-16 10:26:49 -08005800 caller()->AddAudioTrack();
5801 callee()->AddAudioTrack();
henrika4f167df2017-11-01 14:45:55 +01005802 caller()->CreateAndSetAndSignalOffer();
5803 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5804
5805 // Pump messages for a second.
5806 WAIT(false, 1000);
5807 // Since caller has disabled audio recording, the callee shouldn't have
5808 // received anything.
5809 EXPECT_EQ(0, callee()->audio_frames_received());
5810 // As a sanity check, make sure the caller did still see frames on its
5811 // audio level since audio recording is enabled on the calle side.
5812 ASSERT_GT(caller()->audio_frames_received(), 0);
5813
5814 // Enable audio recording again, and ensure audio starts flowing.
5815 caller()->pc()->SetAudioRecording(true);
Seth Hampson2f0d7022018-02-20 11:54:42 -08005816 MediaExpectations media_expectations;
5817 media_expectations.ExpectBidirectionalAudio();
5818 ASSERT_TRUE(ExpectNewFrames(media_expectations));
henrika4f167df2017-11-01 14:45:55 +01005819}
5820
Taylor Brandstetter389a97c2018-01-03 16:26:06 -08005821// Test that after closing PeerConnections, they stop sending any packets (ICE,
5822// DTLS, RTP...).
Seth Hampson2f0d7022018-02-20 11:54:42 -08005823TEST_P(PeerConnectionIntegrationTest, ClosingConnectionStopsPacketFlow) {
Taylor Brandstetter389a97c2018-01-03 16:26:06 -08005824 // Set up audio/video/data, wait for some frames to be received.
5825 ASSERT_TRUE(CreatePeerConnectionWrappers());
5826 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08005827 caller()->AddAudioVideoTracks();
Taylor Brandstetter389a97c2018-01-03 16:26:06 -08005828#ifdef HAVE_SCTP
5829 caller()->CreateDataChannel();
5830#endif
5831 caller()->CreateAndSetAndSignalOffer();
5832 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08005833 MediaExpectations media_expectations;
5834 media_expectations.CalleeExpectsSomeAudioAndVideo();
5835 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Taylor Brandstetter389a97c2018-01-03 16:26:06 -08005836 // Close PeerConnections.
Steve Antond91969e2019-05-30 12:27:03 -07005837 ClosePeerConnections();
Taylor Brandstetter389a97c2018-01-03 16:26:06 -08005838 // Pump messages for a second, and ensure no new packets end up sent.
5839 uint32_t sent_packets_a = virtual_socket_server()->sent_packets();
5840 WAIT(false, 1000);
5841 uint32_t sent_packets_b = virtual_socket_server()->sent_packets();
5842 EXPECT_EQ(sent_packets_a, sent_packets_b);
5843}
5844
Steve Anton7eca0932018-03-30 15:18:41 -07005845// Test that transport stats are generated by the RTCStatsCollector for a
5846// connection that only involves data channels. This is a regression test for
5847// crbug.com/826972.
5848#ifdef HAVE_SCTP
5849TEST_P(PeerConnectionIntegrationTest,
5850 TransportStatsReportedForDataChannelOnlyConnection) {
5851 ASSERT_TRUE(CreatePeerConnectionWrappers());
5852 ConnectFakeSignaling();
5853 caller()->CreateDataChannel();
5854
5855 caller()->CreateAndSetAndSignalOffer();
5856 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5857 ASSERT_TRUE_WAIT(callee()->data_channel(), kDefaultTimeout);
5858
5859 auto caller_report = caller()->NewGetStats();
5860 EXPECT_EQ(1u, caller_report->GetStatsOfType<RTCTransportStats>().size());
5861 auto callee_report = callee()->NewGetStats();
5862 EXPECT_EQ(1u, callee_report->GetStatsOfType<RTCTransportStats>().size());
5863}
5864#endif // HAVE_SCTP
5865
Qingsi Wang7685e862018-06-11 20:15:46 -07005866TEST_P(PeerConnectionIntegrationTest,
5867 IceEventsGeneratedAndLoggedInRtcEventLog) {
5868 ASSERT_TRUE(CreatePeerConnectionWrappersWithFakeRtcEventLog());
5869 ConnectFakeSignaling();
5870 PeerConnectionInterface::RTCOfferAnswerOptions options;
5871 options.offer_to_receive_audio = 1;
5872 caller()->SetOfferAnswerOptions(options);
5873 caller()->CreateAndSetAndSignalOffer();
5874 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
5875 ASSERT_NE(nullptr, caller()->event_log_factory());
5876 ASSERT_NE(nullptr, callee()->event_log_factory());
5877 webrtc::FakeRtcEventLog* caller_event_log =
5878 static_cast<webrtc::FakeRtcEventLog*>(
5879 caller()->event_log_factory()->last_log_created());
5880 webrtc::FakeRtcEventLog* callee_event_log =
5881 static_cast<webrtc::FakeRtcEventLog*>(
5882 callee()->event_log_factory()->last_log_created());
5883 ASSERT_NE(nullptr, caller_event_log);
5884 ASSERT_NE(nullptr, callee_event_log);
5885 int caller_ice_config_count = caller_event_log->GetEventCount(
5886 webrtc::RtcEvent::Type::IceCandidatePairConfig);
5887 int caller_ice_event_count = caller_event_log->GetEventCount(
5888 webrtc::RtcEvent::Type::IceCandidatePairEvent);
5889 int callee_ice_config_count = callee_event_log->GetEventCount(
5890 webrtc::RtcEvent::Type::IceCandidatePairConfig);
5891 int callee_ice_event_count = callee_event_log->GetEventCount(
5892 webrtc::RtcEvent::Type::IceCandidatePairEvent);
5893 EXPECT_LT(0, caller_ice_config_count);
5894 EXPECT_LT(0, caller_ice_event_count);
5895 EXPECT_LT(0, callee_ice_config_count);
5896 EXPECT_LT(0, callee_ice_event_count);
5897}
5898
Qingsi Wangc129c352019-04-18 10:41:58 -07005899TEST_P(PeerConnectionIntegrationTest, RegatherAfterChangingIceTransportType) {
Qingsi Wangc129c352019-04-18 10:41:58 -07005900 static const rtc::SocketAddress turn_server_internal_address{"88.88.88.0",
5901 3478};
5902 static const rtc::SocketAddress turn_server_external_address{"88.88.88.1", 0};
5903
5904 CreateTurnServer(turn_server_internal_address, turn_server_external_address);
5905
5906 webrtc::PeerConnectionInterface::IceServer ice_server;
5907 ice_server.urls.push_back("turn:88.88.88.0:3478");
5908 ice_server.username = "test";
5909 ice_server.password = "test";
5910
5911 PeerConnectionInterface::RTCConfiguration caller_config;
5912 caller_config.servers.push_back(ice_server);
5913 caller_config.type = webrtc::PeerConnectionInterface::kRelay;
5914 caller_config.continual_gathering_policy = PeerConnection::GATHER_CONTINUALLY;
Qingsi Wang1fe119f2019-05-31 16:55:33 -07005915 caller_config.surface_ice_candidates_on_ice_transport_type_changed = true;
Qingsi Wangc129c352019-04-18 10:41:58 -07005916
5917 PeerConnectionInterface::RTCConfiguration callee_config;
5918 callee_config.servers.push_back(ice_server);
5919 callee_config.type = webrtc::PeerConnectionInterface::kRelay;
5920 callee_config.continual_gathering_policy = PeerConnection::GATHER_CONTINUALLY;
Qingsi Wang1fe119f2019-05-31 16:55:33 -07005921 callee_config.surface_ice_candidates_on_ice_transport_type_changed = true;
Qingsi Wangc129c352019-04-18 10:41:58 -07005922
5923 ASSERT_TRUE(
5924 CreatePeerConnectionWrappersWithConfig(caller_config, callee_config));
5925
5926 // Do normal offer/answer and wait for ICE to complete.
5927 ConnectFakeSignaling();
5928 caller()->AddAudioVideoTracks();
5929 callee()->AddAudioVideoTracks();
5930 caller()->CreateAndSetAndSignalOffer();
5931 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5932 // Since we are doing continual gathering, the ICE transport does not reach
5933 // kIceGatheringComplete (see
5934 // P2PTransportChannel::OnCandidatesAllocationDone), and consequently not
5935 // kIceConnectionComplete.
5936 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
5937 caller()->ice_connection_state(), kDefaultTimeout);
5938 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
5939 callee()->ice_connection_state(), kDefaultTimeout);
5940 // Note that we cannot use the metric
5941 // |WebRTC.PeerConnection.CandidatePairType_UDP| in this test since this
5942 // metric is only populated when we reach kIceConnectionComplete in the
5943 // current implementation.
5944 EXPECT_EQ(cricket::RELAY_PORT_TYPE,
5945 caller()->last_candidate_gathered().type());
5946 EXPECT_EQ(cricket::RELAY_PORT_TYPE,
5947 callee()->last_candidate_gathered().type());
5948
5949 // Loosen the caller's candidate filter.
5950 caller_config = caller()->pc()->GetConfiguration();
5951 caller_config.type = webrtc::PeerConnectionInterface::kAll;
5952 caller()->pc()->SetConfiguration(caller_config);
5953 // We should have gathered a new host candidate.
5954 EXPECT_EQ_WAIT(cricket::LOCAL_PORT_TYPE,
5955 caller()->last_candidate_gathered().type(), kDefaultTimeout);
5956
5957 // Loosen the callee's candidate filter.
5958 callee_config = callee()->pc()->GetConfiguration();
5959 callee_config.type = webrtc::PeerConnectionInterface::kAll;
5960 callee()->pc()->SetConfiguration(callee_config);
5961 EXPECT_EQ_WAIT(cricket::LOCAL_PORT_TYPE,
5962 callee()->last_candidate_gathered().type(), kDefaultTimeout);
5963}
5964
Eldar Relloda13ea22019-06-01 12:23:43 +03005965TEST_P(PeerConnectionIntegrationTest, OnIceCandidateError) {
Eldar Relloda13ea22019-06-01 12:23:43 +03005966 static const rtc::SocketAddress turn_server_internal_address{"88.88.88.0",
5967 3478};
5968 static const rtc::SocketAddress turn_server_external_address{"88.88.88.1", 0};
5969
5970 CreateTurnServer(turn_server_internal_address, turn_server_external_address);
5971
5972 webrtc::PeerConnectionInterface::IceServer ice_server;
5973 ice_server.urls.push_back("turn:88.88.88.0:3478");
5974 ice_server.username = "test";
5975 ice_server.password = "123";
5976
5977 PeerConnectionInterface::RTCConfiguration caller_config;
5978 caller_config.servers.push_back(ice_server);
5979 caller_config.type = webrtc::PeerConnectionInterface::kRelay;
5980 caller_config.continual_gathering_policy = PeerConnection::GATHER_CONTINUALLY;
5981
5982 PeerConnectionInterface::RTCConfiguration callee_config;
5983 callee_config.servers.push_back(ice_server);
5984 callee_config.type = webrtc::PeerConnectionInterface::kRelay;
5985 callee_config.continual_gathering_policy = PeerConnection::GATHER_CONTINUALLY;
5986
5987 ASSERT_TRUE(
5988 CreatePeerConnectionWrappersWithConfig(caller_config, callee_config));
5989
5990 // Do normal offer/answer and wait for ICE to complete.
5991 ConnectFakeSignaling();
5992 caller()->AddAudioVideoTracks();
5993 callee()->AddAudioVideoTracks();
5994 caller()->CreateAndSetAndSignalOffer();
5995 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5996 EXPECT_EQ_WAIT(401, caller()->error_event().error_code, kDefaultTimeout);
5997 EXPECT_EQ("Unauthorized", caller()->error_event().error_text);
5998 EXPECT_EQ("turn:88.88.88.0:3478?transport=udp", caller()->error_event().url);
5999 EXPECT_NE(std::string::npos,
6000 caller()->error_event().host_candidate.find(":"));
6001}
6002
Eldar Rello16d4c4d2019-10-09 18:29:44 +03006003TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
6004 AudioKeepsFlowingAfterImplicitRollback) {
6005 PeerConnectionInterface::RTCConfiguration config;
6006 config.sdp_semantics = SdpSemantics::kUnifiedPlan;
6007 config.enable_implicit_rollback = true;
6008 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
6009 ConnectFakeSignaling();
6010 caller()->AddAudioTrack();
6011 callee()->AddAudioTrack();
6012 caller()->CreateAndSetAndSignalOffer();
6013 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
6014 MediaExpectations media_expectations;
6015 media_expectations.ExpectBidirectionalAudio();
6016 ASSERT_TRUE(ExpectNewFrames(media_expectations));
6017 SetSignalIceCandidates(false); // Workaround candidate outrace sdp.
6018 caller()->AddVideoTrack();
6019 callee()->AddVideoTrack();
6020 rtc::scoped_refptr<MockSetSessionDescriptionObserver> observer(
6021 new rtc::RefCountedObject<MockSetSessionDescriptionObserver>());
6022 callee()->pc()->SetLocalDescription(observer,
6023 callee()->CreateOfferAndWait().release());
6024 EXPECT_TRUE_WAIT(observer->called(), kDefaultTimeout);
6025 caller()->CreateAndSetAndSignalOffer(); // Implicit rollback.
6026 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
6027 ASSERT_TRUE(ExpectNewFrames(media_expectations));
6028}
6029
6030TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
6031 ImplicitRollbackVisitsStableState) {
6032 RTCConfiguration config;
6033 config.sdp_semantics = SdpSemantics::kUnifiedPlan;
6034 config.enable_implicit_rollback = true;
6035
6036 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
6037
6038 rtc::scoped_refptr<MockSetSessionDescriptionObserver> sld_observer(
6039 new rtc::RefCountedObject<MockSetSessionDescriptionObserver>());
6040 callee()->pc()->SetLocalDescription(sld_observer,
6041 callee()->CreateOfferAndWait().release());
6042 EXPECT_TRUE_WAIT(sld_observer->called(), kDefaultTimeout);
6043 EXPECT_EQ(sld_observer->error(), "");
6044
6045 rtc::scoped_refptr<MockSetSessionDescriptionObserver> srd_observer(
6046 new rtc::RefCountedObject<MockSetSessionDescriptionObserver>());
6047 callee()->pc()->SetRemoteDescription(
6048 srd_observer, caller()->CreateOfferAndWait().release());
6049 EXPECT_TRUE_WAIT(srd_observer->called(), kDefaultTimeout);
6050 EXPECT_EQ(srd_observer->error(), "");
6051
6052 EXPECT_THAT(callee()->peer_connection_signaling_state_history(),
6053 ElementsAre(PeerConnectionInterface::kHaveLocalOffer,
6054 PeerConnectionInterface::kStable,
6055 PeerConnectionInterface::kHaveRemoteOffer));
6056}
6057
Mirko Bonadeic84f6612019-01-31 12:20:57 +01006058INSTANTIATE_TEST_SUITE_P(PeerConnectionIntegrationTest,
6059 PeerConnectionIntegrationTest,
6060 Values(SdpSemantics::kPlanB,
6061 SdpSemantics::kUnifiedPlan));
Steve Antond3679212018-01-17 17:41:02 -08006062
Steve Anton74255ff2018-01-24 18:32:57 -08006063// Tests that verify interoperability between Plan B and Unified Plan
6064// PeerConnections.
6065class PeerConnectionIntegrationInteropTest
Seth Hampson2f0d7022018-02-20 11:54:42 -08006066 : public PeerConnectionIntegrationBaseTest,
Steve Anton74255ff2018-01-24 18:32:57 -08006067 public ::testing::WithParamInterface<
6068 std::tuple<SdpSemantics, SdpSemantics>> {
6069 protected:
Seth Hampson2f0d7022018-02-20 11:54:42 -08006070 // Setting the SdpSemantics for the base test to kDefault does not matter
6071 // because we specify not to use the test semantics when creating
6072 // PeerConnectionWrappers.
Steve Anton74255ff2018-01-24 18:32:57 -08006073 PeerConnectionIntegrationInteropTest()
Steve Anton3acffc32018-04-12 17:21:03 -07006074 : PeerConnectionIntegrationBaseTest(SdpSemantics::kPlanB),
Seth Hampson2f0d7022018-02-20 11:54:42 -08006075 caller_semantics_(std::get<0>(GetParam())),
Steve Anton74255ff2018-01-24 18:32:57 -08006076 callee_semantics_(std::get<1>(GetParam())) {}
6077
6078 bool CreatePeerConnectionWrappersWithSemantics() {
Steve Anton3acffc32018-04-12 17:21:03 -07006079 return CreatePeerConnectionWrappersWithSdpSemantics(caller_semantics_,
6080 callee_semantics_);
Steve Anton74255ff2018-01-24 18:32:57 -08006081 }
6082
6083 const SdpSemantics caller_semantics_;
6084 const SdpSemantics callee_semantics_;
6085};
6086
6087TEST_P(PeerConnectionIntegrationInteropTest, NoMediaLocalToNoMediaRemote) {
6088 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
6089 ConnectFakeSignaling();
6090
6091 caller()->CreateAndSetAndSignalOffer();
6092 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
6093}
6094
6095TEST_P(PeerConnectionIntegrationInteropTest, OneAudioLocalToNoMediaRemote) {
6096 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
6097 ConnectFakeSignaling();
6098 auto audio_sender = caller()->AddAudioTrack();
6099
6100 caller()->CreateAndSetAndSignalOffer();
6101 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
6102
6103 // Verify that one audio receiver has been created on the remote and that it
6104 // has the same track ID as the sending track.
6105 auto receivers = callee()->pc()->GetReceivers();
6106 ASSERT_EQ(1u, receivers.size());
6107 EXPECT_EQ(cricket::MEDIA_TYPE_AUDIO, receivers[0]->media_type());
6108 EXPECT_EQ(receivers[0]->track()->id(), audio_sender->track()->id());
6109
Seth Hampson2f0d7022018-02-20 11:54:42 -08006110 MediaExpectations media_expectations;
6111 media_expectations.CalleeExpectsSomeAudio();
6112 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08006113}
6114
6115TEST_P(PeerConnectionIntegrationInteropTest, OneAudioOneVideoToNoMediaRemote) {
6116 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
6117 ConnectFakeSignaling();
6118 auto video_sender = caller()->AddVideoTrack();
6119 auto audio_sender = caller()->AddAudioTrack();
6120
6121 caller()->CreateAndSetAndSignalOffer();
6122 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
6123
6124 // Verify that one audio and one video receiver have been created on the
6125 // remote and that they have the same track IDs as the sending tracks.
6126 auto audio_receivers =
6127 callee()->GetReceiversOfType(cricket::MEDIA_TYPE_AUDIO);
6128 ASSERT_EQ(1u, audio_receivers.size());
6129 EXPECT_EQ(audio_receivers[0]->track()->id(), audio_sender->track()->id());
6130 auto video_receivers =
6131 callee()->GetReceiversOfType(cricket::MEDIA_TYPE_VIDEO);
6132 ASSERT_EQ(1u, video_receivers.size());
6133 EXPECT_EQ(video_receivers[0]->track()->id(), video_sender->track()->id());
6134
Seth Hampson2f0d7022018-02-20 11:54:42 -08006135 MediaExpectations media_expectations;
6136 media_expectations.CalleeExpectsSomeAudioAndVideo();
6137 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08006138}
6139
6140TEST_P(PeerConnectionIntegrationInteropTest,
6141 OneAudioOneVideoLocalToOneAudioOneVideoRemote) {
6142 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
6143 ConnectFakeSignaling();
6144 caller()->AddAudioVideoTracks();
6145 callee()->AddAudioVideoTracks();
6146
6147 caller()->CreateAndSetAndSignalOffer();
6148 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
6149
Seth Hampson2f0d7022018-02-20 11:54:42 -08006150 MediaExpectations media_expectations;
6151 media_expectations.ExpectBidirectionalAudioAndVideo();
6152 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08006153}
6154
6155TEST_P(PeerConnectionIntegrationInteropTest,
6156 ReverseRolesOneAudioLocalToOneVideoRemote) {
6157 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
6158 ConnectFakeSignaling();
6159 caller()->AddAudioTrack();
6160 callee()->AddVideoTrack();
6161
6162 caller()->CreateAndSetAndSignalOffer();
6163 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
6164
6165 // Verify that only the audio track has been negotiated.
6166 EXPECT_EQ(0u, caller()->GetReceiversOfType(cricket::MEDIA_TYPE_VIDEO).size());
6167 // Might also check that the callee's NegotiationNeeded flag is set.
6168
6169 // Reverse roles.
6170 callee()->CreateAndSetAndSignalOffer();
6171 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
6172
Seth Hampson2f0d7022018-02-20 11:54:42 -08006173 MediaExpectations media_expectations;
6174 media_expectations.CallerExpectsSomeVideo();
6175 media_expectations.CalleeExpectsSomeAudio();
6176 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08006177}
6178
Mirko Bonadeic84f6612019-01-31 12:20:57 +01006179INSTANTIATE_TEST_SUITE_P(
Steve Antonba42e992018-04-09 14:10:01 -07006180 PeerConnectionIntegrationTest,
6181 PeerConnectionIntegrationInteropTest,
6182 Values(std::make_tuple(SdpSemantics::kPlanB, SdpSemantics::kUnifiedPlan),
6183 std::make_tuple(SdpSemantics::kUnifiedPlan, SdpSemantics::kPlanB)));
6184
6185// Test that if the Unified Plan side offers two video tracks then the Plan B
6186// side will only see the first one and ignore the second.
6187TEST_F(PeerConnectionIntegrationTestPlanB, TwoVideoUnifiedPlanToNoMediaPlanB) {
Steve Anton3acffc32018-04-12 17:21:03 -07006188 ASSERT_TRUE(CreatePeerConnectionWrappersWithSdpSemantics(
6189 SdpSemantics::kUnifiedPlan, SdpSemantics::kPlanB));
Steve Anton74255ff2018-01-24 18:32:57 -08006190 ConnectFakeSignaling();
6191 auto first_sender = caller()->AddVideoTrack();
6192 caller()->AddVideoTrack();
6193
6194 caller()->CreateAndSetAndSignalOffer();
6195 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
6196
6197 // Verify that there is only one receiver and it corresponds to the first
6198 // added track.
6199 auto receivers = callee()->pc()->GetReceivers();
6200 ASSERT_EQ(1u, receivers.size());
6201 EXPECT_TRUE(receivers[0]->track()->enabled());
6202 EXPECT_EQ(first_sender->track()->id(), receivers[0]->track()->id());
6203
Seth Hampson2f0d7022018-02-20 11:54:42 -08006204 MediaExpectations media_expectations;
6205 media_expectations.CalleeExpectsSomeVideo();
6206 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08006207}
6208
Steve Anton2bed3972019-01-04 17:04:30 -08006209// Test that if the initial offer tagged BUNDLE section is rejected due to its
6210// associated RtpTransceiver being stopped and another transceiver is added,
6211// then renegotiation causes the callee to receive the new video track without
6212// error.
6213// This is a regression test for bugs.webrtc.org/9954
6214TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
6215 ReOfferWithStoppedBundleTaggedTransceiver) {
6216 RTCConfiguration config;
6217 config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
6218 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
6219 ConnectFakeSignaling();
6220 auto audio_transceiver_or_error =
6221 caller()->pc()->AddTransceiver(caller()->CreateLocalAudioTrack());
6222 ASSERT_TRUE(audio_transceiver_or_error.ok());
6223 auto audio_transceiver = audio_transceiver_or_error.MoveValue();
6224
6225 caller()->CreateAndSetAndSignalOffer();
6226 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
6227 {
6228 MediaExpectations media_expectations;
6229 media_expectations.CalleeExpectsSomeAudio();
6230 ASSERT_TRUE(ExpectNewFrames(media_expectations));
6231 }
6232
6233 audio_transceiver->Stop();
6234 caller()->pc()->AddTransceiver(caller()->CreateLocalVideoTrack());
6235
6236 caller()->CreateAndSetAndSignalOffer();
6237 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
6238 {
6239 MediaExpectations media_expectations;
6240 media_expectations.CalleeExpectsSomeVideo();
6241 ASSERT_TRUE(ExpectNewFrames(media_expectations));
6242 }
6243}
6244
Harald Alvestrandd61f2a72019-05-08 20:20:59 +02006245#ifdef HAVE_SCTP
6246
6247TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
6248 EndToEndCallWithBundledSctpDataChannel) {
6249 ASSERT_TRUE(CreatePeerConnectionWrappers());
6250 ConnectFakeSignaling();
6251 caller()->CreateDataChannel();
6252 caller()->AddAudioVideoTracks();
6253 callee()->AddAudioVideoTracks();
6254 caller()->SetGeneratedSdpMunger(MakeSpecCompliantSctpOffer);
6255 caller()->CreateAndSetAndSignalOffer();
6256 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
6257 // Ensure that media and data are multiplexed on the same DTLS transport.
6258 // This only works on Unified Plan, because transports are not exposed in plan
6259 // B.
6260 auto sctp_info = caller()->pc()->GetSctpTransport()->Information();
6261 EXPECT_EQ(sctp_info.dtls_transport(),
6262 caller()->pc()->GetSenders()[0]->dtls_transport());
6263}
6264
6265#endif // HAVE_SCTP
6266
deadbeef1dcb1642017-03-29 21:08:16 -07006267} // namespace
Mirko Bonadeiab64e8a2018-12-12 12:10:18 +01006268} // namespace webrtc
deadbeef1dcb1642017-03-29 21:08:16 -07006269
6270#endif // if !defined(THREAD_SANITIZER)