blob: 8aa76754bf28310402e1bd53f49912b2d10f35b7 [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).
Mirko Bonadei6a489f22019-04-09 15:11:12 +02001126class 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();
Harald Alvestrandf3736ed2019-04-08 13:09:30 +02003210 ASSERT_TRUE(caller()->data_channel() != nullptr);
Steve Anton15324772018-01-16 10:26:49 -08003211 caller()->AddAudioVideoTracks();
3212 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003213 caller()->CreateAndSetAndSignalOffer();
3214 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3215 // The caller should still have a data channel, but it should be closed, and
3216 // one should ever have been created for the callee.
3217 EXPECT_TRUE(caller()->data_channel() != nullptr);
3218 EXPECT_FALSE(caller()->data_observer()->IsOpen());
3219 EXPECT_EQ(nullptr, callee()->data_channel());
3220}
3221
3222// This test sets up a call between two parties with audio, and video. When
3223// audio and video is setup and flowing, an RTP data channel is negotiated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003224TEST_P(PeerConnectionIntegrationTest, AddRtpDataChannelInSubsequentOffer) {
Niels Möllerf06f9232018-08-07 12:32:18 +02003225 PeerConnectionInterface::RTCConfiguration rtc_config;
3226 rtc_config.enable_rtp_data_channel = true;
3227 rtc_config.enable_dtls_srtp = false;
3228 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(rtc_config, rtc_config));
deadbeef1dcb1642017-03-29 21:08:16 -07003229 ConnectFakeSignaling();
3230 // Do initial offer/answer with audio/video.
Steve Anton15324772018-01-16 10:26:49 -08003231 caller()->AddAudioVideoTracks();
3232 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003233 caller()->CreateAndSetAndSignalOffer();
3234 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3235 // Create data channel and do new offer and answer.
3236 caller()->CreateDataChannel();
3237 caller()->CreateAndSetAndSignalOffer();
3238 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3239 ASSERT_NE(nullptr, caller()->data_channel());
3240 ASSERT_NE(nullptr, callee()->data_channel());
3241 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3242 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3243 // Ensure data can be sent in both directions.
3244 std::string data = "hello world";
3245 SendRtpDataWithRetries(caller()->data_channel(), data, 5);
3246 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3247 kDefaultTimeout);
3248 SendRtpDataWithRetries(callee()->data_channel(), data, 5);
3249 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3250 kDefaultTimeout);
3251}
3252
3253#ifdef HAVE_SCTP
3254
3255// This test sets up a call between two parties with audio, video and an SCTP
3256// data channel.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003257TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithSctpDataChannel) {
deadbeef1dcb1642017-03-29 21:08:16 -07003258 ASSERT_TRUE(CreatePeerConnectionWrappers());
3259 ConnectFakeSignaling();
3260 // Expect that data channel created on caller side will show up for callee as
3261 // well.
3262 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08003263 caller()->AddAudioVideoTracks();
3264 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003265 caller()->CreateAndSetAndSignalOffer();
3266 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3267 // Ensure the existence of the SCTP data channel didn't impede audio/video.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003268 MediaExpectations media_expectations;
3269 media_expectations.ExpectBidirectionalAudioAndVideo();
3270 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003271 // Caller data channel should already exist (it created one). Callee data
3272 // channel may not exist yet, since negotiation happens in-band, not in SDP.
3273 ASSERT_NE(nullptr, caller()->data_channel());
3274 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3275 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3276 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3277
3278 // Ensure data can be sent in both directions.
3279 std::string data = "hello world";
3280 caller()->data_channel()->Send(DataBuffer(data));
3281 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3282 kDefaultTimeout);
3283 callee()->data_channel()->Send(DataBuffer(data));
3284 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3285 kDefaultTimeout);
3286}
3287
3288// Ensure that when the callee closes an SCTP data channel, the closing
3289// procedure results in the data channel being closed for the caller as well.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003290TEST_P(PeerConnectionIntegrationTest, CalleeClosesSctpDataChannel) {
deadbeef1dcb1642017-03-29 21:08:16 -07003291 // Same procedure as above test.
3292 ASSERT_TRUE(CreatePeerConnectionWrappers());
3293 ConnectFakeSignaling();
3294 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08003295 caller()->AddAudioVideoTracks();
3296 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003297 caller()->CreateAndSetAndSignalOffer();
3298 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3299 ASSERT_NE(nullptr, caller()->data_channel());
3300 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3301 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3302 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3303
3304 // Close the data channel on the callee side, and wait for it to reach the
3305 // "closed" state on both sides.
3306 callee()->data_channel()->Close();
3307 EXPECT_TRUE_WAIT(!caller()->data_observer()->IsOpen(), kDefaultTimeout);
3308 EXPECT_TRUE_WAIT(!callee()->data_observer()->IsOpen(), kDefaultTimeout);
3309}
3310
Seth Hampson2f0d7022018-02-20 11:54:42 -08003311TEST_P(PeerConnectionIntegrationTest, SctpDataChannelConfigSentToOtherSide) {
Steve Antonda6c0952017-10-23 11:41:54 -07003312 ASSERT_TRUE(CreatePeerConnectionWrappers());
3313 ConnectFakeSignaling();
3314 webrtc::DataChannelInit init;
3315 init.id = 53;
3316 init.maxRetransmits = 52;
3317 caller()->CreateDataChannel("data-channel", &init);
Steve Anton15324772018-01-16 10:26:49 -08003318 caller()->AddAudioVideoTracks();
3319 callee()->AddAudioVideoTracks();
Steve Antonda6c0952017-10-23 11:41:54 -07003320 caller()->CreateAndSetAndSignalOffer();
3321 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Steve Anton074dece2017-10-24 13:04:12 -07003322 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3323 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
Harald Alvestrand5c4d2ee2019-04-01 12:58:15 +02003324 // Since "negotiated" is false, the "id" parameter should be ignored.
3325 EXPECT_NE(init.id, callee()->data_channel()->id());
Steve Antonda6c0952017-10-23 11:41:54 -07003326 EXPECT_EQ("data-channel", callee()->data_channel()->label());
3327 EXPECT_EQ(init.maxRetransmits, callee()->data_channel()->maxRetransmits());
3328 EXPECT_FALSE(callee()->data_channel()->negotiated());
3329}
3330
deadbeef1dcb1642017-03-29 21:08:16 -07003331// Test usrsctp's ability to process unordered data stream, where data actually
3332// arrives out of order using simulated delays. Previously there have been some
3333// bugs in this area.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003334TEST_P(PeerConnectionIntegrationTest, StressTestUnorderedSctpDataChannel) {
deadbeef1dcb1642017-03-29 21:08:16 -07003335 // Introduce random network delays.
3336 // Otherwise it's not a true "unordered" test.
3337 virtual_socket_server()->set_delay_mean(20);
3338 virtual_socket_server()->set_delay_stddev(5);
3339 virtual_socket_server()->UpdateDelayDistribution();
3340 // Normal procedure, but with unordered data channel config.
3341 ASSERT_TRUE(CreatePeerConnectionWrappers());
3342 ConnectFakeSignaling();
3343 webrtc::DataChannelInit init;
3344 init.ordered = false;
3345 caller()->CreateDataChannel(&init);
3346 caller()->CreateAndSetAndSignalOffer();
3347 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3348 ASSERT_NE(nullptr, caller()->data_channel());
3349 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3350 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3351 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3352
3353 static constexpr int kNumMessages = 100;
3354 // Deliberately chosen to be larger than the MTU so messages get fragmented.
3355 static constexpr size_t kMaxMessageSize = 4096;
3356 // Create and send random messages.
3357 std::vector<std::string> sent_messages;
3358 for (int i = 0; i < kNumMessages; ++i) {
3359 size_t length =
3360 (rand() % kMaxMessageSize) + 1; // NOLINT (rand_r instead of rand)
3361 std::string message;
3362 ASSERT_TRUE(rtc::CreateRandomString(length, &message));
3363 caller()->data_channel()->Send(DataBuffer(message));
3364 callee()->data_channel()->Send(DataBuffer(message));
3365 sent_messages.push_back(message);
3366 }
3367
3368 // Wait for all messages to be received.
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02003369 EXPECT_EQ_WAIT(rtc::checked_cast<size_t>(kNumMessages),
deadbeef1dcb1642017-03-29 21:08:16 -07003370 caller()->data_observer()->received_message_count(),
3371 kDefaultTimeout);
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02003372 EXPECT_EQ_WAIT(rtc::checked_cast<size_t>(kNumMessages),
deadbeef1dcb1642017-03-29 21:08:16 -07003373 callee()->data_observer()->received_message_count(),
3374 kDefaultTimeout);
3375
3376 // Sort and compare to make sure none of the messages were corrupted.
3377 std::vector<std::string> caller_received_messages =
3378 caller()->data_observer()->messages();
3379 std::vector<std::string> callee_received_messages =
3380 callee()->data_observer()->messages();
Steve Anton64b626b2019-01-28 17:25:26 -08003381 absl::c_sort(sent_messages);
3382 absl::c_sort(caller_received_messages);
3383 absl::c_sort(callee_received_messages);
deadbeef1dcb1642017-03-29 21:08:16 -07003384 EXPECT_EQ(sent_messages, caller_received_messages);
3385 EXPECT_EQ(sent_messages, callee_received_messages);
3386}
3387
3388// This test sets up a call between two parties with audio, and video. When
3389// audio and video are setup and flowing, an SCTP data channel is negotiated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003390TEST_P(PeerConnectionIntegrationTest, AddSctpDataChannelInSubsequentOffer) {
deadbeef1dcb1642017-03-29 21:08:16 -07003391 ASSERT_TRUE(CreatePeerConnectionWrappers());
3392 ConnectFakeSignaling();
3393 // Do initial offer/answer with audio/video.
Steve Anton15324772018-01-16 10:26:49 -08003394 caller()->AddAudioVideoTracks();
3395 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003396 caller()->CreateAndSetAndSignalOffer();
3397 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3398 // Create data channel and do new offer and answer.
3399 caller()->CreateDataChannel();
3400 caller()->CreateAndSetAndSignalOffer();
3401 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3402 // Caller data channel should already exist (it created one). Callee data
3403 // channel may not exist yet, since negotiation happens in-band, not in SDP.
3404 ASSERT_NE(nullptr, caller()->data_channel());
3405 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3406 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3407 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3408 // Ensure data can be sent in both directions.
3409 std::string data = "hello world";
3410 caller()->data_channel()->Send(DataBuffer(data));
3411 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3412 kDefaultTimeout);
3413 callee()->data_channel()->Send(DataBuffer(data));
3414 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3415 kDefaultTimeout);
3416}
3417
deadbeef7914b8c2017-04-21 03:23:33 -07003418// Set up a connection initially just using SCTP data channels, later upgrading
3419// to audio/video, ensuring frames are received end-to-end. Effectively the
3420// inverse of the test above.
3421// This was broken in M57; see https://crbug.com/711243
Seth Hampson2f0d7022018-02-20 11:54:42 -08003422TEST_P(PeerConnectionIntegrationTest, SctpDataChannelToAudioVideoUpgrade) {
deadbeef7914b8c2017-04-21 03:23:33 -07003423 ASSERT_TRUE(CreatePeerConnectionWrappers());
3424 ConnectFakeSignaling();
3425 // Do initial offer/answer with just data channel.
3426 caller()->CreateDataChannel();
3427 caller()->CreateAndSetAndSignalOffer();
3428 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3429 // Wait until data can be sent over the data channel.
3430 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3431 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3432 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3433
3434 // Do subsequent offer/answer with two-way audio and video. Audio and video
3435 // should end up bundled on the DTLS/ICE transport already used for data.
Steve Anton15324772018-01-16 10:26:49 -08003436 caller()->AddAudioVideoTracks();
3437 callee()->AddAudioVideoTracks();
deadbeef7914b8c2017-04-21 03:23:33 -07003438 caller()->CreateAndSetAndSignalOffer();
3439 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08003440 MediaExpectations media_expectations;
3441 media_expectations.ExpectBidirectionalAudioAndVideo();
3442 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef7914b8c2017-04-21 03:23:33 -07003443}
3444
deadbeef8b7e9ad2017-05-25 09:38:55 -07003445static void MakeSpecCompliantSctpOffer(cricket::SessionDescription* desc) {
Seth Hampson1859dc02019-04-16 20:55:57 +00003446 cricket::DataContentDescription* dcd_offer =
3447 GetFirstDataContentDescription(desc);
Steve Antonb1c1de12017-12-21 15:14:30 -08003448 ASSERT_TRUE(dcd_offer);
deadbeef8b7e9ad2017-05-25 09:38:55 -07003449 dcd_offer->set_use_sctpmap(false);
3450 dcd_offer->set_protocol("UDP/DTLS/SCTP");
3451}
3452
3453// Test that the data channel works when a spec-compliant SCTP m= section is
3454// offered (using "a=sctp-port" instead of "a=sctpmap", and using
3455// "UDP/DTLS/SCTP" as the protocol).
Seth Hampson2f0d7022018-02-20 11:54:42 -08003456TEST_P(PeerConnectionIntegrationTest,
deadbeef8b7e9ad2017-05-25 09:38:55 -07003457 DataChannelWorksWhenSpecCompliantSctpOfferReceived) {
3458 ASSERT_TRUE(CreatePeerConnectionWrappers());
3459 ConnectFakeSignaling();
3460 caller()->CreateDataChannel();
3461 caller()->SetGeneratedSdpMunger(MakeSpecCompliantSctpOffer);
3462 caller()->CreateAndSetAndSignalOffer();
3463 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3464 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3465 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3466 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3467
3468 // Ensure data can be sent in both directions.
3469 std::string data = "hello world";
3470 caller()->data_channel()->Send(DataBuffer(data));
3471 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3472 kDefaultTimeout);
3473 callee()->data_channel()->Send(DataBuffer(data));
3474 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3475 kDefaultTimeout);
3476}
3477
deadbeef1dcb1642017-03-29 21:08:16 -07003478#endif // HAVE_SCTP
3479
Bjorn Mellema2eb0a72018-11-09 10:13:51 -08003480// This test sets up a call between two parties with a media transport data
3481// channel.
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08003482TEST_P(PeerConnectionIntegrationTest, MediaTransportDataChannelEndToEnd) {
3483 PeerConnectionInterface::RTCConfiguration rtc_config;
Piotr (Peter) Slatalab1ae10b2019-03-01 11:14:05 -08003484 rtc_config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyRequire;
3485 rtc_config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08003486 rtc_config.use_media_transport_for_data_channels = true;
3487 rtc_config.enable_dtls_srtp = false; // SDES is required for media transport.
3488 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
3489 rtc_config, rtc_config, loopback_media_transports()->first_factory(),
3490 loopback_media_transports()->second_factory()));
3491 ConnectFakeSignaling();
3492
3493 // Expect that data channel created on caller side will show up for callee as
3494 // well.
3495 caller()->CreateDataChannel();
3496 caller()->CreateAndSetAndSignalOffer();
3497 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3498
3499 // Ensure that the media transport is ready.
3500 loopback_media_transports()->SetState(webrtc::MediaTransportState::kWritable);
3501 loopback_media_transports()->FlushAsyncInvokes();
3502
3503 // Caller data channel should already exist (it created one). Callee data
3504 // channel may not exist yet, since negotiation happens in-band, not in SDP.
3505 ASSERT_NE(nullptr, caller()->data_channel());
3506 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3507 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3508 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3509
3510 // Ensure data can be sent in both directions.
3511 std::string data = "hello world";
3512 caller()->data_channel()->Send(DataBuffer(data));
3513 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3514 kDefaultTimeout);
3515 callee()->data_channel()->Send(DataBuffer(data));
3516 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3517 kDefaultTimeout);
3518}
3519
3520// Ensure that when the callee closes a media transport data channel, the
3521// closing procedure results in the data channel being closed for the caller
3522// as well.
3523TEST_P(PeerConnectionIntegrationTest, MediaTransportDataChannelCalleeCloses) {
3524 PeerConnectionInterface::RTCConfiguration rtc_config;
3525 rtc_config.use_media_transport_for_data_channels = true;
3526 rtc_config.enable_dtls_srtp = false; // SDES is required for media transport.
3527 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
3528 rtc_config, rtc_config, loopback_media_transports()->first_factory(),
3529 loopback_media_transports()->second_factory()));
3530 ConnectFakeSignaling();
3531
3532 // Create a data channel on the caller and signal it to the callee.
3533 caller()->CreateDataChannel();
3534 caller()->CreateAndSetAndSignalOffer();
3535 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3536
3537 // Ensure that the media transport is ready.
3538 loopback_media_transports()->SetState(webrtc::MediaTransportState::kWritable);
3539 loopback_media_transports()->FlushAsyncInvokes();
3540
3541 // Data channels exist and open on both ends of the connection.
3542 ASSERT_NE(nullptr, caller()->data_channel());
3543 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3544 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3545 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3546
3547 // Close the data channel on the callee side, and wait for it to reach the
3548 // "closed" state on both sides.
3549 callee()->data_channel()->Close();
3550 EXPECT_TRUE_WAIT(!caller()->data_observer()->IsOpen(), kDefaultTimeout);
3551 EXPECT_TRUE_WAIT(!callee()->data_observer()->IsOpen(), kDefaultTimeout);
3552}
3553
3554TEST_P(PeerConnectionIntegrationTest,
3555 MediaTransportDataChannelConfigSentToOtherSide) {
3556 PeerConnectionInterface::RTCConfiguration rtc_config;
3557 rtc_config.use_media_transport_for_data_channels = true;
3558 rtc_config.enable_dtls_srtp = false; // SDES is required for media transport.
3559 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
3560 rtc_config, rtc_config, loopback_media_transports()->first_factory(),
3561 loopback_media_transports()->second_factory()));
3562 ConnectFakeSignaling();
3563
3564 // Create a data channel with a non-default configuration and signal it to the
3565 // callee.
3566 webrtc::DataChannelInit init;
3567 init.id = 53;
3568 init.maxRetransmits = 52;
3569 caller()->CreateDataChannel("data-channel", &init);
3570 caller()->CreateAndSetAndSignalOffer();
3571 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3572
3573 // Ensure that the media transport is ready.
3574 loopback_media_transports()->SetState(webrtc::MediaTransportState::kWritable);
3575 loopback_media_transports()->FlushAsyncInvokes();
3576
3577 // Ensure that the data channel exists on the callee with the correct
3578 // configuration.
3579 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3580 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
Harald Alvestrand5c4d2ee2019-04-01 12:58:15 +02003581 // Since "negotiate" is false, the "id" parameter is ignored.
3582 EXPECT_NE(init.id, callee()->data_channel()->id());
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08003583 EXPECT_EQ("data-channel", callee()->data_channel()->label());
3584 EXPECT_EQ(init.maxRetransmits, callee()->data_channel()->maxRetransmits());
3585 EXPECT_FALSE(callee()->data_channel()->negotiated());
3586}
3587
Piotr (Peter) Slatalab1ae10b2019-03-01 11:14:05 -08003588TEST_P(PeerConnectionIntegrationTest, MediaTransportOfferUpgrade) {
3589 PeerConnectionInterface::RTCConfiguration rtc_config;
3590 rtc_config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyRequire;
3591 rtc_config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
3592 rtc_config.use_media_transport = true;
3593 rtc_config.enable_dtls_srtp = false; // SDES is required for media transport.
3594 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
3595 rtc_config, rtc_config, loopback_media_transports()->first_factory(),
3596 loopback_media_transports()->second_factory()));
3597 ConnectFakeSignaling();
3598
3599 // Do initial offer/answer with just a video track.
3600 caller()->AddVideoTrack();
3601 callee()->AddVideoTrack();
3602 caller()->CreateAndSetAndSignalOffer();
3603 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3604
3605 // Ensure that the media transport is ready.
3606 loopback_media_transports()->SetState(webrtc::MediaTransportState::kWritable);
3607 loopback_media_transports()->FlushAsyncInvokes();
3608
3609 // Now add an audio track and do another offer/answer.
3610 caller()->AddAudioTrack();
3611 callee()->AddAudioTrack();
3612 caller()->CreateAndSetAndSignalOffer();
3613 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3614
3615 // Ensure both audio and video frames are received end-to-end.
3616 MediaExpectations media_expectations;
3617 media_expectations.ExpectBidirectionalAudioAndVideo();
3618 ASSERT_TRUE(ExpectNewFrames(media_expectations));
3619
3620 // The second offer should not have generated another media transport.
3621 // Media transport was kept alive, and was not recreated.
3622 EXPECT_EQ(1, loopback_media_transports()->first_factory_transport_count());
3623 EXPECT_EQ(1, loopback_media_transports()->second_factory_transport_count());
3624}
3625
3626TEST_P(PeerConnectionIntegrationTest, MediaTransportOfferUpgradeOnTheCallee) {
3627 PeerConnectionInterface::RTCConfiguration rtc_config;
3628 rtc_config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyRequire;
3629 rtc_config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
3630 rtc_config.use_media_transport = true;
3631 rtc_config.enable_dtls_srtp = false; // SDES is required for media transport.
3632 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
3633 rtc_config, rtc_config, loopback_media_transports()->first_factory(),
3634 loopback_media_transports()->second_factory()));
3635 ConnectFakeSignaling();
3636
3637 // Do initial offer/answer with just a video track.
3638 caller()->AddVideoTrack();
3639 callee()->AddVideoTrack();
3640 caller()->CreateAndSetAndSignalOffer();
3641 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3642
3643 // Ensure that the media transport is ready.
3644 loopback_media_transports()->SetState(webrtc::MediaTransportState::kWritable);
3645 loopback_media_transports()->FlushAsyncInvokes();
3646
3647 // Now add an audio track and do another offer/answer.
3648 caller()->AddAudioTrack();
3649 callee()->AddAudioTrack();
3650 callee()->CreateAndSetAndSignalOffer();
3651 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3652
3653 // Ensure both audio and video frames are received end-to-end.
3654 MediaExpectations media_expectations;
3655 media_expectations.ExpectBidirectionalAudioAndVideo();
3656 ASSERT_TRUE(ExpectNewFrames(media_expectations));
3657
3658 // The second offer should not have generated another media transport.
3659 // Media transport was kept alive, and was not recreated.
3660 EXPECT_EQ(1, loopback_media_transports()->first_factory_transport_count());
3661 EXPECT_EQ(1, loopback_media_transports()->second_factory_transport_count());
3662}
3663
Niels Möllerc68d2822018-11-20 14:52:05 +01003664TEST_P(PeerConnectionIntegrationTest, MediaTransportBidirectionalAudio) {
3665 PeerConnectionInterface::RTCConfiguration rtc_config;
Piotr (Peter) Slatalab1ae10b2019-03-01 11:14:05 -08003666 rtc_config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyRequire;
3667 rtc_config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
Niels Möllerc68d2822018-11-20 14:52:05 +01003668 rtc_config.use_media_transport = true;
3669 rtc_config.enable_dtls_srtp = false; // SDES is required for media transport.
3670 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
3671 rtc_config, rtc_config, loopback_media_transports()->first_factory(),
3672 loopback_media_transports()->second_factory()));
3673 ConnectFakeSignaling();
3674
3675 caller()->AddAudioTrack();
3676 callee()->AddAudioTrack();
3677 // Start offer/answer exchange and wait for it to complete.
3678 caller()->CreateAndSetAndSignalOffer();
3679 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3680
3681 // Ensure that the media transport is ready.
3682 loopback_media_transports()->SetState(webrtc::MediaTransportState::kWritable);
3683 loopback_media_transports()->FlushAsyncInvokes();
3684
3685 MediaExpectations media_expectations;
3686 media_expectations.ExpectBidirectionalAudio();
3687 ASSERT_TRUE(ExpectNewFrames(media_expectations));
3688
3689 webrtc::MediaTransportPair::Stats first_stats =
3690 loopback_media_transports()->FirstStats();
3691 webrtc::MediaTransportPair::Stats second_stats =
3692 loopback_media_transports()->SecondStats();
3693
3694 EXPECT_GT(first_stats.received_audio_frames, 0);
3695 EXPECT_GE(second_stats.sent_audio_frames, first_stats.received_audio_frames);
3696
3697 EXPECT_GT(second_stats.received_audio_frames, 0);
3698 EXPECT_GE(first_stats.sent_audio_frames, second_stats.received_audio_frames);
3699}
3700
Niels Möller46879152019-01-07 15:54:47 +01003701TEST_P(PeerConnectionIntegrationTest, MediaTransportBidirectionalVideo) {
3702 PeerConnectionInterface::RTCConfiguration rtc_config;
3703 rtc_config.use_media_transport = true;
3704 rtc_config.enable_dtls_srtp = false; // SDES is required for media transport.
3705 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
3706 rtc_config, rtc_config, loopback_media_transports()->first_factory(),
3707 loopback_media_transports()->second_factory()));
3708 ConnectFakeSignaling();
3709
3710 caller()->AddVideoTrack();
3711 callee()->AddVideoTrack();
3712 // Start offer/answer exchange and wait for it to complete.
3713 caller()->CreateAndSetAndSignalOffer();
3714 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3715
3716 // Ensure that the media transport is ready.
3717 loopback_media_transports()->SetState(webrtc::MediaTransportState::kWritable);
3718 loopback_media_transports()->FlushAsyncInvokes();
3719
3720 MediaExpectations media_expectations;
3721 media_expectations.ExpectBidirectionalVideo();
3722 ASSERT_TRUE(ExpectNewFrames(media_expectations));
3723
3724 webrtc::MediaTransportPair::Stats first_stats =
3725 loopback_media_transports()->FirstStats();
3726 webrtc::MediaTransportPair::Stats second_stats =
3727 loopback_media_transports()->SecondStats();
3728
3729 EXPECT_GT(first_stats.received_video_frames, 0);
3730 EXPECT_GE(second_stats.sent_video_frames, first_stats.received_video_frames);
3731
3732 EXPECT_GT(second_stats.received_video_frames, 0);
3733 EXPECT_GE(first_stats.sent_video_frames, second_stats.received_video_frames);
3734}
3735
Piotr (Peter) Slatala55b91b92019-01-25 13:31:15 -08003736TEST_P(PeerConnectionIntegrationTest,
3737 MediaTransportDataChannelUsesRtpBidirectionalVideo) {
3738 PeerConnectionInterface::RTCConfiguration rtc_config;
3739 rtc_config.use_media_transport = false;
3740 rtc_config.use_media_transport_for_data_channels = true;
3741 rtc_config.enable_dtls_srtp = false; // SDES is required for media transport.
3742 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
3743 rtc_config, rtc_config, loopback_media_transports()->first_factory(),
3744 loopback_media_transports()->second_factory()));
3745 ConnectFakeSignaling();
3746
3747 caller()->AddVideoTrack();
3748 callee()->AddVideoTrack();
3749 // Start offer/answer exchange and wait for it to complete.
3750 caller()->CreateAndSetAndSignalOffer();
3751 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3752
3753 MediaExpectations media_expectations;
3754 media_expectations.ExpectBidirectionalVideo();
3755 ASSERT_TRUE(ExpectNewFrames(media_expectations));
3756}
3757
deadbeef1dcb1642017-03-29 21:08:16 -07003758// Test that the ICE connection and gathering states eventually reach
3759// "complete".
Seth Hampson2f0d7022018-02-20 11:54:42 -08003760TEST_P(PeerConnectionIntegrationTest, IceStatesReachCompletion) {
deadbeef1dcb1642017-03-29 21:08:16 -07003761 ASSERT_TRUE(CreatePeerConnectionWrappers());
3762 ConnectFakeSignaling();
3763 // Do normal offer/answer.
Steve Anton15324772018-01-16 10:26:49 -08003764 caller()->AddAudioVideoTracks();
3765 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003766 caller()->CreateAndSetAndSignalOffer();
3767 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3768 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceGatheringComplete,
3769 caller()->ice_gathering_state(), kMaxWaitForFramesMs);
3770 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceGatheringComplete,
3771 callee()->ice_gathering_state(), kMaxWaitForFramesMs);
3772 // After the best candidate pair is selected and all candidates are signaled,
3773 // the ICE connection state should reach "complete".
3774 // TODO(deadbeef): Currently, the ICE "controlled" agent (the
3775 // answerer/"callee" by default) only reaches "connected". When this is
3776 // fixed, this test should be updated.
3777 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
3778 caller()->ice_connection_state(), kDefaultTimeout);
Alex Loiko9289eda2018-11-23 16:18:59 +00003779 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
3780 callee()->ice_connection_state(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07003781}
3782
Qingsi Wang1dac6d82018-12-12 15:28:47 -08003783constexpr int kOnlyLocalPorts = cricket::PORTALLOCATOR_DISABLE_STUN |
3784 cricket::PORTALLOCATOR_DISABLE_RELAY |
3785 cricket::PORTALLOCATOR_DISABLE_TCP;
Zach Stein6fcdc2f2018-08-23 16:25:55 -07003786
Qingsi Wang1dac6d82018-12-12 15:28:47 -08003787// Use a mock resolver to resolve the hostname back to the original IP on both
3788// sides and check that the ICE connection connects.
Zach Stein6fcdc2f2018-08-23 16:25:55 -07003789TEST_P(PeerConnectionIntegrationTest,
3790 IceStatesReachCompletionWithRemoteHostname) {
Qingsi Wang1dac6d82018-12-12 15:28:47 -08003791 auto caller_resolver_factory =
3792 absl::make_unique<NiceMock<webrtc::MockAsyncResolverFactory>>();
3793 auto callee_resolver_factory =
3794 absl::make_unique<NiceMock<webrtc::MockAsyncResolverFactory>>();
3795 NiceMock<rtc::MockAsyncResolver> callee_async_resolver;
3796 NiceMock<rtc::MockAsyncResolver> caller_async_resolver;
Zach Stein6fcdc2f2018-08-23 16:25:55 -07003797
3798 // This also verifies that the injected AsyncResolverFactory is used by
3799 // P2PTransportChannel.
Qingsi Wang1dac6d82018-12-12 15:28:47 -08003800 EXPECT_CALL(*caller_resolver_factory, Create())
3801 .WillOnce(Return(&caller_async_resolver));
3802 webrtc::PeerConnectionDependencies caller_deps(nullptr);
3803 caller_deps.async_resolver_factory = std::move(caller_resolver_factory);
3804
3805 EXPECT_CALL(*callee_resolver_factory, Create())
3806 .WillOnce(Return(&callee_async_resolver));
3807 webrtc::PeerConnectionDependencies callee_deps(nullptr);
3808 callee_deps.async_resolver_factory = std::move(callee_resolver_factory);
3809
3810 PeerConnectionInterface::RTCConfiguration config;
3811 config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
3812 config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyRequire;
3813
3814 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndDeps(
3815 config, std::move(caller_deps), config, std::move(callee_deps)));
3816
3817 caller()->SetRemoteAsyncResolver(&callee_async_resolver);
3818 callee()->SetRemoteAsyncResolver(&caller_async_resolver);
3819
3820 // Enable hostname candidates with mDNS names.
3821 caller()->network()->CreateMdnsResponder(network_thread());
3822 callee()->network()->CreateMdnsResponder(network_thread());
3823
3824 SetPortAllocatorFlags(kOnlyLocalPorts, kOnlyLocalPorts);
Zach Stein6fcdc2f2018-08-23 16:25:55 -07003825
3826 ConnectFakeSignaling();
3827 caller()->AddAudioVideoTracks();
3828 callee()->AddAudioVideoTracks();
3829 caller()->CreateAndSetAndSignalOffer();
3830 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3831 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
3832 caller()->ice_connection_state(), kDefaultTimeout);
3833 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
3834 callee()->ice_connection_state(), kDefaultTimeout);
Jeroen de Borst833979f2018-12-13 08:25:54 -08003835
3836 EXPECT_EQ(1, webrtc::metrics::NumEvents(
3837 "WebRTC.PeerConnection.CandidatePairType_UDP",
3838 webrtc::kIceCandidatePairHostNameHostName));
Zach Stein6fcdc2f2018-08-23 16:25:55 -07003839}
3840
Steve Antonede9ca52017-10-16 13:04:27 -07003841// Test that firewalling the ICE connection causes the clients to identify the
3842// disconnected state and then removing the firewall causes them to reconnect.
3843class PeerConnectionIntegrationIceStatesTest
Seth Hampson2f0d7022018-02-20 11:54:42 -08003844 : public PeerConnectionIntegrationBaseTest,
3845 public ::testing::WithParamInterface<
3846 std::tuple<SdpSemantics, std::tuple<std::string, uint32_t>>> {
Steve Antonede9ca52017-10-16 13:04:27 -07003847 protected:
Seth Hampson2f0d7022018-02-20 11:54:42 -08003848 PeerConnectionIntegrationIceStatesTest()
3849 : PeerConnectionIntegrationBaseTest(std::get<0>(GetParam())) {
3850 port_allocator_flags_ = std::get<1>(std::get<1>(GetParam()));
Steve Antonede9ca52017-10-16 13:04:27 -07003851 }
3852
3853 void StartStunServer(const SocketAddress& server_address) {
3854 stun_server_.reset(
3855 cricket::TestStunServer::Create(network_thread(), server_address));
3856 }
3857
3858 bool TestIPv6() {
3859 return (port_allocator_flags_ & cricket::PORTALLOCATOR_ENABLE_IPV6);
3860 }
3861
3862 void SetPortAllocatorFlags() {
Qingsi Wang1dac6d82018-12-12 15:28:47 -08003863 PeerConnectionIntegrationBaseTest::SetPortAllocatorFlags(
3864 port_allocator_flags_, port_allocator_flags_);
Steve Antonede9ca52017-10-16 13:04:27 -07003865 }
3866
3867 std::vector<SocketAddress> CallerAddresses() {
3868 std::vector<SocketAddress> addresses;
3869 addresses.push_back(SocketAddress("1.1.1.1", 0));
3870 if (TestIPv6()) {
3871 addresses.push_back(SocketAddress("1111:0:a:b:c:d:e:f", 0));
3872 }
3873 return addresses;
3874 }
3875
3876 std::vector<SocketAddress> CalleeAddresses() {
3877 std::vector<SocketAddress> addresses;
3878 addresses.push_back(SocketAddress("2.2.2.2", 0));
3879 if (TestIPv6()) {
3880 addresses.push_back(SocketAddress("2222:0:a:b:c:d:e:f", 0));
3881 }
3882 return addresses;
3883 }
3884
3885 void SetUpNetworkInterfaces() {
3886 // Remove the default interfaces added by the test infrastructure.
3887 caller()->network()->RemoveInterface(kDefaultLocalAddress);
3888 callee()->network()->RemoveInterface(kDefaultLocalAddress);
3889
3890 // Add network addresses for test.
3891 for (const auto& caller_address : CallerAddresses()) {
3892 caller()->network()->AddInterface(caller_address);
3893 }
3894 for (const auto& callee_address : CalleeAddresses()) {
3895 callee()->network()->AddInterface(callee_address);
3896 }
3897 }
3898
3899 private:
3900 uint32_t port_allocator_flags_;
3901 std::unique_ptr<cricket::TestStunServer> stun_server_;
3902};
3903
3904// Tests that the PeerConnection goes through all the ICE gathering/connection
3905// states over the duration of the call. This includes Disconnected and Failed
3906// states, induced by putting a firewall between the peers and waiting for them
3907// to time out.
Steve Anton83119dd2017-11-10 16:19:52 -08003908TEST_P(PeerConnectionIntegrationIceStatesTest, VerifyIceStates) {
Jonas Olssonb75d9e92019-02-22 10:33:29 +01003909 rtc::ScopedFakeClock fake_clock;
3910 // Some things use a time of "0" as a special value, so we need to start out
3911 // the fake clock at a nonzero time.
3912 fake_clock.AdvanceTime(TimeDelta::seconds(1));
Steve Antonede9ca52017-10-16 13:04:27 -07003913
3914 const SocketAddress kStunServerAddress =
3915 SocketAddress("99.99.99.1", cricket::STUN_SERVER_PORT);
3916 StartStunServer(kStunServerAddress);
3917
3918 PeerConnectionInterface::RTCConfiguration config;
3919 PeerConnectionInterface::IceServer ice_stun_server;
3920 ice_stun_server.urls.push_back(
3921 "stun:" + kStunServerAddress.HostAsURIString() + ":" +
3922 kStunServerAddress.PortAsString());
3923 config.servers.push_back(ice_stun_server);
3924
3925 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
3926 ConnectFakeSignaling();
3927 SetPortAllocatorFlags();
3928 SetUpNetworkInterfaces();
Steve Anton15324772018-01-16 10:26:49 -08003929 caller()->AddAudioVideoTracks();
3930 callee()->AddAudioVideoTracks();
Steve Antonede9ca52017-10-16 13:04:27 -07003931
3932 // Initial state before anything happens.
3933 ASSERT_EQ(PeerConnectionInterface::kIceGatheringNew,
3934 caller()->ice_gathering_state());
3935 ASSERT_EQ(PeerConnectionInterface::kIceConnectionNew,
3936 caller()->ice_connection_state());
Jonas Olsson7a6739e2019-01-15 16:31:55 +01003937 ASSERT_EQ(PeerConnectionInterface::kIceConnectionNew,
3938 caller()->standardized_ice_connection_state());
Steve Antonede9ca52017-10-16 13:04:27 -07003939
3940 // Start the call by creating the offer, setting it as the local description,
3941 // then sending it to the peer who will respond with an answer. This happens
3942 // asynchronously so that we can watch the states as it runs in the
3943 // background.
3944 caller()->CreateAndSetAndSignalOffer();
3945
Jonas Olsson7a6739e2019-01-15 16:31:55 +01003946 ASSERT_EQ(PeerConnectionInterface::kIceConnectionCompleted,
3947 caller()->ice_connection_state());
Jonas Olssonacd8ae72019-02-25 15:26:24 +01003948 ASSERT_EQ(PeerConnectionInterface::kIceConnectionCompleted,
Jonas Olsson7a6739e2019-01-15 16:31:55 +01003949 caller()->standardized_ice_connection_state());
Steve Antonede9ca52017-10-16 13:04:27 -07003950
3951 // Verify that the observer was notified of the intermediate transitions.
3952 EXPECT_THAT(caller()->ice_connection_state_history(),
3953 ElementsAre(PeerConnectionInterface::kIceConnectionChecking,
3954 PeerConnectionInterface::kIceConnectionConnected,
3955 PeerConnectionInterface::kIceConnectionCompleted));
Jonas Olssonacd8ae72019-02-25 15:26:24 +01003956 EXPECT_THAT(caller()->standardized_ice_connection_state_history(),
3957 ElementsAre(PeerConnectionInterface::kIceConnectionChecking,
3958 PeerConnectionInterface::kIceConnectionConnected,
3959 PeerConnectionInterface::kIceConnectionCompleted));
Jonas Olsson635474e2018-10-18 15:58:17 +02003960 EXPECT_THAT(
3961 caller()->peer_connection_state_history(),
3962 ElementsAre(PeerConnectionInterface::PeerConnectionState::kConnecting,
Jonas Olsson635474e2018-10-18 15:58:17 +02003963 PeerConnectionInterface::PeerConnectionState::kConnected));
Steve Antonede9ca52017-10-16 13:04:27 -07003964 EXPECT_THAT(caller()->ice_gathering_state_history(),
3965 ElementsAre(PeerConnectionInterface::kIceGatheringGathering,
3966 PeerConnectionInterface::kIceGatheringComplete));
3967
3968 // Block connections to/from the caller and wait for ICE to become
3969 // disconnected.
3970 for (const auto& caller_address : CallerAddresses()) {
3971 firewall()->AddRule(false, rtc::FP_ANY, rtc::FD_ANY, caller_address);
3972 }
Mirko Bonadei675513b2017-11-09 11:09:25 +01003973 RTC_LOG(LS_INFO) << "Firewall rules applied";
Jonas Olssonb75d9e92019-02-22 10:33:29 +01003974 ASSERT_EQ_SIMULATED_WAIT(PeerConnectionInterface::kIceConnectionDisconnected,
3975 caller()->ice_connection_state(), kDefaultTimeout,
3976 fake_clock);
3977 ASSERT_EQ_SIMULATED_WAIT(PeerConnectionInterface::kIceConnectionDisconnected,
3978 caller()->standardized_ice_connection_state(),
3979 kDefaultTimeout, fake_clock);
Steve Antonede9ca52017-10-16 13:04:27 -07003980
3981 // Let ICE re-establish by removing the firewall rules.
3982 firewall()->ClearRules();
Mirko Bonadei675513b2017-11-09 11:09:25 +01003983 RTC_LOG(LS_INFO) << "Firewall rules cleared";
Jonas Olssonb75d9e92019-02-22 10:33:29 +01003984 ASSERT_EQ_SIMULATED_WAIT(PeerConnectionInterface::kIceConnectionCompleted,
3985 caller()->ice_connection_state(), kDefaultTimeout,
3986 fake_clock);
Jonas Olssonacd8ae72019-02-25 15:26:24 +01003987 ASSERT_EQ_SIMULATED_WAIT(PeerConnectionInterface::kIceConnectionCompleted,
Jonas Olssonb75d9e92019-02-22 10:33:29 +01003988 caller()->standardized_ice_connection_state(),
3989 kDefaultTimeout, fake_clock);
Steve Antonede9ca52017-10-16 13:04:27 -07003990
3991 // According to RFC7675, if there is no response within 30 seconds then the
3992 // peer should consider the other side to have rejected the connection. This
Steve Anton83119dd2017-11-10 16:19:52 -08003993 // is signaled by the state transitioning to "failed".
Steve Antonede9ca52017-10-16 13:04:27 -07003994 constexpr int kConsentTimeout = 30000;
3995 for (const auto& caller_address : CallerAddresses()) {
3996 firewall()->AddRule(false, rtc::FP_ANY, rtc::FD_ANY, caller_address);
3997 }
Mirko Bonadei675513b2017-11-09 11:09:25 +01003998 RTC_LOG(LS_INFO) << "Firewall rules applied again";
Jonas Olssonb75d9e92019-02-22 10:33:29 +01003999 ASSERT_EQ_SIMULATED_WAIT(PeerConnectionInterface::kIceConnectionFailed,
4000 caller()->ice_connection_state(), kConsentTimeout,
4001 fake_clock);
4002 ASSERT_EQ_SIMULATED_WAIT(PeerConnectionInterface::kIceConnectionFailed,
4003 caller()->standardized_ice_connection_state(),
4004 kConsentTimeout, fake_clock);
4005
4006 // We need to manually close the peerconnections before the fake clock goes
4007 // out of scope, or we trigger a DCHECK in rtp_sender.cc when we briefly
4008 // return to using non-faked time.
4009 delete SetCallerPcWrapperAndReturnCurrent(nullptr);
4010 delete SetCalleePcWrapperAndReturnCurrent(nullptr);
4011}
4012
4013// Tests that if the connection doesn't get set up properly we eventually reach
4014// the "failed" iceConnectionState.
4015TEST_P(PeerConnectionIntegrationIceStatesTest, IceStateSetupFailure) {
4016 rtc::ScopedFakeClock fake_clock;
4017 // Some things use a time of "0" as a special value, so we need to start out
4018 // the fake clock at a nonzero time.
4019 fake_clock.AdvanceTime(TimeDelta::seconds(1));
4020
4021 // Block connections to/from the caller and wait for ICE to become
4022 // disconnected.
4023 for (const auto& caller_address : CallerAddresses()) {
4024 firewall()->AddRule(false, rtc::FP_ANY, rtc::FD_ANY, caller_address);
4025 }
4026
4027 ASSERT_TRUE(CreatePeerConnectionWrappers());
4028 ConnectFakeSignaling();
4029 SetPortAllocatorFlags();
4030 SetUpNetworkInterfaces();
4031 caller()->AddAudioVideoTracks();
4032 caller()->CreateAndSetAndSignalOffer();
4033
4034 // According to RFC7675, if there is no response within 30 seconds then the
4035 // peer should consider the other side to have rejected the connection. This
4036 // is signaled by the state transitioning to "failed".
4037 constexpr int kConsentTimeout = 30000;
4038 ASSERT_EQ_SIMULATED_WAIT(PeerConnectionInterface::kIceConnectionFailed,
4039 caller()->standardized_ice_connection_state(),
4040 kConsentTimeout, fake_clock);
4041
4042 // We need to manually close the peerconnections before the fake clock goes
4043 // out of scope, or we trigger a DCHECK in rtp_sender.cc when we briefly
4044 // return to using non-faked time.
4045 delete SetCallerPcWrapperAndReturnCurrent(nullptr);
4046 delete SetCalleePcWrapperAndReturnCurrent(nullptr);
Steve Antonede9ca52017-10-16 13:04:27 -07004047}
4048
4049// Tests that the best connection is set to the appropriate IPv4/IPv6 connection
4050// and that the statistics in the metric observers are updated correctly.
4051TEST_P(PeerConnectionIntegrationIceStatesTest, VerifyBestConnection) {
4052 ASSERT_TRUE(CreatePeerConnectionWrappers());
4053 ConnectFakeSignaling();
4054 SetPortAllocatorFlags();
4055 SetUpNetworkInterfaces();
Steve Anton15324772018-01-16 10:26:49 -08004056 caller()->AddAudioVideoTracks();
4057 callee()->AddAudioVideoTracks();
Steve Antonede9ca52017-10-16 13:04:27 -07004058 caller()->CreateAndSetAndSignalOffer();
4059
4060 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4061
Qingsi Wang7fc821d2018-07-12 12:54:53 -07004062 // TODO(bugs.webrtc.org/9456): Fix it.
4063 const int num_best_ipv4 = webrtc::metrics::NumEvents(
4064 "WebRTC.PeerConnection.IPMetrics", webrtc::kBestConnections_IPv4);
4065 const int num_best_ipv6 = webrtc::metrics::NumEvents(
4066 "WebRTC.PeerConnection.IPMetrics", webrtc::kBestConnections_IPv6);
Steve Antonede9ca52017-10-16 13:04:27 -07004067 if (TestIPv6()) {
4068 // When IPv6 is enabled, we should prefer an IPv6 connection over an IPv4
4069 // connection.
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02004070 EXPECT_EQ(0, num_best_ipv4);
4071 EXPECT_EQ(1, num_best_ipv6);
Steve Antonede9ca52017-10-16 13:04:27 -07004072 } else {
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02004073 EXPECT_EQ(1, num_best_ipv4);
4074 EXPECT_EQ(0, num_best_ipv6);
Steve Antonede9ca52017-10-16 13:04:27 -07004075 }
4076
Qingsi Wang7fc821d2018-07-12 12:54:53 -07004077 EXPECT_EQ(0, webrtc::metrics::NumEvents(
4078 "WebRTC.PeerConnection.CandidatePairType_UDP",
4079 webrtc::kIceCandidatePairHostHost));
4080 EXPECT_EQ(1, webrtc::metrics::NumEvents(
4081 "WebRTC.PeerConnection.CandidatePairType_UDP",
4082 webrtc::kIceCandidatePairHostPublicHostPublic));
Steve Antonede9ca52017-10-16 13:04:27 -07004083}
4084
4085constexpr uint32_t kFlagsIPv4NoStun = cricket::PORTALLOCATOR_DISABLE_TCP |
4086 cricket::PORTALLOCATOR_DISABLE_STUN |
4087 cricket::PORTALLOCATOR_DISABLE_RELAY;
4088constexpr uint32_t kFlagsIPv6NoStun =
4089 cricket::PORTALLOCATOR_DISABLE_TCP | cricket::PORTALLOCATOR_DISABLE_STUN |
4090 cricket::PORTALLOCATOR_ENABLE_IPV6 | cricket::PORTALLOCATOR_DISABLE_RELAY;
4091constexpr uint32_t kFlagsIPv4Stun =
4092 cricket::PORTALLOCATOR_DISABLE_TCP | cricket::PORTALLOCATOR_DISABLE_RELAY;
4093
Mirko Bonadeic84f6612019-01-31 12:20:57 +01004094INSTANTIATE_TEST_SUITE_P(
Seth Hampson2f0d7022018-02-20 11:54:42 -08004095 PeerConnectionIntegrationTest,
4096 PeerConnectionIntegrationIceStatesTest,
4097 Combine(Values(SdpSemantics::kPlanB, SdpSemantics::kUnifiedPlan),
4098 Values(std::make_pair("IPv4 no STUN", kFlagsIPv4NoStun),
4099 std::make_pair("IPv6 no STUN", kFlagsIPv6NoStun),
4100 std::make_pair("IPv4 with STUN", kFlagsIPv4Stun))));
Steve Antonede9ca52017-10-16 13:04:27 -07004101
deadbeef1dcb1642017-03-29 21:08:16 -07004102// This test sets up a call between two parties with audio and video.
4103// During the call, the caller restarts ICE and the test verifies that
4104// new ICE candidates are generated and audio and video still can flow, and the
4105// ICE state reaches completed again.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004106TEST_P(PeerConnectionIntegrationTest, MediaContinuesFlowingAfterIceRestart) {
deadbeef1dcb1642017-03-29 21:08:16 -07004107 ASSERT_TRUE(CreatePeerConnectionWrappers());
4108 ConnectFakeSignaling();
4109 // Do normal offer/answer and wait for ICE to complete.
Steve Anton15324772018-01-16 10:26:49 -08004110 caller()->AddAudioVideoTracks();
4111 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07004112 caller()->CreateAndSetAndSignalOffer();
4113 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4114 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
4115 caller()->ice_connection_state(), kMaxWaitForFramesMs);
Alex Loiko9289eda2018-11-23 16:18:59 +00004116 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
4117 callee()->ice_connection_state(), kMaxWaitForFramesMs);
deadbeef1dcb1642017-03-29 21:08:16 -07004118
4119 // To verify that the ICE restart actually occurs, get
4120 // ufrag/password/candidates before and after restart.
4121 // Create an SDP string of the first audio candidate for both clients.
4122 const webrtc::IceCandidateCollection* audio_candidates_caller =
4123 caller()->pc()->local_description()->candidates(0);
4124 const webrtc::IceCandidateCollection* audio_candidates_callee =
4125 callee()->pc()->local_description()->candidates(0);
4126 ASSERT_GT(audio_candidates_caller->count(), 0u);
4127 ASSERT_GT(audio_candidates_callee->count(), 0u);
4128 std::string caller_candidate_pre_restart;
4129 ASSERT_TRUE(
4130 audio_candidates_caller->at(0)->ToString(&caller_candidate_pre_restart));
4131 std::string callee_candidate_pre_restart;
4132 ASSERT_TRUE(
4133 audio_candidates_callee->at(0)->ToString(&callee_candidate_pre_restart));
4134 const cricket::SessionDescription* desc =
4135 caller()->pc()->local_description()->description();
4136 std::string caller_ufrag_pre_restart =
4137 desc->transport_infos()[0].description.ice_ufrag;
4138 desc = callee()->pc()->local_description()->description();
4139 std::string callee_ufrag_pre_restart =
4140 desc->transport_infos()[0].description.ice_ufrag;
4141
4142 // Have the caller initiate an ICE restart.
4143 caller()->SetOfferAnswerOptions(IceRestartOfferAnswerOptions());
4144 caller()->CreateAndSetAndSignalOffer();
4145 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4146 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
4147 caller()->ice_connection_state(), kMaxWaitForFramesMs);
Alex Loiko9289eda2018-11-23 16:18:59 +00004148 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
deadbeef1dcb1642017-03-29 21:08:16 -07004149 callee()->ice_connection_state(), kMaxWaitForFramesMs);
4150
4151 // Grab the ufrags/candidates again.
4152 audio_candidates_caller = caller()->pc()->local_description()->candidates(0);
4153 audio_candidates_callee = callee()->pc()->local_description()->candidates(0);
4154 ASSERT_GT(audio_candidates_caller->count(), 0u);
4155 ASSERT_GT(audio_candidates_callee->count(), 0u);
4156 std::string caller_candidate_post_restart;
4157 ASSERT_TRUE(
4158 audio_candidates_caller->at(0)->ToString(&caller_candidate_post_restart));
4159 std::string callee_candidate_post_restart;
4160 ASSERT_TRUE(
4161 audio_candidates_callee->at(0)->ToString(&callee_candidate_post_restart));
4162 desc = caller()->pc()->local_description()->description();
4163 std::string caller_ufrag_post_restart =
4164 desc->transport_infos()[0].description.ice_ufrag;
4165 desc = callee()->pc()->local_description()->description();
4166 std::string callee_ufrag_post_restart =
4167 desc->transport_infos()[0].description.ice_ufrag;
4168 // Sanity check that an ICE restart was actually negotiated in SDP.
4169 ASSERT_NE(caller_candidate_pre_restart, caller_candidate_post_restart);
4170 ASSERT_NE(callee_candidate_pre_restart, callee_candidate_post_restart);
4171 ASSERT_NE(caller_ufrag_pre_restart, caller_ufrag_post_restart);
4172 ASSERT_NE(callee_ufrag_pre_restart, callee_ufrag_post_restart);
4173
4174 // Ensure that additional frames are received after the ICE restart.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004175 MediaExpectations media_expectations;
4176 media_expectations.ExpectBidirectionalAudioAndVideo();
4177 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07004178}
4179
4180// Verify that audio/video can be received end-to-end when ICE renomination is
4181// enabled.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004182TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithIceRenomination) {
deadbeef1dcb1642017-03-29 21:08:16 -07004183 PeerConnectionInterface::RTCConfiguration config;
4184 config.enable_ice_renomination = true;
4185 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
4186 ConnectFakeSignaling();
4187 // Do normal offer/answer and wait for some frames to be received in each
4188 // direction.
Steve Anton15324772018-01-16 10:26:49 -08004189 caller()->AddAudioVideoTracks();
4190 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07004191 caller()->CreateAndSetAndSignalOffer();
4192 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4193 // Sanity check that ICE renomination was actually negotiated.
4194 const cricket::SessionDescription* desc =
4195 caller()->pc()->local_description()->description();
4196 for (const cricket::TransportInfo& info : desc->transport_infos()) {
Steve Anton64b626b2019-01-28 17:25:26 -08004197 ASSERT_THAT(info.description.transport_options, Contains("renomination"));
deadbeef1dcb1642017-03-29 21:08:16 -07004198 }
4199 desc = callee()->pc()->local_description()->description();
4200 for (const cricket::TransportInfo& info : desc->transport_infos()) {
Steve Anton64b626b2019-01-28 17:25:26 -08004201 ASSERT_THAT(info.description.transport_options, Contains("renomination"));
deadbeef1dcb1642017-03-29 21:08:16 -07004202 }
Seth Hampson2f0d7022018-02-20 11:54:42 -08004203 MediaExpectations media_expectations;
4204 media_expectations.ExpectBidirectionalAudioAndVideo();
4205 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07004206}
4207
Steve Anton6f25b092017-10-23 09:39:20 -07004208// With a max bundle policy and RTCP muxing, adding a new media description to
4209// the connection should not affect ICE at all because the new media will use
4210// the existing connection.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004211TEST_P(PeerConnectionIntegrationTest,
Steve Anton83119dd2017-11-10 16:19:52 -08004212 AddMediaToConnectedBundleDoesNotRestartIce) {
Steve Anton6f25b092017-10-23 09:39:20 -07004213 PeerConnectionInterface::RTCConfiguration config;
4214 config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
4215 config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyRequire;
4216 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(
4217 config, PeerConnectionInterface::RTCConfiguration()));
4218 ConnectFakeSignaling();
4219
Steve Anton15324772018-01-16 10:26:49 -08004220 caller()->AddAudioTrack();
Steve Anton6f25b092017-10-23 09:39:20 -07004221 caller()->CreateAndSetAndSignalOffer();
4222 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Steve Antonff52f1b2017-10-26 12:24:50 -07004223 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceConnectionCompleted,
4224 caller()->ice_connection_state(), kDefaultTimeout);
Steve Anton6f25b092017-10-23 09:39:20 -07004225
4226 caller()->clear_ice_connection_state_history();
4227
Steve Anton15324772018-01-16 10:26:49 -08004228 caller()->AddVideoTrack();
Steve Anton6f25b092017-10-23 09:39:20 -07004229 caller()->CreateAndSetAndSignalOffer();
4230 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4231
4232 EXPECT_EQ(0u, caller()->ice_connection_state_history().size());
4233}
4234
deadbeef1dcb1642017-03-29 21:08:16 -07004235// This test sets up a call between two parties with audio and video. It then
4236// renegotiates setting the video m-line to "port 0", then later renegotiates
4237// again, enabling video.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004238TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07004239 VideoFlowsAfterMediaSectionIsRejectedAndRecycled) {
4240 ASSERT_TRUE(CreatePeerConnectionWrappers());
4241 ConnectFakeSignaling();
4242
4243 // Do initial negotiation, only sending media from the caller. Will result in
4244 // video and audio recvonly "m=" sections.
Steve Anton15324772018-01-16 10:26:49 -08004245 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07004246 caller()->CreateAndSetAndSignalOffer();
4247 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4248
4249 // Negotiate again, disabling the video "m=" section (the callee will set the
4250 // port to 0 due to offer_to_receive_video = 0).
Seth Hampson2f0d7022018-02-20 11:54:42 -08004251 if (sdp_semantics_ == SdpSemantics::kPlanB) {
4252 PeerConnectionInterface::RTCOfferAnswerOptions options;
4253 options.offer_to_receive_video = 0;
4254 callee()->SetOfferAnswerOptions(options);
4255 } else {
4256 callee()->SetRemoteOfferHandler([this] {
4257 callee()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)->Stop();
4258 });
4259 }
deadbeef1dcb1642017-03-29 21:08:16 -07004260 caller()->CreateAndSetAndSignalOffer();
4261 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4262 // Sanity check that video "m=" section was actually rejected.
4263 const ContentInfo* answer_video_content = cricket::GetFirstVideoContent(
4264 callee()->pc()->local_description()->description());
4265 ASSERT_NE(nullptr, answer_video_content);
4266 ASSERT_TRUE(answer_video_content->rejected);
4267
4268 // Enable video and do negotiation again, making sure video is received
4269 // end-to-end, also adding media stream to callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004270 if (sdp_semantics_ == SdpSemantics::kPlanB) {
4271 PeerConnectionInterface::RTCOfferAnswerOptions options;
4272 options.offer_to_receive_video = 1;
4273 callee()->SetOfferAnswerOptions(options);
4274 } else {
4275 // The caller's transceiver is stopped, so we need to add another track.
4276 auto caller_transceiver =
4277 caller()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO);
4278 EXPECT_TRUE(caller_transceiver->stopped());
4279 caller()->AddVideoTrack();
4280 }
4281 callee()->AddVideoTrack();
4282 callee()->SetRemoteOfferHandler(nullptr);
deadbeef1dcb1642017-03-29 21:08:16 -07004283 caller()->CreateAndSetAndSignalOffer();
4284 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08004285
deadbeef1dcb1642017-03-29 21:08:16 -07004286 // Verify the caller receives frames from the newly added stream, and the
4287 // callee receives additional frames from the re-enabled video m= section.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004288 MediaExpectations media_expectations;
4289 media_expectations.CalleeExpectsSomeAudio();
4290 media_expectations.ExpectBidirectionalVideo();
4291 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07004292}
4293
deadbeef1dcb1642017-03-29 21:08:16 -07004294// This tests that if we negotiate after calling CreateSender but before we
4295// have a track, then set a track later, frames from the newly-set track are
4296// received end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004297TEST_F(PeerConnectionIntegrationTestPlanB,
deadbeef1dcb1642017-03-29 21:08:16 -07004298 MediaFlowsAfterEarlyWarmupWithCreateSender) {
4299 ASSERT_TRUE(CreatePeerConnectionWrappers());
4300 ConnectFakeSignaling();
4301 auto caller_audio_sender =
4302 caller()->pc()->CreateSender("audio", "caller_stream");
4303 auto caller_video_sender =
4304 caller()->pc()->CreateSender("video", "caller_stream");
4305 auto callee_audio_sender =
4306 callee()->pc()->CreateSender("audio", "callee_stream");
4307 auto callee_video_sender =
4308 callee()->pc()->CreateSender("video", "callee_stream");
4309 caller()->CreateAndSetAndSignalOffer();
4310 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
4311 // Wait for ICE to complete, without any tracks being set.
4312 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
4313 caller()->ice_connection_state(), kMaxWaitForFramesMs);
4314 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
4315 callee()->ice_connection_state(), kMaxWaitForFramesMs);
4316 // Now set the tracks, and expect frames to immediately start flowing.
4317 EXPECT_TRUE(caller_audio_sender->SetTrack(caller()->CreateLocalAudioTrack()));
4318 EXPECT_TRUE(caller_video_sender->SetTrack(caller()->CreateLocalVideoTrack()));
4319 EXPECT_TRUE(callee_audio_sender->SetTrack(callee()->CreateLocalAudioTrack()));
4320 EXPECT_TRUE(callee_video_sender->SetTrack(callee()->CreateLocalVideoTrack()));
Seth Hampson2f0d7022018-02-20 11:54:42 -08004321 MediaExpectations media_expectations;
4322 media_expectations.ExpectBidirectionalAudioAndVideo();
4323 ASSERT_TRUE(ExpectNewFrames(media_expectations));
4324}
4325
4326// This tests that if we negotiate after calling AddTransceiver but before we
4327// have a track, then set a track later, frames from the newly-set tracks are
4328// received end-to-end.
4329TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
4330 MediaFlowsAfterEarlyWarmupWithAddTransceiver) {
4331 ASSERT_TRUE(CreatePeerConnectionWrappers());
4332 ConnectFakeSignaling();
4333 auto audio_result = caller()->pc()->AddTransceiver(cricket::MEDIA_TYPE_AUDIO);
4334 ASSERT_EQ(RTCErrorType::NONE, audio_result.error().type());
4335 auto caller_audio_sender = audio_result.MoveValue()->sender();
4336 auto video_result = caller()->pc()->AddTransceiver(cricket::MEDIA_TYPE_VIDEO);
4337 ASSERT_EQ(RTCErrorType::NONE, video_result.error().type());
4338 auto caller_video_sender = video_result.MoveValue()->sender();
4339 callee()->SetRemoteOfferHandler([this] {
4340 ASSERT_EQ(2u, callee()->pc()->GetTransceivers().size());
4341 callee()->pc()->GetTransceivers()[0]->SetDirection(
4342 RtpTransceiverDirection::kSendRecv);
4343 callee()->pc()->GetTransceivers()[1]->SetDirection(
4344 RtpTransceiverDirection::kSendRecv);
4345 });
4346 caller()->CreateAndSetAndSignalOffer();
4347 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
4348 // Wait for ICE to complete, without any tracks being set.
4349 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
4350 caller()->ice_connection_state(), kMaxWaitForFramesMs);
4351 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
4352 callee()->ice_connection_state(), kMaxWaitForFramesMs);
4353 // Now set the tracks, and expect frames to immediately start flowing.
4354 auto callee_audio_sender = callee()->pc()->GetSenders()[0];
4355 auto callee_video_sender = callee()->pc()->GetSenders()[1];
4356 ASSERT_TRUE(caller_audio_sender->SetTrack(caller()->CreateLocalAudioTrack()));
4357 ASSERT_TRUE(caller_video_sender->SetTrack(caller()->CreateLocalVideoTrack()));
4358 ASSERT_TRUE(callee_audio_sender->SetTrack(callee()->CreateLocalAudioTrack()));
4359 ASSERT_TRUE(callee_video_sender->SetTrack(callee()->CreateLocalVideoTrack()));
4360 MediaExpectations media_expectations;
4361 media_expectations.ExpectBidirectionalAudioAndVideo();
4362 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07004363}
4364
4365// This test verifies that a remote video track can be added via AddStream,
4366// and sent end-to-end. For this particular test, it's simply echoed back
4367// from the caller to the callee, rather than being forwarded to a third
4368// PeerConnection.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004369TEST_F(PeerConnectionIntegrationTestPlanB, CanSendRemoteVideoTrack) {
deadbeef1dcb1642017-03-29 21:08:16 -07004370 ASSERT_TRUE(CreatePeerConnectionWrappers());
4371 ConnectFakeSignaling();
4372 // Just send a video track from the caller.
Steve Anton15324772018-01-16 10:26:49 -08004373 caller()->AddVideoTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07004374 caller()->CreateAndSetAndSignalOffer();
4375 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02004376 ASSERT_EQ(1U, callee()->remote_streams()->count());
deadbeef1dcb1642017-03-29 21:08:16 -07004377
4378 // Echo the stream back, and do a new offer/anwer (initiated by callee this
4379 // time).
4380 callee()->pc()->AddStream(callee()->remote_streams()->at(0));
4381 callee()->CreateAndSetAndSignalOffer();
4382 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
4383
Seth Hampson2f0d7022018-02-20 11:54:42 -08004384 MediaExpectations media_expectations;
4385 media_expectations.ExpectBidirectionalVideo();
4386 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07004387}
4388
4389// Test that we achieve the expected end-to-end connection time, using a
4390// fake clock and simulated latency on the media and signaling paths.
4391// We use a TURN<->TURN connection because this is usually the quickest to
4392// set up initially, especially when we're confident the connection will work
4393// and can start sending media before we get a STUN response.
4394//
4395// With various optimizations enabled, here are the network delays we expect to
4396// be on the critical path:
4397// 1. 2 signaling trips: Signaling offer and offerer's TURN candidate, then
4398// signaling answer (with DTLS fingerprint).
4399// 2. 9 media hops: Rest of the DTLS handshake. 3 hops in each direction when
4400// using TURN<->TURN pair, and DTLS exchange is 4 packets,
4401// the first of which should have arrived before the answer.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004402TEST_P(PeerConnectionIntegrationTest, EndToEndConnectionTimeWithTurnTurnPair) {
deadbeef1dcb1642017-03-29 21:08:16 -07004403 rtc::ScopedFakeClock fake_clock;
4404 // Some things use a time of "0" as a special value, so we need to start out
4405 // the fake clock at a nonzero time.
4406 // TODO(deadbeef): Fix this.
Sebastian Jansson5f83cf02018-05-08 14:52:22 +02004407 fake_clock.AdvanceTime(webrtc::TimeDelta::seconds(1));
deadbeef1dcb1642017-03-29 21:08:16 -07004408
4409 static constexpr int media_hop_delay_ms = 50;
4410 static constexpr int signaling_trip_delay_ms = 500;
4411 // For explanation of these values, see comment above.
4412 static constexpr int required_media_hops = 9;
4413 static constexpr int required_signaling_trips = 2;
4414 // For internal delays (such as posting an event asychronously).
4415 static constexpr int allowed_internal_delay_ms = 20;
4416 static constexpr int total_connection_time_ms =
4417 media_hop_delay_ms * required_media_hops +
4418 signaling_trip_delay_ms * required_signaling_trips +
4419 allowed_internal_delay_ms;
4420
4421 static const rtc::SocketAddress turn_server_1_internal_address{"88.88.88.0",
4422 3478};
4423 static const rtc::SocketAddress turn_server_1_external_address{"88.88.88.1",
4424 0};
4425 static const rtc::SocketAddress turn_server_2_internal_address{"99.99.99.0",
4426 3478};
4427 static const rtc::SocketAddress turn_server_2_external_address{"99.99.99.1",
4428 0};
Seth Hampsonaed71642018-06-11 07:41:32 -07004429 cricket::TestTurnServer* turn_server_1 = CreateTurnServer(
4430 turn_server_1_internal_address, turn_server_1_external_address);
Jonas Orelandbdcee282017-10-10 14:01:40 +02004431
Seth Hampsonaed71642018-06-11 07:41:32 -07004432 cricket::TestTurnServer* turn_server_2 = CreateTurnServer(
4433 turn_server_2_internal_address, turn_server_2_external_address);
deadbeef1dcb1642017-03-29 21:08:16 -07004434 // Bypass permission check on received packets so media can be sent before
4435 // the candidate is signaled.
Seth Hampsonaed71642018-06-11 07:41:32 -07004436 network_thread()->Invoke<void>(RTC_FROM_HERE, [turn_server_1] {
4437 turn_server_1->set_enable_permission_checks(false);
4438 });
4439 network_thread()->Invoke<void>(RTC_FROM_HERE, [turn_server_2] {
4440 turn_server_2->set_enable_permission_checks(false);
4441 });
deadbeef1dcb1642017-03-29 21:08:16 -07004442
4443 PeerConnectionInterface::RTCConfiguration client_1_config;
4444 webrtc::PeerConnectionInterface::IceServer ice_server_1;
4445 ice_server_1.urls.push_back("turn:88.88.88.0:3478");
4446 ice_server_1.username = "test";
4447 ice_server_1.password = "test";
4448 client_1_config.servers.push_back(ice_server_1);
4449 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
4450 client_1_config.presume_writable_when_fully_relayed = true;
4451
4452 PeerConnectionInterface::RTCConfiguration client_2_config;
4453 webrtc::PeerConnectionInterface::IceServer ice_server_2;
4454 ice_server_2.urls.push_back("turn:99.99.99.0:3478");
4455 ice_server_2.username = "test";
4456 ice_server_2.password = "test";
4457 client_2_config.servers.push_back(ice_server_2);
4458 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
4459 client_2_config.presume_writable_when_fully_relayed = true;
4460
4461 ASSERT_TRUE(
4462 CreatePeerConnectionWrappersWithConfig(client_1_config, client_2_config));
4463 // Set up the simulated delays.
4464 SetSignalingDelayMs(signaling_trip_delay_ms);
4465 ConnectFakeSignaling();
4466 virtual_socket_server()->set_delay_mean(media_hop_delay_ms);
4467 virtual_socket_server()->UpdateDelayDistribution();
4468
4469 // Set "offer to receive audio/video" without adding any tracks, so we just
4470 // set up ICE/DTLS with no media.
4471 PeerConnectionInterface::RTCOfferAnswerOptions options;
4472 options.offer_to_receive_audio = 1;
4473 options.offer_to_receive_video = 1;
4474 caller()->SetOfferAnswerOptions(options);
4475 caller()->CreateAndSetAndSignalOffer();
deadbeef71452802017-05-07 17:21:01 -07004476 EXPECT_TRUE_SIMULATED_WAIT(DtlsConnected(), total_connection_time_ms,
4477 fake_clock);
Seth Hampson1d4a76d2018-06-19 14:31:41 -07004478 // Closing the PeerConnections destroys the ports before the ScopedFakeClock.
4479 // If this is not done a DCHECK can be hit in ports.cc, because a large
4480 // negative number is calculated for the rtt due to the global clock changing.
4481 caller()->pc()->Close();
4482 callee()->pc()->Close();
deadbeef1dcb1642017-03-29 21:08:16 -07004483}
4484
Jonas Orelandbdcee282017-10-10 14:01:40 +02004485// Verify that a TurnCustomizer passed in through RTCConfiguration
4486// is actually used by the underlying TURN candidate pair.
4487// Note that turnport_unittest.cc contains more detailed, lower-level tests.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004488TEST_P(PeerConnectionIntegrationTest, TurnCustomizerUsedForTurnConnections) {
Jonas Orelandbdcee282017-10-10 14:01:40 +02004489 static const rtc::SocketAddress turn_server_1_internal_address{"88.88.88.0",
4490 3478};
4491 static const rtc::SocketAddress turn_server_1_external_address{"88.88.88.1",
4492 0};
4493 static const rtc::SocketAddress turn_server_2_internal_address{"99.99.99.0",
4494 3478};
4495 static const rtc::SocketAddress turn_server_2_external_address{"99.99.99.1",
4496 0};
Seth Hampsonaed71642018-06-11 07:41:32 -07004497 CreateTurnServer(turn_server_1_internal_address,
4498 turn_server_1_external_address);
4499 CreateTurnServer(turn_server_2_internal_address,
4500 turn_server_2_external_address);
Jonas Orelandbdcee282017-10-10 14:01:40 +02004501
4502 PeerConnectionInterface::RTCConfiguration client_1_config;
4503 webrtc::PeerConnectionInterface::IceServer ice_server_1;
4504 ice_server_1.urls.push_back("turn:88.88.88.0:3478");
4505 ice_server_1.username = "test";
4506 ice_server_1.password = "test";
4507 client_1_config.servers.push_back(ice_server_1);
4508 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
Seth Hampsonaed71642018-06-11 07:41:32 -07004509 auto* customizer1 = CreateTurnCustomizer();
4510 client_1_config.turn_customizer = customizer1;
Jonas Orelandbdcee282017-10-10 14:01:40 +02004511
4512 PeerConnectionInterface::RTCConfiguration client_2_config;
4513 webrtc::PeerConnectionInterface::IceServer ice_server_2;
4514 ice_server_2.urls.push_back("turn:99.99.99.0:3478");
4515 ice_server_2.username = "test";
4516 ice_server_2.password = "test";
4517 client_2_config.servers.push_back(ice_server_2);
4518 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
Seth Hampsonaed71642018-06-11 07:41:32 -07004519 auto* customizer2 = CreateTurnCustomizer();
4520 client_2_config.turn_customizer = customizer2;
Jonas Orelandbdcee282017-10-10 14:01:40 +02004521
4522 ASSERT_TRUE(
4523 CreatePeerConnectionWrappersWithConfig(client_1_config, client_2_config));
4524 ConnectFakeSignaling();
4525
4526 // Set "offer to receive audio/video" without adding any tracks, so we just
4527 // set up ICE/DTLS with no media.
4528 PeerConnectionInterface::RTCOfferAnswerOptions options;
4529 options.offer_to_receive_audio = 1;
4530 options.offer_to_receive_video = 1;
4531 caller()->SetOfferAnswerOptions(options);
4532 caller()->CreateAndSetAndSignalOffer();
4533 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
4534
Seth Hampsonaed71642018-06-11 07:41:32 -07004535 ExpectTurnCustomizerCountersIncremented(customizer1);
4536 ExpectTurnCustomizerCountersIncremented(customizer2);
Jonas Orelandbdcee282017-10-10 14:01:40 +02004537}
4538
Benjamin Wright2d5f3cb2018-05-22 14:46:06 -07004539// Verifies that you can use TCP instead of UDP to connect to a TURN server and
4540// send media between the caller and the callee.
4541TEST_P(PeerConnectionIntegrationTest, TCPUsedForTurnConnections) {
4542 static const rtc::SocketAddress turn_server_internal_address{"88.88.88.0",
4543 3478};
4544 static const rtc::SocketAddress turn_server_external_address{"88.88.88.1", 0};
4545
4546 // Enable TCP for the fake turn server.
Seth Hampsonaed71642018-06-11 07:41:32 -07004547 CreateTurnServer(turn_server_internal_address, turn_server_external_address,
4548 cricket::PROTO_TCP);
Benjamin Wright2d5f3cb2018-05-22 14:46:06 -07004549
4550 webrtc::PeerConnectionInterface::IceServer ice_server;
4551 ice_server.urls.push_back("turn:88.88.88.0:3478?transport=tcp");
4552 ice_server.username = "test";
4553 ice_server.password = "test";
4554
4555 PeerConnectionInterface::RTCConfiguration client_1_config;
4556 client_1_config.servers.push_back(ice_server);
4557 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
4558
4559 PeerConnectionInterface::RTCConfiguration client_2_config;
4560 client_2_config.servers.push_back(ice_server);
4561 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
4562
4563 ASSERT_TRUE(
4564 CreatePeerConnectionWrappersWithConfig(client_1_config, client_2_config));
4565
4566 // Do normal offer/answer and wait for ICE to complete.
4567 ConnectFakeSignaling();
4568 caller()->AddAudioVideoTracks();
4569 callee()->AddAudioVideoTracks();
4570 caller()->CreateAndSetAndSignalOffer();
4571 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4572 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
4573 callee()->ice_connection_state(), kMaxWaitForFramesMs);
4574
4575 MediaExpectations media_expectations;
4576 media_expectations.ExpectBidirectionalAudioAndVideo();
4577 EXPECT_TRUE(ExpectNewFrames(media_expectations));
4578}
4579
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07004580// Verify that a SSLCertificateVerifier passed in through
4581// PeerConnectionDependencies is actually used by the underlying SSL
4582// implementation to determine whether a certificate presented by the TURN
4583// server is accepted by the client. Note that openssladapter_unittest.cc
4584// contains more detailed, lower-level tests.
4585TEST_P(PeerConnectionIntegrationTest,
4586 SSLCertificateVerifierUsedForTurnConnections) {
4587 static const rtc::SocketAddress turn_server_internal_address{"88.88.88.0",
4588 3478};
4589 static const rtc::SocketAddress turn_server_external_address{"88.88.88.1", 0};
4590
4591 // Enable TCP-TLS for the fake turn server. We need to pass in 88.88.88.0 so
4592 // that host name verification passes on the fake certificate.
Seth Hampsonaed71642018-06-11 07:41:32 -07004593 CreateTurnServer(turn_server_internal_address, turn_server_external_address,
4594 cricket::PROTO_TLS, "88.88.88.0");
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07004595
4596 webrtc::PeerConnectionInterface::IceServer ice_server;
4597 ice_server.urls.push_back("turns:88.88.88.0:3478?transport=tcp");
4598 ice_server.username = "test";
4599 ice_server.password = "test";
4600
4601 PeerConnectionInterface::RTCConfiguration client_1_config;
4602 client_1_config.servers.push_back(ice_server);
4603 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
4604
4605 PeerConnectionInterface::RTCConfiguration client_2_config;
4606 client_2_config.servers.push_back(ice_server);
4607 // Setting the type to kRelay forces the connection to go through a TURN
4608 // server.
4609 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
4610
4611 // Get a copy to the pointer so we can verify calls later.
4612 rtc::TestCertificateVerifier* client_1_cert_verifier =
4613 new rtc::TestCertificateVerifier();
4614 client_1_cert_verifier->verify_certificate_ = true;
4615 rtc::TestCertificateVerifier* client_2_cert_verifier =
4616 new rtc::TestCertificateVerifier();
4617 client_2_cert_verifier->verify_certificate_ = true;
4618
4619 // Create the dependencies with the test certificate verifier.
4620 webrtc::PeerConnectionDependencies client_1_deps(nullptr);
4621 client_1_deps.tls_cert_verifier =
4622 std::unique_ptr<rtc::TestCertificateVerifier>(client_1_cert_verifier);
4623 webrtc::PeerConnectionDependencies client_2_deps(nullptr);
4624 client_2_deps.tls_cert_verifier =
4625 std::unique_ptr<rtc::TestCertificateVerifier>(client_2_cert_verifier);
4626
4627 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndDeps(
4628 client_1_config, std::move(client_1_deps), client_2_config,
4629 std::move(client_2_deps)));
4630 ConnectFakeSignaling();
4631
4632 // Set "offer to receive audio/video" without adding any tracks, so we just
4633 // set up ICE/DTLS with no media.
4634 PeerConnectionInterface::RTCOfferAnswerOptions options;
4635 options.offer_to_receive_audio = 1;
4636 options.offer_to_receive_video = 1;
4637 caller()->SetOfferAnswerOptions(options);
4638 caller()->CreateAndSetAndSignalOffer();
4639 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
4640
4641 EXPECT_GT(client_1_cert_verifier->call_count_, 0u);
4642 EXPECT_GT(client_2_cert_verifier->call_count_, 0u);
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07004643}
4644
4645TEST_P(PeerConnectionIntegrationTest,
4646 SSLCertificateVerifierFailureUsedForTurnConnectionsFailsConnection) {
4647 static const rtc::SocketAddress turn_server_internal_address{"88.88.88.0",
4648 3478};
4649 static const rtc::SocketAddress turn_server_external_address{"88.88.88.1", 0};
4650
4651 // Enable TCP-TLS for the fake turn server. We need to pass in 88.88.88.0 so
4652 // that host name verification passes on the fake certificate.
Seth Hampsonaed71642018-06-11 07:41:32 -07004653 CreateTurnServer(turn_server_internal_address, turn_server_external_address,
4654 cricket::PROTO_TLS, "88.88.88.0");
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07004655
4656 webrtc::PeerConnectionInterface::IceServer ice_server;
4657 ice_server.urls.push_back("turns:88.88.88.0:3478?transport=tcp");
4658 ice_server.username = "test";
4659 ice_server.password = "test";
4660
4661 PeerConnectionInterface::RTCConfiguration client_1_config;
4662 client_1_config.servers.push_back(ice_server);
4663 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
4664
4665 PeerConnectionInterface::RTCConfiguration client_2_config;
4666 client_2_config.servers.push_back(ice_server);
4667 // Setting the type to kRelay forces the connection to go through a TURN
4668 // server.
4669 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
4670
4671 // Get a copy to the pointer so we can verify calls later.
4672 rtc::TestCertificateVerifier* client_1_cert_verifier =
4673 new rtc::TestCertificateVerifier();
4674 client_1_cert_verifier->verify_certificate_ = false;
4675 rtc::TestCertificateVerifier* client_2_cert_verifier =
4676 new rtc::TestCertificateVerifier();
4677 client_2_cert_verifier->verify_certificate_ = false;
4678
4679 // Create the dependencies with the test certificate verifier.
4680 webrtc::PeerConnectionDependencies client_1_deps(nullptr);
4681 client_1_deps.tls_cert_verifier =
4682 std::unique_ptr<rtc::TestCertificateVerifier>(client_1_cert_verifier);
4683 webrtc::PeerConnectionDependencies client_2_deps(nullptr);
4684 client_2_deps.tls_cert_verifier =
4685 std::unique_ptr<rtc::TestCertificateVerifier>(client_2_cert_verifier);
4686
4687 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndDeps(
4688 client_1_config, std::move(client_1_deps), client_2_config,
4689 std::move(client_2_deps)));
4690 ConnectFakeSignaling();
4691
4692 // Set "offer to receive audio/video" without adding any tracks, so we just
4693 // set up ICE/DTLS with no media.
4694 PeerConnectionInterface::RTCOfferAnswerOptions options;
4695 options.offer_to_receive_audio = 1;
4696 options.offer_to_receive_video = 1;
4697 caller()->SetOfferAnswerOptions(options);
4698 caller()->CreateAndSetAndSignalOffer();
4699 bool wait_res = true;
4700 // TODO(bugs.webrtc.org/9219): When IceConnectionState is implemented
4701 // properly, should be able to just wait for a state of "failed" instead of
4702 // waiting a fixed 10 seconds.
4703 WAIT_(DtlsConnected(), kDefaultTimeout, wait_res);
4704 ASSERT_FALSE(wait_res);
4705
4706 EXPECT_GT(client_1_cert_verifier->call_count_, 0u);
4707 EXPECT_GT(client_2_cert_verifier->call_count_, 0u);
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07004708}
4709
deadbeefc964d0b2017-04-03 10:03:35 -07004710// Test that audio and video flow end-to-end when codec names don't use the
4711// expected casing, given that they're supposed to be case insensitive. To test
4712// this, all but one codec is removed from each media description, and its
4713// casing is changed.
4714//
4715// In the past, this has regressed and caused crashes/black video, due to the
4716// fact that code at some layers was doing case-insensitive comparisons and
4717// code at other layers was not.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004718TEST_P(PeerConnectionIntegrationTest, CodecNamesAreCaseInsensitive) {
deadbeefc964d0b2017-04-03 10:03:35 -07004719 ASSERT_TRUE(CreatePeerConnectionWrappers());
4720 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08004721 caller()->AddAudioVideoTracks();
4722 callee()->AddAudioVideoTracks();
deadbeefc964d0b2017-04-03 10:03:35 -07004723
4724 // Remove all but one audio/video codec (opus and VP8), and change the
4725 // casing of the caller's generated offer.
4726 caller()->SetGeneratedSdpMunger([](cricket::SessionDescription* description) {
4727 cricket::AudioContentDescription* audio =
4728 GetFirstAudioContentDescription(description);
4729 ASSERT_NE(nullptr, audio);
4730 auto audio_codecs = audio->codecs();
4731 audio_codecs.erase(std::remove_if(audio_codecs.begin(), audio_codecs.end(),
4732 [](const cricket::AudioCodec& codec) {
4733 return codec.name != "opus";
4734 }),
4735 audio_codecs.end());
4736 ASSERT_EQ(1u, audio_codecs.size());
4737 audio_codecs[0].name = "OpUs";
4738 audio->set_codecs(audio_codecs);
4739
4740 cricket::VideoContentDescription* video =
4741 GetFirstVideoContentDescription(description);
4742 ASSERT_NE(nullptr, video);
4743 auto video_codecs = video->codecs();
4744 video_codecs.erase(std::remove_if(video_codecs.begin(), video_codecs.end(),
4745 [](const cricket::VideoCodec& codec) {
4746 return codec.name != "VP8";
4747 }),
4748 video_codecs.end());
4749 ASSERT_EQ(1u, video_codecs.size());
4750 video_codecs[0].name = "vP8";
4751 video->set_codecs(video_codecs);
4752 });
4753
4754 caller()->CreateAndSetAndSignalOffer();
4755 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4756
4757 // Verify frames are still received end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004758 MediaExpectations media_expectations;
4759 media_expectations.ExpectBidirectionalAudioAndVideo();
4760 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeefc964d0b2017-04-03 10:03:35 -07004761}
4762
Jonas Oreland49ac5952018-09-26 16:04:32 +02004763TEST_P(PeerConnectionIntegrationTest, GetSourcesAudio) {
hbos8d609f62017-04-10 07:39:05 -07004764 ASSERT_TRUE(CreatePeerConnectionWrappers());
4765 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08004766 caller()->AddAudioTrack();
hbos8d609f62017-04-10 07:39:05 -07004767 caller()->CreateAndSetAndSignalOffer();
4768 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
deadbeefd8ad7882017-04-18 16:01:17 -07004769 // Wait for one audio frame to be received by the callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004770 MediaExpectations media_expectations;
4771 media_expectations.CalleeExpectsSomeAudio(1);
4772 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Jonas Oreland49ac5952018-09-26 16:04:32 +02004773 ASSERT_EQ(callee()->pc()->GetReceivers().size(), 1u);
hbos8d609f62017-04-10 07:39:05 -07004774 auto receiver = callee()->pc()->GetReceivers()[0];
4775 ASSERT_EQ(receiver->media_type(), cricket::MEDIA_TYPE_AUDIO);
Jonas Oreland49ac5952018-09-26 16:04:32 +02004776 auto sources = receiver->GetSources();
hbos8d609f62017-04-10 07:39:05 -07004777 ASSERT_GT(receiver->GetParameters().encodings.size(), 0u);
4778 EXPECT_EQ(receiver->GetParameters().encodings[0].ssrc,
Jonas Oreland49ac5952018-09-26 16:04:32 +02004779 sources[0].source_id());
4780 EXPECT_EQ(webrtc::RtpSourceType::SSRC, sources[0].source_type());
4781}
4782
4783TEST_P(PeerConnectionIntegrationTest, GetSourcesVideo) {
4784 ASSERT_TRUE(CreatePeerConnectionWrappers());
4785 ConnectFakeSignaling();
4786 caller()->AddVideoTrack();
4787 caller()->CreateAndSetAndSignalOffer();
4788 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4789 // Wait for one video frame to be received by the callee.
4790 MediaExpectations media_expectations;
4791 media_expectations.CalleeExpectsSomeVideo(1);
4792 ASSERT_TRUE(ExpectNewFrames(media_expectations));
4793 ASSERT_EQ(callee()->pc()->GetReceivers().size(), 1u);
4794 auto receiver = callee()->pc()->GetReceivers()[0];
4795 ASSERT_EQ(receiver->media_type(), cricket::MEDIA_TYPE_VIDEO);
4796 auto sources = receiver->GetSources();
4797 ASSERT_GT(receiver->GetParameters().encodings.size(), 0u);
4798 EXPECT_EQ(receiver->GetParameters().encodings[0].ssrc,
4799 sources[0].source_id());
4800 EXPECT_EQ(webrtc::RtpSourceType::SSRC, sources[0].source_type());
hbos8d609f62017-04-10 07:39:05 -07004801}
4802
deadbeef2f425aa2017-04-14 10:41:32 -07004803// Test that if a track is removed and added again with a different stream ID,
4804// the new stream ID is successfully communicated in SDP and media continues to
4805// flow end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004806// TODO(webrtc.bugs.org/8734): This test does not work for Unified Plan because
4807// it will not reuse a transceiver that has already been sending. After creating
4808// a new transceiver it tries to create an offer with two senders of the same
4809// track ids and it fails.
4810TEST_F(PeerConnectionIntegrationTestPlanB, RemoveAndAddTrackWithNewStreamId) {
deadbeef2f425aa2017-04-14 10:41:32 -07004811 ASSERT_TRUE(CreatePeerConnectionWrappers());
4812 ConnectFakeSignaling();
4813
deadbeef2f425aa2017-04-14 10:41:32 -07004814 // Add track using stream 1, do offer/answer.
4815 rtc::scoped_refptr<webrtc::AudioTrackInterface> track =
4816 caller()->CreateLocalAudioTrack();
4817 rtc::scoped_refptr<webrtc::RtpSenderInterface> sender =
Steve Antond78323f2018-07-11 11:13:44 -07004818 caller()->AddTrack(track, {"stream_1"});
deadbeef2f425aa2017-04-14 10:41:32 -07004819 caller()->CreateAndSetAndSignalOffer();
4820 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08004821 {
4822 MediaExpectations media_expectations;
4823 media_expectations.CalleeExpectsSomeAudio(1);
4824 ASSERT_TRUE(ExpectNewFrames(media_expectations));
4825 }
deadbeef2f425aa2017-04-14 10:41:32 -07004826 // Remove the sender, and create a new one with the new stream.
4827 caller()->pc()->RemoveTrack(sender);
Steve Antond78323f2018-07-11 11:13:44 -07004828 sender = caller()->AddTrack(track, {"stream_2"});
deadbeef2f425aa2017-04-14 10:41:32 -07004829 caller()->CreateAndSetAndSignalOffer();
4830 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4831 // Wait for additional audio frames to be received by the callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004832 {
4833 MediaExpectations media_expectations;
4834 media_expectations.CalleeExpectsSomeAudio();
4835 ASSERT_TRUE(ExpectNewFrames(media_expectations));
4836 }
deadbeef2f425aa2017-04-14 10:41:32 -07004837}
4838
Seth Hampson2f0d7022018-02-20 11:54:42 -08004839TEST_P(PeerConnectionIntegrationTest, RtcEventLogOutputWriteCalled) {
Elad Alon99c3fe52017-10-13 16:29:40 +02004840 ASSERT_TRUE(CreatePeerConnectionWrappers());
4841 ConnectFakeSignaling();
4842
Karl Wiberg918f50c2018-07-05 11:40:33 +02004843 auto output = absl::make_unique<testing::NiceMock<MockRtcEventLogOutput>>();
Mirko Bonadei6a489f22019-04-09 15:11:12 +02004844 ON_CALL(*output, IsActive()).WillByDefault(::testing::Return(true));
4845 ON_CALL(*output, Write(::testing::_)).WillByDefault(::testing::Return(true));
Elad Alon99c3fe52017-10-13 16:29:40 +02004846 EXPECT_CALL(*output, Write(::testing::_)).Times(::testing::AtLeast(1));
Bjorn Tereliusde939432017-11-20 17:38:14 +01004847 EXPECT_TRUE(caller()->pc()->StartRtcEventLog(
4848 std::move(output), webrtc::RtcEventLog::kImmediateOutput));
Elad Alon99c3fe52017-10-13 16:29:40 +02004849
Steve Anton15324772018-01-16 10:26:49 -08004850 caller()->AddAudioVideoTracks();
Elad Alon99c3fe52017-10-13 16:29:40 +02004851 caller()->CreateAndSetAndSignalOffer();
4852 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4853}
4854
Steve Antonede9ca52017-10-16 13:04:27 -07004855// Test that if candidates are only signaled by applying full session
4856// descriptions (instead of using AddIceCandidate), the peers can connect to
4857// each other and exchange media.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004858TEST_P(PeerConnectionIntegrationTest, MediaFlowsWhenCandidatesSetOnlyInSdp) {
Steve Antonede9ca52017-10-16 13:04:27 -07004859 ASSERT_TRUE(CreatePeerConnectionWrappers());
4860 // Each side will signal the session descriptions but not candidates.
4861 ConnectFakeSignalingForSdpOnly();
4862
4863 // Add audio video track and exchange the initial offer/answer with media
4864 // information only. This will start ICE gathering on each side.
Steve Anton15324772018-01-16 10:26:49 -08004865 caller()->AddAudioVideoTracks();
4866 callee()->AddAudioVideoTracks();
Steve Antonede9ca52017-10-16 13:04:27 -07004867 caller()->CreateAndSetAndSignalOffer();
4868
4869 // Wait for all candidates to be gathered on both the caller and callee.
4870 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceGatheringComplete,
4871 caller()->ice_gathering_state(), kDefaultTimeout);
4872 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceGatheringComplete,
4873 callee()->ice_gathering_state(), kDefaultTimeout);
4874
4875 // The candidates will now be included in the session description, so
4876 // signaling them will start the ICE connection.
4877 caller()->CreateAndSetAndSignalOffer();
4878 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4879
4880 // Ensure that media flows in both directions.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004881 MediaExpectations media_expectations;
4882 media_expectations.ExpectBidirectionalAudioAndVideo();
4883 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Antonede9ca52017-10-16 13:04:27 -07004884}
4885
henrika5f6bf242017-11-01 11:06:56 +01004886// Test that SetAudioPlayout can be used to disable audio playout from the
4887// start, then later enable it. This may be useful, for example, if the caller
4888// needs to play a local ringtone until some event occurs, after which it
4889// switches to playing the received audio.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004890TEST_P(PeerConnectionIntegrationTest, DisableAndEnableAudioPlayout) {
henrika5f6bf242017-11-01 11:06:56 +01004891 ASSERT_TRUE(CreatePeerConnectionWrappers());
4892 ConnectFakeSignaling();
4893
4894 // Set up audio-only call where audio playout is disabled on caller's side.
4895 caller()->pc()->SetAudioPlayout(false);
Steve Anton15324772018-01-16 10:26:49 -08004896 caller()->AddAudioTrack();
4897 callee()->AddAudioTrack();
henrika5f6bf242017-11-01 11:06:56 +01004898 caller()->CreateAndSetAndSignalOffer();
4899 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4900
4901 // Pump messages for a second.
4902 WAIT(false, 1000);
4903 // Since audio playout is disabled, the caller shouldn't have received
4904 // anything (at the playout level, at least).
4905 EXPECT_EQ(0, caller()->audio_frames_received());
4906 // As a sanity check, make sure the callee (for which playout isn't disabled)
4907 // did still see frames on its audio level.
4908 ASSERT_GT(callee()->audio_frames_received(), 0);
4909
4910 // Enable playout again, and ensure audio starts flowing.
4911 caller()->pc()->SetAudioPlayout(true);
Seth Hampson2f0d7022018-02-20 11:54:42 -08004912 MediaExpectations media_expectations;
4913 media_expectations.ExpectBidirectionalAudio();
4914 ASSERT_TRUE(ExpectNewFrames(media_expectations));
henrika5f6bf242017-11-01 11:06:56 +01004915}
4916
4917double GetAudioEnergyStat(PeerConnectionWrapper* pc) {
4918 auto report = pc->NewGetStats();
4919 auto track_stats_list =
4920 report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
4921 const webrtc::RTCMediaStreamTrackStats* remote_track_stats = nullptr;
4922 for (const auto* track_stats : track_stats_list) {
4923 if (track_stats->remote_source.is_defined() &&
4924 *track_stats->remote_source) {
4925 remote_track_stats = track_stats;
4926 break;
4927 }
4928 }
4929
4930 if (!remote_track_stats->total_audio_energy.is_defined()) {
4931 return 0.0;
4932 }
4933 return *remote_track_stats->total_audio_energy;
4934}
4935
4936// Test that if audio playout is disabled via the SetAudioPlayout() method, then
4937// incoming audio is still processed and statistics are generated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004938TEST_P(PeerConnectionIntegrationTest,
henrika5f6bf242017-11-01 11:06:56 +01004939 DisableAudioPlayoutStillGeneratesAudioStats) {
4940 ASSERT_TRUE(CreatePeerConnectionWrappers());
4941 ConnectFakeSignaling();
4942
4943 // Set up audio-only call where playout is disabled but audio-processing is
4944 // still active.
Steve Anton15324772018-01-16 10:26:49 -08004945 caller()->AddAudioTrack();
4946 callee()->AddAudioTrack();
henrika5f6bf242017-11-01 11:06:56 +01004947 caller()->pc()->SetAudioPlayout(false);
4948
4949 caller()->CreateAndSetAndSignalOffer();
4950 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4951
4952 // Wait for the callee to receive audio stats.
4953 EXPECT_TRUE_WAIT(GetAudioEnergyStat(caller()) > 0, kMaxWaitForFramesMs);
4954}
4955
henrika4f167df2017-11-01 14:45:55 +01004956// Test that SetAudioRecording can be used to disable audio recording from the
4957// start, then later enable it. This may be useful, for example, if the caller
4958// wants to ensure that no audio resources are active before a certain state
4959// is reached.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004960TEST_P(PeerConnectionIntegrationTest, DisableAndEnableAudioRecording) {
henrika4f167df2017-11-01 14:45:55 +01004961 ASSERT_TRUE(CreatePeerConnectionWrappers());
4962 ConnectFakeSignaling();
4963
4964 // Set up audio-only call where audio recording is disabled on caller's side.
4965 caller()->pc()->SetAudioRecording(false);
Steve Anton15324772018-01-16 10:26:49 -08004966 caller()->AddAudioTrack();
4967 callee()->AddAudioTrack();
henrika4f167df2017-11-01 14:45:55 +01004968 caller()->CreateAndSetAndSignalOffer();
4969 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4970
4971 // Pump messages for a second.
4972 WAIT(false, 1000);
4973 // Since caller has disabled audio recording, the callee shouldn't have
4974 // received anything.
4975 EXPECT_EQ(0, callee()->audio_frames_received());
4976 // As a sanity check, make sure the caller did still see frames on its
4977 // audio level since audio recording is enabled on the calle side.
4978 ASSERT_GT(caller()->audio_frames_received(), 0);
4979
4980 // Enable audio recording again, and ensure audio starts flowing.
4981 caller()->pc()->SetAudioRecording(true);
Seth Hampson2f0d7022018-02-20 11:54:42 -08004982 MediaExpectations media_expectations;
4983 media_expectations.ExpectBidirectionalAudio();
4984 ASSERT_TRUE(ExpectNewFrames(media_expectations));
henrika4f167df2017-11-01 14:45:55 +01004985}
4986
Taylor Brandstetter389a97c2018-01-03 16:26:06 -08004987// Test that after closing PeerConnections, they stop sending any packets (ICE,
4988// DTLS, RTP...).
Seth Hampson2f0d7022018-02-20 11:54:42 -08004989TEST_P(PeerConnectionIntegrationTest, ClosingConnectionStopsPacketFlow) {
Taylor Brandstetter389a97c2018-01-03 16:26:06 -08004990 // Set up audio/video/data, wait for some frames to be received.
4991 ASSERT_TRUE(CreatePeerConnectionWrappers());
4992 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08004993 caller()->AddAudioVideoTracks();
Taylor Brandstetter389a97c2018-01-03 16:26:06 -08004994#ifdef HAVE_SCTP
4995 caller()->CreateDataChannel();
4996#endif
4997 caller()->CreateAndSetAndSignalOffer();
4998 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08004999 MediaExpectations media_expectations;
5000 media_expectations.CalleeExpectsSomeAudioAndVideo();
5001 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Taylor Brandstetter389a97c2018-01-03 16:26:06 -08005002 // Close PeerConnections.
5003 caller()->pc()->Close();
5004 callee()->pc()->Close();
5005 // Pump messages for a second, and ensure no new packets end up sent.
5006 uint32_t sent_packets_a = virtual_socket_server()->sent_packets();
5007 WAIT(false, 1000);
5008 uint32_t sent_packets_b = virtual_socket_server()->sent_packets();
5009 EXPECT_EQ(sent_packets_a, sent_packets_b);
5010}
5011
Steve Anton7eca0932018-03-30 15:18:41 -07005012// Test that transport stats are generated by the RTCStatsCollector for a
5013// connection that only involves data channels. This is a regression test for
5014// crbug.com/826972.
5015#ifdef HAVE_SCTP
5016TEST_P(PeerConnectionIntegrationTest,
5017 TransportStatsReportedForDataChannelOnlyConnection) {
5018 ASSERT_TRUE(CreatePeerConnectionWrappers());
5019 ConnectFakeSignaling();
5020 caller()->CreateDataChannel();
5021
5022 caller()->CreateAndSetAndSignalOffer();
5023 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5024 ASSERT_TRUE_WAIT(callee()->data_channel(), kDefaultTimeout);
5025
5026 auto caller_report = caller()->NewGetStats();
5027 EXPECT_EQ(1u, caller_report->GetStatsOfType<RTCTransportStats>().size());
5028 auto callee_report = callee()->NewGetStats();
5029 EXPECT_EQ(1u, callee_report->GetStatsOfType<RTCTransportStats>().size());
5030}
5031#endif // HAVE_SCTP
5032
Qingsi Wang7685e862018-06-11 20:15:46 -07005033TEST_P(PeerConnectionIntegrationTest,
5034 IceEventsGeneratedAndLoggedInRtcEventLog) {
5035 ASSERT_TRUE(CreatePeerConnectionWrappersWithFakeRtcEventLog());
5036 ConnectFakeSignaling();
5037 PeerConnectionInterface::RTCOfferAnswerOptions options;
5038 options.offer_to_receive_audio = 1;
5039 caller()->SetOfferAnswerOptions(options);
5040 caller()->CreateAndSetAndSignalOffer();
5041 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
5042 ASSERT_NE(nullptr, caller()->event_log_factory());
5043 ASSERT_NE(nullptr, callee()->event_log_factory());
5044 webrtc::FakeRtcEventLog* caller_event_log =
5045 static_cast<webrtc::FakeRtcEventLog*>(
5046 caller()->event_log_factory()->last_log_created());
5047 webrtc::FakeRtcEventLog* callee_event_log =
5048 static_cast<webrtc::FakeRtcEventLog*>(
5049 callee()->event_log_factory()->last_log_created());
5050 ASSERT_NE(nullptr, caller_event_log);
5051 ASSERT_NE(nullptr, callee_event_log);
5052 int caller_ice_config_count = caller_event_log->GetEventCount(
5053 webrtc::RtcEvent::Type::IceCandidatePairConfig);
5054 int caller_ice_event_count = caller_event_log->GetEventCount(
5055 webrtc::RtcEvent::Type::IceCandidatePairEvent);
5056 int callee_ice_config_count = callee_event_log->GetEventCount(
5057 webrtc::RtcEvent::Type::IceCandidatePairConfig);
5058 int callee_ice_event_count = callee_event_log->GetEventCount(
5059 webrtc::RtcEvent::Type::IceCandidatePairEvent);
5060 EXPECT_LT(0, caller_ice_config_count);
5061 EXPECT_LT(0, caller_ice_event_count);
5062 EXPECT_LT(0, callee_ice_config_count);
5063 EXPECT_LT(0, callee_ice_event_count);
5064}
5065
Mirko Bonadeic84f6612019-01-31 12:20:57 +01005066INSTANTIATE_TEST_SUITE_P(PeerConnectionIntegrationTest,
5067 PeerConnectionIntegrationTest,
5068 Values(SdpSemantics::kPlanB,
5069 SdpSemantics::kUnifiedPlan));
Steve Antond3679212018-01-17 17:41:02 -08005070
Steve Anton74255ff2018-01-24 18:32:57 -08005071// Tests that verify interoperability between Plan B and Unified Plan
5072// PeerConnections.
5073class PeerConnectionIntegrationInteropTest
Seth Hampson2f0d7022018-02-20 11:54:42 -08005074 : public PeerConnectionIntegrationBaseTest,
Steve Anton74255ff2018-01-24 18:32:57 -08005075 public ::testing::WithParamInterface<
5076 std::tuple<SdpSemantics, SdpSemantics>> {
5077 protected:
Seth Hampson2f0d7022018-02-20 11:54:42 -08005078 // Setting the SdpSemantics for the base test to kDefault does not matter
5079 // because we specify not to use the test semantics when creating
5080 // PeerConnectionWrappers.
Steve Anton74255ff2018-01-24 18:32:57 -08005081 PeerConnectionIntegrationInteropTest()
Steve Anton3acffc32018-04-12 17:21:03 -07005082 : PeerConnectionIntegrationBaseTest(SdpSemantics::kPlanB),
Seth Hampson2f0d7022018-02-20 11:54:42 -08005083 caller_semantics_(std::get<0>(GetParam())),
Steve Anton74255ff2018-01-24 18:32:57 -08005084 callee_semantics_(std::get<1>(GetParam())) {}
5085
5086 bool CreatePeerConnectionWrappersWithSemantics() {
Steve Anton3acffc32018-04-12 17:21:03 -07005087 return CreatePeerConnectionWrappersWithSdpSemantics(caller_semantics_,
5088 callee_semantics_);
Steve Anton74255ff2018-01-24 18:32:57 -08005089 }
5090
5091 const SdpSemantics caller_semantics_;
5092 const SdpSemantics callee_semantics_;
5093};
5094
5095TEST_P(PeerConnectionIntegrationInteropTest, NoMediaLocalToNoMediaRemote) {
5096 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
5097 ConnectFakeSignaling();
5098
5099 caller()->CreateAndSetAndSignalOffer();
5100 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5101}
5102
5103TEST_P(PeerConnectionIntegrationInteropTest, OneAudioLocalToNoMediaRemote) {
5104 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
5105 ConnectFakeSignaling();
5106 auto audio_sender = caller()->AddAudioTrack();
5107
5108 caller()->CreateAndSetAndSignalOffer();
5109 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5110
5111 // Verify that one audio receiver has been created on the remote and that it
5112 // has the same track ID as the sending track.
5113 auto receivers = callee()->pc()->GetReceivers();
5114 ASSERT_EQ(1u, receivers.size());
5115 EXPECT_EQ(cricket::MEDIA_TYPE_AUDIO, receivers[0]->media_type());
5116 EXPECT_EQ(receivers[0]->track()->id(), audio_sender->track()->id());
5117
Seth Hampson2f0d7022018-02-20 11:54:42 -08005118 MediaExpectations media_expectations;
5119 media_expectations.CalleeExpectsSomeAudio();
5120 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08005121}
5122
5123TEST_P(PeerConnectionIntegrationInteropTest, OneAudioOneVideoToNoMediaRemote) {
5124 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
5125 ConnectFakeSignaling();
5126 auto video_sender = caller()->AddVideoTrack();
5127 auto audio_sender = caller()->AddAudioTrack();
5128
5129 caller()->CreateAndSetAndSignalOffer();
5130 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5131
5132 // Verify that one audio and one video receiver have been created on the
5133 // remote and that they have the same track IDs as the sending tracks.
5134 auto audio_receivers =
5135 callee()->GetReceiversOfType(cricket::MEDIA_TYPE_AUDIO);
5136 ASSERT_EQ(1u, audio_receivers.size());
5137 EXPECT_EQ(audio_receivers[0]->track()->id(), audio_sender->track()->id());
5138 auto video_receivers =
5139 callee()->GetReceiversOfType(cricket::MEDIA_TYPE_VIDEO);
5140 ASSERT_EQ(1u, video_receivers.size());
5141 EXPECT_EQ(video_receivers[0]->track()->id(), video_sender->track()->id());
5142
Seth Hampson2f0d7022018-02-20 11:54:42 -08005143 MediaExpectations media_expectations;
5144 media_expectations.CalleeExpectsSomeAudioAndVideo();
5145 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08005146}
5147
5148TEST_P(PeerConnectionIntegrationInteropTest,
5149 OneAudioOneVideoLocalToOneAudioOneVideoRemote) {
5150 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
5151 ConnectFakeSignaling();
5152 caller()->AddAudioVideoTracks();
5153 callee()->AddAudioVideoTracks();
5154
5155 caller()->CreateAndSetAndSignalOffer();
5156 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5157
Seth Hampson2f0d7022018-02-20 11:54:42 -08005158 MediaExpectations media_expectations;
5159 media_expectations.ExpectBidirectionalAudioAndVideo();
5160 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08005161}
5162
5163TEST_P(PeerConnectionIntegrationInteropTest,
5164 ReverseRolesOneAudioLocalToOneVideoRemote) {
5165 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
5166 ConnectFakeSignaling();
5167 caller()->AddAudioTrack();
5168 callee()->AddVideoTrack();
5169
5170 caller()->CreateAndSetAndSignalOffer();
5171 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5172
5173 // Verify that only the audio track has been negotiated.
5174 EXPECT_EQ(0u, caller()->GetReceiversOfType(cricket::MEDIA_TYPE_VIDEO).size());
5175 // Might also check that the callee's NegotiationNeeded flag is set.
5176
5177 // Reverse roles.
5178 callee()->CreateAndSetAndSignalOffer();
5179 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5180
Seth Hampson2f0d7022018-02-20 11:54:42 -08005181 MediaExpectations media_expectations;
5182 media_expectations.CallerExpectsSomeVideo();
5183 media_expectations.CalleeExpectsSomeAudio();
5184 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08005185}
5186
Mirko Bonadeic84f6612019-01-31 12:20:57 +01005187INSTANTIATE_TEST_SUITE_P(
Steve Antonba42e992018-04-09 14:10:01 -07005188 PeerConnectionIntegrationTest,
5189 PeerConnectionIntegrationInteropTest,
5190 Values(std::make_tuple(SdpSemantics::kPlanB, SdpSemantics::kUnifiedPlan),
5191 std::make_tuple(SdpSemantics::kUnifiedPlan, SdpSemantics::kPlanB)));
5192
5193// Test that if the Unified Plan side offers two video tracks then the Plan B
5194// side will only see the first one and ignore the second.
5195TEST_F(PeerConnectionIntegrationTestPlanB, TwoVideoUnifiedPlanToNoMediaPlanB) {
Steve Anton3acffc32018-04-12 17:21:03 -07005196 ASSERT_TRUE(CreatePeerConnectionWrappersWithSdpSemantics(
5197 SdpSemantics::kUnifiedPlan, SdpSemantics::kPlanB));
Steve Anton74255ff2018-01-24 18:32:57 -08005198 ConnectFakeSignaling();
5199 auto first_sender = caller()->AddVideoTrack();
5200 caller()->AddVideoTrack();
5201
5202 caller()->CreateAndSetAndSignalOffer();
5203 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5204
5205 // Verify that there is only one receiver and it corresponds to the first
5206 // added track.
5207 auto receivers = callee()->pc()->GetReceivers();
5208 ASSERT_EQ(1u, receivers.size());
5209 EXPECT_TRUE(receivers[0]->track()->enabled());
5210 EXPECT_EQ(first_sender->track()->id(), receivers[0]->track()->id());
5211
Seth Hampson2f0d7022018-02-20 11:54:42 -08005212 MediaExpectations media_expectations;
5213 media_expectations.CalleeExpectsSomeVideo();
5214 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08005215}
5216
Steve Anton2bed3972019-01-04 17:04:30 -08005217// Test that if the initial offer tagged BUNDLE section is rejected due to its
5218// associated RtpTransceiver being stopped and another transceiver is added,
5219// then renegotiation causes the callee to receive the new video track without
5220// error.
5221// This is a regression test for bugs.webrtc.org/9954
5222TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
5223 ReOfferWithStoppedBundleTaggedTransceiver) {
5224 RTCConfiguration config;
5225 config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
5226 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
5227 ConnectFakeSignaling();
5228 auto audio_transceiver_or_error =
5229 caller()->pc()->AddTransceiver(caller()->CreateLocalAudioTrack());
5230 ASSERT_TRUE(audio_transceiver_or_error.ok());
5231 auto audio_transceiver = audio_transceiver_or_error.MoveValue();
5232
5233 caller()->CreateAndSetAndSignalOffer();
5234 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5235 {
5236 MediaExpectations media_expectations;
5237 media_expectations.CalleeExpectsSomeAudio();
5238 ASSERT_TRUE(ExpectNewFrames(media_expectations));
5239 }
5240
5241 audio_transceiver->Stop();
5242 caller()->pc()->AddTransceiver(caller()->CreateLocalVideoTrack());
5243
5244 caller()->CreateAndSetAndSignalOffer();
5245 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5246 {
5247 MediaExpectations media_expectations;
5248 media_expectations.CalleeExpectsSomeVideo();
5249 ASSERT_TRUE(ExpectNewFrames(media_expectations));
5250 }
5251}
5252
deadbeef1dcb1642017-03-29 21:08:16 -07005253} // namespace
Mirko Bonadeiab64e8a2018-12-12 12:10:18 +01005254} // namespace webrtc
deadbeef1dcb1642017-03-29 21:08:16 -07005255
5256#endif // if !defined(THREAD_SANITIZER)