blob: d51415292ad8f4c9352aee282a870d6b678654d1 [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"
Qingsi Wang1dac6d82018-12-12 15:28:47 -080025#include "absl/memory/memory.h"
Karl Wiberg1b0eae32017-10-17 14:48:54 +020026#include "api/audio_codecs/builtin_audio_decoder_factory.h"
27#include "api/audio_codecs/builtin_audio_encoder_factory.h"
Steve Anton10542f22019-01-11 09:11:00 -080028#include "api/media_stream_interface.h"
29#include "api/peer_connection_interface.h"
30#include "api/peer_connection_proxy.h"
31#include "api/rtp_receiver_interface.h"
Bjorn Mellem175aa2e2018-11-08 11:23:22 -080032#include "api/test/loopback_media_transport.h"
Steve Anton10542f22019-01-11 09:11:00 -080033#include "api/uma_metrics.h"
Anders Carlsson67537952018-05-03 11:28:29 +020034#include "api/video_codecs/builtin_video_decoder_factory.h"
35#include "api/video_codecs/builtin_video_encoder_factory.h"
36#include "api/video_codecs/sdp_video_format.h"
Qingsi Wang7685e862018-06-11 20:15:46 -070037#include "call/call.h"
38#include "logging/rtc_event_log/fake_rtc_event_log_factory.h"
Bjorn Tereliusb8b3c992019-01-09 11:15:34 +010039#include "logging/rtc_event_log/rtc_event_log_factory.h"
Qingsi Wang7685e862018-06-11 20:15:46 -070040#include "logging/rtc_event_log/rtc_event_log_factory_interface.h"
Steve Anton10542f22019-01-11 09:11:00 -080041#include "media/engine/fake_webrtc_video_engine.h"
42#include "media/engine/webrtc_media_engine.h"
Qingsi Wang7685e862018-06-11 20:15:46 -070043#include "modules/audio_processing/include/audio_processing.h"
Steve Anton10542f22019-01-11 09:11:00 -080044#include "p2p/base/mock_async_resolver.h"
45#include "p2p/base/p2p_constants.h"
46#include "p2p/base/port_interface.h"
47#include "p2p/base/test_stun_server.h"
48#include "p2p/base/test_turn_customizer.h"
49#include "p2p/base/test_turn_server.h"
50#include "p2p/client/basic_port_allocator.h"
51#include "pc/dtmf_sender.h"
52#include "pc/local_audio_source.h"
53#include "pc/media_session.h"
54#include "pc/peer_connection.h"
55#include "pc/peer_connection_factory.h"
56#include "pc/rtp_media_utils.h"
57#include "pc/session_description.h"
58#include "pc/test/fake_audio_capture_module.h"
59#include "pc/test/fake_periodic_video_track_source.h"
60#include "pc/test/fake_rtc_certificate_generator.h"
61#include "pc/test/fake_video_track_renderer.h"
62#include "pc/test/mock_peer_connection_observers.h"
Jonas Olssonb75d9e92019-02-22 10:33:29 +010063#include "rtc_base/fake_clock.h"
Steve Anton10542f22019-01-11 09:11:00 -080064#include "rtc_base/fake_network.h"
65#include "rtc_base/firewall_socket_server.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020066#include "rtc_base/gunit.h"
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +020067#include "rtc_base/numerics/safe_conversions.h"
Steve Anton10542f22019-01-11 09:11:00 -080068#include "rtc_base/test_certificate_verifier.h"
69#include "rtc_base/time_utils.h"
70#include "rtc_base/virtual_socket_server.h"
Mirko Bonadei17f48782018-09-28 08:51:10 +020071#include "system_wrappers/include/metrics.h"
Qingsi Wangc129c352019-04-18 10:41:58 -070072#include "test/field_trial.h"
Elad Alon99c3fe52017-10-13 16:29:40 +020073#include "test/gmock.h"
deadbeef1dcb1642017-03-29 21:08:16 -070074
Mirko Bonadeiab64e8a2018-12-12 12:10:18 +010075namespace webrtc {
76namespace {
77
78using ::cricket::ContentInfo;
79using ::cricket::StreamParams;
80using ::rtc::SocketAddress;
81using ::testing::_;
Seth Hampson2f0d7022018-02-20 11:54:42 -080082using ::testing::Combine;
Steve Anton64b626b2019-01-28 17:25:26 -080083using ::testing::Contains;
Mirko Bonadeie46f5db2019-03-26 20:14:46 +010084using ::testing::DoAll;
Steve Antonede9ca52017-10-16 13:04:27 -070085using ::testing::ElementsAre;
Qingsi Wang1dac6d82018-12-12 15:28:47 -080086using ::testing::NiceMock;
Steve Anton64b626b2019-01-28 17:25:26 -080087using ::testing::Return;
Zach Stein6fcdc2f2018-08-23 16:25:55 -070088using ::testing::SetArgPointee;
Steve Antonffa6ce42018-11-30 09:26:08 -080089using ::testing::UnorderedElementsAreArray;
Mirko Bonadeiab64e8a2018-12-12 12:10:18 +010090using ::testing::Values;
Steve Anton74255ff2018-01-24 18:32:57 -080091using RTCConfiguration = PeerConnectionInterface::RTCConfiguration;
deadbeef1dcb1642017-03-29 21:08:16 -070092
93static const int kDefaultTimeout = 10000;
94static const int kMaxWaitForStatsMs = 3000;
95static const int kMaxWaitForActivationMs = 5000;
96static const int kMaxWaitForFramesMs = 10000;
97// Default number of audio/video frames to wait for before considering a test
98// successful.
99static const int kDefaultExpectedAudioFrameCount = 3;
100static const int kDefaultExpectedVideoFrameCount = 3;
101
deadbeef1dcb1642017-03-29 21:08:16 -0700102static const char kDataChannelLabel[] = "data_channel";
103
104// SRTP cipher name negotiated by the tests. This must be updated if the
105// default changes.
Taylor Brandstetterfd350d72018-04-03 16:29:26 -0700106static const int kDefaultSrtpCryptoSuite = rtc::SRTP_AES128_CM_SHA1_80;
deadbeef1dcb1642017-03-29 21:08:16 -0700107static const int kDefaultSrtpCryptoSuiteGcm = rtc::SRTP_AEAD_AES_256_GCM;
108
Steve Antonede9ca52017-10-16 13:04:27 -0700109static const SocketAddress kDefaultLocalAddress("192.168.1.1", 0);
110
deadbeef1dcb1642017-03-29 21:08:16 -0700111// Helper function for constructing offer/answer options to initiate an ICE
112// restart.
113PeerConnectionInterface::RTCOfferAnswerOptions IceRestartOfferAnswerOptions() {
114 PeerConnectionInterface::RTCOfferAnswerOptions options;
115 options.ice_restart = true;
116 return options;
117}
118
deadbeefd8ad7882017-04-18 16:01:17 -0700119// Remove all stream information (SSRCs, track IDs, etc.) and "msid-semantic"
120// attribute from received SDP, simulating a legacy endpoint.
121void RemoveSsrcsAndMsids(cricket::SessionDescription* desc) {
122 for (ContentInfo& content : desc->contents()) {
Steve Antonb1c1de12017-12-21 15:14:30 -0800123 content.media_description()->mutable_streams().clear();
deadbeefd8ad7882017-04-18 16:01:17 -0700124 }
125 desc->set_msid_supported(false);
Henrik Boström5b147782018-12-04 11:25:05 +0100126 desc->set_msid_signaling(0);
deadbeefd8ad7882017-04-18 16:01:17 -0700127}
128
Seth Hampson5897a6e2018-04-03 11:16:33 -0700129// Removes all stream information besides the stream ids, simulating an
130// endpoint that only signals a=msid lines to convey stream_ids.
131void RemoveSsrcsAndKeepMsids(cricket::SessionDescription* desc) {
132 for (ContentInfo& content : desc->contents()) {
Steve Antondf527fd2018-04-27 15:52:03 -0700133 std::string track_id;
Seth Hampson5897a6e2018-04-03 11:16:33 -0700134 std::vector<std::string> stream_ids;
135 if (!content.media_description()->streams().empty()) {
Steve Antondf527fd2018-04-27 15:52:03 -0700136 const StreamParams& first_stream =
137 content.media_description()->streams()[0];
138 track_id = first_stream.id;
139 stream_ids = first_stream.stream_ids();
Seth Hampson5897a6e2018-04-03 11:16:33 -0700140 }
141 content.media_description()->mutable_streams().clear();
Steve Antondf527fd2018-04-27 15:52:03 -0700142 StreamParams new_stream;
143 new_stream.id = track_id;
Seth Hampson5897a6e2018-04-03 11:16:33 -0700144 new_stream.set_stream_ids(stream_ids);
145 content.media_description()->AddStream(new_stream);
146 }
147}
148
zhihuangf8164932017-05-19 13:09:47 -0700149int FindFirstMediaStatsIndexByKind(
150 const std::string& kind,
151 const std::vector<const webrtc::RTCMediaStreamTrackStats*>&
152 media_stats_vec) {
153 for (size_t i = 0; i < media_stats_vec.size(); i++) {
154 if (media_stats_vec[i]->kind.ValueToString() == kind) {
155 return i;
156 }
157 }
158 return -1;
159}
160
deadbeef1dcb1642017-03-29 21:08:16 -0700161class SignalingMessageReceiver {
162 public:
Steve Antona3a92c22017-12-07 10:27:41 -0800163 virtual void ReceiveSdpMessage(SdpType type, const std::string& msg) = 0;
deadbeef1dcb1642017-03-29 21:08:16 -0700164 virtual void ReceiveIceMessage(const std::string& sdp_mid,
165 int sdp_mline_index,
166 const std::string& msg) = 0;
167
168 protected:
169 SignalingMessageReceiver() {}
170 virtual ~SignalingMessageReceiver() {}
171};
172
173class MockRtpReceiverObserver : public webrtc::RtpReceiverObserverInterface {
174 public:
175 explicit MockRtpReceiverObserver(cricket::MediaType media_type)
176 : expected_media_type_(media_type) {}
177
178 void OnFirstPacketReceived(cricket::MediaType media_type) override {
179 ASSERT_EQ(expected_media_type_, media_type);
180 first_packet_received_ = true;
181 }
182
183 bool first_packet_received() const { return first_packet_received_; }
184
185 virtual ~MockRtpReceiverObserver() {}
186
187 private:
188 bool first_packet_received_ = false;
189 cricket::MediaType expected_media_type_;
190};
191
192// Helper class that wraps a peer connection, observes it, and can accept
193// signaling messages from another wrapper.
194//
195// Uses a fake network, fake A/V capture, and optionally fake
196// encoders/decoders, though they aren't used by default since they don't
197// advertise support of any codecs.
Steve Anton94286cb2017-09-26 16:20:19 -0700198// TODO(steveanton): See how this could become a subclass of
Seth Hampson2f0d7022018-02-20 11:54:42 -0800199// PeerConnectionWrapper defined in peerconnectionwrapper.h.
deadbeef1dcb1642017-03-29 21:08:16 -0700200class PeerConnectionWrapper : public webrtc::PeerConnectionObserver,
Steve Anton15324772018-01-16 10:26:49 -0800201 public SignalingMessageReceiver {
deadbeef1dcb1642017-03-29 21:08:16 -0700202 public:
203 // Different factory methods for convenience.
204 // TODO(deadbeef): Could use the pattern of:
205 //
206 // PeerConnectionWrapper =
207 // WrapperBuilder.WithConfig(...).WithOptions(...).build();
208 //
209 // To reduce some code duplication.
210 static PeerConnectionWrapper* CreateWithDtlsIdentityStore(
211 const std::string& debug_name,
212 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator,
213 rtc::Thread* network_thread,
214 rtc::Thread* worker_thread) {
215 PeerConnectionWrapper* client(new PeerConnectionWrapper(debug_name));
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700216 webrtc::PeerConnectionDependencies dependencies(nullptr);
217 dependencies.cert_generator = std::move(cert_generator);
Niels Möllerf06f9232018-08-07 12:32:18 +0200218 if (!client->Init(nullptr, nullptr, std::move(dependencies), network_thread,
Bjorn Mellem175aa2e2018-11-08 11:23:22 -0800219 worker_thread, nullptr,
220 /*media_transport_factory=*/nullptr)) {
deadbeef1dcb1642017-03-29 21:08:16 -0700221 delete client;
222 return nullptr;
223 }
224 return client;
225 }
226
deadbeef2f425aa2017-04-14 10:41:32 -0700227 webrtc::PeerConnectionFactoryInterface* pc_factory() const {
228 return peer_connection_factory_.get();
229 }
230
deadbeef1dcb1642017-03-29 21:08:16 -0700231 webrtc::PeerConnectionInterface* pc() const { return peer_connection_.get(); }
232
233 // If a signaling message receiver is set (via ConnectFakeSignaling), this
234 // will set the whole offer/answer exchange in motion. Just need to wait for
235 // the signaling state to reach "stable".
236 void CreateAndSetAndSignalOffer() {
237 auto offer = CreateOffer();
238 ASSERT_NE(nullptr, offer);
239 EXPECT_TRUE(SetLocalDescriptionAndSendSdpMessage(std::move(offer)));
240 }
241
242 // Sets the options to be used when CreateAndSetAndSignalOffer is called, or
243 // when a remote offer is received (via fake signaling) and an answer is
244 // generated. By default, uses default options.
245 void SetOfferAnswerOptions(
246 const PeerConnectionInterface::RTCOfferAnswerOptions& options) {
247 offer_answer_options_ = options;
248 }
249
250 // Set a callback to be invoked when SDP is received via the fake signaling
251 // channel, which provides an opportunity to munge (modify) the SDP. This is
252 // used to test SDP being applied that a PeerConnection would normally not
253 // generate, but a non-JSEP endpoint might.
254 void SetReceivedSdpMunger(
255 std::function<void(cricket::SessionDescription*)> munger) {
Mirko Bonadeic61ce0d2017-11-21 17:04:20 +0100256 received_sdp_munger_ = std::move(munger);
deadbeef1dcb1642017-03-29 21:08:16 -0700257 }
258
deadbeefc964d0b2017-04-03 10:03:35 -0700259 // Similar to the above, but this is run on SDP immediately after it's
deadbeef1dcb1642017-03-29 21:08:16 -0700260 // generated.
261 void SetGeneratedSdpMunger(
262 std::function<void(cricket::SessionDescription*)> munger) {
Mirko Bonadeic61ce0d2017-11-21 17:04:20 +0100263 generated_sdp_munger_ = std::move(munger);
deadbeef1dcb1642017-03-29 21:08:16 -0700264 }
265
Seth Hampson2f0d7022018-02-20 11:54:42 -0800266 // Set a callback to be invoked when a remote offer is received via the fake
267 // signaling channel. This provides an opportunity to change the
268 // PeerConnection state before an answer is created and sent to the caller.
269 void SetRemoteOfferHandler(std::function<void()> handler) {
270 remote_offer_handler_ = std::move(handler);
271 }
272
Qingsi Wang1dac6d82018-12-12 15:28:47 -0800273 void SetRemoteAsyncResolver(rtc::MockAsyncResolver* resolver) {
274 remote_async_resolver_ = resolver;
Zach Stein6fcdc2f2018-08-23 16:25:55 -0700275 }
276
Steve Antonede9ca52017-10-16 13:04:27 -0700277 // Every ICE connection state in order that has been seen by the observer.
278 std::vector<PeerConnectionInterface::IceConnectionState>
279 ice_connection_state_history() const {
280 return ice_connection_state_history_;
281 }
Steve Anton6f25b092017-10-23 09:39:20 -0700282 void clear_ice_connection_state_history() {
283 ice_connection_state_history_.clear();
284 }
Steve Antonede9ca52017-10-16 13:04:27 -0700285
Jonas Olssonacd8ae72019-02-25 15:26:24 +0100286 // Every standardized ICE connection state in order that has been seen by the
287 // observer.
288 std::vector<PeerConnectionInterface::IceConnectionState>
289 standardized_ice_connection_state_history() const {
290 return standardized_ice_connection_state_history_;
291 }
292
Jonas Olsson635474e2018-10-18 15:58:17 +0200293 // Every PeerConnection state in order that has been seen by the observer.
294 std::vector<PeerConnectionInterface::PeerConnectionState>
295 peer_connection_state_history() const {
296 return peer_connection_state_history_;
297 }
298
Steve Antonede9ca52017-10-16 13:04:27 -0700299 // Every ICE gathering state in order that has been seen by the observer.
300 std::vector<PeerConnectionInterface::IceGatheringState>
301 ice_gathering_state_history() const {
302 return ice_gathering_state_history_;
deadbeef1dcb1642017-03-29 21:08:16 -0700303 }
304
Steve Anton15324772018-01-16 10:26:49 -0800305 void AddAudioVideoTracks() {
306 AddAudioTrack();
307 AddVideoTrack();
deadbeef1dcb1642017-03-29 21:08:16 -0700308 }
309
Steve Anton74255ff2018-01-24 18:32:57 -0800310 rtc::scoped_refptr<RtpSenderInterface> AddAudioTrack() {
311 return AddTrack(CreateLocalAudioTrack());
312 }
deadbeef1dcb1642017-03-29 21:08:16 -0700313
Steve Anton74255ff2018-01-24 18:32:57 -0800314 rtc::scoped_refptr<RtpSenderInterface> AddVideoTrack() {
315 return AddTrack(CreateLocalVideoTrack());
316 }
deadbeef1dcb1642017-03-29 21:08:16 -0700317
318 rtc::scoped_refptr<webrtc::AudioTrackInterface> CreateLocalAudioTrack() {
Niels Möller2d02e082018-05-21 11:23:35 +0200319 cricket::AudioOptions options;
deadbeef1dcb1642017-03-29 21:08:16 -0700320 // Disable highpass filter so that we can get all the test audio frames.
Niels Möller2d02e082018-05-21 11:23:35 +0200321 options.highpass_filter = false;
deadbeef1dcb1642017-03-29 21:08:16 -0700322 rtc::scoped_refptr<webrtc::AudioSourceInterface> source =
Niels Möller2d02e082018-05-21 11:23:35 +0200323 peer_connection_factory_->CreateAudioSource(options);
deadbeef1dcb1642017-03-29 21:08:16 -0700324 // TODO(perkj): Test audio source when it is implemented. Currently audio
325 // always use the default input.
deadbeefb1a15d72017-09-07 14:12:05 -0700326 return peer_connection_factory_->CreateAudioTrack(rtc::CreateRandomUuid(),
deadbeef1dcb1642017-03-29 21:08:16 -0700327 source);
328 }
329
330 rtc::scoped_refptr<webrtc::VideoTrackInterface> CreateLocalVideoTrack() {
Johannes Kron965e7942018-09-13 15:36:20 +0200331 webrtc::FakePeriodicVideoSource::Config config;
332 config.timestamp_offset_ms = rtc::TimeMillis();
333 return CreateLocalVideoTrackInternal(config);
deadbeef1dcb1642017-03-29 21:08:16 -0700334 }
335
336 rtc::scoped_refptr<webrtc::VideoTrackInterface>
Niels Möller5c7efe72018-05-11 10:34:46 +0200337 CreateLocalVideoTrackWithConfig(
338 webrtc::FakePeriodicVideoSource::Config config) {
339 return CreateLocalVideoTrackInternal(config);
deadbeef1dcb1642017-03-29 21:08:16 -0700340 }
341
342 rtc::scoped_refptr<webrtc::VideoTrackInterface>
343 CreateLocalVideoTrackWithRotation(webrtc::VideoRotation rotation) {
Niels Möller5c7efe72018-05-11 10:34:46 +0200344 webrtc::FakePeriodicVideoSource::Config config;
345 config.rotation = rotation;
Johannes Kron965e7942018-09-13 15:36:20 +0200346 config.timestamp_offset_ms = rtc::TimeMillis();
Niels Möller5c7efe72018-05-11 10:34:46 +0200347 return CreateLocalVideoTrackInternal(config);
deadbeef1dcb1642017-03-29 21:08:16 -0700348 }
349
Steve Anton74255ff2018-01-24 18:32:57 -0800350 rtc::scoped_refptr<RtpSenderInterface> AddTrack(
351 rtc::scoped_refptr<MediaStreamTrackInterface> track,
Seth Hampson845e8782018-03-02 11:34:10 -0800352 const std::vector<std::string>& stream_ids = {}) {
353 auto result = pc()->AddTrack(track, stream_ids);
Steve Anton15324772018-01-16 10:26:49 -0800354 EXPECT_EQ(RTCErrorType::NONE, result.error().type());
Steve Anton74255ff2018-01-24 18:32:57 -0800355 return result.MoveValue();
356 }
357
358 std::vector<rtc::scoped_refptr<RtpReceiverInterface>> GetReceiversOfType(
359 cricket::MediaType media_type) {
360 std::vector<rtc::scoped_refptr<RtpReceiverInterface>> receivers;
Mirko Bonadei739baf02019-01-27 17:29:42 +0100361 for (const auto& receiver : pc()->GetReceivers()) {
Steve Anton74255ff2018-01-24 18:32:57 -0800362 if (receiver->media_type() == media_type) {
363 receivers.push_back(receiver);
364 }
365 }
366 return receivers;
deadbeef1dcb1642017-03-29 21:08:16 -0700367 }
368
Seth Hampson2f0d7022018-02-20 11:54:42 -0800369 rtc::scoped_refptr<RtpTransceiverInterface> GetFirstTransceiverOfType(
370 cricket::MediaType media_type) {
371 for (auto transceiver : pc()->GetTransceivers()) {
372 if (transceiver->receiver()->media_type() == media_type) {
373 return transceiver;
374 }
375 }
376 return nullptr;
377 }
378
deadbeef1dcb1642017-03-29 21:08:16 -0700379 bool SignalingStateStable() {
380 return pc()->signaling_state() == webrtc::PeerConnectionInterface::kStable;
381 }
382
383 void CreateDataChannel() { CreateDataChannel(nullptr); }
384
385 void CreateDataChannel(const webrtc::DataChannelInit* init) {
Steve Antonda6c0952017-10-23 11:41:54 -0700386 CreateDataChannel(kDataChannelLabel, init);
387 }
388
389 void CreateDataChannel(const std::string& label,
390 const webrtc::DataChannelInit* init) {
391 data_channel_ = pc()->CreateDataChannel(label, init);
deadbeef1dcb1642017-03-29 21:08:16 -0700392 ASSERT_TRUE(data_channel_.get() != nullptr);
393 data_observer_.reset(new MockDataChannelObserver(data_channel_));
394 }
395
396 DataChannelInterface* data_channel() { return data_channel_; }
397 const MockDataChannelObserver* data_observer() const {
398 return data_observer_.get();
399 }
400
401 int audio_frames_received() const {
402 return fake_audio_capture_module_->frames_received();
403 }
404
405 // Takes minimum of video frames received for each track.
406 //
407 // Can be used like:
408 // EXPECT_GE(expected_frames, min_video_frames_received_per_track());
409 //
410 // To ensure that all video tracks received at least a certain number of
411 // frames.
412 int min_video_frames_received_per_track() const {
413 int min_frames = INT_MAX;
Anders Carlsson5f2bb622018-05-14 09:48:06 +0200414 if (fake_video_renderers_.empty()) {
415 return 0;
deadbeef1dcb1642017-03-29 21:08:16 -0700416 }
deadbeef1dcb1642017-03-29 21:08:16 -0700417
Anders Carlsson5f2bb622018-05-14 09:48:06 +0200418 for (const auto& pair : fake_video_renderers_) {
419 min_frames = std::min(min_frames, pair.second->num_rendered_frames());
deadbeef1dcb1642017-03-29 21:08:16 -0700420 }
Anders Carlsson5f2bb622018-05-14 09:48:06 +0200421 return min_frames;
deadbeef1dcb1642017-03-29 21:08:16 -0700422 }
423
424 // Returns a MockStatsObserver in a state after stats gathering finished,
425 // which can be used to access the gathered stats.
deadbeefd8ad7882017-04-18 16:01:17 -0700426 rtc::scoped_refptr<MockStatsObserver> OldGetStatsForTrack(
deadbeef1dcb1642017-03-29 21:08:16 -0700427 webrtc::MediaStreamTrackInterface* track) {
428 rtc::scoped_refptr<MockStatsObserver> observer(
429 new rtc::RefCountedObject<MockStatsObserver>());
430 EXPECT_TRUE(peer_connection_->GetStats(
431 observer, nullptr, PeerConnectionInterface::kStatsOutputLevelStandard));
432 EXPECT_TRUE_WAIT(observer->called(), kDefaultTimeout);
433 return observer;
434 }
435
436 // Version that doesn't take a track "filter", and gathers all stats.
deadbeefd8ad7882017-04-18 16:01:17 -0700437 rtc::scoped_refptr<MockStatsObserver> OldGetStats() {
438 return OldGetStatsForTrack(nullptr);
439 }
440
441 // Synchronously gets stats and returns them. If it times out, fails the test
442 // and returns null.
443 rtc::scoped_refptr<const webrtc::RTCStatsReport> NewGetStats() {
444 rtc::scoped_refptr<webrtc::MockRTCStatsCollectorCallback> callback(
445 new rtc::RefCountedObject<webrtc::MockRTCStatsCollectorCallback>());
446 peer_connection_->GetStats(callback);
447 EXPECT_TRUE_WAIT(callback->called(), kDefaultTimeout);
448 return callback->report();
deadbeef1dcb1642017-03-29 21:08:16 -0700449 }
450
451 int rendered_width() {
452 EXPECT_FALSE(fake_video_renderers_.empty());
453 return fake_video_renderers_.empty()
454 ? 0
455 : fake_video_renderers_.begin()->second->width();
456 }
457
458 int rendered_height() {
459 EXPECT_FALSE(fake_video_renderers_.empty());
460 return fake_video_renderers_.empty()
461 ? 0
462 : fake_video_renderers_.begin()->second->height();
463 }
464
465 double rendered_aspect_ratio() {
466 if (rendered_height() == 0) {
467 return 0.0;
468 }
469 return static_cast<double>(rendered_width()) / rendered_height();
470 }
471
472 webrtc::VideoRotation rendered_rotation() {
473 EXPECT_FALSE(fake_video_renderers_.empty());
474 return fake_video_renderers_.empty()
475 ? webrtc::kVideoRotation_0
476 : fake_video_renderers_.begin()->second->rotation();
477 }
478
479 int local_rendered_width() {
480 return local_video_renderer_ ? local_video_renderer_->width() : 0;
481 }
482
483 int local_rendered_height() {
484 return local_video_renderer_ ? local_video_renderer_->height() : 0;
485 }
486
487 double local_rendered_aspect_ratio() {
488 if (local_rendered_height() == 0) {
489 return 0.0;
490 }
491 return static_cast<double>(local_rendered_width()) /
492 local_rendered_height();
493 }
494
495 size_t number_of_remote_streams() {
496 if (!pc()) {
497 return 0;
498 }
499 return pc()->remote_streams()->count();
500 }
501
502 StreamCollectionInterface* remote_streams() const {
503 if (!pc()) {
504 ADD_FAILURE();
505 return nullptr;
506 }
507 return pc()->remote_streams();
508 }
509
510 StreamCollectionInterface* local_streams() {
511 if (!pc()) {
512 ADD_FAILURE();
513 return nullptr;
514 }
515 return pc()->local_streams();
516 }
517
518 webrtc::PeerConnectionInterface::SignalingState signaling_state() {
519 return pc()->signaling_state();
520 }
521
522 webrtc::PeerConnectionInterface::IceConnectionState ice_connection_state() {
523 return pc()->ice_connection_state();
524 }
525
Jonas Olsson7a6739e2019-01-15 16:31:55 +0100526 webrtc::PeerConnectionInterface::IceConnectionState
527 standardized_ice_connection_state() {
528 return pc()->standardized_ice_connection_state();
529 }
530
deadbeef1dcb1642017-03-29 21:08:16 -0700531 webrtc::PeerConnectionInterface::IceGatheringState ice_gathering_state() {
532 return pc()->ice_gathering_state();
533 }
534
535 // Returns a MockRtpReceiverObserver for each RtpReceiver returned by
536 // GetReceivers. They're updated automatically when a remote offer/answer
537 // from the fake signaling channel is applied, or when
538 // ResetRtpReceiverObservers below is called.
539 const std::vector<std::unique_ptr<MockRtpReceiverObserver>>&
540 rtp_receiver_observers() {
541 return rtp_receiver_observers_;
542 }
543
544 void ResetRtpReceiverObservers() {
545 rtp_receiver_observers_.clear();
Mirko Bonadeic61ce0d2017-11-21 17:04:20 +0100546 for (const rtc::scoped_refptr<RtpReceiverInterface>& receiver :
547 pc()->GetReceivers()) {
deadbeef1dcb1642017-03-29 21:08:16 -0700548 std::unique_ptr<MockRtpReceiverObserver> observer(
549 new MockRtpReceiverObserver(receiver->media_type()));
550 receiver->SetObserver(observer.get());
551 rtp_receiver_observers_.push_back(std::move(observer));
552 }
553 }
554
Steve Antonede9ca52017-10-16 13:04:27 -0700555 rtc::FakeNetworkManager* network() const {
556 return fake_network_manager_.get();
557 }
558 cricket::PortAllocator* port_allocator() const { return port_allocator_; }
559
Qingsi Wang7685e862018-06-11 20:15:46 -0700560 webrtc::FakeRtcEventLogFactory* event_log_factory() const {
561 return event_log_factory_;
562 }
563
Qingsi Wangc129c352019-04-18 10:41:58 -0700564 const cricket::Candidate& last_candidate_gathered() const {
565 return last_candidate_gathered_;
566 }
567
deadbeef1dcb1642017-03-29 21:08:16 -0700568 private:
569 explicit PeerConnectionWrapper(const std::string& debug_name)
570 : debug_name_(debug_name) {}
571
Bjorn Mellem175aa2e2018-11-08 11:23:22 -0800572 bool Init(
573 const PeerConnectionFactory::Options* options,
574 const PeerConnectionInterface::RTCConfiguration* config,
575 webrtc::PeerConnectionDependencies dependencies,
576 rtc::Thread* network_thread,
577 rtc::Thread* worker_thread,
578 std::unique_ptr<webrtc::FakeRtcEventLogFactory> event_log_factory,
579 std::unique_ptr<webrtc::MediaTransportFactory> media_transport_factory) {
deadbeef1dcb1642017-03-29 21:08:16 -0700580 // There's an error in this test code if Init ends up being called twice.
581 RTC_DCHECK(!peer_connection_);
582 RTC_DCHECK(!peer_connection_factory_);
583
584 fake_network_manager_.reset(new rtc::FakeNetworkManager());
Steve Antonede9ca52017-10-16 13:04:27 -0700585 fake_network_manager_->AddInterface(kDefaultLocalAddress);
deadbeef1dcb1642017-03-29 21:08:16 -0700586
587 std::unique_ptr<cricket::PortAllocator> port_allocator(
588 new cricket::BasicPortAllocator(fake_network_manager_.get()));
Steve Antonede9ca52017-10-16 13:04:27 -0700589 port_allocator_ = port_allocator.get();
deadbeef1dcb1642017-03-29 21:08:16 -0700590 fake_audio_capture_module_ = FakeAudioCaptureModule::Create();
591 if (!fake_audio_capture_module_) {
592 return false;
593 }
deadbeef1dcb1642017-03-29 21:08:16 -0700594 rtc::Thread* const signaling_thread = rtc::Thread::Current();
Qingsi Wang7685e862018-06-11 20:15:46 -0700595
596 webrtc::PeerConnectionFactoryDependencies pc_factory_dependencies;
597 pc_factory_dependencies.network_thread = network_thread;
598 pc_factory_dependencies.worker_thread = worker_thread;
599 pc_factory_dependencies.signaling_thread = signaling_thread;
600 pc_factory_dependencies.media_engine =
601 cricket::WebRtcMediaEngineFactory::Create(
602 rtc::scoped_refptr<webrtc::AudioDeviceModule>(
603 fake_audio_capture_module_),
604 webrtc::CreateBuiltinAudioEncoderFactory(),
605 webrtc::CreateBuiltinAudioDecoderFactory(),
606 webrtc::CreateBuiltinVideoEncoderFactory(),
Qingsi Wang59844ce2018-11-01 04:45:53 +0000607 webrtc::CreateBuiltinVideoDecoderFactory(), nullptr,
Qingsi Wang7685e862018-06-11 20:15:46 -0700608 webrtc::AudioProcessingBuilder().Create());
609 pc_factory_dependencies.call_factory = webrtc::CreateCallFactory();
610 if (event_log_factory) {
611 event_log_factory_ = event_log_factory.get();
612 pc_factory_dependencies.event_log_factory = std::move(event_log_factory);
613 } else {
614 pc_factory_dependencies.event_log_factory =
615 webrtc::CreateRtcEventLogFactory();
616 }
Bjorn Mellem175aa2e2018-11-08 11:23:22 -0800617 if (media_transport_factory) {
618 pc_factory_dependencies.media_transport_factory =
619 std::move(media_transport_factory);
620 }
Qingsi Wang7685e862018-06-11 20:15:46 -0700621 peer_connection_factory_ = webrtc::CreateModularPeerConnectionFactory(
622 std::move(pc_factory_dependencies));
623
deadbeef1dcb1642017-03-29 21:08:16 -0700624 if (!peer_connection_factory_) {
625 return false;
626 }
627 if (options) {
628 peer_connection_factory_->SetOptions(*options);
629 }
Seth Hampson2f0d7022018-02-20 11:54:42 -0800630 if (config) {
631 sdp_semantics_ = config->sdp_semantics;
632 }
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700633
634 dependencies.allocator = std::move(port_allocator);
Niels Möllerf06f9232018-08-07 12:32:18 +0200635 peer_connection_ = CreatePeerConnection(config, std::move(dependencies));
deadbeef1dcb1642017-03-29 21:08:16 -0700636 return peer_connection_.get() != nullptr;
637 }
638
639 rtc::scoped_refptr<webrtc::PeerConnectionInterface> CreatePeerConnection(
deadbeef1dcb1642017-03-29 21:08:16 -0700640 const PeerConnectionInterface::RTCConfiguration* config,
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700641 webrtc::PeerConnectionDependencies dependencies) {
deadbeef1dcb1642017-03-29 21:08:16 -0700642 PeerConnectionInterface::RTCConfiguration modified_config;
643 // If |config| is null, this will result in a default configuration being
644 // used.
645 if (config) {
646 modified_config = *config;
647 }
648 // Disable resolution adaptation; we don't want it interfering with the
649 // test results.
650 // TODO(deadbeef): Do something more robust. Since we're testing for aspect
651 // ratios and not specific resolutions, is this even necessary?
652 modified_config.set_cpu_adaptation(false);
653
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700654 dependencies.observer = this;
deadbeef1dcb1642017-03-29 21:08:16 -0700655 return peer_connection_factory_->CreatePeerConnection(
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700656 modified_config, std::move(dependencies));
deadbeef1dcb1642017-03-29 21:08:16 -0700657 }
658
659 void set_signaling_message_receiver(
660 SignalingMessageReceiver* signaling_message_receiver) {
661 signaling_message_receiver_ = signaling_message_receiver;
662 }
663
664 void set_signaling_delay_ms(int delay_ms) { signaling_delay_ms_ = delay_ms; }
665
Steve Antonede9ca52017-10-16 13:04:27 -0700666 void set_signal_ice_candidates(bool signal) {
667 signal_ice_candidates_ = signal;
668 }
669
deadbeef1dcb1642017-03-29 21:08:16 -0700670 rtc::scoped_refptr<webrtc::VideoTrackInterface> CreateLocalVideoTrackInternal(
Niels Möller5c7efe72018-05-11 10:34:46 +0200671 webrtc::FakePeriodicVideoSource::Config config) {
deadbeef1dcb1642017-03-29 21:08:16 -0700672 // Set max frame rate to 10fps to reduce the risk of test flakiness.
673 // TODO(deadbeef): Do something more robust.
Niels Möller5c7efe72018-05-11 10:34:46 +0200674 config.frame_interval_ms = 100;
deadbeef1dcb1642017-03-29 21:08:16 -0700675
Niels Möller5c7efe72018-05-11 10:34:46 +0200676 video_track_sources_.emplace_back(
Niels Möller0f405822018-05-17 09:16:41 +0200677 new rtc::RefCountedObject<webrtc::FakePeriodicVideoTrackSource>(
678 config, false /* remote */));
deadbeef1dcb1642017-03-29 21:08:16 -0700679 rtc::scoped_refptr<webrtc::VideoTrackInterface> track(
Niels Möller5c7efe72018-05-11 10:34:46 +0200680 peer_connection_factory_->CreateVideoTrack(
681 rtc::CreateRandomUuid(), video_track_sources_.back()));
deadbeef1dcb1642017-03-29 21:08:16 -0700682 if (!local_video_renderer_) {
683 local_video_renderer_.reset(new webrtc::FakeVideoTrackRenderer(track));
684 }
685 return track;
686 }
687
688 void HandleIncomingOffer(const std::string& msg) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100689 RTC_LOG(LS_INFO) << debug_name_ << ": HandleIncomingOffer";
Steve Antona3a92c22017-12-07 10:27:41 -0800690 std::unique_ptr<SessionDescriptionInterface> desc =
691 webrtc::CreateSessionDescription(SdpType::kOffer, msg);
deadbeef1dcb1642017-03-29 21:08:16 -0700692 if (received_sdp_munger_) {
693 received_sdp_munger_(desc->description());
694 }
695
696 EXPECT_TRUE(SetRemoteDescription(std::move(desc)));
697 // Setting a remote description may have changed the number of receivers,
698 // so reset the receiver observers.
699 ResetRtpReceiverObservers();
Seth Hampson2f0d7022018-02-20 11:54:42 -0800700 if (remote_offer_handler_) {
701 remote_offer_handler_();
702 }
deadbeef1dcb1642017-03-29 21:08:16 -0700703 auto answer = CreateAnswer();
704 ASSERT_NE(nullptr, answer);
705 EXPECT_TRUE(SetLocalDescriptionAndSendSdpMessage(std::move(answer)));
706 }
707
708 void HandleIncomingAnswer(const std::string& msg) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100709 RTC_LOG(LS_INFO) << debug_name_ << ": HandleIncomingAnswer";
Steve Antona3a92c22017-12-07 10:27:41 -0800710 std::unique_ptr<SessionDescriptionInterface> desc =
711 webrtc::CreateSessionDescription(SdpType::kAnswer, msg);
deadbeef1dcb1642017-03-29 21:08:16 -0700712 if (received_sdp_munger_) {
713 received_sdp_munger_(desc->description());
714 }
715
716 EXPECT_TRUE(SetRemoteDescription(std::move(desc)));
717 // Set the RtpReceiverObserver after receivers are created.
718 ResetRtpReceiverObservers();
719 }
720
721 // Returns null on failure.
722 std::unique_ptr<SessionDescriptionInterface> CreateOffer() {
723 rtc::scoped_refptr<MockCreateSessionDescriptionObserver> observer(
724 new rtc::RefCountedObject<MockCreateSessionDescriptionObserver>());
725 pc()->CreateOffer(observer, offer_answer_options_);
726 return WaitForDescriptionFromObserver(observer);
727 }
728
729 // Returns null on failure.
730 std::unique_ptr<SessionDescriptionInterface> CreateAnswer() {
731 rtc::scoped_refptr<MockCreateSessionDescriptionObserver> observer(
732 new rtc::RefCountedObject<MockCreateSessionDescriptionObserver>());
733 pc()->CreateAnswer(observer, offer_answer_options_);
734 return WaitForDescriptionFromObserver(observer);
735 }
736
737 std::unique_ptr<SessionDescriptionInterface> WaitForDescriptionFromObserver(
Mirko Bonadeic61ce0d2017-11-21 17:04:20 +0100738 MockCreateSessionDescriptionObserver* observer) {
deadbeef1dcb1642017-03-29 21:08:16 -0700739 EXPECT_EQ_WAIT(true, observer->called(), kDefaultTimeout);
740 if (!observer->result()) {
741 return nullptr;
742 }
743 auto description = observer->MoveDescription();
744 if (generated_sdp_munger_) {
745 generated_sdp_munger_(description->description());
746 }
747 return description;
748 }
749
750 // Setting the local description and sending the SDP message over the fake
751 // signaling channel are combined into the same method because the SDP
752 // message needs to be sent as soon as SetLocalDescription finishes, without
753 // waiting for the observer to be called. This ensures that ICE candidates
754 // don't outrace the description.
755 bool SetLocalDescriptionAndSendSdpMessage(
756 std::unique_ptr<SessionDescriptionInterface> desc) {
757 rtc::scoped_refptr<MockSetSessionDescriptionObserver> observer(
758 new rtc::RefCountedObject<MockSetSessionDescriptionObserver>());
Mirko Bonadei675513b2017-11-09 11:09:25 +0100759 RTC_LOG(LS_INFO) << debug_name_ << ": SetLocalDescriptionAndSendSdpMessage";
Steve Antona3a92c22017-12-07 10:27:41 -0800760 SdpType type = desc->GetType();
deadbeef1dcb1642017-03-29 21:08:16 -0700761 std::string sdp;
762 EXPECT_TRUE(desc->ToString(&sdp));
763 pc()->SetLocalDescription(observer, desc.release());
Seth Hampson2f0d7022018-02-20 11:54:42 -0800764 if (sdp_semantics_ == SdpSemantics::kUnifiedPlan) {
765 RemoveUnusedVideoRenderers();
766 }
deadbeef1dcb1642017-03-29 21:08:16 -0700767 // As mentioned above, we need to send the message immediately after
768 // SetLocalDescription.
769 SendSdpMessage(type, sdp);
770 EXPECT_TRUE_WAIT(observer->called(), kDefaultTimeout);
771 return true;
772 }
773
774 bool SetRemoteDescription(std::unique_ptr<SessionDescriptionInterface> desc) {
775 rtc::scoped_refptr<MockSetSessionDescriptionObserver> observer(
776 new rtc::RefCountedObject<MockSetSessionDescriptionObserver>());
Mirko Bonadei675513b2017-11-09 11:09:25 +0100777 RTC_LOG(LS_INFO) << debug_name_ << ": SetRemoteDescription";
deadbeef1dcb1642017-03-29 21:08:16 -0700778 pc()->SetRemoteDescription(observer, desc.release());
Seth Hampson2f0d7022018-02-20 11:54:42 -0800779 if (sdp_semantics_ == SdpSemantics::kUnifiedPlan) {
780 RemoveUnusedVideoRenderers();
781 }
deadbeef1dcb1642017-03-29 21:08:16 -0700782 EXPECT_TRUE_WAIT(observer->called(), kDefaultTimeout);
783 return observer->result();
784 }
785
Seth Hampson2f0d7022018-02-20 11:54:42 -0800786 // This is a work around to remove unused fake_video_renderers from
787 // transceivers that have either stopped or are no longer receiving.
788 void RemoveUnusedVideoRenderers() {
789 auto transceivers = pc()->GetTransceivers();
790 for (auto& transceiver : transceivers) {
791 if (transceiver->receiver()->media_type() != cricket::MEDIA_TYPE_VIDEO) {
792 continue;
793 }
794 // Remove fake video renderers from any stopped transceivers.
795 if (transceiver->stopped()) {
796 auto it =
797 fake_video_renderers_.find(transceiver->receiver()->track()->id());
798 if (it != fake_video_renderers_.end()) {
799 fake_video_renderers_.erase(it);
800 }
801 }
802 // Remove fake video renderers from any transceivers that are no longer
803 // receiving.
804 if ((transceiver->current_direction() &&
805 !webrtc::RtpTransceiverDirectionHasRecv(
806 *transceiver->current_direction()))) {
807 auto it =
808 fake_video_renderers_.find(transceiver->receiver()->track()->id());
809 if (it != fake_video_renderers_.end()) {
810 fake_video_renderers_.erase(it);
811 }
812 }
813 }
814 }
815
deadbeef1dcb1642017-03-29 21:08:16 -0700816 // Simulate sending a blob of SDP with delay |signaling_delay_ms_| (0 by
817 // default).
Steve Antona3a92c22017-12-07 10:27:41 -0800818 void SendSdpMessage(SdpType type, const std::string& msg) {
deadbeef1dcb1642017-03-29 21:08:16 -0700819 if (signaling_delay_ms_ == 0) {
820 RelaySdpMessageIfReceiverExists(type, msg);
821 } else {
822 invoker_.AsyncInvokeDelayed<void>(
823 RTC_FROM_HERE, rtc::Thread::Current(),
824 rtc::Bind(&PeerConnectionWrapper::RelaySdpMessageIfReceiverExists,
825 this, type, msg),
826 signaling_delay_ms_);
827 }
828 }
829
Steve Antona3a92c22017-12-07 10:27:41 -0800830 void RelaySdpMessageIfReceiverExists(SdpType type, const std::string& msg) {
deadbeef1dcb1642017-03-29 21:08:16 -0700831 if (signaling_message_receiver_) {
832 signaling_message_receiver_->ReceiveSdpMessage(type, msg);
833 }
834 }
835
836 // Simulate trickling an ICE candidate with delay |signaling_delay_ms_| (0 by
837 // default).
838 void SendIceMessage(const std::string& sdp_mid,
839 int sdp_mline_index,
840 const std::string& msg) {
841 if (signaling_delay_ms_ == 0) {
842 RelayIceMessageIfReceiverExists(sdp_mid, sdp_mline_index, msg);
843 } else {
844 invoker_.AsyncInvokeDelayed<void>(
845 RTC_FROM_HERE, rtc::Thread::Current(),
846 rtc::Bind(&PeerConnectionWrapper::RelayIceMessageIfReceiverExists,
847 this, sdp_mid, sdp_mline_index, msg),
848 signaling_delay_ms_);
849 }
850 }
851
852 void RelayIceMessageIfReceiverExists(const std::string& sdp_mid,
853 int sdp_mline_index,
854 const std::string& msg) {
855 if (signaling_message_receiver_) {
856 signaling_message_receiver_->ReceiveIceMessage(sdp_mid, sdp_mline_index,
857 msg);
858 }
859 }
860
861 // SignalingMessageReceiver callbacks.
Steve Antona3a92c22017-12-07 10:27:41 -0800862 void ReceiveSdpMessage(SdpType type, const std::string& msg) override {
863 if (type == SdpType::kOffer) {
deadbeef1dcb1642017-03-29 21:08:16 -0700864 HandleIncomingOffer(msg);
865 } else {
866 HandleIncomingAnswer(msg);
867 }
868 }
869
870 void ReceiveIceMessage(const std::string& sdp_mid,
871 int sdp_mline_index,
872 const std::string& msg) override {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100873 RTC_LOG(LS_INFO) << debug_name_ << ": ReceiveIceMessage";
deadbeef1dcb1642017-03-29 21:08:16 -0700874 std::unique_ptr<webrtc::IceCandidateInterface> candidate(
875 webrtc::CreateIceCandidate(sdp_mid, sdp_mline_index, msg, nullptr));
876 EXPECT_TRUE(pc()->AddIceCandidate(candidate.get()));
877 }
878
879 // PeerConnectionObserver callbacks.
880 void OnSignalingChange(
881 webrtc::PeerConnectionInterface::SignalingState new_state) override {
882 EXPECT_EQ(pc()->signaling_state(), new_state);
883 }
Steve Anton15324772018-01-16 10:26:49 -0800884 void OnAddTrack(rtc::scoped_refptr<RtpReceiverInterface> receiver,
885 const std::vector<rtc::scoped_refptr<MediaStreamInterface>>&
886 streams) override {
887 if (receiver->media_type() == cricket::MEDIA_TYPE_VIDEO) {
888 rtc::scoped_refptr<VideoTrackInterface> video_track(
889 static_cast<VideoTrackInterface*>(receiver->track().get()));
890 ASSERT_TRUE(fake_video_renderers_.find(video_track->id()) ==
deadbeef1dcb1642017-03-29 21:08:16 -0700891 fake_video_renderers_.end());
Steve Anton15324772018-01-16 10:26:49 -0800892 fake_video_renderers_[video_track->id()] =
Karl Wiberg918f50c2018-07-05 11:40:33 +0200893 absl::make_unique<FakeVideoTrackRenderer>(video_track);
deadbeef1dcb1642017-03-29 21:08:16 -0700894 }
895 }
Steve Anton15324772018-01-16 10:26:49 -0800896 void OnRemoveTrack(
897 rtc::scoped_refptr<RtpReceiverInterface> receiver) override {
898 if (receiver->media_type() == cricket::MEDIA_TYPE_VIDEO) {
899 auto it = fake_video_renderers_.find(receiver->track()->id());
900 RTC_DCHECK(it != fake_video_renderers_.end());
901 fake_video_renderers_.erase(it);
902 }
903 }
deadbeef1dcb1642017-03-29 21:08:16 -0700904 void OnRenegotiationNeeded() override {}
905 void OnIceConnectionChange(
906 webrtc::PeerConnectionInterface::IceConnectionState new_state) override {
907 EXPECT_EQ(pc()->ice_connection_state(), new_state);
Steve Antonede9ca52017-10-16 13:04:27 -0700908 ice_connection_state_history_.push_back(new_state);
deadbeef1dcb1642017-03-29 21:08:16 -0700909 }
Jonas Olssonacd8ae72019-02-25 15:26:24 +0100910 void OnStandardizedIceConnectionChange(
911 webrtc::PeerConnectionInterface::IceConnectionState new_state) override {
912 standardized_ice_connection_state_history_.push_back(new_state);
913 }
Jonas Olsson635474e2018-10-18 15:58:17 +0200914 void OnConnectionChange(
915 webrtc::PeerConnectionInterface::PeerConnectionState new_state) override {
916 peer_connection_state_history_.push_back(new_state);
917 }
918
deadbeef1dcb1642017-03-29 21:08:16 -0700919 void OnIceGatheringChange(
920 webrtc::PeerConnectionInterface::IceGatheringState new_state) override {
deadbeef1dcb1642017-03-29 21:08:16 -0700921 EXPECT_EQ(pc()->ice_gathering_state(), new_state);
Steve Antonede9ca52017-10-16 13:04:27 -0700922 ice_gathering_state_history_.push_back(new_state);
deadbeef1dcb1642017-03-29 21:08:16 -0700923 }
924 void OnIceCandidate(const webrtc::IceCandidateInterface* candidate) override {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100925 RTC_LOG(LS_INFO) << debug_name_ << ": OnIceCandidate";
deadbeef1dcb1642017-03-29 21:08:16 -0700926
Qingsi Wang1dac6d82018-12-12 15:28:47 -0800927 if (remote_async_resolver_) {
928 const auto& local_candidate = candidate->candidate();
929 const auto& mdns_responder = network()->GetMdnsResponderForTesting();
930 if (local_candidate.address().IsUnresolvedIP()) {
931 RTC_DCHECK(local_candidate.type() == cricket::LOCAL_PORT_TYPE);
932 rtc::SocketAddress resolved_addr(local_candidate.address());
933 const auto resolved_ip = mdns_responder->GetMappedAddressForName(
934 local_candidate.address().hostname());
935 RTC_DCHECK(!resolved_ip.IsNil());
936 resolved_addr.SetResolvedIP(resolved_ip);
937 EXPECT_CALL(*remote_async_resolver_, GetResolvedAddress(_, _))
938 .WillOnce(DoAll(SetArgPointee<1>(resolved_addr), Return(true)));
939 EXPECT_CALL(*remote_async_resolver_, Destroy(_));
Zach Stein6fcdc2f2018-08-23 16:25:55 -0700940 }
Zach Stein6fcdc2f2018-08-23 16:25:55 -0700941 }
942
deadbeef1dcb1642017-03-29 21:08:16 -0700943 std::string ice_sdp;
Qingsi Wang1dac6d82018-12-12 15:28:47 -0800944 EXPECT_TRUE(candidate->ToString(&ice_sdp));
Steve Antonede9ca52017-10-16 13:04:27 -0700945 if (signaling_message_receiver_ == nullptr || !signal_ice_candidates_) {
deadbeef1dcb1642017-03-29 21:08:16 -0700946 // Remote party may be deleted.
947 return;
948 }
Qingsi Wang1dac6d82018-12-12 15:28:47 -0800949 SendIceMessage(candidate->sdp_mid(), candidate->sdp_mline_index(), ice_sdp);
Qingsi Wangc129c352019-04-18 10:41:58 -0700950 last_candidate_gathered_ = candidate->candidate();
deadbeef1dcb1642017-03-29 21:08:16 -0700951 }
952 void OnDataChannel(
953 rtc::scoped_refptr<DataChannelInterface> data_channel) override {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100954 RTC_LOG(LS_INFO) << debug_name_ << ": OnDataChannel";
deadbeef1dcb1642017-03-29 21:08:16 -0700955 data_channel_ = data_channel;
956 data_observer_.reset(new MockDataChannelObserver(data_channel));
957 }
958
deadbeef1dcb1642017-03-29 21:08:16 -0700959 std::string debug_name_;
960
961 std::unique_ptr<rtc::FakeNetworkManager> fake_network_manager_;
962
963 rtc::scoped_refptr<webrtc::PeerConnectionInterface> peer_connection_;
964 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface>
965 peer_connection_factory_;
966
Steve Antonede9ca52017-10-16 13:04:27 -0700967 cricket::PortAllocator* port_allocator_;
deadbeef1dcb1642017-03-29 21:08:16 -0700968 // Needed to keep track of number of frames sent.
969 rtc::scoped_refptr<FakeAudioCaptureModule> fake_audio_capture_module_;
970 // Needed to keep track of number of frames received.
971 std::map<std::string, std::unique_ptr<webrtc::FakeVideoTrackRenderer>>
972 fake_video_renderers_;
973 // Needed to ensure frames aren't received for removed tracks.
974 std::vector<std::unique_ptr<webrtc::FakeVideoTrackRenderer>>
975 removed_fake_video_renderers_;
deadbeef1dcb1642017-03-29 21:08:16 -0700976
977 // For remote peer communication.
978 SignalingMessageReceiver* signaling_message_receiver_ = nullptr;
979 int signaling_delay_ms_ = 0;
Steve Antonede9ca52017-10-16 13:04:27 -0700980 bool signal_ice_candidates_ = true;
Qingsi Wangc129c352019-04-18 10:41:58 -0700981 cricket::Candidate last_candidate_gathered_;
deadbeef1dcb1642017-03-29 21:08:16 -0700982
Niels Möller5c7efe72018-05-11 10:34:46 +0200983 // Store references to the video sources we've created, so that we can stop
deadbeef1dcb1642017-03-29 21:08:16 -0700984 // them, if required.
Niels Möller5c7efe72018-05-11 10:34:46 +0200985 std::vector<rtc::scoped_refptr<webrtc::VideoTrackSource>>
986 video_track_sources_;
deadbeef1dcb1642017-03-29 21:08:16 -0700987 // |local_video_renderer_| attached to the first created local video track.
988 std::unique_ptr<webrtc::FakeVideoTrackRenderer> local_video_renderer_;
989
Seth Hampson2f0d7022018-02-20 11:54:42 -0800990 SdpSemantics sdp_semantics_;
deadbeef1dcb1642017-03-29 21:08:16 -0700991 PeerConnectionInterface::RTCOfferAnswerOptions offer_answer_options_;
992 std::function<void(cricket::SessionDescription*)> received_sdp_munger_;
993 std::function<void(cricket::SessionDescription*)> generated_sdp_munger_;
Seth Hampson2f0d7022018-02-20 11:54:42 -0800994 std::function<void()> remote_offer_handler_;
Qingsi Wang1dac6d82018-12-12 15:28:47 -0800995 rtc::MockAsyncResolver* remote_async_resolver_ = nullptr;
deadbeef1dcb1642017-03-29 21:08:16 -0700996 rtc::scoped_refptr<DataChannelInterface> data_channel_;
997 std::unique_ptr<MockDataChannelObserver> data_observer_;
998
999 std::vector<std::unique_ptr<MockRtpReceiverObserver>> rtp_receiver_observers_;
1000
Steve Antonede9ca52017-10-16 13:04:27 -07001001 std::vector<PeerConnectionInterface::IceConnectionState>
1002 ice_connection_state_history_;
Jonas Olssonacd8ae72019-02-25 15:26:24 +01001003 std::vector<PeerConnectionInterface::IceConnectionState>
1004 standardized_ice_connection_state_history_;
Jonas Olsson635474e2018-10-18 15:58:17 +02001005 std::vector<PeerConnectionInterface::PeerConnectionState>
1006 peer_connection_state_history_;
Steve Antonede9ca52017-10-16 13:04:27 -07001007 std::vector<PeerConnectionInterface::IceGatheringState>
1008 ice_gathering_state_history_;
deadbeef1dcb1642017-03-29 21:08:16 -07001009
Qingsi Wang7685e862018-06-11 20:15:46 -07001010 webrtc::FakeRtcEventLogFactory* event_log_factory_;
1011
deadbeef1dcb1642017-03-29 21:08:16 -07001012 rtc::AsyncInvoker invoker_;
1013
Seth Hampson2f0d7022018-02-20 11:54:42 -08001014 friend class PeerConnectionIntegrationBaseTest;
deadbeef1dcb1642017-03-29 21:08:16 -07001015};
1016
Elad Alon99c3fe52017-10-13 16:29:40 +02001017class MockRtcEventLogOutput : public webrtc::RtcEventLogOutput {
1018 public:
1019 virtual ~MockRtcEventLogOutput() = default;
1020 MOCK_CONST_METHOD0(IsActive, bool());
1021 MOCK_METHOD1(Write, bool(const std::string&));
1022};
1023
Seth Hampson2f0d7022018-02-20 11:54:42 -08001024// This helper object is used for both specifying how many audio/video frames
1025// are expected to be received for a caller/callee. It provides helper functions
1026// to specify these expectations. The object initially starts in a state of no
1027// expectations.
1028class MediaExpectations {
1029 public:
1030 enum ExpectFrames {
1031 kExpectSomeFrames,
1032 kExpectNoFrames,
1033 kNoExpectation,
1034 };
1035
1036 void ExpectBidirectionalAudioAndVideo() {
1037 ExpectBidirectionalAudio();
1038 ExpectBidirectionalVideo();
1039 }
1040
1041 void ExpectBidirectionalAudio() {
1042 CallerExpectsSomeAudio();
1043 CalleeExpectsSomeAudio();
1044 }
1045
1046 void ExpectNoAudio() {
1047 CallerExpectsNoAudio();
1048 CalleeExpectsNoAudio();
1049 }
1050
1051 void ExpectBidirectionalVideo() {
1052 CallerExpectsSomeVideo();
1053 CalleeExpectsSomeVideo();
1054 }
1055
1056 void ExpectNoVideo() {
1057 CallerExpectsNoVideo();
1058 CalleeExpectsNoVideo();
1059 }
1060
1061 void CallerExpectsSomeAudioAndVideo() {
1062 CallerExpectsSomeAudio();
1063 CallerExpectsSomeVideo();
1064 }
1065
1066 void CalleeExpectsSomeAudioAndVideo() {
1067 CalleeExpectsSomeAudio();
1068 CalleeExpectsSomeVideo();
1069 }
1070
1071 // Caller's audio functions.
1072 void CallerExpectsSomeAudio(
1073 int expected_audio_frames = kDefaultExpectedAudioFrameCount) {
1074 caller_audio_expectation_ = kExpectSomeFrames;
1075 caller_audio_frames_expected_ = expected_audio_frames;
1076 }
1077
1078 void CallerExpectsNoAudio() {
1079 caller_audio_expectation_ = kExpectNoFrames;
1080 caller_audio_frames_expected_ = 0;
1081 }
1082
1083 // Caller's video functions.
1084 void CallerExpectsSomeVideo(
1085 int expected_video_frames = kDefaultExpectedVideoFrameCount) {
1086 caller_video_expectation_ = kExpectSomeFrames;
1087 caller_video_frames_expected_ = expected_video_frames;
1088 }
1089
1090 void CallerExpectsNoVideo() {
1091 caller_video_expectation_ = kExpectNoFrames;
1092 caller_video_frames_expected_ = 0;
1093 }
1094
1095 // Callee's audio functions.
1096 void CalleeExpectsSomeAudio(
1097 int expected_audio_frames = kDefaultExpectedAudioFrameCount) {
1098 callee_audio_expectation_ = kExpectSomeFrames;
1099 callee_audio_frames_expected_ = expected_audio_frames;
1100 }
1101
1102 void CalleeExpectsNoAudio() {
1103 callee_audio_expectation_ = kExpectNoFrames;
1104 callee_audio_frames_expected_ = 0;
1105 }
1106
1107 // Callee's video functions.
1108 void CalleeExpectsSomeVideo(
1109 int expected_video_frames = kDefaultExpectedVideoFrameCount) {
1110 callee_video_expectation_ = kExpectSomeFrames;
1111 callee_video_frames_expected_ = expected_video_frames;
1112 }
1113
1114 void CalleeExpectsNoVideo() {
1115 callee_video_expectation_ = kExpectNoFrames;
1116 callee_video_frames_expected_ = 0;
1117 }
1118
1119 ExpectFrames caller_audio_expectation_ = kNoExpectation;
1120 ExpectFrames caller_video_expectation_ = kNoExpectation;
1121 ExpectFrames callee_audio_expectation_ = kNoExpectation;
1122 ExpectFrames callee_video_expectation_ = kNoExpectation;
1123 int caller_audio_frames_expected_ = 0;
1124 int caller_video_frames_expected_ = 0;
1125 int callee_audio_frames_expected_ = 0;
1126 int callee_video_frames_expected_ = 0;
1127};
1128
deadbeef1dcb1642017-03-29 21:08:16 -07001129// Tests two PeerConnections connecting to each other end-to-end, using a
1130// virtual network, fake A/V capture and fake encoder/decoders. The
1131// PeerConnections share the threads/socket servers, but use separate versions
1132// of everything else (including "PeerConnectionFactory"s).
Mirko Bonadei6a489f22019-04-09 15:11:12 +02001133class PeerConnectionIntegrationBaseTest : public ::testing::Test {
deadbeef1dcb1642017-03-29 21:08:16 -07001134 public:
Seth Hampson2f0d7022018-02-20 11:54:42 -08001135 explicit PeerConnectionIntegrationBaseTest(SdpSemantics sdp_semantics)
1136 : sdp_semantics_(sdp_semantics),
1137 ss_(new rtc::VirtualSocketServer()),
Steve Antonede9ca52017-10-16 13:04:27 -07001138 fss_(new rtc::FirewallSocketServer(ss_.get())),
1139 network_thread_(new rtc::Thread(fss_.get())),
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001140 worker_thread_(rtc::Thread::Create()),
1141 loopback_media_transports_(network_thread_.get()) {
Sebastian Jansson8a793a02018-03-13 15:21:48 +01001142 network_thread_->SetName("PCNetworkThread", this);
1143 worker_thread_->SetName("PCWorkerThread", this);
deadbeef1dcb1642017-03-29 21:08:16 -07001144 RTC_CHECK(network_thread_->Start());
1145 RTC_CHECK(worker_thread_->Start());
Qingsi Wang7fc821d2018-07-12 12:54:53 -07001146 webrtc::metrics::Reset();
deadbeef1dcb1642017-03-29 21:08:16 -07001147 }
1148
Seth Hampson2f0d7022018-02-20 11:54:42 -08001149 ~PeerConnectionIntegrationBaseTest() {
Seth Hampsonaed71642018-06-11 07:41:32 -07001150 // The PeerConnections should deleted before the TurnCustomizers.
1151 // A TurnPort is created with a raw pointer to a TurnCustomizer. The
1152 // TurnPort has the same lifetime as the PeerConnection, so it's expected
1153 // that the TurnCustomizer outlives the life of the PeerConnection or else
1154 // when Send() is called it will hit a seg fault.
deadbeef1dcb1642017-03-29 21:08:16 -07001155 if (caller_) {
1156 caller_->set_signaling_message_receiver(nullptr);
Seth Hampsonaed71642018-06-11 07:41:32 -07001157 delete SetCallerPcWrapperAndReturnCurrent(nullptr);
deadbeef1dcb1642017-03-29 21:08:16 -07001158 }
1159 if (callee_) {
1160 callee_->set_signaling_message_receiver(nullptr);
Seth Hampsonaed71642018-06-11 07:41:32 -07001161 delete SetCalleePcWrapperAndReturnCurrent(nullptr);
deadbeef1dcb1642017-03-29 21:08:16 -07001162 }
Seth Hampsonaed71642018-06-11 07:41:32 -07001163
1164 // If turn servers were created for the test they need to be destroyed on
1165 // the network thread.
1166 network_thread()->Invoke<void>(RTC_FROM_HERE, [this] {
1167 turn_servers_.clear();
1168 turn_customizers_.clear();
1169 });
deadbeef1dcb1642017-03-29 21:08:16 -07001170 }
1171
1172 bool SignalingStateStable() {
1173 return caller_->SignalingStateStable() && callee_->SignalingStateStable();
1174 }
1175
deadbeef71452802017-05-07 17:21:01 -07001176 bool DtlsConnected() {
Alex Loiko9289eda2018-11-23 16:18:59 +00001177 // TODO(deadbeef): kIceConnectionConnected currently means both ICE and DTLS
1178 // are connected. This is an important distinction. Once we have separate
1179 // ICE and DTLS state, this check needs to use the DTLS state.
1180 return (callee()->ice_connection_state() ==
1181 webrtc::PeerConnectionInterface::kIceConnectionConnected ||
1182 callee()->ice_connection_state() ==
1183 webrtc::PeerConnectionInterface::kIceConnectionCompleted) &&
1184 (caller()->ice_connection_state() ==
1185 webrtc::PeerConnectionInterface::kIceConnectionConnected ||
1186 caller()->ice_connection_state() ==
1187 webrtc::PeerConnectionInterface::kIceConnectionCompleted);
deadbeef71452802017-05-07 17:21:01 -07001188 }
1189
Qingsi Wang7685e862018-06-11 20:15:46 -07001190 // When |event_log_factory| is null, the default implementation of the event
1191 // log factory will be used.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001192 std::unique_ptr<PeerConnectionWrapper> CreatePeerConnectionWrapper(
1193 const std::string& debug_name,
Seth Hampson2f0d7022018-02-20 11:54:42 -08001194 const PeerConnectionFactory::Options* options,
1195 const RTCConfiguration* config,
Qingsi Wang7685e862018-06-11 20:15:46 -07001196 webrtc::PeerConnectionDependencies dependencies,
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001197 std::unique_ptr<webrtc::FakeRtcEventLogFactory> event_log_factory,
1198 std::unique_ptr<webrtc::MediaTransportFactory> media_transport_factory) {
Seth Hampson2f0d7022018-02-20 11:54:42 -08001199 RTCConfiguration modified_config;
1200 if (config) {
1201 modified_config = *config;
1202 }
Steve Anton3acffc32018-04-12 17:21:03 -07001203 modified_config.sdp_semantics = sdp_semantics_;
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001204 if (!dependencies.cert_generator) {
1205 dependencies.cert_generator =
Karl Wiberg918f50c2018-07-05 11:40:33 +02001206 absl::make_unique<FakeRTCCertificateGenerator>();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001207 }
1208 std::unique_ptr<PeerConnectionWrapper> client(
1209 new PeerConnectionWrapper(debug_name));
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001210
Niels Möllerf06f9232018-08-07 12:32:18 +02001211 if (!client->Init(options, &modified_config, std::move(dependencies),
1212 network_thread_.get(), worker_thread_.get(),
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001213 std::move(event_log_factory),
1214 std::move(media_transport_factory))) {
Seth Hampson2f0d7022018-02-20 11:54:42 -08001215 return nullptr;
1216 }
1217 return client;
1218 }
1219
Qingsi Wang7685e862018-06-11 20:15:46 -07001220 std::unique_ptr<PeerConnectionWrapper>
1221 CreatePeerConnectionWrapperWithFakeRtcEventLog(
1222 const std::string& debug_name,
Qingsi Wang7685e862018-06-11 20:15:46 -07001223 const PeerConnectionFactory::Options* options,
1224 const RTCConfiguration* config,
1225 webrtc::PeerConnectionDependencies dependencies) {
1226 std::unique_ptr<webrtc::FakeRtcEventLogFactory> event_log_factory(
1227 new webrtc::FakeRtcEventLogFactory(rtc::Thread::Current()));
Niels Möllerf06f9232018-08-07 12:32:18 +02001228 return CreatePeerConnectionWrapper(debug_name, options, config,
Qingsi Wang7685e862018-06-11 20:15:46 -07001229 std::move(dependencies),
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001230 std::move(event_log_factory),
1231 /*media_transport_factory=*/nullptr);
Qingsi Wang7685e862018-06-11 20:15:46 -07001232 }
1233
deadbeef1dcb1642017-03-29 21:08:16 -07001234 bool CreatePeerConnectionWrappers() {
1235 return CreatePeerConnectionWrappersWithConfig(
1236 PeerConnectionInterface::RTCConfiguration(),
1237 PeerConnectionInterface::RTCConfiguration());
1238 }
1239
Steve Anton3acffc32018-04-12 17:21:03 -07001240 bool CreatePeerConnectionWrappersWithSdpSemantics(
1241 SdpSemantics caller_semantics,
1242 SdpSemantics callee_semantics) {
1243 // Can't specify the sdp_semantics in the passed-in configuration since it
1244 // will be overwritten by CreatePeerConnectionWrapper with whatever is
1245 // stored in sdp_semantics_. So get around this by modifying the instance
1246 // variable before calling CreatePeerConnectionWrapper for the caller and
1247 // callee PeerConnections.
1248 SdpSemantics original_semantics = sdp_semantics_;
1249 sdp_semantics_ = caller_semantics;
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001250 caller_ = CreatePeerConnectionWrapper(
Niels Möllerf06f9232018-08-07 12:32:18 +02001251 "Caller", nullptr, nullptr, webrtc::PeerConnectionDependencies(nullptr),
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001252 nullptr, /*media_transport_factory=*/nullptr);
Steve Anton3acffc32018-04-12 17:21:03 -07001253 sdp_semantics_ = callee_semantics;
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001254 callee_ = CreatePeerConnectionWrapper(
Niels Möllerf06f9232018-08-07 12:32:18 +02001255 "Callee", nullptr, nullptr, webrtc::PeerConnectionDependencies(nullptr),
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001256 nullptr, /*media_transport_factory=*/nullptr);
Steve Anton3acffc32018-04-12 17:21:03 -07001257 sdp_semantics_ = original_semantics;
1258 return caller_ && callee_;
1259 }
1260
deadbeef1dcb1642017-03-29 21:08:16 -07001261 bool CreatePeerConnectionWrappersWithConfig(
1262 const PeerConnectionInterface::RTCConfiguration& caller_config,
1263 const PeerConnectionInterface::RTCConfiguration& callee_config) {
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001264 caller_ = CreatePeerConnectionWrapper(
Niels Möllerf06f9232018-08-07 12:32:18 +02001265 "Caller", nullptr, &caller_config,
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001266 webrtc::PeerConnectionDependencies(nullptr), nullptr,
1267 /*media_transport_factory=*/nullptr);
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001268 callee_ = CreatePeerConnectionWrapper(
Niels Möllerf06f9232018-08-07 12:32:18 +02001269 "Callee", nullptr, &callee_config,
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001270 webrtc::PeerConnectionDependencies(nullptr), nullptr,
1271 /*media_transport_factory=*/nullptr);
1272 return caller_ && callee_;
1273 }
1274
1275 bool CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
1276 const PeerConnectionInterface::RTCConfiguration& caller_config,
1277 const PeerConnectionInterface::RTCConfiguration& callee_config,
1278 std::unique_ptr<webrtc::MediaTransportFactory> caller_factory,
1279 std::unique_ptr<webrtc::MediaTransportFactory> callee_factory) {
1280 caller_ =
1281 CreatePeerConnectionWrapper("Caller", nullptr, &caller_config,
1282 webrtc::PeerConnectionDependencies(nullptr),
1283 nullptr, std::move(caller_factory));
1284 callee_ =
1285 CreatePeerConnectionWrapper("Callee", nullptr, &callee_config,
1286 webrtc::PeerConnectionDependencies(nullptr),
1287 nullptr, std::move(callee_factory));
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001288 return caller_ && callee_;
1289 }
1290
1291 bool CreatePeerConnectionWrappersWithConfigAndDeps(
1292 const PeerConnectionInterface::RTCConfiguration& caller_config,
1293 webrtc::PeerConnectionDependencies caller_dependencies,
1294 const PeerConnectionInterface::RTCConfiguration& callee_config,
1295 webrtc::PeerConnectionDependencies callee_dependencies) {
1296 caller_ =
Niels Möllerf06f9232018-08-07 12:32:18 +02001297 CreatePeerConnectionWrapper("Caller", nullptr, &caller_config,
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001298 std::move(caller_dependencies), nullptr,
1299 /*media_transport_factory=*/nullptr);
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001300 callee_ =
Niels Möllerf06f9232018-08-07 12:32:18 +02001301 CreatePeerConnectionWrapper("Callee", nullptr, &callee_config,
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001302 std::move(callee_dependencies), nullptr,
1303 /*media_transport_factory=*/nullptr);
deadbeef1dcb1642017-03-29 21:08:16 -07001304 return caller_ && callee_;
1305 }
1306
1307 bool CreatePeerConnectionWrappersWithOptions(
1308 const PeerConnectionFactory::Options& caller_options,
1309 const PeerConnectionFactory::Options& callee_options) {
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001310 caller_ = CreatePeerConnectionWrapper(
Niels Möllerf06f9232018-08-07 12:32:18 +02001311 "Caller", &caller_options, nullptr,
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001312 webrtc::PeerConnectionDependencies(nullptr), nullptr,
1313 /*media_transport_factory=*/nullptr);
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001314 callee_ = CreatePeerConnectionWrapper(
Niels Möllerf06f9232018-08-07 12:32:18 +02001315 "Callee", &callee_options, nullptr,
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001316 webrtc::PeerConnectionDependencies(nullptr), nullptr,
1317 /*media_transport_factory=*/nullptr);
Qingsi Wang7685e862018-06-11 20:15:46 -07001318 return caller_ && callee_;
1319 }
1320
1321 bool CreatePeerConnectionWrappersWithFakeRtcEventLog() {
1322 PeerConnectionInterface::RTCConfiguration default_config;
1323 caller_ = CreatePeerConnectionWrapperWithFakeRtcEventLog(
Niels Möllerf06f9232018-08-07 12:32:18 +02001324 "Caller", nullptr, &default_config,
Qingsi Wang7685e862018-06-11 20:15:46 -07001325 webrtc::PeerConnectionDependencies(nullptr));
1326 callee_ = CreatePeerConnectionWrapperWithFakeRtcEventLog(
Niels Möllerf06f9232018-08-07 12:32:18 +02001327 "Callee", nullptr, &default_config,
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001328 webrtc::PeerConnectionDependencies(nullptr));
deadbeef1dcb1642017-03-29 21:08:16 -07001329 return caller_ && callee_;
1330 }
1331
Seth Hampson2f0d7022018-02-20 11:54:42 -08001332 std::unique_ptr<PeerConnectionWrapper>
1333 CreatePeerConnectionWrapperWithAlternateKey() {
deadbeef1dcb1642017-03-29 21:08:16 -07001334 std::unique_ptr<FakeRTCCertificateGenerator> cert_generator(
1335 new FakeRTCCertificateGenerator());
1336 cert_generator->use_alternate_key();
1337
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001338 webrtc::PeerConnectionDependencies dependencies(nullptr);
1339 dependencies.cert_generator = std::move(cert_generator);
Niels Möllerf06f9232018-08-07 12:32:18 +02001340 return CreatePeerConnectionWrapper("New Peer", nullptr, nullptr,
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001341 std::move(dependencies), nullptr,
1342 /*media_transport_factory=*/nullptr);
deadbeef1dcb1642017-03-29 21:08:16 -07001343 }
1344
Seth Hampsonaed71642018-06-11 07:41:32 -07001345 cricket::TestTurnServer* CreateTurnServer(
1346 rtc::SocketAddress internal_address,
1347 rtc::SocketAddress external_address,
1348 cricket::ProtocolType type = cricket::ProtocolType::PROTO_UDP,
1349 const std::string& common_name = "test turn server") {
1350 rtc::Thread* thread = network_thread();
1351 std::unique_ptr<cricket::TestTurnServer> turn_server =
1352 network_thread()->Invoke<std::unique_ptr<cricket::TestTurnServer>>(
1353 RTC_FROM_HERE,
1354 [thread, internal_address, external_address, type, common_name] {
Karl Wiberg918f50c2018-07-05 11:40:33 +02001355 return absl::make_unique<cricket::TestTurnServer>(
Seth Hampsonaed71642018-06-11 07:41:32 -07001356 thread, internal_address, external_address, type,
1357 /*ignore_bad_certs=*/true, common_name);
1358 });
1359 turn_servers_.push_back(std::move(turn_server));
1360 // Interactions with the turn server should be done on the network thread.
1361 return turn_servers_.back().get();
1362 }
1363
1364 cricket::TestTurnCustomizer* CreateTurnCustomizer() {
1365 std::unique_ptr<cricket::TestTurnCustomizer> turn_customizer =
1366 network_thread()->Invoke<std::unique_ptr<cricket::TestTurnCustomizer>>(
1367 RTC_FROM_HERE,
Karl Wiberg918f50c2018-07-05 11:40:33 +02001368 [] { return absl::make_unique<cricket::TestTurnCustomizer>(); });
Seth Hampsonaed71642018-06-11 07:41:32 -07001369 turn_customizers_.push_back(std::move(turn_customizer));
1370 // Interactions with the turn customizer should be done on the network
1371 // thread.
1372 return turn_customizers_.back().get();
1373 }
1374
1375 // Checks that the function counters for a TestTurnCustomizer are greater than
1376 // 0.
1377 void ExpectTurnCustomizerCountersIncremented(
1378 cricket::TestTurnCustomizer* turn_customizer) {
1379 unsigned int allow_channel_data_counter =
1380 network_thread()->Invoke<unsigned int>(
1381 RTC_FROM_HERE, [turn_customizer] {
1382 return turn_customizer->allow_channel_data_cnt_;
1383 });
1384 EXPECT_GT(allow_channel_data_counter, 0u);
1385 unsigned int modify_counter = network_thread()->Invoke<unsigned int>(
1386 RTC_FROM_HERE,
1387 [turn_customizer] { return turn_customizer->modify_cnt_; });
1388 EXPECT_GT(modify_counter, 0u);
1389 }
1390
deadbeef1dcb1642017-03-29 21:08:16 -07001391 // Once called, SDP blobs and ICE candidates will be automatically signaled
1392 // between PeerConnections.
1393 void ConnectFakeSignaling() {
1394 caller_->set_signaling_message_receiver(callee_.get());
1395 callee_->set_signaling_message_receiver(caller_.get());
1396 }
1397
Steve Antonede9ca52017-10-16 13:04:27 -07001398 // Once called, SDP blobs will be automatically signaled between
1399 // PeerConnections. Note that ICE candidates will not be signaled unless they
1400 // are in the exchanged SDP blobs.
1401 void ConnectFakeSignalingForSdpOnly() {
1402 ConnectFakeSignaling();
1403 SetSignalIceCandidates(false);
1404 }
1405
deadbeef1dcb1642017-03-29 21:08:16 -07001406 void SetSignalingDelayMs(int delay_ms) {
1407 caller_->set_signaling_delay_ms(delay_ms);
1408 callee_->set_signaling_delay_ms(delay_ms);
1409 }
1410
Steve Antonede9ca52017-10-16 13:04:27 -07001411 void SetSignalIceCandidates(bool signal) {
1412 caller_->set_signal_ice_candidates(signal);
1413 callee_->set_signal_ice_candidates(signal);
1414 }
1415
deadbeef1dcb1642017-03-29 21:08:16 -07001416 // Messages may get lost on the unreliable DataChannel, so we send multiple
1417 // times to avoid test flakiness.
1418 void SendRtpDataWithRetries(webrtc::DataChannelInterface* dc,
1419 const std::string& data,
1420 int retries) {
1421 for (int i = 0; i < retries; ++i) {
1422 dc->Send(DataBuffer(data));
1423 }
1424 }
1425
1426 rtc::Thread* network_thread() { return network_thread_.get(); }
1427
1428 rtc::VirtualSocketServer* virtual_socket_server() { return ss_.get(); }
1429
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001430 webrtc::MediaTransportPair* loopback_media_transports() {
1431 return &loopback_media_transports_;
1432 }
1433
deadbeef1dcb1642017-03-29 21:08:16 -07001434 PeerConnectionWrapper* caller() { return caller_.get(); }
1435
1436 // Set the |caller_| to the |wrapper| passed in and return the
1437 // original |caller_|.
1438 PeerConnectionWrapper* SetCallerPcWrapperAndReturnCurrent(
1439 PeerConnectionWrapper* wrapper) {
1440 PeerConnectionWrapper* old = caller_.release();
1441 caller_.reset(wrapper);
1442 return old;
1443 }
1444
1445 PeerConnectionWrapper* callee() { return callee_.get(); }
1446
1447 // Set the |callee_| to the |wrapper| passed in and return the
1448 // original |callee_|.
1449 PeerConnectionWrapper* SetCalleePcWrapperAndReturnCurrent(
1450 PeerConnectionWrapper* wrapper) {
1451 PeerConnectionWrapper* old = callee_.release();
1452 callee_.reset(wrapper);
1453 return old;
1454 }
1455
Qingsi Wang1dac6d82018-12-12 15:28:47 -08001456 void SetPortAllocatorFlags(uint32_t caller_flags, uint32_t callee_flags) {
1457 network_thread()->Invoke<void>(
1458 RTC_FROM_HERE, rtc::Bind(&cricket::PortAllocator::set_flags,
1459 caller()->port_allocator(), caller_flags));
1460 network_thread()->Invoke<void>(
1461 RTC_FROM_HERE, rtc::Bind(&cricket::PortAllocator::set_flags,
1462 callee()->port_allocator(), callee_flags));
1463 }
1464
Steve Antonede9ca52017-10-16 13:04:27 -07001465 rtc::FirewallSocketServer* firewall() const { return fss_.get(); }
1466
Seth Hampson2f0d7022018-02-20 11:54:42 -08001467 // Expects the provided number of new frames to be received within
1468 // kMaxWaitForFramesMs. The new expected frames are specified in
1469 // |media_expectations|. Returns false if any of the expectations were
1470 // not met.
1471 bool ExpectNewFrames(const MediaExpectations& media_expectations) {
1472 // First initialize the expected frame counts based upon the current
1473 // frame count.
1474 int total_caller_audio_frames_expected = caller()->audio_frames_received();
1475 if (media_expectations.caller_audio_expectation_ ==
1476 MediaExpectations::kExpectSomeFrames) {
1477 total_caller_audio_frames_expected +=
1478 media_expectations.caller_audio_frames_expected_;
1479 }
1480 int total_caller_video_frames_expected =
deadbeef1dcb1642017-03-29 21:08:16 -07001481 caller()->min_video_frames_received_per_track();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001482 if (media_expectations.caller_video_expectation_ ==
1483 MediaExpectations::kExpectSomeFrames) {
1484 total_caller_video_frames_expected +=
1485 media_expectations.caller_video_frames_expected_;
1486 }
1487 int total_callee_audio_frames_expected = callee()->audio_frames_received();
1488 if (media_expectations.callee_audio_expectation_ ==
1489 MediaExpectations::kExpectSomeFrames) {
1490 total_callee_audio_frames_expected +=
1491 media_expectations.callee_audio_frames_expected_;
1492 }
1493 int total_callee_video_frames_expected =
deadbeef1dcb1642017-03-29 21:08:16 -07001494 callee()->min_video_frames_received_per_track();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001495 if (media_expectations.callee_video_expectation_ ==
1496 MediaExpectations::kExpectSomeFrames) {
1497 total_callee_video_frames_expected +=
1498 media_expectations.callee_video_frames_expected_;
1499 }
deadbeef1dcb1642017-03-29 21:08:16 -07001500
Seth Hampson2f0d7022018-02-20 11:54:42 -08001501 // Wait for the expected frames.
deadbeef1dcb1642017-03-29 21:08:16 -07001502 EXPECT_TRUE_WAIT(caller()->audio_frames_received() >=
Seth Hampson2f0d7022018-02-20 11:54:42 -08001503 total_caller_audio_frames_expected &&
deadbeef1dcb1642017-03-29 21:08:16 -07001504 caller()->min_video_frames_received_per_track() >=
Seth Hampson2f0d7022018-02-20 11:54:42 -08001505 total_caller_video_frames_expected &&
deadbeef1dcb1642017-03-29 21:08:16 -07001506 callee()->audio_frames_received() >=
Seth Hampson2f0d7022018-02-20 11:54:42 -08001507 total_callee_audio_frames_expected &&
deadbeef1dcb1642017-03-29 21:08:16 -07001508 callee()->min_video_frames_received_per_track() >=
Seth Hampson2f0d7022018-02-20 11:54:42 -08001509 total_callee_video_frames_expected,
1510 kMaxWaitForFramesMs);
1511 bool expectations_correct =
1512 caller()->audio_frames_received() >=
1513 total_caller_audio_frames_expected &&
1514 caller()->min_video_frames_received_per_track() >=
1515 total_caller_video_frames_expected &&
1516 callee()->audio_frames_received() >=
1517 total_callee_audio_frames_expected &&
1518 callee()->min_video_frames_received_per_track() >=
1519 total_callee_video_frames_expected;
deadbeef1dcb1642017-03-29 21:08:16 -07001520
Seth Hampson2f0d7022018-02-20 11:54:42 -08001521 // After the combined wait, print out a more detailed message upon
1522 // failure.
deadbeef1dcb1642017-03-29 21:08:16 -07001523 EXPECT_GE(caller()->audio_frames_received(),
Seth Hampson2f0d7022018-02-20 11:54:42 -08001524 total_caller_audio_frames_expected);
deadbeef1dcb1642017-03-29 21:08:16 -07001525 EXPECT_GE(caller()->min_video_frames_received_per_track(),
Seth Hampson2f0d7022018-02-20 11:54:42 -08001526 total_caller_video_frames_expected);
deadbeef1dcb1642017-03-29 21:08:16 -07001527 EXPECT_GE(callee()->audio_frames_received(),
Seth Hampson2f0d7022018-02-20 11:54:42 -08001528 total_callee_audio_frames_expected);
deadbeef1dcb1642017-03-29 21:08:16 -07001529 EXPECT_GE(callee()->min_video_frames_received_per_track(),
Seth Hampson2f0d7022018-02-20 11:54:42 -08001530 total_callee_video_frames_expected);
1531
1532 // We want to make sure nothing unexpected was received.
1533 if (media_expectations.caller_audio_expectation_ ==
1534 MediaExpectations::kExpectNoFrames) {
1535 EXPECT_EQ(caller()->audio_frames_received(),
1536 total_caller_audio_frames_expected);
1537 if (caller()->audio_frames_received() !=
1538 total_caller_audio_frames_expected) {
1539 expectations_correct = false;
1540 }
1541 }
1542 if (media_expectations.caller_video_expectation_ ==
1543 MediaExpectations::kExpectNoFrames) {
1544 EXPECT_EQ(caller()->min_video_frames_received_per_track(),
1545 total_caller_video_frames_expected);
1546 if (caller()->min_video_frames_received_per_track() !=
1547 total_caller_video_frames_expected) {
1548 expectations_correct = false;
1549 }
1550 }
1551 if (media_expectations.callee_audio_expectation_ ==
1552 MediaExpectations::kExpectNoFrames) {
1553 EXPECT_EQ(callee()->audio_frames_received(),
1554 total_callee_audio_frames_expected);
1555 if (callee()->audio_frames_received() !=
1556 total_callee_audio_frames_expected) {
1557 expectations_correct = false;
1558 }
1559 }
1560 if (media_expectations.callee_video_expectation_ ==
1561 MediaExpectations::kExpectNoFrames) {
1562 EXPECT_EQ(callee()->min_video_frames_received_per_track(),
1563 total_callee_video_frames_expected);
1564 if (callee()->min_video_frames_received_per_track() !=
1565 total_callee_video_frames_expected) {
1566 expectations_correct = false;
1567 }
1568 }
1569 return expectations_correct;
deadbeef1dcb1642017-03-29 21:08:16 -07001570 }
1571
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07001572 void TestNegotiatedCipherSuite(
1573 const PeerConnectionFactory::Options& caller_options,
1574 const PeerConnectionFactory::Options& callee_options,
1575 int expected_cipher_suite) {
deadbeef1dcb1642017-03-29 21:08:16 -07001576 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(caller_options,
1577 callee_options));
deadbeef1dcb1642017-03-29 21:08:16 -07001578 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001579 caller()->AddAudioVideoTracks();
1580 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001581 caller()->CreateAndSetAndSignalOffer();
Qingsi Wang7fc821d2018-07-12 12:54:53 -07001582 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07001583 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(expected_cipher_suite),
deadbeefd8ad7882017-04-18 16:01:17 -07001584 caller()->OldGetStats()->SrtpCipher(), kDefaultTimeout);
Qingsi Wang7fc821d2018-07-12 12:54:53 -07001585 // TODO(bugs.webrtc.org/9456): Fix it.
Alex Loiko9289eda2018-11-23 16:18:59 +00001586 EXPECT_EQ(1, webrtc::metrics::NumEvents(
Qingsi Wang7fc821d2018-07-12 12:54:53 -07001587 "WebRTC.PeerConnection.SrtpCryptoSuite.Audio",
1588 expected_cipher_suite));
deadbeef1dcb1642017-03-29 21:08:16 -07001589 }
1590
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07001591 void TestGcmNegotiationUsesCipherSuite(bool local_gcm_enabled,
1592 bool remote_gcm_enabled,
1593 int expected_cipher_suite) {
1594 PeerConnectionFactory::Options caller_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07001595 caller_options.crypto_options.srtp.enable_gcm_crypto_suites =
1596 local_gcm_enabled;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07001597 PeerConnectionFactory::Options callee_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07001598 callee_options.crypto_options.srtp.enable_gcm_crypto_suites =
1599 remote_gcm_enabled;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07001600 TestNegotiatedCipherSuite(caller_options, callee_options,
1601 expected_cipher_suite);
1602 }
1603
Seth Hampson2f0d7022018-02-20 11:54:42 -08001604 protected:
Steve Anton3acffc32018-04-12 17:21:03 -07001605 SdpSemantics sdp_semantics_;
Seth Hampson2f0d7022018-02-20 11:54:42 -08001606
deadbeef1dcb1642017-03-29 21:08:16 -07001607 private:
1608 // |ss_| is used by |network_thread_| so it must be destroyed later.
deadbeef1dcb1642017-03-29 21:08:16 -07001609 std::unique_ptr<rtc::VirtualSocketServer> ss_;
Steve Antonede9ca52017-10-16 13:04:27 -07001610 std::unique_ptr<rtc::FirewallSocketServer> fss_;
deadbeef1dcb1642017-03-29 21:08:16 -07001611 // |network_thread_| and |worker_thread_| are used by both
1612 // |caller_| and |callee_| so they must be destroyed
1613 // later.
1614 std::unique_ptr<rtc::Thread> network_thread_;
1615 std::unique_ptr<rtc::Thread> worker_thread_;
Seth Hampsonaed71642018-06-11 07:41:32 -07001616 // The turn servers and turn customizers should be accessed & deleted on the
1617 // network thread to avoid a race with the socket read/write that occurs
1618 // on the network thread.
1619 std::vector<std::unique_ptr<cricket::TestTurnServer>> turn_servers_;
1620 std::vector<std::unique_ptr<cricket::TestTurnCustomizer>> turn_customizers_;
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001621 webrtc::MediaTransportPair loopback_media_transports_;
deadbeef1dcb1642017-03-29 21:08:16 -07001622 std::unique_ptr<PeerConnectionWrapper> caller_;
1623 std::unique_ptr<PeerConnectionWrapper> callee_;
1624};
1625
Seth Hampson2f0d7022018-02-20 11:54:42 -08001626class PeerConnectionIntegrationTest
1627 : public PeerConnectionIntegrationBaseTest,
1628 public ::testing::WithParamInterface<SdpSemantics> {
1629 protected:
1630 PeerConnectionIntegrationTest()
1631 : PeerConnectionIntegrationBaseTest(GetParam()) {}
1632};
1633
1634class PeerConnectionIntegrationTestPlanB
1635 : public PeerConnectionIntegrationBaseTest {
1636 protected:
1637 PeerConnectionIntegrationTestPlanB()
1638 : PeerConnectionIntegrationBaseTest(SdpSemantics::kPlanB) {}
1639};
1640
1641class PeerConnectionIntegrationTestUnifiedPlan
1642 : public PeerConnectionIntegrationBaseTest {
1643 protected:
1644 PeerConnectionIntegrationTestUnifiedPlan()
1645 : PeerConnectionIntegrationBaseTest(SdpSemantics::kUnifiedPlan) {}
1646};
1647
deadbeef1dcb1642017-03-29 21:08:16 -07001648// Test the OnFirstPacketReceived callback from audio/video RtpReceivers. This
1649// includes testing that the callback is invoked if an observer is connected
1650// after the first packet has already been received.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001651TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07001652 RtpReceiverObserverOnFirstPacketReceived) {
1653 ASSERT_TRUE(CreatePeerConnectionWrappers());
1654 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001655 caller()->AddAudioVideoTracks();
1656 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001657 // Start offer/answer exchange and wait for it to complete.
1658 caller()->CreateAndSetAndSignalOffer();
1659 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1660 // Should be one receiver each for audio/video.
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02001661 EXPECT_EQ(2U, caller()->rtp_receiver_observers().size());
1662 EXPECT_EQ(2U, callee()->rtp_receiver_observers().size());
deadbeef1dcb1642017-03-29 21:08:16 -07001663 // Wait for all "first packet received" callbacks to be fired.
1664 EXPECT_TRUE_WAIT(
Steve Anton64b626b2019-01-28 17:25:26 -08001665 absl::c_all_of(caller()->rtp_receiver_observers(),
1666 [](const std::unique_ptr<MockRtpReceiverObserver>& o) {
1667 return o->first_packet_received();
1668 }),
deadbeef1dcb1642017-03-29 21:08:16 -07001669 kMaxWaitForFramesMs);
1670 EXPECT_TRUE_WAIT(
Steve Anton64b626b2019-01-28 17:25:26 -08001671 absl::c_all_of(callee()->rtp_receiver_observers(),
1672 [](const std::unique_ptr<MockRtpReceiverObserver>& o) {
1673 return o->first_packet_received();
1674 }),
deadbeef1dcb1642017-03-29 21:08:16 -07001675 kMaxWaitForFramesMs);
1676 // If new observers are set after the first packet was already received, the
1677 // callback should still be invoked.
1678 caller()->ResetRtpReceiverObservers();
1679 callee()->ResetRtpReceiverObservers();
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02001680 EXPECT_EQ(2U, caller()->rtp_receiver_observers().size());
1681 EXPECT_EQ(2U, callee()->rtp_receiver_observers().size());
deadbeef1dcb1642017-03-29 21:08:16 -07001682 EXPECT_TRUE(
Steve Anton64b626b2019-01-28 17:25:26 -08001683 absl::c_all_of(caller()->rtp_receiver_observers(),
1684 [](const std::unique_ptr<MockRtpReceiverObserver>& o) {
1685 return o->first_packet_received();
1686 }));
deadbeef1dcb1642017-03-29 21:08:16 -07001687 EXPECT_TRUE(
Steve Anton64b626b2019-01-28 17:25:26 -08001688 absl::c_all_of(callee()->rtp_receiver_observers(),
1689 [](const std::unique_ptr<MockRtpReceiverObserver>& o) {
1690 return o->first_packet_received();
1691 }));
deadbeef1dcb1642017-03-29 21:08:16 -07001692}
1693
1694class DummyDtmfObserver : public DtmfSenderObserverInterface {
1695 public:
1696 DummyDtmfObserver() : completed_(false) {}
1697
1698 // Implements DtmfSenderObserverInterface.
1699 void OnToneChange(const std::string& tone) override {
1700 tones_.push_back(tone);
1701 if (tone.empty()) {
1702 completed_ = true;
1703 }
1704 }
1705
1706 const std::vector<std::string>& tones() const { return tones_; }
1707 bool completed() const { return completed_; }
1708
1709 private:
1710 bool completed_;
1711 std::vector<std::string> tones_;
1712};
1713
1714// Assumes |sender| already has an audio track added and the offer/answer
1715// exchange is done.
1716void TestDtmfFromSenderToReceiver(PeerConnectionWrapper* sender,
1717 PeerConnectionWrapper* receiver) {
Steve Anton15324772018-01-16 10:26:49 -08001718 // We should be able to get a DTMF sender from the local sender.
1719 rtc::scoped_refptr<DtmfSenderInterface> dtmf_sender =
1720 sender->pc()->GetSenders().at(0)->GetDtmfSender();
1721 ASSERT_TRUE(dtmf_sender);
deadbeef1dcb1642017-03-29 21:08:16 -07001722 DummyDtmfObserver observer;
deadbeef1dcb1642017-03-29 21:08:16 -07001723 dtmf_sender->RegisterObserver(&observer);
1724
1725 // Test the DtmfSender object just created.
1726 EXPECT_TRUE(dtmf_sender->CanInsertDtmf());
1727 EXPECT_TRUE(dtmf_sender->InsertDtmf("1a", 100, 50));
1728
1729 EXPECT_TRUE_WAIT(observer.completed(), kDefaultTimeout);
1730 std::vector<std::string> tones = {"1", "a", ""};
1731 EXPECT_EQ(tones, observer.tones());
1732 dtmf_sender->UnregisterObserver();
1733 // TODO(deadbeef): Verify the tones were actually received end-to-end.
1734}
1735
1736// Verifies the DtmfSenderObserver callbacks for a DtmfSender (one in each
1737// direction).
Seth Hampson2f0d7022018-02-20 11:54:42 -08001738TEST_P(PeerConnectionIntegrationTest, DtmfSenderObserver) {
deadbeef1dcb1642017-03-29 21:08:16 -07001739 ASSERT_TRUE(CreatePeerConnectionWrappers());
1740 ConnectFakeSignaling();
1741 // Only need audio for DTMF.
Steve Anton15324772018-01-16 10:26:49 -08001742 caller()->AddAudioTrack();
1743 callee()->AddAudioTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07001744 caller()->CreateAndSetAndSignalOffer();
1745 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
deadbeef71452802017-05-07 17:21:01 -07001746 // DTLS must finish before the DTMF sender can be used reliably.
1747 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07001748 TestDtmfFromSenderToReceiver(caller(), callee());
1749 TestDtmfFromSenderToReceiver(callee(), caller());
1750}
1751
1752// Basic end-to-end test, verifying media can be encoded/transmitted/decoded
1753// between two connections, using DTLS-SRTP.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001754TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithDtls) {
deadbeef1dcb1642017-03-29 21:08:16 -07001755 ASSERT_TRUE(CreatePeerConnectionWrappers());
1756 ConnectFakeSignaling();
Harald Alvestrand194939b2018-01-24 16:04:13 +01001757
deadbeef1dcb1642017-03-29 21:08:16 -07001758 // Do normal offer/answer and wait for some frames to be received in each
1759 // direction.
Steve Anton15324772018-01-16 10:26:49 -08001760 caller()->AddAudioVideoTracks();
1761 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001762 caller()->CreateAndSetAndSignalOffer();
1763 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001764 MediaExpectations media_expectations;
1765 media_expectations.ExpectBidirectionalAudioAndVideo();
1766 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Qingsi Wang7fc821d2018-07-12 12:54:53 -07001767 EXPECT_LE(2, webrtc::metrics::NumEvents("WebRTC.PeerConnection.KeyProtocol",
1768 webrtc::kEnumCounterKeyProtocolDtls));
1769 EXPECT_EQ(0, webrtc::metrics::NumEvents("WebRTC.PeerConnection.KeyProtocol",
1770 webrtc::kEnumCounterKeyProtocolSdes));
deadbeef1dcb1642017-03-29 21:08:16 -07001771}
1772
1773// Uses SDES instead of DTLS for key agreement.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001774TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithSdes) {
deadbeef1dcb1642017-03-29 21:08:16 -07001775 PeerConnectionInterface::RTCConfiguration sdes_config;
1776 sdes_config.enable_dtls_srtp.emplace(false);
1777 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(sdes_config, sdes_config));
1778 ConnectFakeSignaling();
1779
1780 // Do normal offer/answer and wait for some frames to be received in each
1781 // direction.
Steve Anton15324772018-01-16 10:26:49 -08001782 caller()->AddAudioVideoTracks();
1783 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001784 caller()->CreateAndSetAndSignalOffer();
1785 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001786 MediaExpectations media_expectations;
1787 media_expectations.ExpectBidirectionalAudioAndVideo();
1788 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Qingsi Wang7fc821d2018-07-12 12:54:53 -07001789 EXPECT_LE(2, webrtc::metrics::NumEvents("WebRTC.PeerConnection.KeyProtocol",
1790 webrtc::kEnumCounterKeyProtocolSdes));
1791 EXPECT_EQ(0, webrtc::metrics::NumEvents("WebRTC.PeerConnection.KeyProtocol",
1792 webrtc::kEnumCounterKeyProtocolDtls));
deadbeef1dcb1642017-03-29 21:08:16 -07001793}
1794
Steve Anton8c0f7a72017-10-03 10:03:10 -07001795// Tests that the GetRemoteAudioSSLCertificate method returns the remote DTLS
1796// certificate once the DTLS handshake has finished.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001797TEST_P(PeerConnectionIntegrationTest,
Steve Anton8c0f7a72017-10-03 10:03:10 -07001798 GetRemoteAudioSSLCertificateReturnsExchangedCertificate) {
1799 auto GetRemoteAudioSSLCertificate = [](PeerConnectionWrapper* wrapper) {
1800 auto pci = reinterpret_cast<PeerConnectionProxy*>(wrapper->pc());
1801 auto pc = reinterpret_cast<PeerConnection*>(pci->internal());
1802 return pc->GetRemoteAudioSSLCertificate();
1803 };
Zhi Huang70b820f2018-01-27 14:16:15 -08001804 auto GetRemoteAudioSSLCertChain = [](PeerConnectionWrapper* wrapper) {
1805 auto pci = reinterpret_cast<PeerConnectionProxy*>(wrapper->pc());
1806 auto pc = reinterpret_cast<PeerConnection*>(pci->internal());
1807 return pc->GetRemoteAudioSSLCertChain();
1808 };
Steve Anton8c0f7a72017-10-03 10:03:10 -07001809
1810 auto caller_cert = rtc::RTCCertificate::FromPEM(kRsaPems[0]);
1811 auto callee_cert = rtc::RTCCertificate::FromPEM(kRsaPems[1]);
1812
1813 // Configure each side with a known certificate so they can be compared later.
1814 PeerConnectionInterface::RTCConfiguration caller_config;
1815 caller_config.enable_dtls_srtp.emplace(true);
1816 caller_config.certificates.push_back(caller_cert);
1817 PeerConnectionInterface::RTCConfiguration callee_config;
1818 callee_config.enable_dtls_srtp.emplace(true);
1819 callee_config.certificates.push_back(callee_cert);
1820 ASSERT_TRUE(
1821 CreatePeerConnectionWrappersWithConfig(caller_config, callee_config));
1822 ConnectFakeSignaling();
1823
1824 // When first initialized, there should not be a remote SSL certificate (and
1825 // calling this method should not crash).
1826 EXPECT_EQ(nullptr, GetRemoteAudioSSLCertificate(caller()));
1827 EXPECT_EQ(nullptr, GetRemoteAudioSSLCertificate(callee()));
Zhi Huang70b820f2018-01-27 14:16:15 -08001828 EXPECT_EQ(nullptr, GetRemoteAudioSSLCertChain(caller()));
1829 EXPECT_EQ(nullptr, GetRemoteAudioSSLCertChain(callee()));
Steve Anton8c0f7a72017-10-03 10:03:10 -07001830
Steve Anton15324772018-01-16 10:26:49 -08001831 caller()->AddAudioTrack();
1832 callee()->AddAudioTrack();
Steve Anton8c0f7a72017-10-03 10:03:10 -07001833 caller()->CreateAndSetAndSignalOffer();
1834 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1835 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
1836
1837 // Once DTLS has been connected, each side should return the other's SSL
1838 // certificate when calling GetRemoteAudioSSLCertificate.
1839
1840 auto caller_remote_cert = GetRemoteAudioSSLCertificate(caller());
1841 ASSERT_TRUE(caller_remote_cert);
Benjamin Wright6c6c9df2018-10-25 01:16:26 -07001842 EXPECT_EQ(callee_cert->GetSSLCertificate().ToPEMString(),
Steve Anton8c0f7a72017-10-03 10:03:10 -07001843 caller_remote_cert->ToPEMString());
1844
1845 auto callee_remote_cert = GetRemoteAudioSSLCertificate(callee());
1846 ASSERT_TRUE(callee_remote_cert);
Benjamin Wright6c6c9df2018-10-25 01:16:26 -07001847 EXPECT_EQ(caller_cert->GetSSLCertificate().ToPEMString(),
Steve Anton8c0f7a72017-10-03 10:03:10 -07001848 callee_remote_cert->ToPEMString());
Zhi Huang70b820f2018-01-27 14:16:15 -08001849
1850 auto caller_remote_cert_chain = GetRemoteAudioSSLCertChain(caller());
1851 ASSERT_TRUE(caller_remote_cert_chain);
1852 ASSERT_EQ(1U, caller_remote_cert_chain->GetSize());
1853 auto remote_cert = &caller_remote_cert_chain->Get(0);
Benjamin Wright6c6c9df2018-10-25 01:16:26 -07001854 EXPECT_EQ(callee_cert->GetSSLCertificate().ToPEMString(),
Zhi Huang70b820f2018-01-27 14:16:15 -08001855 remote_cert->ToPEMString());
1856
1857 auto callee_remote_cert_chain = GetRemoteAudioSSLCertChain(callee());
1858 ASSERT_TRUE(callee_remote_cert_chain);
1859 ASSERT_EQ(1U, callee_remote_cert_chain->GetSize());
1860 remote_cert = &callee_remote_cert_chain->Get(0);
Benjamin Wright6c6c9df2018-10-25 01:16:26 -07001861 EXPECT_EQ(caller_cert->GetSSLCertificate().ToPEMString(),
Zhi Huang70b820f2018-01-27 14:16:15 -08001862 remote_cert->ToPEMString());
Steve Anton8c0f7a72017-10-03 10:03:10 -07001863}
1864
deadbeef1dcb1642017-03-29 21:08:16 -07001865// This test sets up a call between two parties with a source resolution of
1866// 1280x720 and verifies that a 16:9 aspect ratio is received.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001867TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07001868 Send1280By720ResolutionAndReceive16To9AspectRatio) {
1869 ASSERT_TRUE(CreatePeerConnectionWrappers());
1870 ConnectFakeSignaling();
1871
Niels Möller5c7efe72018-05-11 10:34:46 +02001872 // Add video tracks with 16:9 aspect ratio, size 1280 x 720.
1873 webrtc::FakePeriodicVideoSource::Config config;
1874 config.width = 1280;
1875 config.height = 720;
Johannes Kron965e7942018-09-13 15:36:20 +02001876 config.timestamp_offset_ms = rtc::TimeMillis();
Niels Möller5c7efe72018-05-11 10:34:46 +02001877 caller()->AddTrack(caller()->CreateLocalVideoTrackWithConfig(config));
1878 callee()->AddTrack(callee()->CreateLocalVideoTrackWithConfig(config));
deadbeef1dcb1642017-03-29 21:08:16 -07001879
1880 // Do normal offer/answer and wait for at least one frame to be received in
1881 // each direction.
1882 caller()->CreateAndSetAndSignalOffer();
1883 ASSERT_TRUE_WAIT(caller()->min_video_frames_received_per_track() > 0 &&
1884 callee()->min_video_frames_received_per_track() > 0,
1885 kMaxWaitForFramesMs);
1886
1887 // Check rendered aspect ratio.
1888 EXPECT_EQ(16.0 / 9, caller()->local_rendered_aspect_ratio());
1889 EXPECT_EQ(16.0 / 9, caller()->rendered_aspect_ratio());
1890 EXPECT_EQ(16.0 / 9, callee()->local_rendered_aspect_ratio());
1891 EXPECT_EQ(16.0 / 9, callee()->rendered_aspect_ratio());
1892}
1893
1894// This test sets up an one-way call, with media only from caller to
1895// callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001896TEST_P(PeerConnectionIntegrationTest, OneWayMediaCall) {
deadbeef1dcb1642017-03-29 21:08:16 -07001897 ASSERT_TRUE(CreatePeerConnectionWrappers());
1898 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001899 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001900 caller()->CreateAndSetAndSignalOffer();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001901 MediaExpectations media_expectations;
1902 media_expectations.CalleeExpectsSomeAudioAndVideo();
1903 media_expectations.CallerExpectsNoAudio();
1904 media_expectations.CallerExpectsNoVideo();
1905 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07001906}
1907
1908// This test sets up a audio call initially, with the callee rejecting video
1909// initially. Then later the callee decides to upgrade to audio/video, and
1910// initiates a new offer/answer exchange.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001911TEST_P(PeerConnectionIntegrationTest, AudioToVideoUpgrade) {
deadbeef1dcb1642017-03-29 21:08:16 -07001912 ASSERT_TRUE(CreatePeerConnectionWrappers());
1913 ConnectFakeSignaling();
1914 // Initially, offer an audio/video stream from the caller, but refuse to
1915 // send/receive video on the callee side.
Steve Anton15324772018-01-16 10:26:49 -08001916 caller()->AddAudioVideoTracks();
1917 callee()->AddAudioTrack();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001918 if (sdp_semantics_ == SdpSemantics::kPlanB) {
1919 PeerConnectionInterface::RTCOfferAnswerOptions options;
1920 options.offer_to_receive_video = 0;
1921 callee()->SetOfferAnswerOptions(options);
1922 } else {
1923 callee()->SetRemoteOfferHandler([this] {
1924 callee()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)->Stop();
1925 });
1926 }
deadbeef1dcb1642017-03-29 21:08:16 -07001927 // Do offer/answer and make sure audio is still received end-to-end.
1928 caller()->CreateAndSetAndSignalOffer();
1929 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001930 {
1931 MediaExpectations media_expectations;
1932 media_expectations.ExpectBidirectionalAudio();
1933 media_expectations.ExpectNoVideo();
1934 ASSERT_TRUE(ExpectNewFrames(media_expectations));
1935 }
deadbeef1dcb1642017-03-29 21:08:16 -07001936 // Sanity check that the callee's description has a rejected video section.
1937 ASSERT_NE(nullptr, callee()->pc()->local_description());
1938 const ContentInfo* callee_video_content =
1939 GetFirstVideoContent(callee()->pc()->local_description()->description());
1940 ASSERT_NE(nullptr, callee_video_content);
1941 EXPECT_TRUE(callee_video_content->rejected);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001942
deadbeef1dcb1642017-03-29 21:08:16 -07001943 // Now negotiate with video and ensure negotiation succeeds, with video
1944 // frames and additional audio frames being received.
Steve Anton15324772018-01-16 10:26:49 -08001945 callee()->AddVideoTrack();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001946 if (sdp_semantics_ == SdpSemantics::kPlanB) {
1947 PeerConnectionInterface::RTCOfferAnswerOptions options;
1948 options.offer_to_receive_video = 1;
1949 callee()->SetOfferAnswerOptions(options);
1950 } else {
1951 callee()->SetRemoteOfferHandler(nullptr);
1952 caller()->SetRemoteOfferHandler([this] {
1953 // The caller creates a new transceiver to receive video on when receiving
1954 // the offer, but by default it is send only.
1955 auto transceivers = caller()->pc()->GetTransceivers();
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02001956 ASSERT_EQ(3U, transceivers.size());
Seth Hampson2f0d7022018-02-20 11:54:42 -08001957 ASSERT_EQ(cricket::MEDIA_TYPE_VIDEO,
1958 transceivers[2]->receiver()->media_type());
1959 transceivers[2]->sender()->SetTrack(caller()->CreateLocalVideoTrack());
1960 transceivers[2]->SetDirection(RtpTransceiverDirection::kSendRecv);
1961 });
1962 }
deadbeef1dcb1642017-03-29 21:08:16 -07001963 callee()->CreateAndSetAndSignalOffer();
1964 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001965 {
1966 // Expect additional audio frames to be received after the upgrade.
1967 MediaExpectations media_expectations;
1968 media_expectations.ExpectBidirectionalAudioAndVideo();
1969 ASSERT_TRUE(ExpectNewFrames(media_expectations));
1970 }
deadbeef1dcb1642017-03-29 21:08:16 -07001971}
1972
deadbeef4389b4d2017-09-07 09:07:36 -07001973// Simpler than the above test; just add an audio track to an established
1974// video-only connection.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001975TEST_P(PeerConnectionIntegrationTest, AddAudioToVideoOnlyCall) {
deadbeef4389b4d2017-09-07 09:07:36 -07001976 ASSERT_TRUE(CreatePeerConnectionWrappers());
1977 ConnectFakeSignaling();
1978 // Do initial offer/answer with just a video track.
Steve Anton15324772018-01-16 10:26:49 -08001979 caller()->AddVideoTrack();
1980 callee()->AddVideoTrack();
deadbeef4389b4d2017-09-07 09:07:36 -07001981 caller()->CreateAndSetAndSignalOffer();
1982 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1983 // Now add an audio track and do another offer/answer.
Steve Anton15324772018-01-16 10:26:49 -08001984 caller()->AddAudioTrack();
1985 callee()->AddAudioTrack();
deadbeef4389b4d2017-09-07 09:07:36 -07001986 caller()->CreateAndSetAndSignalOffer();
1987 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1988 // Ensure both audio and video frames are received end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001989 MediaExpectations media_expectations;
1990 media_expectations.ExpectBidirectionalAudioAndVideo();
1991 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef4389b4d2017-09-07 09:07:36 -07001992}
1993
deadbeef1dcb1642017-03-29 21:08:16 -07001994// This test sets up a call that's transferred to a new caller with a different
1995// DTLS fingerprint.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001996TEST_P(PeerConnectionIntegrationTest, CallTransferredForCallee) {
deadbeef1dcb1642017-03-29 21:08:16 -07001997 ASSERT_TRUE(CreatePeerConnectionWrappers());
1998 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001999 caller()->AddAudioVideoTracks();
2000 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002001 caller()->CreateAndSetAndSignalOffer();
2002 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2003
2004 // Keep the original peer around which will still send packets to the
2005 // receiving client. These SRTP packets will be dropped.
2006 std::unique_ptr<PeerConnectionWrapper> original_peer(
2007 SetCallerPcWrapperAndReturnCurrent(
Seth Hampson2f0d7022018-02-20 11:54:42 -08002008 CreatePeerConnectionWrapperWithAlternateKey().release()));
deadbeef1dcb1642017-03-29 21:08:16 -07002009 // TODO(deadbeef): Why do we call Close here? That goes against the comment
2010 // directly above.
2011 original_peer->pc()->Close();
2012
2013 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002014 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002015 caller()->CreateAndSetAndSignalOffer();
2016 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2017 // Wait for some additional frames to be transmitted end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002018 MediaExpectations media_expectations;
2019 media_expectations.ExpectBidirectionalAudioAndVideo();
2020 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002021}
2022
2023// This test sets up a call that's transferred to a new callee with a different
2024// DTLS fingerprint.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002025TEST_P(PeerConnectionIntegrationTest, CallTransferredForCaller) {
deadbeef1dcb1642017-03-29 21:08:16 -07002026 ASSERT_TRUE(CreatePeerConnectionWrappers());
2027 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002028 caller()->AddAudioVideoTracks();
2029 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002030 caller()->CreateAndSetAndSignalOffer();
2031 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2032
2033 // Keep the original peer around which will still send packets to the
2034 // receiving client. These SRTP packets will be dropped.
2035 std::unique_ptr<PeerConnectionWrapper> original_peer(
2036 SetCalleePcWrapperAndReturnCurrent(
Seth Hampson2f0d7022018-02-20 11:54:42 -08002037 CreatePeerConnectionWrapperWithAlternateKey().release()));
deadbeef1dcb1642017-03-29 21:08:16 -07002038 // TODO(deadbeef): Why do we call Close here? That goes against the comment
2039 // directly above.
2040 original_peer->pc()->Close();
2041
2042 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002043 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002044 caller()->SetOfferAnswerOptions(IceRestartOfferAnswerOptions());
2045 caller()->CreateAndSetAndSignalOffer();
2046 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2047 // Wait for some additional frames to be transmitted end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002048 MediaExpectations media_expectations;
2049 media_expectations.ExpectBidirectionalAudioAndVideo();
2050 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002051}
2052
2053// This test sets up a non-bundled call and negotiates bundling at the same
2054// time as starting an ICE restart. When bundling is in effect in the restart,
2055// the DTLS-SRTP context should be successfully reset.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002056TEST_P(PeerConnectionIntegrationTest, BundlingEnabledWhileIceRestartOccurs) {
deadbeef1dcb1642017-03-29 21:08:16 -07002057 ASSERT_TRUE(CreatePeerConnectionWrappers());
2058 ConnectFakeSignaling();
2059
Steve Anton15324772018-01-16 10:26:49 -08002060 caller()->AddAudioVideoTracks();
2061 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002062 // Remove the bundle group from the SDP received by the callee.
2063 callee()->SetReceivedSdpMunger([](cricket::SessionDescription* desc) {
2064 desc->RemoveGroupByName("BUNDLE");
2065 });
2066 caller()->CreateAndSetAndSignalOffer();
2067 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002068 {
2069 MediaExpectations media_expectations;
2070 media_expectations.ExpectBidirectionalAudioAndVideo();
2071 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2072 }
deadbeef1dcb1642017-03-29 21:08:16 -07002073 // Now stop removing the BUNDLE group, and trigger an ICE restart.
2074 callee()->SetReceivedSdpMunger(nullptr);
2075 caller()->SetOfferAnswerOptions(IceRestartOfferAnswerOptions());
2076 caller()->CreateAndSetAndSignalOffer();
2077 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2078
2079 // Expect additional frames to be received after the ICE restart.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002080 {
2081 MediaExpectations media_expectations;
2082 media_expectations.ExpectBidirectionalAudioAndVideo();
2083 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2084 }
deadbeef1dcb1642017-03-29 21:08:16 -07002085}
2086
2087// Test CVO (Coordination of Video Orientation). If a video source is rotated
2088// and both peers support the CVO RTP header extension, the actual video frames
2089// don't need to be encoded in different resolutions, since the rotation is
2090// communicated through the RTP header extension.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002091TEST_P(PeerConnectionIntegrationTest, RotatedVideoWithCVOExtension) {
deadbeef1dcb1642017-03-29 21:08:16 -07002092 ASSERT_TRUE(CreatePeerConnectionWrappers());
2093 ConnectFakeSignaling();
2094 // Add rotated video tracks.
Steve Anton15324772018-01-16 10:26:49 -08002095 caller()->AddTrack(
deadbeef1dcb1642017-03-29 21:08:16 -07002096 caller()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_90));
Steve Anton15324772018-01-16 10:26:49 -08002097 callee()->AddTrack(
deadbeef1dcb1642017-03-29 21:08:16 -07002098 callee()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_270));
2099
2100 // Wait for video frames to be received by both sides.
2101 caller()->CreateAndSetAndSignalOffer();
2102 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2103 ASSERT_TRUE_WAIT(caller()->min_video_frames_received_per_track() > 0 &&
2104 callee()->min_video_frames_received_per_track() > 0,
2105 kMaxWaitForFramesMs);
2106
2107 // Ensure that the aspect ratio is unmodified.
2108 // TODO(deadbeef): Where does 4:3 come from? Should be explicit in the test,
2109 // not just assumed.
2110 EXPECT_EQ(4.0 / 3, caller()->local_rendered_aspect_ratio());
2111 EXPECT_EQ(4.0 / 3, caller()->rendered_aspect_ratio());
2112 EXPECT_EQ(4.0 / 3, callee()->local_rendered_aspect_ratio());
2113 EXPECT_EQ(4.0 / 3, callee()->rendered_aspect_ratio());
2114 // Ensure that the CVO bits were surfaced to the renderer.
2115 EXPECT_EQ(webrtc::kVideoRotation_270, caller()->rendered_rotation());
2116 EXPECT_EQ(webrtc::kVideoRotation_90, callee()->rendered_rotation());
2117}
2118
2119// Test that when the CVO extension isn't supported, video is rotated the
2120// old-fashioned way, by encoding rotated frames.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002121TEST_P(PeerConnectionIntegrationTest, RotatedVideoWithoutCVOExtension) {
deadbeef1dcb1642017-03-29 21:08:16 -07002122 ASSERT_TRUE(CreatePeerConnectionWrappers());
2123 ConnectFakeSignaling();
2124 // Add rotated video tracks.
Steve Anton15324772018-01-16 10:26:49 -08002125 caller()->AddTrack(
deadbeef1dcb1642017-03-29 21:08:16 -07002126 caller()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_90));
Steve Anton15324772018-01-16 10:26:49 -08002127 callee()->AddTrack(
deadbeef1dcb1642017-03-29 21:08:16 -07002128 callee()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_270));
2129
2130 // Remove the CVO extension from the offered SDP.
2131 callee()->SetReceivedSdpMunger([](cricket::SessionDescription* desc) {
2132 cricket::VideoContentDescription* video =
2133 GetFirstVideoContentDescription(desc);
2134 video->ClearRtpHeaderExtensions();
2135 });
2136 // Wait for video frames to be received by both sides.
2137 caller()->CreateAndSetAndSignalOffer();
2138 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2139 ASSERT_TRUE_WAIT(caller()->min_video_frames_received_per_track() > 0 &&
2140 callee()->min_video_frames_received_per_track() > 0,
2141 kMaxWaitForFramesMs);
2142
2143 // Expect that the aspect ratio is inversed to account for the 90/270 degree
2144 // rotation.
2145 // TODO(deadbeef): Where does 4:3 come from? Should be explicit in the test,
2146 // not just assumed.
2147 EXPECT_EQ(3.0 / 4, caller()->local_rendered_aspect_ratio());
2148 EXPECT_EQ(3.0 / 4, caller()->rendered_aspect_ratio());
2149 EXPECT_EQ(3.0 / 4, callee()->local_rendered_aspect_ratio());
2150 EXPECT_EQ(3.0 / 4, callee()->rendered_aspect_ratio());
2151 // Expect that each endpoint is unaware of the rotation of the other endpoint.
2152 EXPECT_EQ(webrtc::kVideoRotation_0, caller()->rendered_rotation());
2153 EXPECT_EQ(webrtc::kVideoRotation_0, callee()->rendered_rotation());
2154}
2155
deadbeef1dcb1642017-03-29 21:08:16 -07002156// Test that if the answerer rejects the audio m= section, no audio is sent or
2157// received, but video still can be.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002158TEST_P(PeerConnectionIntegrationTest, AnswererRejectsAudioSection) {
deadbeef1dcb1642017-03-29 21:08:16 -07002159 ASSERT_TRUE(CreatePeerConnectionWrappers());
2160 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002161 caller()->AddAudioVideoTracks();
Seth Hampson2f0d7022018-02-20 11:54:42 -08002162 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2163 // Only add video track for callee, and set offer_to_receive_audio to 0, so
2164 // it will reject the audio m= section completely.
2165 PeerConnectionInterface::RTCOfferAnswerOptions options;
2166 options.offer_to_receive_audio = 0;
2167 callee()->SetOfferAnswerOptions(options);
2168 } else {
2169 // Stopping the audio RtpTransceiver will cause the media section to be
2170 // rejected in the answer.
2171 callee()->SetRemoteOfferHandler([this] {
2172 callee()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_AUDIO)->Stop();
2173 });
2174 }
Steve Anton15324772018-01-16 10:26:49 -08002175 callee()->AddTrack(callee()->CreateLocalVideoTrack());
deadbeef1dcb1642017-03-29 21:08:16 -07002176 // Do offer/answer and wait for successful end-to-end video frames.
2177 caller()->CreateAndSetAndSignalOffer();
2178 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002179 MediaExpectations media_expectations;
2180 media_expectations.ExpectBidirectionalVideo();
2181 media_expectations.ExpectNoAudio();
2182 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2183
deadbeef1dcb1642017-03-29 21:08:16 -07002184 // Sanity check that the callee's description has a rejected audio section.
2185 ASSERT_NE(nullptr, callee()->pc()->local_description());
2186 const ContentInfo* callee_audio_content =
2187 GetFirstAudioContent(callee()->pc()->local_description()->description());
2188 ASSERT_NE(nullptr, callee_audio_content);
2189 EXPECT_TRUE(callee_audio_content->rejected);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002190 if (sdp_semantics_ == SdpSemantics::kUnifiedPlan) {
2191 // The caller's transceiver should have stopped after receiving the answer.
2192 EXPECT_TRUE(caller()
2193 ->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_AUDIO)
2194 ->stopped());
2195 }
deadbeef1dcb1642017-03-29 21:08:16 -07002196}
2197
2198// Test that if the answerer rejects the video m= section, no video is sent or
2199// received, but audio still can be.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002200TEST_P(PeerConnectionIntegrationTest, AnswererRejectsVideoSection) {
deadbeef1dcb1642017-03-29 21:08:16 -07002201 ASSERT_TRUE(CreatePeerConnectionWrappers());
2202 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002203 caller()->AddAudioVideoTracks();
Seth Hampson2f0d7022018-02-20 11:54:42 -08002204 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2205 // Only add audio track for callee, and set offer_to_receive_video to 0, so
2206 // it will reject the video m= section completely.
2207 PeerConnectionInterface::RTCOfferAnswerOptions options;
2208 options.offer_to_receive_video = 0;
2209 callee()->SetOfferAnswerOptions(options);
2210 } else {
2211 // Stopping the video RtpTransceiver will cause the media section to be
2212 // rejected in the answer.
2213 callee()->SetRemoteOfferHandler([this] {
2214 callee()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)->Stop();
2215 });
2216 }
Steve Anton15324772018-01-16 10:26:49 -08002217 callee()->AddTrack(callee()->CreateLocalAudioTrack());
deadbeef1dcb1642017-03-29 21:08:16 -07002218 // Do offer/answer and wait for successful end-to-end audio frames.
2219 caller()->CreateAndSetAndSignalOffer();
2220 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002221 MediaExpectations media_expectations;
2222 media_expectations.ExpectBidirectionalAudio();
2223 media_expectations.ExpectNoVideo();
2224 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2225
deadbeef1dcb1642017-03-29 21:08:16 -07002226 // Sanity check that the callee's description has a rejected video section.
2227 ASSERT_NE(nullptr, callee()->pc()->local_description());
2228 const ContentInfo* callee_video_content =
2229 GetFirstVideoContent(callee()->pc()->local_description()->description());
2230 ASSERT_NE(nullptr, callee_video_content);
2231 EXPECT_TRUE(callee_video_content->rejected);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002232 if (sdp_semantics_ == SdpSemantics::kUnifiedPlan) {
2233 // The caller's transceiver should have stopped after receiving the answer.
2234 EXPECT_TRUE(caller()
2235 ->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)
2236 ->stopped());
2237 }
deadbeef1dcb1642017-03-29 21:08:16 -07002238}
2239
2240// Test that if the answerer rejects both audio and video m= sections, nothing
2241// bad happens.
2242// TODO(deadbeef): Test that a data channel still works. Currently this doesn't
2243// test anything but the fact that negotiation succeeds, which doesn't mean
2244// much.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002245TEST_P(PeerConnectionIntegrationTest, AnswererRejectsAudioAndVideoSections) {
deadbeef1dcb1642017-03-29 21:08:16 -07002246 ASSERT_TRUE(CreatePeerConnectionWrappers());
2247 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002248 caller()->AddAudioVideoTracks();
Seth Hampson2f0d7022018-02-20 11:54:42 -08002249 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2250 // Don't give the callee any tracks, and set offer_to_receive_X to 0, so it
2251 // will reject both audio and video m= sections.
2252 PeerConnectionInterface::RTCOfferAnswerOptions options;
2253 options.offer_to_receive_audio = 0;
2254 options.offer_to_receive_video = 0;
2255 callee()->SetOfferAnswerOptions(options);
2256 } else {
2257 callee()->SetRemoteOfferHandler([this] {
2258 // Stopping all transceivers will cause all media sections to be rejected.
Mirko Bonadei739baf02019-01-27 17:29:42 +01002259 for (const auto& transceiver : callee()->pc()->GetTransceivers()) {
Seth Hampson2f0d7022018-02-20 11:54:42 -08002260 transceiver->Stop();
2261 }
2262 });
2263 }
deadbeef1dcb1642017-03-29 21:08:16 -07002264 // Do offer/answer and wait for stable signaling state.
2265 caller()->CreateAndSetAndSignalOffer();
2266 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002267
deadbeef1dcb1642017-03-29 21:08:16 -07002268 // Sanity check that the callee's description has rejected m= sections.
2269 ASSERT_NE(nullptr, callee()->pc()->local_description());
2270 const ContentInfo* callee_audio_content =
2271 GetFirstAudioContent(callee()->pc()->local_description()->description());
2272 ASSERT_NE(nullptr, callee_audio_content);
2273 EXPECT_TRUE(callee_audio_content->rejected);
2274 const ContentInfo* callee_video_content =
2275 GetFirstVideoContent(callee()->pc()->local_description()->description());
2276 ASSERT_NE(nullptr, callee_video_content);
2277 EXPECT_TRUE(callee_video_content->rejected);
2278}
2279
2280// This test sets up an audio and video call between two parties. After the
2281// call runs for a while, the caller sends an updated offer with video being
2282// rejected. Once the re-negotiation is done, the video flow should stop and
2283// the audio flow should continue.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002284TEST_P(PeerConnectionIntegrationTest, VideoRejectedInSubsequentOffer) {
deadbeef1dcb1642017-03-29 21:08:16 -07002285 ASSERT_TRUE(CreatePeerConnectionWrappers());
2286 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002287 caller()->AddAudioVideoTracks();
2288 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002289 caller()->CreateAndSetAndSignalOffer();
2290 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002291 {
2292 MediaExpectations media_expectations;
2293 media_expectations.ExpectBidirectionalAudioAndVideo();
2294 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2295 }
deadbeef1dcb1642017-03-29 21:08:16 -07002296 // Renegotiate, rejecting the video m= section.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002297 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2298 caller()->SetGeneratedSdpMunger(
2299 [](cricket::SessionDescription* description) {
2300 for (cricket::ContentInfo& content : description->contents()) {
2301 if (cricket::IsVideoContent(&content)) {
2302 content.rejected = true;
2303 }
2304 }
2305 });
2306 } else {
2307 caller()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)->Stop();
2308 }
deadbeef1dcb1642017-03-29 21:08:16 -07002309 caller()->CreateAndSetAndSignalOffer();
2310 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
2311
2312 // Sanity check that the caller's description has a rejected video section.
2313 ASSERT_NE(nullptr, caller()->pc()->local_description());
2314 const ContentInfo* caller_video_content =
2315 GetFirstVideoContent(caller()->pc()->local_description()->description());
2316 ASSERT_NE(nullptr, caller_video_content);
2317 EXPECT_TRUE(caller_video_content->rejected);
deadbeef1dcb1642017-03-29 21:08:16 -07002318 // Wait for some additional audio frames to be received.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002319 {
2320 MediaExpectations media_expectations;
2321 media_expectations.ExpectBidirectionalAudio();
2322 media_expectations.ExpectNoVideo();
2323 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2324 }
deadbeef1dcb1642017-03-29 21:08:16 -07002325}
2326
Taylor Brandstetter60c8dc82018-04-11 15:20:27 -07002327// Do one offer/answer with audio, another that disables it (rejecting the m=
2328// section), and another that re-enables it. Regression test for:
2329// bugs.webrtc.org/6023
2330TEST_F(PeerConnectionIntegrationTestPlanB, EnableAudioAfterRejecting) {
2331 ASSERT_TRUE(CreatePeerConnectionWrappers());
2332 ConnectFakeSignaling();
2333
2334 // Add audio track, do normal offer/answer.
2335 rtc::scoped_refptr<webrtc::AudioTrackInterface> track =
2336 caller()->CreateLocalAudioTrack();
2337 rtc::scoped_refptr<webrtc::RtpSenderInterface> sender =
2338 caller()->pc()->AddTrack(track, {"stream"}).MoveValue();
2339 caller()->CreateAndSetAndSignalOffer();
2340 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2341
2342 // Remove audio track, and set offer_to_receive_audio to false to cause the
2343 // m= section to be completely disabled, not just "recvonly".
2344 caller()->pc()->RemoveTrack(sender);
2345 PeerConnectionInterface::RTCOfferAnswerOptions options;
2346 options.offer_to_receive_audio = 0;
2347 caller()->SetOfferAnswerOptions(options);
2348 caller()->CreateAndSetAndSignalOffer();
2349 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2350
2351 // Add the audio track again, expecting negotiation to succeed and frames to
2352 // flow.
2353 sender = caller()->pc()->AddTrack(track, {"stream"}).MoveValue();
2354 options.offer_to_receive_audio = 1;
2355 caller()->SetOfferAnswerOptions(options);
2356 caller()->CreateAndSetAndSignalOffer();
2357 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2358
2359 MediaExpectations media_expectations;
2360 media_expectations.CalleeExpectsSomeAudio();
2361 EXPECT_TRUE(ExpectNewFrames(media_expectations));
2362}
2363
deadbeef1dcb1642017-03-29 21:08:16 -07002364// Basic end-to-end test, but without SSRC/MSID signaling. This functionality
2365// is needed to support legacy endpoints.
2366// TODO(deadbeef): When we support the MID extension and demuxing on MID, also
2367// add a test for an end-to-end test without MID signaling either (basically,
2368// the minimum acceptable SDP).
Seth Hampson2f0d7022018-02-20 11:54:42 -08002369TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithoutSsrcOrMsidSignaling) {
deadbeef1dcb1642017-03-29 21:08:16 -07002370 ASSERT_TRUE(CreatePeerConnectionWrappers());
2371 ConnectFakeSignaling();
2372 // Add audio and video, testing that packets can be demuxed on payload type.
Steve Anton15324772018-01-16 10:26:49 -08002373 caller()->AddAudioVideoTracks();
2374 callee()->AddAudioVideoTracks();
deadbeefd8ad7882017-04-18 16:01:17 -07002375 // Remove SSRCs and MSIDs from the received offer SDP.
2376 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
deadbeef1dcb1642017-03-29 21:08:16 -07002377 caller()->CreateAndSetAndSignalOffer();
2378 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002379 MediaExpectations media_expectations;
2380 media_expectations.ExpectBidirectionalAudioAndVideo();
2381 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002382}
2383
Seth Hampson5897a6e2018-04-03 11:16:33 -07002384// Basic end-to-end test, without SSRC signaling. This means that the track
2385// was created properly and frames are delivered when the MSIDs are communicated
2386// with a=msid lines and no a=ssrc lines.
2387TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
2388 EndToEndCallWithoutSsrcSignaling) {
2389 const char kStreamId[] = "streamId";
2390 ASSERT_TRUE(CreatePeerConnectionWrappers());
2391 ConnectFakeSignaling();
2392 // Add just audio tracks.
2393 caller()->AddTrack(caller()->CreateLocalAudioTrack(), {kStreamId});
2394 callee()->AddAudioTrack();
2395
2396 // Remove SSRCs from the received offer SDP.
2397 callee()->SetReceivedSdpMunger(RemoveSsrcsAndKeepMsids);
2398 caller()->CreateAndSetAndSignalOffer();
2399 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2400 MediaExpectations media_expectations;
2401 media_expectations.ExpectBidirectionalAudio();
2402 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2403}
2404
Steve Antondf527fd2018-04-27 15:52:03 -07002405// Tests that video flows between multiple video tracks when SSRCs are not
2406// signaled. This exercises the MID RTP header extension which is needed to
2407// demux the incoming video tracks.
2408TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
2409 EndToEndCallWithTwoVideoTracksAndNoSignaledSsrc) {
2410 ASSERT_TRUE(CreatePeerConnectionWrappers());
2411 ConnectFakeSignaling();
2412 caller()->AddVideoTrack();
2413 caller()->AddVideoTrack();
2414 callee()->AddVideoTrack();
2415 callee()->AddVideoTrack();
2416
2417 caller()->SetReceivedSdpMunger(&RemoveSsrcsAndKeepMsids);
2418 callee()->SetReceivedSdpMunger(&RemoveSsrcsAndKeepMsids);
2419 caller()->CreateAndSetAndSignalOffer();
2420 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2421 ASSERT_EQ(2u, caller()->pc()->GetReceivers().size());
2422 ASSERT_EQ(2u, callee()->pc()->GetReceivers().size());
2423
2424 // Expect video to be received in both directions on both tracks.
2425 MediaExpectations media_expectations;
2426 media_expectations.ExpectBidirectionalVideo();
2427 EXPECT_TRUE(ExpectNewFrames(media_expectations));
2428}
2429
Henrik Boström5b147782018-12-04 11:25:05 +01002430TEST_F(PeerConnectionIntegrationTestUnifiedPlan, NoStreamsMsidLinePresent) {
2431 ASSERT_TRUE(CreatePeerConnectionWrappers());
2432 ConnectFakeSignaling();
2433 caller()->AddAudioTrack();
2434 caller()->AddVideoTrack();
2435 caller()->CreateAndSetAndSignalOffer();
2436 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2437 auto callee_receivers = callee()->pc()->GetReceivers();
2438 ASSERT_EQ(2u, callee_receivers.size());
2439 EXPECT_TRUE(callee_receivers[0]->stream_ids().empty());
2440 EXPECT_TRUE(callee_receivers[1]->stream_ids().empty());
2441}
2442
2443TEST_F(PeerConnectionIntegrationTestUnifiedPlan, NoStreamsMsidLineMissing) {
2444 ASSERT_TRUE(CreatePeerConnectionWrappers());
2445 ConnectFakeSignaling();
2446 caller()->AddAudioTrack();
2447 caller()->AddVideoTrack();
2448 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
2449 caller()->CreateAndSetAndSignalOffer();
2450 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2451 auto callee_receivers = callee()->pc()->GetReceivers();
2452 ASSERT_EQ(2u, callee_receivers.size());
2453 ASSERT_EQ(1u, callee_receivers[0]->stream_ids().size());
2454 ASSERT_EQ(1u, callee_receivers[1]->stream_ids().size());
2455 EXPECT_EQ(callee_receivers[0]->stream_ids()[0],
2456 callee_receivers[1]->stream_ids()[0]);
2457 EXPECT_EQ(callee_receivers[0]->streams()[0],
2458 callee_receivers[1]->streams()[0]);
2459}
2460
deadbeef1dcb1642017-03-29 21:08:16 -07002461// Test that if two video tracks are sent (from caller to callee, in this test),
2462// they're transmitted correctly end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002463TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithTwoVideoTracks) {
deadbeef1dcb1642017-03-29 21:08:16 -07002464 ASSERT_TRUE(CreatePeerConnectionWrappers());
2465 ConnectFakeSignaling();
2466 // Add one audio/video stream, and one video-only stream.
Steve Anton15324772018-01-16 10:26:49 -08002467 caller()->AddAudioVideoTracks();
2468 caller()->AddVideoTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07002469 caller()->CreateAndSetAndSignalOffer();
2470 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Steve Anton15324772018-01-16 10:26:49 -08002471 ASSERT_EQ(3u, callee()->pc()->GetReceivers().size());
Seth Hampson2f0d7022018-02-20 11:54:42 -08002472
2473 MediaExpectations media_expectations;
2474 media_expectations.CalleeExpectsSomeAudioAndVideo();
2475 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002476}
2477
2478static void MakeSpecCompliantMaxBundleOffer(cricket::SessionDescription* desc) {
2479 bool first = true;
2480 for (cricket::ContentInfo& content : desc->contents()) {
2481 if (first) {
2482 first = false;
2483 continue;
2484 }
2485 content.bundle_only = true;
2486 }
2487 first = true;
2488 for (cricket::TransportInfo& transport : desc->transport_infos()) {
2489 if (first) {
2490 first = false;
2491 continue;
2492 }
2493 transport.description.ice_ufrag.clear();
2494 transport.description.ice_pwd.clear();
2495 transport.description.connection_role = cricket::CONNECTIONROLE_NONE;
2496 transport.description.identity_fingerprint.reset(nullptr);
2497 }
2498}
2499
2500// Test that if applying a true "max bundle" offer, which uses ports of 0,
2501// "a=bundle-only", omitting "a=fingerprint", "a=setup", "a=ice-ufrag" and
2502// "a=ice-pwd" for all but the audio "m=" section, negotiation still completes
2503// successfully and media flows.
2504// TODO(deadbeef): Update this test to also omit "a=rtcp-mux", once that works.
2505// TODO(deadbeef): Won't need this test once we start generating actual
2506// standards-compliant SDP.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002507TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07002508 EndToEndCallWithSpecCompliantMaxBundleOffer) {
2509 ASSERT_TRUE(CreatePeerConnectionWrappers());
2510 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002511 caller()->AddAudioVideoTracks();
2512 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002513 // Do the equivalent of setting the port to 0, adding a=bundle-only, and
2514 // removing a=ice-ufrag, a=ice-pwd, a=fingerprint and a=setup from all
2515 // but the first m= section.
2516 callee()->SetReceivedSdpMunger(MakeSpecCompliantMaxBundleOffer);
2517 caller()->CreateAndSetAndSignalOffer();
2518 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002519 MediaExpectations media_expectations;
2520 media_expectations.ExpectBidirectionalAudioAndVideo();
2521 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002522}
2523
2524// Test that we can receive the audio output level from a remote audio track.
2525// TODO(deadbeef): Use a fake audio source and verify that the output level is
2526// exactly what the source on the other side was configured with.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002527TEST_P(PeerConnectionIntegrationTest, GetAudioOutputLevelStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-29 21:08:16 -07002528 ASSERT_TRUE(CreatePeerConnectionWrappers());
2529 ConnectFakeSignaling();
2530 // Just add an audio track.
Steve Anton15324772018-01-16 10:26:49 -08002531 caller()->AddAudioTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07002532 caller()->CreateAndSetAndSignalOffer();
2533 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2534
2535 // Get the audio output level stats. Note that the level is not available
2536 // until an RTCP packet has been received.
deadbeefd8ad7882017-04-18 16:01:17 -07002537 EXPECT_TRUE_WAIT(callee()->OldGetStats()->AudioOutputLevel() > 0,
deadbeef1dcb1642017-03-29 21:08:16 -07002538 kMaxWaitForFramesMs);
2539}
2540
2541// Test that an audio input level is reported.
2542// TODO(deadbeef): Use a fake audio source and verify that the input level is
2543// exactly what the source was configured with.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002544TEST_P(PeerConnectionIntegrationTest, GetAudioInputLevelStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-29 21:08:16 -07002545 ASSERT_TRUE(CreatePeerConnectionWrappers());
2546 ConnectFakeSignaling();
2547 // Just add an audio track.
Steve Anton15324772018-01-16 10:26:49 -08002548 caller()->AddAudioTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07002549 caller()->CreateAndSetAndSignalOffer();
2550 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2551
2552 // Get the audio input level stats. The level should be available very
2553 // soon after the test starts.
deadbeefd8ad7882017-04-18 16:01:17 -07002554 EXPECT_TRUE_WAIT(caller()->OldGetStats()->AudioInputLevel() > 0,
deadbeef1dcb1642017-03-29 21:08:16 -07002555 kMaxWaitForStatsMs);
2556}
2557
2558// Test that we can get incoming byte counts from both audio and video tracks.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002559TEST_P(PeerConnectionIntegrationTest, GetBytesReceivedStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-29 21:08:16 -07002560 ASSERT_TRUE(CreatePeerConnectionWrappers());
2561 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002562 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002563 // Do offer/answer, wait for the callee to receive some frames.
2564 caller()->CreateAndSetAndSignalOffer();
2565 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002566
2567 MediaExpectations media_expectations;
2568 media_expectations.CalleeExpectsSomeAudioAndVideo();
2569 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002570
2571 // Get a handle to the remote tracks created, so they can be used as GetStats
2572 // filters.
Mirko Bonadei739baf02019-01-27 17:29:42 +01002573 for (const auto& receiver : callee()->pc()->GetReceivers()) {
Steve Anton15324772018-01-16 10:26:49 -08002574 // We received frames, so we definitely should have nonzero "received bytes"
2575 // stats at this point.
2576 EXPECT_GT(callee()->OldGetStatsForTrack(receiver->track())->BytesReceived(),
2577 0);
2578 }
deadbeef1dcb1642017-03-29 21:08:16 -07002579}
2580
2581// Test that we can get outgoing byte counts from both audio and video tracks.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002582TEST_P(PeerConnectionIntegrationTest, GetBytesSentStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-29 21:08:16 -07002583 ASSERT_TRUE(CreatePeerConnectionWrappers());
2584 ConnectFakeSignaling();
2585 auto audio_track = caller()->CreateLocalAudioTrack();
2586 auto video_track = caller()->CreateLocalVideoTrack();
Steve Anton15324772018-01-16 10:26:49 -08002587 caller()->AddTrack(audio_track);
2588 caller()->AddTrack(video_track);
deadbeef1dcb1642017-03-29 21:08:16 -07002589 // Do offer/answer, wait for the callee to receive some frames.
2590 caller()->CreateAndSetAndSignalOffer();
2591 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002592 MediaExpectations media_expectations;
2593 media_expectations.CalleeExpectsSomeAudioAndVideo();
2594 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002595
2596 // The callee received frames, so we definitely should have nonzero "sent
2597 // bytes" stats at this point.
deadbeefd8ad7882017-04-18 16:01:17 -07002598 EXPECT_GT(caller()->OldGetStatsForTrack(audio_track)->BytesSent(), 0);
2599 EXPECT_GT(caller()->OldGetStatsForTrack(video_track)->BytesSent(), 0);
2600}
2601
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002602// Test that we can get capture start ntp time.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002603TEST_P(PeerConnectionIntegrationTest, GetCaptureStartNtpTimeWithOldStatsApi) {
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002604 ASSERT_TRUE(CreatePeerConnectionWrappers());
2605 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002606 caller()->AddAudioTrack();
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002607
Steve Anton15324772018-01-16 10:26:49 -08002608 callee()->AddAudioTrack();
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002609
2610 // Do offer/answer, wait for the callee to receive some frames.
2611 caller()->CreateAndSetAndSignalOffer();
2612 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2613
2614 // Get the remote audio track created on the receiver, so they can be used as
2615 // GetStats filters.
Steve Antonfc853712018-03-01 13:48:58 -08002616 auto receivers = callee()->pc()->GetReceivers();
2617 ASSERT_EQ(1u, receivers.size());
2618 auto remote_audio_track = receivers[0]->track();
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002619
2620 // Get the audio output level stats. Note that the level is not available
2621 // until an RTCP packet has been received.
Zhi Huange830e682018-03-30 10:48:35 -07002622 EXPECT_TRUE_WAIT(
2623 callee()->OldGetStatsForTrack(remote_audio_track)->CaptureStartNtpTime() >
2624 0,
2625 2 * kMaxWaitForFramesMs);
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002626}
2627
Steve Antona41959e2018-11-28 11:15:33 -08002628// Test that the track ID is associated with all local and remote SSRC stats
2629// using the old GetStats() and more than 1 audio and more than 1 video track.
2630// This is a regression test for crbug.com/906988
2631TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
2632 OldGetStatsAssociatesTrackIdForManyMediaSections) {
2633 ASSERT_TRUE(CreatePeerConnectionWrappers());
2634 ConnectFakeSignaling();
2635 auto audio_sender_1 = caller()->AddAudioTrack();
2636 auto video_sender_1 = caller()->AddVideoTrack();
2637 auto audio_sender_2 = caller()->AddAudioTrack();
2638 auto video_sender_2 = caller()->AddVideoTrack();
2639 caller()->CreateAndSetAndSignalOffer();
2640 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2641
2642 MediaExpectations media_expectations;
2643 media_expectations.CalleeExpectsSomeAudioAndVideo();
2644 ASSERT_TRUE_WAIT(ExpectNewFrames(media_expectations), kDefaultTimeout);
2645
2646 std::vector<std::string> track_ids = {
2647 audio_sender_1->track()->id(), video_sender_1->track()->id(),
2648 audio_sender_2->track()->id(), video_sender_2->track()->id()};
2649
2650 auto caller_stats = caller()->OldGetStats();
2651 EXPECT_THAT(caller_stats->TrackIds(), UnorderedElementsAreArray(track_ids));
2652 auto callee_stats = callee()->OldGetStats();
2653 EXPECT_THAT(callee_stats->TrackIds(), UnorderedElementsAreArray(track_ids));
2654}
2655
Steve Antonffa6ce42018-11-30 09:26:08 -08002656// Test that the new GetStats() returns stats for all outgoing/incoming streams
2657// with the correct track IDs if there are more than one audio and more than one
2658// video senders/receivers.
2659TEST_P(PeerConnectionIntegrationTest, NewGetStatsManyAudioAndManyVideoStreams) {
2660 ASSERT_TRUE(CreatePeerConnectionWrappers());
2661 ConnectFakeSignaling();
2662 auto audio_sender_1 = caller()->AddAudioTrack();
2663 auto video_sender_1 = caller()->AddVideoTrack();
2664 auto audio_sender_2 = caller()->AddAudioTrack();
2665 auto video_sender_2 = caller()->AddVideoTrack();
2666 caller()->CreateAndSetAndSignalOffer();
2667 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2668
2669 MediaExpectations media_expectations;
2670 media_expectations.CalleeExpectsSomeAudioAndVideo();
2671 ASSERT_TRUE_WAIT(ExpectNewFrames(media_expectations), kDefaultTimeout);
2672
2673 std::vector<std::string> track_ids = {
2674 audio_sender_1->track()->id(), video_sender_1->track()->id(),
2675 audio_sender_2->track()->id(), video_sender_2->track()->id()};
2676
2677 rtc::scoped_refptr<const webrtc::RTCStatsReport> caller_report =
2678 caller()->NewGetStats();
2679 ASSERT_TRUE(caller_report);
2680 auto outbound_stream_stats =
2681 caller_report->GetStatsOfType<webrtc::RTCOutboundRTPStreamStats>();
2682 ASSERT_EQ(4u, outbound_stream_stats.size());
2683 std::vector<std::string> outbound_track_ids;
2684 for (const auto& stat : outbound_stream_stats) {
2685 ASSERT_TRUE(stat->bytes_sent.is_defined());
2686 EXPECT_LT(0u, *stat->bytes_sent);
2687 ASSERT_TRUE(stat->track_id.is_defined());
2688 const auto* track_stat =
2689 caller_report->GetAs<webrtc::RTCMediaStreamTrackStats>(*stat->track_id);
2690 ASSERT_TRUE(track_stat);
2691 outbound_track_ids.push_back(*track_stat->track_identifier);
2692 }
2693 EXPECT_THAT(outbound_track_ids, UnorderedElementsAreArray(track_ids));
2694
2695 rtc::scoped_refptr<const webrtc::RTCStatsReport> callee_report =
2696 callee()->NewGetStats();
2697 ASSERT_TRUE(callee_report);
2698 auto inbound_stream_stats =
2699 callee_report->GetStatsOfType<webrtc::RTCInboundRTPStreamStats>();
2700 ASSERT_EQ(4u, inbound_stream_stats.size());
2701 std::vector<std::string> inbound_track_ids;
2702 for (const auto& stat : inbound_stream_stats) {
2703 ASSERT_TRUE(stat->bytes_received.is_defined());
2704 EXPECT_LT(0u, *stat->bytes_received);
2705 ASSERT_TRUE(stat->track_id.is_defined());
2706 const auto* track_stat =
2707 callee_report->GetAs<webrtc::RTCMediaStreamTrackStats>(*stat->track_id);
2708 ASSERT_TRUE(track_stat);
2709 inbound_track_ids.push_back(*track_stat->track_identifier);
2710 }
2711 EXPECT_THAT(inbound_track_ids, UnorderedElementsAreArray(track_ids));
2712}
2713
2714// Test that we can get stats (using the new stats implementation) for
deadbeefd8ad7882017-04-18 16:01:17 -07002715// unsignaled streams. Meaning when SSRCs/MSIDs aren't signaled explicitly in
2716// SDP.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002717TEST_P(PeerConnectionIntegrationTest,
deadbeefd8ad7882017-04-18 16:01:17 -07002718 GetStatsForUnsignaledStreamWithNewStatsApi) {
2719 ASSERT_TRUE(CreatePeerConnectionWrappers());
2720 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002721 caller()->AddAudioTrack();
deadbeefd8ad7882017-04-18 16:01:17 -07002722 // Remove SSRCs and MSIDs from the received offer SDP.
2723 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
2724 caller()->CreateAndSetAndSignalOffer();
2725 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002726 MediaExpectations media_expectations;
2727 media_expectations.CalleeExpectsSomeAudio(1);
2728 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeefd8ad7882017-04-18 16:01:17 -07002729
2730 // We received a frame, so we should have nonzero "bytes received" stats for
2731 // the unsignaled stream, if stats are working for it.
2732 rtc::scoped_refptr<const webrtc::RTCStatsReport> report =
2733 callee()->NewGetStats();
2734 ASSERT_NE(nullptr, report);
2735 auto inbound_stream_stats =
2736 report->GetStatsOfType<webrtc::RTCInboundRTPStreamStats>();
2737 ASSERT_EQ(1U, inbound_stream_stats.size());
2738 ASSERT_TRUE(inbound_stream_stats[0]->bytes_received.is_defined());
2739 ASSERT_GT(*inbound_stream_stats[0]->bytes_received, 0U);
zhihuangf8164932017-05-19 13:09:47 -07002740 ASSERT_TRUE(inbound_stream_stats[0]->track_id.is_defined());
2741}
2742
Taylor Brandstettera4653442018-06-19 09:44:26 -07002743// Same as above but for the legacy stats implementation.
2744TEST_P(PeerConnectionIntegrationTest,
2745 GetStatsForUnsignaledStreamWithOldStatsApi) {
2746 ASSERT_TRUE(CreatePeerConnectionWrappers());
2747 ConnectFakeSignaling();
2748 caller()->AddAudioTrack();
2749 // Remove SSRCs and MSIDs from the received offer SDP.
2750 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
2751 caller()->CreateAndSetAndSignalOffer();
2752 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2753
2754 // Note that, since the old stats implementation associates SSRCs with tracks
2755 // using SDP, when SSRCs aren't signaled in SDP these stats won't have an
2756 // associated track ID. So we can't use the track "selector" argument.
2757 //
2758 // Also, we use "EXPECT_TRUE_WAIT" because the stats collector may decide to
2759 // return cached stats if not enough time has passed since the last update.
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02002760 EXPECT_TRUE_WAIT(callee()->OldGetStats()->BytesReceived() > 0,
Taylor Brandstettera4653442018-06-19 09:44:26 -07002761 kDefaultTimeout);
2762}
2763
zhihuangf8164932017-05-19 13:09:47 -07002764// Test that we can successfully get the media related stats (audio level
2765// etc.) for the unsignaled stream.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002766TEST_P(PeerConnectionIntegrationTest,
zhihuangf8164932017-05-19 13:09:47 -07002767 GetMediaStatsForUnsignaledStreamWithNewStatsApi) {
2768 ASSERT_TRUE(CreatePeerConnectionWrappers());
2769 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002770 caller()->AddAudioVideoTracks();
zhihuangf8164932017-05-19 13:09:47 -07002771 // Remove SSRCs and MSIDs from the received offer SDP.
2772 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
2773 caller()->CreateAndSetAndSignalOffer();
2774 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002775 MediaExpectations media_expectations;
2776 media_expectations.CalleeExpectsSomeAudio(1);
2777 media_expectations.CalleeExpectsSomeVideo(1);
2778 ASSERT_TRUE(ExpectNewFrames(media_expectations));
zhihuangf8164932017-05-19 13:09:47 -07002779
2780 rtc::scoped_refptr<const webrtc::RTCStatsReport> report =
2781 callee()->NewGetStats();
2782 ASSERT_NE(nullptr, report);
2783
2784 auto media_stats = report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
2785 auto audio_index = FindFirstMediaStatsIndexByKind("audio", media_stats);
2786 ASSERT_GE(audio_index, 0);
2787 EXPECT_TRUE(media_stats[audio_index]->audio_level.is_defined());
deadbeef1dcb1642017-03-29 21:08:16 -07002788}
2789
deadbeef4e2deab2017-09-20 13:56:21 -07002790// Helper for test below.
2791void ModifySsrcs(cricket::SessionDescription* desc) {
2792 for (ContentInfo& content : desc->contents()) {
Steve Antondf527fd2018-04-27 15:52:03 -07002793 for (StreamParams& stream :
Steve Antonb1c1de12017-12-21 15:14:30 -08002794 content.media_description()->mutable_streams()) {
deadbeef4e2deab2017-09-20 13:56:21 -07002795 for (uint32_t& ssrc : stream.ssrcs) {
2796 ssrc = rtc::CreateRandomId();
2797 }
2798 }
2799 }
2800}
2801
2802// Test that the "RTCMediaSteamTrackStats" object is updated correctly when
2803// SSRCs are unsignaled, and the SSRC of the received (audio) stream changes.
2804// This should result in two "RTCInboundRTPStreamStats", but only one
2805// "RTCMediaStreamTrackStats", whose counters go up continuously rather than
2806// being reset to 0 once the SSRC change occurs.
2807//
2808// Regression test for this bug:
2809// https://bugs.chromium.org/p/webrtc/issues/detail?id=8158
2810//
2811// The bug causes the track stats to only represent one of the two streams:
2812// whichever one has the higher SSRC. So with this bug, there was a 50% chance
2813// that the track stat counters would reset to 0 when the new stream is
2814// received, and a 50% chance that they'll stop updating (while
2815// "concealed_samples" continues increasing, due to silence being generated for
2816// the inactive stream).
Seth Hampson2f0d7022018-02-20 11:54:42 -08002817TEST_P(PeerConnectionIntegrationTest,
Steve Anton83119dd2017-11-10 16:19:52 -08002818 TrackStatsUpdatedCorrectlyWhenUnsignaledSsrcChanges) {
deadbeef4e2deab2017-09-20 13:56:21 -07002819 ASSERT_TRUE(CreatePeerConnectionWrappers());
2820 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002821 caller()->AddAudioTrack();
deadbeef4e2deab2017-09-20 13:56:21 -07002822 // Remove SSRCs and MSIDs from the received offer SDP, simulating an endpoint
2823 // that doesn't signal SSRCs (from the callee's perspective).
2824 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
2825 caller()->CreateAndSetAndSignalOffer();
2826 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2827 // Wait for 50 audio frames (500ms of audio) to be received by the callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002828 {
2829 MediaExpectations media_expectations;
2830 media_expectations.CalleeExpectsSomeAudio(50);
2831 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2832 }
deadbeef4e2deab2017-09-20 13:56:21 -07002833 // Some audio frames were received, so we should have nonzero "samples
2834 // received" for the track.
2835 rtc::scoped_refptr<const webrtc::RTCStatsReport> report =
2836 callee()->NewGetStats();
2837 ASSERT_NE(nullptr, report);
2838 auto track_stats = report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
2839 ASSERT_EQ(1U, track_stats.size());
2840 ASSERT_TRUE(track_stats[0]->total_samples_received.is_defined());
2841 ASSERT_GT(*track_stats[0]->total_samples_received, 0U);
2842 // uint64_t prev_samples_received = *track_stats[0]->total_samples_received;
2843
2844 // Create a new offer and munge it to cause the caller to use a new SSRC.
2845 caller()->SetGeneratedSdpMunger(ModifySsrcs);
2846 caller()->CreateAndSetAndSignalOffer();
2847 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2848 // Wait for 25 more audio frames (250ms of audio) to be received, from the new
2849 // SSRC.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002850 {
2851 MediaExpectations media_expectations;
2852 media_expectations.CalleeExpectsSomeAudio(25);
2853 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2854 }
deadbeef4e2deab2017-09-20 13:56:21 -07002855
2856 report = callee()->NewGetStats();
2857 ASSERT_NE(nullptr, report);
2858 track_stats = report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
2859 ASSERT_EQ(1U, track_stats.size());
2860 ASSERT_TRUE(track_stats[0]->total_samples_received.is_defined());
2861 // The "total samples received" stat should only be greater than it was
2862 // before.
2863 // TODO(deadbeef): Uncomment this assertion once the bug is completely fixed.
2864 // Right now, the new SSRC will cause the counters to reset to 0.
2865 // EXPECT_GT(*track_stats[0]->total_samples_received, prev_samples_received);
2866
2867 // Additionally, the percentage of concealed samples (samples generated to
Steve Anton83119dd2017-11-10 16:19:52 -08002868 // conceal packet loss) should be less than 50%. If it's greater, that's a
deadbeef4e2deab2017-09-20 13:56:21 -07002869 // good sign that we're seeing stats from the old stream that's no longer
2870 // receiving packets, and is generating concealed samples of silence.
Steve Anton83119dd2017-11-10 16:19:52 -08002871 constexpr double kAcceptableConcealedSamplesPercentage = 0.50;
deadbeef4e2deab2017-09-20 13:56:21 -07002872 ASSERT_TRUE(track_stats[0]->concealed_samples.is_defined());
2873 EXPECT_LT(*track_stats[0]->concealed_samples,
2874 *track_stats[0]->total_samples_received *
2875 kAcceptableConcealedSamplesPercentage);
2876
2877 // Also ensure that we have two "RTCInboundRTPStreamStats" as expected, as a
2878 // sanity check that the SSRC really changed.
2879 // TODO(deadbeef): This isn't working right now, because we're not returning
2880 // *any* stats for the inactive stream. Uncomment when the bug is completely
2881 // fixed.
2882 // auto inbound_stream_stats =
2883 // report->GetStatsOfType<webrtc::RTCInboundRTPStreamStats>();
2884 // ASSERT_EQ(2U, inbound_stream_stats.size());
2885}
2886
deadbeef1dcb1642017-03-29 21:08:16 -07002887// Test that DTLS 1.0 is used if both sides only support DTLS 1.0.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002888TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithDtls10) {
deadbeef1dcb1642017-03-29 21:08:16 -07002889 PeerConnectionFactory::Options dtls_10_options;
2890 dtls_10_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
2891 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(dtls_10_options,
2892 dtls_10_options));
2893 ConnectFakeSignaling();
2894 // Do normal offer/answer and wait for some frames to be received in each
2895 // direction.
Steve Anton15324772018-01-16 10:26:49 -08002896 caller()->AddAudioVideoTracks();
2897 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002898 caller()->CreateAndSetAndSignalOffer();
2899 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002900 MediaExpectations media_expectations;
2901 media_expectations.ExpectBidirectionalAudioAndVideo();
2902 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002903}
2904
2905// Test getting cipher stats and UMA metrics when DTLS 1.0 is negotiated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002906TEST_P(PeerConnectionIntegrationTest, Dtls10CipherStatsAndUmaMetrics) {
deadbeef1dcb1642017-03-29 21:08:16 -07002907 PeerConnectionFactory::Options dtls_10_options;
2908 dtls_10_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
2909 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(dtls_10_options,
2910 dtls_10_options));
2911 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002912 caller()->AddAudioVideoTracks();
2913 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002914 caller()->CreateAndSetAndSignalOffer();
Qingsi Wang7fc821d2018-07-12 12:54:53 -07002915 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07002916 EXPECT_TRUE_WAIT(rtc::SSLStreamAdapter::IsAcceptableCipher(
deadbeefd8ad7882017-04-18 16:01:17 -07002917 caller()->OldGetStats()->DtlsCipher(), rtc::KT_DEFAULT),
deadbeef1dcb1642017-03-29 21:08:16 -07002918 kDefaultTimeout);
2919 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
deadbeefd8ad7882017-04-18 16:01:17 -07002920 caller()->OldGetStats()->SrtpCipher(), kDefaultTimeout);
Qingsi Wang7fc821d2018-07-12 12:54:53 -07002921 // TODO(bugs.webrtc.org/9456): Fix it.
Alex Loiko9289eda2018-11-23 16:18:59 +00002922 EXPECT_EQ(1, webrtc::metrics::NumEvents(
Qingsi Wang7fc821d2018-07-12 12:54:53 -07002923 "WebRTC.PeerConnection.SrtpCryptoSuite.Audio",
2924 kDefaultSrtpCryptoSuite));
deadbeef1dcb1642017-03-29 21:08:16 -07002925}
2926
2927// Test getting cipher stats and UMA metrics when DTLS 1.2 is negotiated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002928TEST_P(PeerConnectionIntegrationTest, Dtls12CipherStatsAndUmaMetrics) {
deadbeef1dcb1642017-03-29 21:08:16 -07002929 PeerConnectionFactory::Options dtls_12_options;
2930 dtls_12_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
2931 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(dtls_12_options,
2932 dtls_12_options));
2933 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002934 caller()->AddAudioVideoTracks();
2935 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002936 caller()->CreateAndSetAndSignalOffer();
Qingsi Wang7fc821d2018-07-12 12:54:53 -07002937 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07002938 EXPECT_TRUE_WAIT(rtc::SSLStreamAdapter::IsAcceptableCipher(
deadbeefd8ad7882017-04-18 16:01:17 -07002939 caller()->OldGetStats()->DtlsCipher(), rtc::KT_DEFAULT),
deadbeef1dcb1642017-03-29 21:08:16 -07002940 kDefaultTimeout);
2941 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
deadbeefd8ad7882017-04-18 16:01:17 -07002942 caller()->OldGetStats()->SrtpCipher(), kDefaultTimeout);
Qingsi Wang7fc821d2018-07-12 12:54:53 -07002943 // TODO(bugs.webrtc.org/9456): Fix it.
Alex Loiko9289eda2018-11-23 16:18:59 +00002944 EXPECT_EQ(1, webrtc::metrics::NumEvents(
Qingsi Wang7fc821d2018-07-12 12:54:53 -07002945 "WebRTC.PeerConnection.SrtpCryptoSuite.Audio",
2946 kDefaultSrtpCryptoSuite));
deadbeef1dcb1642017-03-29 21:08:16 -07002947}
2948
2949// Test that DTLS 1.0 can be used if the caller supports DTLS 1.2 and the
2950// callee only supports 1.0.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002951TEST_P(PeerConnectionIntegrationTest, CallerDtls12ToCalleeDtls10) {
deadbeef1dcb1642017-03-29 21:08:16 -07002952 PeerConnectionFactory::Options caller_options;
2953 caller_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
2954 PeerConnectionFactory::Options callee_options;
2955 callee_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
2956 ASSERT_TRUE(
2957 CreatePeerConnectionWrappersWithOptions(caller_options, callee_options));
2958 ConnectFakeSignaling();
2959 // Do normal offer/answer and wait for some frames to be received in each
2960 // direction.
Steve Anton15324772018-01-16 10:26:49 -08002961 caller()->AddAudioVideoTracks();
2962 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002963 caller()->CreateAndSetAndSignalOffer();
2964 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002965 MediaExpectations media_expectations;
2966 media_expectations.ExpectBidirectionalAudioAndVideo();
2967 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002968}
2969
2970// Test that DTLS 1.0 can be used if the caller only supports DTLS 1.0 and the
2971// callee supports 1.2.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002972TEST_P(PeerConnectionIntegrationTest, CallerDtls10ToCalleeDtls12) {
deadbeef1dcb1642017-03-29 21:08:16 -07002973 PeerConnectionFactory::Options caller_options;
2974 caller_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
2975 PeerConnectionFactory::Options callee_options;
2976 callee_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
2977 ASSERT_TRUE(
2978 CreatePeerConnectionWrappersWithOptions(caller_options, callee_options));
2979 ConnectFakeSignaling();
2980 // Do normal offer/answer and wait for some frames to be received in each
2981 // direction.
Steve Anton15324772018-01-16 10:26:49 -08002982 caller()->AddAudioVideoTracks();
2983 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002984 caller()->CreateAndSetAndSignalOffer();
2985 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002986 MediaExpectations media_expectations;
2987 media_expectations.ExpectBidirectionalAudioAndVideo();
2988 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002989}
2990
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07002991// The three tests below verify that "enable_aes128_sha1_32_crypto_cipher"
2992// works as expected; the cipher should only be used if enabled by both sides.
2993TEST_P(PeerConnectionIntegrationTest,
2994 Aes128Sha1_32_CipherNotUsedWhenOnlyCallerSupported) {
2995 PeerConnectionFactory::Options caller_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07002996 caller_options.crypto_options.srtp.enable_aes128_sha1_32_crypto_cipher = true;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07002997 PeerConnectionFactory::Options callee_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07002998 callee_options.crypto_options.srtp.enable_aes128_sha1_32_crypto_cipher =
2999 false;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07003000 int expected_cipher_suite = rtc::SRTP_AES128_CM_SHA1_80;
3001 TestNegotiatedCipherSuite(caller_options, callee_options,
3002 expected_cipher_suite);
3003}
3004
3005TEST_P(PeerConnectionIntegrationTest,
3006 Aes128Sha1_32_CipherNotUsedWhenOnlyCalleeSupported) {
3007 PeerConnectionFactory::Options caller_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07003008 caller_options.crypto_options.srtp.enable_aes128_sha1_32_crypto_cipher =
3009 false;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07003010 PeerConnectionFactory::Options callee_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07003011 callee_options.crypto_options.srtp.enable_aes128_sha1_32_crypto_cipher = true;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07003012 int expected_cipher_suite = rtc::SRTP_AES128_CM_SHA1_80;
3013 TestNegotiatedCipherSuite(caller_options, callee_options,
3014 expected_cipher_suite);
3015}
3016
3017TEST_P(PeerConnectionIntegrationTest, Aes128Sha1_32_CipherUsedWhenSupported) {
3018 PeerConnectionFactory::Options caller_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07003019 caller_options.crypto_options.srtp.enable_aes128_sha1_32_crypto_cipher = true;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07003020 PeerConnectionFactory::Options callee_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07003021 callee_options.crypto_options.srtp.enable_aes128_sha1_32_crypto_cipher = true;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07003022 int expected_cipher_suite = rtc::SRTP_AES128_CM_SHA1_32;
3023 TestNegotiatedCipherSuite(caller_options, callee_options,
3024 expected_cipher_suite);
3025}
3026
deadbeef1dcb1642017-03-29 21:08:16 -07003027// Test that a non-GCM cipher is used if both sides only support non-GCM.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003028TEST_P(PeerConnectionIntegrationTest, NonGcmCipherUsedWhenGcmNotSupported) {
deadbeef1dcb1642017-03-29 21:08:16 -07003029 bool local_gcm_enabled = false;
3030 bool remote_gcm_enabled = false;
3031 int expected_cipher_suite = kDefaultSrtpCryptoSuite;
3032 TestGcmNegotiationUsesCipherSuite(local_gcm_enabled, remote_gcm_enabled,
3033 expected_cipher_suite);
3034}
3035
3036// Test that a GCM cipher is used if both ends support it.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003037TEST_P(PeerConnectionIntegrationTest, GcmCipherUsedWhenGcmSupported) {
deadbeef1dcb1642017-03-29 21:08:16 -07003038 bool local_gcm_enabled = true;
3039 bool remote_gcm_enabled = true;
3040 int expected_cipher_suite = kDefaultSrtpCryptoSuiteGcm;
3041 TestGcmNegotiationUsesCipherSuite(local_gcm_enabled, remote_gcm_enabled,
3042 expected_cipher_suite);
3043}
3044
3045// Test that GCM isn't used if only the offerer supports it.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003046TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07003047 NonGcmCipherUsedWhenOnlyCallerSupportsGcm) {
3048 bool local_gcm_enabled = true;
3049 bool remote_gcm_enabled = false;
3050 int expected_cipher_suite = kDefaultSrtpCryptoSuite;
3051 TestGcmNegotiationUsesCipherSuite(local_gcm_enabled, remote_gcm_enabled,
3052 expected_cipher_suite);
3053}
3054
3055// Test that GCM isn't used if only the answerer supports it.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003056TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07003057 NonGcmCipherUsedWhenOnlyCalleeSupportsGcm) {
3058 bool local_gcm_enabled = false;
3059 bool remote_gcm_enabled = true;
3060 int expected_cipher_suite = kDefaultSrtpCryptoSuite;
3061 TestGcmNegotiationUsesCipherSuite(local_gcm_enabled, remote_gcm_enabled,
3062 expected_cipher_suite);
3063}
3064
deadbeef7914b8c2017-04-21 03:23:33 -07003065// Verify that media can be transmitted end-to-end when GCM crypto suites are
3066// enabled. Note that the above tests, such as GcmCipherUsedWhenGcmSupported,
3067// only verify that a GCM cipher is negotiated, and not necessarily that SRTP
3068// works with it.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003069TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithGcmCipher) {
deadbeef7914b8c2017-04-21 03:23:33 -07003070 PeerConnectionFactory::Options gcm_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07003071 gcm_options.crypto_options.srtp.enable_gcm_crypto_suites = true;
deadbeef7914b8c2017-04-21 03:23:33 -07003072 ASSERT_TRUE(
3073 CreatePeerConnectionWrappersWithOptions(gcm_options, gcm_options));
3074 ConnectFakeSignaling();
3075 // Do normal offer/answer and wait for some frames to be received in each
3076 // direction.
Steve Anton15324772018-01-16 10:26:49 -08003077 caller()->AddAudioVideoTracks();
3078 callee()->AddAudioVideoTracks();
deadbeef7914b8c2017-04-21 03:23:33 -07003079 caller()->CreateAndSetAndSignalOffer();
3080 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08003081 MediaExpectations media_expectations;
3082 media_expectations.ExpectBidirectionalAudioAndVideo();
3083 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef7914b8c2017-04-21 03:23:33 -07003084}
3085
deadbeef1dcb1642017-03-29 21:08:16 -07003086// This test sets up a call between two parties with audio, video and an RTP
3087// data channel.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003088TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithRtpDataChannel) {
Niels Möllerf06f9232018-08-07 12:32:18 +02003089 PeerConnectionInterface::RTCConfiguration rtc_config;
3090 rtc_config.enable_rtp_data_channel = true;
3091 rtc_config.enable_dtls_srtp = false;
3092 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(rtc_config, rtc_config));
deadbeef1dcb1642017-03-29 21:08:16 -07003093 ConnectFakeSignaling();
3094 // Expect that data channel created on caller side will show up for callee as
3095 // well.
3096 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08003097 caller()->AddAudioVideoTracks();
3098 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003099 caller()->CreateAndSetAndSignalOffer();
3100 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3101 // Ensure the existence of the RTP data channel didn't impede audio/video.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003102 MediaExpectations media_expectations;
3103 media_expectations.ExpectBidirectionalAudioAndVideo();
3104 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003105 ASSERT_NE(nullptr, caller()->data_channel());
3106 ASSERT_NE(nullptr, callee()->data_channel());
3107 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3108 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3109
3110 // Ensure data can be sent in both directions.
3111 std::string data = "hello world";
3112 SendRtpDataWithRetries(caller()->data_channel(), data, 5);
3113 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3114 kDefaultTimeout);
3115 SendRtpDataWithRetries(callee()->data_channel(), data, 5);
3116 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3117 kDefaultTimeout);
3118}
3119
3120// Ensure that an RTP data channel is signaled as closed for the caller when
3121// the callee rejects it in a subsequent offer.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003122TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07003123 RtpDataChannelSignaledClosedInCalleeOffer) {
3124 // Same procedure as above test.
Niels Möllerf06f9232018-08-07 12:32:18 +02003125 PeerConnectionInterface::RTCConfiguration rtc_config;
3126 rtc_config.enable_rtp_data_channel = true;
3127 rtc_config.enable_dtls_srtp = false;
3128 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(rtc_config, rtc_config));
deadbeef1dcb1642017-03-29 21:08:16 -07003129 ConnectFakeSignaling();
3130 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08003131 caller()->AddAudioVideoTracks();
3132 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003133 caller()->CreateAndSetAndSignalOffer();
3134 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3135 ASSERT_NE(nullptr, caller()->data_channel());
3136 ASSERT_NE(nullptr, callee()->data_channel());
3137 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3138 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3139
3140 // Close the data channel on the callee, and do an updated offer/answer.
3141 callee()->data_channel()->Close();
3142 callee()->CreateAndSetAndSignalOffer();
3143 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3144 EXPECT_FALSE(caller()->data_observer()->IsOpen());
3145 EXPECT_FALSE(callee()->data_observer()->IsOpen());
3146}
3147
3148// Tests that data is buffered in an RTP data channel until an observer is
3149// registered for it.
3150//
3151// NOTE: RTP data channels can receive data before the underlying
3152// transport has detected that a channel is writable and thus data can be
3153// received before the data channel state changes to open. That is hard to test
3154// but the same buffering is expected to be used in that case.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003155TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07003156 DataBufferedUntilRtpDataChannelObserverRegistered) {
3157 // Use fake clock and simulated network delay so that we predictably can wait
3158 // until an SCTP message has been delivered without "sleep()"ing.
3159 rtc::ScopedFakeClock fake_clock;
3160 // Some things use a time of "0" as a special value, so we need to start out
3161 // the fake clock at a nonzero time.
3162 // TODO(deadbeef): Fix this.
Sebastian Jansson5f83cf02018-05-08 14:52:22 +02003163 fake_clock.AdvanceTime(webrtc::TimeDelta::seconds(1));
deadbeef1dcb1642017-03-29 21:08:16 -07003164 virtual_socket_server()->set_delay_mean(5); // 5 ms per hop.
3165 virtual_socket_server()->UpdateDelayDistribution();
3166
Niels Möllerf06f9232018-08-07 12:32:18 +02003167 PeerConnectionInterface::RTCConfiguration rtc_config;
3168 rtc_config.enable_rtp_data_channel = true;
3169 rtc_config.enable_dtls_srtp = false;
3170 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(rtc_config, rtc_config));
deadbeef1dcb1642017-03-29 21:08:16 -07003171 ConnectFakeSignaling();
3172 caller()->CreateDataChannel();
3173 caller()->CreateAndSetAndSignalOffer();
3174 ASSERT_TRUE(caller()->data_channel() != nullptr);
3175 ASSERT_TRUE_SIMULATED_WAIT(callee()->data_channel() != nullptr,
3176 kDefaultTimeout, fake_clock);
3177 ASSERT_TRUE_SIMULATED_WAIT(caller()->data_observer()->IsOpen(),
3178 kDefaultTimeout, fake_clock);
3179 ASSERT_EQ_SIMULATED_WAIT(DataChannelInterface::kOpen,
3180 callee()->data_channel()->state(), kDefaultTimeout,
3181 fake_clock);
3182
3183 // Unregister the observer which is normally automatically registered.
3184 callee()->data_channel()->UnregisterObserver();
3185 // Send data and advance fake clock until it should have been received.
3186 std::string data = "hello world";
3187 caller()->data_channel()->Send(DataBuffer(data));
3188 SIMULATED_WAIT(false, 50, fake_clock);
3189
3190 // Attach data channel and expect data to be received immediately. Note that
3191 // EXPECT_EQ_WAIT is used, such that the simulated clock is not advanced any
3192 // further, but data can be received even if the callback is asynchronous.
3193 MockDataChannelObserver new_observer(callee()->data_channel());
3194 EXPECT_EQ_SIMULATED_WAIT(data, new_observer.last_message(), kDefaultTimeout,
3195 fake_clock);
Seth Hampson1d4a76d2018-06-19 14:31:41 -07003196 // Closing the PeerConnections destroys the ports before the ScopedFakeClock.
3197 // If this is not done a DCHECK can be hit in ports.cc, because a large
3198 // negative number is calculated for the rtt due to the global clock changing.
3199 caller()->pc()->Close();
3200 callee()->pc()->Close();
deadbeef1dcb1642017-03-29 21:08:16 -07003201}
3202
3203// This test sets up a call between two parties with audio, video and but only
3204// the caller client supports RTP data channels.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003205TEST_P(PeerConnectionIntegrationTest, RtpDataChannelsRejectedByCallee) {
Niels Möllerf06f9232018-08-07 12:32:18 +02003206 PeerConnectionInterface::RTCConfiguration rtc_config_1;
3207 rtc_config_1.enable_rtp_data_channel = true;
deadbeef1dcb1642017-03-29 21:08:16 -07003208 // Must disable DTLS to make negotiation succeed.
Niels Möllerf06f9232018-08-07 12:32:18 +02003209 rtc_config_1.enable_dtls_srtp = false;
3210 PeerConnectionInterface::RTCConfiguration rtc_config_2;
3211 rtc_config_2.enable_dtls_srtp = false;
3212 rtc_config_2.enable_dtls_srtp = false;
3213 ASSERT_TRUE(
3214 CreatePeerConnectionWrappersWithConfig(rtc_config_1, rtc_config_2));
deadbeef1dcb1642017-03-29 21:08:16 -07003215 ConnectFakeSignaling();
3216 caller()->CreateDataChannel();
Harald Alvestrandf3736ed2019-04-08 13:09:30 +02003217 ASSERT_TRUE(caller()->data_channel() != nullptr);
Steve Anton15324772018-01-16 10:26:49 -08003218 caller()->AddAudioVideoTracks();
3219 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003220 caller()->CreateAndSetAndSignalOffer();
3221 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3222 // The caller should still have a data channel, but it should be closed, and
3223 // one should ever have been created for the callee.
3224 EXPECT_TRUE(caller()->data_channel() != nullptr);
3225 EXPECT_FALSE(caller()->data_observer()->IsOpen());
3226 EXPECT_EQ(nullptr, callee()->data_channel());
3227}
3228
3229// This test sets up a call between two parties with audio, and video. When
3230// audio and video is setup and flowing, an RTP data channel is negotiated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003231TEST_P(PeerConnectionIntegrationTest, AddRtpDataChannelInSubsequentOffer) {
Niels Möllerf06f9232018-08-07 12:32:18 +02003232 PeerConnectionInterface::RTCConfiguration rtc_config;
3233 rtc_config.enable_rtp_data_channel = true;
3234 rtc_config.enable_dtls_srtp = false;
3235 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(rtc_config, rtc_config));
deadbeef1dcb1642017-03-29 21:08:16 -07003236 ConnectFakeSignaling();
3237 // Do initial offer/answer with audio/video.
Steve Anton15324772018-01-16 10:26:49 -08003238 caller()->AddAudioVideoTracks();
3239 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003240 caller()->CreateAndSetAndSignalOffer();
3241 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3242 // Create data channel and do new offer and answer.
3243 caller()->CreateDataChannel();
3244 caller()->CreateAndSetAndSignalOffer();
3245 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3246 ASSERT_NE(nullptr, caller()->data_channel());
3247 ASSERT_NE(nullptr, callee()->data_channel());
3248 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3249 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3250 // Ensure data can be sent in both directions.
3251 std::string data = "hello world";
3252 SendRtpDataWithRetries(caller()->data_channel(), data, 5);
3253 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3254 kDefaultTimeout);
3255 SendRtpDataWithRetries(callee()->data_channel(), data, 5);
3256 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3257 kDefaultTimeout);
3258}
3259
3260#ifdef HAVE_SCTP
3261
3262// This test sets up a call between two parties with audio, video and an SCTP
3263// data channel.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003264TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithSctpDataChannel) {
deadbeef1dcb1642017-03-29 21:08:16 -07003265 ASSERT_TRUE(CreatePeerConnectionWrappers());
3266 ConnectFakeSignaling();
3267 // Expect that data channel created on caller side will show up for callee as
3268 // well.
3269 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08003270 caller()->AddAudioVideoTracks();
3271 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003272 caller()->CreateAndSetAndSignalOffer();
3273 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3274 // Ensure the existence of the SCTP data channel didn't impede audio/video.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003275 MediaExpectations media_expectations;
3276 media_expectations.ExpectBidirectionalAudioAndVideo();
3277 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003278 // Caller data channel should already exist (it created one). Callee data
3279 // channel may not exist yet, since negotiation happens in-band, not in SDP.
3280 ASSERT_NE(nullptr, caller()->data_channel());
3281 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3282 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3283 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3284
3285 // Ensure data can be sent in both directions.
3286 std::string data = "hello world";
3287 caller()->data_channel()->Send(DataBuffer(data));
3288 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3289 kDefaultTimeout);
3290 callee()->data_channel()->Send(DataBuffer(data));
3291 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3292 kDefaultTimeout);
3293}
3294
3295// Ensure that when the callee closes an SCTP data channel, the closing
3296// procedure results in the data channel being closed for the caller as well.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003297TEST_P(PeerConnectionIntegrationTest, CalleeClosesSctpDataChannel) {
deadbeef1dcb1642017-03-29 21:08:16 -07003298 // Same procedure as above test.
3299 ASSERT_TRUE(CreatePeerConnectionWrappers());
3300 ConnectFakeSignaling();
3301 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08003302 caller()->AddAudioVideoTracks();
3303 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003304 caller()->CreateAndSetAndSignalOffer();
3305 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3306 ASSERT_NE(nullptr, caller()->data_channel());
3307 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3308 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3309 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3310
3311 // Close the data channel on the callee side, and wait for it to reach the
3312 // "closed" state on both sides.
3313 callee()->data_channel()->Close();
3314 EXPECT_TRUE_WAIT(!caller()->data_observer()->IsOpen(), kDefaultTimeout);
3315 EXPECT_TRUE_WAIT(!callee()->data_observer()->IsOpen(), kDefaultTimeout);
3316}
3317
Seth Hampson2f0d7022018-02-20 11:54:42 -08003318TEST_P(PeerConnectionIntegrationTest, SctpDataChannelConfigSentToOtherSide) {
Steve Antonda6c0952017-10-23 11:41:54 -07003319 ASSERT_TRUE(CreatePeerConnectionWrappers());
3320 ConnectFakeSignaling();
3321 webrtc::DataChannelInit init;
3322 init.id = 53;
3323 init.maxRetransmits = 52;
3324 caller()->CreateDataChannel("data-channel", &init);
Steve Anton15324772018-01-16 10:26:49 -08003325 caller()->AddAudioVideoTracks();
3326 callee()->AddAudioVideoTracks();
Steve Antonda6c0952017-10-23 11:41:54 -07003327 caller()->CreateAndSetAndSignalOffer();
3328 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Steve Anton074dece2017-10-24 13:04:12 -07003329 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3330 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
Harald Alvestrand5c4d2ee2019-04-01 12:58:15 +02003331 // Since "negotiated" is false, the "id" parameter should be ignored.
3332 EXPECT_NE(init.id, callee()->data_channel()->id());
Steve Antonda6c0952017-10-23 11:41:54 -07003333 EXPECT_EQ("data-channel", callee()->data_channel()->label());
3334 EXPECT_EQ(init.maxRetransmits, callee()->data_channel()->maxRetransmits());
3335 EXPECT_FALSE(callee()->data_channel()->negotiated());
3336}
3337
deadbeef1dcb1642017-03-29 21:08:16 -07003338// Test usrsctp's ability to process unordered data stream, where data actually
3339// arrives out of order using simulated delays. Previously there have been some
3340// bugs in this area.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003341TEST_P(PeerConnectionIntegrationTest, StressTestUnorderedSctpDataChannel) {
deadbeef1dcb1642017-03-29 21:08:16 -07003342 // Introduce random network delays.
3343 // Otherwise it's not a true "unordered" test.
3344 virtual_socket_server()->set_delay_mean(20);
3345 virtual_socket_server()->set_delay_stddev(5);
3346 virtual_socket_server()->UpdateDelayDistribution();
3347 // Normal procedure, but with unordered data channel config.
3348 ASSERT_TRUE(CreatePeerConnectionWrappers());
3349 ConnectFakeSignaling();
3350 webrtc::DataChannelInit init;
3351 init.ordered = false;
3352 caller()->CreateDataChannel(&init);
3353 caller()->CreateAndSetAndSignalOffer();
3354 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3355 ASSERT_NE(nullptr, caller()->data_channel());
3356 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3357 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3358 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3359
3360 static constexpr int kNumMessages = 100;
3361 // Deliberately chosen to be larger than the MTU so messages get fragmented.
3362 static constexpr size_t kMaxMessageSize = 4096;
3363 // Create and send random messages.
3364 std::vector<std::string> sent_messages;
3365 for (int i = 0; i < kNumMessages; ++i) {
3366 size_t length =
3367 (rand() % kMaxMessageSize) + 1; // NOLINT (rand_r instead of rand)
3368 std::string message;
3369 ASSERT_TRUE(rtc::CreateRandomString(length, &message));
3370 caller()->data_channel()->Send(DataBuffer(message));
3371 callee()->data_channel()->Send(DataBuffer(message));
3372 sent_messages.push_back(message);
3373 }
3374
3375 // Wait for all messages to be received.
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02003376 EXPECT_EQ_WAIT(rtc::checked_cast<size_t>(kNumMessages),
deadbeef1dcb1642017-03-29 21:08:16 -07003377 caller()->data_observer()->received_message_count(),
3378 kDefaultTimeout);
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02003379 EXPECT_EQ_WAIT(rtc::checked_cast<size_t>(kNumMessages),
deadbeef1dcb1642017-03-29 21:08:16 -07003380 callee()->data_observer()->received_message_count(),
3381 kDefaultTimeout);
3382
3383 // Sort and compare to make sure none of the messages were corrupted.
3384 std::vector<std::string> caller_received_messages =
3385 caller()->data_observer()->messages();
3386 std::vector<std::string> callee_received_messages =
3387 callee()->data_observer()->messages();
Steve Anton64b626b2019-01-28 17:25:26 -08003388 absl::c_sort(sent_messages);
3389 absl::c_sort(caller_received_messages);
3390 absl::c_sort(callee_received_messages);
deadbeef1dcb1642017-03-29 21:08:16 -07003391 EXPECT_EQ(sent_messages, caller_received_messages);
3392 EXPECT_EQ(sent_messages, callee_received_messages);
3393}
3394
3395// This test sets up a call between two parties with audio, and video. When
3396// audio and video are setup and flowing, an SCTP data channel is negotiated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003397TEST_P(PeerConnectionIntegrationTest, AddSctpDataChannelInSubsequentOffer) {
deadbeef1dcb1642017-03-29 21:08:16 -07003398 ASSERT_TRUE(CreatePeerConnectionWrappers());
3399 ConnectFakeSignaling();
3400 // Do initial offer/answer with audio/video.
Steve Anton15324772018-01-16 10:26:49 -08003401 caller()->AddAudioVideoTracks();
3402 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003403 caller()->CreateAndSetAndSignalOffer();
3404 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3405 // Create data channel and do new offer and answer.
3406 caller()->CreateDataChannel();
3407 caller()->CreateAndSetAndSignalOffer();
3408 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3409 // Caller data channel should already exist (it created one). Callee data
3410 // channel may not exist yet, since negotiation happens in-band, not in SDP.
3411 ASSERT_NE(nullptr, caller()->data_channel());
3412 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3413 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3414 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3415 // Ensure data can be sent in both directions.
3416 std::string data = "hello world";
3417 caller()->data_channel()->Send(DataBuffer(data));
3418 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3419 kDefaultTimeout);
3420 callee()->data_channel()->Send(DataBuffer(data));
3421 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3422 kDefaultTimeout);
3423}
3424
deadbeef7914b8c2017-04-21 03:23:33 -07003425// Set up a connection initially just using SCTP data channels, later upgrading
3426// to audio/video, ensuring frames are received end-to-end. Effectively the
3427// inverse of the test above.
3428// This was broken in M57; see https://crbug.com/711243
Seth Hampson2f0d7022018-02-20 11:54:42 -08003429TEST_P(PeerConnectionIntegrationTest, SctpDataChannelToAudioVideoUpgrade) {
deadbeef7914b8c2017-04-21 03:23:33 -07003430 ASSERT_TRUE(CreatePeerConnectionWrappers());
3431 ConnectFakeSignaling();
3432 // Do initial offer/answer with just data channel.
3433 caller()->CreateDataChannel();
3434 caller()->CreateAndSetAndSignalOffer();
3435 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3436 // Wait until data can be sent over the data channel.
3437 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3438 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3439 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3440
3441 // Do subsequent offer/answer with two-way audio and video. Audio and video
3442 // should end up bundled on the DTLS/ICE transport already used for data.
Steve Anton15324772018-01-16 10:26:49 -08003443 caller()->AddAudioVideoTracks();
3444 callee()->AddAudioVideoTracks();
deadbeef7914b8c2017-04-21 03:23:33 -07003445 caller()->CreateAndSetAndSignalOffer();
3446 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08003447 MediaExpectations media_expectations;
3448 media_expectations.ExpectBidirectionalAudioAndVideo();
3449 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef7914b8c2017-04-21 03:23:33 -07003450}
3451
deadbeef8b7e9ad2017-05-25 09:38:55 -07003452static void MakeSpecCompliantSctpOffer(cricket::SessionDescription* desc) {
Harald Alvestrand141c0ad2019-05-05 19:00:00 +00003453 cricket::DataContentDescription* dcd_offer =
3454 GetFirstDataContentDescription(desc);
Steve Antonb1c1de12017-12-21 15:14:30 -08003455 ASSERT_TRUE(dcd_offer);
deadbeef8b7e9ad2017-05-25 09:38:55 -07003456 dcd_offer->set_use_sctpmap(false);
3457 dcd_offer->set_protocol("UDP/DTLS/SCTP");
3458}
3459
3460// Test that the data channel works when a spec-compliant SCTP m= section is
3461// offered (using "a=sctp-port" instead of "a=sctpmap", and using
3462// "UDP/DTLS/SCTP" as the protocol).
Seth Hampson2f0d7022018-02-20 11:54:42 -08003463TEST_P(PeerConnectionIntegrationTest,
deadbeef8b7e9ad2017-05-25 09:38:55 -07003464 DataChannelWorksWhenSpecCompliantSctpOfferReceived) {
3465 ASSERT_TRUE(CreatePeerConnectionWrappers());
3466 ConnectFakeSignaling();
3467 caller()->CreateDataChannel();
3468 caller()->SetGeneratedSdpMunger(MakeSpecCompliantSctpOffer);
3469 caller()->CreateAndSetAndSignalOffer();
3470 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3471 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3472 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3473 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3474
3475 // Ensure data can be sent in both directions.
3476 std::string data = "hello world";
3477 caller()->data_channel()->Send(DataBuffer(data));
3478 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3479 kDefaultTimeout);
3480 callee()->data_channel()->Send(DataBuffer(data));
3481 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3482 kDefaultTimeout);
3483}
3484
deadbeef1dcb1642017-03-29 21:08:16 -07003485#endif // HAVE_SCTP
3486
Bjorn Mellema2eb0a72018-11-09 10:13:51 -08003487// This test sets up a call between two parties with a media transport data
3488// channel.
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08003489TEST_P(PeerConnectionIntegrationTest, MediaTransportDataChannelEndToEnd) {
3490 PeerConnectionInterface::RTCConfiguration rtc_config;
Piotr (Peter) Slatalab1ae10b2019-03-01 11:14:05 -08003491 rtc_config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyRequire;
3492 rtc_config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08003493 rtc_config.use_media_transport_for_data_channels = true;
3494 rtc_config.enable_dtls_srtp = false; // SDES is required for media transport.
3495 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
3496 rtc_config, rtc_config, loopback_media_transports()->first_factory(),
3497 loopback_media_transports()->second_factory()));
3498 ConnectFakeSignaling();
3499
3500 // Expect that data channel created on caller side will show up for callee as
3501 // well.
3502 caller()->CreateDataChannel();
3503 caller()->CreateAndSetAndSignalOffer();
3504 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3505
3506 // Ensure that the media transport is ready.
3507 loopback_media_transports()->SetState(webrtc::MediaTransportState::kWritable);
3508 loopback_media_transports()->FlushAsyncInvokes();
3509
3510 // Caller data channel should already exist (it created one). Callee data
3511 // channel may not exist yet, since negotiation happens in-band, not in SDP.
3512 ASSERT_NE(nullptr, caller()->data_channel());
3513 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3514 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3515 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3516
3517 // Ensure data can be sent in both directions.
3518 std::string data = "hello world";
3519 caller()->data_channel()->Send(DataBuffer(data));
3520 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3521 kDefaultTimeout);
3522 callee()->data_channel()->Send(DataBuffer(data));
3523 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3524 kDefaultTimeout);
3525}
3526
3527// Ensure that when the callee closes a media transport data channel, the
3528// closing procedure results in the data channel being closed for the caller
3529// as well.
3530TEST_P(PeerConnectionIntegrationTest, MediaTransportDataChannelCalleeCloses) {
3531 PeerConnectionInterface::RTCConfiguration rtc_config;
3532 rtc_config.use_media_transport_for_data_channels = true;
3533 rtc_config.enable_dtls_srtp = false; // SDES is required for media transport.
3534 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
3535 rtc_config, rtc_config, loopback_media_transports()->first_factory(),
3536 loopback_media_transports()->second_factory()));
3537 ConnectFakeSignaling();
3538
3539 // Create a data channel on the caller and signal it to the callee.
3540 caller()->CreateDataChannel();
3541 caller()->CreateAndSetAndSignalOffer();
3542 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3543
3544 // Ensure that the media transport is ready.
3545 loopback_media_transports()->SetState(webrtc::MediaTransportState::kWritable);
3546 loopback_media_transports()->FlushAsyncInvokes();
3547
3548 // Data channels exist and open on both ends of the connection.
3549 ASSERT_NE(nullptr, caller()->data_channel());
3550 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3551 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3552 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3553
3554 // Close the data channel on the callee side, and wait for it to reach the
3555 // "closed" state on both sides.
3556 callee()->data_channel()->Close();
3557 EXPECT_TRUE_WAIT(!caller()->data_observer()->IsOpen(), kDefaultTimeout);
3558 EXPECT_TRUE_WAIT(!callee()->data_observer()->IsOpen(), kDefaultTimeout);
3559}
3560
3561TEST_P(PeerConnectionIntegrationTest,
3562 MediaTransportDataChannelConfigSentToOtherSide) {
3563 PeerConnectionInterface::RTCConfiguration rtc_config;
3564 rtc_config.use_media_transport_for_data_channels = true;
3565 rtc_config.enable_dtls_srtp = false; // SDES is required for media transport.
3566 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
3567 rtc_config, rtc_config, loopback_media_transports()->first_factory(),
3568 loopback_media_transports()->second_factory()));
3569 ConnectFakeSignaling();
3570
3571 // Create a data channel with a non-default configuration and signal it to the
3572 // callee.
3573 webrtc::DataChannelInit init;
3574 init.id = 53;
3575 init.maxRetransmits = 52;
3576 caller()->CreateDataChannel("data-channel", &init);
3577 caller()->CreateAndSetAndSignalOffer();
3578 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3579
3580 // Ensure that the media transport is ready.
3581 loopback_media_transports()->SetState(webrtc::MediaTransportState::kWritable);
3582 loopback_media_transports()->FlushAsyncInvokes();
3583
3584 // Ensure that the data channel exists on the callee with the correct
3585 // configuration.
3586 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3587 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
Harald Alvestrand5c4d2ee2019-04-01 12:58:15 +02003588 // Since "negotiate" is false, the "id" parameter is ignored.
3589 EXPECT_NE(init.id, callee()->data_channel()->id());
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08003590 EXPECT_EQ("data-channel", callee()->data_channel()->label());
3591 EXPECT_EQ(init.maxRetransmits, callee()->data_channel()->maxRetransmits());
3592 EXPECT_FALSE(callee()->data_channel()->negotiated());
3593}
3594
Piotr (Peter) Slatalab1ae10b2019-03-01 11:14:05 -08003595TEST_P(PeerConnectionIntegrationTest, MediaTransportOfferUpgrade) {
3596 PeerConnectionInterface::RTCConfiguration rtc_config;
3597 rtc_config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyRequire;
3598 rtc_config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
3599 rtc_config.use_media_transport = true;
3600 rtc_config.enable_dtls_srtp = false; // SDES is required for media transport.
3601 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
3602 rtc_config, rtc_config, loopback_media_transports()->first_factory(),
3603 loopback_media_transports()->second_factory()));
3604 ConnectFakeSignaling();
3605
3606 // Do initial offer/answer with just a video track.
3607 caller()->AddVideoTrack();
3608 callee()->AddVideoTrack();
3609 caller()->CreateAndSetAndSignalOffer();
3610 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3611
3612 // Ensure that the media transport is ready.
3613 loopback_media_transports()->SetState(webrtc::MediaTransportState::kWritable);
3614 loopback_media_transports()->FlushAsyncInvokes();
3615
3616 // Now add an audio track and do another offer/answer.
3617 caller()->AddAudioTrack();
3618 callee()->AddAudioTrack();
3619 caller()->CreateAndSetAndSignalOffer();
3620 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3621
3622 // Ensure both audio and video frames are received end-to-end.
3623 MediaExpectations media_expectations;
3624 media_expectations.ExpectBidirectionalAudioAndVideo();
3625 ASSERT_TRUE(ExpectNewFrames(media_expectations));
3626
3627 // The second offer should not have generated another media transport.
3628 // Media transport was kept alive, and was not recreated.
3629 EXPECT_EQ(1, loopback_media_transports()->first_factory_transport_count());
3630 EXPECT_EQ(1, loopback_media_transports()->second_factory_transport_count());
3631}
3632
3633TEST_P(PeerConnectionIntegrationTest, MediaTransportOfferUpgradeOnTheCallee) {
3634 PeerConnectionInterface::RTCConfiguration rtc_config;
3635 rtc_config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyRequire;
3636 rtc_config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
3637 rtc_config.use_media_transport = true;
3638 rtc_config.enable_dtls_srtp = false; // SDES is required for media transport.
3639 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
3640 rtc_config, rtc_config, loopback_media_transports()->first_factory(),
3641 loopback_media_transports()->second_factory()));
3642 ConnectFakeSignaling();
3643
3644 // Do initial offer/answer with just a video track.
3645 caller()->AddVideoTrack();
3646 callee()->AddVideoTrack();
3647 caller()->CreateAndSetAndSignalOffer();
3648 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3649
3650 // Ensure that the media transport is ready.
3651 loopback_media_transports()->SetState(webrtc::MediaTransportState::kWritable);
3652 loopback_media_transports()->FlushAsyncInvokes();
3653
3654 // Now add an audio track and do another offer/answer.
3655 caller()->AddAudioTrack();
3656 callee()->AddAudioTrack();
3657 callee()->CreateAndSetAndSignalOffer();
3658 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3659
3660 // Ensure both audio and video frames are received end-to-end.
3661 MediaExpectations media_expectations;
3662 media_expectations.ExpectBidirectionalAudioAndVideo();
3663 ASSERT_TRUE(ExpectNewFrames(media_expectations));
3664
3665 // The second offer should not have generated another media transport.
3666 // Media transport was kept alive, and was not recreated.
3667 EXPECT_EQ(1, loopback_media_transports()->first_factory_transport_count());
3668 EXPECT_EQ(1, loopback_media_transports()->second_factory_transport_count());
3669}
3670
Niels Möllerc68d2822018-11-20 14:52:05 +01003671TEST_P(PeerConnectionIntegrationTest, MediaTransportBidirectionalAudio) {
3672 PeerConnectionInterface::RTCConfiguration rtc_config;
Piotr (Peter) Slatalab1ae10b2019-03-01 11:14:05 -08003673 rtc_config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyRequire;
3674 rtc_config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
Niels Möllerc68d2822018-11-20 14:52:05 +01003675 rtc_config.use_media_transport = true;
3676 rtc_config.enable_dtls_srtp = false; // SDES is required for media transport.
3677 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
3678 rtc_config, rtc_config, loopback_media_transports()->first_factory(),
3679 loopback_media_transports()->second_factory()));
3680 ConnectFakeSignaling();
3681
3682 caller()->AddAudioTrack();
3683 callee()->AddAudioTrack();
3684 // Start offer/answer exchange and wait for it to complete.
3685 caller()->CreateAndSetAndSignalOffer();
3686 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3687
3688 // Ensure that the media transport is ready.
3689 loopback_media_transports()->SetState(webrtc::MediaTransportState::kWritable);
3690 loopback_media_transports()->FlushAsyncInvokes();
3691
3692 MediaExpectations media_expectations;
3693 media_expectations.ExpectBidirectionalAudio();
3694 ASSERT_TRUE(ExpectNewFrames(media_expectations));
3695
3696 webrtc::MediaTransportPair::Stats first_stats =
3697 loopback_media_transports()->FirstStats();
3698 webrtc::MediaTransportPair::Stats second_stats =
3699 loopback_media_transports()->SecondStats();
3700
3701 EXPECT_GT(first_stats.received_audio_frames, 0);
3702 EXPECT_GE(second_stats.sent_audio_frames, first_stats.received_audio_frames);
3703
3704 EXPECT_GT(second_stats.received_audio_frames, 0);
3705 EXPECT_GE(first_stats.sent_audio_frames, second_stats.received_audio_frames);
3706}
3707
Niels Möller46879152019-01-07 15:54:47 +01003708TEST_P(PeerConnectionIntegrationTest, MediaTransportBidirectionalVideo) {
3709 PeerConnectionInterface::RTCConfiguration rtc_config;
3710 rtc_config.use_media_transport = true;
3711 rtc_config.enable_dtls_srtp = false; // SDES is required for media transport.
3712 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
3713 rtc_config, rtc_config, loopback_media_transports()->first_factory(),
3714 loopback_media_transports()->second_factory()));
3715 ConnectFakeSignaling();
3716
3717 caller()->AddVideoTrack();
3718 callee()->AddVideoTrack();
3719 // Start offer/answer exchange and wait for it to complete.
3720 caller()->CreateAndSetAndSignalOffer();
3721 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3722
3723 // Ensure that the media transport is ready.
3724 loopback_media_transports()->SetState(webrtc::MediaTransportState::kWritable);
3725 loopback_media_transports()->FlushAsyncInvokes();
3726
3727 MediaExpectations media_expectations;
3728 media_expectations.ExpectBidirectionalVideo();
3729 ASSERT_TRUE(ExpectNewFrames(media_expectations));
3730
3731 webrtc::MediaTransportPair::Stats first_stats =
3732 loopback_media_transports()->FirstStats();
3733 webrtc::MediaTransportPair::Stats second_stats =
3734 loopback_media_transports()->SecondStats();
3735
3736 EXPECT_GT(first_stats.received_video_frames, 0);
3737 EXPECT_GE(second_stats.sent_video_frames, first_stats.received_video_frames);
3738
3739 EXPECT_GT(second_stats.received_video_frames, 0);
3740 EXPECT_GE(first_stats.sent_video_frames, second_stats.received_video_frames);
3741}
3742
Piotr (Peter) Slatala55b91b92019-01-25 13:31:15 -08003743TEST_P(PeerConnectionIntegrationTest,
3744 MediaTransportDataChannelUsesRtpBidirectionalVideo) {
3745 PeerConnectionInterface::RTCConfiguration rtc_config;
3746 rtc_config.use_media_transport = false;
3747 rtc_config.use_media_transport_for_data_channels = true;
3748 rtc_config.enable_dtls_srtp = false; // SDES is required for media transport.
3749 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
3750 rtc_config, rtc_config, loopback_media_transports()->first_factory(),
3751 loopback_media_transports()->second_factory()));
3752 ConnectFakeSignaling();
3753
3754 caller()->AddVideoTrack();
3755 callee()->AddVideoTrack();
3756 // Start offer/answer exchange and wait for it to complete.
3757 caller()->CreateAndSetAndSignalOffer();
3758 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3759
3760 MediaExpectations media_expectations;
3761 media_expectations.ExpectBidirectionalVideo();
3762 ASSERT_TRUE(ExpectNewFrames(media_expectations));
3763}
3764
deadbeef1dcb1642017-03-29 21:08:16 -07003765// Test that the ICE connection and gathering states eventually reach
3766// "complete".
Seth Hampson2f0d7022018-02-20 11:54:42 -08003767TEST_P(PeerConnectionIntegrationTest, IceStatesReachCompletion) {
deadbeef1dcb1642017-03-29 21:08:16 -07003768 ASSERT_TRUE(CreatePeerConnectionWrappers());
3769 ConnectFakeSignaling();
3770 // Do normal offer/answer.
Steve Anton15324772018-01-16 10:26:49 -08003771 caller()->AddAudioVideoTracks();
3772 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003773 caller()->CreateAndSetAndSignalOffer();
3774 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3775 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceGatheringComplete,
3776 caller()->ice_gathering_state(), kMaxWaitForFramesMs);
3777 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceGatheringComplete,
3778 callee()->ice_gathering_state(), kMaxWaitForFramesMs);
3779 // After the best candidate pair is selected and all candidates are signaled,
3780 // the ICE connection state should reach "complete".
3781 // TODO(deadbeef): Currently, the ICE "controlled" agent (the
3782 // answerer/"callee" by default) only reaches "connected". When this is
3783 // fixed, this test should be updated.
3784 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
3785 caller()->ice_connection_state(), kDefaultTimeout);
Alex Loiko9289eda2018-11-23 16:18:59 +00003786 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
3787 callee()->ice_connection_state(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07003788}
3789
Qingsi Wang1dac6d82018-12-12 15:28:47 -08003790constexpr int kOnlyLocalPorts = cricket::PORTALLOCATOR_DISABLE_STUN |
3791 cricket::PORTALLOCATOR_DISABLE_RELAY |
3792 cricket::PORTALLOCATOR_DISABLE_TCP;
Zach Stein6fcdc2f2018-08-23 16:25:55 -07003793
Qingsi Wang1dac6d82018-12-12 15:28:47 -08003794// Use a mock resolver to resolve the hostname back to the original IP on both
3795// sides and check that the ICE connection connects.
Zach Stein6fcdc2f2018-08-23 16:25:55 -07003796TEST_P(PeerConnectionIntegrationTest,
3797 IceStatesReachCompletionWithRemoteHostname) {
Qingsi Wang1dac6d82018-12-12 15:28:47 -08003798 auto caller_resolver_factory =
3799 absl::make_unique<NiceMock<webrtc::MockAsyncResolverFactory>>();
3800 auto callee_resolver_factory =
3801 absl::make_unique<NiceMock<webrtc::MockAsyncResolverFactory>>();
3802 NiceMock<rtc::MockAsyncResolver> callee_async_resolver;
3803 NiceMock<rtc::MockAsyncResolver> caller_async_resolver;
Zach Stein6fcdc2f2018-08-23 16:25:55 -07003804
3805 // This also verifies that the injected AsyncResolverFactory is used by
3806 // P2PTransportChannel.
Qingsi Wang1dac6d82018-12-12 15:28:47 -08003807 EXPECT_CALL(*caller_resolver_factory, Create())
3808 .WillOnce(Return(&caller_async_resolver));
3809 webrtc::PeerConnectionDependencies caller_deps(nullptr);
3810 caller_deps.async_resolver_factory = std::move(caller_resolver_factory);
3811
3812 EXPECT_CALL(*callee_resolver_factory, Create())
3813 .WillOnce(Return(&callee_async_resolver));
3814 webrtc::PeerConnectionDependencies callee_deps(nullptr);
3815 callee_deps.async_resolver_factory = std::move(callee_resolver_factory);
3816
3817 PeerConnectionInterface::RTCConfiguration config;
3818 config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
3819 config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyRequire;
3820
3821 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndDeps(
3822 config, std::move(caller_deps), config, std::move(callee_deps)));
3823
3824 caller()->SetRemoteAsyncResolver(&callee_async_resolver);
3825 callee()->SetRemoteAsyncResolver(&caller_async_resolver);
3826
3827 // Enable hostname candidates with mDNS names.
3828 caller()->network()->CreateMdnsResponder(network_thread());
3829 callee()->network()->CreateMdnsResponder(network_thread());
3830
3831 SetPortAllocatorFlags(kOnlyLocalPorts, kOnlyLocalPorts);
Zach Stein6fcdc2f2018-08-23 16:25:55 -07003832
3833 ConnectFakeSignaling();
3834 caller()->AddAudioVideoTracks();
3835 callee()->AddAudioVideoTracks();
3836 caller()->CreateAndSetAndSignalOffer();
3837 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3838 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
3839 caller()->ice_connection_state(), kDefaultTimeout);
3840 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
3841 callee()->ice_connection_state(), kDefaultTimeout);
Jeroen de Borst833979f2018-12-13 08:25:54 -08003842
3843 EXPECT_EQ(1, webrtc::metrics::NumEvents(
3844 "WebRTC.PeerConnection.CandidatePairType_UDP",
3845 webrtc::kIceCandidatePairHostNameHostName));
Zach Stein6fcdc2f2018-08-23 16:25:55 -07003846}
3847
Steve Antonede9ca52017-10-16 13:04:27 -07003848// Test that firewalling the ICE connection causes the clients to identify the
3849// disconnected state and then removing the firewall causes them to reconnect.
3850class PeerConnectionIntegrationIceStatesTest
Seth Hampson2f0d7022018-02-20 11:54:42 -08003851 : public PeerConnectionIntegrationBaseTest,
3852 public ::testing::WithParamInterface<
3853 std::tuple<SdpSemantics, std::tuple<std::string, uint32_t>>> {
Steve Antonede9ca52017-10-16 13:04:27 -07003854 protected:
Seth Hampson2f0d7022018-02-20 11:54:42 -08003855 PeerConnectionIntegrationIceStatesTest()
3856 : PeerConnectionIntegrationBaseTest(std::get<0>(GetParam())) {
3857 port_allocator_flags_ = std::get<1>(std::get<1>(GetParam()));
Steve Antonede9ca52017-10-16 13:04:27 -07003858 }
3859
3860 void StartStunServer(const SocketAddress& server_address) {
3861 stun_server_.reset(
3862 cricket::TestStunServer::Create(network_thread(), server_address));
3863 }
3864
3865 bool TestIPv6() {
3866 return (port_allocator_flags_ & cricket::PORTALLOCATOR_ENABLE_IPV6);
3867 }
3868
3869 void SetPortAllocatorFlags() {
Qingsi Wang1dac6d82018-12-12 15:28:47 -08003870 PeerConnectionIntegrationBaseTest::SetPortAllocatorFlags(
3871 port_allocator_flags_, port_allocator_flags_);
Steve Antonede9ca52017-10-16 13:04:27 -07003872 }
3873
3874 std::vector<SocketAddress> CallerAddresses() {
3875 std::vector<SocketAddress> addresses;
3876 addresses.push_back(SocketAddress("1.1.1.1", 0));
3877 if (TestIPv6()) {
3878 addresses.push_back(SocketAddress("1111:0:a:b:c:d:e:f", 0));
3879 }
3880 return addresses;
3881 }
3882
3883 std::vector<SocketAddress> CalleeAddresses() {
3884 std::vector<SocketAddress> addresses;
3885 addresses.push_back(SocketAddress("2.2.2.2", 0));
3886 if (TestIPv6()) {
3887 addresses.push_back(SocketAddress("2222:0:a:b:c:d:e:f", 0));
3888 }
3889 return addresses;
3890 }
3891
3892 void SetUpNetworkInterfaces() {
3893 // Remove the default interfaces added by the test infrastructure.
3894 caller()->network()->RemoveInterface(kDefaultLocalAddress);
3895 callee()->network()->RemoveInterface(kDefaultLocalAddress);
3896
3897 // Add network addresses for test.
3898 for (const auto& caller_address : CallerAddresses()) {
3899 caller()->network()->AddInterface(caller_address);
3900 }
3901 for (const auto& callee_address : CalleeAddresses()) {
3902 callee()->network()->AddInterface(callee_address);
3903 }
3904 }
3905
3906 private:
3907 uint32_t port_allocator_flags_;
3908 std::unique_ptr<cricket::TestStunServer> stun_server_;
3909};
3910
3911// Tests that the PeerConnection goes through all the ICE gathering/connection
3912// states over the duration of the call. This includes Disconnected and Failed
3913// states, induced by putting a firewall between the peers and waiting for them
3914// to time out.
Steve Anton83119dd2017-11-10 16:19:52 -08003915TEST_P(PeerConnectionIntegrationIceStatesTest, VerifyIceStates) {
Jonas Olssonb75d9e92019-02-22 10:33:29 +01003916 rtc::ScopedFakeClock fake_clock;
3917 // Some things use a time of "0" as a special value, so we need to start out
3918 // the fake clock at a nonzero time.
3919 fake_clock.AdvanceTime(TimeDelta::seconds(1));
Steve Antonede9ca52017-10-16 13:04:27 -07003920
3921 const SocketAddress kStunServerAddress =
3922 SocketAddress("99.99.99.1", cricket::STUN_SERVER_PORT);
3923 StartStunServer(kStunServerAddress);
3924
3925 PeerConnectionInterface::RTCConfiguration config;
3926 PeerConnectionInterface::IceServer ice_stun_server;
3927 ice_stun_server.urls.push_back(
3928 "stun:" + kStunServerAddress.HostAsURIString() + ":" +
3929 kStunServerAddress.PortAsString());
3930 config.servers.push_back(ice_stun_server);
3931
3932 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
3933 ConnectFakeSignaling();
3934 SetPortAllocatorFlags();
3935 SetUpNetworkInterfaces();
Steve Anton15324772018-01-16 10:26:49 -08003936 caller()->AddAudioVideoTracks();
3937 callee()->AddAudioVideoTracks();
Steve Antonede9ca52017-10-16 13:04:27 -07003938
3939 // Initial state before anything happens.
3940 ASSERT_EQ(PeerConnectionInterface::kIceGatheringNew,
3941 caller()->ice_gathering_state());
3942 ASSERT_EQ(PeerConnectionInterface::kIceConnectionNew,
3943 caller()->ice_connection_state());
Jonas Olsson7a6739e2019-01-15 16:31:55 +01003944 ASSERT_EQ(PeerConnectionInterface::kIceConnectionNew,
3945 caller()->standardized_ice_connection_state());
Steve Antonede9ca52017-10-16 13:04:27 -07003946
3947 // Start the call by creating the offer, setting it as the local description,
3948 // then sending it to the peer who will respond with an answer. This happens
3949 // asynchronously so that we can watch the states as it runs in the
3950 // background.
3951 caller()->CreateAndSetAndSignalOffer();
3952
Jonas Olsson7a6739e2019-01-15 16:31:55 +01003953 ASSERT_EQ(PeerConnectionInterface::kIceConnectionCompleted,
3954 caller()->ice_connection_state());
Jonas Olssonacd8ae72019-02-25 15:26:24 +01003955 ASSERT_EQ(PeerConnectionInterface::kIceConnectionCompleted,
Jonas Olsson7a6739e2019-01-15 16:31:55 +01003956 caller()->standardized_ice_connection_state());
Steve Antonede9ca52017-10-16 13:04:27 -07003957
3958 // Verify that the observer was notified of the intermediate transitions.
3959 EXPECT_THAT(caller()->ice_connection_state_history(),
3960 ElementsAre(PeerConnectionInterface::kIceConnectionChecking,
3961 PeerConnectionInterface::kIceConnectionConnected,
3962 PeerConnectionInterface::kIceConnectionCompleted));
Jonas Olssonacd8ae72019-02-25 15:26:24 +01003963 EXPECT_THAT(caller()->standardized_ice_connection_state_history(),
3964 ElementsAre(PeerConnectionInterface::kIceConnectionChecking,
3965 PeerConnectionInterface::kIceConnectionConnected,
3966 PeerConnectionInterface::kIceConnectionCompleted));
Jonas Olsson635474e2018-10-18 15:58:17 +02003967 EXPECT_THAT(
3968 caller()->peer_connection_state_history(),
3969 ElementsAre(PeerConnectionInterface::PeerConnectionState::kConnecting,
Jonas Olsson635474e2018-10-18 15:58:17 +02003970 PeerConnectionInterface::PeerConnectionState::kConnected));
Steve Antonede9ca52017-10-16 13:04:27 -07003971 EXPECT_THAT(caller()->ice_gathering_state_history(),
3972 ElementsAre(PeerConnectionInterface::kIceGatheringGathering,
3973 PeerConnectionInterface::kIceGatheringComplete));
3974
3975 // Block connections to/from the caller and wait for ICE to become
3976 // disconnected.
3977 for (const auto& caller_address : CallerAddresses()) {
3978 firewall()->AddRule(false, rtc::FP_ANY, rtc::FD_ANY, caller_address);
3979 }
Mirko Bonadei675513b2017-11-09 11:09:25 +01003980 RTC_LOG(LS_INFO) << "Firewall rules applied";
Jonas Olssonb75d9e92019-02-22 10:33:29 +01003981 ASSERT_EQ_SIMULATED_WAIT(PeerConnectionInterface::kIceConnectionDisconnected,
3982 caller()->ice_connection_state(), kDefaultTimeout,
3983 fake_clock);
3984 ASSERT_EQ_SIMULATED_WAIT(PeerConnectionInterface::kIceConnectionDisconnected,
3985 caller()->standardized_ice_connection_state(),
3986 kDefaultTimeout, fake_clock);
Steve Antonede9ca52017-10-16 13:04:27 -07003987
3988 // Let ICE re-establish by removing the firewall rules.
3989 firewall()->ClearRules();
Mirko Bonadei675513b2017-11-09 11:09:25 +01003990 RTC_LOG(LS_INFO) << "Firewall rules cleared";
Jonas Olssonb75d9e92019-02-22 10:33:29 +01003991 ASSERT_EQ_SIMULATED_WAIT(PeerConnectionInterface::kIceConnectionCompleted,
3992 caller()->ice_connection_state(), kDefaultTimeout,
3993 fake_clock);
Jonas Olssonacd8ae72019-02-25 15:26:24 +01003994 ASSERT_EQ_SIMULATED_WAIT(PeerConnectionInterface::kIceConnectionCompleted,
Jonas Olssonb75d9e92019-02-22 10:33:29 +01003995 caller()->standardized_ice_connection_state(),
3996 kDefaultTimeout, fake_clock);
Steve Antonede9ca52017-10-16 13:04:27 -07003997
3998 // According to RFC7675, if there is no response within 30 seconds then the
3999 // peer should consider the other side to have rejected the connection. This
Steve Anton83119dd2017-11-10 16:19:52 -08004000 // is signaled by the state transitioning to "failed".
Steve Antonede9ca52017-10-16 13:04:27 -07004001 constexpr int kConsentTimeout = 30000;
4002 for (const auto& caller_address : CallerAddresses()) {
4003 firewall()->AddRule(false, rtc::FP_ANY, rtc::FD_ANY, caller_address);
4004 }
Mirko Bonadei675513b2017-11-09 11:09:25 +01004005 RTC_LOG(LS_INFO) << "Firewall rules applied again";
Jonas Olssonb75d9e92019-02-22 10:33:29 +01004006 ASSERT_EQ_SIMULATED_WAIT(PeerConnectionInterface::kIceConnectionFailed,
4007 caller()->ice_connection_state(), kConsentTimeout,
4008 fake_clock);
4009 ASSERT_EQ_SIMULATED_WAIT(PeerConnectionInterface::kIceConnectionFailed,
4010 caller()->standardized_ice_connection_state(),
4011 kConsentTimeout, fake_clock);
4012
4013 // We need to manually close the peerconnections before the fake clock goes
4014 // out of scope, or we trigger a DCHECK in rtp_sender.cc when we briefly
4015 // return to using non-faked time.
4016 delete SetCallerPcWrapperAndReturnCurrent(nullptr);
4017 delete SetCalleePcWrapperAndReturnCurrent(nullptr);
4018}
4019
4020// Tests that if the connection doesn't get set up properly we eventually reach
4021// the "failed" iceConnectionState.
4022TEST_P(PeerConnectionIntegrationIceStatesTest, IceStateSetupFailure) {
4023 rtc::ScopedFakeClock fake_clock;
4024 // Some things use a time of "0" as a special value, so we need to start out
4025 // the fake clock at a nonzero time.
4026 fake_clock.AdvanceTime(TimeDelta::seconds(1));
4027
4028 // Block connections to/from the caller and wait for ICE to become
4029 // disconnected.
4030 for (const auto& caller_address : CallerAddresses()) {
4031 firewall()->AddRule(false, rtc::FP_ANY, rtc::FD_ANY, caller_address);
4032 }
4033
4034 ASSERT_TRUE(CreatePeerConnectionWrappers());
4035 ConnectFakeSignaling();
4036 SetPortAllocatorFlags();
4037 SetUpNetworkInterfaces();
4038 caller()->AddAudioVideoTracks();
4039 caller()->CreateAndSetAndSignalOffer();
4040
4041 // According to RFC7675, if there is no response within 30 seconds then the
4042 // peer should consider the other side to have rejected the connection. This
4043 // is signaled by the state transitioning to "failed".
4044 constexpr int kConsentTimeout = 30000;
4045 ASSERT_EQ_SIMULATED_WAIT(PeerConnectionInterface::kIceConnectionFailed,
4046 caller()->standardized_ice_connection_state(),
4047 kConsentTimeout, fake_clock);
4048
4049 // We need to manually close the peerconnections before the fake clock goes
4050 // out of scope, or we trigger a DCHECK in rtp_sender.cc when we briefly
4051 // return to using non-faked time.
4052 delete SetCallerPcWrapperAndReturnCurrent(nullptr);
4053 delete SetCalleePcWrapperAndReturnCurrent(nullptr);
Steve Antonede9ca52017-10-16 13:04:27 -07004054}
4055
4056// Tests that the best connection is set to the appropriate IPv4/IPv6 connection
4057// and that the statistics in the metric observers are updated correctly.
4058TEST_P(PeerConnectionIntegrationIceStatesTest, VerifyBestConnection) {
4059 ASSERT_TRUE(CreatePeerConnectionWrappers());
4060 ConnectFakeSignaling();
4061 SetPortAllocatorFlags();
4062 SetUpNetworkInterfaces();
Steve Anton15324772018-01-16 10:26:49 -08004063 caller()->AddAudioVideoTracks();
4064 callee()->AddAudioVideoTracks();
Steve Antonede9ca52017-10-16 13:04:27 -07004065 caller()->CreateAndSetAndSignalOffer();
4066
4067 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4068
Qingsi Wang7fc821d2018-07-12 12:54:53 -07004069 // TODO(bugs.webrtc.org/9456): Fix it.
4070 const int num_best_ipv4 = webrtc::metrics::NumEvents(
4071 "WebRTC.PeerConnection.IPMetrics", webrtc::kBestConnections_IPv4);
4072 const int num_best_ipv6 = webrtc::metrics::NumEvents(
4073 "WebRTC.PeerConnection.IPMetrics", webrtc::kBestConnections_IPv6);
Steve Antonede9ca52017-10-16 13:04:27 -07004074 if (TestIPv6()) {
4075 // When IPv6 is enabled, we should prefer an IPv6 connection over an IPv4
4076 // connection.
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02004077 EXPECT_EQ(0, num_best_ipv4);
4078 EXPECT_EQ(1, num_best_ipv6);
Steve Antonede9ca52017-10-16 13:04:27 -07004079 } else {
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02004080 EXPECT_EQ(1, num_best_ipv4);
4081 EXPECT_EQ(0, num_best_ipv6);
Steve Antonede9ca52017-10-16 13:04:27 -07004082 }
4083
Qingsi Wang7fc821d2018-07-12 12:54:53 -07004084 EXPECT_EQ(0, webrtc::metrics::NumEvents(
4085 "WebRTC.PeerConnection.CandidatePairType_UDP",
4086 webrtc::kIceCandidatePairHostHost));
4087 EXPECT_EQ(1, webrtc::metrics::NumEvents(
4088 "WebRTC.PeerConnection.CandidatePairType_UDP",
4089 webrtc::kIceCandidatePairHostPublicHostPublic));
Steve Antonede9ca52017-10-16 13:04:27 -07004090}
4091
4092constexpr uint32_t kFlagsIPv4NoStun = cricket::PORTALLOCATOR_DISABLE_TCP |
4093 cricket::PORTALLOCATOR_DISABLE_STUN |
4094 cricket::PORTALLOCATOR_DISABLE_RELAY;
4095constexpr uint32_t kFlagsIPv6NoStun =
4096 cricket::PORTALLOCATOR_DISABLE_TCP | cricket::PORTALLOCATOR_DISABLE_STUN |
4097 cricket::PORTALLOCATOR_ENABLE_IPV6 | cricket::PORTALLOCATOR_DISABLE_RELAY;
4098constexpr uint32_t kFlagsIPv4Stun =
4099 cricket::PORTALLOCATOR_DISABLE_TCP | cricket::PORTALLOCATOR_DISABLE_RELAY;
4100
Mirko Bonadeic84f6612019-01-31 12:20:57 +01004101INSTANTIATE_TEST_SUITE_P(
Seth Hampson2f0d7022018-02-20 11:54:42 -08004102 PeerConnectionIntegrationTest,
4103 PeerConnectionIntegrationIceStatesTest,
4104 Combine(Values(SdpSemantics::kPlanB, SdpSemantics::kUnifiedPlan),
4105 Values(std::make_pair("IPv4 no STUN", kFlagsIPv4NoStun),
4106 std::make_pair("IPv6 no STUN", kFlagsIPv6NoStun),
4107 std::make_pair("IPv4 with STUN", kFlagsIPv4Stun))));
Steve Antonede9ca52017-10-16 13:04:27 -07004108
deadbeef1dcb1642017-03-29 21:08:16 -07004109// This test sets up a call between two parties with audio and video.
4110// During the call, the caller restarts ICE and the test verifies that
4111// new ICE candidates are generated and audio and video still can flow, and the
4112// ICE state reaches completed again.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004113TEST_P(PeerConnectionIntegrationTest, MediaContinuesFlowingAfterIceRestart) {
deadbeef1dcb1642017-03-29 21:08:16 -07004114 ASSERT_TRUE(CreatePeerConnectionWrappers());
4115 ConnectFakeSignaling();
4116 // Do normal offer/answer and wait for ICE to complete.
Steve Anton15324772018-01-16 10:26:49 -08004117 caller()->AddAudioVideoTracks();
4118 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07004119 caller()->CreateAndSetAndSignalOffer();
4120 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4121 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
4122 caller()->ice_connection_state(), kMaxWaitForFramesMs);
Alex Loiko9289eda2018-11-23 16:18:59 +00004123 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
4124 callee()->ice_connection_state(), kMaxWaitForFramesMs);
deadbeef1dcb1642017-03-29 21:08:16 -07004125
4126 // To verify that the ICE restart actually occurs, get
4127 // ufrag/password/candidates before and after restart.
4128 // Create an SDP string of the first audio candidate for both clients.
4129 const webrtc::IceCandidateCollection* audio_candidates_caller =
4130 caller()->pc()->local_description()->candidates(0);
4131 const webrtc::IceCandidateCollection* audio_candidates_callee =
4132 callee()->pc()->local_description()->candidates(0);
4133 ASSERT_GT(audio_candidates_caller->count(), 0u);
4134 ASSERT_GT(audio_candidates_callee->count(), 0u);
4135 std::string caller_candidate_pre_restart;
4136 ASSERT_TRUE(
4137 audio_candidates_caller->at(0)->ToString(&caller_candidate_pre_restart));
4138 std::string callee_candidate_pre_restart;
4139 ASSERT_TRUE(
4140 audio_candidates_callee->at(0)->ToString(&callee_candidate_pre_restart));
4141 const cricket::SessionDescription* desc =
4142 caller()->pc()->local_description()->description();
4143 std::string caller_ufrag_pre_restart =
4144 desc->transport_infos()[0].description.ice_ufrag;
4145 desc = callee()->pc()->local_description()->description();
4146 std::string callee_ufrag_pre_restart =
4147 desc->transport_infos()[0].description.ice_ufrag;
4148
4149 // Have the caller initiate an ICE restart.
4150 caller()->SetOfferAnswerOptions(IceRestartOfferAnswerOptions());
4151 caller()->CreateAndSetAndSignalOffer();
4152 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4153 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
4154 caller()->ice_connection_state(), kMaxWaitForFramesMs);
Alex Loiko9289eda2018-11-23 16:18:59 +00004155 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
deadbeef1dcb1642017-03-29 21:08:16 -07004156 callee()->ice_connection_state(), kMaxWaitForFramesMs);
4157
4158 // Grab the ufrags/candidates again.
4159 audio_candidates_caller = caller()->pc()->local_description()->candidates(0);
4160 audio_candidates_callee = callee()->pc()->local_description()->candidates(0);
4161 ASSERT_GT(audio_candidates_caller->count(), 0u);
4162 ASSERT_GT(audio_candidates_callee->count(), 0u);
4163 std::string caller_candidate_post_restart;
4164 ASSERT_TRUE(
4165 audio_candidates_caller->at(0)->ToString(&caller_candidate_post_restart));
4166 std::string callee_candidate_post_restart;
4167 ASSERT_TRUE(
4168 audio_candidates_callee->at(0)->ToString(&callee_candidate_post_restart));
4169 desc = caller()->pc()->local_description()->description();
4170 std::string caller_ufrag_post_restart =
4171 desc->transport_infos()[0].description.ice_ufrag;
4172 desc = callee()->pc()->local_description()->description();
4173 std::string callee_ufrag_post_restart =
4174 desc->transport_infos()[0].description.ice_ufrag;
4175 // Sanity check that an ICE restart was actually negotiated in SDP.
4176 ASSERT_NE(caller_candidate_pre_restart, caller_candidate_post_restart);
4177 ASSERT_NE(callee_candidate_pre_restart, callee_candidate_post_restart);
4178 ASSERT_NE(caller_ufrag_pre_restart, caller_ufrag_post_restart);
4179 ASSERT_NE(callee_ufrag_pre_restart, callee_ufrag_post_restart);
4180
4181 // Ensure that additional frames are received after the ICE restart.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004182 MediaExpectations media_expectations;
4183 media_expectations.ExpectBidirectionalAudioAndVideo();
4184 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07004185}
4186
4187// Verify that audio/video can be received end-to-end when ICE renomination is
4188// enabled.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004189TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithIceRenomination) {
deadbeef1dcb1642017-03-29 21:08:16 -07004190 PeerConnectionInterface::RTCConfiguration config;
4191 config.enable_ice_renomination = true;
4192 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
4193 ConnectFakeSignaling();
4194 // Do normal offer/answer and wait for some frames to be received in each
4195 // direction.
Steve Anton15324772018-01-16 10:26:49 -08004196 caller()->AddAudioVideoTracks();
4197 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07004198 caller()->CreateAndSetAndSignalOffer();
4199 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4200 // Sanity check that ICE renomination was actually negotiated.
4201 const cricket::SessionDescription* desc =
4202 caller()->pc()->local_description()->description();
4203 for (const cricket::TransportInfo& info : desc->transport_infos()) {
Steve Anton64b626b2019-01-28 17:25:26 -08004204 ASSERT_THAT(info.description.transport_options, Contains("renomination"));
deadbeef1dcb1642017-03-29 21:08:16 -07004205 }
4206 desc = callee()->pc()->local_description()->description();
4207 for (const cricket::TransportInfo& info : desc->transport_infos()) {
Steve Anton64b626b2019-01-28 17:25:26 -08004208 ASSERT_THAT(info.description.transport_options, Contains("renomination"));
deadbeef1dcb1642017-03-29 21:08:16 -07004209 }
Seth Hampson2f0d7022018-02-20 11:54:42 -08004210 MediaExpectations media_expectations;
4211 media_expectations.ExpectBidirectionalAudioAndVideo();
4212 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07004213}
4214
Steve Anton6f25b092017-10-23 09:39:20 -07004215// With a max bundle policy and RTCP muxing, adding a new media description to
4216// the connection should not affect ICE at all because the new media will use
4217// the existing connection.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004218TEST_P(PeerConnectionIntegrationTest,
Steve Anton83119dd2017-11-10 16:19:52 -08004219 AddMediaToConnectedBundleDoesNotRestartIce) {
Steve Anton6f25b092017-10-23 09:39:20 -07004220 PeerConnectionInterface::RTCConfiguration config;
4221 config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
4222 config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyRequire;
4223 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(
4224 config, PeerConnectionInterface::RTCConfiguration()));
4225 ConnectFakeSignaling();
4226
Steve Anton15324772018-01-16 10:26:49 -08004227 caller()->AddAudioTrack();
Steve Anton6f25b092017-10-23 09:39:20 -07004228 caller()->CreateAndSetAndSignalOffer();
4229 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Steve Antonff52f1b2017-10-26 12:24:50 -07004230 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceConnectionCompleted,
4231 caller()->ice_connection_state(), kDefaultTimeout);
Steve Anton6f25b092017-10-23 09:39:20 -07004232
4233 caller()->clear_ice_connection_state_history();
4234
Steve Anton15324772018-01-16 10:26:49 -08004235 caller()->AddVideoTrack();
Steve Anton6f25b092017-10-23 09:39:20 -07004236 caller()->CreateAndSetAndSignalOffer();
4237 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4238
4239 EXPECT_EQ(0u, caller()->ice_connection_state_history().size());
4240}
4241
deadbeef1dcb1642017-03-29 21:08:16 -07004242// This test sets up a call between two parties with audio and video. It then
4243// renegotiates setting the video m-line to "port 0", then later renegotiates
4244// again, enabling video.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004245TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07004246 VideoFlowsAfterMediaSectionIsRejectedAndRecycled) {
4247 ASSERT_TRUE(CreatePeerConnectionWrappers());
4248 ConnectFakeSignaling();
4249
4250 // Do initial negotiation, only sending media from the caller. Will result in
4251 // video and audio recvonly "m=" sections.
Steve Anton15324772018-01-16 10:26:49 -08004252 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07004253 caller()->CreateAndSetAndSignalOffer();
4254 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4255
4256 // Negotiate again, disabling the video "m=" section (the callee will set the
4257 // port to 0 due to offer_to_receive_video = 0).
Seth Hampson2f0d7022018-02-20 11:54:42 -08004258 if (sdp_semantics_ == SdpSemantics::kPlanB) {
4259 PeerConnectionInterface::RTCOfferAnswerOptions options;
4260 options.offer_to_receive_video = 0;
4261 callee()->SetOfferAnswerOptions(options);
4262 } else {
4263 callee()->SetRemoteOfferHandler([this] {
4264 callee()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)->Stop();
4265 });
4266 }
deadbeef1dcb1642017-03-29 21:08:16 -07004267 caller()->CreateAndSetAndSignalOffer();
4268 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4269 // Sanity check that video "m=" section was actually rejected.
4270 const ContentInfo* answer_video_content = cricket::GetFirstVideoContent(
4271 callee()->pc()->local_description()->description());
4272 ASSERT_NE(nullptr, answer_video_content);
4273 ASSERT_TRUE(answer_video_content->rejected);
4274
4275 // Enable video and do negotiation again, making sure video is received
4276 // end-to-end, also adding media stream to callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004277 if (sdp_semantics_ == SdpSemantics::kPlanB) {
4278 PeerConnectionInterface::RTCOfferAnswerOptions options;
4279 options.offer_to_receive_video = 1;
4280 callee()->SetOfferAnswerOptions(options);
4281 } else {
4282 // The caller's transceiver is stopped, so we need to add another track.
4283 auto caller_transceiver =
4284 caller()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO);
4285 EXPECT_TRUE(caller_transceiver->stopped());
4286 caller()->AddVideoTrack();
4287 }
4288 callee()->AddVideoTrack();
4289 callee()->SetRemoteOfferHandler(nullptr);
deadbeef1dcb1642017-03-29 21:08:16 -07004290 caller()->CreateAndSetAndSignalOffer();
4291 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08004292
deadbeef1dcb1642017-03-29 21:08:16 -07004293 // Verify the caller receives frames from the newly added stream, and the
4294 // callee receives additional frames from the re-enabled video m= section.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004295 MediaExpectations media_expectations;
4296 media_expectations.CalleeExpectsSomeAudio();
4297 media_expectations.ExpectBidirectionalVideo();
4298 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07004299}
4300
deadbeef1dcb1642017-03-29 21:08:16 -07004301// This tests that if we negotiate after calling CreateSender but before we
4302// have a track, then set a track later, frames from the newly-set track are
4303// received end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004304TEST_F(PeerConnectionIntegrationTestPlanB,
deadbeef1dcb1642017-03-29 21:08:16 -07004305 MediaFlowsAfterEarlyWarmupWithCreateSender) {
4306 ASSERT_TRUE(CreatePeerConnectionWrappers());
4307 ConnectFakeSignaling();
4308 auto caller_audio_sender =
4309 caller()->pc()->CreateSender("audio", "caller_stream");
4310 auto caller_video_sender =
4311 caller()->pc()->CreateSender("video", "caller_stream");
4312 auto callee_audio_sender =
4313 callee()->pc()->CreateSender("audio", "callee_stream");
4314 auto callee_video_sender =
4315 callee()->pc()->CreateSender("video", "callee_stream");
4316 caller()->CreateAndSetAndSignalOffer();
4317 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
4318 // Wait for ICE to complete, without any tracks being set.
4319 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
4320 caller()->ice_connection_state(), kMaxWaitForFramesMs);
4321 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
4322 callee()->ice_connection_state(), kMaxWaitForFramesMs);
4323 // Now set the tracks, and expect frames to immediately start flowing.
4324 EXPECT_TRUE(caller_audio_sender->SetTrack(caller()->CreateLocalAudioTrack()));
4325 EXPECT_TRUE(caller_video_sender->SetTrack(caller()->CreateLocalVideoTrack()));
4326 EXPECT_TRUE(callee_audio_sender->SetTrack(callee()->CreateLocalAudioTrack()));
4327 EXPECT_TRUE(callee_video_sender->SetTrack(callee()->CreateLocalVideoTrack()));
Seth Hampson2f0d7022018-02-20 11:54:42 -08004328 MediaExpectations media_expectations;
4329 media_expectations.ExpectBidirectionalAudioAndVideo();
4330 ASSERT_TRUE(ExpectNewFrames(media_expectations));
4331}
4332
4333// This tests that if we negotiate after calling AddTransceiver but before we
4334// have a track, then set a track later, frames from the newly-set tracks are
4335// received end-to-end.
4336TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
4337 MediaFlowsAfterEarlyWarmupWithAddTransceiver) {
4338 ASSERT_TRUE(CreatePeerConnectionWrappers());
4339 ConnectFakeSignaling();
4340 auto audio_result = caller()->pc()->AddTransceiver(cricket::MEDIA_TYPE_AUDIO);
4341 ASSERT_EQ(RTCErrorType::NONE, audio_result.error().type());
4342 auto caller_audio_sender = audio_result.MoveValue()->sender();
4343 auto video_result = caller()->pc()->AddTransceiver(cricket::MEDIA_TYPE_VIDEO);
4344 ASSERT_EQ(RTCErrorType::NONE, video_result.error().type());
4345 auto caller_video_sender = video_result.MoveValue()->sender();
4346 callee()->SetRemoteOfferHandler([this] {
4347 ASSERT_EQ(2u, callee()->pc()->GetTransceivers().size());
4348 callee()->pc()->GetTransceivers()[0]->SetDirection(
4349 RtpTransceiverDirection::kSendRecv);
4350 callee()->pc()->GetTransceivers()[1]->SetDirection(
4351 RtpTransceiverDirection::kSendRecv);
4352 });
4353 caller()->CreateAndSetAndSignalOffer();
4354 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
4355 // Wait for ICE to complete, without any tracks being set.
4356 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
4357 caller()->ice_connection_state(), kMaxWaitForFramesMs);
4358 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
4359 callee()->ice_connection_state(), kMaxWaitForFramesMs);
4360 // Now set the tracks, and expect frames to immediately start flowing.
4361 auto callee_audio_sender = callee()->pc()->GetSenders()[0];
4362 auto callee_video_sender = callee()->pc()->GetSenders()[1];
4363 ASSERT_TRUE(caller_audio_sender->SetTrack(caller()->CreateLocalAudioTrack()));
4364 ASSERT_TRUE(caller_video_sender->SetTrack(caller()->CreateLocalVideoTrack()));
4365 ASSERT_TRUE(callee_audio_sender->SetTrack(callee()->CreateLocalAudioTrack()));
4366 ASSERT_TRUE(callee_video_sender->SetTrack(callee()->CreateLocalVideoTrack()));
4367 MediaExpectations media_expectations;
4368 media_expectations.ExpectBidirectionalAudioAndVideo();
4369 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07004370}
4371
4372// This test verifies that a remote video track can be added via AddStream,
4373// and sent end-to-end. For this particular test, it's simply echoed back
4374// from the caller to the callee, rather than being forwarded to a third
4375// PeerConnection.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004376TEST_F(PeerConnectionIntegrationTestPlanB, CanSendRemoteVideoTrack) {
deadbeef1dcb1642017-03-29 21:08:16 -07004377 ASSERT_TRUE(CreatePeerConnectionWrappers());
4378 ConnectFakeSignaling();
4379 // Just send a video track from the caller.
Steve Anton15324772018-01-16 10:26:49 -08004380 caller()->AddVideoTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07004381 caller()->CreateAndSetAndSignalOffer();
4382 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02004383 ASSERT_EQ(1U, callee()->remote_streams()->count());
deadbeef1dcb1642017-03-29 21:08:16 -07004384
4385 // Echo the stream back, and do a new offer/anwer (initiated by callee this
4386 // time).
4387 callee()->pc()->AddStream(callee()->remote_streams()->at(0));
4388 callee()->CreateAndSetAndSignalOffer();
4389 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
4390
Seth Hampson2f0d7022018-02-20 11:54:42 -08004391 MediaExpectations media_expectations;
4392 media_expectations.ExpectBidirectionalVideo();
4393 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07004394}
4395
4396// Test that we achieve the expected end-to-end connection time, using a
4397// fake clock and simulated latency on the media and signaling paths.
4398// We use a TURN<->TURN connection because this is usually the quickest to
4399// set up initially, especially when we're confident the connection will work
4400// and can start sending media before we get a STUN response.
4401//
4402// With various optimizations enabled, here are the network delays we expect to
4403// be on the critical path:
4404// 1. 2 signaling trips: Signaling offer and offerer's TURN candidate, then
4405// signaling answer (with DTLS fingerprint).
4406// 2. 9 media hops: Rest of the DTLS handshake. 3 hops in each direction when
4407// using TURN<->TURN pair, and DTLS exchange is 4 packets,
4408// the first of which should have arrived before the answer.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004409TEST_P(PeerConnectionIntegrationTest, EndToEndConnectionTimeWithTurnTurnPair) {
deadbeef1dcb1642017-03-29 21:08:16 -07004410 rtc::ScopedFakeClock fake_clock;
4411 // Some things use a time of "0" as a special value, so we need to start out
4412 // the fake clock at a nonzero time.
4413 // TODO(deadbeef): Fix this.
Sebastian Jansson5f83cf02018-05-08 14:52:22 +02004414 fake_clock.AdvanceTime(webrtc::TimeDelta::seconds(1));
deadbeef1dcb1642017-03-29 21:08:16 -07004415
4416 static constexpr int media_hop_delay_ms = 50;
4417 static constexpr int signaling_trip_delay_ms = 500;
4418 // For explanation of these values, see comment above.
4419 static constexpr int required_media_hops = 9;
4420 static constexpr int required_signaling_trips = 2;
4421 // For internal delays (such as posting an event asychronously).
4422 static constexpr int allowed_internal_delay_ms = 20;
4423 static constexpr int total_connection_time_ms =
4424 media_hop_delay_ms * required_media_hops +
4425 signaling_trip_delay_ms * required_signaling_trips +
4426 allowed_internal_delay_ms;
4427
4428 static const rtc::SocketAddress turn_server_1_internal_address{"88.88.88.0",
4429 3478};
4430 static const rtc::SocketAddress turn_server_1_external_address{"88.88.88.1",
4431 0};
4432 static const rtc::SocketAddress turn_server_2_internal_address{"99.99.99.0",
4433 3478};
4434 static const rtc::SocketAddress turn_server_2_external_address{"99.99.99.1",
4435 0};
Seth Hampsonaed71642018-06-11 07:41:32 -07004436 cricket::TestTurnServer* turn_server_1 = CreateTurnServer(
4437 turn_server_1_internal_address, turn_server_1_external_address);
Jonas Orelandbdcee282017-10-10 14:01:40 +02004438
Seth Hampsonaed71642018-06-11 07:41:32 -07004439 cricket::TestTurnServer* turn_server_2 = CreateTurnServer(
4440 turn_server_2_internal_address, turn_server_2_external_address);
deadbeef1dcb1642017-03-29 21:08:16 -07004441 // Bypass permission check on received packets so media can be sent before
4442 // the candidate is signaled.
Seth Hampsonaed71642018-06-11 07:41:32 -07004443 network_thread()->Invoke<void>(RTC_FROM_HERE, [turn_server_1] {
4444 turn_server_1->set_enable_permission_checks(false);
4445 });
4446 network_thread()->Invoke<void>(RTC_FROM_HERE, [turn_server_2] {
4447 turn_server_2->set_enable_permission_checks(false);
4448 });
deadbeef1dcb1642017-03-29 21:08:16 -07004449
4450 PeerConnectionInterface::RTCConfiguration client_1_config;
4451 webrtc::PeerConnectionInterface::IceServer ice_server_1;
4452 ice_server_1.urls.push_back("turn:88.88.88.0:3478");
4453 ice_server_1.username = "test";
4454 ice_server_1.password = "test";
4455 client_1_config.servers.push_back(ice_server_1);
4456 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
4457 client_1_config.presume_writable_when_fully_relayed = true;
4458
4459 PeerConnectionInterface::RTCConfiguration client_2_config;
4460 webrtc::PeerConnectionInterface::IceServer ice_server_2;
4461 ice_server_2.urls.push_back("turn:99.99.99.0:3478");
4462 ice_server_2.username = "test";
4463 ice_server_2.password = "test";
4464 client_2_config.servers.push_back(ice_server_2);
4465 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
4466 client_2_config.presume_writable_when_fully_relayed = true;
4467
4468 ASSERT_TRUE(
4469 CreatePeerConnectionWrappersWithConfig(client_1_config, client_2_config));
4470 // Set up the simulated delays.
4471 SetSignalingDelayMs(signaling_trip_delay_ms);
4472 ConnectFakeSignaling();
4473 virtual_socket_server()->set_delay_mean(media_hop_delay_ms);
4474 virtual_socket_server()->UpdateDelayDistribution();
4475
4476 // Set "offer to receive audio/video" without adding any tracks, so we just
4477 // set up ICE/DTLS with no media.
4478 PeerConnectionInterface::RTCOfferAnswerOptions options;
4479 options.offer_to_receive_audio = 1;
4480 options.offer_to_receive_video = 1;
4481 caller()->SetOfferAnswerOptions(options);
4482 caller()->CreateAndSetAndSignalOffer();
deadbeef71452802017-05-07 17:21:01 -07004483 EXPECT_TRUE_SIMULATED_WAIT(DtlsConnected(), total_connection_time_ms,
4484 fake_clock);
Seth Hampson1d4a76d2018-06-19 14:31:41 -07004485 // Closing the PeerConnections destroys the ports before the ScopedFakeClock.
4486 // If this is not done a DCHECK can be hit in ports.cc, because a large
4487 // negative number is calculated for the rtt due to the global clock changing.
4488 caller()->pc()->Close();
4489 callee()->pc()->Close();
deadbeef1dcb1642017-03-29 21:08:16 -07004490}
4491
Jonas Orelandbdcee282017-10-10 14:01:40 +02004492// Verify that a TurnCustomizer passed in through RTCConfiguration
4493// is actually used by the underlying TURN candidate pair.
4494// Note that turnport_unittest.cc contains more detailed, lower-level tests.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004495TEST_P(PeerConnectionIntegrationTest, TurnCustomizerUsedForTurnConnections) {
Jonas Orelandbdcee282017-10-10 14:01:40 +02004496 static const rtc::SocketAddress turn_server_1_internal_address{"88.88.88.0",
4497 3478};
4498 static const rtc::SocketAddress turn_server_1_external_address{"88.88.88.1",
4499 0};
4500 static const rtc::SocketAddress turn_server_2_internal_address{"99.99.99.0",
4501 3478};
4502 static const rtc::SocketAddress turn_server_2_external_address{"99.99.99.1",
4503 0};
Seth Hampsonaed71642018-06-11 07:41:32 -07004504 CreateTurnServer(turn_server_1_internal_address,
4505 turn_server_1_external_address);
4506 CreateTurnServer(turn_server_2_internal_address,
4507 turn_server_2_external_address);
Jonas Orelandbdcee282017-10-10 14:01:40 +02004508
4509 PeerConnectionInterface::RTCConfiguration client_1_config;
4510 webrtc::PeerConnectionInterface::IceServer ice_server_1;
4511 ice_server_1.urls.push_back("turn:88.88.88.0:3478");
4512 ice_server_1.username = "test";
4513 ice_server_1.password = "test";
4514 client_1_config.servers.push_back(ice_server_1);
4515 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
Seth Hampsonaed71642018-06-11 07:41:32 -07004516 auto* customizer1 = CreateTurnCustomizer();
4517 client_1_config.turn_customizer = customizer1;
Jonas Orelandbdcee282017-10-10 14:01:40 +02004518
4519 PeerConnectionInterface::RTCConfiguration client_2_config;
4520 webrtc::PeerConnectionInterface::IceServer ice_server_2;
4521 ice_server_2.urls.push_back("turn:99.99.99.0:3478");
4522 ice_server_2.username = "test";
4523 ice_server_2.password = "test";
4524 client_2_config.servers.push_back(ice_server_2);
4525 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
Seth Hampsonaed71642018-06-11 07:41:32 -07004526 auto* customizer2 = CreateTurnCustomizer();
4527 client_2_config.turn_customizer = customizer2;
Jonas Orelandbdcee282017-10-10 14:01:40 +02004528
4529 ASSERT_TRUE(
4530 CreatePeerConnectionWrappersWithConfig(client_1_config, client_2_config));
4531 ConnectFakeSignaling();
4532
4533 // Set "offer to receive audio/video" without adding any tracks, so we just
4534 // set up ICE/DTLS with no media.
4535 PeerConnectionInterface::RTCOfferAnswerOptions options;
4536 options.offer_to_receive_audio = 1;
4537 options.offer_to_receive_video = 1;
4538 caller()->SetOfferAnswerOptions(options);
4539 caller()->CreateAndSetAndSignalOffer();
4540 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
4541
Seth Hampsonaed71642018-06-11 07:41:32 -07004542 ExpectTurnCustomizerCountersIncremented(customizer1);
4543 ExpectTurnCustomizerCountersIncremented(customizer2);
Jonas Orelandbdcee282017-10-10 14:01:40 +02004544}
4545
Benjamin Wright2d5f3cb2018-05-22 14:46:06 -07004546// Verifies that you can use TCP instead of UDP to connect to a TURN server and
4547// send media between the caller and the callee.
4548TEST_P(PeerConnectionIntegrationTest, TCPUsedForTurnConnections) {
4549 static const rtc::SocketAddress turn_server_internal_address{"88.88.88.0",
4550 3478};
4551 static const rtc::SocketAddress turn_server_external_address{"88.88.88.1", 0};
4552
4553 // Enable TCP for the fake turn server.
Seth Hampsonaed71642018-06-11 07:41:32 -07004554 CreateTurnServer(turn_server_internal_address, turn_server_external_address,
4555 cricket::PROTO_TCP);
Benjamin Wright2d5f3cb2018-05-22 14:46:06 -07004556
4557 webrtc::PeerConnectionInterface::IceServer ice_server;
4558 ice_server.urls.push_back("turn:88.88.88.0:3478?transport=tcp");
4559 ice_server.username = "test";
4560 ice_server.password = "test";
4561
4562 PeerConnectionInterface::RTCConfiguration client_1_config;
4563 client_1_config.servers.push_back(ice_server);
4564 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
4565
4566 PeerConnectionInterface::RTCConfiguration client_2_config;
4567 client_2_config.servers.push_back(ice_server);
4568 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
4569
4570 ASSERT_TRUE(
4571 CreatePeerConnectionWrappersWithConfig(client_1_config, client_2_config));
4572
4573 // Do normal offer/answer and wait for ICE to complete.
4574 ConnectFakeSignaling();
4575 caller()->AddAudioVideoTracks();
4576 callee()->AddAudioVideoTracks();
4577 caller()->CreateAndSetAndSignalOffer();
4578 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4579 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
4580 callee()->ice_connection_state(), kMaxWaitForFramesMs);
4581
4582 MediaExpectations media_expectations;
4583 media_expectations.ExpectBidirectionalAudioAndVideo();
4584 EXPECT_TRUE(ExpectNewFrames(media_expectations));
4585}
4586
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07004587// Verify that a SSLCertificateVerifier passed in through
4588// PeerConnectionDependencies is actually used by the underlying SSL
4589// implementation to determine whether a certificate presented by the TURN
4590// server is accepted by the client. Note that openssladapter_unittest.cc
4591// contains more detailed, lower-level tests.
4592TEST_P(PeerConnectionIntegrationTest,
4593 SSLCertificateVerifierUsedForTurnConnections) {
4594 static const rtc::SocketAddress turn_server_internal_address{"88.88.88.0",
4595 3478};
4596 static const rtc::SocketAddress turn_server_external_address{"88.88.88.1", 0};
4597
4598 // Enable TCP-TLS for the fake turn server. We need to pass in 88.88.88.0 so
4599 // that host name verification passes on the fake certificate.
Seth Hampsonaed71642018-06-11 07:41:32 -07004600 CreateTurnServer(turn_server_internal_address, turn_server_external_address,
4601 cricket::PROTO_TLS, "88.88.88.0");
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07004602
4603 webrtc::PeerConnectionInterface::IceServer ice_server;
4604 ice_server.urls.push_back("turns:88.88.88.0:3478?transport=tcp");
4605 ice_server.username = "test";
4606 ice_server.password = "test";
4607
4608 PeerConnectionInterface::RTCConfiguration client_1_config;
4609 client_1_config.servers.push_back(ice_server);
4610 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
4611
4612 PeerConnectionInterface::RTCConfiguration client_2_config;
4613 client_2_config.servers.push_back(ice_server);
4614 // Setting the type to kRelay forces the connection to go through a TURN
4615 // server.
4616 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
4617
4618 // Get a copy to the pointer so we can verify calls later.
4619 rtc::TestCertificateVerifier* client_1_cert_verifier =
4620 new rtc::TestCertificateVerifier();
4621 client_1_cert_verifier->verify_certificate_ = true;
4622 rtc::TestCertificateVerifier* client_2_cert_verifier =
4623 new rtc::TestCertificateVerifier();
4624 client_2_cert_verifier->verify_certificate_ = true;
4625
4626 // Create the dependencies with the test certificate verifier.
4627 webrtc::PeerConnectionDependencies client_1_deps(nullptr);
4628 client_1_deps.tls_cert_verifier =
4629 std::unique_ptr<rtc::TestCertificateVerifier>(client_1_cert_verifier);
4630 webrtc::PeerConnectionDependencies client_2_deps(nullptr);
4631 client_2_deps.tls_cert_verifier =
4632 std::unique_ptr<rtc::TestCertificateVerifier>(client_2_cert_verifier);
4633
4634 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndDeps(
4635 client_1_config, std::move(client_1_deps), client_2_config,
4636 std::move(client_2_deps)));
4637 ConnectFakeSignaling();
4638
4639 // Set "offer to receive audio/video" without adding any tracks, so we just
4640 // set up ICE/DTLS with no media.
4641 PeerConnectionInterface::RTCOfferAnswerOptions options;
4642 options.offer_to_receive_audio = 1;
4643 options.offer_to_receive_video = 1;
4644 caller()->SetOfferAnswerOptions(options);
4645 caller()->CreateAndSetAndSignalOffer();
4646 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
4647
4648 EXPECT_GT(client_1_cert_verifier->call_count_, 0u);
4649 EXPECT_GT(client_2_cert_verifier->call_count_, 0u);
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07004650}
4651
4652TEST_P(PeerConnectionIntegrationTest,
4653 SSLCertificateVerifierFailureUsedForTurnConnectionsFailsConnection) {
4654 static const rtc::SocketAddress turn_server_internal_address{"88.88.88.0",
4655 3478};
4656 static const rtc::SocketAddress turn_server_external_address{"88.88.88.1", 0};
4657
4658 // Enable TCP-TLS for the fake turn server. We need to pass in 88.88.88.0 so
4659 // that host name verification passes on the fake certificate.
Seth Hampsonaed71642018-06-11 07:41:32 -07004660 CreateTurnServer(turn_server_internal_address, turn_server_external_address,
4661 cricket::PROTO_TLS, "88.88.88.0");
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07004662
4663 webrtc::PeerConnectionInterface::IceServer ice_server;
4664 ice_server.urls.push_back("turns:88.88.88.0:3478?transport=tcp");
4665 ice_server.username = "test";
4666 ice_server.password = "test";
4667
4668 PeerConnectionInterface::RTCConfiguration client_1_config;
4669 client_1_config.servers.push_back(ice_server);
4670 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
4671
4672 PeerConnectionInterface::RTCConfiguration client_2_config;
4673 client_2_config.servers.push_back(ice_server);
4674 // Setting the type to kRelay forces the connection to go through a TURN
4675 // server.
4676 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
4677
4678 // Get a copy to the pointer so we can verify calls later.
4679 rtc::TestCertificateVerifier* client_1_cert_verifier =
4680 new rtc::TestCertificateVerifier();
4681 client_1_cert_verifier->verify_certificate_ = false;
4682 rtc::TestCertificateVerifier* client_2_cert_verifier =
4683 new rtc::TestCertificateVerifier();
4684 client_2_cert_verifier->verify_certificate_ = false;
4685
4686 // Create the dependencies with the test certificate verifier.
4687 webrtc::PeerConnectionDependencies client_1_deps(nullptr);
4688 client_1_deps.tls_cert_verifier =
4689 std::unique_ptr<rtc::TestCertificateVerifier>(client_1_cert_verifier);
4690 webrtc::PeerConnectionDependencies client_2_deps(nullptr);
4691 client_2_deps.tls_cert_verifier =
4692 std::unique_ptr<rtc::TestCertificateVerifier>(client_2_cert_verifier);
4693
4694 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndDeps(
4695 client_1_config, std::move(client_1_deps), client_2_config,
4696 std::move(client_2_deps)));
4697 ConnectFakeSignaling();
4698
4699 // Set "offer to receive audio/video" without adding any tracks, so we just
4700 // set up ICE/DTLS with no media.
4701 PeerConnectionInterface::RTCOfferAnswerOptions options;
4702 options.offer_to_receive_audio = 1;
4703 options.offer_to_receive_video = 1;
4704 caller()->SetOfferAnswerOptions(options);
4705 caller()->CreateAndSetAndSignalOffer();
4706 bool wait_res = true;
4707 // TODO(bugs.webrtc.org/9219): When IceConnectionState is implemented
4708 // properly, should be able to just wait for a state of "failed" instead of
4709 // waiting a fixed 10 seconds.
4710 WAIT_(DtlsConnected(), kDefaultTimeout, wait_res);
4711 ASSERT_FALSE(wait_res);
4712
4713 EXPECT_GT(client_1_cert_verifier->call_count_, 0u);
4714 EXPECT_GT(client_2_cert_verifier->call_count_, 0u);
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07004715}
4716
deadbeefc964d0b2017-04-03 10:03:35 -07004717// Test that audio and video flow end-to-end when codec names don't use the
4718// expected casing, given that they're supposed to be case insensitive. To test
4719// this, all but one codec is removed from each media description, and its
4720// casing is changed.
4721//
4722// In the past, this has regressed and caused crashes/black video, due to the
4723// fact that code at some layers was doing case-insensitive comparisons and
4724// code at other layers was not.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004725TEST_P(PeerConnectionIntegrationTest, CodecNamesAreCaseInsensitive) {
deadbeefc964d0b2017-04-03 10:03:35 -07004726 ASSERT_TRUE(CreatePeerConnectionWrappers());
4727 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08004728 caller()->AddAudioVideoTracks();
4729 callee()->AddAudioVideoTracks();
deadbeefc964d0b2017-04-03 10:03:35 -07004730
4731 // Remove all but one audio/video codec (opus and VP8), and change the
4732 // casing of the caller's generated offer.
4733 caller()->SetGeneratedSdpMunger([](cricket::SessionDescription* description) {
4734 cricket::AudioContentDescription* audio =
4735 GetFirstAudioContentDescription(description);
4736 ASSERT_NE(nullptr, audio);
4737 auto audio_codecs = audio->codecs();
4738 audio_codecs.erase(std::remove_if(audio_codecs.begin(), audio_codecs.end(),
4739 [](const cricket::AudioCodec& codec) {
4740 return codec.name != "opus";
4741 }),
4742 audio_codecs.end());
4743 ASSERT_EQ(1u, audio_codecs.size());
4744 audio_codecs[0].name = "OpUs";
4745 audio->set_codecs(audio_codecs);
4746
4747 cricket::VideoContentDescription* video =
4748 GetFirstVideoContentDescription(description);
4749 ASSERT_NE(nullptr, video);
4750 auto video_codecs = video->codecs();
4751 video_codecs.erase(std::remove_if(video_codecs.begin(), video_codecs.end(),
4752 [](const cricket::VideoCodec& codec) {
4753 return codec.name != "VP8";
4754 }),
4755 video_codecs.end());
4756 ASSERT_EQ(1u, video_codecs.size());
4757 video_codecs[0].name = "vP8";
4758 video->set_codecs(video_codecs);
4759 });
4760
4761 caller()->CreateAndSetAndSignalOffer();
4762 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4763
4764 // Verify frames are still received end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004765 MediaExpectations media_expectations;
4766 media_expectations.ExpectBidirectionalAudioAndVideo();
4767 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeefc964d0b2017-04-03 10:03:35 -07004768}
4769
Jonas Oreland49ac5952018-09-26 16:04:32 +02004770TEST_P(PeerConnectionIntegrationTest, GetSourcesAudio) {
hbos8d609f62017-04-10 07:39:05 -07004771 ASSERT_TRUE(CreatePeerConnectionWrappers());
4772 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08004773 caller()->AddAudioTrack();
hbos8d609f62017-04-10 07:39:05 -07004774 caller()->CreateAndSetAndSignalOffer();
4775 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
deadbeefd8ad7882017-04-18 16:01:17 -07004776 // Wait for one audio frame to be received by the callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004777 MediaExpectations media_expectations;
4778 media_expectations.CalleeExpectsSomeAudio(1);
4779 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Jonas Oreland49ac5952018-09-26 16:04:32 +02004780 ASSERT_EQ(callee()->pc()->GetReceivers().size(), 1u);
hbos8d609f62017-04-10 07:39:05 -07004781 auto receiver = callee()->pc()->GetReceivers()[0];
4782 ASSERT_EQ(receiver->media_type(), cricket::MEDIA_TYPE_AUDIO);
Jonas Oreland49ac5952018-09-26 16:04:32 +02004783 auto sources = receiver->GetSources();
hbos8d609f62017-04-10 07:39:05 -07004784 ASSERT_GT(receiver->GetParameters().encodings.size(), 0u);
4785 EXPECT_EQ(receiver->GetParameters().encodings[0].ssrc,
Jonas Oreland49ac5952018-09-26 16:04:32 +02004786 sources[0].source_id());
4787 EXPECT_EQ(webrtc::RtpSourceType::SSRC, sources[0].source_type());
4788}
4789
4790TEST_P(PeerConnectionIntegrationTest, GetSourcesVideo) {
4791 ASSERT_TRUE(CreatePeerConnectionWrappers());
4792 ConnectFakeSignaling();
4793 caller()->AddVideoTrack();
4794 caller()->CreateAndSetAndSignalOffer();
4795 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4796 // Wait for one video frame to be received by the callee.
4797 MediaExpectations media_expectations;
4798 media_expectations.CalleeExpectsSomeVideo(1);
4799 ASSERT_TRUE(ExpectNewFrames(media_expectations));
4800 ASSERT_EQ(callee()->pc()->GetReceivers().size(), 1u);
4801 auto receiver = callee()->pc()->GetReceivers()[0];
4802 ASSERT_EQ(receiver->media_type(), cricket::MEDIA_TYPE_VIDEO);
4803 auto sources = receiver->GetSources();
4804 ASSERT_GT(receiver->GetParameters().encodings.size(), 0u);
4805 EXPECT_EQ(receiver->GetParameters().encodings[0].ssrc,
4806 sources[0].source_id());
4807 EXPECT_EQ(webrtc::RtpSourceType::SSRC, sources[0].source_type());
hbos8d609f62017-04-10 07:39:05 -07004808}
4809
deadbeef2f425aa2017-04-14 10:41:32 -07004810// Test that if a track is removed and added again with a different stream ID,
4811// the new stream ID is successfully communicated in SDP and media continues to
4812// flow end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004813// TODO(webrtc.bugs.org/8734): This test does not work for Unified Plan because
4814// it will not reuse a transceiver that has already been sending. After creating
4815// a new transceiver it tries to create an offer with two senders of the same
4816// track ids and it fails.
4817TEST_F(PeerConnectionIntegrationTestPlanB, RemoveAndAddTrackWithNewStreamId) {
deadbeef2f425aa2017-04-14 10:41:32 -07004818 ASSERT_TRUE(CreatePeerConnectionWrappers());
4819 ConnectFakeSignaling();
4820
deadbeef2f425aa2017-04-14 10:41:32 -07004821 // Add track using stream 1, do offer/answer.
4822 rtc::scoped_refptr<webrtc::AudioTrackInterface> track =
4823 caller()->CreateLocalAudioTrack();
4824 rtc::scoped_refptr<webrtc::RtpSenderInterface> sender =
Steve Antond78323f2018-07-11 11:13:44 -07004825 caller()->AddTrack(track, {"stream_1"});
deadbeef2f425aa2017-04-14 10:41:32 -07004826 caller()->CreateAndSetAndSignalOffer();
4827 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08004828 {
4829 MediaExpectations media_expectations;
4830 media_expectations.CalleeExpectsSomeAudio(1);
4831 ASSERT_TRUE(ExpectNewFrames(media_expectations));
4832 }
deadbeef2f425aa2017-04-14 10:41:32 -07004833 // Remove the sender, and create a new one with the new stream.
4834 caller()->pc()->RemoveTrack(sender);
Steve Antond78323f2018-07-11 11:13:44 -07004835 sender = caller()->AddTrack(track, {"stream_2"});
deadbeef2f425aa2017-04-14 10:41:32 -07004836 caller()->CreateAndSetAndSignalOffer();
4837 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4838 // Wait for additional audio frames to be received by the callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004839 {
4840 MediaExpectations media_expectations;
4841 media_expectations.CalleeExpectsSomeAudio();
4842 ASSERT_TRUE(ExpectNewFrames(media_expectations));
4843 }
deadbeef2f425aa2017-04-14 10:41:32 -07004844}
4845
Seth Hampson2f0d7022018-02-20 11:54:42 -08004846TEST_P(PeerConnectionIntegrationTest, RtcEventLogOutputWriteCalled) {
Elad Alon99c3fe52017-10-13 16:29:40 +02004847 ASSERT_TRUE(CreatePeerConnectionWrappers());
4848 ConnectFakeSignaling();
4849
Karl Wiberg918f50c2018-07-05 11:40:33 +02004850 auto output = absl::make_unique<testing::NiceMock<MockRtcEventLogOutput>>();
Mirko Bonadei6a489f22019-04-09 15:11:12 +02004851 ON_CALL(*output, IsActive()).WillByDefault(::testing::Return(true));
4852 ON_CALL(*output, Write(::testing::_)).WillByDefault(::testing::Return(true));
Elad Alon99c3fe52017-10-13 16:29:40 +02004853 EXPECT_CALL(*output, Write(::testing::_)).Times(::testing::AtLeast(1));
Bjorn Tereliusde939432017-11-20 17:38:14 +01004854 EXPECT_TRUE(caller()->pc()->StartRtcEventLog(
4855 std::move(output), webrtc::RtcEventLog::kImmediateOutput));
Elad Alon99c3fe52017-10-13 16:29:40 +02004856
Steve Anton15324772018-01-16 10:26:49 -08004857 caller()->AddAudioVideoTracks();
Elad Alon99c3fe52017-10-13 16:29:40 +02004858 caller()->CreateAndSetAndSignalOffer();
4859 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4860}
4861
Steve Antonede9ca52017-10-16 13:04:27 -07004862// Test that if candidates are only signaled by applying full session
4863// descriptions (instead of using AddIceCandidate), the peers can connect to
4864// each other and exchange media.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004865TEST_P(PeerConnectionIntegrationTest, MediaFlowsWhenCandidatesSetOnlyInSdp) {
Steve Antonede9ca52017-10-16 13:04:27 -07004866 ASSERT_TRUE(CreatePeerConnectionWrappers());
4867 // Each side will signal the session descriptions but not candidates.
4868 ConnectFakeSignalingForSdpOnly();
4869
4870 // Add audio video track and exchange the initial offer/answer with media
4871 // information only. This will start ICE gathering on each side.
Steve Anton15324772018-01-16 10:26:49 -08004872 caller()->AddAudioVideoTracks();
4873 callee()->AddAudioVideoTracks();
Steve Antonede9ca52017-10-16 13:04:27 -07004874 caller()->CreateAndSetAndSignalOffer();
4875
4876 // Wait for all candidates to be gathered on both the caller and callee.
4877 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceGatheringComplete,
4878 caller()->ice_gathering_state(), kDefaultTimeout);
4879 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceGatheringComplete,
4880 callee()->ice_gathering_state(), kDefaultTimeout);
4881
4882 // The candidates will now be included in the session description, so
4883 // signaling them will start the ICE connection.
4884 caller()->CreateAndSetAndSignalOffer();
4885 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4886
4887 // Ensure that media flows in both directions.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004888 MediaExpectations media_expectations;
4889 media_expectations.ExpectBidirectionalAudioAndVideo();
4890 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Antonede9ca52017-10-16 13:04:27 -07004891}
4892
henrika5f6bf242017-11-01 11:06:56 +01004893// Test that SetAudioPlayout can be used to disable audio playout from the
4894// start, then later enable it. This may be useful, for example, if the caller
4895// needs to play a local ringtone until some event occurs, after which it
4896// switches to playing the received audio.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004897TEST_P(PeerConnectionIntegrationTest, DisableAndEnableAudioPlayout) {
henrika5f6bf242017-11-01 11:06:56 +01004898 ASSERT_TRUE(CreatePeerConnectionWrappers());
4899 ConnectFakeSignaling();
4900
4901 // Set up audio-only call where audio playout is disabled on caller's side.
4902 caller()->pc()->SetAudioPlayout(false);
Steve Anton15324772018-01-16 10:26:49 -08004903 caller()->AddAudioTrack();
4904 callee()->AddAudioTrack();
henrika5f6bf242017-11-01 11:06:56 +01004905 caller()->CreateAndSetAndSignalOffer();
4906 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4907
4908 // Pump messages for a second.
4909 WAIT(false, 1000);
4910 // Since audio playout is disabled, the caller shouldn't have received
4911 // anything (at the playout level, at least).
4912 EXPECT_EQ(0, caller()->audio_frames_received());
4913 // As a sanity check, make sure the callee (for which playout isn't disabled)
4914 // did still see frames on its audio level.
4915 ASSERT_GT(callee()->audio_frames_received(), 0);
4916
4917 // Enable playout again, and ensure audio starts flowing.
4918 caller()->pc()->SetAudioPlayout(true);
Seth Hampson2f0d7022018-02-20 11:54:42 -08004919 MediaExpectations media_expectations;
4920 media_expectations.ExpectBidirectionalAudio();
4921 ASSERT_TRUE(ExpectNewFrames(media_expectations));
henrika5f6bf242017-11-01 11:06:56 +01004922}
4923
4924double GetAudioEnergyStat(PeerConnectionWrapper* pc) {
4925 auto report = pc->NewGetStats();
4926 auto track_stats_list =
4927 report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
4928 const webrtc::RTCMediaStreamTrackStats* remote_track_stats = nullptr;
4929 for (const auto* track_stats : track_stats_list) {
4930 if (track_stats->remote_source.is_defined() &&
4931 *track_stats->remote_source) {
4932 remote_track_stats = track_stats;
4933 break;
4934 }
4935 }
4936
4937 if (!remote_track_stats->total_audio_energy.is_defined()) {
4938 return 0.0;
4939 }
4940 return *remote_track_stats->total_audio_energy;
4941}
4942
4943// Test that if audio playout is disabled via the SetAudioPlayout() method, then
4944// incoming audio is still processed and statistics are generated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004945TEST_P(PeerConnectionIntegrationTest,
henrika5f6bf242017-11-01 11:06:56 +01004946 DisableAudioPlayoutStillGeneratesAudioStats) {
4947 ASSERT_TRUE(CreatePeerConnectionWrappers());
4948 ConnectFakeSignaling();
4949
4950 // Set up audio-only call where playout is disabled but audio-processing is
4951 // still active.
Steve Anton15324772018-01-16 10:26:49 -08004952 caller()->AddAudioTrack();
4953 callee()->AddAudioTrack();
henrika5f6bf242017-11-01 11:06:56 +01004954 caller()->pc()->SetAudioPlayout(false);
4955
4956 caller()->CreateAndSetAndSignalOffer();
4957 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4958
4959 // Wait for the callee to receive audio stats.
4960 EXPECT_TRUE_WAIT(GetAudioEnergyStat(caller()) > 0, kMaxWaitForFramesMs);
4961}
4962
henrika4f167df2017-11-01 14:45:55 +01004963// Test that SetAudioRecording can be used to disable audio recording from the
4964// start, then later enable it. This may be useful, for example, if the caller
4965// wants to ensure that no audio resources are active before a certain state
4966// is reached.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004967TEST_P(PeerConnectionIntegrationTest, DisableAndEnableAudioRecording) {
henrika4f167df2017-11-01 14:45:55 +01004968 ASSERT_TRUE(CreatePeerConnectionWrappers());
4969 ConnectFakeSignaling();
4970
4971 // Set up audio-only call where audio recording is disabled on caller's side.
4972 caller()->pc()->SetAudioRecording(false);
Steve Anton15324772018-01-16 10:26:49 -08004973 caller()->AddAudioTrack();
4974 callee()->AddAudioTrack();
henrika4f167df2017-11-01 14:45:55 +01004975 caller()->CreateAndSetAndSignalOffer();
4976 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4977
4978 // Pump messages for a second.
4979 WAIT(false, 1000);
4980 // Since caller has disabled audio recording, the callee shouldn't have
4981 // received anything.
4982 EXPECT_EQ(0, callee()->audio_frames_received());
4983 // As a sanity check, make sure the caller did still see frames on its
4984 // audio level since audio recording is enabled on the calle side.
4985 ASSERT_GT(caller()->audio_frames_received(), 0);
4986
4987 // Enable audio recording again, and ensure audio starts flowing.
4988 caller()->pc()->SetAudioRecording(true);
Seth Hampson2f0d7022018-02-20 11:54:42 -08004989 MediaExpectations media_expectations;
4990 media_expectations.ExpectBidirectionalAudio();
4991 ASSERT_TRUE(ExpectNewFrames(media_expectations));
henrika4f167df2017-11-01 14:45:55 +01004992}
4993
Taylor Brandstetter389a97c2018-01-03 16:26:06 -08004994// Test that after closing PeerConnections, they stop sending any packets (ICE,
4995// DTLS, RTP...).
Seth Hampson2f0d7022018-02-20 11:54:42 -08004996TEST_P(PeerConnectionIntegrationTest, ClosingConnectionStopsPacketFlow) {
Taylor Brandstetter389a97c2018-01-03 16:26:06 -08004997 // Set up audio/video/data, wait for some frames to be received.
4998 ASSERT_TRUE(CreatePeerConnectionWrappers());
4999 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08005000 caller()->AddAudioVideoTracks();
Taylor Brandstetter389a97c2018-01-03 16:26:06 -08005001#ifdef HAVE_SCTP
5002 caller()->CreateDataChannel();
5003#endif
5004 caller()->CreateAndSetAndSignalOffer();
5005 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08005006 MediaExpectations media_expectations;
5007 media_expectations.CalleeExpectsSomeAudioAndVideo();
5008 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Taylor Brandstetter389a97c2018-01-03 16:26:06 -08005009 // Close PeerConnections.
5010 caller()->pc()->Close();
5011 callee()->pc()->Close();
5012 // Pump messages for a second, and ensure no new packets end up sent.
5013 uint32_t sent_packets_a = virtual_socket_server()->sent_packets();
5014 WAIT(false, 1000);
5015 uint32_t sent_packets_b = virtual_socket_server()->sent_packets();
5016 EXPECT_EQ(sent_packets_a, sent_packets_b);
5017}
5018
Steve Anton7eca0932018-03-30 15:18:41 -07005019// Test that transport stats are generated by the RTCStatsCollector for a
5020// connection that only involves data channels. This is a regression test for
5021// crbug.com/826972.
5022#ifdef HAVE_SCTP
5023TEST_P(PeerConnectionIntegrationTest,
5024 TransportStatsReportedForDataChannelOnlyConnection) {
5025 ASSERT_TRUE(CreatePeerConnectionWrappers());
5026 ConnectFakeSignaling();
5027 caller()->CreateDataChannel();
5028
5029 caller()->CreateAndSetAndSignalOffer();
5030 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5031 ASSERT_TRUE_WAIT(callee()->data_channel(), kDefaultTimeout);
5032
5033 auto caller_report = caller()->NewGetStats();
5034 EXPECT_EQ(1u, caller_report->GetStatsOfType<RTCTransportStats>().size());
5035 auto callee_report = callee()->NewGetStats();
5036 EXPECT_EQ(1u, callee_report->GetStatsOfType<RTCTransportStats>().size());
5037}
5038#endif // HAVE_SCTP
5039
Qingsi Wang7685e862018-06-11 20:15:46 -07005040TEST_P(PeerConnectionIntegrationTest,
5041 IceEventsGeneratedAndLoggedInRtcEventLog) {
5042 ASSERT_TRUE(CreatePeerConnectionWrappersWithFakeRtcEventLog());
5043 ConnectFakeSignaling();
5044 PeerConnectionInterface::RTCOfferAnswerOptions options;
5045 options.offer_to_receive_audio = 1;
5046 caller()->SetOfferAnswerOptions(options);
5047 caller()->CreateAndSetAndSignalOffer();
5048 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
5049 ASSERT_NE(nullptr, caller()->event_log_factory());
5050 ASSERT_NE(nullptr, callee()->event_log_factory());
5051 webrtc::FakeRtcEventLog* caller_event_log =
5052 static_cast<webrtc::FakeRtcEventLog*>(
5053 caller()->event_log_factory()->last_log_created());
5054 webrtc::FakeRtcEventLog* callee_event_log =
5055 static_cast<webrtc::FakeRtcEventLog*>(
5056 callee()->event_log_factory()->last_log_created());
5057 ASSERT_NE(nullptr, caller_event_log);
5058 ASSERT_NE(nullptr, callee_event_log);
5059 int caller_ice_config_count = caller_event_log->GetEventCount(
5060 webrtc::RtcEvent::Type::IceCandidatePairConfig);
5061 int caller_ice_event_count = caller_event_log->GetEventCount(
5062 webrtc::RtcEvent::Type::IceCandidatePairEvent);
5063 int callee_ice_config_count = callee_event_log->GetEventCount(
5064 webrtc::RtcEvent::Type::IceCandidatePairConfig);
5065 int callee_ice_event_count = callee_event_log->GetEventCount(
5066 webrtc::RtcEvent::Type::IceCandidatePairEvent);
5067 EXPECT_LT(0, caller_ice_config_count);
5068 EXPECT_LT(0, caller_ice_event_count);
5069 EXPECT_LT(0, callee_ice_config_count);
5070 EXPECT_LT(0, callee_ice_event_count);
5071}
5072
Qingsi Wangc129c352019-04-18 10:41:58 -07005073TEST_P(PeerConnectionIntegrationTest, RegatherAfterChangingIceTransportType) {
5074 webrtc::test::ScopedFieldTrials field_trials(
5075 "WebRTC-GatherOnCandidateFilterChanged/Enabled/");
5076 static const rtc::SocketAddress turn_server_internal_address{"88.88.88.0",
5077 3478};
5078 static const rtc::SocketAddress turn_server_external_address{"88.88.88.1", 0};
5079
5080 CreateTurnServer(turn_server_internal_address, turn_server_external_address);
5081
5082 webrtc::PeerConnectionInterface::IceServer ice_server;
5083 ice_server.urls.push_back("turn:88.88.88.0:3478");
5084 ice_server.username = "test";
5085 ice_server.password = "test";
5086
5087 PeerConnectionInterface::RTCConfiguration caller_config;
5088 caller_config.servers.push_back(ice_server);
5089 caller_config.type = webrtc::PeerConnectionInterface::kRelay;
5090 caller_config.continual_gathering_policy = PeerConnection::GATHER_CONTINUALLY;
5091
5092 PeerConnectionInterface::RTCConfiguration callee_config;
5093 callee_config.servers.push_back(ice_server);
5094 callee_config.type = webrtc::PeerConnectionInterface::kRelay;
5095 callee_config.continual_gathering_policy = PeerConnection::GATHER_CONTINUALLY;
5096
5097 ASSERT_TRUE(
5098 CreatePeerConnectionWrappersWithConfig(caller_config, callee_config));
5099
5100 // Do normal offer/answer and wait for ICE to complete.
5101 ConnectFakeSignaling();
5102 caller()->AddAudioVideoTracks();
5103 callee()->AddAudioVideoTracks();
5104 caller()->CreateAndSetAndSignalOffer();
5105 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5106 // Since we are doing continual gathering, the ICE transport does not reach
5107 // kIceGatheringComplete (see
5108 // P2PTransportChannel::OnCandidatesAllocationDone), and consequently not
5109 // kIceConnectionComplete.
5110 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
5111 caller()->ice_connection_state(), kDefaultTimeout);
5112 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
5113 callee()->ice_connection_state(), kDefaultTimeout);
5114 // Note that we cannot use the metric
5115 // |WebRTC.PeerConnection.CandidatePairType_UDP| in this test since this
5116 // metric is only populated when we reach kIceConnectionComplete in the
5117 // current implementation.
5118 EXPECT_EQ(cricket::RELAY_PORT_TYPE,
5119 caller()->last_candidate_gathered().type());
5120 EXPECT_EQ(cricket::RELAY_PORT_TYPE,
5121 callee()->last_candidate_gathered().type());
5122
5123 // Loosen the caller's candidate filter.
5124 caller_config = caller()->pc()->GetConfiguration();
5125 caller_config.type = webrtc::PeerConnectionInterface::kAll;
5126 caller()->pc()->SetConfiguration(caller_config);
5127 // We should have gathered a new host candidate.
5128 EXPECT_EQ_WAIT(cricket::LOCAL_PORT_TYPE,
5129 caller()->last_candidate_gathered().type(), kDefaultTimeout);
5130
5131 // Loosen the callee's candidate filter.
5132 callee_config = callee()->pc()->GetConfiguration();
5133 callee_config.type = webrtc::PeerConnectionInterface::kAll;
5134 callee()->pc()->SetConfiguration(callee_config);
5135 EXPECT_EQ_WAIT(cricket::LOCAL_PORT_TYPE,
5136 callee()->last_candidate_gathered().type(), kDefaultTimeout);
5137}
5138
Mirko Bonadeic84f6612019-01-31 12:20:57 +01005139INSTANTIATE_TEST_SUITE_P(PeerConnectionIntegrationTest,
5140 PeerConnectionIntegrationTest,
5141 Values(SdpSemantics::kPlanB,
5142 SdpSemantics::kUnifiedPlan));
Steve Antond3679212018-01-17 17:41:02 -08005143
Steve Anton74255ff2018-01-24 18:32:57 -08005144// Tests that verify interoperability between Plan B and Unified Plan
5145// PeerConnections.
5146class PeerConnectionIntegrationInteropTest
Seth Hampson2f0d7022018-02-20 11:54:42 -08005147 : public PeerConnectionIntegrationBaseTest,
Steve Anton74255ff2018-01-24 18:32:57 -08005148 public ::testing::WithParamInterface<
5149 std::tuple<SdpSemantics, SdpSemantics>> {
5150 protected:
Seth Hampson2f0d7022018-02-20 11:54:42 -08005151 // Setting the SdpSemantics for the base test to kDefault does not matter
5152 // because we specify not to use the test semantics when creating
5153 // PeerConnectionWrappers.
Steve Anton74255ff2018-01-24 18:32:57 -08005154 PeerConnectionIntegrationInteropTest()
Steve Anton3acffc32018-04-12 17:21:03 -07005155 : PeerConnectionIntegrationBaseTest(SdpSemantics::kPlanB),
Seth Hampson2f0d7022018-02-20 11:54:42 -08005156 caller_semantics_(std::get<0>(GetParam())),
Steve Anton74255ff2018-01-24 18:32:57 -08005157 callee_semantics_(std::get<1>(GetParam())) {}
5158
5159 bool CreatePeerConnectionWrappersWithSemantics() {
Steve Anton3acffc32018-04-12 17:21:03 -07005160 return CreatePeerConnectionWrappersWithSdpSemantics(caller_semantics_,
5161 callee_semantics_);
Steve Anton74255ff2018-01-24 18:32:57 -08005162 }
5163
5164 const SdpSemantics caller_semantics_;
5165 const SdpSemantics callee_semantics_;
5166};
5167
5168TEST_P(PeerConnectionIntegrationInteropTest, NoMediaLocalToNoMediaRemote) {
5169 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
5170 ConnectFakeSignaling();
5171
5172 caller()->CreateAndSetAndSignalOffer();
5173 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5174}
5175
5176TEST_P(PeerConnectionIntegrationInteropTest, OneAudioLocalToNoMediaRemote) {
5177 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
5178 ConnectFakeSignaling();
5179 auto audio_sender = caller()->AddAudioTrack();
5180
5181 caller()->CreateAndSetAndSignalOffer();
5182 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5183
5184 // Verify that one audio receiver has been created on the remote and that it
5185 // has the same track ID as the sending track.
5186 auto receivers = callee()->pc()->GetReceivers();
5187 ASSERT_EQ(1u, receivers.size());
5188 EXPECT_EQ(cricket::MEDIA_TYPE_AUDIO, receivers[0]->media_type());
5189 EXPECT_EQ(receivers[0]->track()->id(), audio_sender->track()->id());
5190
Seth Hampson2f0d7022018-02-20 11:54:42 -08005191 MediaExpectations media_expectations;
5192 media_expectations.CalleeExpectsSomeAudio();
5193 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08005194}
5195
5196TEST_P(PeerConnectionIntegrationInteropTest, OneAudioOneVideoToNoMediaRemote) {
5197 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
5198 ConnectFakeSignaling();
5199 auto video_sender = caller()->AddVideoTrack();
5200 auto audio_sender = caller()->AddAudioTrack();
5201
5202 caller()->CreateAndSetAndSignalOffer();
5203 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5204
5205 // Verify that one audio and one video receiver have been created on the
5206 // remote and that they have the same track IDs as the sending tracks.
5207 auto audio_receivers =
5208 callee()->GetReceiversOfType(cricket::MEDIA_TYPE_AUDIO);
5209 ASSERT_EQ(1u, audio_receivers.size());
5210 EXPECT_EQ(audio_receivers[0]->track()->id(), audio_sender->track()->id());
5211 auto video_receivers =
5212 callee()->GetReceiversOfType(cricket::MEDIA_TYPE_VIDEO);
5213 ASSERT_EQ(1u, video_receivers.size());
5214 EXPECT_EQ(video_receivers[0]->track()->id(), video_sender->track()->id());
5215
Seth Hampson2f0d7022018-02-20 11:54:42 -08005216 MediaExpectations media_expectations;
5217 media_expectations.CalleeExpectsSomeAudioAndVideo();
5218 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08005219}
5220
5221TEST_P(PeerConnectionIntegrationInteropTest,
5222 OneAudioOneVideoLocalToOneAudioOneVideoRemote) {
5223 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
5224 ConnectFakeSignaling();
5225 caller()->AddAudioVideoTracks();
5226 callee()->AddAudioVideoTracks();
5227
5228 caller()->CreateAndSetAndSignalOffer();
5229 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5230
Seth Hampson2f0d7022018-02-20 11:54:42 -08005231 MediaExpectations media_expectations;
5232 media_expectations.ExpectBidirectionalAudioAndVideo();
5233 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08005234}
5235
5236TEST_P(PeerConnectionIntegrationInteropTest,
5237 ReverseRolesOneAudioLocalToOneVideoRemote) {
5238 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
5239 ConnectFakeSignaling();
5240 caller()->AddAudioTrack();
5241 callee()->AddVideoTrack();
5242
5243 caller()->CreateAndSetAndSignalOffer();
5244 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5245
5246 // Verify that only the audio track has been negotiated.
5247 EXPECT_EQ(0u, caller()->GetReceiversOfType(cricket::MEDIA_TYPE_VIDEO).size());
5248 // Might also check that the callee's NegotiationNeeded flag is set.
5249
5250 // Reverse roles.
5251 callee()->CreateAndSetAndSignalOffer();
5252 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5253
Seth Hampson2f0d7022018-02-20 11:54:42 -08005254 MediaExpectations media_expectations;
5255 media_expectations.CallerExpectsSomeVideo();
5256 media_expectations.CalleeExpectsSomeAudio();
5257 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08005258}
5259
Mirko Bonadeic84f6612019-01-31 12:20:57 +01005260INSTANTIATE_TEST_SUITE_P(
Steve Antonba42e992018-04-09 14:10:01 -07005261 PeerConnectionIntegrationTest,
5262 PeerConnectionIntegrationInteropTest,
5263 Values(std::make_tuple(SdpSemantics::kPlanB, SdpSemantics::kUnifiedPlan),
5264 std::make_tuple(SdpSemantics::kUnifiedPlan, SdpSemantics::kPlanB)));
5265
5266// Test that if the Unified Plan side offers two video tracks then the Plan B
5267// side will only see the first one and ignore the second.
5268TEST_F(PeerConnectionIntegrationTestPlanB, TwoVideoUnifiedPlanToNoMediaPlanB) {
Steve Anton3acffc32018-04-12 17:21:03 -07005269 ASSERT_TRUE(CreatePeerConnectionWrappersWithSdpSemantics(
5270 SdpSemantics::kUnifiedPlan, SdpSemantics::kPlanB));
Steve Anton74255ff2018-01-24 18:32:57 -08005271 ConnectFakeSignaling();
5272 auto first_sender = caller()->AddVideoTrack();
5273 caller()->AddVideoTrack();
5274
5275 caller()->CreateAndSetAndSignalOffer();
5276 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5277
5278 // Verify that there is only one receiver and it corresponds to the first
5279 // added track.
5280 auto receivers = callee()->pc()->GetReceivers();
5281 ASSERT_EQ(1u, receivers.size());
5282 EXPECT_TRUE(receivers[0]->track()->enabled());
5283 EXPECT_EQ(first_sender->track()->id(), receivers[0]->track()->id());
5284
Seth Hampson2f0d7022018-02-20 11:54:42 -08005285 MediaExpectations media_expectations;
5286 media_expectations.CalleeExpectsSomeVideo();
5287 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08005288}
5289
Steve Anton2bed3972019-01-04 17:04:30 -08005290// Test that if the initial offer tagged BUNDLE section is rejected due to its
5291// associated RtpTransceiver being stopped and another transceiver is added,
5292// then renegotiation causes the callee to receive the new video track without
5293// error.
5294// This is a regression test for bugs.webrtc.org/9954
5295TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
5296 ReOfferWithStoppedBundleTaggedTransceiver) {
5297 RTCConfiguration config;
5298 config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
5299 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
5300 ConnectFakeSignaling();
5301 auto audio_transceiver_or_error =
5302 caller()->pc()->AddTransceiver(caller()->CreateLocalAudioTrack());
5303 ASSERT_TRUE(audio_transceiver_or_error.ok());
5304 auto audio_transceiver = audio_transceiver_or_error.MoveValue();
5305
5306 caller()->CreateAndSetAndSignalOffer();
5307 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5308 {
5309 MediaExpectations media_expectations;
5310 media_expectations.CalleeExpectsSomeAudio();
5311 ASSERT_TRUE(ExpectNewFrames(media_expectations));
5312 }
5313
5314 audio_transceiver->Stop();
5315 caller()->pc()->AddTransceiver(caller()->CreateLocalVideoTrack());
5316
5317 caller()->CreateAndSetAndSignalOffer();
5318 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5319 {
5320 MediaExpectations media_expectations;
5321 media_expectations.CalleeExpectsSomeVideo();
5322 ASSERT_TRUE(ExpectNewFrames(media_expectations));
5323 }
5324}
5325
Harald Alvestrandd61f2a72019-05-08 20:20:59 +02005326#ifdef HAVE_SCTP
5327
5328TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
5329 EndToEndCallWithBundledSctpDataChannel) {
5330 ASSERT_TRUE(CreatePeerConnectionWrappers());
5331 ConnectFakeSignaling();
5332 caller()->CreateDataChannel();
5333 caller()->AddAudioVideoTracks();
5334 callee()->AddAudioVideoTracks();
5335 caller()->SetGeneratedSdpMunger(MakeSpecCompliantSctpOffer);
5336 caller()->CreateAndSetAndSignalOffer();
5337 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5338 // Ensure that media and data are multiplexed on the same DTLS transport.
5339 // This only works on Unified Plan, because transports are not exposed in plan
5340 // B.
5341 auto sctp_info = caller()->pc()->GetSctpTransport()->Information();
5342 EXPECT_EQ(sctp_info.dtls_transport(),
5343 caller()->pc()->GetSenders()[0]->dtls_transport());
5344}
5345
5346#endif // HAVE_SCTP
5347
deadbeef1dcb1642017-03-29 21:08:16 -07005348} // namespace
Mirko Bonadeiab64e8a2018-12-12 12:10:18 +01005349} // namespace webrtc
deadbeef1dcb1642017-03-29 21:08:16 -07005350
5351#endif // if !defined(THREAD_SANITIZER)