blob: 536ad01ebd7cec0d0ded46095875749e3f497768 [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 Rello5ab79e62019-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 Rello5ab79e62019-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 Rello5ab79e62019-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 Rello5ab79e62019-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 Rello5ab79e62019-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,
Philipp Hancke2ebbff82019-10-26 06:12:55 +02001638 bool aes_ctr_enabled,
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07001639 int expected_cipher_suite) {
1640 PeerConnectionFactory::Options caller_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07001641 caller_options.crypto_options.srtp.enable_gcm_crypto_suites =
1642 local_gcm_enabled;
Philipp Hancke2ebbff82019-10-26 06:12:55 +02001643 caller_options.crypto_options.srtp.enable_aes128_sha1_80_crypto_cipher =
1644 aes_ctr_enabled;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07001645 PeerConnectionFactory::Options callee_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07001646 callee_options.crypto_options.srtp.enable_gcm_crypto_suites =
1647 remote_gcm_enabled;
Philipp Hancke2ebbff82019-10-26 06:12:55 +02001648 callee_options.crypto_options.srtp.enable_aes128_sha1_80_crypto_cipher =
1649 aes_ctr_enabled;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07001650 TestNegotiatedCipherSuite(caller_options, callee_options,
1651 expected_cipher_suite);
1652 }
1653
Seth Hampson2f0d7022018-02-20 11:54:42 -08001654 protected:
Steve Anton3acffc32018-04-12 17:21:03 -07001655 SdpSemantics sdp_semantics_;
Seth Hampson2f0d7022018-02-20 11:54:42 -08001656
deadbeef1dcb1642017-03-29 21:08:16 -07001657 private:
1658 // |ss_| is used by |network_thread_| so it must be destroyed later.
deadbeef1dcb1642017-03-29 21:08:16 -07001659 std::unique_ptr<rtc::VirtualSocketServer> ss_;
Steve Antonede9ca52017-10-16 13:04:27 -07001660 std::unique_ptr<rtc::FirewallSocketServer> fss_;
deadbeef1dcb1642017-03-29 21:08:16 -07001661 // |network_thread_| and |worker_thread_| are used by both
1662 // |caller_| and |callee_| so they must be destroyed
1663 // later.
1664 std::unique_ptr<rtc::Thread> network_thread_;
1665 std::unique_ptr<rtc::Thread> worker_thread_;
Seth Hampsonaed71642018-06-11 07:41:32 -07001666 // The turn servers and turn customizers should be accessed & deleted on the
1667 // network thread to avoid a race with the socket read/write that occurs
1668 // on the network thread.
1669 std::vector<std::unique_ptr<cricket::TestTurnServer>> turn_servers_;
1670 std::vector<std::unique_ptr<cricket::TestTurnCustomizer>> turn_customizers_;
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001671 webrtc::MediaTransportPair loopback_media_transports_;
deadbeef1dcb1642017-03-29 21:08:16 -07001672 std::unique_ptr<PeerConnectionWrapper> caller_;
1673 std::unique_ptr<PeerConnectionWrapper> callee_;
1674};
1675
Seth Hampson2f0d7022018-02-20 11:54:42 -08001676class PeerConnectionIntegrationTest
1677 : public PeerConnectionIntegrationBaseTest,
1678 public ::testing::WithParamInterface<SdpSemantics> {
1679 protected:
1680 PeerConnectionIntegrationTest()
1681 : PeerConnectionIntegrationBaseTest(GetParam()) {}
1682};
1683
1684class PeerConnectionIntegrationTestPlanB
1685 : public PeerConnectionIntegrationBaseTest {
1686 protected:
1687 PeerConnectionIntegrationTestPlanB()
1688 : PeerConnectionIntegrationBaseTest(SdpSemantics::kPlanB) {}
1689};
1690
1691class PeerConnectionIntegrationTestUnifiedPlan
1692 : public PeerConnectionIntegrationBaseTest {
1693 protected:
1694 PeerConnectionIntegrationTestUnifiedPlan()
1695 : PeerConnectionIntegrationBaseTest(SdpSemantics::kUnifiedPlan) {}
1696};
1697
deadbeef1dcb1642017-03-29 21:08:16 -07001698// Test the OnFirstPacketReceived callback from audio/video RtpReceivers. This
1699// includes testing that the callback is invoked if an observer is connected
1700// after the first packet has already been received.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001701TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07001702 RtpReceiverObserverOnFirstPacketReceived) {
1703 ASSERT_TRUE(CreatePeerConnectionWrappers());
1704 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001705 caller()->AddAudioVideoTracks();
1706 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001707 // Start offer/answer exchange and wait for it to complete.
1708 caller()->CreateAndSetAndSignalOffer();
1709 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1710 // Should be one receiver each for audio/video.
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02001711 EXPECT_EQ(2U, caller()->rtp_receiver_observers().size());
1712 EXPECT_EQ(2U, callee()->rtp_receiver_observers().size());
deadbeef1dcb1642017-03-29 21:08:16 -07001713 // Wait for all "first packet received" callbacks to be fired.
1714 EXPECT_TRUE_WAIT(
Steve Anton64b626b2019-01-28 17:25:26 -08001715 absl::c_all_of(caller()->rtp_receiver_observers(),
1716 [](const std::unique_ptr<MockRtpReceiverObserver>& o) {
1717 return o->first_packet_received();
1718 }),
deadbeef1dcb1642017-03-29 21:08:16 -07001719 kMaxWaitForFramesMs);
1720 EXPECT_TRUE_WAIT(
Steve Anton64b626b2019-01-28 17:25:26 -08001721 absl::c_all_of(callee()->rtp_receiver_observers(),
1722 [](const std::unique_ptr<MockRtpReceiverObserver>& o) {
1723 return o->first_packet_received();
1724 }),
deadbeef1dcb1642017-03-29 21:08:16 -07001725 kMaxWaitForFramesMs);
1726 // If new observers are set after the first packet was already received, the
1727 // callback should still be invoked.
1728 caller()->ResetRtpReceiverObservers();
1729 callee()->ResetRtpReceiverObservers();
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02001730 EXPECT_EQ(2U, caller()->rtp_receiver_observers().size());
1731 EXPECT_EQ(2U, callee()->rtp_receiver_observers().size());
deadbeef1dcb1642017-03-29 21:08:16 -07001732 EXPECT_TRUE(
Steve Anton64b626b2019-01-28 17:25:26 -08001733 absl::c_all_of(caller()->rtp_receiver_observers(),
1734 [](const std::unique_ptr<MockRtpReceiverObserver>& o) {
1735 return o->first_packet_received();
1736 }));
deadbeef1dcb1642017-03-29 21:08:16 -07001737 EXPECT_TRUE(
Steve Anton64b626b2019-01-28 17:25:26 -08001738 absl::c_all_of(callee()->rtp_receiver_observers(),
1739 [](const std::unique_ptr<MockRtpReceiverObserver>& o) {
1740 return o->first_packet_received();
1741 }));
deadbeef1dcb1642017-03-29 21:08:16 -07001742}
1743
1744class DummyDtmfObserver : public DtmfSenderObserverInterface {
1745 public:
1746 DummyDtmfObserver() : completed_(false) {}
1747
1748 // Implements DtmfSenderObserverInterface.
1749 void OnToneChange(const std::string& tone) override {
1750 tones_.push_back(tone);
1751 if (tone.empty()) {
1752 completed_ = true;
1753 }
1754 }
1755
1756 const std::vector<std::string>& tones() const { return tones_; }
1757 bool completed() const { return completed_; }
1758
1759 private:
1760 bool completed_;
1761 std::vector<std::string> tones_;
1762};
1763
1764// Assumes |sender| already has an audio track added and the offer/answer
1765// exchange is done.
1766void TestDtmfFromSenderToReceiver(PeerConnectionWrapper* sender,
1767 PeerConnectionWrapper* receiver) {
Steve Anton15324772018-01-16 10:26:49 -08001768 // We should be able to get a DTMF sender from the local sender.
1769 rtc::scoped_refptr<DtmfSenderInterface> dtmf_sender =
1770 sender->pc()->GetSenders().at(0)->GetDtmfSender();
1771 ASSERT_TRUE(dtmf_sender);
deadbeef1dcb1642017-03-29 21:08:16 -07001772 DummyDtmfObserver observer;
deadbeef1dcb1642017-03-29 21:08:16 -07001773 dtmf_sender->RegisterObserver(&observer);
1774
1775 // Test the DtmfSender object just created.
1776 EXPECT_TRUE(dtmf_sender->CanInsertDtmf());
1777 EXPECT_TRUE(dtmf_sender->InsertDtmf("1a", 100, 50));
1778
1779 EXPECT_TRUE_WAIT(observer.completed(), kDefaultTimeout);
1780 std::vector<std::string> tones = {"1", "a", ""};
1781 EXPECT_EQ(tones, observer.tones());
1782 dtmf_sender->UnregisterObserver();
1783 // TODO(deadbeef): Verify the tones were actually received end-to-end.
1784}
1785
1786// Verifies the DtmfSenderObserver callbacks for a DtmfSender (one in each
1787// direction).
Seth Hampson2f0d7022018-02-20 11:54:42 -08001788TEST_P(PeerConnectionIntegrationTest, DtmfSenderObserver) {
deadbeef1dcb1642017-03-29 21:08:16 -07001789 ASSERT_TRUE(CreatePeerConnectionWrappers());
1790 ConnectFakeSignaling();
1791 // Only need audio for DTMF.
Steve Anton15324772018-01-16 10:26:49 -08001792 caller()->AddAudioTrack();
1793 callee()->AddAudioTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07001794 caller()->CreateAndSetAndSignalOffer();
1795 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
deadbeef71452802017-05-07 17:21:01 -07001796 // DTLS must finish before the DTMF sender can be used reliably.
1797 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07001798 TestDtmfFromSenderToReceiver(caller(), callee());
1799 TestDtmfFromSenderToReceiver(callee(), caller());
1800}
1801
1802// Basic end-to-end test, verifying media can be encoded/transmitted/decoded
1803// between two connections, using DTLS-SRTP.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001804TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithDtls) {
deadbeef1dcb1642017-03-29 21:08:16 -07001805 ASSERT_TRUE(CreatePeerConnectionWrappers());
1806 ConnectFakeSignaling();
Harald Alvestrand194939b2018-01-24 16:04:13 +01001807
deadbeef1dcb1642017-03-29 21:08:16 -07001808 // Do normal offer/answer and wait for some frames to be received in each
1809 // direction.
Steve Anton15324772018-01-16 10:26:49 -08001810 caller()->AddAudioVideoTracks();
1811 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001812 caller()->CreateAndSetAndSignalOffer();
1813 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001814 MediaExpectations media_expectations;
1815 media_expectations.ExpectBidirectionalAudioAndVideo();
1816 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Qingsi Wang7fc821d2018-07-12 12:54:53 -07001817 EXPECT_LE(2, webrtc::metrics::NumEvents("WebRTC.PeerConnection.KeyProtocol",
1818 webrtc::kEnumCounterKeyProtocolDtls));
1819 EXPECT_EQ(0, webrtc::metrics::NumEvents("WebRTC.PeerConnection.KeyProtocol",
1820 webrtc::kEnumCounterKeyProtocolSdes));
deadbeef1dcb1642017-03-29 21:08:16 -07001821}
1822
1823// Uses SDES instead of DTLS for key agreement.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001824TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithSdes) {
deadbeef1dcb1642017-03-29 21:08:16 -07001825 PeerConnectionInterface::RTCConfiguration sdes_config;
1826 sdes_config.enable_dtls_srtp.emplace(false);
1827 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(sdes_config, sdes_config));
1828 ConnectFakeSignaling();
1829
1830 // Do normal offer/answer and wait for some frames to be received in each
1831 // direction.
Steve Anton15324772018-01-16 10:26:49 -08001832 caller()->AddAudioVideoTracks();
1833 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001834 caller()->CreateAndSetAndSignalOffer();
1835 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001836 MediaExpectations media_expectations;
1837 media_expectations.ExpectBidirectionalAudioAndVideo();
1838 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Qingsi Wang7fc821d2018-07-12 12:54:53 -07001839 EXPECT_LE(2, webrtc::metrics::NumEvents("WebRTC.PeerConnection.KeyProtocol",
1840 webrtc::kEnumCounterKeyProtocolSdes));
1841 EXPECT_EQ(0, webrtc::metrics::NumEvents("WebRTC.PeerConnection.KeyProtocol",
1842 webrtc::kEnumCounterKeyProtocolDtls));
deadbeef1dcb1642017-03-29 21:08:16 -07001843}
1844
Steve Anton9a44b2d2019-07-12 12:58:30 -07001845// Basic end-to-end test specifying the |enable_encrypted_rtp_header_extensions|
1846// option to offer encrypted versions of all header extensions alongside the
1847// unencrypted versions.
1848TEST_P(PeerConnectionIntegrationTest,
1849 EndToEndCallWithEncryptedRtpHeaderExtensions) {
1850 CryptoOptions crypto_options;
1851 crypto_options.srtp.enable_encrypted_rtp_header_extensions = true;
1852 PeerConnectionInterface::RTCConfiguration config;
1853 config.crypto_options = crypto_options;
1854 // Note: This allows offering >14 RTP header extensions.
1855 config.offer_extmap_allow_mixed = true;
1856 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
1857 ConnectFakeSignaling();
1858
1859 // Do normal offer/answer and wait for some frames to be received in each
1860 // direction.
1861 caller()->AddAudioVideoTracks();
1862 callee()->AddAudioVideoTracks();
1863 caller()->CreateAndSetAndSignalOffer();
1864 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1865 MediaExpectations media_expectations;
1866 media_expectations.ExpectBidirectionalAudioAndVideo();
1867 ASSERT_TRUE(ExpectNewFrames(media_expectations));
1868}
1869
Steve Anton8c0f7a72017-10-03 10:03:10 -07001870// Tests that the GetRemoteAudioSSLCertificate method returns the remote DTLS
1871// certificate once the DTLS handshake has finished.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001872TEST_P(PeerConnectionIntegrationTest,
Steve Anton8c0f7a72017-10-03 10:03:10 -07001873 GetRemoteAudioSSLCertificateReturnsExchangedCertificate) {
1874 auto GetRemoteAudioSSLCertificate = [](PeerConnectionWrapper* wrapper) {
1875 auto pci = reinterpret_cast<PeerConnectionProxy*>(wrapper->pc());
1876 auto pc = reinterpret_cast<PeerConnection*>(pci->internal());
1877 return pc->GetRemoteAudioSSLCertificate();
1878 };
Zhi Huang70b820f2018-01-27 14:16:15 -08001879 auto GetRemoteAudioSSLCertChain = [](PeerConnectionWrapper* wrapper) {
1880 auto pci = reinterpret_cast<PeerConnectionProxy*>(wrapper->pc());
1881 auto pc = reinterpret_cast<PeerConnection*>(pci->internal());
1882 return pc->GetRemoteAudioSSLCertChain();
1883 };
Steve Anton8c0f7a72017-10-03 10:03:10 -07001884
1885 auto caller_cert = rtc::RTCCertificate::FromPEM(kRsaPems[0]);
1886 auto callee_cert = rtc::RTCCertificate::FromPEM(kRsaPems[1]);
1887
1888 // Configure each side with a known certificate so they can be compared later.
1889 PeerConnectionInterface::RTCConfiguration caller_config;
1890 caller_config.enable_dtls_srtp.emplace(true);
1891 caller_config.certificates.push_back(caller_cert);
1892 PeerConnectionInterface::RTCConfiguration callee_config;
1893 callee_config.enable_dtls_srtp.emplace(true);
1894 callee_config.certificates.push_back(callee_cert);
1895 ASSERT_TRUE(
1896 CreatePeerConnectionWrappersWithConfig(caller_config, callee_config));
1897 ConnectFakeSignaling();
1898
1899 // When first initialized, there should not be a remote SSL certificate (and
1900 // calling this method should not crash).
1901 EXPECT_EQ(nullptr, GetRemoteAudioSSLCertificate(caller()));
1902 EXPECT_EQ(nullptr, GetRemoteAudioSSLCertificate(callee()));
Zhi Huang70b820f2018-01-27 14:16:15 -08001903 EXPECT_EQ(nullptr, GetRemoteAudioSSLCertChain(caller()));
1904 EXPECT_EQ(nullptr, GetRemoteAudioSSLCertChain(callee()));
Steve Anton8c0f7a72017-10-03 10:03:10 -07001905
Steve Anton15324772018-01-16 10:26:49 -08001906 caller()->AddAudioTrack();
1907 callee()->AddAudioTrack();
Steve Anton8c0f7a72017-10-03 10:03:10 -07001908 caller()->CreateAndSetAndSignalOffer();
1909 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1910 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
1911
1912 // Once DTLS has been connected, each side should return the other's SSL
1913 // certificate when calling GetRemoteAudioSSLCertificate.
1914
1915 auto caller_remote_cert = GetRemoteAudioSSLCertificate(caller());
1916 ASSERT_TRUE(caller_remote_cert);
Benjamin Wright6c6c9df2018-10-25 01:16:26 -07001917 EXPECT_EQ(callee_cert->GetSSLCertificate().ToPEMString(),
Steve Anton8c0f7a72017-10-03 10:03:10 -07001918 caller_remote_cert->ToPEMString());
1919
1920 auto callee_remote_cert = GetRemoteAudioSSLCertificate(callee());
1921 ASSERT_TRUE(callee_remote_cert);
Benjamin Wright6c6c9df2018-10-25 01:16:26 -07001922 EXPECT_EQ(caller_cert->GetSSLCertificate().ToPEMString(),
Steve Anton8c0f7a72017-10-03 10:03:10 -07001923 callee_remote_cert->ToPEMString());
Zhi Huang70b820f2018-01-27 14:16:15 -08001924
1925 auto caller_remote_cert_chain = GetRemoteAudioSSLCertChain(caller());
1926 ASSERT_TRUE(caller_remote_cert_chain);
1927 ASSERT_EQ(1U, caller_remote_cert_chain->GetSize());
1928 auto remote_cert = &caller_remote_cert_chain->Get(0);
Benjamin Wright6c6c9df2018-10-25 01:16:26 -07001929 EXPECT_EQ(callee_cert->GetSSLCertificate().ToPEMString(),
Zhi Huang70b820f2018-01-27 14:16:15 -08001930 remote_cert->ToPEMString());
1931
1932 auto callee_remote_cert_chain = GetRemoteAudioSSLCertChain(callee());
1933 ASSERT_TRUE(callee_remote_cert_chain);
1934 ASSERT_EQ(1U, callee_remote_cert_chain->GetSize());
1935 remote_cert = &callee_remote_cert_chain->Get(0);
Benjamin Wright6c6c9df2018-10-25 01:16:26 -07001936 EXPECT_EQ(caller_cert->GetSSLCertificate().ToPEMString(),
Zhi Huang70b820f2018-01-27 14:16:15 -08001937 remote_cert->ToPEMString());
Steve Anton8c0f7a72017-10-03 10:03:10 -07001938}
1939
deadbeef1dcb1642017-03-29 21:08:16 -07001940// This test sets up a call between two parties with a source resolution of
1941// 1280x720 and verifies that a 16:9 aspect ratio is received.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001942TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07001943 Send1280By720ResolutionAndReceive16To9AspectRatio) {
1944 ASSERT_TRUE(CreatePeerConnectionWrappers());
1945 ConnectFakeSignaling();
1946
Niels Möller5c7efe72018-05-11 10:34:46 +02001947 // Add video tracks with 16:9 aspect ratio, size 1280 x 720.
1948 webrtc::FakePeriodicVideoSource::Config config;
1949 config.width = 1280;
1950 config.height = 720;
Johannes Kron965e7942018-09-13 15:36:20 +02001951 config.timestamp_offset_ms = rtc::TimeMillis();
Niels Möller5c7efe72018-05-11 10:34:46 +02001952 caller()->AddTrack(caller()->CreateLocalVideoTrackWithConfig(config));
1953 callee()->AddTrack(callee()->CreateLocalVideoTrackWithConfig(config));
deadbeef1dcb1642017-03-29 21:08:16 -07001954
1955 // Do normal offer/answer and wait for at least one frame to be received in
1956 // each direction.
1957 caller()->CreateAndSetAndSignalOffer();
1958 ASSERT_TRUE_WAIT(caller()->min_video_frames_received_per_track() > 0 &&
1959 callee()->min_video_frames_received_per_track() > 0,
1960 kMaxWaitForFramesMs);
1961
1962 // Check rendered aspect ratio.
1963 EXPECT_EQ(16.0 / 9, caller()->local_rendered_aspect_ratio());
1964 EXPECT_EQ(16.0 / 9, caller()->rendered_aspect_ratio());
1965 EXPECT_EQ(16.0 / 9, callee()->local_rendered_aspect_ratio());
1966 EXPECT_EQ(16.0 / 9, callee()->rendered_aspect_ratio());
1967}
1968
1969// This test sets up an one-way call, with media only from caller to
1970// callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001971TEST_P(PeerConnectionIntegrationTest, OneWayMediaCall) {
deadbeef1dcb1642017-03-29 21:08:16 -07001972 ASSERT_TRUE(CreatePeerConnectionWrappers());
1973 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001974 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001975 caller()->CreateAndSetAndSignalOffer();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001976 MediaExpectations media_expectations;
1977 media_expectations.CalleeExpectsSomeAudioAndVideo();
1978 media_expectations.CallerExpectsNoAudio();
1979 media_expectations.CallerExpectsNoVideo();
1980 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07001981}
1982
1983// This test sets up a audio call initially, with the callee rejecting video
1984// initially. Then later the callee decides to upgrade to audio/video, and
1985// initiates a new offer/answer exchange.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001986TEST_P(PeerConnectionIntegrationTest, AudioToVideoUpgrade) {
deadbeef1dcb1642017-03-29 21:08:16 -07001987 ASSERT_TRUE(CreatePeerConnectionWrappers());
1988 ConnectFakeSignaling();
1989 // Initially, offer an audio/video stream from the caller, but refuse to
1990 // send/receive video on the callee side.
Steve Anton15324772018-01-16 10:26:49 -08001991 caller()->AddAudioVideoTracks();
1992 callee()->AddAudioTrack();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001993 if (sdp_semantics_ == SdpSemantics::kPlanB) {
1994 PeerConnectionInterface::RTCOfferAnswerOptions options;
1995 options.offer_to_receive_video = 0;
1996 callee()->SetOfferAnswerOptions(options);
1997 } else {
1998 callee()->SetRemoteOfferHandler([this] {
1999 callee()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)->Stop();
2000 });
2001 }
deadbeef1dcb1642017-03-29 21:08:16 -07002002 // Do offer/answer and make sure audio is still received end-to-end.
2003 caller()->CreateAndSetAndSignalOffer();
2004 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002005 {
2006 MediaExpectations media_expectations;
2007 media_expectations.ExpectBidirectionalAudio();
2008 media_expectations.ExpectNoVideo();
2009 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2010 }
deadbeef1dcb1642017-03-29 21:08:16 -07002011 // Sanity check that the callee's description has a rejected video section.
2012 ASSERT_NE(nullptr, callee()->pc()->local_description());
2013 const ContentInfo* callee_video_content =
2014 GetFirstVideoContent(callee()->pc()->local_description()->description());
2015 ASSERT_NE(nullptr, callee_video_content);
2016 EXPECT_TRUE(callee_video_content->rejected);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002017
deadbeef1dcb1642017-03-29 21:08:16 -07002018 // Now negotiate with video and ensure negotiation succeeds, with video
2019 // frames and additional audio frames being received.
Steve Anton15324772018-01-16 10:26:49 -08002020 callee()->AddVideoTrack();
Seth Hampson2f0d7022018-02-20 11:54:42 -08002021 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2022 PeerConnectionInterface::RTCOfferAnswerOptions options;
2023 options.offer_to_receive_video = 1;
2024 callee()->SetOfferAnswerOptions(options);
2025 } else {
2026 callee()->SetRemoteOfferHandler(nullptr);
2027 caller()->SetRemoteOfferHandler([this] {
2028 // The caller creates a new transceiver to receive video on when receiving
2029 // the offer, but by default it is send only.
2030 auto transceivers = caller()->pc()->GetTransceivers();
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02002031 ASSERT_EQ(3U, transceivers.size());
Seth Hampson2f0d7022018-02-20 11:54:42 -08002032 ASSERT_EQ(cricket::MEDIA_TYPE_VIDEO,
2033 transceivers[2]->receiver()->media_type());
2034 transceivers[2]->sender()->SetTrack(caller()->CreateLocalVideoTrack());
2035 transceivers[2]->SetDirection(RtpTransceiverDirection::kSendRecv);
2036 });
2037 }
deadbeef1dcb1642017-03-29 21:08:16 -07002038 callee()->CreateAndSetAndSignalOffer();
2039 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002040 {
2041 // Expect additional audio frames to be received after the upgrade.
2042 MediaExpectations media_expectations;
2043 media_expectations.ExpectBidirectionalAudioAndVideo();
2044 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2045 }
deadbeef1dcb1642017-03-29 21:08:16 -07002046}
2047
deadbeef4389b4d2017-09-07 09:07:36 -07002048// Simpler than the above test; just add an audio track to an established
2049// video-only connection.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002050TEST_P(PeerConnectionIntegrationTest, AddAudioToVideoOnlyCall) {
deadbeef4389b4d2017-09-07 09:07:36 -07002051 ASSERT_TRUE(CreatePeerConnectionWrappers());
2052 ConnectFakeSignaling();
2053 // Do initial offer/answer with just a video track.
Steve Anton15324772018-01-16 10:26:49 -08002054 caller()->AddVideoTrack();
2055 callee()->AddVideoTrack();
deadbeef4389b4d2017-09-07 09:07:36 -07002056 caller()->CreateAndSetAndSignalOffer();
2057 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2058 // Now add an audio track and do another offer/answer.
Steve Anton15324772018-01-16 10:26:49 -08002059 caller()->AddAudioTrack();
2060 callee()->AddAudioTrack();
deadbeef4389b4d2017-09-07 09:07:36 -07002061 caller()->CreateAndSetAndSignalOffer();
2062 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2063 // Ensure both audio and video frames are received end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002064 MediaExpectations media_expectations;
2065 media_expectations.ExpectBidirectionalAudioAndVideo();
2066 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef4389b4d2017-09-07 09:07:36 -07002067}
2068
deadbeef1dcb1642017-03-29 21:08:16 -07002069// This test sets up a call that's transferred to a new caller with a different
2070// DTLS fingerprint.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002071TEST_P(PeerConnectionIntegrationTest, CallTransferredForCallee) {
deadbeef1dcb1642017-03-29 21:08:16 -07002072 ASSERT_TRUE(CreatePeerConnectionWrappers());
2073 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002074 caller()->AddAudioVideoTracks();
2075 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002076 caller()->CreateAndSetAndSignalOffer();
2077 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2078
2079 // Keep the original peer around which will still send packets to the
2080 // receiving client. These SRTP packets will be dropped.
2081 std::unique_ptr<PeerConnectionWrapper> original_peer(
2082 SetCallerPcWrapperAndReturnCurrent(
Seth Hampson2f0d7022018-02-20 11:54:42 -08002083 CreatePeerConnectionWrapperWithAlternateKey().release()));
deadbeef1dcb1642017-03-29 21:08:16 -07002084 // TODO(deadbeef): Why do we call Close here? That goes against the comment
2085 // directly above.
2086 original_peer->pc()->Close();
2087
2088 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002089 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002090 caller()->CreateAndSetAndSignalOffer();
2091 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2092 // Wait for some additional frames to be transmitted end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002093 MediaExpectations media_expectations;
2094 media_expectations.ExpectBidirectionalAudioAndVideo();
2095 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002096}
2097
2098// This test sets up a call that's transferred to a new callee with a different
2099// DTLS fingerprint.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002100TEST_P(PeerConnectionIntegrationTest, CallTransferredForCaller) {
deadbeef1dcb1642017-03-29 21:08:16 -07002101 ASSERT_TRUE(CreatePeerConnectionWrappers());
2102 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002103 caller()->AddAudioVideoTracks();
2104 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002105 caller()->CreateAndSetAndSignalOffer();
2106 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2107
2108 // Keep the original peer around which will still send packets to the
2109 // receiving client. These SRTP packets will be dropped.
2110 std::unique_ptr<PeerConnectionWrapper> original_peer(
2111 SetCalleePcWrapperAndReturnCurrent(
Seth Hampson2f0d7022018-02-20 11:54:42 -08002112 CreatePeerConnectionWrapperWithAlternateKey().release()));
deadbeef1dcb1642017-03-29 21:08:16 -07002113 // TODO(deadbeef): Why do we call Close here? That goes against the comment
2114 // directly above.
2115 original_peer->pc()->Close();
2116
2117 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002118 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002119 caller()->SetOfferAnswerOptions(IceRestartOfferAnswerOptions());
2120 caller()->CreateAndSetAndSignalOffer();
2121 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2122 // Wait for some additional frames to be transmitted end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002123 MediaExpectations media_expectations;
2124 media_expectations.ExpectBidirectionalAudioAndVideo();
2125 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002126}
2127
2128// This test sets up a non-bundled call and negotiates bundling at the same
2129// time as starting an ICE restart. When bundling is in effect in the restart,
2130// the DTLS-SRTP context should be successfully reset.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002131TEST_P(PeerConnectionIntegrationTest, BundlingEnabledWhileIceRestartOccurs) {
deadbeef1dcb1642017-03-29 21:08:16 -07002132 ASSERT_TRUE(CreatePeerConnectionWrappers());
2133 ConnectFakeSignaling();
2134
Steve Anton15324772018-01-16 10:26:49 -08002135 caller()->AddAudioVideoTracks();
2136 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002137 // Remove the bundle group from the SDP received by the callee.
2138 callee()->SetReceivedSdpMunger([](cricket::SessionDescription* desc) {
2139 desc->RemoveGroupByName("BUNDLE");
2140 });
2141 caller()->CreateAndSetAndSignalOffer();
2142 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002143 {
2144 MediaExpectations media_expectations;
2145 media_expectations.ExpectBidirectionalAudioAndVideo();
2146 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2147 }
deadbeef1dcb1642017-03-29 21:08:16 -07002148 // Now stop removing the BUNDLE group, and trigger an ICE restart.
2149 callee()->SetReceivedSdpMunger(nullptr);
2150 caller()->SetOfferAnswerOptions(IceRestartOfferAnswerOptions());
2151 caller()->CreateAndSetAndSignalOffer();
2152 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2153
2154 // Expect additional frames to be received after the ICE restart.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002155 {
2156 MediaExpectations media_expectations;
2157 media_expectations.ExpectBidirectionalAudioAndVideo();
2158 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2159 }
deadbeef1dcb1642017-03-29 21:08:16 -07002160}
2161
2162// Test CVO (Coordination of Video Orientation). If a video source is rotated
2163// and both peers support the CVO RTP header extension, the actual video frames
2164// don't need to be encoded in different resolutions, since the rotation is
2165// communicated through the RTP header extension.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002166TEST_P(PeerConnectionIntegrationTest, RotatedVideoWithCVOExtension) {
deadbeef1dcb1642017-03-29 21:08:16 -07002167 ASSERT_TRUE(CreatePeerConnectionWrappers());
2168 ConnectFakeSignaling();
2169 // Add rotated video tracks.
Steve Anton15324772018-01-16 10:26:49 -08002170 caller()->AddTrack(
deadbeef1dcb1642017-03-29 21:08:16 -07002171 caller()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_90));
Steve Anton15324772018-01-16 10:26:49 -08002172 callee()->AddTrack(
deadbeef1dcb1642017-03-29 21:08:16 -07002173 callee()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_270));
2174
2175 // Wait for video frames to be received by both sides.
2176 caller()->CreateAndSetAndSignalOffer();
2177 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2178 ASSERT_TRUE_WAIT(caller()->min_video_frames_received_per_track() > 0 &&
2179 callee()->min_video_frames_received_per_track() > 0,
2180 kMaxWaitForFramesMs);
2181
2182 // Ensure that the aspect ratio is unmodified.
2183 // TODO(deadbeef): Where does 4:3 come from? Should be explicit in the test,
2184 // not just assumed.
2185 EXPECT_EQ(4.0 / 3, caller()->local_rendered_aspect_ratio());
2186 EXPECT_EQ(4.0 / 3, caller()->rendered_aspect_ratio());
2187 EXPECT_EQ(4.0 / 3, callee()->local_rendered_aspect_ratio());
2188 EXPECT_EQ(4.0 / 3, callee()->rendered_aspect_ratio());
2189 // Ensure that the CVO bits were surfaced to the renderer.
2190 EXPECT_EQ(webrtc::kVideoRotation_270, caller()->rendered_rotation());
2191 EXPECT_EQ(webrtc::kVideoRotation_90, callee()->rendered_rotation());
2192}
2193
2194// Test that when the CVO extension isn't supported, video is rotated the
2195// old-fashioned way, by encoding rotated frames.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002196TEST_P(PeerConnectionIntegrationTest, RotatedVideoWithoutCVOExtension) {
deadbeef1dcb1642017-03-29 21:08:16 -07002197 ASSERT_TRUE(CreatePeerConnectionWrappers());
2198 ConnectFakeSignaling();
2199 // Add rotated video tracks.
Steve Anton15324772018-01-16 10:26:49 -08002200 caller()->AddTrack(
deadbeef1dcb1642017-03-29 21:08:16 -07002201 caller()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_90));
Steve Anton15324772018-01-16 10:26:49 -08002202 callee()->AddTrack(
deadbeef1dcb1642017-03-29 21:08:16 -07002203 callee()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_270));
2204
2205 // Remove the CVO extension from the offered SDP.
2206 callee()->SetReceivedSdpMunger([](cricket::SessionDescription* desc) {
2207 cricket::VideoContentDescription* video =
2208 GetFirstVideoContentDescription(desc);
2209 video->ClearRtpHeaderExtensions();
2210 });
2211 // Wait for video frames to be received by both sides.
2212 caller()->CreateAndSetAndSignalOffer();
2213 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2214 ASSERT_TRUE_WAIT(caller()->min_video_frames_received_per_track() > 0 &&
2215 callee()->min_video_frames_received_per_track() > 0,
2216 kMaxWaitForFramesMs);
2217
2218 // Expect that the aspect ratio is inversed to account for the 90/270 degree
2219 // rotation.
2220 // TODO(deadbeef): Where does 4:3 come from? Should be explicit in the test,
2221 // not just assumed.
2222 EXPECT_EQ(3.0 / 4, caller()->local_rendered_aspect_ratio());
2223 EXPECT_EQ(3.0 / 4, caller()->rendered_aspect_ratio());
2224 EXPECT_EQ(3.0 / 4, callee()->local_rendered_aspect_ratio());
2225 EXPECT_EQ(3.0 / 4, callee()->rendered_aspect_ratio());
2226 // Expect that each endpoint is unaware of the rotation of the other endpoint.
2227 EXPECT_EQ(webrtc::kVideoRotation_0, caller()->rendered_rotation());
2228 EXPECT_EQ(webrtc::kVideoRotation_0, callee()->rendered_rotation());
2229}
2230
deadbeef1dcb1642017-03-29 21:08:16 -07002231// Test that if the answerer rejects the audio m= section, no audio is sent or
2232// received, but video still can be.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002233TEST_P(PeerConnectionIntegrationTest, AnswererRejectsAudioSection) {
deadbeef1dcb1642017-03-29 21:08:16 -07002234 ASSERT_TRUE(CreatePeerConnectionWrappers());
2235 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002236 caller()->AddAudioVideoTracks();
Seth Hampson2f0d7022018-02-20 11:54:42 -08002237 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2238 // Only add video track for callee, and set offer_to_receive_audio to 0, so
2239 // it will reject the audio m= section completely.
2240 PeerConnectionInterface::RTCOfferAnswerOptions options;
2241 options.offer_to_receive_audio = 0;
2242 callee()->SetOfferAnswerOptions(options);
2243 } else {
2244 // Stopping the audio RtpTransceiver will cause the media section to be
2245 // rejected in the answer.
2246 callee()->SetRemoteOfferHandler([this] {
2247 callee()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_AUDIO)->Stop();
2248 });
2249 }
Steve Anton15324772018-01-16 10:26:49 -08002250 callee()->AddTrack(callee()->CreateLocalVideoTrack());
deadbeef1dcb1642017-03-29 21:08:16 -07002251 // Do offer/answer and wait for successful end-to-end video frames.
2252 caller()->CreateAndSetAndSignalOffer();
2253 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002254 MediaExpectations media_expectations;
2255 media_expectations.ExpectBidirectionalVideo();
2256 media_expectations.ExpectNoAudio();
2257 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2258
deadbeef1dcb1642017-03-29 21:08:16 -07002259 // Sanity check that the callee's description has a rejected audio section.
2260 ASSERT_NE(nullptr, callee()->pc()->local_description());
2261 const ContentInfo* callee_audio_content =
2262 GetFirstAudioContent(callee()->pc()->local_description()->description());
2263 ASSERT_NE(nullptr, callee_audio_content);
2264 EXPECT_TRUE(callee_audio_content->rejected);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002265 if (sdp_semantics_ == SdpSemantics::kUnifiedPlan) {
2266 // The caller's transceiver should have stopped after receiving the answer.
2267 EXPECT_TRUE(caller()
2268 ->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_AUDIO)
2269 ->stopped());
2270 }
deadbeef1dcb1642017-03-29 21:08:16 -07002271}
2272
2273// Test that if the answerer rejects the video m= section, no video is sent or
2274// received, but audio still can be.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002275TEST_P(PeerConnectionIntegrationTest, AnswererRejectsVideoSection) {
deadbeef1dcb1642017-03-29 21:08:16 -07002276 ASSERT_TRUE(CreatePeerConnectionWrappers());
2277 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002278 caller()->AddAudioVideoTracks();
Seth Hampson2f0d7022018-02-20 11:54:42 -08002279 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2280 // Only add audio track for callee, and set offer_to_receive_video to 0, so
2281 // it will reject the video m= section completely.
2282 PeerConnectionInterface::RTCOfferAnswerOptions options;
2283 options.offer_to_receive_video = 0;
2284 callee()->SetOfferAnswerOptions(options);
2285 } else {
2286 // Stopping the video RtpTransceiver will cause the media section to be
2287 // rejected in the answer.
2288 callee()->SetRemoteOfferHandler([this] {
2289 callee()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)->Stop();
2290 });
2291 }
Steve Anton15324772018-01-16 10:26:49 -08002292 callee()->AddTrack(callee()->CreateLocalAudioTrack());
deadbeef1dcb1642017-03-29 21:08:16 -07002293 // Do offer/answer and wait for successful end-to-end audio frames.
2294 caller()->CreateAndSetAndSignalOffer();
2295 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002296 MediaExpectations media_expectations;
2297 media_expectations.ExpectBidirectionalAudio();
2298 media_expectations.ExpectNoVideo();
2299 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2300
deadbeef1dcb1642017-03-29 21:08:16 -07002301 // Sanity check that the callee's description has a rejected video section.
2302 ASSERT_NE(nullptr, callee()->pc()->local_description());
2303 const ContentInfo* callee_video_content =
2304 GetFirstVideoContent(callee()->pc()->local_description()->description());
2305 ASSERT_NE(nullptr, callee_video_content);
2306 EXPECT_TRUE(callee_video_content->rejected);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002307 if (sdp_semantics_ == SdpSemantics::kUnifiedPlan) {
2308 // The caller's transceiver should have stopped after receiving the answer.
2309 EXPECT_TRUE(caller()
2310 ->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)
2311 ->stopped());
2312 }
deadbeef1dcb1642017-03-29 21:08:16 -07002313}
2314
2315// Test that if the answerer rejects both audio and video m= sections, nothing
2316// bad happens.
2317// TODO(deadbeef): Test that a data channel still works. Currently this doesn't
2318// test anything but the fact that negotiation succeeds, which doesn't mean
2319// much.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002320TEST_P(PeerConnectionIntegrationTest, AnswererRejectsAudioAndVideoSections) {
deadbeef1dcb1642017-03-29 21:08:16 -07002321 ASSERT_TRUE(CreatePeerConnectionWrappers());
2322 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002323 caller()->AddAudioVideoTracks();
Seth Hampson2f0d7022018-02-20 11:54:42 -08002324 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2325 // Don't give the callee any tracks, and set offer_to_receive_X to 0, so it
2326 // will reject both audio and video m= sections.
2327 PeerConnectionInterface::RTCOfferAnswerOptions options;
2328 options.offer_to_receive_audio = 0;
2329 options.offer_to_receive_video = 0;
2330 callee()->SetOfferAnswerOptions(options);
2331 } else {
2332 callee()->SetRemoteOfferHandler([this] {
2333 // Stopping all transceivers will cause all media sections to be rejected.
Mirko Bonadei739baf02019-01-27 17:29:42 +01002334 for (const auto& transceiver : callee()->pc()->GetTransceivers()) {
Seth Hampson2f0d7022018-02-20 11:54:42 -08002335 transceiver->Stop();
2336 }
2337 });
2338 }
deadbeef1dcb1642017-03-29 21:08:16 -07002339 // Do offer/answer and wait for stable signaling state.
2340 caller()->CreateAndSetAndSignalOffer();
2341 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002342
deadbeef1dcb1642017-03-29 21:08:16 -07002343 // Sanity check that the callee's description has rejected m= sections.
2344 ASSERT_NE(nullptr, callee()->pc()->local_description());
2345 const ContentInfo* callee_audio_content =
2346 GetFirstAudioContent(callee()->pc()->local_description()->description());
2347 ASSERT_NE(nullptr, callee_audio_content);
2348 EXPECT_TRUE(callee_audio_content->rejected);
2349 const ContentInfo* callee_video_content =
2350 GetFirstVideoContent(callee()->pc()->local_description()->description());
2351 ASSERT_NE(nullptr, callee_video_content);
2352 EXPECT_TRUE(callee_video_content->rejected);
2353}
2354
2355// This test sets up an audio and video call between two parties. After the
2356// call runs for a while, the caller sends an updated offer with video being
2357// rejected. Once the re-negotiation is done, the video flow should stop and
2358// the audio flow should continue.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002359TEST_P(PeerConnectionIntegrationTest, VideoRejectedInSubsequentOffer) {
deadbeef1dcb1642017-03-29 21:08:16 -07002360 ASSERT_TRUE(CreatePeerConnectionWrappers());
2361 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002362 caller()->AddAudioVideoTracks();
2363 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002364 caller()->CreateAndSetAndSignalOffer();
2365 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002366 {
2367 MediaExpectations media_expectations;
2368 media_expectations.ExpectBidirectionalAudioAndVideo();
2369 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2370 }
deadbeef1dcb1642017-03-29 21:08:16 -07002371 // Renegotiate, rejecting the video m= section.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002372 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2373 caller()->SetGeneratedSdpMunger(
2374 [](cricket::SessionDescription* description) {
2375 for (cricket::ContentInfo& content : description->contents()) {
2376 if (cricket::IsVideoContent(&content)) {
2377 content.rejected = true;
2378 }
2379 }
2380 });
2381 } else {
2382 caller()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)->Stop();
2383 }
deadbeef1dcb1642017-03-29 21:08:16 -07002384 caller()->CreateAndSetAndSignalOffer();
2385 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
2386
2387 // Sanity check that the caller's description has a rejected video section.
2388 ASSERT_NE(nullptr, caller()->pc()->local_description());
2389 const ContentInfo* caller_video_content =
2390 GetFirstVideoContent(caller()->pc()->local_description()->description());
2391 ASSERT_NE(nullptr, caller_video_content);
2392 EXPECT_TRUE(caller_video_content->rejected);
deadbeef1dcb1642017-03-29 21:08:16 -07002393 // Wait for some additional audio frames to be received.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002394 {
2395 MediaExpectations media_expectations;
2396 media_expectations.ExpectBidirectionalAudio();
2397 media_expectations.ExpectNoVideo();
2398 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2399 }
deadbeef1dcb1642017-03-29 21:08:16 -07002400}
2401
Taylor Brandstetter60c8dc82018-04-11 15:20:27 -07002402// Do one offer/answer with audio, another that disables it (rejecting the m=
2403// section), and another that re-enables it. Regression test for:
2404// bugs.webrtc.org/6023
2405TEST_F(PeerConnectionIntegrationTestPlanB, EnableAudioAfterRejecting) {
2406 ASSERT_TRUE(CreatePeerConnectionWrappers());
2407 ConnectFakeSignaling();
2408
2409 // Add audio track, do normal offer/answer.
2410 rtc::scoped_refptr<webrtc::AudioTrackInterface> track =
2411 caller()->CreateLocalAudioTrack();
2412 rtc::scoped_refptr<webrtc::RtpSenderInterface> sender =
2413 caller()->pc()->AddTrack(track, {"stream"}).MoveValue();
2414 caller()->CreateAndSetAndSignalOffer();
2415 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2416
2417 // Remove audio track, and set offer_to_receive_audio to false to cause the
2418 // m= section to be completely disabled, not just "recvonly".
2419 caller()->pc()->RemoveTrack(sender);
2420 PeerConnectionInterface::RTCOfferAnswerOptions options;
2421 options.offer_to_receive_audio = 0;
2422 caller()->SetOfferAnswerOptions(options);
2423 caller()->CreateAndSetAndSignalOffer();
2424 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2425
2426 // Add the audio track again, expecting negotiation to succeed and frames to
2427 // flow.
2428 sender = caller()->pc()->AddTrack(track, {"stream"}).MoveValue();
2429 options.offer_to_receive_audio = 1;
2430 caller()->SetOfferAnswerOptions(options);
2431 caller()->CreateAndSetAndSignalOffer();
2432 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2433
2434 MediaExpectations media_expectations;
2435 media_expectations.CalleeExpectsSomeAudio();
2436 EXPECT_TRUE(ExpectNewFrames(media_expectations));
2437}
2438
deadbeef1dcb1642017-03-29 21:08:16 -07002439// Basic end-to-end test, but without SSRC/MSID signaling. This functionality
2440// is needed to support legacy endpoints.
2441// TODO(deadbeef): When we support the MID extension and demuxing on MID, also
2442// add a test for an end-to-end test without MID signaling either (basically,
2443// the minimum acceptable SDP).
Seth Hampson2f0d7022018-02-20 11:54:42 -08002444TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithoutSsrcOrMsidSignaling) {
deadbeef1dcb1642017-03-29 21:08:16 -07002445 ASSERT_TRUE(CreatePeerConnectionWrappers());
2446 ConnectFakeSignaling();
2447 // Add audio and video, testing that packets can be demuxed on payload type.
Steve Anton15324772018-01-16 10:26:49 -08002448 caller()->AddAudioVideoTracks();
2449 callee()->AddAudioVideoTracks();
deadbeefd8ad7882017-04-18 16:01:17 -07002450 // Remove SSRCs and MSIDs from the received offer SDP.
2451 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
deadbeef1dcb1642017-03-29 21:08:16 -07002452 caller()->CreateAndSetAndSignalOffer();
2453 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002454 MediaExpectations media_expectations;
2455 media_expectations.ExpectBidirectionalAudioAndVideo();
2456 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002457}
2458
Seth Hampson5897a6e2018-04-03 11:16:33 -07002459// Basic end-to-end test, without SSRC signaling. This means that the track
2460// was created properly and frames are delivered when the MSIDs are communicated
2461// with a=msid lines and no a=ssrc lines.
2462TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
2463 EndToEndCallWithoutSsrcSignaling) {
2464 const char kStreamId[] = "streamId";
2465 ASSERT_TRUE(CreatePeerConnectionWrappers());
2466 ConnectFakeSignaling();
2467 // Add just audio tracks.
2468 caller()->AddTrack(caller()->CreateLocalAudioTrack(), {kStreamId});
2469 callee()->AddAudioTrack();
2470
2471 // Remove SSRCs from the received offer SDP.
2472 callee()->SetReceivedSdpMunger(RemoveSsrcsAndKeepMsids);
2473 caller()->CreateAndSetAndSignalOffer();
2474 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2475 MediaExpectations media_expectations;
2476 media_expectations.ExpectBidirectionalAudio();
2477 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2478}
2479
Steve Antondf527fd2018-04-27 15:52:03 -07002480// Tests that video flows between multiple video tracks when SSRCs are not
2481// signaled. This exercises the MID RTP header extension which is needed to
2482// demux the incoming video tracks.
2483TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
2484 EndToEndCallWithTwoVideoTracksAndNoSignaledSsrc) {
2485 ASSERT_TRUE(CreatePeerConnectionWrappers());
2486 ConnectFakeSignaling();
2487 caller()->AddVideoTrack();
2488 caller()->AddVideoTrack();
2489 callee()->AddVideoTrack();
2490 callee()->AddVideoTrack();
2491
2492 caller()->SetReceivedSdpMunger(&RemoveSsrcsAndKeepMsids);
2493 callee()->SetReceivedSdpMunger(&RemoveSsrcsAndKeepMsids);
2494 caller()->CreateAndSetAndSignalOffer();
2495 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2496 ASSERT_EQ(2u, caller()->pc()->GetReceivers().size());
2497 ASSERT_EQ(2u, callee()->pc()->GetReceivers().size());
2498
2499 // Expect video to be received in both directions on both tracks.
2500 MediaExpectations media_expectations;
2501 media_expectations.ExpectBidirectionalVideo();
2502 EXPECT_TRUE(ExpectNewFrames(media_expectations));
2503}
2504
Henrik Boström5b147782018-12-04 11:25:05 +01002505TEST_F(PeerConnectionIntegrationTestUnifiedPlan, NoStreamsMsidLinePresent) {
2506 ASSERT_TRUE(CreatePeerConnectionWrappers());
2507 ConnectFakeSignaling();
2508 caller()->AddAudioTrack();
2509 caller()->AddVideoTrack();
2510 caller()->CreateAndSetAndSignalOffer();
2511 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2512 auto callee_receivers = callee()->pc()->GetReceivers();
2513 ASSERT_EQ(2u, callee_receivers.size());
2514 EXPECT_TRUE(callee_receivers[0]->stream_ids().empty());
2515 EXPECT_TRUE(callee_receivers[1]->stream_ids().empty());
2516}
2517
2518TEST_F(PeerConnectionIntegrationTestUnifiedPlan, NoStreamsMsidLineMissing) {
2519 ASSERT_TRUE(CreatePeerConnectionWrappers());
2520 ConnectFakeSignaling();
2521 caller()->AddAudioTrack();
2522 caller()->AddVideoTrack();
2523 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
2524 caller()->CreateAndSetAndSignalOffer();
2525 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2526 auto callee_receivers = callee()->pc()->GetReceivers();
2527 ASSERT_EQ(2u, callee_receivers.size());
2528 ASSERT_EQ(1u, callee_receivers[0]->stream_ids().size());
2529 ASSERT_EQ(1u, callee_receivers[1]->stream_ids().size());
2530 EXPECT_EQ(callee_receivers[0]->stream_ids()[0],
2531 callee_receivers[1]->stream_ids()[0]);
2532 EXPECT_EQ(callee_receivers[0]->streams()[0],
2533 callee_receivers[1]->streams()[0]);
2534}
2535
deadbeef1dcb1642017-03-29 21:08:16 -07002536// Test that if two video tracks are sent (from caller to callee, in this test),
2537// they're transmitted correctly end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002538TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithTwoVideoTracks) {
deadbeef1dcb1642017-03-29 21:08:16 -07002539 ASSERT_TRUE(CreatePeerConnectionWrappers());
2540 ConnectFakeSignaling();
2541 // Add one audio/video stream, and one video-only stream.
Steve Anton15324772018-01-16 10:26:49 -08002542 caller()->AddAudioVideoTracks();
2543 caller()->AddVideoTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07002544 caller()->CreateAndSetAndSignalOffer();
2545 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Steve Anton15324772018-01-16 10:26:49 -08002546 ASSERT_EQ(3u, callee()->pc()->GetReceivers().size());
Seth Hampson2f0d7022018-02-20 11:54:42 -08002547
2548 MediaExpectations media_expectations;
2549 media_expectations.CalleeExpectsSomeAudioAndVideo();
2550 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002551}
2552
2553static void MakeSpecCompliantMaxBundleOffer(cricket::SessionDescription* desc) {
2554 bool first = true;
2555 for (cricket::ContentInfo& content : desc->contents()) {
2556 if (first) {
2557 first = false;
2558 continue;
2559 }
2560 content.bundle_only = true;
2561 }
2562 first = true;
2563 for (cricket::TransportInfo& transport : desc->transport_infos()) {
2564 if (first) {
2565 first = false;
2566 continue;
2567 }
2568 transport.description.ice_ufrag.clear();
2569 transport.description.ice_pwd.clear();
2570 transport.description.connection_role = cricket::CONNECTIONROLE_NONE;
2571 transport.description.identity_fingerprint.reset(nullptr);
2572 }
2573}
2574
2575// Test that if applying a true "max bundle" offer, which uses ports of 0,
2576// "a=bundle-only", omitting "a=fingerprint", "a=setup", "a=ice-ufrag" and
2577// "a=ice-pwd" for all but the audio "m=" section, negotiation still completes
2578// successfully and media flows.
2579// TODO(deadbeef): Update this test to also omit "a=rtcp-mux", once that works.
2580// TODO(deadbeef): Won't need this test once we start generating actual
2581// standards-compliant SDP.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002582TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07002583 EndToEndCallWithSpecCompliantMaxBundleOffer) {
2584 ASSERT_TRUE(CreatePeerConnectionWrappers());
2585 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002586 caller()->AddAudioVideoTracks();
2587 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002588 // Do the equivalent of setting the port to 0, adding a=bundle-only, and
2589 // removing a=ice-ufrag, a=ice-pwd, a=fingerprint and a=setup from all
2590 // but the first m= section.
2591 callee()->SetReceivedSdpMunger(MakeSpecCompliantMaxBundleOffer);
2592 caller()->CreateAndSetAndSignalOffer();
2593 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002594 MediaExpectations media_expectations;
2595 media_expectations.ExpectBidirectionalAudioAndVideo();
2596 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002597}
2598
2599// Test that we can receive the audio output level from a remote audio track.
2600// TODO(deadbeef): Use a fake audio source and verify that the output level is
2601// exactly what the source on the other side was configured with.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002602TEST_P(PeerConnectionIntegrationTest, GetAudioOutputLevelStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-29 21:08:16 -07002603 ASSERT_TRUE(CreatePeerConnectionWrappers());
2604 ConnectFakeSignaling();
2605 // Just add an audio track.
Steve Anton15324772018-01-16 10:26:49 -08002606 caller()->AddAudioTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07002607 caller()->CreateAndSetAndSignalOffer();
2608 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2609
2610 // Get the audio output level stats. Note that the level is not available
2611 // until an RTCP packet has been received.
deadbeefd8ad7882017-04-18 16:01:17 -07002612 EXPECT_TRUE_WAIT(callee()->OldGetStats()->AudioOutputLevel() > 0,
deadbeef1dcb1642017-03-29 21:08:16 -07002613 kMaxWaitForFramesMs);
2614}
2615
2616// Test that an audio input level is reported.
2617// TODO(deadbeef): Use a fake audio source and verify that the input level is
2618// exactly what the source was configured with.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002619TEST_P(PeerConnectionIntegrationTest, GetAudioInputLevelStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-29 21:08:16 -07002620 ASSERT_TRUE(CreatePeerConnectionWrappers());
2621 ConnectFakeSignaling();
2622 // Just add an audio track.
Steve Anton15324772018-01-16 10:26:49 -08002623 caller()->AddAudioTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07002624 caller()->CreateAndSetAndSignalOffer();
2625 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2626
2627 // Get the audio input level stats. The level should be available very
2628 // soon after the test starts.
deadbeefd8ad7882017-04-18 16:01:17 -07002629 EXPECT_TRUE_WAIT(caller()->OldGetStats()->AudioInputLevel() > 0,
deadbeef1dcb1642017-03-29 21:08:16 -07002630 kMaxWaitForStatsMs);
2631}
2632
2633// Test that we can get incoming byte counts from both audio and video tracks.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002634TEST_P(PeerConnectionIntegrationTest, GetBytesReceivedStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-29 21:08:16 -07002635 ASSERT_TRUE(CreatePeerConnectionWrappers());
2636 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002637 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002638 // Do offer/answer, wait for the callee to receive some frames.
2639 caller()->CreateAndSetAndSignalOffer();
2640 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002641
2642 MediaExpectations media_expectations;
2643 media_expectations.CalleeExpectsSomeAudioAndVideo();
2644 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002645
2646 // Get a handle to the remote tracks created, so they can be used as GetStats
2647 // filters.
Mirko Bonadei739baf02019-01-27 17:29:42 +01002648 for (const auto& receiver : callee()->pc()->GetReceivers()) {
Steve Anton15324772018-01-16 10:26:49 -08002649 // We received frames, so we definitely should have nonzero "received bytes"
2650 // stats at this point.
2651 EXPECT_GT(callee()->OldGetStatsForTrack(receiver->track())->BytesReceived(),
2652 0);
2653 }
deadbeef1dcb1642017-03-29 21:08:16 -07002654}
2655
2656// Test that we can get outgoing byte counts from both audio and video tracks.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002657TEST_P(PeerConnectionIntegrationTest, GetBytesSentStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-29 21:08:16 -07002658 ASSERT_TRUE(CreatePeerConnectionWrappers());
2659 ConnectFakeSignaling();
2660 auto audio_track = caller()->CreateLocalAudioTrack();
2661 auto video_track = caller()->CreateLocalVideoTrack();
Steve Anton15324772018-01-16 10:26:49 -08002662 caller()->AddTrack(audio_track);
2663 caller()->AddTrack(video_track);
deadbeef1dcb1642017-03-29 21:08:16 -07002664 // Do offer/answer, wait for the callee to receive some frames.
2665 caller()->CreateAndSetAndSignalOffer();
2666 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002667 MediaExpectations media_expectations;
2668 media_expectations.CalleeExpectsSomeAudioAndVideo();
2669 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002670
2671 // The callee received frames, so we definitely should have nonzero "sent
2672 // bytes" stats at this point.
deadbeefd8ad7882017-04-18 16:01:17 -07002673 EXPECT_GT(caller()->OldGetStatsForTrack(audio_track)->BytesSent(), 0);
2674 EXPECT_GT(caller()->OldGetStatsForTrack(video_track)->BytesSent(), 0);
2675}
2676
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002677// Test that we can get capture start ntp time.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002678TEST_P(PeerConnectionIntegrationTest, GetCaptureStartNtpTimeWithOldStatsApi) {
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002679 ASSERT_TRUE(CreatePeerConnectionWrappers());
2680 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002681 caller()->AddAudioTrack();
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002682
Steve Anton15324772018-01-16 10:26:49 -08002683 callee()->AddAudioTrack();
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002684
2685 // Do offer/answer, wait for the callee to receive some frames.
2686 caller()->CreateAndSetAndSignalOffer();
2687 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2688
2689 // Get the remote audio track created on the receiver, so they can be used as
2690 // GetStats filters.
Steve Antonfc853712018-03-01 13:48:58 -08002691 auto receivers = callee()->pc()->GetReceivers();
2692 ASSERT_EQ(1u, receivers.size());
2693 auto remote_audio_track = receivers[0]->track();
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002694
2695 // Get the audio output level stats. Note that the level is not available
2696 // until an RTCP packet has been received.
Zhi Huange830e682018-03-30 10:48:35 -07002697 EXPECT_TRUE_WAIT(
2698 callee()->OldGetStatsForTrack(remote_audio_track)->CaptureStartNtpTime() >
2699 0,
2700 2 * kMaxWaitForFramesMs);
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002701}
2702
Steve Antona41959e2018-11-28 11:15:33 -08002703// Test that the track ID is associated with all local and remote SSRC stats
2704// using the old GetStats() and more than 1 audio and more than 1 video track.
2705// This is a regression test for crbug.com/906988
2706TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
2707 OldGetStatsAssociatesTrackIdForManyMediaSections) {
2708 ASSERT_TRUE(CreatePeerConnectionWrappers());
2709 ConnectFakeSignaling();
2710 auto audio_sender_1 = caller()->AddAudioTrack();
2711 auto video_sender_1 = caller()->AddVideoTrack();
2712 auto audio_sender_2 = caller()->AddAudioTrack();
2713 auto video_sender_2 = caller()->AddVideoTrack();
2714 caller()->CreateAndSetAndSignalOffer();
2715 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2716
2717 MediaExpectations media_expectations;
2718 media_expectations.CalleeExpectsSomeAudioAndVideo();
2719 ASSERT_TRUE_WAIT(ExpectNewFrames(media_expectations), kDefaultTimeout);
2720
2721 std::vector<std::string> track_ids = {
2722 audio_sender_1->track()->id(), video_sender_1->track()->id(),
2723 audio_sender_2->track()->id(), video_sender_2->track()->id()};
2724
2725 auto caller_stats = caller()->OldGetStats();
2726 EXPECT_THAT(caller_stats->TrackIds(), UnorderedElementsAreArray(track_ids));
2727 auto callee_stats = callee()->OldGetStats();
2728 EXPECT_THAT(callee_stats->TrackIds(), UnorderedElementsAreArray(track_ids));
2729}
2730
Steve Antonffa6ce42018-11-30 09:26:08 -08002731// Test that the new GetStats() returns stats for all outgoing/incoming streams
2732// with the correct track IDs if there are more than one audio and more than one
2733// video senders/receivers.
2734TEST_P(PeerConnectionIntegrationTest, NewGetStatsManyAudioAndManyVideoStreams) {
2735 ASSERT_TRUE(CreatePeerConnectionWrappers());
2736 ConnectFakeSignaling();
2737 auto audio_sender_1 = caller()->AddAudioTrack();
2738 auto video_sender_1 = caller()->AddVideoTrack();
2739 auto audio_sender_2 = caller()->AddAudioTrack();
2740 auto video_sender_2 = caller()->AddVideoTrack();
2741 caller()->CreateAndSetAndSignalOffer();
2742 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2743
2744 MediaExpectations media_expectations;
2745 media_expectations.CalleeExpectsSomeAudioAndVideo();
2746 ASSERT_TRUE_WAIT(ExpectNewFrames(media_expectations), kDefaultTimeout);
2747
2748 std::vector<std::string> track_ids = {
2749 audio_sender_1->track()->id(), video_sender_1->track()->id(),
2750 audio_sender_2->track()->id(), video_sender_2->track()->id()};
2751
2752 rtc::scoped_refptr<const webrtc::RTCStatsReport> caller_report =
2753 caller()->NewGetStats();
2754 ASSERT_TRUE(caller_report);
2755 auto outbound_stream_stats =
2756 caller_report->GetStatsOfType<webrtc::RTCOutboundRTPStreamStats>();
2757 ASSERT_EQ(4u, outbound_stream_stats.size());
2758 std::vector<std::string> outbound_track_ids;
2759 for (const auto& stat : outbound_stream_stats) {
2760 ASSERT_TRUE(stat->bytes_sent.is_defined());
2761 EXPECT_LT(0u, *stat->bytes_sent);
Rasmus Brandt2efae772019-06-27 14:29:34 +02002762 if (*stat->kind == "video") {
2763 ASSERT_TRUE(stat->key_frames_encoded.is_defined());
2764 EXPECT_GT(*stat->key_frames_encoded, 0u);
2765 ASSERT_TRUE(stat->frames_encoded.is_defined());
2766 EXPECT_GE(*stat->frames_encoded, *stat->key_frames_encoded);
2767 }
Steve Antonffa6ce42018-11-30 09:26:08 -08002768 ASSERT_TRUE(stat->track_id.is_defined());
2769 const auto* track_stat =
2770 caller_report->GetAs<webrtc::RTCMediaStreamTrackStats>(*stat->track_id);
2771 ASSERT_TRUE(track_stat);
2772 outbound_track_ids.push_back(*track_stat->track_identifier);
2773 }
2774 EXPECT_THAT(outbound_track_ids, UnorderedElementsAreArray(track_ids));
2775
2776 rtc::scoped_refptr<const webrtc::RTCStatsReport> callee_report =
2777 callee()->NewGetStats();
2778 ASSERT_TRUE(callee_report);
2779 auto inbound_stream_stats =
2780 callee_report->GetStatsOfType<webrtc::RTCInboundRTPStreamStats>();
2781 ASSERT_EQ(4u, inbound_stream_stats.size());
2782 std::vector<std::string> inbound_track_ids;
2783 for (const auto& stat : inbound_stream_stats) {
2784 ASSERT_TRUE(stat->bytes_received.is_defined());
2785 EXPECT_LT(0u, *stat->bytes_received);
Rasmus Brandt2efae772019-06-27 14:29:34 +02002786 if (*stat->kind == "video") {
2787 ASSERT_TRUE(stat->key_frames_decoded.is_defined());
2788 EXPECT_GT(*stat->key_frames_decoded, 0u);
2789 ASSERT_TRUE(stat->frames_decoded.is_defined());
2790 EXPECT_GE(*stat->frames_decoded, *stat->key_frames_decoded);
2791 }
Steve Antonffa6ce42018-11-30 09:26:08 -08002792 ASSERT_TRUE(stat->track_id.is_defined());
2793 const auto* track_stat =
2794 callee_report->GetAs<webrtc::RTCMediaStreamTrackStats>(*stat->track_id);
2795 ASSERT_TRUE(track_stat);
2796 inbound_track_ids.push_back(*track_stat->track_identifier);
2797 }
2798 EXPECT_THAT(inbound_track_ids, UnorderedElementsAreArray(track_ids));
2799}
2800
2801// Test that we can get stats (using the new stats implementation) for
deadbeefd8ad7882017-04-18 16:01:17 -07002802// unsignaled streams. Meaning when SSRCs/MSIDs aren't signaled explicitly in
2803// SDP.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002804TEST_P(PeerConnectionIntegrationTest,
deadbeefd8ad7882017-04-18 16:01:17 -07002805 GetStatsForUnsignaledStreamWithNewStatsApi) {
2806 ASSERT_TRUE(CreatePeerConnectionWrappers());
2807 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002808 caller()->AddAudioTrack();
deadbeefd8ad7882017-04-18 16:01:17 -07002809 // Remove SSRCs and MSIDs from the received offer SDP.
2810 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
2811 caller()->CreateAndSetAndSignalOffer();
2812 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002813 MediaExpectations media_expectations;
2814 media_expectations.CalleeExpectsSomeAudio(1);
2815 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeefd8ad7882017-04-18 16:01:17 -07002816
2817 // We received a frame, so we should have nonzero "bytes received" stats for
2818 // the unsignaled stream, if stats are working for it.
2819 rtc::scoped_refptr<const webrtc::RTCStatsReport> report =
2820 callee()->NewGetStats();
2821 ASSERT_NE(nullptr, report);
2822 auto inbound_stream_stats =
2823 report->GetStatsOfType<webrtc::RTCInboundRTPStreamStats>();
2824 ASSERT_EQ(1U, inbound_stream_stats.size());
2825 ASSERT_TRUE(inbound_stream_stats[0]->bytes_received.is_defined());
2826 ASSERT_GT(*inbound_stream_stats[0]->bytes_received, 0U);
zhihuangf8164932017-05-19 13:09:47 -07002827 ASSERT_TRUE(inbound_stream_stats[0]->track_id.is_defined());
2828}
2829
Taylor Brandstettera4653442018-06-19 09:44:26 -07002830// Same as above but for the legacy stats implementation.
2831TEST_P(PeerConnectionIntegrationTest,
2832 GetStatsForUnsignaledStreamWithOldStatsApi) {
2833 ASSERT_TRUE(CreatePeerConnectionWrappers());
2834 ConnectFakeSignaling();
2835 caller()->AddAudioTrack();
2836 // Remove SSRCs and MSIDs from the received offer SDP.
2837 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
2838 caller()->CreateAndSetAndSignalOffer();
2839 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2840
2841 // Note that, since the old stats implementation associates SSRCs with tracks
2842 // using SDP, when SSRCs aren't signaled in SDP these stats won't have an
2843 // associated track ID. So we can't use the track "selector" argument.
2844 //
2845 // Also, we use "EXPECT_TRUE_WAIT" because the stats collector may decide to
2846 // return cached stats if not enough time has passed since the last update.
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02002847 EXPECT_TRUE_WAIT(callee()->OldGetStats()->BytesReceived() > 0,
Taylor Brandstettera4653442018-06-19 09:44:26 -07002848 kDefaultTimeout);
2849}
2850
zhihuangf8164932017-05-19 13:09:47 -07002851// Test that we can successfully get the media related stats (audio level
2852// etc.) for the unsignaled stream.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002853TEST_P(PeerConnectionIntegrationTest,
zhihuangf8164932017-05-19 13:09:47 -07002854 GetMediaStatsForUnsignaledStreamWithNewStatsApi) {
2855 ASSERT_TRUE(CreatePeerConnectionWrappers());
2856 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002857 caller()->AddAudioVideoTracks();
zhihuangf8164932017-05-19 13:09:47 -07002858 // Remove SSRCs and MSIDs from the received offer SDP.
2859 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
2860 caller()->CreateAndSetAndSignalOffer();
2861 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002862 MediaExpectations media_expectations;
2863 media_expectations.CalleeExpectsSomeAudio(1);
2864 media_expectations.CalleeExpectsSomeVideo(1);
2865 ASSERT_TRUE(ExpectNewFrames(media_expectations));
zhihuangf8164932017-05-19 13:09:47 -07002866
2867 rtc::scoped_refptr<const webrtc::RTCStatsReport> report =
2868 callee()->NewGetStats();
2869 ASSERT_NE(nullptr, report);
2870
2871 auto media_stats = report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
2872 auto audio_index = FindFirstMediaStatsIndexByKind("audio", media_stats);
2873 ASSERT_GE(audio_index, 0);
2874 EXPECT_TRUE(media_stats[audio_index]->audio_level.is_defined());
deadbeef1dcb1642017-03-29 21:08:16 -07002875}
2876
deadbeef4e2deab2017-09-20 13:56:21 -07002877// Helper for test below.
2878void ModifySsrcs(cricket::SessionDescription* desc) {
2879 for (ContentInfo& content : desc->contents()) {
Steve Antondf527fd2018-04-27 15:52:03 -07002880 for (StreamParams& stream :
Steve Antonb1c1de12017-12-21 15:14:30 -08002881 content.media_description()->mutable_streams()) {
deadbeef4e2deab2017-09-20 13:56:21 -07002882 for (uint32_t& ssrc : stream.ssrcs) {
2883 ssrc = rtc::CreateRandomId();
2884 }
2885 }
2886 }
2887}
2888
2889// Test that the "RTCMediaSteamTrackStats" object is updated correctly when
2890// SSRCs are unsignaled, and the SSRC of the received (audio) stream changes.
2891// This should result in two "RTCInboundRTPStreamStats", but only one
2892// "RTCMediaStreamTrackStats", whose counters go up continuously rather than
2893// being reset to 0 once the SSRC change occurs.
2894//
2895// Regression test for this bug:
2896// https://bugs.chromium.org/p/webrtc/issues/detail?id=8158
2897//
2898// The bug causes the track stats to only represent one of the two streams:
2899// whichever one has the higher SSRC. So with this bug, there was a 50% chance
2900// that the track stat counters would reset to 0 when the new stream is
2901// received, and a 50% chance that they'll stop updating (while
2902// "concealed_samples" continues increasing, due to silence being generated for
2903// the inactive stream).
Seth Hampson2f0d7022018-02-20 11:54:42 -08002904TEST_P(PeerConnectionIntegrationTest,
Steve Anton83119dd2017-11-10 16:19:52 -08002905 TrackStatsUpdatedCorrectlyWhenUnsignaledSsrcChanges) {
deadbeef4e2deab2017-09-20 13:56:21 -07002906 ASSERT_TRUE(CreatePeerConnectionWrappers());
2907 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002908 caller()->AddAudioTrack();
deadbeef4e2deab2017-09-20 13:56:21 -07002909 // Remove SSRCs and MSIDs from the received offer SDP, simulating an endpoint
2910 // that doesn't signal SSRCs (from the callee's perspective).
2911 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
2912 caller()->CreateAndSetAndSignalOffer();
2913 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2914 // Wait for 50 audio frames (500ms of audio) to be received by the callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002915 {
2916 MediaExpectations media_expectations;
2917 media_expectations.CalleeExpectsSomeAudio(50);
2918 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2919 }
deadbeef4e2deab2017-09-20 13:56:21 -07002920 // Some audio frames were received, so we should have nonzero "samples
2921 // received" for the track.
2922 rtc::scoped_refptr<const webrtc::RTCStatsReport> report =
2923 callee()->NewGetStats();
2924 ASSERT_NE(nullptr, report);
2925 auto track_stats = report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
2926 ASSERT_EQ(1U, track_stats.size());
2927 ASSERT_TRUE(track_stats[0]->total_samples_received.is_defined());
2928 ASSERT_GT(*track_stats[0]->total_samples_received, 0U);
2929 // uint64_t prev_samples_received = *track_stats[0]->total_samples_received;
2930
2931 // Create a new offer and munge it to cause the caller to use a new SSRC.
2932 caller()->SetGeneratedSdpMunger(ModifySsrcs);
2933 caller()->CreateAndSetAndSignalOffer();
2934 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2935 // Wait for 25 more audio frames (250ms of audio) to be received, from the new
2936 // SSRC.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002937 {
2938 MediaExpectations media_expectations;
2939 media_expectations.CalleeExpectsSomeAudio(25);
2940 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2941 }
deadbeef4e2deab2017-09-20 13:56:21 -07002942
2943 report = callee()->NewGetStats();
2944 ASSERT_NE(nullptr, report);
2945 track_stats = report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
2946 ASSERT_EQ(1U, track_stats.size());
2947 ASSERT_TRUE(track_stats[0]->total_samples_received.is_defined());
2948 // The "total samples received" stat should only be greater than it was
2949 // before.
2950 // TODO(deadbeef): Uncomment this assertion once the bug is completely fixed.
2951 // Right now, the new SSRC will cause the counters to reset to 0.
2952 // EXPECT_GT(*track_stats[0]->total_samples_received, prev_samples_received);
2953
2954 // Additionally, the percentage of concealed samples (samples generated to
Steve Anton83119dd2017-11-10 16:19:52 -08002955 // conceal packet loss) should be less than 50%. If it's greater, that's a
deadbeef4e2deab2017-09-20 13:56:21 -07002956 // good sign that we're seeing stats from the old stream that's no longer
2957 // receiving packets, and is generating concealed samples of silence.
Steve Anton83119dd2017-11-10 16:19:52 -08002958 constexpr double kAcceptableConcealedSamplesPercentage = 0.50;
deadbeef4e2deab2017-09-20 13:56:21 -07002959 ASSERT_TRUE(track_stats[0]->concealed_samples.is_defined());
2960 EXPECT_LT(*track_stats[0]->concealed_samples,
2961 *track_stats[0]->total_samples_received *
2962 kAcceptableConcealedSamplesPercentage);
2963
2964 // Also ensure that we have two "RTCInboundRTPStreamStats" as expected, as a
2965 // sanity check that the SSRC really changed.
2966 // TODO(deadbeef): This isn't working right now, because we're not returning
2967 // *any* stats for the inactive stream. Uncomment when the bug is completely
2968 // fixed.
2969 // auto inbound_stream_stats =
2970 // report->GetStatsOfType<webrtc::RTCInboundRTPStreamStats>();
2971 // ASSERT_EQ(2U, inbound_stream_stats.size());
2972}
2973
deadbeef1dcb1642017-03-29 21:08:16 -07002974// Test that DTLS 1.0 is used if both sides only support DTLS 1.0.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002975TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithDtls10) {
deadbeef1dcb1642017-03-29 21:08:16 -07002976 PeerConnectionFactory::Options dtls_10_options;
2977 dtls_10_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
2978 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(dtls_10_options,
2979 dtls_10_options));
2980 ConnectFakeSignaling();
2981 // Do normal offer/answer and wait for some frames to be received in each
2982 // direction.
Steve Anton15324772018-01-16 10:26:49 -08002983 caller()->AddAudioVideoTracks();
2984 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002985 caller()->CreateAndSetAndSignalOffer();
2986 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002987 MediaExpectations media_expectations;
2988 media_expectations.ExpectBidirectionalAudioAndVideo();
2989 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002990}
2991
2992// Test getting cipher stats and UMA metrics when DTLS 1.0 is negotiated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002993TEST_P(PeerConnectionIntegrationTest, Dtls10CipherStatsAndUmaMetrics) {
deadbeef1dcb1642017-03-29 21:08:16 -07002994 PeerConnectionFactory::Options dtls_10_options;
2995 dtls_10_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
2996 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(dtls_10_options,
2997 dtls_10_options));
2998 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002999 caller()->AddAudioVideoTracks();
3000 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003001 caller()->CreateAndSetAndSignalOffer();
Qingsi Wang7fc821d2018-07-12 12:54:53 -07003002 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07003003 EXPECT_TRUE_WAIT(rtc::SSLStreamAdapter::IsAcceptableCipher(
deadbeefd8ad7882017-04-18 16:01:17 -07003004 caller()->OldGetStats()->DtlsCipher(), rtc::KT_DEFAULT),
deadbeef1dcb1642017-03-29 21:08:16 -07003005 kDefaultTimeout);
3006 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
deadbeefd8ad7882017-04-18 16:01:17 -07003007 caller()->OldGetStats()->SrtpCipher(), kDefaultTimeout);
Qingsi Wang7fc821d2018-07-12 12:54:53 -07003008 // TODO(bugs.webrtc.org/9456): Fix it.
Alex Loiko9289eda2018-11-23 16:18:59 +00003009 EXPECT_EQ(1, webrtc::metrics::NumEvents(
Qingsi Wang7fc821d2018-07-12 12:54:53 -07003010 "WebRTC.PeerConnection.SrtpCryptoSuite.Audio",
3011 kDefaultSrtpCryptoSuite));
deadbeef1dcb1642017-03-29 21:08:16 -07003012}
3013
3014// Test getting cipher stats and UMA metrics when DTLS 1.2 is negotiated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003015TEST_P(PeerConnectionIntegrationTest, Dtls12CipherStatsAndUmaMetrics) {
deadbeef1dcb1642017-03-29 21:08:16 -07003016 PeerConnectionFactory::Options dtls_12_options;
3017 dtls_12_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
3018 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(dtls_12_options,
3019 dtls_12_options));
3020 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08003021 caller()->AddAudioVideoTracks();
3022 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003023 caller()->CreateAndSetAndSignalOffer();
Qingsi Wang7fc821d2018-07-12 12:54:53 -07003024 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07003025 EXPECT_TRUE_WAIT(rtc::SSLStreamAdapter::IsAcceptableCipher(
deadbeefd8ad7882017-04-18 16:01:17 -07003026 caller()->OldGetStats()->DtlsCipher(), rtc::KT_DEFAULT),
deadbeef1dcb1642017-03-29 21:08:16 -07003027 kDefaultTimeout);
3028 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
deadbeefd8ad7882017-04-18 16:01:17 -07003029 caller()->OldGetStats()->SrtpCipher(), kDefaultTimeout);
Qingsi Wang7fc821d2018-07-12 12:54:53 -07003030 // TODO(bugs.webrtc.org/9456): Fix it.
Alex Loiko9289eda2018-11-23 16:18:59 +00003031 EXPECT_EQ(1, webrtc::metrics::NumEvents(
Qingsi Wang7fc821d2018-07-12 12:54:53 -07003032 "WebRTC.PeerConnection.SrtpCryptoSuite.Audio",
3033 kDefaultSrtpCryptoSuite));
deadbeef1dcb1642017-03-29 21:08:16 -07003034}
3035
3036// Test that DTLS 1.0 can be used if the caller supports DTLS 1.2 and the
3037// callee only supports 1.0.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003038TEST_P(PeerConnectionIntegrationTest, CallerDtls12ToCalleeDtls10) {
deadbeef1dcb1642017-03-29 21:08:16 -07003039 PeerConnectionFactory::Options caller_options;
3040 caller_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
3041 PeerConnectionFactory::Options callee_options;
3042 callee_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
3043 ASSERT_TRUE(
3044 CreatePeerConnectionWrappersWithOptions(caller_options, callee_options));
3045 ConnectFakeSignaling();
3046 // Do normal offer/answer and wait for some frames to be received in each
3047 // direction.
Steve Anton15324772018-01-16 10:26:49 -08003048 caller()->AddAudioVideoTracks();
3049 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003050 caller()->CreateAndSetAndSignalOffer();
3051 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08003052 MediaExpectations media_expectations;
3053 media_expectations.ExpectBidirectionalAudioAndVideo();
3054 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003055}
3056
3057// Test that DTLS 1.0 can be used if the caller only supports DTLS 1.0 and the
3058// callee supports 1.2.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003059TEST_P(PeerConnectionIntegrationTest, CallerDtls10ToCalleeDtls12) {
deadbeef1dcb1642017-03-29 21:08:16 -07003060 PeerConnectionFactory::Options caller_options;
3061 caller_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
3062 PeerConnectionFactory::Options callee_options;
3063 callee_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
3064 ASSERT_TRUE(
3065 CreatePeerConnectionWrappersWithOptions(caller_options, callee_options));
3066 ConnectFakeSignaling();
3067 // Do normal offer/answer and wait for some frames to be received in each
3068 // direction.
Steve Anton15324772018-01-16 10:26:49 -08003069 caller()->AddAudioVideoTracks();
3070 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003071 caller()->CreateAndSetAndSignalOffer();
3072 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08003073 MediaExpectations media_expectations;
3074 media_expectations.ExpectBidirectionalAudioAndVideo();
3075 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003076}
3077
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07003078// The three tests below verify that "enable_aes128_sha1_32_crypto_cipher"
3079// works as expected; the cipher should only be used if enabled by both sides.
3080TEST_P(PeerConnectionIntegrationTest,
3081 Aes128Sha1_32_CipherNotUsedWhenOnlyCallerSupported) {
3082 PeerConnectionFactory::Options caller_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07003083 caller_options.crypto_options.srtp.enable_aes128_sha1_32_crypto_cipher = true;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07003084 PeerConnectionFactory::Options callee_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07003085 callee_options.crypto_options.srtp.enable_aes128_sha1_32_crypto_cipher =
3086 false;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07003087 int expected_cipher_suite = rtc::SRTP_AES128_CM_SHA1_80;
3088 TestNegotiatedCipherSuite(caller_options, callee_options,
3089 expected_cipher_suite);
3090}
3091
3092TEST_P(PeerConnectionIntegrationTest,
3093 Aes128Sha1_32_CipherNotUsedWhenOnlyCalleeSupported) {
3094 PeerConnectionFactory::Options caller_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07003095 caller_options.crypto_options.srtp.enable_aes128_sha1_32_crypto_cipher =
3096 false;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07003097 PeerConnectionFactory::Options callee_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07003098 callee_options.crypto_options.srtp.enable_aes128_sha1_32_crypto_cipher = true;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07003099 int expected_cipher_suite = rtc::SRTP_AES128_CM_SHA1_80;
3100 TestNegotiatedCipherSuite(caller_options, callee_options,
3101 expected_cipher_suite);
3102}
3103
3104TEST_P(PeerConnectionIntegrationTest, Aes128Sha1_32_CipherUsedWhenSupported) {
3105 PeerConnectionFactory::Options caller_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07003106 caller_options.crypto_options.srtp.enable_aes128_sha1_32_crypto_cipher = true;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07003107 PeerConnectionFactory::Options callee_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07003108 callee_options.crypto_options.srtp.enable_aes128_sha1_32_crypto_cipher = true;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07003109 int expected_cipher_suite = rtc::SRTP_AES128_CM_SHA1_32;
3110 TestNegotiatedCipherSuite(caller_options, callee_options,
3111 expected_cipher_suite);
3112}
3113
deadbeef1dcb1642017-03-29 21:08:16 -07003114// Test that a non-GCM cipher is used if both sides only support non-GCM.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003115TEST_P(PeerConnectionIntegrationTest, NonGcmCipherUsedWhenGcmNotSupported) {
deadbeef1dcb1642017-03-29 21:08:16 -07003116 bool local_gcm_enabled = false;
3117 bool remote_gcm_enabled = false;
Philipp Hancke2ebbff82019-10-26 06:12:55 +02003118 bool aes_ctr_enabled = true;
deadbeef1dcb1642017-03-29 21:08:16 -07003119 int expected_cipher_suite = kDefaultSrtpCryptoSuite;
3120 TestGcmNegotiationUsesCipherSuite(local_gcm_enabled, remote_gcm_enabled,
Philipp Hancke2ebbff82019-10-26 06:12:55 +02003121 aes_ctr_enabled, expected_cipher_suite);
deadbeef1dcb1642017-03-29 21:08:16 -07003122}
3123
Philipp Hancke2ebbff82019-10-26 06:12:55 +02003124// Test that a GCM cipher is used if both ends support it and non-GCM is
3125// disabled.
3126TEST_P(PeerConnectionIntegrationTest, GcmCipherUsedWhenOnlyGcmSupported) {
deadbeef1dcb1642017-03-29 21:08:16 -07003127 bool local_gcm_enabled = true;
3128 bool remote_gcm_enabled = true;
Philipp Hancke2ebbff82019-10-26 06:12:55 +02003129 bool aes_ctr_enabled = false;
deadbeef1dcb1642017-03-29 21:08:16 -07003130 int expected_cipher_suite = kDefaultSrtpCryptoSuiteGcm;
3131 TestGcmNegotiationUsesCipherSuite(local_gcm_enabled, remote_gcm_enabled,
Philipp Hancke2ebbff82019-10-26 06:12:55 +02003132 aes_ctr_enabled, expected_cipher_suite);
deadbeef1dcb1642017-03-29 21:08:16 -07003133}
3134
deadbeef7914b8c2017-04-21 03:23:33 -07003135// Verify that media can be transmitted end-to-end when GCM crypto suites are
3136// enabled. Note that the above tests, such as GcmCipherUsedWhenGcmSupported,
3137// only verify that a GCM cipher is negotiated, and not necessarily that SRTP
3138// works with it.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003139TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithGcmCipher) {
deadbeef7914b8c2017-04-21 03:23:33 -07003140 PeerConnectionFactory::Options gcm_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07003141 gcm_options.crypto_options.srtp.enable_gcm_crypto_suites = true;
Philipp Hancke2ebbff82019-10-26 06:12:55 +02003142 gcm_options.crypto_options.srtp.enable_aes128_sha1_80_crypto_cipher = false;
deadbeef7914b8c2017-04-21 03:23:33 -07003143 ASSERT_TRUE(
3144 CreatePeerConnectionWrappersWithOptions(gcm_options, gcm_options));
3145 ConnectFakeSignaling();
3146 // Do normal offer/answer and wait for some frames to be received in each
3147 // direction.
Steve Anton15324772018-01-16 10:26:49 -08003148 caller()->AddAudioVideoTracks();
3149 callee()->AddAudioVideoTracks();
deadbeef7914b8c2017-04-21 03:23:33 -07003150 caller()->CreateAndSetAndSignalOffer();
3151 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08003152 MediaExpectations media_expectations;
3153 media_expectations.ExpectBidirectionalAudioAndVideo();
3154 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef7914b8c2017-04-21 03:23:33 -07003155}
3156
deadbeef1dcb1642017-03-29 21:08:16 -07003157// This test sets up a call between two parties with audio, video and an RTP
3158// data channel.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003159TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithRtpDataChannel) {
Niels Möllerf06f9232018-08-07 12:32:18 +02003160 PeerConnectionInterface::RTCConfiguration rtc_config;
3161 rtc_config.enable_rtp_data_channel = true;
3162 rtc_config.enable_dtls_srtp = false;
3163 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(rtc_config, rtc_config));
deadbeef1dcb1642017-03-29 21:08:16 -07003164 ConnectFakeSignaling();
3165 // Expect that data channel created on caller side will show up for callee as
3166 // well.
3167 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08003168 caller()->AddAudioVideoTracks();
3169 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003170 caller()->CreateAndSetAndSignalOffer();
3171 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3172 // Ensure the existence of the RTP data channel didn't impede audio/video.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003173 MediaExpectations media_expectations;
3174 media_expectations.ExpectBidirectionalAudioAndVideo();
3175 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003176 ASSERT_NE(nullptr, caller()->data_channel());
3177 ASSERT_NE(nullptr, callee()->data_channel());
3178 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3179 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3180
3181 // Ensure data can be sent in both directions.
3182 std::string data = "hello world";
3183 SendRtpDataWithRetries(caller()->data_channel(), data, 5);
3184 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3185 kDefaultTimeout);
3186 SendRtpDataWithRetries(callee()->data_channel(), data, 5);
3187 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3188 kDefaultTimeout);
3189}
3190
3191// Ensure that an RTP data channel is signaled as closed for the caller when
3192// the callee rejects it in a subsequent offer.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003193TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07003194 RtpDataChannelSignaledClosedInCalleeOffer) {
3195 // Same procedure as above test.
Niels Möllerf06f9232018-08-07 12:32:18 +02003196 PeerConnectionInterface::RTCConfiguration rtc_config;
3197 rtc_config.enable_rtp_data_channel = true;
3198 rtc_config.enable_dtls_srtp = false;
3199 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(rtc_config, rtc_config));
deadbeef1dcb1642017-03-29 21:08:16 -07003200 ConnectFakeSignaling();
3201 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08003202 caller()->AddAudioVideoTracks();
3203 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003204 caller()->CreateAndSetAndSignalOffer();
3205 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3206 ASSERT_NE(nullptr, caller()->data_channel());
3207 ASSERT_NE(nullptr, callee()->data_channel());
3208 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3209 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3210
3211 // Close the data channel on the callee, and do an updated offer/answer.
3212 callee()->data_channel()->Close();
3213 callee()->CreateAndSetAndSignalOffer();
3214 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3215 EXPECT_FALSE(caller()->data_observer()->IsOpen());
3216 EXPECT_FALSE(callee()->data_observer()->IsOpen());
3217}
3218
3219// Tests that data is buffered in an RTP data channel until an observer is
3220// registered for it.
3221//
3222// NOTE: RTP data channels can receive data before the underlying
3223// transport has detected that a channel is writable and thus data can be
3224// received before the data channel state changes to open. That is hard to test
3225// but the same buffering is expected to be used in that case.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003226TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07003227 DataBufferedUntilRtpDataChannelObserverRegistered) {
3228 // Use fake clock and simulated network delay so that we predictably can wait
3229 // until an SCTP message has been delivered without "sleep()"ing.
3230 rtc::ScopedFakeClock fake_clock;
3231 // Some things use a time of "0" as a special value, so we need to start out
3232 // the fake clock at a nonzero time.
3233 // TODO(deadbeef): Fix this.
Sebastian Jansson5f83cf02018-05-08 14:52:22 +02003234 fake_clock.AdvanceTime(webrtc::TimeDelta::seconds(1));
deadbeef1dcb1642017-03-29 21:08:16 -07003235 virtual_socket_server()->set_delay_mean(5); // 5 ms per hop.
3236 virtual_socket_server()->UpdateDelayDistribution();
3237
Niels Möllerf06f9232018-08-07 12:32:18 +02003238 PeerConnectionInterface::RTCConfiguration rtc_config;
3239 rtc_config.enable_rtp_data_channel = true;
3240 rtc_config.enable_dtls_srtp = false;
3241 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(rtc_config, rtc_config));
deadbeef1dcb1642017-03-29 21:08:16 -07003242 ConnectFakeSignaling();
3243 caller()->CreateDataChannel();
3244 caller()->CreateAndSetAndSignalOffer();
3245 ASSERT_TRUE(caller()->data_channel() != nullptr);
3246 ASSERT_TRUE_SIMULATED_WAIT(callee()->data_channel() != nullptr,
3247 kDefaultTimeout, fake_clock);
3248 ASSERT_TRUE_SIMULATED_WAIT(caller()->data_observer()->IsOpen(),
3249 kDefaultTimeout, fake_clock);
3250 ASSERT_EQ_SIMULATED_WAIT(DataChannelInterface::kOpen,
3251 callee()->data_channel()->state(), kDefaultTimeout,
3252 fake_clock);
3253
3254 // Unregister the observer which is normally automatically registered.
3255 callee()->data_channel()->UnregisterObserver();
3256 // Send data and advance fake clock until it should have been received.
3257 std::string data = "hello world";
3258 caller()->data_channel()->Send(DataBuffer(data));
3259 SIMULATED_WAIT(false, 50, fake_clock);
3260
3261 // Attach data channel and expect data to be received immediately. Note that
3262 // EXPECT_EQ_WAIT is used, such that the simulated clock is not advanced any
3263 // further, but data can be received even if the callback is asynchronous.
3264 MockDataChannelObserver new_observer(callee()->data_channel());
3265 EXPECT_EQ_SIMULATED_WAIT(data, new_observer.last_message(), kDefaultTimeout,
3266 fake_clock);
Seth Hampson1d4a76d2018-06-19 14:31:41 -07003267 // Closing the PeerConnections destroys the ports before the ScopedFakeClock.
3268 // If this is not done a DCHECK can be hit in ports.cc, because a large
3269 // negative number is calculated for the rtt due to the global clock changing.
Steve Antond91969e2019-05-30 12:27:03 -07003270 ClosePeerConnections();
deadbeef1dcb1642017-03-29 21:08:16 -07003271}
3272
3273// This test sets up a call between two parties with audio, video and but only
3274// the caller client supports RTP data channels.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003275TEST_P(PeerConnectionIntegrationTest, RtpDataChannelsRejectedByCallee) {
Niels Möllerf06f9232018-08-07 12:32:18 +02003276 PeerConnectionInterface::RTCConfiguration rtc_config_1;
3277 rtc_config_1.enable_rtp_data_channel = true;
deadbeef1dcb1642017-03-29 21:08:16 -07003278 // Must disable DTLS to make negotiation succeed.
Niels Möllerf06f9232018-08-07 12:32:18 +02003279 rtc_config_1.enable_dtls_srtp = false;
3280 PeerConnectionInterface::RTCConfiguration rtc_config_2;
3281 rtc_config_2.enable_dtls_srtp = false;
3282 rtc_config_2.enable_dtls_srtp = false;
3283 ASSERT_TRUE(
3284 CreatePeerConnectionWrappersWithConfig(rtc_config_1, rtc_config_2));
deadbeef1dcb1642017-03-29 21:08:16 -07003285 ConnectFakeSignaling();
3286 caller()->CreateDataChannel();
Harald Alvestrandf3736ed2019-04-08 13:09:30 +02003287 ASSERT_TRUE(caller()->data_channel() != nullptr);
Steve Anton15324772018-01-16 10:26:49 -08003288 caller()->AddAudioVideoTracks();
3289 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003290 caller()->CreateAndSetAndSignalOffer();
3291 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3292 // The caller should still have a data channel, but it should be closed, and
3293 // one should ever have been created for the callee.
3294 EXPECT_TRUE(caller()->data_channel() != nullptr);
3295 EXPECT_FALSE(caller()->data_observer()->IsOpen());
3296 EXPECT_EQ(nullptr, callee()->data_channel());
3297}
3298
3299// This test sets up a call between two parties with audio, and video. When
3300// audio and video is setup and flowing, an RTP data channel is negotiated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003301TEST_P(PeerConnectionIntegrationTest, AddRtpDataChannelInSubsequentOffer) {
Niels Möllerf06f9232018-08-07 12:32:18 +02003302 PeerConnectionInterface::RTCConfiguration rtc_config;
3303 rtc_config.enable_rtp_data_channel = true;
3304 rtc_config.enable_dtls_srtp = false;
3305 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(rtc_config, rtc_config));
deadbeef1dcb1642017-03-29 21:08:16 -07003306 ConnectFakeSignaling();
3307 // Do initial offer/answer with audio/video.
Steve Anton15324772018-01-16 10:26:49 -08003308 caller()->AddAudioVideoTracks();
3309 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003310 caller()->CreateAndSetAndSignalOffer();
3311 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3312 // Create data channel and do new offer and answer.
3313 caller()->CreateDataChannel();
3314 caller()->CreateAndSetAndSignalOffer();
3315 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3316 ASSERT_NE(nullptr, caller()->data_channel());
3317 ASSERT_NE(nullptr, callee()->data_channel());
3318 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3319 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3320 // Ensure data can be sent in both directions.
3321 std::string data = "hello world";
3322 SendRtpDataWithRetries(caller()->data_channel(), data, 5);
3323 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3324 kDefaultTimeout);
3325 SendRtpDataWithRetries(callee()->data_channel(), data, 5);
3326 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3327 kDefaultTimeout);
3328}
3329
3330#ifdef HAVE_SCTP
3331
3332// This test sets up a call between two parties with audio, video and an SCTP
3333// data channel.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003334TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithSctpDataChannel) {
deadbeef1dcb1642017-03-29 21:08:16 -07003335 ASSERT_TRUE(CreatePeerConnectionWrappers());
3336 ConnectFakeSignaling();
3337 // Expect that data channel created on caller side will show up for callee as
3338 // well.
3339 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08003340 caller()->AddAudioVideoTracks();
3341 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003342 caller()->CreateAndSetAndSignalOffer();
3343 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3344 // Ensure the existence of the SCTP data channel didn't impede audio/video.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003345 MediaExpectations media_expectations;
3346 media_expectations.ExpectBidirectionalAudioAndVideo();
3347 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003348 // Caller data channel should already exist (it created one). Callee data
3349 // channel may not exist yet, since negotiation happens in-band, not in SDP.
3350 ASSERT_NE(nullptr, caller()->data_channel());
3351 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3352 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3353 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3354
3355 // Ensure data can be sent in both directions.
3356 std::string data = "hello world";
3357 caller()->data_channel()->Send(DataBuffer(data));
3358 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3359 kDefaultTimeout);
3360 callee()->data_channel()->Send(DataBuffer(data));
3361 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3362 kDefaultTimeout);
3363}
3364
3365// Ensure that when the callee closes an SCTP data channel, the closing
3366// procedure results in the data channel being closed for the caller as well.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003367TEST_P(PeerConnectionIntegrationTest, CalleeClosesSctpDataChannel) {
deadbeef1dcb1642017-03-29 21:08:16 -07003368 // Same procedure as above test.
3369 ASSERT_TRUE(CreatePeerConnectionWrappers());
3370 ConnectFakeSignaling();
3371 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08003372 caller()->AddAudioVideoTracks();
3373 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003374 caller()->CreateAndSetAndSignalOffer();
3375 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3376 ASSERT_NE(nullptr, caller()->data_channel());
3377 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3378 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3379 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3380
3381 // Close the data channel on the callee side, and wait for it to reach the
3382 // "closed" state on both sides.
3383 callee()->data_channel()->Close();
3384 EXPECT_TRUE_WAIT(!caller()->data_observer()->IsOpen(), kDefaultTimeout);
3385 EXPECT_TRUE_WAIT(!callee()->data_observer()->IsOpen(), kDefaultTimeout);
3386}
3387
Seth Hampson2f0d7022018-02-20 11:54:42 -08003388TEST_P(PeerConnectionIntegrationTest, SctpDataChannelConfigSentToOtherSide) {
Steve Antonda6c0952017-10-23 11:41:54 -07003389 ASSERT_TRUE(CreatePeerConnectionWrappers());
3390 ConnectFakeSignaling();
3391 webrtc::DataChannelInit init;
3392 init.id = 53;
3393 init.maxRetransmits = 52;
3394 caller()->CreateDataChannel("data-channel", &init);
Steve Anton15324772018-01-16 10:26:49 -08003395 caller()->AddAudioVideoTracks();
3396 callee()->AddAudioVideoTracks();
Steve Antonda6c0952017-10-23 11:41:54 -07003397 caller()->CreateAndSetAndSignalOffer();
3398 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Steve Anton074dece2017-10-24 13:04:12 -07003399 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3400 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
Harald Alvestrand5c4d2ee2019-04-01 12:58:15 +02003401 // Since "negotiated" is false, the "id" parameter should be ignored.
3402 EXPECT_NE(init.id, callee()->data_channel()->id());
Steve Antonda6c0952017-10-23 11:41:54 -07003403 EXPECT_EQ("data-channel", callee()->data_channel()->label());
3404 EXPECT_EQ(init.maxRetransmits, callee()->data_channel()->maxRetransmits());
3405 EXPECT_FALSE(callee()->data_channel()->negotiated());
3406}
3407
deadbeef1dcb1642017-03-29 21:08:16 -07003408// Test usrsctp's ability to process unordered data stream, where data actually
3409// arrives out of order using simulated delays. Previously there have been some
3410// bugs in this area.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003411TEST_P(PeerConnectionIntegrationTest, StressTestUnorderedSctpDataChannel) {
deadbeef1dcb1642017-03-29 21:08:16 -07003412 // Introduce random network delays.
3413 // Otherwise it's not a true "unordered" test.
3414 virtual_socket_server()->set_delay_mean(20);
3415 virtual_socket_server()->set_delay_stddev(5);
3416 virtual_socket_server()->UpdateDelayDistribution();
3417 // Normal procedure, but with unordered data channel config.
3418 ASSERT_TRUE(CreatePeerConnectionWrappers());
3419 ConnectFakeSignaling();
3420 webrtc::DataChannelInit init;
3421 init.ordered = false;
3422 caller()->CreateDataChannel(&init);
3423 caller()->CreateAndSetAndSignalOffer();
3424 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3425 ASSERT_NE(nullptr, caller()->data_channel());
3426 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3427 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3428 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3429
3430 static constexpr int kNumMessages = 100;
3431 // Deliberately chosen to be larger than the MTU so messages get fragmented.
3432 static constexpr size_t kMaxMessageSize = 4096;
3433 // Create and send random messages.
3434 std::vector<std::string> sent_messages;
3435 for (int i = 0; i < kNumMessages; ++i) {
3436 size_t length =
3437 (rand() % kMaxMessageSize) + 1; // NOLINT (rand_r instead of rand)
3438 std::string message;
3439 ASSERT_TRUE(rtc::CreateRandomString(length, &message));
3440 caller()->data_channel()->Send(DataBuffer(message));
3441 callee()->data_channel()->Send(DataBuffer(message));
3442 sent_messages.push_back(message);
3443 }
3444
3445 // Wait for all messages to be received.
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02003446 EXPECT_EQ_WAIT(rtc::checked_cast<size_t>(kNumMessages),
deadbeef1dcb1642017-03-29 21:08:16 -07003447 caller()->data_observer()->received_message_count(),
3448 kDefaultTimeout);
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02003449 EXPECT_EQ_WAIT(rtc::checked_cast<size_t>(kNumMessages),
deadbeef1dcb1642017-03-29 21:08:16 -07003450 callee()->data_observer()->received_message_count(),
3451 kDefaultTimeout);
3452
3453 // Sort and compare to make sure none of the messages were corrupted.
3454 std::vector<std::string> caller_received_messages =
3455 caller()->data_observer()->messages();
3456 std::vector<std::string> callee_received_messages =
3457 callee()->data_observer()->messages();
Steve Anton64b626b2019-01-28 17:25:26 -08003458 absl::c_sort(sent_messages);
3459 absl::c_sort(caller_received_messages);
3460 absl::c_sort(callee_received_messages);
deadbeef1dcb1642017-03-29 21:08:16 -07003461 EXPECT_EQ(sent_messages, caller_received_messages);
3462 EXPECT_EQ(sent_messages, callee_received_messages);
3463}
3464
3465// This test sets up a call between two parties with audio, and video. When
3466// audio and video are setup and flowing, an SCTP data channel is negotiated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003467TEST_P(PeerConnectionIntegrationTest, AddSctpDataChannelInSubsequentOffer) {
deadbeef1dcb1642017-03-29 21:08:16 -07003468 ASSERT_TRUE(CreatePeerConnectionWrappers());
3469 ConnectFakeSignaling();
3470 // Do initial offer/answer with audio/video.
Steve Anton15324772018-01-16 10:26:49 -08003471 caller()->AddAudioVideoTracks();
3472 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003473 caller()->CreateAndSetAndSignalOffer();
3474 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3475 // Create data channel and do new offer and answer.
3476 caller()->CreateDataChannel();
3477 caller()->CreateAndSetAndSignalOffer();
3478 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3479 // Caller data channel should already exist (it created one). Callee data
3480 // channel may not exist yet, since negotiation happens in-band, not in SDP.
3481 ASSERT_NE(nullptr, caller()->data_channel());
3482 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3483 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3484 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3485 // Ensure data can be sent in both directions.
3486 std::string data = "hello world";
3487 caller()->data_channel()->Send(DataBuffer(data));
3488 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3489 kDefaultTimeout);
3490 callee()->data_channel()->Send(DataBuffer(data));
3491 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3492 kDefaultTimeout);
3493}
3494
deadbeef7914b8c2017-04-21 03:23:33 -07003495// Set up a connection initially just using SCTP data channels, later upgrading
3496// to audio/video, ensuring frames are received end-to-end. Effectively the
3497// inverse of the test above.
3498// This was broken in M57; see https://crbug.com/711243
Seth Hampson2f0d7022018-02-20 11:54:42 -08003499TEST_P(PeerConnectionIntegrationTest, SctpDataChannelToAudioVideoUpgrade) {
deadbeef7914b8c2017-04-21 03:23:33 -07003500 ASSERT_TRUE(CreatePeerConnectionWrappers());
3501 ConnectFakeSignaling();
3502 // Do initial offer/answer with just data channel.
3503 caller()->CreateDataChannel();
3504 caller()->CreateAndSetAndSignalOffer();
3505 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3506 // Wait until data can be sent over the data channel.
3507 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3508 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3509 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3510
3511 // Do subsequent offer/answer with two-way audio and video. Audio and video
3512 // should end up bundled on the DTLS/ICE transport already used for data.
Steve Anton15324772018-01-16 10:26:49 -08003513 caller()->AddAudioVideoTracks();
3514 callee()->AddAudioVideoTracks();
deadbeef7914b8c2017-04-21 03:23:33 -07003515 caller()->CreateAndSetAndSignalOffer();
3516 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08003517 MediaExpectations media_expectations;
3518 media_expectations.ExpectBidirectionalAudioAndVideo();
3519 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef7914b8c2017-04-21 03:23:33 -07003520}
3521
deadbeef8b7e9ad2017-05-25 09:38:55 -07003522static void MakeSpecCompliantSctpOffer(cricket::SessionDescription* desc) {
Harald Alvestrand5fc28b12019-05-13 13:36:16 +02003523 cricket::SctpDataContentDescription* dcd_offer =
3524 GetFirstSctpDataContentDescription(desc);
Steve Antonb1c1de12017-12-21 15:14:30 -08003525 ASSERT_TRUE(dcd_offer);
deadbeef8b7e9ad2017-05-25 09:38:55 -07003526 dcd_offer->set_use_sctpmap(false);
3527 dcd_offer->set_protocol("UDP/DTLS/SCTP");
3528}
3529
3530// Test that the data channel works when a spec-compliant SCTP m= section is
3531// offered (using "a=sctp-port" instead of "a=sctpmap", and using
3532// "UDP/DTLS/SCTP" as the protocol).
Seth Hampson2f0d7022018-02-20 11:54:42 -08003533TEST_P(PeerConnectionIntegrationTest,
deadbeef8b7e9ad2017-05-25 09:38:55 -07003534 DataChannelWorksWhenSpecCompliantSctpOfferReceived) {
3535 ASSERT_TRUE(CreatePeerConnectionWrappers());
3536 ConnectFakeSignaling();
3537 caller()->CreateDataChannel();
3538 caller()->SetGeneratedSdpMunger(MakeSpecCompliantSctpOffer);
3539 caller()->CreateAndSetAndSignalOffer();
3540 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3541 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3542 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3543 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3544
3545 // Ensure data can be sent in both directions.
3546 std::string data = "hello world";
3547 caller()->data_channel()->Send(DataBuffer(data));
3548 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3549 kDefaultTimeout);
3550 callee()->data_channel()->Send(DataBuffer(data));
3551 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3552 kDefaultTimeout);
3553}
3554
Bjorn A Mellemb689af42019-08-21 10:44:59 -07003555// Tests that the datagram transport to SCTP fallback works correctly when
3556// datagram transport negotiation fails.
3557TEST_P(PeerConnectionIntegrationTest,
3558 DatagramTransportDataChannelFallbackToSctp) {
3559 PeerConnectionInterface::RTCConfiguration rtc_config;
3560 rtc_config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyRequire;
3561 rtc_config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
3562 rtc_config.use_datagram_transport_for_data_channels = true;
3563
3564 // Configure one endpoint to use datagram transport for data channels while
3565 // the other does not.
3566 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
3567 rtc_config, RTCConfiguration(),
3568 loopback_media_transports()->first_factory(), nullptr));
3569 ConnectFakeSignaling();
3570
3571 // The caller offers a data channel using either datagram transport or SCTP.
3572 caller()->CreateDataChannel();
3573 caller()->AddAudioVideoTracks();
3574 callee()->AddAudioVideoTracks();
3575 caller()->CreateAndSetAndSignalOffer();
3576 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3577
3578 // Negotiation should fallback to SCTP, allowing the data channel to be
3579 // established.
3580 ASSERT_NE(nullptr, caller()->data_channel());
3581 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3582 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3583 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3584
3585 // Ensure data can be sent in both directions.
3586 std::string data = "hello world";
3587 caller()->data_channel()->Send(DataBuffer(data));
3588 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3589 kDefaultTimeout);
3590 callee()->data_channel()->Send(DataBuffer(data));
3591 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3592 kDefaultTimeout);
3593
3594 // Ensure that failure of the datagram negotiation doesn't impede media flow.
3595 MediaExpectations media_expectations;
3596 media_expectations.ExpectBidirectionalAudioAndVideo();
3597 ASSERT_TRUE(ExpectNewFrames(media_expectations));
3598}
3599
Bjorn A Mellemfc604aa2019-09-24 14:59:21 -07003600// Tests that the data channel transport works correctly when datagram transport
3601// negotiation succeeds and does not fall back to SCTP.
3602TEST_P(PeerConnectionIntegrationTest,
3603 DatagramTransportDataChannelDoesNotFallbackToSctp) {
3604 PeerConnectionInterface::RTCConfiguration rtc_config;
3605 rtc_config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyRequire;
3606 rtc_config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
3607 rtc_config.use_datagram_transport_for_data_channels = true;
3608
3609 // Configure one endpoint to use datagram transport for data channels while
3610 // the other does not.
3611 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
3612 rtc_config, rtc_config, loopback_media_transports()->first_factory(),
3613 loopback_media_transports()->second_factory()));
3614 ConnectFakeSignaling();
3615
3616 // The caller offers a data channel using either datagram transport or SCTP.
3617 caller()->CreateDataChannel();
3618 caller()->AddAudioVideoTracks();
3619 callee()->AddAudioVideoTracks();
3620 caller()->CreateAndSetAndSignalOffer();
3621 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3622
3623 // Ensure that the data channel transport is ready.
3624 loopback_media_transports()->SetState(webrtc::MediaTransportState::kWritable);
3625 loopback_media_transports()->FlushAsyncInvokes();
3626
3627 // Negotiation should succeed, allowing the data channel to be established.
3628 ASSERT_NE(nullptr, caller()->data_channel());
3629 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3630 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3631 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3632
3633 // Ensure data can be sent in both directions.
3634 std::string data = "hello world";
3635 caller()->data_channel()->Send(DataBuffer(data));
3636 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3637 kDefaultTimeout);
3638 callee()->data_channel()->Send(DataBuffer(data));
3639 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3640 kDefaultTimeout);
3641
3642 // Ensure that failure of the datagram negotiation doesn't impede media flow.
3643 MediaExpectations media_expectations;
3644 media_expectations.ExpectBidirectionalAudioAndVideo();
3645 ASSERT_TRUE(ExpectNewFrames(media_expectations));
3646}
3647
Bjorn A Mellem8e1343a2019-09-30 15:12:47 -07003648TEST_P(PeerConnectionIntegrationTest,
3649 DatagramTransportDataChannelWithMediaOnCaller) {
3650 // Configure the caller to attempt use of datagram transport for media and
3651 // data channels.
3652 PeerConnectionInterface::RTCConfiguration offerer_config;
3653 offerer_config.rtcp_mux_policy =
3654 PeerConnectionInterface::kRtcpMuxPolicyRequire;
3655 offerer_config.bundle_policy =
3656 PeerConnectionInterface::kBundlePolicyMaxBundle;
3657 offerer_config.use_datagram_transport_for_data_channels = true;
3658 offerer_config.use_datagram_transport = true;
3659
3660 // Configure the callee to only use datagram transport for data channels.
3661 PeerConnectionInterface::RTCConfiguration answerer_config;
3662 answerer_config.rtcp_mux_policy =
3663 PeerConnectionInterface::kRtcpMuxPolicyRequire;
3664 answerer_config.bundle_policy =
3665 PeerConnectionInterface::kBundlePolicyMaxBundle;
3666 answerer_config.use_datagram_transport_for_data_channels = true;
3667
3668 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
3669 offerer_config, answerer_config,
3670 loopback_media_transports()->first_factory(),
3671 loopback_media_transports()->second_factory()));
3672 ConnectFakeSignaling();
3673
3674 // Offer both media and data.
3675 caller()->AddAudioVideoTracks();
3676 callee()->AddAudioVideoTracks();
3677 caller()->CreateDataChannel();
3678 caller()->CreateAndSetAndSignalOffer();
3679 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3680
3681 // Ensure that the data channel transport is ready.
3682 loopback_media_transports()->SetState(webrtc::MediaTransportState::kWritable);
3683 loopback_media_transports()->FlushAsyncInvokes();
3684
3685 ASSERT_NE(nullptr, caller()->data_channel());
3686 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3687 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3688 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3689
3690 // Both endpoints should agree to use datagram transport for data channels.
3691 EXPECT_EQ(nullptr, caller()->pc()->GetSctpTransport());
3692 EXPECT_EQ(nullptr, callee()->pc()->GetSctpTransport());
3693
3694 // Ensure data can be sent in both directions.
3695 std::string data = "hello world";
3696 caller()->data_channel()->Send(DataBuffer(data));
3697 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3698 kDefaultTimeout);
3699 callee()->data_channel()->Send(DataBuffer(data));
3700 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3701 kDefaultTimeout);
3702
3703 // Media flow should not be impacted.
3704 MediaExpectations media_expectations;
3705 media_expectations.ExpectBidirectionalAudioAndVideo();
3706 ASSERT_TRUE(ExpectNewFrames(media_expectations));
3707}
3708
3709TEST_P(PeerConnectionIntegrationTest,
3710 DatagramTransportMediaWithDataChannelOnCaller) {
3711 // Configure the caller to attempt use of datagram transport for media and
3712 // data channels.
3713 PeerConnectionInterface::RTCConfiguration offerer_config;
3714 offerer_config.rtcp_mux_policy =
3715 PeerConnectionInterface::kRtcpMuxPolicyRequire;
3716 offerer_config.bundle_policy =
3717 PeerConnectionInterface::kBundlePolicyMaxBundle;
3718 offerer_config.use_datagram_transport_for_data_channels = true;
3719 offerer_config.use_datagram_transport = true;
3720
3721 // Configure the callee to only use datagram transport for media.
3722 PeerConnectionInterface::RTCConfiguration answerer_config;
3723 answerer_config.rtcp_mux_policy =
3724 PeerConnectionInterface::kRtcpMuxPolicyRequire;
3725 answerer_config.bundle_policy =
3726 PeerConnectionInterface::kBundlePolicyMaxBundle;
3727 answerer_config.use_datagram_transport = true;
3728
3729 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
3730 offerer_config, answerer_config,
3731 loopback_media_transports()->first_factory(),
3732 loopback_media_transports()->second_factory()));
3733 ConnectFakeSignaling();
3734
3735 // Offer both media and data.
3736 caller()->AddAudioVideoTracks();
3737 callee()->AddAudioVideoTracks();
3738 caller()->CreateDataChannel();
3739 caller()->CreateAndSetAndSignalOffer();
3740 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3741
3742 // Ensure that the data channel transport is ready.
3743 loopback_media_transports()->SetState(webrtc::MediaTransportState::kWritable);
3744 loopback_media_transports()->FlushAsyncInvokes();
3745
3746 ASSERT_NE(nullptr, caller()->data_channel());
3747 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3748 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3749 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3750
3751 // Both endpoints should agree to use SCTP for data channels.
3752 EXPECT_NE(nullptr, caller()->pc()->GetSctpTransport());
3753 EXPECT_NE(nullptr, callee()->pc()->GetSctpTransport());
3754
3755 // Ensure data can be sent in both directions.
3756 std::string data = "hello world";
3757 caller()->data_channel()->Send(DataBuffer(data));
3758 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3759 kDefaultTimeout);
3760 callee()->data_channel()->Send(DataBuffer(data));
3761 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3762 kDefaultTimeout);
3763
3764 // Media flow should not be impacted.
3765 MediaExpectations media_expectations;
3766 media_expectations.ExpectBidirectionalAudioAndVideo();
3767 ASSERT_TRUE(ExpectNewFrames(media_expectations));
3768}
3769
3770TEST_P(PeerConnectionIntegrationTest,
3771 DatagramTransportDataChannelWithMediaOnCallee) {
3772 // Configure the caller to attempt use of datagram transport for data
3773 // channels.
3774 PeerConnectionInterface::RTCConfiguration offerer_config;
3775 offerer_config.rtcp_mux_policy =
3776 PeerConnectionInterface::kRtcpMuxPolicyRequire;
3777 offerer_config.bundle_policy =
3778 PeerConnectionInterface::kBundlePolicyMaxBundle;
3779 offerer_config.use_datagram_transport_for_data_channels = true;
3780
3781 // Configure the callee to use datagram transport for data channels and media.
3782 PeerConnectionInterface::RTCConfiguration answerer_config;
3783 answerer_config.rtcp_mux_policy =
3784 PeerConnectionInterface::kRtcpMuxPolicyRequire;
3785 answerer_config.bundle_policy =
3786 PeerConnectionInterface::kBundlePolicyMaxBundle;
3787 answerer_config.use_datagram_transport_for_data_channels = true;
3788 answerer_config.use_datagram_transport = true;
3789
3790 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
3791 offerer_config, answerer_config,
3792 loopback_media_transports()->first_factory(),
3793 loopback_media_transports()->second_factory()));
3794 ConnectFakeSignaling();
3795
3796 // Offer both media and data.
3797 caller()->AddAudioVideoTracks();
3798 callee()->AddAudioVideoTracks();
3799 caller()->CreateDataChannel();
3800 caller()->CreateAndSetAndSignalOffer();
3801 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3802
3803 // Ensure that the data channel transport is ready.
3804 loopback_media_transports()->SetState(webrtc::MediaTransportState::kWritable);
3805 loopback_media_transports()->FlushAsyncInvokes();
3806
3807 ASSERT_NE(nullptr, caller()->data_channel());
3808 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3809 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3810 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3811
3812 // Both endpoints should agree to use datagram transport for data channels.
3813 EXPECT_EQ(nullptr, caller()->pc()->GetSctpTransport());
3814 EXPECT_EQ(nullptr, callee()->pc()->GetSctpTransport());
3815
3816 // Ensure data can be sent in both directions.
3817 std::string data = "hello world";
3818 caller()->data_channel()->Send(DataBuffer(data));
3819 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3820 kDefaultTimeout);
3821 callee()->data_channel()->Send(DataBuffer(data));
3822 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3823 kDefaultTimeout);
3824
3825 // Media flow should not be impacted.
3826 MediaExpectations media_expectations;
3827 media_expectations.ExpectBidirectionalAudioAndVideo();
3828 ASSERT_TRUE(ExpectNewFrames(media_expectations));
3829}
3830
3831TEST_P(PeerConnectionIntegrationTest,
3832 DatagramTransportMediaWithDataChannelOnCallee) {
3833 // Configure the caller to attempt use of datagram transport for media.
3834 PeerConnectionInterface::RTCConfiguration offerer_config;
3835 offerer_config.rtcp_mux_policy =
3836 PeerConnectionInterface::kRtcpMuxPolicyRequire;
3837 offerer_config.bundle_policy =
3838 PeerConnectionInterface::kBundlePolicyMaxBundle;
3839 offerer_config.use_datagram_transport = true;
3840
3841 // Configure the callee to only use datagram transport for media and data
3842 // channels.
3843 PeerConnectionInterface::RTCConfiguration answerer_config;
3844 answerer_config.rtcp_mux_policy =
3845 PeerConnectionInterface::kRtcpMuxPolicyRequire;
3846 answerer_config.bundle_policy =
3847 PeerConnectionInterface::kBundlePolicyMaxBundle;
3848 answerer_config.use_datagram_transport = true;
3849 answerer_config.use_datagram_transport_for_data_channels = true;
3850
3851 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
3852 offerer_config, answerer_config,
3853 loopback_media_transports()->first_factory(),
3854 loopback_media_transports()->second_factory()));
3855 ConnectFakeSignaling();
3856
3857 // Offer both media and data.
3858 caller()->AddAudioVideoTracks();
3859 callee()->AddAudioVideoTracks();
3860 caller()->CreateDataChannel();
3861 caller()->CreateAndSetAndSignalOffer();
3862 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3863
3864 // Ensure that the data channel transport is ready.
3865 loopback_media_transports()->SetState(webrtc::MediaTransportState::kWritable);
3866 loopback_media_transports()->FlushAsyncInvokes();
3867
3868 ASSERT_NE(nullptr, caller()->data_channel());
3869 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3870 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3871 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3872
3873 // Both endpoints should agree to use SCTP for data channels.
3874 EXPECT_NE(nullptr, caller()->pc()->GetSctpTransport());
3875 EXPECT_NE(nullptr, callee()->pc()->GetSctpTransport());
3876
3877 // Ensure data can be sent in both directions.
3878 std::string data = "hello world";
3879 caller()->data_channel()->Send(DataBuffer(data));
3880 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3881 kDefaultTimeout);
3882 callee()->data_channel()->Send(DataBuffer(data));
3883 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3884 kDefaultTimeout);
3885
3886 // Media flow should not be impacted.
3887 MediaExpectations media_expectations;
3888 media_expectations.ExpectBidirectionalAudioAndVideo();
3889 ASSERT_TRUE(ExpectNewFrames(media_expectations));
3890}
3891
3892TEST_P(PeerConnectionIntegrationTest, DatagramTransportDataChannelAndMedia) {
3893 // Configure the caller to use datagram transport for data channels and media.
3894 PeerConnectionInterface::RTCConfiguration offerer_config;
3895 offerer_config.rtcp_mux_policy =
3896 PeerConnectionInterface::kRtcpMuxPolicyRequire;
3897 offerer_config.bundle_policy =
3898 PeerConnectionInterface::kBundlePolicyMaxBundle;
3899 offerer_config.use_datagram_transport_for_data_channels = true;
3900 offerer_config.use_datagram_transport = true;
3901
3902 // Configure the callee to use datagram transport for data channels and media.
3903 PeerConnectionInterface::RTCConfiguration answerer_config;
3904 answerer_config.rtcp_mux_policy =
3905 PeerConnectionInterface::kRtcpMuxPolicyRequire;
3906 answerer_config.bundle_policy =
3907 PeerConnectionInterface::kBundlePolicyMaxBundle;
3908 answerer_config.use_datagram_transport_for_data_channels = true;
3909 answerer_config.use_datagram_transport = true;
3910
3911 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
3912 offerer_config, answerer_config,
3913 loopback_media_transports()->first_factory(),
3914 loopback_media_transports()->second_factory()));
3915 ConnectFakeSignaling();
3916
3917 // Offer both media and data.
3918 caller()->AddAudioVideoTracks();
3919 callee()->AddAudioVideoTracks();
3920 caller()->CreateDataChannel();
3921 caller()->CreateAndSetAndSignalOffer();
3922 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3923
3924 // Ensure that the data channel transport is ready.
3925 loopback_media_transports()->SetState(webrtc::MediaTransportState::kWritable);
3926 loopback_media_transports()->FlushAsyncInvokes();
3927
3928 ASSERT_NE(nullptr, caller()->data_channel());
3929 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3930 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3931 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3932
3933 // Both endpoints should agree to use datagram transport for data channels.
3934 EXPECT_EQ(nullptr, caller()->pc()->GetSctpTransport());
3935 EXPECT_EQ(nullptr, callee()->pc()->GetSctpTransport());
3936
3937 // Ensure data can be sent in both directions.
3938 std::string data = "hello world";
3939 caller()->data_channel()->Send(DataBuffer(data));
3940 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3941 kDefaultTimeout);
3942 callee()->data_channel()->Send(DataBuffer(data));
3943 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3944 kDefaultTimeout);
3945
3946 // Media flow should not be impacted.
3947 MediaExpectations media_expectations;
3948 media_expectations.ExpectBidirectionalAudioAndVideo();
3949 ASSERT_TRUE(ExpectNewFrames(media_expectations));
3950}
3951
Bjorn A Mellem7da4e562019-09-26 11:02:11 -07003952// Tests that data channels use SCTP instead of datagram transport if datagram
3953// transport is configured in receive-only mode on the caller.
3954TEST_P(PeerConnectionIntegrationTest,
3955 DatagramTransportDataChannelReceiveOnlyOnCallerUsesSctp) {
3956 PeerConnectionInterface::RTCConfiguration rtc_config;
3957 rtc_config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyRequire;
3958 rtc_config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
3959 rtc_config.use_datagram_transport_for_data_channels = true;
3960 rtc_config.use_datagram_transport_for_data_channels_receive_only = true;
3961
3962 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
3963 rtc_config, rtc_config, loopback_media_transports()->first_factory(),
3964 loopback_media_transports()->second_factory()));
3965 ConnectFakeSignaling();
3966
3967 // The caller should offer a data channel using SCTP.
3968 caller()->CreateDataChannel();
3969 caller()->AddAudioVideoTracks();
3970 callee()->AddAudioVideoTracks();
3971 caller()->CreateAndSetAndSignalOffer();
3972 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3973
3974 ASSERT_NE(nullptr, caller()->data_channel());
3975 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3976 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3977 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3978
3979 // SCTP transports should be present, since they are in use.
3980 EXPECT_NE(caller()->pc()->GetSctpTransport(), nullptr);
3981 EXPECT_NE(callee()->pc()->GetSctpTransport(), nullptr);
3982
3983 // Ensure data can be sent in both directions.
3984 std::string data = "hello world";
3985 caller()->data_channel()->Send(DataBuffer(data));
3986 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3987 kDefaultTimeout);
3988 callee()->data_channel()->Send(DataBuffer(data));
3989 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3990 kDefaultTimeout);
3991}
3992
deadbeef1dcb1642017-03-29 21:08:16 -07003993#endif // HAVE_SCTP
3994
Bjorn A Mellem7da4e562019-09-26 11:02:11 -07003995// Tests that a callee configured for receive-only use of datagram transport
3996// data channels accepts them on incoming calls.
3997TEST_P(PeerConnectionIntegrationTest,
3998 DatagramTransportDataChannelReceiveOnlyOnCallee) {
3999 PeerConnectionInterface::RTCConfiguration offerer_config;
4000 offerer_config.rtcp_mux_policy =
4001 PeerConnectionInterface::kRtcpMuxPolicyRequire;
4002 offerer_config.bundle_policy =
4003 PeerConnectionInterface::kBundlePolicyMaxBundle;
4004 offerer_config.use_datagram_transport_for_data_channels = true;
4005
4006 PeerConnectionInterface::RTCConfiguration answerer_config;
4007 answerer_config.rtcp_mux_policy =
4008 PeerConnectionInterface::kRtcpMuxPolicyRequire;
4009 answerer_config.bundle_policy =
4010 PeerConnectionInterface::kBundlePolicyMaxBundle;
4011 answerer_config.use_datagram_transport_for_data_channels = true;
4012 answerer_config.use_datagram_transport_for_data_channels_receive_only = true;
4013
4014 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
4015 offerer_config, answerer_config,
4016 loopback_media_transports()->first_factory(),
4017 loopback_media_transports()->second_factory()));
4018 ConnectFakeSignaling();
4019
4020 caller()->CreateDataChannel();
4021 caller()->CreateAndSetAndSignalOffer();
4022 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4023
4024 // Ensure that the data channel transport is ready.
4025 loopback_media_transports()->SetState(webrtc::MediaTransportState::kWritable);
4026 loopback_media_transports()->FlushAsyncInvokes();
4027
4028 ASSERT_NE(nullptr, caller()->data_channel());
4029 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
4030 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
4031 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
4032
4033 // SCTP transports should not be present, since datagram transport is used.
4034 EXPECT_EQ(caller()->pc()->GetSctpTransport(), nullptr);
4035 EXPECT_EQ(callee()->pc()->GetSctpTransport(), nullptr);
4036
4037 // Ensure data can be sent in both directions.
4038 std::string data = "hello world";
4039 caller()->data_channel()->Send(DataBuffer(data));
4040 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
4041 kDefaultTimeout);
4042 callee()->data_channel()->Send(DataBuffer(data));
4043 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
4044 kDefaultTimeout);
4045}
4046
Bjorn A Mellemb689af42019-08-21 10:44:59 -07004047// This test sets up a call between two parties with a datagram transport data
4048// channel.
4049TEST_P(PeerConnectionIntegrationTest, DatagramTransportDataChannelEndToEnd) {
4050 PeerConnectionInterface::RTCConfiguration rtc_config;
4051 rtc_config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyRequire;
4052 rtc_config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
4053 rtc_config.use_datagram_transport_for_data_channels = true;
4054 rtc_config.enable_dtls_srtp = false;
4055 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
4056 rtc_config, rtc_config, loopback_media_transports()->first_factory(),
4057 loopback_media_transports()->second_factory()));
4058 ConnectFakeSignaling();
4059
4060 // Expect that data channel created on caller side will show up for callee as
4061 // well.
4062 caller()->CreateDataChannel();
4063 caller()->CreateAndSetAndSignalOffer();
4064 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4065
Bjorn A Mellemfc604aa2019-09-24 14:59:21 -07004066 // Ensure that the data channel transport is ready.
Bjorn A Mellemb689af42019-08-21 10:44:59 -07004067 loopback_media_transports()->SetState(webrtc::MediaTransportState::kWritable);
4068 loopback_media_transports()->FlushAsyncInvokes();
4069
4070 // Caller data channel should already exist (it created one). Callee data
4071 // channel may not exist yet, since negotiation happens in-band, not in SDP.
4072 ASSERT_NE(nullptr, caller()->data_channel());
4073 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
4074 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
4075 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
4076
4077 // Ensure data can be sent in both directions.
4078 std::string data = "hello world";
4079 caller()->data_channel()->Send(DataBuffer(data));
4080 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
4081 kDefaultTimeout);
4082 callee()->data_channel()->Send(DataBuffer(data));
4083 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
4084 kDefaultTimeout);
4085}
4086
Bjorn A Mellembc3eebc2019-09-23 14:53:54 -07004087// Tests that 'zero-rtt' data channel transports (which are ready-to-send as
4088// soon as they're created) work correctly.
4089TEST_P(PeerConnectionIntegrationTest, DatagramTransportDataChannelZeroRtt) {
4090 PeerConnectionInterface::RTCConfiguration rtc_config;
4091 rtc_config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyRequire;
4092 rtc_config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
4093 rtc_config.use_datagram_transport_for_data_channels = true;
4094 rtc_config.enable_dtls_srtp = false; // SDES is required for media transport.
4095 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
4096 rtc_config, rtc_config, loopback_media_transports()->first_factory(),
4097 loopback_media_transports()->second_factory()));
4098 ConnectFakeSignaling();
4099
4100 // Ensure that the callee's media transport is ready-to-send immediately.
4101 // Note that only the callee can become writable in zero RTTs. The caller
4102 // must wait for the callee's answer.
4103 loopback_media_transports()->SetSecondStateAfterConnect(
4104 webrtc::MediaTransportState::kWritable);
4105 loopback_media_transports()->FlushAsyncInvokes();
4106
4107 // Expect that data channel created on caller side will show up for callee as
4108 // well.
4109 caller()->CreateDataChannel();
4110 caller()->CreateAndSetAndSignalOffer();
4111 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4112
4113 loopback_media_transports()->SetFirstState(
4114 webrtc::MediaTransportState::kWritable);
4115 loopback_media_transports()->FlushAsyncInvokes();
4116
4117 // Caller data channel should already exist (it created one). Callee data
4118 // channel may not exist yet, since negotiation happens in-band, not in SDP.
4119 ASSERT_NE(nullptr, caller()->data_channel());
4120 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
4121 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
4122 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
4123
4124 // Ensure data can be sent in both directions.
4125 std::string data = "hello world";
4126 caller()->data_channel()->Send(DataBuffer(data));
4127 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
4128 kDefaultTimeout);
4129 callee()->data_channel()->Send(DataBuffer(data));
4130 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
4131 kDefaultTimeout);
4132}
4133
Bjorn A Mellemb689af42019-08-21 10:44:59 -07004134// Ensures that when the callee closes a datagram transport data channel, the
4135// closing procedure results in the data channel being closed for the caller
4136// as well.
4137TEST_P(PeerConnectionIntegrationTest,
4138 DatagramTransportDataChannelCalleeCloses) {
4139 PeerConnectionInterface::RTCConfiguration rtc_config;
4140 rtc_config.use_datagram_transport_for_data_channels = true;
4141 rtc_config.enable_dtls_srtp = false;
4142 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
4143 rtc_config, rtc_config, loopback_media_transports()->first_factory(),
4144 loopback_media_transports()->second_factory()));
4145 ConnectFakeSignaling();
4146
4147 // Create a data channel on the caller and signal it to the callee.
4148 caller()->CreateDataChannel();
4149 caller()->CreateAndSetAndSignalOffer();
4150 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4151
Bjorn A Mellemfc604aa2019-09-24 14:59:21 -07004152 // Ensure that the data channel transport is ready.
Bjorn A Mellemb689af42019-08-21 10:44:59 -07004153 loopback_media_transports()->SetState(webrtc::MediaTransportState::kWritable);
4154 loopback_media_transports()->FlushAsyncInvokes();
4155
4156 // Data channels exist and open on both ends of the connection.
4157 ASSERT_NE(nullptr, caller()->data_channel());
4158 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
4159 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
4160 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
4161
4162 // Close the data channel on the callee side, and wait for it to reach the
4163 // "closed" state on both sides.
4164 callee()->data_channel()->Close();
4165 EXPECT_TRUE_WAIT(!caller()->data_observer()->IsOpen(), kDefaultTimeout);
4166 EXPECT_TRUE_WAIT(!callee()->data_observer()->IsOpen(), kDefaultTimeout);
4167}
4168
4169// Tests that datagram transport data channels can do in-band negotiation.
4170TEST_P(PeerConnectionIntegrationTest,
4171 DatagramTransportDataChannelConfigSentToOtherSide) {
4172 PeerConnectionInterface::RTCConfiguration rtc_config;
4173 rtc_config.use_datagram_transport_for_data_channels = true;
4174 rtc_config.enable_dtls_srtp = false;
4175 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
4176 rtc_config, rtc_config, loopback_media_transports()->first_factory(),
4177 loopback_media_transports()->second_factory()));
4178 ConnectFakeSignaling();
4179
4180 // Create a data channel with a non-default configuration and signal it to the
4181 // callee.
4182 webrtc::DataChannelInit init;
4183 init.id = 53;
4184 init.maxRetransmits = 52;
4185 caller()->CreateDataChannel("data-channel", &init);
4186 caller()->CreateAndSetAndSignalOffer();
4187 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4188
Bjorn A Mellemfc604aa2019-09-24 14:59:21 -07004189 // Ensure that the data channel transport is ready.
Bjorn A Mellemb689af42019-08-21 10:44:59 -07004190 loopback_media_transports()->SetState(webrtc::MediaTransportState::kWritable);
4191 loopback_media_transports()->FlushAsyncInvokes();
4192
4193 // Ensure that the data channel exists on the callee with the correct
4194 // configuration.
4195 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
4196 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
4197 // Since "negotiate" is false, the "id" parameter is ignored.
4198 EXPECT_NE(init.id, callee()->data_channel()->id());
4199 EXPECT_EQ("data-channel", callee()->data_channel()->label());
4200 EXPECT_EQ(init.maxRetransmits, callee()->data_channel()->maxRetransmits());
4201 EXPECT_FALSE(callee()->data_channel()->negotiated());
4202}
4203
4204TEST_P(PeerConnectionIntegrationTest,
4205 DatagramTransportDataChannelRejectedWithNoFallback) {
4206 PeerConnectionInterface::RTCConfiguration offerer_config;
4207 offerer_config.rtcp_mux_policy =
4208 PeerConnectionInterface::kRtcpMuxPolicyRequire;
4209 offerer_config.bundle_policy =
4210 PeerConnectionInterface::kBundlePolicyMaxBundle;
4211 offerer_config.use_datagram_transport_for_data_channels = true;
4212 // Disabling DTLS precludes a fallback to SCTP.
4213 offerer_config.enable_dtls_srtp = false;
4214
4215 PeerConnectionInterface::RTCConfiguration answerer_config;
4216 answerer_config.rtcp_mux_policy =
4217 PeerConnectionInterface::kRtcpMuxPolicyRequire;
4218 answerer_config.bundle_policy =
4219 PeerConnectionInterface::kBundlePolicyMaxBundle;
4220 // Both endpoints must disable DTLS or SetRemoteDescription will fail.
4221 answerer_config.enable_dtls_srtp = false;
4222
4223 // Configure one endpoint to use datagram transport for data channels while
4224 // the other does not.
4225 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
4226 offerer_config, answerer_config,
4227 loopback_media_transports()->first_factory(), nullptr));
4228 ConnectFakeSignaling();
4229
4230 // The caller offers a data channel using either datagram transport or SCTP.
4231 caller()->CreateDataChannel();
4232 caller()->AddAudioVideoTracks();
4233 callee()->AddAudioVideoTracks();
4234 caller()->CreateAndSetAndSignalOffer();
4235 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4236
4237 // Caller data channel should already exist (it created one). Callee data
4238 // channel should not exist, since negotiation happens in-band, not in SDP.
4239 EXPECT_NE(nullptr, caller()->data_channel());
4240 EXPECT_EQ(nullptr, callee()->data_channel());
4241
4242 // The caller's data channel should close when the datagram transport is
4243 // rejected.
4244 EXPECT_FALSE(caller()->data_observer()->IsOpen());
4245
4246 // Media flow should not be impacted by the failed data channel.
4247 MediaExpectations media_expectations;
4248 media_expectations.ExpectBidirectionalAudioAndVideo();
4249 ASSERT_TRUE(ExpectNewFrames(media_expectations));
4250}
4251
Bjorn Mellema2eb0a72018-11-09 10:13:51 -08004252// This test sets up a call between two parties with a media transport data
4253// channel.
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08004254TEST_P(PeerConnectionIntegrationTest, MediaTransportDataChannelEndToEnd) {
4255 PeerConnectionInterface::RTCConfiguration rtc_config;
Piotr (Peter) Slatalab1ae10b2019-03-01 11:14:05 -08004256 rtc_config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyRequire;
4257 rtc_config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08004258 rtc_config.use_media_transport_for_data_channels = true;
4259 rtc_config.enable_dtls_srtp = false; // SDES is required for media transport.
4260 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
4261 rtc_config, rtc_config, loopback_media_transports()->first_factory(),
4262 loopback_media_transports()->second_factory()));
4263 ConnectFakeSignaling();
4264
4265 // Expect that data channel created on caller side will show up for callee as
4266 // well.
4267 caller()->CreateDataChannel();
4268 caller()->CreateAndSetAndSignalOffer();
4269 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4270
4271 // Ensure that the media transport is ready.
4272 loopback_media_transports()->SetState(webrtc::MediaTransportState::kWritable);
4273 loopback_media_transports()->FlushAsyncInvokes();
4274
4275 // Caller data channel should already exist (it created one). Callee data
4276 // channel may not exist yet, since negotiation happens in-band, not in SDP.
4277 ASSERT_NE(nullptr, caller()->data_channel());
4278 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
4279 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
4280 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
4281
4282 // Ensure data can be sent in both directions.
4283 std::string data = "hello world";
4284 caller()->data_channel()->Send(DataBuffer(data));
4285 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
4286 kDefaultTimeout);
4287 callee()->data_channel()->Send(DataBuffer(data));
4288 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
4289 kDefaultTimeout);
4290}
4291
Bjorn A Mellembc3eebc2019-09-23 14:53:54 -07004292// Tests that 'zero-rtt' data channel transports (which are ready-to-send as
4293// soon as they're created) work correctly.
4294TEST_P(PeerConnectionIntegrationTest, MediaTransportDataChannelZeroRtt) {
4295 PeerConnectionInterface::RTCConfiguration rtc_config;
4296 rtc_config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyRequire;
4297 rtc_config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
4298 rtc_config.use_media_transport_for_data_channels = true;
4299 rtc_config.enable_dtls_srtp = false; // SDES is required for media transport.
4300 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
4301 rtc_config, rtc_config, loopback_media_transports()->first_factory(),
4302 loopback_media_transports()->second_factory()));
4303 ConnectFakeSignaling();
4304
4305 // Ensure that the callee's media transport is ready-to-send immediately.
4306 // Note that only the callee can become writable in zero RTTs. The caller
4307 // must wait for the callee's answer.
4308 loopback_media_transports()->SetSecondStateAfterConnect(
4309 webrtc::MediaTransportState::kWritable);
4310 loopback_media_transports()->FlushAsyncInvokes();
4311
4312 // Expect that data channel created on caller side will show up for callee as
4313 // well.
4314 caller()->CreateDataChannel();
4315 caller()->CreateAndSetAndSignalOffer();
4316 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4317
4318 loopback_media_transports()->SetFirstState(
4319 webrtc::MediaTransportState::kWritable);
4320 loopback_media_transports()->FlushAsyncInvokes();
4321
4322 // Caller data channel should already exist (it created one). Callee data
4323 // channel may not exist yet, since negotiation happens in-band, not in SDP.
4324 ASSERT_NE(nullptr, caller()->data_channel());
4325 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
4326 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
4327 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
4328
4329 // Ensure data can be sent in both directions.
4330 std::string data = "hello world";
4331 caller()->data_channel()->Send(DataBuffer(data));
4332 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
4333 kDefaultTimeout);
4334 callee()->data_channel()->Send(DataBuffer(data));
4335 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
4336 kDefaultTimeout);
4337}
4338
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08004339// Ensure that when the callee closes a media transport data channel, the
4340// closing procedure results in the data channel being closed for the caller
4341// as well.
4342TEST_P(PeerConnectionIntegrationTest, MediaTransportDataChannelCalleeCloses) {
4343 PeerConnectionInterface::RTCConfiguration rtc_config;
4344 rtc_config.use_media_transport_for_data_channels = true;
4345 rtc_config.enable_dtls_srtp = false; // SDES is required for media transport.
4346 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
4347 rtc_config, rtc_config, loopback_media_transports()->first_factory(),
4348 loopback_media_transports()->second_factory()));
4349 ConnectFakeSignaling();
4350
4351 // Create a data channel on the caller and signal it to the callee.
4352 caller()->CreateDataChannel();
4353 caller()->CreateAndSetAndSignalOffer();
4354 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4355
4356 // Ensure that the media transport is ready.
4357 loopback_media_transports()->SetState(webrtc::MediaTransportState::kWritable);
4358 loopback_media_transports()->FlushAsyncInvokes();
4359
4360 // Data channels exist and open on both ends of the connection.
4361 ASSERT_NE(nullptr, caller()->data_channel());
4362 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
4363 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
4364 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
4365
4366 // Close the data channel on the callee side, and wait for it to reach the
4367 // "closed" state on both sides.
4368 callee()->data_channel()->Close();
4369 EXPECT_TRUE_WAIT(!caller()->data_observer()->IsOpen(), kDefaultTimeout);
4370 EXPECT_TRUE_WAIT(!callee()->data_observer()->IsOpen(), kDefaultTimeout);
4371}
4372
4373TEST_P(PeerConnectionIntegrationTest,
4374 MediaTransportDataChannelConfigSentToOtherSide) {
4375 PeerConnectionInterface::RTCConfiguration rtc_config;
4376 rtc_config.use_media_transport_for_data_channels = true;
4377 rtc_config.enable_dtls_srtp = false; // SDES is required for media transport.
4378 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
4379 rtc_config, rtc_config, loopback_media_transports()->first_factory(),
4380 loopback_media_transports()->second_factory()));
4381 ConnectFakeSignaling();
4382
4383 // Create a data channel with a non-default configuration and signal it to the
4384 // callee.
4385 webrtc::DataChannelInit init;
4386 init.id = 53;
4387 init.maxRetransmits = 52;
4388 caller()->CreateDataChannel("data-channel", &init);
4389 caller()->CreateAndSetAndSignalOffer();
4390 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4391
4392 // Ensure that the media transport is ready.
4393 loopback_media_transports()->SetState(webrtc::MediaTransportState::kWritable);
4394 loopback_media_transports()->FlushAsyncInvokes();
4395
4396 // Ensure that the data channel exists on the callee with the correct
4397 // configuration.
4398 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
4399 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
Harald Alvestrand5c4d2ee2019-04-01 12:58:15 +02004400 // Since "negotiate" is false, the "id" parameter is ignored.
4401 EXPECT_NE(init.id, callee()->data_channel()->id());
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08004402 EXPECT_EQ("data-channel", callee()->data_channel()->label());
4403 EXPECT_EQ(init.maxRetransmits, callee()->data_channel()->maxRetransmits());
4404 EXPECT_FALSE(callee()->data_channel()->negotiated());
4405}
4406
Piotr (Peter) Slatalab1ae10b2019-03-01 11:14:05 -08004407TEST_P(PeerConnectionIntegrationTest, MediaTransportOfferUpgrade) {
4408 PeerConnectionInterface::RTCConfiguration rtc_config;
4409 rtc_config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyRequire;
4410 rtc_config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
4411 rtc_config.use_media_transport = true;
4412 rtc_config.enable_dtls_srtp = false; // SDES is required for media transport.
4413 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
4414 rtc_config, rtc_config, loopback_media_transports()->first_factory(),
4415 loopback_media_transports()->second_factory()));
4416 ConnectFakeSignaling();
4417
4418 // Do initial offer/answer with just a video track.
4419 caller()->AddVideoTrack();
4420 callee()->AddVideoTrack();
4421 caller()->CreateAndSetAndSignalOffer();
4422 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4423
4424 // Ensure that the media transport is ready.
4425 loopback_media_transports()->SetState(webrtc::MediaTransportState::kWritable);
4426 loopback_media_transports()->FlushAsyncInvokes();
4427
4428 // Now add an audio track and do another offer/answer.
4429 caller()->AddAudioTrack();
4430 callee()->AddAudioTrack();
4431 caller()->CreateAndSetAndSignalOffer();
4432 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4433
4434 // Ensure both audio and video frames are received end-to-end.
4435 MediaExpectations media_expectations;
4436 media_expectations.ExpectBidirectionalAudioAndVideo();
4437 ASSERT_TRUE(ExpectNewFrames(media_expectations));
4438
4439 // The second offer should not have generated another media transport.
4440 // Media transport was kept alive, and was not recreated.
4441 EXPECT_EQ(1, loopback_media_transports()->first_factory_transport_count());
4442 EXPECT_EQ(1, loopback_media_transports()->second_factory_transport_count());
4443}
4444
4445TEST_P(PeerConnectionIntegrationTest, MediaTransportOfferUpgradeOnTheCallee) {
4446 PeerConnectionInterface::RTCConfiguration rtc_config;
4447 rtc_config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyRequire;
4448 rtc_config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
4449 rtc_config.use_media_transport = true;
4450 rtc_config.enable_dtls_srtp = false; // SDES is required for media transport.
4451 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
4452 rtc_config, rtc_config, loopback_media_transports()->first_factory(),
4453 loopback_media_transports()->second_factory()));
4454 ConnectFakeSignaling();
4455
4456 // Do initial offer/answer with just a video track.
4457 caller()->AddVideoTrack();
4458 callee()->AddVideoTrack();
4459 caller()->CreateAndSetAndSignalOffer();
4460 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4461
4462 // Ensure that the media transport is ready.
4463 loopback_media_transports()->SetState(webrtc::MediaTransportState::kWritable);
4464 loopback_media_transports()->FlushAsyncInvokes();
4465
4466 // Now add an audio track and do another offer/answer.
4467 caller()->AddAudioTrack();
4468 callee()->AddAudioTrack();
4469 callee()->CreateAndSetAndSignalOffer();
4470 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4471
4472 // Ensure both audio and video frames are received end-to-end.
4473 MediaExpectations media_expectations;
4474 media_expectations.ExpectBidirectionalAudioAndVideo();
4475 ASSERT_TRUE(ExpectNewFrames(media_expectations));
4476
4477 // The second offer should not have generated another media transport.
4478 // Media transport was kept alive, and was not recreated.
4479 EXPECT_EQ(1, loopback_media_transports()->first_factory_transport_count());
4480 EXPECT_EQ(1, loopback_media_transports()->second_factory_transport_count());
4481}
4482
Niels Möllerc68d2822018-11-20 14:52:05 +01004483TEST_P(PeerConnectionIntegrationTest, MediaTransportBidirectionalAudio) {
4484 PeerConnectionInterface::RTCConfiguration rtc_config;
Piotr (Peter) Slatalab1ae10b2019-03-01 11:14:05 -08004485 rtc_config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyRequire;
4486 rtc_config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
Niels Möllerc68d2822018-11-20 14:52:05 +01004487 rtc_config.use_media_transport = true;
4488 rtc_config.enable_dtls_srtp = false; // SDES is required for media transport.
4489 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
4490 rtc_config, rtc_config, loopback_media_transports()->first_factory(),
4491 loopback_media_transports()->second_factory()));
4492 ConnectFakeSignaling();
4493
4494 caller()->AddAudioTrack();
4495 callee()->AddAudioTrack();
4496 // Start offer/answer exchange and wait for it to complete.
4497 caller()->CreateAndSetAndSignalOffer();
4498 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4499
4500 // Ensure that the media transport is ready.
4501 loopback_media_transports()->SetState(webrtc::MediaTransportState::kWritable);
4502 loopback_media_transports()->FlushAsyncInvokes();
4503
4504 MediaExpectations media_expectations;
4505 media_expectations.ExpectBidirectionalAudio();
4506 ASSERT_TRUE(ExpectNewFrames(media_expectations));
4507
4508 webrtc::MediaTransportPair::Stats first_stats =
4509 loopback_media_transports()->FirstStats();
4510 webrtc::MediaTransportPair::Stats second_stats =
4511 loopback_media_transports()->SecondStats();
4512
4513 EXPECT_GT(first_stats.received_audio_frames, 0);
4514 EXPECT_GE(second_stats.sent_audio_frames, first_stats.received_audio_frames);
4515
4516 EXPECT_GT(second_stats.received_audio_frames, 0);
4517 EXPECT_GE(first_stats.sent_audio_frames, second_stats.received_audio_frames);
4518}
4519
Niels Möller46879152019-01-07 15:54:47 +01004520TEST_P(PeerConnectionIntegrationTest, MediaTransportBidirectionalVideo) {
4521 PeerConnectionInterface::RTCConfiguration rtc_config;
4522 rtc_config.use_media_transport = true;
4523 rtc_config.enable_dtls_srtp = false; // SDES is required for media transport.
4524 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
4525 rtc_config, rtc_config, loopback_media_transports()->first_factory(),
4526 loopback_media_transports()->second_factory()));
4527 ConnectFakeSignaling();
4528
4529 caller()->AddVideoTrack();
4530 callee()->AddVideoTrack();
4531 // Start offer/answer exchange and wait for it to complete.
4532 caller()->CreateAndSetAndSignalOffer();
4533 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4534
4535 // Ensure that the media transport is ready.
4536 loopback_media_transports()->SetState(webrtc::MediaTransportState::kWritable);
4537 loopback_media_transports()->FlushAsyncInvokes();
4538
4539 MediaExpectations media_expectations;
4540 media_expectations.ExpectBidirectionalVideo();
4541 ASSERT_TRUE(ExpectNewFrames(media_expectations));
4542
4543 webrtc::MediaTransportPair::Stats first_stats =
4544 loopback_media_transports()->FirstStats();
4545 webrtc::MediaTransportPair::Stats second_stats =
4546 loopback_media_transports()->SecondStats();
4547
4548 EXPECT_GT(first_stats.received_video_frames, 0);
4549 EXPECT_GE(second_stats.sent_video_frames, first_stats.received_video_frames);
4550
4551 EXPECT_GT(second_stats.received_video_frames, 0);
4552 EXPECT_GE(first_stats.sent_video_frames, second_stats.received_video_frames);
4553}
4554
Piotr (Peter) Slatala55b91b92019-01-25 13:31:15 -08004555TEST_P(PeerConnectionIntegrationTest,
4556 MediaTransportDataChannelUsesRtpBidirectionalVideo) {
4557 PeerConnectionInterface::RTCConfiguration rtc_config;
4558 rtc_config.use_media_transport = false;
4559 rtc_config.use_media_transport_for_data_channels = true;
4560 rtc_config.enable_dtls_srtp = false; // SDES is required for media transport.
4561 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
4562 rtc_config, rtc_config, loopback_media_transports()->first_factory(),
4563 loopback_media_transports()->second_factory()));
4564 ConnectFakeSignaling();
4565
4566 caller()->AddVideoTrack();
4567 callee()->AddVideoTrack();
4568 // Start offer/answer exchange and wait for it to complete.
4569 caller()->CreateAndSetAndSignalOffer();
4570 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4571
4572 MediaExpectations media_expectations;
4573 media_expectations.ExpectBidirectionalVideo();
4574 ASSERT_TRUE(ExpectNewFrames(media_expectations));
4575}
4576
deadbeef1dcb1642017-03-29 21:08:16 -07004577// Test that the ICE connection and gathering states eventually reach
4578// "complete".
Seth Hampson2f0d7022018-02-20 11:54:42 -08004579TEST_P(PeerConnectionIntegrationTest, IceStatesReachCompletion) {
deadbeef1dcb1642017-03-29 21:08:16 -07004580 ASSERT_TRUE(CreatePeerConnectionWrappers());
4581 ConnectFakeSignaling();
4582 // Do normal offer/answer.
Steve Anton15324772018-01-16 10:26:49 -08004583 caller()->AddAudioVideoTracks();
4584 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07004585 caller()->CreateAndSetAndSignalOffer();
4586 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4587 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceGatheringComplete,
4588 caller()->ice_gathering_state(), kMaxWaitForFramesMs);
4589 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceGatheringComplete,
4590 callee()->ice_gathering_state(), kMaxWaitForFramesMs);
4591 // After the best candidate pair is selected and all candidates are signaled,
4592 // the ICE connection state should reach "complete".
4593 // TODO(deadbeef): Currently, the ICE "controlled" agent (the
4594 // answerer/"callee" by default) only reaches "connected". When this is
4595 // fixed, this test should be updated.
4596 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
4597 caller()->ice_connection_state(), kDefaultTimeout);
Alex Loiko9289eda2018-11-23 16:18:59 +00004598 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
4599 callee()->ice_connection_state(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07004600}
4601
Qingsi Wang1dac6d82018-12-12 15:28:47 -08004602constexpr int kOnlyLocalPorts = cricket::PORTALLOCATOR_DISABLE_STUN |
4603 cricket::PORTALLOCATOR_DISABLE_RELAY |
4604 cricket::PORTALLOCATOR_DISABLE_TCP;
Zach Stein6fcdc2f2018-08-23 16:25:55 -07004605
Qingsi Wang1dac6d82018-12-12 15:28:47 -08004606// Use a mock resolver to resolve the hostname back to the original IP on both
4607// sides and check that the ICE connection connects.
Zach Stein6fcdc2f2018-08-23 16:25:55 -07004608TEST_P(PeerConnectionIntegrationTest,
4609 IceStatesReachCompletionWithRemoteHostname) {
Qingsi Wang1dac6d82018-12-12 15:28:47 -08004610 auto caller_resolver_factory =
Mirko Bonadei317a1f02019-09-17 17:06:18 +02004611 std::make_unique<NiceMock<webrtc::MockAsyncResolverFactory>>();
Qingsi Wang1dac6d82018-12-12 15:28:47 -08004612 auto callee_resolver_factory =
Mirko Bonadei317a1f02019-09-17 17:06:18 +02004613 std::make_unique<NiceMock<webrtc::MockAsyncResolverFactory>>();
Qingsi Wang1dac6d82018-12-12 15:28:47 -08004614 NiceMock<rtc::MockAsyncResolver> callee_async_resolver;
4615 NiceMock<rtc::MockAsyncResolver> caller_async_resolver;
Zach Stein6fcdc2f2018-08-23 16:25:55 -07004616
4617 // This also verifies that the injected AsyncResolverFactory is used by
4618 // P2PTransportChannel.
Qingsi Wang1dac6d82018-12-12 15:28:47 -08004619 EXPECT_CALL(*caller_resolver_factory, Create())
4620 .WillOnce(Return(&caller_async_resolver));
4621 webrtc::PeerConnectionDependencies caller_deps(nullptr);
4622 caller_deps.async_resolver_factory = std::move(caller_resolver_factory);
4623
4624 EXPECT_CALL(*callee_resolver_factory, Create())
4625 .WillOnce(Return(&callee_async_resolver));
4626 webrtc::PeerConnectionDependencies callee_deps(nullptr);
4627 callee_deps.async_resolver_factory = std::move(callee_resolver_factory);
4628
4629 PeerConnectionInterface::RTCConfiguration config;
4630 config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
4631 config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyRequire;
4632
4633 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndDeps(
4634 config, std::move(caller_deps), config, std::move(callee_deps)));
4635
4636 caller()->SetRemoteAsyncResolver(&callee_async_resolver);
4637 callee()->SetRemoteAsyncResolver(&caller_async_resolver);
4638
4639 // Enable hostname candidates with mDNS names.
Qingsi Wangecd30542019-05-22 14:34:56 -07004640 caller()->SetMdnsResponder(
Mirko Bonadei317a1f02019-09-17 17:06:18 +02004641 std::make_unique<webrtc::FakeMdnsResponder>(network_thread()));
Qingsi Wangecd30542019-05-22 14:34:56 -07004642 callee()->SetMdnsResponder(
Mirko Bonadei317a1f02019-09-17 17:06:18 +02004643 std::make_unique<webrtc::FakeMdnsResponder>(network_thread()));
Qingsi Wang1dac6d82018-12-12 15:28:47 -08004644
4645 SetPortAllocatorFlags(kOnlyLocalPorts, kOnlyLocalPorts);
Zach Stein6fcdc2f2018-08-23 16:25:55 -07004646
4647 ConnectFakeSignaling();
4648 caller()->AddAudioVideoTracks();
4649 callee()->AddAudioVideoTracks();
4650 caller()->CreateAndSetAndSignalOffer();
4651 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4652 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
4653 caller()->ice_connection_state(), kDefaultTimeout);
4654 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
4655 callee()->ice_connection_state(), kDefaultTimeout);
Jeroen de Borst833979f2018-12-13 08:25:54 -08004656
4657 EXPECT_EQ(1, webrtc::metrics::NumEvents(
4658 "WebRTC.PeerConnection.CandidatePairType_UDP",
4659 webrtc::kIceCandidatePairHostNameHostName));
Zach Stein6fcdc2f2018-08-23 16:25:55 -07004660}
4661
Steve Antonede9ca52017-10-16 13:04:27 -07004662// Test that firewalling the ICE connection causes the clients to identify the
4663// disconnected state and then removing the firewall causes them to reconnect.
4664class PeerConnectionIntegrationIceStatesTest
Seth Hampson2f0d7022018-02-20 11:54:42 -08004665 : public PeerConnectionIntegrationBaseTest,
4666 public ::testing::WithParamInterface<
4667 std::tuple<SdpSemantics, std::tuple<std::string, uint32_t>>> {
Steve Antonede9ca52017-10-16 13:04:27 -07004668 protected:
Seth Hampson2f0d7022018-02-20 11:54:42 -08004669 PeerConnectionIntegrationIceStatesTest()
4670 : PeerConnectionIntegrationBaseTest(std::get<0>(GetParam())) {
4671 port_allocator_flags_ = std::get<1>(std::get<1>(GetParam()));
Steve Antonede9ca52017-10-16 13:04:27 -07004672 }
4673
4674 void StartStunServer(const SocketAddress& server_address) {
4675 stun_server_.reset(
4676 cricket::TestStunServer::Create(network_thread(), server_address));
4677 }
4678
4679 bool TestIPv6() {
4680 return (port_allocator_flags_ & cricket::PORTALLOCATOR_ENABLE_IPV6);
4681 }
4682
4683 void SetPortAllocatorFlags() {
Qingsi Wang1dac6d82018-12-12 15:28:47 -08004684 PeerConnectionIntegrationBaseTest::SetPortAllocatorFlags(
4685 port_allocator_flags_, port_allocator_flags_);
Steve Antonede9ca52017-10-16 13:04:27 -07004686 }
4687
4688 std::vector<SocketAddress> CallerAddresses() {
4689 std::vector<SocketAddress> addresses;
4690 addresses.push_back(SocketAddress("1.1.1.1", 0));
4691 if (TestIPv6()) {
4692 addresses.push_back(SocketAddress("1111:0:a:b:c:d:e:f", 0));
4693 }
4694 return addresses;
4695 }
4696
4697 std::vector<SocketAddress> CalleeAddresses() {
4698 std::vector<SocketAddress> addresses;
4699 addresses.push_back(SocketAddress("2.2.2.2", 0));
4700 if (TestIPv6()) {
4701 addresses.push_back(SocketAddress("2222:0:a:b:c:d:e:f", 0));
4702 }
4703 return addresses;
4704 }
4705
4706 void SetUpNetworkInterfaces() {
4707 // Remove the default interfaces added by the test infrastructure.
Qingsi Wangecd30542019-05-22 14:34:56 -07004708 caller()->network_manager()->RemoveInterface(kDefaultLocalAddress);
4709 callee()->network_manager()->RemoveInterface(kDefaultLocalAddress);
Steve Antonede9ca52017-10-16 13:04:27 -07004710
4711 // Add network addresses for test.
4712 for (const auto& caller_address : CallerAddresses()) {
Qingsi Wangecd30542019-05-22 14:34:56 -07004713 caller()->network_manager()->AddInterface(caller_address);
Steve Antonede9ca52017-10-16 13:04:27 -07004714 }
4715 for (const auto& callee_address : CalleeAddresses()) {
Qingsi Wangecd30542019-05-22 14:34:56 -07004716 callee()->network_manager()->AddInterface(callee_address);
Steve Antonede9ca52017-10-16 13:04:27 -07004717 }
4718 }
4719
4720 private:
4721 uint32_t port_allocator_flags_;
4722 std::unique_ptr<cricket::TestStunServer> stun_server_;
4723};
4724
4725// Tests that the PeerConnection goes through all the ICE gathering/connection
4726// states over the duration of the call. This includes Disconnected and Failed
4727// states, induced by putting a firewall between the peers and waiting for them
4728// to time out.
Steve Anton83119dd2017-11-10 16:19:52 -08004729TEST_P(PeerConnectionIntegrationIceStatesTest, VerifyIceStates) {
Jonas Olssonb75d9e92019-02-22 10:33:29 +01004730 rtc::ScopedFakeClock fake_clock;
4731 // Some things use a time of "0" as a special value, so we need to start out
4732 // the fake clock at a nonzero time.
4733 fake_clock.AdvanceTime(TimeDelta::seconds(1));
Steve Antonede9ca52017-10-16 13:04:27 -07004734
4735 const SocketAddress kStunServerAddress =
4736 SocketAddress("99.99.99.1", cricket::STUN_SERVER_PORT);
4737 StartStunServer(kStunServerAddress);
4738
4739 PeerConnectionInterface::RTCConfiguration config;
4740 PeerConnectionInterface::IceServer ice_stun_server;
4741 ice_stun_server.urls.push_back(
4742 "stun:" + kStunServerAddress.HostAsURIString() + ":" +
4743 kStunServerAddress.PortAsString());
4744 config.servers.push_back(ice_stun_server);
4745
4746 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
4747 ConnectFakeSignaling();
4748 SetPortAllocatorFlags();
4749 SetUpNetworkInterfaces();
Steve Anton15324772018-01-16 10:26:49 -08004750 caller()->AddAudioVideoTracks();
4751 callee()->AddAudioVideoTracks();
Steve Antonede9ca52017-10-16 13:04:27 -07004752
4753 // Initial state before anything happens.
4754 ASSERT_EQ(PeerConnectionInterface::kIceGatheringNew,
4755 caller()->ice_gathering_state());
4756 ASSERT_EQ(PeerConnectionInterface::kIceConnectionNew,
4757 caller()->ice_connection_state());
Jonas Olsson7a6739e2019-01-15 16:31:55 +01004758 ASSERT_EQ(PeerConnectionInterface::kIceConnectionNew,
4759 caller()->standardized_ice_connection_state());
Steve Antonede9ca52017-10-16 13:04:27 -07004760
4761 // Start the call by creating the offer, setting it as the local description,
4762 // then sending it to the peer who will respond with an answer. This happens
4763 // asynchronously so that we can watch the states as it runs in the
4764 // background.
4765 caller()->CreateAndSetAndSignalOffer();
4766
Jonas Olsson7a6739e2019-01-15 16:31:55 +01004767 ASSERT_EQ(PeerConnectionInterface::kIceConnectionCompleted,
4768 caller()->ice_connection_state());
Jonas Olssonacd8ae72019-02-25 15:26:24 +01004769 ASSERT_EQ(PeerConnectionInterface::kIceConnectionCompleted,
Jonas Olsson7a6739e2019-01-15 16:31:55 +01004770 caller()->standardized_ice_connection_state());
Steve Antonede9ca52017-10-16 13:04:27 -07004771
4772 // Verify that the observer was notified of the intermediate transitions.
4773 EXPECT_THAT(caller()->ice_connection_state_history(),
4774 ElementsAre(PeerConnectionInterface::kIceConnectionChecking,
4775 PeerConnectionInterface::kIceConnectionConnected,
4776 PeerConnectionInterface::kIceConnectionCompleted));
Jonas Olssonacd8ae72019-02-25 15:26:24 +01004777 EXPECT_THAT(caller()->standardized_ice_connection_state_history(),
4778 ElementsAre(PeerConnectionInterface::kIceConnectionChecking,
4779 PeerConnectionInterface::kIceConnectionConnected,
4780 PeerConnectionInterface::kIceConnectionCompleted));
Jonas Olsson635474e2018-10-18 15:58:17 +02004781 EXPECT_THAT(
4782 caller()->peer_connection_state_history(),
4783 ElementsAre(PeerConnectionInterface::PeerConnectionState::kConnecting,
Jonas Olsson635474e2018-10-18 15:58:17 +02004784 PeerConnectionInterface::PeerConnectionState::kConnected));
Steve Antonede9ca52017-10-16 13:04:27 -07004785 EXPECT_THAT(caller()->ice_gathering_state_history(),
4786 ElementsAre(PeerConnectionInterface::kIceGatheringGathering,
4787 PeerConnectionInterface::kIceGatheringComplete));
4788
4789 // Block connections to/from the caller and wait for ICE to become
4790 // disconnected.
4791 for (const auto& caller_address : CallerAddresses()) {
4792 firewall()->AddRule(false, rtc::FP_ANY, rtc::FD_ANY, caller_address);
4793 }
Mirko Bonadei675513b2017-11-09 11:09:25 +01004794 RTC_LOG(LS_INFO) << "Firewall rules applied";
Jonas Olssonb75d9e92019-02-22 10:33:29 +01004795 ASSERT_EQ_SIMULATED_WAIT(PeerConnectionInterface::kIceConnectionDisconnected,
4796 caller()->ice_connection_state(), kDefaultTimeout,
4797 fake_clock);
4798 ASSERT_EQ_SIMULATED_WAIT(PeerConnectionInterface::kIceConnectionDisconnected,
4799 caller()->standardized_ice_connection_state(),
4800 kDefaultTimeout, fake_clock);
Steve Antonede9ca52017-10-16 13:04:27 -07004801
4802 // Let ICE re-establish by removing the firewall rules.
4803 firewall()->ClearRules();
Mirko Bonadei675513b2017-11-09 11:09:25 +01004804 RTC_LOG(LS_INFO) << "Firewall rules cleared";
Jonas Olssonb75d9e92019-02-22 10:33:29 +01004805 ASSERT_EQ_SIMULATED_WAIT(PeerConnectionInterface::kIceConnectionCompleted,
4806 caller()->ice_connection_state(), kDefaultTimeout,
4807 fake_clock);
Jonas Olssonacd8ae72019-02-25 15:26:24 +01004808 ASSERT_EQ_SIMULATED_WAIT(PeerConnectionInterface::kIceConnectionCompleted,
Jonas Olssonb75d9e92019-02-22 10:33:29 +01004809 caller()->standardized_ice_connection_state(),
4810 kDefaultTimeout, fake_clock);
Steve Antonede9ca52017-10-16 13:04:27 -07004811
4812 // According to RFC7675, if there is no response within 30 seconds then the
4813 // peer should consider the other side to have rejected the connection. This
Steve Anton83119dd2017-11-10 16:19:52 -08004814 // is signaled by the state transitioning to "failed".
Steve Antonede9ca52017-10-16 13:04:27 -07004815 constexpr int kConsentTimeout = 30000;
4816 for (const auto& caller_address : CallerAddresses()) {
4817 firewall()->AddRule(false, rtc::FP_ANY, rtc::FD_ANY, caller_address);
4818 }
Mirko Bonadei675513b2017-11-09 11:09:25 +01004819 RTC_LOG(LS_INFO) << "Firewall rules applied again";
Jonas Olssonb75d9e92019-02-22 10:33:29 +01004820 ASSERT_EQ_SIMULATED_WAIT(PeerConnectionInterface::kIceConnectionFailed,
4821 caller()->ice_connection_state(), kConsentTimeout,
4822 fake_clock);
4823 ASSERT_EQ_SIMULATED_WAIT(PeerConnectionInterface::kIceConnectionFailed,
4824 caller()->standardized_ice_connection_state(),
4825 kConsentTimeout, fake_clock);
4826
4827 // We need to manually close the peerconnections before the fake clock goes
4828 // out of scope, or we trigger a DCHECK in rtp_sender.cc when we briefly
4829 // return to using non-faked time.
4830 delete SetCallerPcWrapperAndReturnCurrent(nullptr);
4831 delete SetCalleePcWrapperAndReturnCurrent(nullptr);
4832}
4833
4834// Tests that if the connection doesn't get set up properly we eventually reach
4835// the "failed" iceConnectionState.
4836TEST_P(PeerConnectionIntegrationIceStatesTest, IceStateSetupFailure) {
4837 rtc::ScopedFakeClock fake_clock;
4838 // Some things use a time of "0" as a special value, so we need to start out
4839 // the fake clock at a nonzero time.
4840 fake_clock.AdvanceTime(TimeDelta::seconds(1));
4841
4842 // Block connections to/from the caller and wait for ICE to become
4843 // disconnected.
4844 for (const auto& caller_address : CallerAddresses()) {
4845 firewall()->AddRule(false, rtc::FP_ANY, rtc::FD_ANY, caller_address);
4846 }
4847
4848 ASSERT_TRUE(CreatePeerConnectionWrappers());
4849 ConnectFakeSignaling();
4850 SetPortAllocatorFlags();
4851 SetUpNetworkInterfaces();
4852 caller()->AddAudioVideoTracks();
4853 caller()->CreateAndSetAndSignalOffer();
4854
4855 // According to RFC7675, if there is no response within 30 seconds then the
4856 // peer should consider the other side to have rejected the connection. This
4857 // is signaled by the state transitioning to "failed".
4858 constexpr int kConsentTimeout = 30000;
4859 ASSERT_EQ_SIMULATED_WAIT(PeerConnectionInterface::kIceConnectionFailed,
4860 caller()->standardized_ice_connection_state(),
4861 kConsentTimeout, fake_clock);
4862
4863 // We need to manually close the peerconnections before the fake clock goes
4864 // out of scope, or we trigger a DCHECK in rtp_sender.cc when we briefly
4865 // return to using non-faked time.
4866 delete SetCallerPcWrapperAndReturnCurrent(nullptr);
4867 delete SetCalleePcWrapperAndReturnCurrent(nullptr);
Steve Antonede9ca52017-10-16 13:04:27 -07004868}
4869
4870// Tests that the best connection is set to the appropriate IPv4/IPv6 connection
4871// and that the statistics in the metric observers are updated correctly.
4872TEST_P(PeerConnectionIntegrationIceStatesTest, VerifyBestConnection) {
4873 ASSERT_TRUE(CreatePeerConnectionWrappers());
4874 ConnectFakeSignaling();
4875 SetPortAllocatorFlags();
4876 SetUpNetworkInterfaces();
Steve Anton15324772018-01-16 10:26:49 -08004877 caller()->AddAudioVideoTracks();
4878 callee()->AddAudioVideoTracks();
Steve Antonede9ca52017-10-16 13:04:27 -07004879 caller()->CreateAndSetAndSignalOffer();
4880
4881 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4882
Qingsi Wang7fc821d2018-07-12 12:54:53 -07004883 // TODO(bugs.webrtc.org/9456): Fix it.
4884 const int num_best_ipv4 = webrtc::metrics::NumEvents(
4885 "WebRTC.PeerConnection.IPMetrics", webrtc::kBestConnections_IPv4);
4886 const int num_best_ipv6 = webrtc::metrics::NumEvents(
4887 "WebRTC.PeerConnection.IPMetrics", webrtc::kBestConnections_IPv6);
Steve Antonede9ca52017-10-16 13:04:27 -07004888 if (TestIPv6()) {
4889 // When IPv6 is enabled, we should prefer an IPv6 connection over an IPv4
4890 // connection.
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02004891 EXPECT_EQ(0, num_best_ipv4);
4892 EXPECT_EQ(1, num_best_ipv6);
Steve Antonede9ca52017-10-16 13:04:27 -07004893 } else {
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02004894 EXPECT_EQ(1, num_best_ipv4);
4895 EXPECT_EQ(0, num_best_ipv6);
Steve Antonede9ca52017-10-16 13:04:27 -07004896 }
4897
Qingsi Wang7fc821d2018-07-12 12:54:53 -07004898 EXPECT_EQ(0, webrtc::metrics::NumEvents(
4899 "WebRTC.PeerConnection.CandidatePairType_UDP",
4900 webrtc::kIceCandidatePairHostHost));
4901 EXPECT_EQ(1, webrtc::metrics::NumEvents(
4902 "WebRTC.PeerConnection.CandidatePairType_UDP",
4903 webrtc::kIceCandidatePairHostPublicHostPublic));
Steve Antonede9ca52017-10-16 13:04:27 -07004904}
4905
4906constexpr uint32_t kFlagsIPv4NoStun = cricket::PORTALLOCATOR_DISABLE_TCP |
4907 cricket::PORTALLOCATOR_DISABLE_STUN |
4908 cricket::PORTALLOCATOR_DISABLE_RELAY;
4909constexpr uint32_t kFlagsIPv6NoStun =
4910 cricket::PORTALLOCATOR_DISABLE_TCP | cricket::PORTALLOCATOR_DISABLE_STUN |
4911 cricket::PORTALLOCATOR_ENABLE_IPV6 | cricket::PORTALLOCATOR_DISABLE_RELAY;
4912constexpr uint32_t kFlagsIPv4Stun =
4913 cricket::PORTALLOCATOR_DISABLE_TCP | cricket::PORTALLOCATOR_DISABLE_RELAY;
4914
Mirko Bonadeic84f6612019-01-31 12:20:57 +01004915INSTANTIATE_TEST_SUITE_P(
Seth Hampson2f0d7022018-02-20 11:54:42 -08004916 PeerConnectionIntegrationTest,
4917 PeerConnectionIntegrationIceStatesTest,
4918 Combine(Values(SdpSemantics::kPlanB, SdpSemantics::kUnifiedPlan),
4919 Values(std::make_pair("IPv4 no STUN", kFlagsIPv4NoStun),
4920 std::make_pair("IPv6 no STUN", kFlagsIPv6NoStun),
4921 std::make_pair("IPv4 with STUN", kFlagsIPv4Stun))));
Steve Antonede9ca52017-10-16 13:04:27 -07004922
deadbeef1dcb1642017-03-29 21:08:16 -07004923// This test sets up a call between two parties with audio and video.
4924// During the call, the caller restarts ICE and the test verifies that
4925// new ICE candidates are generated and audio and video still can flow, and the
4926// ICE state reaches completed again.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004927TEST_P(PeerConnectionIntegrationTest, MediaContinuesFlowingAfterIceRestart) {
deadbeef1dcb1642017-03-29 21:08:16 -07004928 ASSERT_TRUE(CreatePeerConnectionWrappers());
4929 ConnectFakeSignaling();
4930 // Do normal offer/answer and wait for ICE to complete.
Steve Anton15324772018-01-16 10:26:49 -08004931 caller()->AddAudioVideoTracks();
4932 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07004933 caller()->CreateAndSetAndSignalOffer();
4934 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4935 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
4936 caller()->ice_connection_state(), kMaxWaitForFramesMs);
Alex Loiko9289eda2018-11-23 16:18:59 +00004937 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
4938 callee()->ice_connection_state(), kMaxWaitForFramesMs);
deadbeef1dcb1642017-03-29 21:08:16 -07004939
4940 // To verify that the ICE restart actually occurs, get
4941 // ufrag/password/candidates before and after restart.
4942 // Create an SDP string of the first audio candidate for both clients.
4943 const webrtc::IceCandidateCollection* audio_candidates_caller =
4944 caller()->pc()->local_description()->candidates(0);
4945 const webrtc::IceCandidateCollection* audio_candidates_callee =
4946 callee()->pc()->local_description()->candidates(0);
4947 ASSERT_GT(audio_candidates_caller->count(), 0u);
4948 ASSERT_GT(audio_candidates_callee->count(), 0u);
4949 std::string caller_candidate_pre_restart;
4950 ASSERT_TRUE(
4951 audio_candidates_caller->at(0)->ToString(&caller_candidate_pre_restart));
4952 std::string callee_candidate_pre_restart;
4953 ASSERT_TRUE(
4954 audio_candidates_callee->at(0)->ToString(&callee_candidate_pre_restart));
4955 const cricket::SessionDescription* desc =
4956 caller()->pc()->local_description()->description();
4957 std::string caller_ufrag_pre_restart =
4958 desc->transport_infos()[0].description.ice_ufrag;
4959 desc = callee()->pc()->local_description()->description();
4960 std::string callee_ufrag_pre_restart =
4961 desc->transport_infos()[0].description.ice_ufrag;
4962
Alex Drake00c7ecf2019-08-06 10:54:47 -07004963 EXPECT_EQ(caller()->ice_candidate_pair_change_history().size(), 1u);
deadbeef1dcb1642017-03-29 21:08:16 -07004964 // Have the caller initiate an ICE restart.
4965 caller()->SetOfferAnswerOptions(IceRestartOfferAnswerOptions());
4966 caller()->CreateAndSetAndSignalOffer();
4967 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4968 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
4969 caller()->ice_connection_state(), kMaxWaitForFramesMs);
Alex Loiko9289eda2018-11-23 16:18:59 +00004970 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
deadbeef1dcb1642017-03-29 21:08:16 -07004971 callee()->ice_connection_state(), kMaxWaitForFramesMs);
4972
4973 // Grab the ufrags/candidates again.
4974 audio_candidates_caller = caller()->pc()->local_description()->candidates(0);
4975 audio_candidates_callee = callee()->pc()->local_description()->candidates(0);
4976 ASSERT_GT(audio_candidates_caller->count(), 0u);
4977 ASSERT_GT(audio_candidates_callee->count(), 0u);
4978 std::string caller_candidate_post_restart;
4979 ASSERT_TRUE(
4980 audio_candidates_caller->at(0)->ToString(&caller_candidate_post_restart));
4981 std::string callee_candidate_post_restart;
4982 ASSERT_TRUE(
4983 audio_candidates_callee->at(0)->ToString(&callee_candidate_post_restart));
4984 desc = caller()->pc()->local_description()->description();
4985 std::string caller_ufrag_post_restart =
4986 desc->transport_infos()[0].description.ice_ufrag;
4987 desc = callee()->pc()->local_description()->description();
4988 std::string callee_ufrag_post_restart =
4989 desc->transport_infos()[0].description.ice_ufrag;
4990 // Sanity check that an ICE restart was actually negotiated in SDP.
4991 ASSERT_NE(caller_candidate_pre_restart, caller_candidate_post_restart);
4992 ASSERT_NE(callee_candidate_pre_restart, callee_candidate_post_restart);
4993 ASSERT_NE(caller_ufrag_pre_restart, caller_ufrag_post_restart);
4994 ASSERT_NE(callee_ufrag_pre_restart, callee_ufrag_post_restart);
Alex Drake00c7ecf2019-08-06 10:54:47 -07004995 EXPECT_GT(caller()->ice_candidate_pair_change_history().size(), 1u);
deadbeef1dcb1642017-03-29 21:08:16 -07004996
4997 // Ensure that additional frames are received after the ICE restart.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004998 MediaExpectations media_expectations;
4999 media_expectations.ExpectBidirectionalAudioAndVideo();
5000 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07005001}
5002
5003// Verify that audio/video can be received end-to-end when ICE renomination is
5004// enabled.
Seth Hampson2f0d7022018-02-20 11:54:42 -08005005TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithIceRenomination) {
deadbeef1dcb1642017-03-29 21:08:16 -07005006 PeerConnectionInterface::RTCConfiguration config;
5007 config.enable_ice_renomination = true;
5008 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
5009 ConnectFakeSignaling();
5010 // Do normal offer/answer and wait for some frames to be received in each
5011 // direction.
Steve Anton15324772018-01-16 10:26:49 -08005012 caller()->AddAudioVideoTracks();
5013 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07005014 caller()->CreateAndSetAndSignalOffer();
5015 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5016 // Sanity check that ICE renomination was actually negotiated.
5017 const cricket::SessionDescription* desc =
5018 caller()->pc()->local_description()->description();
5019 for (const cricket::TransportInfo& info : desc->transport_infos()) {
Steve Anton64b626b2019-01-28 17:25:26 -08005020 ASSERT_THAT(info.description.transport_options, Contains("renomination"));
deadbeef1dcb1642017-03-29 21:08:16 -07005021 }
5022 desc = callee()->pc()->local_description()->description();
5023 for (const cricket::TransportInfo& info : desc->transport_infos()) {
Steve Anton64b626b2019-01-28 17:25:26 -08005024 ASSERT_THAT(info.description.transport_options, Contains("renomination"));
deadbeef1dcb1642017-03-29 21:08:16 -07005025 }
Seth Hampson2f0d7022018-02-20 11:54:42 -08005026 MediaExpectations media_expectations;
5027 media_expectations.ExpectBidirectionalAudioAndVideo();
5028 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07005029}
5030
Steve Anton6f25b092017-10-23 09:39:20 -07005031// With a max bundle policy and RTCP muxing, adding a new media description to
5032// the connection should not affect ICE at all because the new media will use
5033// the existing connection.
Seth Hampson2f0d7022018-02-20 11:54:42 -08005034TEST_P(PeerConnectionIntegrationTest,
Steve Anton83119dd2017-11-10 16:19:52 -08005035 AddMediaToConnectedBundleDoesNotRestartIce) {
Steve Anton6f25b092017-10-23 09:39:20 -07005036 PeerConnectionInterface::RTCConfiguration config;
5037 config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
5038 config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyRequire;
5039 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(
5040 config, PeerConnectionInterface::RTCConfiguration()));
5041 ConnectFakeSignaling();
5042
Steve Anton15324772018-01-16 10:26:49 -08005043 caller()->AddAudioTrack();
Steve Anton6f25b092017-10-23 09:39:20 -07005044 caller()->CreateAndSetAndSignalOffer();
5045 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Steve Antonff52f1b2017-10-26 12:24:50 -07005046 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceConnectionCompleted,
5047 caller()->ice_connection_state(), kDefaultTimeout);
Steve Anton6f25b092017-10-23 09:39:20 -07005048
5049 caller()->clear_ice_connection_state_history();
5050
Steve Anton15324772018-01-16 10:26:49 -08005051 caller()->AddVideoTrack();
Steve Anton6f25b092017-10-23 09:39:20 -07005052 caller()->CreateAndSetAndSignalOffer();
5053 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5054
5055 EXPECT_EQ(0u, caller()->ice_connection_state_history().size());
5056}
5057
deadbeef1dcb1642017-03-29 21:08:16 -07005058// This test sets up a call between two parties with audio and video. It then
5059// renegotiates setting the video m-line to "port 0", then later renegotiates
5060// again, enabling video.
Seth Hampson2f0d7022018-02-20 11:54:42 -08005061TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07005062 VideoFlowsAfterMediaSectionIsRejectedAndRecycled) {
5063 ASSERT_TRUE(CreatePeerConnectionWrappers());
5064 ConnectFakeSignaling();
5065
5066 // Do initial negotiation, only sending media from the caller. Will result in
5067 // video and audio recvonly "m=" sections.
Steve Anton15324772018-01-16 10:26:49 -08005068 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07005069 caller()->CreateAndSetAndSignalOffer();
5070 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5071
5072 // Negotiate again, disabling the video "m=" section (the callee will set the
5073 // port to 0 due to offer_to_receive_video = 0).
Seth Hampson2f0d7022018-02-20 11:54:42 -08005074 if (sdp_semantics_ == SdpSemantics::kPlanB) {
5075 PeerConnectionInterface::RTCOfferAnswerOptions options;
5076 options.offer_to_receive_video = 0;
5077 callee()->SetOfferAnswerOptions(options);
5078 } else {
5079 callee()->SetRemoteOfferHandler([this] {
5080 callee()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)->Stop();
5081 });
5082 }
deadbeef1dcb1642017-03-29 21:08:16 -07005083 caller()->CreateAndSetAndSignalOffer();
5084 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5085 // Sanity check that video "m=" section was actually rejected.
5086 const ContentInfo* answer_video_content = cricket::GetFirstVideoContent(
5087 callee()->pc()->local_description()->description());
5088 ASSERT_NE(nullptr, answer_video_content);
5089 ASSERT_TRUE(answer_video_content->rejected);
5090
5091 // Enable video and do negotiation again, making sure video is received
5092 // end-to-end, also adding media stream to callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08005093 if (sdp_semantics_ == SdpSemantics::kPlanB) {
5094 PeerConnectionInterface::RTCOfferAnswerOptions options;
5095 options.offer_to_receive_video = 1;
5096 callee()->SetOfferAnswerOptions(options);
5097 } else {
5098 // The caller's transceiver is stopped, so we need to add another track.
5099 auto caller_transceiver =
5100 caller()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO);
5101 EXPECT_TRUE(caller_transceiver->stopped());
5102 caller()->AddVideoTrack();
5103 }
5104 callee()->AddVideoTrack();
5105 callee()->SetRemoteOfferHandler(nullptr);
deadbeef1dcb1642017-03-29 21:08:16 -07005106 caller()->CreateAndSetAndSignalOffer();
5107 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08005108
deadbeef1dcb1642017-03-29 21:08:16 -07005109 // Verify the caller receives frames from the newly added stream, and the
5110 // callee receives additional frames from the re-enabled video m= section.
Seth Hampson2f0d7022018-02-20 11:54:42 -08005111 MediaExpectations media_expectations;
5112 media_expectations.CalleeExpectsSomeAudio();
5113 media_expectations.ExpectBidirectionalVideo();
5114 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07005115}
5116
deadbeef1dcb1642017-03-29 21:08:16 -07005117// This tests that if we negotiate after calling CreateSender but before we
5118// have a track, then set a track later, frames from the newly-set track are
5119// received end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08005120TEST_F(PeerConnectionIntegrationTestPlanB,
deadbeef1dcb1642017-03-29 21:08:16 -07005121 MediaFlowsAfterEarlyWarmupWithCreateSender) {
5122 ASSERT_TRUE(CreatePeerConnectionWrappers());
5123 ConnectFakeSignaling();
5124 auto caller_audio_sender =
5125 caller()->pc()->CreateSender("audio", "caller_stream");
5126 auto caller_video_sender =
5127 caller()->pc()->CreateSender("video", "caller_stream");
5128 auto callee_audio_sender =
5129 callee()->pc()->CreateSender("audio", "callee_stream");
5130 auto callee_video_sender =
5131 callee()->pc()->CreateSender("video", "callee_stream");
5132 caller()->CreateAndSetAndSignalOffer();
5133 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
5134 // Wait for ICE to complete, without any tracks being set.
5135 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
5136 caller()->ice_connection_state(), kMaxWaitForFramesMs);
5137 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
5138 callee()->ice_connection_state(), kMaxWaitForFramesMs);
5139 // Now set the tracks, and expect frames to immediately start flowing.
5140 EXPECT_TRUE(caller_audio_sender->SetTrack(caller()->CreateLocalAudioTrack()));
5141 EXPECT_TRUE(caller_video_sender->SetTrack(caller()->CreateLocalVideoTrack()));
5142 EXPECT_TRUE(callee_audio_sender->SetTrack(callee()->CreateLocalAudioTrack()));
5143 EXPECT_TRUE(callee_video_sender->SetTrack(callee()->CreateLocalVideoTrack()));
Seth Hampson2f0d7022018-02-20 11:54:42 -08005144 MediaExpectations media_expectations;
5145 media_expectations.ExpectBidirectionalAudioAndVideo();
5146 ASSERT_TRUE(ExpectNewFrames(media_expectations));
5147}
5148
5149// This tests that if we negotiate after calling AddTransceiver but before we
5150// have a track, then set a track later, frames from the newly-set tracks are
5151// received end-to-end.
5152TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
5153 MediaFlowsAfterEarlyWarmupWithAddTransceiver) {
5154 ASSERT_TRUE(CreatePeerConnectionWrappers());
5155 ConnectFakeSignaling();
5156 auto audio_result = caller()->pc()->AddTransceiver(cricket::MEDIA_TYPE_AUDIO);
5157 ASSERT_EQ(RTCErrorType::NONE, audio_result.error().type());
5158 auto caller_audio_sender = audio_result.MoveValue()->sender();
5159 auto video_result = caller()->pc()->AddTransceiver(cricket::MEDIA_TYPE_VIDEO);
5160 ASSERT_EQ(RTCErrorType::NONE, video_result.error().type());
5161 auto caller_video_sender = video_result.MoveValue()->sender();
5162 callee()->SetRemoteOfferHandler([this] {
5163 ASSERT_EQ(2u, callee()->pc()->GetTransceivers().size());
5164 callee()->pc()->GetTransceivers()[0]->SetDirection(
5165 RtpTransceiverDirection::kSendRecv);
5166 callee()->pc()->GetTransceivers()[1]->SetDirection(
5167 RtpTransceiverDirection::kSendRecv);
5168 });
5169 caller()->CreateAndSetAndSignalOffer();
5170 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
5171 // Wait for ICE to complete, without any tracks being set.
5172 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
5173 caller()->ice_connection_state(), kMaxWaitForFramesMs);
5174 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
5175 callee()->ice_connection_state(), kMaxWaitForFramesMs);
5176 // Now set the tracks, and expect frames to immediately start flowing.
5177 auto callee_audio_sender = callee()->pc()->GetSenders()[0];
5178 auto callee_video_sender = callee()->pc()->GetSenders()[1];
5179 ASSERT_TRUE(caller_audio_sender->SetTrack(caller()->CreateLocalAudioTrack()));
5180 ASSERT_TRUE(caller_video_sender->SetTrack(caller()->CreateLocalVideoTrack()));
5181 ASSERT_TRUE(callee_audio_sender->SetTrack(callee()->CreateLocalAudioTrack()));
5182 ASSERT_TRUE(callee_video_sender->SetTrack(callee()->CreateLocalVideoTrack()));
5183 MediaExpectations media_expectations;
5184 media_expectations.ExpectBidirectionalAudioAndVideo();
5185 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07005186}
5187
5188// This test verifies that a remote video track can be added via AddStream,
5189// and sent end-to-end. For this particular test, it's simply echoed back
5190// from the caller to the callee, rather than being forwarded to a third
5191// PeerConnection.
Seth Hampson2f0d7022018-02-20 11:54:42 -08005192TEST_F(PeerConnectionIntegrationTestPlanB, CanSendRemoteVideoTrack) {
deadbeef1dcb1642017-03-29 21:08:16 -07005193 ASSERT_TRUE(CreatePeerConnectionWrappers());
5194 ConnectFakeSignaling();
5195 // Just send a video track from the caller.
Steve Anton15324772018-01-16 10:26:49 -08005196 caller()->AddVideoTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07005197 caller()->CreateAndSetAndSignalOffer();
5198 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02005199 ASSERT_EQ(1U, callee()->remote_streams()->count());
deadbeef1dcb1642017-03-29 21:08:16 -07005200
5201 // Echo the stream back, and do a new offer/anwer (initiated by callee this
5202 // time).
5203 callee()->pc()->AddStream(callee()->remote_streams()->at(0));
5204 callee()->CreateAndSetAndSignalOffer();
5205 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
5206
Seth Hampson2f0d7022018-02-20 11:54:42 -08005207 MediaExpectations media_expectations;
5208 media_expectations.ExpectBidirectionalVideo();
5209 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07005210}
5211
5212// Test that we achieve the expected end-to-end connection time, using a
5213// fake clock and simulated latency on the media and signaling paths.
5214// We use a TURN<->TURN connection because this is usually the quickest to
5215// set up initially, especially when we're confident the connection will work
5216// and can start sending media before we get a STUN response.
5217//
5218// With various optimizations enabled, here are the network delays we expect to
5219// be on the critical path:
5220// 1. 2 signaling trips: Signaling offer and offerer's TURN candidate, then
5221// signaling answer (with DTLS fingerprint).
5222// 2. 9 media hops: Rest of the DTLS handshake. 3 hops in each direction when
5223// using TURN<->TURN pair, and DTLS exchange is 4 packets,
5224// the first of which should have arrived before the answer.
Seth Hampson2f0d7022018-02-20 11:54:42 -08005225TEST_P(PeerConnectionIntegrationTest, EndToEndConnectionTimeWithTurnTurnPair) {
deadbeef1dcb1642017-03-29 21:08:16 -07005226 rtc::ScopedFakeClock fake_clock;
5227 // Some things use a time of "0" as a special value, so we need to start out
5228 // the fake clock at a nonzero time.
5229 // TODO(deadbeef): Fix this.
Sebastian Jansson5f83cf02018-05-08 14:52:22 +02005230 fake_clock.AdvanceTime(webrtc::TimeDelta::seconds(1));
deadbeef1dcb1642017-03-29 21:08:16 -07005231
5232 static constexpr int media_hop_delay_ms = 50;
5233 static constexpr int signaling_trip_delay_ms = 500;
5234 // For explanation of these values, see comment above.
5235 static constexpr int required_media_hops = 9;
5236 static constexpr int required_signaling_trips = 2;
5237 // For internal delays (such as posting an event asychronously).
5238 static constexpr int allowed_internal_delay_ms = 20;
5239 static constexpr int total_connection_time_ms =
5240 media_hop_delay_ms * required_media_hops +
5241 signaling_trip_delay_ms * required_signaling_trips +
5242 allowed_internal_delay_ms;
5243
5244 static const rtc::SocketAddress turn_server_1_internal_address{"88.88.88.0",
5245 3478};
5246 static const rtc::SocketAddress turn_server_1_external_address{"88.88.88.1",
5247 0};
5248 static const rtc::SocketAddress turn_server_2_internal_address{"99.99.99.0",
5249 3478};
5250 static const rtc::SocketAddress turn_server_2_external_address{"99.99.99.1",
5251 0};
Seth Hampsonaed71642018-06-11 07:41:32 -07005252 cricket::TestTurnServer* turn_server_1 = CreateTurnServer(
5253 turn_server_1_internal_address, turn_server_1_external_address);
Jonas Orelandbdcee282017-10-10 14:01:40 +02005254
Seth Hampsonaed71642018-06-11 07:41:32 -07005255 cricket::TestTurnServer* turn_server_2 = CreateTurnServer(
5256 turn_server_2_internal_address, turn_server_2_external_address);
deadbeef1dcb1642017-03-29 21:08:16 -07005257 // Bypass permission check on received packets so media can be sent before
5258 // the candidate is signaled.
Seth Hampsonaed71642018-06-11 07:41:32 -07005259 network_thread()->Invoke<void>(RTC_FROM_HERE, [turn_server_1] {
5260 turn_server_1->set_enable_permission_checks(false);
5261 });
5262 network_thread()->Invoke<void>(RTC_FROM_HERE, [turn_server_2] {
5263 turn_server_2->set_enable_permission_checks(false);
5264 });
deadbeef1dcb1642017-03-29 21:08:16 -07005265
5266 PeerConnectionInterface::RTCConfiguration client_1_config;
5267 webrtc::PeerConnectionInterface::IceServer ice_server_1;
5268 ice_server_1.urls.push_back("turn:88.88.88.0:3478");
5269 ice_server_1.username = "test";
5270 ice_server_1.password = "test";
5271 client_1_config.servers.push_back(ice_server_1);
5272 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
5273 client_1_config.presume_writable_when_fully_relayed = true;
5274
5275 PeerConnectionInterface::RTCConfiguration client_2_config;
5276 webrtc::PeerConnectionInterface::IceServer ice_server_2;
5277 ice_server_2.urls.push_back("turn:99.99.99.0:3478");
5278 ice_server_2.username = "test";
5279 ice_server_2.password = "test";
5280 client_2_config.servers.push_back(ice_server_2);
5281 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
5282 client_2_config.presume_writable_when_fully_relayed = true;
5283
5284 ASSERT_TRUE(
5285 CreatePeerConnectionWrappersWithConfig(client_1_config, client_2_config));
5286 // Set up the simulated delays.
5287 SetSignalingDelayMs(signaling_trip_delay_ms);
5288 ConnectFakeSignaling();
5289 virtual_socket_server()->set_delay_mean(media_hop_delay_ms);
5290 virtual_socket_server()->UpdateDelayDistribution();
5291
5292 // Set "offer to receive audio/video" without adding any tracks, so we just
5293 // set up ICE/DTLS with no media.
5294 PeerConnectionInterface::RTCOfferAnswerOptions options;
5295 options.offer_to_receive_audio = 1;
5296 options.offer_to_receive_video = 1;
5297 caller()->SetOfferAnswerOptions(options);
5298 caller()->CreateAndSetAndSignalOffer();
deadbeef71452802017-05-07 17:21:01 -07005299 EXPECT_TRUE_SIMULATED_WAIT(DtlsConnected(), total_connection_time_ms,
5300 fake_clock);
Seth Hampson1d4a76d2018-06-19 14:31:41 -07005301 // Closing the PeerConnections destroys the ports before the ScopedFakeClock.
5302 // If this is not done a DCHECK can be hit in ports.cc, because a large
5303 // negative number is calculated for the rtt due to the global clock changing.
Steve Antond91969e2019-05-30 12:27:03 -07005304 ClosePeerConnections();
deadbeef1dcb1642017-03-29 21:08:16 -07005305}
5306
Jonas Orelandbdcee282017-10-10 14:01:40 +02005307// Verify that a TurnCustomizer passed in through RTCConfiguration
5308// is actually used by the underlying TURN candidate pair.
5309// Note that turnport_unittest.cc contains more detailed, lower-level tests.
Seth Hampson2f0d7022018-02-20 11:54:42 -08005310TEST_P(PeerConnectionIntegrationTest, TurnCustomizerUsedForTurnConnections) {
Jonas Orelandbdcee282017-10-10 14:01:40 +02005311 static const rtc::SocketAddress turn_server_1_internal_address{"88.88.88.0",
5312 3478};
5313 static const rtc::SocketAddress turn_server_1_external_address{"88.88.88.1",
5314 0};
5315 static const rtc::SocketAddress turn_server_2_internal_address{"99.99.99.0",
5316 3478};
5317 static const rtc::SocketAddress turn_server_2_external_address{"99.99.99.1",
5318 0};
Seth Hampsonaed71642018-06-11 07:41:32 -07005319 CreateTurnServer(turn_server_1_internal_address,
5320 turn_server_1_external_address);
5321 CreateTurnServer(turn_server_2_internal_address,
5322 turn_server_2_external_address);
Jonas Orelandbdcee282017-10-10 14:01:40 +02005323
5324 PeerConnectionInterface::RTCConfiguration client_1_config;
5325 webrtc::PeerConnectionInterface::IceServer ice_server_1;
5326 ice_server_1.urls.push_back("turn:88.88.88.0:3478");
5327 ice_server_1.username = "test";
5328 ice_server_1.password = "test";
5329 client_1_config.servers.push_back(ice_server_1);
5330 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
Seth Hampsonaed71642018-06-11 07:41:32 -07005331 auto* customizer1 = CreateTurnCustomizer();
5332 client_1_config.turn_customizer = customizer1;
Jonas Orelandbdcee282017-10-10 14:01:40 +02005333
5334 PeerConnectionInterface::RTCConfiguration client_2_config;
5335 webrtc::PeerConnectionInterface::IceServer ice_server_2;
5336 ice_server_2.urls.push_back("turn:99.99.99.0:3478");
5337 ice_server_2.username = "test";
5338 ice_server_2.password = "test";
5339 client_2_config.servers.push_back(ice_server_2);
5340 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
Seth Hampsonaed71642018-06-11 07:41:32 -07005341 auto* customizer2 = CreateTurnCustomizer();
5342 client_2_config.turn_customizer = customizer2;
Jonas Orelandbdcee282017-10-10 14:01:40 +02005343
5344 ASSERT_TRUE(
5345 CreatePeerConnectionWrappersWithConfig(client_1_config, client_2_config));
5346 ConnectFakeSignaling();
5347
5348 // Set "offer to receive audio/video" without adding any tracks, so we just
5349 // set up ICE/DTLS with no media.
5350 PeerConnectionInterface::RTCOfferAnswerOptions options;
5351 options.offer_to_receive_audio = 1;
5352 options.offer_to_receive_video = 1;
5353 caller()->SetOfferAnswerOptions(options);
5354 caller()->CreateAndSetAndSignalOffer();
5355 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
5356
Seth Hampsonaed71642018-06-11 07:41:32 -07005357 ExpectTurnCustomizerCountersIncremented(customizer1);
5358 ExpectTurnCustomizerCountersIncremented(customizer2);
Jonas Orelandbdcee282017-10-10 14:01:40 +02005359}
5360
Benjamin Wright2d5f3cb2018-05-22 14:46:06 -07005361// Verifies that you can use TCP instead of UDP to connect to a TURN server and
5362// send media between the caller and the callee.
5363TEST_P(PeerConnectionIntegrationTest, TCPUsedForTurnConnections) {
5364 static const rtc::SocketAddress turn_server_internal_address{"88.88.88.0",
5365 3478};
5366 static const rtc::SocketAddress turn_server_external_address{"88.88.88.1", 0};
5367
5368 // Enable TCP for the fake turn server.
Seth Hampsonaed71642018-06-11 07:41:32 -07005369 CreateTurnServer(turn_server_internal_address, turn_server_external_address,
5370 cricket::PROTO_TCP);
Benjamin Wright2d5f3cb2018-05-22 14:46:06 -07005371
5372 webrtc::PeerConnectionInterface::IceServer ice_server;
5373 ice_server.urls.push_back("turn:88.88.88.0:3478?transport=tcp");
5374 ice_server.username = "test";
5375 ice_server.password = "test";
5376
5377 PeerConnectionInterface::RTCConfiguration client_1_config;
5378 client_1_config.servers.push_back(ice_server);
5379 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
5380
5381 PeerConnectionInterface::RTCConfiguration client_2_config;
5382 client_2_config.servers.push_back(ice_server);
5383 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
5384
5385 ASSERT_TRUE(
5386 CreatePeerConnectionWrappersWithConfig(client_1_config, client_2_config));
5387
5388 // Do normal offer/answer and wait for ICE to complete.
5389 ConnectFakeSignaling();
5390 caller()->AddAudioVideoTracks();
5391 callee()->AddAudioVideoTracks();
5392 caller()->CreateAndSetAndSignalOffer();
5393 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5394 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
5395 callee()->ice_connection_state(), kMaxWaitForFramesMs);
5396
5397 MediaExpectations media_expectations;
5398 media_expectations.ExpectBidirectionalAudioAndVideo();
5399 EXPECT_TRUE(ExpectNewFrames(media_expectations));
5400}
5401
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07005402// Verify that a SSLCertificateVerifier passed in through
5403// PeerConnectionDependencies is actually used by the underlying SSL
5404// implementation to determine whether a certificate presented by the TURN
5405// server is accepted by the client. Note that openssladapter_unittest.cc
5406// contains more detailed, lower-level tests.
5407TEST_P(PeerConnectionIntegrationTest,
5408 SSLCertificateVerifierUsedForTurnConnections) {
5409 static const rtc::SocketAddress turn_server_internal_address{"88.88.88.0",
5410 3478};
5411 static const rtc::SocketAddress turn_server_external_address{"88.88.88.1", 0};
5412
5413 // Enable TCP-TLS for the fake turn server. We need to pass in 88.88.88.0 so
5414 // that host name verification passes on the fake certificate.
Seth Hampsonaed71642018-06-11 07:41:32 -07005415 CreateTurnServer(turn_server_internal_address, turn_server_external_address,
5416 cricket::PROTO_TLS, "88.88.88.0");
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07005417
5418 webrtc::PeerConnectionInterface::IceServer ice_server;
5419 ice_server.urls.push_back("turns:88.88.88.0:3478?transport=tcp");
5420 ice_server.username = "test";
5421 ice_server.password = "test";
5422
5423 PeerConnectionInterface::RTCConfiguration client_1_config;
5424 client_1_config.servers.push_back(ice_server);
5425 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
5426
5427 PeerConnectionInterface::RTCConfiguration client_2_config;
5428 client_2_config.servers.push_back(ice_server);
5429 // Setting the type to kRelay forces the connection to go through a TURN
5430 // server.
5431 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
5432
5433 // Get a copy to the pointer so we can verify calls later.
5434 rtc::TestCertificateVerifier* client_1_cert_verifier =
5435 new rtc::TestCertificateVerifier();
5436 client_1_cert_verifier->verify_certificate_ = true;
5437 rtc::TestCertificateVerifier* client_2_cert_verifier =
5438 new rtc::TestCertificateVerifier();
5439 client_2_cert_verifier->verify_certificate_ = true;
5440
5441 // Create the dependencies with the test certificate verifier.
5442 webrtc::PeerConnectionDependencies client_1_deps(nullptr);
5443 client_1_deps.tls_cert_verifier =
5444 std::unique_ptr<rtc::TestCertificateVerifier>(client_1_cert_verifier);
5445 webrtc::PeerConnectionDependencies client_2_deps(nullptr);
5446 client_2_deps.tls_cert_verifier =
5447 std::unique_ptr<rtc::TestCertificateVerifier>(client_2_cert_verifier);
5448
5449 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndDeps(
5450 client_1_config, std::move(client_1_deps), client_2_config,
5451 std::move(client_2_deps)));
5452 ConnectFakeSignaling();
5453
5454 // Set "offer to receive audio/video" without adding any tracks, so we just
5455 // set up ICE/DTLS with no media.
5456 PeerConnectionInterface::RTCOfferAnswerOptions options;
5457 options.offer_to_receive_audio = 1;
5458 options.offer_to_receive_video = 1;
5459 caller()->SetOfferAnswerOptions(options);
5460 caller()->CreateAndSetAndSignalOffer();
5461 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
5462
5463 EXPECT_GT(client_1_cert_verifier->call_count_, 0u);
5464 EXPECT_GT(client_2_cert_verifier->call_count_, 0u);
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07005465}
5466
5467TEST_P(PeerConnectionIntegrationTest,
5468 SSLCertificateVerifierFailureUsedForTurnConnectionsFailsConnection) {
5469 static const rtc::SocketAddress turn_server_internal_address{"88.88.88.0",
5470 3478};
5471 static const rtc::SocketAddress turn_server_external_address{"88.88.88.1", 0};
5472
5473 // Enable TCP-TLS for the fake turn server. We need to pass in 88.88.88.0 so
5474 // that host name verification passes on the fake certificate.
Seth Hampsonaed71642018-06-11 07:41:32 -07005475 CreateTurnServer(turn_server_internal_address, turn_server_external_address,
5476 cricket::PROTO_TLS, "88.88.88.0");
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07005477
5478 webrtc::PeerConnectionInterface::IceServer ice_server;
5479 ice_server.urls.push_back("turns:88.88.88.0:3478?transport=tcp");
5480 ice_server.username = "test";
5481 ice_server.password = "test";
5482
5483 PeerConnectionInterface::RTCConfiguration client_1_config;
5484 client_1_config.servers.push_back(ice_server);
5485 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
5486
5487 PeerConnectionInterface::RTCConfiguration client_2_config;
5488 client_2_config.servers.push_back(ice_server);
5489 // Setting the type to kRelay forces the connection to go through a TURN
5490 // server.
5491 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
5492
5493 // Get a copy to the pointer so we can verify calls later.
5494 rtc::TestCertificateVerifier* client_1_cert_verifier =
5495 new rtc::TestCertificateVerifier();
5496 client_1_cert_verifier->verify_certificate_ = false;
5497 rtc::TestCertificateVerifier* client_2_cert_verifier =
5498 new rtc::TestCertificateVerifier();
5499 client_2_cert_verifier->verify_certificate_ = false;
5500
5501 // Create the dependencies with the test certificate verifier.
5502 webrtc::PeerConnectionDependencies client_1_deps(nullptr);
5503 client_1_deps.tls_cert_verifier =
5504 std::unique_ptr<rtc::TestCertificateVerifier>(client_1_cert_verifier);
5505 webrtc::PeerConnectionDependencies client_2_deps(nullptr);
5506 client_2_deps.tls_cert_verifier =
5507 std::unique_ptr<rtc::TestCertificateVerifier>(client_2_cert_verifier);
5508
5509 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndDeps(
5510 client_1_config, std::move(client_1_deps), client_2_config,
5511 std::move(client_2_deps)));
5512 ConnectFakeSignaling();
5513
5514 // Set "offer to receive audio/video" without adding any tracks, so we just
5515 // set up ICE/DTLS with no media.
5516 PeerConnectionInterface::RTCOfferAnswerOptions options;
5517 options.offer_to_receive_audio = 1;
5518 options.offer_to_receive_video = 1;
5519 caller()->SetOfferAnswerOptions(options);
5520 caller()->CreateAndSetAndSignalOffer();
5521 bool wait_res = true;
5522 // TODO(bugs.webrtc.org/9219): When IceConnectionState is implemented
5523 // properly, should be able to just wait for a state of "failed" instead of
5524 // waiting a fixed 10 seconds.
5525 WAIT_(DtlsConnected(), kDefaultTimeout, wait_res);
5526 ASSERT_FALSE(wait_res);
5527
5528 EXPECT_GT(client_1_cert_verifier->call_count_, 0u);
5529 EXPECT_GT(client_2_cert_verifier->call_count_, 0u);
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07005530}
5531
deadbeefc964d0b2017-04-03 10:03:35 -07005532// Test that audio and video flow end-to-end when codec names don't use the
5533// expected casing, given that they're supposed to be case insensitive. To test
5534// this, all but one codec is removed from each media description, and its
5535// casing is changed.
5536//
5537// In the past, this has regressed and caused crashes/black video, due to the
5538// fact that code at some layers was doing case-insensitive comparisons and
5539// code at other layers was not.
Seth Hampson2f0d7022018-02-20 11:54:42 -08005540TEST_P(PeerConnectionIntegrationTest, CodecNamesAreCaseInsensitive) {
deadbeefc964d0b2017-04-03 10:03:35 -07005541 ASSERT_TRUE(CreatePeerConnectionWrappers());
5542 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08005543 caller()->AddAudioVideoTracks();
5544 callee()->AddAudioVideoTracks();
deadbeefc964d0b2017-04-03 10:03:35 -07005545
5546 // Remove all but one audio/video codec (opus and VP8), and change the
5547 // casing of the caller's generated offer.
5548 caller()->SetGeneratedSdpMunger([](cricket::SessionDescription* description) {
5549 cricket::AudioContentDescription* audio =
5550 GetFirstAudioContentDescription(description);
5551 ASSERT_NE(nullptr, audio);
5552 auto audio_codecs = audio->codecs();
5553 audio_codecs.erase(std::remove_if(audio_codecs.begin(), audio_codecs.end(),
5554 [](const cricket::AudioCodec& codec) {
5555 return codec.name != "opus";
5556 }),
5557 audio_codecs.end());
5558 ASSERT_EQ(1u, audio_codecs.size());
5559 audio_codecs[0].name = "OpUs";
5560 audio->set_codecs(audio_codecs);
5561
5562 cricket::VideoContentDescription* video =
5563 GetFirstVideoContentDescription(description);
5564 ASSERT_NE(nullptr, video);
5565 auto video_codecs = video->codecs();
5566 video_codecs.erase(std::remove_if(video_codecs.begin(), video_codecs.end(),
5567 [](const cricket::VideoCodec& codec) {
5568 return codec.name != "VP8";
5569 }),
5570 video_codecs.end());
5571 ASSERT_EQ(1u, video_codecs.size());
5572 video_codecs[0].name = "vP8";
5573 video->set_codecs(video_codecs);
5574 });
5575
5576 caller()->CreateAndSetAndSignalOffer();
5577 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5578
5579 // Verify frames are still received end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08005580 MediaExpectations media_expectations;
5581 media_expectations.ExpectBidirectionalAudioAndVideo();
5582 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeefc964d0b2017-04-03 10:03:35 -07005583}
5584
Jonas Oreland49ac5952018-09-26 16:04:32 +02005585TEST_P(PeerConnectionIntegrationTest, GetSourcesAudio) {
hbos8d609f62017-04-10 07:39:05 -07005586 ASSERT_TRUE(CreatePeerConnectionWrappers());
5587 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08005588 caller()->AddAudioTrack();
hbos8d609f62017-04-10 07:39:05 -07005589 caller()->CreateAndSetAndSignalOffer();
5590 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
deadbeefd8ad7882017-04-18 16:01:17 -07005591 // Wait for one audio frame to be received by the callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08005592 MediaExpectations media_expectations;
5593 media_expectations.CalleeExpectsSomeAudio(1);
5594 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Jonas Oreland49ac5952018-09-26 16:04:32 +02005595 ASSERT_EQ(callee()->pc()->GetReceivers().size(), 1u);
hbos8d609f62017-04-10 07:39:05 -07005596 auto receiver = callee()->pc()->GetReceivers()[0];
5597 ASSERT_EQ(receiver->media_type(), cricket::MEDIA_TYPE_AUDIO);
Jonas Oreland49ac5952018-09-26 16:04:32 +02005598 auto sources = receiver->GetSources();
hbos8d609f62017-04-10 07:39:05 -07005599 ASSERT_GT(receiver->GetParameters().encodings.size(), 0u);
5600 EXPECT_EQ(receiver->GetParameters().encodings[0].ssrc,
Jonas Oreland49ac5952018-09-26 16:04:32 +02005601 sources[0].source_id());
5602 EXPECT_EQ(webrtc::RtpSourceType::SSRC, sources[0].source_type());
5603}
5604
5605TEST_P(PeerConnectionIntegrationTest, GetSourcesVideo) {
5606 ASSERT_TRUE(CreatePeerConnectionWrappers());
5607 ConnectFakeSignaling();
5608 caller()->AddVideoTrack();
5609 caller()->CreateAndSetAndSignalOffer();
5610 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5611 // Wait for one video frame to be received by the callee.
5612 MediaExpectations media_expectations;
5613 media_expectations.CalleeExpectsSomeVideo(1);
5614 ASSERT_TRUE(ExpectNewFrames(media_expectations));
5615 ASSERT_EQ(callee()->pc()->GetReceivers().size(), 1u);
5616 auto receiver = callee()->pc()->GetReceivers()[0];
5617 ASSERT_EQ(receiver->media_type(), cricket::MEDIA_TYPE_VIDEO);
5618 auto sources = receiver->GetSources();
5619 ASSERT_GT(receiver->GetParameters().encodings.size(), 0u);
Yves Gereyf781bb52019-07-23 19:15:39 +02005620 ASSERT_GT(sources.size(), 0u);
Jonas Oreland49ac5952018-09-26 16:04:32 +02005621 EXPECT_EQ(receiver->GetParameters().encodings[0].ssrc,
5622 sources[0].source_id());
5623 EXPECT_EQ(webrtc::RtpSourceType::SSRC, sources[0].source_type());
hbos8d609f62017-04-10 07:39:05 -07005624}
5625
deadbeef2f425aa2017-04-14 10:41:32 -07005626// Test that if a track is removed and added again with a different stream ID,
5627// the new stream ID is successfully communicated in SDP and media continues to
5628// flow end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08005629// TODO(webrtc.bugs.org/8734): This test does not work for Unified Plan because
5630// it will not reuse a transceiver that has already been sending. After creating
5631// a new transceiver it tries to create an offer with two senders of the same
5632// track ids and it fails.
5633TEST_F(PeerConnectionIntegrationTestPlanB, RemoveAndAddTrackWithNewStreamId) {
deadbeef2f425aa2017-04-14 10:41:32 -07005634 ASSERT_TRUE(CreatePeerConnectionWrappers());
5635 ConnectFakeSignaling();
5636
deadbeef2f425aa2017-04-14 10:41:32 -07005637 // Add track using stream 1, do offer/answer.
5638 rtc::scoped_refptr<webrtc::AudioTrackInterface> track =
5639 caller()->CreateLocalAudioTrack();
5640 rtc::scoped_refptr<webrtc::RtpSenderInterface> sender =
Steve Antond78323f2018-07-11 11:13:44 -07005641 caller()->AddTrack(track, {"stream_1"});
deadbeef2f425aa2017-04-14 10:41:32 -07005642 caller()->CreateAndSetAndSignalOffer();
5643 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08005644 {
5645 MediaExpectations media_expectations;
5646 media_expectations.CalleeExpectsSomeAudio(1);
5647 ASSERT_TRUE(ExpectNewFrames(media_expectations));
5648 }
deadbeef2f425aa2017-04-14 10:41:32 -07005649 // Remove the sender, and create a new one with the new stream.
5650 caller()->pc()->RemoveTrack(sender);
Steve Antond78323f2018-07-11 11:13:44 -07005651 sender = caller()->AddTrack(track, {"stream_2"});
deadbeef2f425aa2017-04-14 10:41:32 -07005652 caller()->CreateAndSetAndSignalOffer();
5653 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5654 // Wait for additional audio frames to be received by the callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08005655 {
5656 MediaExpectations media_expectations;
5657 media_expectations.CalleeExpectsSomeAudio();
5658 ASSERT_TRUE(ExpectNewFrames(media_expectations));
5659 }
deadbeef2f425aa2017-04-14 10:41:32 -07005660}
5661
Seth Hampson2f0d7022018-02-20 11:54:42 -08005662TEST_P(PeerConnectionIntegrationTest, RtcEventLogOutputWriteCalled) {
Elad Alon99c3fe52017-10-13 16:29:40 +02005663 ASSERT_TRUE(CreatePeerConnectionWrappers());
5664 ConnectFakeSignaling();
5665
Mirko Bonadei317a1f02019-09-17 17:06:18 +02005666 auto output = std::make_unique<testing::NiceMock<MockRtcEventLogOutput>>();
Mirko Bonadei6a489f22019-04-09 15:11:12 +02005667 ON_CALL(*output, IsActive()).WillByDefault(::testing::Return(true));
5668 ON_CALL(*output, Write(::testing::_)).WillByDefault(::testing::Return(true));
Elad Alon99c3fe52017-10-13 16:29:40 +02005669 EXPECT_CALL(*output, Write(::testing::_)).Times(::testing::AtLeast(1));
Bjorn Tereliusde939432017-11-20 17:38:14 +01005670 EXPECT_TRUE(caller()->pc()->StartRtcEventLog(
5671 std::move(output), webrtc::RtcEventLog::kImmediateOutput));
Elad Alon99c3fe52017-10-13 16:29:40 +02005672
Steve Anton15324772018-01-16 10:26:49 -08005673 caller()->AddAudioVideoTracks();
Elad Alon99c3fe52017-10-13 16:29:40 +02005674 caller()->CreateAndSetAndSignalOffer();
5675 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5676}
5677
Steve Antonede9ca52017-10-16 13:04:27 -07005678// Test that if candidates are only signaled by applying full session
5679// descriptions (instead of using AddIceCandidate), the peers can connect to
5680// each other and exchange media.
Seth Hampson2f0d7022018-02-20 11:54:42 -08005681TEST_P(PeerConnectionIntegrationTest, MediaFlowsWhenCandidatesSetOnlyInSdp) {
Steve Antonede9ca52017-10-16 13:04:27 -07005682 ASSERT_TRUE(CreatePeerConnectionWrappers());
5683 // Each side will signal the session descriptions but not candidates.
5684 ConnectFakeSignalingForSdpOnly();
5685
5686 // Add audio video track and exchange the initial offer/answer with media
5687 // information only. This will start ICE gathering on each side.
Steve Anton15324772018-01-16 10:26:49 -08005688 caller()->AddAudioVideoTracks();
5689 callee()->AddAudioVideoTracks();
Steve Antonede9ca52017-10-16 13:04:27 -07005690 caller()->CreateAndSetAndSignalOffer();
5691
5692 // Wait for all candidates to be gathered on both the caller and callee.
5693 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceGatheringComplete,
5694 caller()->ice_gathering_state(), kDefaultTimeout);
5695 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceGatheringComplete,
5696 callee()->ice_gathering_state(), kDefaultTimeout);
5697
5698 // The candidates will now be included in the session description, so
5699 // signaling them will start the ICE connection.
5700 caller()->CreateAndSetAndSignalOffer();
5701 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5702
5703 // Ensure that media flows in both directions.
Seth Hampson2f0d7022018-02-20 11:54:42 -08005704 MediaExpectations media_expectations;
5705 media_expectations.ExpectBidirectionalAudioAndVideo();
5706 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Antonede9ca52017-10-16 13:04:27 -07005707}
5708
henrika5f6bf242017-11-01 11:06:56 +01005709// Test that SetAudioPlayout can be used to disable audio playout from the
5710// start, then later enable it. This may be useful, for example, if the caller
5711// needs to play a local ringtone until some event occurs, after which it
5712// switches to playing the received audio.
Seth Hampson2f0d7022018-02-20 11:54:42 -08005713TEST_P(PeerConnectionIntegrationTest, DisableAndEnableAudioPlayout) {
henrika5f6bf242017-11-01 11:06:56 +01005714 ASSERT_TRUE(CreatePeerConnectionWrappers());
5715 ConnectFakeSignaling();
5716
5717 // Set up audio-only call where audio playout is disabled on caller's side.
5718 caller()->pc()->SetAudioPlayout(false);
Steve Anton15324772018-01-16 10:26:49 -08005719 caller()->AddAudioTrack();
5720 callee()->AddAudioTrack();
henrika5f6bf242017-11-01 11:06:56 +01005721 caller()->CreateAndSetAndSignalOffer();
5722 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5723
5724 // Pump messages for a second.
5725 WAIT(false, 1000);
5726 // Since audio playout is disabled, the caller shouldn't have received
5727 // anything (at the playout level, at least).
5728 EXPECT_EQ(0, caller()->audio_frames_received());
5729 // As a sanity check, make sure the callee (for which playout isn't disabled)
5730 // did still see frames on its audio level.
5731 ASSERT_GT(callee()->audio_frames_received(), 0);
5732
5733 // Enable playout again, and ensure audio starts flowing.
5734 caller()->pc()->SetAudioPlayout(true);
Seth Hampson2f0d7022018-02-20 11:54:42 -08005735 MediaExpectations media_expectations;
5736 media_expectations.ExpectBidirectionalAudio();
5737 ASSERT_TRUE(ExpectNewFrames(media_expectations));
henrika5f6bf242017-11-01 11:06:56 +01005738}
5739
5740double GetAudioEnergyStat(PeerConnectionWrapper* pc) {
5741 auto report = pc->NewGetStats();
5742 auto track_stats_list =
5743 report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
5744 const webrtc::RTCMediaStreamTrackStats* remote_track_stats = nullptr;
5745 for (const auto* track_stats : track_stats_list) {
5746 if (track_stats->remote_source.is_defined() &&
5747 *track_stats->remote_source) {
5748 remote_track_stats = track_stats;
5749 break;
5750 }
5751 }
5752
5753 if (!remote_track_stats->total_audio_energy.is_defined()) {
5754 return 0.0;
5755 }
5756 return *remote_track_stats->total_audio_energy;
5757}
5758
5759// Test that if audio playout is disabled via the SetAudioPlayout() method, then
5760// incoming audio is still processed and statistics are generated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08005761TEST_P(PeerConnectionIntegrationTest,
henrika5f6bf242017-11-01 11:06:56 +01005762 DisableAudioPlayoutStillGeneratesAudioStats) {
5763 ASSERT_TRUE(CreatePeerConnectionWrappers());
5764 ConnectFakeSignaling();
5765
5766 // Set up audio-only call where playout is disabled but audio-processing is
5767 // still active.
Steve Anton15324772018-01-16 10:26:49 -08005768 caller()->AddAudioTrack();
5769 callee()->AddAudioTrack();
henrika5f6bf242017-11-01 11:06:56 +01005770 caller()->pc()->SetAudioPlayout(false);
5771
5772 caller()->CreateAndSetAndSignalOffer();
5773 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5774
5775 // Wait for the callee to receive audio stats.
5776 EXPECT_TRUE_WAIT(GetAudioEnergyStat(caller()) > 0, kMaxWaitForFramesMs);
5777}
5778
henrika4f167df2017-11-01 14:45:55 +01005779// Test that SetAudioRecording can be used to disable audio recording from the
5780// start, then later enable it. This may be useful, for example, if the caller
5781// wants to ensure that no audio resources are active before a certain state
5782// is reached.
Seth Hampson2f0d7022018-02-20 11:54:42 -08005783TEST_P(PeerConnectionIntegrationTest, DisableAndEnableAudioRecording) {
henrika4f167df2017-11-01 14:45:55 +01005784 ASSERT_TRUE(CreatePeerConnectionWrappers());
5785 ConnectFakeSignaling();
5786
5787 // Set up audio-only call where audio recording is disabled on caller's side.
5788 caller()->pc()->SetAudioRecording(false);
Steve Anton15324772018-01-16 10:26:49 -08005789 caller()->AddAudioTrack();
5790 callee()->AddAudioTrack();
henrika4f167df2017-11-01 14:45:55 +01005791 caller()->CreateAndSetAndSignalOffer();
5792 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5793
5794 // Pump messages for a second.
5795 WAIT(false, 1000);
5796 // Since caller has disabled audio recording, the callee shouldn't have
5797 // received anything.
5798 EXPECT_EQ(0, callee()->audio_frames_received());
5799 // As a sanity check, make sure the caller did still see frames on its
5800 // audio level since audio recording is enabled on the calle side.
5801 ASSERT_GT(caller()->audio_frames_received(), 0);
5802
5803 // Enable audio recording again, and ensure audio starts flowing.
5804 caller()->pc()->SetAudioRecording(true);
Seth Hampson2f0d7022018-02-20 11:54:42 -08005805 MediaExpectations media_expectations;
5806 media_expectations.ExpectBidirectionalAudio();
5807 ASSERT_TRUE(ExpectNewFrames(media_expectations));
henrika4f167df2017-11-01 14:45:55 +01005808}
5809
Taylor Brandstetter389a97c2018-01-03 16:26:06 -08005810// Test that after closing PeerConnections, they stop sending any packets (ICE,
5811// DTLS, RTP...).
Seth Hampson2f0d7022018-02-20 11:54:42 -08005812TEST_P(PeerConnectionIntegrationTest, ClosingConnectionStopsPacketFlow) {
Taylor Brandstetter389a97c2018-01-03 16:26:06 -08005813 // Set up audio/video/data, wait for some frames to be received.
5814 ASSERT_TRUE(CreatePeerConnectionWrappers());
5815 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08005816 caller()->AddAudioVideoTracks();
Taylor Brandstetter389a97c2018-01-03 16:26:06 -08005817#ifdef HAVE_SCTP
5818 caller()->CreateDataChannel();
5819#endif
5820 caller()->CreateAndSetAndSignalOffer();
5821 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08005822 MediaExpectations media_expectations;
5823 media_expectations.CalleeExpectsSomeAudioAndVideo();
5824 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Taylor Brandstetter389a97c2018-01-03 16:26:06 -08005825 // Close PeerConnections.
Steve Antond91969e2019-05-30 12:27:03 -07005826 ClosePeerConnections();
Taylor Brandstetter389a97c2018-01-03 16:26:06 -08005827 // Pump messages for a second, and ensure no new packets end up sent.
5828 uint32_t sent_packets_a = virtual_socket_server()->sent_packets();
5829 WAIT(false, 1000);
5830 uint32_t sent_packets_b = virtual_socket_server()->sent_packets();
5831 EXPECT_EQ(sent_packets_a, sent_packets_b);
5832}
5833
Steve Anton7eca0932018-03-30 15:18:41 -07005834// Test that transport stats are generated by the RTCStatsCollector for a
5835// connection that only involves data channels. This is a regression test for
5836// crbug.com/826972.
5837#ifdef HAVE_SCTP
5838TEST_P(PeerConnectionIntegrationTest,
5839 TransportStatsReportedForDataChannelOnlyConnection) {
5840 ASSERT_TRUE(CreatePeerConnectionWrappers());
5841 ConnectFakeSignaling();
5842 caller()->CreateDataChannel();
5843
5844 caller()->CreateAndSetAndSignalOffer();
5845 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5846 ASSERT_TRUE_WAIT(callee()->data_channel(), kDefaultTimeout);
5847
5848 auto caller_report = caller()->NewGetStats();
5849 EXPECT_EQ(1u, caller_report->GetStatsOfType<RTCTransportStats>().size());
5850 auto callee_report = callee()->NewGetStats();
5851 EXPECT_EQ(1u, callee_report->GetStatsOfType<RTCTransportStats>().size());
5852}
5853#endif // HAVE_SCTP
5854
Qingsi Wang7685e862018-06-11 20:15:46 -07005855TEST_P(PeerConnectionIntegrationTest,
5856 IceEventsGeneratedAndLoggedInRtcEventLog) {
5857 ASSERT_TRUE(CreatePeerConnectionWrappersWithFakeRtcEventLog());
5858 ConnectFakeSignaling();
5859 PeerConnectionInterface::RTCOfferAnswerOptions options;
5860 options.offer_to_receive_audio = 1;
5861 caller()->SetOfferAnswerOptions(options);
5862 caller()->CreateAndSetAndSignalOffer();
5863 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
5864 ASSERT_NE(nullptr, caller()->event_log_factory());
5865 ASSERT_NE(nullptr, callee()->event_log_factory());
5866 webrtc::FakeRtcEventLog* caller_event_log =
5867 static_cast<webrtc::FakeRtcEventLog*>(
5868 caller()->event_log_factory()->last_log_created());
5869 webrtc::FakeRtcEventLog* callee_event_log =
5870 static_cast<webrtc::FakeRtcEventLog*>(
5871 callee()->event_log_factory()->last_log_created());
5872 ASSERT_NE(nullptr, caller_event_log);
5873 ASSERT_NE(nullptr, callee_event_log);
5874 int caller_ice_config_count = caller_event_log->GetEventCount(
5875 webrtc::RtcEvent::Type::IceCandidatePairConfig);
5876 int caller_ice_event_count = caller_event_log->GetEventCount(
5877 webrtc::RtcEvent::Type::IceCandidatePairEvent);
5878 int callee_ice_config_count = callee_event_log->GetEventCount(
5879 webrtc::RtcEvent::Type::IceCandidatePairConfig);
5880 int callee_ice_event_count = callee_event_log->GetEventCount(
5881 webrtc::RtcEvent::Type::IceCandidatePairEvent);
5882 EXPECT_LT(0, caller_ice_config_count);
5883 EXPECT_LT(0, caller_ice_event_count);
5884 EXPECT_LT(0, callee_ice_config_count);
5885 EXPECT_LT(0, callee_ice_event_count);
5886}
5887
Qingsi Wangc129c352019-04-18 10:41:58 -07005888TEST_P(PeerConnectionIntegrationTest, RegatherAfterChangingIceTransportType) {
Qingsi Wangc129c352019-04-18 10:41:58 -07005889 static const rtc::SocketAddress turn_server_internal_address{"88.88.88.0",
5890 3478};
5891 static const rtc::SocketAddress turn_server_external_address{"88.88.88.1", 0};
5892
5893 CreateTurnServer(turn_server_internal_address, turn_server_external_address);
5894
5895 webrtc::PeerConnectionInterface::IceServer ice_server;
5896 ice_server.urls.push_back("turn:88.88.88.0:3478");
5897 ice_server.username = "test";
5898 ice_server.password = "test";
5899
5900 PeerConnectionInterface::RTCConfiguration caller_config;
5901 caller_config.servers.push_back(ice_server);
5902 caller_config.type = webrtc::PeerConnectionInterface::kRelay;
5903 caller_config.continual_gathering_policy = PeerConnection::GATHER_CONTINUALLY;
Qingsi Wang1fe119f2019-05-31 16:55:33 -07005904 caller_config.surface_ice_candidates_on_ice_transport_type_changed = true;
Qingsi Wangc129c352019-04-18 10:41:58 -07005905
5906 PeerConnectionInterface::RTCConfiguration callee_config;
5907 callee_config.servers.push_back(ice_server);
5908 callee_config.type = webrtc::PeerConnectionInterface::kRelay;
5909 callee_config.continual_gathering_policy = PeerConnection::GATHER_CONTINUALLY;
Qingsi Wang1fe119f2019-05-31 16:55:33 -07005910 callee_config.surface_ice_candidates_on_ice_transport_type_changed = true;
Qingsi Wangc129c352019-04-18 10:41:58 -07005911
5912 ASSERT_TRUE(
5913 CreatePeerConnectionWrappersWithConfig(caller_config, callee_config));
5914
5915 // Do normal offer/answer and wait for ICE to complete.
5916 ConnectFakeSignaling();
5917 caller()->AddAudioVideoTracks();
5918 callee()->AddAudioVideoTracks();
5919 caller()->CreateAndSetAndSignalOffer();
5920 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5921 // Since we are doing continual gathering, the ICE transport does not reach
5922 // kIceGatheringComplete (see
5923 // P2PTransportChannel::OnCandidatesAllocationDone), and consequently not
5924 // kIceConnectionComplete.
5925 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
5926 caller()->ice_connection_state(), kDefaultTimeout);
5927 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
5928 callee()->ice_connection_state(), kDefaultTimeout);
5929 // Note that we cannot use the metric
5930 // |WebRTC.PeerConnection.CandidatePairType_UDP| in this test since this
5931 // metric is only populated when we reach kIceConnectionComplete in the
5932 // current implementation.
5933 EXPECT_EQ(cricket::RELAY_PORT_TYPE,
5934 caller()->last_candidate_gathered().type());
5935 EXPECT_EQ(cricket::RELAY_PORT_TYPE,
5936 callee()->last_candidate_gathered().type());
5937
5938 // Loosen the caller's candidate filter.
5939 caller_config = caller()->pc()->GetConfiguration();
5940 caller_config.type = webrtc::PeerConnectionInterface::kAll;
5941 caller()->pc()->SetConfiguration(caller_config);
5942 // We should have gathered a new host candidate.
5943 EXPECT_EQ_WAIT(cricket::LOCAL_PORT_TYPE,
5944 caller()->last_candidate_gathered().type(), kDefaultTimeout);
5945
5946 // Loosen the callee's candidate filter.
5947 callee_config = callee()->pc()->GetConfiguration();
5948 callee_config.type = webrtc::PeerConnectionInterface::kAll;
5949 callee()->pc()->SetConfiguration(callee_config);
5950 EXPECT_EQ_WAIT(cricket::LOCAL_PORT_TYPE,
5951 callee()->last_candidate_gathered().type(), kDefaultTimeout);
5952}
5953
Eldar Relloda13ea22019-06-01 12:23:43 +03005954TEST_P(PeerConnectionIntegrationTest, OnIceCandidateError) {
Eldar Relloda13ea22019-06-01 12:23:43 +03005955 static const rtc::SocketAddress turn_server_internal_address{"88.88.88.0",
5956 3478};
5957 static const rtc::SocketAddress turn_server_external_address{"88.88.88.1", 0};
5958
5959 CreateTurnServer(turn_server_internal_address, turn_server_external_address);
5960
5961 webrtc::PeerConnectionInterface::IceServer ice_server;
5962 ice_server.urls.push_back("turn:88.88.88.0:3478");
5963 ice_server.username = "test";
5964 ice_server.password = "123";
5965
5966 PeerConnectionInterface::RTCConfiguration caller_config;
5967 caller_config.servers.push_back(ice_server);
5968 caller_config.type = webrtc::PeerConnectionInterface::kRelay;
5969 caller_config.continual_gathering_policy = PeerConnection::GATHER_CONTINUALLY;
5970
5971 PeerConnectionInterface::RTCConfiguration callee_config;
5972 callee_config.servers.push_back(ice_server);
5973 callee_config.type = webrtc::PeerConnectionInterface::kRelay;
5974 callee_config.continual_gathering_policy = PeerConnection::GATHER_CONTINUALLY;
5975
5976 ASSERT_TRUE(
5977 CreatePeerConnectionWrappersWithConfig(caller_config, callee_config));
5978
5979 // Do normal offer/answer and wait for ICE to complete.
5980 ConnectFakeSignaling();
5981 caller()->AddAudioVideoTracks();
5982 callee()->AddAudioVideoTracks();
5983 caller()->CreateAndSetAndSignalOffer();
5984 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5985 EXPECT_EQ_WAIT(401, caller()->error_event().error_code, kDefaultTimeout);
5986 EXPECT_EQ("Unauthorized", caller()->error_event().error_text);
5987 EXPECT_EQ("turn:88.88.88.0:3478?transport=udp", caller()->error_event().url);
5988 EXPECT_NE(std::string::npos,
5989 caller()->error_event().host_candidate.find(":"));
5990}
5991
Eldar Rello5ab79e62019-10-09 18:29:44 +03005992TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
5993 AudioKeepsFlowingAfterImplicitRollback) {
5994 PeerConnectionInterface::RTCConfiguration config;
5995 config.sdp_semantics = SdpSemantics::kUnifiedPlan;
5996 config.enable_implicit_rollback = true;
5997 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
5998 ConnectFakeSignaling();
5999 caller()->AddAudioTrack();
6000 callee()->AddAudioTrack();
6001 caller()->CreateAndSetAndSignalOffer();
6002 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
6003 MediaExpectations media_expectations;
6004 media_expectations.ExpectBidirectionalAudio();
6005 ASSERT_TRUE(ExpectNewFrames(media_expectations));
6006 SetSignalIceCandidates(false); // Workaround candidate outrace sdp.
6007 caller()->AddVideoTrack();
6008 callee()->AddVideoTrack();
6009 rtc::scoped_refptr<MockSetSessionDescriptionObserver> observer(
6010 new rtc::RefCountedObject<MockSetSessionDescriptionObserver>());
6011 callee()->pc()->SetLocalDescription(observer,
6012 callee()->CreateOfferAndWait().release());
6013 EXPECT_TRUE_WAIT(observer->called(), kDefaultTimeout);
6014 caller()->CreateAndSetAndSignalOffer(); // Implicit rollback.
6015 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
6016 ASSERT_TRUE(ExpectNewFrames(media_expectations));
6017}
6018
6019TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
6020 ImplicitRollbackVisitsStableState) {
6021 RTCConfiguration config;
6022 config.sdp_semantics = SdpSemantics::kUnifiedPlan;
6023 config.enable_implicit_rollback = true;
6024
6025 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
6026
6027 rtc::scoped_refptr<MockSetSessionDescriptionObserver> sld_observer(
6028 new rtc::RefCountedObject<MockSetSessionDescriptionObserver>());
6029 callee()->pc()->SetLocalDescription(sld_observer,
6030 callee()->CreateOfferAndWait().release());
6031 EXPECT_TRUE_WAIT(sld_observer->called(), kDefaultTimeout);
6032 EXPECT_EQ(sld_observer->error(), "");
6033
6034 rtc::scoped_refptr<MockSetSessionDescriptionObserver> srd_observer(
6035 new rtc::RefCountedObject<MockSetSessionDescriptionObserver>());
6036 callee()->pc()->SetRemoteDescription(
6037 srd_observer, caller()->CreateOfferAndWait().release());
6038 EXPECT_TRUE_WAIT(srd_observer->called(), kDefaultTimeout);
6039 EXPECT_EQ(srd_observer->error(), "");
6040
6041 EXPECT_THAT(callee()->peer_connection_signaling_state_history(),
6042 ElementsAre(PeerConnectionInterface::kHaveLocalOffer,
6043 PeerConnectionInterface::kStable,
6044 PeerConnectionInterface::kHaveRemoteOffer));
6045}
6046
Mirko Bonadeic84f6612019-01-31 12:20:57 +01006047INSTANTIATE_TEST_SUITE_P(PeerConnectionIntegrationTest,
6048 PeerConnectionIntegrationTest,
6049 Values(SdpSemantics::kPlanB,
6050 SdpSemantics::kUnifiedPlan));
Steve Antond3679212018-01-17 17:41:02 -08006051
Steve Anton74255ff2018-01-24 18:32:57 -08006052// Tests that verify interoperability between Plan B and Unified Plan
6053// PeerConnections.
6054class PeerConnectionIntegrationInteropTest
Seth Hampson2f0d7022018-02-20 11:54:42 -08006055 : public PeerConnectionIntegrationBaseTest,
Steve Anton74255ff2018-01-24 18:32:57 -08006056 public ::testing::WithParamInterface<
6057 std::tuple<SdpSemantics, SdpSemantics>> {
6058 protected:
Seth Hampson2f0d7022018-02-20 11:54:42 -08006059 // Setting the SdpSemantics for the base test to kDefault does not matter
6060 // because we specify not to use the test semantics when creating
6061 // PeerConnectionWrappers.
Steve Anton74255ff2018-01-24 18:32:57 -08006062 PeerConnectionIntegrationInteropTest()
Steve Anton3acffc32018-04-12 17:21:03 -07006063 : PeerConnectionIntegrationBaseTest(SdpSemantics::kPlanB),
Seth Hampson2f0d7022018-02-20 11:54:42 -08006064 caller_semantics_(std::get<0>(GetParam())),
Steve Anton74255ff2018-01-24 18:32:57 -08006065 callee_semantics_(std::get<1>(GetParam())) {}
6066
6067 bool CreatePeerConnectionWrappersWithSemantics() {
Steve Anton3acffc32018-04-12 17:21:03 -07006068 return CreatePeerConnectionWrappersWithSdpSemantics(caller_semantics_,
6069 callee_semantics_);
Steve Anton74255ff2018-01-24 18:32:57 -08006070 }
6071
6072 const SdpSemantics caller_semantics_;
6073 const SdpSemantics callee_semantics_;
6074};
6075
6076TEST_P(PeerConnectionIntegrationInteropTest, NoMediaLocalToNoMediaRemote) {
6077 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
6078 ConnectFakeSignaling();
6079
6080 caller()->CreateAndSetAndSignalOffer();
6081 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
6082}
6083
6084TEST_P(PeerConnectionIntegrationInteropTest, OneAudioLocalToNoMediaRemote) {
6085 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
6086 ConnectFakeSignaling();
6087 auto audio_sender = caller()->AddAudioTrack();
6088
6089 caller()->CreateAndSetAndSignalOffer();
6090 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
6091
6092 // Verify that one audio receiver has been created on the remote and that it
6093 // has the same track ID as the sending track.
6094 auto receivers = callee()->pc()->GetReceivers();
6095 ASSERT_EQ(1u, receivers.size());
6096 EXPECT_EQ(cricket::MEDIA_TYPE_AUDIO, receivers[0]->media_type());
6097 EXPECT_EQ(receivers[0]->track()->id(), audio_sender->track()->id());
6098
Seth Hampson2f0d7022018-02-20 11:54:42 -08006099 MediaExpectations media_expectations;
6100 media_expectations.CalleeExpectsSomeAudio();
6101 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08006102}
6103
6104TEST_P(PeerConnectionIntegrationInteropTest, OneAudioOneVideoToNoMediaRemote) {
6105 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
6106 ConnectFakeSignaling();
6107 auto video_sender = caller()->AddVideoTrack();
6108 auto audio_sender = caller()->AddAudioTrack();
6109
6110 caller()->CreateAndSetAndSignalOffer();
6111 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
6112
6113 // Verify that one audio and one video receiver have been created on the
6114 // remote and that they have the same track IDs as the sending tracks.
6115 auto audio_receivers =
6116 callee()->GetReceiversOfType(cricket::MEDIA_TYPE_AUDIO);
6117 ASSERT_EQ(1u, audio_receivers.size());
6118 EXPECT_EQ(audio_receivers[0]->track()->id(), audio_sender->track()->id());
6119 auto video_receivers =
6120 callee()->GetReceiversOfType(cricket::MEDIA_TYPE_VIDEO);
6121 ASSERT_EQ(1u, video_receivers.size());
6122 EXPECT_EQ(video_receivers[0]->track()->id(), video_sender->track()->id());
6123
Seth Hampson2f0d7022018-02-20 11:54:42 -08006124 MediaExpectations media_expectations;
6125 media_expectations.CalleeExpectsSomeAudioAndVideo();
6126 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08006127}
6128
6129TEST_P(PeerConnectionIntegrationInteropTest,
6130 OneAudioOneVideoLocalToOneAudioOneVideoRemote) {
6131 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
6132 ConnectFakeSignaling();
6133 caller()->AddAudioVideoTracks();
6134 callee()->AddAudioVideoTracks();
6135
6136 caller()->CreateAndSetAndSignalOffer();
6137 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
6138
Seth Hampson2f0d7022018-02-20 11:54:42 -08006139 MediaExpectations media_expectations;
6140 media_expectations.ExpectBidirectionalAudioAndVideo();
6141 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08006142}
6143
6144TEST_P(PeerConnectionIntegrationInteropTest,
6145 ReverseRolesOneAudioLocalToOneVideoRemote) {
6146 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
6147 ConnectFakeSignaling();
6148 caller()->AddAudioTrack();
6149 callee()->AddVideoTrack();
6150
6151 caller()->CreateAndSetAndSignalOffer();
6152 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
6153
6154 // Verify that only the audio track has been negotiated.
6155 EXPECT_EQ(0u, caller()->GetReceiversOfType(cricket::MEDIA_TYPE_VIDEO).size());
6156 // Might also check that the callee's NegotiationNeeded flag is set.
6157
6158 // Reverse roles.
6159 callee()->CreateAndSetAndSignalOffer();
6160 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
6161
Seth Hampson2f0d7022018-02-20 11:54:42 -08006162 MediaExpectations media_expectations;
6163 media_expectations.CallerExpectsSomeVideo();
6164 media_expectations.CalleeExpectsSomeAudio();
6165 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08006166}
6167
Mirko Bonadeic84f6612019-01-31 12:20:57 +01006168INSTANTIATE_TEST_SUITE_P(
Steve Antonba42e992018-04-09 14:10:01 -07006169 PeerConnectionIntegrationTest,
6170 PeerConnectionIntegrationInteropTest,
6171 Values(std::make_tuple(SdpSemantics::kPlanB, SdpSemantics::kUnifiedPlan),
6172 std::make_tuple(SdpSemantics::kUnifiedPlan, SdpSemantics::kPlanB)));
6173
6174// Test that if the Unified Plan side offers two video tracks then the Plan B
6175// side will only see the first one and ignore the second.
6176TEST_F(PeerConnectionIntegrationTestPlanB, TwoVideoUnifiedPlanToNoMediaPlanB) {
Steve Anton3acffc32018-04-12 17:21:03 -07006177 ASSERT_TRUE(CreatePeerConnectionWrappersWithSdpSemantics(
6178 SdpSemantics::kUnifiedPlan, SdpSemantics::kPlanB));
Steve Anton74255ff2018-01-24 18:32:57 -08006179 ConnectFakeSignaling();
6180 auto first_sender = caller()->AddVideoTrack();
6181 caller()->AddVideoTrack();
6182
6183 caller()->CreateAndSetAndSignalOffer();
6184 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
6185
6186 // Verify that there is only one receiver and it corresponds to the first
6187 // added track.
6188 auto receivers = callee()->pc()->GetReceivers();
6189 ASSERT_EQ(1u, receivers.size());
6190 EXPECT_TRUE(receivers[0]->track()->enabled());
6191 EXPECT_EQ(first_sender->track()->id(), receivers[0]->track()->id());
6192
Seth Hampson2f0d7022018-02-20 11:54:42 -08006193 MediaExpectations media_expectations;
6194 media_expectations.CalleeExpectsSomeVideo();
6195 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08006196}
6197
Steve Anton2bed3972019-01-04 17:04:30 -08006198// Test that if the initial offer tagged BUNDLE section is rejected due to its
6199// associated RtpTransceiver being stopped and another transceiver is added,
6200// then renegotiation causes the callee to receive the new video track without
6201// error.
6202// This is a regression test for bugs.webrtc.org/9954
6203TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
6204 ReOfferWithStoppedBundleTaggedTransceiver) {
6205 RTCConfiguration config;
6206 config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
6207 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
6208 ConnectFakeSignaling();
6209 auto audio_transceiver_or_error =
6210 caller()->pc()->AddTransceiver(caller()->CreateLocalAudioTrack());
6211 ASSERT_TRUE(audio_transceiver_or_error.ok());
6212 auto audio_transceiver = audio_transceiver_or_error.MoveValue();
6213
6214 caller()->CreateAndSetAndSignalOffer();
6215 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
6216 {
6217 MediaExpectations media_expectations;
6218 media_expectations.CalleeExpectsSomeAudio();
6219 ASSERT_TRUE(ExpectNewFrames(media_expectations));
6220 }
6221
6222 audio_transceiver->Stop();
6223 caller()->pc()->AddTransceiver(caller()->CreateLocalVideoTrack());
6224
6225 caller()->CreateAndSetAndSignalOffer();
6226 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
6227 {
6228 MediaExpectations media_expectations;
6229 media_expectations.CalleeExpectsSomeVideo();
6230 ASSERT_TRUE(ExpectNewFrames(media_expectations));
6231 }
6232}
6233
Harald Alvestrandd61f2a72019-05-08 20:20:59 +02006234#ifdef HAVE_SCTP
6235
6236TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
6237 EndToEndCallWithBundledSctpDataChannel) {
6238 ASSERT_TRUE(CreatePeerConnectionWrappers());
6239 ConnectFakeSignaling();
6240 caller()->CreateDataChannel();
6241 caller()->AddAudioVideoTracks();
6242 callee()->AddAudioVideoTracks();
6243 caller()->SetGeneratedSdpMunger(MakeSpecCompliantSctpOffer);
6244 caller()->CreateAndSetAndSignalOffer();
6245 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
6246 // Ensure that media and data are multiplexed on the same DTLS transport.
6247 // This only works on Unified Plan, because transports are not exposed in plan
6248 // B.
6249 auto sctp_info = caller()->pc()->GetSctpTransport()->Information();
6250 EXPECT_EQ(sctp_info.dtls_transport(),
6251 caller()->pc()->GetSenders()[0]->dtls_transport());
6252}
6253
6254#endif // HAVE_SCTP
6255
deadbeef1dcb1642017-03-29 21:08:16 -07006256} // namespace
Mirko Bonadeiab64e8a2018-12-12 12:10:18 +01006257} // namespace webrtc
deadbeef1dcb1642017-03-29 21:08:16 -07006258
6259#endif // if !defined(THREAD_SANITIZER)