blob: f87a4476f2e15fc426c915177ce54a78c92bc6ea [file] [log] [blame]
deadbeef1dcb1642017-03-29 21:08:16 -07001/*
2 * Copyright 2012 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11// Disable for TSan v2, see
12// https://code.google.com/p/webrtc/issues/detail?id=1205 for details.
13#if !defined(THREAD_SANITIZER)
14
15#include <stdio.h>
16
deadbeef1dcb1642017-03-29 21:08:16 -070017#include <functional>
18#include <list>
19#include <map>
20#include <memory>
21#include <utility>
22#include <vector>
23
Steve Anton64b626b2019-01-28 17:25:26 -080024#include "absl/algorithm/container.h"
Qingsi Wang1dac6d82018-12-12 15:28:47 -080025#include "absl/memory/memory.h"
Karl Wiberg1b0eae32017-10-17 14:48:54 +020026#include "api/audio_codecs/builtin_audio_decoder_factory.h"
27#include "api/audio_codecs/builtin_audio_encoder_factory.h"
Steve Anton10542f22019-01-11 09:11:00 -080028#include "api/media_stream_interface.h"
29#include "api/peer_connection_interface.h"
30#include "api/peer_connection_proxy.h"
31#include "api/rtp_receiver_interface.h"
Bjorn Mellem175aa2e2018-11-08 11:23:22 -080032#include "api/test/loopback_media_transport.h"
Steve Anton10542f22019-01-11 09:11:00 -080033#include "api/uma_metrics.h"
Anders Carlsson67537952018-05-03 11:28:29 +020034#include "api/video_codecs/builtin_video_decoder_factory.h"
35#include "api/video_codecs/builtin_video_encoder_factory.h"
36#include "api/video_codecs/sdp_video_format.h"
Qingsi Wang7685e862018-06-11 20:15:46 -070037#include "call/call.h"
38#include "logging/rtc_event_log/fake_rtc_event_log_factory.h"
Bjorn Tereliusb8b3c992019-01-09 11:15:34 +010039#include "logging/rtc_event_log/rtc_event_log_factory.h"
Qingsi Wang7685e862018-06-11 20:15:46 -070040#include "logging/rtc_event_log/rtc_event_log_factory_interface.h"
Steve Anton10542f22019-01-11 09:11:00 -080041#include "media/engine/fake_webrtc_video_engine.h"
42#include "media/engine/webrtc_media_engine.h"
Qingsi Wang7685e862018-06-11 20:15:46 -070043#include "modules/audio_processing/include/audio_processing.h"
Steve Anton10542f22019-01-11 09:11:00 -080044#include "p2p/base/mock_async_resolver.h"
45#include "p2p/base/p2p_constants.h"
46#include "p2p/base/port_interface.h"
47#include "p2p/base/test_stun_server.h"
48#include "p2p/base/test_turn_customizer.h"
49#include "p2p/base/test_turn_server.h"
50#include "p2p/client/basic_port_allocator.h"
51#include "pc/dtmf_sender.h"
52#include "pc/local_audio_source.h"
53#include "pc/media_session.h"
54#include "pc/peer_connection.h"
55#include "pc/peer_connection_factory.h"
56#include "pc/rtp_media_utils.h"
57#include "pc/session_description.h"
58#include "pc/test/fake_audio_capture_module.h"
59#include "pc/test/fake_periodic_video_track_source.h"
60#include "pc/test/fake_rtc_certificate_generator.h"
61#include "pc/test/fake_video_track_renderer.h"
62#include "pc/test/mock_peer_connection_observers.h"
Jonas Olssonb75d9e92019-02-22 10:33:29 +010063#include "rtc_base/fake_clock.h"
Steve Anton10542f22019-01-11 09:11:00 -080064#include "rtc_base/fake_network.h"
65#include "rtc_base/firewall_socket_server.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020066#include "rtc_base/gunit.h"
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +020067#include "rtc_base/numerics/safe_conversions.h"
Steve Anton10542f22019-01-11 09:11:00 -080068#include "rtc_base/test_certificate_verifier.h"
69#include "rtc_base/time_utils.h"
70#include "rtc_base/virtual_socket_server.h"
Mirko Bonadei17f48782018-09-28 08:51:10 +020071#include "system_wrappers/include/metrics.h"
Elad Alon99c3fe52017-10-13 16:29:40 +020072#include "test/gmock.h"
deadbeef1dcb1642017-03-29 21:08:16 -070073
Mirko Bonadeiab64e8a2018-12-12 12:10:18 +010074namespace webrtc {
75namespace {
76
77using ::cricket::ContentInfo;
78using ::cricket::StreamParams;
79using ::rtc::SocketAddress;
80using ::testing::_;
Seth Hampson2f0d7022018-02-20 11:54:42 -080081using ::testing::Combine;
Steve Anton64b626b2019-01-28 17:25:26 -080082using ::testing::Contains;
Steve Antonede9ca52017-10-16 13:04:27 -070083using ::testing::ElementsAre;
Qingsi Wang1dac6d82018-12-12 15:28:47 -080084using ::testing::NiceMock;
Steve Anton64b626b2019-01-28 17:25:26 -080085using ::testing::Return;
Zach Stein6fcdc2f2018-08-23 16:25:55 -070086using ::testing::SetArgPointee;
Steve Antonffa6ce42018-11-30 09:26:08 -080087using ::testing::UnorderedElementsAreArray;
Mirko Bonadeiab64e8a2018-12-12 12:10:18 +010088using ::testing::Values;
Steve Anton74255ff2018-01-24 18:32:57 -080089using RTCConfiguration = PeerConnectionInterface::RTCConfiguration;
deadbeef1dcb1642017-03-29 21:08:16 -070090
91static const int kDefaultTimeout = 10000;
92static const int kMaxWaitForStatsMs = 3000;
93static const int kMaxWaitForActivationMs = 5000;
94static const int kMaxWaitForFramesMs = 10000;
95// Default number of audio/video frames to wait for before considering a test
96// successful.
97static const int kDefaultExpectedAudioFrameCount = 3;
98static const int kDefaultExpectedVideoFrameCount = 3;
99
deadbeef1dcb1642017-03-29 21:08:16 -0700100static const char kDataChannelLabel[] = "data_channel";
101
102// SRTP cipher name negotiated by the tests. This must be updated if the
103// default changes.
Taylor Brandstetterfd350d72018-04-03 16:29:26 -0700104static const int kDefaultSrtpCryptoSuite = rtc::SRTP_AES128_CM_SHA1_80;
deadbeef1dcb1642017-03-29 21:08:16 -0700105static const int kDefaultSrtpCryptoSuiteGcm = rtc::SRTP_AEAD_AES_256_GCM;
106
Steve Antonede9ca52017-10-16 13:04:27 -0700107static const SocketAddress kDefaultLocalAddress("192.168.1.1", 0);
108
deadbeef1dcb1642017-03-29 21:08:16 -0700109// Helper function for constructing offer/answer options to initiate an ICE
110// restart.
111PeerConnectionInterface::RTCOfferAnswerOptions IceRestartOfferAnswerOptions() {
112 PeerConnectionInterface::RTCOfferAnswerOptions options;
113 options.ice_restart = true;
114 return options;
115}
116
deadbeefd8ad7882017-04-18 16:01:17 -0700117// Remove all stream information (SSRCs, track IDs, etc.) and "msid-semantic"
118// attribute from received SDP, simulating a legacy endpoint.
119void RemoveSsrcsAndMsids(cricket::SessionDescription* desc) {
120 for (ContentInfo& content : desc->contents()) {
Steve Antonb1c1de12017-12-21 15:14:30 -0800121 content.media_description()->mutable_streams().clear();
deadbeefd8ad7882017-04-18 16:01:17 -0700122 }
123 desc->set_msid_supported(false);
Henrik Boström5b147782018-12-04 11:25:05 +0100124 desc->set_msid_signaling(0);
deadbeefd8ad7882017-04-18 16:01:17 -0700125}
126
Seth Hampson5897a6e2018-04-03 11:16:33 -0700127// Removes all stream information besides the stream ids, simulating an
128// endpoint that only signals a=msid lines to convey stream_ids.
129void RemoveSsrcsAndKeepMsids(cricket::SessionDescription* desc) {
130 for (ContentInfo& content : desc->contents()) {
Steve Antondf527fd2018-04-27 15:52:03 -0700131 std::string track_id;
Seth Hampson5897a6e2018-04-03 11:16:33 -0700132 std::vector<std::string> stream_ids;
133 if (!content.media_description()->streams().empty()) {
Steve Antondf527fd2018-04-27 15:52:03 -0700134 const StreamParams& first_stream =
135 content.media_description()->streams()[0];
136 track_id = first_stream.id;
137 stream_ids = first_stream.stream_ids();
Seth Hampson5897a6e2018-04-03 11:16:33 -0700138 }
139 content.media_description()->mutable_streams().clear();
Steve Antondf527fd2018-04-27 15:52:03 -0700140 StreamParams new_stream;
141 new_stream.id = track_id;
Seth Hampson5897a6e2018-04-03 11:16:33 -0700142 new_stream.set_stream_ids(stream_ids);
143 content.media_description()->AddStream(new_stream);
144 }
145}
146
zhihuangf8164932017-05-19 13:09:47 -0700147int FindFirstMediaStatsIndexByKind(
148 const std::string& kind,
149 const std::vector<const webrtc::RTCMediaStreamTrackStats*>&
150 media_stats_vec) {
151 for (size_t i = 0; i < media_stats_vec.size(); i++) {
152 if (media_stats_vec[i]->kind.ValueToString() == kind) {
153 return i;
154 }
155 }
156 return -1;
157}
158
deadbeef1dcb1642017-03-29 21:08:16 -0700159class SignalingMessageReceiver {
160 public:
Steve Antona3a92c22017-12-07 10:27:41 -0800161 virtual void ReceiveSdpMessage(SdpType type, const std::string& msg) = 0;
deadbeef1dcb1642017-03-29 21:08:16 -0700162 virtual void ReceiveIceMessage(const std::string& sdp_mid,
163 int sdp_mline_index,
164 const std::string& msg) = 0;
165
166 protected:
167 SignalingMessageReceiver() {}
168 virtual ~SignalingMessageReceiver() {}
169};
170
171class MockRtpReceiverObserver : public webrtc::RtpReceiverObserverInterface {
172 public:
173 explicit MockRtpReceiverObserver(cricket::MediaType media_type)
174 : expected_media_type_(media_type) {}
175
176 void OnFirstPacketReceived(cricket::MediaType media_type) override {
177 ASSERT_EQ(expected_media_type_, media_type);
178 first_packet_received_ = true;
179 }
180
181 bool first_packet_received() const { return first_packet_received_; }
182
183 virtual ~MockRtpReceiverObserver() {}
184
185 private:
186 bool first_packet_received_ = false;
187 cricket::MediaType expected_media_type_;
188};
189
190// Helper class that wraps a peer connection, observes it, and can accept
191// signaling messages from another wrapper.
192//
193// Uses a fake network, fake A/V capture, and optionally fake
194// encoders/decoders, though they aren't used by default since they don't
195// advertise support of any codecs.
Steve Anton94286cb2017-09-26 16:20:19 -0700196// TODO(steveanton): See how this could become a subclass of
Seth Hampson2f0d7022018-02-20 11:54:42 -0800197// PeerConnectionWrapper defined in peerconnectionwrapper.h.
deadbeef1dcb1642017-03-29 21:08:16 -0700198class PeerConnectionWrapper : public webrtc::PeerConnectionObserver,
Steve Anton15324772018-01-16 10:26:49 -0800199 public SignalingMessageReceiver {
deadbeef1dcb1642017-03-29 21:08:16 -0700200 public:
201 // Different factory methods for convenience.
202 // TODO(deadbeef): Could use the pattern of:
203 //
204 // PeerConnectionWrapper =
205 // WrapperBuilder.WithConfig(...).WithOptions(...).build();
206 //
207 // To reduce some code duplication.
208 static PeerConnectionWrapper* CreateWithDtlsIdentityStore(
209 const std::string& debug_name,
210 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator,
211 rtc::Thread* network_thread,
212 rtc::Thread* worker_thread) {
213 PeerConnectionWrapper* client(new PeerConnectionWrapper(debug_name));
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700214 webrtc::PeerConnectionDependencies dependencies(nullptr);
215 dependencies.cert_generator = std::move(cert_generator);
Niels Möllerf06f9232018-08-07 12:32:18 +0200216 if (!client->Init(nullptr, nullptr, std::move(dependencies), network_thread,
Bjorn Mellem175aa2e2018-11-08 11:23:22 -0800217 worker_thread, nullptr,
218 /*media_transport_factory=*/nullptr)) {
deadbeef1dcb1642017-03-29 21:08:16 -0700219 delete client;
220 return nullptr;
221 }
222 return client;
223 }
224
deadbeef2f425aa2017-04-14 10:41:32 -0700225 webrtc::PeerConnectionFactoryInterface* pc_factory() const {
226 return peer_connection_factory_.get();
227 }
228
deadbeef1dcb1642017-03-29 21:08:16 -0700229 webrtc::PeerConnectionInterface* pc() const { return peer_connection_.get(); }
230
231 // If a signaling message receiver is set (via ConnectFakeSignaling), this
232 // will set the whole offer/answer exchange in motion. Just need to wait for
233 // the signaling state to reach "stable".
234 void CreateAndSetAndSignalOffer() {
235 auto offer = CreateOffer();
236 ASSERT_NE(nullptr, offer);
237 EXPECT_TRUE(SetLocalDescriptionAndSendSdpMessage(std::move(offer)));
238 }
239
240 // Sets the options to be used when CreateAndSetAndSignalOffer is called, or
241 // when a remote offer is received (via fake signaling) and an answer is
242 // generated. By default, uses default options.
243 void SetOfferAnswerOptions(
244 const PeerConnectionInterface::RTCOfferAnswerOptions& options) {
245 offer_answer_options_ = options;
246 }
247
248 // Set a callback to be invoked when SDP is received via the fake signaling
249 // channel, which provides an opportunity to munge (modify) the SDP. This is
250 // used to test SDP being applied that a PeerConnection would normally not
251 // generate, but a non-JSEP endpoint might.
252 void SetReceivedSdpMunger(
253 std::function<void(cricket::SessionDescription*)> munger) {
Mirko Bonadeic61ce0d2017-11-21 17:04:20 +0100254 received_sdp_munger_ = std::move(munger);
deadbeef1dcb1642017-03-29 21:08:16 -0700255 }
256
deadbeefc964d0b2017-04-03 10:03:35 -0700257 // Similar to the above, but this is run on SDP immediately after it's
deadbeef1dcb1642017-03-29 21:08:16 -0700258 // generated.
259 void SetGeneratedSdpMunger(
260 std::function<void(cricket::SessionDescription*)> munger) {
Mirko Bonadeic61ce0d2017-11-21 17:04:20 +0100261 generated_sdp_munger_ = std::move(munger);
deadbeef1dcb1642017-03-29 21:08:16 -0700262 }
263
Seth Hampson2f0d7022018-02-20 11:54:42 -0800264 // Set a callback to be invoked when a remote offer is received via the fake
265 // signaling channel. This provides an opportunity to change the
266 // PeerConnection state before an answer is created and sent to the caller.
267 void SetRemoteOfferHandler(std::function<void()> handler) {
268 remote_offer_handler_ = std::move(handler);
269 }
270
Qingsi Wang1dac6d82018-12-12 15:28:47 -0800271 void SetRemoteAsyncResolver(rtc::MockAsyncResolver* resolver) {
272 remote_async_resolver_ = resolver;
Zach Stein6fcdc2f2018-08-23 16:25:55 -0700273 }
274
Steve Antonede9ca52017-10-16 13:04:27 -0700275 // Every ICE connection state in order that has been seen by the observer.
276 std::vector<PeerConnectionInterface::IceConnectionState>
277 ice_connection_state_history() const {
278 return ice_connection_state_history_;
279 }
Steve Anton6f25b092017-10-23 09:39:20 -0700280 void clear_ice_connection_state_history() {
281 ice_connection_state_history_.clear();
282 }
Steve Antonede9ca52017-10-16 13:04:27 -0700283
Jonas Olsson635474e2018-10-18 15:58:17 +0200284 // Every PeerConnection state in order that has been seen by the observer.
285 std::vector<PeerConnectionInterface::PeerConnectionState>
286 peer_connection_state_history() const {
287 return peer_connection_state_history_;
288 }
289
Steve Antonede9ca52017-10-16 13:04:27 -0700290 // Every ICE gathering state in order that has been seen by the observer.
291 std::vector<PeerConnectionInterface::IceGatheringState>
292 ice_gathering_state_history() const {
293 return ice_gathering_state_history_;
deadbeef1dcb1642017-03-29 21:08:16 -0700294 }
295
Steve Anton15324772018-01-16 10:26:49 -0800296 void AddAudioVideoTracks() {
297 AddAudioTrack();
298 AddVideoTrack();
deadbeef1dcb1642017-03-29 21:08:16 -0700299 }
300
Steve Anton74255ff2018-01-24 18:32:57 -0800301 rtc::scoped_refptr<RtpSenderInterface> AddAudioTrack() {
302 return AddTrack(CreateLocalAudioTrack());
303 }
deadbeef1dcb1642017-03-29 21:08:16 -0700304
Steve Anton74255ff2018-01-24 18:32:57 -0800305 rtc::scoped_refptr<RtpSenderInterface> AddVideoTrack() {
306 return AddTrack(CreateLocalVideoTrack());
307 }
deadbeef1dcb1642017-03-29 21:08:16 -0700308
309 rtc::scoped_refptr<webrtc::AudioTrackInterface> CreateLocalAudioTrack() {
Niels Möller2d02e082018-05-21 11:23:35 +0200310 cricket::AudioOptions options;
deadbeef1dcb1642017-03-29 21:08:16 -0700311 // Disable highpass filter so that we can get all the test audio frames.
Niels Möller2d02e082018-05-21 11:23:35 +0200312 options.highpass_filter = false;
deadbeef1dcb1642017-03-29 21:08:16 -0700313 rtc::scoped_refptr<webrtc::AudioSourceInterface> source =
Niels Möller2d02e082018-05-21 11:23:35 +0200314 peer_connection_factory_->CreateAudioSource(options);
deadbeef1dcb1642017-03-29 21:08:16 -0700315 // TODO(perkj): Test audio source when it is implemented. Currently audio
316 // always use the default input.
deadbeefb1a15d72017-09-07 14:12:05 -0700317 return peer_connection_factory_->CreateAudioTrack(rtc::CreateRandomUuid(),
deadbeef1dcb1642017-03-29 21:08:16 -0700318 source);
319 }
320
321 rtc::scoped_refptr<webrtc::VideoTrackInterface> CreateLocalVideoTrack() {
Johannes Kron965e7942018-09-13 15:36:20 +0200322 webrtc::FakePeriodicVideoSource::Config config;
323 config.timestamp_offset_ms = rtc::TimeMillis();
324 return CreateLocalVideoTrackInternal(config);
deadbeef1dcb1642017-03-29 21:08:16 -0700325 }
326
327 rtc::scoped_refptr<webrtc::VideoTrackInterface>
Niels Möller5c7efe72018-05-11 10:34:46 +0200328 CreateLocalVideoTrackWithConfig(
329 webrtc::FakePeriodicVideoSource::Config config) {
330 return CreateLocalVideoTrackInternal(config);
deadbeef1dcb1642017-03-29 21:08:16 -0700331 }
332
333 rtc::scoped_refptr<webrtc::VideoTrackInterface>
334 CreateLocalVideoTrackWithRotation(webrtc::VideoRotation rotation) {
Niels Möller5c7efe72018-05-11 10:34:46 +0200335 webrtc::FakePeriodicVideoSource::Config config;
336 config.rotation = rotation;
Johannes Kron965e7942018-09-13 15:36:20 +0200337 config.timestamp_offset_ms = rtc::TimeMillis();
Niels Möller5c7efe72018-05-11 10:34:46 +0200338 return CreateLocalVideoTrackInternal(config);
deadbeef1dcb1642017-03-29 21:08:16 -0700339 }
340
Steve Anton74255ff2018-01-24 18:32:57 -0800341 rtc::scoped_refptr<RtpSenderInterface> AddTrack(
342 rtc::scoped_refptr<MediaStreamTrackInterface> track,
Seth Hampson845e8782018-03-02 11:34:10 -0800343 const std::vector<std::string>& stream_ids = {}) {
344 auto result = pc()->AddTrack(track, stream_ids);
Steve Anton15324772018-01-16 10:26:49 -0800345 EXPECT_EQ(RTCErrorType::NONE, result.error().type());
Steve Anton74255ff2018-01-24 18:32:57 -0800346 return result.MoveValue();
347 }
348
349 std::vector<rtc::scoped_refptr<RtpReceiverInterface>> GetReceiversOfType(
350 cricket::MediaType media_type) {
351 std::vector<rtc::scoped_refptr<RtpReceiverInterface>> receivers;
Mirko Bonadei739baf02019-01-27 17:29:42 +0100352 for (const auto& receiver : pc()->GetReceivers()) {
Steve Anton74255ff2018-01-24 18:32:57 -0800353 if (receiver->media_type() == media_type) {
354 receivers.push_back(receiver);
355 }
356 }
357 return receivers;
deadbeef1dcb1642017-03-29 21:08:16 -0700358 }
359
Seth Hampson2f0d7022018-02-20 11:54:42 -0800360 rtc::scoped_refptr<RtpTransceiverInterface> GetFirstTransceiverOfType(
361 cricket::MediaType media_type) {
362 for (auto transceiver : pc()->GetTransceivers()) {
363 if (transceiver->receiver()->media_type() == media_type) {
364 return transceiver;
365 }
366 }
367 return nullptr;
368 }
369
deadbeef1dcb1642017-03-29 21:08:16 -0700370 bool SignalingStateStable() {
371 return pc()->signaling_state() == webrtc::PeerConnectionInterface::kStable;
372 }
373
374 void CreateDataChannel() { CreateDataChannel(nullptr); }
375
376 void CreateDataChannel(const webrtc::DataChannelInit* init) {
Steve Antonda6c0952017-10-23 11:41:54 -0700377 CreateDataChannel(kDataChannelLabel, init);
378 }
379
380 void CreateDataChannel(const std::string& label,
381 const webrtc::DataChannelInit* init) {
382 data_channel_ = pc()->CreateDataChannel(label, init);
deadbeef1dcb1642017-03-29 21:08:16 -0700383 ASSERT_TRUE(data_channel_.get() != nullptr);
384 data_observer_.reset(new MockDataChannelObserver(data_channel_));
385 }
386
387 DataChannelInterface* data_channel() { return data_channel_; }
388 const MockDataChannelObserver* data_observer() const {
389 return data_observer_.get();
390 }
391
392 int audio_frames_received() const {
393 return fake_audio_capture_module_->frames_received();
394 }
395
396 // Takes minimum of video frames received for each track.
397 //
398 // Can be used like:
399 // EXPECT_GE(expected_frames, min_video_frames_received_per_track());
400 //
401 // To ensure that all video tracks received at least a certain number of
402 // frames.
403 int min_video_frames_received_per_track() const {
404 int min_frames = INT_MAX;
Anders Carlsson5f2bb622018-05-14 09:48:06 +0200405 if (fake_video_renderers_.empty()) {
406 return 0;
deadbeef1dcb1642017-03-29 21:08:16 -0700407 }
deadbeef1dcb1642017-03-29 21:08:16 -0700408
Anders Carlsson5f2bb622018-05-14 09:48:06 +0200409 for (const auto& pair : fake_video_renderers_) {
410 min_frames = std::min(min_frames, pair.second->num_rendered_frames());
deadbeef1dcb1642017-03-29 21:08:16 -0700411 }
Anders Carlsson5f2bb622018-05-14 09:48:06 +0200412 return min_frames;
deadbeef1dcb1642017-03-29 21:08:16 -0700413 }
414
415 // Returns a MockStatsObserver in a state after stats gathering finished,
416 // which can be used to access the gathered stats.
deadbeefd8ad7882017-04-18 16:01:17 -0700417 rtc::scoped_refptr<MockStatsObserver> OldGetStatsForTrack(
deadbeef1dcb1642017-03-29 21:08:16 -0700418 webrtc::MediaStreamTrackInterface* track) {
419 rtc::scoped_refptr<MockStatsObserver> observer(
420 new rtc::RefCountedObject<MockStatsObserver>());
421 EXPECT_TRUE(peer_connection_->GetStats(
422 observer, nullptr, PeerConnectionInterface::kStatsOutputLevelStandard));
423 EXPECT_TRUE_WAIT(observer->called(), kDefaultTimeout);
424 return observer;
425 }
426
427 // Version that doesn't take a track "filter", and gathers all stats.
deadbeefd8ad7882017-04-18 16:01:17 -0700428 rtc::scoped_refptr<MockStatsObserver> OldGetStats() {
429 return OldGetStatsForTrack(nullptr);
430 }
431
432 // Synchronously gets stats and returns them. If it times out, fails the test
433 // and returns null.
434 rtc::scoped_refptr<const webrtc::RTCStatsReport> NewGetStats() {
435 rtc::scoped_refptr<webrtc::MockRTCStatsCollectorCallback> callback(
436 new rtc::RefCountedObject<webrtc::MockRTCStatsCollectorCallback>());
437 peer_connection_->GetStats(callback);
438 EXPECT_TRUE_WAIT(callback->called(), kDefaultTimeout);
439 return callback->report();
deadbeef1dcb1642017-03-29 21:08:16 -0700440 }
441
442 int rendered_width() {
443 EXPECT_FALSE(fake_video_renderers_.empty());
444 return fake_video_renderers_.empty()
445 ? 0
446 : fake_video_renderers_.begin()->second->width();
447 }
448
449 int rendered_height() {
450 EXPECT_FALSE(fake_video_renderers_.empty());
451 return fake_video_renderers_.empty()
452 ? 0
453 : fake_video_renderers_.begin()->second->height();
454 }
455
456 double rendered_aspect_ratio() {
457 if (rendered_height() == 0) {
458 return 0.0;
459 }
460 return static_cast<double>(rendered_width()) / rendered_height();
461 }
462
463 webrtc::VideoRotation rendered_rotation() {
464 EXPECT_FALSE(fake_video_renderers_.empty());
465 return fake_video_renderers_.empty()
466 ? webrtc::kVideoRotation_0
467 : fake_video_renderers_.begin()->second->rotation();
468 }
469
470 int local_rendered_width() {
471 return local_video_renderer_ ? local_video_renderer_->width() : 0;
472 }
473
474 int local_rendered_height() {
475 return local_video_renderer_ ? local_video_renderer_->height() : 0;
476 }
477
478 double local_rendered_aspect_ratio() {
479 if (local_rendered_height() == 0) {
480 return 0.0;
481 }
482 return static_cast<double>(local_rendered_width()) /
483 local_rendered_height();
484 }
485
486 size_t number_of_remote_streams() {
487 if (!pc()) {
488 return 0;
489 }
490 return pc()->remote_streams()->count();
491 }
492
493 StreamCollectionInterface* remote_streams() const {
494 if (!pc()) {
495 ADD_FAILURE();
496 return nullptr;
497 }
498 return pc()->remote_streams();
499 }
500
501 StreamCollectionInterface* local_streams() {
502 if (!pc()) {
503 ADD_FAILURE();
504 return nullptr;
505 }
506 return pc()->local_streams();
507 }
508
509 webrtc::PeerConnectionInterface::SignalingState signaling_state() {
510 return pc()->signaling_state();
511 }
512
513 webrtc::PeerConnectionInterface::IceConnectionState ice_connection_state() {
514 return pc()->ice_connection_state();
515 }
516
Jonas Olsson7a6739e2019-01-15 16:31:55 +0100517 webrtc::PeerConnectionInterface::IceConnectionState
518 standardized_ice_connection_state() {
519 return pc()->standardized_ice_connection_state();
520 }
521
deadbeef1dcb1642017-03-29 21:08:16 -0700522 webrtc::PeerConnectionInterface::IceGatheringState ice_gathering_state() {
523 return pc()->ice_gathering_state();
524 }
525
526 // Returns a MockRtpReceiverObserver for each RtpReceiver returned by
527 // GetReceivers. They're updated automatically when a remote offer/answer
528 // from the fake signaling channel is applied, or when
529 // ResetRtpReceiverObservers below is called.
530 const std::vector<std::unique_ptr<MockRtpReceiverObserver>>&
531 rtp_receiver_observers() {
532 return rtp_receiver_observers_;
533 }
534
535 void ResetRtpReceiverObservers() {
536 rtp_receiver_observers_.clear();
Mirko Bonadeic61ce0d2017-11-21 17:04:20 +0100537 for (const rtc::scoped_refptr<RtpReceiverInterface>& receiver :
538 pc()->GetReceivers()) {
deadbeef1dcb1642017-03-29 21:08:16 -0700539 std::unique_ptr<MockRtpReceiverObserver> observer(
540 new MockRtpReceiverObserver(receiver->media_type()));
541 receiver->SetObserver(observer.get());
542 rtp_receiver_observers_.push_back(std::move(observer));
543 }
544 }
545
Steve Antonede9ca52017-10-16 13:04:27 -0700546 rtc::FakeNetworkManager* network() const {
547 return fake_network_manager_.get();
548 }
549 cricket::PortAllocator* port_allocator() const { return port_allocator_; }
550
Qingsi Wang7685e862018-06-11 20:15:46 -0700551 webrtc::FakeRtcEventLogFactory* event_log_factory() const {
552 return event_log_factory_;
553 }
554
deadbeef1dcb1642017-03-29 21:08:16 -0700555 private:
556 explicit PeerConnectionWrapper(const std::string& debug_name)
557 : debug_name_(debug_name) {}
558
Bjorn Mellem175aa2e2018-11-08 11:23:22 -0800559 bool Init(
560 const PeerConnectionFactory::Options* options,
561 const PeerConnectionInterface::RTCConfiguration* config,
562 webrtc::PeerConnectionDependencies dependencies,
563 rtc::Thread* network_thread,
564 rtc::Thread* worker_thread,
565 std::unique_ptr<webrtc::FakeRtcEventLogFactory> event_log_factory,
566 std::unique_ptr<webrtc::MediaTransportFactory> media_transport_factory) {
deadbeef1dcb1642017-03-29 21:08:16 -0700567 // There's an error in this test code if Init ends up being called twice.
568 RTC_DCHECK(!peer_connection_);
569 RTC_DCHECK(!peer_connection_factory_);
570
571 fake_network_manager_.reset(new rtc::FakeNetworkManager());
Steve Antonede9ca52017-10-16 13:04:27 -0700572 fake_network_manager_->AddInterface(kDefaultLocalAddress);
deadbeef1dcb1642017-03-29 21:08:16 -0700573
574 std::unique_ptr<cricket::PortAllocator> port_allocator(
575 new cricket::BasicPortAllocator(fake_network_manager_.get()));
Steve Antonede9ca52017-10-16 13:04:27 -0700576 port_allocator_ = port_allocator.get();
deadbeef1dcb1642017-03-29 21:08:16 -0700577 fake_audio_capture_module_ = FakeAudioCaptureModule::Create();
578 if (!fake_audio_capture_module_) {
579 return false;
580 }
deadbeef1dcb1642017-03-29 21:08:16 -0700581 rtc::Thread* const signaling_thread = rtc::Thread::Current();
Qingsi Wang7685e862018-06-11 20:15:46 -0700582
583 webrtc::PeerConnectionFactoryDependencies pc_factory_dependencies;
584 pc_factory_dependencies.network_thread = network_thread;
585 pc_factory_dependencies.worker_thread = worker_thread;
586 pc_factory_dependencies.signaling_thread = signaling_thread;
587 pc_factory_dependencies.media_engine =
588 cricket::WebRtcMediaEngineFactory::Create(
589 rtc::scoped_refptr<webrtc::AudioDeviceModule>(
590 fake_audio_capture_module_),
591 webrtc::CreateBuiltinAudioEncoderFactory(),
592 webrtc::CreateBuiltinAudioDecoderFactory(),
593 webrtc::CreateBuiltinVideoEncoderFactory(),
Qingsi Wang59844ce2018-11-01 04:45:53 +0000594 webrtc::CreateBuiltinVideoDecoderFactory(), nullptr,
Qingsi Wang7685e862018-06-11 20:15:46 -0700595 webrtc::AudioProcessingBuilder().Create());
596 pc_factory_dependencies.call_factory = webrtc::CreateCallFactory();
597 if (event_log_factory) {
598 event_log_factory_ = event_log_factory.get();
599 pc_factory_dependencies.event_log_factory = std::move(event_log_factory);
600 } else {
601 pc_factory_dependencies.event_log_factory =
602 webrtc::CreateRtcEventLogFactory();
603 }
Bjorn Mellem175aa2e2018-11-08 11:23:22 -0800604 if (media_transport_factory) {
605 pc_factory_dependencies.media_transport_factory =
606 std::move(media_transport_factory);
607 }
Qingsi Wang7685e862018-06-11 20:15:46 -0700608 peer_connection_factory_ = webrtc::CreateModularPeerConnectionFactory(
609 std::move(pc_factory_dependencies));
610
deadbeef1dcb1642017-03-29 21:08:16 -0700611 if (!peer_connection_factory_) {
612 return false;
613 }
614 if (options) {
615 peer_connection_factory_->SetOptions(*options);
616 }
Seth Hampson2f0d7022018-02-20 11:54:42 -0800617 if (config) {
618 sdp_semantics_ = config->sdp_semantics;
619 }
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700620
621 dependencies.allocator = std::move(port_allocator);
Niels Möllerf06f9232018-08-07 12:32:18 +0200622 peer_connection_ = CreatePeerConnection(config, std::move(dependencies));
deadbeef1dcb1642017-03-29 21:08:16 -0700623 return peer_connection_.get() != nullptr;
624 }
625
626 rtc::scoped_refptr<webrtc::PeerConnectionInterface> CreatePeerConnection(
deadbeef1dcb1642017-03-29 21:08:16 -0700627 const PeerConnectionInterface::RTCConfiguration* config,
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700628 webrtc::PeerConnectionDependencies dependencies) {
deadbeef1dcb1642017-03-29 21:08:16 -0700629 PeerConnectionInterface::RTCConfiguration modified_config;
630 // If |config| is null, this will result in a default configuration being
631 // used.
632 if (config) {
633 modified_config = *config;
634 }
635 // Disable resolution adaptation; we don't want it interfering with the
636 // test results.
637 // TODO(deadbeef): Do something more robust. Since we're testing for aspect
638 // ratios and not specific resolutions, is this even necessary?
639 modified_config.set_cpu_adaptation(false);
640
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700641 dependencies.observer = this;
deadbeef1dcb1642017-03-29 21:08:16 -0700642 return peer_connection_factory_->CreatePeerConnection(
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700643 modified_config, std::move(dependencies));
deadbeef1dcb1642017-03-29 21:08:16 -0700644 }
645
646 void set_signaling_message_receiver(
647 SignalingMessageReceiver* signaling_message_receiver) {
648 signaling_message_receiver_ = signaling_message_receiver;
649 }
650
651 void set_signaling_delay_ms(int delay_ms) { signaling_delay_ms_ = delay_ms; }
652
Steve Antonede9ca52017-10-16 13:04:27 -0700653 void set_signal_ice_candidates(bool signal) {
654 signal_ice_candidates_ = signal;
655 }
656
deadbeef1dcb1642017-03-29 21:08:16 -0700657 rtc::scoped_refptr<webrtc::VideoTrackInterface> CreateLocalVideoTrackInternal(
Niels Möller5c7efe72018-05-11 10:34:46 +0200658 webrtc::FakePeriodicVideoSource::Config config) {
deadbeef1dcb1642017-03-29 21:08:16 -0700659 // Set max frame rate to 10fps to reduce the risk of test flakiness.
660 // TODO(deadbeef): Do something more robust.
Niels Möller5c7efe72018-05-11 10:34:46 +0200661 config.frame_interval_ms = 100;
deadbeef1dcb1642017-03-29 21:08:16 -0700662
Niels Möller5c7efe72018-05-11 10:34:46 +0200663 video_track_sources_.emplace_back(
Niels Möller0f405822018-05-17 09:16:41 +0200664 new rtc::RefCountedObject<webrtc::FakePeriodicVideoTrackSource>(
665 config, false /* remote */));
deadbeef1dcb1642017-03-29 21:08:16 -0700666 rtc::scoped_refptr<webrtc::VideoTrackInterface> track(
Niels Möller5c7efe72018-05-11 10:34:46 +0200667 peer_connection_factory_->CreateVideoTrack(
668 rtc::CreateRandomUuid(), video_track_sources_.back()));
deadbeef1dcb1642017-03-29 21:08:16 -0700669 if (!local_video_renderer_) {
670 local_video_renderer_.reset(new webrtc::FakeVideoTrackRenderer(track));
671 }
672 return track;
673 }
674
675 void HandleIncomingOffer(const std::string& msg) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100676 RTC_LOG(LS_INFO) << debug_name_ << ": HandleIncomingOffer";
Steve Antona3a92c22017-12-07 10:27:41 -0800677 std::unique_ptr<SessionDescriptionInterface> desc =
678 webrtc::CreateSessionDescription(SdpType::kOffer, msg);
deadbeef1dcb1642017-03-29 21:08:16 -0700679 if (received_sdp_munger_) {
680 received_sdp_munger_(desc->description());
681 }
682
683 EXPECT_TRUE(SetRemoteDescription(std::move(desc)));
684 // Setting a remote description may have changed the number of receivers,
685 // so reset the receiver observers.
686 ResetRtpReceiverObservers();
Seth Hampson2f0d7022018-02-20 11:54:42 -0800687 if (remote_offer_handler_) {
688 remote_offer_handler_();
689 }
deadbeef1dcb1642017-03-29 21:08:16 -0700690 auto answer = CreateAnswer();
691 ASSERT_NE(nullptr, answer);
692 EXPECT_TRUE(SetLocalDescriptionAndSendSdpMessage(std::move(answer)));
693 }
694
695 void HandleIncomingAnswer(const std::string& msg) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100696 RTC_LOG(LS_INFO) << debug_name_ << ": HandleIncomingAnswer";
Steve Antona3a92c22017-12-07 10:27:41 -0800697 std::unique_ptr<SessionDescriptionInterface> desc =
698 webrtc::CreateSessionDescription(SdpType::kAnswer, msg);
deadbeef1dcb1642017-03-29 21:08:16 -0700699 if (received_sdp_munger_) {
700 received_sdp_munger_(desc->description());
701 }
702
703 EXPECT_TRUE(SetRemoteDescription(std::move(desc)));
704 // Set the RtpReceiverObserver after receivers are created.
705 ResetRtpReceiverObservers();
706 }
707
708 // Returns null on failure.
709 std::unique_ptr<SessionDescriptionInterface> CreateOffer() {
710 rtc::scoped_refptr<MockCreateSessionDescriptionObserver> observer(
711 new rtc::RefCountedObject<MockCreateSessionDescriptionObserver>());
712 pc()->CreateOffer(observer, offer_answer_options_);
713 return WaitForDescriptionFromObserver(observer);
714 }
715
716 // Returns null on failure.
717 std::unique_ptr<SessionDescriptionInterface> CreateAnswer() {
718 rtc::scoped_refptr<MockCreateSessionDescriptionObserver> observer(
719 new rtc::RefCountedObject<MockCreateSessionDescriptionObserver>());
720 pc()->CreateAnswer(observer, offer_answer_options_);
721 return WaitForDescriptionFromObserver(observer);
722 }
723
724 std::unique_ptr<SessionDescriptionInterface> WaitForDescriptionFromObserver(
Mirko Bonadeic61ce0d2017-11-21 17:04:20 +0100725 MockCreateSessionDescriptionObserver* observer) {
deadbeef1dcb1642017-03-29 21:08:16 -0700726 EXPECT_EQ_WAIT(true, observer->called(), kDefaultTimeout);
727 if (!observer->result()) {
728 return nullptr;
729 }
730 auto description = observer->MoveDescription();
731 if (generated_sdp_munger_) {
732 generated_sdp_munger_(description->description());
733 }
734 return description;
735 }
736
737 // Setting the local description and sending the SDP message over the fake
738 // signaling channel are combined into the same method because the SDP
739 // message needs to be sent as soon as SetLocalDescription finishes, without
740 // waiting for the observer to be called. This ensures that ICE candidates
741 // don't outrace the description.
742 bool SetLocalDescriptionAndSendSdpMessage(
743 std::unique_ptr<SessionDescriptionInterface> desc) {
744 rtc::scoped_refptr<MockSetSessionDescriptionObserver> observer(
745 new rtc::RefCountedObject<MockSetSessionDescriptionObserver>());
Mirko Bonadei675513b2017-11-09 11:09:25 +0100746 RTC_LOG(LS_INFO) << debug_name_ << ": SetLocalDescriptionAndSendSdpMessage";
Steve Antona3a92c22017-12-07 10:27:41 -0800747 SdpType type = desc->GetType();
deadbeef1dcb1642017-03-29 21:08:16 -0700748 std::string sdp;
749 EXPECT_TRUE(desc->ToString(&sdp));
750 pc()->SetLocalDescription(observer, desc.release());
Seth Hampson2f0d7022018-02-20 11:54:42 -0800751 if (sdp_semantics_ == SdpSemantics::kUnifiedPlan) {
752 RemoveUnusedVideoRenderers();
753 }
deadbeef1dcb1642017-03-29 21:08:16 -0700754 // As mentioned above, we need to send the message immediately after
755 // SetLocalDescription.
756 SendSdpMessage(type, sdp);
757 EXPECT_TRUE_WAIT(observer->called(), kDefaultTimeout);
758 return true;
759 }
760
761 bool SetRemoteDescription(std::unique_ptr<SessionDescriptionInterface> desc) {
762 rtc::scoped_refptr<MockSetSessionDescriptionObserver> observer(
763 new rtc::RefCountedObject<MockSetSessionDescriptionObserver>());
Mirko Bonadei675513b2017-11-09 11:09:25 +0100764 RTC_LOG(LS_INFO) << debug_name_ << ": SetRemoteDescription";
deadbeef1dcb1642017-03-29 21:08:16 -0700765 pc()->SetRemoteDescription(observer, desc.release());
Seth Hampson2f0d7022018-02-20 11:54:42 -0800766 if (sdp_semantics_ == SdpSemantics::kUnifiedPlan) {
767 RemoveUnusedVideoRenderers();
768 }
deadbeef1dcb1642017-03-29 21:08:16 -0700769 EXPECT_TRUE_WAIT(observer->called(), kDefaultTimeout);
770 return observer->result();
771 }
772
Seth Hampson2f0d7022018-02-20 11:54:42 -0800773 // This is a work around to remove unused fake_video_renderers from
774 // transceivers that have either stopped or are no longer receiving.
775 void RemoveUnusedVideoRenderers() {
776 auto transceivers = pc()->GetTransceivers();
777 for (auto& transceiver : transceivers) {
778 if (transceiver->receiver()->media_type() != cricket::MEDIA_TYPE_VIDEO) {
779 continue;
780 }
781 // Remove fake video renderers from any stopped transceivers.
782 if (transceiver->stopped()) {
783 auto it =
784 fake_video_renderers_.find(transceiver->receiver()->track()->id());
785 if (it != fake_video_renderers_.end()) {
786 fake_video_renderers_.erase(it);
787 }
788 }
789 // Remove fake video renderers from any transceivers that are no longer
790 // receiving.
791 if ((transceiver->current_direction() &&
792 !webrtc::RtpTransceiverDirectionHasRecv(
793 *transceiver->current_direction()))) {
794 auto it =
795 fake_video_renderers_.find(transceiver->receiver()->track()->id());
796 if (it != fake_video_renderers_.end()) {
797 fake_video_renderers_.erase(it);
798 }
799 }
800 }
801 }
802
deadbeef1dcb1642017-03-29 21:08:16 -0700803 // Simulate sending a blob of SDP with delay |signaling_delay_ms_| (0 by
804 // default).
Steve Antona3a92c22017-12-07 10:27:41 -0800805 void SendSdpMessage(SdpType type, const std::string& msg) {
deadbeef1dcb1642017-03-29 21:08:16 -0700806 if (signaling_delay_ms_ == 0) {
807 RelaySdpMessageIfReceiverExists(type, msg);
808 } else {
809 invoker_.AsyncInvokeDelayed<void>(
810 RTC_FROM_HERE, rtc::Thread::Current(),
811 rtc::Bind(&PeerConnectionWrapper::RelaySdpMessageIfReceiverExists,
812 this, type, msg),
813 signaling_delay_ms_);
814 }
815 }
816
Steve Antona3a92c22017-12-07 10:27:41 -0800817 void RelaySdpMessageIfReceiverExists(SdpType type, const std::string& msg) {
deadbeef1dcb1642017-03-29 21:08:16 -0700818 if (signaling_message_receiver_) {
819 signaling_message_receiver_->ReceiveSdpMessage(type, msg);
820 }
821 }
822
823 // Simulate trickling an ICE candidate with delay |signaling_delay_ms_| (0 by
824 // default).
825 void SendIceMessage(const std::string& sdp_mid,
826 int sdp_mline_index,
827 const std::string& msg) {
828 if (signaling_delay_ms_ == 0) {
829 RelayIceMessageIfReceiverExists(sdp_mid, sdp_mline_index, msg);
830 } else {
831 invoker_.AsyncInvokeDelayed<void>(
832 RTC_FROM_HERE, rtc::Thread::Current(),
833 rtc::Bind(&PeerConnectionWrapper::RelayIceMessageIfReceiverExists,
834 this, sdp_mid, sdp_mline_index, msg),
835 signaling_delay_ms_);
836 }
837 }
838
839 void RelayIceMessageIfReceiverExists(const std::string& sdp_mid,
840 int sdp_mline_index,
841 const std::string& msg) {
842 if (signaling_message_receiver_) {
843 signaling_message_receiver_->ReceiveIceMessage(sdp_mid, sdp_mline_index,
844 msg);
845 }
846 }
847
848 // SignalingMessageReceiver callbacks.
Steve Antona3a92c22017-12-07 10:27:41 -0800849 void ReceiveSdpMessage(SdpType type, const std::string& msg) override {
850 if (type == SdpType::kOffer) {
deadbeef1dcb1642017-03-29 21:08:16 -0700851 HandleIncomingOffer(msg);
852 } else {
853 HandleIncomingAnswer(msg);
854 }
855 }
856
857 void ReceiveIceMessage(const std::string& sdp_mid,
858 int sdp_mline_index,
859 const std::string& msg) override {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100860 RTC_LOG(LS_INFO) << debug_name_ << ": ReceiveIceMessage";
deadbeef1dcb1642017-03-29 21:08:16 -0700861 std::unique_ptr<webrtc::IceCandidateInterface> candidate(
862 webrtc::CreateIceCandidate(sdp_mid, sdp_mline_index, msg, nullptr));
863 EXPECT_TRUE(pc()->AddIceCandidate(candidate.get()));
864 }
865
866 // PeerConnectionObserver callbacks.
867 void OnSignalingChange(
868 webrtc::PeerConnectionInterface::SignalingState new_state) override {
869 EXPECT_EQ(pc()->signaling_state(), new_state);
870 }
Steve Anton15324772018-01-16 10:26:49 -0800871 void OnAddTrack(rtc::scoped_refptr<RtpReceiverInterface> receiver,
872 const std::vector<rtc::scoped_refptr<MediaStreamInterface>>&
873 streams) override {
874 if (receiver->media_type() == cricket::MEDIA_TYPE_VIDEO) {
875 rtc::scoped_refptr<VideoTrackInterface> video_track(
876 static_cast<VideoTrackInterface*>(receiver->track().get()));
877 ASSERT_TRUE(fake_video_renderers_.find(video_track->id()) ==
deadbeef1dcb1642017-03-29 21:08:16 -0700878 fake_video_renderers_.end());
Steve Anton15324772018-01-16 10:26:49 -0800879 fake_video_renderers_[video_track->id()] =
Karl Wiberg918f50c2018-07-05 11:40:33 +0200880 absl::make_unique<FakeVideoTrackRenderer>(video_track);
deadbeef1dcb1642017-03-29 21:08:16 -0700881 }
882 }
Steve Anton15324772018-01-16 10:26:49 -0800883 void OnRemoveTrack(
884 rtc::scoped_refptr<RtpReceiverInterface> receiver) override {
885 if (receiver->media_type() == cricket::MEDIA_TYPE_VIDEO) {
886 auto it = fake_video_renderers_.find(receiver->track()->id());
887 RTC_DCHECK(it != fake_video_renderers_.end());
888 fake_video_renderers_.erase(it);
889 }
890 }
deadbeef1dcb1642017-03-29 21:08:16 -0700891 void OnRenegotiationNeeded() override {}
892 void OnIceConnectionChange(
893 webrtc::PeerConnectionInterface::IceConnectionState new_state) override {
894 EXPECT_EQ(pc()->ice_connection_state(), new_state);
Steve Antonede9ca52017-10-16 13:04:27 -0700895 ice_connection_state_history_.push_back(new_state);
deadbeef1dcb1642017-03-29 21:08:16 -0700896 }
Jonas Olsson635474e2018-10-18 15:58:17 +0200897 void OnConnectionChange(
898 webrtc::PeerConnectionInterface::PeerConnectionState new_state) override {
899 peer_connection_state_history_.push_back(new_state);
900 }
901
deadbeef1dcb1642017-03-29 21:08:16 -0700902 void OnIceGatheringChange(
903 webrtc::PeerConnectionInterface::IceGatheringState new_state) override {
deadbeef1dcb1642017-03-29 21:08:16 -0700904 EXPECT_EQ(pc()->ice_gathering_state(), new_state);
Steve Antonede9ca52017-10-16 13:04:27 -0700905 ice_gathering_state_history_.push_back(new_state);
deadbeef1dcb1642017-03-29 21:08:16 -0700906 }
907 void OnIceCandidate(const webrtc::IceCandidateInterface* candidate) override {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100908 RTC_LOG(LS_INFO) << debug_name_ << ": OnIceCandidate";
deadbeef1dcb1642017-03-29 21:08:16 -0700909
Qingsi Wang1dac6d82018-12-12 15:28:47 -0800910 if (remote_async_resolver_) {
911 const auto& local_candidate = candidate->candidate();
912 const auto& mdns_responder = network()->GetMdnsResponderForTesting();
913 if (local_candidate.address().IsUnresolvedIP()) {
914 RTC_DCHECK(local_candidate.type() == cricket::LOCAL_PORT_TYPE);
915 rtc::SocketAddress resolved_addr(local_candidate.address());
916 const auto resolved_ip = mdns_responder->GetMappedAddressForName(
917 local_candidate.address().hostname());
918 RTC_DCHECK(!resolved_ip.IsNil());
919 resolved_addr.SetResolvedIP(resolved_ip);
920 EXPECT_CALL(*remote_async_resolver_, GetResolvedAddress(_, _))
921 .WillOnce(DoAll(SetArgPointee<1>(resolved_addr), Return(true)));
922 EXPECT_CALL(*remote_async_resolver_, Destroy(_));
Zach Stein6fcdc2f2018-08-23 16:25:55 -0700923 }
Zach Stein6fcdc2f2018-08-23 16:25:55 -0700924 }
925
deadbeef1dcb1642017-03-29 21:08:16 -0700926 std::string ice_sdp;
Qingsi Wang1dac6d82018-12-12 15:28:47 -0800927 EXPECT_TRUE(candidate->ToString(&ice_sdp));
Steve Antonede9ca52017-10-16 13:04:27 -0700928 if (signaling_message_receiver_ == nullptr || !signal_ice_candidates_) {
deadbeef1dcb1642017-03-29 21:08:16 -0700929 // Remote party may be deleted.
930 return;
931 }
Qingsi Wang1dac6d82018-12-12 15:28:47 -0800932 SendIceMessage(candidate->sdp_mid(), candidate->sdp_mline_index(), ice_sdp);
deadbeef1dcb1642017-03-29 21:08:16 -0700933 }
934 void OnDataChannel(
935 rtc::scoped_refptr<DataChannelInterface> data_channel) override {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100936 RTC_LOG(LS_INFO) << debug_name_ << ": OnDataChannel";
deadbeef1dcb1642017-03-29 21:08:16 -0700937 data_channel_ = data_channel;
938 data_observer_.reset(new MockDataChannelObserver(data_channel));
939 }
940
deadbeef1dcb1642017-03-29 21:08:16 -0700941 std::string debug_name_;
942
943 std::unique_ptr<rtc::FakeNetworkManager> fake_network_manager_;
944
945 rtc::scoped_refptr<webrtc::PeerConnectionInterface> peer_connection_;
946 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface>
947 peer_connection_factory_;
948
Steve Antonede9ca52017-10-16 13:04:27 -0700949 cricket::PortAllocator* port_allocator_;
deadbeef1dcb1642017-03-29 21:08:16 -0700950 // Needed to keep track of number of frames sent.
951 rtc::scoped_refptr<FakeAudioCaptureModule> fake_audio_capture_module_;
952 // Needed to keep track of number of frames received.
953 std::map<std::string, std::unique_ptr<webrtc::FakeVideoTrackRenderer>>
954 fake_video_renderers_;
955 // Needed to ensure frames aren't received for removed tracks.
956 std::vector<std::unique_ptr<webrtc::FakeVideoTrackRenderer>>
957 removed_fake_video_renderers_;
deadbeef1dcb1642017-03-29 21:08:16 -0700958
959 // For remote peer communication.
960 SignalingMessageReceiver* signaling_message_receiver_ = nullptr;
961 int signaling_delay_ms_ = 0;
Steve Antonede9ca52017-10-16 13:04:27 -0700962 bool signal_ice_candidates_ = true;
deadbeef1dcb1642017-03-29 21:08:16 -0700963
Niels Möller5c7efe72018-05-11 10:34:46 +0200964 // Store references to the video sources we've created, so that we can stop
deadbeef1dcb1642017-03-29 21:08:16 -0700965 // them, if required.
Niels Möller5c7efe72018-05-11 10:34:46 +0200966 std::vector<rtc::scoped_refptr<webrtc::VideoTrackSource>>
967 video_track_sources_;
deadbeef1dcb1642017-03-29 21:08:16 -0700968 // |local_video_renderer_| attached to the first created local video track.
969 std::unique_ptr<webrtc::FakeVideoTrackRenderer> local_video_renderer_;
970
Seth Hampson2f0d7022018-02-20 11:54:42 -0800971 SdpSemantics sdp_semantics_;
deadbeef1dcb1642017-03-29 21:08:16 -0700972 PeerConnectionInterface::RTCOfferAnswerOptions offer_answer_options_;
973 std::function<void(cricket::SessionDescription*)> received_sdp_munger_;
974 std::function<void(cricket::SessionDescription*)> generated_sdp_munger_;
Seth Hampson2f0d7022018-02-20 11:54:42 -0800975 std::function<void()> remote_offer_handler_;
Qingsi Wang1dac6d82018-12-12 15:28:47 -0800976 rtc::MockAsyncResolver* remote_async_resolver_ = nullptr;
deadbeef1dcb1642017-03-29 21:08:16 -0700977 rtc::scoped_refptr<DataChannelInterface> data_channel_;
978 std::unique_ptr<MockDataChannelObserver> data_observer_;
979
980 std::vector<std::unique_ptr<MockRtpReceiverObserver>> rtp_receiver_observers_;
981
Steve Antonede9ca52017-10-16 13:04:27 -0700982 std::vector<PeerConnectionInterface::IceConnectionState>
983 ice_connection_state_history_;
Jonas Olsson635474e2018-10-18 15:58:17 +0200984 std::vector<PeerConnectionInterface::PeerConnectionState>
985 peer_connection_state_history_;
Steve Antonede9ca52017-10-16 13:04:27 -0700986 std::vector<PeerConnectionInterface::IceGatheringState>
987 ice_gathering_state_history_;
deadbeef1dcb1642017-03-29 21:08:16 -0700988
Qingsi Wang7685e862018-06-11 20:15:46 -0700989 webrtc::FakeRtcEventLogFactory* event_log_factory_;
990
deadbeef1dcb1642017-03-29 21:08:16 -0700991 rtc::AsyncInvoker invoker_;
992
Seth Hampson2f0d7022018-02-20 11:54:42 -0800993 friend class PeerConnectionIntegrationBaseTest;
deadbeef1dcb1642017-03-29 21:08:16 -0700994};
995
Elad Alon99c3fe52017-10-13 16:29:40 +0200996class MockRtcEventLogOutput : public webrtc::RtcEventLogOutput {
997 public:
998 virtual ~MockRtcEventLogOutput() = default;
999 MOCK_CONST_METHOD0(IsActive, bool());
1000 MOCK_METHOD1(Write, bool(const std::string&));
1001};
1002
Seth Hampson2f0d7022018-02-20 11:54:42 -08001003// This helper object is used for both specifying how many audio/video frames
1004// are expected to be received for a caller/callee. It provides helper functions
1005// to specify these expectations. The object initially starts in a state of no
1006// expectations.
1007class MediaExpectations {
1008 public:
1009 enum ExpectFrames {
1010 kExpectSomeFrames,
1011 kExpectNoFrames,
1012 kNoExpectation,
1013 };
1014
1015 void ExpectBidirectionalAudioAndVideo() {
1016 ExpectBidirectionalAudio();
1017 ExpectBidirectionalVideo();
1018 }
1019
1020 void ExpectBidirectionalAudio() {
1021 CallerExpectsSomeAudio();
1022 CalleeExpectsSomeAudio();
1023 }
1024
1025 void ExpectNoAudio() {
1026 CallerExpectsNoAudio();
1027 CalleeExpectsNoAudio();
1028 }
1029
1030 void ExpectBidirectionalVideo() {
1031 CallerExpectsSomeVideo();
1032 CalleeExpectsSomeVideo();
1033 }
1034
1035 void ExpectNoVideo() {
1036 CallerExpectsNoVideo();
1037 CalleeExpectsNoVideo();
1038 }
1039
1040 void CallerExpectsSomeAudioAndVideo() {
1041 CallerExpectsSomeAudio();
1042 CallerExpectsSomeVideo();
1043 }
1044
1045 void CalleeExpectsSomeAudioAndVideo() {
1046 CalleeExpectsSomeAudio();
1047 CalleeExpectsSomeVideo();
1048 }
1049
1050 // Caller's audio functions.
1051 void CallerExpectsSomeAudio(
1052 int expected_audio_frames = kDefaultExpectedAudioFrameCount) {
1053 caller_audio_expectation_ = kExpectSomeFrames;
1054 caller_audio_frames_expected_ = expected_audio_frames;
1055 }
1056
1057 void CallerExpectsNoAudio() {
1058 caller_audio_expectation_ = kExpectNoFrames;
1059 caller_audio_frames_expected_ = 0;
1060 }
1061
1062 // Caller's video functions.
1063 void CallerExpectsSomeVideo(
1064 int expected_video_frames = kDefaultExpectedVideoFrameCount) {
1065 caller_video_expectation_ = kExpectSomeFrames;
1066 caller_video_frames_expected_ = expected_video_frames;
1067 }
1068
1069 void CallerExpectsNoVideo() {
1070 caller_video_expectation_ = kExpectNoFrames;
1071 caller_video_frames_expected_ = 0;
1072 }
1073
1074 // Callee's audio functions.
1075 void CalleeExpectsSomeAudio(
1076 int expected_audio_frames = kDefaultExpectedAudioFrameCount) {
1077 callee_audio_expectation_ = kExpectSomeFrames;
1078 callee_audio_frames_expected_ = expected_audio_frames;
1079 }
1080
1081 void CalleeExpectsNoAudio() {
1082 callee_audio_expectation_ = kExpectNoFrames;
1083 callee_audio_frames_expected_ = 0;
1084 }
1085
1086 // Callee's video functions.
1087 void CalleeExpectsSomeVideo(
1088 int expected_video_frames = kDefaultExpectedVideoFrameCount) {
1089 callee_video_expectation_ = kExpectSomeFrames;
1090 callee_video_frames_expected_ = expected_video_frames;
1091 }
1092
1093 void CalleeExpectsNoVideo() {
1094 callee_video_expectation_ = kExpectNoFrames;
1095 callee_video_frames_expected_ = 0;
1096 }
1097
1098 ExpectFrames caller_audio_expectation_ = kNoExpectation;
1099 ExpectFrames caller_video_expectation_ = kNoExpectation;
1100 ExpectFrames callee_audio_expectation_ = kNoExpectation;
1101 ExpectFrames callee_video_expectation_ = kNoExpectation;
1102 int caller_audio_frames_expected_ = 0;
1103 int caller_video_frames_expected_ = 0;
1104 int callee_audio_frames_expected_ = 0;
1105 int callee_video_frames_expected_ = 0;
1106};
1107
deadbeef1dcb1642017-03-29 21:08:16 -07001108// Tests two PeerConnections connecting to each other end-to-end, using a
1109// virtual network, fake A/V capture and fake encoder/decoders. The
1110// PeerConnections share the threads/socket servers, but use separate versions
1111// of everything else (including "PeerConnectionFactory"s).
Seth Hampson2f0d7022018-02-20 11:54:42 -08001112class PeerConnectionIntegrationBaseTest : public testing::Test {
deadbeef1dcb1642017-03-29 21:08:16 -07001113 public:
Seth Hampson2f0d7022018-02-20 11:54:42 -08001114 explicit PeerConnectionIntegrationBaseTest(SdpSemantics sdp_semantics)
1115 : sdp_semantics_(sdp_semantics),
1116 ss_(new rtc::VirtualSocketServer()),
Steve Antonede9ca52017-10-16 13:04:27 -07001117 fss_(new rtc::FirewallSocketServer(ss_.get())),
1118 network_thread_(new rtc::Thread(fss_.get())),
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001119 worker_thread_(rtc::Thread::Create()),
1120 loopback_media_transports_(network_thread_.get()) {
Sebastian Jansson8a793a02018-03-13 15:21:48 +01001121 network_thread_->SetName("PCNetworkThread", this);
1122 worker_thread_->SetName("PCWorkerThread", this);
deadbeef1dcb1642017-03-29 21:08:16 -07001123 RTC_CHECK(network_thread_->Start());
1124 RTC_CHECK(worker_thread_->Start());
Qingsi Wang7fc821d2018-07-12 12:54:53 -07001125 webrtc::metrics::Reset();
deadbeef1dcb1642017-03-29 21:08:16 -07001126 }
1127
Seth Hampson2f0d7022018-02-20 11:54:42 -08001128 ~PeerConnectionIntegrationBaseTest() {
Seth Hampsonaed71642018-06-11 07:41:32 -07001129 // The PeerConnections should deleted before the TurnCustomizers.
1130 // A TurnPort is created with a raw pointer to a TurnCustomizer. The
1131 // TurnPort has the same lifetime as the PeerConnection, so it's expected
1132 // that the TurnCustomizer outlives the life of the PeerConnection or else
1133 // when Send() is called it will hit a seg fault.
deadbeef1dcb1642017-03-29 21:08:16 -07001134 if (caller_) {
1135 caller_->set_signaling_message_receiver(nullptr);
Seth Hampsonaed71642018-06-11 07:41:32 -07001136 delete SetCallerPcWrapperAndReturnCurrent(nullptr);
deadbeef1dcb1642017-03-29 21:08:16 -07001137 }
1138 if (callee_) {
1139 callee_->set_signaling_message_receiver(nullptr);
Seth Hampsonaed71642018-06-11 07:41:32 -07001140 delete SetCalleePcWrapperAndReturnCurrent(nullptr);
deadbeef1dcb1642017-03-29 21:08:16 -07001141 }
Seth Hampsonaed71642018-06-11 07:41:32 -07001142
1143 // If turn servers were created for the test they need to be destroyed on
1144 // the network thread.
1145 network_thread()->Invoke<void>(RTC_FROM_HERE, [this] {
1146 turn_servers_.clear();
1147 turn_customizers_.clear();
1148 });
deadbeef1dcb1642017-03-29 21:08:16 -07001149 }
1150
1151 bool SignalingStateStable() {
1152 return caller_->SignalingStateStable() && callee_->SignalingStateStable();
1153 }
1154
deadbeef71452802017-05-07 17:21:01 -07001155 bool DtlsConnected() {
Alex Loiko9289eda2018-11-23 16:18:59 +00001156 // TODO(deadbeef): kIceConnectionConnected currently means both ICE and DTLS
1157 // are connected. This is an important distinction. Once we have separate
1158 // ICE and DTLS state, this check needs to use the DTLS state.
1159 return (callee()->ice_connection_state() ==
1160 webrtc::PeerConnectionInterface::kIceConnectionConnected ||
1161 callee()->ice_connection_state() ==
1162 webrtc::PeerConnectionInterface::kIceConnectionCompleted) &&
1163 (caller()->ice_connection_state() ==
1164 webrtc::PeerConnectionInterface::kIceConnectionConnected ||
1165 caller()->ice_connection_state() ==
1166 webrtc::PeerConnectionInterface::kIceConnectionCompleted);
deadbeef71452802017-05-07 17:21:01 -07001167 }
1168
Qingsi Wang7685e862018-06-11 20:15:46 -07001169 // When |event_log_factory| is null, the default implementation of the event
1170 // log factory will be used.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001171 std::unique_ptr<PeerConnectionWrapper> CreatePeerConnectionWrapper(
1172 const std::string& debug_name,
Seth Hampson2f0d7022018-02-20 11:54:42 -08001173 const PeerConnectionFactory::Options* options,
1174 const RTCConfiguration* config,
Qingsi Wang7685e862018-06-11 20:15:46 -07001175 webrtc::PeerConnectionDependencies dependencies,
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001176 std::unique_ptr<webrtc::FakeRtcEventLogFactory> event_log_factory,
1177 std::unique_ptr<webrtc::MediaTransportFactory> media_transport_factory) {
Seth Hampson2f0d7022018-02-20 11:54:42 -08001178 RTCConfiguration modified_config;
1179 if (config) {
1180 modified_config = *config;
1181 }
Steve Anton3acffc32018-04-12 17:21:03 -07001182 modified_config.sdp_semantics = sdp_semantics_;
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001183 if (!dependencies.cert_generator) {
1184 dependencies.cert_generator =
Karl Wiberg918f50c2018-07-05 11:40:33 +02001185 absl::make_unique<FakeRTCCertificateGenerator>();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001186 }
1187 std::unique_ptr<PeerConnectionWrapper> client(
1188 new PeerConnectionWrapper(debug_name));
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001189
Niels Möllerf06f9232018-08-07 12:32:18 +02001190 if (!client->Init(options, &modified_config, std::move(dependencies),
1191 network_thread_.get(), worker_thread_.get(),
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001192 std::move(event_log_factory),
1193 std::move(media_transport_factory))) {
Seth Hampson2f0d7022018-02-20 11:54:42 -08001194 return nullptr;
1195 }
1196 return client;
1197 }
1198
Qingsi Wang7685e862018-06-11 20:15:46 -07001199 std::unique_ptr<PeerConnectionWrapper>
1200 CreatePeerConnectionWrapperWithFakeRtcEventLog(
1201 const std::string& debug_name,
Qingsi Wang7685e862018-06-11 20:15:46 -07001202 const PeerConnectionFactory::Options* options,
1203 const RTCConfiguration* config,
1204 webrtc::PeerConnectionDependencies dependencies) {
1205 std::unique_ptr<webrtc::FakeRtcEventLogFactory> event_log_factory(
1206 new webrtc::FakeRtcEventLogFactory(rtc::Thread::Current()));
Niels Möllerf06f9232018-08-07 12:32:18 +02001207 return CreatePeerConnectionWrapper(debug_name, options, config,
Qingsi Wang7685e862018-06-11 20:15:46 -07001208 std::move(dependencies),
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001209 std::move(event_log_factory),
1210 /*media_transport_factory=*/nullptr);
Qingsi Wang7685e862018-06-11 20:15:46 -07001211 }
1212
deadbeef1dcb1642017-03-29 21:08:16 -07001213 bool CreatePeerConnectionWrappers() {
1214 return CreatePeerConnectionWrappersWithConfig(
1215 PeerConnectionInterface::RTCConfiguration(),
1216 PeerConnectionInterface::RTCConfiguration());
1217 }
1218
Steve Anton3acffc32018-04-12 17:21:03 -07001219 bool CreatePeerConnectionWrappersWithSdpSemantics(
1220 SdpSemantics caller_semantics,
1221 SdpSemantics callee_semantics) {
1222 // Can't specify the sdp_semantics in the passed-in configuration since it
1223 // will be overwritten by CreatePeerConnectionWrapper with whatever is
1224 // stored in sdp_semantics_. So get around this by modifying the instance
1225 // variable before calling CreatePeerConnectionWrapper for the caller and
1226 // callee PeerConnections.
1227 SdpSemantics original_semantics = sdp_semantics_;
1228 sdp_semantics_ = caller_semantics;
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001229 caller_ = CreatePeerConnectionWrapper(
Niels Möllerf06f9232018-08-07 12:32:18 +02001230 "Caller", nullptr, nullptr, webrtc::PeerConnectionDependencies(nullptr),
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001231 nullptr, /*media_transport_factory=*/nullptr);
Steve Anton3acffc32018-04-12 17:21:03 -07001232 sdp_semantics_ = callee_semantics;
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001233 callee_ = CreatePeerConnectionWrapper(
Niels Möllerf06f9232018-08-07 12:32:18 +02001234 "Callee", nullptr, nullptr, webrtc::PeerConnectionDependencies(nullptr),
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001235 nullptr, /*media_transport_factory=*/nullptr);
Steve Anton3acffc32018-04-12 17:21:03 -07001236 sdp_semantics_ = original_semantics;
1237 return caller_ && callee_;
1238 }
1239
deadbeef1dcb1642017-03-29 21:08:16 -07001240 bool CreatePeerConnectionWrappersWithConfig(
1241 const PeerConnectionInterface::RTCConfiguration& caller_config,
1242 const PeerConnectionInterface::RTCConfiguration& callee_config) {
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001243 caller_ = CreatePeerConnectionWrapper(
Niels Möllerf06f9232018-08-07 12:32:18 +02001244 "Caller", nullptr, &caller_config,
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001245 webrtc::PeerConnectionDependencies(nullptr), nullptr,
1246 /*media_transport_factory=*/nullptr);
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001247 callee_ = CreatePeerConnectionWrapper(
Niels Möllerf06f9232018-08-07 12:32:18 +02001248 "Callee", nullptr, &callee_config,
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001249 webrtc::PeerConnectionDependencies(nullptr), nullptr,
1250 /*media_transport_factory=*/nullptr);
1251 return caller_ && callee_;
1252 }
1253
1254 bool CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
1255 const PeerConnectionInterface::RTCConfiguration& caller_config,
1256 const PeerConnectionInterface::RTCConfiguration& callee_config,
1257 std::unique_ptr<webrtc::MediaTransportFactory> caller_factory,
1258 std::unique_ptr<webrtc::MediaTransportFactory> callee_factory) {
1259 caller_ =
1260 CreatePeerConnectionWrapper("Caller", nullptr, &caller_config,
1261 webrtc::PeerConnectionDependencies(nullptr),
1262 nullptr, std::move(caller_factory));
1263 callee_ =
1264 CreatePeerConnectionWrapper("Callee", nullptr, &callee_config,
1265 webrtc::PeerConnectionDependencies(nullptr),
1266 nullptr, std::move(callee_factory));
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001267 return caller_ && callee_;
1268 }
1269
1270 bool CreatePeerConnectionWrappersWithConfigAndDeps(
1271 const PeerConnectionInterface::RTCConfiguration& caller_config,
1272 webrtc::PeerConnectionDependencies caller_dependencies,
1273 const PeerConnectionInterface::RTCConfiguration& callee_config,
1274 webrtc::PeerConnectionDependencies callee_dependencies) {
1275 caller_ =
Niels Möllerf06f9232018-08-07 12:32:18 +02001276 CreatePeerConnectionWrapper("Caller", nullptr, &caller_config,
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001277 std::move(caller_dependencies), nullptr,
1278 /*media_transport_factory=*/nullptr);
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001279 callee_ =
Niels Möllerf06f9232018-08-07 12:32:18 +02001280 CreatePeerConnectionWrapper("Callee", nullptr, &callee_config,
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001281 std::move(callee_dependencies), nullptr,
1282 /*media_transport_factory=*/nullptr);
deadbeef1dcb1642017-03-29 21:08:16 -07001283 return caller_ && callee_;
1284 }
1285
1286 bool CreatePeerConnectionWrappersWithOptions(
1287 const PeerConnectionFactory::Options& caller_options,
1288 const PeerConnectionFactory::Options& callee_options) {
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001289 caller_ = CreatePeerConnectionWrapper(
Niels Möllerf06f9232018-08-07 12:32:18 +02001290 "Caller", &caller_options, nullptr,
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001291 webrtc::PeerConnectionDependencies(nullptr), nullptr,
1292 /*media_transport_factory=*/nullptr);
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001293 callee_ = CreatePeerConnectionWrapper(
Niels Möllerf06f9232018-08-07 12:32:18 +02001294 "Callee", &callee_options, nullptr,
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001295 webrtc::PeerConnectionDependencies(nullptr), nullptr,
1296 /*media_transport_factory=*/nullptr);
Qingsi Wang7685e862018-06-11 20:15:46 -07001297 return caller_ && callee_;
1298 }
1299
1300 bool CreatePeerConnectionWrappersWithFakeRtcEventLog() {
1301 PeerConnectionInterface::RTCConfiguration default_config;
1302 caller_ = CreatePeerConnectionWrapperWithFakeRtcEventLog(
Niels Möllerf06f9232018-08-07 12:32:18 +02001303 "Caller", nullptr, &default_config,
Qingsi Wang7685e862018-06-11 20:15:46 -07001304 webrtc::PeerConnectionDependencies(nullptr));
1305 callee_ = CreatePeerConnectionWrapperWithFakeRtcEventLog(
Niels Möllerf06f9232018-08-07 12:32:18 +02001306 "Callee", nullptr, &default_config,
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001307 webrtc::PeerConnectionDependencies(nullptr));
deadbeef1dcb1642017-03-29 21:08:16 -07001308 return caller_ && callee_;
1309 }
1310
Seth Hampson2f0d7022018-02-20 11:54:42 -08001311 std::unique_ptr<PeerConnectionWrapper>
1312 CreatePeerConnectionWrapperWithAlternateKey() {
deadbeef1dcb1642017-03-29 21:08:16 -07001313 std::unique_ptr<FakeRTCCertificateGenerator> cert_generator(
1314 new FakeRTCCertificateGenerator());
1315 cert_generator->use_alternate_key();
1316
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001317 webrtc::PeerConnectionDependencies dependencies(nullptr);
1318 dependencies.cert_generator = std::move(cert_generator);
Niels Möllerf06f9232018-08-07 12:32:18 +02001319 return CreatePeerConnectionWrapper("New Peer", nullptr, nullptr,
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001320 std::move(dependencies), nullptr,
1321 /*media_transport_factory=*/nullptr);
deadbeef1dcb1642017-03-29 21:08:16 -07001322 }
1323
Seth Hampsonaed71642018-06-11 07:41:32 -07001324 cricket::TestTurnServer* CreateTurnServer(
1325 rtc::SocketAddress internal_address,
1326 rtc::SocketAddress external_address,
1327 cricket::ProtocolType type = cricket::ProtocolType::PROTO_UDP,
1328 const std::string& common_name = "test turn server") {
1329 rtc::Thread* thread = network_thread();
1330 std::unique_ptr<cricket::TestTurnServer> turn_server =
1331 network_thread()->Invoke<std::unique_ptr<cricket::TestTurnServer>>(
1332 RTC_FROM_HERE,
1333 [thread, internal_address, external_address, type, common_name] {
Karl Wiberg918f50c2018-07-05 11:40:33 +02001334 return absl::make_unique<cricket::TestTurnServer>(
Seth Hampsonaed71642018-06-11 07:41:32 -07001335 thread, internal_address, external_address, type,
1336 /*ignore_bad_certs=*/true, common_name);
1337 });
1338 turn_servers_.push_back(std::move(turn_server));
1339 // Interactions with the turn server should be done on the network thread.
1340 return turn_servers_.back().get();
1341 }
1342
1343 cricket::TestTurnCustomizer* CreateTurnCustomizer() {
1344 std::unique_ptr<cricket::TestTurnCustomizer> turn_customizer =
1345 network_thread()->Invoke<std::unique_ptr<cricket::TestTurnCustomizer>>(
1346 RTC_FROM_HERE,
Karl Wiberg918f50c2018-07-05 11:40:33 +02001347 [] { return absl::make_unique<cricket::TestTurnCustomizer>(); });
Seth Hampsonaed71642018-06-11 07:41:32 -07001348 turn_customizers_.push_back(std::move(turn_customizer));
1349 // Interactions with the turn customizer should be done on the network
1350 // thread.
1351 return turn_customizers_.back().get();
1352 }
1353
1354 // Checks that the function counters for a TestTurnCustomizer are greater than
1355 // 0.
1356 void ExpectTurnCustomizerCountersIncremented(
1357 cricket::TestTurnCustomizer* turn_customizer) {
1358 unsigned int allow_channel_data_counter =
1359 network_thread()->Invoke<unsigned int>(
1360 RTC_FROM_HERE, [turn_customizer] {
1361 return turn_customizer->allow_channel_data_cnt_;
1362 });
1363 EXPECT_GT(allow_channel_data_counter, 0u);
1364 unsigned int modify_counter = network_thread()->Invoke<unsigned int>(
1365 RTC_FROM_HERE,
1366 [turn_customizer] { return turn_customizer->modify_cnt_; });
1367 EXPECT_GT(modify_counter, 0u);
1368 }
1369
deadbeef1dcb1642017-03-29 21:08:16 -07001370 // Once called, SDP blobs and ICE candidates will be automatically signaled
1371 // between PeerConnections.
1372 void ConnectFakeSignaling() {
1373 caller_->set_signaling_message_receiver(callee_.get());
1374 callee_->set_signaling_message_receiver(caller_.get());
1375 }
1376
Steve Antonede9ca52017-10-16 13:04:27 -07001377 // Once called, SDP blobs will be automatically signaled between
1378 // PeerConnections. Note that ICE candidates will not be signaled unless they
1379 // are in the exchanged SDP blobs.
1380 void ConnectFakeSignalingForSdpOnly() {
1381 ConnectFakeSignaling();
1382 SetSignalIceCandidates(false);
1383 }
1384
deadbeef1dcb1642017-03-29 21:08:16 -07001385 void SetSignalingDelayMs(int delay_ms) {
1386 caller_->set_signaling_delay_ms(delay_ms);
1387 callee_->set_signaling_delay_ms(delay_ms);
1388 }
1389
Steve Antonede9ca52017-10-16 13:04:27 -07001390 void SetSignalIceCandidates(bool signal) {
1391 caller_->set_signal_ice_candidates(signal);
1392 callee_->set_signal_ice_candidates(signal);
1393 }
1394
deadbeef1dcb1642017-03-29 21:08:16 -07001395 // Messages may get lost on the unreliable DataChannel, so we send multiple
1396 // times to avoid test flakiness.
1397 void SendRtpDataWithRetries(webrtc::DataChannelInterface* dc,
1398 const std::string& data,
1399 int retries) {
1400 for (int i = 0; i < retries; ++i) {
1401 dc->Send(DataBuffer(data));
1402 }
1403 }
1404
1405 rtc::Thread* network_thread() { return network_thread_.get(); }
1406
1407 rtc::VirtualSocketServer* virtual_socket_server() { return ss_.get(); }
1408
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001409 webrtc::MediaTransportPair* loopback_media_transports() {
1410 return &loopback_media_transports_;
1411 }
1412
deadbeef1dcb1642017-03-29 21:08:16 -07001413 PeerConnectionWrapper* caller() { return caller_.get(); }
1414
1415 // Set the |caller_| to the |wrapper| passed in and return the
1416 // original |caller_|.
1417 PeerConnectionWrapper* SetCallerPcWrapperAndReturnCurrent(
1418 PeerConnectionWrapper* wrapper) {
1419 PeerConnectionWrapper* old = caller_.release();
1420 caller_.reset(wrapper);
1421 return old;
1422 }
1423
1424 PeerConnectionWrapper* callee() { return callee_.get(); }
1425
1426 // Set the |callee_| to the |wrapper| passed in and return the
1427 // original |callee_|.
1428 PeerConnectionWrapper* SetCalleePcWrapperAndReturnCurrent(
1429 PeerConnectionWrapper* wrapper) {
1430 PeerConnectionWrapper* old = callee_.release();
1431 callee_.reset(wrapper);
1432 return old;
1433 }
1434
Qingsi Wang1dac6d82018-12-12 15:28:47 -08001435 void SetPortAllocatorFlags(uint32_t caller_flags, uint32_t callee_flags) {
1436 network_thread()->Invoke<void>(
1437 RTC_FROM_HERE, rtc::Bind(&cricket::PortAllocator::set_flags,
1438 caller()->port_allocator(), caller_flags));
1439 network_thread()->Invoke<void>(
1440 RTC_FROM_HERE, rtc::Bind(&cricket::PortAllocator::set_flags,
1441 callee()->port_allocator(), callee_flags));
1442 }
1443
Steve Antonede9ca52017-10-16 13:04:27 -07001444 rtc::FirewallSocketServer* firewall() const { return fss_.get(); }
1445
Seth Hampson2f0d7022018-02-20 11:54:42 -08001446 // Expects the provided number of new frames to be received within
1447 // kMaxWaitForFramesMs. The new expected frames are specified in
1448 // |media_expectations|. Returns false if any of the expectations were
1449 // not met.
1450 bool ExpectNewFrames(const MediaExpectations& media_expectations) {
1451 // First initialize the expected frame counts based upon the current
1452 // frame count.
1453 int total_caller_audio_frames_expected = caller()->audio_frames_received();
1454 if (media_expectations.caller_audio_expectation_ ==
1455 MediaExpectations::kExpectSomeFrames) {
1456 total_caller_audio_frames_expected +=
1457 media_expectations.caller_audio_frames_expected_;
1458 }
1459 int total_caller_video_frames_expected =
deadbeef1dcb1642017-03-29 21:08:16 -07001460 caller()->min_video_frames_received_per_track();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001461 if (media_expectations.caller_video_expectation_ ==
1462 MediaExpectations::kExpectSomeFrames) {
1463 total_caller_video_frames_expected +=
1464 media_expectations.caller_video_frames_expected_;
1465 }
1466 int total_callee_audio_frames_expected = callee()->audio_frames_received();
1467 if (media_expectations.callee_audio_expectation_ ==
1468 MediaExpectations::kExpectSomeFrames) {
1469 total_callee_audio_frames_expected +=
1470 media_expectations.callee_audio_frames_expected_;
1471 }
1472 int total_callee_video_frames_expected =
deadbeef1dcb1642017-03-29 21:08:16 -07001473 callee()->min_video_frames_received_per_track();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001474 if (media_expectations.callee_video_expectation_ ==
1475 MediaExpectations::kExpectSomeFrames) {
1476 total_callee_video_frames_expected +=
1477 media_expectations.callee_video_frames_expected_;
1478 }
deadbeef1dcb1642017-03-29 21:08:16 -07001479
Seth Hampson2f0d7022018-02-20 11:54:42 -08001480 // Wait for the expected frames.
deadbeef1dcb1642017-03-29 21:08:16 -07001481 EXPECT_TRUE_WAIT(caller()->audio_frames_received() >=
Seth Hampson2f0d7022018-02-20 11:54:42 -08001482 total_caller_audio_frames_expected &&
deadbeef1dcb1642017-03-29 21:08:16 -07001483 caller()->min_video_frames_received_per_track() >=
Seth Hampson2f0d7022018-02-20 11:54:42 -08001484 total_caller_video_frames_expected &&
deadbeef1dcb1642017-03-29 21:08:16 -07001485 callee()->audio_frames_received() >=
Seth Hampson2f0d7022018-02-20 11:54:42 -08001486 total_callee_audio_frames_expected &&
deadbeef1dcb1642017-03-29 21:08:16 -07001487 callee()->min_video_frames_received_per_track() >=
Seth Hampson2f0d7022018-02-20 11:54:42 -08001488 total_callee_video_frames_expected,
1489 kMaxWaitForFramesMs);
1490 bool expectations_correct =
1491 caller()->audio_frames_received() >=
1492 total_caller_audio_frames_expected &&
1493 caller()->min_video_frames_received_per_track() >=
1494 total_caller_video_frames_expected &&
1495 callee()->audio_frames_received() >=
1496 total_callee_audio_frames_expected &&
1497 callee()->min_video_frames_received_per_track() >=
1498 total_callee_video_frames_expected;
deadbeef1dcb1642017-03-29 21:08:16 -07001499
Seth Hampson2f0d7022018-02-20 11:54:42 -08001500 // After the combined wait, print out a more detailed message upon
1501 // failure.
deadbeef1dcb1642017-03-29 21:08:16 -07001502 EXPECT_GE(caller()->audio_frames_received(),
Seth Hampson2f0d7022018-02-20 11:54:42 -08001503 total_caller_audio_frames_expected);
deadbeef1dcb1642017-03-29 21:08:16 -07001504 EXPECT_GE(caller()->min_video_frames_received_per_track(),
Seth Hampson2f0d7022018-02-20 11:54:42 -08001505 total_caller_video_frames_expected);
deadbeef1dcb1642017-03-29 21:08:16 -07001506 EXPECT_GE(callee()->audio_frames_received(),
Seth Hampson2f0d7022018-02-20 11:54:42 -08001507 total_callee_audio_frames_expected);
deadbeef1dcb1642017-03-29 21:08:16 -07001508 EXPECT_GE(callee()->min_video_frames_received_per_track(),
Seth Hampson2f0d7022018-02-20 11:54:42 -08001509 total_callee_video_frames_expected);
1510
1511 // We want to make sure nothing unexpected was received.
1512 if (media_expectations.caller_audio_expectation_ ==
1513 MediaExpectations::kExpectNoFrames) {
1514 EXPECT_EQ(caller()->audio_frames_received(),
1515 total_caller_audio_frames_expected);
1516 if (caller()->audio_frames_received() !=
1517 total_caller_audio_frames_expected) {
1518 expectations_correct = false;
1519 }
1520 }
1521 if (media_expectations.caller_video_expectation_ ==
1522 MediaExpectations::kExpectNoFrames) {
1523 EXPECT_EQ(caller()->min_video_frames_received_per_track(),
1524 total_caller_video_frames_expected);
1525 if (caller()->min_video_frames_received_per_track() !=
1526 total_caller_video_frames_expected) {
1527 expectations_correct = false;
1528 }
1529 }
1530 if (media_expectations.callee_audio_expectation_ ==
1531 MediaExpectations::kExpectNoFrames) {
1532 EXPECT_EQ(callee()->audio_frames_received(),
1533 total_callee_audio_frames_expected);
1534 if (callee()->audio_frames_received() !=
1535 total_callee_audio_frames_expected) {
1536 expectations_correct = false;
1537 }
1538 }
1539 if (media_expectations.callee_video_expectation_ ==
1540 MediaExpectations::kExpectNoFrames) {
1541 EXPECT_EQ(callee()->min_video_frames_received_per_track(),
1542 total_callee_video_frames_expected);
1543 if (callee()->min_video_frames_received_per_track() !=
1544 total_callee_video_frames_expected) {
1545 expectations_correct = false;
1546 }
1547 }
1548 return expectations_correct;
deadbeef1dcb1642017-03-29 21:08:16 -07001549 }
1550
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07001551 void TestNegotiatedCipherSuite(
1552 const PeerConnectionFactory::Options& caller_options,
1553 const PeerConnectionFactory::Options& callee_options,
1554 int expected_cipher_suite) {
deadbeef1dcb1642017-03-29 21:08:16 -07001555 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(caller_options,
1556 callee_options));
deadbeef1dcb1642017-03-29 21:08:16 -07001557 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001558 caller()->AddAudioVideoTracks();
1559 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001560 caller()->CreateAndSetAndSignalOffer();
Qingsi Wang7fc821d2018-07-12 12:54:53 -07001561 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07001562 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(expected_cipher_suite),
deadbeefd8ad7882017-04-18 16:01:17 -07001563 caller()->OldGetStats()->SrtpCipher(), kDefaultTimeout);
Qingsi Wang7fc821d2018-07-12 12:54:53 -07001564 // TODO(bugs.webrtc.org/9456): Fix it.
Alex Loiko9289eda2018-11-23 16:18:59 +00001565 EXPECT_EQ(1, webrtc::metrics::NumEvents(
Qingsi Wang7fc821d2018-07-12 12:54:53 -07001566 "WebRTC.PeerConnection.SrtpCryptoSuite.Audio",
1567 expected_cipher_suite));
deadbeef1dcb1642017-03-29 21:08:16 -07001568 }
1569
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07001570 void TestGcmNegotiationUsesCipherSuite(bool local_gcm_enabled,
1571 bool remote_gcm_enabled,
1572 int expected_cipher_suite) {
1573 PeerConnectionFactory::Options caller_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07001574 caller_options.crypto_options.srtp.enable_gcm_crypto_suites =
1575 local_gcm_enabled;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07001576 PeerConnectionFactory::Options callee_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07001577 callee_options.crypto_options.srtp.enable_gcm_crypto_suites =
1578 remote_gcm_enabled;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07001579 TestNegotiatedCipherSuite(caller_options, callee_options,
1580 expected_cipher_suite);
1581 }
1582
Seth Hampson2f0d7022018-02-20 11:54:42 -08001583 protected:
Steve Anton3acffc32018-04-12 17:21:03 -07001584 SdpSemantics sdp_semantics_;
Seth Hampson2f0d7022018-02-20 11:54:42 -08001585
deadbeef1dcb1642017-03-29 21:08:16 -07001586 private:
1587 // |ss_| is used by |network_thread_| so it must be destroyed later.
deadbeef1dcb1642017-03-29 21:08:16 -07001588 std::unique_ptr<rtc::VirtualSocketServer> ss_;
Steve Antonede9ca52017-10-16 13:04:27 -07001589 std::unique_ptr<rtc::FirewallSocketServer> fss_;
deadbeef1dcb1642017-03-29 21:08:16 -07001590 // |network_thread_| and |worker_thread_| are used by both
1591 // |caller_| and |callee_| so they must be destroyed
1592 // later.
1593 std::unique_ptr<rtc::Thread> network_thread_;
1594 std::unique_ptr<rtc::Thread> worker_thread_;
Seth Hampsonaed71642018-06-11 07:41:32 -07001595 // The turn servers and turn customizers should be accessed & deleted on the
1596 // network thread to avoid a race with the socket read/write that occurs
1597 // on the network thread.
1598 std::vector<std::unique_ptr<cricket::TestTurnServer>> turn_servers_;
1599 std::vector<std::unique_ptr<cricket::TestTurnCustomizer>> turn_customizers_;
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001600 webrtc::MediaTransportPair loopback_media_transports_;
deadbeef1dcb1642017-03-29 21:08:16 -07001601 std::unique_ptr<PeerConnectionWrapper> caller_;
1602 std::unique_ptr<PeerConnectionWrapper> callee_;
1603};
1604
Seth Hampson2f0d7022018-02-20 11:54:42 -08001605class PeerConnectionIntegrationTest
1606 : public PeerConnectionIntegrationBaseTest,
1607 public ::testing::WithParamInterface<SdpSemantics> {
1608 protected:
1609 PeerConnectionIntegrationTest()
1610 : PeerConnectionIntegrationBaseTest(GetParam()) {}
1611};
1612
1613class PeerConnectionIntegrationTestPlanB
1614 : public PeerConnectionIntegrationBaseTest {
1615 protected:
1616 PeerConnectionIntegrationTestPlanB()
1617 : PeerConnectionIntegrationBaseTest(SdpSemantics::kPlanB) {}
1618};
1619
1620class PeerConnectionIntegrationTestUnifiedPlan
1621 : public PeerConnectionIntegrationBaseTest {
1622 protected:
1623 PeerConnectionIntegrationTestUnifiedPlan()
1624 : PeerConnectionIntegrationBaseTest(SdpSemantics::kUnifiedPlan) {}
1625};
1626
deadbeef1dcb1642017-03-29 21:08:16 -07001627// Test the OnFirstPacketReceived callback from audio/video RtpReceivers. This
1628// includes testing that the callback is invoked if an observer is connected
1629// after the first packet has already been received.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001630TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07001631 RtpReceiverObserverOnFirstPacketReceived) {
1632 ASSERT_TRUE(CreatePeerConnectionWrappers());
1633 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001634 caller()->AddAudioVideoTracks();
1635 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001636 // Start offer/answer exchange and wait for it to complete.
1637 caller()->CreateAndSetAndSignalOffer();
1638 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1639 // Should be one receiver each for audio/video.
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02001640 EXPECT_EQ(2U, caller()->rtp_receiver_observers().size());
1641 EXPECT_EQ(2U, callee()->rtp_receiver_observers().size());
deadbeef1dcb1642017-03-29 21:08:16 -07001642 // Wait for all "first packet received" callbacks to be fired.
1643 EXPECT_TRUE_WAIT(
Steve Anton64b626b2019-01-28 17:25:26 -08001644 absl::c_all_of(caller()->rtp_receiver_observers(),
1645 [](const std::unique_ptr<MockRtpReceiverObserver>& o) {
1646 return o->first_packet_received();
1647 }),
deadbeef1dcb1642017-03-29 21:08:16 -07001648 kMaxWaitForFramesMs);
1649 EXPECT_TRUE_WAIT(
Steve Anton64b626b2019-01-28 17:25:26 -08001650 absl::c_all_of(callee()->rtp_receiver_observers(),
1651 [](const std::unique_ptr<MockRtpReceiverObserver>& o) {
1652 return o->first_packet_received();
1653 }),
deadbeef1dcb1642017-03-29 21:08:16 -07001654 kMaxWaitForFramesMs);
1655 // If new observers are set after the first packet was already received, the
1656 // callback should still be invoked.
1657 caller()->ResetRtpReceiverObservers();
1658 callee()->ResetRtpReceiverObservers();
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02001659 EXPECT_EQ(2U, caller()->rtp_receiver_observers().size());
1660 EXPECT_EQ(2U, callee()->rtp_receiver_observers().size());
deadbeef1dcb1642017-03-29 21:08:16 -07001661 EXPECT_TRUE(
Steve Anton64b626b2019-01-28 17:25:26 -08001662 absl::c_all_of(caller()->rtp_receiver_observers(),
1663 [](const std::unique_ptr<MockRtpReceiverObserver>& o) {
1664 return o->first_packet_received();
1665 }));
deadbeef1dcb1642017-03-29 21:08:16 -07001666 EXPECT_TRUE(
Steve Anton64b626b2019-01-28 17:25:26 -08001667 absl::c_all_of(callee()->rtp_receiver_observers(),
1668 [](const std::unique_ptr<MockRtpReceiverObserver>& o) {
1669 return o->first_packet_received();
1670 }));
deadbeef1dcb1642017-03-29 21:08:16 -07001671}
1672
1673class DummyDtmfObserver : public DtmfSenderObserverInterface {
1674 public:
1675 DummyDtmfObserver() : completed_(false) {}
1676
1677 // Implements DtmfSenderObserverInterface.
1678 void OnToneChange(const std::string& tone) override {
1679 tones_.push_back(tone);
1680 if (tone.empty()) {
1681 completed_ = true;
1682 }
1683 }
1684
1685 const std::vector<std::string>& tones() const { return tones_; }
1686 bool completed() const { return completed_; }
1687
1688 private:
1689 bool completed_;
1690 std::vector<std::string> tones_;
1691};
1692
1693// Assumes |sender| already has an audio track added and the offer/answer
1694// exchange is done.
1695void TestDtmfFromSenderToReceiver(PeerConnectionWrapper* sender,
1696 PeerConnectionWrapper* receiver) {
Steve Anton15324772018-01-16 10:26:49 -08001697 // We should be able to get a DTMF sender from the local sender.
1698 rtc::scoped_refptr<DtmfSenderInterface> dtmf_sender =
1699 sender->pc()->GetSenders().at(0)->GetDtmfSender();
1700 ASSERT_TRUE(dtmf_sender);
deadbeef1dcb1642017-03-29 21:08:16 -07001701 DummyDtmfObserver observer;
deadbeef1dcb1642017-03-29 21:08:16 -07001702 dtmf_sender->RegisterObserver(&observer);
1703
1704 // Test the DtmfSender object just created.
1705 EXPECT_TRUE(dtmf_sender->CanInsertDtmf());
1706 EXPECT_TRUE(dtmf_sender->InsertDtmf("1a", 100, 50));
1707
1708 EXPECT_TRUE_WAIT(observer.completed(), kDefaultTimeout);
1709 std::vector<std::string> tones = {"1", "a", ""};
1710 EXPECT_EQ(tones, observer.tones());
1711 dtmf_sender->UnregisterObserver();
1712 // TODO(deadbeef): Verify the tones were actually received end-to-end.
1713}
1714
1715// Verifies the DtmfSenderObserver callbacks for a DtmfSender (one in each
1716// direction).
Seth Hampson2f0d7022018-02-20 11:54:42 -08001717TEST_P(PeerConnectionIntegrationTest, DtmfSenderObserver) {
deadbeef1dcb1642017-03-29 21:08:16 -07001718 ASSERT_TRUE(CreatePeerConnectionWrappers());
1719 ConnectFakeSignaling();
1720 // Only need audio for DTMF.
Steve Anton15324772018-01-16 10:26:49 -08001721 caller()->AddAudioTrack();
1722 callee()->AddAudioTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07001723 caller()->CreateAndSetAndSignalOffer();
1724 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
deadbeef71452802017-05-07 17:21:01 -07001725 // DTLS must finish before the DTMF sender can be used reliably.
1726 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07001727 TestDtmfFromSenderToReceiver(caller(), callee());
1728 TestDtmfFromSenderToReceiver(callee(), caller());
1729}
1730
1731// Basic end-to-end test, verifying media can be encoded/transmitted/decoded
1732// between two connections, using DTLS-SRTP.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001733TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithDtls) {
deadbeef1dcb1642017-03-29 21:08:16 -07001734 ASSERT_TRUE(CreatePeerConnectionWrappers());
1735 ConnectFakeSignaling();
Harald Alvestrand194939b2018-01-24 16:04:13 +01001736
deadbeef1dcb1642017-03-29 21:08:16 -07001737 // Do normal offer/answer and wait for some frames to be received in each
1738 // direction.
Steve Anton15324772018-01-16 10:26:49 -08001739 caller()->AddAudioVideoTracks();
1740 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001741 caller()->CreateAndSetAndSignalOffer();
1742 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001743 MediaExpectations media_expectations;
1744 media_expectations.ExpectBidirectionalAudioAndVideo();
1745 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Qingsi Wang7fc821d2018-07-12 12:54:53 -07001746 EXPECT_LE(2, webrtc::metrics::NumEvents("WebRTC.PeerConnection.KeyProtocol",
1747 webrtc::kEnumCounterKeyProtocolDtls));
1748 EXPECT_EQ(0, webrtc::metrics::NumEvents("WebRTC.PeerConnection.KeyProtocol",
1749 webrtc::kEnumCounterKeyProtocolSdes));
deadbeef1dcb1642017-03-29 21:08:16 -07001750}
1751
1752// Uses SDES instead of DTLS for key agreement.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001753TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithSdes) {
deadbeef1dcb1642017-03-29 21:08:16 -07001754 PeerConnectionInterface::RTCConfiguration sdes_config;
1755 sdes_config.enable_dtls_srtp.emplace(false);
1756 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(sdes_config, sdes_config));
1757 ConnectFakeSignaling();
1758
1759 // Do normal offer/answer and wait for some frames to be received in each
1760 // direction.
Steve Anton15324772018-01-16 10:26:49 -08001761 caller()->AddAudioVideoTracks();
1762 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001763 caller()->CreateAndSetAndSignalOffer();
1764 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001765 MediaExpectations media_expectations;
1766 media_expectations.ExpectBidirectionalAudioAndVideo();
1767 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Qingsi Wang7fc821d2018-07-12 12:54:53 -07001768 EXPECT_LE(2, webrtc::metrics::NumEvents("WebRTC.PeerConnection.KeyProtocol",
1769 webrtc::kEnumCounterKeyProtocolSdes));
1770 EXPECT_EQ(0, webrtc::metrics::NumEvents("WebRTC.PeerConnection.KeyProtocol",
1771 webrtc::kEnumCounterKeyProtocolDtls));
deadbeef1dcb1642017-03-29 21:08:16 -07001772}
1773
Steve Anton8c0f7a72017-10-03 10:03:10 -07001774// Tests that the GetRemoteAudioSSLCertificate method returns the remote DTLS
1775// certificate once the DTLS handshake has finished.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001776TEST_P(PeerConnectionIntegrationTest,
Steve Anton8c0f7a72017-10-03 10:03:10 -07001777 GetRemoteAudioSSLCertificateReturnsExchangedCertificate) {
1778 auto GetRemoteAudioSSLCertificate = [](PeerConnectionWrapper* wrapper) {
1779 auto pci = reinterpret_cast<PeerConnectionProxy*>(wrapper->pc());
1780 auto pc = reinterpret_cast<PeerConnection*>(pci->internal());
1781 return pc->GetRemoteAudioSSLCertificate();
1782 };
Zhi Huang70b820f2018-01-27 14:16:15 -08001783 auto GetRemoteAudioSSLCertChain = [](PeerConnectionWrapper* wrapper) {
1784 auto pci = reinterpret_cast<PeerConnectionProxy*>(wrapper->pc());
1785 auto pc = reinterpret_cast<PeerConnection*>(pci->internal());
1786 return pc->GetRemoteAudioSSLCertChain();
1787 };
Steve Anton8c0f7a72017-10-03 10:03:10 -07001788
1789 auto caller_cert = rtc::RTCCertificate::FromPEM(kRsaPems[0]);
1790 auto callee_cert = rtc::RTCCertificate::FromPEM(kRsaPems[1]);
1791
1792 // Configure each side with a known certificate so they can be compared later.
1793 PeerConnectionInterface::RTCConfiguration caller_config;
1794 caller_config.enable_dtls_srtp.emplace(true);
1795 caller_config.certificates.push_back(caller_cert);
1796 PeerConnectionInterface::RTCConfiguration callee_config;
1797 callee_config.enable_dtls_srtp.emplace(true);
1798 callee_config.certificates.push_back(callee_cert);
1799 ASSERT_TRUE(
1800 CreatePeerConnectionWrappersWithConfig(caller_config, callee_config));
1801 ConnectFakeSignaling();
1802
1803 // When first initialized, there should not be a remote SSL certificate (and
1804 // calling this method should not crash).
1805 EXPECT_EQ(nullptr, GetRemoteAudioSSLCertificate(caller()));
1806 EXPECT_EQ(nullptr, GetRemoteAudioSSLCertificate(callee()));
Zhi Huang70b820f2018-01-27 14:16:15 -08001807 EXPECT_EQ(nullptr, GetRemoteAudioSSLCertChain(caller()));
1808 EXPECT_EQ(nullptr, GetRemoteAudioSSLCertChain(callee()));
Steve Anton8c0f7a72017-10-03 10:03:10 -07001809
Steve Anton15324772018-01-16 10:26:49 -08001810 caller()->AddAudioTrack();
1811 callee()->AddAudioTrack();
Steve Anton8c0f7a72017-10-03 10:03:10 -07001812 caller()->CreateAndSetAndSignalOffer();
1813 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1814 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
1815
1816 // Once DTLS has been connected, each side should return the other's SSL
1817 // certificate when calling GetRemoteAudioSSLCertificate.
1818
1819 auto caller_remote_cert = GetRemoteAudioSSLCertificate(caller());
1820 ASSERT_TRUE(caller_remote_cert);
Benjamin Wright6c6c9df2018-10-25 01:16:26 -07001821 EXPECT_EQ(callee_cert->GetSSLCertificate().ToPEMString(),
Steve Anton8c0f7a72017-10-03 10:03:10 -07001822 caller_remote_cert->ToPEMString());
1823
1824 auto callee_remote_cert = GetRemoteAudioSSLCertificate(callee());
1825 ASSERT_TRUE(callee_remote_cert);
Benjamin Wright6c6c9df2018-10-25 01:16:26 -07001826 EXPECT_EQ(caller_cert->GetSSLCertificate().ToPEMString(),
Steve Anton8c0f7a72017-10-03 10:03:10 -07001827 callee_remote_cert->ToPEMString());
Zhi Huang70b820f2018-01-27 14:16:15 -08001828
1829 auto caller_remote_cert_chain = GetRemoteAudioSSLCertChain(caller());
1830 ASSERT_TRUE(caller_remote_cert_chain);
1831 ASSERT_EQ(1U, caller_remote_cert_chain->GetSize());
1832 auto remote_cert = &caller_remote_cert_chain->Get(0);
Benjamin Wright6c6c9df2018-10-25 01:16:26 -07001833 EXPECT_EQ(callee_cert->GetSSLCertificate().ToPEMString(),
Zhi Huang70b820f2018-01-27 14:16:15 -08001834 remote_cert->ToPEMString());
1835
1836 auto callee_remote_cert_chain = GetRemoteAudioSSLCertChain(callee());
1837 ASSERT_TRUE(callee_remote_cert_chain);
1838 ASSERT_EQ(1U, callee_remote_cert_chain->GetSize());
1839 remote_cert = &callee_remote_cert_chain->Get(0);
Benjamin Wright6c6c9df2018-10-25 01:16:26 -07001840 EXPECT_EQ(caller_cert->GetSSLCertificate().ToPEMString(),
Zhi Huang70b820f2018-01-27 14:16:15 -08001841 remote_cert->ToPEMString());
Steve Anton8c0f7a72017-10-03 10:03:10 -07001842}
1843
deadbeef1dcb1642017-03-29 21:08:16 -07001844// This test sets up a call between two parties with a source resolution of
1845// 1280x720 and verifies that a 16:9 aspect ratio is received.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001846TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07001847 Send1280By720ResolutionAndReceive16To9AspectRatio) {
1848 ASSERT_TRUE(CreatePeerConnectionWrappers());
1849 ConnectFakeSignaling();
1850
Niels Möller5c7efe72018-05-11 10:34:46 +02001851 // Add video tracks with 16:9 aspect ratio, size 1280 x 720.
1852 webrtc::FakePeriodicVideoSource::Config config;
1853 config.width = 1280;
1854 config.height = 720;
Johannes Kron965e7942018-09-13 15:36:20 +02001855 config.timestamp_offset_ms = rtc::TimeMillis();
Niels Möller5c7efe72018-05-11 10:34:46 +02001856 caller()->AddTrack(caller()->CreateLocalVideoTrackWithConfig(config));
1857 callee()->AddTrack(callee()->CreateLocalVideoTrackWithConfig(config));
deadbeef1dcb1642017-03-29 21:08:16 -07001858
1859 // Do normal offer/answer and wait for at least one frame to be received in
1860 // each direction.
1861 caller()->CreateAndSetAndSignalOffer();
1862 ASSERT_TRUE_WAIT(caller()->min_video_frames_received_per_track() > 0 &&
1863 callee()->min_video_frames_received_per_track() > 0,
1864 kMaxWaitForFramesMs);
1865
1866 // Check rendered aspect ratio.
1867 EXPECT_EQ(16.0 / 9, caller()->local_rendered_aspect_ratio());
1868 EXPECT_EQ(16.0 / 9, caller()->rendered_aspect_ratio());
1869 EXPECT_EQ(16.0 / 9, callee()->local_rendered_aspect_ratio());
1870 EXPECT_EQ(16.0 / 9, callee()->rendered_aspect_ratio());
1871}
1872
1873// This test sets up an one-way call, with media only from caller to
1874// callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001875TEST_P(PeerConnectionIntegrationTest, OneWayMediaCall) {
deadbeef1dcb1642017-03-29 21:08:16 -07001876 ASSERT_TRUE(CreatePeerConnectionWrappers());
1877 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001878 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001879 caller()->CreateAndSetAndSignalOffer();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001880 MediaExpectations media_expectations;
1881 media_expectations.CalleeExpectsSomeAudioAndVideo();
1882 media_expectations.CallerExpectsNoAudio();
1883 media_expectations.CallerExpectsNoVideo();
1884 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07001885}
1886
1887// This test sets up a audio call initially, with the callee rejecting video
1888// initially. Then later the callee decides to upgrade to audio/video, and
1889// initiates a new offer/answer exchange.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001890TEST_P(PeerConnectionIntegrationTest, AudioToVideoUpgrade) {
deadbeef1dcb1642017-03-29 21:08:16 -07001891 ASSERT_TRUE(CreatePeerConnectionWrappers());
1892 ConnectFakeSignaling();
1893 // Initially, offer an audio/video stream from the caller, but refuse to
1894 // send/receive video on the callee side.
Steve Anton15324772018-01-16 10:26:49 -08001895 caller()->AddAudioVideoTracks();
1896 callee()->AddAudioTrack();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001897 if (sdp_semantics_ == SdpSemantics::kPlanB) {
1898 PeerConnectionInterface::RTCOfferAnswerOptions options;
1899 options.offer_to_receive_video = 0;
1900 callee()->SetOfferAnswerOptions(options);
1901 } else {
1902 callee()->SetRemoteOfferHandler([this] {
1903 callee()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)->Stop();
1904 });
1905 }
deadbeef1dcb1642017-03-29 21:08:16 -07001906 // Do offer/answer and make sure audio is still received end-to-end.
1907 caller()->CreateAndSetAndSignalOffer();
1908 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001909 {
1910 MediaExpectations media_expectations;
1911 media_expectations.ExpectBidirectionalAudio();
1912 media_expectations.ExpectNoVideo();
1913 ASSERT_TRUE(ExpectNewFrames(media_expectations));
1914 }
deadbeef1dcb1642017-03-29 21:08:16 -07001915 // Sanity check that the callee's description has a rejected video section.
1916 ASSERT_NE(nullptr, callee()->pc()->local_description());
1917 const ContentInfo* callee_video_content =
1918 GetFirstVideoContent(callee()->pc()->local_description()->description());
1919 ASSERT_NE(nullptr, callee_video_content);
1920 EXPECT_TRUE(callee_video_content->rejected);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001921
deadbeef1dcb1642017-03-29 21:08:16 -07001922 // Now negotiate with video and ensure negotiation succeeds, with video
1923 // frames and additional audio frames being received.
Steve Anton15324772018-01-16 10:26:49 -08001924 callee()->AddVideoTrack();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001925 if (sdp_semantics_ == SdpSemantics::kPlanB) {
1926 PeerConnectionInterface::RTCOfferAnswerOptions options;
1927 options.offer_to_receive_video = 1;
1928 callee()->SetOfferAnswerOptions(options);
1929 } else {
1930 callee()->SetRemoteOfferHandler(nullptr);
1931 caller()->SetRemoteOfferHandler([this] {
1932 // The caller creates a new transceiver to receive video on when receiving
1933 // the offer, but by default it is send only.
1934 auto transceivers = caller()->pc()->GetTransceivers();
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02001935 ASSERT_EQ(3U, transceivers.size());
Seth Hampson2f0d7022018-02-20 11:54:42 -08001936 ASSERT_EQ(cricket::MEDIA_TYPE_VIDEO,
1937 transceivers[2]->receiver()->media_type());
1938 transceivers[2]->sender()->SetTrack(caller()->CreateLocalVideoTrack());
1939 transceivers[2]->SetDirection(RtpTransceiverDirection::kSendRecv);
1940 });
1941 }
deadbeef1dcb1642017-03-29 21:08:16 -07001942 callee()->CreateAndSetAndSignalOffer();
1943 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001944 {
1945 // Expect additional audio frames to be received after the upgrade.
1946 MediaExpectations media_expectations;
1947 media_expectations.ExpectBidirectionalAudioAndVideo();
1948 ASSERT_TRUE(ExpectNewFrames(media_expectations));
1949 }
deadbeef1dcb1642017-03-29 21:08:16 -07001950}
1951
deadbeef4389b4d2017-09-07 09:07:36 -07001952// Simpler than the above test; just add an audio track to an established
1953// video-only connection.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001954TEST_P(PeerConnectionIntegrationTest, AddAudioToVideoOnlyCall) {
deadbeef4389b4d2017-09-07 09:07:36 -07001955 ASSERT_TRUE(CreatePeerConnectionWrappers());
1956 ConnectFakeSignaling();
1957 // Do initial offer/answer with just a video track.
Steve Anton15324772018-01-16 10:26:49 -08001958 caller()->AddVideoTrack();
1959 callee()->AddVideoTrack();
deadbeef4389b4d2017-09-07 09:07:36 -07001960 caller()->CreateAndSetAndSignalOffer();
1961 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1962 // Now add an audio track and do another offer/answer.
Steve Anton15324772018-01-16 10:26:49 -08001963 caller()->AddAudioTrack();
1964 callee()->AddAudioTrack();
deadbeef4389b4d2017-09-07 09:07:36 -07001965 caller()->CreateAndSetAndSignalOffer();
1966 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1967 // Ensure both audio and video frames are received end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001968 MediaExpectations media_expectations;
1969 media_expectations.ExpectBidirectionalAudioAndVideo();
1970 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef4389b4d2017-09-07 09:07:36 -07001971}
1972
deadbeef1dcb1642017-03-29 21:08:16 -07001973// This test sets up a call that's transferred to a new caller with a different
1974// DTLS fingerprint.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001975TEST_P(PeerConnectionIntegrationTest, CallTransferredForCallee) {
deadbeef1dcb1642017-03-29 21:08:16 -07001976 ASSERT_TRUE(CreatePeerConnectionWrappers());
1977 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001978 caller()->AddAudioVideoTracks();
1979 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001980 caller()->CreateAndSetAndSignalOffer();
1981 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1982
1983 // Keep the original peer around which will still send packets to the
1984 // receiving client. These SRTP packets will be dropped.
1985 std::unique_ptr<PeerConnectionWrapper> original_peer(
1986 SetCallerPcWrapperAndReturnCurrent(
Seth Hampson2f0d7022018-02-20 11:54:42 -08001987 CreatePeerConnectionWrapperWithAlternateKey().release()));
deadbeef1dcb1642017-03-29 21:08:16 -07001988 // TODO(deadbeef): Why do we call Close here? That goes against the comment
1989 // directly above.
1990 original_peer->pc()->Close();
1991
1992 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001993 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001994 caller()->CreateAndSetAndSignalOffer();
1995 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1996 // Wait for some additional frames to be transmitted end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001997 MediaExpectations media_expectations;
1998 media_expectations.ExpectBidirectionalAudioAndVideo();
1999 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002000}
2001
2002// This test sets up a call that's transferred to a new callee with a different
2003// DTLS fingerprint.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002004TEST_P(PeerConnectionIntegrationTest, CallTransferredForCaller) {
deadbeef1dcb1642017-03-29 21:08:16 -07002005 ASSERT_TRUE(CreatePeerConnectionWrappers());
2006 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002007 caller()->AddAudioVideoTracks();
2008 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002009 caller()->CreateAndSetAndSignalOffer();
2010 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2011
2012 // Keep the original peer around which will still send packets to the
2013 // receiving client. These SRTP packets will be dropped.
2014 std::unique_ptr<PeerConnectionWrapper> original_peer(
2015 SetCalleePcWrapperAndReturnCurrent(
Seth Hampson2f0d7022018-02-20 11:54:42 -08002016 CreatePeerConnectionWrapperWithAlternateKey().release()));
deadbeef1dcb1642017-03-29 21:08:16 -07002017 // TODO(deadbeef): Why do we call Close here? That goes against the comment
2018 // directly above.
2019 original_peer->pc()->Close();
2020
2021 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002022 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002023 caller()->SetOfferAnswerOptions(IceRestartOfferAnswerOptions());
2024 caller()->CreateAndSetAndSignalOffer();
2025 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2026 // Wait for some additional frames to be transmitted end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002027 MediaExpectations media_expectations;
2028 media_expectations.ExpectBidirectionalAudioAndVideo();
2029 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002030}
2031
2032// This test sets up a non-bundled call and negotiates bundling at the same
2033// time as starting an ICE restart. When bundling is in effect in the restart,
2034// the DTLS-SRTP context should be successfully reset.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002035TEST_P(PeerConnectionIntegrationTest, BundlingEnabledWhileIceRestartOccurs) {
deadbeef1dcb1642017-03-29 21:08:16 -07002036 ASSERT_TRUE(CreatePeerConnectionWrappers());
2037 ConnectFakeSignaling();
2038
Steve Anton15324772018-01-16 10:26:49 -08002039 caller()->AddAudioVideoTracks();
2040 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002041 // Remove the bundle group from the SDP received by the callee.
2042 callee()->SetReceivedSdpMunger([](cricket::SessionDescription* desc) {
2043 desc->RemoveGroupByName("BUNDLE");
2044 });
2045 caller()->CreateAndSetAndSignalOffer();
2046 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002047 {
2048 MediaExpectations media_expectations;
2049 media_expectations.ExpectBidirectionalAudioAndVideo();
2050 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2051 }
deadbeef1dcb1642017-03-29 21:08:16 -07002052 // Now stop removing the BUNDLE group, and trigger an ICE restart.
2053 callee()->SetReceivedSdpMunger(nullptr);
2054 caller()->SetOfferAnswerOptions(IceRestartOfferAnswerOptions());
2055 caller()->CreateAndSetAndSignalOffer();
2056 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2057
2058 // Expect additional frames to be received after the ICE restart.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002059 {
2060 MediaExpectations media_expectations;
2061 media_expectations.ExpectBidirectionalAudioAndVideo();
2062 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2063 }
deadbeef1dcb1642017-03-29 21:08:16 -07002064}
2065
2066// Test CVO (Coordination of Video Orientation). If a video source is rotated
2067// and both peers support the CVO RTP header extension, the actual video frames
2068// don't need to be encoded in different resolutions, since the rotation is
2069// communicated through the RTP header extension.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002070TEST_P(PeerConnectionIntegrationTest, RotatedVideoWithCVOExtension) {
deadbeef1dcb1642017-03-29 21:08:16 -07002071 ASSERT_TRUE(CreatePeerConnectionWrappers());
2072 ConnectFakeSignaling();
2073 // Add rotated video tracks.
Steve Anton15324772018-01-16 10:26:49 -08002074 caller()->AddTrack(
deadbeef1dcb1642017-03-29 21:08:16 -07002075 caller()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_90));
Steve Anton15324772018-01-16 10:26:49 -08002076 callee()->AddTrack(
deadbeef1dcb1642017-03-29 21:08:16 -07002077 callee()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_270));
2078
2079 // Wait for video frames to be received by both sides.
2080 caller()->CreateAndSetAndSignalOffer();
2081 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2082 ASSERT_TRUE_WAIT(caller()->min_video_frames_received_per_track() > 0 &&
2083 callee()->min_video_frames_received_per_track() > 0,
2084 kMaxWaitForFramesMs);
2085
2086 // Ensure that the aspect ratio is unmodified.
2087 // TODO(deadbeef): Where does 4:3 come from? Should be explicit in the test,
2088 // not just assumed.
2089 EXPECT_EQ(4.0 / 3, caller()->local_rendered_aspect_ratio());
2090 EXPECT_EQ(4.0 / 3, caller()->rendered_aspect_ratio());
2091 EXPECT_EQ(4.0 / 3, callee()->local_rendered_aspect_ratio());
2092 EXPECT_EQ(4.0 / 3, callee()->rendered_aspect_ratio());
2093 // Ensure that the CVO bits were surfaced to the renderer.
2094 EXPECT_EQ(webrtc::kVideoRotation_270, caller()->rendered_rotation());
2095 EXPECT_EQ(webrtc::kVideoRotation_90, callee()->rendered_rotation());
2096}
2097
2098// Test that when the CVO extension isn't supported, video is rotated the
2099// old-fashioned way, by encoding rotated frames.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002100TEST_P(PeerConnectionIntegrationTest, RotatedVideoWithoutCVOExtension) {
deadbeef1dcb1642017-03-29 21:08:16 -07002101 ASSERT_TRUE(CreatePeerConnectionWrappers());
2102 ConnectFakeSignaling();
2103 // Add rotated video tracks.
Steve Anton15324772018-01-16 10:26:49 -08002104 caller()->AddTrack(
deadbeef1dcb1642017-03-29 21:08:16 -07002105 caller()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_90));
Steve Anton15324772018-01-16 10:26:49 -08002106 callee()->AddTrack(
deadbeef1dcb1642017-03-29 21:08:16 -07002107 callee()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_270));
2108
2109 // Remove the CVO extension from the offered SDP.
2110 callee()->SetReceivedSdpMunger([](cricket::SessionDescription* desc) {
2111 cricket::VideoContentDescription* video =
2112 GetFirstVideoContentDescription(desc);
2113 video->ClearRtpHeaderExtensions();
2114 });
2115 // Wait for video frames to be received by both sides.
2116 caller()->CreateAndSetAndSignalOffer();
2117 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2118 ASSERT_TRUE_WAIT(caller()->min_video_frames_received_per_track() > 0 &&
2119 callee()->min_video_frames_received_per_track() > 0,
2120 kMaxWaitForFramesMs);
2121
2122 // Expect that the aspect ratio is inversed to account for the 90/270 degree
2123 // rotation.
2124 // TODO(deadbeef): Where does 4:3 come from? Should be explicit in the test,
2125 // not just assumed.
2126 EXPECT_EQ(3.0 / 4, caller()->local_rendered_aspect_ratio());
2127 EXPECT_EQ(3.0 / 4, caller()->rendered_aspect_ratio());
2128 EXPECT_EQ(3.0 / 4, callee()->local_rendered_aspect_ratio());
2129 EXPECT_EQ(3.0 / 4, callee()->rendered_aspect_ratio());
2130 // Expect that each endpoint is unaware of the rotation of the other endpoint.
2131 EXPECT_EQ(webrtc::kVideoRotation_0, caller()->rendered_rotation());
2132 EXPECT_EQ(webrtc::kVideoRotation_0, callee()->rendered_rotation());
2133}
2134
deadbeef1dcb1642017-03-29 21:08:16 -07002135// Test that if the answerer rejects the audio m= section, no audio is sent or
2136// received, but video still can be.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002137TEST_P(PeerConnectionIntegrationTest, AnswererRejectsAudioSection) {
deadbeef1dcb1642017-03-29 21:08:16 -07002138 ASSERT_TRUE(CreatePeerConnectionWrappers());
2139 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002140 caller()->AddAudioVideoTracks();
Seth Hampson2f0d7022018-02-20 11:54:42 -08002141 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2142 // Only add video track for callee, and set offer_to_receive_audio to 0, so
2143 // it will reject the audio m= section completely.
2144 PeerConnectionInterface::RTCOfferAnswerOptions options;
2145 options.offer_to_receive_audio = 0;
2146 callee()->SetOfferAnswerOptions(options);
2147 } else {
2148 // Stopping the audio RtpTransceiver will cause the media section to be
2149 // rejected in the answer.
2150 callee()->SetRemoteOfferHandler([this] {
2151 callee()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_AUDIO)->Stop();
2152 });
2153 }
Steve Anton15324772018-01-16 10:26:49 -08002154 callee()->AddTrack(callee()->CreateLocalVideoTrack());
deadbeef1dcb1642017-03-29 21:08:16 -07002155 // Do offer/answer and wait for successful end-to-end video frames.
2156 caller()->CreateAndSetAndSignalOffer();
2157 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002158 MediaExpectations media_expectations;
2159 media_expectations.ExpectBidirectionalVideo();
2160 media_expectations.ExpectNoAudio();
2161 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2162
deadbeef1dcb1642017-03-29 21:08:16 -07002163 // Sanity check that the callee's description has a rejected audio section.
2164 ASSERT_NE(nullptr, callee()->pc()->local_description());
2165 const ContentInfo* callee_audio_content =
2166 GetFirstAudioContent(callee()->pc()->local_description()->description());
2167 ASSERT_NE(nullptr, callee_audio_content);
2168 EXPECT_TRUE(callee_audio_content->rejected);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002169 if (sdp_semantics_ == SdpSemantics::kUnifiedPlan) {
2170 // The caller's transceiver should have stopped after receiving the answer.
2171 EXPECT_TRUE(caller()
2172 ->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_AUDIO)
2173 ->stopped());
2174 }
deadbeef1dcb1642017-03-29 21:08:16 -07002175}
2176
2177// Test that if the answerer rejects the video m= section, no video is sent or
2178// received, but audio still can be.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002179TEST_P(PeerConnectionIntegrationTest, AnswererRejectsVideoSection) {
deadbeef1dcb1642017-03-29 21:08:16 -07002180 ASSERT_TRUE(CreatePeerConnectionWrappers());
2181 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002182 caller()->AddAudioVideoTracks();
Seth Hampson2f0d7022018-02-20 11:54:42 -08002183 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2184 // Only add audio track for callee, and set offer_to_receive_video to 0, so
2185 // it will reject the video m= section completely.
2186 PeerConnectionInterface::RTCOfferAnswerOptions options;
2187 options.offer_to_receive_video = 0;
2188 callee()->SetOfferAnswerOptions(options);
2189 } else {
2190 // Stopping the video RtpTransceiver will cause the media section to be
2191 // rejected in the answer.
2192 callee()->SetRemoteOfferHandler([this] {
2193 callee()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)->Stop();
2194 });
2195 }
Steve Anton15324772018-01-16 10:26:49 -08002196 callee()->AddTrack(callee()->CreateLocalAudioTrack());
deadbeef1dcb1642017-03-29 21:08:16 -07002197 // Do offer/answer and wait for successful end-to-end audio frames.
2198 caller()->CreateAndSetAndSignalOffer();
2199 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002200 MediaExpectations media_expectations;
2201 media_expectations.ExpectBidirectionalAudio();
2202 media_expectations.ExpectNoVideo();
2203 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2204
deadbeef1dcb1642017-03-29 21:08:16 -07002205 // Sanity check that the callee's description has a rejected video section.
2206 ASSERT_NE(nullptr, callee()->pc()->local_description());
2207 const ContentInfo* callee_video_content =
2208 GetFirstVideoContent(callee()->pc()->local_description()->description());
2209 ASSERT_NE(nullptr, callee_video_content);
2210 EXPECT_TRUE(callee_video_content->rejected);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002211 if (sdp_semantics_ == SdpSemantics::kUnifiedPlan) {
2212 // The caller's transceiver should have stopped after receiving the answer.
2213 EXPECT_TRUE(caller()
2214 ->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)
2215 ->stopped());
2216 }
deadbeef1dcb1642017-03-29 21:08:16 -07002217}
2218
2219// Test that if the answerer rejects both audio and video m= sections, nothing
2220// bad happens.
2221// TODO(deadbeef): Test that a data channel still works. Currently this doesn't
2222// test anything but the fact that negotiation succeeds, which doesn't mean
2223// much.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002224TEST_P(PeerConnectionIntegrationTest, AnswererRejectsAudioAndVideoSections) {
deadbeef1dcb1642017-03-29 21:08:16 -07002225 ASSERT_TRUE(CreatePeerConnectionWrappers());
2226 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002227 caller()->AddAudioVideoTracks();
Seth Hampson2f0d7022018-02-20 11:54:42 -08002228 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2229 // Don't give the callee any tracks, and set offer_to_receive_X to 0, so it
2230 // will reject both audio and video m= sections.
2231 PeerConnectionInterface::RTCOfferAnswerOptions options;
2232 options.offer_to_receive_audio = 0;
2233 options.offer_to_receive_video = 0;
2234 callee()->SetOfferAnswerOptions(options);
2235 } else {
2236 callee()->SetRemoteOfferHandler([this] {
2237 // Stopping all transceivers will cause all media sections to be rejected.
Mirko Bonadei739baf02019-01-27 17:29:42 +01002238 for (const auto& transceiver : callee()->pc()->GetTransceivers()) {
Seth Hampson2f0d7022018-02-20 11:54:42 -08002239 transceiver->Stop();
2240 }
2241 });
2242 }
deadbeef1dcb1642017-03-29 21:08:16 -07002243 // Do offer/answer and wait for stable signaling state.
2244 caller()->CreateAndSetAndSignalOffer();
2245 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002246
deadbeef1dcb1642017-03-29 21:08:16 -07002247 // Sanity check that the callee's description has rejected m= sections.
2248 ASSERT_NE(nullptr, callee()->pc()->local_description());
2249 const ContentInfo* callee_audio_content =
2250 GetFirstAudioContent(callee()->pc()->local_description()->description());
2251 ASSERT_NE(nullptr, callee_audio_content);
2252 EXPECT_TRUE(callee_audio_content->rejected);
2253 const ContentInfo* callee_video_content =
2254 GetFirstVideoContent(callee()->pc()->local_description()->description());
2255 ASSERT_NE(nullptr, callee_video_content);
2256 EXPECT_TRUE(callee_video_content->rejected);
2257}
2258
2259// This test sets up an audio and video call between two parties. After the
2260// call runs for a while, the caller sends an updated offer with video being
2261// rejected. Once the re-negotiation is done, the video flow should stop and
2262// the audio flow should continue.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002263TEST_P(PeerConnectionIntegrationTest, VideoRejectedInSubsequentOffer) {
deadbeef1dcb1642017-03-29 21:08:16 -07002264 ASSERT_TRUE(CreatePeerConnectionWrappers());
2265 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002266 caller()->AddAudioVideoTracks();
2267 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002268 caller()->CreateAndSetAndSignalOffer();
2269 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002270 {
2271 MediaExpectations media_expectations;
2272 media_expectations.ExpectBidirectionalAudioAndVideo();
2273 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2274 }
deadbeef1dcb1642017-03-29 21:08:16 -07002275 // Renegotiate, rejecting the video m= section.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002276 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2277 caller()->SetGeneratedSdpMunger(
2278 [](cricket::SessionDescription* description) {
2279 for (cricket::ContentInfo& content : description->contents()) {
2280 if (cricket::IsVideoContent(&content)) {
2281 content.rejected = true;
2282 }
2283 }
2284 });
2285 } else {
2286 caller()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)->Stop();
2287 }
deadbeef1dcb1642017-03-29 21:08:16 -07002288 caller()->CreateAndSetAndSignalOffer();
2289 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
2290
2291 // Sanity check that the caller's description has a rejected video section.
2292 ASSERT_NE(nullptr, caller()->pc()->local_description());
2293 const ContentInfo* caller_video_content =
2294 GetFirstVideoContent(caller()->pc()->local_description()->description());
2295 ASSERT_NE(nullptr, caller_video_content);
2296 EXPECT_TRUE(caller_video_content->rejected);
deadbeef1dcb1642017-03-29 21:08:16 -07002297 // Wait for some additional audio frames to be received.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002298 {
2299 MediaExpectations media_expectations;
2300 media_expectations.ExpectBidirectionalAudio();
2301 media_expectations.ExpectNoVideo();
2302 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2303 }
deadbeef1dcb1642017-03-29 21:08:16 -07002304}
2305
Taylor Brandstetter60c8dc82018-04-11 15:20:27 -07002306// Do one offer/answer with audio, another that disables it (rejecting the m=
2307// section), and another that re-enables it. Regression test for:
2308// bugs.webrtc.org/6023
2309TEST_F(PeerConnectionIntegrationTestPlanB, EnableAudioAfterRejecting) {
2310 ASSERT_TRUE(CreatePeerConnectionWrappers());
2311 ConnectFakeSignaling();
2312
2313 // Add audio track, do normal offer/answer.
2314 rtc::scoped_refptr<webrtc::AudioTrackInterface> track =
2315 caller()->CreateLocalAudioTrack();
2316 rtc::scoped_refptr<webrtc::RtpSenderInterface> sender =
2317 caller()->pc()->AddTrack(track, {"stream"}).MoveValue();
2318 caller()->CreateAndSetAndSignalOffer();
2319 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2320
2321 // Remove audio track, and set offer_to_receive_audio to false to cause the
2322 // m= section to be completely disabled, not just "recvonly".
2323 caller()->pc()->RemoveTrack(sender);
2324 PeerConnectionInterface::RTCOfferAnswerOptions options;
2325 options.offer_to_receive_audio = 0;
2326 caller()->SetOfferAnswerOptions(options);
2327 caller()->CreateAndSetAndSignalOffer();
2328 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2329
2330 // Add the audio track again, expecting negotiation to succeed and frames to
2331 // flow.
2332 sender = caller()->pc()->AddTrack(track, {"stream"}).MoveValue();
2333 options.offer_to_receive_audio = 1;
2334 caller()->SetOfferAnswerOptions(options);
2335 caller()->CreateAndSetAndSignalOffer();
2336 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2337
2338 MediaExpectations media_expectations;
2339 media_expectations.CalleeExpectsSomeAudio();
2340 EXPECT_TRUE(ExpectNewFrames(media_expectations));
2341}
2342
deadbeef1dcb1642017-03-29 21:08:16 -07002343// Basic end-to-end test, but without SSRC/MSID signaling. This functionality
2344// is needed to support legacy endpoints.
2345// TODO(deadbeef): When we support the MID extension and demuxing on MID, also
2346// add a test for an end-to-end test without MID signaling either (basically,
2347// the minimum acceptable SDP).
Seth Hampson2f0d7022018-02-20 11:54:42 -08002348TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithoutSsrcOrMsidSignaling) {
deadbeef1dcb1642017-03-29 21:08:16 -07002349 ASSERT_TRUE(CreatePeerConnectionWrappers());
2350 ConnectFakeSignaling();
2351 // Add audio and video, testing that packets can be demuxed on payload type.
Steve Anton15324772018-01-16 10:26:49 -08002352 caller()->AddAudioVideoTracks();
2353 callee()->AddAudioVideoTracks();
deadbeefd8ad7882017-04-18 16:01:17 -07002354 // Remove SSRCs and MSIDs from the received offer SDP.
2355 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
deadbeef1dcb1642017-03-29 21:08:16 -07002356 caller()->CreateAndSetAndSignalOffer();
2357 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002358 MediaExpectations media_expectations;
2359 media_expectations.ExpectBidirectionalAudioAndVideo();
2360 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002361}
2362
Seth Hampson5897a6e2018-04-03 11:16:33 -07002363// Basic end-to-end test, without SSRC signaling. This means that the track
2364// was created properly and frames are delivered when the MSIDs are communicated
2365// with a=msid lines and no a=ssrc lines.
2366TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
2367 EndToEndCallWithoutSsrcSignaling) {
2368 const char kStreamId[] = "streamId";
2369 ASSERT_TRUE(CreatePeerConnectionWrappers());
2370 ConnectFakeSignaling();
2371 // Add just audio tracks.
2372 caller()->AddTrack(caller()->CreateLocalAudioTrack(), {kStreamId});
2373 callee()->AddAudioTrack();
2374
2375 // Remove SSRCs from the received offer SDP.
2376 callee()->SetReceivedSdpMunger(RemoveSsrcsAndKeepMsids);
2377 caller()->CreateAndSetAndSignalOffer();
2378 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2379 MediaExpectations media_expectations;
2380 media_expectations.ExpectBidirectionalAudio();
2381 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2382}
2383
Steve Antondf527fd2018-04-27 15:52:03 -07002384// Tests that video flows between multiple video tracks when SSRCs are not
2385// signaled. This exercises the MID RTP header extension which is needed to
2386// demux the incoming video tracks.
2387TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
2388 EndToEndCallWithTwoVideoTracksAndNoSignaledSsrc) {
2389 ASSERT_TRUE(CreatePeerConnectionWrappers());
2390 ConnectFakeSignaling();
2391 caller()->AddVideoTrack();
2392 caller()->AddVideoTrack();
2393 callee()->AddVideoTrack();
2394 callee()->AddVideoTrack();
2395
2396 caller()->SetReceivedSdpMunger(&RemoveSsrcsAndKeepMsids);
2397 callee()->SetReceivedSdpMunger(&RemoveSsrcsAndKeepMsids);
2398 caller()->CreateAndSetAndSignalOffer();
2399 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2400 ASSERT_EQ(2u, caller()->pc()->GetReceivers().size());
2401 ASSERT_EQ(2u, callee()->pc()->GetReceivers().size());
2402
2403 // Expect video to be received in both directions on both tracks.
2404 MediaExpectations media_expectations;
2405 media_expectations.ExpectBidirectionalVideo();
2406 EXPECT_TRUE(ExpectNewFrames(media_expectations));
2407}
2408
Henrik Boström5b147782018-12-04 11:25:05 +01002409TEST_F(PeerConnectionIntegrationTestUnifiedPlan, NoStreamsMsidLinePresent) {
2410 ASSERT_TRUE(CreatePeerConnectionWrappers());
2411 ConnectFakeSignaling();
2412 caller()->AddAudioTrack();
2413 caller()->AddVideoTrack();
2414 caller()->CreateAndSetAndSignalOffer();
2415 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2416 auto callee_receivers = callee()->pc()->GetReceivers();
2417 ASSERT_EQ(2u, callee_receivers.size());
2418 EXPECT_TRUE(callee_receivers[0]->stream_ids().empty());
2419 EXPECT_TRUE(callee_receivers[1]->stream_ids().empty());
2420}
2421
2422TEST_F(PeerConnectionIntegrationTestUnifiedPlan, NoStreamsMsidLineMissing) {
2423 ASSERT_TRUE(CreatePeerConnectionWrappers());
2424 ConnectFakeSignaling();
2425 caller()->AddAudioTrack();
2426 caller()->AddVideoTrack();
2427 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
2428 caller()->CreateAndSetAndSignalOffer();
2429 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2430 auto callee_receivers = callee()->pc()->GetReceivers();
2431 ASSERT_EQ(2u, callee_receivers.size());
2432 ASSERT_EQ(1u, callee_receivers[0]->stream_ids().size());
2433 ASSERT_EQ(1u, callee_receivers[1]->stream_ids().size());
2434 EXPECT_EQ(callee_receivers[0]->stream_ids()[0],
2435 callee_receivers[1]->stream_ids()[0]);
2436 EXPECT_EQ(callee_receivers[0]->streams()[0],
2437 callee_receivers[1]->streams()[0]);
2438}
2439
deadbeef1dcb1642017-03-29 21:08:16 -07002440// Test that if two video tracks are sent (from caller to callee, in this test),
2441// they're transmitted correctly end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002442TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithTwoVideoTracks) {
deadbeef1dcb1642017-03-29 21:08:16 -07002443 ASSERT_TRUE(CreatePeerConnectionWrappers());
2444 ConnectFakeSignaling();
2445 // Add one audio/video stream, and one video-only stream.
Steve Anton15324772018-01-16 10:26:49 -08002446 caller()->AddAudioVideoTracks();
2447 caller()->AddVideoTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07002448 caller()->CreateAndSetAndSignalOffer();
2449 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Steve Anton15324772018-01-16 10:26:49 -08002450 ASSERT_EQ(3u, callee()->pc()->GetReceivers().size());
Seth Hampson2f0d7022018-02-20 11:54:42 -08002451
2452 MediaExpectations media_expectations;
2453 media_expectations.CalleeExpectsSomeAudioAndVideo();
2454 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002455}
2456
2457static void MakeSpecCompliantMaxBundleOffer(cricket::SessionDescription* desc) {
2458 bool first = true;
2459 for (cricket::ContentInfo& content : desc->contents()) {
2460 if (first) {
2461 first = false;
2462 continue;
2463 }
2464 content.bundle_only = true;
2465 }
2466 first = true;
2467 for (cricket::TransportInfo& transport : desc->transport_infos()) {
2468 if (first) {
2469 first = false;
2470 continue;
2471 }
2472 transport.description.ice_ufrag.clear();
2473 transport.description.ice_pwd.clear();
2474 transport.description.connection_role = cricket::CONNECTIONROLE_NONE;
2475 transport.description.identity_fingerprint.reset(nullptr);
2476 }
2477}
2478
2479// Test that if applying a true "max bundle" offer, which uses ports of 0,
2480// "a=bundle-only", omitting "a=fingerprint", "a=setup", "a=ice-ufrag" and
2481// "a=ice-pwd" for all but the audio "m=" section, negotiation still completes
2482// successfully and media flows.
2483// TODO(deadbeef): Update this test to also omit "a=rtcp-mux", once that works.
2484// TODO(deadbeef): Won't need this test once we start generating actual
2485// standards-compliant SDP.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002486TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07002487 EndToEndCallWithSpecCompliantMaxBundleOffer) {
2488 ASSERT_TRUE(CreatePeerConnectionWrappers());
2489 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002490 caller()->AddAudioVideoTracks();
2491 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002492 // Do the equivalent of setting the port to 0, adding a=bundle-only, and
2493 // removing a=ice-ufrag, a=ice-pwd, a=fingerprint and a=setup from all
2494 // but the first m= section.
2495 callee()->SetReceivedSdpMunger(MakeSpecCompliantMaxBundleOffer);
2496 caller()->CreateAndSetAndSignalOffer();
2497 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002498 MediaExpectations media_expectations;
2499 media_expectations.ExpectBidirectionalAudioAndVideo();
2500 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002501}
2502
2503// Test that we can receive the audio output level from a remote audio track.
2504// TODO(deadbeef): Use a fake audio source and verify that the output level is
2505// exactly what the source on the other side was configured with.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002506TEST_P(PeerConnectionIntegrationTest, GetAudioOutputLevelStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-29 21:08:16 -07002507 ASSERT_TRUE(CreatePeerConnectionWrappers());
2508 ConnectFakeSignaling();
2509 // Just add an audio track.
Steve Anton15324772018-01-16 10:26:49 -08002510 caller()->AddAudioTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07002511 caller()->CreateAndSetAndSignalOffer();
2512 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2513
2514 // Get the audio output level stats. Note that the level is not available
2515 // until an RTCP packet has been received.
deadbeefd8ad7882017-04-18 16:01:17 -07002516 EXPECT_TRUE_WAIT(callee()->OldGetStats()->AudioOutputLevel() > 0,
deadbeef1dcb1642017-03-29 21:08:16 -07002517 kMaxWaitForFramesMs);
2518}
2519
2520// Test that an audio input level is reported.
2521// TODO(deadbeef): Use a fake audio source and verify that the input level is
2522// exactly what the source was configured with.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002523TEST_P(PeerConnectionIntegrationTest, GetAudioInputLevelStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-29 21:08:16 -07002524 ASSERT_TRUE(CreatePeerConnectionWrappers());
2525 ConnectFakeSignaling();
2526 // Just add an audio track.
Steve Anton15324772018-01-16 10:26:49 -08002527 caller()->AddAudioTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07002528 caller()->CreateAndSetAndSignalOffer();
2529 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2530
2531 // Get the audio input level stats. The level should be available very
2532 // soon after the test starts.
deadbeefd8ad7882017-04-18 16:01:17 -07002533 EXPECT_TRUE_WAIT(caller()->OldGetStats()->AudioInputLevel() > 0,
deadbeef1dcb1642017-03-29 21:08:16 -07002534 kMaxWaitForStatsMs);
2535}
2536
2537// Test that we can get incoming byte counts from both audio and video tracks.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002538TEST_P(PeerConnectionIntegrationTest, GetBytesReceivedStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-29 21:08:16 -07002539 ASSERT_TRUE(CreatePeerConnectionWrappers());
2540 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002541 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002542 // Do offer/answer, wait for the callee to receive some frames.
2543 caller()->CreateAndSetAndSignalOffer();
2544 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002545
2546 MediaExpectations media_expectations;
2547 media_expectations.CalleeExpectsSomeAudioAndVideo();
2548 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002549
2550 // Get a handle to the remote tracks created, so they can be used as GetStats
2551 // filters.
Mirko Bonadei739baf02019-01-27 17:29:42 +01002552 for (const auto& receiver : callee()->pc()->GetReceivers()) {
Steve Anton15324772018-01-16 10:26:49 -08002553 // We received frames, so we definitely should have nonzero "received bytes"
2554 // stats at this point.
2555 EXPECT_GT(callee()->OldGetStatsForTrack(receiver->track())->BytesReceived(),
2556 0);
2557 }
deadbeef1dcb1642017-03-29 21:08:16 -07002558}
2559
2560// Test that we can get outgoing byte counts from both audio and video tracks.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002561TEST_P(PeerConnectionIntegrationTest, GetBytesSentStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-29 21:08:16 -07002562 ASSERT_TRUE(CreatePeerConnectionWrappers());
2563 ConnectFakeSignaling();
2564 auto audio_track = caller()->CreateLocalAudioTrack();
2565 auto video_track = caller()->CreateLocalVideoTrack();
Steve Anton15324772018-01-16 10:26:49 -08002566 caller()->AddTrack(audio_track);
2567 caller()->AddTrack(video_track);
deadbeef1dcb1642017-03-29 21:08:16 -07002568 // Do offer/answer, wait for the callee to receive some frames.
2569 caller()->CreateAndSetAndSignalOffer();
2570 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002571 MediaExpectations media_expectations;
2572 media_expectations.CalleeExpectsSomeAudioAndVideo();
2573 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002574
2575 // The callee received frames, so we definitely should have nonzero "sent
2576 // bytes" stats at this point.
deadbeefd8ad7882017-04-18 16:01:17 -07002577 EXPECT_GT(caller()->OldGetStatsForTrack(audio_track)->BytesSent(), 0);
2578 EXPECT_GT(caller()->OldGetStatsForTrack(video_track)->BytesSent(), 0);
2579}
2580
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002581// Test that we can get capture start ntp time.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002582TEST_P(PeerConnectionIntegrationTest, GetCaptureStartNtpTimeWithOldStatsApi) {
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002583 ASSERT_TRUE(CreatePeerConnectionWrappers());
2584 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002585 caller()->AddAudioTrack();
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002586
Steve Anton15324772018-01-16 10:26:49 -08002587 callee()->AddAudioTrack();
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002588
2589 // Do offer/answer, wait for the callee to receive some frames.
2590 caller()->CreateAndSetAndSignalOffer();
2591 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2592
2593 // Get the remote audio track created on the receiver, so they can be used as
2594 // GetStats filters.
Steve Antonfc853712018-03-01 13:48:58 -08002595 auto receivers = callee()->pc()->GetReceivers();
2596 ASSERT_EQ(1u, receivers.size());
2597 auto remote_audio_track = receivers[0]->track();
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002598
2599 // Get the audio output level stats. Note that the level is not available
2600 // until an RTCP packet has been received.
Zhi Huange830e682018-03-30 10:48:35 -07002601 EXPECT_TRUE_WAIT(
2602 callee()->OldGetStatsForTrack(remote_audio_track)->CaptureStartNtpTime() >
2603 0,
2604 2 * kMaxWaitForFramesMs);
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002605}
2606
Steve Antona41959e2018-11-28 11:15:33 -08002607// Test that the track ID is associated with all local and remote SSRC stats
2608// using the old GetStats() and more than 1 audio and more than 1 video track.
2609// This is a regression test for crbug.com/906988
2610TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
2611 OldGetStatsAssociatesTrackIdForManyMediaSections) {
2612 ASSERT_TRUE(CreatePeerConnectionWrappers());
2613 ConnectFakeSignaling();
2614 auto audio_sender_1 = caller()->AddAudioTrack();
2615 auto video_sender_1 = caller()->AddVideoTrack();
2616 auto audio_sender_2 = caller()->AddAudioTrack();
2617 auto video_sender_2 = caller()->AddVideoTrack();
2618 caller()->CreateAndSetAndSignalOffer();
2619 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2620
2621 MediaExpectations media_expectations;
2622 media_expectations.CalleeExpectsSomeAudioAndVideo();
2623 ASSERT_TRUE_WAIT(ExpectNewFrames(media_expectations), kDefaultTimeout);
2624
2625 std::vector<std::string> track_ids = {
2626 audio_sender_1->track()->id(), video_sender_1->track()->id(),
2627 audio_sender_2->track()->id(), video_sender_2->track()->id()};
2628
2629 auto caller_stats = caller()->OldGetStats();
2630 EXPECT_THAT(caller_stats->TrackIds(), UnorderedElementsAreArray(track_ids));
2631 auto callee_stats = callee()->OldGetStats();
2632 EXPECT_THAT(callee_stats->TrackIds(), UnorderedElementsAreArray(track_ids));
2633}
2634
Steve Antonffa6ce42018-11-30 09:26:08 -08002635// Test that the new GetStats() returns stats for all outgoing/incoming streams
2636// with the correct track IDs if there are more than one audio and more than one
2637// video senders/receivers.
2638TEST_P(PeerConnectionIntegrationTest, NewGetStatsManyAudioAndManyVideoStreams) {
2639 ASSERT_TRUE(CreatePeerConnectionWrappers());
2640 ConnectFakeSignaling();
2641 auto audio_sender_1 = caller()->AddAudioTrack();
2642 auto video_sender_1 = caller()->AddVideoTrack();
2643 auto audio_sender_2 = caller()->AddAudioTrack();
2644 auto video_sender_2 = caller()->AddVideoTrack();
2645 caller()->CreateAndSetAndSignalOffer();
2646 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2647
2648 MediaExpectations media_expectations;
2649 media_expectations.CalleeExpectsSomeAudioAndVideo();
2650 ASSERT_TRUE_WAIT(ExpectNewFrames(media_expectations), kDefaultTimeout);
2651
2652 std::vector<std::string> track_ids = {
2653 audio_sender_1->track()->id(), video_sender_1->track()->id(),
2654 audio_sender_2->track()->id(), video_sender_2->track()->id()};
2655
2656 rtc::scoped_refptr<const webrtc::RTCStatsReport> caller_report =
2657 caller()->NewGetStats();
2658 ASSERT_TRUE(caller_report);
2659 auto outbound_stream_stats =
2660 caller_report->GetStatsOfType<webrtc::RTCOutboundRTPStreamStats>();
2661 ASSERT_EQ(4u, outbound_stream_stats.size());
2662 std::vector<std::string> outbound_track_ids;
2663 for (const auto& stat : outbound_stream_stats) {
2664 ASSERT_TRUE(stat->bytes_sent.is_defined());
2665 EXPECT_LT(0u, *stat->bytes_sent);
2666 ASSERT_TRUE(stat->track_id.is_defined());
2667 const auto* track_stat =
2668 caller_report->GetAs<webrtc::RTCMediaStreamTrackStats>(*stat->track_id);
2669 ASSERT_TRUE(track_stat);
2670 outbound_track_ids.push_back(*track_stat->track_identifier);
2671 }
2672 EXPECT_THAT(outbound_track_ids, UnorderedElementsAreArray(track_ids));
2673
2674 rtc::scoped_refptr<const webrtc::RTCStatsReport> callee_report =
2675 callee()->NewGetStats();
2676 ASSERT_TRUE(callee_report);
2677 auto inbound_stream_stats =
2678 callee_report->GetStatsOfType<webrtc::RTCInboundRTPStreamStats>();
2679 ASSERT_EQ(4u, inbound_stream_stats.size());
2680 std::vector<std::string> inbound_track_ids;
2681 for (const auto& stat : inbound_stream_stats) {
2682 ASSERT_TRUE(stat->bytes_received.is_defined());
2683 EXPECT_LT(0u, *stat->bytes_received);
2684 ASSERT_TRUE(stat->track_id.is_defined());
2685 const auto* track_stat =
2686 callee_report->GetAs<webrtc::RTCMediaStreamTrackStats>(*stat->track_id);
2687 ASSERT_TRUE(track_stat);
2688 inbound_track_ids.push_back(*track_stat->track_identifier);
2689 }
2690 EXPECT_THAT(inbound_track_ids, UnorderedElementsAreArray(track_ids));
2691}
2692
2693// Test that we can get stats (using the new stats implementation) for
deadbeefd8ad7882017-04-18 16:01:17 -07002694// unsignaled streams. Meaning when SSRCs/MSIDs aren't signaled explicitly in
2695// SDP.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002696TEST_P(PeerConnectionIntegrationTest,
deadbeefd8ad7882017-04-18 16:01:17 -07002697 GetStatsForUnsignaledStreamWithNewStatsApi) {
2698 ASSERT_TRUE(CreatePeerConnectionWrappers());
2699 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002700 caller()->AddAudioTrack();
deadbeefd8ad7882017-04-18 16:01:17 -07002701 // Remove SSRCs and MSIDs from the received offer SDP.
2702 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
2703 caller()->CreateAndSetAndSignalOffer();
2704 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002705 MediaExpectations media_expectations;
2706 media_expectations.CalleeExpectsSomeAudio(1);
2707 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeefd8ad7882017-04-18 16:01:17 -07002708
2709 // We received a frame, so we should have nonzero "bytes received" stats for
2710 // the unsignaled stream, if stats are working for it.
2711 rtc::scoped_refptr<const webrtc::RTCStatsReport> report =
2712 callee()->NewGetStats();
2713 ASSERT_NE(nullptr, report);
2714 auto inbound_stream_stats =
2715 report->GetStatsOfType<webrtc::RTCInboundRTPStreamStats>();
2716 ASSERT_EQ(1U, inbound_stream_stats.size());
2717 ASSERT_TRUE(inbound_stream_stats[0]->bytes_received.is_defined());
2718 ASSERT_GT(*inbound_stream_stats[0]->bytes_received, 0U);
zhihuangf8164932017-05-19 13:09:47 -07002719 ASSERT_TRUE(inbound_stream_stats[0]->track_id.is_defined());
2720}
2721
Taylor Brandstettera4653442018-06-19 09:44:26 -07002722// Same as above but for the legacy stats implementation.
2723TEST_P(PeerConnectionIntegrationTest,
2724 GetStatsForUnsignaledStreamWithOldStatsApi) {
2725 ASSERT_TRUE(CreatePeerConnectionWrappers());
2726 ConnectFakeSignaling();
2727 caller()->AddAudioTrack();
2728 // Remove SSRCs and MSIDs from the received offer SDP.
2729 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
2730 caller()->CreateAndSetAndSignalOffer();
2731 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2732
2733 // Note that, since the old stats implementation associates SSRCs with tracks
2734 // using SDP, when SSRCs aren't signaled in SDP these stats won't have an
2735 // associated track ID. So we can't use the track "selector" argument.
2736 //
2737 // Also, we use "EXPECT_TRUE_WAIT" because the stats collector may decide to
2738 // return cached stats if not enough time has passed since the last update.
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02002739 EXPECT_TRUE_WAIT(callee()->OldGetStats()->BytesReceived() > 0,
Taylor Brandstettera4653442018-06-19 09:44:26 -07002740 kDefaultTimeout);
2741}
2742
zhihuangf8164932017-05-19 13:09:47 -07002743// Test that we can successfully get the media related stats (audio level
2744// etc.) for the unsignaled stream.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002745TEST_P(PeerConnectionIntegrationTest,
zhihuangf8164932017-05-19 13:09:47 -07002746 GetMediaStatsForUnsignaledStreamWithNewStatsApi) {
2747 ASSERT_TRUE(CreatePeerConnectionWrappers());
2748 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002749 caller()->AddAudioVideoTracks();
zhihuangf8164932017-05-19 13:09:47 -07002750 // Remove SSRCs and MSIDs from the received offer SDP.
2751 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
2752 caller()->CreateAndSetAndSignalOffer();
2753 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002754 MediaExpectations media_expectations;
2755 media_expectations.CalleeExpectsSomeAudio(1);
2756 media_expectations.CalleeExpectsSomeVideo(1);
2757 ASSERT_TRUE(ExpectNewFrames(media_expectations));
zhihuangf8164932017-05-19 13:09:47 -07002758
2759 rtc::scoped_refptr<const webrtc::RTCStatsReport> report =
2760 callee()->NewGetStats();
2761 ASSERT_NE(nullptr, report);
2762
2763 auto media_stats = report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
2764 auto audio_index = FindFirstMediaStatsIndexByKind("audio", media_stats);
2765 ASSERT_GE(audio_index, 0);
2766 EXPECT_TRUE(media_stats[audio_index]->audio_level.is_defined());
deadbeef1dcb1642017-03-29 21:08:16 -07002767}
2768
deadbeef4e2deab2017-09-20 13:56:21 -07002769// Helper for test below.
2770void ModifySsrcs(cricket::SessionDescription* desc) {
2771 for (ContentInfo& content : desc->contents()) {
Steve Antondf527fd2018-04-27 15:52:03 -07002772 for (StreamParams& stream :
Steve Antonb1c1de12017-12-21 15:14:30 -08002773 content.media_description()->mutable_streams()) {
deadbeef4e2deab2017-09-20 13:56:21 -07002774 for (uint32_t& ssrc : stream.ssrcs) {
2775 ssrc = rtc::CreateRandomId();
2776 }
2777 }
2778 }
2779}
2780
2781// Test that the "RTCMediaSteamTrackStats" object is updated correctly when
2782// SSRCs are unsignaled, and the SSRC of the received (audio) stream changes.
2783// This should result in two "RTCInboundRTPStreamStats", but only one
2784// "RTCMediaStreamTrackStats", whose counters go up continuously rather than
2785// being reset to 0 once the SSRC change occurs.
2786//
2787// Regression test for this bug:
2788// https://bugs.chromium.org/p/webrtc/issues/detail?id=8158
2789//
2790// The bug causes the track stats to only represent one of the two streams:
2791// whichever one has the higher SSRC. So with this bug, there was a 50% chance
2792// that the track stat counters would reset to 0 when the new stream is
2793// received, and a 50% chance that they'll stop updating (while
2794// "concealed_samples" continues increasing, due to silence being generated for
2795// the inactive stream).
Seth Hampson2f0d7022018-02-20 11:54:42 -08002796TEST_P(PeerConnectionIntegrationTest,
Steve Anton83119dd2017-11-10 16:19:52 -08002797 TrackStatsUpdatedCorrectlyWhenUnsignaledSsrcChanges) {
deadbeef4e2deab2017-09-20 13:56:21 -07002798 ASSERT_TRUE(CreatePeerConnectionWrappers());
2799 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002800 caller()->AddAudioTrack();
deadbeef4e2deab2017-09-20 13:56:21 -07002801 // Remove SSRCs and MSIDs from the received offer SDP, simulating an endpoint
2802 // that doesn't signal SSRCs (from the callee's perspective).
2803 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
2804 caller()->CreateAndSetAndSignalOffer();
2805 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2806 // Wait for 50 audio frames (500ms of audio) to be received by the callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002807 {
2808 MediaExpectations media_expectations;
2809 media_expectations.CalleeExpectsSomeAudio(50);
2810 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2811 }
deadbeef4e2deab2017-09-20 13:56:21 -07002812 // Some audio frames were received, so we should have nonzero "samples
2813 // received" for the track.
2814 rtc::scoped_refptr<const webrtc::RTCStatsReport> report =
2815 callee()->NewGetStats();
2816 ASSERT_NE(nullptr, report);
2817 auto track_stats = report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
2818 ASSERT_EQ(1U, track_stats.size());
2819 ASSERT_TRUE(track_stats[0]->total_samples_received.is_defined());
2820 ASSERT_GT(*track_stats[0]->total_samples_received, 0U);
2821 // uint64_t prev_samples_received = *track_stats[0]->total_samples_received;
2822
2823 // Create a new offer and munge it to cause the caller to use a new SSRC.
2824 caller()->SetGeneratedSdpMunger(ModifySsrcs);
2825 caller()->CreateAndSetAndSignalOffer();
2826 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2827 // Wait for 25 more audio frames (250ms of audio) to be received, from the new
2828 // SSRC.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002829 {
2830 MediaExpectations media_expectations;
2831 media_expectations.CalleeExpectsSomeAudio(25);
2832 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2833 }
deadbeef4e2deab2017-09-20 13:56:21 -07002834
2835 report = callee()->NewGetStats();
2836 ASSERT_NE(nullptr, report);
2837 track_stats = report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
2838 ASSERT_EQ(1U, track_stats.size());
2839 ASSERT_TRUE(track_stats[0]->total_samples_received.is_defined());
2840 // The "total samples received" stat should only be greater than it was
2841 // before.
2842 // TODO(deadbeef): Uncomment this assertion once the bug is completely fixed.
2843 // Right now, the new SSRC will cause the counters to reset to 0.
2844 // EXPECT_GT(*track_stats[0]->total_samples_received, prev_samples_received);
2845
2846 // Additionally, the percentage of concealed samples (samples generated to
Steve Anton83119dd2017-11-10 16:19:52 -08002847 // conceal packet loss) should be less than 50%. If it's greater, that's a
deadbeef4e2deab2017-09-20 13:56:21 -07002848 // good sign that we're seeing stats from the old stream that's no longer
2849 // receiving packets, and is generating concealed samples of silence.
Steve Anton83119dd2017-11-10 16:19:52 -08002850 constexpr double kAcceptableConcealedSamplesPercentage = 0.50;
deadbeef4e2deab2017-09-20 13:56:21 -07002851 ASSERT_TRUE(track_stats[0]->concealed_samples.is_defined());
2852 EXPECT_LT(*track_stats[0]->concealed_samples,
2853 *track_stats[0]->total_samples_received *
2854 kAcceptableConcealedSamplesPercentage);
2855
2856 // Also ensure that we have two "RTCInboundRTPStreamStats" as expected, as a
2857 // sanity check that the SSRC really changed.
2858 // TODO(deadbeef): This isn't working right now, because we're not returning
2859 // *any* stats for the inactive stream. Uncomment when the bug is completely
2860 // fixed.
2861 // auto inbound_stream_stats =
2862 // report->GetStatsOfType<webrtc::RTCInboundRTPStreamStats>();
2863 // ASSERT_EQ(2U, inbound_stream_stats.size());
2864}
2865
deadbeef1dcb1642017-03-29 21:08:16 -07002866// Test that DTLS 1.0 is used if both sides only support DTLS 1.0.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002867TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithDtls10) {
deadbeef1dcb1642017-03-29 21:08:16 -07002868 PeerConnectionFactory::Options dtls_10_options;
2869 dtls_10_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
2870 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(dtls_10_options,
2871 dtls_10_options));
2872 ConnectFakeSignaling();
2873 // Do normal offer/answer and wait for some frames to be received in each
2874 // direction.
Steve Anton15324772018-01-16 10:26:49 -08002875 caller()->AddAudioVideoTracks();
2876 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002877 caller()->CreateAndSetAndSignalOffer();
2878 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002879 MediaExpectations media_expectations;
2880 media_expectations.ExpectBidirectionalAudioAndVideo();
2881 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002882}
2883
2884// Test getting cipher stats and UMA metrics when DTLS 1.0 is negotiated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002885TEST_P(PeerConnectionIntegrationTest, Dtls10CipherStatsAndUmaMetrics) {
deadbeef1dcb1642017-03-29 21:08:16 -07002886 PeerConnectionFactory::Options dtls_10_options;
2887 dtls_10_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
2888 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(dtls_10_options,
2889 dtls_10_options));
2890 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002891 caller()->AddAudioVideoTracks();
2892 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002893 caller()->CreateAndSetAndSignalOffer();
Qingsi Wang7fc821d2018-07-12 12:54:53 -07002894 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07002895 EXPECT_TRUE_WAIT(rtc::SSLStreamAdapter::IsAcceptableCipher(
deadbeefd8ad7882017-04-18 16:01:17 -07002896 caller()->OldGetStats()->DtlsCipher(), rtc::KT_DEFAULT),
deadbeef1dcb1642017-03-29 21:08:16 -07002897 kDefaultTimeout);
2898 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
deadbeefd8ad7882017-04-18 16:01:17 -07002899 caller()->OldGetStats()->SrtpCipher(), kDefaultTimeout);
Qingsi Wang7fc821d2018-07-12 12:54:53 -07002900 // TODO(bugs.webrtc.org/9456): Fix it.
Alex Loiko9289eda2018-11-23 16:18:59 +00002901 EXPECT_EQ(1, webrtc::metrics::NumEvents(
Qingsi Wang7fc821d2018-07-12 12:54:53 -07002902 "WebRTC.PeerConnection.SrtpCryptoSuite.Audio",
2903 kDefaultSrtpCryptoSuite));
deadbeef1dcb1642017-03-29 21:08:16 -07002904}
2905
2906// Test getting cipher stats and UMA metrics when DTLS 1.2 is negotiated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002907TEST_P(PeerConnectionIntegrationTest, Dtls12CipherStatsAndUmaMetrics) {
deadbeef1dcb1642017-03-29 21:08:16 -07002908 PeerConnectionFactory::Options dtls_12_options;
2909 dtls_12_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
2910 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(dtls_12_options,
2911 dtls_12_options));
2912 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002913 caller()->AddAudioVideoTracks();
2914 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002915 caller()->CreateAndSetAndSignalOffer();
Qingsi Wang7fc821d2018-07-12 12:54:53 -07002916 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07002917 EXPECT_TRUE_WAIT(rtc::SSLStreamAdapter::IsAcceptableCipher(
deadbeefd8ad7882017-04-18 16:01:17 -07002918 caller()->OldGetStats()->DtlsCipher(), rtc::KT_DEFAULT),
deadbeef1dcb1642017-03-29 21:08:16 -07002919 kDefaultTimeout);
2920 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
deadbeefd8ad7882017-04-18 16:01:17 -07002921 caller()->OldGetStats()->SrtpCipher(), kDefaultTimeout);
Qingsi Wang7fc821d2018-07-12 12:54:53 -07002922 // TODO(bugs.webrtc.org/9456): Fix it.
Alex Loiko9289eda2018-11-23 16:18:59 +00002923 EXPECT_EQ(1, webrtc::metrics::NumEvents(
Qingsi Wang7fc821d2018-07-12 12:54:53 -07002924 "WebRTC.PeerConnection.SrtpCryptoSuite.Audio",
2925 kDefaultSrtpCryptoSuite));
deadbeef1dcb1642017-03-29 21:08:16 -07002926}
2927
2928// Test that DTLS 1.0 can be used if the caller supports DTLS 1.2 and the
2929// callee only supports 1.0.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002930TEST_P(PeerConnectionIntegrationTest, CallerDtls12ToCalleeDtls10) {
deadbeef1dcb1642017-03-29 21:08:16 -07002931 PeerConnectionFactory::Options caller_options;
2932 caller_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
2933 PeerConnectionFactory::Options callee_options;
2934 callee_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
2935 ASSERT_TRUE(
2936 CreatePeerConnectionWrappersWithOptions(caller_options, callee_options));
2937 ConnectFakeSignaling();
2938 // Do normal offer/answer and wait for some frames to be received in each
2939 // direction.
Steve Anton15324772018-01-16 10:26:49 -08002940 caller()->AddAudioVideoTracks();
2941 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002942 caller()->CreateAndSetAndSignalOffer();
2943 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002944 MediaExpectations media_expectations;
2945 media_expectations.ExpectBidirectionalAudioAndVideo();
2946 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002947}
2948
2949// Test that DTLS 1.0 can be used if the caller only supports DTLS 1.0 and the
2950// callee supports 1.2.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002951TEST_P(PeerConnectionIntegrationTest, CallerDtls10ToCalleeDtls12) {
deadbeef1dcb1642017-03-29 21:08:16 -07002952 PeerConnectionFactory::Options caller_options;
2953 caller_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
2954 PeerConnectionFactory::Options callee_options;
2955 callee_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
2956 ASSERT_TRUE(
2957 CreatePeerConnectionWrappersWithOptions(caller_options, callee_options));
2958 ConnectFakeSignaling();
2959 // Do normal offer/answer and wait for some frames to be received in each
2960 // direction.
Steve Anton15324772018-01-16 10:26:49 -08002961 caller()->AddAudioVideoTracks();
2962 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002963 caller()->CreateAndSetAndSignalOffer();
2964 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002965 MediaExpectations media_expectations;
2966 media_expectations.ExpectBidirectionalAudioAndVideo();
2967 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002968}
2969
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07002970// The three tests below verify that "enable_aes128_sha1_32_crypto_cipher"
2971// works as expected; the cipher should only be used if enabled by both sides.
2972TEST_P(PeerConnectionIntegrationTest,
2973 Aes128Sha1_32_CipherNotUsedWhenOnlyCallerSupported) {
2974 PeerConnectionFactory::Options caller_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07002975 caller_options.crypto_options.srtp.enable_aes128_sha1_32_crypto_cipher = true;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07002976 PeerConnectionFactory::Options callee_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07002977 callee_options.crypto_options.srtp.enable_aes128_sha1_32_crypto_cipher =
2978 false;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07002979 int expected_cipher_suite = rtc::SRTP_AES128_CM_SHA1_80;
2980 TestNegotiatedCipherSuite(caller_options, callee_options,
2981 expected_cipher_suite);
2982}
2983
2984TEST_P(PeerConnectionIntegrationTest,
2985 Aes128Sha1_32_CipherNotUsedWhenOnlyCalleeSupported) {
2986 PeerConnectionFactory::Options caller_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07002987 caller_options.crypto_options.srtp.enable_aes128_sha1_32_crypto_cipher =
2988 false;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07002989 PeerConnectionFactory::Options callee_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07002990 callee_options.crypto_options.srtp.enable_aes128_sha1_32_crypto_cipher = true;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07002991 int expected_cipher_suite = rtc::SRTP_AES128_CM_SHA1_80;
2992 TestNegotiatedCipherSuite(caller_options, callee_options,
2993 expected_cipher_suite);
2994}
2995
2996TEST_P(PeerConnectionIntegrationTest, Aes128Sha1_32_CipherUsedWhenSupported) {
2997 PeerConnectionFactory::Options caller_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07002998 caller_options.crypto_options.srtp.enable_aes128_sha1_32_crypto_cipher = true;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07002999 PeerConnectionFactory::Options callee_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07003000 callee_options.crypto_options.srtp.enable_aes128_sha1_32_crypto_cipher = true;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07003001 int expected_cipher_suite = rtc::SRTP_AES128_CM_SHA1_32;
3002 TestNegotiatedCipherSuite(caller_options, callee_options,
3003 expected_cipher_suite);
3004}
3005
deadbeef1dcb1642017-03-29 21:08:16 -07003006// Test that a non-GCM cipher is used if both sides only support non-GCM.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003007TEST_P(PeerConnectionIntegrationTest, NonGcmCipherUsedWhenGcmNotSupported) {
deadbeef1dcb1642017-03-29 21:08:16 -07003008 bool local_gcm_enabled = false;
3009 bool remote_gcm_enabled = false;
3010 int expected_cipher_suite = kDefaultSrtpCryptoSuite;
3011 TestGcmNegotiationUsesCipherSuite(local_gcm_enabled, remote_gcm_enabled,
3012 expected_cipher_suite);
3013}
3014
3015// Test that a GCM cipher is used if both ends support it.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003016TEST_P(PeerConnectionIntegrationTest, GcmCipherUsedWhenGcmSupported) {
deadbeef1dcb1642017-03-29 21:08:16 -07003017 bool local_gcm_enabled = true;
3018 bool remote_gcm_enabled = true;
3019 int expected_cipher_suite = kDefaultSrtpCryptoSuiteGcm;
3020 TestGcmNegotiationUsesCipherSuite(local_gcm_enabled, remote_gcm_enabled,
3021 expected_cipher_suite);
3022}
3023
3024// Test that GCM isn't used if only the offerer supports it.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003025TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07003026 NonGcmCipherUsedWhenOnlyCallerSupportsGcm) {
3027 bool local_gcm_enabled = true;
3028 bool remote_gcm_enabled = false;
3029 int expected_cipher_suite = kDefaultSrtpCryptoSuite;
3030 TestGcmNegotiationUsesCipherSuite(local_gcm_enabled, remote_gcm_enabled,
3031 expected_cipher_suite);
3032}
3033
3034// Test that GCM isn't used if only the answerer supports it.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003035TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07003036 NonGcmCipherUsedWhenOnlyCalleeSupportsGcm) {
3037 bool local_gcm_enabled = false;
3038 bool remote_gcm_enabled = true;
3039 int expected_cipher_suite = kDefaultSrtpCryptoSuite;
3040 TestGcmNegotiationUsesCipherSuite(local_gcm_enabled, remote_gcm_enabled,
3041 expected_cipher_suite);
3042}
3043
deadbeef7914b8c2017-04-21 03:23:33 -07003044// Verify that media can be transmitted end-to-end when GCM crypto suites are
3045// enabled. Note that the above tests, such as GcmCipherUsedWhenGcmSupported,
3046// only verify that a GCM cipher is negotiated, and not necessarily that SRTP
3047// works with it.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003048TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithGcmCipher) {
deadbeef7914b8c2017-04-21 03:23:33 -07003049 PeerConnectionFactory::Options gcm_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07003050 gcm_options.crypto_options.srtp.enable_gcm_crypto_suites = true;
deadbeef7914b8c2017-04-21 03:23:33 -07003051 ASSERT_TRUE(
3052 CreatePeerConnectionWrappersWithOptions(gcm_options, gcm_options));
3053 ConnectFakeSignaling();
3054 // Do normal offer/answer and wait for some frames to be received in each
3055 // direction.
Steve Anton15324772018-01-16 10:26:49 -08003056 caller()->AddAudioVideoTracks();
3057 callee()->AddAudioVideoTracks();
deadbeef7914b8c2017-04-21 03:23:33 -07003058 caller()->CreateAndSetAndSignalOffer();
3059 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08003060 MediaExpectations media_expectations;
3061 media_expectations.ExpectBidirectionalAudioAndVideo();
3062 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef7914b8c2017-04-21 03:23:33 -07003063}
3064
deadbeef1dcb1642017-03-29 21:08:16 -07003065// This test sets up a call between two parties with audio, video and an RTP
3066// data channel.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003067TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithRtpDataChannel) {
Niels Möllerf06f9232018-08-07 12:32:18 +02003068 PeerConnectionInterface::RTCConfiguration rtc_config;
3069 rtc_config.enable_rtp_data_channel = true;
3070 rtc_config.enable_dtls_srtp = false;
3071 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(rtc_config, rtc_config));
deadbeef1dcb1642017-03-29 21:08:16 -07003072 ConnectFakeSignaling();
3073 // Expect that data channel created on caller side will show up for callee as
3074 // well.
3075 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08003076 caller()->AddAudioVideoTracks();
3077 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003078 caller()->CreateAndSetAndSignalOffer();
3079 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3080 // Ensure the existence of the RTP data channel didn't impede audio/video.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003081 MediaExpectations media_expectations;
3082 media_expectations.ExpectBidirectionalAudioAndVideo();
3083 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003084 ASSERT_NE(nullptr, caller()->data_channel());
3085 ASSERT_NE(nullptr, callee()->data_channel());
3086 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3087 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3088
3089 // Ensure data can be sent in both directions.
3090 std::string data = "hello world";
3091 SendRtpDataWithRetries(caller()->data_channel(), data, 5);
3092 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3093 kDefaultTimeout);
3094 SendRtpDataWithRetries(callee()->data_channel(), data, 5);
3095 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3096 kDefaultTimeout);
3097}
3098
3099// Ensure that an RTP data channel is signaled as closed for the caller when
3100// the callee rejects it in a subsequent offer.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003101TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07003102 RtpDataChannelSignaledClosedInCalleeOffer) {
3103 // Same procedure as above test.
Niels Möllerf06f9232018-08-07 12:32:18 +02003104 PeerConnectionInterface::RTCConfiguration rtc_config;
3105 rtc_config.enable_rtp_data_channel = true;
3106 rtc_config.enable_dtls_srtp = false;
3107 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(rtc_config, rtc_config));
deadbeef1dcb1642017-03-29 21:08:16 -07003108 ConnectFakeSignaling();
3109 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08003110 caller()->AddAudioVideoTracks();
3111 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003112 caller()->CreateAndSetAndSignalOffer();
3113 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3114 ASSERT_NE(nullptr, caller()->data_channel());
3115 ASSERT_NE(nullptr, callee()->data_channel());
3116 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3117 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3118
3119 // Close the data channel on the callee, and do an updated offer/answer.
3120 callee()->data_channel()->Close();
3121 callee()->CreateAndSetAndSignalOffer();
3122 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3123 EXPECT_FALSE(caller()->data_observer()->IsOpen());
3124 EXPECT_FALSE(callee()->data_observer()->IsOpen());
3125}
3126
3127// Tests that data is buffered in an RTP data channel until an observer is
3128// registered for it.
3129//
3130// NOTE: RTP data channels can receive data before the underlying
3131// transport has detected that a channel is writable and thus data can be
3132// received before the data channel state changes to open. That is hard to test
3133// but the same buffering is expected to be used in that case.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003134TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07003135 DataBufferedUntilRtpDataChannelObserverRegistered) {
3136 // Use fake clock and simulated network delay so that we predictably can wait
3137 // until an SCTP message has been delivered without "sleep()"ing.
3138 rtc::ScopedFakeClock fake_clock;
3139 // Some things use a time of "0" as a special value, so we need to start out
3140 // the fake clock at a nonzero time.
3141 // TODO(deadbeef): Fix this.
Sebastian Jansson5f83cf02018-05-08 14:52:22 +02003142 fake_clock.AdvanceTime(webrtc::TimeDelta::seconds(1));
deadbeef1dcb1642017-03-29 21:08:16 -07003143 virtual_socket_server()->set_delay_mean(5); // 5 ms per hop.
3144 virtual_socket_server()->UpdateDelayDistribution();
3145
Niels Möllerf06f9232018-08-07 12:32:18 +02003146 PeerConnectionInterface::RTCConfiguration rtc_config;
3147 rtc_config.enable_rtp_data_channel = true;
3148 rtc_config.enable_dtls_srtp = false;
3149 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(rtc_config, rtc_config));
deadbeef1dcb1642017-03-29 21:08:16 -07003150 ConnectFakeSignaling();
3151 caller()->CreateDataChannel();
3152 caller()->CreateAndSetAndSignalOffer();
3153 ASSERT_TRUE(caller()->data_channel() != nullptr);
3154 ASSERT_TRUE_SIMULATED_WAIT(callee()->data_channel() != nullptr,
3155 kDefaultTimeout, fake_clock);
3156 ASSERT_TRUE_SIMULATED_WAIT(caller()->data_observer()->IsOpen(),
3157 kDefaultTimeout, fake_clock);
3158 ASSERT_EQ_SIMULATED_WAIT(DataChannelInterface::kOpen,
3159 callee()->data_channel()->state(), kDefaultTimeout,
3160 fake_clock);
3161
3162 // Unregister the observer which is normally automatically registered.
3163 callee()->data_channel()->UnregisterObserver();
3164 // Send data and advance fake clock until it should have been received.
3165 std::string data = "hello world";
3166 caller()->data_channel()->Send(DataBuffer(data));
3167 SIMULATED_WAIT(false, 50, fake_clock);
3168
3169 // Attach data channel and expect data to be received immediately. Note that
3170 // EXPECT_EQ_WAIT is used, such that the simulated clock is not advanced any
3171 // further, but data can be received even if the callback is asynchronous.
3172 MockDataChannelObserver new_observer(callee()->data_channel());
3173 EXPECT_EQ_SIMULATED_WAIT(data, new_observer.last_message(), kDefaultTimeout,
3174 fake_clock);
Seth Hampson1d4a76d2018-06-19 14:31:41 -07003175 // Closing the PeerConnections destroys the ports before the ScopedFakeClock.
3176 // If this is not done a DCHECK can be hit in ports.cc, because a large
3177 // negative number is calculated for the rtt due to the global clock changing.
3178 caller()->pc()->Close();
3179 callee()->pc()->Close();
deadbeef1dcb1642017-03-29 21:08:16 -07003180}
3181
3182// This test sets up a call between two parties with audio, video and but only
3183// the caller client supports RTP data channels.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003184TEST_P(PeerConnectionIntegrationTest, RtpDataChannelsRejectedByCallee) {
Niels Möllerf06f9232018-08-07 12:32:18 +02003185 PeerConnectionInterface::RTCConfiguration rtc_config_1;
3186 rtc_config_1.enable_rtp_data_channel = true;
deadbeef1dcb1642017-03-29 21:08:16 -07003187 // Must disable DTLS to make negotiation succeed.
Niels Möllerf06f9232018-08-07 12:32:18 +02003188 rtc_config_1.enable_dtls_srtp = false;
3189 PeerConnectionInterface::RTCConfiguration rtc_config_2;
3190 rtc_config_2.enable_dtls_srtp = false;
3191 rtc_config_2.enable_dtls_srtp = false;
3192 ASSERT_TRUE(
3193 CreatePeerConnectionWrappersWithConfig(rtc_config_1, rtc_config_2));
deadbeef1dcb1642017-03-29 21:08:16 -07003194 ConnectFakeSignaling();
3195 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08003196 caller()->AddAudioVideoTracks();
3197 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003198 caller()->CreateAndSetAndSignalOffer();
3199 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3200 // The caller should still have a data channel, but it should be closed, and
3201 // one should ever have been created for the callee.
3202 EXPECT_TRUE(caller()->data_channel() != nullptr);
3203 EXPECT_FALSE(caller()->data_observer()->IsOpen());
3204 EXPECT_EQ(nullptr, callee()->data_channel());
3205}
3206
3207// This test sets up a call between two parties with audio, and video. When
3208// audio and video is setup and flowing, an RTP data channel is negotiated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003209TEST_P(PeerConnectionIntegrationTest, AddRtpDataChannelInSubsequentOffer) {
Niels Möllerf06f9232018-08-07 12:32:18 +02003210 PeerConnectionInterface::RTCConfiguration rtc_config;
3211 rtc_config.enable_rtp_data_channel = true;
3212 rtc_config.enable_dtls_srtp = false;
3213 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(rtc_config, rtc_config));
deadbeef1dcb1642017-03-29 21:08:16 -07003214 ConnectFakeSignaling();
3215 // Do initial offer/answer with audio/video.
Steve Anton15324772018-01-16 10:26:49 -08003216 caller()->AddAudioVideoTracks();
3217 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003218 caller()->CreateAndSetAndSignalOffer();
3219 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3220 // Create data channel and do new offer and answer.
3221 caller()->CreateDataChannel();
3222 caller()->CreateAndSetAndSignalOffer();
3223 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3224 ASSERT_NE(nullptr, caller()->data_channel());
3225 ASSERT_NE(nullptr, callee()->data_channel());
3226 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3227 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3228 // Ensure data can be sent in both directions.
3229 std::string data = "hello world";
3230 SendRtpDataWithRetries(caller()->data_channel(), data, 5);
3231 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3232 kDefaultTimeout);
3233 SendRtpDataWithRetries(callee()->data_channel(), data, 5);
3234 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3235 kDefaultTimeout);
3236}
3237
3238#ifdef HAVE_SCTP
3239
3240// This test sets up a call between two parties with audio, video and an SCTP
3241// data channel.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003242TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithSctpDataChannel) {
deadbeef1dcb1642017-03-29 21:08:16 -07003243 ASSERT_TRUE(CreatePeerConnectionWrappers());
3244 ConnectFakeSignaling();
3245 // Expect that data channel created on caller side will show up for callee as
3246 // well.
3247 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08003248 caller()->AddAudioVideoTracks();
3249 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003250 caller()->CreateAndSetAndSignalOffer();
3251 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3252 // Ensure the existence of the SCTP data channel didn't impede audio/video.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003253 MediaExpectations media_expectations;
3254 media_expectations.ExpectBidirectionalAudioAndVideo();
3255 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003256 // Caller data channel should already exist (it created one). Callee data
3257 // channel may not exist yet, since negotiation happens in-band, not in SDP.
3258 ASSERT_NE(nullptr, caller()->data_channel());
3259 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3260 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3261 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3262
3263 // Ensure data can be sent in both directions.
3264 std::string data = "hello world";
3265 caller()->data_channel()->Send(DataBuffer(data));
3266 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3267 kDefaultTimeout);
3268 callee()->data_channel()->Send(DataBuffer(data));
3269 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3270 kDefaultTimeout);
3271}
3272
3273// Ensure that when the callee closes an SCTP data channel, the closing
3274// procedure results in the data channel being closed for the caller as well.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003275TEST_P(PeerConnectionIntegrationTest, CalleeClosesSctpDataChannel) {
deadbeef1dcb1642017-03-29 21:08:16 -07003276 // Same procedure as above test.
3277 ASSERT_TRUE(CreatePeerConnectionWrappers());
3278 ConnectFakeSignaling();
3279 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08003280 caller()->AddAudioVideoTracks();
3281 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003282 caller()->CreateAndSetAndSignalOffer();
3283 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3284 ASSERT_NE(nullptr, caller()->data_channel());
3285 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3286 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3287 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3288
3289 // Close the data channel on the callee side, and wait for it to reach the
3290 // "closed" state on both sides.
3291 callee()->data_channel()->Close();
3292 EXPECT_TRUE_WAIT(!caller()->data_observer()->IsOpen(), kDefaultTimeout);
3293 EXPECT_TRUE_WAIT(!callee()->data_observer()->IsOpen(), kDefaultTimeout);
3294}
3295
Seth Hampson2f0d7022018-02-20 11:54:42 -08003296TEST_P(PeerConnectionIntegrationTest, SctpDataChannelConfigSentToOtherSide) {
Steve Antonda6c0952017-10-23 11:41:54 -07003297 ASSERT_TRUE(CreatePeerConnectionWrappers());
3298 ConnectFakeSignaling();
3299 webrtc::DataChannelInit init;
3300 init.id = 53;
3301 init.maxRetransmits = 52;
3302 caller()->CreateDataChannel("data-channel", &init);
Steve Anton15324772018-01-16 10:26:49 -08003303 caller()->AddAudioVideoTracks();
3304 callee()->AddAudioVideoTracks();
Steve Antonda6c0952017-10-23 11:41:54 -07003305 caller()->CreateAndSetAndSignalOffer();
3306 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Steve Anton074dece2017-10-24 13:04:12 -07003307 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3308 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
Steve Antonda6c0952017-10-23 11:41:54 -07003309 EXPECT_EQ(init.id, callee()->data_channel()->id());
3310 EXPECT_EQ("data-channel", callee()->data_channel()->label());
3311 EXPECT_EQ(init.maxRetransmits, callee()->data_channel()->maxRetransmits());
3312 EXPECT_FALSE(callee()->data_channel()->negotiated());
3313}
3314
deadbeef1dcb1642017-03-29 21:08:16 -07003315// Test usrsctp's ability to process unordered data stream, where data actually
3316// arrives out of order using simulated delays. Previously there have been some
3317// bugs in this area.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003318TEST_P(PeerConnectionIntegrationTest, StressTestUnorderedSctpDataChannel) {
deadbeef1dcb1642017-03-29 21:08:16 -07003319 // Introduce random network delays.
3320 // Otherwise it's not a true "unordered" test.
3321 virtual_socket_server()->set_delay_mean(20);
3322 virtual_socket_server()->set_delay_stddev(5);
3323 virtual_socket_server()->UpdateDelayDistribution();
3324 // Normal procedure, but with unordered data channel config.
3325 ASSERT_TRUE(CreatePeerConnectionWrappers());
3326 ConnectFakeSignaling();
3327 webrtc::DataChannelInit init;
3328 init.ordered = false;
3329 caller()->CreateDataChannel(&init);
3330 caller()->CreateAndSetAndSignalOffer();
3331 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3332 ASSERT_NE(nullptr, caller()->data_channel());
3333 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3334 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3335 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3336
3337 static constexpr int kNumMessages = 100;
3338 // Deliberately chosen to be larger than the MTU so messages get fragmented.
3339 static constexpr size_t kMaxMessageSize = 4096;
3340 // Create and send random messages.
3341 std::vector<std::string> sent_messages;
3342 for (int i = 0; i < kNumMessages; ++i) {
3343 size_t length =
3344 (rand() % kMaxMessageSize) + 1; // NOLINT (rand_r instead of rand)
3345 std::string message;
3346 ASSERT_TRUE(rtc::CreateRandomString(length, &message));
3347 caller()->data_channel()->Send(DataBuffer(message));
3348 callee()->data_channel()->Send(DataBuffer(message));
3349 sent_messages.push_back(message);
3350 }
3351
3352 // Wait for all messages to be received.
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02003353 EXPECT_EQ_WAIT(rtc::checked_cast<size_t>(kNumMessages),
deadbeef1dcb1642017-03-29 21:08:16 -07003354 caller()->data_observer()->received_message_count(),
3355 kDefaultTimeout);
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02003356 EXPECT_EQ_WAIT(rtc::checked_cast<size_t>(kNumMessages),
deadbeef1dcb1642017-03-29 21:08:16 -07003357 callee()->data_observer()->received_message_count(),
3358 kDefaultTimeout);
3359
3360 // Sort and compare to make sure none of the messages were corrupted.
3361 std::vector<std::string> caller_received_messages =
3362 caller()->data_observer()->messages();
3363 std::vector<std::string> callee_received_messages =
3364 callee()->data_observer()->messages();
Steve Anton64b626b2019-01-28 17:25:26 -08003365 absl::c_sort(sent_messages);
3366 absl::c_sort(caller_received_messages);
3367 absl::c_sort(callee_received_messages);
deadbeef1dcb1642017-03-29 21:08:16 -07003368 EXPECT_EQ(sent_messages, caller_received_messages);
3369 EXPECT_EQ(sent_messages, callee_received_messages);
3370}
3371
3372// This test sets up a call between two parties with audio, and video. When
3373// audio and video are setup and flowing, an SCTP data channel is negotiated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003374TEST_P(PeerConnectionIntegrationTest, AddSctpDataChannelInSubsequentOffer) {
deadbeef1dcb1642017-03-29 21:08:16 -07003375 ASSERT_TRUE(CreatePeerConnectionWrappers());
3376 ConnectFakeSignaling();
3377 // Do initial offer/answer with audio/video.
Steve Anton15324772018-01-16 10:26:49 -08003378 caller()->AddAudioVideoTracks();
3379 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003380 caller()->CreateAndSetAndSignalOffer();
3381 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3382 // Create data channel and do new offer and answer.
3383 caller()->CreateDataChannel();
3384 caller()->CreateAndSetAndSignalOffer();
3385 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3386 // Caller data channel should already exist (it created one). Callee data
3387 // channel may not exist yet, since negotiation happens in-band, not in SDP.
3388 ASSERT_NE(nullptr, caller()->data_channel());
3389 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3390 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3391 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3392 // Ensure data can be sent in both directions.
3393 std::string data = "hello world";
3394 caller()->data_channel()->Send(DataBuffer(data));
3395 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3396 kDefaultTimeout);
3397 callee()->data_channel()->Send(DataBuffer(data));
3398 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3399 kDefaultTimeout);
3400}
3401
deadbeef7914b8c2017-04-21 03:23:33 -07003402// Set up a connection initially just using SCTP data channels, later upgrading
3403// to audio/video, ensuring frames are received end-to-end. Effectively the
3404// inverse of the test above.
3405// This was broken in M57; see https://crbug.com/711243
Seth Hampson2f0d7022018-02-20 11:54:42 -08003406TEST_P(PeerConnectionIntegrationTest, SctpDataChannelToAudioVideoUpgrade) {
deadbeef7914b8c2017-04-21 03:23:33 -07003407 ASSERT_TRUE(CreatePeerConnectionWrappers());
3408 ConnectFakeSignaling();
3409 // Do initial offer/answer with just data channel.
3410 caller()->CreateDataChannel();
3411 caller()->CreateAndSetAndSignalOffer();
3412 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3413 // Wait until data can be sent over the data channel.
3414 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3415 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3416 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3417
3418 // Do subsequent offer/answer with two-way audio and video. Audio and video
3419 // should end up bundled on the DTLS/ICE transport already used for data.
Steve Anton15324772018-01-16 10:26:49 -08003420 caller()->AddAudioVideoTracks();
3421 callee()->AddAudioVideoTracks();
deadbeef7914b8c2017-04-21 03:23:33 -07003422 caller()->CreateAndSetAndSignalOffer();
3423 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08003424 MediaExpectations media_expectations;
3425 media_expectations.ExpectBidirectionalAudioAndVideo();
3426 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef7914b8c2017-04-21 03:23:33 -07003427}
3428
deadbeef8b7e9ad2017-05-25 09:38:55 -07003429static void MakeSpecCompliantSctpOffer(cricket::SessionDescription* desc) {
deadbeef8b7e9ad2017-05-25 09:38:55 -07003430 cricket::DataContentDescription* dcd_offer =
Steve Antonb1c1de12017-12-21 15:14:30 -08003431 GetFirstDataContentDescription(desc);
3432 ASSERT_TRUE(dcd_offer);
deadbeef8b7e9ad2017-05-25 09:38:55 -07003433 dcd_offer->set_use_sctpmap(false);
3434 dcd_offer->set_protocol("UDP/DTLS/SCTP");
3435}
3436
3437// Test that the data channel works when a spec-compliant SCTP m= section is
3438// offered (using "a=sctp-port" instead of "a=sctpmap", and using
3439// "UDP/DTLS/SCTP" as the protocol).
Seth Hampson2f0d7022018-02-20 11:54:42 -08003440TEST_P(PeerConnectionIntegrationTest,
deadbeef8b7e9ad2017-05-25 09:38:55 -07003441 DataChannelWorksWhenSpecCompliantSctpOfferReceived) {
3442 ASSERT_TRUE(CreatePeerConnectionWrappers());
3443 ConnectFakeSignaling();
3444 caller()->CreateDataChannel();
3445 caller()->SetGeneratedSdpMunger(MakeSpecCompliantSctpOffer);
3446 caller()->CreateAndSetAndSignalOffer();
3447 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3448 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3449 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3450 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3451
3452 // Ensure data can be sent in both directions.
3453 std::string data = "hello world";
3454 caller()->data_channel()->Send(DataBuffer(data));
3455 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3456 kDefaultTimeout);
3457 callee()->data_channel()->Send(DataBuffer(data));
3458 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3459 kDefaultTimeout);
3460}
3461
deadbeef1dcb1642017-03-29 21:08:16 -07003462#endif // HAVE_SCTP
3463
Bjorn Mellema2eb0a72018-11-09 10:13:51 -08003464// This test sets up a call between two parties with a media transport data
3465// channel.
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08003466TEST_P(PeerConnectionIntegrationTest, MediaTransportDataChannelEndToEnd) {
3467 PeerConnectionInterface::RTCConfiguration rtc_config;
Piotr (Peter) Slatalab1ae10b2019-03-01 11:14:05 -08003468 rtc_config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyRequire;
3469 rtc_config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08003470 rtc_config.use_media_transport_for_data_channels = true;
3471 rtc_config.enable_dtls_srtp = false; // SDES is required for media transport.
3472 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
3473 rtc_config, rtc_config, loopback_media_transports()->first_factory(),
3474 loopback_media_transports()->second_factory()));
3475 ConnectFakeSignaling();
3476
3477 // Expect that data channel created on caller side will show up for callee as
3478 // well.
3479 caller()->CreateDataChannel();
3480 caller()->CreateAndSetAndSignalOffer();
3481 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3482
3483 // Ensure that the media transport is ready.
3484 loopback_media_transports()->SetState(webrtc::MediaTransportState::kWritable);
3485 loopback_media_transports()->FlushAsyncInvokes();
3486
3487 // Caller data channel should already exist (it created one). Callee data
3488 // channel may not exist yet, since negotiation happens in-band, not in SDP.
3489 ASSERT_NE(nullptr, caller()->data_channel());
3490 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3491 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3492 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3493
3494 // Ensure data can be sent in both directions.
3495 std::string data = "hello world";
3496 caller()->data_channel()->Send(DataBuffer(data));
3497 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3498 kDefaultTimeout);
3499 callee()->data_channel()->Send(DataBuffer(data));
3500 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3501 kDefaultTimeout);
3502}
3503
3504// Ensure that when the callee closes a media transport data channel, the
3505// closing procedure results in the data channel being closed for the caller
3506// as well.
3507TEST_P(PeerConnectionIntegrationTest, MediaTransportDataChannelCalleeCloses) {
3508 PeerConnectionInterface::RTCConfiguration rtc_config;
3509 rtc_config.use_media_transport_for_data_channels = true;
3510 rtc_config.enable_dtls_srtp = false; // SDES is required for media transport.
3511 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
3512 rtc_config, rtc_config, loopback_media_transports()->first_factory(),
3513 loopback_media_transports()->second_factory()));
3514 ConnectFakeSignaling();
3515
3516 // Create a data channel on the caller and signal it to the callee.
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 // Data channels exist and open on both ends of the connection.
3526 ASSERT_NE(nullptr, caller()->data_channel());
3527 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3528 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3529 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3530
3531 // Close the data channel on the callee side, and wait for it to reach the
3532 // "closed" state on both sides.
3533 callee()->data_channel()->Close();
3534 EXPECT_TRUE_WAIT(!caller()->data_observer()->IsOpen(), kDefaultTimeout);
3535 EXPECT_TRUE_WAIT(!callee()->data_observer()->IsOpen(), kDefaultTimeout);
3536}
3537
3538TEST_P(PeerConnectionIntegrationTest,
3539 MediaTransportDataChannelConfigSentToOtherSide) {
3540 PeerConnectionInterface::RTCConfiguration rtc_config;
3541 rtc_config.use_media_transport_for_data_channels = true;
3542 rtc_config.enable_dtls_srtp = false; // SDES is required for media transport.
3543 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
3544 rtc_config, rtc_config, loopback_media_transports()->first_factory(),
3545 loopback_media_transports()->second_factory()));
3546 ConnectFakeSignaling();
3547
3548 // Create a data channel with a non-default configuration and signal it to the
3549 // callee.
3550 webrtc::DataChannelInit init;
3551 init.id = 53;
3552 init.maxRetransmits = 52;
3553 caller()->CreateDataChannel("data-channel", &init);
3554 caller()->CreateAndSetAndSignalOffer();
3555 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3556
3557 // Ensure that the media transport is ready.
3558 loopback_media_transports()->SetState(webrtc::MediaTransportState::kWritable);
3559 loopback_media_transports()->FlushAsyncInvokes();
3560
3561 // Ensure that the data channel exists on the callee with the correct
3562 // configuration.
3563 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3564 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3565 EXPECT_EQ(init.id, callee()->data_channel()->id());
3566 EXPECT_EQ("data-channel", callee()->data_channel()->label());
3567 EXPECT_EQ(init.maxRetransmits, callee()->data_channel()->maxRetransmits());
3568 EXPECT_FALSE(callee()->data_channel()->negotiated());
3569}
3570
Piotr (Peter) Slatalab1ae10b2019-03-01 11:14:05 -08003571TEST_P(PeerConnectionIntegrationTest, MediaTransportOfferUpgrade) {
3572 PeerConnectionInterface::RTCConfiguration rtc_config;
3573 rtc_config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyRequire;
3574 rtc_config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
3575 rtc_config.use_media_transport = true;
3576 rtc_config.enable_dtls_srtp = false; // SDES is required for media transport.
3577 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
3578 rtc_config, rtc_config, loopback_media_transports()->first_factory(),
3579 loopback_media_transports()->second_factory()));
3580 ConnectFakeSignaling();
3581
3582 // Do initial offer/answer with just a video track.
3583 caller()->AddVideoTrack();
3584 callee()->AddVideoTrack();
3585 caller()->CreateAndSetAndSignalOffer();
3586 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3587
3588 // Ensure that the media transport is ready.
3589 loopback_media_transports()->SetState(webrtc::MediaTransportState::kWritable);
3590 loopback_media_transports()->FlushAsyncInvokes();
3591
3592 // Now add an audio track and do another offer/answer.
3593 caller()->AddAudioTrack();
3594 callee()->AddAudioTrack();
3595 caller()->CreateAndSetAndSignalOffer();
3596 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3597
3598 // Ensure both audio and video frames are received end-to-end.
3599 MediaExpectations media_expectations;
3600 media_expectations.ExpectBidirectionalAudioAndVideo();
3601 ASSERT_TRUE(ExpectNewFrames(media_expectations));
3602
3603 // The second offer should not have generated another media transport.
3604 // Media transport was kept alive, and was not recreated.
3605 EXPECT_EQ(1, loopback_media_transports()->first_factory_transport_count());
3606 EXPECT_EQ(1, loopback_media_transports()->second_factory_transport_count());
3607}
3608
3609TEST_P(PeerConnectionIntegrationTest, MediaTransportOfferUpgradeOnTheCallee) {
3610 PeerConnectionInterface::RTCConfiguration rtc_config;
3611 rtc_config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyRequire;
3612 rtc_config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
3613 rtc_config.use_media_transport = true;
3614 rtc_config.enable_dtls_srtp = false; // SDES is required for media transport.
3615 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
3616 rtc_config, rtc_config, loopback_media_transports()->first_factory(),
3617 loopback_media_transports()->second_factory()));
3618 ConnectFakeSignaling();
3619
3620 // Do initial offer/answer with just a video track.
3621 caller()->AddVideoTrack();
3622 callee()->AddVideoTrack();
3623 caller()->CreateAndSetAndSignalOffer();
3624 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3625
3626 // Ensure that the media transport is ready.
3627 loopback_media_transports()->SetState(webrtc::MediaTransportState::kWritable);
3628 loopback_media_transports()->FlushAsyncInvokes();
3629
3630 // Now add an audio track and do another offer/answer.
3631 caller()->AddAudioTrack();
3632 callee()->AddAudioTrack();
3633 callee()->CreateAndSetAndSignalOffer();
3634 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3635
3636 // Ensure both audio and video frames are received end-to-end.
3637 MediaExpectations media_expectations;
3638 media_expectations.ExpectBidirectionalAudioAndVideo();
3639 ASSERT_TRUE(ExpectNewFrames(media_expectations));
3640
3641 // The second offer should not have generated another media transport.
3642 // Media transport was kept alive, and was not recreated.
3643 EXPECT_EQ(1, loopback_media_transports()->first_factory_transport_count());
3644 EXPECT_EQ(1, loopback_media_transports()->second_factory_transport_count());
3645}
3646
Niels Möllerc68d2822018-11-20 14:52:05 +01003647TEST_P(PeerConnectionIntegrationTest, MediaTransportBidirectionalAudio) {
3648 PeerConnectionInterface::RTCConfiguration rtc_config;
Piotr (Peter) Slatalab1ae10b2019-03-01 11:14:05 -08003649 rtc_config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyRequire;
3650 rtc_config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
Niels Möllerc68d2822018-11-20 14:52:05 +01003651 rtc_config.use_media_transport = true;
3652 rtc_config.enable_dtls_srtp = false; // SDES is required for media transport.
3653 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
3654 rtc_config, rtc_config, loopback_media_transports()->first_factory(),
3655 loopback_media_transports()->second_factory()));
3656 ConnectFakeSignaling();
3657
3658 caller()->AddAudioTrack();
3659 callee()->AddAudioTrack();
3660 // Start offer/answer exchange and wait for it to complete.
3661 caller()->CreateAndSetAndSignalOffer();
3662 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3663
3664 // Ensure that the media transport is ready.
3665 loopback_media_transports()->SetState(webrtc::MediaTransportState::kWritable);
3666 loopback_media_transports()->FlushAsyncInvokes();
3667
3668 MediaExpectations media_expectations;
3669 media_expectations.ExpectBidirectionalAudio();
3670 ASSERT_TRUE(ExpectNewFrames(media_expectations));
3671
3672 webrtc::MediaTransportPair::Stats first_stats =
3673 loopback_media_transports()->FirstStats();
3674 webrtc::MediaTransportPair::Stats second_stats =
3675 loopback_media_transports()->SecondStats();
3676
3677 EXPECT_GT(first_stats.received_audio_frames, 0);
3678 EXPECT_GE(second_stats.sent_audio_frames, first_stats.received_audio_frames);
3679
3680 EXPECT_GT(second_stats.received_audio_frames, 0);
3681 EXPECT_GE(first_stats.sent_audio_frames, second_stats.received_audio_frames);
3682}
3683
Niels Möller46879152019-01-07 15:54:47 +01003684TEST_P(PeerConnectionIntegrationTest, MediaTransportBidirectionalVideo) {
3685 PeerConnectionInterface::RTCConfiguration rtc_config;
3686 rtc_config.use_media_transport = true;
3687 rtc_config.enable_dtls_srtp = false; // SDES is required for media transport.
3688 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
3689 rtc_config, rtc_config, loopback_media_transports()->first_factory(),
3690 loopback_media_transports()->second_factory()));
3691 ConnectFakeSignaling();
3692
3693 caller()->AddVideoTrack();
3694 callee()->AddVideoTrack();
3695 // Start offer/answer exchange and wait for it to complete.
3696 caller()->CreateAndSetAndSignalOffer();
3697 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3698
3699 // Ensure that the media transport is ready.
3700 loopback_media_transports()->SetState(webrtc::MediaTransportState::kWritable);
3701 loopback_media_transports()->FlushAsyncInvokes();
3702
3703 MediaExpectations media_expectations;
3704 media_expectations.ExpectBidirectionalVideo();
3705 ASSERT_TRUE(ExpectNewFrames(media_expectations));
3706
3707 webrtc::MediaTransportPair::Stats first_stats =
3708 loopback_media_transports()->FirstStats();
3709 webrtc::MediaTransportPair::Stats second_stats =
3710 loopback_media_transports()->SecondStats();
3711
3712 EXPECT_GT(first_stats.received_video_frames, 0);
3713 EXPECT_GE(second_stats.sent_video_frames, first_stats.received_video_frames);
3714
3715 EXPECT_GT(second_stats.received_video_frames, 0);
3716 EXPECT_GE(first_stats.sent_video_frames, second_stats.received_video_frames);
3717}
3718
Piotr (Peter) Slatala55b91b92019-01-25 13:31:15 -08003719TEST_P(PeerConnectionIntegrationTest,
3720 MediaTransportDataChannelUsesRtpBidirectionalVideo) {
3721 PeerConnectionInterface::RTCConfiguration rtc_config;
3722 rtc_config.use_media_transport = false;
3723 rtc_config.use_media_transport_for_data_channels = true;
3724 rtc_config.enable_dtls_srtp = false; // SDES is required for media transport.
3725 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
3726 rtc_config, rtc_config, loopback_media_transports()->first_factory(),
3727 loopback_media_transports()->second_factory()));
3728 ConnectFakeSignaling();
3729
3730 caller()->AddVideoTrack();
3731 callee()->AddVideoTrack();
3732 // Start offer/answer exchange and wait for it to complete.
3733 caller()->CreateAndSetAndSignalOffer();
3734 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3735
3736 MediaExpectations media_expectations;
3737 media_expectations.ExpectBidirectionalVideo();
3738 ASSERT_TRUE(ExpectNewFrames(media_expectations));
3739}
3740
deadbeef1dcb1642017-03-29 21:08:16 -07003741// Test that the ICE connection and gathering states eventually reach
3742// "complete".
Seth Hampson2f0d7022018-02-20 11:54:42 -08003743TEST_P(PeerConnectionIntegrationTest, IceStatesReachCompletion) {
deadbeef1dcb1642017-03-29 21:08:16 -07003744 ASSERT_TRUE(CreatePeerConnectionWrappers());
3745 ConnectFakeSignaling();
3746 // Do normal offer/answer.
Steve Anton15324772018-01-16 10:26:49 -08003747 caller()->AddAudioVideoTracks();
3748 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003749 caller()->CreateAndSetAndSignalOffer();
3750 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3751 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceGatheringComplete,
3752 caller()->ice_gathering_state(), kMaxWaitForFramesMs);
3753 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceGatheringComplete,
3754 callee()->ice_gathering_state(), kMaxWaitForFramesMs);
3755 // After the best candidate pair is selected and all candidates are signaled,
3756 // the ICE connection state should reach "complete".
3757 // TODO(deadbeef): Currently, the ICE "controlled" agent (the
3758 // answerer/"callee" by default) only reaches "connected". When this is
3759 // fixed, this test should be updated.
3760 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
3761 caller()->ice_connection_state(), kDefaultTimeout);
Alex Loiko9289eda2018-11-23 16:18:59 +00003762 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
3763 callee()->ice_connection_state(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07003764}
3765
Qingsi Wang1dac6d82018-12-12 15:28:47 -08003766constexpr int kOnlyLocalPorts = cricket::PORTALLOCATOR_DISABLE_STUN |
3767 cricket::PORTALLOCATOR_DISABLE_RELAY |
3768 cricket::PORTALLOCATOR_DISABLE_TCP;
Zach Stein6fcdc2f2018-08-23 16:25:55 -07003769
Qingsi Wang1dac6d82018-12-12 15:28:47 -08003770// Use a mock resolver to resolve the hostname back to the original IP on both
3771// sides and check that the ICE connection connects.
Zach Stein6fcdc2f2018-08-23 16:25:55 -07003772TEST_P(PeerConnectionIntegrationTest,
3773 IceStatesReachCompletionWithRemoteHostname) {
Qingsi Wang1dac6d82018-12-12 15:28:47 -08003774 auto caller_resolver_factory =
3775 absl::make_unique<NiceMock<webrtc::MockAsyncResolverFactory>>();
3776 auto callee_resolver_factory =
3777 absl::make_unique<NiceMock<webrtc::MockAsyncResolverFactory>>();
3778 NiceMock<rtc::MockAsyncResolver> callee_async_resolver;
3779 NiceMock<rtc::MockAsyncResolver> caller_async_resolver;
Zach Stein6fcdc2f2018-08-23 16:25:55 -07003780
3781 // This also verifies that the injected AsyncResolverFactory is used by
3782 // P2PTransportChannel.
Qingsi Wang1dac6d82018-12-12 15:28:47 -08003783 EXPECT_CALL(*caller_resolver_factory, Create())
3784 .WillOnce(Return(&caller_async_resolver));
3785 webrtc::PeerConnectionDependencies caller_deps(nullptr);
3786 caller_deps.async_resolver_factory = std::move(caller_resolver_factory);
3787
3788 EXPECT_CALL(*callee_resolver_factory, Create())
3789 .WillOnce(Return(&callee_async_resolver));
3790 webrtc::PeerConnectionDependencies callee_deps(nullptr);
3791 callee_deps.async_resolver_factory = std::move(callee_resolver_factory);
3792
3793 PeerConnectionInterface::RTCConfiguration config;
3794 config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
3795 config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyRequire;
3796
3797 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndDeps(
3798 config, std::move(caller_deps), config, std::move(callee_deps)));
3799
3800 caller()->SetRemoteAsyncResolver(&callee_async_resolver);
3801 callee()->SetRemoteAsyncResolver(&caller_async_resolver);
3802
3803 // Enable hostname candidates with mDNS names.
3804 caller()->network()->CreateMdnsResponder(network_thread());
3805 callee()->network()->CreateMdnsResponder(network_thread());
3806
3807 SetPortAllocatorFlags(kOnlyLocalPorts, kOnlyLocalPorts);
Zach Stein6fcdc2f2018-08-23 16:25:55 -07003808
3809 ConnectFakeSignaling();
3810 caller()->AddAudioVideoTracks();
3811 callee()->AddAudioVideoTracks();
3812 caller()->CreateAndSetAndSignalOffer();
3813 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3814 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
3815 caller()->ice_connection_state(), kDefaultTimeout);
3816 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
3817 callee()->ice_connection_state(), kDefaultTimeout);
Jeroen de Borst833979f2018-12-13 08:25:54 -08003818
3819 EXPECT_EQ(1, webrtc::metrics::NumEvents(
3820 "WebRTC.PeerConnection.CandidatePairType_UDP",
3821 webrtc::kIceCandidatePairHostNameHostName));
Zach Stein6fcdc2f2018-08-23 16:25:55 -07003822}
3823
Steve Antonede9ca52017-10-16 13:04:27 -07003824// Test that firewalling the ICE connection causes the clients to identify the
3825// disconnected state and then removing the firewall causes them to reconnect.
3826class PeerConnectionIntegrationIceStatesTest
Seth Hampson2f0d7022018-02-20 11:54:42 -08003827 : public PeerConnectionIntegrationBaseTest,
3828 public ::testing::WithParamInterface<
3829 std::tuple<SdpSemantics, std::tuple<std::string, uint32_t>>> {
Steve Antonede9ca52017-10-16 13:04:27 -07003830 protected:
Seth Hampson2f0d7022018-02-20 11:54:42 -08003831 PeerConnectionIntegrationIceStatesTest()
3832 : PeerConnectionIntegrationBaseTest(std::get<0>(GetParam())) {
3833 port_allocator_flags_ = std::get<1>(std::get<1>(GetParam()));
Steve Antonede9ca52017-10-16 13:04:27 -07003834 }
3835
3836 void StartStunServer(const SocketAddress& server_address) {
3837 stun_server_.reset(
3838 cricket::TestStunServer::Create(network_thread(), server_address));
3839 }
3840
3841 bool TestIPv6() {
3842 return (port_allocator_flags_ & cricket::PORTALLOCATOR_ENABLE_IPV6);
3843 }
3844
3845 void SetPortAllocatorFlags() {
Qingsi Wang1dac6d82018-12-12 15:28:47 -08003846 PeerConnectionIntegrationBaseTest::SetPortAllocatorFlags(
3847 port_allocator_flags_, port_allocator_flags_);
Steve Antonede9ca52017-10-16 13:04:27 -07003848 }
3849
3850 std::vector<SocketAddress> CallerAddresses() {
3851 std::vector<SocketAddress> addresses;
3852 addresses.push_back(SocketAddress("1.1.1.1", 0));
3853 if (TestIPv6()) {
3854 addresses.push_back(SocketAddress("1111:0:a:b:c:d:e:f", 0));
3855 }
3856 return addresses;
3857 }
3858
3859 std::vector<SocketAddress> CalleeAddresses() {
3860 std::vector<SocketAddress> addresses;
3861 addresses.push_back(SocketAddress("2.2.2.2", 0));
3862 if (TestIPv6()) {
3863 addresses.push_back(SocketAddress("2222:0:a:b:c:d:e:f", 0));
3864 }
3865 return addresses;
3866 }
3867
3868 void SetUpNetworkInterfaces() {
3869 // Remove the default interfaces added by the test infrastructure.
3870 caller()->network()->RemoveInterface(kDefaultLocalAddress);
3871 callee()->network()->RemoveInterface(kDefaultLocalAddress);
3872
3873 // Add network addresses for test.
3874 for (const auto& caller_address : CallerAddresses()) {
3875 caller()->network()->AddInterface(caller_address);
3876 }
3877 for (const auto& callee_address : CalleeAddresses()) {
3878 callee()->network()->AddInterface(callee_address);
3879 }
3880 }
3881
3882 private:
3883 uint32_t port_allocator_flags_;
3884 std::unique_ptr<cricket::TestStunServer> stun_server_;
3885};
3886
3887// Tests that the PeerConnection goes through all the ICE gathering/connection
3888// states over the duration of the call. This includes Disconnected and Failed
3889// states, induced by putting a firewall between the peers and waiting for them
3890// to time out.
Steve Anton83119dd2017-11-10 16:19:52 -08003891TEST_P(PeerConnectionIntegrationIceStatesTest, VerifyIceStates) {
Jonas Olssonb75d9e92019-02-22 10:33:29 +01003892 rtc::ScopedFakeClock fake_clock;
3893 // Some things use a time of "0" as a special value, so we need to start out
3894 // the fake clock at a nonzero time.
3895 fake_clock.AdvanceTime(TimeDelta::seconds(1));
Steve Antonede9ca52017-10-16 13:04:27 -07003896
3897 const SocketAddress kStunServerAddress =
3898 SocketAddress("99.99.99.1", cricket::STUN_SERVER_PORT);
3899 StartStunServer(kStunServerAddress);
3900
3901 PeerConnectionInterface::RTCConfiguration config;
3902 PeerConnectionInterface::IceServer ice_stun_server;
3903 ice_stun_server.urls.push_back(
3904 "stun:" + kStunServerAddress.HostAsURIString() + ":" +
3905 kStunServerAddress.PortAsString());
3906 config.servers.push_back(ice_stun_server);
3907
3908 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
3909 ConnectFakeSignaling();
3910 SetPortAllocatorFlags();
3911 SetUpNetworkInterfaces();
Steve Anton15324772018-01-16 10:26:49 -08003912 caller()->AddAudioVideoTracks();
3913 callee()->AddAudioVideoTracks();
Steve Antonede9ca52017-10-16 13:04:27 -07003914
3915 // Initial state before anything happens.
3916 ASSERT_EQ(PeerConnectionInterface::kIceGatheringNew,
3917 caller()->ice_gathering_state());
3918 ASSERT_EQ(PeerConnectionInterface::kIceConnectionNew,
3919 caller()->ice_connection_state());
Jonas Olsson7a6739e2019-01-15 16:31:55 +01003920 ASSERT_EQ(PeerConnectionInterface::kIceConnectionNew,
3921 caller()->standardized_ice_connection_state());
Steve Antonede9ca52017-10-16 13:04:27 -07003922
3923 // Start the call by creating the offer, setting it as the local description,
3924 // then sending it to the peer who will respond with an answer. This happens
3925 // asynchronously so that we can watch the states as it runs in the
3926 // background.
3927 caller()->CreateAndSetAndSignalOffer();
3928
Jonas Olsson7a6739e2019-01-15 16:31:55 +01003929 ASSERT_EQ(PeerConnectionInterface::kIceConnectionCompleted,
3930 caller()->ice_connection_state());
Jonas Olssondf919fb2019-01-22 11:21:00 +01003931 ASSERT_EQ(PeerConnectionInterface::kIceConnectionConnected,
Jonas Olsson7a6739e2019-01-15 16:31:55 +01003932 caller()->standardized_ice_connection_state());
Steve Antonede9ca52017-10-16 13:04:27 -07003933
3934 // Verify that the observer was notified of the intermediate transitions.
3935 EXPECT_THAT(caller()->ice_connection_state_history(),
3936 ElementsAre(PeerConnectionInterface::kIceConnectionChecking,
3937 PeerConnectionInterface::kIceConnectionConnected,
3938 PeerConnectionInterface::kIceConnectionCompleted));
Jonas Olsson635474e2018-10-18 15:58:17 +02003939 EXPECT_THAT(
3940 caller()->peer_connection_state_history(),
3941 ElementsAre(PeerConnectionInterface::PeerConnectionState::kConnecting,
Jonas Olsson635474e2018-10-18 15:58:17 +02003942 PeerConnectionInterface::PeerConnectionState::kConnected));
Steve Antonede9ca52017-10-16 13:04:27 -07003943 EXPECT_THAT(caller()->ice_gathering_state_history(),
3944 ElementsAre(PeerConnectionInterface::kIceGatheringGathering,
3945 PeerConnectionInterface::kIceGatheringComplete));
3946
3947 // Block connections to/from the caller and wait for ICE to become
3948 // disconnected.
3949 for (const auto& caller_address : CallerAddresses()) {
3950 firewall()->AddRule(false, rtc::FP_ANY, rtc::FD_ANY, caller_address);
3951 }
Mirko Bonadei675513b2017-11-09 11:09:25 +01003952 RTC_LOG(LS_INFO) << "Firewall rules applied";
Jonas Olssonb75d9e92019-02-22 10:33:29 +01003953 ASSERT_EQ_SIMULATED_WAIT(PeerConnectionInterface::kIceConnectionDisconnected,
3954 caller()->ice_connection_state(), kDefaultTimeout,
3955 fake_clock);
3956 ASSERT_EQ_SIMULATED_WAIT(PeerConnectionInterface::kIceConnectionDisconnected,
3957 caller()->standardized_ice_connection_state(),
3958 kDefaultTimeout, fake_clock);
Steve Antonede9ca52017-10-16 13:04:27 -07003959
3960 // Let ICE re-establish by removing the firewall rules.
3961 firewall()->ClearRules();
Mirko Bonadei675513b2017-11-09 11:09:25 +01003962 RTC_LOG(LS_INFO) << "Firewall rules cleared";
Jonas Olssonb75d9e92019-02-22 10:33:29 +01003963 ASSERT_EQ_SIMULATED_WAIT(PeerConnectionInterface::kIceConnectionCompleted,
3964 caller()->ice_connection_state(), kDefaultTimeout,
3965 fake_clock);
3966 ASSERT_EQ_SIMULATED_WAIT(PeerConnectionInterface::kIceConnectionConnected,
3967 caller()->standardized_ice_connection_state(),
3968 kDefaultTimeout, fake_clock);
Steve Antonede9ca52017-10-16 13:04:27 -07003969
3970 // According to RFC7675, if there is no response within 30 seconds then the
3971 // peer should consider the other side to have rejected the connection. This
Steve Anton83119dd2017-11-10 16:19:52 -08003972 // is signaled by the state transitioning to "failed".
Steve Antonede9ca52017-10-16 13:04:27 -07003973 constexpr int kConsentTimeout = 30000;
3974 for (const auto& caller_address : CallerAddresses()) {
3975 firewall()->AddRule(false, rtc::FP_ANY, rtc::FD_ANY, caller_address);
3976 }
Mirko Bonadei675513b2017-11-09 11:09:25 +01003977 RTC_LOG(LS_INFO) << "Firewall rules applied again";
Jonas Olssonb75d9e92019-02-22 10:33:29 +01003978 ASSERT_EQ_SIMULATED_WAIT(PeerConnectionInterface::kIceConnectionFailed,
3979 caller()->ice_connection_state(), kConsentTimeout,
3980 fake_clock);
3981 ASSERT_EQ_SIMULATED_WAIT(PeerConnectionInterface::kIceConnectionFailed,
3982 caller()->standardized_ice_connection_state(),
3983 kConsentTimeout, fake_clock);
3984
3985 // We need to manually close the peerconnections before the fake clock goes
3986 // out of scope, or we trigger a DCHECK in rtp_sender.cc when we briefly
3987 // return to using non-faked time.
3988 delete SetCallerPcWrapperAndReturnCurrent(nullptr);
3989 delete SetCalleePcWrapperAndReturnCurrent(nullptr);
3990}
3991
3992// Tests that if the connection doesn't get set up properly we eventually reach
3993// the "failed" iceConnectionState.
3994TEST_P(PeerConnectionIntegrationIceStatesTest, IceStateSetupFailure) {
3995 rtc::ScopedFakeClock fake_clock;
3996 // Some things use a time of "0" as a special value, so we need to start out
3997 // the fake clock at a nonzero time.
3998 fake_clock.AdvanceTime(TimeDelta::seconds(1));
3999
4000 // Block connections to/from the caller and wait for ICE to become
4001 // disconnected.
4002 for (const auto& caller_address : CallerAddresses()) {
4003 firewall()->AddRule(false, rtc::FP_ANY, rtc::FD_ANY, caller_address);
4004 }
4005
4006 ASSERT_TRUE(CreatePeerConnectionWrappers());
4007 ConnectFakeSignaling();
4008 SetPortAllocatorFlags();
4009 SetUpNetworkInterfaces();
4010 caller()->AddAudioVideoTracks();
4011 caller()->CreateAndSetAndSignalOffer();
4012
4013 // According to RFC7675, if there is no response within 30 seconds then the
4014 // peer should consider the other side to have rejected the connection. This
4015 // is signaled by the state transitioning to "failed".
4016 constexpr int kConsentTimeout = 30000;
4017 ASSERT_EQ_SIMULATED_WAIT(PeerConnectionInterface::kIceConnectionFailed,
4018 caller()->standardized_ice_connection_state(),
4019 kConsentTimeout, fake_clock);
4020
4021 // We need to manually close the peerconnections before the fake clock goes
4022 // out of scope, or we trigger a DCHECK in rtp_sender.cc when we briefly
4023 // return to using non-faked time.
4024 delete SetCallerPcWrapperAndReturnCurrent(nullptr);
4025 delete SetCalleePcWrapperAndReturnCurrent(nullptr);
Steve Antonede9ca52017-10-16 13:04:27 -07004026}
4027
4028// Tests that the best connection is set to the appropriate IPv4/IPv6 connection
4029// and that the statistics in the metric observers are updated correctly.
4030TEST_P(PeerConnectionIntegrationIceStatesTest, VerifyBestConnection) {
4031 ASSERT_TRUE(CreatePeerConnectionWrappers());
4032 ConnectFakeSignaling();
4033 SetPortAllocatorFlags();
4034 SetUpNetworkInterfaces();
Steve Anton15324772018-01-16 10:26:49 -08004035 caller()->AddAudioVideoTracks();
4036 callee()->AddAudioVideoTracks();
Steve Antonede9ca52017-10-16 13:04:27 -07004037 caller()->CreateAndSetAndSignalOffer();
4038
4039 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4040
Qingsi Wang7fc821d2018-07-12 12:54:53 -07004041 // TODO(bugs.webrtc.org/9456): Fix it.
4042 const int num_best_ipv4 = webrtc::metrics::NumEvents(
4043 "WebRTC.PeerConnection.IPMetrics", webrtc::kBestConnections_IPv4);
4044 const int num_best_ipv6 = webrtc::metrics::NumEvents(
4045 "WebRTC.PeerConnection.IPMetrics", webrtc::kBestConnections_IPv6);
Steve Antonede9ca52017-10-16 13:04:27 -07004046 if (TestIPv6()) {
4047 // When IPv6 is enabled, we should prefer an IPv6 connection over an IPv4
4048 // connection.
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02004049 EXPECT_EQ(0, num_best_ipv4);
4050 EXPECT_EQ(1, num_best_ipv6);
Steve Antonede9ca52017-10-16 13:04:27 -07004051 } else {
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02004052 EXPECT_EQ(1, num_best_ipv4);
4053 EXPECT_EQ(0, num_best_ipv6);
Steve Antonede9ca52017-10-16 13:04:27 -07004054 }
4055
Qingsi Wang7fc821d2018-07-12 12:54:53 -07004056 EXPECT_EQ(0, webrtc::metrics::NumEvents(
4057 "WebRTC.PeerConnection.CandidatePairType_UDP",
4058 webrtc::kIceCandidatePairHostHost));
4059 EXPECT_EQ(1, webrtc::metrics::NumEvents(
4060 "WebRTC.PeerConnection.CandidatePairType_UDP",
4061 webrtc::kIceCandidatePairHostPublicHostPublic));
Steve Antonede9ca52017-10-16 13:04:27 -07004062}
4063
4064constexpr uint32_t kFlagsIPv4NoStun = cricket::PORTALLOCATOR_DISABLE_TCP |
4065 cricket::PORTALLOCATOR_DISABLE_STUN |
4066 cricket::PORTALLOCATOR_DISABLE_RELAY;
4067constexpr uint32_t kFlagsIPv6NoStun =
4068 cricket::PORTALLOCATOR_DISABLE_TCP | cricket::PORTALLOCATOR_DISABLE_STUN |
4069 cricket::PORTALLOCATOR_ENABLE_IPV6 | cricket::PORTALLOCATOR_DISABLE_RELAY;
4070constexpr uint32_t kFlagsIPv4Stun =
4071 cricket::PORTALLOCATOR_DISABLE_TCP | cricket::PORTALLOCATOR_DISABLE_RELAY;
4072
Mirko Bonadeic84f6612019-01-31 12:20:57 +01004073INSTANTIATE_TEST_SUITE_P(
Seth Hampson2f0d7022018-02-20 11:54:42 -08004074 PeerConnectionIntegrationTest,
4075 PeerConnectionIntegrationIceStatesTest,
4076 Combine(Values(SdpSemantics::kPlanB, SdpSemantics::kUnifiedPlan),
4077 Values(std::make_pair("IPv4 no STUN", kFlagsIPv4NoStun),
4078 std::make_pair("IPv6 no STUN", kFlagsIPv6NoStun),
4079 std::make_pair("IPv4 with STUN", kFlagsIPv4Stun))));
Steve Antonede9ca52017-10-16 13:04:27 -07004080
deadbeef1dcb1642017-03-29 21:08:16 -07004081// This test sets up a call between two parties with audio and video.
4082// During the call, the caller restarts ICE and the test verifies that
4083// new ICE candidates are generated and audio and video still can flow, and the
4084// ICE state reaches completed again.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004085TEST_P(PeerConnectionIntegrationTest, MediaContinuesFlowingAfterIceRestart) {
deadbeef1dcb1642017-03-29 21:08:16 -07004086 ASSERT_TRUE(CreatePeerConnectionWrappers());
4087 ConnectFakeSignaling();
4088 // Do normal offer/answer and wait for ICE to complete.
Steve Anton15324772018-01-16 10:26:49 -08004089 caller()->AddAudioVideoTracks();
4090 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07004091 caller()->CreateAndSetAndSignalOffer();
4092 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4093 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
4094 caller()->ice_connection_state(), kMaxWaitForFramesMs);
Alex Loiko9289eda2018-11-23 16:18:59 +00004095 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
4096 callee()->ice_connection_state(), kMaxWaitForFramesMs);
deadbeef1dcb1642017-03-29 21:08:16 -07004097
4098 // To verify that the ICE restart actually occurs, get
4099 // ufrag/password/candidates before and after restart.
4100 // Create an SDP string of the first audio candidate for both clients.
4101 const webrtc::IceCandidateCollection* audio_candidates_caller =
4102 caller()->pc()->local_description()->candidates(0);
4103 const webrtc::IceCandidateCollection* audio_candidates_callee =
4104 callee()->pc()->local_description()->candidates(0);
4105 ASSERT_GT(audio_candidates_caller->count(), 0u);
4106 ASSERT_GT(audio_candidates_callee->count(), 0u);
4107 std::string caller_candidate_pre_restart;
4108 ASSERT_TRUE(
4109 audio_candidates_caller->at(0)->ToString(&caller_candidate_pre_restart));
4110 std::string callee_candidate_pre_restart;
4111 ASSERT_TRUE(
4112 audio_candidates_callee->at(0)->ToString(&callee_candidate_pre_restart));
4113 const cricket::SessionDescription* desc =
4114 caller()->pc()->local_description()->description();
4115 std::string caller_ufrag_pre_restart =
4116 desc->transport_infos()[0].description.ice_ufrag;
4117 desc = callee()->pc()->local_description()->description();
4118 std::string callee_ufrag_pre_restart =
4119 desc->transport_infos()[0].description.ice_ufrag;
4120
4121 // Have the caller initiate an ICE restart.
4122 caller()->SetOfferAnswerOptions(IceRestartOfferAnswerOptions());
4123 caller()->CreateAndSetAndSignalOffer();
4124 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4125 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
4126 caller()->ice_connection_state(), kMaxWaitForFramesMs);
Alex Loiko9289eda2018-11-23 16:18:59 +00004127 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
deadbeef1dcb1642017-03-29 21:08:16 -07004128 callee()->ice_connection_state(), kMaxWaitForFramesMs);
4129
4130 // Grab the ufrags/candidates again.
4131 audio_candidates_caller = caller()->pc()->local_description()->candidates(0);
4132 audio_candidates_callee = callee()->pc()->local_description()->candidates(0);
4133 ASSERT_GT(audio_candidates_caller->count(), 0u);
4134 ASSERT_GT(audio_candidates_callee->count(), 0u);
4135 std::string caller_candidate_post_restart;
4136 ASSERT_TRUE(
4137 audio_candidates_caller->at(0)->ToString(&caller_candidate_post_restart));
4138 std::string callee_candidate_post_restart;
4139 ASSERT_TRUE(
4140 audio_candidates_callee->at(0)->ToString(&callee_candidate_post_restart));
4141 desc = caller()->pc()->local_description()->description();
4142 std::string caller_ufrag_post_restart =
4143 desc->transport_infos()[0].description.ice_ufrag;
4144 desc = callee()->pc()->local_description()->description();
4145 std::string callee_ufrag_post_restart =
4146 desc->transport_infos()[0].description.ice_ufrag;
4147 // Sanity check that an ICE restart was actually negotiated in SDP.
4148 ASSERT_NE(caller_candidate_pre_restart, caller_candidate_post_restart);
4149 ASSERT_NE(callee_candidate_pre_restart, callee_candidate_post_restart);
4150 ASSERT_NE(caller_ufrag_pre_restart, caller_ufrag_post_restart);
4151 ASSERT_NE(callee_ufrag_pre_restart, callee_ufrag_post_restart);
4152
4153 // Ensure that additional frames are received after the ICE restart.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004154 MediaExpectations media_expectations;
4155 media_expectations.ExpectBidirectionalAudioAndVideo();
4156 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07004157}
4158
4159// Verify that audio/video can be received end-to-end when ICE renomination is
4160// enabled.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004161TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithIceRenomination) {
deadbeef1dcb1642017-03-29 21:08:16 -07004162 PeerConnectionInterface::RTCConfiguration config;
4163 config.enable_ice_renomination = true;
4164 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
4165 ConnectFakeSignaling();
4166 // Do normal offer/answer and wait for some frames to be received in each
4167 // direction.
Steve Anton15324772018-01-16 10:26:49 -08004168 caller()->AddAudioVideoTracks();
4169 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07004170 caller()->CreateAndSetAndSignalOffer();
4171 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4172 // Sanity check that ICE renomination was actually negotiated.
4173 const cricket::SessionDescription* desc =
4174 caller()->pc()->local_description()->description();
4175 for (const cricket::TransportInfo& info : desc->transport_infos()) {
Steve Anton64b626b2019-01-28 17:25:26 -08004176 ASSERT_THAT(info.description.transport_options, Contains("renomination"));
deadbeef1dcb1642017-03-29 21:08:16 -07004177 }
4178 desc = callee()->pc()->local_description()->description();
4179 for (const cricket::TransportInfo& info : desc->transport_infos()) {
Steve Anton64b626b2019-01-28 17:25:26 -08004180 ASSERT_THAT(info.description.transport_options, Contains("renomination"));
deadbeef1dcb1642017-03-29 21:08:16 -07004181 }
Seth Hampson2f0d7022018-02-20 11:54:42 -08004182 MediaExpectations media_expectations;
4183 media_expectations.ExpectBidirectionalAudioAndVideo();
4184 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07004185}
4186
Steve Anton6f25b092017-10-23 09:39:20 -07004187// With a max bundle policy and RTCP muxing, adding a new media description to
4188// the connection should not affect ICE at all because the new media will use
4189// the existing connection.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004190TEST_P(PeerConnectionIntegrationTest,
Steve Anton83119dd2017-11-10 16:19:52 -08004191 AddMediaToConnectedBundleDoesNotRestartIce) {
Steve Anton6f25b092017-10-23 09:39:20 -07004192 PeerConnectionInterface::RTCConfiguration config;
4193 config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
4194 config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyRequire;
4195 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(
4196 config, PeerConnectionInterface::RTCConfiguration()));
4197 ConnectFakeSignaling();
4198
Steve Anton15324772018-01-16 10:26:49 -08004199 caller()->AddAudioTrack();
Steve Anton6f25b092017-10-23 09:39:20 -07004200 caller()->CreateAndSetAndSignalOffer();
4201 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Steve Antonff52f1b2017-10-26 12:24:50 -07004202 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceConnectionCompleted,
4203 caller()->ice_connection_state(), kDefaultTimeout);
Steve Anton6f25b092017-10-23 09:39:20 -07004204
4205 caller()->clear_ice_connection_state_history();
4206
Steve Anton15324772018-01-16 10:26:49 -08004207 caller()->AddVideoTrack();
Steve Anton6f25b092017-10-23 09:39:20 -07004208 caller()->CreateAndSetAndSignalOffer();
4209 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4210
4211 EXPECT_EQ(0u, caller()->ice_connection_state_history().size());
4212}
4213
deadbeef1dcb1642017-03-29 21:08:16 -07004214// This test sets up a call between two parties with audio and video. It then
4215// renegotiates setting the video m-line to "port 0", then later renegotiates
4216// again, enabling video.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004217TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07004218 VideoFlowsAfterMediaSectionIsRejectedAndRecycled) {
4219 ASSERT_TRUE(CreatePeerConnectionWrappers());
4220 ConnectFakeSignaling();
4221
4222 // Do initial negotiation, only sending media from the caller. Will result in
4223 // video and audio recvonly "m=" sections.
Steve Anton15324772018-01-16 10:26:49 -08004224 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07004225 caller()->CreateAndSetAndSignalOffer();
4226 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4227
4228 // Negotiate again, disabling the video "m=" section (the callee will set the
4229 // port to 0 due to offer_to_receive_video = 0).
Seth Hampson2f0d7022018-02-20 11:54:42 -08004230 if (sdp_semantics_ == SdpSemantics::kPlanB) {
4231 PeerConnectionInterface::RTCOfferAnswerOptions options;
4232 options.offer_to_receive_video = 0;
4233 callee()->SetOfferAnswerOptions(options);
4234 } else {
4235 callee()->SetRemoteOfferHandler([this] {
4236 callee()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)->Stop();
4237 });
4238 }
deadbeef1dcb1642017-03-29 21:08:16 -07004239 caller()->CreateAndSetAndSignalOffer();
4240 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4241 // Sanity check that video "m=" section was actually rejected.
4242 const ContentInfo* answer_video_content = cricket::GetFirstVideoContent(
4243 callee()->pc()->local_description()->description());
4244 ASSERT_NE(nullptr, answer_video_content);
4245 ASSERT_TRUE(answer_video_content->rejected);
4246
4247 // Enable video and do negotiation again, making sure video is received
4248 // end-to-end, also adding media stream to callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004249 if (sdp_semantics_ == SdpSemantics::kPlanB) {
4250 PeerConnectionInterface::RTCOfferAnswerOptions options;
4251 options.offer_to_receive_video = 1;
4252 callee()->SetOfferAnswerOptions(options);
4253 } else {
4254 // The caller's transceiver is stopped, so we need to add another track.
4255 auto caller_transceiver =
4256 caller()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO);
4257 EXPECT_TRUE(caller_transceiver->stopped());
4258 caller()->AddVideoTrack();
4259 }
4260 callee()->AddVideoTrack();
4261 callee()->SetRemoteOfferHandler(nullptr);
deadbeef1dcb1642017-03-29 21:08:16 -07004262 caller()->CreateAndSetAndSignalOffer();
4263 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08004264
deadbeef1dcb1642017-03-29 21:08:16 -07004265 // Verify the caller receives frames from the newly added stream, and the
4266 // callee receives additional frames from the re-enabled video m= section.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004267 MediaExpectations media_expectations;
4268 media_expectations.CalleeExpectsSomeAudio();
4269 media_expectations.ExpectBidirectionalVideo();
4270 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07004271}
4272
deadbeef1dcb1642017-03-29 21:08:16 -07004273// This tests that if we negotiate after calling CreateSender but before we
4274// have a track, then set a track later, frames from the newly-set track are
4275// received end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004276TEST_F(PeerConnectionIntegrationTestPlanB,
deadbeef1dcb1642017-03-29 21:08:16 -07004277 MediaFlowsAfterEarlyWarmupWithCreateSender) {
4278 ASSERT_TRUE(CreatePeerConnectionWrappers());
4279 ConnectFakeSignaling();
4280 auto caller_audio_sender =
4281 caller()->pc()->CreateSender("audio", "caller_stream");
4282 auto caller_video_sender =
4283 caller()->pc()->CreateSender("video", "caller_stream");
4284 auto callee_audio_sender =
4285 callee()->pc()->CreateSender("audio", "callee_stream");
4286 auto callee_video_sender =
4287 callee()->pc()->CreateSender("video", "callee_stream");
4288 caller()->CreateAndSetAndSignalOffer();
4289 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
4290 // Wait for ICE to complete, without any tracks being set.
4291 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
4292 caller()->ice_connection_state(), kMaxWaitForFramesMs);
4293 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
4294 callee()->ice_connection_state(), kMaxWaitForFramesMs);
4295 // Now set the tracks, and expect frames to immediately start flowing.
4296 EXPECT_TRUE(caller_audio_sender->SetTrack(caller()->CreateLocalAudioTrack()));
4297 EXPECT_TRUE(caller_video_sender->SetTrack(caller()->CreateLocalVideoTrack()));
4298 EXPECT_TRUE(callee_audio_sender->SetTrack(callee()->CreateLocalAudioTrack()));
4299 EXPECT_TRUE(callee_video_sender->SetTrack(callee()->CreateLocalVideoTrack()));
Seth Hampson2f0d7022018-02-20 11:54:42 -08004300 MediaExpectations media_expectations;
4301 media_expectations.ExpectBidirectionalAudioAndVideo();
4302 ASSERT_TRUE(ExpectNewFrames(media_expectations));
4303}
4304
4305// This tests that if we negotiate after calling AddTransceiver but before we
4306// have a track, then set a track later, frames from the newly-set tracks are
4307// received end-to-end.
4308TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
4309 MediaFlowsAfterEarlyWarmupWithAddTransceiver) {
4310 ASSERT_TRUE(CreatePeerConnectionWrappers());
4311 ConnectFakeSignaling();
4312 auto audio_result = caller()->pc()->AddTransceiver(cricket::MEDIA_TYPE_AUDIO);
4313 ASSERT_EQ(RTCErrorType::NONE, audio_result.error().type());
4314 auto caller_audio_sender = audio_result.MoveValue()->sender();
4315 auto video_result = caller()->pc()->AddTransceiver(cricket::MEDIA_TYPE_VIDEO);
4316 ASSERT_EQ(RTCErrorType::NONE, video_result.error().type());
4317 auto caller_video_sender = video_result.MoveValue()->sender();
4318 callee()->SetRemoteOfferHandler([this] {
4319 ASSERT_EQ(2u, callee()->pc()->GetTransceivers().size());
4320 callee()->pc()->GetTransceivers()[0]->SetDirection(
4321 RtpTransceiverDirection::kSendRecv);
4322 callee()->pc()->GetTransceivers()[1]->SetDirection(
4323 RtpTransceiverDirection::kSendRecv);
4324 });
4325 caller()->CreateAndSetAndSignalOffer();
4326 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
4327 // Wait for ICE to complete, without any tracks being set.
4328 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
4329 caller()->ice_connection_state(), kMaxWaitForFramesMs);
4330 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
4331 callee()->ice_connection_state(), kMaxWaitForFramesMs);
4332 // Now set the tracks, and expect frames to immediately start flowing.
4333 auto callee_audio_sender = callee()->pc()->GetSenders()[0];
4334 auto callee_video_sender = callee()->pc()->GetSenders()[1];
4335 ASSERT_TRUE(caller_audio_sender->SetTrack(caller()->CreateLocalAudioTrack()));
4336 ASSERT_TRUE(caller_video_sender->SetTrack(caller()->CreateLocalVideoTrack()));
4337 ASSERT_TRUE(callee_audio_sender->SetTrack(callee()->CreateLocalAudioTrack()));
4338 ASSERT_TRUE(callee_video_sender->SetTrack(callee()->CreateLocalVideoTrack()));
4339 MediaExpectations media_expectations;
4340 media_expectations.ExpectBidirectionalAudioAndVideo();
4341 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07004342}
4343
4344// This test verifies that a remote video track can be added via AddStream,
4345// and sent end-to-end. For this particular test, it's simply echoed back
4346// from the caller to the callee, rather than being forwarded to a third
4347// PeerConnection.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004348TEST_F(PeerConnectionIntegrationTestPlanB, CanSendRemoteVideoTrack) {
deadbeef1dcb1642017-03-29 21:08:16 -07004349 ASSERT_TRUE(CreatePeerConnectionWrappers());
4350 ConnectFakeSignaling();
4351 // Just send a video track from the caller.
Steve Anton15324772018-01-16 10:26:49 -08004352 caller()->AddVideoTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07004353 caller()->CreateAndSetAndSignalOffer();
4354 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02004355 ASSERT_EQ(1U, callee()->remote_streams()->count());
deadbeef1dcb1642017-03-29 21:08:16 -07004356
4357 // Echo the stream back, and do a new offer/anwer (initiated by callee this
4358 // time).
4359 callee()->pc()->AddStream(callee()->remote_streams()->at(0));
4360 callee()->CreateAndSetAndSignalOffer();
4361 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
4362
Seth Hampson2f0d7022018-02-20 11:54:42 -08004363 MediaExpectations media_expectations;
4364 media_expectations.ExpectBidirectionalVideo();
4365 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07004366}
4367
4368// Test that we achieve the expected end-to-end connection time, using a
4369// fake clock and simulated latency on the media and signaling paths.
4370// We use a TURN<->TURN connection because this is usually the quickest to
4371// set up initially, especially when we're confident the connection will work
4372// and can start sending media before we get a STUN response.
4373//
4374// With various optimizations enabled, here are the network delays we expect to
4375// be on the critical path:
4376// 1. 2 signaling trips: Signaling offer and offerer's TURN candidate, then
4377// signaling answer (with DTLS fingerprint).
4378// 2. 9 media hops: Rest of the DTLS handshake. 3 hops in each direction when
4379// using TURN<->TURN pair, and DTLS exchange is 4 packets,
4380// the first of which should have arrived before the answer.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004381TEST_P(PeerConnectionIntegrationTest, EndToEndConnectionTimeWithTurnTurnPair) {
deadbeef1dcb1642017-03-29 21:08:16 -07004382 rtc::ScopedFakeClock fake_clock;
4383 // Some things use a time of "0" as a special value, so we need to start out
4384 // the fake clock at a nonzero time.
4385 // TODO(deadbeef): Fix this.
Sebastian Jansson5f83cf02018-05-08 14:52:22 +02004386 fake_clock.AdvanceTime(webrtc::TimeDelta::seconds(1));
deadbeef1dcb1642017-03-29 21:08:16 -07004387
4388 static constexpr int media_hop_delay_ms = 50;
4389 static constexpr int signaling_trip_delay_ms = 500;
4390 // For explanation of these values, see comment above.
4391 static constexpr int required_media_hops = 9;
4392 static constexpr int required_signaling_trips = 2;
4393 // For internal delays (such as posting an event asychronously).
4394 static constexpr int allowed_internal_delay_ms = 20;
4395 static constexpr int total_connection_time_ms =
4396 media_hop_delay_ms * required_media_hops +
4397 signaling_trip_delay_ms * required_signaling_trips +
4398 allowed_internal_delay_ms;
4399
4400 static const rtc::SocketAddress turn_server_1_internal_address{"88.88.88.0",
4401 3478};
4402 static const rtc::SocketAddress turn_server_1_external_address{"88.88.88.1",
4403 0};
4404 static const rtc::SocketAddress turn_server_2_internal_address{"99.99.99.0",
4405 3478};
4406 static const rtc::SocketAddress turn_server_2_external_address{"99.99.99.1",
4407 0};
Seth Hampsonaed71642018-06-11 07:41:32 -07004408 cricket::TestTurnServer* turn_server_1 = CreateTurnServer(
4409 turn_server_1_internal_address, turn_server_1_external_address);
Jonas Orelandbdcee282017-10-10 14:01:40 +02004410
Seth Hampsonaed71642018-06-11 07:41:32 -07004411 cricket::TestTurnServer* turn_server_2 = CreateTurnServer(
4412 turn_server_2_internal_address, turn_server_2_external_address);
deadbeef1dcb1642017-03-29 21:08:16 -07004413 // Bypass permission check on received packets so media can be sent before
4414 // the candidate is signaled.
Seth Hampsonaed71642018-06-11 07:41:32 -07004415 network_thread()->Invoke<void>(RTC_FROM_HERE, [turn_server_1] {
4416 turn_server_1->set_enable_permission_checks(false);
4417 });
4418 network_thread()->Invoke<void>(RTC_FROM_HERE, [turn_server_2] {
4419 turn_server_2->set_enable_permission_checks(false);
4420 });
deadbeef1dcb1642017-03-29 21:08:16 -07004421
4422 PeerConnectionInterface::RTCConfiguration client_1_config;
4423 webrtc::PeerConnectionInterface::IceServer ice_server_1;
4424 ice_server_1.urls.push_back("turn:88.88.88.0:3478");
4425 ice_server_1.username = "test";
4426 ice_server_1.password = "test";
4427 client_1_config.servers.push_back(ice_server_1);
4428 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
4429 client_1_config.presume_writable_when_fully_relayed = true;
4430
4431 PeerConnectionInterface::RTCConfiguration client_2_config;
4432 webrtc::PeerConnectionInterface::IceServer ice_server_2;
4433 ice_server_2.urls.push_back("turn:99.99.99.0:3478");
4434 ice_server_2.username = "test";
4435 ice_server_2.password = "test";
4436 client_2_config.servers.push_back(ice_server_2);
4437 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
4438 client_2_config.presume_writable_when_fully_relayed = true;
4439
4440 ASSERT_TRUE(
4441 CreatePeerConnectionWrappersWithConfig(client_1_config, client_2_config));
4442 // Set up the simulated delays.
4443 SetSignalingDelayMs(signaling_trip_delay_ms);
4444 ConnectFakeSignaling();
4445 virtual_socket_server()->set_delay_mean(media_hop_delay_ms);
4446 virtual_socket_server()->UpdateDelayDistribution();
4447
4448 // Set "offer to receive audio/video" without adding any tracks, so we just
4449 // set up ICE/DTLS with no media.
4450 PeerConnectionInterface::RTCOfferAnswerOptions options;
4451 options.offer_to_receive_audio = 1;
4452 options.offer_to_receive_video = 1;
4453 caller()->SetOfferAnswerOptions(options);
4454 caller()->CreateAndSetAndSignalOffer();
deadbeef71452802017-05-07 17:21:01 -07004455 EXPECT_TRUE_SIMULATED_WAIT(DtlsConnected(), total_connection_time_ms,
4456 fake_clock);
Seth Hampson1d4a76d2018-06-19 14:31:41 -07004457 // Closing the PeerConnections destroys the ports before the ScopedFakeClock.
4458 // If this is not done a DCHECK can be hit in ports.cc, because a large
4459 // negative number is calculated for the rtt due to the global clock changing.
4460 caller()->pc()->Close();
4461 callee()->pc()->Close();
deadbeef1dcb1642017-03-29 21:08:16 -07004462}
4463
Jonas Orelandbdcee282017-10-10 14:01:40 +02004464// Verify that a TurnCustomizer passed in through RTCConfiguration
4465// is actually used by the underlying TURN candidate pair.
4466// Note that turnport_unittest.cc contains more detailed, lower-level tests.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004467TEST_P(PeerConnectionIntegrationTest, TurnCustomizerUsedForTurnConnections) {
Jonas Orelandbdcee282017-10-10 14:01:40 +02004468 static const rtc::SocketAddress turn_server_1_internal_address{"88.88.88.0",
4469 3478};
4470 static const rtc::SocketAddress turn_server_1_external_address{"88.88.88.1",
4471 0};
4472 static const rtc::SocketAddress turn_server_2_internal_address{"99.99.99.0",
4473 3478};
4474 static const rtc::SocketAddress turn_server_2_external_address{"99.99.99.1",
4475 0};
Seth Hampsonaed71642018-06-11 07:41:32 -07004476 CreateTurnServer(turn_server_1_internal_address,
4477 turn_server_1_external_address);
4478 CreateTurnServer(turn_server_2_internal_address,
4479 turn_server_2_external_address);
Jonas Orelandbdcee282017-10-10 14:01:40 +02004480
4481 PeerConnectionInterface::RTCConfiguration client_1_config;
4482 webrtc::PeerConnectionInterface::IceServer ice_server_1;
4483 ice_server_1.urls.push_back("turn:88.88.88.0:3478");
4484 ice_server_1.username = "test";
4485 ice_server_1.password = "test";
4486 client_1_config.servers.push_back(ice_server_1);
4487 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
Seth Hampsonaed71642018-06-11 07:41:32 -07004488 auto* customizer1 = CreateTurnCustomizer();
4489 client_1_config.turn_customizer = customizer1;
Jonas Orelandbdcee282017-10-10 14:01:40 +02004490
4491 PeerConnectionInterface::RTCConfiguration client_2_config;
4492 webrtc::PeerConnectionInterface::IceServer ice_server_2;
4493 ice_server_2.urls.push_back("turn:99.99.99.0:3478");
4494 ice_server_2.username = "test";
4495 ice_server_2.password = "test";
4496 client_2_config.servers.push_back(ice_server_2);
4497 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
Seth Hampsonaed71642018-06-11 07:41:32 -07004498 auto* customizer2 = CreateTurnCustomizer();
4499 client_2_config.turn_customizer = customizer2;
Jonas Orelandbdcee282017-10-10 14:01:40 +02004500
4501 ASSERT_TRUE(
4502 CreatePeerConnectionWrappersWithConfig(client_1_config, client_2_config));
4503 ConnectFakeSignaling();
4504
4505 // Set "offer to receive audio/video" without adding any tracks, so we just
4506 // set up ICE/DTLS with no media.
4507 PeerConnectionInterface::RTCOfferAnswerOptions options;
4508 options.offer_to_receive_audio = 1;
4509 options.offer_to_receive_video = 1;
4510 caller()->SetOfferAnswerOptions(options);
4511 caller()->CreateAndSetAndSignalOffer();
4512 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
4513
Seth Hampsonaed71642018-06-11 07:41:32 -07004514 ExpectTurnCustomizerCountersIncremented(customizer1);
4515 ExpectTurnCustomizerCountersIncremented(customizer2);
Jonas Orelandbdcee282017-10-10 14:01:40 +02004516}
4517
Benjamin Wright2d5f3cb2018-05-22 14:46:06 -07004518// Verifies that you can use TCP instead of UDP to connect to a TURN server and
4519// send media between the caller and the callee.
4520TEST_P(PeerConnectionIntegrationTest, TCPUsedForTurnConnections) {
4521 static const rtc::SocketAddress turn_server_internal_address{"88.88.88.0",
4522 3478};
4523 static const rtc::SocketAddress turn_server_external_address{"88.88.88.1", 0};
4524
4525 // Enable TCP for the fake turn server.
Seth Hampsonaed71642018-06-11 07:41:32 -07004526 CreateTurnServer(turn_server_internal_address, turn_server_external_address,
4527 cricket::PROTO_TCP);
Benjamin Wright2d5f3cb2018-05-22 14:46:06 -07004528
4529 webrtc::PeerConnectionInterface::IceServer ice_server;
4530 ice_server.urls.push_back("turn:88.88.88.0:3478?transport=tcp");
4531 ice_server.username = "test";
4532 ice_server.password = "test";
4533
4534 PeerConnectionInterface::RTCConfiguration client_1_config;
4535 client_1_config.servers.push_back(ice_server);
4536 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
4537
4538 PeerConnectionInterface::RTCConfiguration client_2_config;
4539 client_2_config.servers.push_back(ice_server);
4540 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
4541
4542 ASSERT_TRUE(
4543 CreatePeerConnectionWrappersWithConfig(client_1_config, client_2_config));
4544
4545 // Do normal offer/answer and wait for ICE to complete.
4546 ConnectFakeSignaling();
4547 caller()->AddAudioVideoTracks();
4548 callee()->AddAudioVideoTracks();
4549 caller()->CreateAndSetAndSignalOffer();
4550 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4551 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
4552 callee()->ice_connection_state(), kMaxWaitForFramesMs);
4553
4554 MediaExpectations media_expectations;
4555 media_expectations.ExpectBidirectionalAudioAndVideo();
4556 EXPECT_TRUE(ExpectNewFrames(media_expectations));
4557}
4558
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07004559// Verify that a SSLCertificateVerifier passed in through
4560// PeerConnectionDependencies is actually used by the underlying SSL
4561// implementation to determine whether a certificate presented by the TURN
4562// server is accepted by the client. Note that openssladapter_unittest.cc
4563// contains more detailed, lower-level tests.
4564TEST_P(PeerConnectionIntegrationTest,
4565 SSLCertificateVerifierUsedForTurnConnections) {
4566 static const rtc::SocketAddress turn_server_internal_address{"88.88.88.0",
4567 3478};
4568 static const rtc::SocketAddress turn_server_external_address{"88.88.88.1", 0};
4569
4570 // Enable TCP-TLS for the fake turn server. We need to pass in 88.88.88.0 so
4571 // that host name verification passes on the fake certificate.
Seth Hampsonaed71642018-06-11 07:41:32 -07004572 CreateTurnServer(turn_server_internal_address, turn_server_external_address,
4573 cricket::PROTO_TLS, "88.88.88.0");
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07004574
4575 webrtc::PeerConnectionInterface::IceServer ice_server;
4576 ice_server.urls.push_back("turns:88.88.88.0:3478?transport=tcp");
4577 ice_server.username = "test";
4578 ice_server.password = "test";
4579
4580 PeerConnectionInterface::RTCConfiguration client_1_config;
4581 client_1_config.servers.push_back(ice_server);
4582 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
4583
4584 PeerConnectionInterface::RTCConfiguration client_2_config;
4585 client_2_config.servers.push_back(ice_server);
4586 // Setting the type to kRelay forces the connection to go through a TURN
4587 // server.
4588 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
4589
4590 // Get a copy to the pointer so we can verify calls later.
4591 rtc::TestCertificateVerifier* client_1_cert_verifier =
4592 new rtc::TestCertificateVerifier();
4593 client_1_cert_verifier->verify_certificate_ = true;
4594 rtc::TestCertificateVerifier* client_2_cert_verifier =
4595 new rtc::TestCertificateVerifier();
4596 client_2_cert_verifier->verify_certificate_ = true;
4597
4598 // Create the dependencies with the test certificate verifier.
4599 webrtc::PeerConnectionDependencies client_1_deps(nullptr);
4600 client_1_deps.tls_cert_verifier =
4601 std::unique_ptr<rtc::TestCertificateVerifier>(client_1_cert_verifier);
4602 webrtc::PeerConnectionDependencies client_2_deps(nullptr);
4603 client_2_deps.tls_cert_verifier =
4604 std::unique_ptr<rtc::TestCertificateVerifier>(client_2_cert_verifier);
4605
4606 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndDeps(
4607 client_1_config, std::move(client_1_deps), client_2_config,
4608 std::move(client_2_deps)));
4609 ConnectFakeSignaling();
4610
4611 // Set "offer to receive audio/video" without adding any tracks, so we just
4612 // set up ICE/DTLS with no media.
4613 PeerConnectionInterface::RTCOfferAnswerOptions options;
4614 options.offer_to_receive_audio = 1;
4615 options.offer_to_receive_video = 1;
4616 caller()->SetOfferAnswerOptions(options);
4617 caller()->CreateAndSetAndSignalOffer();
4618 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
4619
4620 EXPECT_GT(client_1_cert_verifier->call_count_, 0u);
4621 EXPECT_GT(client_2_cert_verifier->call_count_, 0u);
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07004622}
4623
4624TEST_P(PeerConnectionIntegrationTest,
4625 SSLCertificateVerifierFailureUsedForTurnConnectionsFailsConnection) {
4626 static const rtc::SocketAddress turn_server_internal_address{"88.88.88.0",
4627 3478};
4628 static const rtc::SocketAddress turn_server_external_address{"88.88.88.1", 0};
4629
4630 // Enable TCP-TLS for the fake turn server. We need to pass in 88.88.88.0 so
4631 // that host name verification passes on the fake certificate.
Seth Hampsonaed71642018-06-11 07:41:32 -07004632 CreateTurnServer(turn_server_internal_address, turn_server_external_address,
4633 cricket::PROTO_TLS, "88.88.88.0");
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07004634
4635 webrtc::PeerConnectionInterface::IceServer ice_server;
4636 ice_server.urls.push_back("turns:88.88.88.0:3478?transport=tcp");
4637 ice_server.username = "test";
4638 ice_server.password = "test";
4639
4640 PeerConnectionInterface::RTCConfiguration client_1_config;
4641 client_1_config.servers.push_back(ice_server);
4642 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
4643
4644 PeerConnectionInterface::RTCConfiguration client_2_config;
4645 client_2_config.servers.push_back(ice_server);
4646 // Setting the type to kRelay forces the connection to go through a TURN
4647 // server.
4648 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
4649
4650 // Get a copy to the pointer so we can verify calls later.
4651 rtc::TestCertificateVerifier* client_1_cert_verifier =
4652 new rtc::TestCertificateVerifier();
4653 client_1_cert_verifier->verify_certificate_ = false;
4654 rtc::TestCertificateVerifier* client_2_cert_verifier =
4655 new rtc::TestCertificateVerifier();
4656 client_2_cert_verifier->verify_certificate_ = false;
4657
4658 // Create the dependencies with the test certificate verifier.
4659 webrtc::PeerConnectionDependencies client_1_deps(nullptr);
4660 client_1_deps.tls_cert_verifier =
4661 std::unique_ptr<rtc::TestCertificateVerifier>(client_1_cert_verifier);
4662 webrtc::PeerConnectionDependencies client_2_deps(nullptr);
4663 client_2_deps.tls_cert_verifier =
4664 std::unique_ptr<rtc::TestCertificateVerifier>(client_2_cert_verifier);
4665
4666 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndDeps(
4667 client_1_config, std::move(client_1_deps), client_2_config,
4668 std::move(client_2_deps)));
4669 ConnectFakeSignaling();
4670
4671 // Set "offer to receive audio/video" without adding any tracks, so we just
4672 // set up ICE/DTLS with no media.
4673 PeerConnectionInterface::RTCOfferAnswerOptions options;
4674 options.offer_to_receive_audio = 1;
4675 options.offer_to_receive_video = 1;
4676 caller()->SetOfferAnswerOptions(options);
4677 caller()->CreateAndSetAndSignalOffer();
4678 bool wait_res = true;
4679 // TODO(bugs.webrtc.org/9219): When IceConnectionState is implemented
4680 // properly, should be able to just wait for a state of "failed" instead of
4681 // waiting a fixed 10 seconds.
4682 WAIT_(DtlsConnected(), kDefaultTimeout, wait_res);
4683 ASSERT_FALSE(wait_res);
4684
4685 EXPECT_GT(client_1_cert_verifier->call_count_, 0u);
4686 EXPECT_GT(client_2_cert_verifier->call_count_, 0u);
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07004687}
4688
deadbeefc964d0b2017-04-03 10:03:35 -07004689// Test that audio and video flow end-to-end when codec names don't use the
4690// expected casing, given that they're supposed to be case insensitive. To test
4691// this, all but one codec is removed from each media description, and its
4692// casing is changed.
4693//
4694// In the past, this has regressed and caused crashes/black video, due to the
4695// fact that code at some layers was doing case-insensitive comparisons and
4696// code at other layers was not.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004697TEST_P(PeerConnectionIntegrationTest, CodecNamesAreCaseInsensitive) {
deadbeefc964d0b2017-04-03 10:03:35 -07004698 ASSERT_TRUE(CreatePeerConnectionWrappers());
4699 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08004700 caller()->AddAudioVideoTracks();
4701 callee()->AddAudioVideoTracks();
deadbeefc964d0b2017-04-03 10:03:35 -07004702
4703 // Remove all but one audio/video codec (opus and VP8), and change the
4704 // casing of the caller's generated offer.
4705 caller()->SetGeneratedSdpMunger([](cricket::SessionDescription* description) {
4706 cricket::AudioContentDescription* audio =
4707 GetFirstAudioContentDescription(description);
4708 ASSERT_NE(nullptr, audio);
4709 auto audio_codecs = audio->codecs();
4710 audio_codecs.erase(std::remove_if(audio_codecs.begin(), audio_codecs.end(),
4711 [](const cricket::AudioCodec& codec) {
4712 return codec.name != "opus";
4713 }),
4714 audio_codecs.end());
4715 ASSERT_EQ(1u, audio_codecs.size());
4716 audio_codecs[0].name = "OpUs";
4717 audio->set_codecs(audio_codecs);
4718
4719 cricket::VideoContentDescription* video =
4720 GetFirstVideoContentDescription(description);
4721 ASSERT_NE(nullptr, video);
4722 auto video_codecs = video->codecs();
4723 video_codecs.erase(std::remove_if(video_codecs.begin(), video_codecs.end(),
4724 [](const cricket::VideoCodec& codec) {
4725 return codec.name != "VP8";
4726 }),
4727 video_codecs.end());
4728 ASSERT_EQ(1u, video_codecs.size());
4729 video_codecs[0].name = "vP8";
4730 video->set_codecs(video_codecs);
4731 });
4732
4733 caller()->CreateAndSetAndSignalOffer();
4734 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4735
4736 // Verify frames are still received end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004737 MediaExpectations media_expectations;
4738 media_expectations.ExpectBidirectionalAudioAndVideo();
4739 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeefc964d0b2017-04-03 10:03:35 -07004740}
4741
Jonas Oreland49ac5952018-09-26 16:04:32 +02004742TEST_P(PeerConnectionIntegrationTest, GetSourcesAudio) {
hbos8d609f62017-04-10 07:39:05 -07004743 ASSERT_TRUE(CreatePeerConnectionWrappers());
4744 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08004745 caller()->AddAudioTrack();
hbos8d609f62017-04-10 07:39:05 -07004746 caller()->CreateAndSetAndSignalOffer();
4747 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
deadbeefd8ad7882017-04-18 16:01:17 -07004748 // Wait for one audio frame to be received by the callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004749 MediaExpectations media_expectations;
4750 media_expectations.CalleeExpectsSomeAudio(1);
4751 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Jonas Oreland49ac5952018-09-26 16:04:32 +02004752 ASSERT_EQ(callee()->pc()->GetReceivers().size(), 1u);
hbos8d609f62017-04-10 07:39:05 -07004753 auto receiver = callee()->pc()->GetReceivers()[0];
4754 ASSERT_EQ(receiver->media_type(), cricket::MEDIA_TYPE_AUDIO);
Jonas Oreland49ac5952018-09-26 16:04:32 +02004755 auto sources = receiver->GetSources();
hbos8d609f62017-04-10 07:39:05 -07004756 ASSERT_GT(receiver->GetParameters().encodings.size(), 0u);
4757 EXPECT_EQ(receiver->GetParameters().encodings[0].ssrc,
Jonas Oreland49ac5952018-09-26 16:04:32 +02004758 sources[0].source_id());
4759 EXPECT_EQ(webrtc::RtpSourceType::SSRC, sources[0].source_type());
4760}
4761
4762TEST_P(PeerConnectionIntegrationTest, GetSourcesVideo) {
4763 ASSERT_TRUE(CreatePeerConnectionWrappers());
4764 ConnectFakeSignaling();
4765 caller()->AddVideoTrack();
4766 caller()->CreateAndSetAndSignalOffer();
4767 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4768 // Wait for one video frame to be received by the callee.
4769 MediaExpectations media_expectations;
4770 media_expectations.CalleeExpectsSomeVideo(1);
4771 ASSERT_TRUE(ExpectNewFrames(media_expectations));
4772 ASSERT_EQ(callee()->pc()->GetReceivers().size(), 1u);
4773 auto receiver = callee()->pc()->GetReceivers()[0];
4774 ASSERT_EQ(receiver->media_type(), cricket::MEDIA_TYPE_VIDEO);
4775 auto sources = receiver->GetSources();
4776 ASSERT_GT(receiver->GetParameters().encodings.size(), 0u);
4777 EXPECT_EQ(receiver->GetParameters().encodings[0].ssrc,
4778 sources[0].source_id());
4779 EXPECT_EQ(webrtc::RtpSourceType::SSRC, sources[0].source_type());
hbos8d609f62017-04-10 07:39:05 -07004780}
4781
deadbeef2f425aa2017-04-14 10:41:32 -07004782// Test that if a track is removed and added again with a different stream ID,
4783// the new stream ID is successfully communicated in SDP and media continues to
4784// flow end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004785// TODO(webrtc.bugs.org/8734): This test does not work for Unified Plan because
4786// it will not reuse a transceiver that has already been sending. After creating
4787// a new transceiver it tries to create an offer with two senders of the same
4788// track ids and it fails.
4789TEST_F(PeerConnectionIntegrationTestPlanB, RemoveAndAddTrackWithNewStreamId) {
deadbeef2f425aa2017-04-14 10:41:32 -07004790 ASSERT_TRUE(CreatePeerConnectionWrappers());
4791 ConnectFakeSignaling();
4792
deadbeef2f425aa2017-04-14 10:41:32 -07004793 // Add track using stream 1, do offer/answer.
4794 rtc::scoped_refptr<webrtc::AudioTrackInterface> track =
4795 caller()->CreateLocalAudioTrack();
4796 rtc::scoped_refptr<webrtc::RtpSenderInterface> sender =
Steve Antond78323f2018-07-11 11:13:44 -07004797 caller()->AddTrack(track, {"stream_1"});
deadbeef2f425aa2017-04-14 10:41:32 -07004798 caller()->CreateAndSetAndSignalOffer();
4799 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08004800 {
4801 MediaExpectations media_expectations;
4802 media_expectations.CalleeExpectsSomeAudio(1);
4803 ASSERT_TRUE(ExpectNewFrames(media_expectations));
4804 }
deadbeef2f425aa2017-04-14 10:41:32 -07004805 // Remove the sender, and create a new one with the new stream.
4806 caller()->pc()->RemoveTrack(sender);
Steve Antond78323f2018-07-11 11:13:44 -07004807 sender = caller()->AddTrack(track, {"stream_2"});
deadbeef2f425aa2017-04-14 10:41:32 -07004808 caller()->CreateAndSetAndSignalOffer();
4809 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4810 // Wait for additional audio frames to be received by the callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004811 {
4812 MediaExpectations media_expectations;
4813 media_expectations.CalleeExpectsSomeAudio();
4814 ASSERT_TRUE(ExpectNewFrames(media_expectations));
4815 }
deadbeef2f425aa2017-04-14 10:41:32 -07004816}
4817
Seth Hampson2f0d7022018-02-20 11:54:42 -08004818TEST_P(PeerConnectionIntegrationTest, RtcEventLogOutputWriteCalled) {
Elad Alon99c3fe52017-10-13 16:29:40 +02004819 ASSERT_TRUE(CreatePeerConnectionWrappers());
4820 ConnectFakeSignaling();
4821
Karl Wiberg918f50c2018-07-05 11:40:33 +02004822 auto output = absl::make_unique<testing::NiceMock<MockRtcEventLogOutput>>();
Elad Alon99c3fe52017-10-13 16:29:40 +02004823 ON_CALL(*output, IsActive()).WillByDefault(testing::Return(true));
4824 ON_CALL(*output, Write(::testing::_)).WillByDefault(testing::Return(true));
4825 EXPECT_CALL(*output, Write(::testing::_)).Times(::testing::AtLeast(1));
Bjorn Tereliusde939432017-11-20 17:38:14 +01004826 EXPECT_TRUE(caller()->pc()->StartRtcEventLog(
4827 std::move(output), webrtc::RtcEventLog::kImmediateOutput));
Elad Alon99c3fe52017-10-13 16:29:40 +02004828
Steve Anton15324772018-01-16 10:26:49 -08004829 caller()->AddAudioVideoTracks();
Elad Alon99c3fe52017-10-13 16:29:40 +02004830 caller()->CreateAndSetAndSignalOffer();
4831 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4832}
4833
Steve Antonede9ca52017-10-16 13:04:27 -07004834// Test that if candidates are only signaled by applying full session
4835// descriptions (instead of using AddIceCandidate), the peers can connect to
4836// each other and exchange media.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004837TEST_P(PeerConnectionIntegrationTest, MediaFlowsWhenCandidatesSetOnlyInSdp) {
Steve Antonede9ca52017-10-16 13:04:27 -07004838 ASSERT_TRUE(CreatePeerConnectionWrappers());
4839 // Each side will signal the session descriptions but not candidates.
4840 ConnectFakeSignalingForSdpOnly();
4841
4842 // Add audio video track and exchange the initial offer/answer with media
4843 // information only. This will start ICE gathering on each side.
Steve Anton15324772018-01-16 10:26:49 -08004844 caller()->AddAudioVideoTracks();
4845 callee()->AddAudioVideoTracks();
Steve Antonede9ca52017-10-16 13:04:27 -07004846 caller()->CreateAndSetAndSignalOffer();
4847
4848 // Wait for all candidates to be gathered on both the caller and callee.
4849 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceGatheringComplete,
4850 caller()->ice_gathering_state(), kDefaultTimeout);
4851 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceGatheringComplete,
4852 callee()->ice_gathering_state(), kDefaultTimeout);
4853
4854 // The candidates will now be included in the session description, so
4855 // signaling them will start the ICE connection.
4856 caller()->CreateAndSetAndSignalOffer();
4857 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4858
4859 // Ensure that media flows in both directions.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004860 MediaExpectations media_expectations;
4861 media_expectations.ExpectBidirectionalAudioAndVideo();
4862 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Antonede9ca52017-10-16 13:04:27 -07004863}
4864
henrika5f6bf242017-11-01 11:06:56 +01004865// Test that SetAudioPlayout can be used to disable audio playout from the
4866// start, then later enable it. This may be useful, for example, if the caller
4867// needs to play a local ringtone until some event occurs, after which it
4868// switches to playing the received audio.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004869TEST_P(PeerConnectionIntegrationTest, DisableAndEnableAudioPlayout) {
henrika5f6bf242017-11-01 11:06:56 +01004870 ASSERT_TRUE(CreatePeerConnectionWrappers());
4871 ConnectFakeSignaling();
4872
4873 // Set up audio-only call where audio playout is disabled on caller's side.
4874 caller()->pc()->SetAudioPlayout(false);
Steve Anton15324772018-01-16 10:26:49 -08004875 caller()->AddAudioTrack();
4876 callee()->AddAudioTrack();
henrika5f6bf242017-11-01 11:06:56 +01004877 caller()->CreateAndSetAndSignalOffer();
4878 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4879
4880 // Pump messages for a second.
4881 WAIT(false, 1000);
4882 // Since audio playout is disabled, the caller shouldn't have received
4883 // anything (at the playout level, at least).
4884 EXPECT_EQ(0, caller()->audio_frames_received());
4885 // As a sanity check, make sure the callee (for which playout isn't disabled)
4886 // did still see frames on its audio level.
4887 ASSERT_GT(callee()->audio_frames_received(), 0);
4888
4889 // Enable playout again, and ensure audio starts flowing.
4890 caller()->pc()->SetAudioPlayout(true);
Seth Hampson2f0d7022018-02-20 11:54:42 -08004891 MediaExpectations media_expectations;
4892 media_expectations.ExpectBidirectionalAudio();
4893 ASSERT_TRUE(ExpectNewFrames(media_expectations));
henrika5f6bf242017-11-01 11:06:56 +01004894}
4895
4896double GetAudioEnergyStat(PeerConnectionWrapper* pc) {
4897 auto report = pc->NewGetStats();
4898 auto track_stats_list =
4899 report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
4900 const webrtc::RTCMediaStreamTrackStats* remote_track_stats = nullptr;
4901 for (const auto* track_stats : track_stats_list) {
4902 if (track_stats->remote_source.is_defined() &&
4903 *track_stats->remote_source) {
4904 remote_track_stats = track_stats;
4905 break;
4906 }
4907 }
4908
4909 if (!remote_track_stats->total_audio_energy.is_defined()) {
4910 return 0.0;
4911 }
4912 return *remote_track_stats->total_audio_energy;
4913}
4914
4915// Test that if audio playout is disabled via the SetAudioPlayout() method, then
4916// incoming audio is still processed and statistics are generated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004917TEST_P(PeerConnectionIntegrationTest,
henrika5f6bf242017-11-01 11:06:56 +01004918 DisableAudioPlayoutStillGeneratesAudioStats) {
4919 ASSERT_TRUE(CreatePeerConnectionWrappers());
4920 ConnectFakeSignaling();
4921
4922 // Set up audio-only call where playout is disabled but audio-processing is
4923 // still active.
Steve Anton15324772018-01-16 10:26:49 -08004924 caller()->AddAudioTrack();
4925 callee()->AddAudioTrack();
henrika5f6bf242017-11-01 11:06:56 +01004926 caller()->pc()->SetAudioPlayout(false);
4927
4928 caller()->CreateAndSetAndSignalOffer();
4929 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4930
4931 // Wait for the callee to receive audio stats.
4932 EXPECT_TRUE_WAIT(GetAudioEnergyStat(caller()) > 0, kMaxWaitForFramesMs);
4933}
4934
henrika4f167df2017-11-01 14:45:55 +01004935// Test that SetAudioRecording can be used to disable audio recording from the
4936// start, then later enable it. This may be useful, for example, if the caller
4937// wants to ensure that no audio resources are active before a certain state
4938// is reached.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004939TEST_P(PeerConnectionIntegrationTest, DisableAndEnableAudioRecording) {
henrika4f167df2017-11-01 14:45:55 +01004940 ASSERT_TRUE(CreatePeerConnectionWrappers());
4941 ConnectFakeSignaling();
4942
4943 // Set up audio-only call where audio recording is disabled on caller's side.
4944 caller()->pc()->SetAudioRecording(false);
Steve Anton15324772018-01-16 10:26:49 -08004945 caller()->AddAudioTrack();
4946 callee()->AddAudioTrack();
henrika4f167df2017-11-01 14:45:55 +01004947 caller()->CreateAndSetAndSignalOffer();
4948 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4949
4950 // Pump messages for a second.
4951 WAIT(false, 1000);
4952 // Since caller has disabled audio recording, the callee shouldn't have
4953 // received anything.
4954 EXPECT_EQ(0, callee()->audio_frames_received());
4955 // As a sanity check, make sure the caller did still see frames on its
4956 // audio level since audio recording is enabled on the calle side.
4957 ASSERT_GT(caller()->audio_frames_received(), 0);
4958
4959 // Enable audio recording again, and ensure audio starts flowing.
4960 caller()->pc()->SetAudioRecording(true);
Seth Hampson2f0d7022018-02-20 11:54:42 -08004961 MediaExpectations media_expectations;
4962 media_expectations.ExpectBidirectionalAudio();
4963 ASSERT_TRUE(ExpectNewFrames(media_expectations));
henrika4f167df2017-11-01 14:45:55 +01004964}
4965
Taylor Brandstetter389a97c2018-01-03 16:26:06 -08004966// Test that after closing PeerConnections, they stop sending any packets (ICE,
4967// DTLS, RTP...).
Seth Hampson2f0d7022018-02-20 11:54:42 -08004968TEST_P(PeerConnectionIntegrationTest, ClosingConnectionStopsPacketFlow) {
Taylor Brandstetter389a97c2018-01-03 16:26:06 -08004969 // Set up audio/video/data, wait for some frames to be received.
4970 ASSERT_TRUE(CreatePeerConnectionWrappers());
4971 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08004972 caller()->AddAudioVideoTracks();
Taylor Brandstetter389a97c2018-01-03 16:26:06 -08004973#ifdef HAVE_SCTP
4974 caller()->CreateDataChannel();
4975#endif
4976 caller()->CreateAndSetAndSignalOffer();
4977 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08004978 MediaExpectations media_expectations;
4979 media_expectations.CalleeExpectsSomeAudioAndVideo();
4980 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Taylor Brandstetter389a97c2018-01-03 16:26:06 -08004981 // Close PeerConnections.
4982 caller()->pc()->Close();
4983 callee()->pc()->Close();
4984 // Pump messages for a second, and ensure no new packets end up sent.
4985 uint32_t sent_packets_a = virtual_socket_server()->sent_packets();
4986 WAIT(false, 1000);
4987 uint32_t sent_packets_b = virtual_socket_server()->sent_packets();
4988 EXPECT_EQ(sent_packets_a, sent_packets_b);
4989}
4990
Steve Anton7eca0932018-03-30 15:18:41 -07004991// Test that transport stats are generated by the RTCStatsCollector for a
4992// connection that only involves data channels. This is a regression test for
4993// crbug.com/826972.
4994#ifdef HAVE_SCTP
4995TEST_P(PeerConnectionIntegrationTest,
4996 TransportStatsReportedForDataChannelOnlyConnection) {
4997 ASSERT_TRUE(CreatePeerConnectionWrappers());
4998 ConnectFakeSignaling();
4999 caller()->CreateDataChannel();
5000
5001 caller()->CreateAndSetAndSignalOffer();
5002 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5003 ASSERT_TRUE_WAIT(callee()->data_channel(), kDefaultTimeout);
5004
5005 auto caller_report = caller()->NewGetStats();
5006 EXPECT_EQ(1u, caller_report->GetStatsOfType<RTCTransportStats>().size());
5007 auto callee_report = callee()->NewGetStats();
5008 EXPECT_EQ(1u, callee_report->GetStatsOfType<RTCTransportStats>().size());
5009}
5010#endif // HAVE_SCTP
5011
Qingsi Wang7685e862018-06-11 20:15:46 -07005012TEST_P(PeerConnectionIntegrationTest,
5013 IceEventsGeneratedAndLoggedInRtcEventLog) {
5014 ASSERT_TRUE(CreatePeerConnectionWrappersWithFakeRtcEventLog());
5015 ConnectFakeSignaling();
5016 PeerConnectionInterface::RTCOfferAnswerOptions options;
5017 options.offer_to_receive_audio = 1;
5018 caller()->SetOfferAnswerOptions(options);
5019 caller()->CreateAndSetAndSignalOffer();
5020 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
5021 ASSERT_NE(nullptr, caller()->event_log_factory());
5022 ASSERT_NE(nullptr, callee()->event_log_factory());
5023 webrtc::FakeRtcEventLog* caller_event_log =
5024 static_cast<webrtc::FakeRtcEventLog*>(
5025 caller()->event_log_factory()->last_log_created());
5026 webrtc::FakeRtcEventLog* callee_event_log =
5027 static_cast<webrtc::FakeRtcEventLog*>(
5028 callee()->event_log_factory()->last_log_created());
5029 ASSERT_NE(nullptr, caller_event_log);
5030 ASSERT_NE(nullptr, callee_event_log);
5031 int caller_ice_config_count = caller_event_log->GetEventCount(
5032 webrtc::RtcEvent::Type::IceCandidatePairConfig);
5033 int caller_ice_event_count = caller_event_log->GetEventCount(
5034 webrtc::RtcEvent::Type::IceCandidatePairEvent);
5035 int callee_ice_config_count = callee_event_log->GetEventCount(
5036 webrtc::RtcEvent::Type::IceCandidatePairConfig);
5037 int callee_ice_event_count = callee_event_log->GetEventCount(
5038 webrtc::RtcEvent::Type::IceCandidatePairEvent);
5039 EXPECT_LT(0, caller_ice_config_count);
5040 EXPECT_LT(0, caller_ice_event_count);
5041 EXPECT_LT(0, callee_ice_config_count);
5042 EXPECT_LT(0, callee_ice_event_count);
5043}
5044
Mirko Bonadeic84f6612019-01-31 12:20:57 +01005045INSTANTIATE_TEST_SUITE_P(PeerConnectionIntegrationTest,
5046 PeerConnectionIntegrationTest,
5047 Values(SdpSemantics::kPlanB,
5048 SdpSemantics::kUnifiedPlan));
Steve Antond3679212018-01-17 17:41:02 -08005049
Steve Anton74255ff2018-01-24 18:32:57 -08005050// Tests that verify interoperability between Plan B and Unified Plan
5051// PeerConnections.
5052class PeerConnectionIntegrationInteropTest
Seth Hampson2f0d7022018-02-20 11:54:42 -08005053 : public PeerConnectionIntegrationBaseTest,
Steve Anton74255ff2018-01-24 18:32:57 -08005054 public ::testing::WithParamInterface<
5055 std::tuple<SdpSemantics, SdpSemantics>> {
5056 protected:
Seth Hampson2f0d7022018-02-20 11:54:42 -08005057 // Setting the SdpSemantics for the base test to kDefault does not matter
5058 // because we specify not to use the test semantics when creating
5059 // PeerConnectionWrappers.
Steve Anton74255ff2018-01-24 18:32:57 -08005060 PeerConnectionIntegrationInteropTest()
Steve Anton3acffc32018-04-12 17:21:03 -07005061 : PeerConnectionIntegrationBaseTest(SdpSemantics::kPlanB),
Seth Hampson2f0d7022018-02-20 11:54:42 -08005062 caller_semantics_(std::get<0>(GetParam())),
Steve Anton74255ff2018-01-24 18:32:57 -08005063 callee_semantics_(std::get<1>(GetParam())) {}
5064
5065 bool CreatePeerConnectionWrappersWithSemantics() {
Steve Anton3acffc32018-04-12 17:21:03 -07005066 return CreatePeerConnectionWrappersWithSdpSemantics(caller_semantics_,
5067 callee_semantics_);
Steve Anton74255ff2018-01-24 18:32:57 -08005068 }
5069
5070 const SdpSemantics caller_semantics_;
5071 const SdpSemantics callee_semantics_;
5072};
5073
5074TEST_P(PeerConnectionIntegrationInteropTest, NoMediaLocalToNoMediaRemote) {
5075 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
5076 ConnectFakeSignaling();
5077
5078 caller()->CreateAndSetAndSignalOffer();
5079 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5080}
5081
5082TEST_P(PeerConnectionIntegrationInteropTest, OneAudioLocalToNoMediaRemote) {
5083 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
5084 ConnectFakeSignaling();
5085 auto audio_sender = caller()->AddAudioTrack();
5086
5087 caller()->CreateAndSetAndSignalOffer();
5088 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5089
5090 // Verify that one audio receiver has been created on the remote and that it
5091 // has the same track ID as the sending track.
5092 auto receivers = callee()->pc()->GetReceivers();
5093 ASSERT_EQ(1u, receivers.size());
5094 EXPECT_EQ(cricket::MEDIA_TYPE_AUDIO, receivers[0]->media_type());
5095 EXPECT_EQ(receivers[0]->track()->id(), audio_sender->track()->id());
5096
Seth Hampson2f0d7022018-02-20 11:54:42 -08005097 MediaExpectations media_expectations;
5098 media_expectations.CalleeExpectsSomeAudio();
5099 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08005100}
5101
5102TEST_P(PeerConnectionIntegrationInteropTest, OneAudioOneVideoToNoMediaRemote) {
5103 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
5104 ConnectFakeSignaling();
5105 auto video_sender = caller()->AddVideoTrack();
5106 auto audio_sender = caller()->AddAudioTrack();
5107
5108 caller()->CreateAndSetAndSignalOffer();
5109 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5110
5111 // Verify that one audio and one video receiver have been created on the
5112 // remote and that they have the same track IDs as the sending tracks.
5113 auto audio_receivers =
5114 callee()->GetReceiversOfType(cricket::MEDIA_TYPE_AUDIO);
5115 ASSERT_EQ(1u, audio_receivers.size());
5116 EXPECT_EQ(audio_receivers[0]->track()->id(), audio_sender->track()->id());
5117 auto video_receivers =
5118 callee()->GetReceiversOfType(cricket::MEDIA_TYPE_VIDEO);
5119 ASSERT_EQ(1u, video_receivers.size());
5120 EXPECT_EQ(video_receivers[0]->track()->id(), video_sender->track()->id());
5121
Seth Hampson2f0d7022018-02-20 11:54:42 -08005122 MediaExpectations media_expectations;
5123 media_expectations.CalleeExpectsSomeAudioAndVideo();
5124 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08005125}
5126
5127TEST_P(PeerConnectionIntegrationInteropTest,
5128 OneAudioOneVideoLocalToOneAudioOneVideoRemote) {
5129 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
5130 ConnectFakeSignaling();
5131 caller()->AddAudioVideoTracks();
5132 callee()->AddAudioVideoTracks();
5133
5134 caller()->CreateAndSetAndSignalOffer();
5135 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5136
Seth Hampson2f0d7022018-02-20 11:54:42 -08005137 MediaExpectations media_expectations;
5138 media_expectations.ExpectBidirectionalAudioAndVideo();
5139 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08005140}
5141
5142TEST_P(PeerConnectionIntegrationInteropTest,
5143 ReverseRolesOneAudioLocalToOneVideoRemote) {
5144 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
5145 ConnectFakeSignaling();
5146 caller()->AddAudioTrack();
5147 callee()->AddVideoTrack();
5148
5149 caller()->CreateAndSetAndSignalOffer();
5150 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5151
5152 // Verify that only the audio track has been negotiated.
5153 EXPECT_EQ(0u, caller()->GetReceiversOfType(cricket::MEDIA_TYPE_VIDEO).size());
5154 // Might also check that the callee's NegotiationNeeded flag is set.
5155
5156 // Reverse roles.
5157 callee()->CreateAndSetAndSignalOffer();
5158 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5159
Seth Hampson2f0d7022018-02-20 11:54:42 -08005160 MediaExpectations media_expectations;
5161 media_expectations.CallerExpectsSomeVideo();
5162 media_expectations.CalleeExpectsSomeAudio();
5163 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08005164}
5165
Mirko Bonadeic84f6612019-01-31 12:20:57 +01005166INSTANTIATE_TEST_SUITE_P(
Steve Antonba42e992018-04-09 14:10:01 -07005167 PeerConnectionIntegrationTest,
5168 PeerConnectionIntegrationInteropTest,
5169 Values(std::make_tuple(SdpSemantics::kPlanB, SdpSemantics::kUnifiedPlan),
5170 std::make_tuple(SdpSemantics::kUnifiedPlan, SdpSemantics::kPlanB)));
5171
5172// Test that if the Unified Plan side offers two video tracks then the Plan B
5173// side will only see the first one and ignore the second.
5174TEST_F(PeerConnectionIntegrationTestPlanB, TwoVideoUnifiedPlanToNoMediaPlanB) {
Steve Anton3acffc32018-04-12 17:21:03 -07005175 ASSERT_TRUE(CreatePeerConnectionWrappersWithSdpSemantics(
5176 SdpSemantics::kUnifiedPlan, SdpSemantics::kPlanB));
Steve Anton74255ff2018-01-24 18:32:57 -08005177 ConnectFakeSignaling();
5178 auto first_sender = caller()->AddVideoTrack();
5179 caller()->AddVideoTrack();
5180
5181 caller()->CreateAndSetAndSignalOffer();
5182 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5183
5184 // Verify that there is only one receiver and it corresponds to the first
5185 // added track.
5186 auto receivers = callee()->pc()->GetReceivers();
5187 ASSERT_EQ(1u, receivers.size());
5188 EXPECT_TRUE(receivers[0]->track()->enabled());
5189 EXPECT_EQ(first_sender->track()->id(), receivers[0]->track()->id());
5190
Seth Hampson2f0d7022018-02-20 11:54:42 -08005191 MediaExpectations media_expectations;
5192 media_expectations.CalleeExpectsSomeVideo();
5193 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08005194}
5195
Steve Anton2bed3972019-01-04 17:04:30 -08005196// Test that if the initial offer tagged BUNDLE section is rejected due to its
5197// associated RtpTransceiver being stopped and another transceiver is added,
5198// then renegotiation causes the callee to receive the new video track without
5199// error.
5200// This is a regression test for bugs.webrtc.org/9954
5201TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
5202 ReOfferWithStoppedBundleTaggedTransceiver) {
5203 RTCConfiguration config;
5204 config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
5205 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
5206 ConnectFakeSignaling();
5207 auto audio_transceiver_or_error =
5208 caller()->pc()->AddTransceiver(caller()->CreateLocalAudioTrack());
5209 ASSERT_TRUE(audio_transceiver_or_error.ok());
5210 auto audio_transceiver = audio_transceiver_or_error.MoveValue();
5211
5212 caller()->CreateAndSetAndSignalOffer();
5213 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5214 {
5215 MediaExpectations media_expectations;
5216 media_expectations.CalleeExpectsSomeAudio();
5217 ASSERT_TRUE(ExpectNewFrames(media_expectations));
5218 }
5219
5220 audio_transceiver->Stop();
5221 caller()->pc()->AddTransceiver(caller()->CreateLocalVideoTrack());
5222
5223 caller()->CreateAndSetAndSignalOffer();
5224 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5225 {
5226 MediaExpectations media_expectations;
5227 media_expectations.CalleeExpectsSomeVideo();
5228 ASSERT_TRUE(ExpectNewFrames(media_expectations));
5229 }
5230}
5231
deadbeef1dcb1642017-03-29 21:08:16 -07005232} // namespace
Mirko Bonadeiab64e8a2018-12-12 12:10:18 +01005233} // namespace webrtc
deadbeef1dcb1642017-03-29 21:08:16 -07005234
5235#endif // if !defined(THREAD_SANITIZER)