blob: 7b3163ba0eec0b9e5ad107d4940fceaaebe9a4d8 [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"
Qingsi Wangecd30542019-05-22 14:34:56 -070064#include "rtc_base/fake_mdns_responder.h"
Steve Anton10542f22019-01-11 09:11:00 -080065#include "rtc_base/fake_network.h"
66#include "rtc_base/firewall_socket_server.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020067#include "rtc_base/gunit.h"
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +020068#include "rtc_base/numerics/safe_conversions.h"
Steve Anton10542f22019-01-11 09:11:00 -080069#include "rtc_base/test_certificate_verifier.h"
70#include "rtc_base/time_utils.h"
71#include "rtc_base/virtual_socket_server.h"
Mirko Bonadei17f48782018-09-28 08:51:10 +020072#include "system_wrappers/include/metrics.h"
Qingsi Wangc129c352019-04-18 10:41:58 -070073#include "test/field_trial.h"
Elad Alon99c3fe52017-10-13 16:29:40 +020074#include "test/gmock.h"
deadbeef1dcb1642017-03-29 21:08:16 -070075
Mirko Bonadeiab64e8a2018-12-12 12:10:18 +010076namespace webrtc {
77namespace {
78
79using ::cricket::ContentInfo;
80using ::cricket::StreamParams;
81using ::rtc::SocketAddress;
82using ::testing::_;
Seth Hampson2f0d7022018-02-20 11:54:42 -080083using ::testing::Combine;
Steve Anton64b626b2019-01-28 17:25:26 -080084using ::testing::Contains;
Mirko Bonadeie46f5db2019-03-26 20:14:46 +010085using ::testing::DoAll;
Steve Antonede9ca52017-10-16 13:04:27 -070086using ::testing::ElementsAre;
Qingsi Wang1dac6d82018-12-12 15:28:47 -080087using ::testing::NiceMock;
Steve Anton64b626b2019-01-28 17:25:26 -080088using ::testing::Return;
Zach Stein6fcdc2f2018-08-23 16:25:55 -070089using ::testing::SetArgPointee;
Steve Antonffa6ce42018-11-30 09:26:08 -080090using ::testing::UnorderedElementsAreArray;
Mirko Bonadeiab64e8a2018-12-12 12:10:18 +010091using ::testing::Values;
Steve Anton74255ff2018-01-24 18:32:57 -080092using RTCConfiguration = PeerConnectionInterface::RTCConfiguration;
deadbeef1dcb1642017-03-29 21:08:16 -070093
94static const int kDefaultTimeout = 10000;
95static const int kMaxWaitForStatsMs = 3000;
96static const int kMaxWaitForActivationMs = 5000;
97static const int kMaxWaitForFramesMs = 10000;
98// Default number of audio/video frames to wait for before considering a test
99// successful.
100static const int kDefaultExpectedAudioFrameCount = 3;
101static const int kDefaultExpectedVideoFrameCount = 3;
102
deadbeef1dcb1642017-03-29 21:08:16 -0700103static const char kDataChannelLabel[] = "data_channel";
104
105// SRTP cipher name negotiated by the tests. This must be updated if the
106// default changes.
Taylor Brandstetterfd350d72018-04-03 16:29:26 -0700107static const int kDefaultSrtpCryptoSuite = rtc::SRTP_AES128_CM_SHA1_80;
deadbeef1dcb1642017-03-29 21:08:16 -0700108static const int kDefaultSrtpCryptoSuiteGcm = rtc::SRTP_AEAD_AES_256_GCM;
109
Steve Antonede9ca52017-10-16 13:04:27 -0700110static const SocketAddress kDefaultLocalAddress("192.168.1.1", 0);
111
deadbeef1dcb1642017-03-29 21:08:16 -0700112// Helper function for constructing offer/answer options to initiate an ICE
113// restart.
114PeerConnectionInterface::RTCOfferAnswerOptions IceRestartOfferAnswerOptions() {
115 PeerConnectionInterface::RTCOfferAnswerOptions options;
116 options.ice_restart = true;
117 return options;
118}
119
deadbeefd8ad7882017-04-18 16:01:17 -0700120// Remove all stream information (SSRCs, track IDs, etc.) and "msid-semantic"
121// attribute from received SDP, simulating a legacy endpoint.
122void RemoveSsrcsAndMsids(cricket::SessionDescription* desc) {
123 for (ContentInfo& content : desc->contents()) {
Steve Antonb1c1de12017-12-21 15:14:30 -0800124 content.media_description()->mutable_streams().clear();
deadbeefd8ad7882017-04-18 16:01:17 -0700125 }
126 desc->set_msid_supported(false);
Henrik Boström5b147782018-12-04 11:25:05 +0100127 desc->set_msid_signaling(0);
deadbeefd8ad7882017-04-18 16:01:17 -0700128}
129
Seth Hampson5897a6e2018-04-03 11:16:33 -0700130// Removes all stream information besides the stream ids, simulating an
131// endpoint that only signals a=msid lines to convey stream_ids.
132void RemoveSsrcsAndKeepMsids(cricket::SessionDescription* desc) {
133 for (ContentInfo& content : desc->contents()) {
Steve Antondf527fd2018-04-27 15:52:03 -0700134 std::string track_id;
Seth Hampson5897a6e2018-04-03 11:16:33 -0700135 std::vector<std::string> stream_ids;
136 if (!content.media_description()->streams().empty()) {
Steve Antondf527fd2018-04-27 15:52:03 -0700137 const StreamParams& first_stream =
138 content.media_description()->streams()[0];
139 track_id = first_stream.id;
140 stream_ids = first_stream.stream_ids();
Seth Hampson5897a6e2018-04-03 11:16:33 -0700141 }
142 content.media_description()->mutable_streams().clear();
Steve Antondf527fd2018-04-27 15:52:03 -0700143 StreamParams new_stream;
144 new_stream.id = track_id;
Seth Hampson5897a6e2018-04-03 11:16:33 -0700145 new_stream.set_stream_ids(stream_ids);
146 content.media_description()->AddStream(new_stream);
147 }
148}
149
zhihuangf8164932017-05-19 13:09:47 -0700150int FindFirstMediaStatsIndexByKind(
151 const std::string& kind,
152 const std::vector<const webrtc::RTCMediaStreamTrackStats*>&
153 media_stats_vec) {
154 for (size_t i = 0; i < media_stats_vec.size(); i++) {
155 if (media_stats_vec[i]->kind.ValueToString() == kind) {
156 return i;
157 }
158 }
159 return -1;
160}
161
deadbeef1dcb1642017-03-29 21:08:16 -0700162class SignalingMessageReceiver {
163 public:
Steve Antona3a92c22017-12-07 10:27:41 -0800164 virtual void ReceiveSdpMessage(SdpType type, const std::string& msg) = 0;
deadbeef1dcb1642017-03-29 21:08:16 -0700165 virtual void ReceiveIceMessage(const std::string& sdp_mid,
166 int sdp_mline_index,
167 const std::string& msg) = 0;
168
169 protected:
170 SignalingMessageReceiver() {}
171 virtual ~SignalingMessageReceiver() {}
172};
173
174class MockRtpReceiverObserver : public webrtc::RtpReceiverObserverInterface {
175 public:
176 explicit MockRtpReceiverObserver(cricket::MediaType media_type)
177 : expected_media_type_(media_type) {}
178
179 void OnFirstPacketReceived(cricket::MediaType media_type) override {
180 ASSERT_EQ(expected_media_type_, media_type);
181 first_packet_received_ = true;
182 }
183
184 bool first_packet_received() const { return first_packet_received_; }
185
186 virtual ~MockRtpReceiverObserver() {}
187
188 private:
189 bool first_packet_received_ = false;
190 cricket::MediaType expected_media_type_;
191};
192
193// Helper class that wraps a peer connection, observes it, and can accept
194// signaling messages from another wrapper.
195//
196// Uses a fake network, fake A/V capture, and optionally fake
197// encoders/decoders, though they aren't used by default since they don't
198// advertise support of any codecs.
Steve Anton94286cb2017-09-26 16:20:19 -0700199// TODO(steveanton): See how this could become a subclass of
Seth Hampson2f0d7022018-02-20 11:54:42 -0800200// PeerConnectionWrapper defined in peerconnectionwrapper.h.
deadbeef1dcb1642017-03-29 21:08:16 -0700201class PeerConnectionWrapper : public webrtc::PeerConnectionObserver,
Steve Anton15324772018-01-16 10:26:49 -0800202 public SignalingMessageReceiver {
deadbeef1dcb1642017-03-29 21:08:16 -0700203 public:
204 // Different factory methods for convenience.
205 // TODO(deadbeef): Could use the pattern of:
206 //
207 // PeerConnectionWrapper =
208 // WrapperBuilder.WithConfig(...).WithOptions(...).build();
209 //
210 // To reduce some code duplication.
211 static PeerConnectionWrapper* CreateWithDtlsIdentityStore(
212 const std::string& debug_name,
213 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator,
214 rtc::Thread* network_thread,
215 rtc::Thread* worker_thread) {
216 PeerConnectionWrapper* client(new PeerConnectionWrapper(debug_name));
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700217 webrtc::PeerConnectionDependencies dependencies(nullptr);
218 dependencies.cert_generator = std::move(cert_generator);
Niels Möllerf06f9232018-08-07 12:32:18 +0200219 if (!client->Init(nullptr, nullptr, std::move(dependencies), network_thread,
Bjorn Mellem175aa2e2018-11-08 11:23:22 -0800220 worker_thread, nullptr,
221 /*media_transport_factory=*/nullptr)) {
deadbeef1dcb1642017-03-29 21:08:16 -0700222 delete client;
223 return nullptr;
224 }
225 return client;
226 }
227
deadbeef2f425aa2017-04-14 10:41:32 -0700228 webrtc::PeerConnectionFactoryInterface* pc_factory() const {
229 return peer_connection_factory_.get();
230 }
231
deadbeef1dcb1642017-03-29 21:08:16 -0700232 webrtc::PeerConnectionInterface* pc() const { return peer_connection_.get(); }
233
234 // If a signaling message receiver is set (via ConnectFakeSignaling), this
235 // will set the whole offer/answer exchange in motion. Just need to wait for
236 // the signaling state to reach "stable".
237 void CreateAndSetAndSignalOffer() {
238 auto offer = CreateOffer();
239 ASSERT_NE(nullptr, offer);
240 EXPECT_TRUE(SetLocalDescriptionAndSendSdpMessage(std::move(offer)));
241 }
242
243 // Sets the options to be used when CreateAndSetAndSignalOffer is called, or
244 // when a remote offer is received (via fake signaling) and an answer is
245 // generated. By default, uses default options.
246 void SetOfferAnswerOptions(
247 const PeerConnectionInterface::RTCOfferAnswerOptions& options) {
248 offer_answer_options_ = options;
249 }
250
251 // Set a callback to be invoked when SDP is received via the fake signaling
252 // channel, which provides an opportunity to munge (modify) the SDP. This is
253 // used to test SDP being applied that a PeerConnection would normally not
254 // generate, but a non-JSEP endpoint might.
255 void SetReceivedSdpMunger(
256 std::function<void(cricket::SessionDescription*)> munger) {
Mirko Bonadeic61ce0d2017-11-21 17:04:20 +0100257 received_sdp_munger_ = std::move(munger);
deadbeef1dcb1642017-03-29 21:08:16 -0700258 }
259
deadbeefc964d0b2017-04-03 10:03:35 -0700260 // Similar to the above, but this is run on SDP immediately after it's
deadbeef1dcb1642017-03-29 21:08:16 -0700261 // generated.
262 void SetGeneratedSdpMunger(
263 std::function<void(cricket::SessionDescription*)> munger) {
Mirko Bonadeic61ce0d2017-11-21 17:04:20 +0100264 generated_sdp_munger_ = std::move(munger);
deadbeef1dcb1642017-03-29 21:08:16 -0700265 }
266
Seth Hampson2f0d7022018-02-20 11:54:42 -0800267 // Set a callback to be invoked when a remote offer is received via the fake
268 // signaling channel. This provides an opportunity to change the
269 // PeerConnection state before an answer is created and sent to the caller.
270 void SetRemoteOfferHandler(std::function<void()> handler) {
271 remote_offer_handler_ = std::move(handler);
272 }
273
Qingsi Wang1dac6d82018-12-12 15:28:47 -0800274 void SetRemoteAsyncResolver(rtc::MockAsyncResolver* resolver) {
275 remote_async_resolver_ = resolver;
Zach Stein6fcdc2f2018-08-23 16:25:55 -0700276 }
277
Steve Antonede9ca52017-10-16 13:04:27 -0700278 // Every ICE connection state in order that has been seen by the observer.
279 std::vector<PeerConnectionInterface::IceConnectionState>
280 ice_connection_state_history() const {
281 return ice_connection_state_history_;
282 }
Steve Anton6f25b092017-10-23 09:39:20 -0700283 void clear_ice_connection_state_history() {
284 ice_connection_state_history_.clear();
285 }
Steve Antonede9ca52017-10-16 13:04:27 -0700286
Jonas Olssonacd8ae72019-02-25 15:26:24 +0100287 // Every standardized ICE connection state in order that has been seen by the
288 // observer.
289 std::vector<PeerConnectionInterface::IceConnectionState>
290 standardized_ice_connection_state_history() const {
291 return standardized_ice_connection_state_history_;
292 }
293
Jonas Olsson635474e2018-10-18 15:58:17 +0200294 // Every PeerConnection state in order that has been seen by the observer.
295 std::vector<PeerConnectionInterface::PeerConnectionState>
296 peer_connection_state_history() const {
297 return peer_connection_state_history_;
298 }
299
Steve Antonede9ca52017-10-16 13:04:27 -0700300 // Every ICE gathering state in order that has been seen by the observer.
301 std::vector<PeerConnectionInterface::IceGatheringState>
302 ice_gathering_state_history() const {
303 return ice_gathering_state_history_;
deadbeef1dcb1642017-03-29 21:08:16 -0700304 }
305
Steve Anton15324772018-01-16 10:26:49 -0800306 void AddAudioVideoTracks() {
307 AddAudioTrack();
308 AddVideoTrack();
deadbeef1dcb1642017-03-29 21:08:16 -0700309 }
310
Steve Anton74255ff2018-01-24 18:32:57 -0800311 rtc::scoped_refptr<RtpSenderInterface> AddAudioTrack() {
312 return AddTrack(CreateLocalAudioTrack());
313 }
deadbeef1dcb1642017-03-29 21:08:16 -0700314
Steve Anton74255ff2018-01-24 18:32:57 -0800315 rtc::scoped_refptr<RtpSenderInterface> AddVideoTrack() {
316 return AddTrack(CreateLocalVideoTrack());
317 }
deadbeef1dcb1642017-03-29 21:08:16 -0700318
319 rtc::scoped_refptr<webrtc::AudioTrackInterface> CreateLocalAudioTrack() {
Niels Möller2d02e082018-05-21 11:23:35 +0200320 cricket::AudioOptions options;
deadbeef1dcb1642017-03-29 21:08:16 -0700321 // Disable highpass filter so that we can get all the test audio frames.
Niels Möller2d02e082018-05-21 11:23:35 +0200322 options.highpass_filter = false;
deadbeef1dcb1642017-03-29 21:08:16 -0700323 rtc::scoped_refptr<webrtc::AudioSourceInterface> source =
Niels Möller2d02e082018-05-21 11:23:35 +0200324 peer_connection_factory_->CreateAudioSource(options);
deadbeef1dcb1642017-03-29 21:08:16 -0700325 // TODO(perkj): Test audio source when it is implemented. Currently audio
326 // always use the default input.
deadbeefb1a15d72017-09-07 14:12:05 -0700327 return peer_connection_factory_->CreateAudioTrack(rtc::CreateRandomUuid(),
deadbeef1dcb1642017-03-29 21:08:16 -0700328 source);
329 }
330
331 rtc::scoped_refptr<webrtc::VideoTrackInterface> CreateLocalVideoTrack() {
Johannes Kron965e7942018-09-13 15:36:20 +0200332 webrtc::FakePeriodicVideoSource::Config config;
333 config.timestamp_offset_ms = rtc::TimeMillis();
334 return CreateLocalVideoTrackInternal(config);
deadbeef1dcb1642017-03-29 21:08:16 -0700335 }
336
337 rtc::scoped_refptr<webrtc::VideoTrackInterface>
Niels Möller5c7efe72018-05-11 10:34:46 +0200338 CreateLocalVideoTrackWithConfig(
339 webrtc::FakePeriodicVideoSource::Config config) {
340 return CreateLocalVideoTrackInternal(config);
deadbeef1dcb1642017-03-29 21:08:16 -0700341 }
342
343 rtc::scoped_refptr<webrtc::VideoTrackInterface>
344 CreateLocalVideoTrackWithRotation(webrtc::VideoRotation rotation) {
Niels Möller5c7efe72018-05-11 10:34:46 +0200345 webrtc::FakePeriodicVideoSource::Config config;
346 config.rotation = rotation;
Johannes Kron965e7942018-09-13 15:36:20 +0200347 config.timestamp_offset_ms = rtc::TimeMillis();
Niels Möller5c7efe72018-05-11 10:34:46 +0200348 return CreateLocalVideoTrackInternal(config);
deadbeef1dcb1642017-03-29 21:08:16 -0700349 }
350
Steve Anton74255ff2018-01-24 18:32:57 -0800351 rtc::scoped_refptr<RtpSenderInterface> AddTrack(
352 rtc::scoped_refptr<MediaStreamTrackInterface> track,
Seth Hampson845e8782018-03-02 11:34:10 -0800353 const std::vector<std::string>& stream_ids = {}) {
354 auto result = pc()->AddTrack(track, stream_ids);
Steve Anton15324772018-01-16 10:26:49 -0800355 EXPECT_EQ(RTCErrorType::NONE, result.error().type());
Steve Anton74255ff2018-01-24 18:32:57 -0800356 return result.MoveValue();
357 }
358
359 std::vector<rtc::scoped_refptr<RtpReceiverInterface>> GetReceiversOfType(
360 cricket::MediaType media_type) {
361 std::vector<rtc::scoped_refptr<RtpReceiverInterface>> receivers;
Mirko Bonadei739baf02019-01-27 17:29:42 +0100362 for (const auto& receiver : pc()->GetReceivers()) {
Steve Anton74255ff2018-01-24 18:32:57 -0800363 if (receiver->media_type() == media_type) {
364 receivers.push_back(receiver);
365 }
366 }
367 return receivers;
deadbeef1dcb1642017-03-29 21:08:16 -0700368 }
369
Seth Hampson2f0d7022018-02-20 11:54:42 -0800370 rtc::scoped_refptr<RtpTransceiverInterface> GetFirstTransceiverOfType(
371 cricket::MediaType media_type) {
372 for (auto transceiver : pc()->GetTransceivers()) {
373 if (transceiver->receiver()->media_type() == media_type) {
374 return transceiver;
375 }
376 }
377 return nullptr;
378 }
379
deadbeef1dcb1642017-03-29 21:08:16 -0700380 bool SignalingStateStable() {
381 return pc()->signaling_state() == webrtc::PeerConnectionInterface::kStable;
382 }
383
384 void CreateDataChannel() { CreateDataChannel(nullptr); }
385
386 void CreateDataChannel(const webrtc::DataChannelInit* init) {
Steve Antonda6c0952017-10-23 11:41:54 -0700387 CreateDataChannel(kDataChannelLabel, init);
388 }
389
390 void CreateDataChannel(const std::string& label,
391 const webrtc::DataChannelInit* init) {
392 data_channel_ = pc()->CreateDataChannel(label, init);
deadbeef1dcb1642017-03-29 21:08:16 -0700393 ASSERT_TRUE(data_channel_.get() != nullptr);
394 data_observer_.reset(new MockDataChannelObserver(data_channel_));
395 }
396
397 DataChannelInterface* data_channel() { return data_channel_; }
398 const MockDataChannelObserver* data_observer() const {
399 return data_observer_.get();
400 }
401
402 int audio_frames_received() const {
403 return fake_audio_capture_module_->frames_received();
404 }
405
406 // Takes minimum of video frames received for each track.
407 //
408 // Can be used like:
409 // EXPECT_GE(expected_frames, min_video_frames_received_per_track());
410 //
411 // To ensure that all video tracks received at least a certain number of
412 // frames.
413 int min_video_frames_received_per_track() const {
414 int min_frames = INT_MAX;
Anders Carlsson5f2bb622018-05-14 09:48:06 +0200415 if (fake_video_renderers_.empty()) {
416 return 0;
deadbeef1dcb1642017-03-29 21:08:16 -0700417 }
deadbeef1dcb1642017-03-29 21:08:16 -0700418
Anders Carlsson5f2bb622018-05-14 09:48:06 +0200419 for (const auto& pair : fake_video_renderers_) {
420 min_frames = std::min(min_frames, pair.second->num_rendered_frames());
deadbeef1dcb1642017-03-29 21:08:16 -0700421 }
Anders Carlsson5f2bb622018-05-14 09:48:06 +0200422 return min_frames;
deadbeef1dcb1642017-03-29 21:08:16 -0700423 }
424
425 // Returns a MockStatsObserver in a state after stats gathering finished,
426 // which can be used to access the gathered stats.
deadbeefd8ad7882017-04-18 16:01:17 -0700427 rtc::scoped_refptr<MockStatsObserver> OldGetStatsForTrack(
deadbeef1dcb1642017-03-29 21:08:16 -0700428 webrtc::MediaStreamTrackInterface* track) {
429 rtc::scoped_refptr<MockStatsObserver> observer(
430 new rtc::RefCountedObject<MockStatsObserver>());
431 EXPECT_TRUE(peer_connection_->GetStats(
432 observer, nullptr, PeerConnectionInterface::kStatsOutputLevelStandard));
433 EXPECT_TRUE_WAIT(observer->called(), kDefaultTimeout);
434 return observer;
435 }
436
437 // Version that doesn't take a track "filter", and gathers all stats.
deadbeefd8ad7882017-04-18 16:01:17 -0700438 rtc::scoped_refptr<MockStatsObserver> OldGetStats() {
439 return OldGetStatsForTrack(nullptr);
440 }
441
442 // Synchronously gets stats and returns them. If it times out, fails the test
443 // and returns null.
444 rtc::scoped_refptr<const webrtc::RTCStatsReport> NewGetStats() {
445 rtc::scoped_refptr<webrtc::MockRTCStatsCollectorCallback> callback(
446 new rtc::RefCountedObject<webrtc::MockRTCStatsCollectorCallback>());
447 peer_connection_->GetStats(callback);
448 EXPECT_TRUE_WAIT(callback->called(), kDefaultTimeout);
449 return callback->report();
deadbeef1dcb1642017-03-29 21:08:16 -0700450 }
451
452 int rendered_width() {
453 EXPECT_FALSE(fake_video_renderers_.empty());
454 return fake_video_renderers_.empty()
455 ? 0
456 : fake_video_renderers_.begin()->second->width();
457 }
458
459 int rendered_height() {
460 EXPECT_FALSE(fake_video_renderers_.empty());
461 return fake_video_renderers_.empty()
462 ? 0
463 : fake_video_renderers_.begin()->second->height();
464 }
465
466 double rendered_aspect_ratio() {
467 if (rendered_height() == 0) {
468 return 0.0;
469 }
470 return static_cast<double>(rendered_width()) / rendered_height();
471 }
472
473 webrtc::VideoRotation rendered_rotation() {
474 EXPECT_FALSE(fake_video_renderers_.empty());
475 return fake_video_renderers_.empty()
476 ? webrtc::kVideoRotation_0
477 : fake_video_renderers_.begin()->second->rotation();
478 }
479
480 int local_rendered_width() {
481 return local_video_renderer_ ? local_video_renderer_->width() : 0;
482 }
483
484 int local_rendered_height() {
485 return local_video_renderer_ ? local_video_renderer_->height() : 0;
486 }
487
488 double local_rendered_aspect_ratio() {
489 if (local_rendered_height() == 0) {
490 return 0.0;
491 }
492 return static_cast<double>(local_rendered_width()) /
493 local_rendered_height();
494 }
495
496 size_t number_of_remote_streams() {
497 if (!pc()) {
498 return 0;
499 }
500 return pc()->remote_streams()->count();
501 }
502
503 StreamCollectionInterface* remote_streams() const {
504 if (!pc()) {
505 ADD_FAILURE();
506 return nullptr;
507 }
508 return pc()->remote_streams();
509 }
510
511 StreamCollectionInterface* local_streams() {
512 if (!pc()) {
513 ADD_FAILURE();
514 return nullptr;
515 }
516 return pc()->local_streams();
517 }
518
519 webrtc::PeerConnectionInterface::SignalingState signaling_state() {
520 return pc()->signaling_state();
521 }
522
523 webrtc::PeerConnectionInterface::IceConnectionState ice_connection_state() {
524 return pc()->ice_connection_state();
525 }
526
Jonas Olsson7a6739e2019-01-15 16:31:55 +0100527 webrtc::PeerConnectionInterface::IceConnectionState
528 standardized_ice_connection_state() {
529 return pc()->standardized_ice_connection_state();
530 }
531
deadbeef1dcb1642017-03-29 21:08:16 -0700532 webrtc::PeerConnectionInterface::IceGatheringState ice_gathering_state() {
533 return pc()->ice_gathering_state();
534 }
535
536 // Returns a MockRtpReceiverObserver for each RtpReceiver returned by
537 // GetReceivers. They're updated automatically when a remote offer/answer
538 // from the fake signaling channel is applied, or when
539 // ResetRtpReceiverObservers below is called.
540 const std::vector<std::unique_ptr<MockRtpReceiverObserver>>&
541 rtp_receiver_observers() {
542 return rtp_receiver_observers_;
543 }
544
545 void ResetRtpReceiverObservers() {
546 rtp_receiver_observers_.clear();
Mirko Bonadeic61ce0d2017-11-21 17:04:20 +0100547 for (const rtc::scoped_refptr<RtpReceiverInterface>& receiver :
548 pc()->GetReceivers()) {
deadbeef1dcb1642017-03-29 21:08:16 -0700549 std::unique_ptr<MockRtpReceiverObserver> observer(
550 new MockRtpReceiverObserver(receiver->media_type()));
551 receiver->SetObserver(observer.get());
552 rtp_receiver_observers_.push_back(std::move(observer));
553 }
554 }
555
Qingsi Wangecd30542019-05-22 14:34:56 -0700556 rtc::FakeNetworkManager* network_manager() const {
Steve Antonede9ca52017-10-16 13:04:27 -0700557 return fake_network_manager_.get();
558 }
559 cricket::PortAllocator* port_allocator() const { return port_allocator_; }
560
Qingsi Wang7685e862018-06-11 20:15:46 -0700561 webrtc::FakeRtcEventLogFactory* event_log_factory() const {
562 return event_log_factory_;
563 }
564
Qingsi Wangc129c352019-04-18 10:41:58 -0700565 const cricket::Candidate& last_candidate_gathered() const {
566 return last_candidate_gathered_;
567 }
568
Qingsi Wangecd30542019-05-22 14:34:56 -0700569 // Sets the mDNS responder for the owned fake network manager and keeps a
570 // reference to the responder.
571 void SetMdnsResponder(
572 std::unique_ptr<webrtc::FakeMdnsResponder> mdns_responder) {
573 RTC_DCHECK(mdns_responder != nullptr);
574 mdns_responder_ = mdns_responder.get();
575 network_manager()->set_mdns_responder(std::move(mdns_responder));
576 }
577
deadbeef1dcb1642017-03-29 21:08:16 -0700578 private:
579 explicit PeerConnectionWrapper(const std::string& debug_name)
580 : debug_name_(debug_name) {}
581
Bjorn Mellem175aa2e2018-11-08 11:23:22 -0800582 bool Init(
583 const PeerConnectionFactory::Options* options,
584 const PeerConnectionInterface::RTCConfiguration* config,
585 webrtc::PeerConnectionDependencies dependencies,
586 rtc::Thread* network_thread,
587 rtc::Thread* worker_thread,
588 std::unique_ptr<webrtc::FakeRtcEventLogFactory> event_log_factory,
589 std::unique_ptr<webrtc::MediaTransportFactory> media_transport_factory) {
deadbeef1dcb1642017-03-29 21:08:16 -0700590 // There's an error in this test code if Init ends up being called twice.
591 RTC_DCHECK(!peer_connection_);
592 RTC_DCHECK(!peer_connection_factory_);
593
594 fake_network_manager_.reset(new rtc::FakeNetworkManager());
Steve Antonede9ca52017-10-16 13:04:27 -0700595 fake_network_manager_->AddInterface(kDefaultLocalAddress);
deadbeef1dcb1642017-03-29 21:08:16 -0700596
597 std::unique_ptr<cricket::PortAllocator> port_allocator(
598 new cricket::BasicPortAllocator(fake_network_manager_.get()));
Steve Antonede9ca52017-10-16 13:04:27 -0700599 port_allocator_ = port_allocator.get();
deadbeef1dcb1642017-03-29 21:08:16 -0700600 fake_audio_capture_module_ = FakeAudioCaptureModule::Create();
601 if (!fake_audio_capture_module_) {
602 return false;
603 }
deadbeef1dcb1642017-03-29 21:08:16 -0700604 rtc::Thread* const signaling_thread = rtc::Thread::Current();
Qingsi Wang7685e862018-06-11 20:15:46 -0700605
606 webrtc::PeerConnectionFactoryDependencies pc_factory_dependencies;
607 pc_factory_dependencies.network_thread = network_thread;
608 pc_factory_dependencies.worker_thread = worker_thread;
609 pc_factory_dependencies.signaling_thread = signaling_thread;
610 pc_factory_dependencies.media_engine =
611 cricket::WebRtcMediaEngineFactory::Create(
612 rtc::scoped_refptr<webrtc::AudioDeviceModule>(
613 fake_audio_capture_module_),
614 webrtc::CreateBuiltinAudioEncoderFactory(),
615 webrtc::CreateBuiltinAudioDecoderFactory(),
616 webrtc::CreateBuiltinVideoEncoderFactory(),
Qingsi Wang59844ce2018-11-01 04:45:53 +0000617 webrtc::CreateBuiltinVideoDecoderFactory(), nullptr,
Qingsi Wang7685e862018-06-11 20:15:46 -0700618 webrtc::AudioProcessingBuilder().Create());
619 pc_factory_dependencies.call_factory = webrtc::CreateCallFactory();
620 if (event_log_factory) {
621 event_log_factory_ = event_log_factory.get();
622 pc_factory_dependencies.event_log_factory = std::move(event_log_factory);
623 } else {
624 pc_factory_dependencies.event_log_factory =
625 webrtc::CreateRtcEventLogFactory();
626 }
Bjorn Mellem175aa2e2018-11-08 11:23:22 -0800627 if (media_transport_factory) {
628 pc_factory_dependencies.media_transport_factory =
629 std::move(media_transport_factory);
630 }
Qingsi Wang7685e862018-06-11 20:15:46 -0700631 peer_connection_factory_ = webrtc::CreateModularPeerConnectionFactory(
632 std::move(pc_factory_dependencies));
633
deadbeef1dcb1642017-03-29 21:08:16 -0700634 if (!peer_connection_factory_) {
635 return false;
636 }
637 if (options) {
638 peer_connection_factory_->SetOptions(*options);
639 }
Seth Hampson2f0d7022018-02-20 11:54:42 -0800640 if (config) {
641 sdp_semantics_ = config->sdp_semantics;
642 }
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700643
644 dependencies.allocator = std::move(port_allocator);
Niels Möllerf06f9232018-08-07 12:32:18 +0200645 peer_connection_ = CreatePeerConnection(config, std::move(dependencies));
deadbeef1dcb1642017-03-29 21:08:16 -0700646 return peer_connection_.get() != nullptr;
647 }
648
649 rtc::scoped_refptr<webrtc::PeerConnectionInterface> CreatePeerConnection(
deadbeef1dcb1642017-03-29 21:08:16 -0700650 const PeerConnectionInterface::RTCConfiguration* config,
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700651 webrtc::PeerConnectionDependencies dependencies) {
deadbeef1dcb1642017-03-29 21:08:16 -0700652 PeerConnectionInterface::RTCConfiguration modified_config;
653 // If |config| is null, this will result in a default configuration being
654 // used.
655 if (config) {
656 modified_config = *config;
657 }
658 // Disable resolution adaptation; we don't want it interfering with the
659 // test results.
660 // TODO(deadbeef): Do something more robust. Since we're testing for aspect
661 // ratios and not specific resolutions, is this even necessary?
662 modified_config.set_cpu_adaptation(false);
663
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700664 dependencies.observer = this;
deadbeef1dcb1642017-03-29 21:08:16 -0700665 return peer_connection_factory_->CreatePeerConnection(
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700666 modified_config, std::move(dependencies));
deadbeef1dcb1642017-03-29 21:08:16 -0700667 }
668
669 void set_signaling_message_receiver(
670 SignalingMessageReceiver* signaling_message_receiver) {
671 signaling_message_receiver_ = signaling_message_receiver;
672 }
673
674 void set_signaling_delay_ms(int delay_ms) { signaling_delay_ms_ = delay_ms; }
675
Steve Antonede9ca52017-10-16 13:04:27 -0700676 void set_signal_ice_candidates(bool signal) {
677 signal_ice_candidates_ = signal;
678 }
679
deadbeef1dcb1642017-03-29 21:08:16 -0700680 rtc::scoped_refptr<webrtc::VideoTrackInterface> CreateLocalVideoTrackInternal(
Niels Möller5c7efe72018-05-11 10:34:46 +0200681 webrtc::FakePeriodicVideoSource::Config config) {
deadbeef1dcb1642017-03-29 21:08:16 -0700682 // Set max frame rate to 10fps to reduce the risk of test flakiness.
683 // TODO(deadbeef): Do something more robust.
Niels Möller5c7efe72018-05-11 10:34:46 +0200684 config.frame_interval_ms = 100;
deadbeef1dcb1642017-03-29 21:08:16 -0700685
Niels Möller5c7efe72018-05-11 10:34:46 +0200686 video_track_sources_.emplace_back(
Niels Möller0f405822018-05-17 09:16:41 +0200687 new rtc::RefCountedObject<webrtc::FakePeriodicVideoTrackSource>(
688 config, false /* remote */));
deadbeef1dcb1642017-03-29 21:08:16 -0700689 rtc::scoped_refptr<webrtc::VideoTrackInterface> track(
Niels Möller5c7efe72018-05-11 10:34:46 +0200690 peer_connection_factory_->CreateVideoTrack(
691 rtc::CreateRandomUuid(), video_track_sources_.back()));
deadbeef1dcb1642017-03-29 21:08:16 -0700692 if (!local_video_renderer_) {
693 local_video_renderer_.reset(new webrtc::FakeVideoTrackRenderer(track));
694 }
695 return track;
696 }
697
698 void HandleIncomingOffer(const std::string& msg) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100699 RTC_LOG(LS_INFO) << debug_name_ << ": HandleIncomingOffer";
Steve Antona3a92c22017-12-07 10:27:41 -0800700 std::unique_ptr<SessionDescriptionInterface> desc =
701 webrtc::CreateSessionDescription(SdpType::kOffer, msg);
deadbeef1dcb1642017-03-29 21:08:16 -0700702 if (received_sdp_munger_) {
703 received_sdp_munger_(desc->description());
704 }
705
706 EXPECT_TRUE(SetRemoteDescription(std::move(desc)));
707 // Setting a remote description may have changed the number of receivers,
708 // so reset the receiver observers.
709 ResetRtpReceiverObservers();
Seth Hampson2f0d7022018-02-20 11:54:42 -0800710 if (remote_offer_handler_) {
711 remote_offer_handler_();
712 }
deadbeef1dcb1642017-03-29 21:08:16 -0700713 auto answer = CreateAnswer();
714 ASSERT_NE(nullptr, answer);
715 EXPECT_TRUE(SetLocalDescriptionAndSendSdpMessage(std::move(answer)));
716 }
717
718 void HandleIncomingAnswer(const std::string& msg) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100719 RTC_LOG(LS_INFO) << debug_name_ << ": HandleIncomingAnswer";
Steve Antona3a92c22017-12-07 10:27:41 -0800720 std::unique_ptr<SessionDescriptionInterface> desc =
721 webrtc::CreateSessionDescription(SdpType::kAnswer, msg);
deadbeef1dcb1642017-03-29 21:08:16 -0700722 if (received_sdp_munger_) {
723 received_sdp_munger_(desc->description());
724 }
725
726 EXPECT_TRUE(SetRemoteDescription(std::move(desc)));
727 // Set the RtpReceiverObserver after receivers are created.
728 ResetRtpReceiverObservers();
729 }
730
731 // Returns null on failure.
732 std::unique_ptr<SessionDescriptionInterface> CreateOffer() {
733 rtc::scoped_refptr<MockCreateSessionDescriptionObserver> observer(
734 new rtc::RefCountedObject<MockCreateSessionDescriptionObserver>());
735 pc()->CreateOffer(observer, offer_answer_options_);
736 return WaitForDescriptionFromObserver(observer);
737 }
738
739 // Returns null on failure.
740 std::unique_ptr<SessionDescriptionInterface> CreateAnswer() {
741 rtc::scoped_refptr<MockCreateSessionDescriptionObserver> observer(
742 new rtc::RefCountedObject<MockCreateSessionDescriptionObserver>());
743 pc()->CreateAnswer(observer, offer_answer_options_);
744 return WaitForDescriptionFromObserver(observer);
745 }
746
747 std::unique_ptr<SessionDescriptionInterface> WaitForDescriptionFromObserver(
Mirko Bonadeic61ce0d2017-11-21 17:04:20 +0100748 MockCreateSessionDescriptionObserver* observer) {
deadbeef1dcb1642017-03-29 21:08:16 -0700749 EXPECT_EQ_WAIT(true, observer->called(), kDefaultTimeout);
750 if (!observer->result()) {
751 return nullptr;
752 }
753 auto description = observer->MoveDescription();
754 if (generated_sdp_munger_) {
755 generated_sdp_munger_(description->description());
756 }
757 return description;
758 }
759
760 // Setting the local description and sending the SDP message over the fake
761 // signaling channel are combined into the same method because the SDP
762 // message needs to be sent as soon as SetLocalDescription finishes, without
763 // waiting for the observer to be called. This ensures that ICE candidates
764 // don't outrace the description.
765 bool SetLocalDescriptionAndSendSdpMessage(
766 std::unique_ptr<SessionDescriptionInterface> desc) {
767 rtc::scoped_refptr<MockSetSessionDescriptionObserver> observer(
768 new rtc::RefCountedObject<MockSetSessionDescriptionObserver>());
Mirko Bonadei675513b2017-11-09 11:09:25 +0100769 RTC_LOG(LS_INFO) << debug_name_ << ": SetLocalDescriptionAndSendSdpMessage";
Steve Antona3a92c22017-12-07 10:27:41 -0800770 SdpType type = desc->GetType();
deadbeef1dcb1642017-03-29 21:08:16 -0700771 std::string sdp;
772 EXPECT_TRUE(desc->ToString(&sdp));
773 pc()->SetLocalDescription(observer, desc.release());
Seth Hampson2f0d7022018-02-20 11:54:42 -0800774 if (sdp_semantics_ == SdpSemantics::kUnifiedPlan) {
775 RemoveUnusedVideoRenderers();
776 }
deadbeef1dcb1642017-03-29 21:08:16 -0700777 // As mentioned above, we need to send the message immediately after
778 // SetLocalDescription.
779 SendSdpMessage(type, sdp);
780 EXPECT_TRUE_WAIT(observer->called(), kDefaultTimeout);
781 return true;
782 }
783
784 bool SetRemoteDescription(std::unique_ptr<SessionDescriptionInterface> desc) {
785 rtc::scoped_refptr<MockSetSessionDescriptionObserver> observer(
786 new rtc::RefCountedObject<MockSetSessionDescriptionObserver>());
Mirko Bonadei675513b2017-11-09 11:09:25 +0100787 RTC_LOG(LS_INFO) << debug_name_ << ": SetRemoteDescription";
deadbeef1dcb1642017-03-29 21:08:16 -0700788 pc()->SetRemoteDescription(observer, desc.release());
Seth Hampson2f0d7022018-02-20 11:54:42 -0800789 if (sdp_semantics_ == SdpSemantics::kUnifiedPlan) {
790 RemoveUnusedVideoRenderers();
791 }
deadbeef1dcb1642017-03-29 21:08:16 -0700792 EXPECT_TRUE_WAIT(observer->called(), kDefaultTimeout);
793 return observer->result();
794 }
795
Seth Hampson2f0d7022018-02-20 11:54:42 -0800796 // This is a work around to remove unused fake_video_renderers from
797 // transceivers that have either stopped or are no longer receiving.
798 void RemoveUnusedVideoRenderers() {
799 auto transceivers = pc()->GetTransceivers();
800 for (auto& transceiver : transceivers) {
801 if (transceiver->receiver()->media_type() != cricket::MEDIA_TYPE_VIDEO) {
802 continue;
803 }
804 // Remove fake video renderers from any stopped transceivers.
805 if (transceiver->stopped()) {
806 auto it =
807 fake_video_renderers_.find(transceiver->receiver()->track()->id());
808 if (it != fake_video_renderers_.end()) {
809 fake_video_renderers_.erase(it);
810 }
811 }
812 // Remove fake video renderers from any transceivers that are no longer
813 // receiving.
814 if ((transceiver->current_direction() &&
815 !webrtc::RtpTransceiverDirectionHasRecv(
816 *transceiver->current_direction()))) {
817 auto it =
818 fake_video_renderers_.find(transceiver->receiver()->track()->id());
819 if (it != fake_video_renderers_.end()) {
820 fake_video_renderers_.erase(it);
821 }
822 }
823 }
824 }
825
deadbeef1dcb1642017-03-29 21:08:16 -0700826 // Simulate sending a blob of SDP with delay |signaling_delay_ms_| (0 by
827 // default).
Steve Antona3a92c22017-12-07 10:27:41 -0800828 void SendSdpMessage(SdpType type, const std::string& msg) {
deadbeef1dcb1642017-03-29 21:08:16 -0700829 if (signaling_delay_ms_ == 0) {
830 RelaySdpMessageIfReceiverExists(type, msg);
831 } else {
832 invoker_.AsyncInvokeDelayed<void>(
833 RTC_FROM_HERE, rtc::Thread::Current(),
834 rtc::Bind(&PeerConnectionWrapper::RelaySdpMessageIfReceiverExists,
835 this, type, msg),
836 signaling_delay_ms_);
837 }
838 }
839
Steve Antona3a92c22017-12-07 10:27:41 -0800840 void RelaySdpMessageIfReceiverExists(SdpType type, const std::string& msg) {
deadbeef1dcb1642017-03-29 21:08:16 -0700841 if (signaling_message_receiver_) {
842 signaling_message_receiver_->ReceiveSdpMessage(type, msg);
843 }
844 }
845
846 // Simulate trickling an ICE candidate with delay |signaling_delay_ms_| (0 by
847 // default).
848 void SendIceMessage(const std::string& sdp_mid,
849 int sdp_mline_index,
850 const std::string& msg) {
851 if (signaling_delay_ms_ == 0) {
852 RelayIceMessageIfReceiverExists(sdp_mid, sdp_mline_index, msg);
853 } else {
854 invoker_.AsyncInvokeDelayed<void>(
855 RTC_FROM_HERE, rtc::Thread::Current(),
856 rtc::Bind(&PeerConnectionWrapper::RelayIceMessageIfReceiverExists,
857 this, sdp_mid, sdp_mline_index, msg),
858 signaling_delay_ms_);
859 }
860 }
861
862 void RelayIceMessageIfReceiverExists(const std::string& sdp_mid,
863 int sdp_mline_index,
864 const std::string& msg) {
865 if (signaling_message_receiver_) {
866 signaling_message_receiver_->ReceiveIceMessage(sdp_mid, sdp_mline_index,
867 msg);
868 }
869 }
870
871 // SignalingMessageReceiver callbacks.
Steve Antona3a92c22017-12-07 10:27:41 -0800872 void ReceiveSdpMessage(SdpType type, const std::string& msg) override {
873 if (type == SdpType::kOffer) {
deadbeef1dcb1642017-03-29 21:08:16 -0700874 HandleIncomingOffer(msg);
875 } else {
876 HandleIncomingAnswer(msg);
877 }
878 }
879
880 void ReceiveIceMessage(const std::string& sdp_mid,
881 int sdp_mline_index,
882 const std::string& msg) override {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100883 RTC_LOG(LS_INFO) << debug_name_ << ": ReceiveIceMessage";
deadbeef1dcb1642017-03-29 21:08:16 -0700884 std::unique_ptr<webrtc::IceCandidateInterface> candidate(
885 webrtc::CreateIceCandidate(sdp_mid, sdp_mline_index, msg, nullptr));
886 EXPECT_TRUE(pc()->AddIceCandidate(candidate.get()));
887 }
888
889 // PeerConnectionObserver callbacks.
890 void OnSignalingChange(
891 webrtc::PeerConnectionInterface::SignalingState new_state) override {
892 EXPECT_EQ(pc()->signaling_state(), new_state);
893 }
Steve Anton15324772018-01-16 10:26:49 -0800894 void OnAddTrack(rtc::scoped_refptr<RtpReceiverInterface> receiver,
895 const std::vector<rtc::scoped_refptr<MediaStreamInterface>>&
896 streams) override {
897 if (receiver->media_type() == cricket::MEDIA_TYPE_VIDEO) {
898 rtc::scoped_refptr<VideoTrackInterface> video_track(
899 static_cast<VideoTrackInterface*>(receiver->track().get()));
900 ASSERT_TRUE(fake_video_renderers_.find(video_track->id()) ==
deadbeef1dcb1642017-03-29 21:08:16 -0700901 fake_video_renderers_.end());
Steve Anton15324772018-01-16 10:26:49 -0800902 fake_video_renderers_[video_track->id()] =
Karl Wiberg918f50c2018-07-05 11:40:33 +0200903 absl::make_unique<FakeVideoTrackRenderer>(video_track);
deadbeef1dcb1642017-03-29 21:08:16 -0700904 }
905 }
Steve Anton15324772018-01-16 10:26:49 -0800906 void OnRemoveTrack(
907 rtc::scoped_refptr<RtpReceiverInterface> receiver) override {
908 if (receiver->media_type() == cricket::MEDIA_TYPE_VIDEO) {
909 auto it = fake_video_renderers_.find(receiver->track()->id());
910 RTC_DCHECK(it != fake_video_renderers_.end());
911 fake_video_renderers_.erase(it);
912 }
913 }
deadbeef1dcb1642017-03-29 21:08:16 -0700914 void OnRenegotiationNeeded() override {}
915 void OnIceConnectionChange(
916 webrtc::PeerConnectionInterface::IceConnectionState new_state) override {
917 EXPECT_EQ(pc()->ice_connection_state(), new_state);
Steve Antonede9ca52017-10-16 13:04:27 -0700918 ice_connection_state_history_.push_back(new_state);
deadbeef1dcb1642017-03-29 21:08:16 -0700919 }
Jonas Olssonacd8ae72019-02-25 15:26:24 +0100920 void OnStandardizedIceConnectionChange(
921 webrtc::PeerConnectionInterface::IceConnectionState new_state) override {
922 standardized_ice_connection_state_history_.push_back(new_state);
923 }
Jonas Olsson635474e2018-10-18 15:58:17 +0200924 void OnConnectionChange(
925 webrtc::PeerConnectionInterface::PeerConnectionState new_state) override {
926 peer_connection_state_history_.push_back(new_state);
927 }
928
deadbeef1dcb1642017-03-29 21:08:16 -0700929 void OnIceGatheringChange(
930 webrtc::PeerConnectionInterface::IceGatheringState new_state) override {
deadbeef1dcb1642017-03-29 21:08:16 -0700931 EXPECT_EQ(pc()->ice_gathering_state(), new_state);
Steve Antonede9ca52017-10-16 13:04:27 -0700932 ice_gathering_state_history_.push_back(new_state);
deadbeef1dcb1642017-03-29 21:08:16 -0700933 }
934 void OnIceCandidate(const webrtc::IceCandidateInterface* candidate) override {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100935 RTC_LOG(LS_INFO) << debug_name_ << ": OnIceCandidate";
deadbeef1dcb1642017-03-29 21:08:16 -0700936
Qingsi Wang1dac6d82018-12-12 15:28:47 -0800937 if (remote_async_resolver_) {
938 const auto& local_candidate = candidate->candidate();
Qingsi Wang1dac6d82018-12-12 15:28:47 -0800939 if (local_candidate.address().IsUnresolvedIP()) {
940 RTC_DCHECK(local_candidate.type() == cricket::LOCAL_PORT_TYPE);
941 rtc::SocketAddress resolved_addr(local_candidate.address());
Qingsi Wangecd30542019-05-22 14:34:56 -0700942 const auto resolved_ip = mdns_responder_->GetMappedAddressForName(
Qingsi Wang1dac6d82018-12-12 15:28:47 -0800943 local_candidate.address().hostname());
944 RTC_DCHECK(!resolved_ip.IsNil());
945 resolved_addr.SetResolvedIP(resolved_ip);
946 EXPECT_CALL(*remote_async_resolver_, GetResolvedAddress(_, _))
947 .WillOnce(DoAll(SetArgPointee<1>(resolved_addr), Return(true)));
948 EXPECT_CALL(*remote_async_resolver_, Destroy(_));
Zach Stein6fcdc2f2018-08-23 16:25:55 -0700949 }
Zach Stein6fcdc2f2018-08-23 16:25:55 -0700950 }
951
deadbeef1dcb1642017-03-29 21:08:16 -0700952 std::string ice_sdp;
Qingsi Wang1dac6d82018-12-12 15:28:47 -0800953 EXPECT_TRUE(candidate->ToString(&ice_sdp));
Steve Antonede9ca52017-10-16 13:04:27 -0700954 if (signaling_message_receiver_ == nullptr || !signal_ice_candidates_) {
deadbeef1dcb1642017-03-29 21:08:16 -0700955 // Remote party may be deleted.
956 return;
957 }
Qingsi Wang1dac6d82018-12-12 15:28:47 -0800958 SendIceMessage(candidate->sdp_mid(), candidate->sdp_mline_index(), ice_sdp);
Qingsi Wangc129c352019-04-18 10:41:58 -0700959 last_candidate_gathered_ = candidate->candidate();
deadbeef1dcb1642017-03-29 21:08:16 -0700960 }
961 void OnDataChannel(
962 rtc::scoped_refptr<DataChannelInterface> data_channel) override {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100963 RTC_LOG(LS_INFO) << debug_name_ << ": OnDataChannel";
deadbeef1dcb1642017-03-29 21:08:16 -0700964 data_channel_ = data_channel;
965 data_observer_.reset(new MockDataChannelObserver(data_channel));
966 }
967
deadbeef1dcb1642017-03-29 21:08:16 -0700968 std::string debug_name_;
969
970 std::unique_ptr<rtc::FakeNetworkManager> fake_network_manager_;
Qingsi Wangecd30542019-05-22 14:34:56 -0700971 // Reference to the mDNS responder owned by |fake_network_manager_| after set.
972 webrtc::FakeMdnsResponder* mdns_responder_ = nullptr;
deadbeef1dcb1642017-03-29 21:08:16 -0700973
974 rtc::scoped_refptr<webrtc::PeerConnectionInterface> peer_connection_;
975 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface>
976 peer_connection_factory_;
977
Steve Antonede9ca52017-10-16 13:04:27 -0700978 cricket::PortAllocator* port_allocator_;
deadbeef1dcb1642017-03-29 21:08:16 -0700979 // Needed to keep track of number of frames sent.
980 rtc::scoped_refptr<FakeAudioCaptureModule> fake_audio_capture_module_;
981 // Needed to keep track of number of frames received.
982 std::map<std::string, std::unique_ptr<webrtc::FakeVideoTrackRenderer>>
983 fake_video_renderers_;
984 // Needed to ensure frames aren't received for removed tracks.
985 std::vector<std::unique_ptr<webrtc::FakeVideoTrackRenderer>>
986 removed_fake_video_renderers_;
deadbeef1dcb1642017-03-29 21:08:16 -0700987
988 // For remote peer communication.
989 SignalingMessageReceiver* signaling_message_receiver_ = nullptr;
990 int signaling_delay_ms_ = 0;
Steve Antonede9ca52017-10-16 13:04:27 -0700991 bool signal_ice_candidates_ = true;
Qingsi Wangc129c352019-04-18 10:41:58 -0700992 cricket::Candidate last_candidate_gathered_;
deadbeef1dcb1642017-03-29 21:08:16 -0700993
Niels Möller5c7efe72018-05-11 10:34:46 +0200994 // Store references to the video sources we've created, so that we can stop
deadbeef1dcb1642017-03-29 21:08:16 -0700995 // them, if required.
Niels Möller5c7efe72018-05-11 10:34:46 +0200996 std::vector<rtc::scoped_refptr<webrtc::VideoTrackSource>>
997 video_track_sources_;
deadbeef1dcb1642017-03-29 21:08:16 -0700998 // |local_video_renderer_| attached to the first created local video track.
999 std::unique_ptr<webrtc::FakeVideoTrackRenderer> local_video_renderer_;
1000
Seth Hampson2f0d7022018-02-20 11:54:42 -08001001 SdpSemantics sdp_semantics_;
deadbeef1dcb1642017-03-29 21:08:16 -07001002 PeerConnectionInterface::RTCOfferAnswerOptions offer_answer_options_;
1003 std::function<void(cricket::SessionDescription*)> received_sdp_munger_;
1004 std::function<void(cricket::SessionDescription*)> generated_sdp_munger_;
Seth Hampson2f0d7022018-02-20 11:54:42 -08001005 std::function<void()> remote_offer_handler_;
Qingsi Wang1dac6d82018-12-12 15:28:47 -08001006 rtc::MockAsyncResolver* remote_async_resolver_ = nullptr;
deadbeef1dcb1642017-03-29 21:08:16 -07001007 rtc::scoped_refptr<DataChannelInterface> data_channel_;
1008 std::unique_ptr<MockDataChannelObserver> data_observer_;
1009
1010 std::vector<std::unique_ptr<MockRtpReceiverObserver>> rtp_receiver_observers_;
1011
Steve Antonede9ca52017-10-16 13:04:27 -07001012 std::vector<PeerConnectionInterface::IceConnectionState>
1013 ice_connection_state_history_;
Jonas Olssonacd8ae72019-02-25 15:26:24 +01001014 std::vector<PeerConnectionInterface::IceConnectionState>
1015 standardized_ice_connection_state_history_;
Jonas Olsson635474e2018-10-18 15:58:17 +02001016 std::vector<PeerConnectionInterface::PeerConnectionState>
1017 peer_connection_state_history_;
Steve Antonede9ca52017-10-16 13:04:27 -07001018 std::vector<PeerConnectionInterface::IceGatheringState>
1019 ice_gathering_state_history_;
deadbeef1dcb1642017-03-29 21:08:16 -07001020
Qingsi Wang7685e862018-06-11 20:15:46 -07001021 webrtc::FakeRtcEventLogFactory* event_log_factory_;
1022
deadbeef1dcb1642017-03-29 21:08:16 -07001023 rtc::AsyncInvoker invoker_;
1024
Seth Hampson2f0d7022018-02-20 11:54:42 -08001025 friend class PeerConnectionIntegrationBaseTest;
deadbeef1dcb1642017-03-29 21:08:16 -07001026};
1027
Elad Alon99c3fe52017-10-13 16:29:40 +02001028class MockRtcEventLogOutput : public webrtc::RtcEventLogOutput {
1029 public:
1030 virtual ~MockRtcEventLogOutput() = default;
1031 MOCK_CONST_METHOD0(IsActive, bool());
1032 MOCK_METHOD1(Write, bool(const std::string&));
1033};
1034
Seth Hampson2f0d7022018-02-20 11:54:42 -08001035// This helper object is used for both specifying how many audio/video frames
1036// are expected to be received for a caller/callee. It provides helper functions
1037// to specify these expectations. The object initially starts in a state of no
1038// expectations.
1039class MediaExpectations {
1040 public:
1041 enum ExpectFrames {
1042 kExpectSomeFrames,
1043 kExpectNoFrames,
1044 kNoExpectation,
1045 };
1046
1047 void ExpectBidirectionalAudioAndVideo() {
1048 ExpectBidirectionalAudio();
1049 ExpectBidirectionalVideo();
1050 }
1051
1052 void ExpectBidirectionalAudio() {
1053 CallerExpectsSomeAudio();
1054 CalleeExpectsSomeAudio();
1055 }
1056
1057 void ExpectNoAudio() {
1058 CallerExpectsNoAudio();
1059 CalleeExpectsNoAudio();
1060 }
1061
1062 void ExpectBidirectionalVideo() {
1063 CallerExpectsSomeVideo();
1064 CalleeExpectsSomeVideo();
1065 }
1066
1067 void ExpectNoVideo() {
1068 CallerExpectsNoVideo();
1069 CalleeExpectsNoVideo();
1070 }
1071
1072 void CallerExpectsSomeAudioAndVideo() {
1073 CallerExpectsSomeAudio();
1074 CallerExpectsSomeVideo();
1075 }
1076
1077 void CalleeExpectsSomeAudioAndVideo() {
1078 CalleeExpectsSomeAudio();
1079 CalleeExpectsSomeVideo();
1080 }
1081
1082 // Caller's audio functions.
1083 void CallerExpectsSomeAudio(
1084 int expected_audio_frames = kDefaultExpectedAudioFrameCount) {
1085 caller_audio_expectation_ = kExpectSomeFrames;
1086 caller_audio_frames_expected_ = expected_audio_frames;
1087 }
1088
1089 void CallerExpectsNoAudio() {
1090 caller_audio_expectation_ = kExpectNoFrames;
1091 caller_audio_frames_expected_ = 0;
1092 }
1093
1094 // Caller's video functions.
1095 void CallerExpectsSomeVideo(
1096 int expected_video_frames = kDefaultExpectedVideoFrameCount) {
1097 caller_video_expectation_ = kExpectSomeFrames;
1098 caller_video_frames_expected_ = expected_video_frames;
1099 }
1100
1101 void CallerExpectsNoVideo() {
1102 caller_video_expectation_ = kExpectNoFrames;
1103 caller_video_frames_expected_ = 0;
1104 }
1105
1106 // Callee's audio functions.
1107 void CalleeExpectsSomeAudio(
1108 int expected_audio_frames = kDefaultExpectedAudioFrameCount) {
1109 callee_audio_expectation_ = kExpectSomeFrames;
1110 callee_audio_frames_expected_ = expected_audio_frames;
1111 }
1112
1113 void CalleeExpectsNoAudio() {
1114 callee_audio_expectation_ = kExpectNoFrames;
1115 callee_audio_frames_expected_ = 0;
1116 }
1117
1118 // Callee's video functions.
1119 void CalleeExpectsSomeVideo(
1120 int expected_video_frames = kDefaultExpectedVideoFrameCount) {
1121 callee_video_expectation_ = kExpectSomeFrames;
1122 callee_video_frames_expected_ = expected_video_frames;
1123 }
1124
1125 void CalleeExpectsNoVideo() {
1126 callee_video_expectation_ = kExpectNoFrames;
1127 callee_video_frames_expected_ = 0;
1128 }
1129
1130 ExpectFrames caller_audio_expectation_ = kNoExpectation;
1131 ExpectFrames caller_video_expectation_ = kNoExpectation;
1132 ExpectFrames callee_audio_expectation_ = kNoExpectation;
1133 ExpectFrames callee_video_expectation_ = kNoExpectation;
1134 int caller_audio_frames_expected_ = 0;
1135 int caller_video_frames_expected_ = 0;
1136 int callee_audio_frames_expected_ = 0;
1137 int callee_video_frames_expected_ = 0;
1138};
1139
deadbeef1dcb1642017-03-29 21:08:16 -07001140// Tests two PeerConnections connecting to each other end-to-end, using a
1141// virtual network, fake A/V capture and fake encoder/decoders. The
1142// PeerConnections share the threads/socket servers, but use separate versions
1143// of everything else (including "PeerConnectionFactory"s).
Mirko Bonadei6a489f22019-04-09 15:11:12 +02001144class PeerConnectionIntegrationBaseTest : public ::testing::Test {
deadbeef1dcb1642017-03-29 21:08:16 -07001145 public:
Seth Hampson2f0d7022018-02-20 11:54:42 -08001146 explicit PeerConnectionIntegrationBaseTest(SdpSemantics sdp_semantics)
1147 : sdp_semantics_(sdp_semantics),
1148 ss_(new rtc::VirtualSocketServer()),
Steve Antonede9ca52017-10-16 13:04:27 -07001149 fss_(new rtc::FirewallSocketServer(ss_.get())),
1150 network_thread_(new rtc::Thread(fss_.get())),
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001151 worker_thread_(rtc::Thread::Create()),
1152 loopback_media_transports_(network_thread_.get()) {
Sebastian Jansson8a793a02018-03-13 15:21:48 +01001153 network_thread_->SetName("PCNetworkThread", this);
1154 worker_thread_->SetName("PCWorkerThread", this);
deadbeef1dcb1642017-03-29 21:08:16 -07001155 RTC_CHECK(network_thread_->Start());
1156 RTC_CHECK(worker_thread_->Start());
Qingsi Wang7fc821d2018-07-12 12:54:53 -07001157 webrtc::metrics::Reset();
deadbeef1dcb1642017-03-29 21:08:16 -07001158 }
1159
Seth Hampson2f0d7022018-02-20 11:54:42 -08001160 ~PeerConnectionIntegrationBaseTest() {
Seth Hampsonaed71642018-06-11 07:41:32 -07001161 // The PeerConnections should deleted before the TurnCustomizers.
1162 // A TurnPort is created with a raw pointer to a TurnCustomizer. The
1163 // TurnPort has the same lifetime as the PeerConnection, so it's expected
1164 // that the TurnCustomizer outlives the life of the PeerConnection or else
1165 // when Send() is called it will hit a seg fault.
deadbeef1dcb1642017-03-29 21:08:16 -07001166 if (caller_) {
1167 caller_->set_signaling_message_receiver(nullptr);
Seth Hampsonaed71642018-06-11 07:41:32 -07001168 delete SetCallerPcWrapperAndReturnCurrent(nullptr);
deadbeef1dcb1642017-03-29 21:08:16 -07001169 }
1170 if (callee_) {
1171 callee_->set_signaling_message_receiver(nullptr);
Seth Hampsonaed71642018-06-11 07:41:32 -07001172 delete SetCalleePcWrapperAndReturnCurrent(nullptr);
deadbeef1dcb1642017-03-29 21:08:16 -07001173 }
Seth Hampsonaed71642018-06-11 07:41:32 -07001174
1175 // If turn servers were created for the test they need to be destroyed on
1176 // the network thread.
1177 network_thread()->Invoke<void>(RTC_FROM_HERE, [this] {
1178 turn_servers_.clear();
1179 turn_customizers_.clear();
1180 });
deadbeef1dcb1642017-03-29 21:08:16 -07001181 }
1182
1183 bool SignalingStateStable() {
1184 return caller_->SignalingStateStable() && callee_->SignalingStateStable();
1185 }
1186
deadbeef71452802017-05-07 17:21:01 -07001187 bool DtlsConnected() {
Alex Loiko9289eda2018-11-23 16:18:59 +00001188 // TODO(deadbeef): kIceConnectionConnected currently means both ICE and DTLS
1189 // are connected. This is an important distinction. Once we have separate
1190 // ICE and DTLS state, this check needs to use the DTLS state.
1191 return (callee()->ice_connection_state() ==
1192 webrtc::PeerConnectionInterface::kIceConnectionConnected ||
1193 callee()->ice_connection_state() ==
1194 webrtc::PeerConnectionInterface::kIceConnectionCompleted) &&
1195 (caller()->ice_connection_state() ==
1196 webrtc::PeerConnectionInterface::kIceConnectionConnected ||
1197 caller()->ice_connection_state() ==
1198 webrtc::PeerConnectionInterface::kIceConnectionCompleted);
deadbeef71452802017-05-07 17:21:01 -07001199 }
1200
Qingsi Wang7685e862018-06-11 20:15:46 -07001201 // When |event_log_factory| is null, the default implementation of the event
1202 // log factory will be used.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001203 std::unique_ptr<PeerConnectionWrapper> CreatePeerConnectionWrapper(
1204 const std::string& debug_name,
Seth Hampson2f0d7022018-02-20 11:54:42 -08001205 const PeerConnectionFactory::Options* options,
1206 const RTCConfiguration* config,
Qingsi Wang7685e862018-06-11 20:15:46 -07001207 webrtc::PeerConnectionDependencies dependencies,
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001208 std::unique_ptr<webrtc::FakeRtcEventLogFactory> event_log_factory,
1209 std::unique_ptr<webrtc::MediaTransportFactory> media_transport_factory) {
Seth Hampson2f0d7022018-02-20 11:54:42 -08001210 RTCConfiguration modified_config;
1211 if (config) {
1212 modified_config = *config;
1213 }
Steve Anton3acffc32018-04-12 17:21:03 -07001214 modified_config.sdp_semantics = sdp_semantics_;
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001215 if (!dependencies.cert_generator) {
1216 dependencies.cert_generator =
Karl Wiberg918f50c2018-07-05 11:40:33 +02001217 absl::make_unique<FakeRTCCertificateGenerator>();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001218 }
1219 std::unique_ptr<PeerConnectionWrapper> client(
1220 new PeerConnectionWrapper(debug_name));
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001221
Niels Möllerf06f9232018-08-07 12:32:18 +02001222 if (!client->Init(options, &modified_config, std::move(dependencies),
1223 network_thread_.get(), worker_thread_.get(),
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001224 std::move(event_log_factory),
1225 std::move(media_transport_factory))) {
Seth Hampson2f0d7022018-02-20 11:54:42 -08001226 return nullptr;
1227 }
1228 return client;
1229 }
1230
Qingsi Wang7685e862018-06-11 20:15:46 -07001231 std::unique_ptr<PeerConnectionWrapper>
1232 CreatePeerConnectionWrapperWithFakeRtcEventLog(
1233 const std::string& debug_name,
Qingsi Wang7685e862018-06-11 20:15:46 -07001234 const PeerConnectionFactory::Options* options,
1235 const RTCConfiguration* config,
1236 webrtc::PeerConnectionDependencies dependencies) {
1237 std::unique_ptr<webrtc::FakeRtcEventLogFactory> event_log_factory(
1238 new webrtc::FakeRtcEventLogFactory(rtc::Thread::Current()));
Niels Möllerf06f9232018-08-07 12:32:18 +02001239 return CreatePeerConnectionWrapper(debug_name, options, config,
Qingsi Wang7685e862018-06-11 20:15:46 -07001240 std::move(dependencies),
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001241 std::move(event_log_factory),
1242 /*media_transport_factory=*/nullptr);
Qingsi Wang7685e862018-06-11 20:15:46 -07001243 }
1244
deadbeef1dcb1642017-03-29 21:08:16 -07001245 bool CreatePeerConnectionWrappers() {
1246 return CreatePeerConnectionWrappersWithConfig(
1247 PeerConnectionInterface::RTCConfiguration(),
1248 PeerConnectionInterface::RTCConfiguration());
1249 }
1250
Steve Anton3acffc32018-04-12 17:21:03 -07001251 bool CreatePeerConnectionWrappersWithSdpSemantics(
1252 SdpSemantics caller_semantics,
1253 SdpSemantics callee_semantics) {
1254 // Can't specify the sdp_semantics in the passed-in configuration since it
1255 // will be overwritten by CreatePeerConnectionWrapper with whatever is
1256 // stored in sdp_semantics_. So get around this by modifying the instance
1257 // variable before calling CreatePeerConnectionWrapper for the caller and
1258 // callee PeerConnections.
1259 SdpSemantics original_semantics = sdp_semantics_;
1260 sdp_semantics_ = caller_semantics;
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001261 caller_ = CreatePeerConnectionWrapper(
Niels Möllerf06f9232018-08-07 12:32:18 +02001262 "Caller", nullptr, nullptr, webrtc::PeerConnectionDependencies(nullptr),
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001263 nullptr, /*media_transport_factory=*/nullptr);
Steve Anton3acffc32018-04-12 17:21:03 -07001264 sdp_semantics_ = callee_semantics;
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001265 callee_ = CreatePeerConnectionWrapper(
Niels Möllerf06f9232018-08-07 12:32:18 +02001266 "Callee", nullptr, nullptr, webrtc::PeerConnectionDependencies(nullptr),
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001267 nullptr, /*media_transport_factory=*/nullptr);
Steve Anton3acffc32018-04-12 17:21:03 -07001268 sdp_semantics_ = original_semantics;
1269 return caller_ && callee_;
1270 }
1271
deadbeef1dcb1642017-03-29 21:08:16 -07001272 bool CreatePeerConnectionWrappersWithConfig(
1273 const PeerConnectionInterface::RTCConfiguration& caller_config,
1274 const PeerConnectionInterface::RTCConfiguration& callee_config) {
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001275 caller_ = CreatePeerConnectionWrapper(
Niels Möllerf06f9232018-08-07 12:32:18 +02001276 "Caller", nullptr, &caller_config,
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001277 webrtc::PeerConnectionDependencies(nullptr), nullptr,
1278 /*media_transport_factory=*/nullptr);
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001279 callee_ = CreatePeerConnectionWrapper(
Niels Möllerf06f9232018-08-07 12:32:18 +02001280 "Callee", nullptr, &callee_config,
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001281 webrtc::PeerConnectionDependencies(nullptr), nullptr,
1282 /*media_transport_factory=*/nullptr);
1283 return caller_ && callee_;
1284 }
1285
1286 bool CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
1287 const PeerConnectionInterface::RTCConfiguration& caller_config,
1288 const PeerConnectionInterface::RTCConfiguration& callee_config,
1289 std::unique_ptr<webrtc::MediaTransportFactory> caller_factory,
1290 std::unique_ptr<webrtc::MediaTransportFactory> callee_factory) {
1291 caller_ =
1292 CreatePeerConnectionWrapper("Caller", nullptr, &caller_config,
1293 webrtc::PeerConnectionDependencies(nullptr),
1294 nullptr, std::move(caller_factory));
1295 callee_ =
1296 CreatePeerConnectionWrapper("Callee", nullptr, &callee_config,
1297 webrtc::PeerConnectionDependencies(nullptr),
1298 nullptr, std::move(callee_factory));
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001299 return caller_ && callee_;
1300 }
1301
1302 bool CreatePeerConnectionWrappersWithConfigAndDeps(
1303 const PeerConnectionInterface::RTCConfiguration& caller_config,
1304 webrtc::PeerConnectionDependencies caller_dependencies,
1305 const PeerConnectionInterface::RTCConfiguration& callee_config,
1306 webrtc::PeerConnectionDependencies callee_dependencies) {
1307 caller_ =
Niels Möllerf06f9232018-08-07 12:32:18 +02001308 CreatePeerConnectionWrapper("Caller", nullptr, &caller_config,
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001309 std::move(caller_dependencies), nullptr,
1310 /*media_transport_factory=*/nullptr);
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001311 callee_ =
Niels Möllerf06f9232018-08-07 12:32:18 +02001312 CreatePeerConnectionWrapper("Callee", nullptr, &callee_config,
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001313 std::move(callee_dependencies), nullptr,
1314 /*media_transport_factory=*/nullptr);
deadbeef1dcb1642017-03-29 21:08:16 -07001315 return caller_ && callee_;
1316 }
1317
1318 bool CreatePeerConnectionWrappersWithOptions(
1319 const PeerConnectionFactory::Options& caller_options,
1320 const PeerConnectionFactory::Options& callee_options) {
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001321 caller_ = CreatePeerConnectionWrapper(
Niels Möllerf06f9232018-08-07 12:32:18 +02001322 "Caller", &caller_options, nullptr,
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001323 webrtc::PeerConnectionDependencies(nullptr), nullptr,
1324 /*media_transport_factory=*/nullptr);
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001325 callee_ = CreatePeerConnectionWrapper(
Niels Möllerf06f9232018-08-07 12:32:18 +02001326 "Callee", &callee_options, nullptr,
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001327 webrtc::PeerConnectionDependencies(nullptr), nullptr,
1328 /*media_transport_factory=*/nullptr);
Qingsi Wang7685e862018-06-11 20:15:46 -07001329 return caller_ && callee_;
1330 }
1331
1332 bool CreatePeerConnectionWrappersWithFakeRtcEventLog() {
1333 PeerConnectionInterface::RTCConfiguration default_config;
1334 caller_ = CreatePeerConnectionWrapperWithFakeRtcEventLog(
Niels Möllerf06f9232018-08-07 12:32:18 +02001335 "Caller", nullptr, &default_config,
Qingsi Wang7685e862018-06-11 20:15:46 -07001336 webrtc::PeerConnectionDependencies(nullptr));
1337 callee_ = CreatePeerConnectionWrapperWithFakeRtcEventLog(
Niels Möllerf06f9232018-08-07 12:32:18 +02001338 "Callee", nullptr, &default_config,
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001339 webrtc::PeerConnectionDependencies(nullptr));
deadbeef1dcb1642017-03-29 21:08:16 -07001340 return caller_ && callee_;
1341 }
1342
Seth Hampson2f0d7022018-02-20 11:54:42 -08001343 std::unique_ptr<PeerConnectionWrapper>
1344 CreatePeerConnectionWrapperWithAlternateKey() {
deadbeef1dcb1642017-03-29 21:08:16 -07001345 std::unique_ptr<FakeRTCCertificateGenerator> cert_generator(
1346 new FakeRTCCertificateGenerator());
1347 cert_generator->use_alternate_key();
1348
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001349 webrtc::PeerConnectionDependencies dependencies(nullptr);
1350 dependencies.cert_generator = std::move(cert_generator);
Niels Möllerf06f9232018-08-07 12:32:18 +02001351 return CreatePeerConnectionWrapper("New Peer", nullptr, nullptr,
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001352 std::move(dependencies), nullptr,
1353 /*media_transport_factory=*/nullptr);
deadbeef1dcb1642017-03-29 21:08:16 -07001354 }
1355
Seth Hampsonaed71642018-06-11 07:41:32 -07001356 cricket::TestTurnServer* CreateTurnServer(
1357 rtc::SocketAddress internal_address,
1358 rtc::SocketAddress external_address,
1359 cricket::ProtocolType type = cricket::ProtocolType::PROTO_UDP,
1360 const std::string& common_name = "test turn server") {
1361 rtc::Thread* thread = network_thread();
1362 std::unique_ptr<cricket::TestTurnServer> turn_server =
1363 network_thread()->Invoke<std::unique_ptr<cricket::TestTurnServer>>(
1364 RTC_FROM_HERE,
1365 [thread, internal_address, external_address, type, common_name] {
Karl Wiberg918f50c2018-07-05 11:40:33 +02001366 return absl::make_unique<cricket::TestTurnServer>(
Seth Hampsonaed71642018-06-11 07:41:32 -07001367 thread, internal_address, external_address, type,
1368 /*ignore_bad_certs=*/true, common_name);
1369 });
1370 turn_servers_.push_back(std::move(turn_server));
1371 // Interactions with the turn server should be done on the network thread.
1372 return turn_servers_.back().get();
1373 }
1374
1375 cricket::TestTurnCustomizer* CreateTurnCustomizer() {
1376 std::unique_ptr<cricket::TestTurnCustomizer> turn_customizer =
1377 network_thread()->Invoke<std::unique_ptr<cricket::TestTurnCustomizer>>(
1378 RTC_FROM_HERE,
Karl Wiberg918f50c2018-07-05 11:40:33 +02001379 [] { return absl::make_unique<cricket::TestTurnCustomizer>(); });
Seth Hampsonaed71642018-06-11 07:41:32 -07001380 turn_customizers_.push_back(std::move(turn_customizer));
1381 // Interactions with the turn customizer should be done on the network
1382 // thread.
1383 return turn_customizers_.back().get();
1384 }
1385
1386 // Checks that the function counters for a TestTurnCustomizer are greater than
1387 // 0.
1388 void ExpectTurnCustomizerCountersIncremented(
1389 cricket::TestTurnCustomizer* turn_customizer) {
1390 unsigned int allow_channel_data_counter =
1391 network_thread()->Invoke<unsigned int>(
1392 RTC_FROM_HERE, [turn_customizer] {
1393 return turn_customizer->allow_channel_data_cnt_;
1394 });
1395 EXPECT_GT(allow_channel_data_counter, 0u);
1396 unsigned int modify_counter = network_thread()->Invoke<unsigned int>(
1397 RTC_FROM_HERE,
1398 [turn_customizer] { return turn_customizer->modify_cnt_; });
1399 EXPECT_GT(modify_counter, 0u);
1400 }
1401
deadbeef1dcb1642017-03-29 21:08:16 -07001402 // Once called, SDP blobs and ICE candidates will be automatically signaled
1403 // between PeerConnections.
1404 void ConnectFakeSignaling() {
1405 caller_->set_signaling_message_receiver(callee_.get());
1406 callee_->set_signaling_message_receiver(caller_.get());
1407 }
1408
Steve Antonede9ca52017-10-16 13:04:27 -07001409 // Once called, SDP blobs will be automatically signaled between
1410 // PeerConnections. Note that ICE candidates will not be signaled unless they
1411 // are in the exchanged SDP blobs.
1412 void ConnectFakeSignalingForSdpOnly() {
1413 ConnectFakeSignaling();
1414 SetSignalIceCandidates(false);
1415 }
1416
deadbeef1dcb1642017-03-29 21:08:16 -07001417 void SetSignalingDelayMs(int delay_ms) {
1418 caller_->set_signaling_delay_ms(delay_ms);
1419 callee_->set_signaling_delay_ms(delay_ms);
1420 }
1421
Steve Antonede9ca52017-10-16 13:04:27 -07001422 void SetSignalIceCandidates(bool signal) {
1423 caller_->set_signal_ice_candidates(signal);
1424 callee_->set_signal_ice_candidates(signal);
1425 }
1426
deadbeef1dcb1642017-03-29 21:08:16 -07001427 // Messages may get lost on the unreliable DataChannel, so we send multiple
1428 // times to avoid test flakiness.
1429 void SendRtpDataWithRetries(webrtc::DataChannelInterface* dc,
1430 const std::string& data,
1431 int retries) {
1432 for (int i = 0; i < retries; ++i) {
1433 dc->Send(DataBuffer(data));
1434 }
1435 }
1436
1437 rtc::Thread* network_thread() { return network_thread_.get(); }
1438
1439 rtc::VirtualSocketServer* virtual_socket_server() { return ss_.get(); }
1440
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001441 webrtc::MediaTransportPair* loopback_media_transports() {
1442 return &loopback_media_transports_;
1443 }
1444
deadbeef1dcb1642017-03-29 21:08:16 -07001445 PeerConnectionWrapper* caller() { return caller_.get(); }
1446
1447 // Set the |caller_| to the |wrapper| passed in and return the
1448 // original |caller_|.
1449 PeerConnectionWrapper* SetCallerPcWrapperAndReturnCurrent(
1450 PeerConnectionWrapper* wrapper) {
1451 PeerConnectionWrapper* old = caller_.release();
1452 caller_.reset(wrapper);
1453 return old;
1454 }
1455
1456 PeerConnectionWrapper* callee() { return callee_.get(); }
1457
1458 // Set the |callee_| to the |wrapper| passed in and return the
1459 // original |callee_|.
1460 PeerConnectionWrapper* SetCalleePcWrapperAndReturnCurrent(
1461 PeerConnectionWrapper* wrapper) {
1462 PeerConnectionWrapper* old = callee_.release();
1463 callee_.reset(wrapper);
1464 return old;
1465 }
1466
Qingsi Wang1dac6d82018-12-12 15:28:47 -08001467 void SetPortAllocatorFlags(uint32_t caller_flags, uint32_t callee_flags) {
1468 network_thread()->Invoke<void>(
1469 RTC_FROM_HERE, rtc::Bind(&cricket::PortAllocator::set_flags,
1470 caller()->port_allocator(), caller_flags));
1471 network_thread()->Invoke<void>(
1472 RTC_FROM_HERE, rtc::Bind(&cricket::PortAllocator::set_flags,
1473 callee()->port_allocator(), callee_flags));
1474 }
1475
Steve Antonede9ca52017-10-16 13:04:27 -07001476 rtc::FirewallSocketServer* firewall() const { return fss_.get(); }
1477
Seth Hampson2f0d7022018-02-20 11:54:42 -08001478 // Expects the provided number of new frames to be received within
1479 // kMaxWaitForFramesMs. The new expected frames are specified in
1480 // |media_expectations|. Returns false if any of the expectations were
1481 // not met.
1482 bool ExpectNewFrames(const MediaExpectations& media_expectations) {
1483 // First initialize the expected frame counts based upon the current
1484 // frame count.
1485 int total_caller_audio_frames_expected = caller()->audio_frames_received();
1486 if (media_expectations.caller_audio_expectation_ ==
1487 MediaExpectations::kExpectSomeFrames) {
1488 total_caller_audio_frames_expected +=
1489 media_expectations.caller_audio_frames_expected_;
1490 }
1491 int total_caller_video_frames_expected =
deadbeef1dcb1642017-03-29 21:08:16 -07001492 caller()->min_video_frames_received_per_track();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001493 if (media_expectations.caller_video_expectation_ ==
1494 MediaExpectations::kExpectSomeFrames) {
1495 total_caller_video_frames_expected +=
1496 media_expectations.caller_video_frames_expected_;
1497 }
1498 int total_callee_audio_frames_expected = callee()->audio_frames_received();
1499 if (media_expectations.callee_audio_expectation_ ==
1500 MediaExpectations::kExpectSomeFrames) {
1501 total_callee_audio_frames_expected +=
1502 media_expectations.callee_audio_frames_expected_;
1503 }
1504 int total_callee_video_frames_expected =
deadbeef1dcb1642017-03-29 21:08:16 -07001505 callee()->min_video_frames_received_per_track();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001506 if (media_expectations.callee_video_expectation_ ==
1507 MediaExpectations::kExpectSomeFrames) {
1508 total_callee_video_frames_expected +=
1509 media_expectations.callee_video_frames_expected_;
1510 }
deadbeef1dcb1642017-03-29 21:08:16 -07001511
Seth Hampson2f0d7022018-02-20 11:54:42 -08001512 // Wait for the expected frames.
deadbeef1dcb1642017-03-29 21:08:16 -07001513 EXPECT_TRUE_WAIT(caller()->audio_frames_received() >=
Seth Hampson2f0d7022018-02-20 11:54:42 -08001514 total_caller_audio_frames_expected &&
deadbeef1dcb1642017-03-29 21:08:16 -07001515 caller()->min_video_frames_received_per_track() >=
Seth Hampson2f0d7022018-02-20 11:54:42 -08001516 total_caller_video_frames_expected &&
deadbeef1dcb1642017-03-29 21:08:16 -07001517 callee()->audio_frames_received() >=
Seth Hampson2f0d7022018-02-20 11:54:42 -08001518 total_callee_audio_frames_expected &&
deadbeef1dcb1642017-03-29 21:08:16 -07001519 callee()->min_video_frames_received_per_track() >=
Seth Hampson2f0d7022018-02-20 11:54:42 -08001520 total_callee_video_frames_expected,
1521 kMaxWaitForFramesMs);
1522 bool expectations_correct =
1523 caller()->audio_frames_received() >=
1524 total_caller_audio_frames_expected &&
1525 caller()->min_video_frames_received_per_track() >=
1526 total_caller_video_frames_expected &&
1527 callee()->audio_frames_received() >=
1528 total_callee_audio_frames_expected &&
1529 callee()->min_video_frames_received_per_track() >=
1530 total_callee_video_frames_expected;
deadbeef1dcb1642017-03-29 21:08:16 -07001531
Seth Hampson2f0d7022018-02-20 11:54:42 -08001532 // After the combined wait, print out a more detailed message upon
1533 // failure.
deadbeef1dcb1642017-03-29 21:08:16 -07001534 EXPECT_GE(caller()->audio_frames_received(),
Seth Hampson2f0d7022018-02-20 11:54:42 -08001535 total_caller_audio_frames_expected);
deadbeef1dcb1642017-03-29 21:08:16 -07001536 EXPECT_GE(caller()->min_video_frames_received_per_track(),
Seth Hampson2f0d7022018-02-20 11:54:42 -08001537 total_caller_video_frames_expected);
deadbeef1dcb1642017-03-29 21:08:16 -07001538 EXPECT_GE(callee()->audio_frames_received(),
Seth Hampson2f0d7022018-02-20 11:54:42 -08001539 total_callee_audio_frames_expected);
deadbeef1dcb1642017-03-29 21:08:16 -07001540 EXPECT_GE(callee()->min_video_frames_received_per_track(),
Seth Hampson2f0d7022018-02-20 11:54:42 -08001541 total_callee_video_frames_expected);
1542
1543 // We want to make sure nothing unexpected was received.
1544 if (media_expectations.caller_audio_expectation_ ==
1545 MediaExpectations::kExpectNoFrames) {
1546 EXPECT_EQ(caller()->audio_frames_received(),
1547 total_caller_audio_frames_expected);
1548 if (caller()->audio_frames_received() !=
1549 total_caller_audio_frames_expected) {
1550 expectations_correct = false;
1551 }
1552 }
1553 if (media_expectations.caller_video_expectation_ ==
1554 MediaExpectations::kExpectNoFrames) {
1555 EXPECT_EQ(caller()->min_video_frames_received_per_track(),
1556 total_caller_video_frames_expected);
1557 if (caller()->min_video_frames_received_per_track() !=
1558 total_caller_video_frames_expected) {
1559 expectations_correct = false;
1560 }
1561 }
1562 if (media_expectations.callee_audio_expectation_ ==
1563 MediaExpectations::kExpectNoFrames) {
1564 EXPECT_EQ(callee()->audio_frames_received(),
1565 total_callee_audio_frames_expected);
1566 if (callee()->audio_frames_received() !=
1567 total_callee_audio_frames_expected) {
1568 expectations_correct = false;
1569 }
1570 }
1571 if (media_expectations.callee_video_expectation_ ==
1572 MediaExpectations::kExpectNoFrames) {
1573 EXPECT_EQ(callee()->min_video_frames_received_per_track(),
1574 total_callee_video_frames_expected);
1575 if (callee()->min_video_frames_received_per_track() !=
1576 total_callee_video_frames_expected) {
1577 expectations_correct = false;
1578 }
1579 }
1580 return expectations_correct;
deadbeef1dcb1642017-03-29 21:08:16 -07001581 }
1582
Steve Antond91969e2019-05-30 12:27:03 -07001583 void ClosePeerConnections() {
1584 caller()->pc()->Close();
1585 callee()->pc()->Close();
1586 }
1587
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07001588 void TestNegotiatedCipherSuite(
1589 const PeerConnectionFactory::Options& caller_options,
1590 const PeerConnectionFactory::Options& callee_options,
1591 int expected_cipher_suite) {
deadbeef1dcb1642017-03-29 21:08:16 -07001592 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(caller_options,
1593 callee_options));
deadbeef1dcb1642017-03-29 21:08:16 -07001594 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001595 caller()->AddAudioVideoTracks();
1596 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001597 caller()->CreateAndSetAndSignalOffer();
Qingsi Wang7fc821d2018-07-12 12:54:53 -07001598 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07001599 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(expected_cipher_suite),
deadbeefd8ad7882017-04-18 16:01:17 -07001600 caller()->OldGetStats()->SrtpCipher(), kDefaultTimeout);
Qingsi Wang7fc821d2018-07-12 12:54:53 -07001601 // TODO(bugs.webrtc.org/9456): Fix it.
Alex Loiko9289eda2018-11-23 16:18:59 +00001602 EXPECT_EQ(1, webrtc::metrics::NumEvents(
Qingsi Wang7fc821d2018-07-12 12:54:53 -07001603 "WebRTC.PeerConnection.SrtpCryptoSuite.Audio",
1604 expected_cipher_suite));
deadbeef1dcb1642017-03-29 21:08:16 -07001605 }
1606
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07001607 void TestGcmNegotiationUsesCipherSuite(bool local_gcm_enabled,
1608 bool remote_gcm_enabled,
1609 int expected_cipher_suite) {
1610 PeerConnectionFactory::Options caller_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07001611 caller_options.crypto_options.srtp.enable_gcm_crypto_suites =
1612 local_gcm_enabled;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07001613 PeerConnectionFactory::Options callee_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07001614 callee_options.crypto_options.srtp.enable_gcm_crypto_suites =
1615 remote_gcm_enabled;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07001616 TestNegotiatedCipherSuite(caller_options, callee_options,
1617 expected_cipher_suite);
1618 }
1619
Seth Hampson2f0d7022018-02-20 11:54:42 -08001620 protected:
Steve Anton3acffc32018-04-12 17:21:03 -07001621 SdpSemantics sdp_semantics_;
Seth Hampson2f0d7022018-02-20 11:54:42 -08001622
deadbeef1dcb1642017-03-29 21:08:16 -07001623 private:
1624 // |ss_| is used by |network_thread_| so it must be destroyed later.
deadbeef1dcb1642017-03-29 21:08:16 -07001625 std::unique_ptr<rtc::VirtualSocketServer> ss_;
Steve Antonede9ca52017-10-16 13:04:27 -07001626 std::unique_ptr<rtc::FirewallSocketServer> fss_;
deadbeef1dcb1642017-03-29 21:08:16 -07001627 // |network_thread_| and |worker_thread_| are used by both
1628 // |caller_| and |callee_| so they must be destroyed
1629 // later.
1630 std::unique_ptr<rtc::Thread> network_thread_;
1631 std::unique_ptr<rtc::Thread> worker_thread_;
Seth Hampsonaed71642018-06-11 07:41:32 -07001632 // The turn servers and turn customizers should be accessed & deleted on the
1633 // network thread to avoid a race with the socket read/write that occurs
1634 // on the network thread.
1635 std::vector<std::unique_ptr<cricket::TestTurnServer>> turn_servers_;
1636 std::vector<std::unique_ptr<cricket::TestTurnCustomizer>> turn_customizers_;
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001637 webrtc::MediaTransportPair loopback_media_transports_;
deadbeef1dcb1642017-03-29 21:08:16 -07001638 std::unique_ptr<PeerConnectionWrapper> caller_;
1639 std::unique_ptr<PeerConnectionWrapper> callee_;
1640};
1641
Seth Hampson2f0d7022018-02-20 11:54:42 -08001642class PeerConnectionIntegrationTest
1643 : public PeerConnectionIntegrationBaseTest,
1644 public ::testing::WithParamInterface<SdpSemantics> {
1645 protected:
1646 PeerConnectionIntegrationTest()
1647 : PeerConnectionIntegrationBaseTest(GetParam()) {}
1648};
1649
1650class PeerConnectionIntegrationTestPlanB
1651 : public PeerConnectionIntegrationBaseTest {
1652 protected:
1653 PeerConnectionIntegrationTestPlanB()
1654 : PeerConnectionIntegrationBaseTest(SdpSemantics::kPlanB) {}
1655};
1656
1657class PeerConnectionIntegrationTestUnifiedPlan
1658 : public PeerConnectionIntegrationBaseTest {
1659 protected:
1660 PeerConnectionIntegrationTestUnifiedPlan()
1661 : PeerConnectionIntegrationBaseTest(SdpSemantics::kUnifiedPlan) {}
1662};
1663
deadbeef1dcb1642017-03-29 21:08:16 -07001664// Test the OnFirstPacketReceived callback from audio/video RtpReceivers. This
1665// includes testing that the callback is invoked if an observer is connected
1666// after the first packet has already been received.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001667TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07001668 RtpReceiverObserverOnFirstPacketReceived) {
1669 ASSERT_TRUE(CreatePeerConnectionWrappers());
1670 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001671 caller()->AddAudioVideoTracks();
1672 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001673 // Start offer/answer exchange and wait for it to complete.
1674 caller()->CreateAndSetAndSignalOffer();
1675 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1676 // Should be one receiver each for audio/video.
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02001677 EXPECT_EQ(2U, caller()->rtp_receiver_observers().size());
1678 EXPECT_EQ(2U, callee()->rtp_receiver_observers().size());
deadbeef1dcb1642017-03-29 21:08:16 -07001679 // Wait for all "first packet received" callbacks to be fired.
1680 EXPECT_TRUE_WAIT(
Steve Anton64b626b2019-01-28 17:25:26 -08001681 absl::c_all_of(caller()->rtp_receiver_observers(),
1682 [](const std::unique_ptr<MockRtpReceiverObserver>& o) {
1683 return o->first_packet_received();
1684 }),
deadbeef1dcb1642017-03-29 21:08:16 -07001685 kMaxWaitForFramesMs);
1686 EXPECT_TRUE_WAIT(
Steve Anton64b626b2019-01-28 17:25:26 -08001687 absl::c_all_of(callee()->rtp_receiver_observers(),
1688 [](const std::unique_ptr<MockRtpReceiverObserver>& o) {
1689 return o->first_packet_received();
1690 }),
deadbeef1dcb1642017-03-29 21:08:16 -07001691 kMaxWaitForFramesMs);
1692 // If new observers are set after the first packet was already received, the
1693 // callback should still be invoked.
1694 caller()->ResetRtpReceiverObservers();
1695 callee()->ResetRtpReceiverObservers();
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02001696 EXPECT_EQ(2U, caller()->rtp_receiver_observers().size());
1697 EXPECT_EQ(2U, callee()->rtp_receiver_observers().size());
deadbeef1dcb1642017-03-29 21:08:16 -07001698 EXPECT_TRUE(
Steve Anton64b626b2019-01-28 17:25:26 -08001699 absl::c_all_of(caller()->rtp_receiver_observers(),
1700 [](const std::unique_ptr<MockRtpReceiverObserver>& o) {
1701 return o->first_packet_received();
1702 }));
deadbeef1dcb1642017-03-29 21:08:16 -07001703 EXPECT_TRUE(
Steve Anton64b626b2019-01-28 17:25:26 -08001704 absl::c_all_of(callee()->rtp_receiver_observers(),
1705 [](const std::unique_ptr<MockRtpReceiverObserver>& o) {
1706 return o->first_packet_received();
1707 }));
deadbeef1dcb1642017-03-29 21:08:16 -07001708}
1709
1710class DummyDtmfObserver : public DtmfSenderObserverInterface {
1711 public:
1712 DummyDtmfObserver() : completed_(false) {}
1713
1714 // Implements DtmfSenderObserverInterface.
1715 void OnToneChange(const std::string& tone) override {
1716 tones_.push_back(tone);
1717 if (tone.empty()) {
1718 completed_ = true;
1719 }
1720 }
1721
1722 const std::vector<std::string>& tones() const { return tones_; }
1723 bool completed() const { return completed_; }
1724
1725 private:
1726 bool completed_;
1727 std::vector<std::string> tones_;
1728};
1729
1730// Assumes |sender| already has an audio track added and the offer/answer
1731// exchange is done.
1732void TestDtmfFromSenderToReceiver(PeerConnectionWrapper* sender,
1733 PeerConnectionWrapper* receiver) {
Steve Anton15324772018-01-16 10:26:49 -08001734 // We should be able to get a DTMF sender from the local sender.
1735 rtc::scoped_refptr<DtmfSenderInterface> dtmf_sender =
1736 sender->pc()->GetSenders().at(0)->GetDtmfSender();
1737 ASSERT_TRUE(dtmf_sender);
deadbeef1dcb1642017-03-29 21:08:16 -07001738 DummyDtmfObserver observer;
deadbeef1dcb1642017-03-29 21:08:16 -07001739 dtmf_sender->RegisterObserver(&observer);
1740
1741 // Test the DtmfSender object just created.
1742 EXPECT_TRUE(dtmf_sender->CanInsertDtmf());
1743 EXPECT_TRUE(dtmf_sender->InsertDtmf("1a", 100, 50));
1744
1745 EXPECT_TRUE_WAIT(observer.completed(), kDefaultTimeout);
1746 std::vector<std::string> tones = {"1", "a", ""};
1747 EXPECT_EQ(tones, observer.tones());
1748 dtmf_sender->UnregisterObserver();
1749 // TODO(deadbeef): Verify the tones were actually received end-to-end.
1750}
1751
1752// Verifies the DtmfSenderObserver callbacks for a DtmfSender (one in each
1753// direction).
Seth Hampson2f0d7022018-02-20 11:54:42 -08001754TEST_P(PeerConnectionIntegrationTest, DtmfSenderObserver) {
deadbeef1dcb1642017-03-29 21:08:16 -07001755 ASSERT_TRUE(CreatePeerConnectionWrappers());
1756 ConnectFakeSignaling();
1757 // Only need audio for DTMF.
Steve Anton15324772018-01-16 10:26:49 -08001758 caller()->AddAudioTrack();
1759 callee()->AddAudioTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07001760 caller()->CreateAndSetAndSignalOffer();
1761 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
deadbeef71452802017-05-07 17:21:01 -07001762 // DTLS must finish before the DTMF sender can be used reliably.
1763 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07001764 TestDtmfFromSenderToReceiver(caller(), callee());
1765 TestDtmfFromSenderToReceiver(callee(), caller());
1766}
1767
1768// Basic end-to-end test, verifying media can be encoded/transmitted/decoded
1769// between two connections, using DTLS-SRTP.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001770TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithDtls) {
deadbeef1dcb1642017-03-29 21:08:16 -07001771 ASSERT_TRUE(CreatePeerConnectionWrappers());
1772 ConnectFakeSignaling();
Harald Alvestrand194939b2018-01-24 16:04:13 +01001773
deadbeef1dcb1642017-03-29 21:08:16 -07001774 // Do normal offer/answer and wait for some frames to be received in each
1775 // direction.
Steve Anton15324772018-01-16 10:26:49 -08001776 caller()->AddAudioVideoTracks();
1777 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001778 caller()->CreateAndSetAndSignalOffer();
1779 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001780 MediaExpectations media_expectations;
1781 media_expectations.ExpectBidirectionalAudioAndVideo();
1782 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Qingsi Wang7fc821d2018-07-12 12:54:53 -07001783 EXPECT_LE(2, webrtc::metrics::NumEvents("WebRTC.PeerConnection.KeyProtocol",
1784 webrtc::kEnumCounterKeyProtocolDtls));
1785 EXPECT_EQ(0, webrtc::metrics::NumEvents("WebRTC.PeerConnection.KeyProtocol",
1786 webrtc::kEnumCounterKeyProtocolSdes));
deadbeef1dcb1642017-03-29 21:08:16 -07001787}
1788
1789// Uses SDES instead of DTLS for key agreement.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001790TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithSdes) {
deadbeef1dcb1642017-03-29 21:08:16 -07001791 PeerConnectionInterface::RTCConfiguration sdes_config;
1792 sdes_config.enable_dtls_srtp.emplace(false);
1793 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(sdes_config, sdes_config));
1794 ConnectFakeSignaling();
1795
1796 // Do normal offer/answer and wait for some frames to be received in each
1797 // direction.
Steve Anton15324772018-01-16 10:26:49 -08001798 caller()->AddAudioVideoTracks();
1799 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001800 caller()->CreateAndSetAndSignalOffer();
1801 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001802 MediaExpectations media_expectations;
1803 media_expectations.ExpectBidirectionalAudioAndVideo();
1804 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Qingsi Wang7fc821d2018-07-12 12:54:53 -07001805 EXPECT_LE(2, webrtc::metrics::NumEvents("WebRTC.PeerConnection.KeyProtocol",
1806 webrtc::kEnumCounterKeyProtocolSdes));
1807 EXPECT_EQ(0, webrtc::metrics::NumEvents("WebRTC.PeerConnection.KeyProtocol",
1808 webrtc::kEnumCounterKeyProtocolDtls));
deadbeef1dcb1642017-03-29 21:08:16 -07001809}
1810
Steve Anton8c0f7a72017-10-03 10:03:10 -07001811// Tests that the GetRemoteAudioSSLCertificate method returns the remote DTLS
1812// certificate once the DTLS handshake has finished.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001813TEST_P(PeerConnectionIntegrationTest,
Steve Anton8c0f7a72017-10-03 10:03:10 -07001814 GetRemoteAudioSSLCertificateReturnsExchangedCertificate) {
1815 auto GetRemoteAudioSSLCertificate = [](PeerConnectionWrapper* wrapper) {
1816 auto pci = reinterpret_cast<PeerConnectionProxy*>(wrapper->pc());
1817 auto pc = reinterpret_cast<PeerConnection*>(pci->internal());
1818 return pc->GetRemoteAudioSSLCertificate();
1819 };
Zhi Huang70b820f2018-01-27 14:16:15 -08001820 auto GetRemoteAudioSSLCertChain = [](PeerConnectionWrapper* wrapper) {
1821 auto pci = reinterpret_cast<PeerConnectionProxy*>(wrapper->pc());
1822 auto pc = reinterpret_cast<PeerConnection*>(pci->internal());
1823 return pc->GetRemoteAudioSSLCertChain();
1824 };
Steve Anton8c0f7a72017-10-03 10:03:10 -07001825
1826 auto caller_cert = rtc::RTCCertificate::FromPEM(kRsaPems[0]);
1827 auto callee_cert = rtc::RTCCertificate::FromPEM(kRsaPems[1]);
1828
1829 // Configure each side with a known certificate so they can be compared later.
1830 PeerConnectionInterface::RTCConfiguration caller_config;
1831 caller_config.enable_dtls_srtp.emplace(true);
1832 caller_config.certificates.push_back(caller_cert);
1833 PeerConnectionInterface::RTCConfiguration callee_config;
1834 callee_config.enable_dtls_srtp.emplace(true);
1835 callee_config.certificates.push_back(callee_cert);
1836 ASSERT_TRUE(
1837 CreatePeerConnectionWrappersWithConfig(caller_config, callee_config));
1838 ConnectFakeSignaling();
1839
1840 // When first initialized, there should not be a remote SSL certificate (and
1841 // calling this method should not crash).
1842 EXPECT_EQ(nullptr, GetRemoteAudioSSLCertificate(caller()));
1843 EXPECT_EQ(nullptr, GetRemoteAudioSSLCertificate(callee()));
Zhi Huang70b820f2018-01-27 14:16:15 -08001844 EXPECT_EQ(nullptr, GetRemoteAudioSSLCertChain(caller()));
1845 EXPECT_EQ(nullptr, GetRemoteAudioSSLCertChain(callee()));
Steve Anton8c0f7a72017-10-03 10:03:10 -07001846
Steve Anton15324772018-01-16 10:26:49 -08001847 caller()->AddAudioTrack();
1848 callee()->AddAudioTrack();
Steve Anton8c0f7a72017-10-03 10:03:10 -07001849 caller()->CreateAndSetAndSignalOffer();
1850 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1851 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
1852
1853 // Once DTLS has been connected, each side should return the other's SSL
1854 // certificate when calling GetRemoteAudioSSLCertificate.
1855
1856 auto caller_remote_cert = GetRemoteAudioSSLCertificate(caller());
1857 ASSERT_TRUE(caller_remote_cert);
Benjamin Wright6c6c9df2018-10-25 01:16:26 -07001858 EXPECT_EQ(callee_cert->GetSSLCertificate().ToPEMString(),
Steve Anton8c0f7a72017-10-03 10:03:10 -07001859 caller_remote_cert->ToPEMString());
1860
1861 auto callee_remote_cert = GetRemoteAudioSSLCertificate(callee());
1862 ASSERT_TRUE(callee_remote_cert);
Benjamin Wright6c6c9df2018-10-25 01:16:26 -07001863 EXPECT_EQ(caller_cert->GetSSLCertificate().ToPEMString(),
Steve Anton8c0f7a72017-10-03 10:03:10 -07001864 callee_remote_cert->ToPEMString());
Zhi Huang70b820f2018-01-27 14:16:15 -08001865
1866 auto caller_remote_cert_chain = GetRemoteAudioSSLCertChain(caller());
1867 ASSERT_TRUE(caller_remote_cert_chain);
1868 ASSERT_EQ(1U, caller_remote_cert_chain->GetSize());
1869 auto remote_cert = &caller_remote_cert_chain->Get(0);
Benjamin Wright6c6c9df2018-10-25 01:16:26 -07001870 EXPECT_EQ(callee_cert->GetSSLCertificate().ToPEMString(),
Zhi Huang70b820f2018-01-27 14:16:15 -08001871 remote_cert->ToPEMString());
1872
1873 auto callee_remote_cert_chain = GetRemoteAudioSSLCertChain(callee());
1874 ASSERT_TRUE(callee_remote_cert_chain);
1875 ASSERT_EQ(1U, callee_remote_cert_chain->GetSize());
1876 remote_cert = &callee_remote_cert_chain->Get(0);
Benjamin Wright6c6c9df2018-10-25 01:16:26 -07001877 EXPECT_EQ(caller_cert->GetSSLCertificate().ToPEMString(),
Zhi Huang70b820f2018-01-27 14:16:15 -08001878 remote_cert->ToPEMString());
Steve Anton8c0f7a72017-10-03 10:03:10 -07001879}
1880
deadbeef1dcb1642017-03-29 21:08:16 -07001881// This test sets up a call between two parties with a source resolution of
1882// 1280x720 and verifies that a 16:9 aspect ratio is received.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001883TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07001884 Send1280By720ResolutionAndReceive16To9AspectRatio) {
1885 ASSERT_TRUE(CreatePeerConnectionWrappers());
1886 ConnectFakeSignaling();
1887
Niels Möller5c7efe72018-05-11 10:34:46 +02001888 // Add video tracks with 16:9 aspect ratio, size 1280 x 720.
1889 webrtc::FakePeriodicVideoSource::Config config;
1890 config.width = 1280;
1891 config.height = 720;
Johannes Kron965e7942018-09-13 15:36:20 +02001892 config.timestamp_offset_ms = rtc::TimeMillis();
Niels Möller5c7efe72018-05-11 10:34:46 +02001893 caller()->AddTrack(caller()->CreateLocalVideoTrackWithConfig(config));
1894 callee()->AddTrack(callee()->CreateLocalVideoTrackWithConfig(config));
deadbeef1dcb1642017-03-29 21:08:16 -07001895
1896 // Do normal offer/answer and wait for at least one frame to be received in
1897 // each direction.
1898 caller()->CreateAndSetAndSignalOffer();
1899 ASSERT_TRUE_WAIT(caller()->min_video_frames_received_per_track() > 0 &&
1900 callee()->min_video_frames_received_per_track() > 0,
1901 kMaxWaitForFramesMs);
1902
1903 // Check rendered aspect ratio.
1904 EXPECT_EQ(16.0 / 9, caller()->local_rendered_aspect_ratio());
1905 EXPECT_EQ(16.0 / 9, caller()->rendered_aspect_ratio());
1906 EXPECT_EQ(16.0 / 9, callee()->local_rendered_aspect_ratio());
1907 EXPECT_EQ(16.0 / 9, callee()->rendered_aspect_ratio());
1908}
1909
1910// This test sets up an one-way call, with media only from caller to
1911// callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001912TEST_P(PeerConnectionIntegrationTest, OneWayMediaCall) {
deadbeef1dcb1642017-03-29 21:08:16 -07001913 ASSERT_TRUE(CreatePeerConnectionWrappers());
1914 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001915 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001916 caller()->CreateAndSetAndSignalOffer();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001917 MediaExpectations media_expectations;
1918 media_expectations.CalleeExpectsSomeAudioAndVideo();
1919 media_expectations.CallerExpectsNoAudio();
1920 media_expectations.CallerExpectsNoVideo();
1921 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07001922}
1923
1924// This test sets up a audio call initially, with the callee rejecting video
1925// initially. Then later the callee decides to upgrade to audio/video, and
1926// initiates a new offer/answer exchange.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001927TEST_P(PeerConnectionIntegrationTest, AudioToVideoUpgrade) {
deadbeef1dcb1642017-03-29 21:08:16 -07001928 ASSERT_TRUE(CreatePeerConnectionWrappers());
1929 ConnectFakeSignaling();
1930 // Initially, offer an audio/video stream from the caller, but refuse to
1931 // send/receive video on the callee side.
Steve Anton15324772018-01-16 10:26:49 -08001932 caller()->AddAudioVideoTracks();
1933 callee()->AddAudioTrack();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001934 if (sdp_semantics_ == SdpSemantics::kPlanB) {
1935 PeerConnectionInterface::RTCOfferAnswerOptions options;
1936 options.offer_to_receive_video = 0;
1937 callee()->SetOfferAnswerOptions(options);
1938 } else {
1939 callee()->SetRemoteOfferHandler([this] {
1940 callee()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)->Stop();
1941 });
1942 }
deadbeef1dcb1642017-03-29 21:08:16 -07001943 // Do offer/answer and make sure audio is still received end-to-end.
1944 caller()->CreateAndSetAndSignalOffer();
1945 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001946 {
1947 MediaExpectations media_expectations;
1948 media_expectations.ExpectBidirectionalAudio();
1949 media_expectations.ExpectNoVideo();
1950 ASSERT_TRUE(ExpectNewFrames(media_expectations));
1951 }
deadbeef1dcb1642017-03-29 21:08:16 -07001952 // Sanity check that the callee's description has a rejected video section.
1953 ASSERT_NE(nullptr, callee()->pc()->local_description());
1954 const ContentInfo* callee_video_content =
1955 GetFirstVideoContent(callee()->pc()->local_description()->description());
1956 ASSERT_NE(nullptr, callee_video_content);
1957 EXPECT_TRUE(callee_video_content->rejected);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001958
deadbeef1dcb1642017-03-29 21:08:16 -07001959 // Now negotiate with video and ensure negotiation succeeds, with video
1960 // frames and additional audio frames being received.
Steve Anton15324772018-01-16 10:26:49 -08001961 callee()->AddVideoTrack();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001962 if (sdp_semantics_ == SdpSemantics::kPlanB) {
1963 PeerConnectionInterface::RTCOfferAnswerOptions options;
1964 options.offer_to_receive_video = 1;
1965 callee()->SetOfferAnswerOptions(options);
1966 } else {
1967 callee()->SetRemoteOfferHandler(nullptr);
1968 caller()->SetRemoteOfferHandler([this] {
1969 // The caller creates a new transceiver to receive video on when receiving
1970 // the offer, but by default it is send only.
1971 auto transceivers = caller()->pc()->GetTransceivers();
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02001972 ASSERT_EQ(3U, transceivers.size());
Seth Hampson2f0d7022018-02-20 11:54:42 -08001973 ASSERT_EQ(cricket::MEDIA_TYPE_VIDEO,
1974 transceivers[2]->receiver()->media_type());
1975 transceivers[2]->sender()->SetTrack(caller()->CreateLocalVideoTrack());
1976 transceivers[2]->SetDirection(RtpTransceiverDirection::kSendRecv);
1977 });
1978 }
deadbeef1dcb1642017-03-29 21:08:16 -07001979 callee()->CreateAndSetAndSignalOffer();
1980 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001981 {
1982 // Expect additional audio frames to be received after the upgrade.
1983 MediaExpectations media_expectations;
1984 media_expectations.ExpectBidirectionalAudioAndVideo();
1985 ASSERT_TRUE(ExpectNewFrames(media_expectations));
1986 }
deadbeef1dcb1642017-03-29 21:08:16 -07001987}
1988
deadbeef4389b4d2017-09-07 09:07:36 -07001989// Simpler than the above test; just add an audio track to an established
1990// video-only connection.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001991TEST_P(PeerConnectionIntegrationTest, AddAudioToVideoOnlyCall) {
deadbeef4389b4d2017-09-07 09:07:36 -07001992 ASSERT_TRUE(CreatePeerConnectionWrappers());
1993 ConnectFakeSignaling();
1994 // Do initial offer/answer with just a video track.
Steve Anton15324772018-01-16 10:26:49 -08001995 caller()->AddVideoTrack();
1996 callee()->AddVideoTrack();
deadbeef4389b4d2017-09-07 09:07:36 -07001997 caller()->CreateAndSetAndSignalOffer();
1998 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1999 // Now add an audio track and do another offer/answer.
Steve Anton15324772018-01-16 10:26:49 -08002000 caller()->AddAudioTrack();
2001 callee()->AddAudioTrack();
deadbeef4389b4d2017-09-07 09:07:36 -07002002 caller()->CreateAndSetAndSignalOffer();
2003 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2004 // Ensure both audio and video frames are received end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002005 MediaExpectations media_expectations;
2006 media_expectations.ExpectBidirectionalAudioAndVideo();
2007 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef4389b4d2017-09-07 09:07:36 -07002008}
2009
deadbeef1dcb1642017-03-29 21:08:16 -07002010// This test sets up a call that's transferred to a new caller with a different
2011// DTLS fingerprint.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002012TEST_P(PeerConnectionIntegrationTest, CallTransferredForCallee) {
deadbeef1dcb1642017-03-29 21:08:16 -07002013 ASSERT_TRUE(CreatePeerConnectionWrappers());
2014 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002015 caller()->AddAudioVideoTracks();
2016 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002017 caller()->CreateAndSetAndSignalOffer();
2018 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2019
2020 // Keep the original peer around which will still send packets to the
2021 // receiving client. These SRTP packets will be dropped.
2022 std::unique_ptr<PeerConnectionWrapper> original_peer(
2023 SetCallerPcWrapperAndReturnCurrent(
Seth Hampson2f0d7022018-02-20 11:54:42 -08002024 CreatePeerConnectionWrapperWithAlternateKey().release()));
deadbeef1dcb1642017-03-29 21:08:16 -07002025 // TODO(deadbeef): Why do we call Close here? That goes against the comment
2026 // directly above.
2027 original_peer->pc()->Close();
2028
2029 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002030 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002031 caller()->CreateAndSetAndSignalOffer();
2032 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2033 // Wait for some additional frames to be transmitted end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002034 MediaExpectations media_expectations;
2035 media_expectations.ExpectBidirectionalAudioAndVideo();
2036 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002037}
2038
2039// This test sets up a call that's transferred to a new callee with a different
2040// DTLS fingerprint.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002041TEST_P(PeerConnectionIntegrationTest, CallTransferredForCaller) {
deadbeef1dcb1642017-03-29 21:08:16 -07002042 ASSERT_TRUE(CreatePeerConnectionWrappers());
2043 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002044 caller()->AddAudioVideoTracks();
2045 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002046 caller()->CreateAndSetAndSignalOffer();
2047 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2048
2049 // Keep the original peer around which will still send packets to the
2050 // receiving client. These SRTP packets will be dropped.
2051 std::unique_ptr<PeerConnectionWrapper> original_peer(
2052 SetCalleePcWrapperAndReturnCurrent(
Seth Hampson2f0d7022018-02-20 11:54:42 -08002053 CreatePeerConnectionWrapperWithAlternateKey().release()));
deadbeef1dcb1642017-03-29 21:08:16 -07002054 // TODO(deadbeef): Why do we call Close here? That goes against the comment
2055 // directly above.
2056 original_peer->pc()->Close();
2057
2058 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002059 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002060 caller()->SetOfferAnswerOptions(IceRestartOfferAnswerOptions());
2061 caller()->CreateAndSetAndSignalOffer();
2062 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2063 // Wait for some additional frames to be transmitted end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002064 MediaExpectations media_expectations;
2065 media_expectations.ExpectBidirectionalAudioAndVideo();
2066 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002067}
2068
2069// This test sets up a non-bundled call and negotiates bundling at the same
2070// time as starting an ICE restart. When bundling is in effect in the restart,
2071// the DTLS-SRTP context should be successfully reset.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002072TEST_P(PeerConnectionIntegrationTest, BundlingEnabledWhileIceRestartOccurs) {
deadbeef1dcb1642017-03-29 21:08:16 -07002073 ASSERT_TRUE(CreatePeerConnectionWrappers());
2074 ConnectFakeSignaling();
2075
Steve Anton15324772018-01-16 10:26:49 -08002076 caller()->AddAudioVideoTracks();
2077 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002078 // Remove the bundle group from the SDP received by the callee.
2079 callee()->SetReceivedSdpMunger([](cricket::SessionDescription* desc) {
2080 desc->RemoveGroupByName("BUNDLE");
2081 });
2082 caller()->CreateAndSetAndSignalOffer();
2083 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002084 {
2085 MediaExpectations media_expectations;
2086 media_expectations.ExpectBidirectionalAudioAndVideo();
2087 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2088 }
deadbeef1dcb1642017-03-29 21:08:16 -07002089 // Now stop removing the BUNDLE group, and trigger an ICE restart.
2090 callee()->SetReceivedSdpMunger(nullptr);
2091 caller()->SetOfferAnswerOptions(IceRestartOfferAnswerOptions());
2092 caller()->CreateAndSetAndSignalOffer();
2093 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2094
2095 // Expect additional frames to be received after the ICE restart.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002096 {
2097 MediaExpectations media_expectations;
2098 media_expectations.ExpectBidirectionalAudioAndVideo();
2099 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2100 }
deadbeef1dcb1642017-03-29 21:08:16 -07002101}
2102
2103// Test CVO (Coordination of Video Orientation). If a video source is rotated
2104// and both peers support the CVO RTP header extension, the actual video frames
2105// don't need to be encoded in different resolutions, since the rotation is
2106// communicated through the RTP header extension.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002107TEST_P(PeerConnectionIntegrationTest, RotatedVideoWithCVOExtension) {
deadbeef1dcb1642017-03-29 21:08:16 -07002108 ASSERT_TRUE(CreatePeerConnectionWrappers());
2109 ConnectFakeSignaling();
2110 // Add rotated video tracks.
Steve Anton15324772018-01-16 10:26:49 -08002111 caller()->AddTrack(
deadbeef1dcb1642017-03-29 21:08:16 -07002112 caller()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_90));
Steve Anton15324772018-01-16 10:26:49 -08002113 callee()->AddTrack(
deadbeef1dcb1642017-03-29 21:08:16 -07002114 callee()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_270));
2115
2116 // Wait for video frames to be received by both sides.
2117 caller()->CreateAndSetAndSignalOffer();
2118 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2119 ASSERT_TRUE_WAIT(caller()->min_video_frames_received_per_track() > 0 &&
2120 callee()->min_video_frames_received_per_track() > 0,
2121 kMaxWaitForFramesMs);
2122
2123 // Ensure that the aspect ratio is unmodified.
2124 // TODO(deadbeef): Where does 4:3 come from? Should be explicit in the test,
2125 // not just assumed.
2126 EXPECT_EQ(4.0 / 3, caller()->local_rendered_aspect_ratio());
2127 EXPECT_EQ(4.0 / 3, caller()->rendered_aspect_ratio());
2128 EXPECT_EQ(4.0 / 3, callee()->local_rendered_aspect_ratio());
2129 EXPECT_EQ(4.0 / 3, callee()->rendered_aspect_ratio());
2130 // Ensure that the CVO bits were surfaced to the renderer.
2131 EXPECT_EQ(webrtc::kVideoRotation_270, caller()->rendered_rotation());
2132 EXPECT_EQ(webrtc::kVideoRotation_90, callee()->rendered_rotation());
2133}
2134
2135// Test that when the CVO extension isn't supported, video is rotated the
2136// old-fashioned way, by encoding rotated frames.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002137TEST_P(PeerConnectionIntegrationTest, RotatedVideoWithoutCVOExtension) {
deadbeef1dcb1642017-03-29 21:08:16 -07002138 ASSERT_TRUE(CreatePeerConnectionWrappers());
2139 ConnectFakeSignaling();
2140 // Add rotated video tracks.
Steve Anton15324772018-01-16 10:26:49 -08002141 caller()->AddTrack(
deadbeef1dcb1642017-03-29 21:08:16 -07002142 caller()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_90));
Steve Anton15324772018-01-16 10:26:49 -08002143 callee()->AddTrack(
deadbeef1dcb1642017-03-29 21:08:16 -07002144 callee()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_270));
2145
2146 // Remove the CVO extension from the offered SDP.
2147 callee()->SetReceivedSdpMunger([](cricket::SessionDescription* desc) {
2148 cricket::VideoContentDescription* video =
2149 GetFirstVideoContentDescription(desc);
2150 video->ClearRtpHeaderExtensions();
2151 });
2152 // Wait for video frames to be received by both sides.
2153 caller()->CreateAndSetAndSignalOffer();
2154 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2155 ASSERT_TRUE_WAIT(caller()->min_video_frames_received_per_track() > 0 &&
2156 callee()->min_video_frames_received_per_track() > 0,
2157 kMaxWaitForFramesMs);
2158
2159 // Expect that the aspect ratio is inversed to account for the 90/270 degree
2160 // rotation.
2161 // TODO(deadbeef): Where does 4:3 come from? Should be explicit in the test,
2162 // not just assumed.
2163 EXPECT_EQ(3.0 / 4, caller()->local_rendered_aspect_ratio());
2164 EXPECT_EQ(3.0 / 4, caller()->rendered_aspect_ratio());
2165 EXPECT_EQ(3.0 / 4, callee()->local_rendered_aspect_ratio());
2166 EXPECT_EQ(3.0 / 4, callee()->rendered_aspect_ratio());
2167 // Expect that each endpoint is unaware of the rotation of the other endpoint.
2168 EXPECT_EQ(webrtc::kVideoRotation_0, caller()->rendered_rotation());
2169 EXPECT_EQ(webrtc::kVideoRotation_0, callee()->rendered_rotation());
2170}
2171
deadbeef1dcb1642017-03-29 21:08:16 -07002172// Test that if the answerer rejects the audio m= section, no audio is sent or
2173// received, but video still can be.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002174TEST_P(PeerConnectionIntegrationTest, AnswererRejectsAudioSection) {
deadbeef1dcb1642017-03-29 21:08:16 -07002175 ASSERT_TRUE(CreatePeerConnectionWrappers());
2176 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002177 caller()->AddAudioVideoTracks();
Seth Hampson2f0d7022018-02-20 11:54:42 -08002178 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2179 // Only add video track for callee, and set offer_to_receive_audio to 0, so
2180 // it will reject the audio m= section completely.
2181 PeerConnectionInterface::RTCOfferAnswerOptions options;
2182 options.offer_to_receive_audio = 0;
2183 callee()->SetOfferAnswerOptions(options);
2184 } else {
2185 // Stopping the audio RtpTransceiver will cause the media section to be
2186 // rejected in the answer.
2187 callee()->SetRemoteOfferHandler([this] {
2188 callee()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_AUDIO)->Stop();
2189 });
2190 }
Steve Anton15324772018-01-16 10:26:49 -08002191 callee()->AddTrack(callee()->CreateLocalVideoTrack());
deadbeef1dcb1642017-03-29 21:08:16 -07002192 // Do offer/answer and wait for successful end-to-end video frames.
2193 caller()->CreateAndSetAndSignalOffer();
2194 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002195 MediaExpectations media_expectations;
2196 media_expectations.ExpectBidirectionalVideo();
2197 media_expectations.ExpectNoAudio();
2198 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2199
deadbeef1dcb1642017-03-29 21:08:16 -07002200 // Sanity check that the callee's description has a rejected audio section.
2201 ASSERT_NE(nullptr, callee()->pc()->local_description());
2202 const ContentInfo* callee_audio_content =
2203 GetFirstAudioContent(callee()->pc()->local_description()->description());
2204 ASSERT_NE(nullptr, callee_audio_content);
2205 EXPECT_TRUE(callee_audio_content->rejected);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002206 if (sdp_semantics_ == SdpSemantics::kUnifiedPlan) {
2207 // The caller's transceiver should have stopped after receiving the answer.
2208 EXPECT_TRUE(caller()
2209 ->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_AUDIO)
2210 ->stopped());
2211 }
deadbeef1dcb1642017-03-29 21:08:16 -07002212}
2213
2214// Test that if the answerer rejects the video m= section, no video is sent or
2215// received, but audio still can be.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002216TEST_P(PeerConnectionIntegrationTest, AnswererRejectsVideoSection) {
deadbeef1dcb1642017-03-29 21:08:16 -07002217 ASSERT_TRUE(CreatePeerConnectionWrappers());
2218 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002219 caller()->AddAudioVideoTracks();
Seth Hampson2f0d7022018-02-20 11:54:42 -08002220 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2221 // Only add audio track for callee, and set offer_to_receive_video to 0, so
2222 // it will reject the video m= section completely.
2223 PeerConnectionInterface::RTCOfferAnswerOptions options;
2224 options.offer_to_receive_video = 0;
2225 callee()->SetOfferAnswerOptions(options);
2226 } else {
2227 // Stopping the video RtpTransceiver will cause the media section to be
2228 // rejected in the answer.
2229 callee()->SetRemoteOfferHandler([this] {
2230 callee()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)->Stop();
2231 });
2232 }
Steve Anton15324772018-01-16 10:26:49 -08002233 callee()->AddTrack(callee()->CreateLocalAudioTrack());
deadbeef1dcb1642017-03-29 21:08:16 -07002234 // Do offer/answer and wait for successful end-to-end audio frames.
2235 caller()->CreateAndSetAndSignalOffer();
2236 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002237 MediaExpectations media_expectations;
2238 media_expectations.ExpectBidirectionalAudio();
2239 media_expectations.ExpectNoVideo();
2240 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2241
deadbeef1dcb1642017-03-29 21:08:16 -07002242 // Sanity check that the callee's description has a rejected video section.
2243 ASSERT_NE(nullptr, callee()->pc()->local_description());
2244 const ContentInfo* callee_video_content =
2245 GetFirstVideoContent(callee()->pc()->local_description()->description());
2246 ASSERT_NE(nullptr, callee_video_content);
2247 EXPECT_TRUE(callee_video_content->rejected);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002248 if (sdp_semantics_ == SdpSemantics::kUnifiedPlan) {
2249 // The caller's transceiver should have stopped after receiving the answer.
2250 EXPECT_TRUE(caller()
2251 ->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)
2252 ->stopped());
2253 }
deadbeef1dcb1642017-03-29 21:08:16 -07002254}
2255
2256// Test that if the answerer rejects both audio and video m= sections, nothing
2257// bad happens.
2258// TODO(deadbeef): Test that a data channel still works. Currently this doesn't
2259// test anything but the fact that negotiation succeeds, which doesn't mean
2260// much.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002261TEST_P(PeerConnectionIntegrationTest, AnswererRejectsAudioAndVideoSections) {
deadbeef1dcb1642017-03-29 21:08:16 -07002262 ASSERT_TRUE(CreatePeerConnectionWrappers());
2263 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002264 caller()->AddAudioVideoTracks();
Seth Hampson2f0d7022018-02-20 11:54:42 -08002265 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2266 // Don't give the callee any tracks, and set offer_to_receive_X to 0, so it
2267 // will reject both audio and video m= sections.
2268 PeerConnectionInterface::RTCOfferAnswerOptions options;
2269 options.offer_to_receive_audio = 0;
2270 options.offer_to_receive_video = 0;
2271 callee()->SetOfferAnswerOptions(options);
2272 } else {
2273 callee()->SetRemoteOfferHandler([this] {
2274 // Stopping all transceivers will cause all media sections to be rejected.
Mirko Bonadei739baf02019-01-27 17:29:42 +01002275 for (const auto& transceiver : callee()->pc()->GetTransceivers()) {
Seth Hampson2f0d7022018-02-20 11:54:42 -08002276 transceiver->Stop();
2277 }
2278 });
2279 }
deadbeef1dcb1642017-03-29 21:08:16 -07002280 // Do offer/answer and wait for stable signaling state.
2281 caller()->CreateAndSetAndSignalOffer();
2282 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002283
deadbeef1dcb1642017-03-29 21:08:16 -07002284 // Sanity check that the callee's description has rejected m= sections.
2285 ASSERT_NE(nullptr, callee()->pc()->local_description());
2286 const ContentInfo* callee_audio_content =
2287 GetFirstAudioContent(callee()->pc()->local_description()->description());
2288 ASSERT_NE(nullptr, callee_audio_content);
2289 EXPECT_TRUE(callee_audio_content->rejected);
2290 const ContentInfo* callee_video_content =
2291 GetFirstVideoContent(callee()->pc()->local_description()->description());
2292 ASSERT_NE(nullptr, callee_video_content);
2293 EXPECT_TRUE(callee_video_content->rejected);
2294}
2295
2296// This test sets up an audio and video call between two parties. After the
2297// call runs for a while, the caller sends an updated offer with video being
2298// rejected. Once the re-negotiation is done, the video flow should stop and
2299// the audio flow should continue.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002300TEST_P(PeerConnectionIntegrationTest, VideoRejectedInSubsequentOffer) {
deadbeef1dcb1642017-03-29 21:08:16 -07002301 ASSERT_TRUE(CreatePeerConnectionWrappers());
2302 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002303 caller()->AddAudioVideoTracks();
2304 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002305 caller()->CreateAndSetAndSignalOffer();
2306 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002307 {
2308 MediaExpectations media_expectations;
2309 media_expectations.ExpectBidirectionalAudioAndVideo();
2310 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2311 }
deadbeef1dcb1642017-03-29 21:08:16 -07002312 // Renegotiate, rejecting the video m= section.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002313 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2314 caller()->SetGeneratedSdpMunger(
2315 [](cricket::SessionDescription* description) {
2316 for (cricket::ContentInfo& content : description->contents()) {
2317 if (cricket::IsVideoContent(&content)) {
2318 content.rejected = true;
2319 }
2320 }
2321 });
2322 } else {
2323 caller()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)->Stop();
2324 }
deadbeef1dcb1642017-03-29 21:08:16 -07002325 caller()->CreateAndSetAndSignalOffer();
2326 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
2327
2328 // Sanity check that the caller's description has a rejected video section.
2329 ASSERT_NE(nullptr, caller()->pc()->local_description());
2330 const ContentInfo* caller_video_content =
2331 GetFirstVideoContent(caller()->pc()->local_description()->description());
2332 ASSERT_NE(nullptr, caller_video_content);
2333 EXPECT_TRUE(caller_video_content->rejected);
deadbeef1dcb1642017-03-29 21:08:16 -07002334 // Wait for some additional audio frames to be received.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002335 {
2336 MediaExpectations media_expectations;
2337 media_expectations.ExpectBidirectionalAudio();
2338 media_expectations.ExpectNoVideo();
2339 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2340 }
deadbeef1dcb1642017-03-29 21:08:16 -07002341}
2342
Taylor Brandstetter60c8dc82018-04-11 15:20:27 -07002343// Do one offer/answer with audio, another that disables it (rejecting the m=
2344// section), and another that re-enables it. Regression test for:
2345// bugs.webrtc.org/6023
2346TEST_F(PeerConnectionIntegrationTestPlanB, EnableAudioAfterRejecting) {
2347 ASSERT_TRUE(CreatePeerConnectionWrappers());
2348 ConnectFakeSignaling();
2349
2350 // Add audio track, do normal offer/answer.
2351 rtc::scoped_refptr<webrtc::AudioTrackInterface> track =
2352 caller()->CreateLocalAudioTrack();
2353 rtc::scoped_refptr<webrtc::RtpSenderInterface> sender =
2354 caller()->pc()->AddTrack(track, {"stream"}).MoveValue();
2355 caller()->CreateAndSetAndSignalOffer();
2356 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2357
2358 // Remove audio track, and set offer_to_receive_audio to false to cause the
2359 // m= section to be completely disabled, not just "recvonly".
2360 caller()->pc()->RemoveTrack(sender);
2361 PeerConnectionInterface::RTCOfferAnswerOptions options;
2362 options.offer_to_receive_audio = 0;
2363 caller()->SetOfferAnswerOptions(options);
2364 caller()->CreateAndSetAndSignalOffer();
2365 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2366
2367 // Add the audio track again, expecting negotiation to succeed and frames to
2368 // flow.
2369 sender = caller()->pc()->AddTrack(track, {"stream"}).MoveValue();
2370 options.offer_to_receive_audio = 1;
2371 caller()->SetOfferAnswerOptions(options);
2372 caller()->CreateAndSetAndSignalOffer();
2373 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2374
2375 MediaExpectations media_expectations;
2376 media_expectations.CalleeExpectsSomeAudio();
2377 EXPECT_TRUE(ExpectNewFrames(media_expectations));
2378}
2379
deadbeef1dcb1642017-03-29 21:08:16 -07002380// Basic end-to-end test, but without SSRC/MSID signaling. This functionality
2381// is needed to support legacy endpoints.
2382// TODO(deadbeef): When we support the MID extension and demuxing on MID, also
2383// add a test for an end-to-end test without MID signaling either (basically,
2384// the minimum acceptable SDP).
Seth Hampson2f0d7022018-02-20 11:54:42 -08002385TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithoutSsrcOrMsidSignaling) {
deadbeef1dcb1642017-03-29 21:08:16 -07002386 ASSERT_TRUE(CreatePeerConnectionWrappers());
2387 ConnectFakeSignaling();
2388 // Add audio and video, testing that packets can be demuxed on payload type.
Steve Anton15324772018-01-16 10:26:49 -08002389 caller()->AddAudioVideoTracks();
2390 callee()->AddAudioVideoTracks();
deadbeefd8ad7882017-04-18 16:01:17 -07002391 // Remove SSRCs and MSIDs from the received offer SDP.
2392 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
deadbeef1dcb1642017-03-29 21:08:16 -07002393 caller()->CreateAndSetAndSignalOffer();
2394 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002395 MediaExpectations media_expectations;
2396 media_expectations.ExpectBidirectionalAudioAndVideo();
2397 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002398}
2399
Seth Hampson5897a6e2018-04-03 11:16:33 -07002400// Basic end-to-end test, without SSRC signaling. This means that the track
2401// was created properly and frames are delivered when the MSIDs are communicated
2402// with a=msid lines and no a=ssrc lines.
2403TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
2404 EndToEndCallWithoutSsrcSignaling) {
2405 const char kStreamId[] = "streamId";
2406 ASSERT_TRUE(CreatePeerConnectionWrappers());
2407 ConnectFakeSignaling();
2408 // Add just audio tracks.
2409 caller()->AddTrack(caller()->CreateLocalAudioTrack(), {kStreamId});
2410 callee()->AddAudioTrack();
2411
2412 // Remove SSRCs from the received offer SDP.
2413 callee()->SetReceivedSdpMunger(RemoveSsrcsAndKeepMsids);
2414 caller()->CreateAndSetAndSignalOffer();
2415 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2416 MediaExpectations media_expectations;
2417 media_expectations.ExpectBidirectionalAudio();
2418 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2419}
2420
Steve Antondf527fd2018-04-27 15:52:03 -07002421// Tests that video flows between multiple video tracks when SSRCs are not
2422// signaled. This exercises the MID RTP header extension which is needed to
2423// demux the incoming video tracks.
2424TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
2425 EndToEndCallWithTwoVideoTracksAndNoSignaledSsrc) {
2426 ASSERT_TRUE(CreatePeerConnectionWrappers());
2427 ConnectFakeSignaling();
2428 caller()->AddVideoTrack();
2429 caller()->AddVideoTrack();
2430 callee()->AddVideoTrack();
2431 callee()->AddVideoTrack();
2432
2433 caller()->SetReceivedSdpMunger(&RemoveSsrcsAndKeepMsids);
2434 callee()->SetReceivedSdpMunger(&RemoveSsrcsAndKeepMsids);
2435 caller()->CreateAndSetAndSignalOffer();
2436 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2437 ASSERT_EQ(2u, caller()->pc()->GetReceivers().size());
2438 ASSERT_EQ(2u, callee()->pc()->GetReceivers().size());
2439
2440 // Expect video to be received in both directions on both tracks.
2441 MediaExpectations media_expectations;
2442 media_expectations.ExpectBidirectionalVideo();
2443 EXPECT_TRUE(ExpectNewFrames(media_expectations));
2444}
2445
Henrik Boström5b147782018-12-04 11:25:05 +01002446TEST_F(PeerConnectionIntegrationTestUnifiedPlan, NoStreamsMsidLinePresent) {
2447 ASSERT_TRUE(CreatePeerConnectionWrappers());
2448 ConnectFakeSignaling();
2449 caller()->AddAudioTrack();
2450 caller()->AddVideoTrack();
2451 caller()->CreateAndSetAndSignalOffer();
2452 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2453 auto callee_receivers = callee()->pc()->GetReceivers();
2454 ASSERT_EQ(2u, callee_receivers.size());
2455 EXPECT_TRUE(callee_receivers[0]->stream_ids().empty());
2456 EXPECT_TRUE(callee_receivers[1]->stream_ids().empty());
2457}
2458
2459TEST_F(PeerConnectionIntegrationTestUnifiedPlan, NoStreamsMsidLineMissing) {
2460 ASSERT_TRUE(CreatePeerConnectionWrappers());
2461 ConnectFakeSignaling();
2462 caller()->AddAudioTrack();
2463 caller()->AddVideoTrack();
2464 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
2465 caller()->CreateAndSetAndSignalOffer();
2466 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2467 auto callee_receivers = callee()->pc()->GetReceivers();
2468 ASSERT_EQ(2u, callee_receivers.size());
2469 ASSERT_EQ(1u, callee_receivers[0]->stream_ids().size());
2470 ASSERT_EQ(1u, callee_receivers[1]->stream_ids().size());
2471 EXPECT_EQ(callee_receivers[0]->stream_ids()[0],
2472 callee_receivers[1]->stream_ids()[0]);
2473 EXPECT_EQ(callee_receivers[0]->streams()[0],
2474 callee_receivers[1]->streams()[0]);
2475}
2476
deadbeef1dcb1642017-03-29 21:08:16 -07002477// Test that if two video tracks are sent (from caller to callee, in this test),
2478// they're transmitted correctly end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002479TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithTwoVideoTracks) {
deadbeef1dcb1642017-03-29 21:08:16 -07002480 ASSERT_TRUE(CreatePeerConnectionWrappers());
2481 ConnectFakeSignaling();
2482 // Add one audio/video stream, and one video-only stream.
Steve Anton15324772018-01-16 10:26:49 -08002483 caller()->AddAudioVideoTracks();
2484 caller()->AddVideoTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07002485 caller()->CreateAndSetAndSignalOffer();
2486 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Steve Anton15324772018-01-16 10:26:49 -08002487 ASSERT_EQ(3u, callee()->pc()->GetReceivers().size());
Seth Hampson2f0d7022018-02-20 11:54:42 -08002488
2489 MediaExpectations media_expectations;
2490 media_expectations.CalleeExpectsSomeAudioAndVideo();
2491 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002492}
2493
2494static void MakeSpecCompliantMaxBundleOffer(cricket::SessionDescription* desc) {
2495 bool first = true;
2496 for (cricket::ContentInfo& content : desc->contents()) {
2497 if (first) {
2498 first = false;
2499 continue;
2500 }
2501 content.bundle_only = true;
2502 }
2503 first = true;
2504 for (cricket::TransportInfo& transport : desc->transport_infos()) {
2505 if (first) {
2506 first = false;
2507 continue;
2508 }
2509 transport.description.ice_ufrag.clear();
2510 transport.description.ice_pwd.clear();
2511 transport.description.connection_role = cricket::CONNECTIONROLE_NONE;
2512 transport.description.identity_fingerprint.reset(nullptr);
2513 }
2514}
2515
2516// Test that if applying a true "max bundle" offer, which uses ports of 0,
2517// "a=bundle-only", omitting "a=fingerprint", "a=setup", "a=ice-ufrag" and
2518// "a=ice-pwd" for all but the audio "m=" section, negotiation still completes
2519// successfully and media flows.
2520// TODO(deadbeef): Update this test to also omit "a=rtcp-mux", once that works.
2521// TODO(deadbeef): Won't need this test once we start generating actual
2522// standards-compliant SDP.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002523TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07002524 EndToEndCallWithSpecCompliantMaxBundleOffer) {
2525 ASSERT_TRUE(CreatePeerConnectionWrappers());
2526 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002527 caller()->AddAudioVideoTracks();
2528 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002529 // Do the equivalent of setting the port to 0, adding a=bundle-only, and
2530 // removing a=ice-ufrag, a=ice-pwd, a=fingerprint and a=setup from all
2531 // but the first m= section.
2532 callee()->SetReceivedSdpMunger(MakeSpecCompliantMaxBundleOffer);
2533 caller()->CreateAndSetAndSignalOffer();
2534 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002535 MediaExpectations media_expectations;
2536 media_expectations.ExpectBidirectionalAudioAndVideo();
2537 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002538}
2539
2540// Test that we can receive the audio output level from a remote audio track.
2541// TODO(deadbeef): Use a fake audio source and verify that the output level is
2542// exactly what the source on the other side was configured with.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002543TEST_P(PeerConnectionIntegrationTest, GetAudioOutputLevelStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-29 21:08:16 -07002544 ASSERT_TRUE(CreatePeerConnectionWrappers());
2545 ConnectFakeSignaling();
2546 // Just add an audio track.
Steve Anton15324772018-01-16 10:26:49 -08002547 caller()->AddAudioTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07002548 caller()->CreateAndSetAndSignalOffer();
2549 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2550
2551 // Get the audio output level stats. Note that the level is not available
2552 // until an RTCP packet has been received.
deadbeefd8ad7882017-04-18 16:01:17 -07002553 EXPECT_TRUE_WAIT(callee()->OldGetStats()->AudioOutputLevel() > 0,
deadbeef1dcb1642017-03-29 21:08:16 -07002554 kMaxWaitForFramesMs);
2555}
2556
2557// Test that an audio input level is reported.
2558// TODO(deadbeef): Use a fake audio source and verify that the input level is
2559// exactly what the source was configured with.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002560TEST_P(PeerConnectionIntegrationTest, GetAudioInputLevelStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-29 21:08:16 -07002561 ASSERT_TRUE(CreatePeerConnectionWrappers());
2562 ConnectFakeSignaling();
2563 // Just add an audio track.
Steve Anton15324772018-01-16 10:26:49 -08002564 caller()->AddAudioTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07002565 caller()->CreateAndSetAndSignalOffer();
2566 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2567
2568 // Get the audio input level stats. The level should be available very
2569 // soon after the test starts.
deadbeefd8ad7882017-04-18 16:01:17 -07002570 EXPECT_TRUE_WAIT(caller()->OldGetStats()->AudioInputLevel() > 0,
deadbeef1dcb1642017-03-29 21:08:16 -07002571 kMaxWaitForStatsMs);
2572}
2573
2574// Test that we can get incoming byte counts from both audio and video tracks.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002575TEST_P(PeerConnectionIntegrationTest, GetBytesReceivedStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-29 21:08:16 -07002576 ASSERT_TRUE(CreatePeerConnectionWrappers());
2577 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002578 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002579 // Do offer/answer, wait for the callee to receive some frames.
2580 caller()->CreateAndSetAndSignalOffer();
2581 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002582
2583 MediaExpectations media_expectations;
2584 media_expectations.CalleeExpectsSomeAudioAndVideo();
2585 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002586
2587 // Get a handle to the remote tracks created, so they can be used as GetStats
2588 // filters.
Mirko Bonadei739baf02019-01-27 17:29:42 +01002589 for (const auto& receiver : callee()->pc()->GetReceivers()) {
Steve Anton15324772018-01-16 10:26:49 -08002590 // We received frames, so we definitely should have nonzero "received bytes"
2591 // stats at this point.
2592 EXPECT_GT(callee()->OldGetStatsForTrack(receiver->track())->BytesReceived(),
2593 0);
2594 }
deadbeef1dcb1642017-03-29 21:08:16 -07002595}
2596
2597// Test that we can get outgoing byte counts from both audio and video tracks.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002598TEST_P(PeerConnectionIntegrationTest, GetBytesSentStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-29 21:08:16 -07002599 ASSERT_TRUE(CreatePeerConnectionWrappers());
2600 ConnectFakeSignaling();
2601 auto audio_track = caller()->CreateLocalAudioTrack();
2602 auto video_track = caller()->CreateLocalVideoTrack();
Steve Anton15324772018-01-16 10:26:49 -08002603 caller()->AddTrack(audio_track);
2604 caller()->AddTrack(video_track);
deadbeef1dcb1642017-03-29 21:08:16 -07002605 // Do offer/answer, wait for the callee to receive some frames.
2606 caller()->CreateAndSetAndSignalOffer();
2607 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002608 MediaExpectations media_expectations;
2609 media_expectations.CalleeExpectsSomeAudioAndVideo();
2610 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002611
2612 // The callee received frames, so we definitely should have nonzero "sent
2613 // bytes" stats at this point.
deadbeefd8ad7882017-04-18 16:01:17 -07002614 EXPECT_GT(caller()->OldGetStatsForTrack(audio_track)->BytesSent(), 0);
2615 EXPECT_GT(caller()->OldGetStatsForTrack(video_track)->BytesSent(), 0);
2616}
2617
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002618// Test that we can get capture start ntp time.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002619TEST_P(PeerConnectionIntegrationTest, GetCaptureStartNtpTimeWithOldStatsApi) {
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002620 ASSERT_TRUE(CreatePeerConnectionWrappers());
2621 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002622 caller()->AddAudioTrack();
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002623
Steve Anton15324772018-01-16 10:26:49 -08002624 callee()->AddAudioTrack();
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002625
2626 // Do offer/answer, wait for the callee to receive some frames.
2627 caller()->CreateAndSetAndSignalOffer();
2628 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2629
2630 // Get the remote audio track created on the receiver, so they can be used as
2631 // GetStats filters.
Steve Antonfc853712018-03-01 13:48:58 -08002632 auto receivers = callee()->pc()->GetReceivers();
2633 ASSERT_EQ(1u, receivers.size());
2634 auto remote_audio_track = receivers[0]->track();
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002635
2636 // Get the audio output level stats. Note that the level is not available
2637 // until an RTCP packet has been received.
Zhi Huange830e682018-03-30 10:48:35 -07002638 EXPECT_TRUE_WAIT(
2639 callee()->OldGetStatsForTrack(remote_audio_track)->CaptureStartNtpTime() >
2640 0,
2641 2 * kMaxWaitForFramesMs);
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002642}
2643
Steve Antona41959e2018-11-28 11:15:33 -08002644// Test that the track ID is associated with all local and remote SSRC stats
2645// using the old GetStats() and more than 1 audio and more than 1 video track.
2646// This is a regression test for crbug.com/906988
2647TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
2648 OldGetStatsAssociatesTrackIdForManyMediaSections) {
2649 ASSERT_TRUE(CreatePeerConnectionWrappers());
2650 ConnectFakeSignaling();
2651 auto audio_sender_1 = caller()->AddAudioTrack();
2652 auto video_sender_1 = caller()->AddVideoTrack();
2653 auto audio_sender_2 = caller()->AddAudioTrack();
2654 auto video_sender_2 = caller()->AddVideoTrack();
2655 caller()->CreateAndSetAndSignalOffer();
2656 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2657
2658 MediaExpectations media_expectations;
2659 media_expectations.CalleeExpectsSomeAudioAndVideo();
2660 ASSERT_TRUE_WAIT(ExpectNewFrames(media_expectations), kDefaultTimeout);
2661
2662 std::vector<std::string> track_ids = {
2663 audio_sender_1->track()->id(), video_sender_1->track()->id(),
2664 audio_sender_2->track()->id(), video_sender_2->track()->id()};
2665
2666 auto caller_stats = caller()->OldGetStats();
2667 EXPECT_THAT(caller_stats->TrackIds(), UnorderedElementsAreArray(track_ids));
2668 auto callee_stats = callee()->OldGetStats();
2669 EXPECT_THAT(callee_stats->TrackIds(), UnorderedElementsAreArray(track_ids));
2670}
2671
Steve Antonffa6ce42018-11-30 09:26:08 -08002672// Test that the new GetStats() returns stats for all outgoing/incoming streams
2673// with the correct track IDs if there are more than one audio and more than one
2674// video senders/receivers.
2675TEST_P(PeerConnectionIntegrationTest, NewGetStatsManyAudioAndManyVideoStreams) {
2676 ASSERT_TRUE(CreatePeerConnectionWrappers());
2677 ConnectFakeSignaling();
2678 auto audio_sender_1 = caller()->AddAudioTrack();
2679 auto video_sender_1 = caller()->AddVideoTrack();
2680 auto audio_sender_2 = caller()->AddAudioTrack();
2681 auto video_sender_2 = caller()->AddVideoTrack();
2682 caller()->CreateAndSetAndSignalOffer();
2683 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2684
2685 MediaExpectations media_expectations;
2686 media_expectations.CalleeExpectsSomeAudioAndVideo();
2687 ASSERT_TRUE_WAIT(ExpectNewFrames(media_expectations), kDefaultTimeout);
2688
2689 std::vector<std::string> track_ids = {
2690 audio_sender_1->track()->id(), video_sender_1->track()->id(),
2691 audio_sender_2->track()->id(), video_sender_2->track()->id()};
2692
2693 rtc::scoped_refptr<const webrtc::RTCStatsReport> caller_report =
2694 caller()->NewGetStats();
2695 ASSERT_TRUE(caller_report);
2696 auto outbound_stream_stats =
2697 caller_report->GetStatsOfType<webrtc::RTCOutboundRTPStreamStats>();
2698 ASSERT_EQ(4u, outbound_stream_stats.size());
2699 std::vector<std::string> outbound_track_ids;
2700 for (const auto& stat : outbound_stream_stats) {
2701 ASSERT_TRUE(stat->bytes_sent.is_defined());
2702 EXPECT_LT(0u, *stat->bytes_sent);
2703 ASSERT_TRUE(stat->track_id.is_defined());
2704 const auto* track_stat =
2705 caller_report->GetAs<webrtc::RTCMediaStreamTrackStats>(*stat->track_id);
2706 ASSERT_TRUE(track_stat);
2707 outbound_track_ids.push_back(*track_stat->track_identifier);
2708 }
2709 EXPECT_THAT(outbound_track_ids, UnorderedElementsAreArray(track_ids));
2710
2711 rtc::scoped_refptr<const webrtc::RTCStatsReport> callee_report =
2712 callee()->NewGetStats();
2713 ASSERT_TRUE(callee_report);
2714 auto inbound_stream_stats =
2715 callee_report->GetStatsOfType<webrtc::RTCInboundRTPStreamStats>();
2716 ASSERT_EQ(4u, inbound_stream_stats.size());
2717 std::vector<std::string> inbound_track_ids;
2718 for (const auto& stat : inbound_stream_stats) {
2719 ASSERT_TRUE(stat->bytes_received.is_defined());
2720 EXPECT_LT(0u, *stat->bytes_received);
2721 ASSERT_TRUE(stat->track_id.is_defined());
2722 const auto* track_stat =
2723 callee_report->GetAs<webrtc::RTCMediaStreamTrackStats>(*stat->track_id);
2724 ASSERT_TRUE(track_stat);
2725 inbound_track_ids.push_back(*track_stat->track_identifier);
2726 }
2727 EXPECT_THAT(inbound_track_ids, UnorderedElementsAreArray(track_ids));
2728}
2729
2730// Test that we can get stats (using the new stats implementation) for
deadbeefd8ad7882017-04-18 16:01:17 -07002731// unsignaled streams. Meaning when SSRCs/MSIDs aren't signaled explicitly in
2732// SDP.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002733TEST_P(PeerConnectionIntegrationTest,
deadbeefd8ad7882017-04-18 16:01:17 -07002734 GetStatsForUnsignaledStreamWithNewStatsApi) {
2735 ASSERT_TRUE(CreatePeerConnectionWrappers());
2736 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002737 caller()->AddAudioTrack();
deadbeefd8ad7882017-04-18 16:01:17 -07002738 // Remove SSRCs and MSIDs from the received offer SDP.
2739 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
2740 caller()->CreateAndSetAndSignalOffer();
2741 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002742 MediaExpectations media_expectations;
2743 media_expectations.CalleeExpectsSomeAudio(1);
2744 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeefd8ad7882017-04-18 16:01:17 -07002745
2746 // We received a frame, so we should have nonzero "bytes received" stats for
2747 // the unsignaled stream, if stats are working for it.
2748 rtc::scoped_refptr<const webrtc::RTCStatsReport> report =
2749 callee()->NewGetStats();
2750 ASSERT_NE(nullptr, report);
2751 auto inbound_stream_stats =
2752 report->GetStatsOfType<webrtc::RTCInboundRTPStreamStats>();
2753 ASSERT_EQ(1U, inbound_stream_stats.size());
2754 ASSERT_TRUE(inbound_stream_stats[0]->bytes_received.is_defined());
2755 ASSERT_GT(*inbound_stream_stats[0]->bytes_received, 0U);
zhihuangf8164932017-05-19 13:09:47 -07002756 ASSERT_TRUE(inbound_stream_stats[0]->track_id.is_defined());
2757}
2758
Taylor Brandstettera4653442018-06-19 09:44:26 -07002759// Same as above but for the legacy stats implementation.
2760TEST_P(PeerConnectionIntegrationTest,
2761 GetStatsForUnsignaledStreamWithOldStatsApi) {
2762 ASSERT_TRUE(CreatePeerConnectionWrappers());
2763 ConnectFakeSignaling();
2764 caller()->AddAudioTrack();
2765 // Remove SSRCs and MSIDs from the received offer SDP.
2766 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
2767 caller()->CreateAndSetAndSignalOffer();
2768 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2769
2770 // Note that, since the old stats implementation associates SSRCs with tracks
2771 // using SDP, when SSRCs aren't signaled in SDP these stats won't have an
2772 // associated track ID. So we can't use the track "selector" argument.
2773 //
2774 // Also, we use "EXPECT_TRUE_WAIT" because the stats collector may decide to
2775 // return cached stats if not enough time has passed since the last update.
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02002776 EXPECT_TRUE_WAIT(callee()->OldGetStats()->BytesReceived() > 0,
Taylor Brandstettera4653442018-06-19 09:44:26 -07002777 kDefaultTimeout);
2778}
2779
zhihuangf8164932017-05-19 13:09:47 -07002780// Test that we can successfully get the media related stats (audio level
2781// etc.) for the unsignaled stream.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002782TEST_P(PeerConnectionIntegrationTest,
zhihuangf8164932017-05-19 13:09:47 -07002783 GetMediaStatsForUnsignaledStreamWithNewStatsApi) {
2784 ASSERT_TRUE(CreatePeerConnectionWrappers());
2785 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002786 caller()->AddAudioVideoTracks();
zhihuangf8164932017-05-19 13:09:47 -07002787 // Remove SSRCs and MSIDs from the received offer SDP.
2788 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
2789 caller()->CreateAndSetAndSignalOffer();
2790 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002791 MediaExpectations media_expectations;
2792 media_expectations.CalleeExpectsSomeAudio(1);
2793 media_expectations.CalleeExpectsSomeVideo(1);
2794 ASSERT_TRUE(ExpectNewFrames(media_expectations));
zhihuangf8164932017-05-19 13:09:47 -07002795
2796 rtc::scoped_refptr<const webrtc::RTCStatsReport> report =
2797 callee()->NewGetStats();
2798 ASSERT_NE(nullptr, report);
2799
2800 auto media_stats = report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
2801 auto audio_index = FindFirstMediaStatsIndexByKind("audio", media_stats);
2802 ASSERT_GE(audio_index, 0);
2803 EXPECT_TRUE(media_stats[audio_index]->audio_level.is_defined());
deadbeef1dcb1642017-03-29 21:08:16 -07002804}
2805
deadbeef4e2deab2017-09-20 13:56:21 -07002806// Helper for test below.
2807void ModifySsrcs(cricket::SessionDescription* desc) {
2808 for (ContentInfo& content : desc->contents()) {
Steve Antondf527fd2018-04-27 15:52:03 -07002809 for (StreamParams& stream :
Steve Antonb1c1de12017-12-21 15:14:30 -08002810 content.media_description()->mutable_streams()) {
deadbeef4e2deab2017-09-20 13:56:21 -07002811 for (uint32_t& ssrc : stream.ssrcs) {
2812 ssrc = rtc::CreateRandomId();
2813 }
2814 }
2815 }
2816}
2817
2818// Test that the "RTCMediaSteamTrackStats" object is updated correctly when
2819// SSRCs are unsignaled, and the SSRC of the received (audio) stream changes.
2820// This should result in two "RTCInboundRTPStreamStats", but only one
2821// "RTCMediaStreamTrackStats", whose counters go up continuously rather than
2822// being reset to 0 once the SSRC change occurs.
2823//
2824// Regression test for this bug:
2825// https://bugs.chromium.org/p/webrtc/issues/detail?id=8158
2826//
2827// The bug causes the track stats to only represent one of the two streams:
2828// whichever one has the higher SSRC. So with this bug, there was a 50% chance
2829// that the track stat counters would reset to 0 when the new stream is
2830// received, and a 50% chance that they'll stop updating (while
2831// "concealed_samples" continues increasing, due to silence being generated for
2832// the inactive stream).
Seth Hampson2f0d7022018-02-20 11:54:42 -08002833TEST_P(PeerConnectionIntegrationTest,
Steve Anton83119dd2017-11-10 16:19:52 -08002834 TrackStatsUpdatedCorrectlyWhenUnsignaledSsrcChanges) {
deadbeef4e2deab2017-09-20 13:56:21 -07002835 ASSERT_TRUE(CreatePeerConnectionWrappers());
2836 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002837 caller()->AddAudioTrack();
deadbeef4e2deab2017-09-20 13:56:21 -07002838 // Remove SSRCs and MSIDs from the received offer SDP, simulating an endpoint
2839 // that doesn't signal SSRCs (from the callee's perspective).
2840 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
2841 caller()->CreateAndSetAndSignalOffer();
2842 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2843 // Wait for 50 audio frames (500ms of audio) to be received by the callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002844 {
2845 MediaExpectations media_expectations;
2846 media_expectations.CalleeExpectsSomeAudio(50);
2847 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2848 }
deadbeef4e2deab2017-09-20 13:56:21 -07002849 // Some audio frames were received, so we should have nonzero "samples
2850 // received" for the track.
2851 rtc::scoped_refptr<const webrtc::RTCStatsReport> report =
2852 callee()->NewGetStats();
2853 ASSERT_NE(nullptr, report);
2854 auto track_stats = report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
2855 ASSERT_EQ(1U, track_stats.size());
2856 ASSERT_TRUE(track_stats[0]->total_samples_received.is_defined());
2857 ASSERT_GT(*track_stats[0]->total_samples_received, 0U);
2858 // uint64_t prev_samples_received = *track_stats[0]->total_samples_received;
2859
2860 // Create a new offer and munge it to cause the caller to use a new SSRC.
2861 caller()->SetGeneratedSdpMunger(ModifySsrcs);
2862 caller()->CreateAndSetAndSignalOffer();
2863 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2864 // Wait for 25 more audio frames (250ms of audio) to be received, from the new
2865 // SSRC.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002866 {
2867 MediaExpectations media_expectations;
2868 media_expectations.CalleeExpectsSomeAudio(25);
2869 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2870 }
deadbeef4e2deab2017-09-20 13:56:21 -07002871
2872 report = callee()->NewGetStats();
2873 ASSERT_NE(nullptr, report);
2874 track_stats = report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
2875 ASSERT_EQ(1U, track_stats.size());
2876 ASSERT_TRUE(track_stats[0]->total_samples_received.is_defined());
2877 // The "total samples received" stat should only be greater than it was
2878 // before.
2879 // TODO(deadbeef): Uncomment this assertion once the bug is completely fixed.
2880 // Right now, the new SSRC will cause the counters to reset to 0.
2881 // EXPECT_GT(*track_stats[0]->total_samples_received, prev_samples_received);
2882
2883 // Additionally, the percentage of concealed samples (samples generated to
Steve Anton83119dd2017-11-10 16:19:52 -08002884 // conceal packet loss) should be less than 50%. If it's greater, that's a
deadbeef4e2deab2017-09-20 13:56:21 -07002885 // good sign that we're seeing stats from the old stream that's no longer
2886 // receiving packets, and is generating concealed samples of silence.
Steve Anton83119dd2017-11-10 16:19:52 -08002887 constexpr double kAcceptableConcealedSamplesPercentage = 0.50;
deadbeef4e2deab2017-09-20 13:56:21 -07002888 ASSERT_TRUE(track_stats[0]->concealed_samples.is_defined());
2889 EXPECT_LT(*track_stats[0]->concealed_samples,
2890 *track_stats[0]->total_samples_received *
2891 kAcceptableConcealedSamplesPercentage);
2892
2893 // Also ensure that we have two "RTCInboundRTPStreamStats" as expected, as a
2894 // sanity check that the SSRC really changed.
2895 // TODO(deadbeef): This isn't working right now, because we're not returning
2896 // *any* stats for the inactive stream. Uncomment when the bug is completely
2897 // fixed.
2898 // auto inbound_stream_stats =
2899 // report->GetStatsOfType<webrtc::RTCInboundRTPStreamStats>();
2900 // ASSERT_EQ(2U, inbound_stream_stats.size());
2901}
2902
deadbeef1dcb1642017-03-29 21:08:16 -07002903// Test that DTLS 1.0 is used if both sides only support DTLS 1.0.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002904TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithDtls10) {
deadbeef1dcb1642017-03-29 21:08:16 -07002905 PeerConnectionFactory::Options dtls_10_options;
2906 dtls_10_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
2907 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(dtls_10_options,
2908 dtls_10_options));
2909 ConnectFakeSignaling();
2910 // Do normal offer/answer and wait for some frames to be received in each
2911 // direction.
Steve Anton15324772018-01-16 10:26:49 -08002912 caller()->AddAudioVideoTracks();
2913 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002914 caller()->CreateAndSetAndSignalOffer();
2915 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002916 MediaExpectations media_expectations;
2917 media_expectations.ExpectBidirectionalAudioAndVideo();
2918 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002919}
2920
2921// Test getting cipher stats and UMA metrics when DTLS 1.0 is negotiated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002922TEST_P(PeerConnectionIntegrationTest, Dtls10CipherStatsAndUmaMetrics) {
deadbeef1dcb1642017-03-29 21:08:16 -07002923 PeerConnectionFactory::Options dtls_10_options;
2924 dtls_10_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
2925 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(dtls_10_options,
2926 dtls_10_options));
2927 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002928 caller()->AddAudioVideoTracks();
2929 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002930 caller()->CreateAndSetAndSignalOffer();
Qingsi Wang7fc821d2018-07-12 12:54:53 -07002931 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07002932 EXPECT_TRUE_WAIT(rtc::SSLStreamAdapter::IsAcceptableCipher(
deadbeefd8ad7882017-04-18 16:01:17 -07002933 caller()->OldGetStats()->DtlsCipher(), rtc::KT_DEFAULT),
deadbeef1dcb1642017-03-29 21:08:16 -07002934 kDefaultTimeout);
2935 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
deadbeefd8ad7882017-04-18 16:01:17 -07002936 caller()->OldGetStats()->SrtpCipher(), kDefaultTimeout);
Qingsi Wang7fc821d2018-07-12 12:54:53 -07002937 // TODO(bugs.webrtc.org/9456): Fix it.
Alex Loiko9289eda2018-11-23 16:18:59 +00002938 EXPECT_EQ(1, webrtc::metrics::NumEvents(
Qingsi Wang7fc821d2018-07-12 12:54:53 -07002939 "WebRTC.PeerConnection.SrtpCryptoSuite.Audio",
2940 kDefaultSrtpCryptoSuite));
deadbeef1dcb1642017-03-29 21:08:16 -07002941}
2942
2943// Test getting cipher stats and UMA metrics when DTLS 1.2 is negotiated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002944TEST_P(PeerConnectionIntegrationTest, Dtls12CipherStatsAndUmaMetrics) {
deadbeef1dcb1642017-03-29 21:08:16 -07002945 PeerConnectionFactory::Options dtls_12_options;
2946 dtls_12_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
2947 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(dtls_12_options,
2948 dtls_12_options));
2949 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002950 caller()->AddAudioVideoTracks();
2951 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002952 caller()->CreateAndSetAndSignalOffer();
Qingsi Wang7fc821d2018-07-12 12:54:53 -07002953 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07002954 EXPECT_TRUE_WAIT(rtc::SSLStreamAdapter::IsAcceptableCipher(
deadbeefd8ad7882017-04-18 16:01:17 -07002955 caller()->OldGetStats()->DtlsCipher(), rtc::KT_DEFAULT),
deadbeef1dcb1642017-03-29 21:08:16 -07002956 kDefaultTimeout);
2957 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
deadbeefd8ad7882017-04-18 16:01:17 -07002958 caller()->OldGetStats()->SrtpCipher(), kDefaultTimeout);
Qingsi Wang7fc821d2018-07-12 12:54:53 -07002959 // TODO(bugs.webrtc.org/9456): Fix it.
Alex Loiko9289eda2018-11-23 16:18:59 +00002960 EXPECT_EQ(1, webrtc::metrics::NumEvents(
Qingsi Wang7fc821d2018-07-12 12:54:53 -07002961 "WebRTC.PeerConnection.SrtpCryptoSuite.Audio",
2962 kDefaultSrtpCryptoSuite));
deadbeef1dcb1642017-03-29 21:08:16 -07002963}
2964
2965// Test that DTLS 1.0 can be used if the caller supports DTLS 1.2 and the
2966// callee only supports 1.0.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002967TEST_P(PeerConnectionIntegrationTest, CallerDtls12ToCalleeDtls10) {
deadbeef1dcb1642017-03-29 21:08:16 -07002968 PeerConnectionFactory::Options caller_options;
2969 caller_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
2970 PeerConnectionFactory::Options callee_options;
2971 callee_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
2972 ASSERT_TRUE(
2973 CreatePeerConnectionWrappersWithOptions(caller_options, callee_options));
2974 ConnectFakeSignaling();
2975 // Do normal offer/answer and wait for some frames to be received in each
2976 // direction.
Steve Anton15324772018-01-16 10:26:49 -08002977 caller()->AddAudioVideoTracks();
2978 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002979 caller()->CreateAndSetAndSignalOffer();
2980 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002981 MediaExpectations media_expectations;
2982 media_expectations.ExpectBidirectionalAudioAndVideo();
2983 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002984}
2985
2986// Test that DTLS 1.0 can be used if the caller only supports DTLS 1.0 and the
2987// callee supports 1.2.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002988TEST_P(PeerConnectionIntegrationTest, CallerDtls10ToCalleeDtls12) {
deadbeef1dcb1642017-03-29 21:08:16 -07002989 PeerConnectionFactory::Options caller_options;
2990 caller_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
2991 PeerConnectionFactory::Options callee_options;
2992 callee_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
2993 ASSERT_TRUE(
2994 CreatePeerConnectionWrappersWithOptions(caller_options, callee_options));
2995 ConnectFakeSignaling();
2996 // Do normal offer/answer and wait for some frames to be received in each
2997 // direction.
Steve Anton15324772018-01-16 10:26:49 -08002998 caller()->AddAudioVideoTracks();
2999 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003000 caller()->CreateAndSetAndSignalOffer();
3001 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08003002 MediaExpectations media_expectations;
3003 media_expectations.ExpectBidirectionalAudioAndVideo();
3004 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003005}
3006
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07003007// The three tests below verify that "enable_aes128_sha1_32_crypto_cipher"
3008// works as expected; the cipher should only be used if enabled by both sides.
3009TEST_P(PeerConnectionIntegrationTest,
3010 Aes128Sha1_32_CipherNotUsedWhenOnlyCallerSupported) {
3011 PeerConnectionFactory::Options caller_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07003012 caller_options.crypto_options.srtp.enable_aes128_sha1_32_crypto_cipher = true;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07003013 PeerConnectionFactory::Options callee_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07003014 callee_options.crypto_options.srtp.enable_aes128_sha1_32_crypto_cipher =
3015 false;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07003016 int expected_cipher_suite = rtc::SRTP_AES128_CM_SHA1_80;
3017 TestNegotiatedCipherSuite(caller_options, callee_options,
3018 expected_cipher_suite);
3019}
3020
3021TEST_P(PeerConnectionIntegrationTest,
3022 Aes128Sha1_32_CipherNotUsedWhenOnlyCalleeSupported) {
3023 PeerConnectionFactory::Options caller_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07003024 caller_options.crypto_options.srtp.enable_aes128_sha1_32_crypto_cipher =
3025 false;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07003026 PeerConnectionFactory::Options callee_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07003027 callee_options.crypto_options.srtp.enable_aes128_sha1_32_crypto_cipher = true;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07003028 int expected_cipher_suite = rtc::SRTP_AES128_CM_SHA1_80;
3029 TestNegotiatedCipherSuite(caller_options, callee_options,
3030 expected_cipher_suite);
3031}
3032
3033TEST_P(PeerConnectionIntegrationTest, Aes128Sha1_32_CipherUsedWhenSupported) {
3034 PeerConnectionFactory::Options caller_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07003035 caller_options.crypto_options.srtp.enable_aes128_sha1_32_crypto_cipher = true;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07003036 PeerConnectionFactory::Options callee_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07003037 callee_options.crypto_options.srtp.enable_aes128_sha1_32_crypto_cipher = true;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07003038 int expected_cipher_suite = rtc::SRTP_AES128_CM_SHA1_32;
3039 TestNegotiatedCipherSuite(caller_options, callee_options,
3040 expected_cipher_suite);
3041}
3042
deadbeef1dcb1642017-03-29 21:08:16 -07003043// Test that a non-GCM cipher is used if both sides only support non-GCM.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003044TEST_P(PeerConnectionIntegrationTest, NonGcmCipherUsedWhenGcmNotSupported) {
deadbeef1dcb1642017-03-29 21:08:16 -07003045 bool local_gcm_enabled = false;
3046 bool remote_gcm_enabled = false;
3047 int expected_cipher_suite = kDefaultSrtpCryptoSuite;
3048 TestGcmNegotiationUsesCipherSuite(local_gcm_enabled, remote_gcm_enabled,
3049 expected_cipher_suite);
3050}
3051
3052// Test that a GCM cipher is used if both ends support it.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003053TEST_P(PeerConnectionIntegrationTest, GcmCipherUsedWhenGcmSupported) {
deadbeef1dcb1642017-03-29 21:08:16 -07003054 bool local_gcm_enabled = true;
3055 bool remote_gcm_enabled = true;
3056 int expected_cipher_suite = kDefaultSrtpCryptoSuiteGcm;
3057 TestGcmNegotiationUsesCipherSuite(local_gcm_enabled, remote_gcm_enabled,
3058 expected_cipher_suite);
3059}
3060
3061// Test that GCM isn't used if only the offerer supports it.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003062TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07003063 NonGcmCipherUsedWhenOnlyCallerSupportsGcm) {
3064 bool local_gcm_enabled = true;
3065 bool remote_gcm_enabled = false;
3066 int expected_cipher_suite = kDefaultSrtpCryptoSuite;
3067 TestGcmNegotiationUsesCipherSuite(local_gcm_enabled, remote_gcm_enabled,
3068 expected_cipher_suite);
3069}
3070
3071// Test that GCM isn't used if only the answerer supports it.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003072TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07003073 NonGcmCipherUsedWhenOnlyCalleeSupportsGcm) {
3074 bool local_gcm_enabled = false;
3075 bool remote_gcm_enabled = true;
3076 int expected_cipher_suite = kDefaultSrtpCryptoSuite;
3077 TestGcmNegotiationUsesCipherSuite(local_gcm_enabled, remote_gcm_enabled,
3078 expected_cipher_suite);
3079}
3080
deadbeef7914b8c2017-04-21 03:23:33 -07003081// Verify that media can be transmitted end-to-end when GCM crypto suites are
3082// enabled. Note that the above tests, such as GcmCipherUsedWhenGcmSupported,
3083// only verify that a GCM cipher is negotiated, and not necessarily that SRTP
3084// works with it.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003085TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithGcmCipher) {
deadbeef7914b8c2017-04-21 03:23:33 -07003086 PeerConnectionFactory::Options gcm_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07003087 gcm_options.crypto_options.srtp.enable_gcm_crypto_suites = true;
deadbeef7914b8c2017-04-21 03:23:33 -07003088 ASSERT_TRUE(
3089 CreatePeerConnectionWrappersWithOptions(gcm_options, gcm_options));
3090 ConnectFakeSignaling();
3091 // Do normal offer/answer and wait for some frames to be received in each
3092 // direction.
Steve Anton15324772018-01-16 10:26:49 -08003093 caller()->AddAudioVideoTracks();
3094 callee()->AddAudioVideoTracks();
deadbeef7914b8c2017-04-21 03:23:33 -07003095 caller()->CreateAndSetAndSignalOffer();
3096 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08003097 MediaExpectations media_expectations;
3098 media_expectations.ExpectBidirectionalAudioAndVideo();
3099 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef7914b8c2017-04-21 03:23:33 -07003100}
3101
deadbeef1dcb1642017-03-29 21:08:16 -07003102// This test sets up a call between two parties with audio, video and an RTP
3103// data channel.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003104TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithRtpDataChannel) {
Niels Möllerf06f9232018-08-07 12:32:18 +02003105 PeerConnectionInterface::RTCConfiguration rtc_config;
3106 rtc_config.enable_rtp_data_channel = true;
3107 rtc_config.enable_dtls_srtp = false;
3108 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(rtc_config, rtc_config));
deadbeef1dcb1642017-03-29 21:08:16 -07003109 ConnectFakeSignaling();
3110 // Expect that data channel created on caller side will show up for callee as
3111 // well.
3112 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08003113 caller()->AddAudioVideoTracks();
3114 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003115 caller()->CreateAndSetAndSignalOffer();
3116 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3117 // Ensure the existence of the RTP data channel didn't impede audio/video.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003118 MediaExpectations media_expectations;
3119 media_expectations.ExpectBidirectionalAudioAndVideo();
3120 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003121 ASSERT_NE(nullptr, caller()->data_channel());
3122 ASSERT_NE(nullptr, callee()->data_channel());
3123 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3124 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3125
3126 // Ensure data can be sent in both directions.
3127 std::string data = "hello world";
3128 SendRtpDataWithRetries(caller()->data_channel(), data, 5);
3129 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3130 kDefaultTimeout);
3131 SendRtpDataWithRetries(callee()->data_channel(), data, 5);
3132 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3133 kDefaultTimeout);
3134}
3135
3136// Ensure that an RTP data channel is signaled as closed for the caller when
3137// the callee rejects it in a subsequent offer.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003138TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07003139 RtpDataChannelSignaledClosedInCalleeOffer) {
3140 // Same procedure as above test.
Niels Möllerf06f9232018-08-07 12:32:18 +02003141 PeerConnectionInterface::RTCConfiguration rtc_config;
3142 rtc_config.enable_rtp_data_channel = true;
3143 rtc_config.enable_dtls_srtp = false;
3144 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(rtc_config, rtc_config));
deadbeef1dcb1642017-03-29 21:08:16 -07003145 ConnectFakeSignaling();
3146 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08003147 caller()->AddAudioVideoTracks();
3148 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003149 caller()->CreateAndSetAndSignalOffer();
3150 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3151 ASSERT_NE(nullptr, caller()->data_channel());
3152 ASSERT_NE(nullptr, callee()->data_channel());
3153 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3154 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3155
3156 // Close the data channel on the callee, and do an updated offer/answer.
3157 callee()->data_channel()->Close();
3158 callee()->CreateAndSetAndSignalOffer();
3159 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3160 EXPECT_FALSE(caller()->data_observer()->IsOpen());
3161 EXPECT_FALSE(callee()->data_observer()->IsOpen());
3162}
3163
3164// Tests that data is buffered in an RTP data channel until an observer is
3165// registered for it.
3166//
3167// NOTE: RTP data channels can receive data before the underlying
3168// transport has detected that a channel is writable and thus data can be
3169// received before the data channel state changes to open. That is hard to test
3170// but the same buffering is expected to be used in that case.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003171TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07003172 DataBufferedUntilRtpDataChannelObserverRegistered) {
3173 // Use fake clock and simulated network delay so that we predictably can wait
3174 // until an SCTP message has been delivered without "sleep()"ing.
3175 rtc::ScopedFakeClock fake_clock;
3176 // Some things use a time of "0" as a special value, so we need to start out
3177 // the fake clock at a nonzero time.
3178 // TODO(deadbeef): Fix this.
Sebastian Jansson5f83cf02018-05-08 14:52:22 +02003179 fake_clock.AdvanceTime(webrtc::TimeDelta::seconds(1));
deadbeef1dcb1642017-03-29 21:08:16 -07003180 virtual_socket_server()->set_delay_mean(5); // 5 ms per hop.
3181 virtual_socket_server()->UpdateDelayDistribution();
3182
Niels Möllerf06f9232018-08-07 12:32:18 +02003183 PeerConnectionInterface::RTCConfiguration rtc_config;
3184 rtc_config.enable_rtp_data_channel = true;
3185 rtc_config.enable_dtls_srtp = false;
3186 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(rtc_config, rtc_config));
deadbeef1dcb1642017-03-29 21:08:16 -07003187 ConnectFakeSignaling();
3188 caller()->CreateDataChannel();
3189 caller()->CreateAndSetAndSignalOffer();
3190 ASSERT_TRUE(caller()->data_channel() != nullptr);
3191 ASSERT_TRUE_SIMULATED_WAIT(callee()->data_channel() != nullptr,
3192 kDefaultTimeout, fake_clock);
3193 ASSERT_TRUE_SIMULATED_WAIT(caller()->data_observer()->IsOpen(),
3194 kDefaultTimeout, fake_clock);
3195 ASSERT_EQ_SIMULATED_WAIT(DataChannelInterface::kOpen,
3196 callee()->data_channel()->state(), kDefaultTimeout,
3197 fake_clock);
3198
3199 // Unregister the observer which is normally automatically registered.
3200 callee()->data_channel()->UnregisterObserver();
3201 // Send data and advance fake clock until it should have been received.
3202 std::string data = "hello world";
3203 caller()->data_channel()->Send(DataBuffer(data));
3204 SIMULATED_WAIT(false, 50, fake_clock);
3205
3206 // Attach data channel and expect data to be received immediately. Note that
3207 // EXPECT_EQ_WAIT is used, such that the simulated clock is not advanced any
3208 // further, but data can be received even if the callback is asynchronous.
3209 MockDataChannelObserver new_observer(callee()->data_channel());
3210 EXPECT_EQ_SIMULATED_WAIT(data, new_observer.last_message(), kDefaultTimeout,
3211 fake_clock);
Seth Hampson1d4a76d2018-06-19 14:31:41 -07003212 // Closing the PeerConnections destroys the ports before the ScopedFakeClock.
3213 // If this is not done a DCHECK can be hit in ports.cc, because a large
3214 // negative number is calculated for the rtt due to the global clock changing.
Steve Antond91969e2019-05-30 12:27:03 -07003215 ClosePeerConnections();
deadbeef1dcb1642017-03-29 21:08:16 -07003216}
3217
3218// This test sets up a call between two parties with audio, video and but only
3219// the caller client supports RTP data channels.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003220TEST_P(PeerConnectionIntegrationTest, RtpDataChannelsRejectedByCallee) {
Niels Möllerf06f9232018-08-07 12:32:18 +02003221 PeerConnectionInterface::RTCConfiguration rtc_config_1;
3222 rtc_config_1.enable_rtp_data_channel = true;
deadbeef1dcb1642017-03-29 21:08:16 -07003223 // Must disable DTLS to make negotiation succeed.
Niels Möllerf06f9232018-08-07 12:32:18 +02003224 rtc_config_1.enable_dtls_srtp = false;
3225 PeerConnectionInterface::RTCConfiguration rtc_config_2;
3226 rtc_config_2.enable_dtls_srtp = false;
3227 rtc_config_2.enable_dtls_srtp = false;
3228 ASSERT_TRUE(
3229 CreatePeerConnectionWrappersWithConfig(rtc_config_1, rtc_config_2));
deadbeef1dcb1642017-03-29 21:08:16 -07003230 ConnectFakeSignaling();
3231 caller()->CreateDataChannel();
Harald Alvestrandf3736ed2019-04-08 13:09:30 +02003232 ASSERT_TRUE(caller()->data_channel() != nullptr);
Steve Anton15324772018-01-16 10:26:49 -08003233 caller()->AddAudioVideoTracks();
3234 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003235 caller()->CreateAndSetAndSignalOffer();
3236 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3237 // The caller should still have a data channel, but it should be closed, and
3238 // one should ever have been created for the callee.
3239 EXPECT_TRUE(caller()->data_channel() != nullptr);
3240 EXPECT_FALSE(caller()->data_observer()->IsOpen());
3241 EXPECT_EQ(nullptr, callee()->data_channel());
3242}
3243
3244// This test sets up a call between two parties with audio, and video. When
3245// audio and video is setup and flowing, an RTP data channel is negotiated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003246TEST_P(PeerConnectionIntegrationTest, AddRtpDataChannelInSubsequentOffer) {
Niels Möllerf06f9232018-08-07 12:32:18 +02003247 PeerConnectionInterface::RTCConfiguration rtc_config;
3248 rtc_config.enable_rtp_data_channel = true;
3249 rtc_config.enable_dtls_srtp = false;
3250 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(rtc_config, rtc_config));
deadbeef1dcb1642017-03-29 21:08:16 -07003251 ConnectFakeSignaling();
3252 // Do initial offer/answer with audio/video.
Steve Anton15324772018-01-16 10:26:49 -08003253 caller()->AddAudioVideoTracks();
3254 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003255 caller()->CreateAndSetAndSignalOffer();
3256 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3257 // Create data channel and do new offer and answer.
3258 caller()->CreateDataChannel();
3259 caller()->CreateAndSetAndSignalOffer();
3260 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3261 ASSERT_NE(nullptr, caller()->data_channel());
3262 ASSERT_NE(nullptr, callee()->data_channel());
3263 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3264 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3265 // Ensure data can be sent in both directions.
3266 std::string data = "hello world";
3267 SendRtpDataWithRetries(caller()->data_channel(), data, 5);
3268 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3269 kDefaultTimeout);
3270 SendRtpDataWithRetries(callee()->data_channel(), data, 5);
3271 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3272 kDefaultTimeout);
3273}
3274
3275#ifdef HAVE_SCTP
3276
3277// This test sets up a call between two parties with audio, video and an SCTP
3278// data channel.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003279TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithSctpDataChannel) {
deadbeef1dcb1642017-03-29 21:08:16 -07003280 ASSERT_TRUE(CreatePeerConnectionWrappers());
3281 ConnectFakeSignaling();
3282 // Expect that data channel created on caller side will show up for callee as
3283 // well.
3284 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08003285 caller()->AddAudioVideoTracks();
3286 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003287 caller()->CreateAndSetAndSignalOffer();
3288 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3289 // Ensure the existence of the SCTP data channel didn't impede audio/video.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003290 MediaExpectations media_expectations;
3291 media_expectations.ExpectBidirectionalAudioAndVideo();
3292 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003293 // Caller data channel should already exist (it created one). Callee data
3294 // channel may not exist yet, since negotiation happens in-band, not in SDP.
3295 ASSERT_NE(nullptr, caller()->data_channel());
3296 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3297 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3298 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3299
3300 // Ensure data can be sent in both directions.
3301 std::string data = "hello world";
3302 caller()->data_channel()->Send(DataBuffer(data));
3303 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3304 kDefaultTimeout);
3305 callee()->data_channel()->Send(DataBuffer(data));
3306 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3307 kDefaultTimeout);
3308}
3309
3310// Ensure that when the callee closes an SCTP data channel, the closing
3311// procedure results in the data channel being closed for the caller as well.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003312TEST_P(PeerConnectionIntegrationTest, CalleeClosesSctpDataChannel) {
deadbeef1dcb1642017-03-29 21:08:16 -07003313 // Same procedure as above test.
3314 ASSERT_TRUE(CreatePeerConnectionWrappers());
3315 ConnectFakeSignaling();
3316 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08003317 caller()->AddAudioVideoTracks();
3318 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003319 caller()->CreateAndSetAndSignalOffer();
3320 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3321 ASSERT_NE(nullptr, caller()->data_channel());
3322 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3323 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3324 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3325
3326 // Close the data channel on the callee side, and wait for it to reach the
3327 // "closed" state on both sides.
3328 callee()->data_channel()->Close();
3329 EXPECT_TRUE_WAIT(!caller()->data_observer()->IsOpen(), kDefaultTimeout);
3330 EXPECT_TRUE_WAIT(!callee()->data_observer()->IsOpen(), kDefaultTimeout);
3331}
3332
Seth Hampson2f0d7022018-02-20 11:54:42 -08003333TEST_P(PeerConnectionIntegrationTest, SctpDataChannelConfigSentToOtherSide) {
Steve Antonda6c0952017-10-23 11:41:54 -07003334 ASSERT_TRUE(CreatePeerConnectionWrappers());
3335 ConnectFakeSignaling();
3336 webrtc::DataChannelInit init;
3337 init.id = 53;
3338 init.maxRetransmits = 52;
3339 caller()->CreateDataChannel("data-channel", &init);
Steve Anton15324772018-01-16 10:26:49 -08003340 caller()->AddAudioVideoTracks();
3341 callee()->AddAudioVideoTracks();
Steve Antonda6c0952017-10-23 11:41:54 -07003342 caller()->CreateAndSetAndSignalOffer();
3343 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Steve Anton074dece2017-10-24 13:04:12 -07003344 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3345 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
Harald Alvestrand5c4d2ee2019-04-01 12:58:15 +02003346 // Since "negotiated" is false, the "id" parameter should be ignored.
3347 EXPECT_NE(init.id, callee()->data_channel()->id());
Steve Antonda6c0952017-10-23 11:41:54 -07003348 EXPECT_EQ("data-channel", callee()->data_channel()->label());
3349 EXPECT_EQ(init.maxRetransmits, callee()->data_channel()->maxRetransmits());
3350 EXPECT_FALSE(callee()->data_channel()->negotiated());
3351}
3352
deadbeef1dcb1642017-03-29 21:08:16 -07003353// Test usrsctp's ability to process unordered data stream, where data actually
3354// arrives out of order using simulated delays. Previously there have been some
3355// bugs in this area.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003356TEST_P(PeerConnectionIntegrationTest, StressTestUnorderedSctpDataChannel) {
deadbeef1dcb1642017-03-29 21:08:16 -07003357 // Introduce random network delays.
3358 // Otherwise it's not a true "unordered" test.
3359 virtual_socket_server()->set_delay_mean(20);
3360 virtual_socket_server()->set_delay_stddev(5);
3361 virtual_socket_server()->UpdateDelayDistribution();
3362 // Normal procedure, but with unordered data channel config.
3363 ASSERT_TRUE(CreatePeerConnectionWrappers());
3364 ConnectFakeSignaling();
3365 webrtc::DataChannelInit init;
3366 init.ordered = false;
3367 caller()->CreateDataChannel(&init);
3368 caller()->CreateAndSetAndSignalOffer();
3369 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3370 ASSERT_NE(nullptr, caller()->data_channel());
3371 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3372 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3373 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3374
3375 static constexpr int kNumMessages = 100;
3376 // Deliberately chosen to be larger than the MTU so messages get fragmented.
3377 static constexpr size_t kMaxMessageSize = 4096;
3378 // Create and send random messages.
3379 std::vector<std::string> sent_messages;
3380 for (int i = 0; i < kNumMessages; ++i) {
3381 size_t length =
3382 (rand() % kMaxMessageSize) + 1; // NOLINT (rand_r instead of rand)
3383 std::string message;
3384 ASSERT_TRUE(rtc::CreateRandomString(length, &message));
3385 caller()->data_channel()->Send(DataBuffer(message));
3386 callee()->data_channel()->Send(DataBuffer(message));
3387 sent_messages.push_back(message);
3388 }
3389
3390 // Wait for all messages to be received.
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02003391 EXPECT_EQ_WAIT(rtc::checked_cast<size_t>(kNumMessages),
deadbeef1dcb1642017-03-29 21:08:16 -07003392 caller()->data_observer()->received_message_count(),
3393 kDefaultTimeout);
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02003394 EXPECT_EQ_WAIT(rtc::checked_cast<size_t>(kNumMessages),
deadbeef1dcb1642017-03-29 21:08:16 -07003395 callee()->data_observer()->received_message_count(),
3396 kDefaultTimeout);
3397
3398 // Sort and compare to make sure none of the messages were corrupted.
3399 std::vector<std::string> caller_received_messages =
3400 caller()->data_observer()->messages();
3401 std::vector<std::string> callee_received_messages =
3402 callee()->data_observer()->messages();
Steve Anton64b626b2019-01-28 17:25:26 -08003403 absl::c_sort(sent_messages);
3404 absl::c_sort(caller_received_messages);
3405 absl::c_sort(callee_received_messages);
deadbeef1dcb1642017-03-29 21:08:16 -07003406 EXPECT_EQ(sent_messages, caller_received_messages);
3407 EXPECT_EQ(sent_messages, callee_received_messages);
3408}
3409
3410// This test sets up a call between two parties with audio, and video. When
3411// audio and video are setup and flowing, an SCTP data channel is negotiated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003412TEST_P(PeerConnectionIntegrationTest, AddSctpDataChannelInSubsequentOffer) {
deadbeef1dcb1642017-03-29 21:08:16 -07003413 ASSERT_TRUE(CreatePeerConnectionWrappers());
3414 ConnectFakeSignaling();
3415 // Do initial offer/answer with audio/video.
Steve Anton15324772018-01-16 10:26:49 -08003416 caller()->AddAudioVideoTracks();
3417 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003418 caller()->CreateAndSetAndSignalOffer();
3419 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3420 // Create data channel and do new offer and answer.
3421 caller()->CreateDataChannel();
3422 caller()->CreateAndSetAndSignalOffer();
3423 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3424 // Caller data channel should already exist (it created one). Callee data
3425 // channel may not exist yet, since negotiation happens in-band, not in SDP.
3426 ASSERT_NE(nullptr, caller()->data_channel());
3427 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3428 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3429 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3430 // Ensure data can be sent in both directions.
3431 std::string data = "hello world";
3432 caller()->data_channel()->Send(DataBuffer(data));
3433 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3434 kDefaultTimeout);
3435 callee()->data_channel()->Send(DataBuffer(data));
3436 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3437 kDefaultTimeout);
3438}
3439
deadbeef7914b8c2017-04-21 03:23:33 -07003440// Set up a connection initially just using SCTP data channels, later upgrading
3441// to audio/video, ensuring frames are received end-to-end. Effectively the
3442// inverse of the test above.
3443// This was broken in M57; see https://crbug.com/711243
Seth Hampson2f0d7022018-02-20 11:54:42 -08003444TEST_P(PeerConnectionIntegrationTest, SctpDataChannelToAudioVideoUpgrade) {
deadbeef7914b8c2017-04-21 03:23:33 -07003445 ASSERT_TRUE(CreatePeerConnectionWrappers());
3446 ConnectFakeSignaling();
3447 // Do initial offer/answer with just data channel.
3448 caller()->CreateDataChannel();
3449 caller()->CreateAndSetAndSignalOffer();
3450 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3451 // Wait until data can be sent over the data channel.
3452 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3453 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3454 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3455
3456 // Do subsequent offer/answer with two-way audio and video. Audio and video
3457 // should end up bundled on the DTLS/ICE transport already used for data.
Steve Anton15324772018-01-16 10:26:49 -08003458 caller()->AddAudioVideoTracks();
3459 callee()->AddAudioVideoTracks();
deadbeef7914b8c2017-04-21 03:23:33 -07003460 caller()->CreateAndSetAndSignalOffer();
3461 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08003462 MediaExpectations media_expectations;
3463 media_expectations.ExpectBidirectionalAudioAndVideo();
3464 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef7914b8c2017-04-21 03:23:33 -07003465}
3466
deadbeef8b7e9ad2017-05-25 09:38:55 -07003467static void MakeSpecCompliantSctpOffer(cricket::SessionDescription* desc) {
Harald Alvestrand5fc28b12019-05-13 13:36:16 +02003468 cricket::SctpDataContentDescription* dcd_offer =
3469 GetFirstSctpDataContentDescription(desc);
Steve Antonb1c1de12017-12-21 15:14:30 -08003470 ASSERT_TRUE(dcd_offer);
deadbeef8b7e9ad2017-05-25 09:38:55 -07003471 dcd_offer->set_use_sctpmap(false);
3472 dcd_offer->set_protocol("UDP/DTLS/SCTP");
3473}
3474
3475// Test that the data channel works when a spec-compliant SCTP m= section is
3476// offered (using "a=sctp-port" instead of "a=sctpmap", and using
3477// "UDP/DTLS/SCTP" as the protocol).
Seth Hampson2f0d7022018-02-20 11:54:42 -08003478TEST_P(PeerConnectionIntegrationTest,
deadbeef8b7e9ad2017-05-25 09:38:55 -07003479 DataChannelWorksWhenSpecCompliantSctpOfferReceived) {
3480 ASSERT_TRUE(CreatePeerConnectionWrappers());
3481 ConnectFakeSignaling();
3482 caller()->CreateDataChannel();
3483 caller()->SetGeneratedSdpMunger(MakeSpecCompliantSctpOffer);
3484 caller()->CreateAndSetAndSignalOffer();
3485 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3486 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3487 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3488 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3489
3490 // Ensure data can be sent in both directions.
3491 std::string data = "hello world";
3492 caller()->data_channel()->Send(DataBuffer(data));
3493 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3494 kDefaultTimeout);
3495 callee()->data_channel()->Send(DataBuffer(data));
3496 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3497 kDefaultTimeout);
3498}
3499
deadbeef1dcb1642017-03-29 21:08:16 -07003500#endif // HAVE_SCTP
3501
Bjorn Mellema2eb0a72018-11-09 10:13:51 -08003502// This test sets up a call between two parties with a media transport data
3503// channel.
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08003504TEST_P(PeerConnectionIntegrationTest, MediaTransportDataChannelEndToEnd) {
3505 PeerConnectionInterface::RTCConfiguration rtc_config;
Piotr (Peter) Slatalab1ae10b2019-03-01 11:14:05 -08003506 rtc_config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyRequire;
3507 rtc_config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08003508 rtc_config.use_media_transport_for_data_channels = true;
3509 rtc_config.enable_dtls_srtp = false; // SDES is required for media transport.
3510 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
3511 rtc_config, rtc_config, loopback_media_transports()->first_factory(),
3512 loopback_media_transports()->second_factory()));
3513 ConnectFakeSignaling();
3514
3515 // Expect that data channel created on caller side will show up for callee as
3516 // well.
3517 caller()->CreateDataChannel();
3518 caller()->CreateAndSetAndSignalOffer();
3519 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3520
3521 // Ensure that the media transport is ready.
3522 loopback_media_transports()->SetState(webrtc::MediaTransportState::kWritable);
3523 loopback_media_transports()->FlushAsyncInvokes();
3524
3525 // Caller data channel should already exist (it created one). Callee data
3526 // channel may not exist yet, since negotiation happens in-band, not in SDP.
3527 ASSERT_NE(nullptr, caller()->data_channel());
3528 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3529 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3530 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3531
3532 // Ensure data can be sent in both directions.
3533 std::string data = "hello world";
3534 caller()->data_channel()->Send(DataBuffer(data));
3535 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3536 kDefaultTimeout);
3537 callee()->data_channel()->Send(DataBuffer(data));
3538 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3539 kDefaultTimeout);
3540}
3541
3542// Ensure that when the callee closes a media transport data channel, the
3543// closing procedure results in the data channel being closed for the caller
3544// as well.
3545TEST_P(PeerConnectionIntegrationTest, MediaTransportDataChannelCalleeCloses) {
3546 PeerConnectionInterface::RTCConfiguration rtc_config;
3547 rtc_config.use_media_transport_for_data_channels = true;
3548 rtc_config.enable_dtls_srtp = false; // SDES is required for media transport.
3549 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
3550 rtc_config, rtc_config, loopback_media_transports()->first_factory(),
3551 loopback_media_transports()->second_factory()));
3552 ConnectFakeSignaling();
3553
3554 // Create a data channel on the caller and signal it to the callee.
3555 caller()->CreateDataChannel();
3556 caller()->CreateAndSetAndSignalOffer();
3557 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3558
3559 // Ensure that the media transport is ready.
3560 loopback_media_transports()->SetState(webrtc::MediaTransportState::kWritable);
3561 loopback_media_transports()->FlushAsyncInvokes();
3562
3563 // Data channels exist and open on both ends of the connection.
3564 ASSERT_NE(nullptr, caller()->data_channel());
3565 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3566 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3567 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3568
3569 // Close the data channel on the callee side, and wait for it to reach the
3570 // "closed" state on both sides.
3571 callee()->data_channel()->Close();
3572 EXPECT_TRUE_WAIT(!caller()->data_observer()->IsOpen(), kDefaultTimeout);
3573 EXPECT_TRUE_WAIT(!callee()->data_observer()->IsOpen(), kDefaultTimeout);
3574}
3575
3576TEST_P(PeerConnectionIntegrationTest,
3577 MediaTransportDataChannelConfigSentToOtherSide) {
3578 PeerConnectionInterface::RTCConfiguration rtc_config;
3579 rtc_config.use_media_transport_for_data_channels = true;
3580 rtc_config.enable_dtls_srtp = false; // SDES is required for media transport.
3581 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
3582 rtc_config, rtc_config, loopback_media_transports()->first_factory(),
3583 loopback_media_transports()->second_factory()));
3584 ConnectFakeSignaling();
3585
3586 // Create a data channel with a non-default configuration and signal it to the
3587 // callee.
3588 webrtc::DataChannelInit init;
3589 init.id = 53;
3590 init.maxRetransmits = 52;
3591 caller()->CreateDataChannel("data-channel", &init);
3592 caller()->CreateAndSetAndSignalOffer();
3593 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3594
3595 // Ensure that the media transport is ready.
3596 loopback_media_transports()->SetState(webrtc::MediaTransportState::kWritable);
3597 loopback_media_transports()->FlushAsyncInvokes();
3598
3599 // Ensure that the data channel exists on the callee with the correct
3600 // configuration.
3601 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3602 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
Harald Alvestrand5c4d2ee2019-04-01 12:58:15 +02003603 // Since "negotiate" is false, the "id" parameter is ignored.
3604 EXPECT_NE(init.id, callee()->data_channel()->id());
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08003605 EXPECT_EQ("data-channel", callee()->data_channel()->label());
3606 EXPECT_EQ(init.maxRetransmits, callee()->data_channel()->maxRetransmits());
3607 EXPECT_FALSE(callee()->data_channel()->negotiated());
3608}
3609
Piotr (Peter) Slatalab1ae10b2019-03-01 11:14:05 -08003610TEST_P(PeerConnectionIntegrationTest, MediaTransportOfferUpgrade) {
3611 PeerConnectionInterface::RTCConfiguration rtc_config;
3612 rtc_config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyRequire;
3613 rtc_config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
3614 rtc_config.use_media_transport = true;
3615 rtc_config.enable_dtls_srtp = false; // SDES is required for media transport.
3616 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
3617 rtc_config, rtc_config, loopback_media_transports()->first_factory(),
3618 loopback_media_transports()->second_factory()));
3619 ConnectFakeSignaling();
3620
3621 // Do initial offer/answer with just a video track.
3622 caller()->AddVideoTrack();
3623 callee()->AddVideoTrack();
3624 caller()->CreateAndSetAndSignalOffer();
3625 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3626
3627 // Ensure that the media transport is ready.
3628 loopback_media_transports()->SetState(webrtc::MediaTransportState::kWritable);
3629 loopback_media_transports()->FlushAsyncInvokes();
3630
3631 // Now add an audio track and do another offer/answer.
3632 caller()->AddAudioTrack();
3633 callee()->AddAudioTrack();
3634 caller()->CreateAndSetAndSignalOffer();
3635 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3636
3637 // Ensure both audio and video frames are received end-to-end.
3638 MediaExpectations media_expectations;
3639 media_expectations.ExpectBidirectionalAudioAndVideo();
3640 ASSERT_TRUE(ExpectNewFrames(media_expectations));
3641
3642 // The second offer should not have generated another media transport.
3643 // Media transport was kept alive, and was not recreated.
3644 EXPECT_EQ(1, loopback_media_transports()->first_factory_transport_count());
3645 EXPECT_EQ(1, loopback_media_transports()->second_factory_transport_count());
3646}
3647
3648TEST_P(PeerConnectionIntegrationTest, MediaTransportOfferUpgradeOnTheCallee) {
3649 PeerConnectionInterface::RTCConfiguration rtc_config;
3650 rtc_config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyRequire;
3651 rtc_config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
3652 rtc_config.use_media_transport = true;
3653 rtc_config.enable_dtls_srtp = false; // SDES is required for media transport.
3654 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
3655 rtc_config, rtc_config, loopback_media_transports()->first_factory(),
3656 loopback_media_transports()->second_factory()));
3657 ConnectFakeSignaling();
3658
3659 // Do initial offer/answer with just a video track.
3660 caller()->AddVideoTrack();
3661 callee()->AddVideoTrack();
3662 caller()->CreateAndSetAndSignalOffer();
3663 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3664
3665 // Ensure that the media transport is ready.
3666 loopback_media_transports()->SetState(webrtc::MediaTransportState::kWritable);
3667 loopback_media_transports()->FlushAsyncInvokes();
3668
3669 // Now add an audio track and do another offer/answer.
3670 caller()->AddAudioTrack();
3671 callee()->AddAudioTrack();
3672 callee()->CreateAndSetAndSignalOffer();
3673 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3674
3675 // Ensure both audio and video frames are received end-to-end.
3676 MediaExpectations media_expectations;
3677 media_expectations.ExpectBidirectionalAudioAndVideo();
3678 ASSERT_TRUE(ExpectNewFrames(media_expectations));
3679
3680 // The second offer should not have generated another media transport.
3681 // Media transport was kept alive, and was not recreated.
3682 EXPECT_EQ(1, loopback_media_transports()->first_factory_transport_count());
3683 EXPECT_EQ(1, loopback_media_transports()->second_factory_transport_count());
3684}
3685
Niels Möllerc68d2822018-11-20 14:52:05 +01003686TEST_P(PeerConnectionIntegrationTest, MediaTransportBidirectionalAudio) {
3687 PeerConnectionInterface::RTCConfiguration rtc_config;
Piotr (Peter) Slatalab1ae10b2019-03-01 11:14:05 -08003688 rtc_config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyRequire;
3689 rtc_config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
Niels Möllerc68d2822018-11-20 14:52:05 +01003690 rtc_config.use_media_transport = true;
3691 rtc_config.enable_dtls_srtp = false; // SDES is required for media transport.
3692 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
3693 rtc_config, rtc_config, loopback_media_transports()->first_factory(),
3694 loopback_media_transports()->second_factory()));
3695 ConnectFakeSignaling();
3696
3697 caller()->AddAudioTrack();
3698 callee()->AddAudioTrack();
3699 // Start offer/answer exchange and wait for it to complete.
3700 caller()->CreateAndSetAndSignalOffer();
3701 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3702
3703 // Ensure that the media transport is ready.
3704 loopback_media_transports()->SetState(webrtc::MediaTransportState::kWritable);
3705 loopback_media_transports()->FlushAsyncInvokes();
3706
3707 MediaExpectations media_expectations;
3708 media_expectations.ExpectBidirectionalAudio();
3709 ASSERT_TRUE(ExpectNewFrames(media_expectations));
3710
3711 webrtc::MediaTransportPair::Stats first_stats =
3712 loopback_media_transports()->FirstStats();
3713 webrtc::MediaTransportPair::Stats second_stats =
3714 loopback_media_transports()->SecondStats();
3715
3716 EXPECT_GT(first_stats.received_audio_frames, 0);
3717 EXPECT_GE(second_stats.sent_audio_frames, first_stats.received_audio_frames);
3718
3719 EXPECT_GT(second_stats.received_audio_frames, 0);
3720 EXPECT_GE(first_stats.sent_audio_frames, second_stats.received_audio_frames);
3721}
3722
Niels Möller46879152019-01-07 15:54:47 +01003723TEST_P(PeerConnectionIntegrationTest, MediaTransportBidirectionalVideo) {
3724 PeerConnectionInterface::RTCConfiguration rtc_config;
3725 rtc_config.use_media_transport = true;
3726 rtc_config.enable_dtls_srtp = false; // SDES is required for media transport.
3727 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
3728 rtc_config, rtc_config, loopback_media_transports()->first_factory(),
3729 loopback_media_transports()->second_factory()));
3730 ConnectFakeSignaling();
3731
3732 caller()->AddVideoTrack();
3733 callee()->AddVideoTrack();
3734 // Start offer/answer exchange and wait for it to complete.
3735 caller()->CreateAndSetAndSignalOffer();
3736 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3737
3738 // Ensure that the media transport is ready.
3739 loopback_media_transports()->SetState(webrtc::MediaTransportState::kWritable);
3740 loopback_media_transports()->FlushAsyncInvokes();
3741
3742 MediaExpectations media_expectations;
3743 media_expectations.ExpectBidirectionalVideo();
3744 ASSERT_TRUE(ExpectNewFrames(media_expectations));
3745
3746 webrtc::MediaTransportPair::Stats first_stats =
3747 loopback_media_transports()->FirstStats();
3748 webrtc::MediaTransportPair::Stats second_stats =
3749 loopback_media_transports()->SecondStats();
3750
3751 EXPECT_GT(first_stats.received_video_frames, 0);
3752 EXPECT_GE(second_stats.sent_video_frames, first_stats.received_video_frames);
3753
3754 EXPECT_GT(second_stats.received_video_frames, 0);
3755 EXPECT_GE(first_stats.sent_video_frames, second_stats.received_video_frames);
3756}
3757
Piotr (Peter) Slatala55b91b92019-01-25 13:31:15 -08003758TEST_P(PeerConnectionIntegrationTest,
3759 MediaTransportDataChannelUsesRtpBidirectionalVideo) {
3760 PeerConnectionInterface::RTCConfiguration rtc_config;
3761 rtc_config.use_media_transport = false;
3762 rtc_config.use_media_transport_for_data_channels = true;
3763 rtc_config.enable_dtls_srtp = false; // SDES is required for media transport.
3764 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
3765 rtc_config, rtc_config, loopback_media_transports()->first_factory(),
3766 loopback_media_transports()->second_factory()));
3767 ConnectFakeSignaling();
3768
3769 caller()->AddVideoTrack();
3770 callee()->AddVideoTrack();
3771 // Start offer/answer exchange and wait for it to complete.
3772 caller()->CreateAndSetAndSignalOffer();
3773 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3774
3775 MediaExpectations media_expectations;
3776 media_expectations.ExpectBidirectionalVideo();
3777 ASSERT_TRUE(ExpectNewFrames(media_expectations));
3778}
3779
deadbeef1dcb1642017-03-29 21:08:16 -07003780// Test that the ICE connection and gathering states eventually reach
3781// "complete".
Seth Hampson2f0d7022018-02-20 11:54:42 -08003782TEST_P(PeerConnectionIntegrationTest, IceStatesReachCompletion) {
deadbeef1dcb1642017-03-29 21:08:16 -07003783 ASSERT_TRUE(CreatePeerConnectionWrappers());
3784 ConnectFakeSignaling();
3785 // Do normal offer/answer.
Steve Anton15324772018-01-16 10:26:49 -08003786 caller()->AddAudioVideoTracks();
3787 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003788 caller()->CreateAndSetAndSignalOffer();
3789 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3790 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceGatheringComplete,
3791 caller()->ice_gathering_state(), kMaxWaitForFramesMs);
3792 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceGatheringComplete,
3793 callee()->ice_gathering_state(), kMaxWaitForFramesMs);
3794 // After the best candidate pair is selected and all candidates are signaled,
3795 // the ICE connection state should reach "complete".
3796 // TODO(deadbeef): Currently, the ICE "controlled" agent (the
3797 // answerer/"callee" by default) only reaches "connected". When this is
3798 // fixed, this test should be updated.
3799 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
3800 caller()->ice_connection_state(), kDefaultTimeout);
Alex Loiko9289eda2018-11-23 16:18:59 +00003801 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
3802 callee()->ice_connection_state(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07003803}
3804
Qingsi Wang1dac6d82018-12-12 15:28:47 -08003805constexpr int kOnlyLocalPorts = cricket::PORTALLOCATOR_DISABLE_STUN |
3806 cricket::PORTALLOCATOR_DISABLE_RELAY |
3807 cricket::PORTALLOCATOR_DISABLE_TCP;
Zach Stein6fcdc2f2018-08-23 16:25:55 -07003808
Qingsi Wang1dac6d82018-12-12 15:28:47 -08003809// Use a mock resolver to resolve the hostname back to the original IP on both
3810// sides and check that the ICE connection connects.
Zach Stein6fcdc2f2018-08-23 16:25:55 -07003811TEST_P(PeerConnectionIntegrationTest,
3812 IceStatesReachCompletionWithRemoteHostname) {
Qingsi Wang1dac6d82018-12-12 15:28:47 -08003813 auto caller_resolver_factory =
3814 absl::make_unique<NiceMock<webrtc::MockAsyncResolverFactory>>();
3815 auto callee_resolver_factory =
3816 absl::make_unique<NiceMock<webrtc::MockAsyncResolverFactory>>();
3817 NiceMock<rtc::MockAsyncResolver> callee_async_resolver;
3818 NiceMock<rtc::MockAsyncResolver> caller_async_resolver;
Zach Stein6fcdc2f2018-08-23 16:25:55 -07003819
3820 // This also verifies that the injected AsyncResolverFactory is used by
3821 // P2PTransportChannel.
Qingsi Wang1dac6d82018-12-12 15:28:47 -08003822 EXPECT_CALL(*caller_resolver_factory, Create())
3823 .WillOnce(Return(&caller_async_resolver));
3824 webrtc::PeerConnectionDependencies caller_deps(nullptr);
3825 caller_deps.async_resolver_factory = std::move(caller_resolver_factory);
3826
3827 EXPECT_CALL(*callee_resolver_factory, Create())
3828 .WillOnce(Return(&callee_async_resolver));
3829 webrtc::PeerConnectionDependencies callee_deps(nullptr);
3830 callee_deps.async_resolver_factory = std::move(callee_resolver_factory);
3831
3832 PeerConnectionInterface::RTCConfiguration config;
3833 config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
3834 config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyRequire;
3835
3836 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndDeps(
3837 config, std::move(caller_deps), config, std::move(callee_deps)));
3838
3839 caller()->SetRemoteAsyncResolver(&callee_async_resolver);
3840 callee()->SetRemoteAsyncResolver(&caller_async_resolver);
3841
3842 // Enable hostname candidates with mDNS names.
Qingsi Wangecd30542019-05-22 14:34:56 -07003843 caller()->SetMdnsResponder(
3844 absl::make_unique<webrtc::FakeMdnsResponder>(network_thread()));
3845 callee()->SetMdnsResponder(
3846 absl::make_unique<webrtc::FakeMdnsResponder>(network_thread()));
Qingsi Wang1dac6d82018-12-12 15:28:47 -08003847
3848 SetPortAllocatorFlags(kOnlyLocalPorts, kOnlyLocalPorts);
Zach Stein6fcdc2f2018-08-23 16:25:55 -07003849
3850 ConnectFakeSignaling();
3851 caller()->AddAudioVideoTracks();
3852 callee()->AddAudioVideoTracks();
3853 caller()->CreateAndSetAndSignalOffer();
3854 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3855 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
3856 caller()->ice_connection_state(), kDefaultTimeout);
3857 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
3858 callee()->ice_connection_state(), kDefaultTimeout);
Jeroen de Borst833979f2018-12-13 08:25:54 -08003859
3860 EXPECT_EQ(1, webrtc::metrics::NumEvents(
3861 "WebRTC.PeerConnection.CandidatePairType_UDP",
3862 webrtc::kIceCandidatePairHostNameHostName));
Zach Stein6fcdc2f2018-08-23 16:25:55 -07003863}
3864
Steve Antonede9ca52017-10-16 13:04:27 -07003865// Test that firewalling the ICE connection causes the clients to identify the
3866// disconnected state and then removing the firewall causes them to reconnect.
3867class PeerConnectionIntegrationIceStatesTest
Seth Hampson2f0d7022018-02-20 11:54:42 -08003868 : public PeerConnectionIntegrationBaseTest,
3869 public ::testing::WithParamInterface<
3870 std::tuple<SdpSemantics, std::tuple<std::string, uint32_t>>> {
Steve Antonede9ca52017-10-16 13:04:27 -07003871 protected:
Seth Hampson2f0d7022018-02-20 11:54:42 -08003872 PeerConnectionIntegrationIceStatesTest()
3873 : PeerConnectionIntegrationBaseTest(std::get<0>(GetParam())) {
3874 port_allocator_flags_ = std::get<1>(std::get<1>(GetParam()));
Steve Antonede9ca52017-10-16 13:04:27 -07003875 }
3876
3877 void StartStunServer(const SocketAddress& server_address) {
3878 stun_server_.reset(
3879 cricket::TestStunServer::Create(network_thread(), server_address));
3880 }
3881
3882 bool TestIPv6() {
3883 return (port_allocator_flags_ & cricket::PORTALLOCATOR_ENABLE_IPV6);
3884 }
3885
3886 void SetPortAllocatorFlags() {
Qingsi Wang1dac6d82018-12-12 15:28:47 -08003887 PeerConnectionIntegrationBaseTest::SetPortAllocatorFlags(
3888 port_allocator_flags_, port_allocator_flags_);
Steve Antonede9ca52017-10-16 13:04:27 -07003889 }
3890
3891 std::vector<SocketAddress> CallerAddresses() {
3892 std::vector<SocketAddress> addresses;
3893 addresses.push_back(SocketAddress("1.1.1.1", 0));
3894 if (TestIPv6()) {
3895 addresses.push_back(SocketAddress("1111:0:a:b:c:d:e:f", 0));
3896 }
3897 return addresses;
3898 }
3899
3900 std::vector<SocketAddress> CalleeAddresses() {
3901 std::vector<SocketAddress> addresses;
3902 addresses.push_back(SocketAddress("2.2.2.2", 0));
3903 if (TestIPv6()) {
3904 addresses.push_back(SocketAddress("2222:0:a:b:c:d:e:f", 0));
3905 }
3906 return addresses;
3907 }
3908
3909 void SetUpNetworkInterfaces() {
3910 // Remove the default interfaces added by the test infrastructure.
Qingsi Wangecd30542019-05-22 14:34:56 -07003911 caller()->network_manager()->RemoveInterface(kDefaultLocalAddress);
3912 callee()->network_manager()->RemoveInterface(kDefaultLocalAddress);
Steve Antonede9ca52017-10-16 13:04:27 -07003913
3914 // Add network addresses for test.
3915 for (const auto& caller_address : CallerAddresses()) {
Qingsi Wangecd30542019-05-22 14:34:56 -07003916 caller()->network_manager()->AddInterface(caller_address);
Steve Antonede9ca52017-10-16 13:04:27 -07003917 }
3918 for (const auto& callee_address : CalleeAddresses()) {
Qingsi Wangecd30542019-05-22 14:34:56 -07003919 callee()->network_manager()->AddInterface(callee_address);
Steve Antonede9ca52017-10-16 13:04:27 -07003920 }
3921 }
3922
3923 private:
3924 uint32_t port_allocator_flags_;
3925 std::unique_ptr<cricket::TestStunServer> stun_server_;
3926};
3927
3928// Tests that the PeerConnection goes through all the ICE gathering/connection
3929// states over the duration of the call. This includes Disconnected and Failed
3930// states, induced by putting a firewall between the peers and waiting for them
3931// to time out.
Steve Anton83119dd2017-11-10 16:19:52 -08003932TEST_P(PeerConnectionIntegrationIceStatesTest, VerifyIceStates) {
Jonas Olssonb75d9e92019-02-22 10:33:29 +01003933 rtc::ScopedFakeClock fake_clock;
3934 // Some things use a time of "0" as a special value, so we need to start out
3935 // the fake clock at a nonzero time.
3936 fake_clock.AdvanceTime(TimeDelta::seconds(1));
Steve Antonede9ca52017-10-16 13:04:27 -07003937
3938 const SocketAddress kStunServerAddress =
3939 SocketAddress("99.99.99.1", cricket::STUN_SERVER_PORT);
3940 StartStunServer(kStunServerAddress);
3941
3942 PeerConnectionInterface::RTCConfiguration config;
3943 PeerConnectionInterface::IceServer ice_stun_server;
3944 ice_stun_server.urls.push_back(
3945 "stun:" + kStunServerAddress.HostAsURIString() + ":" +
3946 kStunServerAddress.PortAsString());
3947 config.servers.push_back(ice_stun_server);
3948
3949 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
3950 ConnectFakeSignaling();
3951 SetPortAllocatorFlags();
3952 SetUpNetworkInterfaces();
Steve Anton15324772018-01-16 10:26:49 -08003953 caller()->AddAudioVideoTracks();
3954 callee()->AddAudioVideoTracks();
Steve Antonede9ca52017-10-16 13:04:27 -07003955
3956 // Initial state before anything happens.
3957 ASSERT_EQ(PeerConnectionInterface::kIceGatheringNew,
3958 caller()->ice_gathering_state());
3959 ASSERT_EQ(PeerConnectionInterface::kIceConnectionNew,
3960 caller()->ice_connection_state());
Jonas Olsson7a6739e2019-01-15 16:31:55 +01003961 ASSERT_EQ(PeerConnectionInterface::kIceConnectionNew,
3962 caller()->standardized_ice_connection_state());
Steve Antonede9ca52017-10-16 13:04:27 -07003963
3964 // Start the call by creating the offer, setting it as the local description,
3965 // then sending it to the peer who will respond with an answer. This happens
3966 // asynchronously so that we can watch the states as it runs in the
3967 // background.
3968 caller()->CreateAndSetAndSignalOffer();
3969
Jonas Olsson7a6739e2019-01-15 16:31:55 +01003970 ASSERT_EQ(PeerConnectionInterface::kIceConnectionCompleted,
3971 caller()->ice_connection_state());
Jonas Olssonacd8ae72019-02-25 15:26:24 +01003972 ASSERT_EQ(PeerConnectionInterface::kIceConnectionCompleted,
Jonas Olsson7a6739e2019-01-15 16:31:55 +01003973 caller()->standardized_ice_connection_state());
Steve Antonede9ca52017-10-16 13:04:27 -07003974
3975 // Verify that the observer was notified of the intermediate transitions.
3976 EXPECT_THAT(caller()->ice_connection_state_history(),
3977 ElementsAre(PeerConnectionInterface::kIceConnectionChecking,
3978 PeerConnectionInterface::kIceConnectionConnected,
3979 PeerConnectionInterface::kIceConnectionCompleted));
Jonas Olssonacd8ae72019-02-25 15:26:24 +01003980 EXPECT_THAT(caller()->standardized_ice_connection_state_history(),
3981 ElementsAre(PeerConnectionInterface::kIceConnectionChecking,
3982 PeerConnectionInterface::kIceConnectionConnected,
3983 PeerConnectionInterface::kIceConnectionCompleted));
Jonas Olsson635474e2018-10-18 15:58:17 +02003984 EXPECT_THAT(
3985 caller()->peer_connection_state_history(),
3986 ElementsAre(PeerConnectionInterface::PeerConnectionState::kConnecting,
Jonas Olsson635474e2018-10-18 15:58:17 +02003987 PeerConnectionInterface::PeerConnectionState::kConnected));
Steve Antonede9ca52017-10-16 13:04:27 -07003988 EXPECT_THAT(caller()->ice_gathering_state_history(),
3989 ElementsAre(PeerConnectionInterface::kIceGatheringGathering,
3990 PeerConnectionInterface::kIceGatheringComplete));
3991
3992 // Block connections to/from the caller and wait for ICE to become
3993 // disconnected.
3994 for (const auto& caller_address : CallerAddresses()) {
3995 firewall()->AddRule(false, rtc::FP_ANY, rtc::FD_ANY, caller_address);
3996 }
Mirko Bonadei675513b2017-11-09 11:09:25 +01003997 RTC_LOG(LS_INFO) << "Firewall rules applied";
Jonas Olssonb75d9e92019-02-22 10:33:29 +01003998 ASSERT_EQ_SIMULATED_WAIT(PeerConnectionInterface::kIceConnectionDisconnected,
3999 caller()->ice_connection_state(), kDefaultTimeout,
4000 fake_clock);
4001 ASSERT_EQ_SIMULATED_WAIT(PeerConnectionInterface::kIceConnectionDisconnected,
4002 caller()->standardized_ice_connection_state(),
4003 kDefaultTimeout, fake_clock);
Steve Antonede9ca52017-10-16 13:04:27 -07004004
4005 // Let ICE re-establish by removing the firewall rules.
4006 firewall()->ClearRules();
Mirko Bonadei675513b2017-11-09 11:09:25 +01004007 RTC_LOG(LS_INFO) << "Firewall rules cleared";
Jonas Olssonb75d9e92019-02-22 10:33:29 +01004008 ASSERT_EQ_SIMULATED_WAIT(PeerConnectionInterface::kIceConnectionCompleted,
4009 caller()->ice_connection_state(), kDefaultTimeout,
4010 fake_clock);
Jonas Olssonacd8ae72019-02-25 15:26:24 +01004011 ASSERT_EQ_SIMULATED_WAIT(PeerConnectionInterface::kIceConnectionCompleted,
Jonas Olssonb75d9e92019-02-22 10:33:29 +01004012 caller()->standardized_ice_connection_state(),
4013 kDefaultTimeout, fake_clock);
Steve Antonede9ca52017-10-16 13:04:27 -07004014
4015 // According to RFC7675, if there is no response within 30 seconds then the
4016 // peer should consider the other side to have rejected the connection. This
Steve Anton83119dd2017-11-10 16:19:52 -08004017 // is signaled by the state transitioning to "failed".
Steve Antonede9ca52017-10-16 13:04:27 -07004018 constexpr int kConsentTimeout = 30000;
4019 for (const auto& caller_address : CallerAddresses()) {
4020 firewall()->AddRule(false, rtc::FP_ANY, rtc::FD_ANY, caller_address);
4021 }
Mirko Bonadei675513b2017-11-09 11:09:25 +01004022 RTC_LOG(LS_INFO) << "Firewall rules applied again";
Jonas Olssonb75d9e92019-02-22 10:33:29 +01004023 ASSERT_EQ_SIMULATED_WAIT(PeerConnectionInterface::kIceConnectionFailed,
4024 caller()->ice_connection_state(), kConsentTimeout,
4025 fake_clock);
4026 ASSERT_EQ_SIMULATED_WAIT(PeerConnectionInterface::kIceConnectionFailed,
4027 caller()->standardized_ice_connection_state(),
4028 kConsentTimeout, fake_clock);
4029
4030 // We need to manually close the peerconnections before the fake clock goes
4031 // out of scope, or we trigger a DCHECK in rtp_sender.cc when we briefly
4032 // return to using non-faked time.
4033 delete SetCallerPcWrapperAndReturnCurrent(nullptr);
4034 delete SetCalleePcWrapperAndReturnCurrent(nullptr);
4035}
4036
4037// Tests that if the connection doesn't get set up properly we eventually reach
4038// the "failed" iceConnectionState.
4039TEST_P(PeerConnectionIntegrationIceStatesTest, IceStateSetupFailure) {
4040 rtc::ScopedFakeClock fake_clock;
4041 // Some things use a time of "0" as a special value, so we need to start out
4042 // the fake clock at a nonzero time.
4043 fake_clock.AdvanceTime(TimeDelta::seconds(1));
4044
4045 // Block connections to/from the caller and wait for ICE to become
4046 // disconnected.
4047 for (const auto& caller_address : CallerAddresses()) {
4048 firewall()->AddRule(false, rtc::FP_ANY, rtc::FD_ANY, caller_address);
4049 }
4050
4051 ASSERT_TRUE(CreatePeerConnectionWrappers());
4052 ConnectFakeSignaling();
4053 SetPortAllocatorFlags();
4054 SetUpNetworkInterfaces();
4055 caller()->AddAudioVideoTracks();
4056 caller()->CreateAndSetAndSignalOffer();
4057
4058 // According to RFC7675, if there is no response within 30 seconds then the
4059 // peer should consider the other side to have rejected the connection. This
4060 // is signaled by the state transitioning to "failed".
4061 constexpr int kConsentTimeout = 30000;
4062 ASSERT_EQ_SIMULATED_WAIT(PeerConnectionInterface::kIceConnectionFailed,
4063 caller()->standardized_ice_connection_state(),
4064 kConsentTimeout, fake_clock);
4065
4066 // We need to manually close the peerconnections before the fake clock goes
4067 // out of scope, or we trigger a DCHECK in rtp_sender.cc when we briefly
4068 // return to using non-faked time.
4069 delete SetCallerPcWrapperAndReturnCurrent(nullptr);
4070 delete SetCalleePcWrapperAndReturnCurrent(nullptr);
Steve Antonede9ca52017-10-16 13:04:27 -07004071}
4072
4073// Tests that the best connection is set to the appropriate IPv4/IPv6 connection
4074// and that the statistics in the metric observers are updated correctly.
4075TEST_P(PeerConnectionIntegrationIceStatesTest, VerifyBestConnection) {
4076 ASSERT_TRUE(CreatePeerConnectionWrappers());
4077 ConnectFakeSignaling();
4078 SetPortAllocatorFlags();
4079 SetUpNetworkInterfaces();
Steve Anton15324772018-01-16 10:26:49 -08004080 caller()->AddAudioVideoTracks();
4081 callee()->AddAudioVideoTracks();
Steve Antonede9ca52017-10-16 13:04:27 -07004082 caller()->CreateAndSetAndSignalOffer();
4083
4084 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4085
Qingsi Wang7fc821d2018-07-12 12:54:53 -07004086 // TODO(bugs.webrtc.org/9456): Fix it.
4087 const int num_best_ipv4 = webrtc::metrics::NumEvents(
4088 "WebRTC.PeerConnection.IPMetrics", webrtc::kBestConnections_IPv4);
4089 const int num_best_ipv6 = webrtc::metrics::NumEvents(
4090 "WebRTC.PeerConnection.IPMetrics", webrtc::kBestConnections_IPv6);
Steve Antonede9ca52017-10-16 13:04:27 -07004091 if (TestIPv6()) {
4092 // When IPv6 is enabled, we should prefer an IPv6 connection over an IPv4
4093 // connection.
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02004094 EXPECT_EQ(0, num_best_ipv4);
4095 EXPECT_EQ(1, num_best_ipv6);
Steve Antonede9ca52017-10-16 13:04:27 -07004096 } else {
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02004097 EXPECT_EQ(1, num_best_ipv4);
4098 EXPECT_EQ(0, num_best_ipv6);
Steve Antonede9ca52017-10-16 13:04:27 -07004099 }
4100
Qingsi Wang7fc821d2018-07-12 12:54:53 -07004101 EXPECT_EQ(0, webrtc::metrics::NumEvents(
4102 "WebRTC.PeerConnection.CandidatePairType_UDP",
4103 webrtc::kIceCandidatePairHostHost));
4104 EXPECT_EQ(1, webrtc::metrics::NumEvents(
4105 "WebRTC.PeerConnection.CandidatePairType_UDP",
4106 webrtc::kIceCandidatePairHostPublicHostPublic));
Steve Antonede9ca52017-10-16 13:04:27 -07004107}
4108
4109constexpr uint32_t kFlagsIPv4NoStun = cricket::PORTALLOCATOR_DISABLE_TCP |
4110 cricket::PORTALLOCATOR_DISABLE_STUN |
4111 cricket::PORTALLOCATOR_DISABLE_RELAY;
4112constexpr uint32_t kFlagsIPv6NoStun =
4113 cricket::PORTALLOCATOR_DISABLE_TCP | cricket::PORTALLOCATOR_DISABLE_STUN |
4114 cricket::PORTALLOCATOR_ENABLE_IPV6 | cricket::PORTALLOCATOR_DISABLE_RELAY;
4115constexpr uint32_t kFlagsIPv4Stun =
4116 cricket::PORTALLOCATOR_DISABLE_TCP | cricket::PORTALLOCATOR_DISABLE_RELAY;
4117
Mirko Bonadeic84f6612019-01-31 12:20:57 +01004118INSTANTIATE_TEST_SUITE_P(
Seth Hampson2f0d7022018-02-20 11:54:42 -08004119 PeerConnectionIntegrationTest,
4120 PeerConnectionIntegrationIceStatesTest,
4121 Combine(Values(SdpSemantics::kPlanB, SdpSemantics::kUnifiedPlan),
4122 Values(std::make_pair("IPv4 no STUN", kFlagsIPv4NoStun),
4123 std::make_pair("IPv6 no STUN", kFlagsIPv6NoStun),
4124 std::make_pair("IPv4 with STUN", kFlagsIPv4Stun))));
Steve Antonede9ca52017-10-16 13:04:27 -07004125
deadbeef1dcb1642017-03-29 21:08:16 -07004126// This test sets up a call between two parties with audio and video.
4127// During the call, the caller restarts ICE and the test verifies that
4128// new ICE candidates are generated and audio and video still can flow, and the
4129// ICE state reaches completed again.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004130TEST_P(PeerConnectionIntegrationTest, MediaContinuesFlowingAfterIceRestart) {
deadbeef1dcb1642017-03-29 21:08:16 -07004131 ASSERT_TRUE(CreatePeerConnectionWrappers());
4132 ConnectFakeSignaling();
4133 // Do normal offer/answer and wait for ICE to complete.
Steve Anton15324772018-01-16 10:26:49 -08004134 caller()->AddAudioVideoTracks();
4135 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07004136 caller()->CreateAndSetAndSignalOffer();
4137 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4138 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
4139 caller()->ice_connection_state(), kMaxWaitForFramesMs);
Alex Loiko9289eda2018-11-23 16:18:59 +00004140 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
4141 callee()->ice_connection_state(), kMaxWaitForFramesMs);
deadbeef1dcb1642017-03-29 21:08:16 -07004142
4143 // To verify that the ICE restart actually occurs, get
4144 // ufrag/password/candidates before and after restart.
4145 // Create an SDP string of the first audio candidate for both clients.
4146 const webrtc::IceCandidateCollection* audio_candidates_caller =
4147 caller()->pc()->local_description()->candidates(0);
4148 const webrtc::IceCandidateCollection* audio_candidates_callee =
4149 callee()->pc()->local_description()->candidates(0);
4150 ASSERT_GT(audio_candidates_caller->count(), 0u);
4151 ASSERT_GT(audio_candidates_callee->count(), 0u);
4152 std::string caller_candidate_pre_restart;
4153 ASSERT_TRUE(
4154 audio_candidates_caller->at(0)->ToString(&caller_candidate_pre_restart));
4155 std::string callee_candidate_pre_restart;
4156 ASSERT_TRUE(
4157 audio_candidates_callee->at(0)->ToString(&callee_candidate_pre_restart));
4158 const cricket::SessionDescription* desc =
4159 caller()->pc()->local_description()->description();
4160 std::string caller_ufrag_pre_restart =
4161 desc->transport_infos()[0].description.ice_ufrag;
4162 desc = callee()->pc()->local_description()->description();
4163 std::string callee_ufrag_pre_restart =
4164 desc->transport_infos()[0].description.ice_ufrag;
4165
4166 // Have the caller initiate an ICE restart.
4167 caller()->SetOfferAnswerOptions(IceRestartOfferAnswerOptions());
4168 caller()->CreateAndSetAndSignalOffer();
4169 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4170 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
4171 caller()->ice_connection_state(), kMaxWaitForFramesMs);
Alex Loiko9289eda2018-11-23 16:18:59 +00004172 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
deadbeef1dcb1642017-03-29 21:08:16 -07004173 callee()->ice_connection_state(), kMaxWaitForFramesMs);
4174
4175 // Grab the ufrags/candidates again.
4176 audio_candidates_caller = caller()->pc()->local_description()->candidates(0);
4177 audio_candidates_callee = callee()->pc()->local_description()->candidates(0);
4178 ASSERT_GT(audio_candidates_caller->count(), 0u);
4179 ASSERT_GT(audio_candidates_callee->count(), 0u);
4180 std::string caller_candidate_post_restart;
4181 ASSERT_TRUE(
4182 audio_candidates_caller->at(0)->ToString(&caller_candidate_post_restart));
4183 std::string callee_candidate_post_restart;
4184 ASSERT_TRUE(
4185 audio_candidates_callee->at(0)->ToString(&callee_candidate_post_restart));
4186 desc = caller()->pc()->local_description()->description();
4187 std::string caller_ufrag_post_restart =
4188 desc->transport_infos()[0].description.ice_ufrag;
4189 desc = callee()->pc()->local_description()->description();
4190 std::string callee_ufrag_post_restart =
4191 desc->transport_infos()[0].description.ice_ufrag;
4192 // Sanity check that an ICE restart was actually negotiated in SDP.
4193 ASSERT_NE(caller_candidate_pre_restart, caller_candidate_post_restart);
4194 ASSERT_NE(callee_candidate_pre_restart, callee_candidate_post_restart);
4195 ASSERT_NE(caller_ufrag_pre_restart, caller_ufrag_post_restart);
4196 ASSERT_NE(callee_ufrag_pre_restart, callee_ufrag_post_restart);
4197
4198 // Ensure that additional frames are received after the ICE restart.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004199 MediaExpectations media_expectations;
4200 media_expectations.ExpectBidirectionalAudioAndVideo();
4201 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07004202}
4203
4204// Verify that audio/video can be received end-to-end when ICE renomination is
4205// enabled.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004206TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithIceRenomination) {
deadbeef1dcb1642017-03-29 21:08:16 -07004207 PeerConnectionInterface::RTCConfiguration config;
4208 config.enable_ice_renomination = true;
4209 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
4210 ConnectFakeSignaling();
4211 // Do normal offer/answer and wait for some frames to be received in each
4212 // direction.
Steve Anton15324772018-01-16 10:26:49 -08004213 caller()->AddAudioVideoTracks();
4214 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07004215 caller()->CreateAndSetAndSignalOffer();
4216 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4217 // Sanity check that ICE renomination was actually negotiated.
4218 const cricket::SessionDescription* desc =
4219 caller()->pc()->local_description()->description();
4220 for (const cricket::TransportInfo& info : desc->transport_infos()) {
Steve Anton64b626b2019-01-28 17:25:26 -08004221 ASSERT_THAT(info.description.transport_options, Contains("renomination"));
deadbeef1dcb1642017-03-29 21:08:16 -07004222 }
4223 desc = callee()->pc()->local_description()->description();
4224 for (const cricket::TransportInfo& info : desc->transport_infos()) {
Steve Anton64b626b2019-01-28 17:25:26 -08004225 ASSERT_THAT(info.description.transport_options, Contains("renomination"));
deadbeef1dcb1642017-03-29 21:08:16 -07004226 }
Seth Hampson2f0d7022018-02-20 11:54:42 -08004227 MediaExpectations media_expectations;
4228 media_expectations.ExpectBidirectionalAudioAndVideo();
4229 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07004230}
4231
Steve Anton6f25b092017-10-23 09:39:20 -07004232// With a max bundle policy and RTCP muxing, adding a new media description to
4233// the connection should not affect ICE at all because the new media will use
4234// the existing connection.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004235TEST_P(PeerConnectionIntegrationTest,
Steve Anton83119dd2017-11-10 16:19:52 -08004236 AddMediaToConnectedBundleDoesNotRestartIce) {
Steve Anton6f25b092017-10-23 09:39:20 -07004237 PeerConnectionInterface::RTCConfiguration config;
4238 config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
4239 config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyRequire;
4240 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(
4241 config, PeerConnectionInterface::RTCConfiguration()));
4242 ConnectFakeSignaling();
4243
Steve Anton15324772018-01-16 10:26:49 -08004244 caller()->AddAudioTrack();
Steve Anton6f25b092017-10-23 09:39:20 -07004245 caller()->CreateAndSetAndSignalOffer();
4246 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Steve Antonff52f1b2017-10-26 12:24:50 -07004247 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceConnectionCompleted,
4248 caller()->ice_connection_state(), kDefaultTimeout);
Steve Anton6f25b092017-10-23 09:39:20 -07004249
4250 caller()->clear_ice_connection_state_history();
4251
Steve Anton15324772018-01-16 10:26:49 -08004252 caller()->AddVideoTrack();
Steve Anton6f25b092017-10-23 09:39:20 -07004253 caller()->CreateAndSetAndSignalOffer();
4254 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4255
4256 EXPECT_EQ(0u, caller()->ice_connection_state_history().size());
4257}
4258
deadbeef1dcb1642017-03-29 21:08:16 -07004259// This test sets up a call between two parties with audio and video. It then
4260// renegotiates setting the video m-line to "port 0", then later renegotiates
4261// again, enabling video.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004262TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07004263 VideoFlowsAfterMediaSectionIsRejectedAndRecycled) {
4264 ASSERT_TRUE(CreatePeerConnectionWrappers());
4265 ConnectFakeSignaling();
4266
4267 // Do initial negotiation, only sending media from the caller. Will result in
4268 // video and audio recvonly "m=" sections.
Steve Anton15324772018-01-16 10:26:49 -08004269 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07004270 caller()->CreateAndSetAndSignalOffer();
4271 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4272
4273 // Negotiate again, disabling the video "m=" section (the callee will set the
4274 // port to 0 due to offer_to_receive_video = 0).
Seth Hampson2f0d7022018-02-20 11:54:42 -08004275 if (sdp_semantics_ == SdpSemantics::kPlanB) {
4276 PeerConnectionInterface::RTCOfferAnswerOptions options;
4277 options.offer_to_receive_video = 0;
4278 callee()->SetOfferAnswerOptions(options);
4279 } else {
4280 callee()->SetRemoteOfferHandler([this] {
4281 callee()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)->Stop();
4282 });
4283 }
deadbeef1dcb1642017-03-29 21:08:16 -07004284 caller()->CreateAndSetAndSignalOffer();
4285 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4286 // Sanity check that video "m=" section was actually rejected.
4287 const ContentInfo* answer_video_content = cricket::GetFirstVideoContent(
4288 callee()->pc()->local_description()->description());
4289 ASSERT_NE(nullptr, answer_video_content);
4290 ASSERT_TRUE(answer_video_content->rejected);
4291
4292 // Enable video and do negotiation again, making sure video is received
4293 // end-to-end, also adding media stream to callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004294 if (sdp_semantics_ == SdpSemantics::kPlanB) {
4295 PeerConnectionInterface::RTCOfferAnswerOptions options;
4296 options.offer_to_receive_video = 1;
4297 callee()->SetOfferAnswerOptions(options);
4298 } else {
4299 // The caller's transceiver is stopped, so we need to add another track.
4300 auto caller_transceiver =
4301 caller()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO);
4302 EXPECT_TRUE(caller_transceiver->stopped());
4303 caller()->AddVideoTrack();
4304 }
4305 callee()->AddVideoTrack();
4306 callee()->SetRemoteOfferHandler(nullptr);
deadbeef1dcb1642017-03-29 21:08:16 -07004307 caller()->CreateAndSetAndSignalOffer();
4308 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08004309
deadbeef1dcb1642017-03-29 21:08:16 -07004310 // Verify the caller receives frames from the newly added stream, and the
4311 // callee receives additional frames from the re-enabled video m= section.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004312 MediaExpectations media_expectations;
4313 media_expectations.CalleeExpectsSomeAudio();
4314 media_expectations.ExpectBidirectionalVideo();
4315 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07004316}
4317
deadbeef1dcb1642017-03-29 21:08:16 -07004318// This tests that if we negotiate after calling CreateSender but before we
4319// have a track, then set a track later, frames from the newly-set track are
4320// received end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004321TEST_F(PeerConnectionIntegrationTestPlanB,
deadbeef1dcb1642017-03-29 21:08:16 -07004322 MediaFlowsAfterEarlyWarmupWithCreateSender) {
4323 ASSERT_TRUE(CreatePeerConnectionWrappers());
4324 ConnectFakeSignaling();
4325 auto caller_audio_sender =
4326 caller()->pc()->CreateSender("audio", "caller_stream");
4327 auto caller_video_sender =
4328 caller()->pc()->CreateSender("video", "caller_stream");
4329 auto callee_audio_sender =
4330 callee()->pc()->CreateSender("audio", "callee_stream");
4331 auto callee_video_sender =
4332 callee()->pc()->CreateSender("video", "callee_stream");
4333 caller()->CreateAndSetAndSignalOffer();
4334 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
4335 // Wait for ICE to complete, without any tracks being set.
4336 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
4337 caller()->ice_connection_state(), kMaxWaitForFramesMs);
4338 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
4339 callee()->ice_connection_state(), kMaxWaitForFramesMs);
4340 // Now set the tracks, and expect frames to immediately start flowing.
4341 EXPECT_TRUE(caller_audio_sender->SetTrack(caller()->CreateLocalAudioTrack()));
4342 EXPECT_TRUE(caller_video_sender->SetTrack(caller()->CreateLocalVideoTrack()));
4343 EXPECT_TRUE(callee_audio_sender->SetTrack(callee()->CreateLocalAudioTrack()));
4344 EXPECT_TRUE(callee_video_sender->SetTrack(callee()->CreateLocalVideoTrack()));
Seth Hampson2f0d7022018-02-20 11:54:42 -08004345 MediaExpectations media_expectations;
4346 media_expectations.ExpectBidirectionalAudioAndVideo();
4347 ASSERT_TRUE(ExpectNewFrames(media_expectations));
4348}
4349
4350// This tests that if we negotiate after calling AddTransceiver but before we
4351// have a track, then set a track later, frames from the newly-set tracks are
4352// received end-to-end.
4353TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
4354 MediaFlowsAfterEarlyWarmupWithAddTransceiver) {
4355 ASSERT_TRUE(CreatePeerConnectionWrappers());
4356 ConnectFakeSignaling();
4357 auto audio_result = caller()->pc()->AddTransceiver(cricket::MEDIA_TYPE_AUDIO);
4358 ASSERT_EQ(RTCErrorType::NONE, audio_result.error().type());
4359 auto caller_audio_sender = audio_result.MoveValue()->sender();
4360 auto video_result = caller()->pc()->AddTransceiver(cricket::MEDIA_TYPE_VIDEO);
4361 ASSERT_EQ(RTCErrorType::NONE, video_result.error().type());
4362 auto caller_video_sender = video_result.MoveValue()->sender();
4363 callee()->SetRemoteOfferHandler([this] {
4364 ASSERT_EQ(2u, callee()->pc()->GetTransceivers().size());
4365 callee()->pc()->GetTransceivers()[0]->SetDirection(
4366 RtpTransceiverDirection::kSendRecv);
4367 callee()->pc()->GetTransceivers()[1]->SetDirection(
4368 RtpTransceiverDirection::kSendRecv);
4369 });
4370 caller()->CreateAndSetAndSignalOffer();
4371 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
4372 // Wait for ICE to complete, without any tracks being set.
4373 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
4374 caller()->ice_connection_state(), kMaxWaitForFramesMs);
4375 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
4376 callee()->ice_connection_state(), kMaxWaitForFramesMs);
4377 // Now set the tracks, and expect frames to immediately start flowing.
4378 auto callee_audio_sender = callee()->pc()->GetSenders()[0];
4379 auto callee_video_sender = callee()->pc()->GetSenders()[1];
4380 ASSERT_TRUE(caller_audio_sender->SetTrack(caller()->CreateLocalAudioTrack()));
4381 ASSERT_TRUE(caller_video_sender->SetTrack(caller()->CreateLocalVideoTrack()));
4382 ASSERT_TRUE(callee_audio_sender->SetTrack(callee()->CreateLocalAudioTrack()));
4383 ASSERT_TRUE(callee_video_sender->SetTrack(callee()->CreateLocalVideoTrack()));
4384 MediaExpectations media_expectations;
4385 media_expectations.ExpectBidirectionalAudioAndVideo();
4386 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07004387}
4388
4389// This test verifies that a remote video track can be added via AddStream,
4390// and sent end-to-end. For this particular test, it's simply echoed back
4391// from the caller to the callee, rather than being forwarded to a third
4392// PeerConnection.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004393TEST_F(PeerConnectionIntegrationTestPlanB, CanSendRemoteVideoTrack) {
deadbeef1dcb1642017-03-29 21:08:16 -07004394 ASSERT_TRUE(CreatePeerConnectionWrappers());
4395 ConnectFakeSignaling();
4396 // Just send a video track from the caller.
Steve Anton15324772018-01-16 10:26:49 -08004397 caller()->AddVideoTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07004398 caller()->CreateAndSetAndSignalOffer();
4399 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02004400 ASSERT_EQ(1U, callee()->remote_streams()->count());
deadbeef1dcb1642017-03-29 21:08:16 -07004401
4402 // Echo the stream back, and do a new offer/anwer (initiated by callee this
4403 // time).
4404 callee()->pc()->AddStream(callee()->remote_streams()->at(0));
4405 callee()->CreateAndSetAndSignalOffer();
4406 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
4407
Seth Hampson2f0d7022018-02-20 11:54:42 -08004408 MediaExpectations media_expectations;
4409 media_expectations.ExpectBidirectionalVideo();
4410 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07004411}
4412
4413// Test that we achieve the expected end-to-end connection time, using a
4414// fake clock and simulated latency on the media and signaling paths.
4415// We use a TURN<->TURN connection because this is usually the quickest to
4416// set up initially, especially when we're confident the connection will work
4417// and can start sending media before we get a STUN response.
4418//
4419// With various optimizations enabled, here are the network delays we expect to
4420// be on the critical path:
4421// 1. 2 signaling trips: Signaling offer and offerer's TURN candidate, then
4422// signaling answer (with DTLS fingerprint).
4423// 2. 9 media hops: Rest of the DTLS handshake. 3 hops in each direction when
4424// using TURN<->TURN pair, and DTLS exchange is 4 packets,
4425// the first of which should have arrived before the answer.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004426TEST_P(PeerConnectionIntegrationTest, EndToEndConnectionTimeWithTurnTurnPair) {
deadbeef1dcb1642017-03-29 21:08:16 -07004427 rtc::ScopedFakeClock fake_clock;
4428 // Some things use a time of "0" as a special value, so we need to start out
4429 // the fake clock at a nonzero time.
4430 // TODO(deadbeef): Fix this.
Sebastian Jansson5f83cf02018-05-08 14:52:22 +02004431 fake_clock.AdvanceTime(webrtc::TimeDelta::seconds(1));
deadbeef1dcb1642017-03-29 21:08:16 -07004432
4433 static constexpr int media_hop_delay_ms = 50;
4434 static constexpr int signaling_trip_delay_ms = 500;
4435 // For explanation of these values, see comment above.
4436 static constexpr int required_media_hops = 9;
4437 static constexpr int required_signaling_trips = 2;
4438 // For internal delays (such as posting an event asychronously).
4439 static constexpr int allowed_internal_delay_ms = 20;
4440 static constexpr int total_connection_time_ms =
4441 media_hop_delay_ms * required_media_hops +
4442 signaling_trip_delay_ms * required_signaling_trips +
4443 allowed_internal_delay_ms;
4444
4445 static const rtc::SocketAddress turn_server_1_internal_address{"88.88.88.0",
4446 3478};
4447 static const rtc::SocketAddress turn_server_1_external_address{"88.88.88.1",
4448 0};
4449 static const rtc::SocketAddress turn_server_2_internal_address{"99.99.99.0",
4450 3478};
4451 static const rtc::SocketAddress turn_server_2_external_address{"99.99.99.1",
4452 0};
Seth Hampsonaed71642018-06-11 07:41:32 -07004453 cricket::TestTurnServer* turn_server_1 = CreateTurnServer(
4454 turn_server_1_internal_address, turn_server_1_external_address);
Jonas Orelandbdcee282017-10-10 14:01:40 +02004455
Seth Hampsonaed71642018-06-11 07:41:32 -07004456 cricket::TestTurnServer* turn_server_2 = CreateTurnServer(
4457 turn_server_2_internal_address, turn_server_2_external_address);
deadbeef1dcb1642017-03-29 21:08:16 -07004458 // Bypass permission check on received packets so media can be sent before
4459 // the candidate is signaled.
Seth Hampsonaed71642018-06-11 07:41:32 -07004460 network_thread()->Invoke<void>(RTC_FROM_HERE, [turn_server_1] {
4461 turn_server_1->set_enable_permission_checks(false);
4462 });
4463 network_thread()->Invoke<void>(RTC_FROM_HERE, [turn_server_2] {
4464 turn_server_2->set_enable_permission_checks(false);
4465 });
deadbeef1dcb1642017-03-29 21:08:16 -07004466
4467 PeerConnectionInterface::RTCConfiguration client_1_config;
4468 webrtc::PeerConnectionInterface::IceServer ice_server_1;
4469 ice_server_1.urls.push_back("turn:88.88.88.0:3478");
4470 ice_server_1.username = "test";
4471 ice_server_1.password = "test";
4472 client_1_config.servers.push_back(ice_server_1);
4473 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
4474 client_1_config.presume_writable_when_fully_relayed = true;
4475
4476 PeerConnectionInterface::RTCConfiguration client_2_config;
4477 webrtc::PeerConnectionInterface::IceServer ice_server_2;
4478 ice_server_2.urls.push_back("turn:99.99.99.0:3478");
4479 ice_server_2.username = "test";
4480 ice_server_2.password = "test";
4481 client_2_config.servers.push_back(ice_server_2);
4482 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
4483 client_2_config.presume_writable_when_fully_relayed = true;
4484
4485 ASSERT_TRUE(
4486 CreatePeerConnectionWrappersWithConfig(client_1_config, client_2_config));
4487 // Set up the simulated delays.
4488 SetSignalingDelayMs(signaling_trip_delay_ms);
4489 ConnectFakeSignaling();
4490 virtual_socket_server()->set_delay_mean(media_hop_delay_ms);
4491 virtual_socket_server()->UpdateDelayDistribution();
4492
4493 // Set "offer to receive audio/video" without adding any tracks, so we just
4494 // set up ICE/DTLS with no media.
4495 PeerConnectionInterface::RTCOfferAnswerOptions options;
4496 options.offer_to_receive_audio = 1;
4497 options.offer_to_receive_video = 1;
4498 caller()->SetOfferAnswerOptions(options);
4499 caller()->CreateAndSetAndSignalOffer();
deadbeef71452802017-05-07 17:21:01 -07004500 EXPECT_TRUE_SIMULATED_WAIT(DtlsConnected(), total_connection_time_ms,
4501 fake_clock);
Seth Hampson1d4a76d2018-06-19 14:31:41 -07004502 // Closing the PeerConnections destroys the ports before the ScopedFakeClock.
4503 // If this is not done a DCHECK can be hit in ports.cc, because a large
4504 // negative number is calculated for the rtt due to the global clock changing.
Steve Antond91969e2019-05-30 12:27:03 -07004505 ClosePeerConnections();
deadbeef1dcb1642017-03-29 21:08:16 -07004506}
4507
Jonas Orelandbdcee282017-10-10 14:01:40 +02004508// Verify that a TurnCustomizer passed in through RTCConfiguration
4509// is actually used by the underlying TURN candidate pair.
4510// Note that turnport_unittest.cc contains more detailed, lower-level tests.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004511TEST_P(PeerConnectionIntegrationTest, TurnCustomizerUsedForTurnConnections) {
Jonas Orelandbdcee282017-10-10 14:01:40 +02004512 static const rtc::SocketAddress turn_server_1_internal_address{"88.88.88.0",
4513 3478};
4514 static const rtc::SocketAddress turn_server_1_external_address{"88.88.88.1",
4515 0};
4516 static const rtc::SocketAddress turn_server_2_internal_address{"99.99.99.0",
4517 3478};
4518 static const rtc::SocketAddress turn_server_2_external_address{"99.99.99.1",
4519 0};
Seth Hampsonaed71642018-06-11 07:41:32 -07004520 CreateTurnServer(turn_server_1_internal_address,
4521 turn_server_1_external_address);
4522 CreateTurnServer(turn_server_2_internal_address,
4523 turn_server_2_external_address);
Jonas Orelandbdcee282017-10-10 14:01:40 +02004524
4525 PeerConnectionInterface::RTCConfiguration client_1_config;
4526 webrtc::PeerConnectionInterface::IceServer ice_server_1;
4527 ice_server_1.urls.push_back("turn:88.88.88.0:3478");
4528 ice_server_1.username = "test";
4529 ice_server_1.password = "test";
4530 client_1_config.servers.push_back(ice_server_1);
4531 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
Seth Hampsonaed71642018-06-11 07:41:32 -07004532 auto* customizer1 = CreateTurnCustomizer();
4533 client_1_config.turn_customizer = customizer1;
Jonas Orelandbdcee282017-10-10 14:01:40 +02004534
4535 PeerConnectionInterface::RTCConfiguration client_2_config;
4536 webrtc::PeerConnectionInterface::IceServer ice_server_2;
4537 ice_server_2.urls.push_back("turn:99.99.99.0:3478");
4538 ice_server_2.username = "test";
4539 ice_server_2.password = "test";
4540 client_2_config.servers.push_back(ice_server_2);
4541 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
Seth Hampsonaed71642018-06-11 07:41:32 -07004542 auto* customizer2 = CreateTurnCustomizer();
4543 client_2_config.turn_customizer = customizer2;
Jonas Orelandbdcee282017-10-10 14:01:40 +02004544
4545 ASSERT_TRUE(
4546 CreatePeerConnectionWrappersWithConfig(client_1_config, client_2_config));
4547 ConnectFakeSignaling();
4548
4549 // Set "offer to receive audio/video" without adding any tracks, so we just
4550 // set up ICE/DTLS with no media.
4551 PeerConnectionInterface::RTCOfferAnswerOptions options;
4552 options.offer_to_receive_audio = 1;
4553 options.offer_to_receive_video = 1;
4554 caller()->SetOfferAnswerOptions(options);
4555 caller()->CreateAndSetAndSignalOffer();
4556 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
4557
Seth Hampsonaed71642018-06-11 07:41:32 -07004558 ExpectTurnCustomizerCountersIncremented(customizer1);
4559 ExpectTurnCustomizerCountersIncremented(customizer2);
Jonas Orelandbdcee282017-10-10 14:01:40 +02004560}
4561
Benjamin Wright2d5f3cb2018-05-22 14:46:06 -07004562// Verifies that you can use TCP instead of UDP to connect to a TURN server and
4563// send media between the caller and the callee.
4564TEST_P(PeerConnectionIntegrationTest, TCPUsedForTurnConnections) {
4565 static const rtc::SocketAddress turn_server_internal_address{"88.88.88.0",
4566 3478};
4567 static const rtc::SocketAddress turn_server_external_address{"88.88.88.1", 0};
4568
4569 // Enable TCP for the fake turn server.
Seth Hampsonaed71642018-06-11 07:41:32 -07004570 CreateTurnServer(turn_server_internal_address, turn_server_external_address,
4571 cricket::PROTO_TCP);
Benjamin Wright2d5f3cb2018-05-22 14:46:06 -07004572
4573 webrtc::PeerConnectionInterface::IceServer ice_server;
4574 ice_server.urls.push_back("turn:88.88.88.0:3478?transport=tcp");
4575 ice_server.username = "test";
4576 ice_server.password = "test";
4577
4578 PeerConnectionInterface::RTCConfiguration client_1_config;
4579 client_1_config.servers.push_back(ice_server);
4580 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
4581
4582 PeerConnectionInterface::RTCConfiguration client_2_config;
4583 client_2_config.servers.push_back(ice_server);
4584 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
4585
4586 ASSERT_TRUE(
4587 CreatePeerConnectionWrappersWithConfig(client_1_config, client_2_config));
4588
4589 // Do normal offer/answer and wait for ICE to complete.
4590 ConnectFakeSignaling();
4591 caller()->AddAudioVideoTracks();
4592 callee()->AddAudioVideoTracks();
4593 caller()->CreateAndSetAndSignalOffer();
4594 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4595 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
4596 callee()->ice_connection_state(), kMaxWaitForFramesMs);
4597
4598 MediaExpectations media_expectations;
4599 media_expectations.ExpectBidirectionalAudioAndVideo();
4600 EXPECT_TRUE(ExpectNewFrames(media_expectations));
4601}
4602
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07004603// Verify that a SSLCertificateVerifier passed in through
4604// PeerConnectionDependencies is actually used by the underlying SSL
4605// implementation to determine whether a certificate presented by the TURN
4606// server is accepted by the client. Note that openssladapter_unittest.cc
4607// contains more detailed, lower-level tests.
4608TEST_P(PeerConnectionIntegrationTest,
4609 SSLCertificateVerifierUsedForTurnConnections) {
4610 static const rtc::SocketAddress turn_server_internal_address{"88.88.88.0",
4611 3478};
4612 static const rtc::SocketAddress turn_server_external_address{"88.88.88.1", 0};
4613
4614 // Enable TCP-TLS for the fake turn server. We need to pass in 88.88.88.0 so
4615 // that host name verification passes on the fake certificate.
Seth Hampsonaed71642018-06-11 07:41:32 -07004616 CreateTurnServer(turn_server_internal_address, turn_server_external_address,
4617 cricket::PROTO_TLS, "88.88.88.0");
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07004618
4619 webrtc::PeerConnectionInterface::IceServer ice_server;
4620 ice_server.urls.push_back("turns:88.88.88.0:3478?transport=tcp");
4621 ice_server.username = "test";
4622 ice_server.password = "test";
4623
4624 PeerConnectionInterface::RTCConfiguration client_1_config;
4625 client_1_config.servers.push_back(ice_server);
4626 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
4627
4628 PeerConnectionInterface::RTCConfiguration client_2_config;
4629 client_2_config.servers.push_back(ice_server);
4630 // Setting the type to kRelay forces the connection to go through a TURN
4631 // server.
4632 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
4633
4634 // Get a copy to the pointer so we can verify calls later.
4635 rtc::TestCertificateVerifier* client_1_cert_verifier =
4636 new rtc::TestCertificateVerifier();
4637 client_1_cert_verifier->verify_certificate_ = true;
4638 rtc::TestCertificateVerifier* client_2_cert_verifier =
4639 new rtc::TestCertificateVerifier();
4640 client_2_cert_verifier->verify_certificate_ = true;
4641
4642 // Create the dependencies with the test certificate verifier.
4643 webrtc::PeerConnectionDependencies client_1_deps(nullptr);
4644 client_1_deps.tls_cert_verifier =
4645 std::unique_ptr<rtc::TestCertificateVerifier>(client_1_cert_verifier);
4646 webrtc::PeerConnectionDependencies client_2_deps(nullptr);
4647 client_2_deps.tls_cert_verifier =
4648 std::unique_ptr<rtc::TestCertificateVerifier>(client_2_cert_verifier);
4649
4650 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndDeps(
4651 client_1_config, std::move(client_1_deps), client_2_config,
4652 std::move(client_2_deps)));
4653 ConnectFakeSignaling();
4654
4655 // Set "offer to receive audio/video" without adding any tracks, so we just
4656 // set up ICE/DTLS with no media.
4657 PeerConnectionInterface::RTCOfferAnswerOptions options;
4658 options.offer_to_receive_audio = 1;
4659 options.offer_to_receive_video = 1;
4660 caller()->SetOfferAnswerOptions(options);
4661 caller()->CreateAndSetAndSignalOffer();
4662 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
4663
4664 EXPECT_GT(client_1_cert_verifier->call_count_, 0u);
4665 EXPECT_GT(client_2_cert_verifier->call_count_, 0u);
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07004666}
4667
4668TEST_P(PeerConnectionIntegrationTest,
4669 SSLCertificateVerifierFailureUsedForTurnConnectionsFailsConnection) {
4670 static const rtc::SocketAddress turn_server_internal_address{"88.88.88.0",
4671 3478};
4672 static const rtc::SocketAddress turn_server_external_address{"88.88.88.1", 0};
4673
4674 // Enable TCP-TLS for the fake turn server. We need to pass in 88.88.88.0 so
4675 // that host name verification passes on the fake certificate.
Seth Hampsonaed71642018-06-11 07:41:32 -07004676 CreateTurnServer(turn_server_internal_address, turn_server_external_address,
4677 cricket::PROTO_TLS, "88.88.88.0");
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07004678
4679 webrtc::PeerConnectionInterface::IceServer ice_server;
4680 ice_server.urls.push_back("turns:88.88.88.0:3478?transport=tcp");
4681 ice_server.username = "test";
4682 ice_server.password = "test";
4683
4684 PeerConnectionInterface::RTCConfiguration client_1_config;
4685 client_1_config.servers.push_back(ice_server);
4686 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
4687
4688 PeerConnectionInterface::RTCConfiguration client_2_config;
4689 client_2_config.servers.push_back(ice_server);
4690 // Setting the type to kRelay forces the connection to go through a TURN
4691 // server.
4692 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
4693
4694 // Get a copy to the pointer so we can verify calls later.
4695 rtc::TestCertificateVerifier* client_1_cert_verifier =
4696 new rtc::TestCertificateVerifier();
4697 client_1_cert_verifier->verify_certificate_ = false;
4698 rtc::TestCertificateVerifier* client_2_cert_verifier =
4699 new rtc::TestCertificateVerifier();
4700 client_2_cert_verifier->verify_certificate_ = false;
4701
4702 // Create the dependencies with the test certificate verifier.
4703 webrtc::PeerConnectionDependencies client_1_deps(nullptr);
4704 client_1_deps.tls_cert_verifier =
4705 std::unique_ptr<rtc::TestCertificateVerifier>(client_1_cert_verifier);
4706 webrtc::PeerConnectionDependencies client_2_deps(nullptr);
4707 client_2_deps.tls_cert_verifier =
4708 std::unique_ptr<rtc::TestCertificateVerifier>(client_2_cert_verifier);
4709
4710 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndDeps(
4711 client_1_config, std::move(client_1_deps), client_2_config,
4712 std::move(client_2_deps)));
4713 ConnectFakeSignaling();
4714
4715 // Set "offer to receive audio/video" without adding any tracks, so we just
4716 // set up ICE/DTLS with no media.
4717 PeerConnectionInterface::RTCOfferAnswerOptions options;
4718 options.offer_to_receive_audio = 1;
4719 options.offer_to_receive_video = 1;
4720 caller()->SetOfferAnswerOptions(options);
4721 caller()->CreateAndSetAndSignalOffer();
4722 bool wait_res = true;
4723 // TODO(bugs.webrtc.org/9219): When IceConnectionState is implemented
4724 // properly, should be able to just wait for a state of "failed" instead of
4725 // waiting a fixed 10 seconds.
4726 WAIT_(DtlsConnected(), kDefaultTimeout, wait_res);
4727 ASSERT_FALSE(wait_res);
4728
4729 EXPECT_GT(client_1_cert_verifier->call_count_, 0u);
4730 EXPECT_GT(client_2_cert_verifier->call_count_, 0u);
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07004731}
4732
deadbeefc964d0b2017-04-03 10:03:35 -07004733// Test that audio and video flow end-to-end when codec names don't use the
4734// expected casing, given that they're supposed to be case insensitive. To test
4735// this, all but one codec is removed from each media description, and its
4736// casing is changed.
4737//
4738// In the past, this has regressed and caused crashes/black video, due to the
4739// fact that code at some layers was doing case-insensitive comparisons and
4740// code at other layers was not.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004741TEST_P(PeerConnectionIntegrationTest, CodecNamesAreCaseInsensitive) {
deadbeefc964d0b2017-04-03 10:03:35 -07004742 ASSERT_TRUE(CreatePeerConnectionWrappers());
4743 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08004744 caller()->AddAudioVideoTracks();
4745 callee()->AddAudioVideoTracks();
deadbeefc964d0b2017-04-03 10:03:35 -07004746
4747 // Remove all but one audio/video codec (opus and VP8), and change the
4748 // casing of the caller's generated offer.
4749 caller()->SetGeneratedSdpMunger([](cricket::SessionDescription* description) {
4750 cricket::AudioContentDescription* audio =
4751 GetFirstAudioContentDescription(description);
4752 ASSERT_NE(nullptr, audio);
4753 auto audio_codecs = audio->codecs();
4754 audio_codecs.erase(std::remove_if(audio_codecs.begin(), audio_codecs.end(),
4755 [](const cricket::AudioCodec& codec) {
4756 return codec.name != "opus";
4757 }),
4758 audio_codecs.end());
4759 ASSERT_EQ(1u, audio_codecs.size());
4760 audio_codecs[0].name = "OpUs";
4761 audio->set_codecs(audio_codecs);
4762
4763 cricket::VideoContentDescription* video =
4764 GetFirstVideoContentDescription(description);
4765 ASSERT_NE(nullptr, video);
4766 auto video_codecs = video->codecs();
4767 video_codecs.erase(std::remove_if(video_codecs.begin(), video_codecs.end(),
4768 [](const cricket::VideoCodec& codec) {
4769 return codec.name != "VP8";
4770 }),
4771 video_codecs.end());
4772 ASSERT_EQ(1u, video_codecs.size());
4773 video_codecs[0].name = "vP8";
4774 video->set_codecs(video_codecs);
4775 });
4776
4777 caller()->CreateAndSetAndSignalOffer();
4778 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4779
4780 // Verify frames are still received end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004781 MediaExpectations media_expectations;
4782 media_expectations.ExpectBidirectionalAudioAndVideo();
4783 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeefc964d0b2017-04-03 10:03:35 -07004784}
4785
Jonas Oreland49ac5952018-09-26 16:04:32 +02004786TEST_P(PeerConnectionIntegrationTest, GetSourcesAudio) {
hbos8d609f62017-04-10 07:39:05 -07004787 ASSERT_TRUE(CreatePeerConnectionWrappers());
4788 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08004789 caller()->AddAudioTrack();
hbos8d609f62017-04-10 07:39:05 -07004790 caller()->CreateAndSetAndSignalOffer();
4791 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
deadbeefd8ad7882017-04-18 16:01:17 -07004792 // Wait for one audio frame to be received by the callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004793 MediaExpectations media_expectations;
4794 media_expectations.CalleeExpectsSomeAudio(1);
4795 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Jonas Oreland49ac5952018-09-26 16:04:32 +02004796 ASSERT_EQ(callee()->pc()->GetReceivers().size(), 1u);
hbos8d609f62017-04-10 07:39:05 -07004797 auto receiver = callee()->pc()->GetReceivers()[0];
4798 ASSERT_EQ(receiver->media_type(), cricket::MEDIA_TYPE_AUDIO);
Jonas Oreland49ac5952018-09-26 16:04:32 +02004799 auto sources = receiver->GetSources();
hbos8d609f62017-04-10 07:39:05 -07004800 ASSERT_GT(receiver->GetParameters().encodings.size(), 0u);
4801 EXPECT_EQ(receiver->GetParameters().encodings[0].ssrc,
Jonas Oreland49ac5952018-09-26 16:04:32 +02004802 sources[0].source_id());
4803 EXPECT_EQ(webrtc::RtpSourceType::SSRC, sources[0].source_type());
4804}
4805
4806TEST_P(PeerConnectionIntegrationTest, GetSourcesVideo) {
4807 ASSERT_TRUE(CreatePeerConnectionWrappers());
4808 ConnectFakeSignaling();
4809 caller()->AddVideoTrack();
4810 caller()->CreateAndSetAndSignalOffer();
4811 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4812 // Wait for one video frame to be received by the callee.
4813 MediaExpectations media_expectations;
4814 media_expectations.CalleeExpectsSomeVideo(1);
4815 ASSERT_TRUE(ExpectNewFrames(media_expectations));
4816 ASSERT_EQ(callee()->pc()->GetReceivers().size(), 1u);
4817 auto receiver = callee()->pc()->GetReceivers()[0];
4818 ASSERT_EQ(receiver->media_type(), cricket::MEDIA_TYPE_VIDEO);
4819 auto sources = receiver->GetSources();
4820 ASSERT_GT(receiver->GetParameters().encodings.size(), 0u);
4821 EXPECT_EQ(receiver->GetParameters().encodings[0].ssrc,
4822 sources[0].source_id());
4823 EXPECT_EQ(webrtc::RtpSourceType::SSRC, sources[0].source_type());
hbos8d609f62017-04-10 07:39:05 -07004824}
4825
deadbeef2f425aa2017-04-14 10:41:32 -07004826// Test that if a track is removed and added again with a different stream ID,
4827// the new stream ID is successfully communicated in SDP and media continues to
4828// flow end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004829// TODO(webrtc.bugs.org/8734): This test does not work for Unified Plan because
4830// it will not reuse a transceiver that has already been sending. After creating
4831// a new transceiver it tries to create an offer with two senders of the same
4832// track ids and it fails.
4833TEST_F(PeerConnectionIntegrationTestPlanB, RemoveAndAddTrackWithNewStreamId) {
deadbeef2f425aa2017-04-14 10:41:32 -07004834 ASSERT_TRUE(CreatePeerConnectionWrappers());
4835 ConnectFakeSignaling();
4836
deadbeef2f425aa2017-04-14 10:41:32 -07004837 // Add track using stream 1, do offer/answer.
4838 rtc::scoped_refptr<webrtc::AudioTrackInterface> track =
4839 caller()->CreateLocalAudioTrack();
4840 rtc::scoped_refptr<webrtc::RtpSenderInterface> sender =
Steve Antond78323f2018-07-11 11:13:44 -07004841 caller()->AddTrack(track, {"stream_1"});
deadbeef2f425aa2017-04-14 10:41:32 -07004842 caller()->CreateAndSetAndSignalOffer();
4843 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08004844 {
4845 MediaExpectations media_expectations;
4846 media_expectations.CalleeExpectsSomeAudio(1);
4847 ASSERT_TRUE(ExpectNewFrames(media_expectations));
4848 }
deadbeef2f425aa2017-04-14 10:41:32 -07004849 // Remove the sender, and create a new one with the new stream.
4850 caller()->pc()->RemoveTrack(sender);
Steve Antond78323f2018-07-11 11:13:44 -07004851 sender = caller()->AddTrack(track, {"stream_2"});
deadbeef2f425aa2017-04-14 10:41:32 -07004852 caller()->CreateAndSetAndSignalOffer();
4853 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4854 // Wait for additional audio frames to be received by the callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004855 {
4856 MediaExpectations media_expectations;
4857 media_expectations.CalleeExpectsSomeAudio();
4858 ASSERT_TRUE(ExpectNewFrames(media_expectations));
4859 }
deadbeef2f425aa2017-04-14 10:41:32 -07004860}
4861
Seth Hampson2f0d7022018-02-20 11:54:42 -08004862TEST_P(PeerConnectionIntegrationTest, RtcEventLogOutputWriteCalled) {
Elad Alon99c3fe52017-10-13 16:29:40 +02004863 ASSERT_TRUE(CreatePeerConnectionWrappers());
4864 ConnectFakeSignaling();
4865
Karl Wiberg918f50c2018-07-05 11:40:33 +02004866 auto output = absl::make_unique<testing::NiceMock<MockRtcEventLogOutput>>();
Mirko Bonadei6a489f22019-04-09 15:11:12 +02004867 ON_CALL(*output, IsActive()).WillByDefault(::testing::Return(true));
4868 ON_CALL(*output, Write(::testing::_)).WillByDefault(::testing::Return(true));
Elad Alon99c3fe52017-10-13 16:29:40 +02004869 EXPECT_CALL(*output, Write(::testing::_)).Times(::testing::AtLeast(1));
Bjorn Tereliusde939432017-11-20 17:38:14 +01004870 EXPECT_TRUE(caller()->pc()->StartRtcEventLog(
4871 std::move(output), webrtc::RtcEventLog::kImmediateOutput));
Elad Alon99c3fe52017-10-13 16:29:40 +02004872
Steve Anton15324772018-01-16 10:26:49 -08004873 caller()->AddAudioVideoTracks();
Elad Alon99c3fe52017-10-13 16:29:40 +02004874 caller()->CreateAndSetAndSignalOffer();
4875 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4876}
4877
Steve Antonede9ca52017-10-16 13:04:27 -07004878// Test that if candidates are only signaled by applying full session
4879// descriptions (instead of using AddIceCandidate), the peers can connect to
4880// each other and exchange media.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004881TEST_P(PeerConnectionIntegrationTest, MediaFlowsWhenCandidatesSetOnlyInSdp) {
Steve Antonede9ca52017-10-16 13:04:27 -07004882 ASSERT_TRUE(CreatePeerConnectionWrappers());
4883 // Each side will signal the session descriptions but not candidates.
4884 ConnectFakeSignalingForSdpOnly();
4885
4886 // Add audio video track and exchange the initial offer/answer with media
4887 // information only. This will start ICE gathering on each side.
Steve Anton15324772018-01-16 10:26:49 -08004888 caller()->AddAudioVideoTracks();
4889 callee()->AddAudioVideoTracks();
Steve Antonede9ca52017-10-16 13:04:27 -07004890 caller()->CreateAndSetAndSignalOffer();
4891
4892 // Wait for all candidates to be gathered on both the caller and callee.
4893 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceGatheringComplete,
4894 caller()->ice_gathering_state(), kDefaultTimeout);
4895 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceGatheringComplete,
4896 callee()->ice_gathering_state(), kDefaultTimeout);
4897
4898 // The candidates will now be included in the session description, so
4899 // signaling them will start the ICE connection.
4900 caller()->CreateAndSetAndSignalOffer();
4901 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4902
4903 // Ensure that media flows in both directions.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004904 MediaExpectations media_expectations;
4905 media_expectations.ExpectBidirectionalAudioAndVideo();
4906 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Antonede9ca52017-10-16 13:04:27 -07004907}
4908
henrika5f6bf242017-11-01 11:06:56 +01004909// Test that SetAudioPlayout can be used to disable audio playout from the
4910// start, then later enable it. This may be useful, for example, if the caller
4911// needs to play a local ringtone until some event occurs, after which it
4912// switches to playing the received audio.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004913TEST_P(PeerConnectionIntegrationTest, DisableAndEnableAudioPlayout) {
henrika5f6bf242017-11-01 11:06:56 +01004914 ASSERT_TRUE(CreatePeerConnectionWrappers());
4915 ConnectFakeSignaling();
4916
4917 // Set up audio-only call where audio playout is disabled on caller's side.
4918 caller()->pc()->SetAudioPlayout(false);
Steve Anton15324772018-01-16 10:26:49 -08004919 caller()->AddAudioTrack();
4920 callee()->AddAudioTrack();
henrika5f6bf242017-11-01 11:06:56 +01004921 caller()->CreateAndSetAndSignalOffer();
4922 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4923
4924 // Pump messages for a second.
4925 WAIT(false, 1000);
4926 // Since audio playout is disabled, the caller shouldn't have received
4927 // anything (at the playout level, at least).
4928 EXPECT_EQ(0, caller()->audio_frames_received());
4929 // As a sanity check, make sure the callee (for which playout isn't disabled)
4930 // did still see frames on its audio level.
4931 ASSERT_GT(callee()->audio_frames_received(), 0);
4932
4933 // Enable playout again, and ensure audio starts flowing.
4934 caller()->pc()->SetAudioPlayout(true);
Seth Hampson2f0d7022018-02-20 11:54:42 -08004935 MediaExpectations media_expectations;
4936 media_expectations.ExpectBidirectionalAudio();
4937 ASSERT_TRUE(ExpectNewFrames(media_expectations));
henrika5f6bf242017-11-01 11:06:56 +01004938}
4939
4940double GetAudioEnergyStat(PeerConnectionWrapper* pc) {
4941 auto report = pc->NewGetStats();
4942 auto track_stats_list =
4943 report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
4944 const webrtc::RTCMediaStreamTrackStats* remote_track_stats = nullptr;
4945 for (const auto* track_stats : track_stats_list) {
4946 if (track_stats->remote_source.is_defined() &&
4947 *track_stats->remote_source) {
4948 remote_track_stats = track_stats;
4949 break;
4950 }
4951 }
4952
4953 if (!remote_track_stats->total_audio_energy.is_defined()) {
4954 return 0.0;
4955 }
4956 return *remote_track_stats->total_audio_energy;
4957}
4958
4959// Test that if audio playout is disabled via the SetAudioPlayout() method, then
4960// incoming audio is still processed and statistics are generated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004961TEST_P(PeerConnectionIntegrationTest,
henrika5f6bf242017-11-01 11:06:56 +01004962 DisableAudioPlayoutStillGeneratesAudioStats) {
4963 ASSERT_TRUE(CreatePeerConnectionWrappers());
4964 ConnectFakeSignaling();
4965
4966 // Set up audio-only call where playout is disabled but audio-processing is
4967 // still active.
Steve Anton15324772018-01-16 10:26:49 -08004968 caller()->AddAudioTrack();
4969 callee()->AddAudioTrack();
henrika5f6bf242017-11-01 11:06:56 +01004970 caller()->pc()->SetAudioPlayout(false);
4971
4972 caller()->CreateAndSetAndSignalOffer();
4973 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4974
4975 // Wait for the callee to receive audio stats.
4976 EXPECT_TRUE_WAIT(GetAudioEnergyStat(caller()) > 0, kMaxWaitForFramesMs);
4977}
4978
henrika4f167df2017-11-01 14:45:55 +01004979// Test that SetAudioRecording can be used to disable audio recording from the
4980// start, then later enable it. This may be useful, for example, if the caller
4981// wants to ensure that no audio resources are active before a certain state
4982// is reached.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004983TEST_P(PeerConnectionIntegrationTest, DisableAndEnableAudioRecording) {
henrika4f167df2017-11-01 14:45:55 +01004984 ASSERT_TRUE(CreatePeerConnectionWrappers());
4985 ConnectFakeSignaling();
4986
4987 // Set up audio-only call where audio recording is disabled on caller's side.
4988 caller()->pc()->SetAudioRecording(false);
Steve Anton15324772018-01-16 10:26:49 -08004989 caller()->AddAudioTrack();
4990 callee()->AddAudioTrack();
henrika4f167df2017-11-01 14:45:55 +01004991 caller()->CreateAndSetAndSignalOffer();
4992 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4993
4994 // Pump messages for a second.
4995 WAIT(false, 1000);
4996 // Since caller has disabled audio recording, the callee shouldn't have
4997 // received anything.
4998 EXPECT_EQ(0, callee()->audio_frames_received());
4999 // As a sanity check, make sure the caller did still see frames on its
5000 // audio level since audio recording is enabled on the calle side.
5001 ASSERT_GT(caller()->audio_frames_received(), 0);
5002
5003 // Enable audio recording again, and ensure audio starts flowing.
5004 caller()->pc()->SetAudioRecording(true);
Seth Hampson2f0d7022018-02-20 11:54:42 -08005005 MediaExpectations media_expectations;
5006 media_expectations.ExpectBidirectionalAudio();
5007 ASSERT_TRUE(ExpectNewFrames(media_expectations));
henrika4f167df2017-11-01 14:45:55 +01005008}
5009
Taylor Brandstetter389a97c2018-01-03 16:26:06 -08005010// Test that after closing PeerConnections, they stop sending any packets (ICE,
5011// DTLS, RTP...).
Seth Hampson2f0d7022018-02-20 11:54:42 -08005012TEST_P(PeerConnectionIntegrationTest, ClosingConnectionStopsPacketFlow) {
Taylor Brandstetter389a97c2018-01-03 16:26:06 -08005013 // Set up audio/video/data, wait for some frames to be received.
5014 ASSERT_TRUE(CreatePeerConnectionWrappers());
5015 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08005016 caller()->AddAudioVideoTracks();
Taylor Brandstetter389a97c2018-01-03 16:26:06 -08005017#ifdef HAVE_SCTP
5018 caller()->CreateDataChannel();
5019#endif
5020 caller()->CreateAndSetAndSignalOffer();
5021 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08005022 MediaExpectations media_expectations;
5023 media_expectations.CalleeExpectsSomeAudioAndVideo();
5024 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Taylor Brandstetter389a97c2018-01-03 16:26:06 -08005025 // Close PeerConnections.
Steve Antond91969e2019-05-30 12:27:03 -07005026 ClosePeerConnections();
Taylor Brandstetter389a97c2018-01-03 16:26:06 -08005027 // Pump messages for a second, and ensure no new packets end up sent.
5028 uint32_t sent_packets_a = virtual_socket_server()->sent_packets();
5029 WAIT(false, 1000);
5030 uint32_t sent_packets_b = virtual_socket_server()->sent_packets();
5031 EXPECT_EQ(sent_packets_a, sent_packets_b);
5032}
5033
Steve Anton7eca0932018-03-30 15:18:41 -07005034// Test that transport stats are generated by the RTCStatsCollector for a
5035// connection that only involves data channels. This is a regression test for
5036// crbug.com/826972.
5037#ifdef HAVE_SCTP
5038TEST_P(PeerConnectionIntegrationTest,
5039 TransportStatsReportedForDataChannelOnlyConnection) {
5040 ASSERT_TRUE(CreatePeerConnectionWrappers());
5041 ConnectFakeSignaling();
5042 caller()->CreateDataChannel();
5043
5044 caller()->CreateAndSetAndSignalOffer();
5045 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5046 ASSERT_TRUE_WAIT(callee()->data_channel(), kDefaultTimeout);
5047
5048 auto caller_report = caller()->NewGetStats();
5049 EXPECT_EQ(1u, caller_report->GetStatsOfType<RTCTransportStats>().size());
5050 auto callee_report = callee()->NewGetStats();
5051 EXPECT_EQ(1u, callee_report->GetStatsOfType<RTCTransportStats>().size());
5052}
5053#endif // HAVE_SCTP
5054
Qingsi Wang7685e862018-06-11 20:15:46 -07005055TEST_P(PeerConnectionIntegrationTest,
5056 IceEventsGeneratedAndLoggedInRtcEventLog) {
5057 ASSERT_TRUE(CreatePeerConnectionWrappersWithFakeRtcEventLog());
5058 ConnectFakeSignaling();
5059 PeerConnectionInterface::RTCOfferAnswerOptions options;
5060 options.offer_to_receive_audio = 1;
5061 caller()->SetOfferAnswerOptions(options);
5062 caller()->CreateAndSetAndSignalOffer();
5063 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
5064 ASSERT_NE(nullptr, caller()->event_log_factory());
5065 ASSERT_NE(nullptr, callee()->event_log_factory());
5066 webrtc::FakeRtcEventLog* caller_event_log =
5067 static_cast<webrtc::FakeRtcEventLog*>(
5068 caller()->event_log_factory()->last_log_created());
5069 webrtc::FakeRtcEventLog* callee_event_log =
5070 static_cast<webrtc::FakeRtcEventLog*>(
5071 callee()->event_log_factory()->last_log_created());
5072 ASSERT_NE(nullptr, caller_event_log);
5073 ASSERT_NE(nullptr, callee_event_log);
5074 int caller_ice_config_count = caller_event_log->GetEventCount(
5075 webrtc::RtcEvent::Type::IceCandidatePairConfig);
5076 int caller_ice_event_count = caller_event_log->GetEventCount(
5077 webrtc::RtcEvent::Type::IceCandidatePairEvent);
5078 int callee_ice_config_count = callee_event_log->GetEventCount(
5079 webrtc::RtcEvent::Type::IceCandidatePairConfig);
5080 int callee_ice_event_count = callee_event_log->GetEventCount(
5081 webrtc::RtcEvent::Type::IceCandidatePairEvent);
5082 EXPECT_LT(0, caller_ice_config_count);
5083 EXPECT_LT(0, caller_ice_event_count);
5084 EXPECT_LT(0, callee_ice_config_count);
5085 EXPECT_LT(0, callee_ice_event_count);
5086}
5087
Qingsi Wangc129c352019-04-18 10:41:58 -07005088TEST_P(PeerConnectionIntegrationTest, RegatherAfterChangingIceTransportType) {
5089 webrtc::test::ScopedFieldTrials field_trials(
5090 "WebRTC-GatherOnCandidateFilterChanged/Enabled/");
5091 static const rtc::SocketAddress turn_server_internal_address{"88.88.88.0",
5092 3478};
5093 static const rtc::SocketAddress turn_server_external_address{"88.88.88.1", 0};
5094
5095 CreateTurnServer(turn_server_internal_address, turn_server_external_address);
5096
5097 webrtc::PeerConnectionInterface::IceServer ice_server;
5098 ice_server.urls.push_back("turn:88.88.88.0:3478");
5099 ice_server.username = "test";
5100 ice_server.password = "test";
5101
5102 PeerConnectionInterface::RTCConfiguration caller_config;
5103 caller_config.servers.push_back(ice_server);
5104 caller_config.type = webrtc::PeerConnectionInterface::kRelay;
5105 caller_config.continual_gathering_policy = PeerConnection::GATHER_CONTINUALLY;
5106
5107 PeerConnectionInterface::RTCConfiguration callee_config;
5108 callee_config.servers.push_back(ice_server);
5109 callee_config.type = webrtc::PeerConnectionInterface::kRelay;
5110 callee_config.continual_gathering_policy = PeerConnection::GATHER_CONTINUALLY;
5111
5112 ASSERT_TRUE(
5113 CreatePeerConnectionWrappersWithConfig(caller_config, callee_config));
5114
5115 // Do normal offer/answer and wait for ICE to complete.
5116 ConnectFakeSignaling();
5117 caller()->AddAudioVideoTracks();
5118 callee()->AddAudioVideoTracks();
5119 caller()->CreateAndSetAndSignalOffer();
5120 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5121 // Since we are doing continual gathering, the ICE transport does not reach
5122 // kIceGatheringComplete (see
5123 // P2PTransportChannel::OnCandidatesAllocationDone), and consequently not
5124 // kIceConnectionComplete.
5125 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
5126 caller()->ice_connection_state(), kDefaultTimeout);
5127 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
5128 callee()->ice_connection_state(), kDefaultTimeout);
5129 // Note that we cannot use the metric
5130 // |WebRTC.PeerConnection.CandidatePairType_UDP| in this test since this
5131 // metric is only populated when we reach kIceConnectionComplete in the
5132 // current implementation.
5133 EXPECT_EQ(cricket::RELAY_PORT_TYPE,
5134 caller()->last_candidate_gathered().type());
5135 EXPECT_EQ(cricket::RELAY_PORT_TYPE,
5136 callee()->last_candidate_gathered().type());
5137
5138 // Loosen the caller's candidate filter.
5139 caller_config = caller()->pc()->GetConfiguration();
5140 caller_config.type = webrtc::PeerConnectionInterface::kAll;
5141 caller()->pc()->SetConfiguration(caller_config);
5142 // We should have gathered a new host candidate.
5143 EXPECT_EQ_WAIT(cricket::LOCAL_PORT_TYPE,
5144 caller()->last_candidate_gathered().type(), kDefaultTimeout);
5145
5146 // Loosen the callee's candidate filter.
5147 callee_config = callee()->pc()->GetConfiguration();
5148 callee_config.type = webrtc::PeerConnectionInterface::kAll;
5149 callee()->pc()->SetConfiguration(callee_config);
5150 EXPECT_EQ_WAIT(cricket::LOCAL_PORT_TYPE,
5151 callee()->last_candidate_gathered().type(), kDefaultTimeout);
Steve Antond91969e2019-05-30 12:27:03 -07005152
5153 // PeerConnections must be closed before ScopedFieldTrials goes out of scope.
5154 ClosePeerConnections();
Qingsi Wangc129c352019-04-18 10:41:58 -07005155}
5156
Mirko Bonadeic84f6612019-01-31 12:20:57 +01005157INSTANTIATE_TEST_SUITE_P(PeerConnectionIntegrationTest,
5158 PeerConnectionIntegrationTest,
5159 Values(SdpSemantics::kPlanB,
5160 SdpSemantics::kUnifiedPlan));
Steve Antond3679212018-01-17 17:41:02 -08005161
Steve Anton74255ff2018-01-24 18:32:57 -08005162// Tests that verify interoperability between Plan B and Unified Plan
5163// PeerConnections.
5164class PeerConnectionIntegrationInteropTest
Seth Hampson2f0d7022018-02-20 11:54:42 -08005165 : public PeerConnectionIntegrationBaseTest,
Steve Anton74255ff2018-01-24 18:32:57 -08005166 public ::testing::WithParamInterface<
5167 std::tuple<SdpSemantics, SdpSemantics>> {
5168 protected:
Seth Hampson2f0d7022018-02-20 11:54:42 -08005169 // Setting the SdpSemantics for the base test to kDefault does not matter
5170 // because we specify not to use the test semantics when creating
5171 // PeerConnectionWrappers.
Steve Anton74255ff2018-01-24 18:32:57 -08005172 PeerConnectionIntegrationInteropTest()
Steve Anton3acffc32018-04-12 17:21:03 -07005173 : PeerConnectionIntegrationBaseTest(SdpSemantics::kPlanB),
Seth Hampson2f0d7022018-02-20 11:54:42 -08005174 caller_semantics_(std::get<0>(GetParam())),
Steve Anton74255ff2018-01-24 18:32:57 -08005175 callee_semantics_(std::get<1>(GetParam())) {}
5176
5177 bool CreatePeerConnectionWrappersWithSemantics() {
Steve Anton3acffc32018-04-12 17:21:03 -07005178 return CreatePeerConnectionWrappersWithSdpSemantics(caller_semantics_,
5179 callee_semantics_);
Steve Anton74255ff2018-01-24 18:32:57 -08005180 }
5181
5182 const SdpSemantics caller_semantics_;
5183 const SdpSemantics callee_semantics_;
5184};
5185
5186TEST_P(PeerConnectionIntegrationInteropTest, NoMediaLocalToNoMediaRemote) {
5187 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
5188 ConnectFakeSignaling();
5189
5190 caller()->CreateAndSetAndSignalOffer();
5191 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5192}
5193
5194TEST_P(PeerConnectionIntegrationInteropTest, OneAudioLocalToNoMediaRemote) {
5195 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
5196 ConnectFakeSignaling();
5197 auto audio_sender = caller()->AddAudioTrack();
5198
5199 caller()->CreateAndSetAndSignalOffer();
5200 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5201
5202 // Verify that one audio receiver has been created on the remote and that it
5203 // has the same track ID as the sending track.
5204 auto receivers = callee()->pc()->GetReceivers();
5205 ASSERT_EQ(1u, receivers.size());
5206 EXPECT_EQ(cricket::MEDIA_TYPE_AUDIO, receivers[0]->media_type());
5207 EXPECT_EQ(receivers[0]->track()->id(), audio_sender->track()->id());
5208
Seth Hampson2f0d7022018-02-20 11:54:42 -08005209 MediaExpectations media_expectations;
5210 media_expectations.CalleeExpectsSomeAudio();
5211 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08005212}
5213
5214TEST_P(PeerConnectionIntegrationInteropTest, OneAudioOneVideoToNoMediaRemote) {
5215 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
5216 ConnectFakeSignaling();
5217 auto video_sender = caller()->AddVideoTrack();
5218 auto audio_sender = caller()->AddAudioTrack();
5219
5220 caller()->CreateAndSetAndSignalOffer();
5221 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5222
5223 // Verify that one audio and one video receiver have been created on the
5224 // remote and that they have the same track IDs as the sending tracks.
5225 auto audio_receivers =
5226 callee()->GetReceiversOfType(cricket::MEDIA_TYPE_AUDIO);
5227 ASSERT_EQ(1u, audio_receivers.size());
5228 EXPECT_EQ(audio_receivers[0]->track()->id(), audio_sender->track()->id());
5229 auto video_receivers =
5230 callee()->GetReceiversOfType(cricket::MEDIA_TYPE_VIDEO);
5231 ASSERT_EQ(1u, video_receivers.size());
5232 EXPECT_EQ(video_receivers[0]->track()->id(), video_sender->track()->id());
5233
Seth Hampson2f0d7022018-02-20 11:54:42 -08005234 MediaExpectations media_expectations;
5235 media_expectations.CalleeExpectsSomeAudioAndVideo();
5236 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08005237}
5238
5239TEST_P(PeerConnectionIntegrationInteropTest,
5240 OneAudioOneVideoLocalToOneAudioOneVideoRemote) {
5241 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
5242 ConnectFakeSignaling();
5243 caller()->AddAudioVideoTracks();
5244 callee()->AddAudioVideoTracks();
5245
5246 caller()->CreateAndSetAndSignalOffer();
5247 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5248
Seth Hampson2f0d7022018-02-20 11:54:42 -08005249 MediaExpectations media_expectations;
5250 media_expectations.ExpectBidirectionalAudioAndVideo();
5251 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08005252}
5253
5254TEST_P(PeerConnectionIntegrationInteropTest,
5255 ReverseRolesOneAudioLocalToOneVideoRemote) {
5256 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
5257 ConnectFakeSignaling();
5258 caller()->AddAudioTrack();
5259 callee()->AddVideoTrack();
5260
5261 caller()->CreateAndSetAndSignalOffer();
5262 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5263
5264 // Verify that only the audio track has been negotiated.
5265 EXPECT_EQ(0u, caller()->GetReceiversOfType(cricket::MEDIA_TYPE_VIDEO).size());
5266 // Might also check that the callee's NegotiationNeeded flag is set.
5267
5268 // Reverse roles.
5269 callee()->CreateAndSetAndSignalOffer();
5270 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5271
Seth Hampson2f0d7022018-02-20 11:54:42 -08005272 MediaExpectations media_expectations;
5273 media_expectations.CallerExpectsSomeVideo();
5274 media_expectations.CalleeExpectsSomeAudio();
5275 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08005276}
5277
Mirko Bonadeic84f6612019-01-31 12:20:57 +01005278INSTANTIATE_TEST_SUITE_P(
Steve Antonba42e992018-04-09 14:10:01 -07005279 PeerConnectionIntegrationTest,
5280 PeerConnectionIntegrationInteropTest,
5281 Values(std::make_tuple(SdpSemantics::kPlanB, SdpSemantics::kUnifiedPlan),
5282 std::make_tuple(SdpSemantics::kUnifiedPlan, SdpSemantics::kPlanB)));
5283
5284// Test that if the Unified Plan side offers two video tracks then the Plan B
5285// side will only see the first one and ignore the second.
5286TEST_F(PeerConnectionIntegrationTestPlanB, TwoVideoUnifiedPlanToNoMediaPlanB) {
Steve Anton3acffc32018-04-12 17:21:03 -07005287 ASSERT_TRUE(CreatePeerConnectionWrappersWithSdpSemantics(
5288 SdpSemantics::kUnifiedPlan, SdpSemantics::kPlanB));
Steve Anton74255ff2018-01-24 18:32:57 -08005289 ConnectFakeSignaling();
5290 auto first_sender = caller()->AddVideoTrack();
5291 caller()->AddVideoTrack();
5292
5293 caller()->CreateAndSetAndSignalOffer();
5294 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5295
5296 // Verify that there is only one receiver and it corresponds to the first
5297 // added track.
5298 auto receivers = callee()->pc()->GetReceivers();
5299 ASSERT_EQ(1u, receivers.size());
5300 EXPECT_TRUE(receivers[0]->track()->enabled());
5301 EXPECT_EQ(first_sender->track()->id(), receivers[0]->track()->id());
5302
Seth Hampson2f0d7022018-02-20 11:54:42 -08005303 MediaExpectations media_expectations;
5304 media_expectations.CalleeExpectsSomeVideo();
5305 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08005306}
5307
Steve Anton2bed3972019-01-04 17:04:30 -08005308// Test that if the initial offer tagged BUNDLE section is rejected due to its
5309// associated RtpTransceiver being stopped and another transceiver is added,
5310// then renegotiation causes the callee to receive the new video track without
5311// error.
5312// This is a regression test for bugs.webrtc.org/9954
5313TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
5314 ReOfferWithStoppedBundleTaggedTransceiver) {
5315 RTCConfiguration config;
5316 config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
5317 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
5318 ConnectFakeSignaling();
5319 auto audio_transceiver_or_error =
5320 caller()->pc()->AddTransceiver(caller()->CreateLocalAudioTrack());
5321 ASSERT_TRUE(audio_transceiver_or_error.ok());
5322 auto audio_transceiver = audio_transceiver_or_error.MoveValue();
5323
5324 caller()->CreateAndSetAndSignalOffer();
5325 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5326 {
5327 MediaExpectations media_expectations;
5328 media_expectations.CalleeExpectsSomeAudio();
5329 ASSERT_TRUE(ExpectNewFrames(media_expectations));
5330 }
5331
5332 audio_transceiver->Stop();
5333 caller()->pc()->AddTransceiver(caller()->CreateLocalVideoTrack());
5334
5335 caller()->CreateAndSetAndSignalOffer();
5336 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5337 {
5338 MediaExpectations media_expectations;
5339 media_expectations.CalleeExpectsSomeVideo();
5340 ASSERT_TRUE(ExpectNewFrames(media_expectations));
5341 }
5342}
5343
Harald Alvestrandd61f2a72019-05-08 20:20:59 +02005344#ifdef HAVE_SCTP
5345
5346TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
5347 EndToEndCallWithBundledSctpDataChannel) {
5348 ASSERT_TRUE(CreatePeerConnectionWrappers());
5349 ConnectFakeSignaling();
5350 caller()->CreateDataChannel();
5351 caller()->AddAudioVideoTracks();
5352 callee()->AddAudioVideoTracks();
5353 caller()->SetGeneratedSdpMunger(MakeSpecCompliantSctpOffer);
5354 caller()->CreateAndSetAndSignalOffer();
5355 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5356 // Ensure that media and data are multiplexed on the same DTLS transport.
5357 // This only works on Unified Plan, because transports are not exposed in plan
5358 // B.
5359 auto sctp_info = caller()->pc()->GetSctpTransport()->Information();
5360 EXPECT_EQ(sctp_info.dtls_transport(),
5361 caller()->pc()->GetSenders()[0]->dtls_transport());
5362}
5363
5364#endif // HAVE_SCTP
5365
deadbeef1dcb1642017-03-29 21:08:16 -07005366} // namespace
Mirko Bonadeiab64e8a2018-12-12 12:10:18 +01005367} // namespace webrtc
deadbeef1dcb1642017-03-29 21:08:16 -07005368
5369#endif // if !defined(THREAD_SANITIZER)