blob: c14bb91be793a913c857037e578b0674b3a0f37b [file] [log] [blame]
deadbeef1dcb1642017-03-29 21:08:16 -07001/*
2 * Copyright 2012 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11// Disable for TSan v2, see
12// https://code.google.com/p/webrtc/issues/detail?id=1205 for details.
13#if !defined(THREAD_SANITIZER)
14
15#include <stdio.h>
16
deadbeef1dcb1642017-03-29 21:08:16 -070017#include <functional>
18#include <list>
19#include <map>
20#include <memory>
21#include <utility>
22#include <vector>
23
Steve Anton64b626b2019-01-28 17:25:26 -080024#include "absl/algorithm/container.h"
Qingsi Wang1dac6d82018-12-12 15:28:47 -080025#include "absl/memory/memory.h"
Karl Wiberg1b0eae32017-10-17 14:48:54 +020026#include "api/audio_codecs/builtin_audio_decoder_factory.h"
27#include "api/audio_codecs/builtin_audio_encoder_factory.h"
Steve Anton10542f22019-01-11 09:11:00 -080028#include "api/media_stream_interface.h"
29#include "api/peer_connection_interface.h"
30#include "api/peer_connection_proxy.h"
31#include "api/rtp_receiver_interface.h"
Bjorn Mellem175aa2e2018-11-08 11:23:22 -080032#include "api/test/loopback_media_transport.h"
Steve Anton10542f22019-01-11 09:11:00 -080033#include "api/uma_metrics.h"
Anders Carlsson67537952018-05-03 11:28:29 +020034#include "api/video_codecs/builtin_video_decoder_factory.h"
35#include "api/video_codecs/builtin_video_encoder_factory.h"
36#include "api/video_codecs/sdp_video_format.h"
Qingsi Wang7685e862018-06-11 20:15:46 -070037#include "call/call.h"
38#include "logging/rtc_event_log/fake_rtc_event_log_factory.h"
Bjorn Tereliusb8b3c992019-01-09 11:15:34 +010039#include "logging/rtc_event_log/rtc_event_log_factory.h"
Qingsi Wang7685e862018-06-11 20:15:46 -070040#include "logging/rtc_event_log/rtc_event_log_factory_interface.h"
Steve Anton10542f22019-01-11 09:11:00 -080041#include "media/engine/fake_webrtc_video_engine.h"
42#include "media/engine/webrtc_media_engine.h"
Qingsi Wang7685e862018-06-11 20:15:46 -070043#include "modules/audio_processing/include/audio_processing.h"
Steve Anton10542f22019-01-11 09:11:00 -080044#include "p2p/base/mock_async_resolver.h"
45#include "p2p/base/p2p_constants.h"
46#include "p2p/base/port_interface.h"
47#include "p2p/base/test_stun_server.h"
48#include "p2p/base/test_turn_customizer.h"
49#include "p2p/base/test_turn_server.h"
50#include "p2p/client/basic_port_allocator.h"
51#include "pc/dtmf_sender.h"
52#include "pc/local_audio_source.h"
53#include "pc/media_session.h"
54#include "pc/peer_connection.h"
55#include "pc/peer_connection_factory.h"
56#include "pc/rtp_media_utils.h"
57#include "pc/session_description.h"
58#include "pc/test/fake_audio_capture_module.h"
59#include "pc/test/fake_periodic_video_track_source.h"
60#include "pc/test/fake_rtc_certificate_generator.h"
61#include "pc/test/fake_video_track_renderer.h"
62#include "pc/test/mock_peer_connection_observers.h"
Jonas Olssonb75d9e92019-02-22 10:33:29 +010063#include "rtc_base/fake_clock.h"
Steve Anton10542f22019-01-11 09:11:00 -080064#include "rtc_base/fake_network.h"
65#include "rtc_base/firewall_socket_server.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020066#include "rtc_base/gunit.h"
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +020067#include "rtc_base/numerics/safe_conversions.h"
Steve Anton10542f22019-01-11 09:11:00 -080068#include "rtc_base/test_certificate_verifier.h"
69#include "rtc_base/time_utils.h"
70#include "rtc_base/virtual_socket_server.h"
Mirko Bonadei17f48782018-09-28 08:51:10 +020071#include "system_wrappers/include/metrics.h"
Elad Alon99c3fe52017-10-13 16:29:40 +020072#include "test/gmock.h"
deadbeef1dcb1642017-03-29 21:08:16 -070073
Mirko Bonadeiab64e8a2018-12-12 12:10:18 +010074namespace webrtc {
75namespace {
76
77using ::cricket::ContentInfo;
78using ::cricket::StreamParams;
79using ::rtc::SocketAddress;
80using ::testing::_;
Seth Hampson2f0d7022018-02-20 11:54:42 -080081using ::testing::Combine;
Steve Anton64b626b2019-01-28 17:25:26 -080082using ::testing::Contains;
Steve Antonede9ca52017-10-16 13:04:27 -070083using ::testing::ElementsAre;
Qingsi Wang1dac6d82018-12-12 15:28:47 -080084using ::testing::NiceMock;
Steve Anton64b626b2019-01-28 17:25:26 -080085using ::testing::Return;
Zach Stein6fcdc2f2018-08-23 16:25:55 -070086using ::testing::SetArgPointee;
Steve Antonffa6ce42018-11-30 09:26:08 -080087using ::testing::UnorderedElementsAreArray;
Mirko Bonadeiab64e8a2018-12-12 12:10:18 +010088using ::testing::Values;
Steve Anton74255ff2018-01-24 18:32:57 -080089using RTCConfiguration = PeerConnectionInterface::RTCConfiguration;
deadbeef1dcb1642017-03-29 21:08:16 -070090
91static const int kDefaultTimeout = 10000;
92static const int kMaxWaitForStatsMs = 3000;
93static const int kMaxWaitForActivationMs = 5000;
94static const int kMaxWaitForFramesMs = 10000;
95// Default number of audio/video frames to wait for before considering a test
96// successful.
97static const int kDefaultExpectedAudioFrameCount = 3;
98static const int kDefaultExpectedVideoFrameCount = 3;
99
deadbeef1dcb1642017-03-29 21:08:16 -0700100static const char kDataChannelLabel[] = "data_channel";
101
102// SRTP cipher name negotiated by the tests. This must be updated if the
103// default changes.
Taylor Brandstetterfd350d72018-04-03 16:29:26 -0700104static const int kDefaultSrtpCryptoSuite = rtc::SRTP_AES128_CM_SHA1_80;
deadbeef1dcb1642017-03-29 21:08:16 -0700105static const int kDefaultSrtpCryptoSuiteGcm = rtc::SRTP_AEAD_AES_256_GCM;
106
Steve Antonede9ca52017-10-16 13:04:27 -0700107static const SocketAddress kDefaultLocalAddress("192.168.1.1", 0);
108
deadbeef1dcb1642017-03-29 21:08:16 -0700109// Helper function for constructing offer/answer options to initiate an ICE
110// restart.
111PeerConnectionInterface::RTCOfferAnswerOptions IceRestartOfferAnswerOptions() {
112 PeerConnectionInterface::RTCOfferAnswerOptions options;
113 options.ice_restart = true;
114 return options;
115}
116
deadbeefd8ad7882017-04-18 16:01:17 -0700117// Remove all stream information (SSRCs, track IDs, etc.) and "msid-semantic"
118// attribute from received SDP, simulating a legacy endpoint.
119void RemoveSsrcsAndMsids(cricket::SessionDescription* desc) {
120 for (ContentInfo& content : desc->contents()) {
Steve Antonb1c1de12017-12-21 15:14:30 -0800121 content.media_description()->mutable_streams().clear();
deadbeefd8ad7882017-04-18 16:01:17 -0700122 }
123 desc->set_msid_supported(false);
Henrik Boström5b147782018-12-04 11:25:05 +0100124 desc->set_msid_signaling(0);
deadbeefd8ad7882017-04-18 16:01:17 -0700125}
126
Seth Hampson5897a6e2018-04-03 11:16:33 -0700127// Removes all stream information besides the stream ids, simulating an
128// endpoint that only signals a=msid lines to convey stream_ids.
129void RemoveSsrcsAndKeepMsids(cricket::SessionDescription* desc) {
130 for (ContentInfo& content : desc->contents()) {
Steve Antondf527fd2018-04-27 15:52:03 -0700131 std::string track_id;
Seth Hampson5897a6e2018-04-03 11:16:33 -0700132 std::vector<std::string> stream_ids;
133 if (!content.media_description()->streams().empty()) {
Steve Antondf527fd2018-04-27 15:52:03 -0700134 const StreamParams& first_stream =
135 content.media_description()->streams()[0];
136 track_id = first_stream.id;
137 stream_ids = first_stream.stream_ids();
Seth Hampson5897a6e2018-04-03 11:16:33 -0700138 }
139 content.media_description()->mutable_streams().clear();
Steve Antondf527fd2018-04-27 15:52:03 -0700140 StreamParams new_stream;
141 new_stream.id = track_id;
Seth Hampson5897a6e2018-04-03 11:16:33 -0700142 new_stream.set_stream_ids(stream_ids);
143 content.media_description()->AddStream(new_stream);
144 }
145}
146
zhihuangf8164932017-05-19 13:09:47 -0700147int FindFirstMediaStatsIndexByKind(
148 const std::string& kind,
149 const std::vector<const webrtc::RTCMediaStreamTrackStats*>&
150 media_stats_vec) {
151 for (size_t i = 0; i < media_stats_vec.size(); i++) {
152 if (media_stats_vec[i]->kind.ValueToString() == kind) {
153 return i;
154 }
155 }
156 return -1;
157}
158
deadbeef1dcb1642017-03-29 21:08:16 -0700159class SignalingMessageReceiver {
160 public:
Steve Antona3a92c22017-12-07 10:27:41 -0800161 virtual void ReceiveSdpMessage(SdpType type, const std::string& msg) = 0;
deadbeef1dcb1642017-03-29 21:08:16 -0700162 virtual void ReceiveIceMessage(const std::string& sdp_mid,
163 int sdp_mline_index,
164 const std::string& msg) = 0;
165
166 protected:
167 SignalingMessageReceiver() {}
168 virtual ~SignalingMessageReceiver() {}
169};
170
171class MockRtpReceiverObserver : public webrtc::RtpReceiverObserverInterface {
172 public:
173 explicit MockRtpReceiverObserver(cricket::MediaType media_type)
174 : expected_media_type_(media_type) {}
175
176 void OnFirstPacketReceived(cricket::MediaType media_type) override {
177 ASSERT_EQ(expected_media_type_, media_type);
178 first_packet_received_ = true;
179 }
180
181 bool first_packet_received() const { return first_packet_received_; }
182
183 virtual ~MockRtpReceiverObserver() {}
184
185 private:
186 bool first_packet_received_ = false;
187 cricket::MediaType expected_media_type_;
188};
189
190// Helper class that wraps a peer connection, observes it, and can accept
191// signaling messages from another wrapper.
192//
193// Uses a fake network, fake A/V capture, and optionally fake
194// encoders/decoders, though they aren't used by default since they don't
195// advertise support of any codecs.
Steve Anton94286cb2017-09-26 16:20:19 -0700196// TODO(steveanton): See how this could become a subclass of
Seth Hampson2f0d7022018-02-20 11:54:42 -0800197// PeerConnectionWrapper defined in peerconnectionwrapper.h.
deadbeef1dcb1642017-03-29 21:08:16 -0700198class PeerConnectionWrapper : public webrtc::PeerConnectionObserver,
Steve Anton15324772018-01-16 10:26:49 -0800199 public SignalingMessageReceiver {
deadbeef1dcb1642017-03-29 21:08:16 -0700200 public:
201 // Different factory methods for convenience.
202 // TODO(deadbeef): Could use the pattern of:
203 //
204 // PeerConnectionWrapper =
205 // WrapperBuilder.WithConfig(...).WithOptions(...).build();
206 //
207 // To reduce some code duplication.
208 static PeerConnectionWrapper* CreateWithDtlsIdentityStore(
209 const std::string& debug_name,
210 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator,
211 rtc::Thread* network_thread,
212 rtc::Thread* worker_thread) {
213 PeerConnectionWrapper* client(new PeerConnectionWrapper(debug_name));
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700214 webrtc::PeerConnectionDependencies dependencies(nullptr);
215 dependencies.cert_generator = std::move(cert_generator);
Niels Möllerf06f9232018-08-07 12:32:18 +0200216 if (!client->Init(nullptr, nullptr, std::move(dependencies), network_thread,
Bjorn Mellem175aa2e2018-11-08 11:23:22 -0800217 worker_thread, nullptr,
218 /*media_transport_factory=*/nullptr)) {
deadbeef1dcb1642017-03-29 21:08:16 -0700219 delete client;
220 return nullptr;
221 }
222 return client;
223 }
224
deadbeef2f425aa2017-04-14 10:41:32 -0700225 webrtc::PeerConnectionFactoryInterface* pc_factory() const {
226 return peer_connection_factory_.get();
227 }
228
deadbeef1dcb1642017-03-29 21:08:16 -0700229 webrtc::PeerConnectionInterface* pc() const { return peer_connection_.get(); }
230
231 // If a signaling message receiver is set (via ConnectFakeSignaling), this
232 // will set the whole offer/answer exchange in motion. Just need to wait for
233 // the signaling state to reach "stable".
234 void CreateAndSetAndSignalOffer() {
235 auto offer = CreateOffer();
236 ASSERT_NE(nullptr, offer);
237 EXPECT_TRUE(SetLocalDescriptionAndSendSdpMessage(std::move(offer)));
238 }
239
240 // Sets the options to be used when CreateAndSetAndSignalOffer is called, or
241 // when a remote offer is received (via fake signaling) and an answer is
242 // generated. By default, uses default options.
243 void SetOfferAnswerOptions(
244 const PeerConnectionInterface::RTCOfferAnswerOptions& options) {
245 offer_answer_options_ = options;
246 }
247
248 // Set a callback to be invoked when SDP is received via the fake signaling
249 // channel, which provides an opportunity to munge (modify) the SDP. This is
250 // used to test SDP being applied that a PeerConnection would normally not
251 // generate, but a non-JSEP endpoint might.
252 void SetReceivedSdpMunger(
253 std::function<void(cricket::SessionDescription*)> munger) {
Mirko Bonadeic61ce0d2017-11-21 17:04:20 +0100254 received_sdp_munger_ = std::move(munger);
deadbeef1dcb1642017-03-29 21:08:16 -0700255 }
256
deadbeefc964d0b2017-04-03 10:03:35 -0700257 // Similar to the above, but this is run on SDP immediately after it's
deadbeef1dcb1642017-03-29 21:08:16 -0700258 // generated.
259 void SetGeneratedSdpMunger(
260 std::function<void(cricket::SessionDescription*)> munger) {
Mirko Bonadeic61ce0d2017-11-21 17:04:20 +0100261 generated_sdp_munger_ = std::move(munger);
deadbeef1dcb1642017-03-29 21:08:16 -0700262 }
263
Seth Hampson2f0d7022018-02-20 11:54:42 -0800264 // Set a callback to be invoked when a remote offer is received via the fake
265 // signaling channel. This provides an opportunity to change the
266 // PeerConnection state before an answer is created and sent to the caller.
267 void SetRemoteOfferHandler(std::function<void()> handler) {
268 remote_offer_handler_ = std::move(handler);
269 }
270
Qingsi Wang1dac6d82018-12-12 15:28:47 -0800271 void SetRemoteAsyncResolver(rtc::MockAsyncResolver* resolver) {
272 remote_async_resolver_ = resolver;
Zach Stein6fcdc2f2018-08-23 16:25:55 -0700273 }
274
Steve Antonede9ca52017-10-16 13:04:27 -0700275 // Every ICE connection state in order that has been seen by the observer.
276 std::vector<PeerConnectionInterface::IceConnectionState>
277 ice_connection_state_history() const {
278 return ice_connection_state_history_;
279 }
Steve Anton6f25b092017-10-23 09:39:20 -0700280 void clear_ice_connection_state_history() {
281 ice_connection_state_history_.clear();
282 }
Steve Antonede9ca52017-10-16 13:04:27 -0700283
Jonas Olssonacd8ae72019-02-25 15:26:24 +0100284 // Every standardized ICE connection state in order that has been seen by the
285 // observer.
286 std::vector<PeerConnectionInterface::IceConnectionState>
287 standardized_ice_connection_state_history() const {
288 return standardized_ice_connection_state_history_;
289 }
290
Jonas Olsson635474e2018-10-18 15:58:17 +0200291 // Every PeerConnection state in order that has been seen by the observer.
292 std::vector<PeerConnectionInterface::PeerConnectionState>
293 peer_connection_state_history() const {
294 return peer_connection_state_history_;
295 }
296
Steve Antonede9ca52017-10-16 13:04:27 -0700297 // Every ICE gathering state in order that has been seen by the observer.
298 std::vector<PeerConnectionInterface::IceGatheringState>
299 ice_gathering_state_history() const {
300 return ice_gathering_state_history_;
deadbeef1dcb1642017-03-29 21:08:16 -0700301 }
302
Steve Anton15324772018-01-16 10:26:49 -0800303 void AddAudioVideoTracks() {
304 AddAudioTrack();
305 AddVideoTrack();
deadbeef1dcb1642017-03-29 21:08:16 -0700306 }
307
Steve Anton74255ff2018-01-24 18:32:57 -0800308 rtc::scoped_refptr<RtpSenderInterface> AddAudioTrack() {
309 return AddTrack(CreateLocalAudioTrack());
310 }
deadbeef1dcb1642017-03-29 21:08:16 -0700311
Steve Anton74255ff2018-01-24 18:32:57 -0800312 rtc::scoped_refptr<RtpSenderInterface> AddVideoTrack() {
313 return AddTrack(CreateLocalVideoTrack());
314 }
deadbeef1dcb1642017-03-29 21:08:16 -0700315
316 rtc::scoped_refptr<webrtc::AudioTrackInterface> CreateLocalAudioTrack() {
Niels Möller2d02e082018-05-21 11:23:35 +0200317 cricket::AudioOptions options;
deadbeef1dcb1642017-03-29 21:08:16 -0700318 // Disable highpass filter so that we can get all the test audio frames.
Niels Möller2d02e082018-05-21 11:23:35 +0200319 options.highpass_filter = false;
deadbeef1dcb1642017-03-29 21:08:16 -0700320 rtc::scoped_refptr<webrtc::AudioSourceInterface> source =
Niels Möller2d02e082018-05-21 11:23:35 +0200321 peer_connection_factory_->CreateAudioSource(options);
deadbeef1dcb1642017-03-29 21:08:16 -0700322 // TODO(perkj): Test audio source when it is implemented. Currently audio
323 // always use the default input.
deadbeefb1a15d72017-09-07 14:12:05 -0700324 return peer_connection_factory_->CreateAudioTrack(rtc::CreateRandomUuid(),
deadbeef1dcb1642017-03-29 21:08:16 -0700325 source);
326 }
327
328 rtc::scoped_refptr<webrtc::VideoTrackInterface> CreateLocalVideoTrack() {
Johannes Kron965e7942018-09-13 15:36:20 +0200329 webrtc::FakePeriodicVideoSource::Config config;
330 config.timestamp_offset_ms = rtc::TimeMillis();
331 return CreateLocalVideoTrackInternal(config);
deadbeef1dcb1642017-03-29 21:08:16 -0700332 }
333
334 rtc::scoped_refptr<webrtc::VideoTrackInterface>
Niels Möller5c7efe72018-05-11 10:34:46 +0200335 CreateLocalVideoTrackWithConfig(
336 webrtc::FakePeriodicVideoSource::Config config) {
337 return CreateLocalVideoTrackInternal(config);
deadbeef1dcb1642017-03-29 21:08:16 -0700338 }
339
340 rtc::scoped_refptr<webrtc::VideoTrackInterface>
341 CreateLocalVideoTrackWithRotation(webrtc::VideoRotation rotation) {
Niels Möller5c7efe72018-05-11 10:34:46 +0200342 webrtc::FakePeriodicVideoSource::Config config;
343 config.rotation = rotation;
Johannes Kron965e7942018-09-13 15:36:20 +0200344 config.timestamp_offset_ms = rtc::TimeMillis();
Niels Möller5c7efe72018-05-11 10:34:46 +0200345 return CreateLocalVideoTrackInternal(config);
deadbeef1dcb1642017-03-29 21:08:16 -0700346 }
347
Steve Anton74255ff2018-01-24 18:32:57 -0800348 rtc::scoped_refptr<RtpSenderInterface> AddTrack(
349 rtc::scoped_refptr<MediaStreamTrackInterface> track,
Seth Hampson845e8782018-03-02 11:34:10 -0800350 const std::vector<std::string>& stream_ids = {}) {
351 auto result = pc()->AddTrack(track, stream_ids);
Steve Anton15324772018-01-16 10:26:49 -0800352 EXPECT_EQ(RTCErrorType::NONE, result.error().type());
Steve Anton74255ff2018-01-24 18:32:57 -0800353 return result.MoveValue();
354 }
355
356 std::vector<rtc::scoped_refptr<RtpReceiverInterface>> GetReceiversOfType(
357 cricket::MediaType media_type) {
358 std::vector<rtc::scoped_refptr<RtpReceiverInterface>> receivers;
Mirko Bonadei739baf02019-01-27 17:29:42 +0100359 for (const auto& receiver : pc()->GetReceivers()) {
Steve Anton74255ff2018-01-24 18:32:57 -0800360 if (receiver->media_type() == media_type) {
361 receivers.push_back(receiver);
362 }
363 }
364 return receivers;
deadbeef1dcb1642017-03-29 21:08:16 -0700365 }
366
Seth Hampson2f0d7022018-02-20 11:54:42 -0800367 rtc::scoped_refptr<RtpTransceiverInterface> GetFirstTransceiverOfType(
368 cricket::MediaType media_type) {
369 for (auto transceiver : pc()->GetTransceivers()) {
370 if (transceiver->receiver()->media_type() == media_type) {
371 return transceiver;
372 }
373 }
374 return nullptr;
375 }
376
deadbeef1dcb1642017-03-29 21:08:16 -0700377 bool SignalingStateStable() {
378 return pc()->signaling_state() == webrtc::PeerConnectionInterface::kStable;
379 }
380
381 void CreateDataChannel() { CreateDataChannel(nullptr); }
382
383 void CreateDataChannel(const webrtc::DataChannelInit* init) {
Steve Antonda6c0952017-10-23 11:41:54 -0700384 CreateDataChannel(kDataChannelLabel, init);
385 }
386
387 void CreateDataChannel(const std::string& label,
388 const webrtc::DataChannelInit* init) {
389 data_channel_ = pc()->CreateDataChannel(label, init);
deadbeef1dcb1642017-03-29 21:08:16 -0700390 ASSERT_TRUE(data_channel_.get() != nullptr);
391 data_observer_.reset(new MockDataChannelObserver(data_channel_));
392 }
393
394 DataChannelInterface* data_channel() { return data_channel_; }
395 const MockDataChannelObserver* data_observer() const {
396 return data_observer_.get();
397 }
398
399 int audio_frames_received() const {
400 return fake_audio_capture_module_->frames_received();
401 }
402
403 // Takes minimum of video frames received for each track.
404 //
405 // Can be used like:
406 // EXPECT_GE(expected_frames, min_video_frames_received_per_track());
407 //
408 // To ensure that all video tracks received at least a certain number of
409 // frames.
410 int min_video_frames_received_per_track() const {
411 int min_frames = INT_MAX;
Anders Carlsson5f2bb622018-05-14 09:48:06 +0200412 if (fake_video_renderers_.empty()) {
413 return 0;
deadbeef1dcb1642017-03-29 21:08:16 -0700414 }
deadbeef1dcb1642017-03-29 21:08:16 -0700415
Anders Carlsson5f2bb622018-05-14 09:48:06 +0200416 for (const auto& pair : fake_video_renderers_) {
417 min_frames = std::min(min_frames, pair.second->num_rendered_frames());
deadbeef1dcb1642017-03-29 21:08:16 -0700418 }
Anders Carlsson5f2bb622018-05-14 09:48:06 +0200419 return min_frames;
deadbeef1dcb1642017-03-29 21:08:16 -0700420 }
421
422 // Returns a MockStatsObserver in a state after stats gathering finished,
423 // which can be used to access the gathered stats.
deadbeefd8ad7882017-04-18 16:01:17 -0700424 rtc::scoped_refptr<MockStatsObserver> OldGetStatsForTrack(
deadbeef1dcb1642017-03-29 21:08:16 -0700425 webrtc::MediaStreamTrackInterface* track) {
426 rtc::scoped_refptr<MockStatsObserver> observer(
427 new rtc::RefCountedObject<MockStatsObserver>());
428 EXPECT_TRUE(peer_connection_->GetStats(
429 observer, nullptr, PeerConnectionInterface::kStatsOutputLevelStandard));
430 EXPECT_TRUE_WAIT(observer->called(), kDefaultTimeout);
431 return observer;
432 }
433
434 // Version that doesn't take a track "filter", and gathers all stats.
deadbeefd8ad7882017-04-18 16:01:17 -0700435 rtc::scoped_refptr<MockStatsObserver> OldGetStats() {
436 return OldGetStatsForTrack(nullptr);
437 }
438
439 // Synchronously gets stats and returns them. If it times out, fails the test
440 // and returns null.
441 rtc::scoped_refptr<const webrtc::RTCStatsReport> NewGetStats() {
442 rtc::scoped_refptr<webrtc::MockRTCStatsCollectorCallback> callback(
443 new rtc::RefCountedObject<webrtc::MockRTCStatsCollectorCallback>());
444 peer_connection_->GetStats(callback);
445 EXPECT_TRUE_WAIT(callback->called(), kDefaultTimeout);
446 return callback->report();
deadbeef1dcb1642017-03-29 21:08:16 -0700447 }
448
449 int rendered_width() {
450 EXPECT_FALSE(fake_video_renderers_.empty());
451 return fake_video_renderers_.empty()
452 ? 0
453 : fake_video_renderers_.begin()->second->width();
454 }
455
456 int rendered_height() {
457 EXPECT_FALSE(fake_video_renderers_.empty());
458 return fake_video_renderers_.empty()
459 ? 0
460 : fake_video_renderers_.begin()->second->height();
461 }
462
463 double rendered_aspect_ratio() {
464 if (rendered_height() == 0) {
465 return 0.0;
466 }
467 return static_cast<double>(rendered_width()) / rendered_height();
468 }
469
470 webrtc::VideoRotation rendered_rotation() {
471 EXPECT_FALSE(fake_video_renderers_.empty());
472 return fake_video_renderers_.empty()
473 ? webrtc::kVideoRotation_0
474 : fake_video_renderers_.begin()->second->rotation();
475 }
476
477 int local_rendered_width() {
478 return local_video_renderer_ ? local_video_renderer_->width() : 0;
479 }
480
481 int local_rendered_height() {
482 return local_video_renderer_ ? local_video_renderer_->height() : 0;
483 }
484
485 double local_rendered_aspect_ratio() {
486 if (local_rendered_height() == 0) {
487 return 0.0;
488 }
489 return static_cast<double>(local_rendered_width()) /
490 local_rendered_height();
491 }
492
493 size_t number_of_remote_streams() {
494 if (!pc()) {
495 return 0;
496 }
497 return pc()->remote_streams()->count();
498 }
499
500 StreamCollectionInterface* remote_streams() const {
501 if (!pc()) {
502 ADD_FAILURE();
503 return nullptr;
504 }
505 return pc()->remote_streams();
506 }
507
508 StreamCollectionInterface* local_streams() {
509 if (!pc()) {
510 ADD_FAILURE();
511 return nullptr;
512 }
513 return pc()->local_streams();
514 }
515
516 webrtc::PeerConnectionInterface::SignalingState signaling_state() {
517 return pc()->signaling_state();
518 }
519
520 webrtc::PeerConnectionInterface::IceConnectionState ice_connection_state() {
521 return pc()->ice_connection_state();
522 }
523
Jonas Olsson7a6739e2019-01-15 16:31:55 +0100524 webrtc::PeerConnectionInterface::IceConnectionState
525 standardized_ice_connection_state() {
526 return pc()->standardized_ice_connection_state();
527 }
528
deadbeef1dcb1642017-03-29 21:08:16 -0700529 webrtc::PeerConnectionInterface::IceGatheringState ice_gathering_state() {
530 return pc()->ice_gathering_state();
531 }
532
533 // Returns a MockRtpReceiverObserver for each RtpReceiver returned by
534 // GetReceivers. They're updated automatically when a remote offer/answer
535 // from the fake signaling channel is applied, or when
536 // ResetRtpReceiverObservers below is called.
537 const std::vector<std::unique_ptr<MockRtpReceiverObserver>>&
538 rtp_receiver_observers() {
539 return rtp_receiver_observers_;
540 }
541
542 void ResetRtpReceiverObservers() {
543 rtp_receiver_observers_.clear();
Mirko Bonadeic61ce0d2017-11-21 17:04:20 +0100544 for (const rtc::scoped_refptr<RtpReceiverInterface>& receiver :
545 pc()->GetReceivers()) {
deadbeef1dcb1642017-03-29 21:08:16 -0700546 std::unique_ptr<MockRtpReceiverObserver> observer(
547 new MockRtpReceiverObserver(receiver->media_type()));
548 receiver->SetObserver(observer.get());
549 rtp_receiver_observers_.push_back(std::move(observer));
550 }
551 }
552
Steve Antonede9ca52017-10-16 13:04:27 -0700553 rtc::FakeNetworkManager* network() const {
554 return fake_network_manager_.get();
555 }
556 cricket::PortAllocator* port_allocator() const { return port_allocator_; }
557
Qingsi Wang7685e862018-06-11 20:15:46 -0700558 webrtc::FakeRtcEventLogFactory* event_log_factory() const {
559 return event_log_factory_;
560 }
561
deadbeef1dcb1642017-03-29 21:08:16 -0700562 private:
563 explicit PeerConnectionWrapper(const std::string& debug_name)
564 : debug_name_(debug_name) {}
565
Bjorn Mellem175aa2e2018-11-08 11:23:22 -0800566 bool Init(
567 const PeerConnectionFactory::Options* options,
568 const PeerConnectionInterface::RTCConfiguration* config,
569 webrtc::PeerConnectionDependencies dependencies,
570 rtc::Thread* network_thread,
571 rtc::Thread* worker_thread,
572 std::unique_ptr<webrtc::FakeRtcEventLogFactory> event_log_factory,
573 std::unique_ptr<webrtc::MediaTransportFactory> media_transport_factory) {
deadbeef1dcb1642017-03-29 21:08:16 -0700574 // There's an error in this test code if Init ends up being called twice.
575 RTC_DCHECK(!peer_connection_);
576 RTC_DCHECK(!peer_connection_factory_);
577
578 fake_network_manager_.reset(new rtc::FakeNetworkManager());
Steve Antonede9ca52017-10-16 13:04:27 -0700579 fake_network_manager_->AddInterface(kDefaultLocalAddress);
deadbeef1dcb1642017-03-29 21:08:16 -0700580
581 std::unique_ptr<cricket::PortAllocator> port_allocator(
582 new cricket::BasicPortAllocator(fake_network_manager_.get()));
Steve Antonede9ca52017-10-16 13:04:27 -0700583 port_allocator_ = port_allocator.get();
deadbeef1dcb1642017-03-29 21:08:16 -0700584 fake_audio_capture_module_ = FakeAudioCaptureModule::Create();
585 if (!fake_audio_capture_module_) {
586 return false;
587 }
deadbeef1dcb1642017-03-29 21:08:16 -0700588 rtc::Thread* const signaling_thread = rtc::Thread::Current();
Qingsi Wang7685e862018-06-11 20:15:46 -0700589
590 webrtc::PeerConnectionFactoryDependencies pc_factory_dependencies;
591 pc_factory_dependencies.network_thread = network_thread;
592 pc_factory_dependencies.worker_thread = worker_thread;
593 pc_factory_dependencies.signaling_thread = signaling_thread;
594 pc_factory_dependencies.media_engine =
595 cricket::WebRtcMediaEngineFactory::Create(
596 rtc::scoped_refptr<webrtc::AudioDeviceModule>(
597 fake_audio_capture_module_),
598 webrtc::CreateBuiltinAudioEncoderFactory(),
599 webrtc::CreateBuiltinAudioDecoderFactory(),
600 webrtc::CreateBuiltinVideoEncoderFactory(),
Qingsi Wang59844ce2018-11-01 04:45:53 +0000601 webrtc::CreateBuiltinVideoDecoderFactory(), nullptr,
Qingsi Wang7685e862018-06-11 20:15:46 -0700602 webrtc::AudioProcessingBuilder().Create());
603 pc_factory_dependencies.call_factory = webrtc::CreateCallFactory();
604 if (event_log_factory) {
605 event_log_factory_ = event_log_factory.get();
606 pc_factory_dependencies.event_log_factory = std::move(event_log_factory);
607 } else {
608 pc_factory_dependencies.event_log_factory =
609 webrtc::CreateRtcEventLogFactory();
610 }
Bjorn Mellem175aa2e2018-11-08 11:23:22 -0800611 if (media_transport_factory) {
612 pc_factory_dependencies.media_transport_factory =
613 std::move(media_transport_factory);
614 }
Qingsi Wang7685e862018-06-11 20:15:46 -0700615 peer_connection_factory_ = webrtc::CreateModularPeerConnectionFactory(
616 std::move(pc_factory_dependencies));
617
deadbeef1dcb1642017-03-29 21:08:16 -0700618 if (!peer_connection_factory_) {
619 return false;
620 }
621 if (options) {
622 peer_connection_factory_->SetOptions(*options);
623 }
Seth Hampson2f0d7022018-02-20 11:54:42 -0800624 if (config) {
625 sdp_semantics_ = config->sdp_semantics;
626 }
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700627
628 dependencies.allocator = std::move(port_allocator);
Niels Möllerf06f9232018-08-07 12:32:18 +0200629 peer_connection_ = CreatePeerConnection(config, std::move(dependencies));
deadbeef1dcb1642017-03-29 21:08:16 -0700630 return peer_connection_.get() != nullptr;
631 }
632
633 rtc::scoped_refptr<webrtc::PeerConnectionInterface> CreatePeerConnection(
deadbeef1dcb1642017-03-29 21:08:16 -0700634 const PeerConnectionInterface::RTCConfiguration* config,
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700635 webrtc::PeerConnectionDependencies dependencies) {
deadbeef1dcb1642017-03-29 21:08:16 -0700636 PeerConnectionInterface::RTCConfiguration modified_config;
637 // If |config| is null, this will result in a default configuration being
638 // used.
639 if (config) {
640 modified_config = *config;
641 }
642 // Disable resolution adaptation; we don't want it interfering with the
643 // test results.
644 // TODO(deadbeef): Do something more robust. Since we're testing for aspect
645 // ratios and not specific resolutions, is this even necessary?
646 modified_config.set_cpu_adaptation(false);
647
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700648 dependencies.observer = this;
deadbeef1dcb1642017-03-29 21:08:16 -0700649 return peer_connection_factory_->CreatePeerConnection(
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700650 modified_config, std::move(dependencies));
deadbeef1dcb1642017-03-29 21:08:16 -0700651 }
652
653 void set_signaling_message_receiver(
654 SignalingMessageReceiver* signaling_message_receiver) {
655 signaling_message_receiver_ = signaling_message_receiver;
656 }
657
658 void set_signaling_delay_ms(int delay_ms) { signaling_delay_ms_ = delay_ms; }
659
Steve Antonede9ca52017-10-16 13:04:27 -0700660 void set_signal_ice_candidates(bool signal) {
661 signal_ice_candidates_ = signal;
662 }
663
deadbeef1dcb1642017-03-29 21:08:16 -0700664 rtc::scoped_refptr<webrtc::VideoTrackInterface> CreateLocalVideoTrackInternal(
Niels Möller5c7efe72018-05-11 10:34:46 +0200665 webrtc::FakePeriodicVideoSource::Config config) {
deadbeef1dcb1642017-03-29 21:08:16 -0700666 // Set max frame rate to 10fps to reduce the risk of test flakiness.
667 // TODO(deadbeef): Do something more robust.
Niels Möller5c7efe72018-05-11 10:34:46 +0200668 config.frame_interval_ms = 100;
deadbeef1dcb1642017-03-29 21:08:16 -0700669
Niels Möller5c7efe72018-05-11 10:34:46 +0200670 video_track_sources_.emplace_back(
Niels Möller0f405822018-05-17 09:16:41 +0200671 new rtc::RefCountedObject<webrtc::FakePeriodicVideoTrackSource>(
672 config, false /* remote */));
deadbeef1dcb1642017-03-29 21:08:16 -0700673 rtc::scoped_refptr<webrtc::VideoTrackInterface> track(
Niels Möller5c7efe72018-05-11 10:34:46 +0200674 peer_connection_factory_->CreateVideoTrack(
675 rtc::CreateRandomUuid(), video_track_sources_.back()));
deadbeef1dcb1642017-03-29 21:08:16 -0700676 if (!local_video_renderer_) {
677 local_video_renderer_.reset(new webrtc::FakeVideoTrackRenderer(track));
678 }
679 return track;
680 }
681
682 void HandleIncomingOffer(const std::string& msg) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100683 RTC_LOG(LS_INFO) << debug_name_ << ": HandleIncomingOffer";
Steve Antona3a92c22017-12-07 10:27:41 -0800684 std::unique_ptr<SessionDescriptionInterface> desc =
685 webrtc::CreateSessionDescription(SdpType::kOffer, msg);
deadbeef1dcb1642017-03-29 21:08:16 -0700686 if (received_sdp_munger_) {
687 received_sdp_munger_(desc->description());
688 }
689
690 EXPECT_TRUE(SetRemoteDescription(std::move(desc)));
691 // Setting a remote description may have changed the number of receivers,
692 // so reset the receiver observers.
693 ResetRtpReceiverObservers();
Seth Hampson2f0d7022018-02-20 11:54:42 -0800694 if (remote_offer_handler_) {
695 remote_offer_handler_();
696 }
deadbeef1dcb1642017-03-29 21:08:16 -0700697 auto answer = CreateAnswer();
698 ASSERT_NE(nullptr, answer);
699 EXPECT_TRUE(SetLocalDescriptionAndSendSdpMessage(std::move(answer)));
700 }
701
702 void HandleIncomingAnswer(const std::string& msg) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100703 RTC_LOG(LS_INFO) << debug_name_ << ": HandleIncomingAnswer";
Steve Antona3a92c22017-12-07 10:27:41 -0800704 std::unique_ptr<SessionDescriptionInterface> desc =
705 webrtc::CreateSessionDescription(SdpType::kAnswer, msg);
deadbeef1dcb1642017-03-29 21:08:16 -0700706 if (received_sdp_munger_) {
707 received_sdp_munger_(desc->description());
708 }
709
710 EXPECT_TRUE(SetRemoteDescription(std::move(desc)));
711 // Set the RtpReceiverObserver after receivers are created.
712 ResetRtpReceiverObservers();
713 }
714
715 // Returns null on failure.
716 std::unique_ptr<SessionDescriptionInterface> CreateOffer() {
717 rtc::scoped_refptr<MockCreateSessionDescriptionObserver> observer(
718 new rtc::RefCountedObject<MockCreateSessionDescriptionObserver>());
719 pc()->CreateOffer(observer, offer_answer_options_);
720 return WaitForDescriptionFromObserver(observer);
721 }
722
723 // Returns null on failure.
724 std::unique_ptr<SessionDescriptionInterface> CreateAnswer() {
725 rtc::scoped_refptr<MockCreateSessionDescriptionObserver> observer(
726 new rtc::RefCountedObject<MockCreateSessionDescriptionObserver>());
727 pc()->CreateAnswer(observer, offer_answer_options_);
728 return WaitForDescriptionFromObserver(observer);
729 }
730
731 std::unique_ptr<SessionDescriptionInterface> WaitForDescriptionFromObserver(
Mirko Bonadeic61ce0d2017-11-21 17:04:20 +0100732 MockCreateSessionDescriptionObserver* observer) {
deadbeef1dcb1642017-03-29 21:08:16 -0700733 EXPECT_EQ_WAIT(true, observer->called(), kDefaultTimeout);
734 if (!observer->result()) {
735 return nullptr;
736 }
737 auto description = observer->MoveDescription();
738 if (generated_sdp_munger_) {
739 generated_sdp_munger_(description->description());
740 }
741 return description;
742 }
743
744 // Setting the local description and sending the SDP message over the fake
745 // signaling channel are combined into the same method because the SDP
746 // message needs to be sent as soon as SetLocalDescription finishes, without
747 // waiting for the observer to be called. This ensures that ICE candidates
748 // don't outrace the description.
749 bool SetLocalDescriptionAndSendSdpMessage(
750 std::unique_ptr<SessionDescriptionInterface> desc) {
751 rtc::scoped_refptr<MockSetSessionDescriptionObserver> observer(
752 new rtc::RefCountedObject<MockSetSessionDescriptionObserver>());
Mirko Bonadei675513b2017-11-09 11:09:25 +0100753 RTC_LOG(LS_INFO) << debug_name_ << ": SetLocalDescriptionAndSendSdpMessage";
Steve Antona3a92c22017-12-07 10:27:41 -0800754 SdpType type = desc->GetType();
deadbeef1dcb1642017-03-29 21:08:16 -0700755 std::string sdp;
756 EXPECT_TRUE(desc->ToString(&sdp));
757 pc()->SetLocalDescription(observer, desc.release());
Seth Hampson2f0d7022018-02-20 11:54:42 -0800758 if (sdp_semantics_ == SdpSemantics::kUnifiedPlan) {
759 RemoveUnusedVideoRenderers();
760 }
deadbeef1dcb1642017-03-29 21:08:16 -0700761 // As mentioned above, we need to send the message immediately after
762 // SetLocalDescription.
763 SendSdpMessage(type, sdp);
764 EXPECT_TRUE_WAIT(observer->called(), kDefaultTimeout);
765 return true;
766 }
767
768 bool SetRemoteDescription(std::unique_ptr<SessionDescriptionInterface> desc) {
769 rtc::scoped_refptr<MockSetSessionDescriptionObserver> observer(
770 new rtc::RefCountedObject<MockSetSessionDescriptionObserver>());
Mirko Bonadei675513b2017-11-09 11:09:25 +0100771 RTC_LOG(LS_INFO) << debug_name_ << ": SetRemoteDescription";
deadbeef1dcb1642017-03-29 21:08:16 -0700772 pc()->SetRemoteDescription(observer, desc.release());
Seth Hampson2f0d7022018-02-20 11:54:42 -0800773 if (sdp_semantics_ == SdpSemantics::kUnifiedPlan) {
774 RemoveUnusedVideoRenderers();
775 }
deadbeef1dcb1642017-03-29 21:08:16 -0700776 EXPECT_TRUE_WAIT(observer->called(), kDefaultTimeout);
777 return observer->result();
778 }
779
Seth Hampson2f0d7022018-02-20 11:54:42 -0800780 // This is a work around to remove unused fake_video_renderers from
781 // transceivers that have either stopped or are no longer receiving.
782 void RemoveUnusedVideoRenderers() {
783 auto transceivers = pc()->GetTransceivers();
784 for (auto& transceiver : transceivers) {
785 if (transceiver->receiver()->media_type() != cricket::MEDIA_TYPE_VIDEO) {
786 continue;
787 }
788 // Remove fake video renderers from any stopped transceivers.
789 if (transceiver->stopped()) {
790 auto it =
791 fake_video_renderers_.find(transceiver->receiver()->track()->id());
792 if (it != fake_video_renderers_.end()) {
793 fake_video_renderers_.erase(it);
794 }
795 }
796 // Remove fake video renderers from any transceivers that are no longer
797 // receiving.
798 if ((transceiver->current_direction() &&
799 !webrtc::RtpTransceiverDirectionHasRecv(
800 *transceiver->current_direction()))) {
801 auto it =
802 fake_video_renderers_.find(transceiver->receiver()->track()->id());
803 if (it != fake_video_renderers_.end()) {
804 fake_video_renderers_.erase(it);
805 }
806 }
807 }
808 }
809
deadbeef1dcb1642017-03-29 21:08:16 -0700810 // Simulate sending a blob of SDP with delay |signaling_delay_ms_| (0 by
811 // default).
Steve Antona3a92c22017-12-07 10:27:41 -0800812 void SendSdpMessage(SdpType type, const std::string& msg) {
deadbeef1dcb1642017-03-29 21:08:16 -0700813 if (signaling_delay_ms_ == 0) {
814 RelaySdpMessageIfReceiverExists(type, msg);
815 } else {
816 invoker_.AsyncInvokeDelayed<void>(
817 RTC_FROM_HERE, rtc::Thread::Current(),
818 rtc::Bind(&PeerConnectionWrapper::RelaySdpMessageIfReceiverExists,
819 this, type, msg),
820 signaling_delay_ms_);
821 }
822 }
823
Steve Antona3a92c22017-12-07 10:27:41 -0800824 void RelaySdpMessageIfReceiverExists(SdpType type, const std::string& msg) {
deadbeef1dcb1642017-03-29 21:08:16 -0700825 if (signaling_message_receiver_) {
826 signaling_message_receiver_->ReceiveSdpMessage(type, msg);
827 }
828 }
829
830 // Simulate trickling an ICE candidate with delay |signaling_delay_ms_| (0 by
831 // default).
832 void SendIceMessage(const std::string& sdp_mid,
833 int sdp_mline_index,
834 const std::string& msg) {
835 if (signaling_delay_ms_ == 0) {
836 RelayIceMessageIfReceiverExists(sdp_mid, sdp_mline_index, msg);
837 } else {
838 invoker_.AsyncInvokeDelayed<void>(
839 RTC_FROM_HERE, rtc::Thread::Current(),
840 rtc::Bind(&PeerConnectionWrapper::RelayIceMessageIfReceiverExists,
841 this, sdp_mid, sdp_mline_index, msg),
842 signaling_delay_ms_);
843 }
844 }
845
846 void RelayIceMessageIfReceiverExists(const std::string& sdp_mid,
847 int sdp_mline_index,
848 const std::string& msg) {
849 if (signaling_message_receiver_) {
850 signaling_message_receiver_->ReceiveIceMessage(sdp_mid, sdp_mline_index,
851 msg);
852 }
853 }
854
855 // SignalingMessageReceiver callbacks.
Steve Antona3a92c22017-12-07 10:27:41 -0800856 void ReceiveSdpMessage(SdpType type, const std::string& msg) override {
857 if (type == SdpType::kOffer) {
deadbeef1dcb1642017-03-29 21:08:16 -0700858 HandleIncomingOffer(msg);
859 } else {
860 HandleIncomingAnswer(msg);
861 }
862 }
863
864 void ReceiveIceMessage(const std::string& sdp_mid,
865 int sdp_mline_index,
866 const std::string& msg) override {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100867 RTC_LOG(LS_INFO) << debug_name_ << ": ReceiveIceMessage";
deadbeef1dcb1642017-03-29 21:08:16 -0700868 std::unique_ptr<webrtc::IceCandidateInterface> candidate(
869 webrtc::CreateIceCandidate(sdp_mid, sdp_mline_index, msg, nullptr));
870 EXPECT_TRUE(pc()->AddIceCandidate(candidate.get()));
871 }
872
873 // PeerConnectionObserver callbacks.
874 void OnSignalingChange(
875 webrtc::PeerConnectionInterface::SignalingState new_state) override {
876 EXPECT_EQ(pc()->signaling_state(), new_state);
877 }
Steve Anton15324772018-01-16 10:26:49 -0800878 void OnAddTrack(rtc::scoped_refptr<RtpReceiverInterface> receiver,
879 const std::vector<rtc::scoped_refptr<MediaStreamInterface>>&
880 streams) override {
881 if (receiver->media_type() == cricket::MEDIA_TYPE_VIDEO) {
882 rtc::scoped_refptr<VideoTrackInterface> video_track(
883 static_cast<VideoTrackInterface*>(receiver->track().get()));
884 ASSERT_TRUE(fake_video_renderers_.find(video_track->id()) ==
deadbeef1dcb1642017-03-29 21:08:16 -0700885 fake_video_renderers_.end());
Steve Anton15324772018-01-16 10:26:49 -0800886 fake_video_renderers_[video_track->id()] =
Karl Wiberg918f50c2018-07-05 11:40:33 +0200887 absl::make_unique<FakeVideoTrackRenderer>(video_track);
deadbeef1dcb1642017-03-29 21:08:16 -0700888 }
889 }
Steve Anton15324772018-01-16 10:26:49 -0800890 void OnRemoveTrack(
891 rtc::scoped_refptr<RtpReceiverInterface> receiver) override {
892 if (receiver->media_type() == cricket::MEDIA_TYPE_VIDEO) {
893 auto it = fake_video_renderers_.find(receiver->track()->id());
894 RTC_DCHECK(it != fake_video_renderers_.end());
895 fake_video_renderers_.erase(it);
896 }
897 }
deadbeef1dcb1642017-03-29 21:08:16 -0700898 void OnRenegotiationNeeded() override {}
899 void OnIceConnectionChange(
900 webrtc::PeerConnectionInterface::IceConnectionState new_state) override {
901 EXPECT_EQ(pc()->ice_connection_state(), new_state);
Steve Antonede9ca52017-10-16 13:04:27 -0700902 ice_connection_state_history_.push_back(new_state);
deadbeef1dcb1642017-03-29 21:08:16 -0700903 }
Jonas Olssonacd8ae72019-02-25 15:26:24 +0100904 void OnStandardizedIceConnectionChange(
905 webrtc::PeerConnectionInterface::IceConnectionState new_state) override {
906 standardized_ice_connection_state_history_.push_back(new_state);
907 }
Jonas Olsson635474e2018-10-18 15:58:17 +0200908 void OnConnectionChange(
909 webrtc::PeerConnectionInterface::PeerConnectionState new_state) override {
910 peer_connection_state_history_.push_back(new_state);
911 }
912
deadbeef1dcb1642017-03-29 21:08:16 -0700913 void OnIceGatheringChange(
914 webrtc::PeerConnectionInterface::IceGatheringState new_state) override {
deadbeef1dcb1642017-03-29 21:08:16 -0700915 EXPECT_EQ(pc()->ice_gathering_state(), new_state);
Steve Antonede9ca52017-10-16 13:04:27 -0700916 ice_gathering_state_history_.push_back(new_state);
deadbeef1dcb1642017-03-29 21:08:16 -0700917 }
918 void OnIceCandidate(const webrtc::IceCandidateInterface* candidate) override {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100919 RTC_LOG(LS_INFO) << debug_name_ << ": OnIceCandidate";
deadbeef1dcb1642017-03-29 21:08:16 -0700920
Qingsi Wang1dac6d82018-12-12 15:28:47 -0800921 if (remote_async_resolver_) {
922 const auto& local_candidate = candidate->candidate();
923 const auto& mdns_responder = network()->GetMdnsResponderForTesting();
924 if (local_candidate.address().IsUnresolvedIP()) {
925 RTC_DCHECK(local_candidate.type() == cricket::LOCAL_PORT_TYPE);
926 rtc::SocketAddress resolved_addr(local_candidate.address());
927 const auto resolved_ip = mdns_responder->GetMappedAddressForName(
928 local_candidate.address().hostname());
929 RTC_DCHECK(!resolved_ip.IsNil());
930 resolved_addr.SetResolvedIP(resolved_ip);
931 EXPECT_CALL(*remote_async_resolver_, GetResolvedAddress(_, _))
932 .WillOnce(DoAll(SetArgPointee<1>(resolved_addr), Return(true)));
933 EXPECT_CALL(*remote_async_resolver_, Destroy(_));
Zach Stein6fcdc2f2018-08-23 16:25:55 -0700934 }
Zach Stein6fcdc2f2018-08-23 16:25:55 -0700935 }
936
deadbeef1dcb1642017-03-29 21:08:16 -0700937 std::string ice_sdp;
Qingsi Wang1dac6d82018-12-12 15:28:47 -0800938 EXPECT_TRUE(candidate->ToString(&ice_sdp));
Steve Antonede9ca52017-10-16 13:04:27 -0700939 if (signaling_message_receiver_ == nullptr || !signal_ice_candidates_) {
deadbeef1dcb1642017-03-29 21:08:16 -0700940 // Remote party may be deleted.
941 return;
942 }
Qingsi Wang1dac6d82018-12-12 15:28:47 -0800943 SendIceMessage(candidate->sdp_mid(), candidate->sdp_mline_index(), ice_sdp);
deadbeef1dcb1642017-03-29 21:08:16 -0700944 }
945 void OnDataChannel(
946 rtc::scoped_refptr<DataChannelInterface> data_channel) override {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100947 RTC_LOG(LS_INFO) << debug_name_ << ": OnDataChannel";
deadbeef1dcb1642017-03-29 21:08:16 -0700948 data_channel_ = data_channel;
949 data_observer_.reset(new MockDataChannelObserver(data_channel));
950 }
951
deadbeef1dcb1642017-03-29 21:08:16 -0700952 std::string debug_name_;
953
954 std::unique_ptr<rtc::FakeNetworkManager> fake_network_manager_;
955
956 rtc::scoped_refptr<webrtc::PeerConnectionInterface> peer_connection_;
957 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface>
958 peer_connection_factory_;
959
Steve Antonede9ca52017-10-16 13:04:27 -0700960 cricket::PortAllocator* port_allocator_;
deadbeef1dcb1642017-03-29 21:08:16 -0700961 // Needed to keep track of number of frames sent.
962 rtc::scoped_refptr<FakeAudioCaptureModule> fake_audio_capture_module_;
963 // Needed to keep track of number of frames received.
964 std::map<std::string, std::unique_ptr<webrtc::FakeVideoTrackRenderer>>
965 fake_video_renderers_;
966 // Needed to ensure frames aren't received for removed tracks.
967 std::vector<std::unique_ptr<webrtc::FakeVideoTrackRenderer>>
968 removed_fake_video_renderers_;
deadbeef1dcb1642017-03-29 21:08:16 -0700969
970 // For remote peer communication.
971 SignalingMessageReceiver* signaling_message_receiver_ = nullptr;
972 int signaling_delay_ms_ = 0;
Steve Antonede9ca52017-10-16 13:04:27 -0700973 bool signal_ice_candidates_ = true;
deadbeef1dcb1642017-03-29 21:08:16 -0700974
Niels Möller5c7efe72018-05-11 10:34:46 +0200975 // Store references to the video sources we've created, so that we can stop
deadbeef1dcb1642017-03-29 21:08:16 -0700976 // them, if required.
Niels Möller5c7efe72018-05-11 10:34:46 +0200977 std::vector<rtc::scoped_refptr<webrtc::VideoTrackSource>>
978 video_track_sources_;
deadbeef1dcb1642017-03-29 21:08:16 -0700979 // |local_video_renderer_| attached to the first created local video track.
980 std::unique_ptr<webrtc::FakeVideoTrackRenderer> local_video_renderer_;
981
Seth Hampson2f0d7022018-02-20 11:54:42 -0800982 SdpSemantics sdp_semantics_;
deadbeef1dcb1642017-03-29 21:08:16 -0700983 PeerConnectionInterface::RTCOfferAnswerOptions offer_answer_options_;
984 std::function<void(cricket::SessionDescription*)> received_sdp_munger_;
985 std::function<void(cricket::SessionDescription*)> generated_sdp_munger_;
Seth Hampson2f0d7022018-02-20 11:54:42 -0800986 std::function<void()> remote_offer_handler_;
Qingsi Wang1dac6d82018-12-12 15:28:47 -0800987 rtc::MockAsyncResolver* remote_async_resolver_ = nullptr;
deadbeef1dcb1642017-03-29 21:08:16 -0700988 rtc::scoped_refptr<DataChannelInterface> data_channel_;
989 std::unique_ptr<MockDataChannelObserver> data_observer_;
990
991 std::vector<std::unique_ptr<MockRtpReceiverObserver>> rtp_receiver_observers_;
992
Steve Antonede9ca52017-10-16 13:04:27 -0700993 std::vector<PeerConnectionInterface::IceConnectionState>
994 ice_connection_state_history_;
Jonas Olssonacd8ae72019-02-25 15:26:24 +0100995 std::vector<PeerConnectionInterface::IceConnectionState>
996 standardized_ice_connection_state_history_;
Jonas Olsson635474e2018-10-18 15:58:17 +0200997 std::vector<PeerConnectionInterface::PeerConnectionState>
998 peer_connection_state_history_;
Steve Antonede9ca52017-10-16 13:04:27 -0700999 std::vector<PeerConnectionInterface::IceGatheringState>
1000 ice_gathering_state_history_;
deadbeef1dcb1642017-03-29 21:08:16 -07001001
Qingsi Wang7685e862018-06-11 20:15:46 -07001002 webrtc::FakeRtcEventLogFactory* event_log_factory_;
1003
deadbeef1dcb1642017-03-29 21:08:16 -07001004 rtc::AsyncInvoker invoker_;
1005
Seth Hampson2f0d7022018-02-20 11:54:42 -08001006 friend class PeerConnectionIntegrationBaseTest;
deadbeef1dcb1642017-03-29 21:08:16 -07001007};
1008
Elad Alon99c3fe52017-10-13 16:29:40 +02001009class MockRtcEventLogOutput : public webrtc::RtcEventLogOutput {
1010 public:
1011 virtual ~MockRtcEventLogOutput() = default;
1012 MOCK_CONST_METHOD0(IsActive, bool());
1013 MOCK_METHOD1(Write, bool(const std::string&));
1014};
1015
Seth Hampson2f0d7022018-02-20 11:54:42 -08001016// This helper object is used for both specifying how many audio/video frames
1017// are expected to be received for a caller/callee. It provides helper functions
1018// to specify these expectations. The object initially starts in a state of no
1019// expectations.
1020class MediaExpectations {
1021 public:
1022 enum ExpectFrames {
1023 kExpectSomeFrames,
1024 kExpectNoFrames,
1025 kNoExpectation,
1026 };
1027
1028 void ExpectBidirectionalAudioAndVideo() {
1029 ExpectBidirectionalAudio();
1030 ExpectBidirectionalVideo();
1031 }
1032
1033 void ExpectBidirectionalAudio() {
1034 CallerExpectsSomeAudio();
1035 CalleeExpectsSomeAudio();
1036 }
1037
1038 void ExpectNoAudio() {
1039 CallerExpectsNoAudio();
1040 CalleeExpectsNoAudio();
1041 }
1042
1043 void ExpectBidirectionalVideo() {
1044 CallerExpectsSomeVideo();
1045 CalleeExpectsSomeVideo();
1046 }
1047
1048 void ExpectNoVideo() {
1049 CallerExpectsNoVideo();
1050 CalleeExpectsNoVideo();
1051 }
1052
1053 void CallerExpectsSomeAudioAndVideo() {
1054 CallerExpectsSomeAudio();
1055 CallerExpectsSomeVideo();
1056 }
1057
1058 void CalleeExpectsSomeAudioAndVideo() {
1059 CalleeExpectsSomeAudio();
1060 CalleeExpectsSomeVideo();
1061 }
1062
1063 // Caller's audio functions.
1064 void CallerExpectsSomeAudio(
1065 int expected_audio_frames = kDefaultExpectedAudioFrameCount) {
1066 caller_audio_expectation_ = kExpectSomeFrames;
1067 caller_audio_frames_expected_ = expected_audio_frames;
1068 }
1069
1070 void CallerExpectsNoAudio() {
1071 caller_audio_expectation_ = kExpectNoFrames;
1072 caller_audio_frames_expected_ = 0;
1073 }
1074
1075 // Caller's video functions.
1076 void CallerExpectsSomeVideo(
1077 int expected_video_frames = kDefaultExpectedVideoFrameCount) {
1078 caller_video_expectation_ = kExpectSomeFrames;
1079 caller_video_frames_expected_ = expected_video_frames;
1080 }
1081
1082 void CallerExpectsNoVideo() {
1083 caller_video_expectation_ = kExpectNoFrames;
1084 caller_video_frames_expected_ = 0;
1085 }
1086
1087 // Callee's audio functions.
1088 void CalleeExpectsSomeAudio(
1089 int expected_audio_frames = kDefaultExpectedAudioFrameCount) {
1090 callee_audio_expectation_ = kExpectSomeFrames;
1091 callee_audio_frames_expected_ = expected_audio_frames;
1092 }
1093
1094 void CalleeExpectsNoAudio() {
1095 callee_audio_expectation_ = kExpectNoFrames;
1096 callee_audio_frames_expected_ = 0;
1097 }
1098
1099 // Callee's video functions.
1100 void CalleeExpectsSomeVideo(
1101 int expected_video_frames = kDefaultExpectedVideoFrameCount) {
1102 callee_video_expectation_ = kExpectSomeFrames;
1103 callee_video_frames_expected_ = expected_video_frames;
1104 }
1105
1106 void CalleeExpectsNoVideo() {
1107 callee_video_expectation_ = kExpectNoFrames;
1108 callee_video_frames_expected_ = 0;
1109 }
1110
1111 ExpectFrames caller_audio_expectation_ = kNoExpectation;
1112 ExpectFrames caller_video_expectation_ = kNoExpectation;
1113 ExpectFrames callee_audio_expectation_ = kNoExpectation;
1114 ExpectFrames callee_video_expectation_ = kNoExpectation;
1115 int caller_audio_frames_expected_ = 0;
1116 int caller_video_frames_expected_ = 0;
1117 int callee_audio_frames_expected_ = 0;
1118 int callee_video_frames_expected_ = 0;
1119};
1120
deadbeef1dcb1642017-03-29 21:08:16 -07001121// Tests two PeerConnections connecting to each other end-to-end, using a
1122// virtual network, fake A/V capture and fake encoder/decoders. The
1123// PeerConnections share the threads/socket servers, but use separate versions
1124// of everything else (including "PeerConnectionFactory"s).
Seth Hampson2f0d7022018-02-20 11:54:42 -08001125class PeerConnectionIntegrationBaseTest : public testing::Test {
deadbeef1dcb1642017-03-29 21:08:16 -07001126 public:
Seth Hampson2f0d7022018-02-20 11:54:42 -08001127 explicit PeerConnectionIntegrationBaseTest(SdpSemantics sdp_semantics)
1128 : sdp_semantics_(sdp_semantics),
1129 ss_(new rtc::VirtualSocketServer()),
Steve Antonede9ca52017-10-16 13:04:27 -07001130 fss_(new rtc::FirewallSocketServer(ss_.get())),
1131 network_thread_(new rtc::Thread(fss_.get())),
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001132 worker_thread_(rtc::Thread::Create()),
1133 loopback_media_transports_(network_thread_.get()) {
Sebastian Jansson8a793a02018-03-13 15:21:48 +01001134 network_thread_->SetName("PCNetworkThread", this);
1135 worker_thread_->SetName("PCWorkerThread", this);
deadbeef1dcb1642017-03-29 21:08:16 -07001136 RTC_CHECK(network_thread_->Start());
1137 RTC_CHECK(worker_thread_->Start());
Qingsi Wang7fc821d2018-07-12 12:54:53 -07001138 webrtc::metrics::Reset();
deadbeef1dcb1642017-03-29 21:08:16 -07001139 }
1140
Seth Hampson2f0d7022018-02-20 11:54:42 -08001141 ~PeerConnectionIntegrationBaseTest() {
Seth Hampsonaed71642018-06-11 07:41:32 -07001142 // The PeerConnections should deleted before the TurnCustomizers.
1143 // A TurnPort is created with a raw pointer to a TurnCustomizer. The
1144 // TurnPort has the same lifetime as the PeerConnection, so it's expected
1145 // that the TurnCustomizer outlives the life of the PeerConnection or else
1146 // when Send() is called it will hit a seg fault.
deadbeef1dcb1642017-03-29 21:08:16 -07001147 if (caller_) {
1148 caller_->set_signaling_message_receiver(nullptr);
Seth Hampsonaed71642018-06-11 07:41:32 -07001149 delete SetCallerPcWrapperAndReturnCurrent(nullptr);
deadbeef1dcb1642017-03-29 21:08:16 -07001150 }
1151 if (callee_) {
1152 callee_->set_signaling_message_receiver(nullptr);
Seth Hampsonaed71642018-06-11 07:41:32 -07001153 delete SetCalleePcWrapperAndReturnCurrent(nullptr);
deadbeef1dcb1642017-03-29 21:08:16 -07001154 }
Seth Hampsonaed71642018-06-11 07:41:32 -07001155
1156 // If turn servers were created for the test they need to be destroyed on
1157 // the network thread.
1158 network_thread()->Invoke<void>(RTC_FROM_HERE, [this] {
1159 turn_servers_.clear();
1160 turn_customizers_.clear();
1161 });
deadbeef1dcb1642017-03-29 21:08:16 -07001162 }
1163
1164 bool SignalingStateStable() {
1165 return caller_->SignalingStateStable() && callee_->SignalingStateStable();
1166 }
1167
deadbeef71452802017-05-07 17:21:01 -07001168 bool DtlsConnected() {
Alex Loiko9289eda2018-11-23 16:18:59 +00001169 // TODO(deadbeef): kIceConnectionConnected currently means both ICE and DTLS
1170 // are connected. This is an important distinction. Once we have separate
1171 // ICE and DTLS state, this check needs to use the DTLS state.
1172 return (callee()->ice_connection_state() ==
1173 webrtc::PeerConnectionInterface::kIceConnectionConnected ||
1174 callee()->ice_connection_state() ==
1175 webrtc::PeerConnectionInterface::kIceConnectionCompleted) &&
1176 (caller()->ice_connection_state() ==
1177 webrtc::PeerConnectionInterface::kIceConnectionConnected ||
1178 caller()->ice_connection_state() ==
1179 webrtc::PeerConnectionInterface::kIceConnectionCompleted);
deadbeef71452802017-05-07 17:21:01 -07001180 }
1181
Qingsi Wang7685e862018-06-11 20:15:46 -07001182 // When |event_log_factory| is null, the default implementation of the event
1183 // log factory will be used.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001184 std::unique_ptr<PeerConnectionWrapper> CreatePeerConnectionWrapper(
1185 const std::string& debug_name,
Seth Hampson2f0d7022018-02-20 11:54:42 -08001186 const PeerConnectionFactory::Options* options,
1187 const RTCConfiguration* config,
Qingsi Wang7685e862018-06-11 20:15:46 -07001188 webrtc::PeerConnectionDependencies dependencies,
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001189 std::unique_ptr<webrtc::FakeRtcEventLogFactory> event_log_factory,
1190 std::unique_ptr<webrtc::MediaTransportFactory> media_transport_factory) {
Seth Hampson2f0d7022018-02-20 11:54:42 -08001191 RTCConfiguration modified_config;
1192 if (config) {
1193 modified_config = *config;
1194 }
Steve Anton3acffc32018-04-12 17:21:03 -07001195 modified_config.sdp_semantics = sdp_semantics_;
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001196 if (!dependencies.cert_generator) {
1197 dependencies.cert_generator =
Karl Wiberg918f50c2018-07-05 11:40:33 +02001198 absl::make_unique<FakeRTCCertificateGenerator>();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001199 }
1200 std::unique_ptr<PeerConnectionWrapper> client(
1201 new PeerConnectionWrapper(debug_name));
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001202
Niels Möllerf06f9232018-08-07 12:32:18 +02001203 if (!client->Init(options, &modified_config, std::move(dependencies),
1204 network_thread_.get(), worker_thread_.get(),
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001205 std::move(event_log_factory),
1206 std::move(media_transport_factory))) {
Seth Hampson2f0d7022018-02-20 11:54:42 -08001207 return nullptr;
1208 }
1209 return client;
1210 }
1211
Qingsi Wang7685e862018-06-11 20:15:46 -07001212 std::unique_ptr<PeerConnectionWrapper>
1213 CreatePeerConnectionWrapperWithFakeRtcEventLog(
1214 const std::string& debug_name,
Qingsi Wang7685e862018-06-11 20:15:46 -07001215 const PeerConnectionFactory::Options* options,
1216 const RTCConfiguration* config,
1217 webrtc::PeerConnectionDependencies dependencies) {
1218 std::unique_ptr<webrtc::FakeRtcEventLogFactory> event_log_factory(
1219 new webrtc::FakeRtcEventLogFactory(rtc::Thread::Current()));
Niels Möllerf06f9232018-08-07 12:32:18 +02001220 return CreatePeerConnectionWrapper(debug_name, options, config,
Qingsi Wang7685e862018-06-11 20:15:46 -07001221 std::move(dependencies),
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001222 std::move(event_log_factory),
1223 /*media_transport_factory=*/nullptr);
Qingsi Wang7685e862018-06-11 20:15:46 -07001224 }
1225
deadbeef1dcb1642017-03-29 21:08:16 -07001226 bool CreatePeerConnectionWrappers() {
1227 return CreatePeerConnectionWrappersWithConfig(
1228 PeerConnectionInterface::RTCConfiguration(),
1229 PeerConnectionInterface::RTCConfiguration());
1230 }
1231
Steve Anton3acffc32018-04-12 17:21:03 -07001232 bool CreatePeerConnectionWrappersWithSdpSemantics(
1233 SdpSemantics caller_semantics,
1234 SdpSemantics callee_semantics) {
1235 // Can't specify the sdp_semantics in the passed-in configuration since it
1236 // will be overwritten by CreatePeerConnectionWrapper with whatever is
1237 // stored in sdp_semantics_. So get around this by modifying the instance
1238 // variable before calling CreatePeerConnectionWrapper for the caller and
1239 // callee PeerConnections.
1240 SdpSemantics original_semantics = sdp_semantics_;
1241 sdp_semantics_ = caller_semantics;
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001242 caller_ = CreatePeerConnectionWrapper(
Niels Möllerf06f9232018-08-07 12:32:18 +02001243 "Caller", nullptr, nullptr, webrtc::PeerConnectionDependencies(nullptr),
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001244 nullptr, /*media_transport_factory=*/nullptr);
Steve Anton3acffc32018-04-12 17:21:03 -07001245 sdp_semantics_ = callee_semantics;
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001246 callee_ = CreatePeerConnectionWrapper(
Niels Möllerf06f9232018-08-07 12:32:18 +02001247 "Callee", nullptr, nullptr, webrtc::PeerConnectionDependencies(nullptr),
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001248 nullptr, /*media_transport_factory=*/nullptr);
Steve Anton3acffc32018-04-12 17:21:03 -07001249 sdp_semantics_ = original_semantics;
1250 return caller_ && callee_;
1251 }
1252
deadbeef1dcb1642017-03-29 21:08:16 -07001253 bool CreatePeerConnectionWrappersWithConfig(
1254 const PeerConnectionInterface::RTCConfiguration& caller_config,
1255 const PeerConnectionInterface::RTCConfiguration& callee_config) {
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001256 caller_ = CreatePeerConnectionWrapper(
Niels Möllerf06f9232018-08-07 12:32:18 +02001257 "Caller", nullptr, &caller_config,
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001258 webrtc::PeerConnectionDependencies(nullptr), nullptr,
1259 /*media_transport_factory=*/nullptr);
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001260 callee_ = CreatePeerConnectionWrapper(
Niels Möllerf06f9232018-08-07 12:32:18 +02001261 "Callee", nullptr, &callee_config,
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001262 webrtc::PeerConnectionDependencies(nullptr), nullptr,
1263 /*media_transport_factory=*/nullptr);
1264 return caller_ && callee_;
1265 }
1266
1267 bool CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
1268 const PeerConnectionInterface::RTCConfiguration& caller_config,
1269 const PeerConnectionInterface::RTCConfiguration& callee_config,
1270 std::unique_ptr<webrtc::MediaTransportFactory> caller_factory,
1271 std::unique_ptr<webrtc::MediaTransportFactory> callee_factory) {
1272 caller_ =
1273 CreatePeerConnectionWrapper("Caller", nullptr, &caller_config,
1274 webrtc::PeerConnectionDependencies(nullptr),
1275 nullptr, std::move(caller_factory));
1276 callee_ =
1277 CreatePeerConnectionWrapper("Callee", nullptr, &callee_config,
1278 webrtc::PeerConnectionDependencies(nullptr),
1279 nullptr, std::move(callee_factory));
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001280 return caller_ && callee_;
1281 }
1282
1283 bool CreatePeerConnectionWrappersWithConfigAndDeps(
1284 const PeerConnectionInterface::RTCConfiguration& caller_config,
1285 webrtc::PeerConnectionDependencies caller_dependencies,
1286 const PeerConnectionInterface::RTCConfiguration& callee_config,
1287 webrtc::PeerConnectionDependencies callee_dependencies) {
1288 caller_ =
Niels Möllerf06f9232018-08-07 12:32:18 +02001289 CreatePeerConnectionWrapper("Caller", nullptr, &caller_config,
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001290 std::move(caller_dependencies), nullptr,
1291 /*media_transport_factory=*/nullptr);
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001292 callee_ =
Niels Möllerf06f9232018-08-07 12:32:18 +02001293 CreatePeerConnectionWrapper("Callee", nullptr, &callee_config,
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001294 std::move(callee_dependencies), nullptr,
1295 /*media_transport_factory=*/nullptr);
deadbeef1dcb1642017-03-29 21:08:16 -07001296 return caller_ && callee_;
1297 }
1298
1299 bool CreatePeerConnectionWrappersWithOptions(
1300 const PeerConnectionFactory::Options& caller_options,
1301 const PeerConnectionFactory::Options& callee_options) {
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001302 caller_ = CreatePeerConnectionWrapper(
Niels Möllerf06f9232018-08-07 12:32:18 +02001303 "Caller", &caller_options, nullptr,
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001304 webrtc::PeerConnectionDependencies(nullptr), nullptr,
1305 /*media_transport_factory=*/nullptr);
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001306 callee_ = CreatePeerConnectionWrapper(
Niels Möllerf06f9232018-08-07 12:32:18 +02001307 "Callee", &callee_options, nullptr,
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001308 webrtc::PeerConnectionDependencies(nullptr), nullptr,
1309 /*media_transport_factory=*/nullptr);
Qingsi Wang7685e862018-06-11 20:15:46 -07001310 return caller_ && callee_;
1311 }
1312
1313 bool CreatePeerConnectionWrappersWithFakeRtcEventLog() {
1314 PeerConnectionInterface::RTCConfiguration default_config;
1315 caller_ = CreatePeerConnectionWrapperWithFakeRtcEventLog(
Niels Möllerf06f9232018-08-07 12:32:18 +02001316 "Caller", nullptr, &default_config,
Qingsi Wang7685e862018-06-11 20:15:46 -07001317 webrtc::PeerConnectionDependencies(nullptr));
1318 callee_ = CreatePeerConnectionWrapperWithFakeRtcEventLog(
Niels Möllerf06f9232018-08-07 12:32:18 +02001319 "Callee", nullptr, &default_config,
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001320 webrtc::PeerConnectionDependencies(nullptr));
deadbeef1dcb1642017-03-29 21:08:16 -07001321 return caller_ && callee_;
1322 }
1323
Seth Hampson2f0d7022018-02-20 11:54:42 -08001324 std::unique_ptr<PeerConnectionWrapper>
1325 CreatePeerConnectionWrapperWithAlternateKey() {
deadbeef1dcb1642017-03-29 21:08:16 -07001326 std::unique_ptr<FakeRTCCertificateGenerator> cert_generator(
1327 new FakeRTCCertificateGenerator());
1328 cert_generator->use_alternate_key();
1329
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001330 webrtc::PeerConnectionDependencies dependencies(nullptr);
1331 dependencies.cert_generator = std::move(cert_generator);
Niels Möllerf06f9232018-08-07 12:32:18 +02001332 return CreatePeerConnectionWrapper("New Peer", nullptr, nullptr,
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001333 std::move(dependencies), nullptr,
1334 /*media_transport_factory=*/nullptr);
deadbeef1dcb1642017-03-29 21:08:16 -07001335 }
1336
Seth Hampsonaed71642018-06-11 07:41:32 -07001337 cricket::TestTurnServer* CreateTurnServer(
1338 rtc::SocketAddress internal_address,
1339 rtc::SocketAddress external_address,
1340 cricket::ProtocolType type = cricket::ProtocolType::PROTO_UDP,
1341 const std::string& common_name = "test turn server") {
1342 rtc::Thread* thread = network_thread();
1343 std::unique_ptr<cricket::TestTurnServer> turn_server =
1344 network_thread()->Invoke<std::unique_ptr<cricket::TestTurnServer>>(
1345 RTC_FROM_HERE,
1346 [thread, internal_address, external_address, type, common_name] {
Karl Wiberg918f50c2018-07-05 11:40:33 +02001347 return absl::make_unique<cricket::TestTurnServer>(
Seth Hampsonaed71642018-06-11 07:41:32 -07001348 thread, internal_address, external_address, type,
1349 /*ignore_bad_certs=*/true, common_name);
1350 });
1351 turn_servers_.push_back(std::move(turn_server));
1352 // Interactions with the turn server should be done on the network thread.
1353 return turn_servers_.back().get();
1354 }
1355
1356 cricket::TestTurnCustomizer* CreateTurnCustomizer() {
1357 std::unique_ptr<cricket::TestTurnCustomizer> turn_customizer =
1358 network_thread()->Invoke<std::unique_ptr<cricket::TestTurnCustomizer>>(
1359 RTC_FROM_HERE,
Karl Wiberg918f50c2018-07-05 11:40:33 +02001360 [] { return absl::make_unique<cricket::TestTurnCustomizer>(); });
Seth Hampsonaed71642018-06-11 07:41:32 -07001361 turn_customizers_.push_back(std::move(turn_customizer));
1362 // Interactions with the turn customizer should be done on the network
1363 // thread.
1364 return turn_customizers_.back().get();
1365 }
1366
1367 // Checks that the function counters for a TestTurnCustomizer are greater than
1368 // 0.
1369 void ExpectTurnCustomizerCountersIncremented(
1370 cricket::TestTurnCustomizer* turn_customizer) {
1371 unsigned int allow_channel_data_counter =
1372 network_thread()->Invoke<unsigned int>(
1373 RTC_FROM_HERE, [turn_customizer] {
1374 return turn_customizer->allow_channel_data_cnt_;
1375 });
1376 EXPECT_GT(allow_channel_data_counter, 0u);
1377 unsigned int modify_counter = network_thread()->Invoke<unsigned int>(
1378 RTC_FROM_HERE,
1379 [turn_customizer] { return turn_customizer->modify_cnt_; });
1380 EXPECT_GT(modify_counter, 0u);
1381 }
1382
deadbeef1dcb1642017-03-29 21:08:16 -07001383 // Once called, SDP blobs and ICE candidates will be automatically signaled
1384 // between PeerConnections.
1385 void ConnectFakeSignaling() {
1386 caller_->set_signaling_message_receiver(callee_.get());
1387 callee_->set_signaling_message_receiver(caller_.get());
1388 }
1389
Steve Antonede9ca52017-10-16 13:04:27 -07001390 // Once called, SDP blobs will be automatically signaled between
1391 // PeerConnections. Note that ICE candidates will not be signaled unless they
1392 // are in the exchanged SDP blobs.
1393 void ConnectFakeSignalingForSdpOnly() {
1394 ConnectFakeSignaling();
1395 SetSignalIceCandidates(false);
1396 }
1397
deadbeef1dcb1642017-03-29 21:08:16 -07001398 void SetSignalingDelayMs(int delay_ms) {
1399 caller_->set_signaling_delay_ms(delay_ms);
1400 callee_->set_signaling_delay_ms(delay_ms);
1401 }
1402
Steve Antonede9ca52017-10-16 13:04:27 -07001403 void SetSignalIceCandidates(bool signal) {
1404 caller_->set_signal_ice_candidates(signal);
1405 callee_->set_signal_ice_candidates(signal);
1406 }
1407
deadbeef1dcb1642017-03-29 21:08:16 -07001408 // Messages may get lost on the unreliable DataChannel, so we send multiple
1409 // times to avoid test flakiness.
1410 void SendRtpDataWithRetries(webrtc::DataChannelInterface* dc,
1411 const std::string& data,
1412 int retries) {
1413 for (int i = 0; i < retries; ++i) {
1414 dc->Send(DataBuffer(data));
1415 }
1416 }
1417
1418 rtc::Thread* network_thread() { return network_thread_.get(); }
1419
1420 rtc::VirtualSocketServer* virtual_socket_server() { return ss_.get(); }
1421
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001422 webrtc::MediaTransportPair* loopback_media_transports() {
1423 return &loopback_media_transports_;
1424 }
1425
deadbeef1dcb1642017-03-29 21:08:16 -07001426 PeerConnectionWrapper* caller() { return caller_.get(); }
1427
1428 // Set the |caller_| to the |wrapper| passed in and return the
1429 // original |caller_|.
1430 PeerConnectionWrapper* SetCallerPcWrapperAndReturnCurrent(
1431 PeerConnectionWrapper* wrapper) {
1432 PeerConnectionWrapper* old = caller_.release();
1433 caller_.reset(wrapper);
1434 return old;
1435 }
1436
1437 PeerConnectionWrapper* callee() { return callee_.get(); }
1438
1439 // Set the |callee_| to the |wrapper| passed in and return the
1440 // original |callee_|.
1441 PeerConnectionWrapper* SetCalleePcWrapperAndReturnCurrent(
1442 PeerConnectionWrapper* wrapper) {
1443 PeerConnectionWrapper* old = callee_.release();
1444 callee_.reset(wrapper);
1445 return old;
1446 }
1447
Qingsi Wang1dac6d82018-12-12 15:28:47 -08001448 void SetPortAllocatorFlags(uint32_t caller_flags, uint32_t callee_flags) {
1449 network_thread()->Invoke<void>(
1450 RTC_FROM_HERE, rtc::Bind(&cricket::PortAllocator::set_flags,
1451 caller()->port_allocator(), caller_flags));
1452 network_thread()->Invoke<void>(
1453 RTC_FROM_HERE, rtc::Bind(&cricket::PortAllocator::set_flags,
1454 callee()->port_allocator(), callee_flags));
1455 }
1456
Steve Antonede9ca52017-10-16 13:04:27 -07001457 rtc::FirewallSocketServer* firewall() const { return fss_.get(); }
1458
Seth Hampson2f0d7022018-02-20 11:54:42 -08001459 // Expects the provided number of new frames to be received within
1460 // kMaxWaitForFramesMs. The new expected frames are specified in
1461 // |media_expectations|. Returns false if any of the expectations were
1462 // not met.
1463 bool ExpectNewFrames(const MediaExpectations& media_expectations) {
1464 // First initialize the expected frame counts based upon the current
1465 // frame count.
1466 int total_caller_audio_frames_expected = caller()->audio_frames_received();
1467 if (media_expectations.caller_audio_expectation_ ==
1468 MediaExpectations::kExpectSomeFrames) {
1469 total_caller_audio_frames_expected +=
1470 media_expectations.caller_audio_frames_expected_;
1471 }
1472 int total_caller_video_frames_expected =
deadbeef1dcb1642017-03-29 21:08:16 -07001473 caller()->min_video_frames_received_per_track();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001474 if (media_expectations.caller_video_expectation_ ==
1475 MediaExpectations::kExpectSomeFrames) {
1476 total_caller_video_frames_expected +=
1477 media_expectations.caller_video_frames_expected_;
1478 }
1479 int total_callee_audio_frames_expected = callee()->audio_frames_received();
1480 if (media_expectations.callee_audio_expectation_ ==
1481 MediaExpectations::kExpectSomeFrames) {
1482 total_callee_audio_frames_expected +=
1483 media_expectations.callee_audio_frames_expected_;
1484 }
1485 int total_callee_video_frames_expected =
deadbeef1dcb1642017-03-29 21:08:16 -07001486 callee()->min_video_frames_received_per_track();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001487 if (media_expectations.callee_video_expectation_ ==
1488 MediaExpectations::kExpectSomeFrames) {
1489 total_callee_video_frames_expected +=
1490 media_expectations.callee_video_frames_expected_;
1491 }
deadbeef1dcb1642017-03-29 21:08:16 -07001492
Seth Hampson2f0d7022018-02-20 11:54:42 -08001493 // Wait for the expected frames.
deadbeef1dcb1642017-03-29 21:08:16 -07001494 EXPECT_TRUE_WAIT(caller()->audio_frames_received() >=
Seth Hampson2f0d7022018-02-20 11:54:42 -08001495 total_caller_audio_frames_expected &&
deadbeef1dcb1642017-03-29 21:08:16 -07001496 caller()->min_video_frames_received_per_track() >=
Seth Hampson2f0d7022018-02-20 11:54:42 -08001497 total_caller_video_frames_expected &&
deadbeef1dcb1642017-03-29 21:08:16 -07001498 callee()->audio_frames_received() >=
Seth Hampson2f0d7022018-02-20 11:54:42 -08001499 total_callee_audio_frames_expected &&
deadbeef1dcb1642017-03-29 21:08:16 -07001500 callee()->min_video_frames_received_per_track() >=
Seth Hampson2f0d7022018-02-20 11:54:42 -08001501 total_callee_video_frames_expected,
1502 kMaxWaitForFramesMs);
1503 bool expectations_correct =
1504 caller()->audio_frames_received() >=
1505 total_caller_audio_frames_expected &&
1506 caller()->min_video_frames_received_per_track() >=
1507 total_caller_video_frames_expected &&
1508 callee()->audio_frames_received() >=
1509 total_callee_audio_frames_expected &&
1510 callee()->min_video_frames_received_per_track() >=
1511 total_callee_video_frames_expected;
deadbeef1dcb1642017-03-29 21:08:16 -07001512
Seth Hampson2f0d7022018-02-20 11:54:42 -08001513 // After the combined wait, print out a more detailed message upon
1514 // failure.
deadbeef1dcb1642017-03-29 21:08:16 -07001515 EXPECT_GE(caller()->audio_frames_received(),
Seth Hampson2f0d7022018-02-20 11:54:42 -08001516 total_caller_audio_frames_expected);
deadbeef1dcb1642017-03-29 21:08:16 -07001517 EXPECT_GE(caller()->min_video_frames_received_per_track(),
Seth Hampson2f0d7022018-02-20 11:54:42 -08001518 total_caller_video_frames_expected);
deadbeef1dcb1642017-03-29 21:08:16 -07001519 EXPECT_GE(callee()->audio_frames_received(),
Seth Hampson2f0d7022018-02-20 11:54:42 -08001520 total_callee_audio_frames_expected);
deadbeef1dcb1642017-03-29 21:08:16 -07001521 EXPECT_GE(callee()->min_video_frames_received_per_track(),
Seth Hampson2f0d7022018-02-20 11:54:42 -08001522 total_callee_video_frames_expected);
1523
1524 // We want to make sure nothing unexpected was received.
1525 if (media_expectations.caller_audio_expectation_ ==
1526 MediaExpectations::kExpectNoFrames) {
1527 EXPECT_EQ(caller()->audio_frames_received(),
1528 total_caller_audio_frames_expected);
1529 if (caller()->audio_frames_received() !=
1530 total_caller_audio_frames_expected) {
1531 expectations_correct = false;
1532 }
1533 }
1534 if (media_expectations.caller_video_expectation_ ==
1535 MediaExpectations::kExpectNoFrames) {
1536 EXPECT_EQ(caller()->min_video_frames_received_per_track(),
1537 total_caller_video_frames_expected);
1538 if (caller()->min_video_frames_received_per_track() !=
1539 total_caller_video_frames_expected) {
1540 expectations_correct = false;
1541 }
1542 }
1543 if (media_expectations.callee_audio_expectation_ ==
1544 MediaExpectations::kExpectNoFrames) {
1545 EXPECT_EQ(callee()->audio_frames_received(),
1546 total_callee_audio_frames_expected);
1547 if (callee()->audio_frames_received() !=
1548 total_callee_audio_frames_expected) {
1549 expectations_correct = false;
1550 }
1551 }
1552 if (media_expectations.callee_video_expectation_ ==
1553 MediaExpectations::kExpectNoFrames) {
1554 EXPECT_EQ(callee()->min_video_frames_received_per_track(),
1555 total_callee_video_frames_expected);
1556 if (callee()->min_video_frames_received_per_track() !=
1557 total_callee_video_frames_expected) {
1558 expectations_correct = false;
1559 }
1560 }
1561 return expectations_correct;
deadbeef1dcb1642017-03-29 21:08:16 -07001562 }
1563
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07001564 void TestNegotiatedCipherSuite(
1565 const PeerConnectionFactory::Options& caller_options,
1566 const PeerConnectionFactory::Options& callee_options,
1567 int expected_cipher_suite) {
deadbeef1dcb1642017-03-29 21:08:16 -07001568 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(caller_options,
1569 callee_options));
deadbeef1dcb1642017-03-29 21:08:16 -07001570 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001571 caller()->AddAudioVideoTracks();
1572 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001573 caller()->CreateAndSetAndSignalOffer();
Qingsi Wang7fc821d2018-07-12 12:54:53 -07001574 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07001575 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(expected_cipher_suite),
deadbeefd8ad7882017-04-18 16:01:17 -07001576 caller()->OldGetStats()->SrtpCipher(), kDefaultTimeout);
Qingsi Wang7fc821d2018-07-12 12:54:53 -07001577 // TODO(bugs.webrtc.org/9456): Fix it.
Alex Loiko9289eda2018-11-23 16:18:59 +00001578 EXPECT_EQ(1, webrtc::metrics::NumEvents(
Qingsi Wang7fc821d2018-07-12 12:54:53 -07001579 "WebRTC.PeerConnection.SrtpCryptoSuite.Audio",
1580 expected_cipher_suite));
deadbeef1dcb1642017-03-29 21:08:16 -07001581 }
1582
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07001583 void TestGcmNegotiationUsesCipherSuite(bool local_gcm_enabled,
1584 bool remote_gcm_enabled,
1585 int expected_cipher_suite) {
1586 PeerConnectionFactory::Options caller_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07001587 caller_options.crypto_options.srtp.enable_gcm_crypto_suites =
1588 local_gcm_enabled;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07001589 PeerConnectionFactory::Options callee_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07001590 callee_options.crypto_options.srtp.enable_gcm_crypto_suites =
1591 remote_gcm_enabled;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07001592 TestNegotiatedCipherSuite(caller_options, callee_options,
1593 expected_cipher_suite);
1594 }
1595
Seth Hampson2f0d7022018-02-20 11:54:42 -08001596 protected:
Steve Anton3acffc32018-04-12 17:21:03 -07001597 SdpSemantics sdp_semantics_;
Seth Hampson2f0d7022018-02-20 11:54:42 -08001598
deadbeef1dcb1642017-03-29 21:08:16 -07001599 private:
1600 // |ss_| is used by |network_thread_| so it must be destroyed later.
deadbeef1dcb1642017-03-29 21:08:16 -07001601 std::unique_ptr<rtc::VirtualSocketServer> ss_;
Steve Antonede9ca52017-10-16 13:04:27 -07001602 std::unique_ptr<rtc::FirewallSocketServer> fss_;
deadbeef1dcb1642017-03-29 21:08:16 -07001603 // |network_thread_| and |worker_thread_| are used by both
1604 // |caller_| and |callee_| so they must be destroyed
1605 // later.
1606 std::unique_ptr<rtc::Thread> network_thread_;
1607 std::unique_ptr<rtc::Thread> worker_thread_;
Seth Hampsonaed71642018-06-11 07:41:32 -07001608 // The turn servers and turn customizers should be accessed & deleted on the
1609 // network thread to avoid a race with the socket read/write that occurs
1610 // on the network thread.
1611 std::vector<std::unique_ptr<cricket::TestTurnServer>> turn_servers_;
1612 std::vector<std::unique_ptr<cricket::TestTurnCustomizer>> turn_customizers_;
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001613 webrtc::MediaTransportPair loopback_media_transports_;
deadbeef1dcb1642017-03-29 21:08:16 -07001614 std::unique_ptr<PeerConnectionWrapper> caller_;
1615 std::unique_ptr<PeerConnectionWrapper> callee_;
1616};
1617
Seth Hampson2f0d7022018-02-20 11:54:42 -08001618class PeerConnectionIntegrationTest
1619 : public PeerConnectionIntegrationBaseTest,
1620 public ::testing::WithParamInterface<SdpSemantics> {
1621 protected:
1622 PeerConnectionIntegrationTest()
1623 : PeerConnectionIntegrationBaseTest(GetParam()) {}
1624};
1625
1626class PeerConnectionIntegrationTestPlanB
1627 : public PeerConnectionIntegrationBaseTest {
1628 protected:
1629 PeerConnectionIntegrationTestPlanB()
1630 : PeerConnectionIntegrationBaseTest(SdpSemantics::kPlanB) {}
1631};
1632
1633class PeerConnectionIntegrationTestUnifiedPlan
1634 : public PeerConnectionIntegrationBaseTest {
1635 protected:
1636 PeerConnectionIntegrationTestUnifiedPlan()
1637 : PeerConnectionIntegrationBaseTest(SdpSemantics::kUnifiedPlan) {}
1638};
1639
deadbeef1dcb1642017-03-29 21:08:16 -07001640// Test the OnFirstPacketReceived callback from audio/video RtpReceivers. This
1641// includes testing that the callback is invoked if an observer is connected
1642// after the first packet has already been received.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001643TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07001644 RtpReceiverObserverOnFirstPacketReceived) {
1645 ASSERT_TRUE(CreatePeerConnectionWrappers());
1646 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001647 caller()->AddAudioVideoTracks();
1648 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001649 // Start offer/answer exchange and wait for it to complete.
1650 caller()->CreateAndSetAndSignalOffer();
1651 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1652 // Should be one receiver each for audio/video.
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02001653 EXPECT_EQ(2U, caller()->rtp_receiver_observers().size());
1654 EXPECT_EQ(2U, callee()->rtp_receiver_observers().size());
deadbeef1dcb1642017-03-29 21:08:16 -07001655 // Wait for all "first packet received" callbacks to be fired.
1656 EXPECT_TRUE_WAIT(
Steve Anton64b626b2019-01-28 17:25:26 -08001657 absl::c_all_of(caller()->rtp_receiver_observers(),
1658 [](const std::unique_ptr<MockRtpReceiverObserver>& o) {
1659 return o->first_packet_received();
1660 }),
deadbeef1dcb1642017-03-29 21:08:16 -07001661 kMaxWaitForFramesMs);
1662 EXPECT_TRUE_WAIT(
Steve Anton64b626b2019-01-28 17:25:26 -08001663 absl::c_all_of(callee()->rtp_receiver_observers(),
1664 [](const std::unique_ptr<MockRtpReceiverObserver>& o) {
1665 return o->first_packet_received();
1666 }),
deadbeef1dcb1642017-03-29 21:08:16 -07001667 kMaxWaitForFramesMs);
1668 // If new observers are set after the first packet was already received, the
1669 // callback should still be invoked.
1670 caller()->ResetRtpReceiverObservers();
1671 callee()->ResetRtpReceiverObservers();
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02001672 EXPECT_EQ(2U, caller()->rtp_receiver_observers().size());
1673 EXPECT_EQ(2U, callee()->rtp_receiver_observers().size());
deadbeef1dcb1642017-03-29 21:08:16 -07001674 EXPECT_TRUE(
Steve Anton64b626b2019-01-28 17:25:26 -08001675 absl::c_all_of(caller()->rtp_receiver_observers(),
1676 [](const std::unique_ptr<MockRtpReceiverObserver>& o) {
1677 return o->first_packet_received();
1678 }));
deadbeef1dcb1642017-03-29 21:08:16 -07001679 EXPECT_TRUE(
Steve Anton64b626b2019-01-28 17:25:26 -08001680 absl::c_all_of(callee()->rtp_receiver_observers(),
1681 [](const std::unique_ptr<MockRtpReceiverObserver>& o) {
1682 return o->first_packet_received();
1683 }));
deadbeef1dcb1642017-03-29 21:08:16 -07001684}
1685
1686class DummyDtmfObserver : public DtmfSenderObserverInterface {
1687 public:
1688 DummyDtmfObserver() : completed_(false) {}
1689
1690 // Implements DtmfSenderObserverInterface.
1691 void OnToneChange(const std::string& tone) override {
1692 tones_.push_back(tone);
1693 if (tone.empty()) {
1694 completed_ = true;
1695 }
1696 }
1697
1698 const std::vector<std::string>& tones() const { return tones_; }
1699 bool completed() const { return completed_; }
1700
1701 private:
1702 bool completed_;
1703 std::vector<std::string> tones_;
1704};
1705
1706// Assumes |sender| already has an audio track added and the offer/answer
1707// exchange is done.
1708void TestDtmfFromSenderToReceiver(PeerConnectionWrapper* sender,
1709 PeerConnectionWrapper* receiver) {
Steve Anton15324772018-01-16 10:26:49 -08001710 // We should be able to get a DTMF sender from the local sender.
1711 rtc::scoped_refptr<DtmfSenderInterface> dtmf_sender =
1712 sender->pc()->GetSenders().at(0)->GetDtmfSender();
1713 ASSERT_TRUE(dtmf_sender);
deadbeef1dcb1642017-03-29 21:08:16 -07001714 DummyDtmfObserver observer;
deadbeef1dcb1642017-03-29 21:08:16 -07001715 dtmf_sender->RegisterObserver(&observer);
1716
1717 // Test the DtmfSender object just created.
1718 EXPECT_TRUE(dtmf_sender->CanInsertDtmf());
1719 EXPECT_TRUE(dtmf_sender->InsertDtmf("1a", 100, 50));
1720
1721 EXPECT_TRUE_WAIT(observer.completed(), kDefaultTimeout);
1722 std::vector<std::string> tones = {"1", "a", ""};
1723 EXPECT_EQ(tones, observer.tones());
1724 dtmf_sender->UnregisterObserver();
1725 // TODO(deadbeef): Verify the tones were actually received end-to-end.
1726}
1727
1728// Verifies the DtmfSenderObserver callbacks for a DtmfSender (one in each
1729// direction).
Seth Hampson2f0d7022018-02-20 11:54:42 -08001730TEST_P(PeerConnectionIntegrationTest, DtmfSenderObserver) {
deadbeef1dcb1642017-03-29 21:08:16 -07001731 ASSERT_TRUE(CreatePeerConnectionWrappers());
1732 ConnectFakeSignaling();
1733 // Only need audio for DTMF.
Steve Anton15324772018-01-16 10:26:49 -08001734 caller()->AddAudioTrack();
1735 callee()->AddAudioTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07001736 caller()->CreateAndSetAndSignalOffer();
1737 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
deadbeef71452802017-05-07 17:21:01 -07001738 // DTLS must finish before the DTMF sender can be used reliably.
1739 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07001740 TestDtmfFromSenderToReceiver(caller(), callee());
1741 TestDtmfFromSenderToReceiver(callee(), caller());
1742}
1743
1744// Basic end-to-end test, verifying media can be encoded/transmitted/decoded
1745// between two connections, using DTLS-SRTP.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001746TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithDtls) {
deadbeef1dcb1642017-03-29 21:08:16 -07001747 ASSERT_TRUE(CreatePeerConnectionWrappers());
1748 ConnectFakeSignaling();
Harald Alvestrand194939b2018-01-24 16:04:13 +01001749
deadbeef1dcb1642017-03-29 21:08:16 -07001750 // Do normal offer/answer and wait for some frames to be received in each
1751 // direction.
Steve Anton15324772018-01-16 10:26:49 -08001752 caller()->AddAudioVideoTracks();
1753 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001754 caller()->CreateAndSetAndSignalOffer();
1755 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001756 MediaExpectations media_expectations;
1757 media_expectations.ExpectBidirectionalAudioAndVideo();
1758 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Qingsi Wang7fc821d2018-07-12 12:54:53 -07001759 EXPECT_LE(2, webrtc::metrics::NumEvents("WebRTC.PeerConnection.KeyProtocol",
1760 webrtc::kEnumCounterKeyProtocolDtls));
1761 EXPECT_EQ(0, webrtc::metrics::NumEvents("WebRTC.PeerConnection.KeyProtocol",
1762 webrtc::kEnumCounterKeyProtocolSdes));
deadbeef1dcb1642017-03-29 21:08:16 -07001763}
1764
1765// Uses SDES instead of DTLS for key agreement.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001766TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithSdes) {
deadbeef1dcb1642017-03-29 21:08:16 -07001767 PeerConnectionInterface::RTCConfiguration sdes_config;
1768 sdes_config.enable_dtls_srtp.emplace(false);
1769 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(sdes_config, sdes_config));
1770 ConnectFakeSignaling();
1771
1772 // Do normal offer/answer and wait for some frames to be received in each
1773 // direction.
Steve Anton15324772018-01-16 10:26:49 -08001774 caller()->AddAudioVideoTracks();
1775 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001776 caller()->CreateAndSetAndSignalOffer();
1777 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001778 MediaExpectations media_expectations;
1779 media_expectations.ExpectBidirectionalAudioAndVideo();
1780 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Qingsi Wang7fc821d2018-07-12 12:54:53 -07001781 EXPECT_LE(2, webrtc::metrics::NumEvents("WebRTC.PeerConnection.KeyProtocol",
1782 webrtc::kEnumCounterKeyProtocolSdes));
1783 EXPECT_EQ(0, webrtc::metrics::NumEvents("WebRTC.PeerConnection.KeyProtocol",
1784 webrtc::kEnumCounterKeyProtocolDtls));
deadbeef1dcb1642017-03-29 21:08:16 -07001785}
1786
Steve Anton8c0f7a72017-10-03 10:03:10 -07001787// Tests that the GetRemoteAudioSSLCertificate method returns the remote DTLS
1788// certificate once the DTLS handshake has finished.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001789TEST_P(PeerConnectionIntegrationTest,
Steve Anton8c0f7a72017-10-03 10:03:10 -07001790 GetRemoteAudioSSLCertificateReturnsExchangedCertificate) {
1791 auto GetRemoteAudioSSLCertificate = [](PeerConnectionWrapper* wrapper) {
1792 auto pci = reinterpret_cast<PeerConnectionProxy*>(wrapper->pc());
1793 auto pc = reinterpret_cast<PeerConnection*>(pci->internal());
1794 return pc->GetRemoteAudioSSLCertificate();
1795 };
Zhi Huang70b820f2018-01-27 14:16:15 -08001796 auto GetRemoteAudioSSLCertChain = [](PeerConnectionWrapper* wrapper) {
1797 auto pci = reinterpret_cast<PeerConnectionProxy*>(wrapper->pc());
1798 auto pc = reinterpret_cast<PeerConnection*>(pci->internal());
1799 return pc->GetRemoteAudioSSLCertChain();
1800 };
Steve Anton8c0f7a72017-10-03 10:03:10 -07001801
1802 auto caller_cert = rtc::RTCCertificate::FromPEM(kRsaPems[0]);
1803 auto callee_cert = rtc::RTCCertificate::FromPEM(kRsaPems[1]);
1804
1805 // Configure each side with a known certificate so they can be compared later.
1806 PeerConnectionInterface::RTCConfiguration caller_config;
1807 caller_config.enable_dtls_srtp.emplace(true);
1808 caller_config.certificates.push_back(caller_cert);
1809 PeerConnectionInterface::RTCConfiguration callee_config;
1810 callee_config.enable_dtls_srtp.emplace(true);
1811 callee_config.certificates.push_back(callee_cert);
1812 ASSERT_TRUE(
1813 CreatePeerConnectionWrappersWithConfig(caller_config, callee_config));
1814 ConnectFakeSignaling();
1815
1816 // When first initialized, there should not be a remote SSL certificate (and
1817 // calling this method should not crash).
1818 EXPECT_EQ(nullptr, GetRemoteAudioSSLCertificate(caller()));
1819 EXPECT_EQ(nullptr, GetRemoteAudioSSLCertificate(callee()));
Zhi Huang70b820f2018-01-27 14:16:15 -08001820 EXPECT_EQ(nullptr, GetRemoteAudioSSLCertChain(caller()));
1821 EXPECT_EQ(nullptr, GetRemoteAudioSSLCertChain(callee()));
Steve Anton8c0f7a72017-10-03 10:03:10 -07001822
Steve Anton15324772018-01-16 10:26:49 -08001823 caller()->AddAudioTrack();
1824 callee()->AddAudioTrack();
Steve Anton8c0f7a72017-10-03 10:03:10 -07001825 caller()->CreateAndSetAndSignalOffer();
1826 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1827 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
1828
1829 // Once DTLS has been connected, each side should return the other's SSL
1830 // certificate when calling GetRemoteAudioSSLCertificate.
1831
1832 auto caller_remote_cert = GetRemoteAudioSSLCertificate(caller());
1833 ASSERT_TRUE(caller_remote_cert);
Benjamin Wright6c6c9df2018-10-25 01:16:26 -07001834 EXPECT_EQ(callee_cert->GetSSLCertificate().ToPEMString(),
Steve Anton8c0f7a72017-10-03 10:03:10 -07001835 caller_remote_cert->ToPEMString());
1836
1837 auto callee_remote_cert = GetRemoteAudioSSLCertificate(callee());
1838 ASSERT_TRUE(callee_remote_cert);
Benjamin Wright6c6c9df2018-10-25 01:16:26 -07001839 EXPECT_EQ(caller_cert->GetSSLCertificate().ToPEMString(),
Steve Anton8c0f7a72017-10-03 10:03:10 -07001840 callee_remote_cert->ToPEMString());
Zhi Huang70b820f2018-01-27 14:16:15 -08001841
1842 auto caller_remote_cert_chain = GetRemoteAudioSSLCertChain(caller());
1843 ASSERT_TRUE(caller_remote_cert_chain);
1844 ASSERT_EQ(1U, caller_remote_cert_chain->GetSize());
1845 auto remote_cert = &caller_remote_cert_chain->Get(0);
Benjamin Wright6c6c9df2018-10-25 01:16:26 -07001846 EXPECT_EQ(callee_cert->GetSSLCertificate().ToPEMString(),
Zhi Huang70b820f2018-01-27 14:16:15 -08001847 remote_cert->ToPEMString());
1848
1849 auto callee_remote_cert_chain = GetRemoteAudioSSLCertChain(callee());
1850 ASSERT_TRUE(callee_remote_cert_chain);
1851 ASSERT_EQ(1U, callee_remote_cert_chain->GetSize());
1852 remote_cert = &callee_remote_cert_chain->Get(0);
Benjamin Wright6c6c9df2018-10-25 01:16:26 -07001853 EXPECT_EQ(caller_cert->GetSSLCertificate().ToPEMString(),
Zhi Huang70b820f2018-01-27 14:16:15 -08001854 remote_cert->ToPEMString());
Steve Anton8c0f7a72017-10-03 10:03:10 -07001855}
1856
deadbeef1dcb1642017-03-29 21:08:16 -07001857// This test sets up a call between two parties with a source resolution of
1858// 1280x720 and verifies that a 16:9 aspect ratio is received.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001859TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07001860 Send1280By720ResolutionAndReceive16To9AspectRatio) {
1861 ASSERT_TRUE(CreatePeerConnectionWrappers());
1862 ConnectFakeSignaling();
1863
Niels Möller5c7efe72018-05-11 10:34:46 +02001864 // Add video tracks with 16:9 aspect ratio, size 1280 x 720.
1865 webrtc::FakePeriodicVideoSource::Config config;
1866 config.width = 1280;
1867 config.height = 720;
Johannes Kron965e7942018-09-13 15:36:20 +02001868 config.timestamp_offset_ms = rtc::TimeMillis();
Niels Möller5c7efe72018-05-11 10:34:46 +02001869 caller()->AddTrack(caller()->CreateLocalVideoTrackWithConfig(config));
1870 callee()->AddTrack(callee()->CreateLocalVideoTrackWithConfig(config));
deadbeef1dcb1642017-03-29 21:08:16 -07001871
1872 // Do normal offer/answer and wait for at least one frame to be received in
1873 // each direction.
1874 caller()->CreateAndSetAndSignalOffer();
1875 ASSERT_TRUE_WAIT(caller()->min_video_frames_received_per_track() > 0 &&
1876 callee()->min_video_frames_received_per_track() > 0,
1877 kMaxWaitForFramesMs);
1878
1879 // Check rendered aspect ratio.
1880 EXPECT_EQ(16.0 / 9, caller()->local_rendered_aspect_ratio());
1881 EXPECT_EQ(16.0 / 9, caller()->rendered_aspect_ratio());
1882 EXPECT_EQ(16.0 / 9, callee()->local_rendered_aspect_ratio());
1883 EXPECT_EQ(16.0 / 9, callee()->rendered_aspect_ratio());
1884}
1885
1886// This test sets up an one-way call, with media only from caller to
1887// callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001888TEST_P(PeerConnectionIntegrationTest, OneWayMediaCall) {
deadbeef1dcb1642017-03-29 21:08:16 -07001889 ASSERT_TRUE(CreatePeerConnectionWrappers());
1890 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001891 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001892 caller()->CreateAndSetAndSignalOffer();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001893 MediaExpectations media_expectations;
1894 media_expectations.CalleeExpectsSomeAudioAndVideo();
1895 media_expectations.CallerExpectsNoAudio();
1896 media_expectations.CallerExpectsNoVideo();
1897 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07001898}
1899
1900// This test sets up a audio call initially, with the callee rejecting video
1901// initially. Then later the callee decides to upgrade to audio/video, and
1902// initiates a new offer/answer exchange.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001903TEST_P(PeerConnectionIntegrationTest, AudioToVideoUpgrade) {
deadbeef1dcb1642017-03-29 21:08:16 -07001904 ASSERT_TRUE(CreatePeerConnectionWrappers());
1905 ConnectFakeSignaling();
1906 // Initially, offer an audio/video stream from the caller, but refuse to
1907 // send/receive video on the callee side.
Steve Anton15324772018-01-16 10:26:49 -08001908 caller()->AddAudioVideoTracks();
1909 callee()->AddAudioTrack();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001910 if (sdp_semantics_ == SdpSemantics::kPlanB) {
1911 PeerConnectionInterface::RTCOfferAnswerOptions options;
1912 options.offer_to_receive_video = 0;
1913 callee()->SetOfferAnswerOptions(options);
1914 } else {
1915 callee()->SetRemoteOfferHandler([this] {
1916 callee()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)->Stop();
1917 });
1918 }
deadbeef1dcb1642017-03-29 21:08:16 -07001919 // Do offer/answer and make sure audio is still received end-to-end.
1920 caller()->CreateAndSetAndSignalOffer();
1921 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001922 {
1923 MediaExpectations media_expectations;
1924 media_expectations.ExpectBidirectionalAudio();
1925 media_expectations.ExpectNoVideo();
1926 ASSERT_TRUE(ExpectNewFrames(media_expectations));
1927 }
deadbeef1dcb1642017-03-29 21:08:16 -07001928 // Sanity check that the callee's description has a rejected video section.
1929 ASSERT_NE(nullptr, callee()->pc()->local_description());
1930 const ContentInfo* callee_video_content =
1931 GetFirstVideoContent(callee()->pc()->local_description()->description());
1932 ASSERT_NE(nullptr, callee_video_content);
1933 EXPECT_TRUE(callee_video_content->rejected);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001934
deadbeef1dcb1642017-03-29 21:08:16 -07001935 // Now negotiate with video and ensure negotiation succeeds, with video
1936 // frames and additional audio frames being received.
Steve Anton15324772018-01-16 10:26:49 -08001937 callee()->AddVideoTrack();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001938 if (sdp_semantics_ == SdpSemantics::kPlanB) {
1939 PeerConnectionInterface::RTCOfferAnswerOptions options;
1940 options.offer_to_receive_video = 1;
1941 callee()->SetOfferAnswerOptions(options);
1942 } else {
1943 callee()->SetRemoteOfferHandler(nullptr);
1944 caller()->SetRemoteOfferHandler([this] {
1945 // The caller creates a new transceiver to receive video on when receiving
1946 // the offer, but by default it is send only.
1947 auto transceivers = caller()->pc()->GetTransceivers();
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02001948 ASSERT_EQ(3U, transceivers.size());
Seth Hampson2f0d7022018-02-20 11:54:42 -08001949 ASSERT_EQ(cricket::MEDIA_TYPE_VIDEO,
1950 transceivers[2]->receiver()->media_type());
1951 transceivers[2]->sender()->SetTrack(caller()->CreateLocalVideoTrack());
1952 transceivers[2]->SetDirection(RtpTransceiverDirection::kSendRecv);
1953 });
1954 }
deadbeef1dcb1642017-03-29 21:08:16 -07001955 callee()->CreateAndSetAndSignalOffer();
1956 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001957 {
1958 // Expect additional audio frames to be received after the upgrade.
1959 MediaExpectations media_expectations;
1960 media_expectations.ExpectBidirectionalAudioAndVideo();
1961 ASSERT_TRUE(ExpectNewFrames(media_expectations));
1962 }
deadbeef1dcb1642017-03-29 21:08:16 -07001963}
1964
deadbeef4389b4d2017-09-07 09:07:36 -07001965// Simpler than the above test; just add an audio track to an established
1966// video-only connection.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001967TEST_P(PeerConnectionIntegrationTest, AddAudioToVideoOnlyCall) {
deadbeef4389b4d2017-09-07 09:07:36 -07001968 ASSERT_TRUE(CreatePeerConnectionWrappers());
1969 ConnectFakeSignaling();
1970 // Do initial offer/answer with just a video track.
Steve Anton15324772018-01-16 10:26:49 -08001971 caller()->AddVideoTrack();
1972 callee()->AddVideoTrack();
deadbeef4389b4d2017-09-07 09:07:36 -07001973 caller()->CreateAndSetAndSignalOffer();
1974 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1975 // Now add an audio track and do another offer/answer.
Steve Anton15324772018-01-16 10:26:49 -08001976 caller()->AddAudioTrack();
1977 callee()->AddAudioTrack();
deadbeef4389b4d2017-09-07 09:07:36 -07001978 caller()->CreateAndSetAndSignalOffer();
1979 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1980 // Ensure both audio and video frames are received end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001981 MediaExpectations media_expectations;
1982 media_expectations.ExpectBidirectionalAudioAndVideo();
1983 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef4389b4d2017-09-07 09:07:36 -07001984}
1985
deadbeef1dcb1642017-03-29 21:08:16 -07001986// This test sets up a call that's transferred to a new caller with a different
1987// DTLS fingerprint.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001988TEST_P(PeerConnectionIntegrationTest, CallTransferredForCallee) {
deadbeef1dcb1642017-03-29 21:08:16 -07001989 ASSERT_TRUE(CreatePeerConnectionWrappers());
1990 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001991 caller()->AddAudioVideoTracks();
1992 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001993 caller()->CreateAndSetAndSignalOffer();
1994 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1995
1996 // Keep the original peer around which will still send packets to the
1997 // receiving client. These SRTP packets will be dropped.
1998 std::unique_ptr<PeerConnectionWrapper> original_peer(
1999 SetCallerPcWrapperAndReturnCurrent(
Seth Hampson2f0d7022018-02-20 11:54:42 -08002000 CreatePeerConnectionWrapperWithAlternateKey().release()));
deadbeef1dcb1642017-03-29 21:08:16 -07002001 // TODO(deadbeef): Why do we call Close here? That goes against the comment
2002 // directly above.
2003 original_peer->pc()->Close();
2004
2005 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002006 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002007 caller()->CreateAndSetAndSignalOffer();
2008 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2009 // Wait for some additional frames to be transmitted end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002010 MediaExpectations media_expectations;
2011 media_expectations.ExpectBidirectionalAudioAndVideo();
2012 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002013}
2014
2015// This test sets up a call that's transferred to a new callee with a different
2016// DTLS fingerprint.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002017TEST_P(PeerConnectionIntegrationTest, CallTransferredForCaller) {
deadbeef1dcb1642017-03-29 21:08:16 -07002018 ASSERT_TRUE(CreatePeerConnectionWrappers());
2019 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002020 caller()->AddAudioVideoTracks();
2021 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002022 caller()->CreateAndSetAndSignalOffer();
2023 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2024
2025 // Keep the original peer around which will still send packets to the
2026 // receiving client. These SRTP packets will be dropped.
2027 std::unique_ptr<PeerConnectionWrapper> original_peer(
2028 SetCalleePcWrapperAndReturnCurrent(
Seth Hampson2f0d7022018-02-20 11:54:42 -08002029 CreatePeerConnectionWrapperWithAlternateKey().release()));
deadbeef1dcb1642017-03-29 21:08:16 -07002030 // TODO(deadbeef): Why do we call Close here? That goes against the comment
2031 // directly above.
2032 original_peer->pc()->Close();
2033
2034 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002035 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002036 caller()->SetOfferAnswerOptions(IceRestartOfferAnswerOptions());
2037 caller()->CreateAndSetAndSignalOffer();
2038 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2039 // Wait for some additional frames to be transmitted end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002040 MediaExpectations media_expectations;
2041 media_expectations.ExpectBidirectionalAudioAndVideo();
2042 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002043}
2044
2045// This test sets up a non-bundled call and negotiates bundling at the same
2046// time as starting an ICE restart. When bundling is in effect in the restart,
2047// the DTLS-SRTP context should be successfully reset.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002048TEST_P(PeerConnectionIntegrationTest, BundlingEnabledWhileIceRestartOccurs) {
deadbeef1dcb1642017-03-29 21:08:16 -07002049 ASSERT_TRUE(CreatePeerConnectionWrappers());
2050 ConnectFakeSignaling();
2051
Steve Anton15324772018-01-16 10:26:49 -08002052 caller()->AddAudioVideoTracks();
2053 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002054 // Remove the bundle group from the SDP received by the callee.
2055 callee()->SetReceivedSdpMunger([](cricket::SessionDescription* desc) {
2056 desc->RemoveGroupByName("BUNDLE");
2057 });
2058 caller()->CreateAndSetAndSignalOffer();
2059 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002060 {
2061 MediaExpectations media_expectations;
2062 media_expectations.ExpectBidirectionalAudioAndVideo();
2063 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2064 }
deadbeef1dcb1642017-03-29 21:08:16 -07002065 // Now stop removing the BUNDLE group, and trigger an ICE restart.
2066 callee()->SetReceivedSdpMunger(nullptr);
2067 caller()->SetOfferAnswerOptions(IceRestartOfferAnswerOptions());
2068 caller()->CreateAndSetAndSignalOffer();
2069 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2070
2071 // Expect additional frames to be received after the ICE restart.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002072 {
2073 MediaExpectations media_expectations;
2074 media_expectations.ExpectBidirectionalAudioAndVideo();
2075 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2076 }
deadbeef1dcb1642017-03-29 21:08:16 -07002077}
2078
2079// Test CVO (Coordination of Video Orientation). If a video source is rotated
2080// and both peers support the CVO RTP header extension, the actual video frames
2081// don't need to be encoded in different resolutions, since the rotation is
2082// communicated through the RTP header extension.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002083TEST_P(PeerConnectionIntegrationTest, RotatedVideoWithCVOExtension) {
deadbeef1dcb1642017-03-29 21:08:16 -07002084 ASSERT_TRUE(CreatePeerConnectionWrappers());
2085 ConnectFakeSignaling();
2086 // Add rotated video tracks.
Steve Anton15324772018-01-16 10:26:49 -08002087 caller()->AddTrack(
deadbeef1dcb1642017-03-29 21:08:16 -07002088 caller()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_90));
Steve Anton15324772018-01-16 10:26:49 -08002089 callee()->AddTrack(
deadbeef1dcb1642017-03-29 21:08:16 -07002090 callee()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_270));
2091
2092 // Wait for video frames to be received by both sides.
2093 caller()->CreateAndSetAndSignalOffer();
2094 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2095 ASSERT_TRUE_WAIT(caller()->min_video_frames_received_per_track() > 0 &&
2096 callee()->min_video_frames_received_per_track() > 0,
2097 kMaxWaitForFramesMs);
2098
2099 // Ensure that the aspect ratio is unmodified.
2100 // TODO(deadbeef): Where does 4:3 come from? Should be explicit in the test,
2101 // not just assumed.
2102 EXPECT_EQ(4.0 / 3, caller()->local_rendered_aspect_ratio());
2103 EXPECT_EQ(4.0 / 3, caller()->rendered_aspect_ratio());
2104 EXPECT_EQ(4.0 / 3, callee()->local_rendered_aspect_ratio());
2105 EXPECT_EQ(4.0 / 3, callee()->rendered_aspect_ratio());
2106 // Ensure that the CVO bits were surfaced to the renderer.
2107 EXPECT_EQ(webrtc::kVideoRotation_270, caller()->rendered_rotation());
2108 EXPECT_EQ(webrtc::kVideoRotation_90, callee()->rendered_rotation());
2109}
2110
2111// Test that when the CVO extension isn't supported, video is rotated the
2112// old-fashioned way, by encoding rotated frames.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002113TEST_P(PeerConnectionIntegrationTest, RotatedVideoWithoutCVOExtension) {
deadbeef1dcb1642017-03-29 21:08:16 -07002114 ASSERT_TRUE(CreatePeerConnectionWrappers());
2115 ConnectFakeSignaling();
2116 // Add rotated video tracks.
Steve Anton15324772018-01-16 10:26:49 -08002117 caller()->AddTrack(
deadbeef1dcb1642017-03-29 21:08:16 -07002118 caller()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_90));
Steve Anton15324772018-01-16 10:26:49 -08002119 callee()->AddTrack(
deadbeef1dcb1642017-03-29 21:08:16 -07002120 callee()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_270));
2121
2122 // Remove the CVO extension from the offered SDP.
2123 callee()->SetReceivedSdpMunger([](cricket::SessionDescription* desc) {
2124 cricket::VideoContentDescription* video =
2125 GetFirstVideoContentDescription(desc);
2126 video->ClearRtpHeaderExtensions();
2127 });
2128 // Wait for video frames to be received by both sides.
2129 caller()->CreateAndSetAndSignalOffer();
2130 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2131 ASSERT_TRUE_WAIT(caller()->min_video_frames_received_per_track() > 0 &&
2132 callee()->min_video_frames_received_per_track() > 0,
2133 kMaxWaitForFramesMs);
2134
2135 // Expect that the aspect ratio is inversed to account for the 90/270 degree
2136 // rotation.
2137 // TODO(deadbeef): Where does 4:3 come from? Should be explicit in the test,
2138 // not just assumed.
2139 EXPECT_EQ(3.0 / 4, caller()->local_rendered_aspect_ratio());
2140 EXPECT_EQ(3.0 / 4, caller()->rendered_aspect_ratio());
2141 EXPECT_EQ(3.0 / 4, callee()->local_rendered_aspect_ratio());
2142 EXPECT_EQ(3.0 / 4, callee()->rendered_aspect_ratio());
2143 // Expect that each endpoint is unaware of the rotation of the other endpoint.
2144 EXPECT_EQ(webrtc::kVideoRotation_0, caller()->rendered_rotation());
2145 EXPECT_EQ(webrtc::kVideoRotation_0, callee()->rendered_rotation());
2146}
2147
deadbeef1dcb1642017-03-29 21:08:16 -07002148// Test that if the answerer rejects the audio m= section, no audio is sent or
2149// received, but video still can be.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002150TEST_P(PeerConnectionIntegrationTest, AnswererRejectsAudioSection) {
deadbeef1dcb1642017-03-29 21:08:16 -07002151 ASSERT_TRUE(CreatePeerConnectionWrappers());
2152 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002153 caller()->AddAudioVideoTracks();
Seth Hampson2f0d7022018-02-20 11:54:42 -08002154 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2155 // Only add video track for callee, and set offer_to_receive_audio to 0, so
2156 // it will reject the audio m= section completely.
2157 PeerConnectionInterface::RTCOfferAnswerOptions options;
2158 options.offer_to_receive_audio = 0;
2159 callee()->SetOfferAnswerOptions(options);
2160 } else {
2161 // Stopping the audio RtpTransceiver will cause the media section to be
2162 // rejected in the answer.
2163 callee()->SetRemoteOfferHandler([this] {
2164 callee()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_AUDIO)->Stop();
2165 });
2166 }
Steve Anton15324772018-01-16 10:26:49 -08002167 callee()->AddTrack(callee()->CreateLocalVideoTrack());
deadbeef1dcb1642017-03-29 21:08:16 -07002168 // Do offer/answer and wait for successful end-to-end video frames.
2169 caller()->CreateAndSetAndSignalOffer();
2170 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002171 MediaExpectations media_expectations;
2172 media_expectations.ExpectBidirectionalVideo();
2173 media_expectations.ExpectNoAudio();
2174 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2175
deadbeef1dcb1642017-03-29 21:08:16 -07002176 // Sanity check that the callee's description has a rejected audio section.
2177 ASSERT_NE(nullptr, callee()->pc()->local_description());
2178 const ContentInfo* callee_audio_content =
2179 GetFirstAudioContent(callee()->pc()->local_description()->description());
2180 ASSERT_NE(nullptr, callee_audio_content);
2181 EXPECT_TRUE(callee_audio_content->rejected);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002182 if (sdp_semantics_ == SdpSemantics::kUnifiedPlan) {
2183 // The caller's transceiver should have stopped after receiving the answer.
2184 EXPECT_TRUE(caller()
2185 ->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_AUDIO)
2186 ->stopped());
2187 }
deadbeef1dcb1642017-03-29 21:08:16 -07002188}
2189
2190// Test that if the answerer rejects the video m= section, no video is sent or
2191// received, but audio still can be.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002192TEST_P(PeerConnectionIntegrationTest, AnswererRejectsVideoSection) {
deadbeef1dcb1642017-03-29 21:08:16 -07002193 ASSERT_TRUE(CreatePeerConnectionWrappers());
2194 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002195 caller()->AddAudioVideoTracks();
Seth Hampson2f0d7022018-02-20 11:54:42 -08002196 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2197 // Only add audio track for callee, and set offer_to_receive_video to 0, so
2198 // it will reject the video m= section completely.
2199 PeerConnectionInterface::RTCOfferAnswerOptions options;
2200 options.offer_to_receive_video = 0;
2201 callee()->SetOfferAnswerOptions(options);
2202 } else {
2203 // Stopping the video RtpTransceiver will cause the media section to be
2204 // rejected in the answer.
2205 callee()->SetRemoteOfferHandler([this] {
2206 callee()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)->Stop();
2207 });
2208 }
Steve Anton15324772018-01-16 10:26:49 -08002209 callee()->AddTrack(callee()->CreateLocalAudioTrack());
deadbeef1dcb1642017-03-29 21:08:16 -07002210 // Do offer/answer and wait for successful end-to-end audio frames.
2211 caller()->CreateAndSetAndSignalOffer();
2212 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002213 MediaExpectations media_expectations;
2214 media_expectations.ExpectBidirectionalAudio();
2215 media_expectations.ExpectNoVideo();
2216 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2217
deadbeef1dcb1642017-03-29 21:08:16 -07002218 // Sanity check that the callee's description has a rejected video section.
2219 ASSERT_NE(nullptr, callee()->pc()->local_description());
2220 const ContentInfo* callee_video_content =
2221 GetFirstVideoContent(callee()->pc()->local_description()->description());
2222 ASSERT_NE(nullptr, callee_video_content);
2223 EXPECT_TRUE(callee_video_content->rejected);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002224 if (sdp_semantics_ == SdpSemantics::kUnifiedPlan) {
2225 // The caller's transceiver should have stopped after receiving the answer.
2226 EXPECT_TRUE(caller()
2227 ->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)
2228 ->stopped());
2229 }
deadbeef1dcb1642017-03-29 21:08:16 -07002230}
2231
2232// Test that if the answerer rejects both audio and video m= sections, nothing
2233// bad happens.
2234// TODO(deadbeef): Test that a data channel still works. Currently this doesn't
2235// test anything but the fact that negotiation succeeds, which doesn't mean
2236// much.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002237TEST_P(PeerConnectionIntegrationTest, AnswererRejectsAudioAndVideoSections) {
deadbeef1dcb1642017-03-29 21:08:16 -07002238 ASSERT_TRUE(CreatePeerConnectionWrappers());
2239 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002240 caller()->AddAudioVideoTracks();
Seth Hampson2f0d7022018-02-20 11:54:42 -08002241 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2242 // Don't give the callee any tracks, and set offer_to_receive_X to 0, so it
2243 // will reject both audio and video m= sections.
2244 PeerConnectionInterface::RTCOfferAnswerOptions options;
2245 options.offer_to_receive_audio = 0;
2246 options.offer_to_receive_video = 0;
2247 callee()->SetOfferAnswerOptions(options);
2248 } else {
2249 callee()->SetRemoteOfferHandler([this] {
2250 // Stopping all transceivers will cause all media sections to be rejected.
Mirko Bonadei739baf02019-01-27 17:29:42 +01002251 for (const auto& transceiver : callee()->pc()->GetTransceivers()) {
Seth Hampson2f0d7022018-02-20 11:54:42 -08002252 transceiver->Stop();
2253 }
2254 });
2255 }
deadbeef1dcb1642017-03-29 21:08:16 -07002256 // Do offer/answer and wait for stable signaling state.
2257 caller()->CreateAndSetAndSignalOffer();
2258 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002259
deadbeef1dcb1642017-03-29 21:08:16 -07002260 // Sanity check that the callee's description has rejected m= sections.
2261 ASSERT_NE(nullptr, callee()->pc()->local_description());
2262 const ContentInfo* callee_audio_content =
2263 GetFirstAudioContent(callee()->pc()->local_description()->description());
2264 ASSERT_NE(nullptr, callee_audio_content);
2265 EXPECT_TRUE(callee_audio_content->rejected);
2266 const ContentInfo* callee_video_content =
2267 GetFirstVideoContent(callee()->pc()->local_description()->description());
2268 ASSERT_NE(nullptr, callee_video_content);
2269 EXPECT_TRUE(callee_video_content->rejected);
2270}
2271
2272// This test sets up an audio and video call between two parties. After the
2273// call runs for a while, the caller sends an updated offer with video being
2274// rejected. Once the re-negotiation is done, the video flow should stop and
2275// the audio flow should continue.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002276TEST_P(PeerConnectionIntegrationTest, VideoRejectedInSubsequentOffer) {
deadbeef1dcb1642017-03-29 21:08:16 -07002277 ASSERT_TRUE(CreatePeerConnectionWrappers());
2278 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002279 caller()->AddAudioVideoTracks();
2280 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002281 caller()->CreateAndSetAndSignalOffer();
2282 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002283 {
2284 MediaExpectations media_expectations;
2285 media_expectations.ExpectBidirectionalAudioAndVideo();
2286 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2287 }
deadbeef1dcb1642017-03-29 21:08:16 -07002288 // Renegotiate, rejecting the video m= section.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002289 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2290 caller()->SetGeneratedSdpMunger(
2291 [](cricket::SessionDescription* description) {
2292 for (cricket::ContentInfo& content : description->contents()) {
2293 if (cricket::IsVideoContent(&content)) {
2294 content.rejected = true;
2295 }
2296 }
2297 });
2298 } else {
2299 caller()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)->Stop();
2300 }
deadbeef1dcb1642017-03-29 21:08:16 -07002301 caller()->CreateAndSetAndSignalOffer();
2302 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
2303
2304 // Sanity check that the caller's description has a rejected video section.
2305 ASSERT_NE(nullptr, caller()->pc()->local_description());
2306 const ContentInfo* caller_video_content =
2307 GetFirstVideoContent(caller()->pc()->local_description()->description());
2308 ASSERT_NE(nullptr, caller_video_content);
2309 EXPECT_TRUE(caller_video_content->rejected);
deadbeef1dcb1642017-03-29 21:08:16 -07002310 // Wait for some additional audio frames to be received.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002311 {
2312 MediaExpectations media_expectations;
2313 media_expectations.ExpectBidirectionalAudio();
2314 media_expectations.ExpectNoVideo();
2315 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2316 }
deadbeef1dcb1642017-03-29 21:08:16 -07002317}
2318
Taylor Brandstetter60c8dc82018-04-11 15:20:27 -07002319// Do one offer/answer with audio, another that disables it (rejecting the m=
2320// section), and another that re-enables it. Regression test for:
2321// bugs.webrtc.org/6023
2322TEST_F(PeerConnectionIntegrationTestPlanB, EnableAudioAfterRejecting) {
2323 ASSERT_TRUE(CreatePeerConnectionWrappers());
2324 ConnectFakeSignaling();
2325
2326 // Add audio track, do normal offer/answer.
2327 rtc::scoped_refptr<webrtc::AudioTrackInterface> track =
2328 caller()->CreateLocalAudioTrack();
2329 rtc::scoped_refptr<webrtc::RtpSenderInterface> sender =
2330 caller()->pc()->AddTrack(track, {"stream"}).MoveValue();
2331 caller()->CreateAndSetAndSignalOffer();
2332 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2333
2334 // Remove audio track, and set offer_to_receive_audio to false to cause the
2335 // m= section to be completely disabled, not just "recvonly".
2336 caller()->pc()->RemoveTrack(sender);
2337 PeerConnectionInterface::RTCOfferAnswerOptions options;
2338 options.offer_to_receive_audio = 0;
2339 caller()->SetOfferAnswerOptions(options);
2340 caller()->CreateAndSetAndSignalOffer();
2341 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2342
2343 // Add the audio track again, expecting negotiation to succeed and frames to
2344 // flow.
2345 sender = caller()->pc()->AddTrack(track, {"stream"}).MoveValue();
2346 options.offer_to_receive_audio = 1;
2347 caller()->SetOfferAnswerOptions(options);
2348 caller()->CreateAndSetAndSignalOffer();
2349 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2350
2351 MediaExpectations media_expectations;
2352 media_expectations.CalleeExpectsSomeAudio();
2353 EXPECT_TRUE(ExpectNewFrames(media_expectations));
2354}
2355
deadbeef1dcb1642017-03-29 21:08:16 -07002356// Basic end-to-end test, but without SSRC/MSID signaling. This functionality
2357// is needed to support legacy endpoints.
2358// TODO(deadbeef): When we support the MID extension and demuxing on MID, also
2359// add a test for an end-to-end test without MID signaling either (basically,
2360// the minimum acceptable SDP).
Seth Hampson2f0d7022018-02-20 11:54:42 -08002361TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithoutSsrcOrMsidSignaling) {
deadbeef1dcb1642017-03-29 21:08:16 -07002362 ASSERT_TRUE(CreatePeerConnectionWrappers());
2363 ConnectFakeSignaling();
2364 // Add audio and video, testing that packets can be demuxed on payload type.
Steve Anton15324772018-01-16 10:26:49 -08002365 caller()->AddAudioVideoTracks();
2366 callee()->AddAudioVideoTracks();
deadbeefd8ad7882017-04-18 16:01:17 -07002367 // Remove SSRCs and MSIDs from the received offer SDP.
2368 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
deadbeef1dcb1642017-03-29 21:08:16 -07002369 caller()->CreateAndSetAndSignalOffer();
2370 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002371 MediaExpectations media_expectations;
2372 media_expectations.ExpectBidirectionalAudioAndVideo();
2373 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002374}
2375
Seth Hampson5897a6e2018-04-03 11:16:33 -07002376// Basic end-to-end test, without SSRC signaling. This means that the track
2377// was created properly and frames are delivered when the MSIDs are communicated
2378// with a=msid lines and no a=ssrc lines.
2379TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
2380 EndToEndCallWithoutSsrcSignaling) {
2381 const char kStreamId[] = "streamId";
2382 ASSERT_TRUE(CreatePeerConnectionWrappers());
2383 ConnectFakeSignaling();
2384 // Add just audio tracks.
2385 caller()->AddTrack(caller()->CreateLocalAudioTrack(), {kStreamId});
2386 callee()->AddAudioTrack();
2387
2388 // Remove SSRCs from the received offer SDP.
2389 callee()->SetReceivedSdpMunger(RemoveSsrcsAndKeepMsids);
2390 caller()->CreateAndSetAndSignalOffer();
2391 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2392 MediaExpectations media_expectations;
2393 media_expectations.ExpectBidirectionalAudio();
2394 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2395}
2396
Steve Antondf527fd2018-04-27 15:52:03 -07002397// Tests that video flows between multiple video tracks when SSRCs are not
2398// signaled. This exercises the MID RTP header extension which is needed to
2399// demux the incoming video tracks.
2400TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
2401 EndToEndCallWithTwoVideoTracksAndNoSignaledSsrc) {
2402 ASSERT_TRUE(CreatePeerConnectionWrappers());
2403 ConnectFakeSignaling();
2404 caller()->AddVideoTrack();
2405 caller()->AddVideoTrack();
2406 callee()->AddVideoTrack();
2407 callee()->AddVideoTrack();
2408
2409 caller()->SetReceivedSdpMunger(&RemoveSsrcsAndKeepMsids);
2410 callee()->SetReceivedSdpMunger(&RemoveSsrcsAndKeepMsids);
2411 caller()->CreateAndSetAndSignalOffer();
2412 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2413 ASSERT_EQ(2u, caller()->pc()->GetReceivers().size());
2414 ASSERT_EQ(2u, callee()->pc()->GetReceivers().size());
2415
2416 // Expect video to be received in both directions on both tracks.
2417 MediaExpectations media_expectations;
2418 media_expectations.ExpectBidirectionalVideo();
2419 EXPECT_TRUE(ExpectNewFrames(media_expectations));
2420}
2421
Henrik Boström5b147782018-12-04 11:25:05 +01002422TEST_F(PeerConnectionIntegrationTestUnifiedPlan, NoStreamsMsidLinePresent) {
2423 ASSERT_TRUE(CreatePeerConnectionWrappers());
2424 ConnectFakeSignaling();
2425 caller()->AddAudioTrack();
2426 caller()->AddVideoTrack();
2427 caller()->CreateAndSetAndSignalOffer();
2428 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2429 auto callee_receivers = callee()->pc()->GetReceivers();
2430 ASSERT_EQ(2u, callee_receivers.size());
2431 EXPECT_TRUE(callee_receivers[0]->stream_ids().empty());
2432 EXPECT_TRUE(callee_receivers[1]->stream_ids().empty());
2433}
2434
2435TEST_F(PeerConnectionIntegrationTestUnifiedPlan, NoStreamsMsidLineMissing) {
2436 ASSERT_TRUE(CreatePeerConnectionWrappers());
2437 ConnectFakeSignaling();
2438 caller()->AddAudioTrack();
2439 caller()->AddVideoTrack();
2440 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
2441 caller()->CreateAndSetAndSignalOffer();
2442 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2443 auto callee_receivers = callee()->pc()->GetReceivers();
2444 ASSERT_EQ(2u, callee_receivers.size());
2445 ASSERT_EQ(1u, callee_receivers[0]->stream_ids().size());
2446 ASSERT_EQ(1u, callee_receivers[1]->stream_ids().size());
2447 EXPECT_EQ(callee_receivers[0]->stream_ids()[0],
2448 callee_receivers[1]->stream_ids()[0]);
2449 EXPECT_EQ(callee_receivers[0]->streams()[0],
2450 callee_receivers[1]->streams()[0]);
2451}
2452
deadbeef1dcb1642017-03-29 21:08:16 -07002453// Test that if two video tracks are sent (from caller to callee, in this test),
2454// they're transmitted correctly end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002455TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithTwoVideoTracks) {
deadbeef1dcb1642017-03-29 21:08:16 -07002456 ASSERT_TRUE(CreatePeerConnectionWrappers());
2457 ConnectFakeSignaling();
2458 // Add one audio/video stream, and one video-only stream.
Steve Anton15324772018-01-16 10:26:49 -08002459 caller()->AddAudioVideoTracks();
2460 caller()->AddVideoTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07002461 caller()->CreateAndSetAndSignalOffer();
2462 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Steve Anton15324772018-01-16 10:26:49 -08002463 ASSERT_EQ(3u, callee()->pc()->GetReceivers().size());
Seth Hampson2f0d7022018-02-20 11:54:42 -08002464
2465 MediaExpectations media_expectations;
2466 media_expectations.CalleeExpectsSomeAudioAndVideo();
2467 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002468}
2469
2470static void MakeSpecCompliantMaxBundleOffer(cricket::SessionDescription* desc) {
2471 bool first = true;
2472 for (cricket::ContentInfo& content : desc->contents()) {
2473 if (first) {
2474 first = false;
2475 continue;
2476 }
2477 content.bundle_only = true;
2478 }
2479 first = true;
2480 for (cricket::TransportInfo& transport : desc->transport_infos()) {
2481 if (first) {
2482 first = false;
2483 continue;
2484 }
2485 transport.description.ice_ufrag.clear();
2486 transport.description.ice_pwd.clear();
2487 transport.description.connection_role = cricket::CONNECTIONROLE_NONE;
2488 transport.description.identity_fingerprint.reset(nullptr);
2489 }
2490}
2491
2492// Test that if applying a true "max bundle" offer, which uses ports of 0,
2493// "a=bundle-only", omitting "a=fingerprint", "a=setup", "a=ice-ufrag" and
2494// "a=ice-pwd" for all but the audio "m=" section, negotiation still completes
2495// successfully and media flows.
2496// TODO(deadbeef): Update this test to also omit "a=rtcp-mux", once that works.
2497// TODO(deadbeef): Won't need this test once we start generating actual
2498// standards-compliant SDP.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002499TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07002500 EndToEndCallWithSpecCompliantMaxBundleOffer) {
2501 ASSERT_TRUE(CreatePeerConnectionWrappers());
2502 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002503 caller()->AddAudioVideoTracks();
2504 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002505 // Do the equivalent of setting the port to 0, adding a=bundle-only, and
2506 // removing a=ice-ufrag, a=ice-pwd, a=fingerprint and a=setup from all
2507 // but the first m= section.
2508 callee()->SetReceivedSdpMunger(MakeSpecCompliantMaxBundleOffer);
2509 caller()->CreateAndSetAndSignalOffer();
2510 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002511 MediaExpectations media_expectations;
2512 media_expectations.ExpectBidirectionalAudioAndVideo();
2513 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002514}
2515
2516// Test that we can receive the audio output level from a remote audio track.
2517// TODO(deadbeef): Use a fake audio source and verify that the output level is
2518// exactly what the source on the other side was configured with.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002519TEST_P(PeerConnectionIntegrationTest, GetAudioOutputLevelStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-29 21:08:16 -07002520 ASSERT_TRUE(CreatePeerConnectionWrappers());
2521 ConnectFakeSignaling();
2522 // Just add an audio track.
Steve Anton15324772018-01-16 10:26:49 -08002523 caller()->AddAudioTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07002524 caller()->CreateAndSetAndSignalOffer();
2525 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2526
2527 // Get the audio output level stats. Note that the level is not available
2528 // until an RTCP packet has been received.
deadbeefd8ad7882017-04-18 16:01:17 -07002529 EXPECT_TRUE_WAIT(callee()->OldGetStats()->AudioOutputLevel() > 0,
deadbeef1dcb1642017-03-29 21:08:16 -07002530 kMaxWaitForFramesMs);
2531}
2532
2533// Test that an audio input level is reported.
2534// TODO(deadbeef): Use a fake audio source and verify that the input level is
2535// exactly what the source was configured with.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002536TEST_P(PeerConnectionIntegrationTest, GetAudioInputLevelStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-29 21:08:16 -07002537 ASSERT_TRUE(CreatePeerConnectionWrappers());
2538 ConnectFakeSignaling();
2539 // Just add an audio track.
Steve Anton15324772018-01-16 10:26:49 -08002540 caller()->AddAudioTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07002541 caller()->CreateAndSetAndSignalOffer();
2542 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2543
2544 // Get the audio input level stats. The level should be available very
2545 // soon after the test starts.
deadbeefd8ad7882017-04-18 16:01:17 -07002546 EXPECT_TRUE_WAIT(caller()->OldGetStats()->AudioInputLevel() > 0,
deadbeef1dcb1642017-03-29 21:08:16 -07002547 kMaxWaitForStatsMs);
2548}
2549
2550// Test that we can get incoming byte counts from both audio and video tracks.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002551TEST_P(PeerConnectionIntegrationTest, GetBytesReceivedStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-29 21:08:16 -07002552 ASSERT_TRUE(CreatePeerConnectionWrappers());
2553 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002554 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002555 // Do offer/answer, wait for the callee to receive some frames.
2556 caller()->CreateAndSetAndSignalOffer();
2557 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002558
2559 MediaExpectations media_expectations;
2560 media_expectations.CalleeExpectsSomeAudioAndVideo();
2561 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002562
2563 // Get a handle to the remote tracks created, so they can be used as GetStats
2564 // filters.
Mirko Bonadei739baf02019-01-27 17:29:42 +01002565 for (const auto& receiver : callee()->pc()->GetReceivers()) {
Steve Anton15324772018-01-16 10:26:49 -08002566 // We received frames, so we definitely should have nonzero "received bytes"
2567 // stats at this point.
2568 EXPECT_GT(callee()->OldGetStatsForTrack(receiver->track())->BytesReceived(),
2569 0);
2570 }
deadbeef1dcb1642017-03-29 21:08:16 -07002571}
2572
2573// Test that we can get outgoing byte counts from both audio and video tracks.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002574TEST_P(PeerConnectionIntegrationTest, GetBytesSentStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-29 21:08:16 -07002575 ASSERT_TRUE(CreatePeerConnectionWrappers());
2576 ConnectFakeSignaling();
2577 auto audio_track = caller()->CreateLocalAudioTrack();
2578 auto video_track = caller()->CreateLocalVideoTrack();
Steve Anton15324772018-01-16 10:26:49 -08002579 caller()->AddTrack(audio_track);
2580 caller()->AddTrack(video_track);
deadbeef1dcb1642017-03-29 21:08:16 -07002581 // Do offer/answer, wait for the callee to receive some frames.
2582 caller()->CreateAndSetAndSignalOffer();
2583 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002584 MediaExpectations media_expectations;
2585 media_expectations.CalleeExpectsSomeAudioAndVideo();
2586 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002587
2588 // The callee received frames, so we definitely should have nonzero "sent
2589 // bytes" stats at this point.
deadbeefd8ad7882017-04-18 16:01:17 -07002590 EXPECT_GT(caller()->OldGetStatsForTrack(audio_track)->BytesSent(), 0);
2591 EXPECT_GT(caller()->OldGetStatsForTrack(video_track)->BytesSent(), 0);
2592}
2593
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002594// Test that we can get capture start ntp time.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002595TEST_P(PeerConnectionIntegrationTest, GetCaptureStartNtpTimeWithOldStatsApi) {
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002596 ASSERT_TRUE(CreatePeerConnectionWrappers());
2597 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002598 caller()->AddAudioTrack();
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002599
Steve Anton15324772018-01-16 10:26:49 -08002600 callee()->AddAudioTrack();
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002601
2602 // Do offer/answer, wait for the callee to receive some frames.
2603 caller()->CreateAndSetAndSignalOffer();
2604 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2605
2606 // Get the remote audio track created on the receiver, so they can be used as
2607 // GetStats filters.
Steve Antonfc853712018-03-01 13:48:58 -08002608 auto receivers = callee()->pc()->GetReceivers();
2609 ASSERT_EQ(1u, receivers.size());
2610 auto remote_audio_track = receivers[0]->track();
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002611
2612 // Get the audio output level stats. Note that the level is not available
2613 // until an RTCP packet has been received.
Zhi Huange830e682018-03-30 10:48:35 -07002614 EXPECT_TRUE_WAIT(
2615 callee()->OldGetStatsForTrack(remote_audio_track)->CaptureStartNtpTime() >
2616 0,
2617 2 * kMaxWaitForFramesMs);
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002618}
2619
Steve Antona41959e2018-11-28 11:15:33 -08002620// Test that the track ID is associated with all local and remote SSRC stats
2621// using the old GetStats() and more than 1 audio and more than 1 video track.
2622// This is a regression test for crbug.com/906988
2623TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
2624 OldGetStatsAssociatesTrackIdForManyMediaSections) {
2625 ASSERT_TRUE(CreatePeerConnectionWrappers());
2626 ConnectFakeSignaling();
2627 auto audio_sender_1 = caller()->AddAudioTrack();
2628 auto video_sender_1 = caller()->AddVideoTrack();
2629 auto audio_sender_2 = caller()->AddAudioTrack();
2630 auto video_sender_2 = caller()->AddVideoTrack();
2631 caller()->CreateAndSetAndSignalOffer();
2632 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2633
2634 MediaExpectations media_expectations;
2635 media_expectations.CalleeExpectsSomeAudioAndVideo();
2636 ASSERT_TRUE_WAIT(ExpectNewFrames(media_expectations), kDefaultTimeout);
2637
2638 std::vector<std::string> track_ids = {
2639 audio_sender_1->track()->id(), video_sender_1->track()->id(),
2640 audio_sender_2->track()->id(), video_sender_2->track()->id()};
2641
2642 auto caller_stats = caller()->OldGetStats();
2643 EXPECT_THAT(caller_stats->TrackIds(), UnorderedElementsAreArray(track_ids));
2644 auto callee_stats = callee()->OldGetStats();
2645 EXPECT_THAT(callee_stats->TrackIds(), UnorderedElementsAreArray(track_ids));
2646}
2647
Steve Antonffa6ce42018-11-30 09:26:08 -08002648// Test that the new GetStats() returns stats for all outgoing/incoming streams
2649// with the correct track IDs if there are more than one audio and more than one
2650// video senders/receivers.
2651TEST_P(PeerConnectionIntegrationTest, NewGetStatsManyAudioAndManyVideoStreams) {
2652 ASSERT_TRUE(CreatePeerConnectionWrappers());
2653 ConnectFakeSignaling();
2654 auto audio_sender_1 = caller()->AddAudioTrack();
2655 auto video_sender_1 = caller()->AddVideoTrack();
2656 auto audio_sender_2 = caller()->AddAudioTrack();
2657 auto video_sender_2 = caller()->AddVideoTrack();
2658 caller()->CreateAndSetAndSignalOffer();
2659 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2660
2661 MediaExpectations media_expectations;
2662 media_expectations.CalleeExpectsSomeAudioAndVideo();
2663 ASSERT_TRUE_WAIT(ExpectNewFrames(media_expectations), kDefaultTimeout);
2664
2665 std::vector<std::string> track_ids = {
2666 audio_sender_1->track()->id(), video_sender_1->track()->id(),
2667 audio_sender_2->track()->id(), video_sender_2->track()->id()};
2668
2669 rtc::scoped_refptr<const webrtc::RTCStatsReport> caller_report =
2670 caller()->NewGetStats();
2671 ASSERT_TRUE(caller_report);
2672 auto outbound_stream_stats =
2673 caller_report->GetStatsOfType<webrtc::RTCOutboundRTPStreamStats>();
2674 ASSERT_EQ(4u, outbound_stream_stats.size());
2675 std::vector<std::string> outbound_track_ids;
2676 for (const auto& stat : outbound_stream_stats) {
2677 ASSERT_TRUE(stat->bytes_sent.is_defined());
2678 EXPECT_LT(0u, *stat->bytes_sent);
2679 ASSERT_TRUE(stat->track_id.is_defined());
2680 const auto* track_stat =
2681 caller_report->GetAs<webrtc::RTCMediaStreamTrackStats>(*stat->track_id);
2682 ASSERT_TRUE(track_stat);
2683 outbound_track_ids.push_back(*track_stat->track_identifier);
2684 }
2685 EXPECT_THAT(outbound_track_ids, UnorderedElementsAreArray(track_ids));
2686
2687 rtc::scoped_refptr<const webrtc::RTCStatsReport> callee_report =
2688 callee()->NewGetStats();
2689 ASSERT_TRUE(callee_report);
2690 auto inbound_stream_stats =
2691 callee_report->GetStatsOfType<webrtc::RTCInboundRTPStreamStats>();
2692 ASSERT_EQ(4u, inbound_stream_stats.size());
2693 std::vector<std::string> inbound_track_ids;
2694 for (const auto& stat : inbound_stream_stats) {
2695 ASSERT_TRUE(stat->bytes_received.is_defined());
2696 EXPECT_LT(0u, *stat->bytes_received);
2697 ASSERT_TRUE(stat->track_id.is_defined());
2698 const auto* track_stat =
2699 callee_report->GetAs<webrtc::RTCMediaStreamTrackStats>(*stat->track_id);
2700 ASSERT_TRUE(track_stat);
2701 inbound_track_ids.push_back(*track_stat->track_identifier);
2702 }
2703 EXPECT_THAT(inbound_track_ids, UnorderedElementsAreArray(track_ids));
2704}
2705
2706// Test that we can get stats (using the new stats implementation) for
deadbeefd8ad7882017-04-18 16:01:17 -07002707// unsignaled streams. Meaning when SSRCs/MSIDs aren't signaled explicitly in
2708// SDP.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002709TEST_P(PeerConnectionIntegrationTest,
deadbeefd8ad7882017-04-18 16:01:17 -07002710 GetStatsForUnsignaledStreamWithNewStatsApi) {
2711 ASSERT_TRUE(CreatePeerConnectionWrappers());
2712 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002713 caller()->AddAudioTrack();
deadbeefd8ad7882017-04-18 16:01:17 -07002714 // Remove SSRCs and MSIDs from the received offer SDP.
2715 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
2716 caller()->CreateAndSetAndSignalOffer();
2717 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002718 MediaExpectations media_expectations;
2719 media_expectations.CalleeExpectsSomeAudio(1);
2720 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeefd8ad7882017-04-18 16:01:17 -07002721
2722 // We received a frame, so we should have nonzero "bytes received" stats for
2723 // the unsignaled stream, if stats are working for it.
2724 rtc::scoped_refptr<const webrtc::RTCStatsReport> report =
2725 callee()->NewGetStats();
2726 ASSERT_NE(nullptr, report);
2727 auto inbound_stream_stats =
2728 report->GetStatsOfType<webrtc::RTCInboundRTPStreamStats>();
2729 ASSERT_EQ(1U, inbound_stream_stats.size());
2730 ASSERT_TRUE(inbound_stream_stats[0]->bytes_received.is_defined());
2731 ASSERT_GT(*inbound_stream_stats[0]->bytes_received, 0U);
zhihuangf8164932017-05-19 13:09:47 -07002732 ASSERT_TRUE(inbound_stream_stats[0]->track_id.is_defined());
2733}
2734
Taylor Brandstettera4653442018-06-19 09:44:26 -07002735// Same as above but for the legacy stats implementation.
2736TEST_P(PeerConnectionIntegrationTest,
2737 GetStatsForUnsignaledStreamWithOldStatsApi) {
2738 ASSERT_TRUE(CreatePeerConnectionWrappers());
2739 ConnectFakeSignaling();
2740 caller()->AddAudioTrack();
2741 // Remove SSRCs and MSIDs from the received offer SDP.
2742 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
2743 caller()->CreateAndSetAndSignalOffer();
2744 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2745
2746 // Note that, since the old stats implementation associates SSRCs with tracks
2747 // using SDP, when SSRCs aren't signaled in SDP these stats won't have an
2748 // associated track ID. So we can't use the track "selector" argument.
2749 //
2750 // Also, we use "EXPECT_TRUE_WAIT" because the stats collector may decide to
2751 // return cached stats if not enough time has passed since the last update.
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02002752 EXPECT_TRUE_WAIT(callee()->OldGetStats()->BytesReceived() > 0,
Taylor Brandstettera4653442018-06-19 09:44:26 -07002753 kDefaultTimeout);
2754}
2755
zhihuangf8164932017-05-19 13:09:47 -07002756// Test that we can successfully get the media related stats (audio level
2757// etc.) for the unsignaled stream.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002758TEST_P(PeerConnectionIntegrationTest,
zhihuangf8164932017-05-19 13:09:47 -07002759 GetMediaStatsForUnsignaledStreamWithNewStatsApi) {
2760 ASSERT_TRUE(CreatePeerConnectionWrappers());
2761 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002762 caller()->AddAudioVideoTracks();
zhihuangf8164932017-05-19 13:09:47 -07002763 // Remove SSRCs and MSIDs from the received offer SDP.
2764 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
2765 caller()->CreateAndSetAndSignalOffer();
2766 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002767 MediaExpectations media_expectations;
2768 media_expectations.CalleeExpectsSomeAudio(1);
2769 media_expectations.CalleeExpectsSomeVideo(1);
2770 ASSERT_TRUE(ExpectNewFrames(media_expectations));
zhihuangf8164932017-05-19 13:09:47 -07002771
2772 rtc::scoped_refptr<const webrtc::RTCStatsReport> report =
2773 callee()->NewGetStats();
2774 ASSERT_NE(nullptr, report);
2775
2776 auto media_stats = report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
2777 auto audio_index = FindFirstMediaStatsIndexByKind("audio", media_stats);
2778 ASSERT_GE(audio_index, 0);
2779 EXPECT_TRUE(media_stats[audio_index]->audio_level.is_defined());
deadbeef1dcb1642017-03-29 21:08:16 -07002780}
2781
deadbeef4e2deab2017-09-20 13:56:21 -07002782// Helper for test below.
2783void ModifySsrcs(cricket::SessionDescription* desc) {
2784 for (ContentInfo& content : desc->contents()) {
Steve Antondf527fd2018-04-27 15:52:03 -07002785 for (StreamParams& stream :
Steve Antonb1c1de12017-12-21 15:14:30 -08002786 content.media_description()->mutable_streams()) {
deadbeef4e2deab2017-09-20 13:56:21 -07002787 for (uint32_t& ssrc : stream.ssrcs) {
2788 ssrc = rtc::CreateRandomId();
2789 }
2790 }
2791 }
2792}
2793
2794// Test that the "RTCMediaSteamTrackStats" object is updated correctly when
2795// SSRCs are unsignaled, and the SSRC of the received (audio) stream changes.
2796// This should result in two "RTCInboundRTPStreamStats", but only one
2797// "RTCMediaStreamTrackStats", whose counters go up continuously rather than
2798// being reset to 0 once the SSRC change occurs.
2799//
2800// Regression test for this bug:
2801// https://bugs.chromium.org/p/webrtc/issues/detail?id=8158
2802//
2803// The bug causes the track stats to only represent one of the two streams:
2804// whichever one has the higher SSRC. So with this bug, there was a 50% chance
2805// that the track stat counters would reset to 0 when the new stream is
2806// received, and a 50% chance that they'll stop updating (while
2807// "concealed_samples" continues increasing, due to silence being generated for
2808// the inactive stream).
Seth Hampson2f0d7022018-02-20 11:54:42 -08002809TEST_P(PeerConnectionIntegrationTest,
Steve Anton83119dd2017-11-10 16:19:52 -08002810 TrackStatsUpdatedCorrectlyWhenUnsignaledSsrcChanges) {
deadbeef4e2deab2017-09-20 13:56:21 -07002811 ASSERT_TRUE(CreatePeerConnectionWrappers());
2812 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002813 caller()->AddAudioTrack();
deadbeef4e2deab2017-09-20 13:56:21 -07002814 // Remove SSRCs and MSIDs from the received offer SDP, simulating an endpoint
2815 // that doesn't signal SSRCs (from the callee's perspective).
2816 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
2817 caller()->CreateAndSetAndSignalOffer();
2818 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2819 // Wait for 50 audio frames (500ms of audio) to be received by the callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002820 {
2821 MediaExpectations media_expectations;
2822 media_expectations.CalleeExpectsSomeAudio(50);
2823 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2824 }
deadbeef4e2deab2017-09-20 13:56:21 -07002825 // Some audio frames were received, so we should have nonzero "samples
2826 // received" for the track.
2827 rtc::scoped_refptr<const webrtc::RTCStatsReport> report =
2828 callee()->NewGetStats();
2829 ASSERT_NE(nullptr, report);
2830 auto track_stats = report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
2831 ASSERT_EQ(1U, track_stats.size());
2832 ASSERT_TRUE(track_stats[0]->total_samples_received.is_defined());
2833 ASSERT_GT(*track_stats[0]->total_samples_received, 0U);
2834 // uint64_t prev_samples_received = *track_stats[0]->total_samples_received;
2835
2836 // Create a new offer and munge it to cause the caller to use a new SSRC.
2837 caller()->SetGeneratedSdpMunger(ModifySsrcs);
2838 caller()->CreateAndSetAndSignalOffer();
2839 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2840 // Wait for 25 more audio frames (250ms of audio) to be received, from the new
2841 // SSRC.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002842 {
2843 MediaExpectations media_expectations;
2844 media_expectations.CalleeExpectsSomeAudio(25);
2845 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2846 }
deadbeef4e2deab2017-09-20 13:56:21 -07002847
2848 report = callee()->NewGetStats();
2849 ASSERT_NE(nullptr, report);
2850 track_stats = report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
2851 ASSERT_EQ(1U, track_stats.size());
2852 ASSERT_TRUE(track_stats[0]->total_samples_received.is_defined());
2853 // The "total samples received" stat should only be greater than it was
2854 // before.
2855 // TODO(deadbeef): Uncomment this assertion once the bug is completely fixed.
2856 // Right now, the new SSRC will cause the counters to reset to 0.
2857 // EXPECT_GT(*track_stats[0]->total_samples_received, prev_samples_received);
2858
2859 // Additionally, the percentage of concealed samples (samples generated to
Steve Anton83119dd2017-11-10 16:19:52 -08002860 // conceal packet loss) should be less than 50%. If it's greater, that's a
deadbeef4e2deab2017-09-20 13:56:21 -07002861 // good sign that we're seeing stats from the old stream that's no longer
2862 // receiving packets, and is generating concealed samples of silence.
Steve Anton83119dd2017-11-10 16:19:52 -08002863 constexpr double kAcceptableConcealedSamplesPercentage = 0.50;
deadbeef4e2deab2017-09-20 13:56:21 -07002864 ASSERT_TRUE(track_stats[0]->concealed_samples.is_defined());
2865 EXPECT_LT(*track_stats[0]->concealed_samples,
2866 *track_stats[0]->total_samples_received *
2867 kAcceptableConcealedSamplesPercentage);
2868
2869 // Also ensure that we have two "RTCInboundRTPStreamStats" as expected, as a
2870 // sanity check that the SSRC really changed.
2871 // TODO(deadbeef): This isn't working right now, because we're not returning
2872 // *any* stats for the inactive stream. Uncomment when the bug is completely
2873 // fixed.
2874 // auto inbound_stream_stats =
2875 // report->GetStatsOfType<webrtc::RTCInboundRTPStreamStats>();
2876 // ASSERT_EQ(2U, inbound_stream_stats.size());
2877}
2878
deadbeef1dcb1642017-03-29 21:08:16 -07002879// Test that DTLS 1.0 is used if both sides only support DTLS 1.0.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002880TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithDtls10) {
deadbeef1dcb1642017-03-29 21:08:16 -07002881 PeerConnectionFactory::Options dtls_10_options;
2882 dtls_10_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
2883 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(dtls_10_options,
2884 dtls_10_options));
2885 ConnectFakeSignaling();
2886 // Do normal offer/answer and wait for some frames to be received in each
2887 // direction.
Steve Anton15324772018-01-16 10:26:49 -08002888 caller()->AddAudioVideoTracks();
2889 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002890 caller()->CreateAndSetAndSignalOffer();
2891 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002892 MediaExpectations media_expectations;
2893 media_expectations.ExpectBidirectionalAudioAndVideo();
2894 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002895}
2896
2897// Test getting cipher stats and UMA metrics when DTLS 1.0 is negotiated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002898TEST_P(PeerConnectionIntegrationTest, Dtls10CipherStatsAndUmaMetrics) {
deadbeef1dcb1642017-03-29 21:08:16 -07002899 PeerConnectionFactory::Options dtls_10_options;
2900 dtls_10_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
2901 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(dtls_10_options,
2902 dtls_10_options));
2903 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002904 caller()->AddAudioVideoTracks();
2905 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002906 caller()->CreateAndSetAndSignalOffer();
Qingsi Wang7fc821d2018-07-12 12:54:53 -07002907 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07002908 EXPECT_TRUE_WAIT(rtc::SSLStreamAdapter::IsAcceptableCipher(
deadbeefd8ad7882017-04-18 16:01:17 -07002909 caller()->OldGetStats()->DtlsCipher(), rtc::KT_DEFAULT),
deadbeef1dcb1642017-03-29 21:08:16 -07002910 kDefaultTimeout);
2911 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
deadbeefd8ad7882017-04-18 16:01:17 -07002912 caller()->OldGetStats()->SrtpCipher(), kDefaultTimeout);
Qingsi Wang7fc821d2018-07-12 12:54:53 -07002913 // TODO(bugs.webrtc.org/9456): Fix it.
Alex Loiko9289eda2018-11-23 16:18:59 +00002914 EXPECT_EQ(1, webrtc::metrics::NumEvents(
Qingsi Wang7fc821d2018-07-12 12:54:53 -07002915 "WebRTC.PeerConnection.SrtpCryptoSuite.Audio",
2916 kDefaultSrtpCryptoSuite));
deadbeef1dcb1642017-03-29 21:08:16 -07002917}
2918
2919// Test getting cipher stats and UMA metrics when DTLS 1.2 is negotiated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002920TEST_P(PeerConnectionIntegrationTest, Dtls12CipherStatsAndUmaMetrics) {
deadbeef1dcb1642017-03-29 21:08:16 -07002921 PeerConnectionFactory::Options dtls_12_options;
2922 dtls_12_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
2923 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(dtls_12_options,
2924 dtls_12_options));
2925 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002926 caller()->AddAudioVideoTracks();
2927 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002928 caller()->CreateAndSetAndSignalOffer();
Qingsi Wang7fc821d2018-07-12 12:54:53 -07002929 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07002930 EXPECT_TRUE_WAIT(rtc::SSLStreamAdapter::IsAcceptableCipher(
deadbeefd8ad7882017-04-18 16:01:17 -07002931 caller()->OldGetStats()->DtlsCipher(), rtc::KT_DEFAULT),
deadbeef1dcb1642017-03-29 21:08:16 -07002932 kDefaultTimeout);
2933 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
deadbeefd8ad7882017-04-18 16:01:17 -07002934 caller()->OldGetStats()->SrtpCipher(), kDefaultTimeout);
Qingsi Wang7fc821d2018-07-12 12:54:53 -07002935 // TODO(bugs.webrtc.org/9456): Fix it.
Alex Loiko9289eda2018-11-23 16:18:59 +00002936 EXPECT_EQ(1, webrtc::metrics::NumEvents(
Qingsi Wang7fc821d2018-07-12 12:54:53 -07002937 "WebRTC.PeerConnection.SrtpCryptoSuite.Audio",
2938 kDefaultSrtpCryptoSuite));
deadbeef1dcb1642017-03-29 21:08:16 -07002939}
2940
2941// Test that DTLS 1.0 can be used if the caller supports DTLS 1.2 and the
2942// callee only supports 1.0.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002943TEST_P(PeerConnectionIntegrationTest, CallerDtls12ToCalleeDtls10) {
deadbeef1dcb1642017-03-29 21:08:16 -07002944 PeerConnectionFactory::Options caller_options;
2945 caller_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
2946 PeerConnectionFactory::Options callee_options;
2947 callee_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
2948 ASSERT_TRUE(
2949 CreatePeerConnectionWrappersWithOptions(caller_options, callee_options));
2950 ConnectFakeSignaling();
2951 // Do normal offer/answer and wait for some frames to be received in each
2952 // direction.
Steve Anton15324772018-01-16 10:26:49 -08002953 caller()->AddAudioVideoTracks();
2954 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002955 caller()->CreateAndSetAndSignalOffer();
2956 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002957 MediaExpectations media_expectations;
2958 media_expectations.ExpectBidirectionalAudioAndVideo();
2959 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002960}
2961
2962// Test that DTLS 1.0 can be used if the caller only supports DTLS 1.0 and the
2963// callee supports 1.2.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002964TEST_P(PeerConnectionIntegrationTest, CallerDtls10ToCalleeDtls12) {
deadbeef1dcb1642017-03-29 21:08:16 -07002965 PeerConnectionFactory::Options caller_options;
2966 caller_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
2967 PeerConnectionFactory::Options callee_options;
2968 callee_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
2969 ASSERT_TRUE(
2970 CreatePeerConnectionWrappersWithOptions(caller_options, callee_options));
2971 ConnectFakeSignaling();
2972 // Do normal offer/answer and wait for some frames to be received in each
2973 // direction.
Steve Anton15324772018-01-16 10:26:49 -08002974 caller()->AddAudioVideoTracks();
2975 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002976 caller()->CreateAndSetAndSignalOffer();
2977 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002978 MediaExpectations media_expectations;
2979 media_expectations.ExpectBidirectionalAudioAndVideo();
2980 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002981}
2982
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07002983// The three tests below verify that "enable_aes128_sha1_32_crypto_cipher"
2984// works as expected; the cipher should only be used if enabled by both sides.
2985TEST_P(PeerConnectionIntegrationTest,
2986 Aes128Sha1_32_CipherNotUsedWhenOnlyCallerSupported) {
2987 PeerConnectionFactory::Options caller_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07002988 caller_options.crypto_options.srtp.enable_aes128_sha1_32_crypto_cipher = true;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07002989 PeerConnectionFactory::Options callee_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07002990 callee_options.crypto_options.srtp.enable_aes128_sha1_32_crypto_cipher =
2991 false;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07002992 int expected_cipher_suite = rtc::SRTP_AES128_CM_SHA1_80;
2993 TestNegotiatedCipherSuite(caller_options, callee_options,
2994 expected_cipher_suite);
2995}
2996
2997TEST_P(PeerConnectionIntegrationTest,
2998 Aes128Sha1_32_CipherNotUsedWhenOnlyCalleeSupported) {
2999 PeerConnectionFactory::Options caller_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07003000 caller_options.crypto_options.srtp.enable_aes128_sha1_32_crypto_cipher =
3001 false;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07003002 PeerConnectionFactory::Options callee_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07003003 callee_options.crypto_options.srtp.enable_aes128_sha1_32_crypto_cipher = true;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07003004 int expected_cipher_suite = rtc::SRTP_AES128_CM_SHA1_80;
3005 TestNegotiatedCipherSuite(caller_options, callee_options,
3006 expected_cipher_suite);
3007}
3008
3009TEST_P(PeerConnectionIntegrationTest, Aes128Sha1_32_CipherUsedWhenSupported) {
3010 PeerConnectionFactory::Options caller_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07003011 caller_options.crypto_options.srtp.enable_aes128_sha1_32_crypto_cipher = true;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07003012 PeerConnectionFactory::Options callee_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07003013 callee_options.crypto_options.srtp.enable_aes128_sha1_32_crypto_cipher = true;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07003014 int expected_cipher_suite = rtc::SRTP_AES128_CM_SHA1_32;
3015 TestNegotiatedCipherSuite(caller_options, callee_options,
3016 expected_cipher_suite);
3017}
3018
deadbeef1dcb1642017-03-29 21:08:16 -07003019// Test that a non-GCM cipher is used if both sides only support non-GCM.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003020TEST_P(PeerConnectionIntegrationTest, NonGcmCipherUsedWhenGcmNotSupported) {
deadbeef1dcb1642017-03-29 21:08:16 -07003021 bool local_gcm_enabled = false;
3022 bool remote_gcm_enabled = false;
3023 int expected_cipher_suite = kDefaultSrtpCryptoSuite;
3024 TestGcmNegotiationUsesCipherSuite(local_gcm_enabled, remote_gcm_enabled,
3025 expected_cipher_suite);
3026}
3027
3028// Test that a GCM cipher is used if both ends support it.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003029TEST_P(PeerConnectionIntegrationTest, GcmCipherUsedWhenGcmSupported) {
deadbeef1dcb1642017-03-29 21:08:16 -07003030 bool local_gcm_enabled = true;
3031 bool remote_gcm_enabled = true;
3032 int expected_cipher_suite = kDefaultSrtpCryptoSuiteGcm;
3033 TestGcmNegotiationUsesCipherSuite(local_gcm_enabled, remote_gcm_enabled,
3034 expected_cipher_suite);
3035}
3036
3037// Test that GCM isn't used if only the offerer supports it.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003038TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07003039 NonGcmCipherUsedWhenOnlyCallerSupportsGcm) {
3040 bool local_gcm_enabled = true;
3041 bool remote_gcm_enabled = false;
3042 int expected_cipher_suite = kDefaultSrtpCryptoSuite;
3043 TestGcmNegotiationUsesCipherSuite(local_gcm_enabled, remote_gcm_enabled,
3044 expected_cipher_suite);
3045}
3046
3047// Test that GCM isn't used if only the answerer supports it.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003048TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07003049 NonGcmCipherUsedWhenOnlyCalleeSupportsGcm) {
3050 bool local_gcm_enabled = false;
3051 bool remote_gcm_enabled = true;
3052 int expected_cipher_suite = kDefaultSrtpCryptoSuite;
3053 TestGcmNegotiationUsesCipherSuite(local_gcm_enabled, remote_gcm_enabled,
3054 expected_cipher_suite);
3055}
3056
deadbeef7914b8c2017-04-21 03:23:33 -07003057// Verify that media can be transmitted end-to-end when GCM crypto suites are
3058// enabled. Note that the above tests, such as GcmCipherUsedWhenGcmSupported,
3059// only verify that a GCM cipher is negotiated, and not necessarily that SRTP
3060// works with it.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003061TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithGcmCipher) {
deadbeef7914b8c2017-04-21 03:23:33 -07003062 PeerConnectionFactory::Options gcm_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07003063 gcm_options.crypto_options.srtp.enable_gcm_crypto_suites = true;
deadbeef7914b8c2017-04-21 03:23:33 -07003064 ASSERT_TRUE(
3065 CreatePeerConnectionWrappersWithOptions(gcm_options, gcm_options));
3066 ConnectFakeSignaling();
3067 // Do normal offer/answer and wait for some frames to be received in each
3068 // direction.
Steve Anton15324772018-01-16 10:26:49 -08003069 caller()->AddAudioVideoTracks();
3070 callee()->AddAudioVideoTracks();
deadbeef7914b8c2017-04-21 03:23:33 -07003071 caller()->CreateAndSetAndSignalOffer();
3072 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08003073 MediaExpectations media_expectations;
3074 media_expectations.ExpectBidirectionalAudioAndVideo();
3075 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef7914b8c2017-04-21 03:23:33 -07003076}
3077
deadbeef1dcb1642017-03-29 21:08:16 -07003078// This test sets up a call between two parties with audio, video and an RTP
3079// data channel.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003080TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithRtpDataChannel) {
Niels Möllerf06f9232018-08-07 12:32:18 +02003081 PeerConnectionInterface::RTCConfiguration rtc_config;
3082 rtc_config.enable_rtp_data_channel = true;
3083 rtc_config.enable_dtls_srtp = false;
3084 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(rtc_config, rtc_config));
deadbeef1dcb1642017-03-29 21:08:16 -07003085 ConnectFakeSignaling();
3086 // Expect that data channel created on caller side will show up for callee as
3087 // well.
3088 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08003089 caller()->AddAudioVideoTracks();
3090 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003091 caller()->CreateAndSetAndSignalOffer();
3092 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3093 // Ensure the existence of the RTP data channel didn't impede audio/video.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003094 MediaExpectations media_expectations;
3095 media_expectations.ExpectBidirectionalAudioAndVideo();
3096 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003097 ASSERT_NE(nullptr, caller()->data_channel());
3098 ASSERT_NE(nullptr, callee()->data_channel());
3099 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3100 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3101
3102 // Ensure data can be sent in both directions.
3103 std::string data = "hello world";
3104 SendRtpDataWithRetries(caller()->data_channel(), data, 5);
3105 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3106 kDefaultTimeout);
3107 SendRtpDataWithRetries(callee()->data_channel(), data, 5);
3108 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3109 kDefaultTimeout);
3110}
3111
3112// Ensure that an RTP data channel is signaled as closed for the caller when
3113// the callee rejects it in a subsequent offer.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003114TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07003115 RtpDataChannelSignaledClosedInCalleeOffer) {
3116 // Same procedure as above test.
Niels Möllerf06f9232018-08-07 12:32:18 +02003117 PeerConnectionInterface::RTCConfiguration rtc_config;
3118 rtc_config.enable_rtp_data_channel = true;
3119 rtc_config.enable_dtls_srtp = false;
3120 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(rtc_config, rtc_config));
deadbeef1dcb1642017-03-29 21:08:16 -07003121 ConnectFakeSignaling();
3122 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08003123 caller()->AddAudioVideoTracks();
3124 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003125 caller()->CreateAndSetAndSignalOffer();
3126 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3127 ASSERT_NE(nullptr, caller()->data_channel());
3128 ASSERT_NE(nullptr, callee()->data_channel());
3129 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3130 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3131
3132 // Close the data channel on the callee, and do an updated offer/answer.
3133 callee()->data_channel()->Close();
3134 callee()->CreateAndSetAndSignalOffer();
3135 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3136 EXPECT_FALSE(caller()->data_observer()->IsOpen());
3137 EXPECT_FALSE(callee()->data_observer()->IsOpen());
3138}
3139
3140// Tests that data is buffered in an RTP data channel until an observer is
3141// registered for it.
3142//
3143// NOTE: RTP data channels can receive data before the underlying
3144// transport has detected that a channel is writable and thus data can be
3145// received before the data channel state changes to open. That is hard to test
3146// but the same buffering is expected to be used in that case.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003147TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07003148 DataBufferedUntilRtpDataChannelObserverRegistered) {
3149 // Use fake clock and simulated network delay so that we predictably can wait
3150 // until an SCTP message has been delivered without "sleep()"ing.
3151 rtc::ScopedFakeClock fake_clock;
3152 // Some things use a time of "0" as a special value, so we need to start out
3153 // the fake clock at a nonzero time.
3154 // TODO(deadbeef): Fix this.
Sebastian Jansson5f83cf02018-05-08 14:52:22 +02003155 fake_clock.AdvanceTime(webrtc::TimeDelta::seconds(1));
deadbeef1dcb1642017-03-29 21:08:16 -07003156 virtual_socket_server()->set_delay_mean(5); // 5 ms per hop.
3157 virtual_socket_server()->UpdateDelayDistribution();
3158
Niels Möllerf06f9232018-08-07 12:32:18 +02003159 PeerConnectionInterface::RTCConfiguration rtc_config;
3160 rtc_config.enable_rtp_data_channel = true;
3161 rtc_config.enable_dtls_srtp = false;
3162 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(rtc_config, rtc_config));
deadbeef1dcb1642017-03-29 21:08:16 -07003163 ConnectFakeSignaling();
3164 caller()->CreateDataChannel();
3165 caller()->CreateAndSetAndSignalOffer();
3166 ASSERT_TRUE(caller()->data_channel() != nullptr);
3167 ASSERT_TRUE_SIMULATED_WAIT(callee()->data_channel() != nullptr,
3168 kDefaultTimeout, fake_clock);
3169 ASSERT_TRUE_SIMULATED_WAIT(caller()->data_observer()->IsOpen(),
3170 kDefaultTimeout, fake_clock);
3171 ASSERT_EQ_SIMULATED_WAIT(DataChannelInterface::kOpen,
3172 callee()->data_channel()->state(), kDefaultTimeout,
3173 fake_clock);
3174
3175 // Unregister the observer which is normally automatically registered.
3176 callee()->data_channel()->UnregisterObserver();
3177 // Send data and advance fake clock until it should have been received.
3178 std::string data = "hello world";
3179 caller()->data_channel()->Send(DataBuffer(data));
3180 SIMULATED_WAIT(false, 50, fake_clock);
3181
3182 // Attach data channel and expect data to be received immediately. Note that
3183 // EXPECT_EQ_WAIT is used, such that the simulated clock is not advanced any
3184 // further, but data can be received even if the callback is asynchronous.
3185 MockDataChannelObserver new_observer(callee()->data_channel());
3186 EXPECT_EQ_SIMULATED_WAIT(data, new_observer.last_message(), kDefaultTimeout,
3187 fake_clock);
Seth Hampson1d4a76d2018-06-19 14:31:41 -07003188 // Closing the PeerConnections destroys the ports before the ScopedFakeClock.
3189 // If this is not done a DCHECK can be hit in ports.cc, because a large
3190 // negative number is calculated for the rtt due to the global clock changing.
3191 caller()->pc()->Close();
3192 callee()->pc()->Close();
deadbeef1dcb1642017-03-29 21:08:16 -07003193}
3194
3195// This test sets up a call between two parties with audio, video and but only
3196// the caller client supports RTP data channels.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003197TEST_P(PeerConnectionIntegrationTest, RtpDataChannelsRejectedByCallee) {
Niels Möllerf06f9232018-08-07 12:32:18 +02003198 PeerConnectionInterface::RTCConfiguration rtc_config_1;
3199 rtc_config_1.enable_rtp_data_channel = true;
deadbeef1dcb1642017-03-29 21:08:16 -07003200 // Must disable DTLS to make negotiation succeed.
Niels Möllerf06f9232018-08-07 12:32:18 +02003201 rtc_config_1.enable_dtls_srtp = false;
3202 PeerConnectionInterface::RTCConfiguration rtc_config_2;
3203 rtc_config_2.enable_dtls_srtp = false;
3204 rtc_config_2.enable_dtls_srtp = false;
3205 ASSERT_TRUE(
3206 CreatePeerConnectionWrappersWithConfig(rtc_config_1, rtc_config_2));
deadbeef1dcb1642017-03-29 21:08:16 -07003207 ConnectFakeSignaling();
3208 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08003209 caller()->AddAudioVideoTracks();
3210 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003211 caller()->CreateAndSetAndSignalOffer();
3212 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3213 // The caller should still have a data channel, but it should be closed, and
3214 // one should ever have been created for the callee.
3215 EXPECT_TRUE(caller()->data_channel() != nullptr);
3216 EXPECT_FALSE(caller()->data_observer()->IsOpen());
3217 EXPECT_EQ(nullptr, callee()->data_channel());
3218}
3219
3220// This test sets up a call between two parties with audio, and video. When
3221// audio and video is setup and flowing, an RTP data channel is negotiated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003222TEST_P(PeerConnectionIntegrationTest, AddRtpDataChannelInSubsequentOffer) {
Niels Möllerf06f9232018-08-07 12:32:18 +02003223 PeerConnectionInterface::RTCConfiguration rtc_config;
3224 rtc_config.enable_rtp_data_channel = true;
3225 rtc_config.enable_dtls_srtp = false;
3226 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(rtc_config, rtc_config));
deadbeef1dcb1642017-03-29 21:08:16 -07003227 ConnectFakeSignaling();
3228 // Do initial offer/answer with audio/video.
Steve Anton15324772018-01-16 10:26:49 -08003229 caller()->AddAudioVideoTracks();
3230 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003231 caller()->CreateAndSetAndSignalOffer();
3232 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3233 // Create data channel and do new offer and answer.
3234 caller()->CreateDataChannel();
3235 caller()->CreateAndSetAndSignalOffer();
3236 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3237 ASSERT_NE(nullptr, caller()->data_channel());
3238 ASSERT_NE(nullptr, callee()->data_channel());
3239 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3240 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3241 // Ensure data can be sent in both directions.
3242 std::string data = "hello world";
3243 SendRtpDataWithRetries(caller()->data_channel(), data, 5);
3244 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3245 kDefaultTimeout);
3246 SendRtpDataWithRetries(callee()->data_channel(), data, 5);
3247 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3248 kDefaultTimeout);
3249}
3250
3251#ifdef HAVE_SCTP
3252
3253// This test sets up a call between two parties with audio, video and an SCTP
3254// data channel.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003255TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithSctpDataChannel) {
deadbeef1dcb1642017-03-29 21:08:16 -07003256 ASSERT_TRUE(CreatePeerConnectionWrappers());
3257 ConnectFakeSignaling();
3258 // Expect that data channel created on caller side will show up for callee as
3259 // well.
3260 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08003261 caller()->AddAudioVideoTracks();
3262 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003263 caller()->CreateAndSetAndSignalOffer();
3264 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3265 // Ensure the existence of the SCTP data channel didn't impede audio/video.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003266 MediaExpectations media_expectations;
3267 media_expectations.ExpectBidirectionalAudioAndVideo();
3268 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003269 // Caller data channel should already exist (it created one). Callee data
3270 // channel may not exist yet, since negotiation happens in-band, not in SDP.
3271 ASSERT_NE(nullptr, caller()->data_channel());
3272 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3273 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3274 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3275
3276 // Ensure data can be sent in both directions.
3277 std::string data = "hello world";
3278 caller()->data_channel()->Send(DataBuffer(data));
3279 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3280 kDefaultTimeout);
3281 callee()->data_channel()->Send(DataBuffer(data));
3282 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3283 kDefaultTimeout);
3284}
3285
3286// Ensure that when the callee closes an SCTP data channel, the closing
3287// procedure results in the data channel being closed for the caller as well.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003288TEST_P(PeerConnectionIntegrationTest, CalleeClosesSctpDataChannel) {
deadbeef1dcb1642017-03-29 21:08:16 -07003289 // Same procedure as above test.
3290 ASSERT_TRUE(CreatePeerConnectionWrappers());
3291 ConnectFakeSignaling();
3292 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08003293 caller()->AddAudioVideoTracks();
3294 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003295 caller()->CreateAndSetAndSignalOffer();
3296 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3297 ASSERT_NE(nullptr, caller()->data_channel());
3298 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3299 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3300 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3301
3302 // Close the data channel on the callee side, and wait for it to reach the
3303 // "closed" state on both sides.
3304 callee()->data_channel()->Close();
3305 EXPECT_TRUE_WAIT(!caller()->data_observer()->IsOpen(), kDefaultTimeout);
3306 EXPECT_TRUE_WAIT(!callee()->data_observer()->IsOpen(), kDefaultTimeout);
3307}
3308
Seth Hampson2f0d7022018-02-20 11:54:42 -08003309TEST_P(PeerConnectionIntegrationTest, SctpDataChannelConfigSentToOtherSide) {
Steve Antonda6c0952017-10-23 11:41:54 -07003310 ASSERT_TRUE(CreatePeerConnectionWrappers());
3311 ConnectFakeSignaling();
3312 webrtc::DataChannelInit init;
3313 init.id = 53;
3314 init.maxRetransmits = 52;
3315 caller()->CreateDataChannel("data-channel", &init);
Steve Anton15324772018-01-16 10:26:49 -08003316 caller()->AddAudioVideoTracks();
3317 callee()->AddAudioVideoTracks();
Steve Antonda6c0952017-10-23 11:41:54 -07003318 caller()->CreateAndSetAndSignalOffer();
3319 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Steve Anton074dece2017-10-24 13:04:12 -07003320 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3321 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
Steve Antonda6c0952017-10-23 11:41:54 -07003322 EXPECT_EQ(init.id, callee()->data_channel()->id());
3323 EXPECT_EQ("data-channel", callee()->data_channel()->label());
3324 EXPECT_EQ(init.maxRetransmits, callee()->data_channel()->maxRetransmits());
3325 EXPECT_FALSE(callee()->data_channel()->negotiated());
3326}
3327
deadbeef1dcb1642017-03-29 21:08:16 -07003328// Test usrsctp's ability to process unordered data stream, where data actually
3329// arrives out of order using simulated delays. Previously there have been some
3330// bugs in this area.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003331TEST_P(PeerConnectionIntegrationTest, StressTestUnorderedSctpDataChannel) {
deadbeef1dcb1642017-03-29 21:08:16 -07003332 // Introduce random network delays.
3333 // Otherwise it's not a true "unordered" test.
3334 virtual_socket_server()->set_delay_mean(20);
3335 virtual_socket_server()->set_delay_stddev(5);
3336 virtual_socket_server()->UpdateDelayDistribution();
3337 // Normal procedure, but with unordered data channel config.
3338 ASSERT_TRUE(CreatePeerConnectionWrappers());
3339 ConnectFakeSignaling();
3340 webrtc::DataChannelInit init;
3341 init.ordered = false;
3342 caller()->CreateDataChannel(&init);
3343 caller()->CreateAndSetAndSignalOffer();
3344 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3345 ASSERT_NE(nullptr, caller()->data_channel());
3346 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3347 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3348 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3349
3350 static constexpr int kNumMessages = 100;
3351 // Deliberately chosen to be larger than the MTU so messages get fragmented.
3352 static constexpr size_t kMaxMessageSize = 4096;
3353 // Create and send random messages.
3354 std::vector<std::string> sent_messages;
3355 for (int i = 0; i < kNumMessages; ++i) {
3356 size_t length =
3357 (rand() % kMaxMessageSize) + 1; // NOLINT (rand_r instead of rand)
3358 std::string message;
3359 ASSERT_TRUE(rtc::CreateRandomString(length, &message));
3360 caller()->data_channel()->Send(DataBuffer(message));
3361 callee()->data_channel()->Send(DataBuffer(message));
3362 sent_messages.push_back(message);
3363 }
3364
3365 // Wait for all messages to be received.
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02003366 EXPECT_EQ_WAIT(rtc::checked_cast<size_t>(kNumMessages),
deadbeef1dcb1642017-03-29 21:08:16 -07003367 caller()->data_observer()->received_message_count(),
3368 kDefaultTimeout);
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02003369 EXPECT_EQ_WAIT(rtc::checked_cast<size_t>(kNumMessages),
deadbeef1dcb1642017-03-29 21:08:16 -07003370 callee()->data_observer()->received_message_count(),
3371 kDefaultTimeout);
3372
3373 // Sort and compare to make sure none of the messages were corrupted.
3374 std::vector<std::string> caller_received_messages =
3375 caller()->data_observer()->messages();
3376 std::vector<std::string> callee_received_messages =
3377 callee()->data_observer()->messages();
Steve Anton64b626b2019-01-28 17:25:26 -08003378 absl::c_sort(sent_messages);
3379 absl::c_sort(caller_received_messages);
3380 absl::c_sort(callee_received_messages);
deadbeef1dcb1642017-03-29 21:08:16 -07003381 EXPECT_EQ(sent_messages, caller_received_messages);
3382 EXPECT_EQ(sent_messages, callee_received_messages);
3383}
3384
3385// This test sets up a call between two parties with audio, and video. When
3386// audio and video are setup and flowing, an SCTP data channel is negotiated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003387TEST_P(PeerConnectionIntegrationTest, AddSctpDataChannelInSubsequentOffer) {
deadbeef1dcb1642017-03-29 21:08:16 -07003388 ASSERT_TRUE(CreatePeerConnectionWrappers());
3389 ConnectFakeSignaling();
3390 // Do initial offer/answer with audio/video.
Steve Anton15324772018-01-16 10:26:49 -08003391 caller()->AddAudioVideoTracks();
3392 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003393 caller()->CreateAndSetAndSignalOffer();
3394 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3395 // Create data channel and do new offer and answer.
3396 caller()->CreateDataChannel();
3397 caller()->CreateAndSetAndSignalOffer();
3398 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3399 // Caller data channel should already exist (it created one). Callee data
3400 // channel may not exist yet, since negotiation happens in-band, not in SDP.
3401 ASSERT_NE(nullptr, caller()->data_channel());
3402 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3403 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3404 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3405 // Ensure data can be sent in both directions.
3406 std::string data = "hello world";
3407 caller()->data_channel()->Send(DataBuffer(data));
3408 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3409 kDefaultTimeout);
3410 callee()->data_channel()->Send(DataBuffer(data));
3411 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3412 kDefaultTimeout);
3413}
3414
deadbeef7914b8c2017-04-21 03:23:33 -07003415// Set up a connection initially just using SCTP data channels, later upgrading
3416// to audio/video, ensuring frames are received end-to-end. Effectively the
3417// inverse of the test above.
3418// This was broken in M57; see https://crbug.com/711243
Seth Hampson2f0d7022018-02-20 11:54:42 -08003419TEST_P(PeerConnectionIntegrationTest, SctpDataChannelToAudioVideoUpgrade) {
deadbeef7914b8c2017-04-21 03:23:33 -07003420 ASSERT_TRUE(CreatePeerConnectionWrappers());
3421 ConnectFakeSignaling();
3422 // Do initial offer/answer with just data channel.
3423 caller()->CreateDataChannel();
3424 caller()->CreateAndSetAndSignalOffer();
3425 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3426 // Wait until data can be sent over the data channel.
3427 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3428 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3429 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3430
3431 // Do subsequent offer/answer with two-way audio and video. Audio and video
3432 // should end up bundled on the DTLS/ICE transport already used for data.
Steve Anton15324772018-01-16 10:26:49 -08003433 caller()->AddAudioVideoTracks();
3434 callee()->AddAudioVideoTracks();
deadbeef7914b8c2017-04-21 03:23:33 -07003435 caller()->CreateAndSetAndSignalOffer();
3436 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08003437 MediaExpectations media_expectations;
3438 media_expectations.ExpectBidirectionalAudioAndVideo();
3439 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef7914b8c2017-04-21 03:23:33 -07003440}
3441
deadbeef8b7e9ad2017-05-25 09:38:55 -07003442static void MakeSpecCompliantSctpOffer(cricket::SessionDescription* desc) {
deadbeef8b7e9ad2017-05-25 09:38:55 -07003443 cricket::DataContentDescription* dcd_offer =
Steve Antonb1c1de12017-12-21 15:14:30 -08003444 GetFirstDataContentDescription(desc);
3445 ASSERT_TRUE(dcd_offer);
deadbeef8b7e9ad2017-05-25 09:38:55 -07003446 dcd_offer->set_use_sctpmap(false);
3447 dcd_offer->set_protocol("UDP/DTLS/SCTP");
3448}
3449
3450// Test that the data channel works when a spec-compliant SCTP m= section is
3451// offered (using "a=sctp-port" instead of "a=sctpmap", and using
3452// "UDP/DTLS/SCTP" as the protocol).
Seth Hampson2f0d7022018-02-20 11:54:42 -08003453TEST_P(PeerConnectionIntegrationTest,
deadbeef8b7e9ad2017-05-25 09:38:55 -07003454 DataChannelWorksWhenSpecCompliantSctpOfferReceived) {
3455 ASSERT_TRUE(CreatePeerConnectionWrappers());
3456 ConnectFakeSignaling();
3457 caller()->CreateDataChannel();
3458 caller()->SetGeneratedSdpMunger(MakeSpecCompliantSctpOffer);
3459 caller()->CreateAndSetAndSignalOffer();
3460 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3461 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3462 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3463 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3464
3465 // Ensure data can be sent in both directions.
3466 std::string data = "hello world";
3467 caller()->data_channel()->Send(DataBuffer(data));
3468 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3469 kDefaultTimeout);
3470 callee()->data_channel()->Send(DataBuffer(data));
3471 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3472 kDefaultTimeout);
3473}
3474
deadbeef1dcb1642017-03-29 21:08:16 -07003475#endif // HAVE_SCTP
3476
Bjorn Mellema2eb0a72018-11-09 10:13:51 -08003477// This test sets up a call between two parties with a media transport data
3478// channel.
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08003479TEST_P(PeerConnectionIntegrationTest, MediaTransportDataChannelEndToEnd) {
3480 PeerConnectionInterface::RTCConfiguration rtc_config;
Piotr (Peter) Slatalab1ae10b2019-03-01 11:14:05 -08003481 rtc_config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyRequire;
3482 rtc_config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08003483 rtc_config.use_media_transport_for_data_channels = true;
3484 rtc_config.enable_dtls_srtp = false; // SDES is required for media transport.
3485 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
3486 rtc_config, rtc_config, loopback_media_transports()->first_factory(),
3487 loopback_media_transports()->second_factory()));
3488 ConnectFakeSignaling();
3489
3490 // Expect that data channel created on caller side will show up for callee as
3491 // well.
3492 caller()->CreateDataChannel();
3493 caller()->CreateAndSetAndSignalOffer();
3494 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3495
3496 // Ensure that the media transport is ready.
3497 loopback_media_transports()->SetState(webrtc::MediaTransportState::kWritable);
3498 loopback_media_transports()->FlushAsyncInvokes();
3499
3500 // Caller data channel should already exist (it created one). Callee data
3501 // channel may not exist yet, since negotiation happens in-band, not in SDP.
3502 ASSERT_NE(nullptr, caller()->data_channel());
3503 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3504 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3505 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3506
3507 // Ensure data can be sent in both directions.
3508 std::string data = "hello world";
3509 caller()->data_channel()->Send(DataBuffer(data));
3510 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3511 kDefaultTimeout);
3512 callee()->data_channel()->Send(DataBuffer(data));
3513 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3514 kDefaultTimeout);
3515}
3516
3517// Ensure that when the callee closes a media transport data channel, the
3518// closing procedure results in the data channel being closed for the caller
3519// as well.
3520TEST_P(PeerConnectionIntegrationTest, MediaTransportDataChannelCalleeCloses) {
3521 PeerConnectionInterface::RTCConfiguration rtc_config;
3522 rtc_config.use_media_transport_for_data_channels = true;
3523 rtc_config.enable_dtls_srtp = false; // SDES is required for media transport.
3524 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
3525 rtc_config, rtc_config, loopback_media_transports()->first_factory(),
3526 loopback_media_transports()->second_factory()));
3527 ConnectFakeSignaling();
3528
3529 // Create a data channel on the caller and signal it to the callee.
3530 caller()->CreateDataChannel();
3531 caller()->CreateAndSetAndSignalOffer();
3532 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3533
3534 // Ensure that the media transport is ready.
3535 loopback_media_transports()->SetState(webrtc::MediaTransportState::kWritable);
3536 loopback_media_transports()->FlushAsyncInvokes();
3537
3538 // Data channels exist and open on both ends of the connection.
3539 ASSERT_NE(nullptr, caller()->data_channel());
3540 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3541 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3542 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3543
3544 // Close the data channel on the callee side, and wait for it to reach the
3545 // "closed" state on both sides.
3546 callee()->data_channel()->Close();
3547 EXPECT_TRUE_WAIT(!caller()->data_observer()->IsOpen(), kDefaultTimeout);
3548 EXPECT_TRUE_WAIT(!callee()->data_observer()->IsOpen(), kDefaultTimeout);
3549}
3550
3551TEST_P(PeerConnectionIntegrationTest,
3552 MediaTransportDataChannelConfigSentToOtherSide) {
3553 PeerConnectionInterface::RTCConfiguration rtc_config;
3554 rtc_config.use_media_transport_for_data_channels = true;
3555 rtc_config.enable_dtls_srtp = false; // SDES is required for media transport.
3556 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
3557 rtc_config, rtc_config, loopback_media_transports()->first_factory(),
3558 loopback_media_transports()->second_factory()));
3559 ConnectFakeSignaling();
3560
3561 // Create a data channel with a non-default configuration and signal it to the
3562 // callee.
3563 webrtc::DataChannelInit init;
3564 init.id = 53;
3565 init.maxRetransmits = 52;
3566 caller()->CreateDataChannel("data-channel", &init);
3567 caller()->CreateAndSetAndSignalOffer();
3568 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3569
3570 // Ensure that the media transport is ready.
3571 loopback_media_transports()->SetState(webrtc::MediaTransportState::kWritable);
3572 loopback_media_transports()->FlushAsyncInvokes();
3573
3574 // Ensure that the data channel exists on the callee with the correct
3575 // configuration.
3576 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3577 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3578 EXPECT_EQ(init.id, callee()->data_channel()->id());
3579 EXPECT_EQ("data-channel", callee()->data_channel()->label());
3580 EXPECT_EQ(init.maxRetransmits, callee()->data_channel()->maxRetransmits());
3581 EXPECT_FALSE(callee()->data_channel()->negotiated());
3582}
3583
Piotr (Peter) Slatalab1ae10b2019-03-01 11:14:05 -08003584TEST_P(PeerConnectionIntegrationTest, MediaTransportOfferUpgrade) {
3585 PeerConnectionInterface::RTCConfiguration rtc_config;
3586 rtc_config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyRequire;
3587 rtc_config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
3588 rtc_config.use_media_transport = true;
3589 rtc_config.enable_dtls_srtp = false; // SDES is required for media transport.
3590 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
3591 rtc_config, rtc_config, loopback_media_transports()->first_factory(),
3592 loopback_media_transports()->second_factory()));
3593 ConnectFakeSignaling();
3594
3595 // Do initial offer/answer with just a video track.
3596 caller()->AddVideoTrack();
3597 callee()->AddVideoTrack();
3598 caller()->CreateAndSetAndSignalOffer();
3599 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3600
3601 // Ensure that the media transport is ready.
3602 loopback_media_transports()->SetState(webrtc::MediaTransportState::kWritable);
3603 loopback_media_transports()->FlushAsyncInvokes();
3604
3605 // Now add an audio track and do another offer/answer.
3606 caller()->AddAudioTrack();
3607 callee()->AddAudioTrack();
3608 caller()->CreateAndSetAndSignalOffer();
3609 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3610
3611 // Ensure both audio and video frames are received end-to-end.
3612 MediaExpectations media_expectations;
3613 media_expectations.ExpectBidirectionalAudioAndVideo();
3614 ASSERT_TRUE(ExpectNewFrames(media_expectations));
3615
3616 // The second offer should not have generated another media transport.
3617 // Media transport was kept alive, and was not recreated.
3618 EXPECT_EQ(1, loopback_media_transports()->first_factory_transport_count());
3619 EXPECT_EQ(1, loopback_media_transports()->second_factory_transport_count());
3620}
3621
3622TEST_P(PeerConnectionIntegrationTest, MediaTransportOfferUpgradeOnTheCallee) {
3623 PeerConnectionInterface::RTCConfiguration rtc_config;
3624 rtc_config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyRequire;
3625 rtc_config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
3626 rtc_config.use_media_transport = true;
3627 rtc_config.enable_dtls_srtp = false; // SDES is required for media transport.
3628 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
3629 rtc_config, rtc_config, loopback_media_transports()->first_factory(),
3630 loopback_media_transports()->second_factory()));
3631 ConnectFakeSignaling();
3632
3633 // Do initial offer/answer with just a video track.
3634 caller()->AddVideoTrack();
3635 callee()->AddVideoTrack();
3636 caller()->CreateAndSetAndSignalOffer();
3637 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3638
3639 // Ensure that the media transport is ready.
3640 loopback_media_transports()->SetState(webrtc::MediaTransportState::kWritable);
3641 loopback_media_transports()->FlushAsyncInvokes();
3642
3643 // Now add an audio track and do another offer/answer.
3644 caller()->AddAudioTrack();
3645 callee()->AddAudioTrack();
3646 callee()->CreateAndSetAndSignalOffer();
3647 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3648
3649 // Ensure both audio and video frames are received end-to-end.
3650 MediaExpectations media_expectations;
3651 media_expectations.ExpectBidirectionalAudioAndVideo();
3652 ASSERT_TRUE(ExpectNewFrames(media_expectations));
3653
3654 // The second offer should not have generated another media transport.
3655 // Media transport was kept alive, and was not recreated.
3656 EXPECT_EQ(1, loopback_media_transports()->first_factory_transport_count());
3657 EXPECT_EQ(1, loopback_media_transports()->second_factory_transport_count());
3658}
3659
Niels Möllerc68d2822018-11-20 14:52:05 +01003660TEST_P(PeerConnectionIntegrationTest, MediaTransportBidirectionalAudio) {
3661 PeerConnectionInterface::RTCConfiguration rtc_config;
Piotr (Peter) Slatalab1ae10b2019-03-01 11:14:05 -08003662 rtc_config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyRequire;
3663 rtc_config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
Niels Möllerc68d2822018-11-20 14:52:05 +01003664 rtc_config.use_media_transport = true;
3665 rtc_config.enable_dtls_srtp = false; // SDES is required for media transport.
3666 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
3667 rtc_config, rtc_config, loopback_media_transports()->first_factory(),
3668 loopback_media_transports()->second_factory()));
3669 ConnectFakeSignaling();
3670
3671 caller()->AddAudioTrack();
3672 callee()->AddAudioTrack();
3673 // Start offer/answer exchange and wait for it to complete.
3674 caller()->CreateAndSetAndSignalOffer();
3675 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3676
3677 // Ensure that the media transport is ready.
3678 loopback_media_transports()->SetState(webrtc::MediaTransportState::kWritable);
3679 loopback_media_transports()->FlushAsyncInvokes();
3680
3681 MediaExpectations media_expectations;
3682 media_expectations.ExpectBidirectionalAudio();
3683 ASSERT_TRUE(ExpectNewFrames(media_expectations));
3684
3685 webrtc::MediaTransportPair::Stats first_stats =
3686 loopback_media_transports()->FirstStats();
3687 webrtc::MediaTransportPair::Stats second_stats =
3688 loopback_media_transports()->SecondStats();
3689
3690 EXPECT_GT(first_stats.received_audio_frames, 0);
3691 EXPECT_GE(second_stats.sent_audio_frames, first_stats.received_audio_frames);
3692
3693 EXPECT_GT(second_stats.received_audio_frames, 0);
3694 EXPECT_GE(first_stats.sent_audio_frames, second_stats.received_audio_frames);
3695}
3696
Niels Möller46879152019-01-07 15:54:47 +01003697TEST_P(PeerConnectionIntegrationTest, MediaTransportBidirectionalVideo) {
3698 PeerConnectionInterface::RTCConfiguration rtc_config;
3699 rtc_config.use_media_transport = true;
3700 rtc_config.enable_dtls_srtp = false; // SDES is required for media transport.
3701 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
3702 rtc_config, rtc_config, loopback_media_transports()->first_factory(),
3703 loopback_media_transports()->second_factory()));
3704 ConnectFakeSignaling();
3705
3706 caller()->AddVideoTrack();
3707 callee()->AddVideoTrack();
3708 // Start offer/answer exchange and wait for it to complete.
3709 caller()->CreateAndSetAndSignalOffer();
3710 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3711
3712 // Ensure that the media transport is ready.
3713 loopback_media_transports()->SetState(webrtc::MediaTransportState::kWritable);
3714 loopback_media_transports()->FlushAsyncInvokes();
3715
3716 MediaExpectations media_expectations;
3717 media_expectations.ExpectBidirectionalVideo();
3718 ASSERT_TRUE(ExpectNewFrames(media_expectations));
3719
3720 webrtc::MediaTransportPair::Stats first_stats =
3721 loopback_media_transports()->FirstStats();
3722 webrtc::MediaTransportPair::Stats second_stats =
3723 loopback_media_transports()->SecondStats();
3724
3725 EXPECT_GT(first_stats.received_video_frames, 0);
3726 EXPECT_GE(second_stats.sent_video_frames, first_stats.received_video_frames);
3727
3728 EXPECT_GT(second_stats.received_video_frames, 0);
3729 EXPECT_GE(first_stats.sent_video_frames, second_stats.received_video_frames);
3730}
3731
Piotr (Peter) Slatala55b91b92019-01-25 13:31:15 -08003732TEST_P(PeerConnectionIntegrationTest,
3733 MediaTransportDataChannelUsesRtpBidirectionalVideo) {
3734 PeerConnectionInterface::RTCConfiguration rtc_config;
3735 rtc_config.use_media_transport = false;
3736 rtc_config.use_media_transport_for_data_channels = true;
3737 rtc_config.enable_dtls_srtp = false; // SDES is required for media transport.
3738 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
3739 rtc_config, rtc_config, loopback_media_transports()->first_factory(),
3740 loopback_media_transports()->second_factory()));
3741 ConnectFakeSignaling();
3742
3743 caller()->AddVideoTrack();
3744 callee()->AddVideoTrack();
3745 // Start offer/answer exchange and wait for it to complete.
3746 caller()->CreateAndSetAndSignalOffer();
3747 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3748
3749 MediaExpectations media_expectations;
3750 media_expectations.ExpectBidirectionalVideo();
3751 ASSERT_TRUE(ExpectNewFrames(media_expectations));
3752}
3753
deadbeef1dcb1642017-03-29 21:08:16 -07003754// Test that the ICE connection and gathering states eventually reach
3755// "complete".
Seth Hampson2f0d7022018-02-20 11:54:42 -08003756TEST_P(PeerConnectionIntegrationTest, IceStatesReachCompletion) {
deadbeef1dcb1642017-03-29 21:08:16 -07003757 ASSERT_TRUE(CreatePeerConnectionWrappers());
3758 ConnectFakeSignaling();
3759 // Do normal offer/answer.
Steve Anton15324772018-01-16 10:26:49 -08003760 caller()->AddAudioVideoTracks();
3761 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003762 caller()->CreateAndSetAndSignalOffer();
3763 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3764 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceGatheringComplete,
3765 caller()->ice_gathering_state(), kMaxWaitForFramesMs);
3766 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceGatheringComplete,
3767 callee()->ice_gathering_state(), kMaxWaitForFramesMs);
3768 // After the best candidate pair is selected and all candidates are signaled,
3769 // the ICE connection state should reach "complete".
3770 // TODO(deadbeef): Currently, the ICE "controlled" agent (the
3771 // answerer/"callee" by default) only reaches "connected". When this is
3772 // fixed, this test should be updated.
3773 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
3774 caller()->ice_connection_state(), kDefaultTimeout);
Alex Loiko9289eda2018-11-23 16:18:59 +00003775 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
3776 callee()->ice_connection_state(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07003777}
3778
Qingsi Wang1dac6d82018-12-12 15:28:47 -08003779constexpr int kOnlyLocalPorts = cricket::PORTALLOCATOR_DISABLE_STUN |
3780 cricket::PORTALLOCATOR_DISABLE_RELAY |
3781 cricket::PORTALLOCATOR_DISABLE_TCP;
Zach Stein6fcdc2f2018-08-23 16:25:55 -07003782
Qingsi Wang1dac6d82018-12-12 15:28:47 -08003783// Use a mock resolver to resolve the hostname back to the original IP on both
3784// sides and check that the ICE connection connects.
Zach Stein6fcdc2f2018-08-23 16:25:55 -07003785TEST_P(PeerConnectionIntegrationTest,
3786 IceStatesReachCompletionWithRemoteHostname) {
Qingsi Wang1dac6d82018-12-12 15:28:47 -08003787 auto caller_resolver_factory =
3788 absl::make_unique<NiceMock<webrtc::MockAsyncResolverFactory>>();
3789 auto callee_resolver_factory =
3790 absl::make_unique<NiceMock<webrtc::MockAsyncResolverFactory>>();
3791 NiceMock<rtc::MockAsyncResolver> callee_async_resolver;
3792 NiceMock<rtc::MockAsyncResolver> caller_async_resolver;
Zach Stein6fcdc2f2018-08-23 16:25:55 -07003793
3794 // This also verifies that the injected AsyncResolverFactory is used by
3795 // P2PTransportChannel.
Qingsi Wang1dac6d82018-12-12 15:28:47 -08003796 EXPECT_CALL(*caller_resolver_factory, Create())
3797 .WillOnce(Return(&caller_async_resolver));
3798 webrtc::PeerConnectionDependencies caller_deps(nullptr);
3799 caller_deps.async_resolver_factory = std::move(caller_resolver_factory);
3800
3801 EXPECT_CALL(*callee_resolver_factory, Create())
3802 .WillOnce(Return(&callee_async_resolver));
3803 webrtc::PeerConnectionDependencies callee_deps(nullptr);
3804 callee_deps.async_resolver_factory = std::move(callee_resolver_factory);
3805
3806 PeerConnectionInterface::RTCConfiguration config;
3807 config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
3808 config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyRequire;
3809
3810 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndDeps(
3811 config, std::move(caller_deps), config, std::move(callee_deps)));
3812
3813 caller()->SetRemoteAsyncResolver(&callee_async_resolver);
3814 callee()->SetRemoteAsyncResolver(&caller_async_resolver);
3815
3816 // Enable hostname candidates with mDNS names.
3817 caller()->network()->CreateMdnsResponder(network_thread());
3818 callee()->network()->CreateMdnsResponder(network_thread());
3819
3820 SetPortAllocatorFlags(kOnlyLocalPorts, kOnlyLocalPorts);
Zach Stein6fcdc2f2018-08-23 16:25:55 -07003821
3822 ConnectFakeSignaling();
3823 caller()->AddAudioVideoTracks();
3824 callee()->AddAudioVideoTracks();
3825 caller()->CreateAndSetAndSignalOffer();
3826 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3827 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
3828 caller()->ice_connection_state(), kDefaultTimeout);
3829 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
3830 callee()->ice_connection_state(), kDefaultTimeout);
Jeroen de Borst833979f2018-12-13 08:25:54 -08003831
3832 EXPECT_EQ(1, webrtc::metrics::NumEvents(
3833 "WebRTC.PeerConnection.CandidatePairType_UDP",
3834 webrtc::kIceCandidatePairHostNameHostName));
Zach Stein6fcdc2f2018-08-23 16:25:55 -07003835}
3836
Steve Antonede9ca52017-10-16 13:04:27 -07003837// Test that firewalling the ICE connection causes the clients to identify the
3838// disconnected state and then removing the firewall causes them to reconnect.
3839class PeerConnectionIntegrationIceStatesTest
Seth Hampson2f0d7022018-02-20 11:54:42 -08003840 : public PeerConnectionIntegrationBaseTest,
3841 public ::testing::WithParamInterface<
3842 std::tuple<SdpSemantics, std::tuple<std::string, uint32_t>>> {
Steve Antonede9ca52017-10-16 13:04:27 -07003843 protected:
Seth Hampson2f0d7022018-02-20 11:54:42 -08003844 PeerConnectionIntegrationIceStatesTest()
3845 : PeerConnectionIntegrationBaseTest(std::get<0>(GetParam())) {
3846 port_allocator_flags_ = std::get<1>(std::get<1>(GetParam()));
Steve Antonede9ca52017-10-16 13:04:27 -07003847 }
3848
3849 void StartStunServer(const SocketAddress& server_address) {
3850 stun_server_.reset(
3851 cricket::TestStunServer::Create(network_thread(), server_address));
3852 }
3853
3854 bool TestIPv6() {
3855 return (port_allocator_flags_ & cricket::PORTALLOCATOR_ENABLE_IPV6);
3856 }
3857
3858 void SetPortAllocatorFlags() {
Qingsi Wang1dac6d82018-12-12 15:28:47 -08003859 PeerConnectionIntegrationBaseTest::SetPortAllocatorFlags(
3860 port_allocator_flags_, port_allocator_flags_);
Steve Antonede9ca52017-10-16 13:04:27 -07003861 }
3862
3863 std::vector<SocketAddress> CallerAddresses() {
3864 std::vector<SocketAddress> addresses;
3865 addresses.push_back(SocketAddress("1.1.1.1", 0));
3866 if (TestIPv6()) {
3867 addresses.push_back(SocketAddress("1111:0:a:b:c:d:e:f", 0));
3868 }
3869 return addresses;
3870 }
3871
3872 std::vector<SocketAddress> CalleeAddresses() {
3873 std::vector<SocketAddress> addresses;
3874 addresses.push_back(SocketAddress("2.2.2.2", 0));
3875 if (TestIPv6()) {
3876 addresses.push_back(SocketAddress("2222:0:a:b:c:d:e:f", 0));
3877 }
3878 return addresses;
3879 }
3880
3881 void SetUpNetworkInterfaces() {
3882 // Remove the default interfaces added by the test infrastructure.
3883 caller()->network()->RemoveInterface(kDefaultLocalAddress);
3884 callee()->network()->RemoveInterface(kDefaultLocalAddress);
3885
3886 // Add network addresses for test.
3887 for (const auto& caller_address : CallerAddresses()) {
3888 caller()->network()->AddInterface(caller_address);
3889 }
3890 for (const auto& callee_address : CalleeAddresses()) {
3891 callee()->network()->AddInterface(callee_address);
3892 }
3893 }
3894
3895 private:
3896 uint32_t port_allocator_flags_;
3897 std::unique_ptr<cricket::TestStunServer> stun_server_;
3898};
3899
3900// Tests that the PeerConnection goes through all the ICE gathering/connection
3901// states over the duration of the call. This includes Disconnected and Failed
3902// states, induced by putting a firewall between the peers and waiting for them
3903// to time out.
Steve Anton83119dd2017-11-10 16:19:52 -08003904TEST_P(PeerConnectionIntegrationIceStatesTest, VerifyIceStates) {
Jonas Olssonb75d9e92019-02-22 10:33:29 +01003905 rtc::ScopedFakeClock fake_clock;
3906 // Some things use a time of "0" as a special value, so we need to start out
3907 // the fake clock at a nonzero time.
3908 fake_clock.AdvanceTime(TimeDelta::seconds(1));
Steve Antonede9ca52017-10-16 13:04:27 -07003909
3910 const SocketAddress kStunServerAddress =
3911 SocketAddress("99.99.99.1", cricket::STUN_SERVER_PORT);
3912 StartStunServer(kStunServerAddress);
3913
3914 PeerConnectionInterface::RTCConfiguration config;
3915 PeerConnectionInterface::IceServer ice_stun_server;
3916 ice_stun_server.urls.push_back(
3917 "stun:" + kStunServerAddress.HostAsURIString() + ":" +
3918 kStunServerAddress.PortAsString());
3919 config.servers.push_back(ice_stun_server);
3920
3921 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
3922 ConnectFakeSignaling();
3923 SetPortAllocatorFlags();
3924 SetUpNetworkInterfaces();
Steve Anton15324772018-01-16 10:26:49 -08003925 caller()->AddAudioVideoTracks();
3926 callee()->AddAudioVideoTracks();
Steve Antonede9ca52017-10-16 13:04:27 -07003927
3928 // Initial state before anything happens.
3929 ASSERT_EQ(PeerConnectionInterface::kIceGatheringNew,
3930 caller()->ice_gathering_state());
3931 ASSERT_EQ(PeerConnectionInterface::kIceConnectionNew,
3932 caller()->ice_connection_state());
Jonas Olsson7a6739e2019-01-15 16:31:55 +01003933 ASSERT_EQ(PeerConnectionInterface::kIceConnectionNew,
3934 caller()->standardized_ice_connection_state());
Steve Antonede9ca52017-10-16 13:04:27 -07003935
3936 // Start the call by creating the offer, setting it as the local description,
3937 // then sending it to the peer who will respond with an answer. This happens
3938 // asynchronously so that we can watch the states as it runs in the
3939 // background.
3940 caller()->CreateAndSetAndSignalOffer();
3941
Jonas Olsson7a6739e2019-01-15 16:31:55 +01003942 ASSERT_EQ(PeerConnectionInterface::kIceConnectionCompleted,
3943 caller()->ice_connection_state());
Jonas Olssonacd8ae72019-02-25 15:26:24 +01003944 ASSERT_EQ(PeerConnectionInterface::kIceConnectionCompleted,
Jonas Olsson7a6739e2019-01-15 16:31:55 +01003945 caller()->standardized_ice_connection_state());
Steve Antonede9ca52017-10-16 13:04:27 -07003946
3947 // Verify that the observer was notified of the intermediate transitions.
3948 EXPECT_THAT(caller()->ice_connection_state_history(),
3949 ElementsAre(PeerConnectionInterface::kIceConnectionChecking,
3950 PeerConnectionInterface::kIceConnectionConnected,
3951 PeerConnectionInterface::kIceConnectionCompleted));
Jonas Olssonacd8ae72019-02-25 15:26:24 +01003952 EXPECT_THAT(caller()->standardized_ice_connection_state_history(),
3953 ElementsAre(PeerConnectionInterface::kIceConnectionChecking,
3954 PeerConnectionInterface::kIceConnectionConnected,
3955 PeerConnectionInterface::kIceConnectionCompleted));
Jonas Olsson635474e2018-10-18 15:58:17 +02003956 EXPECT_THAT(
3957 caller()->peer_connection_state_history(),
3958 ElementsAre(PeerConnectionInterface::PeerConnectionState::kConnecting,
Jonas Olsson635474e2018-10-18 15:58:17 +02003959 PeerConnectionInterface::PeerConnectionState::kConnected));
Steve Antonede9ca52017-10-16 13:04:27 -07003960 EXPECT_THAT(caller()->ice_gathering_state_history(),
3961 ElementsAre(PeerConnectionInterface::kIceGatheringGathering,
3962 PeerConnectionInterface::kIceGatheringComplete));
3963
3964 // Block connections to/from the caller and wait for ICE to become
3965 // disconnected.
3966 for (const auto& caller_address : CallerAddresses()) {
3967 firewall()->AddRule(false, rtc::FP_ANY, rtc::FD_ANY, caller_address);
3968 }
Mirko Bonadei675513b2017-11-09 11:09:25 +01003969 RTC_LOG(LS_INFO) << "Firewall rules applied";
Jonas Olssonb75d9e92019-02-22 10:33:29 +01003970 ASSERT_EQ_SIMULATED_WAIT(PeerConnectionInterface::kIceConnectionDisconnected,
3971 caller()->ice_connection_state(), kDefaultTimeout,
3972 fake_clock);
3973 ASSERT_EQ_SIMULATED_WAIT(PeerConnectionInterface::kIceConnectionDisconnected,
3974 caller()->standardized_ice_connection_state(),
3975 kDefaultTimeout, fake_clock);
Steve Antonede9ca52017-10-16 13:04:27 -07003976
3977 // Let ICE re-establish by removing the firewall rules.
3978 firewall()->ClearRules();
Mirko Bonadei675513b2017-11-09 11:09:25 +01003979 RTC_LOG(LS_INFO) << "Firewall rules cleared";
Jonas Olssonb75d9e92019-02-22 10:33:29 +01003980 ASSERT_EQ_SIMULATED_WAIT(PeerConnectionInterface::kIceConnectionCompleted,
3981 caller()->ice_connection_state(), kDefaultTimeout,
3982 fake_clock);
Jonas Olssonacd8ae72019-02-25 15:26:24 +01003983 ASSERT_EQ_SIMULATED_WAIT(PeerConnectionInterface::kIceConnectionCompleted,
Jonas Olssonb75d9e92019-02-22 10:33:29 +01003984 caller()->standardized_ice_connection_state(),
3985 kDefaultTimeout, fake_clock);
Steve Antonede9ca52017-10-16 13:04:27 -07003986
3987 // According to RFC7675, if there is no response within 30 seconds then the
3988 // peer should consider the other side to have rejected the connection. This
Steve Anton83119dd2017-11-10 16:19:52 -08003989 // is signaled by the state transitioning to "failed".
Steve Antonede9ca52017-10-16 13:04:27 -07003990 constexpr int kConsentTimeout = 30000;
3991 for (const auto& caller_address : CallerAddresses()) {
3992 firewall()->AddRule(false, rtc::FP_ANY, rtc::FD_ANY, caller_address);
3993 }
Mirko Bonadei675513b2017-11-09 11:09:25 +01003994 RTC_LOG(LS_INFO) << "Firewall rules applied again";
Jonas Olssonb75d9e92019-02-22 10:33:29 +01003995 ASSERT_EQ_SIMULATED_WAIT(PeerConnectionInterface::kIceConnectionFailed,
3996 caller()->ice_connection_state(), kConsentTimeout,
3997 fake_clock);
3998 ASSERT_EQ_SIMULATED_WAIT(PeerConnectionInterface::kIceConnectionFailed,
3999 caller()->standardized_ice_connection_state(),
4000 kConsentTimeout, fake_clock);
4001
4002 // We need to manually close the peerconnections before the fake clock goes
4003 // out of scope, or we trigger a DCHECK in rtp_sender.cc when we briefly
4004 // return to using non-faked time.
4005 delete SetCallerPcWrapperAndReturnCurrent(nullptr);
4006 delete SetCalleePcWrapperAndReturnCurrent(nullptr);
4007}
4008
4009// Tests that if the connection doesn't get set up properly we eventually reach
4010// the "failed" iceConnectionState.
4011TEST_P(PeerConnectionIntegrationIceStatesTest, IceStateSetupFailure) {
4012 rtc::ScopedFakeClock fake_clock;
4013 // Some things use a time of "0" as a special value, so we need to start out
4014 // the fake clock at a nonzero time.
4015 fake_clock.AdvanceTime(TimeDelta::seconds(1));
4016
4017 // Block connections to/from the caller and wait for ICE to become
4018 // disconnected.
4019 for (const auto& caller_address : CallerAddresses()) {
4020 firewall()->AddRule(false, rtc::FP_ANY, rtc::FD_ANY, caller_address);
4021 }
4022
4023 ASSERT_TRUE(CreatePeerConnectionWrappers());
4024 ConnectFakeSignaling();
4025 SetPortAllocatorFlags();
4026 SetUpNetworkInterfaces();
4027 caller()->AddAudioVideoTracks();
4028 caller()->CreateAndSetAndSignalOffer();
4029
4030 // According to RFC7675, if there is no response within 30 seconds then the
4031 // peer should consider the other side to have rejected the connection. This
4032 // is signaled by the state transitioning to "failed".
4033 constexpr int kConsentTimeout = 30000;
4034 ASSERT_EQ_SIMULATED_WAIT(PeerConnectionInterface::kIceConnectionFailed,
4035 caller()->standardized_ice_connection_state(),
4036 kConsentTimeout, fake_clock);
4037
4038 // We need to manually close the peerconnections before the fake clock goes
4039 // out of scope, or we trigger a DCHECK in rtp_sender.cc when we briefly
4040 // return to using non-faked time.
4041 delete SetCallerPcWrapperAndReturnCurrent(nullptr);
4042 delete SetCalleePcWrapperAndReturnCurrent(nullptr);
Steve Antonede9ca52017-10-16 13:04:27 -07004043}
4044
4045// Tests that the best connection is set to the appropriate IPv4/IPv6 connection
4046// and that the statistics in the metric observers are updated correctly.
4047TEST_P(PeerConnectionIntegrationIceStatesTest, VerifyBestConnection) {
4048 ASSERT_TRUE(CreatePeerConnectionWrappers());
4049 ConnectFakeSignaling();
4050 SetPortAllocatorFlags();
4051 SetUpNetworkInterfaces();
Steve Anton15324772018-01-16 10:26:49 -08004052 caller()->AddAudioVideoTracks();
4053 callee()->AddAudioVideoTracks();
Steve Antonede9ca52017-10-16 13:04:27 -07004054 caller()->CreateAndSetAndSignalOffer();
4055
4056 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4057
Qingsi Wang7fc821d2018-07-12 12:54:53 -07004058 // TODO(bugs.webrtc.org/9456): Fix it.
4059 const int num_best_ipv4 = webrtc::metrics::NumEvents(
4060 "WebRTC.PeerConnection.IPMetrics", webrtc::kBestConnections_IPv4);
4061 const int num_best_ipv6 = webrtc::metrics::NumEvents(
4062 "WebRTC.PeerConnection.IPMetrics", webrtc::kBestConnections_IPv6);
Steve Antonede9ca52017-10-16 13:04:27 -07004063 if (TestIPv6()) {
4064 // When IPv6 is enabled, we should prefer an IPv6 connection over an IPv4
4065 // connection.
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02004066 EXPECT_EQ(0, num_best_ipv4);
4067 EXPECT_EQ(1, num_best_ipv6);
Steve Antonede9ca52017-10-16 13:04:27 -07004068 } else {
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02004069 EXPECT_EQ(1, num_best_ipv4);
4070 EXPECT_EQ(0, num_best_ipv6);
Steve Antonede9ca52017-10-16 13:04:27 -07004071 }
4072
Qingsi Wang7fc821d2018-07-12 12:54:53 -07004073 EXPECT_EQ(0, webrtc::metrics::NumEvents(
4074 "WebRTC.PeerConnection.CandidatePairType_UDP",
4075 webrtc::kIceCandidatePairHostHost));
4076 EXPECT_EQ(1, webrtc::metrics::NumEvents(
4077 "WebRTC.PeerConnection.CandidatePairType_UDP",
4078 webrtc::kIceCandidatePairHostPublicHostPublic));
Steve Antonede9ca52017-10-16 13:04:27 -07004079}
4080
4081constexpr uint32_t kFlagsIPv4NoStun = cricket::PORTALLOCATOR_DISABLE_TCP |
4082 cricket::PORTALLOCATOR_DISABLE_STUN |
4083 cricket::PORTALLOCATOR_DISABLE_RELAY;
4084constexpr uint32_t kFlagsIPv6NoStun =
4085 cricket::PORTALLOCATOR_DISABLE_TCP | cricket::PORTALLOCATOR_DISABLE_STUN |
4086 cricket::PORTALLOCATOR_ENABLE_IPV6 | cricket::PORTALLOCATOR_DISABLE_RELAY;
4087constexpr uint32_t kFlagsIPv4Stun =
4088 cricket::PORTALLOCATOR_DISABLE_TCP | cricket::PORTALLOCATOR_DISABLE_RELAY;
4089
Mirko Bonadeic84f6612019-01-31 12:20:57 +01004090INSTANTIATE_TEST_SUITE_P(
Seth Hampson2f0d7022018-02-20 11:54:42 -08004091 PeerConnectionIntegrationTest,
4092 PeerConnectionIntegrationIceStatesTest,
4093 Combine(Values(SdpSemantics::kPlanB, SdpSemantics::kUnifiedPlan),
4094 Values(std::make_pair("IPv4 no STUN", kFlagsIPv4NoStun),
4095 std::make_pair("IPv6 no STUN", kFlagsIPv6NoStun),
4096 std::make_pair("IPv4 with STUN", kFlagsIPv4Stun))));
Steve Antonede9ca52017-10-16 13:04:27 -07004097
deadbeef1dcb1642017-03-29 21:08:16 -07004098// This test sets up a call between two parties with audio and video.
4099// During the call, the caller restarts ICE and the test verifies that
4100// new ICE candidates are generated and audio and video still can flow, and the
4101// ICE state reaches completed again.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004102TEST_P(PeerConnectionIntegrationTest, MediaContinuesFlowingAfterIceRestart) {
deadbeef1dcb1642017-03-29 21:08:16 -07004103 ASSERT_TRUE(CreatePeerConnectionWrappers());
4104 ConnectFakeSignaling();
4105 // Do normal offer/answer and wait for ICE to complete.
Steve Anton15324772018-01-16 10:26:49 -08004106 caller()->AddAudioVideoTracks();
4107 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07004108 caller()->CreateAndSetAndSignalOffer();
4109 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4110 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
4111 caller()->ice_connection_state(), kMaxWaitForFramesMs);
Alex Loiko9289eda2018-11-23 16:18:59 +00004112 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
4113 callee()->ice_connection_state(), kMaxWaitForFramesMs);
deadbeef1dcb1642017-03-29 21:08:16 -07004114
4115 // To verify that the ICE restart actually occurs, get
4116 // ufrag/password/candidates before and after restart.
4117 // Create an SDP string of the first audio candidate for both clients.
4118 const webrtc::IceCandidateCollection* audio_candidates_caller =
4119 caller()->pc()->local_description()->candidates(0);
4120 const webrtc::IceCandidateCollection* audio_candidates_callee =
4121 callee()->pc()->local_description()->candidates(0);
4122 ASSERT_GT(audio_candidates_caller->count(), 0u);
4123 ASSERT_GT(audio_candidates_callee->count(), 0u);
4124 std::string caller_candidate_pre_restart;
4125 ASSERT_TRUE(
4126 audio_candidates_caller->at(0)->ToString(&caller_candidate_pre_restart));
4127 std::string callee_candidate_pre_restart;
4128 ASSERT_TRUE(
4129 audio_candidates_callee->at(0)->ToString(&callee_candidate_pre_restart));
4130 const cricket::SessionDescription* desc =
4131 caller()->pc()->local_description()->description();
4132 std::string caller_ufrag_pre_restart =
4133 desc->transport_infos()[0].description.ice_ufrag;
4134 desc = callee()->pc()->local_description()->description();
4135 std::string callee_ufrag_pre_restart =
4136 desc->transport_infos()[0].description.ice_ufrag;
4137
4138 // Have the caller initiate an ICE restart.
4139 caller()->SetOfferAnswerOptions(IceRestartOfferAnswerOptions());
4140 caller()->CreateAndSetAndSignalOffer();
4141 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4142 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
4143 caller()->ice_connection_state(), kMaxWaitForFramesMs);
Alex Loiko9289eda2018-11-23 16:18:59 +00004144 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
deadbeef1dcb1642017-03-29 21:08:16 -07004145 callee()->ice_connection_state(), kMaxWaitForFramesMs);
4146
4147 // Grab the ufrags/candidates again.
4148 audio_candidates_caller = caller()->pc()->local_description()->candidates(0);
4149 audio_candidates_callee = callee()->pc()->local_description()->candidates(0);
4150 ASSERT_GT(audio_candidates_caller->count(), 0u);
4151 ASSERT_GT(audio_candidates_callee->count(), 0u);
4152 std::string caller_candidate_post_restart;
4153 ASSERT_TRUE(
4154 audio_candidates_caller->at(0)->ToString(&caller_candidate_post_restart));
4155 std::string callee_candidate_post_restart;
4156 ASSERT_TRUE(
4157 audio_candidates_callee->at(0)->ToString(&callee_candidate_post_restart));
4158 desc = caller()->pc()->local_description()->description();
4159 std::string caller_ufrag_post_restart =
4160 desc->transport_infos()[0].description.ice_ufrag;
4161 desc = callee()->pc()->local_description()->description();
4162 std::string callee_ufrag_post_restart =
4163 desc->transport_infos()[0].description.ice_ufrag;
4164 // Sanity check that an ICE restart was actually negotiated in SDP.
4165 ASSERT_NE(caller_candidate_pre_restart, caller_candidate_post_restart);
4166 ASSERT_NE(callee_candidate_pre_restart, callee_candidate_post_restart);
4167 ASSERT_NE(caller_ufrag_pre_restart, caller_ufrag_post_restart);
4168 ASSERT_NE(callee_ufrag_pre_restart, callee_ufrag_post_restart);
4169
4170 // Ensure that additional frames are received after the ICE restart.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004171 MediaExpectations media_expectations;
4172 media_expectations.ExpectBidirectionalAudioAndVideo();
4173 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07004174}
4175
4176// Verify that audio/video can be received end-to-end when ICE renomination is
4177// enabled.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004178TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithIceRenomination) {
deadbeef1dcb1642017-03-29 21:08:16 -07004179 PeerConnectionInterface::RTCConfiguration config;
4180 config.enable_ice_renomination = true;
4181 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
4182 ConnectFakeSignaling();
4183 // Do normal offer/answer and wait for some frames to be received in each
4184 // direction.
Steve Anton15324772018-01-16 10:26:49 -08004185 caller()->AddAudioVideoTracks();
4186 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07004187 caller()->CreateAndSetAndSignalOffer();
4188 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4189 // Sanity check that ICE renomination was actually negotiated.
4190 const cricket::SessionDescription* desc =
4191 caller()->pc()->local_description()->description();
4192 for (const cricket::TransportInfo& info : desc->transport_infos()) {
Steve Anton64b626b2019-01-28 17:25:26 -08004193 ASSERT_THAT(info.description.transport_options, Contains("renomination"));
deadbeef1dcb1642017-03-29 21:08:16 -07004194 }
4195 desc = callee()->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 }
Seth Hampson2f0d7022018-02-20 11:54:42 -08004199 MediaExpectations media_expectations;
4200 media_expectations.ExpectBidirectionalAudioAndVideo();
4201 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07004202}
4203
Steve Anton6f25b092017-10-23 09:39:20 -07004204// With a max bundle policy and RTCP muxing, adding a new media description to
4205// the connection should not affect ICE at all because the new media will use
4206// the existing connection.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004207TEST_P(PeerConnectionIntegrationTest,
Steve Anton83119dd2017-11-10 16:19:52 -08004208 AddMediaToConnectedBundleDoesNotRestartIce) {
Steve Anton6f25b092017-10-23 09:39:20 -07004209 PeerConnectionInterface::RTCConfiguration config;
4210 config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
4211 config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyRequire;
4212 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(
4213 config, PeerConnectionInterface::RTCConfiguration()));
4214 ConnectFakeSignaling();
4215
Steve Anton15324772018-01-16 10:26:49 -08004216 caller()->AddAudioTrack();
Steve Anton6f25b092017-10-23 09:39:20 -07004217 caller()->CreateAndSetAndSignalOffer();
4218 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Steve Antonff52f1b2017-10-26 12:24:50 -07004219 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceConnectionCompleted,
4220 caller()->ice_connection_state(), kDefaultTimeout);
Steve Anton6f25b092017-10-23 09:39:20 -07004221
4222 caller()->clear_ice_connection_state_history();
4223
Steve Anton15324772018-01-16 10:26:49 -08004224 caller()->AddVideoTrack();
Steve Anton6f25b092017-10-23 09:39:20 -07004225 caller()->CreateAndSetAndSignalOffer();
4226 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4227
4228 EXPECT_EQ(0u, caller()->ice_connection_state_history().size());
4229}
4230
deadbeef1dcb1642017-03-29 21:08:16 -07004231// This test sets up a call between two parties with audio and video. It then
4232// renegotiates setting the video m-line to "port 0", then later renegotiates
4233// again, enabling video.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004234TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07004235 VideoFlowsAfterMediaSectionIsRejectedAndRecycled) {
4236 ASSERT_TRUE(CreatePeerConnectionWrappers());
4237 ConnectFakeSignaling();
4238
4239 // Do initial negotiation, only sending media from the caller. Will result in
4240 // video and audio recvonly "m=" sections.
Steve Anton15324772018-01-16 10:26:49 -08004241 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07004242 caller()->CreateAndSetAndSignalOffer();
4243 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4244
4245 // Negotiate again, disabling the video "m=" section (the callee will set the
4246 // port to 0 due to offer_to_receive_video = 0).
Seth Hampson2f0d7022018-02-20 11:54:42 -08004247 if (sdp_semantics_ == SdpSemantics::kPlanB) {
4248 PeerConnectionInterface::RTCOfferAnswerOptions options;
4249 options.offer_to_receive_video = 0;
4250 callee()->SetOfferAnswerOptions(options);
4251 } else {
4252 callee()->SetRemoteOfferHandler([this] {
4253 callee()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)->Stop();
4254 });
4255 }
deadbeef1dcb1642017-03-29 21:08:16 -07004256 caller()->CreateAndSetAndSignalOffer();
4257 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4258 // Sanity check that video "m=" section was actually rejected.
4259 const ContentInfo* answer_video_content = cricket::GetFirstVideoContent(
4260 callee()->pc()->local_description()->description());
4261 ASSERT_NE(nullptr, answer_video_content);
4262 ASSERT_TRUE(answer_video_content->rejected);
4263
4264 // Enable video and do negotiation again, making sure video is received
4265 // end-to-end, also adding media stream to callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004266 if (sdp_semantics_ == SdpSemantics::kPlanB) {
4267 PeerConnectionInterface::RTCOfferAnswerOptions options;
4268 options.offer_to_receive_video = 1;
4269 callee()->SetOfferAnswerOptions(options);
4270 } else {
4271 // The caller's transceiver is stopped, so we need to add another track.
4272 auto caller_transceiver =
4273 caller()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO);
4274 EXPECT_TRUE(caller_transceiver->stopped());
4275 caller()->AddVideoTrack();
4276 }
4277 callee()->AddVideoTrack();
4278 callee()->SetRemoteOfferHandler(nullptr);
deadbeef1dcb1642017-03-29 21:08:16 -07004279 caller()->CreateAndSetAndSignalOffer();
4280 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08004281
deadbeef1dcb1642017-03-29 21:08:16 -07004282 // Verify the caller receives frames from the newly added stream, and the
4283 // callee receives additional frames from the re-enabled video m= section.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004284 MediaExpectations media_expectations;
4285 media_expectations.CalleeExpectsSomeAudio();
4286 media_expectations.ExpectBidirectionalVideo();
4287 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07004288}
4289
deadbeef1dcb1642017-03-29 21:08:16 -07004290// This tests that if we negotiate after calling CreateSender but before we
4291// have a track, then set a track later, frames from the newly-set track are
4292// received end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004293TEST_F(PeerConnectionIntegrationTestPlanB,
deadbeef1dcb1642017-03-29 21:08:16 -07004294 MediaFlowsAfterEarlyWarmupWithCreateSender) {
4295 ASSERT_TRUE(CreatePeerConnectionWrappers());
4296 ConnectFakeSignaling();
4297 auto caller_audio_sender =
4298 caller()->pc()->CreateSender("audio", "caller_stream");
4299 auto caller_video_sender =
4300 caller()->pc()->CreateSender("video", "caller_stream");
4301 auto callee_audio_sender =
4302 callee()->pc()->CreateSender("audio", "callee_stream");
4303 auto callee_video_sender =
4304 callee()->pc()->CreateSender("video", "callee_stream");
4305 caller()->CreateAndSetAndSignalOffer();
4306 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
4307 // Wait for ICE to complete, without any tracks being set.
4308 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
4309 caller()->ice_connection_state(), kMaxWaitForFramesMs);
4310 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
4311 callee()->ice_connection_state(), kMaxWaitForFramesMs);
4312 // Now set the tracks, and expect frames to immediately start flowing.
4313 EXPECT_TRUE(caller_audio_sender->SetTrack(caller()->CreateLocalAudioTrack()));
4314 EXPECT_TRUE(caller_video_sender->SetTrack(caller()->CreateLocalVideoTrack()));
4315 EXPECT_TRUE(callee_audio_sender->SetTrack(callee()->CreateLocalAudioTrack()));
4316 EXPECT_TRUE(callee_video_sender->SetTrack(callee()->CreateLocalVideoTrack()));
Seth Hampson2f0d7022018-02-20 11:54:42 -08004317 MediaExpectations media_expectations;
4318 media_expectations.ExpectBidirectionalAudioAndVideo();
4319 ASSERT_TRUE(ExpectNewFrames(media_expectations));
4320}
4321
4322// This tests that if we negotiate after calling AddTransceiver but before we
4323// have a track, then set a track later, frames from the newly-set tracks are
4324// received end-to-end.
4325TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
4326 MediaFlowsAfterEarlyWarmupWithAddTransceiver) {
4327 ASSERT_TRUE(CreatePeerConnectionWrappers());
4328 ConnectFakeSignaling();
4329 auto audio_result = caller()->pc()->AddTransceiver(cricket::MEDIA_TYPE_AUDIO);
4330 ASSERT_EQ(RTCErrorType::NONE, audio_result.error().type());
4331 auto caller_audio_sender = audio_result.MoveValue()->sender();
4332 auto video_result = caller()->pc()->AddTransceiver(cricket::MEDIA_TYPE_VIDEO);
4333 ASSERT_EQ(RTCErrorType::NONE, video_result.error().type());
4334 auto caller_video_sender = video_result.MoveValue()->sender();
4335 callee()->SetRemoteOfferHandler([this] {
4336 ASSERT_EQ(2u, callee()->pc()->GetTransceivers().size());
4337 callee()->pc()->GetTransceivers()[0]->SetDirection(
4338 RtpTransceiverDirection::kSendRecv);
4339 callee()->pc()->GetTransceivers()[1]->SetDirection(
4340 RtpTransceiverDirection::kSendRecv);
4341 });
4342 caller()->CreateAndSetAndSignalOffer();
4343 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
4344 // Wait for ICE to complete, without any tracks being set.
4345 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
4346 caller()->ice_connection_state(), kMaxWaitForFramesMs);
4347 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
4348 callee()->ice_connection_state(), kMaxWaitForFramesMs);
4349 // Now set the tracks, and expect frames to immediately start flowing.
4350 auto callee_audio_sender = callee()->pc()->GetSenders()[0];
4351 auto callee_video_sender = callee()->pc()->GetSenders()[1];
4352 ASSERT_TRUE(caller_audio_sender->SetTrack(caller()->CreateLocalAudioTrack()));
4353 ASSERT_TRUE(caller_video_sender->SetTrack(caller()->CreateLocalVideoTrack()));
4354 ASSERT_TRUE(callee_audio_sender->SetTrack(callee()->CreateLocalAudioTrack()));
4355 ASSERT_TRUE(callee_video_sender->SetTrack(callee()->CreateLocalVideoTrack()));
4356 MediaExpectations media_expectations;
4357 media_expectations.ExpectBidirectionalAudioAndVideo();
4358 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07004359}
4360
4361// This test verifies that a remote video track can be added via AddStream,
4362// and sent end-to-end. For this particular test, it's simply echoed back
4363// from the caller to the callee, rather than being forwarded to a third
4364// PeerConnection.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004365TEST_F(PeerConnectionIntegrationTestPlanB, CanSendRemoteVideoTrack) {
deadbeef1dcb1642017-03-29 21:08:16 -07004366 ASSERT_TRUE(CreatePeerConnectionWrappers());
4367 ConnectFakeSignaling();
4368 // Just send a video track from the caller.
Steve Anton15324772018-01-16 10:26:49 -08004369 caller()->AddVideoTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07004370 caller()->CreateAndSetAndSignalOffer();
4371 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02004372 ASSERT_EQ(1U, callee()->remote_streams()->count());
deadbeef1dcb1642017-03-29 21:08:16 -07004373
4374 // Echo the stream back, and do a new offer/anwer (initiated by callee this
4375 // time).
4376 callee()->pc()->AddStream(callee()->remote_streams()->at(0));
4377 callee()->CreateAndSetAndSignalOffer();
4378 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
4379
Seth Hampson2f0d7022018-02-20 11:54:42 -08004380 MediaExpectations media_expectations;
4381 media_expectations.ExpectBidirectionalVideo();
4382 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07004383}
4384
4385// Test that we achieve the expected end-to-end connection time, using a
4386// fake clock and simulated latency on the media and signaling paths.
4387// We use a TURN<->TURN connection because this is usually the quickest to
4388// set up initially, especially when we're confident the connection will work
4389// and can start sending media before we get a STUN response.
4390//
4391// With various optimizations enabled, here are the network delays we expect to
4392// be on the critical path:
4393// 1. 2 signaling trips: Signaling offer and offerer's TURN candidate, then
4394// signaling answer (with DTLS fingerprint).
4395// 2. 9 media hops: Rest of the DTLS handshake. 3 hops in each direction when
4396// using TURN<->TURN pair, and DTLS exchange is 4 packets,
4397// the first of which should have arrived before the answer.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004398TEST_P(PeerConnectionIntegrationTest, EndToEndConnectionTimeWithTurnTurnPair) {
deadbeef1dcb1642017-03-29 21:08:16 -07004399 rtc::ScopedFakeClock fake_clock;
4400 // Some things use a time of "0" as a special value, so we need to start out
4401 // the fake clock at a nonzero time.
4402 // TODO(deadbeef): Fix this.
Sebastian Jansson5f83cf02018-05-08 14:52:22 +02004403 fake_clock.AdvanceTime(webrtc::TimeDelta::seconds(1));
deadbeef1dcb1642017-03-29 21:08:16 -07004404
4405 static constexpr int media_hop_delay_ms = 50;
4406 static constexpr int signaling_trip_delay_ms = 500;
4407 // For explanation of these values, see comment above.
4408 static constexpr int required_media_hops = 9;
4409 static constexpr int required_signaling_trips = 2;
4410 // For internal delays (such as posting an event asychronously).
4411 static constexpr int allowed_internal_delay_ms = 20;
4412 static constexpr int total_connection_time_ms =
4413 media_hop_delay_ms * required_media_hops +
4414 signaling_trip_delay_ms * required_signaling_trips +
4415 allowed_internal_delay_ms;
4416
4417 static const rtc::SocketAddress turn_server_1_internal_address{"88.88.88.0",
4418 3478};
4419 static const rtc::SocketAddress turn_server_1_external_address{"88.88.88.1",
4420 0};
4421 static const rtc::SocketAddress turn_server_2_internal_address{"99.99.99.0",
4422 3478};
4423 static const rtc::SocketAddress turn_server_2_external_address{"99.99.99.1",
4424 0};
Seth Hampsonaed71642018-06-11 07:41:32 -07004425 cricket::TestTurnServer* turn_server_1 = CreateTurnServer(
4426 turn_server_1_internal_address, turn_server_1_external_address);
Jonas Orelandbdcee282017-10-10 14:01:40 +02004427
Seth Hampsonaed71642018-06-11 07:41:32 -07004428 cricket::TestTurnServer* turn_server_2 = CreateTurnServer(
4429 turn_server_2_internal_address, turn_server_2_external_address);
deadbeef1dcb1642017-03-29 21:08:16 -07004430 // Bypass permission check on received packets so media can be sent before
4431 // the candidate is signaled.
Seth Hampsonaed71642018-06-11 07:41:32 -07004432 network_thread()->Invoke<void>(RTC_FROM_HERE, [turn_server_1] {
4433 turn_server_1->set_enable_permission_checks(false);
4434 });
4435 network_thread()->Invoke<void>(RTC_FROM_HERE, [turn_server_2] {
4436 turn_server_2->set_enable_permission_checks(false);
4437 });
deadbeef1dcb1642017-03-29 21:08:16 -07004438
4439 PeerConnectionInterface::RTCConfiguration client_1_config;
4440 webrtc::PeerConnectionInterface::IceServer ice_server_1;
4441 ice_server_1.urls.push_back("turn:88.88.88.0:3478");
4442 ice_server_1.username = "test";
4443 ice_server_1.password = "test";
4444 client_1_config.servers.push_back(ice_server_1);
4445 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
4446 client_1_config.presume_writable_when_fully_relayed = true;
4447
4448 PeerConnectionInterface::RTCConfiguration client_2_config;
4449 webrtc::PeerConnectionInterface::IceServer ice_server_2;
4450 ice_server_2.urls.push_back("turn:99.99.99.0:3478");
4451 ice_server_2.username = "test";
4452 ice_server_2.password = "test";
4453 client_2_config.servers.push_back(ice_server_2);
4454 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
4455 client_2_config.presume_writable_when_fully_relayed = true;
4456
4457 ASSERT_TRUE(
4458 CreatePeerConnectionWrappersWithConfig(client_1_config, client_2_config));
4459 // Set up the simulated delays.
4460 SetSignalingDelayMs(signaling_trip_delay_ms);
4461 ConnectFakeSignaling();
4462 virtual_socket_server()->set_delay_mean(media_hop_delay_ms);
4463 virtual_socket_server()->UpdateDelayDistribution();
4464
4465 // Set "offer to receive audio/video" without adding any tracks, so we just
4466 // set up ICE/DTLS with no media.
4467 PeerConnectionInterface::RTCOfferAnswerOptions options;
4468 options.offer_to_receive_audio = 1;
4469 options.offer_to_receive_video = 1;
4470 caller()->SetOfferAnswerOptions(options);
4471 caller()->CreateAndSetAndSignalOffer();
deadbeef71452802017-05-07 17:21:01 -07004472 EXPECT_TRUE_SIMULATED_WAIT(DtlsConnected(), total_connection_time_ms,
4473 fake_clock);
Seth Hampson1d4a76d2018-06-19 14:31:41 -07004474 // Closing the PeerConnections destroys the ports before the ScopedFakeClock.
4475 // If this is not done a DCHECK can be hit in ports.cc, because a large
4476 // negative number is calculated for the rtt due to the global clock changing.
4477 caller()->pc()->Close();
4478 callee()->pc()->Close();
deadbeef1dcb1642017-03-29 21:08:16 -07004479}
4480
Jonas Orelandbdcee282017-10-10 14:01:40 +02004481// Verify that a TurnCustomizer passed in through RTCConfiguration
4482// is actually used by the underlying TURN candidate pair.
4483// Note that turnport_unittest.cc contains more detailed, lower-level tests.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004484TEST_P(PeerConnectionIntegrationTest, TurnCustomizerUsedForTurnConnections) {
Jonas Orelandbdcee282017-10-10 14:01:40 +02004485 static const rtc::SocketAddress turn_server_1_internal_address{"88.88.88.0",
4486 3478};
4487 static const rtc::SocketAddress turn_server_1_external_address{"88.88.88.1",
4488 0};
4489 static const rtc::SocketAddress turn_server_2_internal_address{"99.99.99.0",
4490 3478};
4491 static const rtc::SocketAddress turn_server_2_external_address{"99.99.99.1",
4492 0};
Seth Hampsonaed71642018-06-11 07:41:32 -07004493 CreateTurnServer(turn_server_1_internal_address,
4494 turn_server_1_external_address);
4495 CreateTurnServer(turn_server_2_internal_address,
4496 turn_server_2_external_address);
Jonas Orelandbdcee282017-10-10 14:01:40 +02004497
4498 PeerConnectionInterface::RTCConfiguration client_1_config;
4499 webrtc::PeerConnectionInterface::IceServer ice_server_1;
4500 ice_server_1.urls.push_back("turn:88.88.88.0:3478");
4501 ice_server_1.username = "test";
4502 ice_server_1.password = "test";
4503 client_1_config.servers.push_back(ice_server_1);
4504 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
Seth Hampsonaed71642018-06-11 07:41:32 -07004505 auto* customizer1 = CreateTurnCustomizer();
4506 client_1_config.turn_customizer = customizer1;
Jonas Orelandbdcee282017-10-10 14:01:40 +02004507
4508 PeerConnectionInterface::RTCConfiguration client_2_config;
4509 webrtc::PeerConnectionInterface::IceServer ice_server_2;
4510 ice_server_2.urls.push_back("turn:99.99.99.0:3478");
4511 ice_server_2.username = "test";
4512 ice_server_2.password = "test";
4513 client_2_config.servers.push_back(ice_server_2);
4514 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
Seth Hampsonaed71642018-06-11 07:41:32 -07004515 auto* customizer2 = CreateTurnCustomizer();
4516 client_2_config.turn_customizer = customizer2;
Jonas Orelandbdcee282017-10-10 14:01:40 +02004517
4518 ASSERT_TRUE(
4519 CreatePeerConnectionWrappersWithConfig(client_1_config, client_2_config));
4520 ConnectFakeSignaling();
4521
4522 // Set "offer to receive audio/video" without adding any tracks, so we just
4523 // set up ICE/DTLS with no media.
4524 PeerConnectionInterface::RTCOfferAnswerOptions options;
4525 options.offer_to_receive_audio = 1;
4526 options.offer_to_receive_video = 1;
4527 caller()->SetOfferAnswerOptions(options);
4528 caller()->CreateAndSetAndSignalOffer();
4529 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
4530
Seth Hampsonaed71642018-06-11 07:41:32 -07004531 ExpectTurnCustomizerCountersIncremented(customizer1);
4532 ExpectTurnCustomizerCountersIncremented(customizer2);
Jonas Orelandbdcee282017-10-10 14:01:40 +02004533}
4534
Benjamin Wright2d5f3cb2018-05-22 14:46:06 -07004535// Verifies that you can use TCP instead of UDP to connect to a TURN server and
4536// send media between the caller and the callee.
4537TEST_P(PeerConnectionIntegrationTest, TCPUsedForTurnConnections) {
4538 static const rtc::SocketAddress turn_server_internal_address{"88.88.88.0",
4539 3478};
4540 static const rtc::SocketAddress turn_server_external_address{"88.88.88.1", 0};
4541
4542 // Enable TCP for the fake turn server.
Seth Hampsonaed71642018-06-11 07:41:32 -07004543 CreateTurnServer(turn_server_internal_address, turn_server_external_address,
4544 cricket::PROTO_TCP);
Benjamin Wright2d5f3cb2018-05-22 14:46:06 -07004545
4546 webrtc::PeerConnectionInterface::IceServer ice_server;
4547 ice_server.urls.push_back("turn:88.88.88.0:3478?transport=tcp");
4548 ice_server.username = "test";
4549 ice_server.password = "test";
4550
4551 PeerConnectionInterface::RTCConfiguration client_1_config;
4552 client_1_config.servers.push_back(ice_server);
4553 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
4554
4555 PeerConnectionInterface::RTCConfiguration client_2_config;
4556 client_2_config.servers.push_back(ice_server);
4557 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
4558
4559 ASSERT_TRUE(
4560 CreatePeerConnectionWrappersWithConfig(client_1_config, client_2_config));
4561
4562 // Do normal offer/answer and wait for ICE to complete.
4563 ConnectFakeSignaling();
4564 caller()->AddAudioVideoTracks();
4565 callee()->AddAudioVideoTracks();
4566 caller()->CreateAndSetAndSignalOffer();
4567 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4568 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
4569 callee()->ice_connection_state(), kMaxWaitForFramesMs);
4570
4571 MediaExpectations media_expectations;
4572 media_expectations.ExpectBidirectionalAudioAndVideo();
4573 EXPECT_TRUE(ExpectNewFrames(media_expectations));
4574}
4575
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07004576// Verify that a SSLCertificateVerifier passed in through
4577// PeerConnectionDependencies is actually used by the underlying SSL
4578// implementation to determine whether a certificate presented by the TURN
4579// server is accepted by the client. Note that openssladapter_unittest.cc
4580// contains more detailed, lower-level tests.
4581TEST_P(PeerConnectionIntegrationTest,
4582 SSLCertificateVerifierUsedForTurnConnections) {
4583 static const rtc::SocketAddress turn_server_internal_address{"88.88.88.0",
4584 3478};
4585 static const rtc::SocketAddress turn_server_external_address{"88.88.88.1", 0};
4586
4587 // Enable TCP-TLS for the fake turn server. We need to pass in 88.88.88.0 so
4588 // that host name verification passes on the fake certificate.
Seth Hampsonaed71642018-06-11 07:41:32 -07004589 CreateTurnServer(turn_server_internal_address, turn_server_external_address,
4590 cricket::PROTO_TLS, "88.88.88.0");
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07004591
4592 webrtc::PeerConnectionInterface::IceServer ice_server;
4593 ice_server.urls.push_back("turns:88.88.88.0:3478?transport=tcp");
4594 ice_server.username = "test";
4595 ice_server.password = "test";
4596
4597 PeerConnectionInterface::RTCConfiguration client_1_config;
4598 client_1_config.servers.push_back(ice_server);
4599 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
4600
4601 PeerConnectionInterface::RTCConfiguration client_2_config;
4602 client_2_config.servers.push_back(ice_server);
4603 // Setting the type to kRelay forces the connection to go through a TURN
4604 // server.
4605 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
4606
4607 // Get a copy to the pointer so we can verify calls later.
4608 rtc::TestCertificateVerifier* client_1_cert_verifier =
4609 new rtc::TestCertificateVerifier();
4610 client_1_cert_verifier->verify_certificate_ = true;
4611 rtc::TestCertificateVerifier* client_2_cert_verifier =
4612 new rtc::TestCertificateVerifier();
4613 client_2_cert_verifier->verify_certificate_ = true;
4614
4615 // Create the dependencies with the test certificate verifier.
4616 webrtc::PeerConnectionDependencies client_1_deps(nullptr);
4617 client_1_deps.tls_cert_verifier =
4618 std::unique_ptr<rtc::TestCertificateVerifier>(client_1_cert_verifier);
4619 webrtc::PeerConnectionDependencies client_2_deps(nullptr);
4620 client_2_deps.tls_cert_verifier =
4621 std::unique_ptr<rtc::TestCertificateVerifier>(client_2_cert_verifier);
4622
4623 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndDeps(
4624 client_1_config, std::move(client_1_deps), client_2_config,
4625 std::move(client_2_deps)));
4626 ConnectFakeSignaling();
4627
4628 // Set "offer to receive audio/video" without adding any tracks, so we just
4629 // set up ICE/DTLS with no media.
4630 PeerConnectionInterface::RTCOfferAnswerOptions options;
4631 options.offer_to_receive_audio = 1;
4632 options.offer_to_receive_video = 1;
4633 caller()->SetOfferAnswerOptions(options);
4634 caller()->CreateAndSetAndSignalOffer();
4635 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
4636
4637 EXPECT_GT(client_1_cert_verifier->call_count_, 0u);
4638 EXPECT_GT(client_2_cert_verifier->call_count_, 0u);
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07004639}
4640
4641TEST_P(PeerConnectionIntegrationTest,
4642 SSLCertificateVerifierFailureUsedForTurnConnectionsFailsConnection) {
4643 static const rtc::SocketAddress turn_server_internal_address{"88.88.88.0",
4644 3478};
4645 static const rtc::SocketAddress turn_server_external_address{"88.88.88.1", 0};
4646
4647 // Enable TCP-TLS for the fake turn server. We need to pass in 88.88.88.0 so
4648 // that host name verification passes on the fake certificate.
Seth Hampsonaed71642018-06-11 07:41:32 -07004649 CreateTurnServer(turn_server_internal_address, turn_server_external_address,
4650 cricket::PROTO_TLS, "88.88.88.0");
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07004651
4652 webrtc::PeerConnectionInterface::IceServer ice_server;
4653 ice_server.urls.push_back("turns:88.88.88.0:3478?transport=tcp");
4654 ice_server.username = "test";
4655 ice_server.password = "test";
4656
4657 PeerConnectionInterface::RTCConfiguration client_1_config;
4658 client_1_config.servers.push_back(ice_server);
4659 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
4660
4661 PeerConnectionInterface::RTCConfiguration client_2_config;
4662 client_2_config.servers.push_back(ice_server);
4663 // Setting the type to kRelay forces the connection to go through a TURN
4664 // server.
4665 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
4666
4667 // Get a copy to the pointer so we can verify calls later.
4668 rtc::TestCertificateVerifier* client_1_cert_verifier =
4669 new rtc::TestCertificateVerifier();
4670 client_1_cert_verifier->verify_certificate_ = false;
4671 rtc::TestCertificateVerifier* client_2_cert_verifier =
4672 new rtc::TestCertificateVerifier();
4673 client_2_cert_verifier->verify_certificate_ = false;
4674
4675 // Create the dependencies with the test certificate verifier.
4676 webrtc::PeerConnectionDependencies client_1_deps(nullptr);
4677 client_1_deps.tls_cert_verifier =
4678 std::unique_ptr<rtc::TestCertificateVerifier>(client_1_cert_verifier);
4679 webrtc::PeerConnectionDependencies client_2_deps(nullptr);
4680 client_2_deps.tls_cert_verifier =
4681 std::unique_ptr<rtc::TestCertificateVerifier>(client_2_cert_verifier);
4682
4683 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndDeps(
4684 client_1_config, std::move(client_1_deps), client_2_config,
4685 std::move(client_2_deps)));
4686 ConnectFakeSignaling();
4687
4688 // Set "offer to receive audio/video" without adding any tracks, so we just
4689 // set up ICE/DTLS with no media.
4690 PeerConnectionInterface::RTCOfferAnswerOptions options;
4691 options.offer_to_receive_audio = 1;
4692 options.offer_to_receive_video = 1;
4693 caller()->SetOfferAnswerOptions(options);
4694 caller()->CreateAndSetAndSignalOffer();
4695 bool wait_res = true;
4696 // TODO(bugs.webrtc.org/9219): When IceConnectionState is implemented
4697 // properly, should be able to just wait for a state of "failed" instead of
4698 // waiting a fixed 10 seconds.
4699 WAIT_(DtlsConnected(), kDefaultTimeout, wait_res);
4700 ASSERT_FALSE(wait_res);
4701
4702 EXPECT_GT(client_1_cert_verifier->call_count_, 0u);
4703 EXPECT_GT(client_2_cert_verifier->call_count_, 0u);
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07004704}
4705
deadbeefc964d0b2017-04-03 10:03:35 -07004706// Test that audio and video flow end-to-end when codec names don't use the
4707// expected casing, given that they're supposed to be case insensitive. To test
4708// this, all but one codec is removed from each media description, and its
4709// casing is changed.
4710//
4711// In the past, this has regressed and caused crashes/black video, due to the
4712// fact that code at some layers was doing case-insensitive comparisons and
4713// code at other layers was not.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004714TEST_P(PeerConnectionIntegrationTest, CodecNamesAreCaseInsensitive) {
deadbeefc964d0b2017-04-03 10:03:35 -07004715 ASSERT_TRUE(CreatePeerConnectionWrappers());
4716 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08004717 caller()->AddAudioVideoTracks();
4718 callee()->AddAudioVideoTracks();
deadbeefc964d0b2017-04-03 10:03:35 -07004719
4720 // Remove all but one audio/video codec (opus and VP8), and change the
4721 // casing of the caller's generated offer.
4722 caller()->SetGeneratedSdpMunger([](cricket::SessionDescription* description) {
4723 cricket::AudioContentDescription* audio =
4724 GetFirstAudioContentDescription(description);
4725 ASSERT_NE(nullptr, audio);
4726 auto audio_codecs = audio->codecs();
4727 audio_codecs.erase(std::remove_if(audio_codecs.begin(), audio_codecs.end(),
4728 [](const cricket::AudioCodec& codec) {
4729 return codec.name != "opus";
4730 }),
4731 audio_codecs.end());
4732 ASSERT_EQ(1u, audio_codecs.size());
4733 audio_codecs[0].name = "OpUs";
4734 audio->set_codecs(audio_codecs);
4735
4736 cricket::VideoContentDescription* video =
4737 GetFirstVideoContentDescription(description);
4738 ASSERT_NE(nullptr, video);
4739 auto video_codecs = video->codecs();
4740 video_codecs.erase(std::remove_if(video_codecs.begin(), video_codecs.end(),
4741 [](const cricket::VideoCodec& codec) {
4742 return codec.name != "VP8";
4743 }),
4744 video_codecs.end());
4745 ASSERT_EQ(1u, video_codecs.size());
4746 video_codecs[0].name = "vP8";
4747 video->set_codecs(video_codecs);
4748 });
4749
4750 caller()->CreateAndSetAndSignalOffer();
4751 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4752
4753 // Verify frames are still received end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004754 MediaExpectations media_expectations;
4755 media_expectations.ExpectBidirectionalAudioAndVideo();
4756 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeefc964d0b2017-04-03 10:03:35 -07004757}
4758
Jonas Oreland49ac5952018-09-26 16:04:32 +02004759TEST_P(PeerConnectionIntegrationTest, GetSourcesAudio) {
hbos8d609f62017-04-10 07:39:05 -07004760 ASSERT_TRUE(CreatePeerConnectionWrappers());
4761 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08004762 caller()->AddAudioTrack();
hbos8d609f62017-04-10 07:39:05 -07004763 caller()->CreateAndSetAndSignalOffer();
4764 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
deadbeefd8ad7882017-04-18 16:01:17 -07004765 // Wait for one audio frame to be received by the callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004766 MediaExpectations media_expectations;
4767 media_expectations.CalleeExpectsSomeAudio(1);
4768 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Jonas Oreland49ac5952018-09-26 16:04:32 +02004769 ASSERT_EQ(callee()->pc()->GetReceivers().size(), 1u);
hbos8d609f62017-04-10 07:39:05 -07004770 auto receiver = callee()->pc()->GetReceivers()[0];
4771 ASSERT_EQ(receiver->media_type(), cricket::MEDIA_TYPE_AUDIO);
Jonas Oreland49ac5952018-09-26 16:04:32 +02004772 auto sources = receiver->GetSources();
hbos8d609f62017-04-10 07:39:05 -07004773 ASSERT_GT(receiver->GetParameters().encodings.size(), 0u);
4774 EXPECT_EQ(receiver->GetParameters().encodings[0].ssrc,
Jonas Oreland49ac5952018-09-26 16:04:32 +02004775 sources[0].source_id());
4776 EXPECT_EQ(webrtc::RtpSourceType::SSRC, sources[0].source_type());
4777}
4778
4779TEST_P(PeerConnectionIntegrationTest, GetSourcesVideo) {
4780 ASSERT_TRUE(CreatePeerConnectionWrappers());
4781 ConnectFakeSignaling();
4782 caller()->AddVideoTrack();
4783 caller()->CreateAndSetAndSignalOffer();
4784 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4785 // Wait for one video frame to be received by the callee.
4786 MediaExpectations media_expectations;
4787 media_expectations.CalleeExpectsSomeVideo(1);
4788 ASSERT_TRUE(ExpectNewFrames(media_expectations));
4789 ASSERT_EQ(callee()->pc()->GetReceivers().size(), 1u);
4790 auto receiver = callee()->pc()->GetReceivers()[0];
4791 ASSERT_EQ(receiver->media_type(), cricket::MEDIA_TYPE_VIDEO);
4792 auto sources = receiver->GetSources();
4793 ASSERT_GT(receiver->GetParameters().encodings.size(), 0u);
4794 EXPECT_EQ(receiver->GetParameters().encodings[0].ssrc,
4795 sources[0].source_id());
4796 EXPECT_EQ(webrtc::RtpSourceType::SSRC, sources[0].source_type());
hbos8d609f62017-04-10 07:39:05 -07004797}
4798
deadbeef2f425aa2017-04-14 10:41:32 -07004799// Test that if a track is removed and added again with a different stream ID,
4800// the new stream ID is successfully communicated in SDP and media continues to
4801// flow end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004802// TODO(webrtc.bugs.org/8734): This test does not work for Unified Plan because
4803// it will not reuse a transceiver that has already been sending. After creating
4804// a new transceiver it tries to create an offer with two senders of the same
4805// track ids and it fails.
4806TEST_F(PeerConnectionIntegrationTestPlanB, RemoveAndAddTrackWithNewStreamId) {
deadbeef2f425aa2017-04-14 10:41:32 -07004807 ASSERT_TRUE(CreatePeerConnectionWrappers());
4808 ConnectFakeSignaling();
4809
deadbeef2f425aa2017-04-14 10:41:32 -07004810 // Add track using stream 1, do offer/answer.
4811 rtc::scoped_refptr<webrtc::AudioTrackInterface> track =
4812 caller()->CreateLocalAudioTrack();
4813 rtc::scoped_refptr<webrtc::RtpSenderInterface> sender =
Steve Antond78323f2018-07-11 11:13:44 -07004814 caller()->AddTrack(track, {"stream_1"});
deadbeef2f425aa2017-04-14 10:41:32 -07004815 caller()->CreateAndSetAndSignalOffer();
4816 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08004817 {
4818 MediaExpectations media_expectations;
4819 media_expectations.CalleeExpectsSomeAudio(1);
4820 ASSERT_TRUE(ExpectNewFrames(media_expectations));
4821 }
deadbeef2f425aa2017-04-14 10:41:32 -07004822 // Remove the sender, and create a new one with the new stream.
4823 caller()->pc()->RemoveTrack(sender);
Steve Antond78323f2018-07-11 11:13:44 -07004824 sender = caller()->AddTrack(track, {"stream_2"});
deadbeef2f425aa2017-04-14 10:41:32 -07004825 caller()->CreateAndSetAndSignalOffer();
4826 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4827 // Wait for additional audio frames to be received by the callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004828 {
4829 MediaExpectations media_expectations;
4830 media_expectations.CalleeExpectsSomeAudio();
4831 ASSERT_TRUE(ExpectNewFrames(media_expectations));
4832 }
deadbeef2f425aa2017-04-14 10:41:32 -07004833}
4834
Seth Hampson2f0d7022018-02-20 11:54:42 -08004835TEST_P(PeerConnectionIntegrationTest, RtcEventLogOutputWriteCalled) {
Elad Alon99c3fe52017-10-13 16:29:40 +02004836 ASSERT_TRUE(CreatePeerConnectionWrappers());
4837 ConnectFakeSignaling();
4838
Karl Wiberg918f50c2018-07-05 11:40:33 +02004839 auto output = absl::make_unique<testing::NiceMock<MockRtcEventLogOutput>>();
Elad Alon99c3fe52017-10-13 16:29:40 +02004840 ON_CALL(*output, IsActive()).WillByDefault(testing::Return(true));
4841 ON_CALL(*output, Write(::testing::_)).WillByDefault(testing::Return(true));
4842 EXPECT_CALL(*output, Write(::testing::_)).Times(::testing::AtLeast(1));
Bjorn Tereliusde939432017-11-20 17:38:14 +01004843 EXPECT_TRUE(caller()->pc()->StartRtcEventLog(
4844 std::move(output), webrtc::RtcEventLog::kImmediateOutput));
Elad Alon99c3fe52017-10-13 16:29:40 +02004845
Steve Anton15324772018-01-16 10:26:49 -08004846 caller()->AddAudioVideoTracks();
Elad Alon99c3fe52017-10-13 16:29:40 +02004847 caller()->CreateAndSetAndSignalOffer();
4848 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4849}
4850
Steve Antonede9ca52017-10-16 13:04:27 -07004851// Test that if candidates are only signaled by applying full session
4852// descriptions (instead of using AddIceCandidate), the peers can connect to
4853// each other and exchange media.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004854TEST_P(PeerConnectionIntegrationTest, MediaFlowsWhenCandidatesSetOnlyInSdp) {
Steve Antonede9ca52017-10-16 13:04:27 -07004855 ASSERT_TRUE(CreatePeerConnectionWrappers());
4856 // Each side will signal the session descriptions but not candidates.
4857 ConnectFakeSignalingForSdpOnly();
4858
4859 // Add audio video track and exchange the initial offer/answer with media
4860 // information only. This will start ICE gathering on each side.
Steve Anton15324772018-01-16 10:26:49 -08004861 caller()->AddAudioVideoTracks();
4862 callee()->AddAudioVideoTracks();
Steve Antonede9ca52017-10-16 13:04:27 -07004863 caller()->CreateAndSetAndSignalOffer();
4864
4865 // Wait for all candidates to be gathered on both the caller and callee.
4866 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceGatheringComplete,
4867 caller()->ice_gathering_state(), kDefaultTimeout);
4868 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceGatheringComplete,
4869 callee()->ice_gathering_state(), kDefaultTimeout);
4870
4871 // The candidates will now be included in the session description, so
4872 // signaling them will start the ICE connection.
4873 caller()->CreateAndSetAndSignalOffer();
4874 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4875
4876 // Ensure that media flows in both directions.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004877 MediaExpectations media_expectations;
4878 media_expectations.ExpectBidirectionalAudioAndVideo();
4879 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Antonede9ca52017-10-16 13:04:27 -07004880}
4881
henrika5f6bf242017-11-01 11:06:56 +01004882// Test that SetAudioPlayout can be used to disable audio playout from the
4883// start, then later enable it. This may be useful, for example, if the caller
4884// needs to play a local ringtone until some event occurs, after which it
4885// switches to playing the received audio.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004886TEST_P(PeerConnectionIntegrationTest, DisableAndEnableAudioPlayout) {
henrika5f6bf242017-11-01 11:06:56 +01004887 ASSERT_TRUE(CreatePeerConnectionWrappers());
4888 ConnectFakeSignaling();
4889
4890 // Set up audio-only call where audio playout is disabled on caller's side.
4891 caller()->pc()->SetAudioPlayout(false);
Steve Anton15324772018-01-16 10:26:49 -08004892 caller()->AddAudioTrack();
4893 callee()->AddAudioTrack();
henrika5f6bf242017-11-01 11:06:56 +01004894 caller()->CreateAndSetAndSignalOffer();
4895 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4896
4897 // Pump messages for a second.
4898 WAIT(false, 1000);
4899 // Since audio playout is disabled, the caller shouldn't have received
4900 // anything (at the playout level, at least).
4901 EXPECT_EQ(0, caller()->audio_frames_received());
4902 // As a sanity check, make sure the callee (for which playout isn't disabled)
4903 // did still see frames on its audio level.
4904 ASSERT_GT(callee()->audio_frames_received(), 0);
4905
4906 // Enable playout again, and ensure audio starts flowing.
4907 caller()->pc()->SetAudioPlayout(true);
Seth Hampson2f0d7022018-02-20 11:54:42 -08004908 MediaExpectations media_expectations;
4909 media_expectations.ExpectBidirectionalAudio();
4910 ASSERT_TRUE(ExpectNewFrames(media_expectations));
henrika5f6bf242017-11-01 11:06:56 +01004911}
4912
4913double GetAudioEnergyStat(PeerConnectionWrapper* pc) {
4914 auto report = pc->NewGetStats();
4915 auto track_stats_list =
4916 report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
4917 const webrtc::RTCMediaStreamTrackStats* remote_track_stats = nullptr;
4918 for (const auto* track_stats : track_stats_list) {
4919 if (track_stats->remote_source.is_defined() &&
4920 *track_stats->remote_source) {
4921 remote_track_stats = track_stats;
4922 break;
4923 }
4924 }
4925
4926 if (!remote_track_stats->total_audio_energy.is_defined()) {
4927 return 0.0;
4928 }
4929 return *remote_track_stats->total_audio_energy;
4930}
4931
4932// Test that if audio playout is disabled via the SetAudioPlayout() method, then
4933// incoming audio is still processed and statistics are generated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004934TEST_P(PeerConnectionIntegrationTest,
henrika5f6bf242017-11-01 11:06:56 +01004935 DisableAudioPlayoutStillGeneratesAudioStats) {
4936 ASSERT_TRUE(CreatePeerConnectionWrappers());
4937 ConnectFakeSignaling();
4938
4939 // Set up audio-only call where playout is disabled but audio-processing is
4940 // still active.
Steve Anton15324772018-01-16 10:26:49 -08004941 caller()->AddAudioTrack();
4942 callee()->AddAudioTrack();
henrika5f6bf242017-11-01 11:06:56 +01004943 caller()->pc()->SetAudioPlayout(false);
4944
4945 caller()->CreateAndSetAndSignalOffer();
4946 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4947
4948 // Wait for the callee to receive audio stats.
4949 EXPECT_TRUE_WAIT(GetAudioEnergyStat(caller()) > 0, kMaxWaitForFramesMs);
4950}
4951
henrika4f167df2017-11-01 14:45:55 +01004952// Test that SetAudioRecording can be used to disable audio recording from the
4953// start, then later enable it. This may be useful, for example, if the caller
4954// wants to ensure that no audio resources are active before a certain state
4955// is reached.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004956TEST_P(PeerConnectionIntegrationTest, DisableAndEnableAudioRecording) {
henrika4f167df2017-11-01 14:45:55 +01004957 ASSERT_TRUE(CreatePeerConnectionWrappers());
4958 ConnectFakeSignaling();
4959
4960 // Set up audio-only call where audio recording is disabled on caller's side.
4961 caller()->pc()->SetAudioRecording(false);
Steve Anton15324772018-01-16 10:26:49 -08004962 caller()->AddAudioTrack();
4963 callee()->AddAudioTrack();
henrika4f167df2017-11-01 14:45:55 +01004964 caller()->CreateAndSetAndSignalOffer();
4965 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4966
4967 // Pump messages for a second.
4968 WAIT(false, 1000);
4969 // Since caller has disabled audio recording, the callee shouldn't have
4970 // received anything.
4971 EXPECT_EQ(0, callee()->audio_frames_received());
4972 // As a sanity check, make sure the caller did still see frames on its
4973 // audio level since audio recording is enabled on the calle side.
4974 ASSERT_GT(caller()->audio_frames_received(), 0);
4975
4976 // Enable audio recording again, and ensure audio starts flowing.
4977 caller()->pc()->SetAudioRecording(true);
Seth Hampson2f0d7022018-02-20 11:54:42 -08004978 MediaExpectations media_expectations;
4979 media_expectations.ExpectBidirectionalAudio();
4980 ASSERT_TRUE(ExpectNewFrames(media_expectations));
henrika4f167df2017-11-01 14:45:55 +01004981}
4982
Taylor Brandstetter389a97c2018-01-03 16:26:06 -08004983// Test that after closing PeerConnections, they stop sending any packets (ICE,
4984// DTLS, RTP...).
Seth Hampson2f0d7022018-02-20 11:54:42 -08004985TEST_P(PeerConnectionIntegrationTest, ClosingConnectionStopsPacketFlow) {
Taylor Brandstetter389a97c2018-01-03 16:26:06 -08004986 // Set up audio/video/data, wait for some frames to be received.
4987 ASSERT_TRUE(CreatePeerConnectionWrappers());
4988 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08004989 caller()->AddAudioVideoTracks();
Taylor Brandstetter389a97c2018-01-03 16:26:06 -08004990#ifdef HAVE_SCTP
4991 caller()->CreateDataChannel();
4992#endif
4993 caller()->CreateAndSetAndSignalOffer();
4994 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08004995 MediaExpectations media_expectations;
4996 media_expectations.CalleeExpectsSomeAudioAndVideo();
4997 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Taylor Brandstetter389a97c2018-01-03 16:26:06 -08004998 // Close PeerConnections.
4999 caller()->pc()->Close();
5000 callee()->pc()->Close();
5001 // Pump messages for a second, and ensure no new packets end up sent.
5002 uint32_t sent_packets_a = virtual_socket_server()->sent_packets();
5003 WAIT(false, 1000);
5004 uint32_t sent_packets_b = virtual_socket_server()->sent_packets();
5005 EXPECT_EQ(sent_packets_a, sent_packets_b);
5006}
5007
Steve Anton7eca0932018-03-30 15:18:41 -07005008// Test that transport stats are generated by the RTCStatsCollector for a
5009// connection that only involves data channels. This is a regression test for
5010// crbug.com/826972.
5011#ifdef HAVE_SCTP
5012TEST_P(PeerConnectionIntegrationTest,
5013 TransportStatsReportedForDataChannelOnlyConnection) {
5014 ASSERT_TRUE(CreatePeerConnectionWrappers());
5015 ConnectFakeSignaling();
5016 caller()->CreateDataChannel();
5017
5018 caller()->CreateAndSetAndSignalOffer();
5019 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5020 ASSERT_TRUE_WAIT(callee()->data_channel(), kDefaultTimeout);
5021
5022 auto caller_report = caller()->NewGetStats();
5023 EXPECT_EQ(1u, caller_report->GetStatsOfType<RTCTransportStats>().size());
5024 auto callee_report = callee()->NewGetStats();
5025 EXPECT_EQ(1u, callee_report->GetStatsOfType<RTCTransportStats>().size());
5026}
5027#endif // HAVE_SCTP
5028
Qingsi Wang7685e862018-06-11 20:15:46 -07005029TEST_P(PeerConnectionIntegrationTest,
5030 IceEventsGeneratedAndLoggedInRtcEventLog) {
5031 ASSERT_TRUE(CreatePeerConnectionWrappersWithFakeRtcEventLog());
5032 ConnectFakeSignaling();
5033 PeerConnectionInterface::RTCOfferAnswerOptions options;
5034 options.offer_to_receive_audio = 1;
5035 caller()->SetOfferAnswerOptions(options);
5036 caller()->CreateAndSetAndSignalOffer();
5037 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
5038 ASSERT_NE(nullptr, caller()->event_log_factory());
5039 ASSERT_NE(nullptr, callee()->event_log_factory());
5040 webrtc::FakeRtcEventLog* caller_event_log =
5041 static_cast<webrtc::FakeRtcEventLog*>(
5042 caller()->event_log_factory()->last_log_created());
5043 webrtc::FakeRtcEventLog* callee_event_log =
5044 static_cast<webrtc::FakeRtcEventLog*>(
5045 callee()->event_log_factory()->last_log_created());
5046 ASSERT_NE(nullptr, caller_event_log);
5047 ASSERT_NE(nullptr, callee_event_log);
5048 int caller_ice_config_count = caller_event_log->GetEventCount(
5049 webrtc::RtcEvent::Type::IceCandidatePairConfig);
5050 int caller_ice_event_count = caller_event_log->GetEventCount(
5051 webrtc::RtcEvent::Type::IceCandidatePairEvent);
5052 int callee_ice_config_count = callee_event_log->GetEventCount(
5053 webrtc::RtcEvent::Type::IceCandidatePairConfig);
5054 int callee_ice_event_count = callee_event_log->GetEventCount(
5055 webrtc::RtcEvent::Type::IceCandidatePairEvent);
5056 EXPECT_LT(0, caller_ice_config_count);
5057 EXPECT_LT(0, caller_ice_event_count);
5058 EXPECT_LT(0, callee_ice_config_count);
5059 EXPECT_LT(0, callee_ice_event_count);
5060}
5061
Mirko Bonadeic84f6612019-01-31 12:20:57 +01005062INSTANTIATE_TEST_SUITE_P(PeerConnectionIntegrationTest,
5063 PeerConnectionIntegrationTest,
5064 Values(SdpSemantics::kPlanB,
5065 SdpSemantics::kUnifiedPlan));
Steve Antond3679212018-01-17 17:41:02 -08005066
Steve Anton74255ff2018-01-24 18:32:57 -08005067// Tests that verify interoperability between Plan B and Unified Plan
5068// PeerConnections.
5069class PeerConnectionIntegrationInteropTest
Seth Hampson2f0d7022018-02-20 11:54:42 -08005070 : public PeerConnectionIntegrationBaseTest,
Steve Anton74255ff2018-01-24 18:32:57 -08005071 public ::testing::WithParamInterface<
5072 std::tuple<SdpSemantics, SdpSemantics>> {
5073 protected:
Seth Hampson2f0d7022018-02-20 11:54:42 -08005074 // Setting the SdpSemantics for the base test to kDefault does not matter
5075 // because we specify not to use the test semantics when creating
5076 // PeerConnectionWrappers.
Steve Anton74255ff2018-01-24 18:32:57 -08005077 PeerConnectionIntegrationInteropTest()
Steve Anton3acffc32018-04-12 17:21:03 -07005078 : PeerConnectionIntegrationBaseTest(SdpSemantics::kPlanB),
Seth Hampson2f0d7022018-02-20 11:54:42 -08005079 caller_semantics_(std::get<0>(GetParam())),
Steve Anton74255ff2018-01-24 18:32:57 -08005080 callee_semantics_(std::get<1>(GetParam())) {}
5081
5082 bool CreatePeerConnectionWrappersWithSemantics() {
Steve Anton3acffc32018-04-12 17:21:03 -07005083 return CreatePeerConnectionWrappersWithSdpSemantics(caller_semantics_,
5084 callee_semantics_);
Steve Anton74255ff2018-01-24 18:32:57 -08005085 }
5086
5087 const SdpSemantics caller_semantics_;
5088 const SdpSemantics callee_semantics_;
5089};
5090
5091TEST_P(PeerConnectionIntegrationInteropTest, NoMediaLocalToNoMediaRemote) {
5092 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
5093 ConnectFakeSignaling();
5094
5095 caller()->CreateAndSetAndSignalOffer();
5096 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5097}
5098
5099TEST_P(PeerConnectionIntegrationInteropTest, OneAudioLocalToNoMediaRemote) {
5100 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
5101 ConnectFakeSignaling();
5102 auto audio_sender = caller()->AddAudioTrack();
5103
5104 caller()->CreateAndSetAndSignalOffer();
5105 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5106
5107 // Verify that one audio receiver has been created on the remote and that it
5108 // has the same track ID as the sending track.
5109 auto receivers = callee()->pc()->GetReceivers();
5110 ASSERT_EQ(1u, receivers.size());
5111 EXPECT_EQ(cricket::MEDIA_TYPE_AUDIO, receivers[0]->media_type());
5112 EXPECT_EQ(receivers[0]->track()->id(), audio_sender->track()->id());
5113
Seth Hampson2f0d7022018-02-20 11:54:42 -08005114 MediaExpectations media_expectations;
5115 media_expectations.CalleeExpectsSomeAudio();
5116 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08005117}
5118
5119TEST_P(PeerConnectionIntegrationInteropTest, OneAudioOneVideoToNoMediaRemote) {
5120 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
5121 ConnectFakeSignaling();
5122 auto video_sender = caller()->AddVideoTrack();
5123 auto audio_sender = caller()->AddAudioTrack();
5124
5125 caller()->CreateAndSetAndSignalOffer();
5126 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5127
5128 // Verify that one audio and one video receiver have been created on the
5129 // remote and that they have the same track IDs as the sending tracks.
5130 auto audio_receivers =
5131 callee()->GetReceiversOfType(cricket::MEDIA_TYPE_AUDIO);
5132 ASSERT_EQ(1u, audio_receivers.size());
5133 EXPECT_EQ(audio_receivers[0]->track()->id(), audio_sender->track()->id());
5134 auto video_receivers =
5135 callee()->GetReceiversOfType(cricket::MEDIA_TYPE_VIDEO);
5136 ASSERT_EQ(1u, video_receivers.size());
5137 EXPECT_EQ(video_receivers[0]->track()->id(), video_sender->track()->id());
5138
Seth Hampson2f0d7022018-02-20 11:54:42 -08005139 MediaExpectations media_expectations;
5140 media_expectations.CalleeExpectsSomeAudioAndVideo();
5141 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08005142}
5143
5144TEST_P(PeerConnectionIntegrationInteropTest,
5145 OneAudioOneVideoLocalToOneAudioOneVideoRemote) {
5146 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
5147 ConnectFakeSignaling();
5148 caller()->AddAudioVideoTracks();
5149 callee()->AddAudioVideoTracks();
5150
5151 caller()->CreateAndSetAndSignalOffer();
5152 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5153
Seth Hampson2f0d7022018-02-20 11:54:42 -08005154 MediaExpectations media_expectations;
5155 media_expectations.ExpectBidirectionalAudioAndVideo();
5156 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08005157}
5158
5159TEST_P(PeerConnectionIntegrationInteropTest,
5160 ReverseRolesOneAudioLocalToOneVideoRemote) {
5161 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
5162 ConnectFakeSignaling();
5163 caller()->AddAudioTrack();
5164 callee()->AddVideoTrack();
5165
5166 caller()->CreateAndSetAndSignalOffer();
5167 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5168
5169 // Verify that only the audio track has been negotiated.
5170 EXPECT_EQ(0u, caller()->GetReceiversOfType(cricket::MEDIA_TYPE_VIDEO).size());
5171 // Might also check that the callee's NegotiationNeeded flag is set.
5172
5173 // Reverse roles.
5174 callee()->CreateAndSetAndSignalOffer();
5175 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5176
Seth Hampson2f0d7022018-02-20 11:54:42 -08005177 MediaExpectations media_expectations;
5178 media_expectations.CallerExpectsSomeVideo();
5179 media_expectations.CalleeExpectsSomeAudio();
5180 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08005181}
5182
Mirko Bonadeic84f6612019-01-31 12:20:57 +01005183INSTANTIATE_TEST_SUITE_P(
Steve Antonba42e992018-04-09 14:10:01 -07005184 PeerConnectionIntegrationTest,
5185 PeerConnectionIntegrationInteropTest,
5186 Values(std::make_tuple(SdpSemantics::kPlanB, SdpSemantics::kUnifiedPlan),
5187 std::make_tuple(SdpSemantics::kUnifiedPlan, SdpSemantics::kPlanB)));
5188
5189// Test that if the Unified Plan side offers two video tracks then the Plan B
5190// side will only see the first one and ignore the second.
5191TEST_F(PeerConnectionIntegrationTestPlanB, TwoVideoUnifiedPlanToNoMediaPlanB) {
Steve Anton3acffc32018-04-12 17:21:03 -07005192 ASSERT_TRUE(CreatePeerConnectionWrappersWithSdpSemantics(
5193 SdpSemantics::kUnifiedPlan, SdpSemantics::kPlanB));
Steve Anton74255ff2018-01-24 18:32:57 -08005194 ConnectFakeSignaling();
5195 auto first_sender = caller()->AddVideoTrack();
5196 caller()->AddVideoTrack();
5197
5198 caller()->CreateAndSetAndSignalOffer();
5199 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5200
5201 // Verify that there is only one receiver and it corresponds to the first
5202 // added track.
5203 auto receivers = callee()->pc()->GetReceivers();
5204 ASSERT_EQ(1u, receivers.size());
5205 EXPECT_TRUE(receivers[0]->track()->enabled());
5206 EXPECT_EQ(first_sender->track()->id(), receivers[0]->track()->id());
5207
Seth Hampson2f0d7022018-02-20 11:54:42 -08005208 MediaExpectations media_expectations;
5209 media_expectations.CalleeExpectsSomeVideo();
5210 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08005211}
5212
Steve Anton2bed3972019-01-04 17:04:30 -08005213// Test that if the initial offer tagged BUNDLE section is rejected due to its
5214// associated RtpTransceiver being stopped and another transceiver is added,
5215// then renegotiation causes the callee to receive the new video track without
5216// error.
5217// This is a regression test for bugs.webrtc.org/9954
5218TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
5219 ReOfferWithStoppedBundleTaggedTransceiver) {
5220 RTCConfiguration config;
5221 config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
5222 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
5223 ConnectFakeSignaling();
5224 auto audio_transceiver_or_error =
5225 caller()->pc()->AddTransceiver(caller()->CreateLocalAudioTrack());
5226 ASSERT_TRUE(audio_transceiver_or_error.ok());
5227 auto audio_transceiver = audio_transceiver_or_error.MoveValue();
5228
5229 caller()->CreateAndSetAndSignalOffer();
5230 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5231 {
5232 MediaExpectations media_expectations;
5233 media_expectations.CalleeExpectsSomeAudio();
5234 ASSERT_TRUE(ExpectNewFrames(media_expectations));
5235 }
5236
5237 audio_transceiver->Stop();
5238 caller()->pc()->AddTransceiver(caller()->CreateLocalVideoTrack());
5239
5240 caller()->CreateAndSetAndSignalOffer();
5241 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5242 {
5243 MediaExpectations media_expectations;
5244 media_expectations.CalleeExpectsSomeVideo();
5245 ASSERT_TRUE(ExpectNewFrames(media_expectations));
5246 }
5247}
5248
deadbeef1dcb1642017-03-29 21:08:16 -07005249} // namespace
Mirko Bonadeiab64e8a2018-12-12 12:10:18 +01005250} // namespace webrtc
deadbeef1dcb1642017-03-29 21:08:16 -07005251
5252#endif // if !defined(THREAD_SANITIZER)