blob: 33aa0381362690cd965f315d3bfba8d65b0e8570 [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;
Mirko Bonadeie46f5db2019-03-26 20:14:46 +010083using ::testing::DoAll;
Steve Antonede9ca52017-10-16 13:04:27 -070084using ::testing::ElementsAre;
Qingsi Wang1dac6d82018-12-12 15:28:47 -080085using ::testing::NiceMock;
Steve Anton64b626b2019-01-28 17:25:26 -080086using ::testing::Return;
Zach Stein6fcdc2f2018-08-23 16:25:55 -070087using ::testing::SetArgPointee;
Steve Antonffa6ce42018-11-30 09:26:08 -080088using ::testing::UnorderedElementsAreArray;
Mirko Bonadeiab64e8a2018-12-12 12:10:18 +010089using ::testing::Values;
Steve Anton74255ff2018-01-24 18:32:57 -080090using RTCConfiguration = PeerConnectionInterface::RTCConfiguration;
deadbeef1dcb1642017-03-29 21:08:16 -070091
92static const int kDefaultTimeout = 10000;
93static const int kMaxWaitForStatsMs = 3000;
94static const int kMaxWaitForActivationMs = 5000;
95static const int kMaxWaitForFramesMs = 10000;
96// Default number of audio/video frames to wait for before considering a test
97// successful.
98static const int kDefaultExpectedAudioFrameCount = 3;
99static const int kDefaultExpectedVideoFrameCount = 3;
100
deadbeef1dcb1642017-03-29 21:08:16 -0700101static const char kDataChannelLabel[] = "data_channel";
102
103// SRTP cipher name negotiated by the tests. This must be updated if the
104// default changes.
Taylor Brandstetterfd350d72018-04-03 16:29:26 -0700105static const int kDefaultSrtpCryptoSuite = rtc::SRTP_AES128_CM_SHA1_80;
deadbeef1dcb1642017-03-29 21:08:16 -0700106static const int kDefaultSrtpCryptoSuiteGcm = rtc::SRTP_AEAD_AES_256_GCM;
107
Steve Antonede9ca52017-10-16 13:04:27 -0700108static const SocketAddress kDefaultLocalAddress("192.168.1.1", 0);
109
deadbeef1dcb1642017-03-29 21:08:16 -0700110// Helper function for constructing offer/answer options to initiate an ICE
111// restart.
112PeerConnectionInterface::RTCOfferAnswerOptions IceRestartOfferAnswerOptions() {
113 PeerConnectionInterface::RTCOfferAnswerOptions options;
114 options.ice_restart = true;
115 return options;
116}
117
deadbeefd8ad7882017-04-18 16:01:17 -0700118// Remove all stream information (SSRCs, track IDs, etc.) and "msid-semantic"
119// attribute from received SDP, simulating a legacy endpoint.
120void RemoveSsrcsAndMsids(cricket::SessionDescription* desc) {
121 for (ContentInfo& content : desc->contents()) {
Steve Antonb1c1de12017-12-21 15:14:30 -0800122 content.media_description()->mutable_streams().clear();
deadbeefd8ad7882017-04-18 16:01:17 -0700123 }
124 desc->set_msid_supported(false);
Henrik Boström5b147782018-12-04 11:25:05 +0100125 desc->set_msid_signaling(0);
deadbeefd8ad7882017-04-18 16:01:17 -0700126}
127
Seth Hampson5897a6e2018-04-03 11:16:33 -0700128// Removes all stream information besides the stream ids, simulating an
129// endpoint that only signals a=msid lines to convey stream_ids.
130void RemoveSsrcsAndKeepMsids(cricket::SessionDescription* desc) {
131 for (ContentInfo& content : desc->contents()) {
Steve Antondf527fd2018-04-27 15:52:03 -0700132 std::string track_id;
Seth Hampson5897a6e2018-04-03 11:16:33 -0700133 std::vector<std::string> stream_ids;
134 if (!content.media_description()->streams().empty()) {
Steve Antondf527fd2018-04-27 15:52:03 -0700135 const StreamParams& first_stream =
136 content.media_description()->streams()[0];
137 track_id = first_stream.id;
138 stream_ids = first_stream.stream_ids();
Seth Hampson5897a6e2018-04-03 11:16:33 -0700139 }
140 content.media_description()->mutable_streams().clear();
Steve Antondf527fd2018-04-27 15:52:03 -0700141 StreamParams new_stream;
142 new_stream.id = track_id;
Seth Hampson5897a6e2018-04-03 11:16:33 -0700143 new_stream.set_stream_ids(stream_ids);
144 content.media_description()->AddStream(new_stream);
145 }
146}
147
zhihuangf8164932017-05-19 13:09:47 -0700148int FindFirstMediaStatsIndexByKind(
149 const std::string& kind,
150 const std::vector<const webrtc::RTCMediaStreamTrackStats*>&
151 media_stats_vec) {
152 for (size_t i = 0; i < media_stats_vec.size(); i++) {
153 if (media_stats_vec[i]->kind.ValueToString() == kind) {
154 return i;
155 }
156 }
157 return -1;
158}
159
deadbeef1dcb1642017-03-29 21:08:16 -0700160class SignalingMessageReceiver {
161 public:
Steve Antona3a92c22017-12-07 10:27:41 -0800162 virtual void ReceiveSdpMessage(SdpType type, const std::string& msg) = 0;
deadbeef1dcb1642017-03-29 21:08:16 -0700163 virtual void ReceiveIceMessage(const std::string& sdp_mid,
164 int sdp_mline_index,
165 const std::string& msg) = 0;
166
167 protected:
168 SignalingMessageReceiver() {}
169 virtual ~SignalingMessageReceiver() {}
170};
171
172class MockRtpReceiverObserver : public webrtc::RtpReceiverObserverInterface {
173 public:
174 explicit MockRtpReceiverObserver(cricket::MediaType media_type)
175 : expected_media_type_(media_type) {}
176
177 void OnFirstPacketReceived(cricket::MediaType media_type) override {
178 ASSERT_EQ(expected_media_type_, media_type);
179 first_packet_received_ = true;
180 }
181
182 bool first_packet_received() const { return first_packet_received_; }
183
184 virtual ~MockRtpReceiverObserver() {}
185
186 private:
187 bool first_packet_received_ = false;
188 cricket::MediaType expected_media_type_;
189};
190
191// Helper class that wraps a peer connection, observes it, and can accept
192// signaling messages from another wrapper.
193//
194// Uses a fake network, fake A/V capture, and optionally fake
195// encoders/decoders, though they aren't used by default since they don't
196// advertise support of any codecs.
Steve Anton94286cb2017-09-26 16:20:19 -0700197// TODO(steveanton): See how this could become a subclass of
Seth Hampson2f0d7022018-02-20 11:54:42 -0800198// PeerConnectionWrapper defined in peerconnectionwrapper.h.
deadbeef1dcb1642017-03-29 21:08:16 -0700199class PeerConnectionWrapper : public webrtc::PeerConnectionObserver,
Steve Anton15324772018-01-16 10:26:49 -0800200 public SignalingMessageReceiver {
deadbeef1dcb1642017-03-29 21:08:16 -0700201 public:
202 // Different factory methods for convenience.
203 // TODO(deadbeef): Could use the pattern of:
204 //
205 // PeerConnectionWrapper =
206 // WrapperBuilder.WithConfig(...).WithOptions(...).build();
207 //
208 // To reduce some code duplication.
209 static PeerConnectionWrapper* CreateWithDtlsIdentityStore(
210 const std::string& debug_name,
211 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator,
212 rtc::Thread* network_thread,
213 rtc::Thread* worker_thread) {
214 PeerConnectionWrapper* client(new PeerConnectionWrapper(debug_name));
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700215 webrtc::PeerConnectionDependencies dependencies(nullptr);
216 dependencies.cert_generator = std::move(cert_generator);
Niels Möllerf06f9232018-08-07 12:32:18 +0200217 if (!client->Init(nullptr, nullptr, std::move(dependencies), network_thread,
Bjorn Mellem175aa2e2018-11-08 11:23:22 -0800218 worker_thread, nullptr,
219 /*media_transport_factory=*/nullptr)) {
deadbeef1dcb1642017-03-29 21:08:16 -0700220 delete client;
221 return nullptr;
222 }
223 return client;
224 }
225
deadbeef2f425aa2017-04-14 10:41:32 -0700226 webrtc::PeerConnectionFactoryInterface* pc_factory() const {
227 return peer_connection_factory_.get();
228 }
229
deadbeef1dcb1642017-03-29 21:08:16 -0700230 webrtc::PeerConnectionInterface* pc() const { return peer_connection_.get(); }
231
232 // If a signaling message receiver is set (via ConnectFakeSignaling), this
233 // will set the whole offer/answer exchange in motion. Just need to wait for
234 // the signaling state to reach "stable".
235 void CreateAndSetAndSignalOffer() {
236 auto offer = CreateOffer();
237 ASSERT_NE(nullptr, offer);
238 EXPECT_TRUE(SetLocalDescriptionAndSendSdpMessage(std::move(offer)));
239 }
240
241 // Sets the options to be used when CreateAndSetAndSignalOffer is called, or
242 // when a remote offer is received (via fake signaling) and an answer is
243 // generated. By default, uses default options.
244 void SetOfferAnswerOptions(
245 const PeerConnectionInterface::RTCOfferAnswerOptions& options) {
246 offer_answer_options_ = options;
247 }
248
249 // Set a callback to be invoked when SDP is received via the fake signaling
250 // channel, which provides an opportunity to munge (modify) the SDP. This is
251 // used to test SDP being applied that a PeerConnection would normally not
252 // generate, but a non-JSEP endpoint might.
253 void SetReceivedSdpMunger(
254 std::function<void(cricket::SessionDescription*)> munger) {
Mirko Bonadeic61ce0d2017-11-21 17:04:20 +0100255 received_sdp_munger_ = std::move(munger);
deadbeef1dcb1642017-03-29 21:08:16 -0700256 }
257
deadbeefc964d0b2017-04-03 10:03:35 -0700258 // Similar to the above, but this is run on SDP immediately after it's
deadbeef1dcb1642017-03-29 21:08:16 -0700259 // generated.
260 void SetGeneratedSdpMunger(
261 std::function<void(cricket::SessionDescription*)> munger) {
Mirko Bonadeic61ce0d2017-11-21 17:04:20 +0100262 generated_sdp_munger_ = std::move(munger);
deadbeef1dcb1642017-03-29 21:08:16 -0700263 }
264
Seth Hampson2f0d7022018-02-20 11:54:42 -0800265 // Set a callback to be invoked when a remote offer is received via the fake
266 // signaling channel. This provides an opportunity to change the
267 // PeerConnection state before an answer is created and sent to the caller.
268 void SetRemoteOfferHandler(std::function<void()> handler) {
269 remote_offer_handler_ = std::move(handler);
270 }
271
Qingsi Wang1dac6d82018-12-12 15:28:47 -0800272 void SetRemoteAsyncResolver(rtc::MockAsyncResolver* resolver) {
273 remote_async_resolver_ = resolver;
Zach Stein6fcdc2f2018-08-23 16:25:55 -0700274 }
275
Steve Antonede9ca52017-10-16 13:04:27 -0700276 // Every ICE connection state in order that has been seen by the observer.
277 std::vector<PeerConnectionInterface::IceConnectionState>
278 ice_connection_state_history() const {
279 return ice_connection_state_history_;
280 }
Steve Anton6f25b092017-10-23 09:39:20 -0700281 void clear_ice_connection_state_history() {
282 ice_connection_state_history_.clear();
283 }
Steve Antonede9ca52017-10-16 13:04:27 -0700284
Jonas Olssonacd8ae72019-02-25 15:26:24 +0100285 // Every standardized ICE connection state in order that has been seen by the
286 // observer.
287 std::vector<PeerConnectionInterface::IceConnectionState>
288 standardized_ice_connection_state_history() const {
289 return standardized_ice_connection_state_history_;
290 }
291
Jonas Olsson635474e2018-10-18 15:58:17 +0200292 // Every PeerConnection state in order that has been seen by the observer.
293 std::vector<PeerConnectionInterface::PeerConnectionState>
294 peer_connection_state_history() const {
295 return peer_connection_state_history_;
296 }
297
Steve Antonede9ca52017-10-16 13:04:27 -0700298 // Every ICE gathering state in order that has been seen by the observer.
299 std::vector<PeerConnectionInterface::IceGatheringState>
300 ice_gathering_state_history() const {
301 return ice_gathering_state_history_;
deadbeef1dcb1642017-03-29 21:08:16 -0700302 }
303
Steve Anton15324772018-01-16 10:26:49 -0800304 void AddAudioVideoTracks() {
305 AddAudioTrack();
306 AddVideoTrack();
deadbeef1dcb1642017-03-29 21:08:16 -0700307 }
308
Steve Anton74255ff2018-01-24 18:32:57 -0800309 rtc::scoped_refptr<RtpSenderInterface> AddAudioTrack() {
310 return AddTrack(CreateLocalAudioTrack());
311 }
deadbeef1dcb1642017-03-29 21:08:16 -0700312
Steve Anton74255ff2018-01-24 18:32:57 -0800313 rtc::scoped_refptr<RtpSenderInterface> AddVideoTrack() {
314 return AddTrack(CreateLocalVideoTrack());
315 }
deadbeef1dcb1642017-03-29 21:08:16 -0700316
317 rtc::scoped_refptr<webrtc::AudioTrackInterface> CreateLocalAudioTrack() {
Niels Möller2d02e082018-05-21 11:23:35 +0200318 cricket::AudioOptions options;
deadbeef1dcb1642017-03-29 21:08:16 -0700319 // Disable highpass filter so that we can get all the test audio frames.
Niels Möller2d02e082018-05-21 11:23:35 +0200320 options.highpass_filter = false;
deadbeef1dcb1642017-03-29 21:08:16 -0700321 rtc::scoped_refptr<webrtc::AudioSourceInterface> source =
Niels Möller2d02e082018-05-21 11:23:35 +0200322 peer_connection_factory_->CreateAudioSource(options);
deadbeef1dcb1642017-03-29 21:08:16 -0700323 // TODO(perkj): Test audio source when it is implemented. Currently audio
324 // always use the default input.
deadbeefb1a15d72017-09-07 14:12:05 -0700325 return peer_connection_factory_->CreateAudioTrack(rtc::CreateRandomUuid(),
deadbeef1dcb1642017-03-29 21:08:16 -0700326 source);
327 }
328
329 rtc::scoped_refptr<webrtc::VideoTrackInterface> CreateLocalVideoTrack() {
Johannes Kron965e7942018-09-13 15:36:20 +0200330 webrtc::FakePeriodicVideoSource::Config config;
331 config.timestamp_offset_ms = rtc::TimeMillis();
332 return CreateLocalVideoTrackInternal(config);
deadbeef1dcb1642017-03-29 21:08:16 -0700333 }
334
335 rtc::scoped_refptr<webrtc::VideoTrackInterface>
Niels Möller5c7efe72018-05-11 10:34:46 +0200336 CreateLocalVideoTrackWithConfig(
337 webrtc::FakePeriodicVideoSource::Config config) {
338 return CreateLocalVideoTrackInternal(config);
deadbeef1dcb1642017-03-29 21:08:16 -0700339 }
340
341 rtc::scoped_refptr<webrtc::VideoTrackInterface>
342 CreateLocalVideoTrackWithRotation(webrtc::VideoRotation rotation) {
Niels Möller5c7efe72018-05-11 10:34:46 +0200343 webrtc::FakePeriodicVideoSource::Config config;
344 config.rotation = rotation;
Johannes Kron965e7942018-09-13 15:36:20 +0200345 config.timestamp_offset_ms = rtc::TimeMillis();
Niels Möller5c7efe72018-05-11 10:34:46 +0200346 return CreateLocalVideoTrackInternal(config);
deadbeef1dcb1642017-03-29 21:08:16 -0700347 }
348
Steve Anton74255ff2018-01-24 18:32:57 -0800349 rtc::scoped_refptr<RtpSenderInterface> AddTrack(
350 rtc::scoped_refptr<MediaStreamTrackInterface> track,
Seth Hampson845e8782018-03-02 11:34:10 -0800351 const std::vector<std::string>& stream_ids = {}) {
352 auto result = pc()->AddTrack(track, stream_ids);
Steve Anton15324772018-01-16 10:26:49 -0800353 EXPECT_EQ(RTCErrorType::NONE, result.error().type());
Steve Anton74255ff2018-01-24 18:32:57 -0800354 return result.MoveValue();
355 }
356
357 std::vector<rtc::scoped_refptr<RtpReceiverInterface>> GetReceiversOfType(
358 cricket::MediaType media_type) {
359 std::vector<rtc::scoped_refptr<RtpReceiverInterface>> receivers;
Mirko Bonadei739baf02019-01-27 17:29:42 +0100360 for (const auto& receiver : pc()->GetReceivers()) {
Steve Anton74255ff2018-01-24 18:32:57 -0800361 if (receiver->media_type() == media_type) {
362 receivers.push_back(receiver);
363 }
364 }
365 return receivers;
deadbeef1dcb1642017-03-29 21:08:16 -0700366 }
367
Seth Hampson2f0d7022018-02-20 11:54:42 -0800368 rtc::scoped_refptr<RtpTransceiverInterface> GetFirstTransceiverOfType(
369 cricket::MediaType media_type) {
370 for (auto transceiver : pc()->GetTransceivers()) {
371 if (transceiver->receiver()->media_type() == media_type) {
372 return transceiver;
373 }
374 }
375 return nullptr;
376 }
377
deadbeef1dcb1642017-03-29 21:08:16 -0700378 bool SignalingStateStable() {
379 return pc()->signaling_state() == webrtc::PeerConnectionInterface::kStable;
380 }
381
382 void CreateDataChannel() { CreateDataChannel(nullptr); }
383
384 void CreateDataChannel(const webrtc::DataChannelInit* init) {
Steve Antonda6c0952017-10-23 11:41:54 -0700385 CreateDataChannel(kDataChannelLabel, init);
386 }
387
388 void CreateDataChannel(const std::string& label,
389 const webrtc::DataChannelInit* init) {
390 data_channel_ = pc()->CreateDataChannel(label, init);
deadbeef1dcb1642017-03-29 21:08:16 -0700391 ASSERT_TRUE(data_channel_.get() != nullptr);
392 data_observer_.reset(new MockDataChannelObserver(data_channel_));
393 }
394
395 DataChannelInterface* data_channel() { return data_channel_; }
396 const MockDataChannelObserver* data_observer() const {
397 return data_observer_.get();
398 }
399
400 int audio_frames_received() const {
401 return fake_audio_capture_module_->frames_received();
402 }
403
404 // Takes minimum of video frames received for each track.
405 //
406 // Can be used like:
407 // EXPECT_GE(expected_frames, min_video_frames_received_per_track());
408 //
409 // To ensure that all video tracks received at least a certain number of
410 // frames.
411 int min_video_frames_received_per_track() const {
412 int min_frames = INT_MAX;
Anders Carlsson5f2bb622018-05-14 09:48:06 +0200413 if (fake_video_renderers_.empty()) {
414 return 0;
deadbeef1dcb1642017-03-29 21:08:16 -0700415 }
deadbeef1dcb1642017-03-29 21:08:16 -0700416
Anders Carlsson5f2bb622018-05-14 09:48:06 +0200417 for (const auto& pair : fake_video_renderers_) {
418 min_frames = std::min(min_frames, pair.second->num_rendered_frames());
deadbeef1dcb1642017-03-29 21:08:16 -0700419 }
Anders Carlsson5f2bb622018-05-14 09:48:06 +0200420 return min_frames;
deadbeef1dcb1642017-03-29 21:08:16 -0700421 }
422
423 // Returns a MockStatsObserver in a state after stats gathering finished,
424 // which can be used to access the gathered stats.
deadbeefd8ad7882017-04-18 16:01:17 -0700425 rtc::scoped_refptr<MockStatsObserver> OldGetStatsForTrack(
deadbeef1dcb1642017-03-29 21:08:16 -0700426 webrtc::MediaStreamTrackInterface* track) {
427 rtc::scoped_refptr<MockStatsObserver> observer(
428 new rtc::RefCountedObject<MockStatsObserver>());
429 EXPECT_TRUE(peer_connection_->GetStats(
430 observer, nullptr, PeerConnectionInterface::kStatsOutputLevelStandard));
431 EXPECT_TRUE_WAIT(observer->called(), kDefaultTimeout);
432 return observer;
433 }
434
435 // Version that doesn't take a track "filter", and gathers all stats.
deadbeefd8ad7882017-04-18 16:01:17 -0700436 rtc::scoped_refptr<MockStatsObserver> OldGetStats() {
437 return OldGetStatsForTrack(nullptr);
438 }
439
440 // Synchronously gets stats and returns them. If it times out, fails the test
441 // and returns null.
442 rtc::scoped_refptr<const webrtc::RTCStatsReport> NewGetStats() {
443 rtc::scoped_refptr<webrtc::MockRTCStatsCollectorCallback> callback(
444 new rtc::RefCountedObject<webrtc::MockRTCStatsCollectorCallback>());
445 peer_connection_->GetStats(callback);
446 EXPECT_TRUE_WAIT(callback->called(), kDefaultTimeout);
447 return callback->report();
deadbeef1dcb1642017-03-29 21:08:16 -0700448 }
449
450 int rendered_width() {
451 EXPECT_FALSE(fake_video_renderers_.empty());
452 return fake_video_renderers_.empty()
453 ? 0
454 : fake_video_renderers_.begin()->second->width();
455 }
456
457 int rendered_height() {
458 EXPECT_FALSE(fake_video_renderers_.empty());
459 return fake_video_renderers_.empty()
460 ? 0
461 : fake_video_renderers_.begin()->second->height();
462 }
463
464 double rendered_aspect_ratio() {
465 if (rendered_height() == 0) {
466 return 0.0;
467 }
468 return static_cast<double>(rendered_width()) / rendered_height();
469 }
470
471 webrtc::VideoRotation rendered_rotation() {
472 EXPECT_FALSE(fake_video_renderers_.empty());
473 return fake_video_renderers_.empty()
474 ? webrtc::kVideoRotation_0
475 : fake_video_renderers_.begin()->second->rotation();
476 }
477
478 int local_rendered_width() {
479 return local_video_renderer_ ? local_video_renderer_->width() : 0;
480 }
481
482 int local_rendered_height() {
483 return local_video_renderer_ ? local_video_renderer_->height() : 0;
484 }
485
486 double local_rendered_aspect_ratio() {
487 if (local_rendered_height() == 0) {
488 return 0.0;
489 }
490 return static_cast<double>(local_rendered_width()) /
491 local_rendered_height();
492 }
493
494 size_t number_of_remote_streams() {
495 if (!pc()) {
496 return 0;
497 }
498 return pc()->remote_streams()->count();
499 }
500
501 StreamCollectionInterface* remote_streams() const {
502 if (!pc()) {
503 ADD_FAILURE();
504 return nullptr;
505 }
506 return pc()->remote_streams();
507 }
508
509 StreamCollectionInterface* local_streams() {
510 if (!pc()) {
511 ADD_FAILURE();
512 return nullptr;
513 }
514 return pc()->local_streams();
515 }
516
517 webrtc::PeerConnectionInterface::SignalingState signaling_state() {
518 return pc()->signaling_state();
519 }
520
521 webrtc::PeerConnectionInterface::IceConnectionState ice_connection_state() {
522 return pc()->ice_connection_state();
523 }
524
Jonas Olsson7a6739e2019-01-15 16:31:55 +0100525 webrtc::PeerConnectionInterface::IceConnectionState
526 standardized_ice_connection_state() {
527 return pc()->standardized_ice_connection_state();
528 }
529
deadbeef1dcb1642017-03-29 21:08:16 -0700530 webrtc::PeerConnectionInterface::IceGatheringState ice_gathering_state() {
531 return pc()->ice_gathering_state();
532 }
533
534 // Returns a MockRtpReceiverObserver for each RtpReceiver returned by
535 // GetReceivers. They're updated automatically when a remote offer/answer
536 // from the fake signaling channel is applied, or when
537 // ResetRtpReceiverObservers below is called.
538 const std::vector<std::unique_ptr<MockRtpReceiverObserver>>&
539 rtp_receiver_observers() {
540 return rtp_receiver_observers_;
541 }
542
543 void ResetRtpReceiverObservers() {
544 rtp_receiver_observers_.clear();
Mirko Bonadeic61ce0d2017-11-21 17:04:20 +0100545 for (const rtc::scoped_refptr<RtpReceiverInterface>& receiver :
546 pc()->GetReceivers()) {
deadbeef1dcb1642017-03-29 21:08:16 -0700547 std::unique_ptr<MockRtpReceiverObserver> observer(
548 new MockRtpReceiverObserver(receiver->media_type()));
549 receiver->SetObserver(observer.get());
550 rtp_receiver_observers_.push_back(std::move(observer));
551 }
552 }
553
Steve Antonede9ca52017-10-16 13:04:27 -0700554 rtc::FakeNetworkManager* network() const {
555 return fake_network_manager_.get();
556 }
557 cricket::PortAllocator* port_allocator() const { return port_allocator_; }
558
Qingsi Wang7685e862018-06-11 20:15:46 -0700559 webrtc::FakeRtcEventLogFactory* event_log_factory() const {
560 return event_log_factory_;
561 }
562
deadbeef1dcb1642017-03-29 21:08:16 -0700563 private:
564 explicit PeerConnectionWrapper(const std::string& debug_name)
565 : debug_name_(debug_name) {}
566
Bjorn Mellem175aa2e2018-11-08 11:23:22 -0800567 bool Init(
568 const PeerConnectionFactory::Options* options,
569 const PeerConnectionInterface::RTCConfiguration* config,
570 webrtc::PeerConnectionDependencies dependencies,
571 rtc::Thread* network_thread,
572 rtc::Thread* worker_thread,
573 std::unique_ptr<webrtc::FakeRtcEventLogFactory> event_log_factory,
574 std::unique_ptr<webrtc::MediaTransportFactory> media_transport_factory) {
deadbeef1dcb1642017-03-29 21:08:16 -0700575 // There's an error in this test code if Init ends up being called twice.
576 RTC_DCHECK(!peer_connection_);
577 RTC_DCHECK(!peer_connection_factory_);
578
579 fake_network_manager_.reset(new rtc::FakeNetworkManager());
Steve Antonede9ca52017-10-16 13:04:27 -0700580 fake_network_manager_->AddInterface(kDefaultLocalAddress);
deadbeef1dcb1642017-03-29 21:08:16 -0700581
582 std::unique_ptr<cricket::PortAllocator> port_allocator(
583 new cricket::BasicPortAllocator(fake_network_manager_.get()));
Steve Antonede9ca52017-10-16 13:04:27 -0700584 port_allocator_ = port_allocator.get();
deadbeef1dcb1642017-03-29 21:08:16 -0700585 fake_audio_capture_module_ = FakeAudioCaptureModule::Create();
586 if (!fake_audio_capture_module_) {
587 return false;
588 }
deadbeef1dcb1642017-03-29 21:08:16 -0700589 rtc::Thread* const signaling_thread = rtc::Thread::Current();
Qingsi Wang7685e862018-06-11 20:15:46 -0700590
591 webrtc::PeerConnectionFactoryDependencies pc_factory_dependencies;
592 pc_factory_dependencies.network_thread = network_thread;
593 pc_factory_dependencies.worker_thread = worker_thread;
594 pc_factory_dependencies.signaling_thread = signaling_thread;
595 pc_factory_dependencies.media_engine =
596 cricket::WebRtcMediaEngineFactory::Create(
597 rtc::scoped_refptr<webrtc::AudioDeviceModule>(
598 fake_audio_capture_module_),
599 webrtc::CreateBuiltinAudioEncoderFactory(),
600 webrtc::CreateBuiltinAudioDecoderFactory(),
601 webrtc::CreateBuiltinVideoEncoderFactory(),
Qingsi Wang59844ce2018-11-01 04:45:53 +0000602 webrtc::CreateBuiltinVideoDecoderFactory(), nullptr,
Qingsi Wang7685e862018-06-11 20:15:46 -0700603 webrtc::AudioProcessingBuilder().Create());
604 pc_factory_dependencies.call_factory = webrtc::CreateCallFactory();
605 if (event_log_factory) {
606 event_log_factory_ = event_log_factory.get();
607 pc_factory_dependencies.event_log_factory = std::move(event_log_factory);
608 } else {
609 pc_factory_dependencies.event_log_factory =
610 webrtc::CreateRtcEventLogFactory();
611 }
Bjorn Mellem175aa2e2018-11-08 11:23:22 -0800612 if (media_transport_factory) {
613 pc_factory_dependencies.media_transport_factory =
614 std::move(media_transport_factory);
615 }
Qingsi Wang7685e862018-06-11 20:15:46 -0700616 peer_connection_factory_ = webrtc::CreateModularPeerConnectionFactory(
617 std::move(pc_factory_dependencies));
618
deadbeef1dcb1642017-03-29 21:08:16 -0700619 if (!peer_connection_factory_) {
620 return false;
621 }
622 if (options) {
623 peer_connection_factory_->SetOptions(*options);
624 }
Seth Hampson2f0d7022018-02-20 11:54:42 -0800625 if (config) {
626 sdp_semantics_ = config->sdp_semantics;
627 }
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700628
629 dependencies.allocator = std::move(port_allocator);
Niels Möllerf06f9232018-08-07 12:32:18 +0200630 peer_connection_ = CreatePeerConnection(config, std::move(dependencies));
deadbeef1dcb1642017-03-29 21:08:16 -0700631 return peer_connection_.get() != nullptr;
632 }
633
634 rtc::scoped_refptr<webrtc::PeerConnectionInterface> CreatePeerConnection(
deadbeef1dcb1642017-03-29 21:08:16 -0700635 const PeerConnectionInterface::RTCConfiguration* config,
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700636 webrtc::PeerConnectionDependencies dependencies) {
deadbeef1dcb1642017-03-29 21:08:16 -0700637 PeerConnectionInterface::RTCConfiguration modified_config;
638 // If |config| is null, this will result in a default configuration being
639 // used.
640 if (config) {
641 modified_config = *config;
642 }
643 // Disable resolution adaptation; we don't want it interfering with the
644 // test results.
645 // TODO(deadbeef): Do something more robust. Since we're testing for aspect
646 // ratios and not specific resolutions, is this even necessary?
647 modified_config.set_cpu_adaptation(false);
648
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700649 dependencies.observer = this;
deadbeef1dcb1642017-03-29 21:08:16 -0700650 return peer_connection_factory_->CreatePeerConnection(
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700651 modified_config, std::move(dependencies));
deadbeef1dcb1642017-03-29 21:08:16 -0700652 }
653
654 void set_signaling_message_receiver(
655 SignalingMessageReceiver* signaling_message_receiver) {
656 signaling_message_receiver_ = signaling_message_receiver;
657 }
658
659 void set_signaling_delay_ms(int delay_ms) { signaling_delay_ms_ = delay_ms; }
660
Steve Antonede9ca52017-10-16 13:04:27 -0700661 void set_signal_ice_candidates(bool signal) {
662 signal_ice_candidates_ = signal;
663 }
664
deadbeef1dcb1642017-03-29 21:08:16 -0700665 rtc::scoped_refptr<webrtc::VideoTrackInterface> CreateLocalVideoTrackInternal(
Niels Möller5c7efe72018-05-11 10:34:46 +0200666 webrtc::FakePeriodicVideoSource::Config config) {
deadbeef1dcb1642017-03-29 21:08:16 -0700667 // Set max frame rate to 10fps to reduce the risk of test flakiness.
668 // TODO(deadbeef): Do something more robust.
Niels Möller5c7efe72018-05-11 10:34:46 +0200669 config.frame_interval_ms = 100;
deadbeef1dcb1642017-03-29 21:08:16 -0700670
Niels Möller5c7efe72018-05-11 10:34:46 +0200671 video_track_sources_.emplace_back(
Niels Möller0f405822018-05-17 09:16:41 +0200672 new rtc::RefCountedObject<webrtc::FakePeriodicVideoTrackSource>(
673 config, false /* remote */));
deadbeef1dcb1642017-03-29 21:08:16 -0700674 rtc::scoped_refptr<webrtc::VideoTrackInterface> track(
Niels Möller5c7efe72018-05-11 10:34:46 +0200675 peer_connection_factory_->CreateVideoTrack(
676 rtc::CreateRandomUuid(), video_track_sources_.back()));
deadbeef1dcb1642017-03-29 21:08:16 -0700677 if (!local_video_renderer_) {
678 local_video_renderer_.reset(new webrtc::FakeVideoTrackRenderer(track));
679 }
680 return track;
681 }
682
683 void HandleIncomingOffer(const std::string& msg) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100684 RTC_LOG(LS_INFO) << debug_name_ << ": HandleIncomingOffer";
Steve Antona3a92c22017-12-07 10:27:41 -0800685 std::unique_ptr<SessionDescriptionInterface> desc =
686 webrtc::CreateSessionDescription(SdpType::kOffer, msg);
deadbeef1dcb1642017-03-29 21:08:16 -0700687 if (received_sdp_munger_) {
688 received_sdp_munger_(desc->description());
689 }
690
691 EXPECT_TRUE(SetRemoteDescription(std::move(desc)));
692 // Setting a remote description may have changed the number of receivers,
693 // so reset the receiver observers.
694 ResetRtpReceiverObservers();
Seth Hampson2f0d7022018-02-20 11:54:42 -0800695 if (remote_offer_handler_) {
696 remote_offer_handler_();
697 }
deadbeef1dcb1642017-03-29 21:08:16 -0700698 auto answer = CreateAnswer();
699 ASSERT_NE(nullptr, answer);
700 EXPECT_TRUE(SetLocalDescriptionAndSendSdpMessage(std::move(answer)));
701 }
702
703 void HandleIncomingAnswer(const std::string& msg) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100704 RTC_LOG(LS_INFO) << debug_name_ << ": HandleIncomingAnswer";
Steve Antona3a92c22017-12-07 10:27:41 -0800705 std::unique_ptr<SessionDescriptionInterface> desc =
706 webrtc::CreateSessionDescription(SdpType::kAnswer, msg);
deadbeef1dcb1642017-03-29 21:08:16 -0700707 if (received_sdp_munger_) {
708 received_sdp_munger_(desc->description());
709 }
710
711 EXPECT_TRUE(SetRemoteDescription(std::move(desc)));
712 // Set the RtpReceiverObserver after receivers are created.
713 ResetRtpReceiverObservers();
714 }
715
716 // Returns null on failure.
717 std::unique_ptr<SessionDescriptionInterface> CreateOffer() {
718 rtc::scoped_refptr<MockCreateSessionDescriptionObserver> observer(
719 new rtc::RefCountedObject<MockCreateSessionDescriptionObserver>());
720 pc()->CreateOffer(observer, offer_answer_options_);
721 return WaitForDescriptionFromObserver(observer);
722 }
723
724 // Returns null on failure.
725 std::unique_ptr<SessionDescriptionInterface> CreateAnswer() {
726 rtc::scoped_refptr<MockCreateSessionDescriptionObserver> observer(
727 new rtc::RefCountedObject<MockCreateSessionDescriptionObserver>());
728 pc()->CreateAnswer(observer, offer_answer_options_);
729 return WaitForDescriptionFromObserver(observer);
730 }
731
732 std::unique_ptr<SessionDescriptionInterface> WaitForDescriptionFromObserver(
Mirko Bonadeic61ce0d2017-11-21 17:04:20 +0100733 MockCreateSessionDescriptionObserver* observer) {
deadbeef1dcb1642017-03-29 21:08:16 -0700734 EXPECT_EQ_WAIT(true, observer->called(), kDefaultTimeout);
735 if (!observer->result()) {
736 return nullptr;
737 }
738 auto description = observer->MoveDescription();
739 if (generated_sdp_munger_) {
740 generated_sdp_munger_(description->description());
741 }
742 return description;
743 }
744
745 // Setting the local description and sending the SDP message over the fake
746 // signaling channel are combined into the same method because the SDP
747 // message needs to be sent as soon as SetLocalDescription finishes, without
748 // waiting for the observer to be called. This ensures that ICE candidates
749 // don't outrace the description.
750 bool SetLocalDescriptionAndSendSdpMessage(
751 std::unique_ptr<SessionDescriptionInterface> desc) {
752 rtc::scoped_refptr<MockSetSessionDescriptionObserver> observer(
753 new rtc::RefCountedObject<MockSetSessionDescriptionObserver>());
Mirko Bonadei675513b2017-11-09 11:09:25 +0100754 RTC_LOG(LS_INFO) << debug_name_ << ": SetLocalDescriptionAndSendSdpMessage";
Steve Antona3a92c22017-12-07 10:27:41 -0800755 SdpType type = desc->GetType();
deadbeef1dcb1642017-03-29 21:08:16 -0700756 std::string sdp;
757 EXPECT_TRUE(desc->ToString(&sdp));
758 pc()->SetLocalDescription(observer, desc.release());
Seth Hampson2f0d7022018-02-20 11:54:42 -0800759 if (sdp_semantics_ == SdpSemantics::kUnifiedPlan) {
760 RemoveUnusedVideoRenderers();
761 }
deadbeef1dcb1642017-03-29 21:08:16 -0700762 // As mentioned above, we need to send the message immediately after
763 // SetLocalDescription.
764 SendSdpMessage(type, sdp);
765 EXPECT_TRUE_WAIT(observer->called(), kDefaultTimeout);
766 return true;
767 }
768
769 bool SetRemoteDescription(std::unique_ptr<SessionDescriptionInterface> desc) {
770 rtc::scoped_refptr<MockSetSessionDescriptionObserver> observer(
771 new rtc::RefCountedObject<MockSetSessionDescriptionObserver>());
Mirko Bonadei675513b2017-11-09 11:09:25 +0100772 RTC_LOG(LS_INFO) << debug_name_ << ": SetRemoteDescription";
deadbeef1dcb1642017-03-29 21:08:16 -0700773 pc()->SetRemoteDescription(observer, desc.release());
Seth Hampson2f0d7022018-02-20 11:54:42 -0800774 if (sdp_semantics_ == SdpSemantics::kUnifiedPlan) {
775 RemoveUnusedVideoRenderers();
776 }
deadbeef1dcb1642017-03-29 21:08:16 -0700777 EXPECT_TRUE_WAIT(observer->called(), kDefaultTimeout);
778 return observer->result();
779 }
780
Seth Hampson2f0d7022018-02-20 11:54:42 -0800781 // This is a work around to remove unused fake_video_renderers from
782 // transceivers that have either stopped or are no longer receiving.
783 void RemoveUnusedVideoRenderers() {
784 auto transceivers = pc()->GetTransceivers();
785 for (auto& transceiver : transceivers) {
786 if (transceiver->receiver()->media_type() != cricket::MEDIA_TYPE_VIDEO) {
787 continue;
788 }
789 // Remove fake video renderers from any stopped transceivers.
790 if (transceiver->stopped()) {
791 auto it =
792 fake_video_renderers_.find(transceiver->receiver()->track()->id());
793 if (it != fake_video_renderers_.end()) {
794 fake_video_renderers_.erase(it);
795 }
796 }
797 // Remove fake video renderers from any transceivers that are no longer
798 // receiving.
799 if ((transceiver->current_direction() &&
800 !webrtc::RtpTransceiverDirectionHasRecv(
801 *transceiver->current_direction()))) {
802 auto it =
803 fake_video_renderers_.find(transceiver->receiver()->track()->id());
804 if (it != fake_video_renderers_.end()) {
805 fake_video_renderers_.erase(it);
806 }
807 }
808 }
809 }
810
deadbeef1dcb1642017-03-29 21:08:16 -0700811 // Simulate sending a blob of SDP with delay |signaling_delay_ms_| (0 by
812 // default).
Steve Antona3a92c22017-12-07 10:27:41 -0800813 void SendSdpMessage(SdpType type, const std::string& msg) {
deadbeef1dcb1642017-03-29 21:08:16 -0700814 if (signaling_delay_ms_ == 0) {
815 RelaySdpMessageIfReceiverExists(type, msg);
816 } else {
817 invoker_.AsyncInvokeDelayed<void>(
818 RTC_FROM_HERE, rtc::Thread::Current(),
819 rtc::Bind(&PeerConnectionWrapper::RelaySdpMessageIfReceiverExists,
820 this, type, msg),
821 signaling_delay_ms_);
822 }
823 }
824
Steve Antona3a92c22017-12-07 10:27:41 -0800825 void RelaySdpMessageIfReceiverExists(SdpType type, const std::string& msg) {
deadbeef1dcb1642017-03-29 21:08:16 -0700826 if (signaling_message_receiver_) {
827 signaling_message_receiver_->ReceiveSdpMessage(type, msg);
828 }
829 }
830
831 // Simulate trickling an ICE candidate with delay |signaling_delay_ms_| (0 by
832 // default).
833 void SendIceMessage(const std::string& sdp_mid,
834 int sdp_mline_index,
835 const std::string& msg) {
836 if (signaling_delay_ms_ == 0) {
837 RelayIceMessageIfReceiverExists(sdp_mid, sdp_mline_index, msg);
838 } else {
839 invoker_.AsyncInvokeDelayed<void>(
840 RTC_FROM_HERE, rtc::Thread::Current(),
841 rtc::Bind(&PeerConnectionWrapper::RelayIceMessageIfReceiverExists,
842 this, sdp_mid, sdp_mline_index, msg),
843 signaling_delay_ms_);
844 }
845 }
846
847 void RelayIceMessageIfReceiverExists(const std::string& sdp_mid,
848 int sdp_mline_index,
849 const std::string& msg) {
850 if (signaling_message_receiver_) {
851 signaling_message_receiver_->ReceiveIceMessage(sdp_mid, sdp_mline_index,
852 msg);
853 }
854 }
855
856 // SignalingMessageReceiver callbacks.
Steve Antona3a92c22017-12-07 10:27:41 -0800857 void ReceiveSdpMessage(SdpType type, const std::string& msg) override {
858 if (type == SdpType::kOffer) {
deadbeef1dcb1642017-03-29 21:08:16 -0700859 HandleIncomingOffer(msg);
860 } else {
861 HandleIncomingAnswer(msg);
862 }
863 }
864
865 void ReceiveIceMessage(const std::string& sdp_mid,
866 int sdp_mline_index,
867 const std::string& msg) override {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100868 RTC_LOG(LS_INFO) << debug_name_ << ": ReceiveIceMessage";
deadbeef1dcb1642017-03-29 21:08:16 -0700869 std::unique_ptr<webrtc::IceCandidateInterface> candidate(
870 webrtc::CreateIceCandidate(sdp_mid, sdp_mline_index, msg, nullptr));
871 EXPECT_TRUE(pc()->AddIceCandidate(candidate.get()));
872 }
873
874 // PeerConnectionObserver callbacks.
875 void OnSignalingChange(
876 webrtc::PeerConnectionInterface::SignalingState new_state) override {
877 EXPECT_EQ(pc()->signaling_state(), new_state);
878 }
Steve Anton15324772018-01-16 10:26:49 -0800879 void OnAddTrack(rtc::scoped_refptr<RtpReceiverInterface> receiver,
880 const std::vector<rtc::scoped_refptr<MediaStreamInterface>>&
881 streams) override {
882 if (receiver->media_type() == cricket::MEDIA_TYPE_VIDEO) {
883 rtc::scoped_refptr<VideoTrackInterface> video_track(
884 static_cast<VideoTrackInterface*>(receiver->track().get()));
885 ASSERT_TRUE(fake_video_renderers_.find(video_track->id()) ==
deadbeef1dcb1642017-03-29 21:08:16 -0700886 fake_video_renderers_.end());
Steve Anton15324772018-01-16 10:26:49 -0800887 fake_video_renderers_[video_track->id()] =
Karl Wiberg918f50c2018-07-05 11:40:33 +0200888 absl::make_unique<FakeVideoTrackRenderer>(video_track);
deadbeef1dcb1642017-03-29 21:08:16 -0700889 }
890 }
Steve Anton15324772018-01-16 10:26:49 -0800891 void OnRemoveTrack(
892 rtc::scoped_refptr<RtpReceiverInterface> receiver) override {
893 if (receiver->media_type() == cricket::MEDIA_TYPE_VIDEO) {
894 auto it = fake_video_renderers_.find(receiver->track()->id());
895 RTC_DCHECK(it != fake_video_renderers_.end());
896 fake_video_renderers_.erase(it);
897 }
898 }
deadbeef1dcb1642017-03-29 21:08:16 -0700899 void OnRenegotiationNeeded() override {}
900 void OnIceConnectionChange(
901 webrtc::PeerConnectionInterface::IceConnectionState new_state) override {
902 EXPECT_EQ(pc()->ice_connection_state(), new_state);
Steve Antonede9ca52017-10-16 13:04:27 -0700903 ice_connection_state_history_.push_back(new_state);
deadbeef1dcb1642017-03-29 21:08:16 -0700904 }
Jonas Olssonacd8ae72019-02-25 15:26:24 +0100905 void OnStandardizedIceConnectionChange(
906 webrtc::PeerConnectionInterface::IceConnectionState new_state) override {
907 standardized_ice_connection_state_history_.push_back(new_state);
908 }
Jonas Olsson635474e2018-10-18 15:58:17 +0200909 void OnConnectionChange(
910 webrtc::PeerConnectionInterface::PeerConnectionState new_state) override {
911 peer_connection_state_history_.push_back(new_state);
912 }
913
deadbeef1dcb1642017-03-29 21:08:16 -0700914 void OnIceGatheringChange(
915 webrtc::PeerConnectionInterface::IceGatheringState new_state) override {
deadbeef1dcb1642017-03-29 21:08:16 -0700916 EXPECT_EQ(pc()->ice_gathering_state(), new_state);
Steve Antonede9ca52017-10-16 13:04:27 -0700917 ice_gathering_state_history_.push_back(new_state);
deadbeef1dcb1642017-03-29 21:08:16 -0700918 }
919 void OnIceCandidate(const webrtc::IceCandidateInterface* candidate) override {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100920 RTC_LOG(LS_INFO) << debug_name_ << ": OnIceCandidate";
deadbeef1dcb1642017-03-29 21:08:16 -0700921
Qingsi Wang1dac6d82018-12-12 15:28:47 -0800922 if (remote_async_resolver_) {
923 const auto& local_candidate = candidate->candidate();
924 const auto& mdns_responder = network()->GetMdnsResponderForTesting();
925 if (local_candidate.address().IsUnresolvedIP()) {
926 RTC_DCHECK(local_candidate.type() == cricket::LOCAL_PORT_TYPE);
927 rtc::SocketAddress resolved_addr(local_candidate.address());
928 const auto resolved_ip = mdns_responder->GetMappedAddressForName(
929 local_candidate.address().hostname());
930 RTC_DCHECK(!resolved_ip.IsNil());
931 resolved_addr.SetResolvedIP(resolved_ip);
932 EXPECT_CALL(*remote_async_resolver_, GetResolvedAddress(_, _))
933 .WillOnce(DoAll(SetArgPointee<1>(resolved_addr), Return(true)));
934 EXPECT_CALL(*remote_async_resolver_, Destroy(_));
Zach Stein6fcdc2f2018-08-23 16:25:55 -0700935 }
Zach Stein6fcdc2f2018-08-23 16:25:55 -0700936 }
937
deadbeef1dcb1642017-03-29 21:08:16 -0700938 std::string ice_sdp;
Qingsi Wang1dac6d82018-12-12 15:28:47 -0800939 EXPECT_TRUE(candidate->ToString(&ice_sdp));
Steve Antonede9ca52017-10-16 13:04:27 -0700940 if (signaling_message_receiver_ == nullptr || !signal_ice_candidates_) {
deadbeef1dcb1642017-03-29 21:08:16 -0700941 // Remote party may be deleted.
942 return;
943 }
Qingsi Wang1dac6d82018-12-12 15:28:47 -0800944 SendIceMessage(candidate->sdp_mid(), candidate->sdp_mline_index(), ice_sdp);
deadbeef1dcb1642017-03-29 21:08:16 -0700945 }
946 void OnDataChannel(
947 rtc::scoped_refptr<DataChannelInterface> data_channel) override {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100948 RTC_LOG(LS_INFO) << debug_name_ << ": OnDataChannel";
deadbeef1dcb1642017-03-29 21:08:16 -0700949 data_channel_ = data_channel;
950 data_observer_.reset(new MockDataChannelObserver(data_channel));
951 }
952
deadbeef1dcb1642017-03-29 21:08:16 -0700953 std::string debug_name_;
954
955 std::unique_ptr<rtc::FakeNetworkManager> fake_network_manager_;
956
957 rtc::scoped_refptr<webrtc::PeerConnectionInterface> peer_connection_;
958 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface>
959 peer_connection_factory_;
960
Steve Antonede9ca52017-10-16 13:04:27 -0700961 cricket::PortAllocator* port_allocator_;
deadbeef1dcb1642017-03-29 21:08:16 -0700962 // Needed to keep track of number of frames sent.
963 rtc::scoped_refptr<FakeAudioCaptureModule> fake_audio_capture_module_;
964 // Needed to keep track of number of frames received.
965 std::map<std::string, std::unique_ptr<webrtc::FakeVideoTrackRenderer>>
966 fake_video_renderers_;
967 // Needed to ensure frames aren't received for removed tracks.
968 std::vector<std::unique_ptr<webrtc::FakeVideoTrackRenderer>>
969 removed_fake_video_renderers_;
deadbeef1dcb1642017-03-29 21:08:16 -0700970
971 // For remote peer communication.
972 SignalingMessageReceiver* signaling_message_receiver_ = nullptr;
973 int signaling_delay_ms_ = 0;
Steve Antonede9ca52017-10-16 13:04:27 -0700974 bool signal_ice_candidates_ = true;
deadbeef1dcb1642017-03-29 21:08:16 -0700975
Niels Möller5c7efe72018-05-11 10:34:46 +0200976 // Store references to the video sources we've created, so that we can stop
deadbeef1dcb1642017-03-29 21:08:16 -0700977 // them, if required.
Niels Möller5c7efe72018-05-11 10:34:46 +0200978 std::vector<rtc::scoped_refptr<webrtc::VideoTrackSource>>
979 video_track_sources_;
deadbeef1dcb1642017-03-29 21:08:16 -0700980 // |local_video_renderer_| attached to the first created local video track.
981 std::unique_ptr<webrtc::FakeVideoTrackRenderer> local_video_renderer_;
982
Seth Hampson2f0d7022018-02-20 11:54:42 -0800983 SdpSemantics sdp_semantics_;
deadbeef1dcb1642017-03-29 21:08:16 -0700984 PeerConnectionInterface::RTCOfferAnswerOptions offer_answer_options_;
985 std::function<void(cricket::SessionDescription*)> received_sdp_munger_;
986 std::function<void(cricket::SessionDescription*)> generated_sdp_munger_;
Seth Hampson2f0d7022018-02-20 11:54:42 -0800987 std::function<void()> remote_offer_handler_;
Qingsi Wang1dac6d82018-12-12 15:28:47 -0800988 rtc::MockAsyncResolver* remote_async_resolver_ = nullptr;
deadbeef1dcb1642017-03-29 21:08:16 -0700989 rtc::scoped_refptr<DataChannelInterface> data_channel_;
990 std::unique_ptr<MockDataChannelObserver> data_observer_;
991
992 std::vector<std::unique_ptr<MockRtpReceiverObserver>> rtp_receiver_observers_;
993
Steve Antonede9ca52017-10-16 13:04:27 -0700994 std::vector<PeerConnectionInterface::IceConnectionState>
995 ice_connection_state_history_;
Jonas Olssonacd8ae72019-02-25 15:26:24 +0100996 std::vector<PeerConnectionInterface::IceConnectionState>
997 standardized_ice_connection_state_history_;
Jonas Olsson635474e2018-10-18 15:58:17 +0200998 std::vector<PeerConnectionInterface::PeerConnectionState>
999 peer_connection_state_history_;
Steve Antonede9ca52017-10-16 13:04:27 -07001000 std::vector<PeerConnectionInterface::IceGatheringState>
1001 ice_gathering_state_history_;
deadbeef1dcb1642017-03-29 21:08:16 -07001002
Qingsi Wang7685e862018-06-11 20:15:46 -07001003 webrtc::FakeRtcEventLogFactory* event_log_factory_;
1004
deadbeef1dcb1642017-03-29 21:08:16 -07001005 rtc::AsyncInvoker invoker_;
1006
Seth Hampson2f0d7022018-02-20 11:54:42 -08001007 friend class PeerConnectionIntegrationBaseTest;
deadbeef1dcb1642017-03-29 21:08:16 -07001008};
1009
Elad Alon99c3fe52017-10-13 16:29:40 +02001010class MockRtcEventLogOutput : public webrtc::RtcEventLogOutput {
1011 public:
1012 virtual ~MockRtcEventLogOutput() = default;
1013 MOCK_CONST_METHOD0(IsActive, bool());
1014 MOCK_METHOD1(Write, bool(const std::string&));
1015};
1016
Seth Hampson2f0d7022018-02-20 11:54:42 -08001017// This helper object is used for both specifying how many audio/video frames
1018// are expected to be received for a caller/callee. It provides helper functions
1019// to specify these expectations. The object initially starts in a state of no
1020// expectations.
1021class MediaExpectations {
1022 public:
1023 enum ExpectFrames {
1024 kExpectSomeFrames,
1025 kExpectNoFrames,
1026 kNoExpectation,
1027 };
1028
1029 void ExpectBidirectionalAudioAndVideo() {
1030 ExpectBidirectionalAudio();
1031 ExpectBidirectionalVideo();
1032 }
1033
1034 void ExpectBidirectionalAudio() {
1035 CallerExpectsSomeAudio();
1036 CalleeExpectsSomeAudio();
1037 }
1038
1039 void ExpectNoAudio() {
1040 CallerExpectsNoAudio();
1041 CalleeExpectsNoAudio();
1042 }
1043
1044 void ExpectBidirectionalVideo() {
1045 CallerExpectsSomeVideo();
1046 CalleeExpectsSomeVideo();
1047 }
1048
1049 void ExpectNoVideo() {
1050 CallerExpectsNoVideo();
1051 CalleeExpectsNoVideo();
1052 }
1053
1054 void CallerExpectsSomeAudioAndVideo() {
1055 CallerExpectsSomeAudio();
1056 CallerExpectsSomeVideo();
1057 }
1058
1059 void CalleeExpectsSomeAudioAndVideo() {
1060 CalleeExpectsSomeAudio();
1061 CalleeExpectsSomeVideo();
1062 }
1063
1064 // Caller's audio functions.
1065 void CallerExpectsSomeAudio(
1066 int expected_audio_frames = kDefaultExpectedAudioFrameCount) {
1067 caller_audio_expectation_ = kExpectSomeFrames;
1068 caller_audio_frames_expected_ = expected_audio_frames;
1069 }
1070
1071 void CallerExpectsNoAudio() {
1072 caller_audio_expectation_ = kExpectNoFrames;
1073 caller_audio_frames_expected_ = 0;
1074 }
1075
1076 // Caller's video functions.
1077 void CallerExpectsSomeVideo(
1078 int expected_video_frames = kDefaultExpectedVideoFrameCount) {
1079 caller_video_expectation_ = kExpectSomeFrames;
1080 caller_video_frames_expected_ = expected_video_frames;
1081 }
1082
1083 void CallerExpectsNoVideo() {
1084 caller_video_expectation_ = kExpectNoFrames;
1085 caller_video_frames_expected_ = 0;
1086 }
1087
1088 // Callee's audio functions.
1089 void CalleeExpectsSomeAudio(
1090 int expected_audio_frames = kDefaultExpectedAudioFrameCount) {
1091 callee_audio_expectation_ = kExpectSomeFrames;
1092 callee_audio_frames_expected_ = expected_audio_frames;
1093 }
1094
1095 void CalleeExpectsNoAudio() {
1096 callee_audio_expectation_ = kExpectNoFrames;
1097 callee_audio_frames_expected_ = 0;
1098 }
1099
1100 // Callee's video functions.
1101 void CalleeExpectsSomeVideo(
1102 int expected_video_frames = kDefaultExpectedVideoFrameCount) {
1103 callee_video_expectation_ = kExpectSomeFrames;
1104 callee_video_frames_expected_ = expected_video_frames;
1105 }
1106
1107 void CalleeExpectsNoVideo() {
1108 callee_video_expectation_ = kExpectNoFrames;
1109 callee_video_frames_expected_ = 0;
1110 }
1111
1112 ExpectFrames caller_audio_expectation_ = kNoExpectation;
1113 ExpectFrames caller_video_expectation_ = kNoExpectation;
1114 ExpectFrames callee_audio_expectation_ = kNoExpectation;
1115 ExpectFrames callee_video_expectation_ = kNoExpectation;
1116 int caller_audio_frames_expected_ = 0;
1117 int caller_video_frames_expected_ = 0;
1118 int callee_audio_frames_expected_ = 0;
1119 int callee_video_frames_expected_ = 0;
1120};
1121
deadbeef1dcb1642017-03-29 21:08:16 -07001122// Tests two PeerConnections connecting to each other end-to-end, using a
1123// virtual network, fake A/V capture and fake encoder/decoders. The
1124// PeerConnections share the threads/socket servers, but use separate versions
1125// of everything else (including "PeerConnectionFactory"s).
Seth Hampson2f0d7022018-02-20 11:54:42 -08001126class PeerConnectionIntegrationBaseTest : public testing::Test {
deadbeef1dcb1642017-03-29 21:08:16 -07001127 public:
Seth Hampson2f0d7022018-02-20 11:54:42 -08001128 explicit PeerConnectionIntegrationBaseTest(SdpSemantics sdp_semantics)
1129 : sdp_semantics_(sdp_semantics),
1130 ss_(new rtc::VirtualSocketServer()),
Steve Antonede9ca52017-10-16 13:04:27 -07001131 fss_(new rtc::FirewallSocketServer(ss_.get())),
1132 network_thread_(new rtc::Thread(fss_.get())),
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001133 worker_thread_(rtc::Thread::Create()),
1134 loopback_media_transports_(network_thread_.get()) {
Sebastian Jansson8a793a02018-03-13 15:21:48 +01001135 network_thread_->SetName("PCNetworkThread", this);
1136 worker_thread_->SetName("PCWorkerThread", this);
deadbeef1dcb1642017-03-29 21:08:16 -07001137 RTC_CHECK(network_thread_->Start());
1138 RTC_CHECK(worker_thread_->Start());
Qingsi Wang7fc821d2018-07-12 12:54:53 -07001139 webrtc::metrics::Reset();
deadbeef1dcb1642017-03-29 21:08:16 -07001140 }
1141
Seth Hampson2f0d7022018-02-20 11:54:42 -08001142 ~PeerConnectionIntegrationBaseTest() {
Seth Hampsonaed71642018-06-11 07:41:32 -07001143 // The PeerConnections should deleted before the TurnCustomizers.
1144 // A TurnPort is created with a raw pointer to a TurnCustomizer. The
1145 // TurnPort has the same lifetime as the PeerConnection, so it's expected
1146 // that the TurnCustomizer outlives the life of the PeerConnection or else
1147 // when Send() is called it will hit a seg fault.
deadbeef1dcb1642017-03-29 21:08:16 -07001148 if (caller_) {
1149 caller_->set_signaling_message_receiver(nullptr);
Seth Hampsonaed71642018-06-11 07:41:32 -07001150 delete SetCallerPcWrapperAndReturnCurrent(nullptr);
deadbeef1dcb1642017-03-29 21:08:16 -07001151 }
1152 if (callee_) {
1153 callee_->set_signaling_message_receiver(nullptr);
Seth Hampsonaed71642018-06-11 07:41:32 -07001154 delete SetCalleePcWrapperAndReturnCurrent(nullptr);
deadbeef1dcb1642017-03-29 21:08:16 -07001155 }
Seth Hampsonaed71642018-06-11 07:41:32 -07001156
1157 // If turn servers were created for the test they need to be destroyed on
1158 // the network thread.
1159 network_thread()->Invoke<void>(RTC_FROM_HERE, [this] {
1160 turn_servers_.clear();
1161 turn_customizers_.clear();
1162 });
deadbeef1dcb1642017-03-29 21:08:16 -07001163 }
1164
1165 bool SignalingStateStable() {
1166 return caller_->SignalingStateStable() && callee_->SignalingStateStable();
1167 }
1168
deadbeef71452802017-05-07 17:21:01 -07001169 bool DtlsConnected() {
Alex Loiko9289eda2018-11-23 16:18:59 +00001170 // TODO(deadbeef): kIceConnectionConnected currently means both ICE and DTLS
1171 // are connected. This is an important distinction. Once we have separate
1172 // ICE and DTLS state, this check needs to use the DTLS state.
1173 return (callee()->ice_connection_state() ==
1174 webrtc::PeerConnectionInterface::kIceConnectionConnected ||
1175 callee()->ice_connection_state() ==
1176 webrtc::PeerConnectionInterface::kIceConnectionCompleted) &&
1177 (caller()->ice_connection_state() ==
1178 webrtc::PeerConnectionInterface::kIceConnectionConnected ||
1179 caller()->ice_connection_state() ==
1180 webrtc::PeerConnectionInterface::kIceConnectionCompleted);
deadbeef71452802017-05-07 17:21:01 -07001181 }
1182
Qingsi Wang7685e862018-06-11 20:15:46 -07001183 // When |event_log_factory| is null, the default implementation of the event
1184 // log factory will be used.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001185 std::unique_ptr<PeerConnectionWrapper> CreatePeerConnectionWrapper(
1186 const std::string& debug_name,
Seth Hampson2f0d7022018-02-20 11:54:42 -08001187 const PeerConnectionFactory::Options* options,
1188 const RTCConfiguration* config,
Qingsi Wang7685e862018-06-11 20:15:46 -07001189 webrtc::PeerConnectionDependencies dependencies,
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001190 std::unique_ptr<webrtc::FakeRtcEventLogFactory> event_log_factory,
1191 std::unique_ptr<webrtc::MediaTransportFactory> media_transport_factory) {
Seth Hampson2f0d7022018-02-20 11:54:42 -08001192 RTCConfiguration modified_config;
1193 if (config) {
1194 modified_config = *config;
1195 }
Steve Anton3acffc32018-04-12 17:21:03 -07001196 modified_config.sdp_semantics = sdp_semantics_;
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001197 if (!dependencies.cert_generator) {
1198 dependencies.cert_generator =
Karl Wiberg918f50c2018-07-05 11:40:33 +02001199 absl::make_unique<FakeRTCCertificateGenerator>();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001200 }
1201 std::unique_ptr<PeerConnectionWrapper> client(
1202 new PeerConnectionWrapper(debug_name));
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001203
Niels Möllerf06f9232018-08-07 12:32:18 +02001204 if (!client->Init(options, &modified_config, std::move(dependencies),
1205 network_thread_.get(), worker_thread_.get(),
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001206 std::move(event_log_factory),
1207 std::move(media_transport_factory))) {
Seth Hampson2f0d7022018-02-20 11:54:42 -08001208 return nullptr;
1209 }
1210 return client;
1211 }
1212
Qingsi Wang7685e862018-06-11 20:15:46 -07001213 std::unique_ptr<PeerConnectionWrapper>
1214 CreatePeerConnectionWrapperWithFakeRtcEventLog(
1215 const std::string& debug_name,
Qingsi Wang7685e862018-06-11 20:15:46 -07001216 const PeerConnectionFactory::Options* options,
1217 const RTCConfiguration* config,
1218 webrtc::PeerConnectionDependencies dependencies) {
1219 std::unique_ptr<webrtc::FakeRtcEventLogFactory> event_log_factory(
1220 new webrtc::FakeRtcEventLogFactory(rtc::Thread::Current()));
Niels Möllerf06f9232018-08-07 12:32:18 +02001221 return CreatePeerConnectionWrapper(debug_name, options, config,
Qingsi Wang7685e862018-06-11 20:15:46 -07001222 std::move(dependencies),
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001223 std::move(event_log_factory),
1224 /*media_transport_factory=*/nullptr);
Qingsi Wang7685e862018-06-11 20:15:46 -07001225 }
1226
deadbeef1dcb1642017-03-29 21:08:16 -07001227 bool CreatePeerConnectionWrappers() {
1228 return CreatePeerConnectionWrappersWithConfig(
1229 PeerConnectionInterface::RTCConfiguration(),
1230 PeerConnectionInterface::RTCConfiguration());
1231 }
1232
Steve Anton3acffc32018-04-12 17:21:03 -07001233 bool CreatePeerConnectionWrappersWithSdpSemantics(
1234 SdpSemantics caller_semantics,
1235 SdpSemantics callee_semantics) {
1236 // Can't specify the sdp_semantics in the passed-in configuration since it
1237 // will be overwritten by CreatePeerConnectionWrapper with whatever is
1238 // stored in sdp_semantics_. So get around this by modifying the instance
1239 // variable before calling CreatePeerConnectionWrapper for the caller and
1240 // callee PeerConnections.
1241 SdpSemantics original_semantics = sdp_semantics_;
1242 sdp_semantics_ = caller_semantics;
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001243 caller_ = CreatePeerConnectionWrapper(
Niels Möllerf06f9232018-08-07 12:32:18 +02001244 "Caller", nullptr, nullptr, webrtc::PeerConnectionDependencies(nullptr),
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001245 nullptr, /*media_transport_factory=*/nullptr);
Steve Anton3acffc32018-04-12 17:21:03 -07001246 sdp_semantics_ = callee_semantics;
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001247 callee_ = CreatePeerConnectionWrapper(
Niels Möllerf06f9232018-08-07 12:32:18 +02001248 "Callee", nullptr, nullptr, webrtc::PeerConnectionDependencies(nullptr),
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001249 nullptr, /*media_transport_factory=*/nullptr);
Steve Anton3acffc32018-04-12 17:21:03 -07001250 sdp_semantics_ = original_semantics;
1251 return caller_ && callee_;
1252 }
1253
deadbeef1dcb1642017-03-29 21:08:16 -07001254 bool CreatePeerConnectionWrappersWithConfig(
1255 const PeerConnectionInterface::RTCConfiguration& caller_config,
1256 const PeerConnectionInterface::RTCConfiguration& callee_config) {
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001257 caller_ = CreatePeerConnectionWrapper(
Niels Möllerf06f9232018-08-07 12:32:18 +02001258 "Caller", nullptr, &caller_config,
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001259 webrtc::PeerConnectionDependencies(nullptr), nullptr,
1260 /*media_transport_factory=*/nullptr);
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001261 callee_ = CreatePeerConnectionWrapper(
Niels Möllerf06f9232018-08-07 12:32:18 +02001262 "Callee", nullptr, &callee_config,
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001263 webrtc::PeerConnectionDependencies(nullptr), nullptr,
1264 /*media_transport_factory=*/nullptr);
1265 return caller_ && callee_;
1266 }
1267
1268 bool CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
1269 const PeerConnectionInterface::RTCConfiguration& caller_config,
1270 const PeerConnectionInterface::RTCConfiguration& callee_config,
1271 std::unique_ptr<webrtc::MediaTransportFactory> caller_factory,
1272 std::unique_ptr<webrtc::MediaTransportFactory> callee_factory) {
1273 caller_ =
1274 CreatePeerConnectionWrapper("Caller", nullptr, &caller_config,
1275 webrtc::PeerConnectionDependencies(nullptr),
1276 nullptr, std::move(caller_factory));
1277 callee_ =
1278 CreatePeerConnectionWrapper("Callee", nullptr, &callee_config,
1279 webrtc::PeerConnectionDependencies(nullptr),
1280 nullptr, std::move(callee_factory));
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001281 return caller_ && callee_;
1282 }
1283
1284 bool CreatePeerConnectionWrappersWithConfigAndDeps(
1285 const PeerConnectionInterface::RTCConfiguration& caller_config,
1286 webrtc::PeerConnectionDependencies caller_dependencies,
1287 const PeerConnectionInterface::RTCConfiguration& callee_config,
1288 webrtc::PeerConnectionDependencies callee_dependencies) {
1289 caller_ =
Niels Möllerf06f9232018-08-07 12:32:18 +02001290 CreatePeerConnectionWrapper("Caller", nullptr, &caller_config,
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001291 std::move(caller_dependencies), nullptr,
1292 /*media_transport_factory=*/nullptr);
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001293 callee_ =
Niels Möllerf06f9232018-08-07 12:32:18 +02001294 CreatePeerConnectionWrapper("Callee", nullptr, &callee_config,
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001295 std::move(callee_dependencies), nullptr,
1296 /*media_transport_factory=*/nullptr);
deadbeef1dcb1642017-03-29 21:08:16 -07001297 return caller_ && callee_;
1298 }
1299
1300 bool CreatePeerConnectionWrappersWithOptions(
1301 const PeerConnectionFactory::Options& caller_options,
1302 const PeerConnectionFactory::Options& callee_options) {
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001303 caller_ = CreatePeerConnectionWrapper(
Niels Möllerf06f9232018-08-07 12:32:18 +02001304 "Caller", &caller_options, nullptr,
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001305 webrtc::PeerConnectionDependencies(nullptr), nullptr,
1306 /*media_transport_factory=*/nullptr);
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001307 callee_ = CreatePeerConnectionWrapper(
Niels Möllerf06f9232018-08-07 12:32:18 +02001308 "Callee", &callee_options, nullptr,
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001309 webrtc::PeerConnectionDependencies(nullptr), nullptr,
1310 /*media_transport_factory=*/nullptr);
Qingsi Wang7685e862018-06-11 20:15:46 -07001311 return caller_ && callee_;
1312 }
1313
1314 bool CreatePeerConnectionWrappersWithFakeRtcEventLog() {
1315 PeerConnectionInterface::RTCConfiguration default_config;
1316 caller_ = CreatePeerConnectionWrapperWithFakeRtcEventLog(
Niels Möllerf06f9232018-08-07 12:32:18 +02001317 "Caller", nullptr, &default_config,
Qingsi Wang7685e862018-06-11 20:15:46 -07001318 webrtc::PeerConnectionDependencies(nullptr));
1319 callee_ = CreatePeerConnectionWrapperWithFakeRtcEventLog(
Niels Möllerf06f9232018-08-07 12:32:18 +02001320 "Callee", nullptr, &default_config,
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001321 webrtc::PeerConnectionDependencies(nullptr));
deadbeef1dcb1642017-03-29 21:08:16 -07001322 return caller_ && callee_;
1323 }
1324
Seth Hampson2f0d7022018-02-20 11:54:42 -08001325 std::unique_ptr<PeerConnectionWrapper>
1326 CreatePeerConnectionWrapperWithAlternateKey() {
deadbeef1dcb1642017-03-29 21:08:16 -07001327 std::unique_ptr<FakeRTCCertificateGenerator> cert_generator(
1328 new FakeRTCCertificateGenerator());
1329 cert_generator->use_alternate_key();
1330
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001331 webrtc::PeerConnectionDependencies dependencies(nullptr);
1332 dependencies.cert_generator = std::move(cert_generator);
Niels Möllerf06f9232018-08-07 12:32:18 +02001333 return CreatePeerConnectionWrapper("New Peer", nullptr, nullptr,
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001334 std::move(dependencies), nullptr,
1335 /*media_transport_factory=*/nullptr);
deadbeef1dcb1642017-03-29 21:08:16 -07001336 }
1337
Seth Hampsonaed71642018-06-11 07:41:32 -07001338 cricket::TestTurnServer* CreateTurnServer(
1339 rtc::SocketAddress internal_address,
1340 rtc::SocketAddress external_address,
1341 cricket::ProtocolType type = cricket::ProtocolType::PROTO_UDP,
1342 const std::string& common_name = "test turn server") {
1343 rtc::Thread* thread = network_thread();
1344 std::unique_ptr<cricket::TestTurnServer> turn_server =
1345 network_thread()->Invoke<std::unique_ptr<cricket::TestTurnServer>>(
1346 RTC_FROM_HERE,
1347 [thread, internal_address, external_address, type, common_name] {
Karl Wiberg918f50c2018-07-05 11:40:33 +02001348 return absl::make_unique<cricket::TestTurnServer>(
Seth Hampsonaed71642018-06-11 07:41:32 -07001349 thread, internal_address, external_address, type,
1350 /*ignore_bad_certs=*/true, common_name);
1351 });
1352 turn_servers_.push_back(std::move(turn_server));
1353 // Interactions with the turn server should be done on the network thread.
1354 return turn_servers_.back().get();
1355 }
1356
1357 cricket::TestTurnCustomizer* CreateTurnCustomizer() {
1358 std::unique_ptr<cricket::TestTurnCustomizer> turn_customizer =
1359 network_thread()->Invoke<std::unique_ptr<cricket::TestTurnCustomizer>>(
1360 RTC_FROM_HERE,
Karl Wiberg918f50c2018-07-05 11:40:33 +02001361 [] { return absl::make_unique<cricket::TestTurnCustomizer>(); });
Seth Hampsonaed71642018-06-11 07:41:32 -07001362 turn_customizers_.push_back(std::move(turn_customizer));
1363 // Interactions with the turn customizer should be done on the network
1364 // thread.
1365 return turn_customizers_.back().get();
1366 }
1367
1368 // Checks that the function counters for a TestTurnCustomizer are greater than
1369 // 0.
1370 void ExpectTurnCustomizerCountersIncremented(
1371 cricket::TestTurnCustomizer* turn_customizer) {
1372 unsigned int allow_channel_data_counter =
1373 network_thread()->Invoke<unsigned int>(
1374 RTC_FROM_HERE, [turn_customizer] {
1375 return turn_customizer->allow_channel_data_cnt_;
1376 });
1377 EXPECT_GT(allow_channel_data_counter, 0u);
1378 unsigned int modify_counter = network_thread()->Invoke<unsigned int>(
1379 RTC_FROM_HERE,
1380 [turn_customizer] { return turn_customizer->modify_cnt_; });
1381 EXPECT_GT(modify_counter, 0u);
1382 }
1383
deadbeef1dcb1642017-03-29 21:08:16 -07001384 // Once called, SDP blobs and ICE candidates will be automatically signaled
1385 // between PeerConnections.
1386 void ConnectFakeSignaling() {
1387 caller_->set_signaling_message_receiver(callee_.get());
1388 callee_->set_signaling_message_receiver(caller_.get());
1389 }
1390
Steve Antonede9ca52017-10-16 13:04:27 -07001391 // Once called, SDP blobs will be automatically signaled between
1392 // PeerConnections. Note that ICE candidates will not be signaled unless they
1393 // are in the exchanged SDP blobs.
1394 void ConnectFakeSignalingForSdpOnly() {
1395 ConnectFakeSignaling();
1396 SetSignalIceCandidates(false);
1397 }
1398
deadbeef1dcb1642017-03-29 21:08:16 -07001399 void SetSignalingDelayMs(int delay_ms) {
1400 caller_->set_signaling_delay_ms(delay_ms);
1401 callee_->set_signaling_delay_ms(delay_ms);
1402 }
1403
Steve Antonede9ca52017-10-16 13:04:27 -07001404 void SetSignalIceCandidates(bool signal) {
1405 caller_->set_signal_ice_candidates(signal);
1406 callee_->set_signal_ice_candidates(signal);
1407 }
1408
deadbeef1dcb1642017-03-29 21:08:16 -07001409 // Messages may get lost on the unreliable DataChannel, so we send multiple
1410 // times to avoid test flakiness.
1411 void SendRtpDataWithRetries(webrtc::DataChannelInterface* dc,
1412 const std::string& data,
1413 int retries) {
1414 for (int i = 0; i < retries; ++i) {
1415 dc->Send(DataBuffer(data));
1416 }
1417 }
1418
1419 rtc::Thread* network_thread() { return network_thread_.get(); }
1420
1421 rtc::VirtualSocketServer* virtual_socket_server() { return ss_.get(); }
1422
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001423 webrtc::MediaTransportPair* loopback_media_transports() {
1424 return &loopback_media_transports_;
1425 }
1426
deadbeef1dcb1642017-03-29 21:08:16 -07001427 PeerConnectionWrapper* caller() { return caller_.get(); }
1428
1429 // Set the |caller_| to the |wrapper| passed in and return the
1430 // original |caller_|.
1431 PeerConnectionWrapper* SetCallerPcWrapperAndReturnCurrent(
1432 PeerConnectionWrapper* wrapper) {
1433 PeerConnectionWrapper* old = caller_.release();
1434 caller_.reset(wrapper);
1435 return old;
1436 }
1437
1438 PeerConnectionWrapper* callee() { return callee_.get(); }
1439
1440 // Set the |callee_| to the |wrapper| passed in and return the
1441 // original |callee_|.
1442 PeerConnectionWrapper* SetCalleePcWrapperAndReturnCurrent(
1443 PeerConnectionWrapper* wrapper) {
1444 PeerConnectionWrapper* old = callee_.release();
1445 callee_.reset(wrapper);
1446 return old;
1447 }
1448
Qingsi Wang1dac6d82018-12-12 15:28:47 -08001449 void SetPortAllocatorFlags(uint32_t caller_flags, uint32_t callee_flags) {
1450 network_thread()->Invoke<void>(
1451 RTC_FROM_HERE, rtc::Bind(&cricket::PortAllocator::set_flags,
1452 caller()->port_allocator(), caller_flags));
1453 network_thread()->Invoke<void>(
1454 RTC_FROM_HERE, rtc::Bind(&cricket::PortAllocator::set_flags,
1455 callee()->port_allocator(), callee_flags));
1456 }
1457
Steve Antonede9ca52017-10-16 13:04:27 -07001458 rtc::FirewallSocketServer* firewall() const { return fss_.get(); }
1459
Seth Hampson2f0d7022018-02-20 11:54:42 -08001460 // Expects the provided number of new frames to be received within
1461 // kMaxWaitForFramesMs. The new expected frames are specified in
1462 // |media_expectations|. Returns false if any of the expectations were
1463 // not met.
1464 bool ExpectNewFrames(const MediaExpectations& media_expectations) {
1465 // First initialize the expected frame counts based upon the current
1466 // frame count.
1467 int total_caller_audio_frames_expected = caller()->audio_frames_received();
1468 if (media_expectations.caller_audio_expectation_ ==
1469 MediaExpectations::kExpectSomeFrames) {
1470 total_caller_audio_frames_expected +=
1471 media_expectations.caller_audio_frames_expected_;
1472 }
1473 int total_caller_video_frames_expected =
deadbeef1dcb1642017-03-29 21:08:16 -07001474 caller()->min_video_frames_received_per_track();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001475 if (media_expectations.caller_video_expectation_ ==
1476 MediaExpectations::kExpectSomeFrames) {
1477 total_caller_video_frames_expected +=
1478 media_expectations.caller_video_frames_expected_;
1479 }
1480 int total_callee_audio_frames_expected = callee()->audio_frames_received();
1481 if (media_expectations.callee_audio_expectation_ ==
1482 MediaExpectations::kExpectSomeFrames) {
1483 total_callee_audio_frames_expected +=
1484 media_expectations.callee_audio_frames_expected_;
1485 }
1486 int total_callee_video_frames_expected =
deadbeef1dcb1642017-03-29 21:08:16 -07001487 callee()->min_video_frames_received_per_track();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001488 if (media_expectations.callee_video_expectation_ ==
1489 MediaExpectations::kExpectSomeFrames) {
1490 total_callee_video_frames_expected +=
1491 media_expectations.callee_video_frames_expected_;
1492 }
deadbeef1dcb1642017-03-29 21:08:16 -07001493
Seth Hampson2f0d7022018-02-20 11:54:42 -08001494 // Wait for the expected frames.
deadbeef1dcb1642017-03-29 21:08:16 -07001495 EXPECT_TRUE_WAIT(caller()->audio_frames_received() >=
Seth Hampson2f0d7022018-02-20 11:54:42 -08001496 total_caller_audio_frames_expected &&
deadbeef1dcb1642017-03-29 21:08:16 -07001497 caller()->min_video_frames_received_per_track() >=
Seth Hampson2f0d7022018-02-20 11:54:42 -08001498 total_caller_video_frames_expected &&
deadbeef1dcb1642017-03-29 21:08:16 -07001499 callee()->audio_frames_received() >=
Seth Hampson2f0d7022018-02-20 11:54:42 -08001500 total_callee_audio_frames_expected &&
deadbeef1dcb1642017-03-29 21:08:16 -07001501 callee()->min_video_frames_received_per_track() >=
Seth Hampson2f0d7022018-02-20 11:54:42 -08001502 total_callee_video_frames_expected,
1503 kMaxWaitForFramesMs);
1504 bool expectations_correct =
1505 caller()->audio_frames_received() >=
1506 total_caller_audio_frames_expected &&
1507 caller()->min_video_frames_received_per_track() >=
1508 total_caller_video_frames_expected &&
1509 callee()->audio_frames_received() >=
1510 total_callee_audio_frames_expected &&
1511 callee()->min_video_frames_received_per_track() >=
1512 total_callee_video_frames_expected;
deadbeef1dcb1642017-03-29 21:08:16 -07001513
Seth Hampson2f0d7022018-02-20 11:54:42 -08001514 // After the combined wait, print out a more detailed message upon
1515 // failure.
deadbeef1dcb1642017-03-29 21:08:16 -07001516 EXPECT_GE(caller()->audio_frames_received(),
Seth Hampson2f0d7022018-02-20 11:54:42 -08001517 total_caller_audio_frames_expected);
deadbeef1dcb1642017-03-29 21:08:16 -07001518 EXPECT_GE(caller()->min_video_frames_received_per_track(),
Seth Hampson2f0d7022018-02-20 11:54:42 -08001519 total_caller_video_frames_expected);
deadbeef1dcb1642017-03-29 21:08:16 -07001520 EXPECT_GE(callee()->audio_frames_received(),
Seth Hampson2f0d7022018-02-20 11:54:42 -08001521 total_callee_audio_frames_expected);
deadbeef1dcb1642017-03-29 21:08:16 -07001522 EXPECT_GE(callee()->min_video_frames_received_per_track(),
Seth Hampson2f0d7022018-02-20 11:54:42 -08001523 total_callee_video_frames_expected);
1524
1525 // We want to make sure nothing unexpected was received.
1526 if (media_expectations.caller_audio_expectation_ ==
1527 MediaExpectations::kExpectNoFrames) {
1528 EXPECT_EQ(caller()->audio_frames_received(),
1529 total_caller_audio_frames_expected);
1530 if (caller()->audio_frames_received() !=
1531 total_caller_audio_frames_expected) {
1532 expectations_correct = false;
1533 }
1534 }
1535 if (media_expectations.caller_video_expectation_ ==
1536 MediaExpectations::kExpectNoFrames) {
1537 EXPECT_EQ(caller()->min_video_frames_received_per_track(),
1538 total_caller_video_frames_expected);
1539 if (caller()->min_video_frames_received_per_track() !=
1540 total_caller_video_frames_expected) {
1541 expectations_correct = false;
1542 }
1543 }
1544 if (media_expectations.callee_audio_expectation_ ==
1545 MediaExpectations::kExpectNoFrames) {
1546 EXPECT_EQ(callee()->audio_frames_received(),
1547 total_callee_audio_frames_expected);
1548 if (callee()->audio_frames_received() !=
1549 total_callee_audio_frames_expected) {
1550 expectations_correct = false;
1551 }
1552 }
1553 if (media_expectations.callee_video_expectation_ ==
1554 MediaExpectations::kExpectNoFrames) {
1555 EXPECT_EQ(callee()->min_video_frames_received_per_track(),
1556 total_callee_video_frames_expected);
1557 if (callee()->min_video_frames_received_per_track() !=
1558 total_callee_video_frames_expected) {
1559 expectations_correct = false;
1560 }
1561 }
1562 return expectations_correct;
deadbeef1dcb1642017-03-29 21:08:16 -07001563 }
1564
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07001565 void TestNegotiatedCipherSuite(
1566 const PeerConnectionFactory::Options& caller_options,
1567 const PeerConnectionFactory::Options& callee_options,
1568 int expected_cipher_suite) {
deadbeef1dcb1642017-03-29 21:08:16 -07001569 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(caller_options,
1570 callee_options));
deadbeef1dcb1642017-03-29 21:08:16 -07001571 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001572 caller()->AddAudioVideoTracks();
1573 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001574 caller()->CreateAndSetAndSignalOffer();
Qingsi Wang7fc821d2018-07-12 12:54:53 -07001575 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07001576 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(expected_cipher_suite),
deadbeefd8ad7882017-04-18 16:01:17 -07001577 caller()->OldGetStats()->SrtpCipher(), kDefaultTimeout);
Qingsi Wang7fc821d2018-07-12 12:54:53 -07001578 // TODO(bugs.webrtc.org/9456): Fix it.
Alex Loiko9289eda2018-11-23 16:18:59 +00001579 EXPECT_EQ(1, webrtc::metrics::NumEvents(
Qingsi Wang7fc821d2018-07-12 12:54:53 -07001580 "WebRTC.PeerConnection.SrtpCryptoSuite.Audio",
1581 expected_cipher_suite));
deadbeef1dcb1642017-03-29 21:08:16 -07001582 }
1583
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07001584 void TestGcmNegotiationUsesCipherSuite(bool local_gcm_enabled,
1585 bool remote_gcm_enabled,
1586 int expected_cipher_suite) {
1587 PeerConnectionFactory::Options caller_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07001588 caller_options.crypto_options.srtp.enable_gcm_crypto_suites =
1589 local_gcm_enabled;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07001590 PeerConnectionFactory::Options callee_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07001591 callee_options.crypto_options.srtp.enable_gcm_crypto_suites =
1592 remote_gcm_enabled;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07001593 TestNegotiatedCipherSuite(caller_options, callee_options,
1594 expected_cipher_suite);
1595 }
1596
Seth Hampson2f0d7022018-02-20 11:54:42 -08001597 protected:
Steve Anton3acffc32018-04-12 17:21:03 -07001598 SdpSemantics sdp_semantics_;
Seth Hampson2f0d7022018-02-20 11:54:42 -08001599
deadbeef1dcb1642017-03-29 21:08:16 -07001600 private:
1601 // |ss_| is used by |network_thread_| so it must be destroyed later.
deadbeef1dcb1642017-03-29 21:08:16 -07001602 std::unique_ptr<rtc::VirtualSocketServer> ss_;
Steve Antonede9ca52017-10-16 13:04:27 -07001603 std::unique_ptr<rtc::FirewallSocketServer> fss_;
deadbeef1dcb1642017-03-29 21:08:16 -07001604 // |network_thread_| and |worker_thread_| are used by both
1605 // |caller_| and |callee_| so they must be destroyed
1606 // later.
1607 std::unique_ptr<rtc::Thread> network_thread_;
1608 std::unique_ptr<rtc::Thread> worker_thread_;
Seth Hampsonaed71642018-06-11 07:41:32 -07001609 // The turn servers and turn customizers should be accessed & deleted on the
1610 // network thread to avoid a race with the socket read/write that occurs
1611 // on the network thread.
1612 std::vector<std::unique_ptr<cricket::TestTurnServer>> turn_servers_;
1613 std::vector<std::unique_ptr<cricket::TestTurnCustomizer>> turn_customizers_;
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001614 webrtc::MediaTransportPair loopback_media_transports_;
deadbeef1dcb1642017-03-29 21:08:16 -07001615 std::unique_ptr<PeerConnectionWrapper> caller_;
1616 std::unique_ptr<PeerConnectionWrapper> callee_;
1617};
1618
Seth Hampson2f0d7022018-02-20 11:54:42 -08001619class PeerConnectionIntegrationTest
1620 : public PeerConnectionIntegrationBaseTest,
1621 public ::testing::WithParamInterface<SdpSemantics> {
1622 protected:
1623 PeerConnectionIntegrationTest()
1624 : PeerConnectionIntegrationBaseTest(GetParam()) {}
1625};
1626
1627class PeerConnectionIntegrationTestPlanB
1628 : public PeerConnectionIntegrationBaseTest {
1629 protected:
1630 PeerConnectionIntegrationTestPlanB()
1631 : PeerConnectionIntegrationBaseTest(SdpSemantics::kPlanB) {}
1632};
1633
1634class PeerConnectionIntegrationTestUnifiedPlan
1635 : public PeerConnectionIntegrationBaseTest {
1636 protected:
1637 PeerConnectionIntegrationTestUnifiedPlan()
1638 : PeerConnectionIntegrationBaseTest(SdpSemantics::kUnifiedPlan) {}
1639};
1640
deadbeef1dcb1642017-03-29 21:08:16 -07001641// Test the OnFirstPacketReceived callback from audio/video RtpReceivers. This
1642// includes testing that the callback is invoked if an observer is connected
1643// after the first packet has already been received.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001644TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07001645 RtpReceiverObserverOnFirstPacketReceived) {
1646 ASSERT_TRUE(CreatePeerConnectionWrappers());
1647 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001648 caller()->AddAudioVideoTracks();
1649 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001650 // Start offer/answer exchange and wait for it to complete.
1651 caller()->CreateAndSetAndSignalOffer();
1652 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1653 // Should be one receiver each for audio/video.
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02001654 EXPECT_EQ(2U, caller()->rtp_receiver_observers().size());
1655 EXPECT_EQ(2U, callee()->rtp_receiver_observers().size());
deadbeef1dcb1642017-03-29 21:08:16 -07001656 // Wait for all "first packet received" callbacks to be fired.
1657 EXPECT_TRUE_WAIT(
Steve Anton64b626b2019-01-28 17:25:26 -08001658 absl::c_all_of(caller()->rtp_receiver_observers(),
1659 [](const std::unique_ptr<MockRtpReceiverObserver>& o) {
1660 return o->first_packet_received();
1661 }),
deadbeef1dcb1642017-03-29 21:08:16 -07001662 kMaxWaitForFramesMs);
1663 EXPECT_TRUE_WAIT(
Steve Anton64b626b2019-01-28 17:25:26 -08001664 absl::c_all_of(callee()->rtp_receiver_observers(),
1665 [](const std::unique_ptr<MockRtpReceiverObserver>& o) {
1666 return o->first_packet_received();
1667 }),
deadbeef1dcb1642017-03-29 21:08:16 -07001668 kMaxWaitForFramesMs);
1669 // If new observers are set after the first packet was already received, the
1670 // callback should still be invoked.
1671 caller()->ResetRtpReceiverObservers();
1672 callee()->ResetRtpReceiverObservers();
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02001673 EXPECT_EQ(2U, caller()->rtp_receiver_observers().size());
1674 EXPECT_EQ(2U, callee()->rtp_receiver_observers().size());
deadbeef1dcb1642017-03-29 21:08:16 -07001675 EXPECT_TRUE(
Steve Anton64b626b2019-01-28 17:25:26 -08001676 absl::c_all_of(caller()->rtp_receiver_observers(),
1677 [](const std::unique_ptr<MockRtpReceiverObserver>& o) {
1678 return o->first_packet_received();
1679 }));
deadbeef1dcb1642017-03-29 21:08:16 -07001680 EXPECT_TRUE(
Steve Anton64b626b2019-01-28 17:25:26 -08001681 absl::c_all_of(callee()->rtp_receiver_observers(),
1682 [](const std::unique_ptr<MockRtpReceiverObserver>& o) {
1683 return o->first_packet_received();
1684 }));
deadbeef1dcb1642017-03-29 21:08:16 -07001685}
1686
1687class DummyDtmfObserver : public DtmfSenderObserverInterface {
1688 public:
1689 DummyDtmfObserver() : completed_(false) {}
1690
1691 // Implements DtmfSenderObserverInterface.
1692 void OnToneChange(const std::string& tone) override {
1693 tones_.push_back(tone);
1694 if (tone.empty()) {
1695 completed_ = true;
1696 }
1697 }
1698
1699 const std::vector<std::string>& tones() const { return tones_; }
1700 bool completed() const { return completed_; }
1701
1702 private:
1703 bool completed_;
1704 std::vector<std::string> tones_;
1705};
1706
1707// Assumes |sender| already has an audio track added and the offer/answer
1708// exchange is done.
1709void TestDtmfFromSenderToReceiver(PeerConnectionWrapper* sender,
1710 PeerConnectionWrapper* receiver) {
Steve Anton15324772018-01-16 10:26:49 -08001711 // We should be able to get a DTMF sender from the local sender.
1712 rtc::scoped_refptr<DtmfSenderInterface> dtmf_sender =
1713 sender->pc()->GetSenders().at(0)->GetDtmfSender();
1714 ASSERT_TRUE(dtmf_sender);
deadbeef1dcb1642017-03-29 21:08:16 -07001715 DummyDtmfObserver observer;
deadbeef1dcb1642017-03-29 21:08:16 -07001716 dtmf_sender->RegisterObserver(&observer);
1717
1718 // Test the DtmfSender object just created.
1719 EXPECT_TRUE(dtmf_sender->CanInsertDtmf());
1720 EXPECT_TRUE(dtmf_sender->InsertDtmf("1a", 100, 50));
1721
1722 EXPECT_TRUE_WAIT(observer.completed(), kDefaultTimeout);
1723 std::vector<std::string> tones = {"1", "a", ""};
1724 EXPECT_EQ(tones, observer.tones());
1725 dtmf_sender->UnregisterObserver();
1726 // TODO(deadbeef): Verify the tones were actually received end-to-end.
1727}
1728
1729// Verifies the DtmfSenderObserver callbacks for a DtmfSender (one in each
1730// direction).
Seth Hampson2f0d7022018-02-20 11:54:42 -08001731TEST_P(PeerConnectionIntegrationTest, DtmfSenderObserver) {
deadbeef1dcb1642017-03-29 21:08:16 -07001732 ASSERT_TRUE(CreatePeerConnectionWrappers());
1733 ConnectFakeSignaling();
1734 // Only need audio for DTMF.
Steve Anton15324772018-01-16 10:26:49 -08001735 caller()->AddAudioTrack();
1736 callee()->AddAudioTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07001737 caller()->CreateAndSetAndSignalOffer();
1738 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
deadbeef71452802017-05-07 17:21:01 -07001739 // DTLS must finish before the DTMF sender can be used reliably.
1740 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07001741 TestDtmfFromSenderToReceiver(caller(), callee());
1742 TestDtmfFromSenderToReceiver(callee(), caller());
1743}
1744
1745// Basic end-to-end test, verifying media can be encoded/transmitted/decoded
1746// between two connections, using DTLS-SRTP.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001747TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithDtls) {
deadbeef1dcb1642017-03-29 21:08:16 -07001748 ASSERT_TRUE(CreatePeerConnectionWrappers());
1749 ConnectFakeSignaling();
Harald Alvestrand194939b2018-01-24 16:04:13 +01001750
deadbeef1dcb1642017-03-29 21:08:16 -07001751 // Do normal offer/answer and wait for some frames to be received in each
1752 // direction.
Steve Anton15324772018-01-16 10:26:49 -08001753 caller()->AddAudioVideoTracks();
1754 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001755 caller()->CreateAndSetAndSignalOffer();
1756 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001757 MediaExpectations media_expectations;
1758 media_expectations.ExpectBidirectionalAudioAndVideo();
1759 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Qingsi Wang7fc821d2018-07-12 12:54:53 -07001760 EXPECT_LE(2, webrtc::metrics::NumEvents("WebRTC.PeerConnection.KeyProtocol",
1761 webrtc::kEnumCounterKeyProtocolDtls));
1762 EXPECT_EQ(0, webrtc::metrics::NumEvents("WebRTC.PeerConnection.KeyProtocol",
1763 webrtc::kEnumCounterKeyProtocolSdes));
deadbeef1dcb1642017-03-29 21:08:16 -07001764}
1765
1766// Uses SDES instead of DTLS for key agreement.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001767TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithSdes) {
deadbeef1dcb1642017-03-29 21:08:16 -07001768 PeerConnectionInterface::RTCConfiguration sdes_config;
1769 sdes_config.enable_dtls_srtp.emplace(false);
1770 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(sdes_config, sdes_config));
1771 ConnectFakeSignaling();
1772
1773 // Do normal offer/answer and wait for some frames to be received in each
1774 // direction.
Steve Anton15324772018-01-16 10:26:49 -08001775 caller()->AddAudioVideoTracks();
1776 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001777 caller()->CreateAndSetAndSignalOffer();
1778 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001779 MediaExpectations media_expectations;
1780 media_expectations.ExpectBidirectionalAudioAndVideo();
1781 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Qingsi Wang7fc821d2018-07-12 12:54:53 -07001782 EXPECT_LE(2, webrtc::metrics::NumEvents("WebRTC.PeerConnection.KeyProtocol",
1783 webrtc::kEnumCounterKeyProtocolSdes));
1784 EXPECT_EQ(0, webrtc::metrics::NumEvents("WebRTC.PeerConnection.KeyProtocol",
1785 webrtc::kEnumCounterKeyProtocolDtls));
deadbeef1dcb1642017-03-29 21:08:16 -07001786}
1787
Steve Anton8c0f7a72017-10-03 10:03:10 -07001788// Tests that the GetRemoteAudioSSLCertificate method returns the remote DTLS
1789// certificate once the DTLS handshake has finished.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001790TEST_P(PeerConnectionIntegrationTest,
Steve Anton8c0f7a72017-10-03 10:03:10 -07001791 GetRemoteAudioSSLCertificateReturnsExchangedCertificate) {
1792 auto GetRemoteAudioSSLCertificate = [](PeerConnectionWrapper* wrapper) {
1793 auto pci = reinterpret_cast<PeerConnectionProxy*>(wrapper->pc());
1794 auto pc = reinterpret_cast<PeerConnection*>(pci->internal());
1795 return pc->GetRemoteAudioSSLCertificate();
1796 };
Zhi Huang70b820f2018-01-27 14:16:15 -08001797 auto GetRemoteAudioSSLCertChain = [](PeerConnectionWrapper* wrapper) {
1798 auto pci = reinterpret_cast<PeerConnectionProxy*>(wrapper->pc());
1799 auto pc = reinterpret_cast<PeerConnection*>(pci->internal());
1800 return pc->GetRemoteAudioSSLCertChain();
1801 };
Steve Anton8c0f7a72017-10-03 10:03:10 -07001802
1803 auto caller_cert = rtc::RTCCertificate::FromPEM(kRsaPems[0]);
1804 auto callee_cert = rtc::RTCCertificate::FromPEM(kRsaPems[1]);
1805
1806 // Configure each side with a known certificate so they can be compared later.
1807 PeerConnectionInterface::RTCConfiguration caller_config;
1808 caller_config.enable_dtls_srtp.emplace(true);
1809 caller_config.certificates.push_back(caller_cert);
1810 PeerConnectionInterface::RTCConfiguration callee_config;
1811 callee_config.enable_dtls_srtp.emplace(true);
1812 callee_config.certificates.push_back(callee_cert);
1813 ASSERT_TRUE(
1814 CreatePeerConnectionWrappersWithConfig(caller_config, callee_config));
1815 ConnectFakeSignaling();
1816
1817 // When first initialized, there should not be a remote SSL certificate (and
1818 // calling this method should not crash).
1819 EXPECT_EQ(nullptr, GetRemoteAudioSSLCertificate(caller()));
1820 EXPECT_EQ(nullptr, GetRemoteAudioSSLCertificate(callee()));
Zhi Huang70b820f2018-01-27 14:16:15 -08001821 EXPECT_EQ(nullptr, GetRemoteAudioSSLCertChain(caller()));
1822 EXPECT_EQ(nullptr, GetRemoteAudioSSLCertChain(callee()));
Steve Anton8c0f7a72017-10-03 10:03:10 -07001823
Steve Anton15324772018-01-16 10:26:49 -08001824 caller()->AddAudioTrack();
1825 callee()->AddAudioTrack();
Steve Anton8c0f7a72017-10-03 10:03:10 -07001826 caller()->CreateAndSetAndSignalOffer();
1827 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1828 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
1829
1830 // Once DTLS has been connected, each side should return the other's SSL
1831 // certificate when calling GetRemoteAudioSSLCertificate.
1832
1833 auto caller_remote_cert = GetRemoteAudioSSLCertificate(caller());
1834 ASSERT_TRUE(caller_remote_cert);
Benjamin Wright6c6c9df2018-10-25 01:16:26 -07001835 EXPECT_EQ(callee_cert->GetSSLCertificate().ToPEMString(),
Steve Anton8c0f7a72017-10-03 10:03:10 -07001836 caller_remote_cert->ToPEMString());
1837
1838 auto callee_remote_cert = GetRemoteAudioSSLCertificate(callee());
1839 ASSERT_TRUE(callee_remote_cert);
Benjamin Wright6c6c9df2018-10-25 01:16:26 -07001840 EXPECT_EQ(caller_cert->GetSSLCertificate().ToPEMString(),
Steve Anton8c0f7a72017-10-03 10:03:10 -07001841 callee_remote_cert->ToPEMString());
Zhi Huang70b820f2018-01-27 14:16:15 -08001842
1843 auto caller_remote_cert_chain = GetRemoteAudioSSLCertChain(caller());
1844 ASSERT_TRUE(caller_remote_cert_chain);
1845 ASSERT_EQ(1U, caller_remote_cert_chain->GetSize());
1846 auto remote_cert = &caller_remote_cert_chain->Get(0);
Benjamin Wright6c6c9df2018-10-25 01:16:26 -07001847 EXPECT_EQ(callee_cert->GetSSLCertificate().ToPEMString(),
Zhi Huang70b820f2018-01-27 14:16:15 -08001848 remote_cert->ToPEMString());
1849
1850 auto callee_remote_cert_chain = GetRemoteAudioSSLCertChain(callee());
1851 ASSERT_TRUE(callee_remote_cert_chain);
1852 ASSERT_EQ(1U, callee_remote_cert_chain->GetSize());
1853 remote_cert = &callee_remote_cert_chain->Get(0);
Benjamin Wright6c6c9df2018-10-25 01:16:26 -07001854 EXPECT_EQ(caller_cert->GetSSLCertificate().ToPEMString(),
Zhi Huang70b820f2018-01-27 14:16:15 -08001855 remote_cert->ToPEMString());
Steve Anton8c0f7a72017-10-03 10:03:10 -07001856}
1857
deadbeef1dcb1642017-03-29 21:08:16 -07001858// This test sets up a call between two parties with a source resolution of
1859// 1280x720 and verifies that a 16:9 aspect ratio is received.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001860TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07001861 Send1280By720ResolutionAndReceive16To9AspectRatio) {
1862 ASSERT_TRUE(CreatePeerConnectionWrappers());
1863 ConnectFakeSignaling();
1864
Niels Möller5c7efe72018-05-11 10:34:46 +02001865 // Add video tracks with 16:9 aspect ratio, size 1280 x 720.
1866 webrtc::FakePeriodicVideoSource::Config config;
1867 config.width = 1280;
1868 config.height = 720;
Johannes Kron965e7942018-09-13 15:36:20 +02001869 config.timestamp_offset_ms = rtc::TimeMillis();
Niels Möller5c7efe72018-05-11 10:34:46 +02001870 caller()->AddTrack(caller()->CreateLocalVideoTrackWithConfig(config));
1871 callee()->AddTrack(callee()->CreateLocalVideoTrackWithConfig(config));
deadbeef1dcb1642017-03-29 21:08:16 -07001872
1873 // Do normal offer/answer and wait for at least one frame to be received in
1874 // each direction.
1875 caller()->CreateAndSetAndSignalOffer();
1876 ASSERT_TRUE_WAIT(caller()->min_video_frames_received_per_track() > 0 &&
1877 callee()->min_video_frames_received_per_track() > 0,
1878 kMaxWaitForFramesMs);
1879
1880 // Check rendered aspect ratio.
1881 EXPECT_EQ(16.0 / 9, caller()->local_rendered_aspect_ratio());
1882 EXPECT_EQ(16.0 / 9, caller()->rendered_aspect_ratio());
1883 EXPECT_EQ(16.0 / 9, callee()->local_rendered_aspect_ratio());
1884 EXPECT_EQ(16.0 / 9, callee()->rendered_aspect_ratio());
1885}
1886
1887// This test sets up an one-way call, with media only from caller to
1888// callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001889TEST_P(PeerConnectionIntegrationTest, OneWayMediaCall) {
deadbeef1dcb1642017-03-29 21:08:16 -07001890 ASSERT_TRUE(CreatePeerConnectionWrappers());
1891 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001892 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001893 caller()->CreateAndSetAndSignalOffer();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001894 MediaExpectations media_expectations;
1895 media_expectations.CalleeExpectsSomeAudioAndVideo();
1896 media_expectations.CallerExpectsNoAudio();
1897 media_expectations.CallerExpectsNoVideo();
1898 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07001899}
1900
1901// This test sets up a audio call initially, with the callee rejecting video
1902// initially. Then later the callee decides to upgrade to audio/video, and
1903// initiates a new offer/answer exchange.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001904TEST_P(PeerConnectionIntegrationTest, AudioToVideoUpgrade) {
deadbeef1dcb1642017-03-29 21:08:16 -07001905 ASSERT_TRUE(CreatePeerConnectionWrappers());
1906 ConnectFakeSignaling();
1907 // Initially, offer an audio/video stream from the caller, but refuse to
1908 // send/receive video on the callee side.
Steve Anton15324772018-01-16 10:26:49 -08001909 caller()->AddAudioVideoTracks();
1910 callee()->AddAudioTrack();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001911 if (sdp_semantics_ == SdpSemantics::kPlanB) {
1912 PeerConnectionInterface::RTCOfferAnswerOptions options;
1913 options.offer_to_receive_video = 0;
1914 callee()->SetOfferAnswerOptions(options);
1915 } else {
1916 callee()->SetRemoteOfferHandler([this] {
1917 callee()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)->Stop();
1918 });
1919 }
deadbeef1dcb1642017-03-29 21:08:16 -07001920 // Do offer/answer and make sure audio is still received end-to-end.
1921 caller()->CreateAndSetAndSignalOffer();
1922 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001923 {
1924 MediaExpectations media_expectations;
1925 media_expectations.ExpectBidirectionalAudio();
1926 media_expectations.ExpectNoVideo();
1927 ASSERT_TRUE(ExpectNewFrames(media_expectations));
1928 }
deadbeef1dcb1642017-03-29 21:08:16 -07001929 // Sanity check that the callee's description has a rejected video section.
1930 ASSERT_NE(nullptr, callee()->pc()->local_description());
1931 const ContentInfo* callee_video_content =
1932 GetFirstVideoContent(callee()->pc()->local_description()->description());
1933 ASSERT_NE(nullptr, callee_video_content);
1934 EXPECT_TRUE(callee_video_content->rejected);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001935
deadbeef1dcb1642017-03-29 21:08:16 -07001936 // Now negotiate with video and ensure negotiation succeeds, with video
1937 // frames and additional audio frames being received.
Steve Anton15324772018-01-16 10:26:49 -08001938 callee()->AddVideoTrack();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001939 if (sdp_semantics_ == SdpSemantics::kPlanB) {
1940 PeerConnectionInterface::RTCOfferAnswerOptions options;
1941 options.offer_to_receive_video = 1;
1942 callee()->SetOfferAnswerOptions(options);
1943 } else {
1944 callee()->SetRemoteOfferHandler(nullptr);
1945 caller()->SetRemoteOfferHandler([this] {
1946 // The caller creates a new transceiver to receive video on when receiving
1947 // the offer, but by default it is send only.
1948 auto transceivers = caller()->pc()->GetTransceivers();
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02001949 ASSERT_EQ(3U, transceivers.size());
Seth Hampson2f0d7022018-02-20 11:54:42 -08001950 ASSERT_EQ(cricket::MEDIA_TYPE_VIDEO,
1951 transceivers[2]->receiver()->media_type());
1952 transceivers[2]->sender()->SetTrack(caller()->CreateLocalVideoTrack());
1953 transceivers[2]->SetDirection(RtpTransceiverDirection::kSendRecv);
1954 });
1955 }
deadbeef1dcb1642017-03-29 21:08:16 -07001956 callee()->CreateAndSetAndSignalOffer();
1957 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001958 {
1959 // Expect additional audio frames to be received after the upgrade.
1960 MediaExpectations media_expectations;
1961 media_expectations.ExpectBidirectionalAudioAndVideo();
1962 ASSERT_TRUE(ExpectNewFrames(media_expectations));
1963 }
deadbeef1dcb1642017-03-29 21:08:16 -07001964}
1965
deadbeef4389b4d2017-09-07 09:07:36 -07001966// Simpler than the above test; just add an audio track to an established
1967// video-only connection.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001968TEST_P(PeerConnectionIntegrationTest, AddAudioToVideoOnlyCall) {
deadbeef4389b4d2017-09-07 09:07:36 -07001969 ASSERT_TRUE(CreatePeerConnectionWrappers());
1970 ConnectFakeSignaling();
1971 // Do initial offer/answer with just a video track.
Steve Anton15324772018-01-16 10:26:49 -08001972 caller()->AddVideoTrack();
1973 callee()->AddVideoTrack();
deadbeef4389b4d2017-09-07 09:07:36 -07001974 caller()->CreateAndSetAndSignalOffer();
1975 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1976 // Now add an audio track and do another offer/answer.
Steve Anton15324772018-01-16 10:26:49 -08001977 caller()->AddAudioTrack();
1978 callee()->AddAudioTrack();
deadbeef4389b4d2017-09-07 09:07:36 -07001979 caller()->CreateAndSetAndSignalOffer();
1980 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1981 // Ensure both audio and video frames are received end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001982 MediaExpectations media_expectations;
1983 media_expectations.ExpectBidirectionalAudioAndVideo();
1984 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef4389b4d2017-09-07 09:07:36 -07001985}
1986
deadbeef1dcb1642017-03-29 21:08:16 -07001987// This test sets up a call that's transferred to a new caller with a different
1988// DTLS fingerprint.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001989TEST_P(PeerConnectionIntegrationTest, CallTransferredForCallee) {
deadbeef1dcb1642017-03-29 21:08:16 -07001990 ASSERT_TRUE(CreatePeerConnectionWrappers());
1991 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001992 caller()->AddAudioVideoTracks();
1993 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001994 caller()->CreateAndSetAndSignalOffer();
1995 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1996
1997 // Keep the original peer around which will still send packets to the
1998 // receiving client. These SRTP packets will be dropped.
1999 std::unique_ptr<PeerConnectionWrapper> original_peer(
2000 SetCallerPcWrapperAndReturnCurrent(
Seth Hampson2f0d7022018-02-20 11:54:42 -08002001 CreatePeerConnectionWrapperWithAlternateKey().release()));
deadbeef1dcb1642017-03-29 21:08:16 -07002002 // TODO(deadbeef): Why do we call Close here? That goes against the comment
2003 // directly above.
2004 original_peer->pc()->Close();
2005
2006 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002007 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002008 caller()->CreateAndSetAndSignalOffer();
2009 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2010 // Wait for some additional frames to be transmitted end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002011 MediaExpectations media_expectations;
2012 media_expectations.ExpectBidirectionalAudioAndVideo();
2013 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002014}
2015
2016// This test sets up a call that's transferred to a new callee with a different
2017// DTLS fingerprint.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002018TEST_P(PeerConnectionIntegrationTest, CallTransferredForCaller) {
deadbeef1dcb1642017-03-29 21:08:16 -07002019 ASSERT_TRUE(CreatePeerConnectionWrappers());
2020 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002021 caller()->AddAudioVideoTracks();
2022 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002023 caller()->CreateAndSetAndSignalOffer();
2024 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2025
2026 // Keep the original peer around which will still send packets to the
2027 // receiving client. These SRTP packets will be dropped.
2028 std::unique_ptr<PeerConnectionWrapper> original_peer(
2029 SetCalleePcWrapperAndReturnCurrent(
Seth Hampson2f0d7022018-02-20 11:54:42 -08002030 CreatePeerConnectionWrapperWithAlternateKey().release()));
deadbeef1dcb1642017-03-29 21:08:16 -07002031 // TODO(deadbeef): Why do we call Close here? That goes against the comment
2032 // directly above.
2033 original_peer->pc()->Close();
2034
2035 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002036 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002037 caller()->SetOfferAnswerOptions(IceRestartOfferAnswerOptions());
2038 caller()->CreateAndSetAndSignalOffer();
2039 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2040 // Wait for some additional frames to be transmitted end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002041 MediaExpectations media_expectations;
2042 media_expectations.ExpectBidirectionalAudioAndVideo();
2043 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002044}
2045
2046// This test sets up a non-bundled call and negotiates bundling at the same
2047// time as starting an ICE restart. When bundling is in effect in the restart,
2048// the DTLS-SRTP context should be successfully reset.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002049TEST_P(PeerConnectionIntegrationTest, BundlingEnabledWhileIceRestartOccurs) {
deadbeef1dcb1642017-03-29 21:08:16 -07002050 ASSERT_TRUE(CreatePeerConnectionWrappers());
2051 ConnectFakeSignaling();
2052
Steve Anton15324772018-01-16 10:26:49 -08002053 caller()->AddAudioVideoTracks();
2054 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002055 // Remove the bundle group from the SDP received by the callee.
2056 callee()->SetReceivedSdpMunger([](cricket::SessionDescription* desc) {
2057 desc->RemoveGroupByName("BUNDLE");
2058 });
2059 caller()->CreateAndSetAndSignalOffer();
2060 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002061 {
2062 MediaExpectations media_expectations;
2063 media_expectations.ExpectBidirectionalAudioAndVideo();
2064 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2065 }
deadbeef1dcb1642017-03-29 21:08:16 -07002066 // Now stop removing the BUNDLE group, and trigger an ICE restart.
2067 callee()->SetReceivedSdpMunger(nullptr);
2068 caller()->SetOfferAnswerOptions(IceRestartOfferAnswerOptions());
2069 caller()->CreateAndSetAndSignalOffer();
2070 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2071
2072 // Expect additional frames to be received after the ICE restart.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002073 {
2074 MediaExpectations media_expectations;
2075 media_expectations.ExpectBidirectionalAudioAndVideo();
2076 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2077 }
deadbeef1dcb1642017-03-29 21:08:16 -07002078}
2079
2080// Test CVO (Coordination of Video Orientation). If a video source is rotated
2081// and both peers support the CVO RTP header extension, the actual video frames
2082// don't need to be encoded in different resolutions, since the rotation is
2083// communicated through the RTP header extension.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002084TEST_P(PeerConnectionIntegrationTest, RotatedVideoWithCVOExtension) {
deadbeef1dcb1642017-03-29 21:08:16 -07002085 ASSERT_TRUE(CreatePeerConnectionWrappers());
2086 ConnectFakeSignaling();
2087 // Add rotated video tracks.
Steve Anton15324772018-01-16 10:26:49 -08002088 caller()->AddTrack(
deadbeef1dcb1642017-03-29 21:08:16 -07002089 caller()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_90));
Steve Anton15324772018-01-16 10:26:49 -08002090 callee()->AddTrack(
deadbeef1dcb1642017-03-29 21:08:16 -07002091 callee()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_270));
2092
2093 // Wait for video frames to be received by both sides.
2094 caller()->CreateAndSetAndSignalOffer();
2095 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2096 ASSERT_TRUE_WAIT(caller()->min_video_frames_received_per_track() > 0 &&
2097 callee()->min_video_frames_received_per_track() > 0,
2098 kMaxWaitForFramesMs);
2099
2100 // Ensure that the aspect ratio is unmodified.
2101 // TODO(deadbeef): Where does 4:3 come from? Should be explicit in the test,
2102 // not just assumed.
2103 EXPECT_EQ(4.0 / 3, caller()->local_rendered_aspect_ratio());
2104 EXPECT_EQ(4.0 / 3, caller()->rendered_aspect_ratio());
2105 EXPECT_EQ(4.0 / 3, callee()->local_rendered_aspect_ratio());
2106 EXPECT_EQ(4.0 / 3, callee()->rendered_aspect_ratio());
2107 // Ensure that the CVO bits were surfaced to the renderer.
2108 EXPECT_EQ(webrtc::kVideoRotation_270, caller()->rendered_rotation());
2109 EXPECT_EQ(webrtc::kVideoRotation_90, callee()->rendered_rotation());
2110}
2111
2112// Test that when the CVO extension isn't supported, video is rotated the
2113// old-fashioned way, by encoding rotated frames.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002114TEST_P(PeerConnectionIntegrationTest, RotatedVideoWithoutCVOExtension) {
deadbeef1dcb1642017-03-29 21:08:16 -07002115 ASSERT_TRUE(CreatePeerConnectionWrappers());
2116 ConnectFakeSignaling();
2117 // Add rotated video tracks.
Steve Anton15324772018-01-16 10:26:49 -08002118 caller()->AddTrack(
deadbeef1dcb1642017-03-29 21:08:16 -07002119 caller()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_90));
Steve Anton15324772018-01-16 10:26:49 -08002120 callee()->AddTrack(
deadbeef1dcb1642017-03-29 21:08:16 -07002121 callee()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_270));
2122
2123 // Remove the CVO extension from the offered SDP.
2124 callee()->SetReceivedSdpMunger([](cricket::SessionDescription* desc) {
2125 cricket::VideoContentDescription* video =
2126 GetFirstVideoContentDescription(desc);
2127 video->ClearRtpHeaderExtensions();
2128 });
2129 // Wait for video frames to be received by both sides.
2130 caller()->CreateAndSetAndSignalOffer();
2131 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2132 ASSERT_TRUE_WAIT(caller()->min_video_frames_received_per_track() > 0 &&
2133 callee()->min_video_frames_received_per_track() > 0,
2134 kMaxWaitForFramesMs);
2135
2136 // Expect that the aspect ratio is inversed to account for the 90/270 degree
2137 // rotation.
2138 // TODO(deadbeef): Where does 4:3 come from? Should be explicit in the test,
2139 // not just assumed.
2140 EXPECT_EQ(3.0 / 4, caller()->local_rendered_aspect_ratio());
2141 EXPECT_EQ(3.0 / 4, caller()->rendered_aspect_ratio());
2142 EXPECT_EQ(3.0 / 4, callee()->local_rendered_aspect_ratio());
2143 EXPECT_EQ(3.0 / 4, callee()->rendered_aspect_ratio());
2144 // Expect that each endpoint is unaware of the rotation of the other endpoint.
2145 EXPECT_EQ(webrtc::kVideoRotation_0, caller()->rendered_rotation());
2146 EXPECT_EQ(webrtc::kVideoRotation_0, callee()->rendered_rotation());
2147}
2148
deadbeef1dcb1642017-03-29 21:08:16 -07002149// Test that if the answerer rejects the audio m= section, no audio is sent or
2150// received, but video still can be.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002151TEST_P(PeerConnectionIntegrationTest, AnswererRejectsAudioSection) {
deadbeef1dcb1642017-03-29 21:08:16 -07002152 ASSERT_TRUE(CreatePeerConnectionWrappers());
2153 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002154 caller()->AddAudioVideoTracks();
Seth Hampson2f0d7022018-02-20 11:54:42 -08002155 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2156 // Only add video track for callee, and set offer_to_receive_audio to 0, so
2157 // it will reject the audio m= section completely.
2158 PeerConnectionInterface::RTCOfferAnswerOptions options;
2159 options.offer_to_receive_audio = 0;
2160 callee()->SetOfferAnswerOptions(options);
2161 } else {
2162 // Stopping the audio RtpTransceiver will cause the media section to be
2163 // rejected in the answer.
2164 callee()->SetRemoteOfferHandler([this] {
2165 callee()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_AUDIO)->Stop();
2166 });
2167 }
Steve Anton15324772018-01-16 10:26:49 -08002168 callee()->AddTrack(callee()->CreateLocalVideoTrack());
deadbeef1dcb1642017-03-29 21:08:16 -07002169 // Do offer/answer and wait for successful end-to-end video frames.
2170 caller()->CreateAndSetAndSignalOffer();
2171 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002172 MediaExpectations media_expectations;
2173 media_expectations.ExpectBidirectionalVideo();
2174 media_expectations.ExpectNoAudio();
2175 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2176
deadbeef1dcb1642017-03-29 21:08:16 -07002177 // Sanity check that the callee's description has a rejected audio section.
2178 ASSERT_NE(nullptr, callee()->pc()->local_description());
2179 const ContentInfo* callee_audio_content =
2180 GetFirstAudioContent(callee()->pc()->local_description()->description());
2181 ASSERT_NE(nullptr, callee_audio_content);
2182 EXPECT_TRUE(callee_audio_content->rejected);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002183 if (sdp_semantics_ == SdpSemantics::kUnifiedPlan) {
2184 // The caller's transceiver should have stopped after receiving the answer.
2185 EXPECT_TRUE(caller()
2186 ->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_AUDIO)
2187 ->stopped());
2188 }
deadbeef1dcb1642017-03-29 21:08:16 -07002189}
2190
2191// Test that if the answerer rejects the video m= section, no video is sent or
2192// received, but audio still can be.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002193TEST_P(PeerConnectionIntegrationTest, AnswererRejectsVideoSection) {
deadbeef1dcb1642017-03-29 21:08:16 -07002194 ASSERT_TRUE(CreatePeerConnectionWrappers());
2195 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002196 caller()->AddAudioVideoTracks();
Seth Hampson2f0d7022018-02-20 11:54:42 -08002197 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2198 // Only add audio track for callee, and set offer_to_receive_video to 0, so
2199 // it will reject the video m= section completely.
2200 PeerConnectionInterface::RTCOfferAnswerOptions options;
2201 options.offer_to_receive_video = 0;
2202 callee()->SetOfferAnswerOptions(options);
2203 } else {
2204 // Stopping the video RtpTransceiver will cause the media section to be
2205 // rejected in the answer.
2206 callee()->SetRemoteOfferHandler([this] {
2207 callee()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)->Stop();
2208 });
2209 }
Steve Anton15324772018-01-16 10:26:49 -08002210 callee()->AddTrack(callee()->CreateLocalAudioTrack());
deadbeef1dcb1642017-03-29 21:08:16 -07002211 // Do offer/answer and wait for successful end-to-end audio frames.
2212 caller()->CreateAndSetAndSignalOffer();
2213 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002214 MediaExpectations media_expectations;
2215 media_expectations.ExpectBidirectionalAudio();
2216 media_expectations.ExpectNoVideo();
2217 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2218
deadbeef1dcb1642017-03-29 21:08:16 -07002219 // Sanity check that the callee's description has a rejected video section.
2220 ASSERT_NE(nullptr, callee()->pc()->local_description());
2221 const ContentInfo* callee_video_content =
2222 GetFirstVideoContent(callee()->pc()->local_description()->description());
2223 ASSERT_NE(nullptr, callee_video_content);
2224 EXPECT_TRUE(callee_video_content->rejected);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002225 if (sdp_semantics_ == SdpSemantics::kUnifiedPlan) {
2226 // The caller's transceiver should have stopped after receiving the answer.
2227 EXPECT_TRUE(caller()
2228 ->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)
2229 ->stopped());
2230 }
deadbeef1dcb1642017-03-29 21:08:16 -07002231}
2232
2233// Test that if the answerer rejects both audio and video m= sections, nothing
2234// bad happens.
2235// TODO(deadbeef): Test that a data channel still works. Currently this doesn't
2236// test anything but the fact that negotiation succeeds, which doesn't mean
2237// much.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002238TEST_P(PeerConnectionIntegrationTest, AnswererRejectsAudioAndVideoSections) {
deadbeef1dcb1642017-03-29 21:08:16 -07002239 ASSERT_TRUE(CreatePeerConnectionWrappers());
2240 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002241 caller()->AddAudioVideoTracks();
Seth Hampson2f0d7022018-02-20 11:54:42 -08002242 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2243 // Don't give the callee any tracks, and set offer_to_receive_X to 0, so it
2244 // will reject both audio and video m= sections.
2245 PeerConnectionInterface::RTCOfferAnswerOptions options;
2246 options.offer_to_receive_audio = 0;
2247 options.offer_to_receive_video = 0;
2248 callee()->SetOfferAnswerOptions(options);
2249 } else {
2250 callee()->SetRemoteOfferHandler([this] {
2251 // Stopping all transceivers will cause all media sections to be rejected.
Mirko Bonadei739baf02019-01-27 17:29:42 +01002252 for (const auto& transceiver : callee()->pc()->GetTransceivers()) {
Seth Hampson2f0d7022018-02-20 11:54:42 -08002253 transceiver->Stop();
2254 }
2255 });
2256 }
deadbeef1dcb1642017-03-29 21:08:16 -07002257 // Do offer/answer and wait for stable signaling state.
2258 caller()->CreateAndSetAndSignalOffer();
2259 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002260
deadbeef1dcb1642017-03-29 21:08:16 -07002261 // Sanity check that the callee's description has rejected m= sections.
2262 ASSERT_NE(nullptr, callee()->pc()->local_description());
2263 const ContentInfo* callee_audio_content =
2264 GetFirstAudioContent(callee()->pc()->local_description()->description());
2265 ASSERT_NE(nullptr, callee_audio_content);
2266 EXPECT_TRUE(callee_audio_content->rejected);
2267 const ContentInfo* callee_video_content =
2268 GetFirstVideoContent(callee()->pc()->local_description()->description());
2269 ASSERT_NE(nullptr, callee_video_content);
2270 EXPECT_TRUE(callee_video_content->rejected);
2271}
2272
2273// This test sets up an audio and video call between two parties. After the
2274// call runs for a while, the caller sends an updated offer with video being
2275// rejected. Once the re-negotiation is done, the video flow should stop and
2276// the audio flow should continue.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002277TEST_P(PeerConnectionIntegrationTest, VideoRejectedInSubsequentOffer) {
deadbeef1dcb1642017-03-29 21:08:16 -07002278 ASSERT_TRUE(CreatePeerConnectionWrappers());
2279 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002280 caller()->AddAudioVideoTracks();
2281 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002282 caller()->CreateAndSetAndSignalOffer();
2283 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002284 {
2285 MediaExpectations media_expectations;
2286 media_expectations.ExpectBidirectionalAudioAndVideo();
2287 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2288 }
deadbeef1dcb1642017-03-29 21:08:16 -07002289 // Renegotiate, rejecting the video m= section.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002290 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2291 caller()->SetGeneratedSdpMunger(
2292 [](cricket::SessionDescription* description) {
2293 for (cricket::ContentInfo& content : description->contents()) {
2294 if (cricket::IsVideoContent(&content)) {
2295 content.rejected = true;
2296 }
2297 }
2298 });
2299 } else {
2300 caller()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)->Stop();
2301 }
deadbeef1dcb1642017-03-29 21:08:16 -07002302 caller()->CreateAndSetAndSignalOffer();
2303 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
2304
2305 // Sanity check that the caller's description has a rejected video section.
2306 ASSERT_NE(nullptr, caller()->pc()->local_description());
2307 const ContentInfo* caller_video_content =
2308 GetFirstVideoContent(caller()->pc()->local_description()->description());
2309 ASSERT_NE(nullptr, caller_video_content);
2310 EXPECT_TRUE(caller_video_content->rejected);
deadbeef1dcb1642017-03-29 21:08:16 -07002311 // Wait for some additional audio frames to be received.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002312 {
2313 MediaExpectations media_expectations;
2314 media_expectations.ExpectBidirectionalAudio();
2315 media_expectations.ExpectNoVideo();
2316 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2317 }
deadbeef1dcb1642017-03-29 21:08:16 -07002318}
2319
Taylor Brandstetter60c8dc82018-04-11 15:20:27 -07002320// Do one offer/answer with audio, another that disables it (rejecting the m=
2321// section), and another that re-enables it. Regression test for:
2322// bugs.webrtc.org/6023
2323TEST_F(PeerConnectionIntegrationTestPlanB, EnableAudioAfterRejecting) {
2324 ASSERT_TRUE(CreatePeerConnectionWrappers());
2325 ConnectFakeSignaling();
2326
2327 // Add audio track, do normal offer/answer.
2328 rtc::scoped_refptr<webrtc::AudioTrackInterface> track =
2329 caller()->CreateLocalAudioTrack();
2330 rtc::scoped_refptr<webrtc::RtpSenderInterface> sender =
2331 caller()->pc()->AddTrack(track, {"stream"}).MoveValue();
2332 caller()->CreateAndSetAndSignalOffer();
2333 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2334
2335 // Remove audio track, and set offer_to_receive_audio to false to cause the
2336 // m= section to be completely disabled, not just "recvonly".
2337 caller()->pc()->RemoveTrack(sender);
2338 PeerConnectionInterface::RTCOfferAnswerOptions options;
2339 options.offer_to_receive_audio = 0;
2340 caller()->SetOfferAnswerOptions(options);
2341 caller()->CreateAndSetAndSignalOffer();
2342 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2343
2344 // Add the audio track again, expecting negotiation to succeed and frames to
2345 // flow.
2346 sender = caller()->pc()->AddTrack(track, {"stream"}).MoveValue();
2347 options.offer_to_receive_audio = 1;
2348 caller()->SetOfferAnswerOptions(options);
2349 caller()->CreateAndSetAndSignalOffer();
2350 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2351
2352 MediaExpectations media_expectations;
2353 media_expectations.CalleeExpectsSomeAudio();
2354 EXPECT_TRUE(ExpectNewFrames(media_expectations));
2355}
2356
deadbeef1dcb1642017-03-29 21:08:16 -07002357// Basic end-to-end test, but without SSRC/MSID signaling. This functionality
2358// is needed to support legacy endpoints.
2359// TODO(deadbeef): When we support the MID extension and demuxing on MID, also
2360// add a test for an end-to-end test without MID signaling either (basically,
2361// the minimum acceptable SDP).
Seth Hampson2f0d7022018-02-20 11:54:42 -08002362TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithoutSsrcOrMsidSignaling) {
deadbeef1dcb1642017-03-29 21:08:16 -07002363 ASSERT_TRUE(CreatePeerConnectionWrappers());
2364 ConnectFakeSignaling();
2365 // Add audio and video, testing that packets can be demuxed on payload type.
Steve Anton15324772018-01-16 10:26:49 -08002366 caller()->AddAudioVideoTracks();
2367 callee()->AddAudioVideoTracks();
deadbeefd8ad7882017-04-18 16:01:17 -07002368 // Remove SSRCs and MSIDs from the received offer SDP.
2369 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
deadbeef1dcb1642017-03-29 21:08:16 -07002370 caller()->CreateAndSetAndSignalOffer();
2371 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002372 MediaExpectations media_expectations;
2373 media_expectations.ExpectBidirectionalAudioAndVideo();
2374 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002375}
2376
Seth Hampson5897a6e2018-04-03 11:16:33 -07002377// Basic end-to-end test, without SSRC signaling. This means that the track
2378// was created properly and frames are delivered when the MSIDs are communicated
2379// with a=msid lines and no a=ssrc lines.
2380TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
2381 EndToEndCallWithoutSsrcSignaling) {
2382 const char kStreamId[] = "streamId";
2383 ASSERT_TRUE(CreatePeerConnectionWrappers());
2384 ConnectFakeSignaling();
2385 // Add just audio tracks.
2386 caller()->AddTrack(caller()->CreateLocalAudioTrack(), {kStreamId});
2387 callee()->AddAudioTrack();
2388
2389 // Remove SSRCs from the received offer SDP.
2390 callee()->SetReceivedSdpMunger(RemoveSsrcsAndKeepMsids);
2391 caller()->CreateAndSetAndSignalOffer();
2392 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2393 MediaExpectations media_expectations;
2394 media_expectations.ExpectBidirectionalAudio();
2395 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2396}
2397
Steve Antondf527fd2018-04-27 15:52:03 -07002398// Tests that video flows between multiple video tracks when SSRCs are not
2399// signaled. This exercises the MID RTP header extension which is needed to
2400// demux the incoming video tracks.
2401TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
2402 EndToEndCallWithTwoVideoTracksAndNoSignaledSsrc) {
2403 ASSERT_TRUE(CreatePeerConnectionWrappers());
2404 ConnectFakeSignaling();
2405 caller()->AddVideoTrack();
2406 caller()->AddVideoTrack();
2407 callee()->AddVideoTrack();
2408 callee()->AddVideoTrack();
2409
2410 caller()->SetReceivedSdpMunger(&RemoveSsrcsAndKeepMsids);
2411 callee()->SetReceivedSdpMunger(&RemoveSsrcsAndKeepMsids);
2412 caller()->CreateAndSetAndSignalOffer();
2413 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2414 ASSERT_EQ(2u, caller()->pc()->GetReceivers().size());
2415 ASSERT_EQ(2u, callee()->pc()->GetReceivers().size());
2416
2417 // Expect video to be received in both directions on both tracks.
2418 MediaExpectations media_expectations;
2419 media_expectations.ExpectBidirectionalVideo();
2420 EXPECT_TRUE(ExpectNewFrames(media_expectations));
2421}
2422
Henrik Boström5b147782018-12-04 11:25:05 +01002423TEST_F(PeerConnectionIntegrationTestUnifiedPlan, NoStreamsMsidLinePresent) {
2424 ASSERT_TRUE(CreatePeerConnectionWrappers());
2425 ConnectFakeSignaling();
2426 caller()->AddAudioTrack();
2427 caller()->AddVideoTrack();
2428 caller()->CreateAndSetAndSignalOffer();
2429 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2430 auto callee_receivers = callee()->pc()->GetReceivers();
2431 ASSERT_EQ(2u, callee_receivers.size());
2432 EXPECT_TRUE(callee_receivers[0]->stream_ids().empty());
2433 EXPECT_TRUE(callee_receivers[1]->stream_ids().empty());
2434}
2435
2436TEST_F(PeerConnectionIntegrationTestUnifiedPlan, NoStreamsMsidLineMissing) {
2437 ASSERT_TRUE(CreatePeerConnectionWrappers());
2438 ConnectFakeSignaling();
2439 caller()->AddAudioTrack();
2440 caller()->AddVideoTrack();
2441 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
2442 caller()->CreateAndSetAndSignalOffer();
2443 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2444 auto callee_receivers = callee()->pc()->GetReceivers();
2445 ASSERT_EQ(2u, callee_receivers.size());
2446 ASSERT_EQ(1u, callee_receivers[0]->stream_ids().size());
2447 ASSERT_EQ(1u, callee_receivers[1]->stream_ids().size());
2448 EXPECT_EQ(callee_receivers[0]->stream_ids()[0],
2449 callee_receivers[1]->stream_ids()[0]);
2450 EXPECT_EQ(callee_receivers[0]->streams()[0],
2451 callee_receivers[1]->streams()[0]);
2452}
2453
deadbeef1dcb1642017-03-29 21:08:16 -07002454// Test that if two video tracks are sent (from caller to callee, in this test),
2455// they're transmitted correctly end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002456TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithTwoVideoTracks) {
deadbeef1dcb1642017-03-29 21:08:16 -07002457 ASSERT_TRUE(CreatePeerConnectionWrappers());
2458 ConnectFakeSignaling();
2459 // Add one audio/video stream, and one video-only stream.
Steve Anton15324772018-01-16 10:26:49 -08002460 caller()->AddAudioVideoTracks();
2461 caller()->AddVideoTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07002462 caller()->CreateAndSetAndSignalOffer();
2463 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Steve Anton15324772018-01-16 10:26:49 -08002464 ASSERT_EQ(3u, callee()->pc()->GetReceivers().size());
Seth Hampson2f0d7022018-02-20 11:54:42 -08002465
2466 MediaExpectations media_expectations;
2467 media_expectations.CalleeExpectsSomeAudioAndVideo();
2468 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002469}
2470
2471static void MakeSpecCompliantMaxBundleOffer(cricket::SessionDescription* desc) {
2472 bool first = true;
2473 for (cricket::ContentInfo& content : desc->contents()) {
2474 if (first) {
2475 first = false;
2476 continue;
2477 }
2478 content.bundle_only = true;
2479 }
2480 first = true;
2481 for (cricket::TransportInfo& transport : desc->transport_infos()) {
2482 if (first) {
2483 first = false;
2484 continue;
2485 }
2486 transport.description.ice_ufrag.clear();
2487 transport.description.ice_pwd.clear();
2488 transport.description.connection_role = cricket::CONNECTIONROLE_NONE;
2489 transport.description.identity_fingerprint.reset(nullptr);
2490 }
2491}
2492
2493// Test that if applying a true "max bundle" offer, which uses ports of 0,
2494// "a=bundle-only", omitting "a=fingerprint", "a=setup", "a=ice-ufrag" and
2495// "a=ice-pwd" for all but the audio "m=" section, negotiation still completes
2496// successfully and media flows.
2497// TODO(deadbeef): Update this test to also omit "a=rtcp-mux", once that works.
2498// TODO(deadbeef): Won't need this test once we start generating actual
2499// standards-compliant SDP.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002500TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07002501 EndToEndCallWithSpecCompliantMaxBundleOffer) {
2502 ASSERT_TRUE(CreatePeerConnectionWrappers());
2503 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002504 caller()->AddAudioVideoTracks();
2505 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002506 // Do the equivalent of setting the port to 0, adding a=bundle-only, and
2507 // removing a=ice-ufrag, a=ice-pwd, a=fingerprint and a=setup from all
2508 // but the first m= section.
2509 callee()->SetReceivedSdpMunger(MakeSpecCompliantMaxBundleOffer);
2510 caller()->CreateAndSetAndSignalOffer();
2511 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002512 MediaExpectations media_expectations;
2513 media_expectations.ExpectBidirectionalAudioAndVideo();
2514 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002515}
2516
2517// Test that we can receive the audio output level from a remote audio track.
2518// TODO(deadbeef): Use a fake audio source and verify that the output level is
2519// exactly what the source on the other side was configured with.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002520TEST_P(PeerConnectionIntegrationTest, GetAudioOutputLevelStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-29 21:08:16 -07002521 ASSERT_TRUE(CreatePeerConnectionWrappers());
2522 ConnectFakeSignaling();
2523 // Just add an audio track.
Steve Anton15324772018-01-16 10:26:49 -08002524 caller()->AddAudioTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07002525 caller()->CreateAndSetAndSignalOffer();
2526 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2527
2528 // Get the audio output level stats. Note that the level is not available
2529 // until an RTCP packet has been received.
deadbeefd8ad7882017-04-18 16:01:17 -07002530 EXPECT_TRUE_WAIT(callee()->OldGetStats()->AudioOutputLevel() > 0,
deadbeef1dcb1642017-03-29 21:08:16 -07002531 kMaxWaitForFramesMs);
2532}
2533
2534// Test that an audio input level is reported.
2535// TODO(deadbeef): Use a fake audio source and verify that the input level is
2536// exactly what the source was configured with.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002537TEST_P(PeerConnectionIntegrationTest, GetAudioInputLevelStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-29 21:08:16 -07002538 ASSERT_TRUE(CreatePeerConnectionWrappers());
2539 ConnectFakeSignaling();
2540 // Just add an audio track.
Steve Anton15324772018-01-16 10:26:49 -08002541 caller()->AddAudioTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07002542 caller()->CreateAndSetAndSignalOffer();
2543 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2544
2545 // Get the audio input level stats. The level should be available very
2546 // soon after the test starts.
deadbeefd8ad7882017-04-18 16:01:17 -07002547 EXPECT_TRUE_WAIT(caller()->OldGetStats()->AudioInputLevel() > 0,
deadbeef1dcb1642017-03-29 21:08:16 -07002548 kMaxWaitForStatsMs);
2549}
2550
2551// Test that we can get incoming byte counts from both audio and video tracks.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002552TEST_P(PeerConnectionIntegrationTest, GetBytesReceivedStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-29 21:08:16 -07002553 ASSERT_TRUE(CreatePeerConnectionWrappers());
2554 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002555 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002556 // Do offer/answer, wait for the callee to receive some frames.
2557 caller()->CreateAndSetAndSignalOffer();
2558 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002559
2560 MediaExpectations media_expectations;
2561 media_expectations.CalleeExpectsSomeAudioAndVideo();
2562 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002563
2564 // Get a handle to the remote tracks created, so they can be used as GetStats
2565 // filters.
Mirko Bonadei739baf02019-01-27 17:29:42 +01002566 for (const auto& receiver : callee()->pc()->GetReceivers()) {
Steve Anton15324772018-01-16 10:26:49 -08002567 // We received frames, so we definitely should have nonzero "received bytes"
2568 // stats at this point.
2569 EXPECT_GT(callee()->OldGetStatsForTrack(receiver->track())->BytesReceived(),
2570 0);
2571 }
deadbeef1dcb1642017-03-29 21:08:16 -07002572}
2573
2574// Test that we can get outgoing byte counts from both audio and video tracks.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002575TEST_P(PeerConnectionIntegrationTest, GetBytesSentStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-29 21:08:16 -07002576 ASSERT_TRUE(CreatePeerConnectionWrappers());
2577 ConnectFakeSignaling();
2578 auto audio_track = caller()->CreateLocalAudioTrack();
2579 auto video_track = caller()->CreateLocalVideoTrack();
Steve Anton15324772018-01-16 10:26:49 -08002580 caller()->AddTrack(audio_track);
2581 caller()->AddTrack(video_track);
deadbeef1dcb1642017-03-29 21:08:16 -07002582 // Do offer/answer, wait for the callee to receive some frames.
2583 caller()->CreateAndSetAndSignalOffer();
2584 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002585 MediaExpectations media_expectations;
2586 media_expectations.CalleeExpectsSomeAudioAndVideo();
2587 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002588
2589 // The callee received frames, so we definitely should have nonzero "sent
2590 // bytes" stats at this point.
deadbeefd8ad7882017-04-18 16:01:17 -07002591 EXPECT_GT(caller()->OldGetStatsForTrack(audio_track)->BytesSent(), 0);
2592 EXPECT_GT(caller()->OldGetStatsForTrack(video_track)->BytesSent(), 0);
2593}
2594
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002595// Test that we can get capture start ntp time.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002596TEST_P(PeerConnectionIntegrationTest, GetCaptureStartNtpTimeWithOldStatsApi) {
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002597 ASSERT_TRUE(CreatePeerConnectionWrappers());
2598 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002599 caller()->AddAudioTrack();
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002600
Steve Anton15324772018-01-16 10:26:49 -08002601 callee()->AddAudioTrack();
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002602
2603 // Do offer/answer, wait for the callee to receive some frames.
2604 caller()->CreateAndSetAndSignalOffer();
2605 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2606
2607 // Get the remote audio track created on the receiver, so they can be used as
2608 // GetStats filters.
Steve Antonfc853712018-03-01 13:48:58 -08002609 auto receivers = callee()->pc()->GetReceivers();
2610 ASSERT_EQ(1u, receivers.size());
2611 auto remote_audio_track = receivers[0]->track();
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002612
2613 // Get the audio output level stats. Note that the level is not available
2614 // until an RTCP packet has been received.
Zhi Huange830e682018-03-30 10:48:35 -07002615 EXPECT_TRUE_WAIT(
2616 callee()->OldGetStatsForTrack(remote_audio_track)->CaptureStartNtpTime() >
2617 0,
2618 2 * kMaxWaitForFramesMs);
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002619}
2620
Steve Antona41959e2018-11-28 11:15:33 -08002621// Test that the track ID is associated with all local and remote SSRC stats
2622// using the old GetStats() and more than 1 audio and more than 1 video track.
2623// This is a regression test for crbug.com/906988
2624TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
2625 OldGetStatsAssociatesTrackIdForManyMediaSections) {
2626 ASSERT_TRUE(CreatePeerConnectionWrappers());
2627 ConnectFakeSignaling();
2628 auto audio_sender_1 = caller()->AddAudioTrack();
2629 auto video_sender_1 = caller()->AddVideoTrack();
2630 auto audio_sender_2 = caller()->AddAudioTrack();
2631 auto video_sender_2 = caller()->AddVideoTrack();
2632 caller()->CreateAndSetAndSignalOffer();
2633 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2634
2635 MediaExpectations media_expectations;
2636 media_expectations.CalleeExpectsSomeAudioAndVideo();
2637 ASSERT_TRUE_WAIT(ExpectNewFrames(media_expectations), kDefaultTimeout);
2638
2639 std::vector<std::string> track_ids = {
2640 audio_sender_1->track()->id(), video_sender_1->track()->id(),
2641 audio_sender_2->track()->id(), video_sender_2->track()->id()};
2642
2643 auto caller_stats = caller()->OldGetStats();
2644 EXPECT_THAT(caller_stats->TrackIds(), UnorderedElementsAreArray(track_ids));
2645 auto callee_stats = callee()->OldGetStats();
2646 EXPECT_THAT(callee_stats->TrackIds(), UnorderedElementsAreArray(track_ids));
2647}
2648
Steve Antonffa6ce42018-11-30 09:26:08 -08002649// Test that the new GetStats() returns stats for all outgoing/incoming streams
2650// with the correct track IDs if there are more than one audio and more than one
2651// video senders/receivers.
2652TEST_P(PeerConnectionIntegrationTest, NewGetStatsManyAudioAndManyVideoStreams) {
2653 ASSERT_TRUE(CreatePeerConnectionWrappers());
2654 ConnectFakeSignaling();
2655 auto audio_sender_1 = caller()->AddAudioTrack();
2656 auto video_sender_1 = caller()->AddVideoTrack();
2657 auto audio_sender_2 = caller()->AddAudioTrack();
2658 auto video_sender_2 = caller()->AddVideoTrack();
2659 caller()->CreateAndSetAndSignalOffer();
2660 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2661
2662 MediaExpectations media_expectations;
2663 media_expectations.CalleeExpectsSomeAudioAndVideo();
2664 ASSERT_TRUE_WAIT(ExpectNewFrames(media_expectations), kDefaultTimeout);
2665
2666 std::vector<std::string> track_ids = {
2667 audio_sender_1->track()->id(), video_sender_1->track()->id(),
2668 audio_sender_2->track()->id(), video_sender_2->track()->id()};
2669
2670 rtc::scoped_refptr<const webrtc::RTCStatsReport> caller_report =
2671 caller()->NewGetStats();
2672 ASSERT_TRUE(caller_report);
2673 auto outbound_stream_stats =
2674 caller_report->GetStatsOfType<webrtc::RTCOutboundRTPStreamStats>();
2675 ASSERT_EQ(4u, outbound_stream_stats.size());
2676 std::vector<std::string> outbound_track_ids;
2677 for (const auto& stat : outbound_stream_stats) {
2678 ASSERT_TRUE(stat->bytes_sent.is_defined());
2679 EXPECT_LT(0u, *stat->bytes_sent);
2680 ASSERT_TRUE(stat->track_id.is_defined());
2681 const auto* track_stat =
2682 caller_report->GetAs<webrtc::RTCMediaStreamTrackStats>(*stat->track_id);
2683 ASSERT_TRUE(track_stat);
2684 outbound_track_ids.push_back(*track_stat->track_identifier);
2685 }
2686 EXPECT_THAT(outbound_track_ids, UnorderedElementsAreArray(track_ids));
2687
2688 rtc::scoped_refptr<const webrtc::RTCStatsReport> callee_report =
2689 callee()->NewGetStats();
2690 ASSERT_TRUE(callee_report);
2691 auto inbound_stream_stats =
2692 callee_report->GetStatsOfType<webrtc::RTCInboundRTPStreamStats>();
2693 ASSERT_EQ(4u, inbound_stream_stats.size());
2694 std::vector<std::string> inbound_track_ids;
2695 for (const auto& stat : inbound_stream_stats) {
2696 ASSERT_TRUE(stat->bytes_received.is_defined());
2697 EXPECT_LT(0u, *stat->bytes_received);
2698 ASSERT_TRUE(stat->track_id.is_defined());
2699 const auto* track_stat =
2700 callee_report->GetAs<webrtc::RTCMediaStreamTrackStats>(*stat->track_id);
2701 ASSERT_TRUE(track_stat);
2702 inbound_track_ids.push_back(*track_stat->track_identifier);
2703 }
2704 EXPECT_THAT(inbound_track_ids, UnorderedElementsAreArray(track_ids));
2705}
2706
2707// Test that we can get stats (using the new stats implementation) for
deadbeefd8ad7882017-04-18 16:01:17 -07002708// unsignaled streams. Meaning when SSRCs/MSIDs aren't signaled explicitly in
2709// SDP.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002710TEST_P(PeerConnectionIntegrationTest,
deadbeefd8ad7882017-04-18 16:01:17 -07002711 GetStatsForUnsignaledStreamWithNewStatsApi) {
2712 ASSERT_TRUE(CreatePeerConnectionWrappers());
2713 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002714 caller()->AddAudioTrack();
deadbeefd8ad7882017-04-18 16:01:17 -07002715 // Remove SSRCs and MSIDs from the received offer SDP.
2716 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
2717 caller()->CreateAndSetAndSignalOffer();
2718 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002719 MediaExpectations media_expectations;
2720 media_expectations.CalleeExpectsSomeAudio(1);
2721 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeefd8ad7882017-04-18 16:01:17 -07002722
2723 // We received a frame, so we should have nonzero "bytes received" stats for
2724 // the unsignaled stream, if stats are working for it.
2725 rtc::scoped_refptr<const webrtc::RTCStatsReport> report =
2726 callee()->NewGetStats();
2727 ASSERT_NE(nullptr, report);
2728 auto inbound_stream_stats =
2729 report->GetStatsOfType<webrtc::RTCInboundRTPStreamStats>();
2730 ASSERT_EQ(1U, inbound_stream_stats.size());
2731 ASSERT_TRUE(inbound_stream_stats[0]->bytes_received.is_defined());
2732 ASSERT_GT(*inbound_stream_stats[0]->bytes_received, 0U);
zhihuangf8164932017-05-19 13:09:47 -07002733 ASSERT_TRUE(inbound_stream_stats[0]->track_id.is_defined());
2734}
2735
Taylor Brandstettera4653442018-06-19 09:44:26 -07002736// Same as above but for the legacy stats implementation.
2737TEST_P(PeerConnectionIntegrationTest,
2738 GetStatsForUnsignaledStreamWithOldStatsApi) {
2739 ASSERT_TRUE(CreatePeerConnectionWrappers());
2740 ConnectFakeSignaling();
2741 caller()->AddAudioTrack();
2742 // Remove SSRCs and MSIDs from the received offer SDP.
2743 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
2744 caller()->CreateAndSetAndSignalOffer();
2745 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2746
2747 // Note that, since the old stats implementation associates SSRCs with tracks
2748 // using SDP, when SSRCs aren't signaled in SDP these stats won't have an
2749 // associated track ID. So we can't use the track "selector" argument.
2750 //
2751 // Also, we use "EXPECT_TRUE_WAIT" because the stats collector may decide to
2752 // return cached stats if not enough time has passed since the last update.
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02002753 EXPECT_TRUE_WAIT(callee()->OldGetStats()->BytesReceived() > 0,
Taylor Brandstettera4653442018-06-19 09:44:26 -07002754 kDefaultTimeout);
2755}
2756
zhihuangf8164932017-05-19 13:09:47 -07002757// Test that we can successfully get the media related stats (audio level
2758// etc.) for the unsignaled stream.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002759TEST_P(PeerConnectionIntegrationTest,
zhihuangf8164932017-05-19 13:09:47 -07002760 GetMediaStatsForUnsignaledStreamWithNewStatsApi) {
2761 ASSERT_TRUE(CreatePeerConnectionWrappers());
2762 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002763 caller()->AddAudioVideoTracks();
zhihuangf8164932017-05-19 13:09:47 -07002764 // Remove SSRCs and MSIDs from the received offer SDP.
2765 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
2766 caller()->CreateAndSetAndSignalOffer();
2767 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002768 MediaExpectations media_expectations;
2769 media_expectations.CalleeExpectsSomeAudio(1);
2770 media_expectations.CalleeExpectsSomeVideo(1);
2771 ASSERT_TRUE(ExpectNewFrames(media_expectations));
zhihuangf8164932017-05-19 13:09:47 -07002772
2773 rtc::scoped_refptr<const webrtc::RTCStatsReport> report =
2774 callee()->NewGetStats();
2775 ASSERT_NE(nullptr, report);
2776
2777 auto media_stats = report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
2778 auto audio_index = FindFirstMediaStatsIndexByKind("audio", media_stats);
2779 ASSERT_GE(audio_index, 0);
2780 EXPECT_TRUE(media_stats[audio_index]->audio_level.is_defined());
deadbeef1dcb1642017-03-29 21:08:16 -07002781}
2782
deadbeef4e2deab2017-09-20 13:56:21 -07002783// Helper for test below.
2784void ModifySsrcs(cricket::SessionDescription* desc) {
2785 for (ContentInfo& content : desc->contents()) {
Steve Antondf527fd2018-04-27 15:52:03 -07002786 for (StreamParams& stream :
Steve Antonb1c1de12017-12-21 15:14:30 -08002787 content.media_description()->mutable_streams()) {
deadbeef4e2deab2017-09-20 13:56:21 -07002788 for (uint32_t& ssrc : stream.ssrcs) {
2789 ssrc = rtc::CreateRandomId();
2790 }
2791 }
2792 }
2793}
2794
2795// Test that the "RTCMediaSteamTrackStats" object is updated correctly when
2796// SSRCs are unsignaled, and the SSRC of the received (audio) stream changes.
2797// This should result in two "RTCInboundRTPStreamStats", but only one
2798// "RTCMediaStreamTrackStats", whose counters go up continuously rather than
2799// being reset to 0 once the SSRC change occurs.
2800//
2801// Regression test for this bug:
2802// https://bugs.chromium.org/p/webrtc/issues/detail?id=8158
2803//
2804// The bug causes the track stats to only represent one of the two streams:
2805// whichever one has the higher SSRC. So with this bug, there was a 50% chance
2806// that the track stat counters would reset to 0 when the new stream is
2807// received, and a 50% chance that they'll stop updating (while
2808// "concealed_samples" continues increasing, due to silence being generated for
2809// the inactive stream).
Seth Hampson2f0d7022018-02-20 11:54:42 -08002810TEST_P(PeerConnectionIntegrationTest,
Steve Anton83119dd2017-11-10 16:19:52 -08002811 TrackStatsUpdatedCorrectlyWhenUnsignaledSsrcChanges) {
deadbeef4e2deab2017-09-20 13:56:21 -07002812 ASSERT_TRUE(CreatePeerConnectionWrappers());
2813 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002814 caller()->AddAudioTrack();
deadbeef4e2deab2017-09-20 13:56:21 -07002815 // Remove SSRCs and MSIDs from the received offer SDP, simulating an endpoint
2816 // that doesn't signal SSRCs (from the callee's perspective).
2817 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
2818 caller()->CreateAndSetAndSignalOffer();
2819 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2820 // Wait for 50 audio frames (500ms of audio) to be received by the callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002821 {
2822 MediaExpectations media_expectations;
2823 media_expectations.CalleeExpectsSomeAudio(50);
2824 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2825 }
deadbeef4e2deab2017-09-20 13:56:21 -07002826 // Some audio frames were received, so we should have nonzero "samples
2827 // received" for the track.
2828 rtc::scoped_refptr<const webrtc::RTCStatsReport> report =
2829 callee()->NewGetStats();
2830 ASSERT_NE(nullptr, report);
2831 auto track_stats = report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
2832 ASSERT_EQ(1U, track_stats.size());
2833 ASSERT_TRUE(track_stats[0]->total_samples_received.is_defined());
2834 ASSERT_GT(*track_stats[0]->total_samples_received, 0U);
2835 // uint64_t prev_samples_received = *track_stats[0]->total_samples_received;
2836
2837 // Create a new offer and munge it to cause the caller to use a new SSRC.
2838 caller()->SetGeneratedSdpMunger(ModifySsrcs);
2839 caller()->CreateAndSetAndSignalOffer();
2840 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2841 // Wait for 25 more audio frames (250ms of audio) to be received, from the new
2842 // SSRC.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002843 {
2844 MediaExpectations media_expectations;
2845 media_expectations.CalleeExpectsSomeAudio(25);
2846 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2847 }
deadbeef4e2deab2017-09-20 13:56:21 -07002848
2849 report = callee()->NewGetStats();
2850 ASSERT_NE(nullptr, report);
2851 track_stats = report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
2852 ASSERT_EQ(1U, track_stats.size());
2853 ASSERT_TRUE(track_stats[0]->total_samples_received.is_defined());
2854 // The "total samples received" stat should only be greater than it was
2855 // before.
2856 // TODO(deadbeef): Uncomment this assertion once the bug is completely fixed.
2857 // Right now, the new SSRC will cause the counters to reset to 0.
2858 // EXPECT_GT(*track_stats[0]->total_samples_received, prev_samples_received);
2859
2860 // Additionally, the percentage of concealed samples (samples generated to
Steve Anton83119dd2017-11-10 16:19:52 -08002861 // conceal packet loss) should be less than 50%. If it's greater, that's a
deadbeef4e2deab2017-09-20 13:56:21 -07002862 // good sign that we're seeing stats from the old stream that's no longer
2863 // receiving packets, and is generating concealed samples of silence.
Steve Anton83119dd2017-11-10 16:19:52 -08002864 constexpr double kAcceptableConcealedSamplesPercentage = 0.50;
deadbeef4e2deab2017-09-20 13:56:21 -07002865 ASSERT_TRUE(track_stats[0]->concealed_samples.is_defined());
2866 EXPECT_LT(*track_stats[0]->concealed_samples,
2867 *track_stats[0]->total_samples_received *
2868 kAcceptableConcealedSamplesPercentage);
2869
2870 // Also ensure that we have two "RTCInboundRTPStreamStats" as expected, as a
2871 // sanity check that the SSRC really changed.
2872 // TODO(deadbeef): This isn't working right now, because we're not returning
2873 // *any* stats for the inactive stream. Uncomment when the bug is completely
2874 // fixed.
2875 // auto inbound_stream_stats =
2876 // report->GetStatsOfType<webrtc::RTCInboundRTPStreamStats>();
2877 // ASSERT_EQ(2U, inbound_stream_stats.size());
2878}
2879
deadbeef1dcb1642017-03-29 21:08:16 -07002880// Test that DTLS 1.0 is used if both sides only support DTLS 1.0.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002881TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithDtls10) {
deadbeef1dcb1642017-03-29 21:08:16 -07002882 PeerConnectionFactory::Options dtls_10_options;
2883 dtls_10_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
2884 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(dtls_10_options,
2885 dtls_10_options));
2886 ConnectFakeSignaling();
2887 // Do normal offer/answer and wait for some frames to be received in each
2888 // direction.
Steve Anton15324772018-01-16 10:26:49 -08002889 caller()->AddAudioVideoTracks();
2890 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002891 caller()->CreateAndSetAndSignalOffer();
2892 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002893 MediaExpectations media_expectations;
2894 media_expectations.ExpectBidirectionalAudioAndVideo();
2895 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002896}
2897
2898// Test getting cipher stats and UMA metrics when DTLS 1.0 is negotiated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002899TEST_P(PeerConnectionIntegrationTest, Dtls10CipherStatsAndUmaMetrics) {
deadbeef1dcb1642017-03-29 21:08:16 -07002900 PeerConnectionFactory::Options dtls_10_options;
2901 dtls_10_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
2902 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(dtls_10_options,
2903 dtls_10_options));
2904 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002905 caller()->AddAudioVideoTracks();
2906 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002907 caller()->CreateAndSetAndSignalOffer();
Qingsi Wang7fc821d2018-07-12 12:54:53 -07002908 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07002909 EXPECT_TRUE_WAIT(rtc::SSLStreamAdapter::IsAcceptableCipher(
deadbeefd8ad7882017-04-18 16:01:17 -07002910 caller()->OldGetStats()->DtlsCipher(), rtc::KT_DEFAULT),
deadbeef1dcb1642017-03-29 21:08:16 -07002911 kDefaultTimeout);
2912 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
deadbeefd8ad7882017-04-18 16:01:17 -07002913 caller()->OldGetStats()->SrtpCipher(), kDefaultTimeout);
Qingsi Wang7fc821d2018-07-12 12:54:53 -07002914 // TODO(bugs.webrtc.org/9456): Fix it.
Alex Loiko9289eda2018-11-23 16:18:59 +00002915 EXPECT_EQ(1, webrtc::metrics::NumEvents(
Qingsi Wang7fc821d2018-07-12 12:54:53 -07002916 "WebRTC.PeerConnection.SrtpCryptoSuite.Audio",
2917 kDefaultSrtpCryptoSuite));
deadbeef1dcb1642017-03-29 21:08:16 -07002918}
2919
2920// Test getting cipher stats and UMA metrics when DTLS 1.2 is negotiated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002921TEST_P(PeerConnectionIntegrationTest, Dtls12CipherStatsAndUmaMetrics) {
deadbeef1dcb1642017-03-29 21:08:16 -07002922 PeerConnectionFactory::Options dtls_12_options;
2923 dtls_12_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
2924 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(dtls_12_options,
2925 dtls_12_options));
2926 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002927 caller()->AddAudioVideoTracks();
2928 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002929 caller()->CreateAndSetAndSignalOffer();
Qingsi Wang7fc821d2018-07-12 12:54:53 -07002930 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07002931 EXPECT_TRUE_WAIT(rtc::SSLStreamAdapter::IsAcceptableCipher(
deadbeefd8ad7882017-04-18 16:01:17 -07002932 caller()->OldGetStats()->DtlsCipher(), rtc::KT_DEFAULT),
deadbeef1dcb1642017-03-29 21:08:16 -07002933 kDefaultTimeout);
2934 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
deadbeefd8ad7882017-04-18 16:01:17 -07002935 caller()->OldGetStats()->SrtpCipher(), kDefaultTimeout);
Qingsi Wang7fc821d2018-07-12 12:54:53 -07002936 // TODO(bugs.webrtc.org/9456): Fix it.
Alex Loiko9289eda2018-11-23 16:18:59 +00002937 EXPECT_EQ(1, webrtc::metrics::NumEvents(
Qingsi Wang7fc821d2018-07-12 12:54:53 -07002938 "WebRTC.PeerConnection.SrtpCryptoSuite.Audio",
2939 kDefaultSrtpCryptoSuite));
deadbeef1dcb1642017-03-29 21:08:16 -07002940}
2941
2942// Test that DTLS 1.0 can be used if the caller supports DTLS 1.2 and the
2943// callee only supports 1.0.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002944TEST_P(PeerConnectionIntegrationTest, CallerDtls12ToCalleeDtls10) {
deadbeef1dcb1642017-03-29 21:08:16 -07002945 PeerConnectionFactory::Options caller_options;
2946 caller_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
2947 PeerConnectionFactory::Options callee_options;
2948 callee_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
2949 ASSERT_TRUE(
2950 CreatePeerConnectionWrappersWithOptions(caller_options, callee_options));
2951 ConnectFakeSignaling();
2952 // Do normal offer/answer and wait for some frames to be received in each
2953 // direction.
Steve Anton15324772018-01-16 10:26:49 -08002954 caller()->AddAudioVideoTracks();
2955 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002956 caller()->CreateAndSetAndSignalOffer();
2957 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002958 MediaExpectations media_expectations;
2959 media_expectations.ExpectBidirectionalAudioAndVideo();
2960 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002961}
2962
2963// Test that DTLS 1.0 can be used if the caller only supports DTLS 1.0 and the
2964// callee supports 1.2.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002965TEST_P(PeerConnectionIntegrationTest, CallerDtls10ToCalleeDtls12) {
deadbeef1dcb1642017-03-29 21:08:16 -07002966 PeerConnectionFactory::Options caller_options;
2967 caller_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
2968 PeerConnectionFactory::Options callee_options;
2969 callee_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
2970 ASSERT_TRUE(
2971 CreatePeerConnectionWrappersWithOptions(caller_options, callee_options));
2972 ConnectFakeSignaling();
2973 // Do normal offer/answer and wait for some frames to be received in each
2974 // direction.
Steve Anton15324772018-01-16 10:26:49 -08002975 caller()->AddAudioVideoTracks();
2976 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002977 caller()->CreateAndSetAndSignalOffer();
2978 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002979 MediaExpectations media_expectations;
2980 media_expectations.ExpectBidirectionalAudioAndVideo();
2981 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002982}
2983
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07002984// The three tests below verify that "enable_aes128_sha1_32_crypto_cipher"
2985// works as expected; the cipher should only be used if enabled by both sides.
2986TEST_P(PeerConnectionIntegrationTest,
2987 Aes128Sha1_32_CipherNotUsedWhenOnlyCallerSupported) {
2988 PeerConnectionFactory::Options caller_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07002989 caller_options.crypto_options.srtp.enable_aes128_sha1_32_crypto_cipher = true;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07002990 PeerConnectionFactory::Options callee_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07002991 callee_options.crypto_options.srtp.enable_aes128_sha1_32_crypto_cipher =
2992 false;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07002993 int expected_cipher_suite = rtc::SRTP_AES128_CM_SHA1_80;
2994 TestNegotiatedCipherSuite(caller_options, callee_options,
2995 expected_cipher_suite);
2996}
2997
2998TEST_P(PeerConnectionIntegrationTest,
2999 Aes128Sha1_32_CipherNotUsedWhenOnlyCalleeSupported) {
3000 PeerConnectionFactory::Options caller_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07003001 caller_options.crypto_options.srtp.enable_aes128_sha1_32_crypto_cipher =
3002 false;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07003003 PeerConnectionFactory::Options callee_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07003004 callee_options.crypto_options.srtp.enable_aes128_sha1_32_crypto_cipher = true;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07003005 int expected_cipher_suite = rtc::SRTP_AES128_CM_SHA1_80;
3006 TestNegotiatedCipherSuite(caller_options, callee_options,
3007 expected_cipher_suite);
3008}
3009
3010TEST_P(PeerConnectionIntegrationTest, Aes128Sha1_32_CipherUsedWhenSupported) {
3011 PeerConnectionFactory::Options caller_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07003012 caller_options.crypto_options.srtp.enable_aes128_sha1_32_crypto_cipher = true;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07003013 PeerConnectionFactory::Options callee_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07003014 callee_options.crypto_options.srtp.enable_aes128_sha1_32_crypto_cipher = true;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07003015 int expected_cipher_suite = rtc::SRTP_AES128_CM_SHA1_32;
3016 TestNegotiatedCipherSuite(caller_options, callee_options,
3017 expected_cipher_suite);
3018}
3019
deadbeef1dcb1642017-03-29 21:08:16 -07003020// Test that a non-GCM cipher is used if both sides only support non-GCM.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003021TEST_P(PeerConnectionIntegrationTest, NonGcmCipherUsedWhenGcmNotSupported) {
deadbeef1dcb1642017-03-29 21:08:16 -07003022 bool local_gcm_enabled = false;
3023 bool remote_gcm_enabled = false;
3024 int expected_cipher_suite = kDefaultSrtpCryptoSuite;
3025 TestGcmNegotiationUsesCipherSuite(local_gcm_enabled, remote_gcm_enabled,
3026 expected_cipher_suite);
3027}
3028
3029// Test that a GCM cipher is used if both ends support it.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003030TEST_P(PeerConnectionIntegrationTest, GcmCipherUsedWhenGcmSupported) {
deadbeef1dcb1642017-03-29 21:08:16 -07003031 bool local_gcm_enabled = true;
3032 bool remote_gcm_enabled = true;
3033 int expected_cipher_suite = kDefaultSrtpCryptoSuiteGcm;
3034 TestGcmNegotiationUsesCipherSuite(local_gcm_enabled, remote_gcm_enabled,
3035 expected_cipher_suite);
3036}
3037
3038// Test that GCM isn't used if only the offerer supports it.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003039TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07003040 NonGcmCipherUsedWhenOnlyCallerSupportsGcm) {
3041 bool local_gcm_enabled = true;
3042 bool remote_gcm_enabled = false;
3043 int expected_cipher_suite = kDefaultSrtpCryptoSuite;
3044 TestGcmNegotiationUsesCipherSuite(local_gcm_enabled, remote_gcm_enabled,
3045 expected_cipher_suite);
3046}
3047
3048// Test that GCM isn't used if only the answerer supports it.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003049TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07003050 NonGcmCipherUsedWhenOnlyCalleeSupportsGcm) {
3051 bool local_gcm_enabled = false;
3052 bool remote_gcm_enabled = true;
3053 int expected_cipher_suite = kDefaultSrtpCryptoSuite;
3054 TestGcmNegotiationUsesCipherSuite(local_gcm_enabled, remote_gcm_enabled,
3055 expected_cipher_suite);
3056}
3057
deadbeef7914b8c2017-04-21 03:23:33 -07003058// Verify that media can be transmitted end-to-end when GCM crypto suites are
3059// enabled. Note that the above tests, such as GcmCipherUsedWhenGcmSupported,
3060// only verify that a GCM cipher is negotiated, and not necessarily that SRTP
3061// works with it.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003062TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithGcmCipher) {
deadbeef7914b8c2017-04-21 03:23:33 -07003063 PeerConnectionFactory::Options gcm_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07003064 gcm_options.crypto_options.srtp.enable_gcm_crypto_suites = true;
deadbeef7914b8c2017-04-21 03:23:33 -07003065 ASSERT_TRUE(
3066 CreatePeerConnectionWrappersWithOptions(gcm_options, gcm_options));
3067 ConnectFakeSignaling();
3068 // Do normal offer/answer and wait for some frames to be received in each
3069 // direction.
Steve Anton15324772018-01-16 10:26:49 -08003070 caller()->AddAudioVideoTracks();
3071 callee()->AddAudioVideoTracks();
deadbeef7914b8c2017-04-21 03:23:33 -07003072 caller()->CreateAndSetAndSignalOffer();
3073 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08003074 MediaExpectations media_expectations;
3075 media_expectations.ExpectBidirectionalAudioAndVideo();
3076 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef7914b8c2017-04-21 03:23:33 -07003077}
3078
deadbeef1dcb1642017-03-29 21:08:16 -07003079// This test sets up a call between two parties with audio, video and an RTP
3080// data channel.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003081TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithRtpDataChannel) {
Niels Möllerf06f9232018-08-07 12:32:18 +02003082 PeerConnectionInterface::RTCConfiguration rtc_config;
3083 rtc_config.enable_rtp_data_channel = true;
3084 rtc_config.enable_dtls_srtp = false;
3085 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(rtc_config, rtc_config));
deadbeef1dcb1642017-03-29 21:08:16 -07003086 ConnectFakeSignaling();
3087 // Expect that data channel created on caller side will show up for callee as
3088 // well.
3089 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08003090 caller()->AddAudioVideoTracks();
3091 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003092 caller()->CreateAndSetAndSignalOffer();
3093 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3094 // Ensure the existence of the RTP data channel didn't impede audio/video.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003095 MediaExpectations media_expectations;
3096 media_expectations.ExpectBidirectionalAudioAndVideo();
3097 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003098 ASSERT_NE(nullptr, caller()->data_channel());
3099 ASSERT_NE(nullptr, callee()->data_channel());
3100 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3101 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3102
3103 // Ensure data can be sent in both directions.
3104 std::string data = "hello world";
3105 SendRtpDataWithRetries(caller()->data_channel(), data, 5);
3106 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3107 kDefaultTimeout);
3108 SendRtpDataWithRetries(callee()->data_channel(), data, 5);
3109 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3110 kDefaultTimeout);
3111}
3112
3113// Ensure that an RTP data channel is signaled as closed for the caller when
3114// the callee rejects it in a subsequent offer.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003115TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07003116 RtpDataChannelSignaledClosedInCalleeOffer) {
3117 // Same procedure as above test.
Niels Möllerf06f9232018-08-07 12:32:18 +02003118 PeerConnectionInterface::RTCConfiguration rtc_config;
3119 rtc_config.enable_rtp_data_channel = true;
3120 rtc_config.enable_dtls_srtp = false;
3121 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(rtc_config, rtc_config));
deadbeef1dcb1642017-03-29 21:08:16 -07003122 ConnectFakeSignaling();
3123 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08003124 caller()->AddAudioVideoTracks();
3125 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003126 caller()->CreateAndSetAndSignalOffer();
3127 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3128 ASSERT_NE(nullptr, caller()->data_channel());
3129 ASSERT_NE(nullptr, callee()->data_channel());
3130 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3131 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3132
3133 // Close the data channel on the callee, and do an updated offer/answer.
3134 callee()->data_channel()->Close();
3135 callee()->CreateAndSetAndSignalOffer();
3136 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3137 EXPECT_FALSE(caller()->data_observer()->IsOpen());
3138 EXPECT_FALSE(callee()->data_observer()->IsOpen());
3139}
3140
3141// Tests that data is buffered in an RTP data channel until an observer is
3142// registered for it.
3143//
3144// NOTE: RTP data channels can receive data before the underlying
3145// transport has detected that a channel is writable and thus data can be
3146// received before the data channel state changes to open. That is hard to test
3147// but the same buffering is expected to be used in that case.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003148TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07003149 DataBufferedUntilRtpDataChannelObserverRegistered) {
3150 // Use fake clock and simulated network delay so that we predictably can wait
3151 // until an SCTP message has been delivered without "sleep()"ing.
3152 rtc::ScopedFakeClock fake_clock;
3153 // Some things use a time of "0" as a special value, so we need to start out
3154 // the fake clock at a nonzero time.
3155 // TODO(deadbeef): Fix this.
Sebastian Jansson5f83cf02018-05-08 14:52:22 +02003156 fake_clock.AdvanceTime(webrtc::TimeDelta::seconds(1));
deadbeef1dcb1642017-03-29 21:08:16 -07003157 virtual_socket_server()->set_delay_mean(5); // 5 ms per hop.
3158 virtual_socket_server()->UpdateDelayDistribution();
3159
Niels Möllerf06f9232018-08-07 12:32:18 +02003160 PeerConnectionInterface::RTCConfiguration rtc_config;
3161 rtc_config.enable_rtp_data_channel = true;
3162 rtc_config.enable_dtls_srtp = false;
3163 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(rtc_config, rtc_config));
deadbeef1dcb1642017-03-29 21:08:16 -07003164 ConnectFakeSignaling();
3165 caller()->CreateDataChannel();
3166 caller()->CreateAndSetAndSignalOffer();
3167 ASSERT_TRUE(caller()->data_channel() != nullptr);
3168 ASSERT_TRUE_SIMULATED_WAIT(callee()->data_channel() != nullptr,
3169 kDefaultTimeout, fake_clock);
3170 ASSERT_TRUE_SIMULATED_WAIT(caller()->data_observer()->IsOpen(),
3171 kDefaultTimeout, fake_clock);
3172 ASSERT_EQ_SIMULATED_WAIT(DataChannelInterface::kOpen,
3173 callee()->data_channel()->state(), kDefaultTimeout,
3174 fake_clock);
3175
3176 // Unregister the observer which is normally automatically registered.
3177 callee()->data_channel()->UnregisterObserver();
3178 // Send data and advance fake clock until it should have been received.
3179 std::string data = "hello world";
3180 caller()->data_channel()->Send(DataBuffer(data));
3181 SIMULATED_WAIT(false, 50, fake_clock);
3182
3183 // Attach data channel and expect data to be received immediately. Note that
3184 // EXPECT_EQ_WAIT is used, such that the simulated clock is not advanced any
3185 // further, but data can be received even if the callback is asynchronous.
3186 MockDataChannelObserver new_observer(callee()->data_channel());
3187 EXPECT_EQ_SIMULATED_WAIT(data, new_observer.last_message(), kDefaultTimeout,
3188 fake_clock);
Seth Hampson1d4a76d2018-06-19 14:31:41 -07003189 // Closing the PeerConnections destroys the ports before the ScopedFakeClock.
3190 // If this is not done a DCHECK can be hit in ports.cc, because a large
3191 // negative number is calculated for the rtt due to the global clock changing.
3192 caller()->pc()->Close();
3193 callee()->pc()->Close();
deadbeef1dcb1642017-03-29 21:08:16 -07003194}
3195
3196// This test sets up a call between two parties with audio, video and but only
3197// the caller client supports RTP data channels.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003198TEST_P(PeerConnectionIntegrationTest, RtpDataChannelsRejectedByCallee) {
Niels Möllerf06f9232018-08-07 12:32:18 +02003199 PeerConnectionInterface::RTCConfiguration rtc_config_1;
3200 rtc_config_1.enable_rtp_data_channel = true;
deadbeef1dcb1642017-03-29 21:08:16 -07003201 // Must disable DTLS to make negotiation succeed.
Niels Möllerf06f9232018-08-07 12:32:18 +02003202 rtc_config_1.enable_dtls_srtp = false;
3203 PeerConnectionInterface::RTCConfiguration rtc_config_2;
3204 rtc_config_2.enable_dtls_srtp = false;
3205 rtc_config_2.enable_dtls_srtp = false;
3206 ASSERT_TRUE(
3207 CreatePeerConnectionWrappersWithConfig(rtc_config_1, rtc_config_2));
deadbeef1dcb1642017-03-29 21:08:16 -07003208 ConnectFakeSignaling();
3209 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08003210 caller()->AddAudioVideoTracks();
3211 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003212 caller()->CreateAndSetAndSignalOffer();
3213 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3214 // The caller should still have a data channel, but it should be closed, and
3215 // one should ever have been created for the callee.
3216 EXPECT_TRUE(caller()->data_channel() != nullptr);
3217 EXPECT_FALSE(caller()->data_observer()->IsOpen());
3218 EXPECT_EQ(nullptr, callee()->data_channel());
3219}
3220
3221// This test sets up a call between two parties with audio, and video. When
3222// audio and video is setup and flowing, an RTP data channel is negotiated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003223TEST_P(PeerConnectionIntegrationTest, AddRtpDataChannelInSubsequentOffer) {
Niels Möllerf06f9232018-08-07 12:32:18 +02003224 PeerConnectionInterface::RTCConfiguration rtc_config;
3225 rtc_config.enable_rtp_data_channel = true;
3226 rtc_config.enable_dtls_srtp = false;
3227 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(rtc_config, rtc_config));
deadbeef1dcb1642017-03-29 21:08:16 -07003228 ConnectFakeSignaling();
3229 // Do initial offer/answer with audio/video.
Steve Anton15324772018-01-16 10:26:49 -08003230 caller()->AddAudioVideoTracks();
3231 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003232 caller()->CreateAndSetAndSignalOffer();
3233 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3234 // Create data channel and do new offer and answer.
3235 caller()->CreateDataChannel();
3236 caller()->CreateAndSetAndSignalOffer();
3237 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3238 ASSERT_NE(nullptr, caller()->data_channel());
3239 ASSERT_NE(nullptr, callee()->data_channel());
3240 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3241 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3242 // Ensure data can be sent in both directions.
3243 std::string data = "hello world";
3244 SendRtpDataWithRetries(caller()->data_channel(), data, 5);
3245 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3246 kDefaultTimeout);
3247 SendRtpDataWithRetries(callee()->data_channel(), data, 5);
3248 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3249 kDefaultTimeout);
3250}
3251
3252#ifdef HAVE_SCTP
3253
3254// This test sets up a call between two parties with audio, video and an SCTP
3255// data channel.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003256TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithSctpDataChannel) {
deadbeef1dcb1642017-03-29 21:08:16 -07003257 ASSERT_TRUE(CreatePeerConnectionWrappers());
3258 ConnectFakeSignaling();
3259 // Expect that data channel created on caller side will show up for callee as
3260 // well.
3261 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08003262 caller()->AddAudioVideoTracks();
3263 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003264 caller()->CreateAndSetAndSignalOffer();
3265 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3266 // Ensure the existence of the SCTP data channel didn't impede audio/video.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003267 MediaExpectations media_expectations;
3268 media_expectations.ExpectBidirectionalAudioAndVideo();
3269 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003270 // Caller data channel should already exist (it created one). Callee data
3271 // channel may not exist yet, since negotiation happens in-band, not in SDP.
3272 ASSERT_NE(nullptr, caller()->data_channel());
3273 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3274 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3275 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3276
3277 // Ensure data can be sent in both directions.
3278 std::string data = "hello world";
3279 caller()->data_channel()->Send(DataBuffer(data));
3280 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3281 kDefaultTimeout);
3282 callee()->data_channel()->Send(DataBuffer(data));
3283 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3284 kDefaultTimeout);
3285}
3286
3287// Ensure that when the callee closes an SCTP data channel, the closing
3288// procedure results in the data channel being closed for the caller as well.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003289TEST_P(PeerConnectionIntegrationTest, CalleeClosesSctpDataChannel) {
deadbeef1dcb1642017-03-29 21:08:16 -07003290 // Same procedure as above test.
3291 ASSERT_TRUE(CreatePeerConnectionWrappers());
3292 ConnectFakeSignaling();
3293 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08003294 caller()->AddAudioVideoTracks();
3295 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003296 caller()->CreateAndSetAndSignalOffer();
3297 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3298 ASSERT_NE(nullptr, caller()->data_channel());
3299 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3300 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3301 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3302
3303 // Close the data channel on the callee side, and wait for it to reach the
3304 // "closed" state on both sides.
3305 callee()->data_channel()->Close();
3306 EXPECT_TRUE_WAIT(!caller()->data_observer()->IsOpen(), kDefaultTimeout);
3307 EXPECT_TRUE_WAIT(!callee()->data_observer()->IsOpen(), kDefaultTimeout);
3308}
3309
Seth Hampson2f0d7022018-02-20 11:54:42 -08003310TEST_P(PeerConnectionIntegrationTest, SctpDataChannelConfigSentToOtherSide) {
Steve Antonda6c0952017-10-23 11:41:54 -07003311 ASSERT_TRUE(CreatePeerConnectionWrappers());
3312 ConnectFakeSignaling();
3313 webrtc::DataChannelInit init;
3314 init.id = 53;
3315 init.maxRetransmits = 52;
3316 caller()->CreateDataChannel("data-channel", &init);
Steve Anton15324772018-01-16 10:26:49 -08003317 caller()->AddAudioVideoTracks();
3318 callee()->AddAudioVideoTracks();
Steve Antonda6c0952017-10-23 11:41:54 -07003319 caller()->CreateAndSetAndSignalOffer();
3320 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Steve Anton074dece2017-10-24 13:04:12 -07003321 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3322 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
Steve Antonda6c0952017-10-23 11:41:54 -07003323 EXPECT_EQ(init.id, callee()->data_channel()->id());
3324 EXPECT_EQ("data-channel", callee()->data_channel()->label());
3325 EXPECT_EQ(init.maxRetransmits, callee()->data_channel()->maxRetransmits());
3326 EXPECT_FALSE(callee()->data_channel()->negotiated());
3327}
3328
deadbeef1dcb1642017-03-29 21:08:16 -07003329// Test usrsctp's ability to process unordered data stream, where data actually
3330// arrives out of order using simulated delays. Previously there have been some
3331// bugs in this area.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003332TEST_P(PeerConnectionIntegrationTest, StressTestUnorderedSctpDataChannel) {
deadbeef1dcb1642017-03-29 21:08:16 -07003333 // Introduce random network delays.
3334 // Otherwise it's not a true "unordered" test.
3335 virtual_socket_server()->set_delay_mean(20);
3336 virtual_socket_server()->set_delay_stddev(5);
3337 virtual_socket_server()->UpdateDelayDistribution();
3338 // Normal procedure, but with unordered data channel config.
3339 ASSERT_TRUE(CreatePeerConnectionWrappers());
3340 ConnectFakeSignaling();
3341 webrtc::DataChannelInit init;
3342 init.ordered = false;
3343 caller()->CreateDataChannel(&init);
3344 caller()->CreateAndSetAndSignalOffer();
3345 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3346 ASSERT_NE(nullptr, caller()->data_channel());
3347 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3348 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3349 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3350
3351 static constexpr int kNumMessages = 100;
3352 // Deliberately chosen to be larger than the MTU so messages get fragmented.
3353 static constexpr size_t kMaxMessageSize = 4096;
3354 // Create and send random messages.
3355 std::vector<std::string> sent_messages;
3356 for (int i = 0; i < kNumMessages; ++i) {
3357 size_t length =
3358 (rand() % kMaxMessageSize) + 1; // NOLINT (rand_r instead of rand)
3359 std::string message;
3360 ASSERT_TRUE(rtc::CreateRandomString(length, &message));
3361 caller()->data_channel()->Send(DataBuffer(message));
3362 callee()->data_channel()->Send(DataBuffer(message));
3363 sent_messages.push_back(message);
3364 }
3365
3366 // Wait for all messages to be received.
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02003367 EXPECT_EQ_WAIT(rtc::checked_cast<size_t>(kNumMessages),
deadbeef1dcb1642017-03-29 21:08:16 -07003368 caller()->data_observer()->received_message_count(),
3369 kDefaultTimeout);
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02003370 EXPECT_EQ_WAIT(rtc::checked_cast<size_t>(kNumMessages),
deadbeef1dcb1642017-03-29 21:08:16 -07003371 callee()->data_observer()->received_message_count(),
3372 kDefaultTimeout);
3373
3374 // Sort and compare to make sure none of the messages were corrupted.
3375 std::vector<std::string> caller_received_messages =
3376 caller()->data_observer()->messages();
3377 std::vector<std::string> callee_received_messages =
3378 callee()->data_observer()->messages();
Steve Anton64b626b2019-01-28 17:25:26 -08003379 absl::c_sort(sent_messages);
3380 absl::c_sort(caller_received_messages);
3381 absl::c_sort(callee_received_messages);
deadbeef1dcb1642017-03-29 21:08:16 -07003382 EXPECT_EQ(sent_messages, caller_received_messages);
3383 EXPECT_EQ(sent_messages, callee_received_messages);
3384}
3385
3386// This test sets up a call between two parties with audio, and video. When
3387// audio and video are setup and flowing, an SCTP data channel is negotiated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003388TEST_P(PeerConnectionIntegrationTest, AddSctpDataChannelInSubsequentOffer) {
deadbeef1dcb1642017-03-29 21:08:16 -07003389 ASSERT_TRUE(CreatePeerConnectionWrappers());
3390 ConnectFakeSignaling();
3391 // Do initial offer/answer with audio/video.
Steve Anton15324772018-01-16 10:26:49 -08003392 caller()->AddAudioVideoTracks();
3393 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003394 caller()->CreateAndSetAndSignalOffer();
3395 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3396 // Create data channel and do new offer and answer.
3397 caller()->CreateDataChannel();
3398 caller()->CreateAndSetAndSignalOffer();
3399 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3400 // Caller data channel should already exist (it created one). Callee data
3401 // channel may not exist yet, since negotiation happens in-band, not in SDP.
3402 ASSERT_NE(nullptr, caller()->data_channel());
3403 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3404 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3405 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3406 // Ensure data can be sent in both directions.
3407 std::string data = "hello world";
3408 caller()->data_channel()->Send(DataBuffer(data));
3409 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3410 kDefaultTimeout);
3411 callee()->data_channel()->Send(DataBuffer(data));
3412 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3413 kDefaultTimeout);
3414}
3415
deadbeef7914b8c2017-04-21 03:23:33 -07003416// Set up a connection initially just using SCTP data channels, later upgrading
3417// to audio/video, ensuring frames are received end-to-end. Effectively the
3418// inverse of the test above.
3419// This was broken in M57; see https://crbug.com/711243
Seth Hampson2f0d7022018-02-20 11:54:42 -08003420TEST_P(PeerConnectionIntegrationTest, SctpDataChannelToAudioVideoUpgrade) {
deadbeef7914b8c2017-04-21 03:23:33 -07003421 ASSERT_TRUE(CreatePeerConnectionWrappers());
3422 ConnectFakeSignaling();
3423 // Do initial offer/answer with just data channel.
3424 caller()->CreateDataChannel();
3425 caller()->CreateAndSetAndSignalOffer();
3426 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3427 // Wait until data can be sent over the data channel.
3428 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3429 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3430 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3431
3432 // Do subsequent offer/answer with two-way audio and video. Audio and video
3433 // should end up bundled on the DTLS/ICE transport already used for data.
Steve Anton15324772018-01-16 10:26:49 -08003434 caller()->AddAudioVideoTracks();
3435 callee()->AddAudioVideoTracks();
deadbeef7914b8c2017-04-21 03:23:33 -07003436 caller()->CreateAndSetAndSignalOffer();
3437 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08003438 MediaExpectations media_expectations;
3439 media_expectations.ExpectBidirectionalAudioAndVideo();
3440 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef7914b8c2017-04-21 03:23:33 -07003441}
3442
deadbeef8b7e9ad2017-05-25 09:38:55 -07003443static void MakeSpecCompliantSctpOffer(cricket::SessionDescription* desc) {
deadbeef8b7e9ad2017-05-25 09:38:55 -07003444 cricket::DataContentDescription* dcd_offer =
Steve Antonb1c1de12017-12-21 15:14:30 -08003445 GetFirstDataContentDescription(desc);
3446 ASSERT_TRUE(dcd_offer);
deadbeef8b7e9ad2017-05-25 09:38:55 -07003447 dcd_offer->set_use_sctpmap(false);
3448 dcd_offer->set_protocol("UDP/DTLS/SCTP");
3449}
3450
3451// Test that the data channel works when a spec-compliant SCTP m= section is
3452// offered (using "a=sctp-port" instead of "a=sctpmap", and using
3453// "UDP/DTLS/SCTP" as the protocol).
Seth Hampson2f0d7022018-02-20 11:54:42 -08003454TEST_P(PeerConnectionIntegrationTest,
deadbeef8b7e9ad2017-05-25 09:38:55 -07003455 DataChannelWorksWhenSpecCompliantSctpOfferReceived) {
3456 ASSERT_TRUE(CreatePeerConnectionWrappers());
3457 ConnectFakeSignaling();
3458 caller()->CreateDataChannel();
3459 caller()->SetGeneratedSdpMunger(MakeSpecCompliantSctpOffer);
3460 caller()->CreateAndSetAndSignalOffer();
3461 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3462 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3463 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3464 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3465
3466 // Ensure data can be sent in both directions.
3467 std::string data = "hello world";
3468 caller()->data_channel()->Send(DataBuffer(data));
3469 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3470 kDefaultTimeout);
3471 callee()->data_channel()->Send(DataBuffer(data));
3472 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3473 kDefaultTimeout);
3474}
3475
deadbeef1dcb1642017-03-29 21:08:16 -07003476#endif // HAVE_SCTP
3477
Bjorn Mellema2eb0a72018-11-09 10:13:51 -08003478// This test sets up a call between two parties with a media transport data
3479// channel.
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08003480TEST_P(PeerConnectionIntegrationTest, MediaTransportDataChannelEndToEnd) {
3481 PeerConnectionInterface::RTCConfiguration rtc_config;
Piotr (Peter) Slatalab1ae10b2019-03-01 11:14:05 -08003482 rtc_config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyRequire;
3483 rtc_config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08003484 rtc_config.use_media_transport_for_data_channels = true;
3485 rtc_config.enable_dtls_srtp = false; // SDES is required for media transport.
3486 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
3487 rtc_config, rtc_config, loopback_media_transports()->first_factory(),
3488 loopback_media_transports()->second_factory()));
3489 ConnectFakeSignaling();
3490
3491 // Expect that data channel created on caller side will show up for callee as
3492 // well.
3493 caller()->CreateDataChannel();
3494 caller()->CreateAndSetAndSignalOffer();
3495 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3496
3497 // Ensure that the media transport is ready.
3498 loopback_media_transports()->SetState(webrtc::MediaTransportState::kWritable);
3499 loopback_media_transports()->FlushAsyncInvokes();
3500
3501 // Caller data channel should already exist (it created one). Callee data
3502 // channel may not exist yet, since negotiation happens in-band, not in SDP.
3503 ASSERT_NE(nullptr, caller()->data_channel());
3504 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3505 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3506 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3507
3508 // Ensure data can be sent in both directions.
3509 std::string data = "hello world";
3510 caller()->data_channel()->Send(DataBuffer(data));
3511 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3512 kDefaultTimeout);
3513 callee()->data_channel()->Send(DataBuffer(data));
3514 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3515 kDefaultTimeout);
3516}
3517
3518// Ensure that when the callee closes a media transport data channel, the
3519// closing procedure results in the data channel being closed for the caller
3520// as well.
3521TEST_P(PeerConnectionIntegrationTest, MediaTransportDataChannelCalleeCloses) {
3522 PeerConnectionInterface::RTCConfiguration rtc_config;
3523 rtc_config.use_media_transport_for_data_channels = true;
3524 rtc_config.enable_dtls_srtp = false; // SDES is required for media transport.
3525 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
3526 rtc_config, rtc_config, loopback_media_transports()->first_factory(),
3527 loopback_media_transports()->second_factory()));
3528 ConnectFakeSignaling();
3529
3530 // Create a data channel on the caller and signal it to the callee.
3531 caller()->CreateDataChannel();
3532 caller()->CreateAndSetAndSignalOffer();
3533 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3534
3535 // Ensure that the media transport is ready.
3536 loopback_media_transports()->SetState(webrtc::MediaTransportState::kWritable);
3537 loopback_media_transports()->FlushAsyncInvokes();
3538
3539 // Data channels exist and open on both ends of the connection.
3540 ASSERT_NE(nullptr, caller()->data_channel());
3541 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3542 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3543 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3544
3545 // Close the data channel on the callee side, and wait for it to reach the
3546 // "closed" state on both sides.
3547 callee()->data_channel()->Close();
3548 EXPECT_TRUE_WAIT(!caller()->data_observer()->IsOpen(), kDefaultTimeout);
3549 EXPECT_TRUE_WAIT(!callee()->data_observer()->IsOpen(), kDefaultTimeout);
3550}
3551
3552TEST_P(PeerConnectionIntegrationTest,
3553 MediaTransportDataChannelConfigSentToOtherSide) {
3554 PeerConnectionInterface::RTCConfiguration rtc_config;
3555 rtc_config.use_media_transport_for_data_channels = true;
3556 rtc_config.enable_dtls_srtp = false; // SDES is required for media transport.
3557 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
3558 rtc_config, rtc_config, loopback_media_transports()->first_factory(),
3559 loopback_media_transports()->second_factory()));
3560 ConnectFakeSignaling();
3561
3562 // Create a data channel with a non-default configuration and signal it to the
3563 // callee.
3564 webrtc::DataChannelInit init;
3565 init.id = 53;
3566 init.maxRetransmits = 52;
3567 caller()->CreateDataChannel("data-channel", &init);
3568 caller()->CreateAndSetAndSignalOffer();
3569 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3570
3571 // Ensure that the media transport is ready.
3572 loopback_media_transports()->SetState(webrtc::MediaTransportState::kWritable);
3573 loopback_media_transports()->FlushAsyncInvokes();
3574
3575 // Ensure that the data channel exists on the callee with the correct
3576 // configuration.
3577 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3578 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3579 EXPECT_EQ(init.id, callee()->data_channel()->id());
3580 EXPECT_EQ("data-channel", callee()->data_channel()->label());
3581 EXPECT_EQ(init.maxRetransmits, callee()->data_channel()->maxRetransmits());
3582 EXPECT_FALSE(callee()->data_channel()->negotiated());
3583}
3584
Piotr (Peter) Slatalab1ae10b2019-03-01 11:14:05 -08003585TEST_P(PeerConnectionIntegrationTest, MediaTransportOfferUpgrade) {
3586 PeerConnectionInterface::RTCConfiguration rtc_config;
3587 rtc_config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyRequire;
3588 rtc_config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
3589 rtc_config.use_media_transport = true;
3590 rtc_config.enable_dtls_srtp = false; // SDES is required for media transport.
3591 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
3592 rtc_config, rtc_config, loopback_media_transports()->first_factory(),
3593 loopback_media_transports()->second_factory()));
3594 ConnectFakeSignaling();
3595
3596 // Do initial offer/answer with just a video track.
3597 caller()->AddVideoTrack();
3598 callee()->AddVideoTrack();
3599 caller()->CreateAndSetAndSignalOffer();
3600 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3601
3602 // Ensure that the media transport is ready.
3603 loopback_media_transports()->SetState(webrtc::MediaTransportState::kWritable);
3604 loopback_media_transports()->FlushAsyncInvokes();
3605
3606 // Now add an audio track and do another offer/answer.
3607 caller()->AddAudioTrack();
3608 callee()->AddAudioTrack();
3609 caller()->CreateAndSetAndSignalOffer();
3610 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3611
3612 // Ensure both audio and video frames are received end-to-end.
3613 MediaExpectations media_expectations;
3614 media_expectations.ExpectBidirectionalAudioAndVideo();
3615 ASSERT_TRUE(ExpectNewFrames(media_expectations));
3616
3617 // The second offer should not have generated another media transport.
3618 // Media transport was kept alive, and was not recreated.
3619 EXPECT_EQ(1, loopback_media_transports()->first_factory_transport_count());
3620 EXPECT_EQ(1, loopback_media_transports()->second_factory_transport_count());
3621}
3622
3623TEST_P(PeerConnectionIntegrationTest, MediaTransportOfferUpgradeOnTheCallee) {
3624 PeerConnectionInterface::RTCConfiguration rtc_config;
3625 rtc_config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyRequire;
3626 rtc_config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
3627 rtc_config.use_media_transport = true;
3628 rtc_config.enable_dtls_srtp = false; // SDES is required for media transport.
3629 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
3630 rtc_config, rtc_config, loopback_media_transports()->first_factory(),
3631 loopback_media_transports()->second_factory()));
3632 ConnectFakeSignaling();
3633
3634 // Do initial offer/answer with just a video track.
3635 caller()->AddVideoTrack();
3636 callee()->AddVideoTrack();
3637 caller()->CreateAndSetAndSignalOffer();
3638 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3639
3640 // Ensure that the media transport is ready.
3641 loopback_media_transports()->SetState(webrtc::MediaTransportState::kWritable);
3642 loopback_media_transports()->FlushAsyncInvokes();
3643
3644 // Now add an audio track and do another offer/answer.
3645 caller()->AddAudioTrack();
3646 callee()->AddAudioTrack();
3647 callee()->CreateAndSetAndSignalOffer();
3648 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3649
3650 // Ensure both audio and video frames are received end-to-end.
3651 MediaExpectations media_expectations;
3652 media_expectations.ExpectBidirectionalAudioAndVideo();
3653 ASSERT_TRUE(ExpectNewFrames(media_expectations));
3654
3655 // The second offer should not have generated another media transport.
3656 // Media transport was kept alive, and was not recreated.
3657 EXPECT_EQ(1, loopback_media_transports()->first_factory_transport_count());
3658 EXPECT_EQ(1, loopback_media_transports()->second_factory_transport_count());
3659}
3660
Niels Möllerc68d2822018-11-20 14:52:05 +01003661TEST_P(PeerConnectionIntegrationTest, MediaTransportBidirectionalAudio) {
3662 PeerConnectionInterface::RTCConfiguration rtc_config;
Piotr (Peter) Slatalab1ae10b2019-03-01 11:14:05 -08003663 rtc_config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyRequire;
3664 rtc_config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
Niels Möllerc68d2822018-11-20 14:52:05 +01003665 rtc_config.use_media_transport = true;
3666 rtc_config.enable_dtls_srtp = false; // SDES is required for media transport.
3667 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
3668 rtc_config, rtc_config, loopback_media_transports()->first_factory(),
3669 loopback_media_transports()->second_factory()));
3670 ConnectFakeSignaling();
3671
3672 caller()->AddAudioTrack();
3673 callee()->AddAudioTrack();
3674 // Start offer/answer exchange and wait for it to complete.
3675 caller()->CreateAndSetAndSignalOffer();
3676 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3677
3678 // Ensure that the media transport is ready.
3679 loopback_media_transports()->SetState(webrtc::MediaTransportState::kWritable);
3680 loopback_media_transports()->FlushAsyncInvokes();
3681
3682 MediaExpectations media_expectations;
3683 media_expectations.ExpectBidirectionalAudio();
3684 ASSERT_TRUE(ExpectNewFrames(media_expectations));
3685
3686 webrtc::MediaTransportPair::Stats first_stats =
3687 loopback_media_transports()->FirstStats();
3688 webrtc::MediaTransportPair::Stats second_stats =
3689 loopback_media_transports()->SecondStats();
3690
3691 EXPECT_GT(first_stats.received_audio_frames, 0);
3692 EXPECT_GE(second_stats.sent_audio_frames, first_stats.received_audio_frames);
3693
3694 EXPECT_GT(second_stats.received_audio_frames, 0);
3695 EXPECT_GE(first_stats.sent_audio_frames, second_stats.received_audio_frames);
3696}
3697
Niels Möller46879152019-01-07 15:54:47 +01003698TEST_P(PeerConnectionIntegrationTest, MediaTransportBidirectionalVideo) {
3699 PeerConnectionInterface::RTCConfiguration rtc_config;
3700 rtc_config.use_media_transport = true;
3701 rtc_config.enable_dtls_srtp = false; // SDES is required for media transport.
3702 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
3703 rtc_config, rtc_config, loopback_media_transports()->first_factory(),
3704 loopback_media_transports()->second_factory()));
3705 ConnectFakeSignaling();
3706
3707 caller()->AddVideoTrack();
3708 callee()->AddVideoTrack();
3709 // Start offer/answer exchange and wait for it to complete.
3710 caller()->CreateAndSetAndSignalOffer();
3711 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3712
3713 // Ensure that the media transport is ready.
3714 loopback_media_transports()->SetState(webrtc::MediaTransportState::kWritable);
3715 loopback_media_transports()->FlushAsyncInvokes();
3716
3717 MediaExpectations media_expectations;
3718 media_expectations.ExpectBidirectionalVideo();
3719 ASSERT_TRUE(ExpectNewFrames(media_expectations));
3720
3721 webrtc::MediaTransportPair::Stats first_stats =
3722 loopback_media_transports()->FirstStats();
3723 webrtc::MediaTransportPair::Stats second_stats =
3724 loopback_media_transports()->SecondStats();
3725
3726 EXPECT_GT(first_stats.received_video_frames, 0);
3727 EXPECT_GE(second_stats.sent_video_frames, first_stats.received_video_frames);
3728
3729 EXPECT_GT(second_stats.received_video_frames, 0);
3730 EXPECT_GE(first_stats.sent_video_frames, second_stats.received_video_frames);
3731}
3732
Piotr (Peter) Slatala55b91b92019-01-25 13:31:15 -08003733TEST_P(PeerConnectionIntegrationTest,
3734 MediaTransportDataChannelUsesRtpBidirectionalVideo) {
3735 PeerConnectionInterface::RTCConfiguration rtc_config;
3736 rtc_config.use_media_transport = false;
3737 rtc_config.use_media_transport_for_data_channels = true;
3738 rtc_config.enable_dtls_srtp = false; // SDES is required for media transport.
3739 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
3740 rtc_config, rtc_config, loopback_media_transports()->first_factory(),
3741 loopback_media_transports()->second_factory()));
3742 ConnectFakeSignaling();
3743
3744 caller()->AddVideoTrack();
3745 callee()->AddVideoTrack();
3746 // Start offer/answer exchange and wait for it to complete.
3747 caller()->CreateAndSetAndSignalOffer();
3748 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3749
3750 MediaExpectations media_expectations;
3751 media_expectations.ExpectBidirectionalVideo();
3752 ASSERT_TRUE(ExpectNewFrames(media_expectations));
3753}
3754
deadbeef1dcb1642017-03-29 21:08:16 -07003755// Test that the ICE connection and gathering states eventually reach
3756// "complete".
Seth Hampson2f0d7022018-02-20 11:54:42 -08003757TEST_P(PeerConnectionIntegrationTest, IceStatesReachCompletion) {
deadbeef1dcb1642017-03-29 21:08:16 -07003758 ASSERT_TRUE(CreatePeerConnectionWrappers());
3759 ConnectFakeSignaling();
3760 // Do normal offer/answer.
Steve Anton15324772018-01-16 10:26:49 -08003761 caller()->AddAudioVideoTracks();
3762 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003763 caller()->CreateAndSetAndSignalOffer();
3764 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3765 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceGatheringComplete,
3766 caller()->ice_gathering_state(), kMaxWaitForFramesMs);
3767 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceGatheringComplete,
3768 callee()->ice_gathering_state(), kMaxWaitForFramesMs);
3769 // After the best candidate pair is selected and all candidates are signaled,
3770 // the ICE connection state should reach "complete".
3771 // TODO(deadbeef): Currently, the ICE "controlled" agent (the
3772 // answerer/"callee" by default) only reaches "connected". When this is
3773 // fixed, this test should be updated.
3774 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
3775 caller()->ice_connection_state(), kDefaultTimeout);
Alex Loiko9289eda2018-11-23 16:18:59 +00003776 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
3777 callee()->ice_connection_state(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07003778}
3779
Qingsi Wang1dac6d82018-12-12 15:28:47 -08003780constexpr int kOnlyLocalPorts = cricket::PORTALLOCATOR_DISABLE_STUN |
3781 cricket::PORTALLOCATOR_DISABLE_RELAY |
3782 cricket::PORTALLOCATOR_DISABLE_TCP;
Zach Stein6fcdc2f2018-08-23 16:25:55 -07003783
Qingsi Wang1dac6d82018-12-12 15:28:47 -08003784// Use a mock resolver to resolve the hostname back to the original IP on both
3785// sides and check that the ICE connection connects.
Zach Stein6fcdc2f2018-08-23 16:25:55 -07003786TEST_P(PeerConnectionIntegrationTest,
3787 IceStatesReachCompletionWithRemoteHostname) {
Qingsi Wang1dac6d82018-12-12 15:28:47 -08003788 auto caller_resolver_factory =
3789 absl::make_unique<NiceMock<webrtc::MockAsyncResolverFactory>>();
3790 auto callee_resolver_factory =
3791 absl::make_unique<NiceMock<webrtc::MockAsyncResolverFactory>>();
3792 NiceMock<rtc::MockAsyncResolver> callee_async_resolver;
3793 NiceMock<rtc::MockAsyncResolver> caller_async_resolver;
Zach Stein6fcdc2f2018-08-23 16:25:55 -07003794
3795 // This also verifies that the injected AsyncResolverFactory is used by
3796 // P2PTransportChannel.
Qingsi Wang1dac6d82018-12-12 15:28:47 -08003797 EXPECT_CALL(*caller_resolver_factory, Create())
3798 .WillOnce(Return(&caller_async_resolver));
3799 webrtc::PeerConnectionDependencies caller_deps(nullptr);
3800 caller_deps.async_resolver_factory = std::move(caller_resolver_factory);
3801
3802 EXPECT_CALL(*callee_resolver_factory, Create())
3803 .WillOnce(Return(&callee_async_resolver));
3804 webrtc::PeerConnectionDependencies callee_deps(nullptr);
3805 callee_deps.async_resolver_factory = std::move(callee_resolver_factory);
3806
3807 PeerConnectionInterface::RTCConfiguration config;
3808 config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
3809 config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyRequire;
3810
3811 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndDeps(
3812 config, std::move(caller_deps), config, std::move(callee_deps)));
3813
3814 caller()->SetRemoteAsyncResolver(&callee_async_resolver);
3815 callee()->SetRemoteAsyncResolver(&caller_async_resolver);
3816
3817 // Enable hostname candidates with mDNS names.
3818 caller()->network()->CreateMdnsResponder(network_thread());
3819 callee()->network()->CreateMdnsResponder(network_thread());
3820
3821 SetPortAllocatorFlags(kOnlyLocalPorts, kOnlyLocalPorts);
Zach Stein6fcdc2f2018-08-23 16:25:55 -07003822
3823 ConnectFakeSignaling();
3824 caller()->AddAudioVideoTracks();
3825 callee()->AddAudioVideoTracks();
3826 caller()->CreateAndSetAndSignalOffer();
3827 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3828 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
3829 caller()->ice_connection_state(), kDefaultTimeout);
3830 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
3831 callee()->ice_connection_state(), kDefaultTimeout);
Jeroen de Borst833979f2018-12-13 08:25:54 -08003832
3833 EXPECT_EQ(1, webrtc::metrics::NumEvents(
3834 "WebRTC.PeerConnection.CandidatePairType_UDP",
3835 webrtc::kIceCandidatePairHostNameHostName));
Zach Stein6fcdc2f2018-08-23 16:25:55 -07003836}
3837
Steve Antonede9ca52017-10-16 13:04:27 -07003838// Test that firewalling the ICE connection causes the clients to identify the
3839// disconnected state and then removing the firewall causes them to reconnect.
3840class PeerConnectionIntegrationIceStatesTest
Seth Hampson2f0d7022018-02-20 11:54:42 -08003841 : public PeerConnectionIntegrationBaseTest,
3842 public ::testing::WithParamInterface<
3843 std::tuple<SdpSemantics, std::tuple<std::string, uint32_t>>> {
Steve Antonede9ca52017-10-16 13:04:27 -07003844 protected:
Seth Hampson2f0d7022018-02-20 11:54:42 -08003845 PeerConnectionIntegrationIceStatesTest()
3846 : PeerConnectionIntegrationBaseTest(std::get<0>(GetParam())) {
3847 port_allocator_flags_ = std::get<1>(std::get<1>(GetParam()));
Steve Antonede9ca52017-10-16 13:04:27 -07003848 }
3849
3850 void StartStunServer(const SocketAddress& server_address) {
3851 stun_server_.reset(
3852 cricket::TestStunServer::Create(network_thread(), server_address));
3853 }
3854
3855 bool TestIPv6() {
3856 return (port_allocator_flags_ & cricket::PORTALLOCATOR_ENABLE_IPV6);
3857 }
3858
3859 void SetPortAllocatorFlags() {
Qingsi Wang1dac6d82018-12-12 15:28:47 -08003860 PeerConnectionIntegrationBaseTest::SetPortAllocatorFlags(
3861 port_allocator_flags_, port_allocator_flags_);
Steve Antonede9ca52017-10-16 13:04:27 -07003862 }
3863
3864 std::vector<SocketAddress> CallerAddresses() {
3865 std::vector<SocketAddress> addresses;
3866 addresses.push_back(SocketAddress("1.1.1.1", 0));
3867 if (TestIPv6()) {
3868 addresses.push_back(SocketAddress("1111:0:a:b:c:d:e:f", 0));
3869 }
3870 return addresses;
3871 }
3872
3873 std::vector<SocketAddress> CalleeAddresses() {
3874 std::vector<SocketAddress> addresses;
3875 addresses.push_back(SocketAddress("2.2.2.2", 0));
3876 if (TestIPv6()) {
3877 addresses.push_back(SocketAddress("2222:0:a:b:c:d:e:f", 0));
3878 }
3879 return addresses;
3880 }
3881
3882 void SetUpNetworkInterfaces() {
3883 // Remove the default interfaces added by the test infrastructure.
3884 caller()->network()->RemoveInterface(kDefaultLocalAddress);
3885 callee()->network()->RemoveInterface(kDefaultLocalAddress);
3886
3887 // Add network addresses for test.
3888 for (const auto& caller_address : CallerAddresses()) {
3889 caller()->network()->AddInterface(caller_address);
3890 }
3891 for (const auto& callee_address : CalleeAddresses()) {
3892 callee()->network()->AddInterface(callee_address);
3893 }
3894 }
3895
3896 private:
3897 uint32_t port_allocator_flags_;
3898 std::unique_ptr<cricket::TestStunServer> stun_server_;
3899};
3900
3901// Tests that the PeerConnection goes through all the ICE gathering/connection
3902// states over the duration of the call. This includes Disconnected and Failed
3903// states, induced by putting a firewall between the peers and waiting for them
3904// to time out.
Steve Anton83119dd2017-11-10 16:19:52 -08003905TEST_P(PeerConnectionIntegrationIceStatesTest, VerifyIceStates) {
Jonas Olssonb75d9e92019-02-22 10:33:29 +01003906 rtc::ScopedFakeClock fake_clock;
3907 // Some things use a time of "0" as a special value, so we need to start out
3908 // the fake clock at a nonzero time.
3909 fake_clock.AdvanceTime(TimeDelta::seconds(1));
Steve Antonede9ca52017-10-16 13:04:27 -07003910
3911 const SocketAddress kStunServerAddress =
3912 SocketAddress("99.99.99.1", cricket::STUN_SERVER_PORT);
3913 StartStunServer(kStunServerAddress);
3914
3915 PeerConnectionInterface::RTCConfiguration config;
3916 PeerConnectionInterface::IceServer ice_stun_server;
3917 ice_stun_server.urls.push_back(
3918 "stun:" + kStunServerAddress.HostAsURIString() + ":" +
3919 kStunServerAddress.PortAsString());
3920 config.servers.push_back(ice_stun_server);
3921
3922 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
3923 ConnectFakeSignaling();
3924 SetPortAllocatorFlags();
3925 SetUpNetworkInterfaces();
Steve Anton15324772018-01-16 10:26:49 -08003926 caller()->AddAudioVideoTracks();
3927 callee()->AddAudioVideoTracks();
Steve Antonede9ca52017-10-16 13:04:27 -07003928
3929 // Initial state before anything happens.
3930 ASSERT_EQ(PeerConnectionInterface::kIceGatheringNew,
3931 caller()->ice_gathering_state());
3932 ASSERT_EQ(PeerConnectionInterface::kIceConnectionNew,
3933 caller()->ice_connection_state());
Jonas Olsson7a6739e2019-01-15 16:31:55 +01003934 ASSERT_EQ(PeerConnectionInterface::kIceConnectionNew,
3935 caller()->standardized_ice_connection_state());
Steve Antonede9ca52017-10-16 13:04:27 -07003936
3937 // Start the call by creating the offer, setting it as the local description,
3938 // then sending it to the peer who will respond with an answer. This happens
3939 // asynchronously so that we can watch the states as it runs in the
3940 // background.
3941 caller()->CreateAndSetAndSignalOffer();
3942
Jonas Olsson7a6739e2019-01-15 16:31:55 +01003943 ASSERT_EQ(PeerConnectionInterface::kIceConnectionCompleted,
3944 caller()->ice_connection_state());
Jonas Olssonacd8ae72019-02-25 15:26:24 +01003945 ASSERT_EQ(PeerConnectionInterface::kIceConnectionCompleted,
Jonas Olsson7a6739e2019-01-15 16:31:55 +01003946 caller()->standardized_ice_connection_state());
Steve Antonede9ca52017-10-16 13:04:27 -07003947
3948 // Verify that the observer was notified of the intermediate transitions.
3949 EXPECT_THAT(caller()->ice_connection_state_history(),
3950 ElementsAre(PeerConnectionInterface::kIceConnectionChecking,
3951 PeerConnectionInterface::kIceConnectionConnected,
3952 PeerConnectionInterface::kIceConnectionCompleted));
Jonas Olssonacd8ae72019-02-25 15:26:24 +01003953 EXPECT_THAT(caller()->standardized_ice_connection_state_history(),
3954 ElementsAre(PeerConnectionInterface::kIceConnectionChecking,
3955 PeerConnectionInterface::kIceConnectionConnected,
3956 PeerConnectionInterface::kIceConnectionCompleted));
Jonas Olsson635474e2018-10-18 15:58:17 +02003957 EXPECT_THAT(
3958 caller()->peer_connection_state_history(),
3959 ElementsAre(PeerConnectionInterface::PeerConnectionState::kConnecting,
Jonas Olsson635474e2018-10-18 15:58:17 +02003960 PeerConnectionInterface::PeerConnectionState::kConnected));
Steve Antonede9ca52017-10-16 13:04:27 -07003961 EXPECT_THAT(caller()->ice_gathering_state_history(),
3962 ElementsAre(PeerConnectionInterface::kIceGatheringGathering,
3963 PeerConnectionInterface::kIceGatheringComplete));
3964
3965 // Block connections to/from the caller and wait for ICE to become
3966 // disconnected.
3967 for (const auto& caller_address : CallerAddresses()) {
3968 firewall()->AddRule(false, rtc::FP_ANY, rtc::FD_ANY, caller_address);
3969 }
Mirko Bonadei675513b2017-11-09 11:09:25 +01003970 RTC_LOG(LS_INFO) << "Firewall rules applied";
Jonas Olssonb75d9e92019-02-22 10:33:29 +01003971 ASSERT_EQ_SIMULATED_WAIT(PeerConnectionInterface::kIceConnectionDisconnected,
3972 caller()->ice_connection_state(), kDefaultTimeout,
3973 fake_clock);
3974 ASSERT_EQ_SIMULATED_WAIT(PeerConnectionInterface::kIceConnectionDisconnected,
3975 caller()->standardized_ice_connection_state(),
3976 kDefaultTimeout, fake_clock);
Steve Antonede9ca52017-10-16 13:04:27 -07003977
3978 // Let ICE re-establish by removing the firewall rules.
3979 firewall()->ClearRules();
Mirko Bonadei675513b2017-11-09 11:09:25 +01003980 RTC_LOG(LS_INFO) << "Firewall rules cleared";
Jonas Olssonb75d9e92019-02-22 10:33:29 +01003981 ASSERT_EQ_SIMULATED_WAIT(PeerConnectionInterface::kIceConnectionCompleted,
3982 caller()->ice_connection_state(), kDefaultTimeout,
3983 fake_clock);
Jonas Olssonacd8ae72019-02-25 15:26:24 +01003984 ASSERT_EQ_SIMULATED_WAIT(PeerConnectionInterface::kIceConnectionCompleted,
Jonas Olssonb75d9e92019-02-22 10:33:29 +01003985 caller()->standardized_ice_connection_state(),
3986 kDefaultTimeout, fake_clock);
Steve Antonede9ca52017-10-16 13:04:27 -07003987
3988 // According to RFC7675, if there is no response within 30 seconds then the
3989 // peer should consider the other side to have rejected the connection. This
Steve Anton83119dd2017-11-10 16:19:52 -08003990 // is signaled by the state transitioning to "failed".
Steve Antonede9ca52017-10-16 13:04:27 -07003991 constexpr int kConsentTimeout = 30000;
3992 for (const auto& caller_address : CallerAddresses()) {
3993 firewall()->AddRule(false, rtc::FP_ANY, rtc::FD_ANY, caller_address);
3994 }
Mirko Bonadei675513b2017-11-09 11:09:25 +01003995 RTC_LOG(LS_INFO) << "Firewall rules applied again";
Jonas Olssonb75d9e92019-02-22 10:33:29 +01003996 ASSERT_EQ_SIMULATED_WAIT(PeerConnectionInterface::kIceConnectionFailed,
3997 caller()->ice_connection_state(), kConsentTimeout,
3998 fake_clock);
3999 ASSERT_EQ_SIMULATED_WAIT(PeerConnectionInterface::kIceConnectionFailed,
4000 caller()->standardized_ice_connection_state(),
4001 kConsentTimeout, fake_clock);
4002
4003 // We need to manually close the peerconnections before the fake clock goes
4004 // out of scope, or we trigger a DCHECK in rtp_sender.cc when we briefly
4005 // return to using non-faked time.
4006 delete SetCallerPcWrapperAndReturnCurrent(nullptr);
4007 delete SetCalleePcWrapperAndReturnCurrent(nullptr);
4008}
4009
4010// Tests that if the connection doesn't get set up properly we eventually reach
4011// the "failed" iceConnectionState.
4012TEST_P(PeerConnectionIntegrationIceStatesTest, IceStateSetupFailure) {
4013 rtc::ScopedFakeClock fake_clock;
4014 // Some things use a time of "0" as a special value, so we need to start out
4015 // the fake clock at a nonzero time.
4016 fake_clock.AdvanceTime(TimeDelta::seconds(1));
4017
4018 // Block connections to/from the caller and wait for ICE to become
4019 // disconnected.
4020 for (const auto& caller_address : CallerAddresses()) {
4021 firewall()->AddRule(false, rtc::FP_ANY, rtc::FD_ANY, caller_address);
4022 }
4023
4024 ASSERT_TRUE(CreatePeerConnectionWrappers());
4025 ConnectFakeSignaling();
4026 SetPortAllocatorFlags();
4027 SetUpNetworkInterfaces();
4028 caller()->AddAudioVideoTracks();
4029 caller()->CreateAndSetAndSignalOffer();
4030
4031 // According to RFC7675, if there is no response within 30 seconds then the
4032 // peer should consider the other side to have rejected the connection. This
4033 // is signaled by the state transitioning to "failed".
4034 constexpr int kConsentTimeout = 30000;
4035 ASSERT_EQ_SIMULATED_WAIT(PeerConnectionInterface::kIceConnectionFailed,
4036 caller()->standardized_ice_connection_state(),
4037 kConsentTimeout, fake_clock);
4038
4039 // We need to manually close the peerconnections before the fake clock goes
4040 // out of scope, or we trigger a DCHECK in rtp_sender.cc when we briefly
4041 // return to using non-faked time.
4042 delete SetCallerPcWrapperAndReturnCurrent(nullptr);
4043 delete SetCalleePcWrapperAndReturnCurrent(nullptr);
Steve Antonede9ca52017-10-16 13:04:27 -07004044}
4045
4046// Tests that the best connection is set to the appropriate IPv4/IPv6 connection
4047// and that the statistics in the metric observers are updated correctly.
4048TEST_P(PeerConnectionIntegrationIceStatesTest, VerifyBestConnection) {
4049 ASSERT_TRUE(CreatePeerConnectionWrappers());
4050 ConnectFakeSignaling();
4051 SetPortAllocatorFlags();
4052 SetUpNetworkInterfaces();
Steve Anton15324772018-01-16 10:26:49 -08004053 caller()->AddAudioVideoTracks();
4054 callee()->AddAudioVideoTracks();
Steve Antonede9ca52017-10-16 13:04:27 -07004055 caller()->CreateAndSetAndSignalOffer();
4056
4057 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4058
Qingsi Wang7fc821d2018-07-12 12:54:53 -07004059 // TODO(bugs.webrtc.org/9456): Fix it.
4060 const int num_best_ipv4 = webrtc::metrics::NumEvents(
4061 "WebRTC.PeerConnection.IPMetrics", webrtc::kBestConnections_IPv4);
4062 const int num_best_ipv6 = webrtc::metrics::NumEvents(
4063 "WebRTC.PeerConnection.IPMetrics", webrtc::kBestConnections_IPv6);
Steve Antonede9ca52017-10-16 13:04:27 -07004064 if (TestIPv6()) {
4065 // When IPv6 is enabled, we should prefer an IPv6 connection over an IPv4
4066 // connection.
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02004067 EXPECT_EQ(0, num_best_ipv4);
4068 EXPECT_EQ(1, num_best_ipv6);
Steve Antonede9ca52017-10-16 13:04:27 -07004069 } else {
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02004070 EXPECT_EQ(1, num_best_ipv4);
4071 EXPECT_EQ(0, num_best_ipv6);
Steve Antonede9ca52017-10-16 13:04:27 -07004072 }
4073
Qingsi Wang7fc821d2018-07-12 12:54:53 -07004074 EXPECT_EQ(0, webrtc::metrics::NumEvents(
4075 "WebRTC.PeerConnection.CandidatePairType_UDP",
4076 webrtc::kIceCandidatePairHostHost));
4077 EXPECT_EQ(1, webrtc::metrics::NumEvents(
4078 "WebRTC.PeerConnection.CandidatePairType_UDP",
4079 webrtc::kIceCandidatePairHostPublicHostPublic));
Steve Antonede9ca52017-10-16 13:04:27 -07004080}
4081
4082constexpr uint32_t kFlagsIPv4NoStun = cricket::PORTALLOCATOR_DISABLE_TCP |
4083 cricket::PORTALLOCATOR_DISABLE_STUN |
4084 cricket::PORTALLOCATOR_DISABLE_RELAY;
4085constexpr uint32_t kFlagsIPv6NoStun =
4086 cricket::PORTALLOCATOR_DISABLE_TCP | cricket::PORTALLOCATOR_DISABLE_STUN |
4087 cricket::PORTALLOCATOR_ENABLE_IPV6 | cricket::PORTALLOCATOR_DISABLE_RELAY;
4088constexpr uint32_t kFlagsIPv4Stun =
4089 cricket::PORTALLOCATOR_DISABLE_TCP | cricket::PORTALLOCATOR_DISABLE_RELAY;
4090
Mirko Bonadeic84f6612019-01-31 12:20:57 +01004091INSTANTIATE_TEST_SUITE_P(
Seth Hampson2f0d7022018-02-20 11:54:42 -08004092 PeerConnectionIntegrationTest,
4093 PeerConnectionIntegrationIceStatesTest,
4094 Combine(Values(SdpSemantics::kPlanB, SdpSemantics::kUnifiedPlan),
4095 Values(std::make_pair("IPv4 no STUN", kFlagsIPv4NoStun),
4096 std::make_pair("IPv6 no STUN", kFlagsIPv6NoStun),
4097 std::make_pair("IPv4 with STUN", kFlagsIPv4Stun))));
Steve Antonede9ca52017-10-16 13:04:27 -07004098
deadbeef1dcb1642017-03-29 21:08:16 -07004099// This test sets up a call between two parties with audio and video.
4100// During the call, the caller restarts ICE and the test verifies that
4101// new ICE candidates are generated and audio and video still can flow, and the
4102// ICE state reaches completed again.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004103TEST_P(PeerConnectionIntegrationTest, MediaContinuesFlowingAfterIceRestart) {
deadbeef1dcb1642017-03-29 21:08:16 -07004104 ASSERT_TRUE(CreatePeerConnectionWrappers());
4105 ConnectFakeSignaling();
4106 // Do normal offer/answer and wait for ICE to complete.
Steve Anton15324772018-01-16 10:26:49 -08004107 caller()->AddAudioVideoTracks();
4108 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07004109 caller()->CreateAndSetAndSignalOffer();
4110 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4111 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
4112 caller()->ice_connection_state(), kMaxWaitForFramesMs);
Alex Loiko9289eda2018-11-23 16:18:59 +00004113 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
4114 callee()->ice_connection_state(), kMaxWaitForFramesMs);
deadbeef1dcb1642017-03-29 21:08:16 -07004115
4116 // To verify that the ICE restart actually occurs, get
4117 // ufrag/password/candidates before and after restart.
4118 // Create an SDP string of the first audio candidate for both clients.
4119 const webrtc::IceCandidateCollection* audio_candidates_caller =
4120 caller()->pc()->local_description()->candidates(0);
4121 const webrtc::IceCandidateCollection* audio_candidates_callee =
4122 callee()->pc()->local_description()->candidates(0);
4123 ASSERT_GT(audio_candidates_caller->count(), 0u);
4124 ASSERT_GT(audio_candidates_callee->count(), 0u);
4125 std::string caller_candidate_pre_restart;
4126 ASSERT_TRUE(
4127 audio_candidates_caller->at(0)->ToString(&caller_candidate_pre_restart));
4128 std::string callee_candidate_pre_restart;
4129 ASSERT_TRUE(
4130 audio_candidates_callee->at(0)->ToString(&callee_candidate_pre_restart));
4131 const cricket::SessionDescription* desc =
4132 caller()->pc()->local_description()->description();
4133 std::string caller_ufrag_pre_restart =
4134 desc->transport_infos()[0].description.ice_ufrag;
4135 desc = callee()->pc()->local_description()->description();
4136 std::string callee_ufrag_pre_restart =
4137 desc->transport_infos()[0].description.ice_ufrag;
4138
4139 // Have the caller initiate an ICE restart.
4140 caller()->SetOfferAnswerOptions(IceRestartOfferAnswerOptions());
4141 caller()->CreateAndSetAndSignalOffer();
4142 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4143 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
4144 caller()->ice_connection_state(), kMaxWaitForFramesMs);
Alex Loiko9289eda2018-11-23 16:18:59 +00004145 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
deadbeef1dcb1642017-03-29 21:08:16 -07004146 callee()->ice_connection_state(), kMaxWaitForFramesMs);
4147
4148 // Grab the ufrags/candidates again.
4149 audio_candidates_caller = caller()->pc()->local_description()->candidates(0);
4150 audio_candidates_callee = callee()->pc()->local_description()->candidates(0);
4151 ASSERT_GT(audio_candidates_caller->count(), 0u);
4152 ASSERT_GT(audio_candidates_callee->count(), 0u);
4153 std::string caller_candidate_post_restart;
4154 ASSERT_TRUE(
4155 audio_candidates_caller->at(0)->ToString(&caller_candidate_post_restart));
4156 std::string callee_candidate_post_restart;
4157 ASSERT_TRUE(
4158 audio_candidates_callee->at(0)->ToString(&callee_candidate_post_restart));
4159 desc = caller()->pc()->local_description()->description();
4160 std::string caller_ufrag_post_restart =
4161 desc->transport_infos()[0].description.ice_ufrag;
4162 desc = callee()->pc()->local_description()->description();
4163 std::string callee_ufrag_post_restart =
4164 desc->transport_infos()[0].description.ice_ufrag;
4165 // Sanity check that an ICE restart was actually negotiated in SDP.
4166 ASSERT_NE(caller_candidate_pre_restart, caller_candidate_post_restart);
4167 ASSERT_NE(callee_candidate_pre_restart, callee_candidate_post_restart);
4168 ASSERT_NE(caller_ufrag_pre_restart, caller_ufrag_post_restart);
4169 ASSERT_NE(callee_ufrag_pre_restart, callee_ufrag_post_restart);
4170
4171 // Ensure that additional frames are received after the ICE restart.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004172 MediaExpectations media_expectations;
4173 media_expectations.ExpectBidirectionalAudioAndVideo();
4174 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07004175}
4176
4177// Verify that audio/video can be received end-to-end when ICE renomination is
4178// enabled.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004179TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithIceRenomination) {
deadbeef1dcb1642017-03-29 21:08:16 -07004180 PeerConnectionInterface::RTCConfiguration config;
4181 config.enable_ice_renomination = true;
4182 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
4183 ConnectFakeSignaling();
4184 // Do normal offer/answer and wait for some frames to be received in each
4185 // direction.
Steve Anton15324772018-01-16 10:26:49 -08004186 caller()->AddAudioVideoTracks();
4187 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07004188 caller()->CreateAndSetAndSignalOffer();
4189 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4190 // Sanity check that ICE renomination was actually negotiated.
4191 const cricket::SessionDescription* desc =
4192 caller()->pc()->local_description()->description();
4193 for (const cricket::TransportInfo& info : desc->transport_infos()) {
Steve Anton64b626b2019-01-28 17:25:26 -08004194 ASSERT_THAT(info.description.transport_options, Contains("renomination"));
deadbeef1dcb1642017-03-29 21:08:16 -07004195 }
4196 desc = callee()->pc()->local_description()->description();
4197 for (const cricket::TransportInfo& info : desc->transport_infos()) {
Steve Anton64b626b2019-01-28 17:25:26 -08004198 ASSERT_THAT(info.description.transport_options, Contains("renomination"));
deadbeef1dcb1642017-03-29 21:08:16 -07004199 }
Seth Hampson2f0d7022018-02-20 11:54:42 -08004200 MediaExpectations media_expectations;
4201 media_expectations.ExpectBidirectionalAudioAndVideo();
4202 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07004203}
4204
Steve Anton6f25b092017-10-23 09:39:20 -07004205// With a max bundle policy and RTCP muxing, adding a new media description to
4206// the connection should not affect ICE at all because the new media will use
4207// the existing connection.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004208TEST_P(PeerConnectionIntegrationTest,
Steve Anton83119dd2017-11-10 16:19:52 -08004209 AddMediaToConnectedBundleDoesNotRestartIce) {
Steve Anton6f25b092017-10-23 09:39:20 -07004210 PeerConnectionInterface::RTCConfiguration config;
4211 config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
4212 config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyRequire;
4213 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(
4214 config, PeerConnectionInterface::RTCConfiguration()));
4215 ConnectFakeSignaling();
4216
Steve Anton15324772018-01-16 10:26:49 -08004217 caller()->AddAudioTrack();
Steve Anton6f25b092017-10-23 09:39:20 -07004218 caller()->CreateAndSetAndSignalOffer();
4219 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Steve Antonff52f1b2017-10-26 12:24:50 -07004220 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceConnectionCompleted,
4221 caller()->ice_connection_state(), kDefaultTimeout);
Steve Anton6f25b092017-10-23 09:39:20 -07004222
4223 caller()->clear_ice_connection_state_history();
4224
Steve Anton15324772018-01-16 10:26:49 -08004225 caller()->AddVideoTrack();
Steve Anton6f25b092017-10-23 09:39:20 -07004226 caller()->CreateAndSetAndSignalOffer();
4227 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4228
4229 EXPECT_EQ(0u, caller()->ice_connection_state_history().size());
4230}
4231
deadbeef1dcb1642017-03-29 21:08:16 -07004232// This test sets up a call between two parties with audio and video. It then
4233// renegotiates setting the video m-line to "port 0", then later renegotiates
4234// again, enabling video.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004235TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07004236 VideoFlowsAfterMediaSectionIsRejectedAndRecycled) {
4237 ASSERT_TRUE(CreatePeerConnectionWrappers());
4238 ConnectFakeSignaling();
4239
4240 // Do initial negotiation, only sending media from the caller. Will result in
4241 // video and audio recvonly "m=" sections.
Steve Anton15324772018-01-16 10:26:49 -08004242 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07004243 caller()->CreateAndSetAndSignalOffer();
4244 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4245
4246 // Negotiate again, disabling the video "m=" section (the callee will set the
4247 // port to 0 due to offer_to_receive_video = 0).
Seth Hampson2f0d7022018-02-20 11:54:42 -08004248 if (sdp_semantics_ == SdpSemantics::kPlanB) {
4249 PeerConnectionInterface::RTCOfferAnswerOptions options;
4250 options.offer_to_receive_video = 0;
4251 callee()->SetOfferAnswerOptions(options);
4252 } else {
4253 callee()->SetRemoteOfferHandler([this] {
4254 callee()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)->Stop();
4255 });
4256 }
deadbeef1dcb1642017-03-29 21:08:16 -07004257 caller()->CreateAndSetAndSignalOffer();
4258 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4259 // Sanity check that video "m=" section was actually rejected.
4260 const ContentInfo* answer_video_content = cricket::GetFirstVideoContent(
4261 callee()->pc()->local_description()->description());
4262 ASSERT_NE(nullptr, answer_video_content);
4263 ASSERT_TRUE(answer_video_content->rejected);
4264
4265 // Enable video and do negotiation again, making sure video is received
4266 // end-to-end, also adding media stream to callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004267 if (sdp_semantics_ == SdpSemantics::kPlanB) {
4268 PeerConnectionInterface::RTCOfferAnswerOptions options;
4269 options.offer_to_receive_video = 1;
4270 callee()->SetOfferAnswerOptions(options);
4271 } else {
4272 // The caller's transceiver is stopped, so we need to add another track.
4273 auto caller_transceiver =
4274 caller()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO);
4275 EXPECT_TRUE(caller_transceiver->stopped());
4276 caller()->AddVideoTrack();
4277 }
4278 callee()->AddVideoTrack();
4279 callee()->SetRemoteOfferHandler(nullptr);
deadbeef1dcb1642017-03-29 21:08:16 -07004280 caller()->CreateAndSetAndSignalOffer();
4281 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08004282
deadbeef1dcb1642017-03-29 21:08:16 -07004283 // Verify the caller receives frames from the newly added stream, and the
4284 // callee receives additional frames from the re-enabled video m= section.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004285 MediaExpectations media_expectations;
4286 media_expectations.CalleeExpectsSomeAudio();
4287 media_expectations.ExpectBidirectionalVideo();
4288 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07004289}
4290
deadbeef1dcb1642017-03-29 21:08:16 -07004291// This tests that if we negotiate after calling CreateSender but before we
4292// have a track, then set a track later, frames from the newly-set track are
4293// received end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004294TEST_F(PeerConnectionIntegrationTestPlanB,
deadbeef1dcb1642017-03-29 21:08:16 -07004295 MediaFlowsAfterEarlyWarmupWithCreateSender) {
4296 ASSERT_TRUE(CreatePeerConnectionWrappers());
4297 ConnectFakeSignaling();
4298 auto caller_audio_sender =
4299 caller()->pc()->CreateSender("audio", "caller_stream");
4300 auto caller_video_sender =
4301 caller()->pc()->CreateSender("video", "caller_stream");
4302 auto callee_audio_sender =
4303 callee()->pc()->CreateSender("audio", "callee_stream");
4304 auto callee_video_sender =
4305 callee()->pc()->CreateSender("video", "callee_stream");
4306 caller()->CreateAndSetAndSignalOffer();
4307 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
4308 // Wait for ICE to complete, without any tracks being set.
4309 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
4310 caller()->ice_connection_state(), kMaxWaitForFramesMs);
4311 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
4312 callee()->ice_connection_state(), kMaxWaitForFramesMs);
4313 // Now set the tracks, and expect frames to immediately start flowing.
4314 EXPECT_TRUE(caller_audio_sender->SetTrack(caller()->CreateLocalAudioTrack()));
4315 EXPECT_TRUE(caller_video_sender->SetTrack(caller()->CreateLocalVideoTrack()));
4316 EXPECT_TRUE(callee_audio_sender->SetTrack(callee()->CreateLocalAudioTrack()));
4317 EXPECT_TRUE(callee_video_sender->SetTrack(callee()->CreateLocalVideoTrack()));
Seth Hampson2f0d7022018-02-20 11:54:42 -08004318 MediaExpectations media_expectations;
4319 media_expectations.ExpectBidirectionalAudioAndVideo();
4320 ASSERT_TRUE(ExpectNewFrames(media_expectations));
4321}
4322
4323// This tests that if we negotiate after calling AddTransceiver but before we
4324// have a track, then set a track later, frames from the newly-set tracks are
4325// received end-to-end.
4326TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
4327 MediaFlowsAfterEarlyWarmupWithAddTransceiver) {
4328 ASSERT_TRUE(CreatePeerConnectionWrappers());
4329 ConnectFakeSignaling();
4330 auto audio_result = caller()->pc()->AddTransceiver(cricket::MEDIA_TYPE_AUDIO);
4331 ASSERT_EQ(RTCErrorType::NONE, audio_result.error().type());
4332 auto caller_audio_sender = audio_result.MoveValue()->sender();
4333 auto video_result = caller()->pc()->AddTransceiver(cricket::MEDIA_TYPE_VIDEO);
4334 ASSERT_EQ(RTCErrorType::NONE, video_result.error().type());
4335 auto caller_video_sender = video_result.MoveValue()->sender();
4336 callee()->SetRemoteOfferHandler([this] {
4337 ASSERT_EQ(2u, callee()->pc()->GetTransceivers().size());
4338 callee()->pc()->GetTransceivers()[0]->SetDirection(
4339 RtpTransceiverDirection::kSendRecv);
4340 callee()->pc()->GetTransceivers()[1]->SetDirection(
4341 RtpTransceiverDirection::kSendRecv);
4342 });
4343 caller()->CreateAndSetAndSignalOffer();
4344 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
4345 // Wait for ICE to complete, without any tracks being set.
4346 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
4347 caller()->ice_connection_state(), kMaxWaitForFramesMs);
4348 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
4349 callee()->ice_connection_state(), kMaxWaitForFramesMs);
4350 // Now set the tracks, and expect frames to immediately start flowing.
4351 auto callee_audio_sender = callee()->pc()->GetSenders()[0];
4352 auto callee_video_sender = callee()->pc()->GetSenders()[1];
4353 ASSERT_TRUE(caller_audio_sender->SetTrack(caller()->CreateLocalAudioTrack()));
4354 ASSERT_TRUE(caller_video_sender->SetTrack(caller()->CreateLocalVideoTrack()));
4355 ASSERT_TRUE(callee_audio_sender->SetTrack(callee()->CreateLocalAudioTrack()));
4356 ASSERT_TRUE(callee_video_sender->SetTrack(callee()->CreateLocalVideoTrack()));
4357 MediaExpectations media_expectations;
4358 media_expectations.ExpectBidirectionalAudioAndVideo();
4359 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07004360}
4361
4362// This test verifies that a remote video track can be added via AddStream,
4363// and sent end-to-end. For this particular test, it's simply echoed back
4364// from the caller to the callee, rather than being forwarded to a third
4365// PeerConnection.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004366TEST_F(PeerConnectionIntegrationTestPlanB, CanSendRemoteVideoTrack) {
deadbeef1dcb1642017-03-29 21:08:16 -07004367 ASSERT_TRUE(CreatePeerConnectionWrappers());
4368 ConnectFakeSignaling();
4369 // Just send a video track from the caller.
Steve Anton15324772018-01-16 10:26:49 -08004370 caller()->AddVideoTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07004371 caller()->CreateAndSetAndSignalOffer();
4372 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02004373 ASSERT_EQ(1U, callee()->remote_streams()->count());
deadbeef1dcb1642017-03-29 21:08:16 -07004374
4375 // Echo the stream back, and do a new offer/anwer (initiated by callee this
4376 // time).
4377 callee()->pc()->AddStream(callee()->remote_streams()->at(0));
4378 callee()->CreateAndSetAndSignalOffer();
4379 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
4380
Seth Hampson2f0d7022018-02-20 11:54:42 -08004381 MediaExpectations media_expectations;
4382 media_expectations.ExpectBidirectionalVideo();
4383 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07004384}
4385
4386// Test that we achieve the expected end-to-end connection time, using a
4387// fake clock and simulated latency on the media and signaling paths.
4388// We use a TURN<->TURN connection because this is usually the quickest to
4389// set up initially, especially when we're confident the connection will work
4390// and can start sending media before we get a STUN response.
4391//
4392// With various optimizations enabled, here are the network delays we expect to
4393// be on the critical path:
4394// 1. 2 signaling trips: Signaling offer and offerer's TURN candidate, then
4395// signaling answer (with DTLS fingerprint).
4396// 2. 9 media hops: Rest of the DTLS handshake. 3 hops in each direction when
4397// using TURN<->TURN pair, and DTLS exchange is 4 packets,
4398// the first of which should have arrived before the answer.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004399TEST_P(PeerConnectionIntegrationTest, EndToEndConnectionTimeWithTurnTurnPair) {
deadbeef1dcb1642017-03-29 21:08:16 -07004400 rtc::ScopedFakeClock fake_clock;
4401 // Some things use a time of "0" as a special value, so we need to start out
4402 // the fake clock at a nonzero time.
4403 // TODO(deadbeef): Fix this.
Sebastian Jansson5f83cf02018-05-08 14:52:22 +02004404 fake_clock.AdvanceTime(webrtc::TimeDelta::seconds(1));
deadbeef1dcb1642017-03-29 21:08:16 -07004405
4406 static constexpr int media_hop_delay_ms = 50;
4407 static constexpr int signaling_trip_delay_ms = 500;
4408 // For explanation of these values, see comment above.
4409 static constexpr int required_media_hops = 9;
4410 static constexpr int required_signaling_trips = 2;
4411 // For internal delays (such as posting an event asychronously).
4412 static constexpr int allowed_internal_delay_ms = 20;
4413 static constexpr int total_connection_time_ms =
4414 media_hop_delay_ms * required_media_hops +
4415 signaling_trip_delay_ms * required_signaling_trips +
4416 allowed_internal_delay_ms;
4417
4418 static const rtc::SocketAddress turn_server_1_internal_address{"88.88.88.0",
4419 3478};
4420 static const rtc::SocketAddress turn_server_1_external_address{"88.88.88.1",
4421 0};
4422 static const rtc::SocketAddress turn_server_2_internal_address{"99.99.99.0",
4423 3478};
4424 static const rtc::SocketAddress turn_server_2_external_address{"99.99.99.1",
4425 0};
Seth Hampsonaed71642018-06-11 07:41:32 -07004426 cricket::TestTurnServer* turn_server_1 = CreateTurnServer(
4427 turn_server_1_internal_address, turn_server_1_external_address);
Jonas Orelandbdcee282017-10-10 14:01:40 +02004428
Seth Hampsonaed71642018-06-11 07:41:32 -07004429 cricket::TestTurnServer* turn_server_2 = CreateTurnServer(
4430 turn_server_2_internal_address, turn_server_2_external_address);
deadbeef1dcb1642017-03-29 21:08:16 -07004431 // Bypass permission check on received packets so media can be sent before
4432 // the candidate is signaled.
Seth Hampsonaed71642018-06-11 07:41:32 -07004433 network_thread()->Invoke<void>(RTC_FROM_HERE, [turn_server_1] {
4434 turn_server_1->set_enable_permission_checks(false);
4435 });
4436 network_thread()->Invoke<void>(RTC_FROM_HERE, [turn_server_2] {
4437 turn_server_2->set_enable_permission_checks(false);
4438 });
deadbeef1dcb1642017-03-29 21:08:16 -07004439
4440 PeerConnectionInterface::RTCConfiguration client_1_config;
4441 webrtc::PeerConnectionInterface::IceServer ice_server_1;
4442 ice_server_1.urls.push_back("turn:88.88.88.0:3478");
4443 ice_server_1.username = "test";
4444 ice_server_1.password = "test";
4445 client_1_config.servers.push_back(ice_server_1);
4446 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
4447 client_1_config.presume_writable_when_fully_relayed = true;
4448
4449 PeerConnectionInterface::RTCConfiguration client_2_config;
4450 webrtc::PeerConnectionInterface::IceServer ice_server_2;
4451 ice_server_2.urls.push_back("turn:99.99.99.0:3478");
4452 ice_server_2.username = "test";
4453 ice_server_2.password = "test";
4454 client_2_config.servers.push_back(ice_server_2);
4455 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
4456 client_2_config.presume_writable_when_fully_relayed = true;
4457
4458 ASSERT_TRUE(
4459 CreatePeerConnectionWrappersWithConfig(client_1_config, client_2_config));
4460 // Set up the simulated delays.
4461 SetSignalingDelayMs(signaling_trip_delay_ms);
4462 ConnectFakeSignaling();
4463 virtual_socket_server()->set_delay_mean(media_hop_delay_ms);
4464 virtual_socket_server()->UpdateDelayDistribution();
4465
4466 // Set "offer to receive audio/video" without adding any tracks, so we just
4467 // set up ICE/DTLS with no media.
4468 PeerConnectionInterface::RTCOfferAnswerOptions options;
4469 options.offer_to_receive_audio = 1;
4470 options.offer_to_receive_video = 1;
4471 caller()->SetOfferAnswerOptions(options);
4472 caller()->CreateAndSetAndSignalOffer();
deadbeef71452802017-05-07 17:21:01 -07004473 EXPECT_TRUE_SIMULATED_WAIT(DtlsConnected(), total_connection_time_ms,
4474 fake_clock);
Seth Hampson1d4a76d2018-06-19 14:31:41 -07004475 // Closing the PeerConnections destroys the ports before the ScopedFakeClock.
4476 // If this is not done a DCHECK can be hit in ports.cc, because a large
4477 // negative number is calculated for the rtt due to the global clock changing.
4478 caller()->pc()->Close();
4479 callee()->pc()->Close();
deadbeef1dcb1642017-03-29 21:08:16 -07004480}
4481
Jonas Orelandbdcee282017-10-10 14:01:40 +02004482// Verify that a TurnCustomizer passed in through RTCConfiguration
4483// is actually used by the underlying TURN candidate pair.
4484// Note that turnport_unittest.cc contains more detailed, lower-level tests.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004485TEST_P(PeerConnectionIntegrationTest, TurnCustomizerUsedForTurnConnections) {
Jonas Orelandbdcee282017-10-10 14:01:40 +02004486 static const rtc::SocketAddress turn_server_1_internal_address{"88.88.88.0",
4487 3478};
4488 static const rtc::SocketAddress turn_server_1_external_address{"88.88.88.1",
4489 0};
4490 static const rtc::SocketAddress turn_server_2_internal_address{"99.99.99.0",
4491 3478};
4492 static const rtc::SocketAddress turn_server_2_external_address{"99.99.99.1",
4493 0};
Seth Hampsonaed71642018-06-11 07:41:32 -07004494 CreateTurnServer(turn_server_1_internal_address,
4495 turn_server_1_external_address);
4496 CreateTurnServer(turn_server_2_internal_address,
4497 turn_server_2_external_address);
Jonas Orelandbdcee282017-10-10 14:01:40 +02004498
4499 PeerConnectionInterface::RTCConfiguration client_1_config;
4500 webrtc::PeerConnectionInterface::IceServer ice_server_1;
4501 ice_server_1.urls.push_back("turn:88.88.88.0:3478");
4502 ice_server_1.username = "test";
4503 ice_server_1.password = "test";
4504 client_1_config.servers.push_back(ice_server_1);
4505 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
Seth Hampsonaed71642018-06-11 07:41:32 -07004506 auto* customizer1 = CreateTurnCustomizer();
4507 client_1_config.turn_customizer = customizer1;
Jonas Orelandbdcee282017-10-10 14:01:40 +02004508
4509 PeerConnectionInterface::RTCConfiguration client_2_config;
4510 webrtc::PeerConnectionInterface::IceServer ice_server_2;
4511 ice_server_2.urls.push_back("turn:99.99.99.0:3478");
4512 ice_server_2.username = "test";
4513 ice_server_2.password = "test";
4514 client_2_config.servers.push_back(ice_server_2);
4515 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
Seth Hampsonaed71642018-06-11 07:41:32 -07004516 auto* customizer2 = CreateTurnCustomizer();
4517 client_2_config.turn_customizer = customizer2;
Jonas Orelandbdcee282017-10-10 14:01:40 +02004518
4519 ASSERT_TRUE(
4520 CreatePeerConnectionWrappersWithConfig(client_1_config, client_2_config));
4521 ConnectFakeSignaling();
4522
4523 // Set "offer to receive audio/video" without adding any tracks, so we just
4524 // set up ICE/DTLS with no media.
4525 PeerConnectionInterface::RTCOfferAnswerOptions options;
4526 options.offer_to_receive_audio = 1;
4527 options.offer_to_receive_video = 1;
4528 caller()->SetOfferAnswerOptions(options);
4529 caller()->CreateAndSetAndSignalOffer();
4530 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
4531
Seth Hampsonaed71642018-06-11 07:41:32 -07004532 ExpectTurnCustomizerCountersIncremented(customizer1);
4533 ExpectTurnCustomizerCountersIncremented(customizer2);
Jonas Orelandbdcee282017-10-10 14:01:40 +02004534}
4535
Benjamin Wright2d5f3cb2018-05-22 14:46:06 -07004536// Verifies that you can use TCP instead of UDP to connect to a TURN server and
4537// send media between the caller and the callee.
4538TEST_P(PeerConnectionIntegrationTest, TCPUsedForTurnConnections) {
4539 static const rtc::SocketAddress turn_server_internal_address{"88.88.88.0",
4540 3478};
4541 static const rtc::SocketAddress turn_server_external_address{"88.88.88.1", 0};
4542
4543 // Enable TCP for the fake turn server.
Seth Hampsonaed71642018-06-11 07:41:32 -07004544 CreateTurnServer(turn_server_internal_address, turn_server_external_address,
4545 cricket::PROTO_TCP);
Benjamin Wright2d5f3cb2018-05-22 14:46:06 -07004546
4547 webrtc::PeerConnectionInterface::IceServer ice_server;
4548 ice_server.urls.push_back("turn:88.88.88.0:3478?transport=tcp");
4549 ice_server.username = "test";
4550 ice_server.password = "test";
4551
4552 PeerConnectionInterface::RTCConfiguration client_1_config;
4553 client_1_config.servers.push_back(ice_server);
4554 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
4555
4556 PeerConnectionInterface::RTCConfiguration client_2_config;
4557 client_2_config.servers.push_back(ice_server);
4558 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
4559
4560 ASSERT_TRUE(
4561 CreatePeerConnectionWrappersWithConfig(client_1_config, client_2_config));
4562
4563 // Do normal offer/answer and wait for ICE to complete.
4564 ConnectFakeSignaling();
4565 caller()->AddAudioVideoTracks();
4566 callee()->AddAudioVideoTracks();
4567 caller()->CreateAndSetAndSignalOffer();
4568 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4569 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
4570 callee()->ice_connection_state(), kMaxWaitForFramesMs);
4571
4572 MediaExpectations media_expectations;
4573 media_expectations.ExpectBidirectionalAudioAndVideo();
4574 EXPECT_TRUE(ExpectNewFrames(media_expectations));
4575}
4576
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07004577// Verify that a SSLCertificateVerifier passed in through
4578// PeerConnectionDependencies is actually used by the underlying SSL
4579// implementation to determine whether a certificate presented by the TURN
4580// server is accepted by the client. Note that openssladapter_unittest.cc
4581// contains more detailed, lower-level tests.
4582TEST_P(PeerConnectionIntegrationTest,
4583 SSLCertificateVerifierUsedForTurnConnections) {
4584 static const rtc::SocketAddress turn_server_internal_address{"88.88.88.0",
4585 3478};
4586 static const rtc::SocketAddress turn_server_external_address{"88.88.88.1", 0};
4587
4588 // Enable TCP-TLS for the fake turn server. We need to pass in 88.88.88.0 so
4589 // that host name verification passes on the fake certificate.
Seth Hampsonaed71642018-06-11 07:41:32 -07004590 CreateTurnServer(turn_server_internal_address, turn_server_external_address,
4591 cricket::PROTO_TLS, "88.88.88.0");
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07004592
4593 webrtc::PeerConnectionInterface::IceServer ice_server;
4594 ice_server.urls.push_back("turns:88.88.88.0:3478?transport=tcp");
4595 ice_server.username = "test";
4596 ice_server.password = "test";
4597
4598 PeerConnectionInterface::RTCConfiguration client_1_config;
4599 client_1_config.servers.push_back(ice_server);
4600 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
4601
4602 PeerConnectionInterface::RTCConfiguration client_2_config;
4603 client_2_config.servers.push_back(ice_server);
4604 // Setting the type to kRelay forces the connection to go through a TURN
4605 // server.
4606 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
4607
4608 // Get a copy to the pointer so we can verify calls later.
4609 rtc::TestCertificateVerifier* client_1_cert_verifier =
4610 new rtc::TestCertificateVerifier();
4611 client_1_cert_verifier->verify_certificate_ = true;
4612 rtc::TestCertificateVerifier* client_2_cert_verifier =
4613 new rtc::TestCertificateVerifier();
4614 client_2_cert_verifier->verify_certificate_ = true;
4615
4616 // Create the dependencies with the test certificate verifier.
4617 webrtc::PeerConnectionDependencies client_1_deps(nullptr);
4618 client_1_deps.tls_cert_verifier =
4619 std::unique_ptr<rtc::TestCertificateVerifier>(client_1_cert_verifier);
4620 webrtc::PeerConnectionDependencies client_2_deps(nullptr);
4621 client_2_deps.tls_cert_verifier =
4622 std::unique_ptr<rtc::TestCertificateVerifier>(client_2_cert_verifier);
4623
4624 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndDeps(
4625 client_1_config, std::move(client_1_deps), client_2_config,
4626 std::move(client_2_deps)));
4627 ConnectFakeSignaling();
4628
4629 // Set "offer to receive audio/video" without adding any tracks, so we just
4630 // set up ICE/DTLS with no media.
4631 PeerConnectionInterface::RTCOfferAnswerOptions options;
4632 options.offer_to_receive_audio = 1;
4633 options.offer_to_receive_video = 1;
4634 caller()->SetOfferAnswerOptions(options);
4635 caller()->CreateAndSetAndSignalOffer();
4636 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
4637
4638 EXPECT_GT(client_1_cert_verifier->call_count_, 0u);
4639 EXPECT_GT(client_2_cert_verifier->call_count_, 0u);
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07004640}
4641
4642TEST_P(PeerConnectionIntegrationTest,
4643 SSLCertificateVerifierFailureUsedForTurnConnectionsFailsConnection) {
4644 static const rtc::SocketAddress turn_server_internal_address{"88.88.88.0",
4645 3478};
4646 static const rtc::SocketAddress turn_server_external_address{"88.88.88.1", 0};
4647
4648 // Enable TCP-TLS for the fake turn server. We need to pass in 88.88.88.0 so
4649 // that host name verification passes on the fake certificate.
Seth Hampsonaed71642018-06-11 07:41:32 -07004650 CreateTurnServer(turn_server_internal_address, turn_server_external_address,
4651 cricket::PROTO_TLS, "88.88.88.0");
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07004652
4653 webrtc::PeerConnectionInterface::IceServer ice_server;
4654 ice_server.urls.push_back("turns:88.88.88.0:3478?transport=tcp");
4655 ice_server.username = "test";
4656 ice_server.password = "test";
4657
4658 PeerConnectionInterface::RTCConfiguration client_1_config;
4659 client_1_config.servers.push_back(ice_server);
4660 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
4661
4662 PeerConnectionInterface::RTCConfiguration client_2_config;
4663 client_2_config.servers.push_back(ice_server);
4664 // Setting the type to kRelay forces the connection to go through a TURN
4665 // server.
4666 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
4667
4668 // Get a copy to the pointer so we can verify calls later.
4669 rtc::TestCertificateVerifier* client_1_cert_verifier =
4670 new rtc::TestCertificateVerifier();
4671 client_1_cert_verifier->verify_certificate_ = false;
4672 rtc::TestCertificateVerifier* client_2_cert_verifier =
4673 new rtc::TestCertificateVerifier();
4674 client_2_cert_verifier->verify_certificate_ = false;
4675
4676 // Create the dependencies with the test certificate verifier.
4677 webrtc::PeerConnectionDependencies client_1_deps(nullptr);
4678 client_1_deps.tls_cert_verifier =
4679 std::unique_ptr<rtc::TestCertificateVerifier>(client_1_cert_verifier);
4680 webrtc::PeerConnectionDependencies client_2_deps(nullptr);
4681 client_2_deps.tls_cert_verifier =
4682 std::unique_ptr<rtc::TestCertificateVerifier>(client_2_cert_verifier);
4683
4684 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndDeps(
4685 client_1_config, std::move(client_1_deps), client_2_config,
4686 std::move(client_2_deps)));
4687 ConnectFakeSignaling();
4688
4689 // Set "offer to receive audio/video" without adding any tracks, so we just
4690 // set up ICE/DTLS with no media.
4691 PeerConnectionInterface::RTCOfferAnswerOptions options;
4692 options.offer_to_receive_audio = 1;
4693 options.offer_to_receive_video = 1;
4694 caller()->SetOfferAnswerOptions(options);
4695 caller()->CreateAndSetAndSignalOffer();
4696 bool wait_res = true;
4697 // TODO(bugs.webrtc.org/9219): When IceConnectionState is implemented
4698 // properly, should be able to just wait for a state of "failed" instead of
4699 // waiting a fixed 10 seconds.
4700 WAIT_(DtlsConnected(), kDefaultTimeout, wait_res);
4701 ASSERT_FALSE(wait_res);
4702
4703 EXPECT_GT(client_1_cert_verifier->call_count_, 0u);
4704 EXPECT_GT(client_2_cert_verifier->call_count_, 0u);
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07004705}
4706
deadbeefc964d0b2017-04-03 10:03:35 -07004707// Test that audio and video flow end-to-end when codec names don't use the
4708// expected casing, given that they're supposed to be case insensitive. To test
4709// this, all but one codec is removed from each media description, and its
4710// casing is changed.
4711//
4712// In the past, this has regressed and caused crashes/black video, due to the
4713// fact that code at some layers was doing case-insensitive comparisons and
4714// code at other layers was not.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004715TEST_P(PeerConnectionIntegrationTest, CodecNamesAreCaseInsensitive) {
deadbeefc964d0b2017-04-03 10:03:35 -07004716 ASSERT_TRUE(CreatePeerConnectionWrappers());
4717 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08004718 caller()->AddAudioVideoTracks();
4719 callee()->AddAudioVideoTracks();
deadbeefc964d0b2017-04-03 10:03:35 -07004720
4721 // Remove all but one audio/video codec (opus and VP8), and change the
4722 // casing of the caller's generated offer.
4723 caller()->SetGeneratedSdpMunger([](cricket::SessionDescription* description) {
4724 cricket::AudioContentDescription* audio =
4725 GetFirstAudioContentDescription(description);
4726 ASSERT_NE(nullptr, audio);
4727 auto audio_codecs = audio->codecs();
4728 audio_codecs.erase(std::remove_if(audio_codecs.begin(), audio_codecs.end(),
4729 [](const cricket::AudioCodec& codec) {
4730 return codec.name != "opus";
4731 }),
4732 audio_codecs.end());
4733 ASSERT_EQ(1u, audio_codecs.size());
4734 audio_codecs[0].name = "OpUs";
4735 audio->set_codecs(audio_codecs);
4736
4737 cricket::VideoContentDescription* video =
4738 GetFirstVideoContentDescription(description);
4739 ASSERT_NE(nullptr, video);
4740 auto video_codecs = video->codecs();
4741 video_codecs.erase(std::remove_if(video_codecs.begin(), video_codecs.end(),
4742 [](const cricket::VideoCodec& codec) {
4743 return codec.name != "VP8";
4744 }),
4745 video_codecs.end());
4746 ASSERT_EQ(1u, video_codecs.size());
4747 video_codecs[0].name = "vP8";
4748 video->set_codecs(video_codecs);
4749 });
4750
4751 caller()->CreateAndSetAndSignalOffer();
4752 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4753
4754 // Verify frames are still received end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004755 MediaExpectations media_expectations;
4756 media_expectations.ExpectBidirectionalAudioAndVideo();
4757 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeefc964d0b2017-04-03 10:03:35 -07004758}
4759
Jonas Oreland49ac5952018-09-26 16:04:32 +02004760TEST_P(PeerConnectionIntegrationTest, GetSourcesAudio) {
hbos8d609f62017-04-10 07:39:05 -07004761 ASSERT_TRUE(CreatePeerConnectionWrappers());
4762 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08004763 caller()->AddAudioTrack();
hbos8d609f62017-04-10 07:39:05 -07004764 caller()->CreateAndSetAndSignalOffer();
4765 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
deadbeefd8ad7882017-04-18 16:01:17 -07004766 // Wait for one audio frame to be received by the callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004767 MediaExpectations media_expectations;
4768 media_expectations.CalleeExpectsSomeAudio(1);
4769 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Jonas Oreland49ac5952018-09-26 16:04:32 +02004770 ASSERT_EQ(callee()->pc()->GetReceivers().size(), 1u);
hbos8d609f62017-04-10 07:39:05 -07004771 auto receiver = callee()->pc()->GetReceivers()[0];
4772 ASSERT_EQ(receiver->media_type(), cricket::MEDIA_TYPE_AUDIO);
Jonas Oreland49ac5952018-09-26 16:04:32 +02004773 auto sources = receiver->GetSources();
hbos8d609f62017-04-10 07:39:05 -07004774 ASSERT_GT(receiver->GetParameters().encodings.size(), 0u);
4775 EXPECT_EQ(receiver->GetParameters().encodings[0].ssrc,
Jonas Oreland49ac5952018-09-26 16:04:32 +02004776 sources[0].source_id());
4777 EXPECT_EQ(webrtc::RtpSourceType::SSRC, sources[0].source_type());
4778}
4779
4780TEST_P(PeerConnectionIntegrationTest, GetSourcesVideo) {
4781 ASSERT_TRUE(CreatePeerConnectionWrappers());
4782 ConnectFakeSignaling();
4783 caller()->AddVideoTrack();
4784 caller()->CreateAndSetAndSignalOffer();
4785 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4786 // Wait for one video frame to be received by the callee.
4787 MediaExpectations media_expectations;
4788 media_expectations.CalleeExpectsSomeVideo(1);
4789 ASSERT_TRUE(ExpectNewFrames(media_expectations));
4790 ASSERT_EQ(callee()->pc()->GetReceivers().size(), 1u);
4791 auto receiver = callee()->pc()->GetReceivers()[0];
4792 ASSERT_EQ(receiver->media_type(), cricket::MEDIA_TYPE_VIDEO);
4793 auto sources = receiver->GetSources();
4794 ASSERT_GT(receiver->GetParameters().encodings.size(), 0u);
4795 EXPECT_EQ(receiver->GetParameters().encodings[0].ssrc,
4796 sources[0].source_id());
4797 EXPECT_EQ(webrtc::RtpSourceType::SSRC, sources[0].source_type());
hbos8d609f62017-04-10 07:39:05 -07004798}
4799
deadbeef2f425aa2017-04-14 10:41:32 -07004800// Test that if a track is removed and added again with a different stream ID,
4801// the new stream ID is successfully communicated in SDP and media continues to
4802// flow end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004803// TODO(webrtc.bugs.org/8734): This test does not work for Unified Plan because
4804// it will not reuse a transceiver that has already been sending. After creating
4805// a new transceiver it tries to create an offer with two senders of the same
4806// track ids and it fails.
4807TEST_F(PeerConnectionIntegrationTestPlanB, RemoveAndAddTrackWithNewStreamId) {
deadbeef2f425aa2017-04-14 10:41:32 -07004808 ASSERT_TRUE(CreatePeerConnectionWrappers());
4809 ConnectFakeSignaling();
4810
deadbeef2f425aa2017-04-14 10:41:32 -07004811 // Add track using stream 1, do offer/answer.
4812 rtc::scoped_refptr<webrtc::AudioTrackInterface> track =
4813 caller()->CreateLocalAudioTrack();
4814 rtc::scoped_refptr<webrtc::RtpSenderInterface> sender =
Steve Antond78323f2018-07-11 11:13:44 -07004815 caller()->AddTrack(track, {"stream_1"});
deadbeef2f425aa2017-04-14 10:41:32 -07004816 caller()->CreateAndSetAndSignalOffer();
4817 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08004818 {
4819 MediaExpectations media_expectations;
4820 media_expectations.CalleeExpectsSomeAudio(1);
4821 ASSERT_TRUE(ExpectNewFrames(media_expectations));
4822 }
deadbeef2f425aa2017-04-14 10:41:32 -07004823 // Remove the sender, and create a new one with the new stream.
4824 caller()->pc()->RemoveTrack(sender);
Steve Antond78323f2018-07-11 11:13:44 -07004825 sender = caller()->AddTrack(track, {"stream_2"});
deadbeef2f425aa2017-04-14 10:41:32 -07004826 caller()->CreateAndSetAndSignalOffer();
4827 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4828 // Wait for additional audio frames to be received by the callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004829 {
4830 MediaExpectations media_expectations;
4831 media_expectations.CalleeExpectsSomeAudio();
4832 ASSERT_TRUE(ExpectNewFrames(media_expectations));
4833 }
deadbeef2f425aa2017-04-14 10:41:32 -07004834}
4835
Seth Hampson2f0d7022018-02-20 11:54:42 -08004836TEST_P(PeerConnectionIntegrationTest, RtcEventLogOutputWriteCalled) {
Elad Alon99c3fe52017-10-13 16:29:40 +02004837 ASSERT_TRUE(CreatePeerConnectionWrappers());
4838 ConnectFakeSignaling();
4839
Karl Wiberg918f50c2018-07-05 11:40:33 +02004840 auto output = absl::make_unique<testing::NiceMock<MockRtcEventLogOutput>>();
Elad Alon99c3fe52017-10-13 16:29:40 +02004841 ON_CALL(*output, IsActive()).WillByDefault(testing::Return(true));
4842 ON_CALL(*output, Write(::testing::_)).WillByDefault(testing::Return(true));
4843 EXPECT_CALL(*output, Write(::testing::_)).Times(::testing::AtLeast(1));
Bjorn Tereliusde939432017-11-20 17:38:14 +01004844 EXPECT_TRUE(caller()->pc()->StartRtcEventLog(
4845 std::move(output), webrtc::RtcEventLog::kImmediateOutput));
Elad Alon99c3fe52017-10-13 16:29:40 +02004846
Steve Anton15324772018-01-16 10:26:49 -08004847 caller()->AddAudioVideoTracks();
Elad Alon99c3fe52017-10-13 16:29:40 +02004848 caller()->CreateAndSetAndSignalOffer();
4849 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4850}
4851
Steve Antonede9ca52017-10-16 13:04:27 -07004852// Test that if candidates are only signaled by applying full session
4853// descriptions (instead of using AddIceCandidate), the peers can connect to
4854// each other and exchange media.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004855TEST_P(PeerConnectionIntegrationTest, MediaFlowsWhenCandidatesSetOnlyInSdp) {
Steve Antonede9ca52017-10-16 13:04:27 -07004856 ASSERT_TRUE(CreatePeerConnectionWrappers());
4857 // Each side will signal the session descriptions but not candidates.
4858 ConnectFakeSignalingForSdpOnly();
4859
4860 // Add audio video track and exchange the initial offer/answer with media
4861 // information only. This will start ICE gathering on each side.
Steve Anton15324772018-01-16 10:26:49 -08004862 caller()->AddAudioVideoTracks();
4863 callee()->AddAudioVideoTracks();
Steve Antonede9ca52017-10-16 13:04:27 -07004864 caller()->CreateAndSetAndSignalOffer();
4865
4866 // Wait for all candidates to be gathered on both the caller and callee.
4867 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceGatheringComplete,
4868 caller()->ice_gathering_state(), kDefaultTimeout);
4869 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceGatheringComplete,
4870 callee()->ice_gathering_state(), kDefaultTimeout);
4871
4872 // The candidates will now be included in the session description, so
4873 // signaling them will start the ICE connection.
4874 caller()->CreateAndSetAndSignalOffer();
4875 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4876
4877 // Ensure that media flows in both directions.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004878 MediaExpectations media_expectations;
4879 media_expectations.ExpectBidirectionalAudioAndVideo();
4880 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Antonede9ca52017-10-16 13:04:27 -07004881}
4882
henrika5f6bf242017-11-01 11:06:56 +01004883// Test that SetAudioPlayout can be used to disable audio playout from the
4884// start, then later enable it. This may be useful, for example, if the caller
4885// needs to play a local ringtone until some event occurs, after which it
4886// switches to playing the received audio.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004887TEST_P(PeerConnectionIntegrationTest, DisableAndEnableAudioPlayout) {
henrika5f6bf242017-11-01 11:06:56 +01004888 ASSERT_TRUE(CreatePeerConnectionWrappers());
4889 ConnectFakeSignaling();
4890
4891 // Set up audio-only call where audio playout is disabled on caller's side.
4892 caller()->pc()->SetAudioPlayout(false);
Steve Anton15324772018-01-16 10:26:49 -08004893 caller()->AddAudioTrack();
4894 callee()->AddAudioTrack();
henrika5f6bf242017-11-01 11:06:56 +01004895 caller()->CreateAndSetAndSignalOffer();
4896 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4897
4898 // Pump messages for a second.
4899 WAIT(false, 1000);
4900 // Since audio playout is disabled, the caller shouldn't have received
4901 // anything (at the playout level, at least).
4902 EXPECT_EQ(0, caller()->audio_frames_received());
4903 // As a sanity check, make sure the callee (for which playout isn't disabled)
4904 // did still see frames on its audio level.
4905 ASSERT_GT(callee()->audio_frames_received(), 0);
4906
4907 // Enable playout again, and ensure audio starts flowing.
4908 caller()->pc()->SetAudioPlayout(true);
Seth Hampson2f0d7022018-02-20 11:54:42 -08004909 MediaExpectations media_expectations;
4910 media_expectations.ExpectBidirectionalAudio();
4911 ASSERT_TRUE(ExpectNewFrames(media_expectations));
henrika5f6bf242017-11-01 11:06:56 +01004912}
4913
4914double GetAudioEnergyStat(PeerConnectionWrapper* pc) {
4915 auto report = pc->NewGetStats();
4916 auto track_stats_list =
4917 report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
4918 const webrtc::RTCMediaStreamTrackStats* remote_track_stats = nullptr;
4919 for (const auto* track_stats : track_stats_list) {
4920 if (track_stats->remote_source.is_defined() &&
4921 *track_stats->remote_source) {
4922 remote_track_stats = track_stats;
4923 break;
4924 }
4925 }
4926
4927 if (!remote_track_stats->total_audio_energy.is_defined()) {
4928 return 0.0;
4929 }
4930 return *remote_track_stats->total_audio_energy;
4931}
4932
4933// Test that if audio playout is disabled via the SetAudioPlayout() method, then
4934// incoming audio is still processed and statistics are generated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004935TEST_P(PeerConnectionIntegrationTest,
henrika5f6bf242017-11-01 11:06:56 +01004936 DisableAudioPlayoutStillGeneratesAudioStats) {
4937 ASSERT_TRUE(CreatePeerConnectionWrappers());
4938 ConnectFakeSignaling();
4939
4940 // Set up audio-only call where playout is disabled but audio-processing is
4941 // still active.
Steve Anton15324772018-01-16 10:26:49 -08004942 caller()->AddAudioTrack();
4943 callee()->AddAudioTrack();
henrika5f6bf242017-11-01 11:06:56 +01004944 caller()->pc()->SetAudioPlayout(false);
4945
4946 caller()->CreateAndSetAndSignalOffer();
4947 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4948
4949 // Wait for the callee to receive audio stats.
4950 EXPECT_TRUE_WAIT(GetAudioEnergyStat(caller()) > 0, kMaxWaitForFramesMs);
4951}
4952
henrika4f167df2017-11-01 14:45:55 +01004953// Test that SetAudioRecording can be used to disable audio recording from the
4954// start, then later enable it. This may be useful, for example, if the caller
4955// wants to ensure that no audio resources are active before a certain state
4956// is reached.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004957TEST_P(PeerConnectionIntegrationTest, DisableAndEnableAudioRecording) {
henrika4f167df2017-11-01 14:45:55 +01004958 ASSERT_TRUE(CreatePeerConnectionWrappers());
4959 ConnectFakeSignaling();
4960
4961 // Set up audio-only call where audio recording is disabled on caller's side.
4962 caller()->pc()->SetAudioRecording(false);
Steve Anton15324772018-01-16 10:26:49 -08004963 caller()->AddAudioTrack();
4964 callee()->AddAudioTrack();
henrika4f167df2017-11-01 14:45:55 +01004965 caller()->CreateAndSetAndSignalOffer();
4966 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4967
4968 // Pump messages for a second.
4969 WAIT(false, 1000);
4970 // Since caller has disabled audio recording, the callee shouldn't have
4971 // received anything.
4972 EXPECT_EQ(0, callee()->audio_frames_received());
4973 // As a sanity check, make sure the caller did still see frames on its
4974 // audio level since audio recording is enabled on the calle side.
4975 ASSERT_GT(caller()->audio_frames_received(), 0);
4976
4977 // Enable audio recording again, and ensure audio starts flowing.
4978 caller()->pc()->SetAudioRecording(true);
Seth Hampson2f0d7022018-02-20 11:54:42 -08004979 MediaExpectations media_expectations;
4980 media_expectations.ExpectBidirectionalAudio();
4981 ASSERT_TRUE(ExpectNewFrames(media_expectations));
henrika4f167df2017-11-01 14:45:55 +01004982}
4983
Taylor Brandstetter389a97c2018-01-03 16:26:06 -08004984// Test that after closing PeerConnections, they stop sending any packets (ICE,
4985// DTLS, RTP...).
Seth Hampson2f0d7022018-02-20 11:54:42 -08004986TEST_P(PeerConnectionIntegrationTest, ClosingConnectionStopsPacketFlow) {
Taylor Brandstetter389a97c2018-01-03 16:26:06 -08004987 // Set up audio/video/data, wait for some frames to be received.
4988 ASSERT_TRUE(CreatePeerConnectionWrappers());
4989 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08004990 caller()->AddAudioVideoTracks();
Taylor Brandstetter389a97c2018-01-03 16:26:06 -08004991#ifdef HAVE_SCTP
4992 caller()->CreateDataChannel();
4993#endif
4994 caller()->CreateAndSetAndSignalOffer();
4995 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08004996 MediaExpectations media_expectations;
4997 media_expectations.CalleeExpectsSomeAudioAndVideo();
4998 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Taylor Brandstetter389a97c2018-01-03 16:26:06 -08004999 // Close PeerConnections.
5000 caller()->pc()->Close();
5001 callee()->pc()->Close();
5002 // Pump messages for a second, and ensure no new packets end up sent.
5003 uint32_t sent_packets_a = virtual_socket_server()->sent_packets();
5004 WAIT(false, 1000);
5005 uint32_t sent_packets_b = virtual_socket_server()->sent_packets();
5006 EXPECT_EQ(sent_packets_a, sent_packets_b);
5007}
5008
Steve Anton7eca0932018-03-30 15:18:41 -07005009// Test that transport stats are generated by the RTCStatsCollector for a
5010// connection that only involves data channels. This is a regression test for
5011// crbug.com/826972.
5012#ifdef HAVE_SCTP
5013TEST_P(PeerConnectionIntegrationTest,
5014 TransportStatsReportedForDataChannelOnlyConnection) {
5015 ASSERT_TRUE(CreatePeerConnectionWrappers());
5016 ConnectFakeSignaling();
5017 caller()->CreateDataChannel();
5018
5019 caller()->CreateAndSetAndSignalOffer();
5020 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5021 ASSERT_TRUE_WAIT(callee()->data_channel(), kDefaultTimeout);
5022
5023 auto caller_report = caller()->NewGetStats();
5024 EXPECT_EQ(1u, caller_report->GetStatsOfType<RTCTransportStats>().size());
5025 auto callee_report = callee()->NewGetStats();
5026 EXPECT_EQ(1u, callee_report->GetStatsOfType<RTCTransportStats>().size());
5027}
5028#endif // HAVE_SCTP
5029
Qingsi Wang7685e862018-06-11 20:15:46 -07005030TEST_P(PeerConnectionIntegrationTest,
5031 IceEventsGeneratedAndLoggedInRtcEventLog) {
5032 ASSERT_TRUE(CreatePeerConnectionWrappersWithFakeRtcEventLog());
5033 ConnectFakeSignaling();
5034 PeerConnectionInterface::RTCOfferAnswerOptions options;
5035 options.offer_to_receive_audio = 1;
5036 caller()->SetOfferAnswerOptions(options);
5037 caller()->CreateAndSetAndSignalOffer();
5038 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
5039 ASSERT_NE(nullptr, caller()->event_log_factory());
5040 ASSERT_NE(nullptr, callee()->event_log_factory());
5041 webrtc::FakeRtcEventLog* caller_event_log =
5042 static_cast<webrtc::FakeRtcEventLog*>(
5043 caller()->event_log_factory()->last_log_created());
5044 webrtc::FakeRtcEventLog* callee_event_log =
5045 static_cast<webrtc::FakeRtcEventLog*>(
5046 callee()->event_log_factory()->last_log_created());
5047 ASSERT_NE(nullptr, caller_event_log);
5048 ASSERT_NE(nullptr, callee_event_log);
5049 int caller_ice_config_count = caller_event_log->GetEventCount(
5050 webrtc::RtcEvent::Type::IceCandidatePairConfig);
5051 int caller_ice_event_count = caller_event_log->GetEventCount(
5052 webrtc::RtcEvent::Type::IceCandidatePairEvent);
5053 int callee_ice_config_count = callee_event_log->GetEventCount(
5054 webrtc::RtcEvent::Type::IceCandidatePairConfig);
5055 int callee_ice_event_count = callee_event_log->GetEventCount(
5056 webrtc::RtcEvent::Type::IceCandidatePairEvent);
5057 EXPECT_LT(0, caller_ice_config_count);
5058 EXPECT_LT(0, caller_ice_event_count);
5059 EXPECT_LT(0, callee_ice_config_count);
5060 EXPECT_LT(0, callee_ice_event_count);
5061}
5062
Mirko Bonadeic84f6612019-01-31 12:20:57 +01005063INSTANTIATE_TEST_SUITE_P(PeerConnectionIntegrationTest,
5064 PeerConnectionIntegrationTest,
5065 Values(SdpSemantics::kPlanB,
5066 SdpSemantics::kUnifiedPlan));
Steve Antond3679212018-01-17 17:41:02 -08005067
Steve Anton74255ff2018-01-24 18:32:57 -08005068// Tests that verify interoperability between Plan B and Unified Plan
5069// PeerConnections.
5070class PeerConnectionIntegrationInteropTest
Seth Hampson2f0d7022018-02-20 11:54:42 -08005071 : public PeerConnectionIntegrationBaseTest,
Steve Anton74255ff2018-01-24 18:32:57 -08005072 public ::testing::WithParamInterface<
5073 std::tuple<SdpSemantics, SdpSemantics>> {
5074 protected:
Seth Hampson2f0d7022018-02-20 11:54:42 -08005075 // Setting the SdpSemantics for the base test to kDefault does not matter
5076 // because we specify not to use the test semantics when creating
5077 // PeerConnectionWrappers.
Steve Anton74255ff2018-01-24 18:32:57 -08005078 PeerConnectionIntegrationInteropTest()
Steve Anton3acffc32018-04-12 17:21:03 -07005079 : PeerConnectionIntegrationBaseTest(SdpSemantics::kPlanB),
Seth Hampson2f0d7022018-02-20 11:54:42 -08005080 caller_semantics_(std::get<0>(GetParam())),
Steve Anton74255ff2018-01-24 18:32:57 -08005081 callee_semantics_(std::get<1>(GetParam())) {}
5082
5083 bool CreatePeerConnectionWrappersWithSemantics() {
Steve Anton3acffc32018-04-12 17:21:03 -07005084 return CreatePeerConnectionWrappersWithSdpSemantics(caller_semantics_,
5085 callee_semantics_);
Steve Anton74255ff2018-01-24 18:32:57 -08005086 }
5087
5088 const SdpSemantics caller_semantics_;
5089 const SdpSemantics callee_semantics_;
5090};
5091
5092TEST_P(PeerConnectionIntegrationInteropTest, NoMediaLocalToNoMediaRemote) {
5093 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
5094 ConnectFakeSignaling();
5095
5096 caller()->CreateAndSetAndSignalOffer();
5097 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5098}
5099
5100TEST_P(PeerConnectionIntegrationInteropTest, OneAudioLocalToNoMediaRemote) {
5101 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
5102 ConnectFakeSignaling();
5103 auto audio_sender = caller()->AddAudioTrack();
5104
5105 caller()->CreateAndSetAndSignalOffer();
5106 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5107
5108 // Verify that one audio receiver has been created on the remote and that it
5109 // has the same track ID as the sending track.
5110 auto receivers = callee()->pc()->GetReceivers();
5111 ASSERT_EQ(1u, receivers.size());
5112 EXPECT_EQ(cricket::MEDIA_TYPE_AUDIO, receivers[0]->media_type());
5113 EXPECT_EQ(receivers[0]->track()->id(), audio_sender->track()->id());
5114
Seth Hampson2f0d7022018-02-20 11:54:42 -08005115 MediaExpectations media_expectations;
5116 media_expectations.CalleeExpectsSomeAudio();
5117 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08005118}
5119
5120TEST_P(PeerConnectionIntegrationInteropTest, OneAudioOneVideoToNoMediaRemote) {
5121 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
5122 ConnectFakeSignaling();
5123 auto video_sender = caller()->AddVideoTrack();
5124 auto audio_sender = caller()->AddAudioTrack();
5125
5126 caller()->CreateAndSetAndSignalOffer();
5127 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5128
5129 // Verify that one audio and one video receiver have been created on the
5130 // remote and that they have the same track IDs as the sending tracks.
5131 auto audio_receivers =
5132 callee()->GetReceiversOfType(cricket::MEDIA_TYPE_AUDIO);
5133 ASSERT_EQ(1u, audio_receivers.size());
5134 EXPECT_EQ(audio_receivers[0]->track()->id(), audio_sender->track()->id());
5135 auto video_receivers =
5136 callee()->GetReceiversOfType(cricket::MEDIA_TYPE_VIDEO);
5137 ASSERT_EQ(1u, video_receivers.size());
5138 EXPECT_EQ(video_receivers[0]->track()->id(), video_sender->track()->id());
5139
Seth Hampson2f0d7022018-02-20 11:54:42 -08005140 MediaExpectations media_expectations;
5141 media_expectations.CalleeExpectsSomeAudioAndVideo();
5142 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08005143}
5144
5145TEST_P(PeerConnectionIntegrationInteropTest,
5146 OneAudioOneVideoLocalToOneAudioOneVideoRemote) {
5147 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
5148 ConnectFakeSignaling();
5149 caller()->AddAudioVideoTracks();
5150 callee()->AddAudioVideoTracks();
5151
5152 caller()->CreateAndSetAndSignalOffer();
5153 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5154
Seth Hampson2f0d7022018-02-20 11:54:42 -08005155 MediaExpectations media_expectations;
5156 media_expectations.ExpectBidirectionalAudioAndVideo();
5157 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08005158}
5159
5160TEST_P(PeerConnectionIntegrationInteropTest,
5161 ReverseRolesOneAudioLocalToOneVideoRemote) {
5162 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
5163 ConnectFakeSignaling();
5164 caller()->AddAudioTrack();
5165 callee()->AddVideoTrack();
5166
5167 caller()->CreateAndSetAndSignalOffer();
5168 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5169
5170 // Verify that only the audio track has been negotiated.
5171 EXPECT_EQ(0u, caller()->GetReceiversOfType(cricket::MEDIA_TYPE_VIDEO).size());
5172 // Might also check that the callee's NegotiationNeeded flag is set.
5173
5174 // Reverse roles.
5175 callee()->CreateAndSetAndSignalOffer();
5176 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5177
Seth Hampson2f0d7022018-02-20 11:54:42 -08005178 MediaExpectations media_expectations;
5179 media_expectations.CallerExpectsSomeVideo();
5180 media_expectations.CalleeExpectsSomeAudio();
5181 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08005182}
5183
Mirko Bonadeic84f6612019-01-31 12:20:57 +01005184INSTANTIATE_TEST_SUITE_P(
Steve Antonba42e992018-04-09 14:10:01 -07005185 PeerConnectionIntegrationTest,
5186 PeerConnectionIntegrationInteropTest,
5187 Values(std::make_tuple(SdpSemantics::kPlanB, SdpSemantics::kUnifiedPlan),
5188 std::make_tuple(SdpSemantics::kUnifiedPlan, SdpSemantics::kPlanB)));
5189
5190// Test that if the Unified Plan side offers two video tracks then the Plan B
5191// side will only see the first one and ignore the second.
5192TEST_F(PeerConnectionIntegrationTestPlanB, TwoVideoUnifiedPlanToNoMediaPlanB) {
Steve Anton3acffc32018-04-12 17:21:03 -07005193 ASSERT_TRUE(CreatePeerConnectionWrappersWithSdpSemantics(
5194 SdpSemantics::kUnifiedPlan, SdpSemantics::kPlanB));
Steve Anton74255ff2018-01-24 18:32:57 -08005195 ConnectFakeSignaling();
5196 auto first_sender = caller()->AddVideoTrack();
5197 caller()->AddVideoTrack();
5198
5199 caller()->CreateAndSetAndSignalOffer();
5200 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5201
5202 // Verify that there is only one receiver and it corresponds to the first
5203 // added track.
5204 auto receivers = callee()->pc()->GetReceivers();
5205 ASSERT_EQ(1u, receivers.size());
5206 EXPECT_TRUE(receivers[0]->track()->enabled());
5207 EXPECT_EQ(first_sender->track()->id(), receivers[0]->track()->id());
5208
Seth Hampson2f0d7022018-02-20 11:54:42 -08005209 MediaExpectations media_expectations;
5210 media_expectations.CalleeExpectsSomeVideo();
5211 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08005212}
5213
Steve Anton2bed3972019-01-04 17:04:30 -08005214// Test that if the initial offer tagged BUNDLE section is rejected due to its
5215// associated RtpTransceiver being stopped and another transceiver is added,
5216// then renegotiation causes the callee to receive the new video track without
5217// error.
5218// This is a regression test for bugs.webrtc.org/9954
5219TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
5220 ReOfferWithStoppedBundleTaggedTransceiver) {
5221 RTCConfiguration config;
5222 config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
5223 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
5224 ConnectFakeSignaling();
5225 auto audio_transceiver_or_error =
5226 caller()->pc()->AddTransceiver(caller()->CreateLocalAudioTrack());
5227 ASSERT_TRUE(audio_transceiver_or_error.ok());
5228 auto audio_transceiver = audio_transceiver_or_error.MoveValue();
5229
5230 caller()->CreateAndSetAndSignalOffer();
5231 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5232 {
5233 MediaExpectations media_expectations;
5234 media_expectations.CalleeExpectsSomeAudio();
5235 ASSERT_TRUE(ExpectNewFrames(media_expectations));
5236 }
5237
5238 audio_transceiver->Stop();
5239 caller()->pc()->AddTransceiver(caller()->CreateLocalVideoTrack());
5240
5241 caller()->CreateAndSetAndSignalOffer();
5242 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5243 {
5244 MediaExpectations media_expectations;
5245 media_expectations.CalleeExpectsSomeVideo();
5246 ASSERT_TRUE(ExpectNewFrames(media_expectations));
5247 }
5248}
5249
deadbeef1dcb1642017-03-29 21:08:16 -07005250} // namespace
Mirko Bonadeiab64e8a2018-12-12 12:10:18 +01005251} // namespace webrtc
deadbeef1dcb1642017-03-29 21:08:16 -07005252
5253#endif // if !defined(THREAD_SANITIZER)