blob: f36ba1e52f1e8fb53ce96aa24584cee2bf86b4d0 [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"
Steve Anton10542f22019-01-11 09:11:00 -080026#include "api/media_stream_interface.h"
27#include "api/peer_connection_interface.h"
28#include "api/peer_connection_proxy.h"
Danil Chapovalov9da25bd2019-06-20 10:19:42 +020029#include "api/rtc_event_log/rtc_event_log_factory.h"
Steve Anton10542f22019-01-11 09:11:00 -080030#include "api/rtp_receiver_interface.h"
Danil Chapovalov9da25bd2019-06-20 10:19:42 +020031#include "api/task_queue/default_task_queue_factory.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/sdp_video_format.h"
Qingsi Wang7685e862018-06-11 20:15:46 -070035#include "call/call.h"
36#include "logging/rtc_event_log/fake_rtc_event_log_factory.h"
Steve Anton10542f22019-01-11 09:11:00 -080037#include "media/engine/fake_webrtc_video_engine.h"
38#include "media/engine/webrtc_media_engine.h"
Danil Chapovalov9da25bd2019-06-20 10:19:42 +020039#include "media/engine/webrtc_media_engine_defaults.h"
Steve Anton10542f22019-01-11 09:11:00 -080040#include "p2p/base/mock_async_resolver.h"
41#include "p2p/base/p2p_constants.h"
42#include "p2p/base/port_interface.h"
43#include "p2p/base/test_stun_server.h"
44#include "p2p/base/test_turn_customizer.h"
45#include "p2p/base/test_turn_server.h"
46#include "p2p/client/basic_port_allocator.h"
47#include "pc/dtmf_sender.h"
48#include "pc/local_audio_source.h"
49#include "pc/media_session.h"
50#include "pc/peer_connection.h"
51#include "pc/peer_connection_factory.h"
52#include "pc/rtp_media_utils.h"
53#include "pc/session_description.h"
54#include "pc/test/fake_audio_capture_module.h"
55#include "pc/test/fake_periodic_video_track_source.h"
56#include "pc/test/fake_rtc_certificate_generator.h"
57#include "pc/test/fake_video_track_renderer.h"
58#include "pc/test/mock_peer_connection_observers.h"
Jonas Olssonb75d9e92019-02-22 10:33:29 +010059#include "rtc_base/fake_clock.h"
Qingsi Wangecd30542019-05-22 14:34:56 -070060#include "rtc_base/fake_mdns_responder.h"
Steve Anton10542f22019-01-11 09:11:00 -080061#include "rtc_base/fake_network.h"
62#include "rtc_base/firewall_socket_server.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020063#include "rtc_base/gunit.h"
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +020064#include "rtc_base/numerics/safe_conversions.h"
Steve Anton10542f22019-01-11 09:11:00 -080065#include "rtc_base/test_certificate_verifier.h"
66#include "rtc_base/time_utils.h"
67#include "rtc_base/virtual_socket_server.h"
Mirko Bonadei17f48782018-09-28 08:51:10 +020068#include "system_wrappers/include/metrics.h"
Qingsi Wangc129c352019-04-18 10:41:58 -070069#include "test/field_trial.h"
Elad Alon99c3fe52017-10-13 16:29:40 +020070#include "test/gmock.h"
deadbeef1dcb1642017-03-29 21:08:16 -070071
Mirko Bonadeiab64e8a2018-12-12 12:10:18 +010072namespace webrtc {
73namespace {
74
75using ::cricket::ContentInfo;
76using ::cricket::StreamParams;
77using ::rtc::SocketAddress;
78using ::testing::_;
Seth Hampson2f0d7022018-02-20 11:54:42 -080079using ::testing::Combine;
Steve Anton64b626b2019-01-28 17:25:26 -080080using ::testing::Contains;
Mirko Bonadeie46f5db2019-03-26 20:14:46 +010081using ::testing::DoAll;
Steve Antonede9ca52017-10-16 13:04:27 -070082using ::testing::ElementsAre;
Qingsi Wang1dac6d82018-12-12 15:28:47 -080083using ::testing::NiceMock;
Steve Anton64b626b2019-01-28 17:25:26 -080084using ::testing::Return;
Zach Stein6fcdc2f2018-08-23 16:25:55 -070085using ::testing::SetArgPointee;
Steve Antonffa6ce42018-11-30 09:26:08 -080086using ::testing::UnorderedElementsAreArray;
Mirko Bonadeiab64e8a2018-12-12 12:10:18 +010087using ::testing::Values;
Steve Anton74255ff2018-01-24 18:32:57 -080088using RTCConfiguration = PeerConnectionInterface::RTCConfiguration;
deadbeef1dcb1642017-03-29 21:08:16 -070089
90static const int kDefaultTimeout = 10000;
91static const int kMaxWaitForStatsMs = 3000;
92static const int kMaxWaitForActivationMs = 5000;
93static const int kMaxWaitForFramesMs = 10000;
94// Default number of audio/video frames to wait for before considering a test
95// successful.
96static const int kDefaultExpectedAudioFrameCount = 3;
97static const int kDefaultExpectedVideoFrameCount = 3;
98
deadbeef1dcb1642017-03-29 21:08:16 -070099static const char kDataChannelLabel[] = "data_channel";
100
101// SRTP cipher name negotiated by the tests. This must be updated if the
102// default changes.
Taylor Brandstetterfd350d72018-04-03 16:29:26 -0700103static const int kDefaultSrtpCryptoSuite = rtc::SRTP_AES128_CM_SHA1_80;
deadbeef1dcb1642017-03-29 21:08:16 -0700104static const int kDefaultSrtpCryptoSuiteGcm = rtc::SRTP_AEAD_AES_256_GCM;
105
Steve Antonede9ca52017-10-16 13:04:27 -0700106static const SocketAddress kDefaultLocalAddress("192.168.1.1", 0);
107
deadbeef1dcb1642017-03-29 21:08:16 -0700108// Helper function for constructing offer/answer options to initiate an ICE
109// restart.
110PeerConnectionInterface::RTCOfferAnswerOptions IceRestartOfferAnswerOptions() {
111 PeerConnectionInterface::RTCOfferAnswerOptions options;
112 options.ice_restart = true;
113 return options;
114}
115
deadbeefd8ad7882017-04-18 16:01:17 -0700116// Remove all stream information (SSRCs, track IDs, etc.) and "msid-semantic"
117// attribute from received SDP, simulating a legacy endpoint.
118void RemoveSsrcsAndMsids(cricket::SessionDescription* desc) {
119 for (ContentInfo& content : desc->contents()) {
Steve Antonb1c1de12017-12-21 15:14:30 -0800120 content.media_description()->mutable_streams().clear();
deadbeefd8ad7882017-04-18 16:01:17 -0700121 }
122 desc->set_msid_supported(false);
Henrik Boström5b147782018-12-04 11:25:05 +0100123 desc->set_msid_signaling(0);
deadbeefd8ad7882017-04-18 16:01:17 -0700124}
125
Seth Hampson5897a6e2018-04-03 11:16:33 -0700126// Removes all stream information besides the stream ids, simulating an
127// endpoint that only signals a=msid lines to convey stream_ids.
128void RemoveSsrcsAndKeepMsids(cricket::SessionDescription* desc) {
129 for (ContentInfo& content : desc->contents()) {
Steve Antondf527fd2018-04-27 15:52:03 -0700130 std::string track_id;
Seth Hampson5897a6e2018-04-03 11:16:33 -0700131 std::vector<std::string> stream_ids;
132 if (!content.media_description()->streams().empty()) {
Steve Antondf527fd2018-04-27 15:52:03 -0700133 const StreamParams& first_stream =
134 content.media_description()->streams()[0];
135 track_id = first_stream.id;
136 stream_ids = first_stream.stream_ids();
Seth Hampson5897a6e2018-04-03 11:16:33 -0700137 }
138 content.media_description()->mutable_streams().clear();
Steve Antondf527fd2018-04-27 15:52:03 -0700139 StreamParams new_stream;
140 new_stream.id = track_id;
Seth Hampson5897a6e2018-04-03 11:16:33 -0700141 new_stream.set_stream_ids(stream_ids);
142 content.media_description()->AddStream(new_stream);
143 }
144}
145
zhihuangf8164932017-05-19 13:09:47 -0700146int FindFirstMediaStatsIndexByKind(
147 const std::string& kind,
148 const std::vector<const webrtc::RTCMediaStreamTrackStats*>&
149 media_stats_vec) {
150 for (size_t i = 0; i < media_stats_vec.size(); i++) {
151 if (media_stats_vec[i]->kind.ValueToString() == kind) {
152 return i;
153 }
154 }
155 return -1;
156}
157
deadbeef1dcb1642017-03-29 21:08:16 -0700158class SignalingMessageReceiver {
159 public:
Steve Antona3a92c22017-12-07 10:27:41 -0800160 virtual void ReceiveSdpMessage(SdpType type, const std::string& msg) = 0;
deadbeef1dcb1642017-03-29 21:08:16 -0700161 virtual void ReceiveIceMessage(const std::string& sdp_mid,
162 int sdp_mline_index,
163 const std::string& msg) = 0;
164
165 protected:
166 SignalingMessageReceiver() {}
167 virtual ~SignalingMessageReceiver() {}
168};
169
170class MockRtpReceiverObserver : public webrtc::RtpReceiverObserverInterface {
171 public:
172 explicit MockRtpReceiverObserver(cricket::MediaType media_type)
173 : expected_media_type_(media_type) {}
174
175 void OnFirstPacketReceived(cricket::MediaType media_type) override {
176 ASSERT_EQ(expected_media_type_, media_type);
177 first_packet_received_ = true;
178 }
179
180 bool first_packet_received() const { return first_packet_received_; }
181
182 virtual ~MockRtpReceiverObserver() {}
183
184 private:
185 bool first_packet_received_ = false;
186 cricket::MediaType expected_media_type_;
187};
188
189// Helper class that wraps a peer connection, observes it, and can accept
190// signaling messages from another wrapper.
191//
192// Uses a fake network, fake A/V capture, and optionally fake
193// encoders/decoders, though they aren't used by default since they don't
194// advertise support of any codecs.
Steve Anton94286cb2017-09-26 16:20:19 -0700195// TODO(steveanton): See how this could become a subclass of
Seth Hampson2f0d7022018-02-20 11:54:42 -0800196// PeerConnectionWrapper defined in peerconnectionwrapper.h.
deadbeef1dcb1642017-03-29 21:08:16 -0700197class PeerConnectionWrapper : public webrtc::PeerConnectionObserver,
Steve Anton15324772018-01-16 10:26:49 -0800198 public SignalingMessageReceiver {
deadbeef1dcb1642017-03-29 21:08:16 -0700199 public:
200 // Different factory methods for convenience.
201 // TODO(deadbeef): Could use the pattern of:
202 //
203 // PeerConnectionWrapper =
204 // WrapperBuilder.WithConfig(...).WithOptions(...).build();
205 //
206 // To reduce some code duplication.
207 static PeerConnectionWrapper* CreateWithDtlsIdentityStore(
208 const std::string& debug_name,
209 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator,
210 rtc::Thread* network_thread,
211 rtc::Thread* worker_thread) {
212 PeerConnectionWrapper* client(new PeerConnectionWrapper(debug_name));
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700213 webrtc::PeerConnectionDependencies dependencies(nullptr);
214 dependencies.cert_generator = std::move(cert_generator);
Niels Möllerf06f9232018-08-07 12:32:18 +0200215 if (!client->Init(nullptr, nullptr, std::move(dependencies), network_thread,
Bjorn Mellem175aa2e2018-11-08 11:23:22 -0800216 worker_thread, nullptr,
217 /*media_transport_factory=*/nullptr)) {
deadbeef1dcb1642017-03-29 21:08:16 -0700218 delete client;
219 return nullptr;
220 }
221 return client;
222 }
223
deadbeef2f425aa2017-04-14 10:41:32 -0700224 webrtc::PeerConnectionFactoryInterface* pc_factory() const {
225 return peer_connection_factory_.get();
226 }
227
deadbeef1dcb1642017-03-29 21:08:16 -0700228 webrtc::PeerConnectionInterface* pc() const { return peer_connection_.get(); }
229
230 // If a signaling message receiver is set (via ConnectFakeSignaling), this
231 // will set the whole offer/answer exchange in motion. Just need to wait for
232 // the signaling state to reach "stable".
233 void CreateAndSetAndSignalOffer() {
234 auto offer = CreateOffer();
235 ASSERT_NE(nullptr, offer);
236 EXPECT_TRUE(SetLocalDescriptionAndSendSdpMessage(std::move(offer)));
237 }
238
239 // Sets the options to be used when CreateAndSetAndSignalOffer is called, or
240 // when a remote offer is received (via fake signaling) and an answer is
241 // generated. By default, uses default options.
242 void SetOfferAnswerOptions(
243 const PeerConnectionInterface::RTCOfferAnswerOptions& options) {
244 offer_answer_options_ = options;
245 }
246
247 // Set a callback to be invoked when SDP is received via the fake signaling
248 // channel, which provides an opportunity to munge (modify) the SDP. This is
249 // used to test SDP being applied that a PeerConnection would normally not
250 // generate, but a non-JSEP endpoint might.
251 void SetReceivedSdpMunger(
252 std::function<void(cricket::SessionDescription*)> munger) {
Mirko Bonadeic61ce0d2017-11-21 17:04:20 +0100253 received_sdp_munger_ = std::move(munger);
deadbeef1dcb1642017-03-29 21:08:16 -0700254 }
255
deadbeefc964d0b2017-04-03 10:03:35 -0700256 // Similar to the above, but this is run on SDP immediately after it's
deadbeef1dcb1642017-03-29 21:08:16 -0700257 // generated.
258 void SetGeneratedSdpMunger(
259 std::function<void(cricket::SessionDescription*)> munger) {
Mirko Bonadeic61ce0d2017-11-21 17:04:20 +0100260 generated_sdp_munger_ = std::move(munger);
deadbeef1dcb1642017-03-29 21:08:16 -0700261 }
262
Seth Hampson2f0d7022018-02-20 11:54:42 -0800263 // Set a callback to be invoked when a remote offer is received via the fake
264 // signaling channel. This provides an opportunity to change the
265 // PeerConnection state before an answer is created and sent to the caller.
266 void SetRemoteOfferHandler(std::function<void()> handler) {
267 remote_offer_handler_ = std::move(handler);
268 }
269
Qingsi Wang1dac6d82018-12-12 15:28:47 -0800270 void SetRemoteAsyncResolver(rtc::MockAsyncResolver* resolver) {
271 remote_async_resolver_ = resolver;
Zach Stein6fcdc2f2018-08-23 16:25:55 -0700272 }
273
Steve Antonede9ca52017-10-16 13:04:27 -0700274 // Every ICE connection state in order that has been seen by the observer.
275 std::vector<PeerConnectionInterface::IceConnectionState>
276 ice_connection_state_history() const {
277 return ice_connection_state_history_;
278 }
Steve Anton6f25b092017-10-23 09:39:20 -0700279 void clear_ice_connection_state_history() {
280 ice_connection_state_history_.clear();
281 }
Steve Antonede9ca52017-10-16 13:04:27 -0700282
Jonas Olssonacd8ae72019-02-25 15:26:24 +0100283 // Every standardized ICE connection state in order that has been seen by the
284 // observer.
285 std::vector<PeerConnectionInterface::IceConnectionState>
286 standardized_ice_connection_state_history() const {
287 return standardized_ice_connection_state_history_;
288 }
289
Jonas Olsson635474e2018-10-18 15:58:17 +0200290 // Every PeerConnection state in order that has been seen by the observer.
291 std::vector<PeerConnectionInterface::PeerConnectionState>
292 peer_connection_state_history() const {
293 return peer_connection_state_history_;
294 }
295
Steve Antonede9ca52017-10-16 13:04:27 -0700296 // Every ICE gathering state in order that has been seen by the observer.
297 std::vector<PeerConnectionInterface::IceGatheringState>
298 ice_gathering_state_history() const {
299 return ice_gathering_state_history_;
deadbeef1dcb1642017-03-29 21:08:16 -0700300 }
301
Steve Anton15324772018-01-16 10:26:49 -0800302 void AddAudioVideoTracks() {
303 AddAudioTrack();
304 AddVideoTrack();
deadbeef1dcb1642017-03-29 21:08:16 -0700305 }
306
Steve Anton74255ff2018-01-24 18:32:57 -0800307 rtc::scoped_refptr<RtpSenderInterface> AddAudioTrack() {
308 return AddTrack(CreateLocalAudioTrack());
309 }
deadbeef1dcb1642017-03-29 21:08:16 -0700310
Steve Anton74255ff2018-01-24 18:32:57 -0800311 rtc::scoped_refptr<RtpSenderInterface> AddVideoTrack() {
312 return AddTrack(CreateLocalVideoTrack());
313 }
deadbeef1dcb1642017-03-29 21:08:16 -0700314
315 rtc::scoped_refptr<webrtc::AudioTrackInterface> CreateLocalAudioTrack() {
Niels Möller2d02e082018-05-21 11:23:35 +0200316 cricket::AudioOptions options;
deadbeef1dcb1642017-03-29 21:08:16 -0700317 // Disable highpass filter so that we can get all the test audio frames.
Niels Möller2d02e082018-05-21 11:23:35 +0200318 options.highpass_filter = false;
deadbeef1dcb1642017-03-29 21:08:16 -0700319 rtc::scoped_refptr<webrtc::AudioSourceInterface> source =
Niels Möller2d02e082018-05-21 11:23:35 +0200320 peer_connection_factory_->CreateAudioSource(options);
deadbeef1dcb1642017-03-29 21:08:16 -0700321 // TODO(perkj): Test audio source when it is implemented. Currently audio
322 // always use the default input.
deadbeefb1a15d72017-09-07 14:12:05 -0700323 return peer_connection_factory_->CreateAudioTrack(rtc::CreateRandomUuid(),
deadbeef1dcb1642017-03-29 21:08:16 -0700324 source);
325 }
326
327 rtc::scoped_refptr<webrtc::VideoTrackInterface> CreateLocalVideoTrack() {
Johannes Kron965e7942018-09-13 15:36:20 +0200328 webrtc::FakePeriodicVideoSource::Config config;
329 config.timestamp_offset_ms = rtc::TimeMillis();
330 return CreateLocalVideoTrackInternal(config);
deadbeef1dcb1642017-03-29 21:08:16 -0700331 }
332
333 rtc::scoped_refptr<webrtc::VideoTrackInterface>
Niels Möller5c7efe72018-05-11 10:34:46 +0200334 CreateLocalVideoTrackWithConfig(
335 webrtc::FakePeriodicVideoSource::Config config) {
336 return CreateLocalVideoTrackInternal(config);
deadbeef1dcb1642017-03-29 21:08:16 -0700337 }
338
339 rtc::scoped_refptr<webrtc::VideoTrackInterface>
340 CreateLocalVideoTrackWithRotation(webrtc::VideoRotation rotation) {
Niels Möller5c7efe72018-05-11 10:34:46 +0200341 webrtc::FakePeriodicVideoSource::Config config;
342 config.rotation = rotation;
Johannes Kron965e7942018-09-13 15:36:20 +0200343 config.timestamp_offset_ms = rtc::TimeMillis();
Niels Möller5c7efe72018-05-11 10:34:46 +0200344 return CreateLocalVideoTrackInternal(config);
deadbeef1dcb1642017-03-29 21:08:16 -0700345 }
346
Steve Anton74255ff2018-01-24 18:32:57 -0800347 rtc::scoped_refptr<RtpSenderInterface> AddTrack(
348 rtc::scoped_refptr<MediaStreamTrackInterface> track,
Seth Hampson845e8782018-03-02 11:34:10 -0800349 const std::vector<std::string>& stream_ids = {}) {
350 auto result = pc()->AddTrack(track, stream_ids);
Steve Anton15324772018-01-16 10:26:49 -0800351 EXPECT_EQ(RTCErrorType::NONE, result.error().type());
Steve Anton74255ff2018-01-24 18:32:57 -0800352 return result.MoveValue();
353 }
354
355 std::vector<rtc::scoped_refptr<RtpReceiverInterface>> GetReceiversOfType(
356 cricket::MediaType media_type) {
357 std::vector<rtc::scoped_refptr<RtpReceiverInterface>> receivers;
Mirko Bonadei739baf02019-01-27 17:29:42 +0100358 for (const auto& receiver : pc()->GetReceivers()) {
Steve Anton74255ff2018-01-24 18:32:57 -0800359 if (receiver->media_type() == media_type) {
360 receivers.push_back(receiver);
361 }
362 }
363 return receivers;
deadbeef1dcb1642017-03-29 21:08:16 -0700364 }
365
Seth Hampson2f0d7022018-02-20 11:54:42 -0800366 rtc::scoped_refptr<RtpTransceiverInterface> GetFirstTransceiverOfType(
367 cricket::MediaType media_type) {
368 for (auto transceiver : pc()->GetTransceivers()) {
369 if (transceiver->receiver()->media_type() == media_type) {
370 return transceiver;
371 }
372 }
373 return nullptr;
374 }
375
deadbeef1dcb1642017-03-29 21:08:16 -0700376 bool SignalingStateStable() {
377 return pc()->signaling_state() == webrtc::PeerConnectionInterface::kStable;
378 }
379
380 void CreateDataChannel() { CreateDataChannel(nullptr); }
381
382 void CreateDataChannel(const webrtc::DataChannelInit* init) {
Steve Antonda6c0952017-10-23 11:41:54 -0700383 CreateDataChannel(kDataChannelLabel, init);
384 }
385
386 void CreateDataChannel(const std::string& label,
387 const webrtc::DataChannelInit* init) {
388 data_channel_ = pc()->CreateDataChannel(label, init);
deadbeef1dcb1642017-03-29 21:08:16 -0700389 ASSERT_TRUE(data_channel_.get() != nullptr);
390 data_observer_.reset(new MockDataChannelObserver(data_channel_));
391 }
392
393 DataChannelInterface* data_channel() { return data_channel_; }
394 const MockDataChannelObserver* data_observer() const {
395 return data_observer_.get();
396 }
397
398 int audio_frames_received() const {
399 return fake_audio_capture_module_->frames_received();
400 }
401
402 // Takes minimum of video frames received for each track.
403 //
404 // Can be used like:
405 // EXPECT_GE(expected_frames, min_video_frames_received_per_track());
406 //
407 // To ensure that all video tracks received at least a certain number of
408 // frames.
409 int min_video_frames_received_per_track() const {
410 int min_frames = INT_MAX;
Anders Carlsson5f2bb622018-05-14 09:48:06 +0200411 if (fake_video_renderers_.empty()) {
412 return 0;
deadbeef1dcb1642017-03-29 21:08:16 -0700413 }
deadbeef1dcb1642017-03-29 21:08:16 -0700414
Anders Carlsson5f2bb622018-05-14 09:48:06 +0200415 for (const auto& pair : fake_video_renderers_) {
416 min_frames = std::min(min_frames, pair.second->num_rendered_frames());
deadbeef1dcb1642017-03-29 21:08:16 -0700417 }
Anders Carlsson5f2bb622018-05-14 09:48:06 +0200418 return min_frames;
deadbeef1dcb1642017-03-29 21:08:16 -0700419 }
420
421 // Returns a MockStatsObserver in a state after stats gathering finished,
422 // which can be used to access the gathered stats.
deadbeefd8ad7882017-04-18 16:01:17 -0700423 rtc::scoped_refptr<MockStatsObserver> OldGetStatsForTrack(
deadbeef1dcb1642017-03-29 21:08:16 -0700424 webrtc::MediaStreamTrackInterface* track) {
425 rtc::scoped_refptr<MockStatsObserver> observer(
426 new rtc::RefCountedObject<MockStatsObserver>());
427 EXPECT_TRUE(peer_connection_->GetStats(
428 observer, nullptr, PeerConnectionInterface::kStatsOutputLevelStandard));
429 EXPECT_TRUE_WAIT(observer->called(), kDefaultTimeout);
430 return observer;
431 }
432
433 // Version that doesn't take a track "filter", and gathers all stats.
deadbeefd8ad7882017-04-18 16:01:17 -0700434 rtc::scoped_refptr<MockStatsObserver> OldGetStats() {
435 return OldGetStatsForTrack(nullptr);
436 }
437
438 // Synchronously gets stats and returns them. If it times out, fails the test
439 // and returns null.
440 rtc::scoped_refptr<const webrtc::RTCStatsReport> NewGetStats() {
441 rtc::scoped_refptr<webrtc::MockRTCStatsCollectorCallback> callback(
442 new rtc::RefCountedObject<webrtc::MockRTCStatsCollectorCallback>());
443 peer_connection_->GetStats(callback);
444 EXPECT_TRUE_WAIT(callback->called(), kDefaultTimeout);
445 return callback->report();
deadbeef1dcb1642017-03-29 21:08:16 -0700446 }
447
448 int rendered_width() {
449 EXPECT_FALSE(fake_video_renderers_.empty());
450 return fake_video_renderers_.empty()
451 ? 0
452 : fake_video_renderers_.begin()->second->width();
453 }
454
455 int rendered_height() {
456 EXPECT_FALSE(fake_video_renderers_.empty());
457 return fake_video_renderers_.empty()
458 ? 0
459 : fake_video_renderers_.begin()->second->height();
460 }
461
462 double rendered_aspect_ratio() {
463 if (rendered_height() == 0) {
464 return 0.0;
465 }
466 return static_cast<double>(rendered_width()) / rendered_height();
467 }
468
469 webrtc::VideoRotation rendered_rotation() {
470 EXPECT_FALSE(fake_video_renderers_.empty());
471 return fake_video_renderers_.empty()
472 ? webrtc::kVideoRotation_0
473 : fake_video_renderers_.begin()->second->rotation();
474 }
475
476 int local_rendered_width() {
477 return local_video_renderer_ ? local_video_renderer_->width() : 0;
478 }
479
480 int local_rendered_height() {
481 return local_video_renderer_ ? local_video_renderer_->height() : 0;
482 }
483
484 double local_rendered_aspect_ratio() {
485 if (local_rendered_height() == 0) {
486 return 0.0;
487 }
488 return static_cast<double>(local_rendered_width()) /
489 local_rendered_height();
490 }
491
492 size_t number_of_remote_streams() {
493 if (!pc()) {
494 return 0;
495 }
496 return pc()->remote_streams()->count();
497 }
498
499 StreamCollectionInterface* remote_streams() const {
500 if (!pc()) {
501 ADD_FAILURE();
502 return nullptr;
503 }
504 return pc()->remote_streams();
505 }
506
507 StreamCollectionInterface* local_streams() {
508 if (!pc()) {
509 ADD_FAILURE();
510 return nullptr;
511 }
512 return pc()->local_streams();
513 }
514
515 webrtc::PeerConnectionInterface::SignalingState signaling_state() {
516 return pc()->signaling_state();
517 }
518
519 webrtc::PeerConnectionInterface::IceConnectionState ice_connection_state() {
520 return pc()->ice_connection_state();
521 }
522
Jonas Olsson7a6739e2019-01-15 16:31:55 +0100523 webrtc::PeerConnectionInterface::IceConnectionState
524 standardized_ice_connection_state() {
525 return pc()->standardized_ice_connection_state();
526 }
527
deadbeef1dcb1642017-03-29 21:08:16 -0700528 webrtc::PeerConnectionInterface::IceGatheringState ice_gathering_state() {
529 return pc()->ice_gathering_state();
530 }
531
532 // Returns a MockRtpReceiverObserver for each RtpReceiver returned by
533 // GetReceivers. They're updated automatically when a remote offer/answer
534 // from the fake signaling channel is applied, or when
535 // ResetRtpReceiverObservers below is called.
536 const std::vector<std::unique_ptr<MockRtpReceiverObserver>>&
537 rtp_receiver_observers() {
538 return rtp_receiver_observers_;
539 }
540
541 void ResetRtpReceiverObservers() {
542 rtp_receiver_observers_.clear();
Mirko Bonadeic61ce0d2017-11-21 17:04:20 +0100543 for (const rtc::scoped_refptr<RtpReceiverInterface>& receiver :
544 pc()->GetReceivers()) {
deadbeef1dcb1642017-03-29 21:08:16 -0700545 std::unique_ptr<MockRtpReceiverObserver> observer(
546 new MockRtpReceiverObserver(receiver->media_type()));
547 receiver->SetObserver(observer.get());
548 rtp_receiver_observers_.push_back(std::move(observer));
549 }
550 }
551
Qingsi Wangecd30542019-05-22 14:34:56 -0700552 rtc::FakeNetworkManager* network_manager() const {
Steve Antonede9ca52017-10-16 13:04:27 -0700553 return fake_network_manager_.get();
554 }
555 cricket::PortAllocator* port_allocator() const { return port_allocator_; }
556
Qingsi Wang7685e862018-06-11 20:15:46 -0700557 webrtc::FakeRtcEventLogFactory* event_log_factory() const {
558 return event_log_factory_;
559 }
560
Qingsi Wangc129c352019-04-18 10:41:58 -0700561 const cricket::Candidate& last_candidate_gathered() const {
562 return last_candidate_gathered_;
563 }
Eldar Relloda13ea22019-06-01 12:23:43 +0300564 const cricket::IceCandidateErrorEvent& error_event() const {
565 return error_event_;
566 }
Qingsi Wangc129c352019-04-18 10:41:58 -0700567
Qingsi Wangecd30542019-05-22 14:34:56 -0700568 // Sets the mDNS responder for the owned fake network manager and keeps a
569 // reference to the responder.
570 void SetMdnsResponder(
571 std::unique_ptr<webrtc::FakeMdnsResponder> mdns_responder) {
572 RTC_DCHECK(mdns_responder != nullptr);
573 mdns_responder_ = mdns_responder.get();
574 network_manager()->set_mdns_responder(std::move(mdns_responder));
575 }
576
deadbeef1dcb1642017-03-29 21:08:16 -0700577 private:
578 explicit PeerConnectionWrapper(const std::string& debug_name)
579 : debug_name_(debug_name) {}
580
Bjorn Mellem175aa2e2018-11-08 11:23:22 -0800581 bool Init(
582 const PeerConnectionFactory::Options* options,
583 const PeerConnectionInterface::RTCConfiguration* config,
584 webrtc::PeerConnectionDependencies dependencies,
585 rtc::Thread* network_thread,
586 rtc::Thread* worker_thread,
587 std::unique_ptr<webrtc::FakeRtcEventLogFactory> event_log_factory,
588 std::unique_ptr<webrtc::MediaTransportFactory> media_transport_factory) {
deadbeef1dcb1642017-03-29 21:08:16 -0700589 // There's an error in this test code if Init ends up being called twice.
590 RTC_DCHECK(!peer_connection_);
591 RTC_DCHECK(!peer_connection_factory_);
592
593 fake_network_manager_.reset(new rtc::FakeNetworkManager());
Steve Antonede9ca52017-10-16 13:04:27 -0700594 fake_network_manager_->AddInterface(kDefaultLocalAddress);
deadbeef1dcb1642017-03-29 21:08:16 -0700595
596 std::unique_ptr<cricket::PortAllocator> port_allocator(
597 new cricket::BasicPortAllocator(fake_network_manager_.get()));
Steve Antonede9ca52017-10-16 13:04:27 -0700598 port_allocator_ = port_allocator.get();
deadbeef1dcb1642017-03-29 21:08:16 -0700599 fake_audio_capture_module_ = FakeAudioCaptureModule::Create();
600 if (!fake_audio_capture_module_) {
601 return false;
602 }
deadbeef1dcb1642017-03-29 21:08:16 -0700603 rtc::Thread* const signaling_thread = rtc::Thread::Current();
Qingsi Wang7685e862018-06-11 20:15:46 -0700604
605 webrtc::PeerConnectionFactoryDependencies pc_factory_dependencies;
606 pc_factory_dependencies.network_thread = network_thread;
607 pc_factory_dependencies.worker_thread = worker_thread;
608 pc_factory_dependencies.signaling_thread = signaling_thread;
Danil Chapovalov9da25bd2019-06-20 10:19:42 +0200609 pc_factory_dependencies.task_queue_factory =
610 webrtc::CreateDefaultTaskQueueFactory();
611 cricket::MediaEngineDependencies media_deps;
612 media_deps.task_queue_factory =
613 pc_factory_dependencies.task_queue_factory.get();
614 media_deps.adm = fake_audio_capture_module_;
615 webrtc::SetMediaEngineDefaults(&media_deps);
Qingsi Wang7685e862018-06-11 20:15:46 -0700616 pc_factory_dependencies.media_engine =
Danil Chapovalov9da25bd2019-06-20 10:19:42 +0200617 cricket::CreateMediaEngine(std::move(media_deps));
Qingsi Wang7685e862018-06-11 20:15:46 -0700618 pc_factory_dependencies.call_factory = webrtc::CreateCallFactory();
619 if (event_log_factory) {
620 event_log_factory_ = event_log_factory.get();
621 pc_factory_dependencies.event_log_factory = std::move(event_log_factory);
622 } else {
623 pc_factory_dependencies.event_log_factory =
Danil Chapovalov9da25bd2019-06-20 10:19:42 +0200624 absl::make_unique<webrtc::RtcEventLogFactory>(
625 pc_factory_dependencies.task_queue_factory.get());
Qingsi Wang7685e862018-06-11 20:15:46 -0700626 }
Bjorn Mellem175aa2e2018-11-08 11:23:22 -0800627 if (media_transport_factory) {
628 pc_factory_dependencies.media_transport_factory =
629 std::move(media_transport_factory);
630 }
Qingsi Wang7685e862018-06-11 20:15:46 -0700631 peer_connection_factory_ = webrtc::CreateModularPeerConnectionFactory(
632 std::move(pc_factory_dependencies));
633
deadbeef1dcb1642017-03-29 21:08:16 -0700634 if (!peer_connection_factory_) {
635 return false;
636 }
637 if (options) {
638 peer_connection_factory_->SetOptions(*options);
639 }
Seth Hampson2f0d7022018-02-20 11:54:42 -0800640 if (config) {
641 sdp_semantics_ = config->sdp_semantics;
642 }
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700643
644 dependencies.allocator = std::move(port_allocator);
Niels Möllerf06f9232018-08-07 12:32:18 +0200645 peer_connection_ = CreatePeerConnection(config, std::move(dependencies));
deadbeef1dcb1642017-03-29 21:08:16 -0700646 return peer_connection_.get() != nullptr;
647 }
648
649 rtc::scoped_refptr<webrtc::PeerConnectionInterface> CreatePeerConnection(
deadbeef1dcb1642017-03-29 21:08:16 -0700650 const PeerConnectionInterface::RTCConfiguration* config,
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700651 webrtc::PeerConnectionDependencies dependencies) {
deadbeef1dcb1642017-03-29 21:08:16 -0700652 PeerConnectionInterface::RTCConfiguration modified_config;
653 // If |config| is null, this will result in a default configuration being
654 // used.
655 if (config) {
656 modified_config = *config;
657 }
658 // Disable resolution adaptation; we don't want it interfering with the
659 // test results.
660 // TODO(deadbeef): Do something more robust. Since we're testing for aspect
661 // ratios and not specific resolutions, is this even necessary?
662 modified_config.set_cpu_adaptation(false);
663
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700664 dependencies.observer = this;
deadbeef1dcb1642017-03-29 21:08:16 -0700665 return peer_connection_factory_->CreatePeerConnection(
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700666 modified_config, std::move(dependencies));
deadbeef1dcb1642017-03-29 21:08:16 -0700667 }
668
669 void set_signaling_message_receiver(
670 SignalingMessageReceiver* signaling_message_receiver) {
671 signaling_message_receiver_ = signaling_message_receiver;
672 }
673
674 void set_signaling_delay_ms(int delay_ms) { signaling_delay_ms_ = delay_ms; }
675
Steve Antonede9ca52017-10-16 13:04:27 -0700676 void set_signal_ice_candidates(bool signal) {
677 signal_ice_candidates_ = signal;
678 }
679
deadbeef1dcb1642017-03-29 21:08:16 -0700680 rtc::scoped_refptr<webrtc::VideoTrackInterface> CreateLocalVideoTrackInternal(
Niels Möller5c7efe72018-05-11 10:34:46 +0200681 webrtc::FakePeriodicVideoSource::Config config) {
deadbeef1dcb1642017-03-29 21:08:16 -0700682 // Set max frame rate to 10fps to reduce the risk of test flakiness.
683 // TODO(deadbeef): Do something more robust.
Niels Möller5c7efe72018-05-11 10:34:46 +0200684 config.frame_interval_ms = 100;
deadbeef1dcb1642017-03-29 21:08:16 -0700685
Niels Möller5c7efe72018-05-11 10:34:46 +0200686 video_track_sources_.emplace_back(
Niels Möller0f405822018-05-17 09:16:41 +0200687 new rtc::RefCountedObject<webrtc::FakePeriodicVideoTrackSource>(
688 config, false /* remote */));
deadbeef1dcb1642017-03-29 21:08:16 -0700689 rtc::scoped_refptr<webrtc::VideoTrackInterface> track(
Niels Möller5c7efe72018-05-11 10:34:46 +0200690 peer_connection_factory_->CreateVideoTrack(
691 rtc::CreateRandomUuid(), video_track_sources_.back()));
deadbeef1dcb1642017-03-29 21:08:16 -0700692 if (!local_video_renderer_) {
693 local_video_renderer_.reset(new webrtc::FakeVideoTrackRenderer(track));
694 }
695 return track;
696 }
697
698 void HandleIncomingOffer(const std::string& msg) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100699 RTC_LOG(LS_INFO) << debug_name_ << ": HandleIncomingOffer";
Steve Antona3a92c22017-12-07 10:27:41 -0800700 std::unique_ptr<SessionDescriptionInterface> desc =
701 webrtc::CreateSessionDescription(SdpType::kOffer, msg);
deadbeef1dcb1642017-03-29 21:08:16 -0700702 if (received_sdp_munger_) {
703 received_sdp_munger_(desc->description());
704 }
705
706 EXPECT_TRUE(SetRemoteDescription(std::move(desc)));
707 // Setting a remote description may have changed the number of receivers,
708 // so reset the receiver observers.
709 ResetRtpReceiverObservers();
Seth Hampson2f0d7022018-02-20 11:54:42 -0800710 if (remote_offer_handler_) {
711 remote_offer_handler_();
712 }
deadbeef1dcb1642017-03-29 21:08:16 -0700713 auto answer = CreateAnswer();
714 ASSERT_NE(nullptr, answer);
715 EXPECT_TRUE(SetLocalDescriptionAndSendSdpMessage(std::move(answer)));
716 }
717
718 void HandleIncomingAnswer(const std::string& msg) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100719 RTC_LOG(LS_INFO) << debug_name_ << ": HandleIncomingAnswer";
Steve Antona3a92c22017-12-07 10:27:41 -0800720 std::unique_ptr<SessionDescriptionInterface> desc =
721 webrtc::CreateSessionDescription(SdpType::kAnswer, msg);
deadbeef1dcb1642017-03-29 21:08:16 -0700722 if (received_sdp_munger_) {
723 received_sdp_munger_(desc->description());
724 }
725
726 EXPECT_TRUE(SetRemoteDescription(std::move(desc)));
727 // Set the RtpReceiverObserver after receivers are created.
728 ResetRtpReceiverObservers();
729 }
730
731 // Returns null on failure.
732 std::unique_ptr<SessionDescriptionInterface> CreateOffer() {
733 rtc::scoped_refptr<MockCreateSessionDescriptionObserver> observer(
734 new rtc::RefCountedObject<MockCreateSessionDescriptionObserver>());
735 pc()->CreateOffer(observer, offer_answer_options_);
736 return WaitForDescriptionFromObserver(observer);
737 }
738
739 // Returns null on failure.
740 std::unique_ptr<SessionDescriptionInterface> CreateAnswer() {
741 rtc::scoped_refptr<MockCreateSessionDescriptionObserver> observer(
742 new rtc::RefCountedObject<MockCreateSessionDescriptionObserver>());
743 pc()->CreateAnswer(observer, offer_answer_options_);
744 return WaitForDescriptionFromObserver(observer);
745 }
746
747 std::unique_ptr<SessionDescriptionInterface> WaitForDescriptionFromObserver(
Mirko Bonadeic61ce0d2017-11-21 17:04:20 +0100748 MockCreateSessionDescriptionObserver* observer) {
deadbeef1dcb1642017-03-29 21:08:16 -0700749 EXPECT_EQ_WAIT(true, observer->called(), kDefaultTimeout);
750 if (!observer->result()) {
751 return nullptr;
752 }
753 auto description = observer->MoveDescription();
754 if (generated_sdp_munger_) {
755 generated_sdp_munger_(description->description());
756 }
757 return description;
758 }
759
760 // Setting the local description and sending the SDP message over the fake
761 // signaling channel are combined into the same method because the SDP
762 // message needs to be sent as soon as SetLocalDescription finishes, without
763 // waiting for the observer to be called. This ensures that ICE candidates
764 // don't outrace the description.
765 bool SetLocalDescriptionAndSendSdpMessage(
766 std::unique_ptr<SessionDescriptionInterface> desc) {
767 rtc::scoped_refptr<MockSetSessionDescriptionObserver> observer(
768 new rtc::RefCountedObject<MockSetSessionDescriptionObserver>());
Mirko Bonadei675513b2017-11-09 11:09:25 +0100769 RTC_LOG(LS_INFO) << debug_name_ << ": SetLocalDescriptionAndSendSdpMessage";
Steve Antona3a92c22017-12-07 10:27:41 -0800770 SdpType type = desc->GetType();
deadbeef1dcb1642017-03-29 21:08:16 -0700771 std::string sdp;
772 EXPECT_TRUE(desc->ToString(&sdp));
773 pc()->SetLocalDescription(observer, desc.release());
Seth Hampson2f0d7022018-02-20 11:54:42 -0800774 if (sdp_semantics_ == SdpSemantics::kUnifiedPlan) {
775 RemoveUnusedVideoRenderers();
776 }
deadbeef1dcb1642017-03-29 21:08:16 -0700777 // As mentioned above, we need to send the message immediately after
778 // SetLocalDescription.
779 SendSdpMessage(type, sdp);
780 EXPECT_TRUE_WAIT(observer->called(), kDefaultTimeout);
781 return true;
782 }
783
784 bool SetRemoteDescription(std::unique_ptr<SessionDescriptionInterface> desc) {
785 rtc::scoped_refptr<MockSetSessionDescriptionObserver> observer(
786 new rtc::RefCountedObject<MockSetSessionDescriptionObserver>());
Mirko Bonadei675513b2017-11-09 11:09:25 +0100787 RTC_LOG(LS_INFO) << debug_name_ << ": SetRemoteDescription";
deadbeef1dcb1642017-03-29 21:08:16 -0700788 pc()->SetRemoteDescription(observer, desc.release());
Seth Hampson2f0d7022018-02-20 11:54:42 -0800789 if (sdp_semantics_ == SdpSemantics::kUnifiedPlan) {
790 RemoveUnusedVideoRenderers();
791 }
deadbeef1dcb1642017-03-29 21:08:16 -0700792 EXPECT_TRUE_WAIT(observer->called(), kDefaultTimeout);
793 return observer->result();
794 }
795
Seth Hampson2f0d7022018-02-20 11:54:42 -0800796 // This is a work around to remove unused fake_video_renderers from
797 // transceivers that have either stopped or are no longer receiving.
798 void RemoveUnusedVideoRenderers() {
799 auto transceivers = pc()->GetTransceivers();
800 for (auto& transceiver : transceivers) {
801 if (transceiver->receiver()->media_type() != cricket::MEDIA_TYPE_VIDEO) {
802 continue;
803 }
804 // Remove fake video renderers from any stopped transceivers.
805 if (transceiver->stopped()) {
806 auto it =
807 fake_video_renderers_.find(transceiver->receiver()->track()->id());
808 if (it != fake_video_renderers_.end()) {
809 fake_video_renderers_.erase(it);
810 }
811 }
812 // Remove fake video renderers from any transceivers that are no longer
813 // receiving.
814 if ((transceiver->current_direction() &&
815 !webrtc::RtpTransceiverDirectionHasRecv(
816 *transceiver->current_direction()))) {
817 auto it =
818 fake_video_renderers_.find(transceiver->receiver()->track()->id());
819 if (it != fake_video_renderers_.end()) {
820 fake_video_renderers_.erase(it);
821 }
822 }
823 }
824 }
825
deadbeef1dcb1642017-03-29 21:08:16 -0700826 // Simulate sending a blob of SDP with delay |signaling_delay_ms_| (0 by
827 // default).
Steve Antona3a92c22017-12-07 10:27:41 -0800828 void SendSdpMessage(SdpType type, const std::string& msg) {
deadbeef1dcb1642017-03-29 21:08:16 -0700829 if (signaling_delay_ms_ == 0) {
830 RelaySdpMessageIfReceiverExists(type, msg);
831 } else {
832 invoker_.AsyncInvokeDelayed<void>(
833 RTC_FROM_HERE, rtc::Thread::Current(),
834 rtc::Bind(&PeerConnectionWrapper::RelaySdpMessageIfReceiverExists,
835 this, type, msg),
836 signaling_delay_ms_);
837 }
838 }
839
Steve Antona3a92c22017-12-07 10:27:41 -0800840 void RelaySdpMessageIfReceiverExists(SdpType type, const std::string& msg) {
deadbeef1dcb1642017-03-29 21:08:16 -0700841 if (signaling_message_receiver_) {
842 signaling_message_receiver_->ReceiveSdpMessage(type, msg);
843 }
844 }
845
846 // Simulate trickling an ICE candidate with delay |signaling_delay_ms_| (0 by
847 // default).
848 void SendIceMessage(const std::string& sdp_mid,
849 int sdp_mline_index,
850 const std::string& msg) {
851 if (signaling_delay_ms_ == 0) {
852 RelayIceMessageIfReceiverExists(sdp_mid, sdp_mline_index, msg);
853 } else {
854 invoker_.AsyncInvokeDelayed<void>(
855 RTC_FROM_HERE, rtc::Thread::Current(),
856 rtc::Bind(&PeerConnectionWrapper::RelayIceMessageIfReceiverExists,
857 this, sdp_mid, sdp_mline_index, msg),
858 signaling_delay_ms_);
859 }
860 }
861
862 void RelayIceMessageIfReceiverExists(const std::string& sdp_mid,
863 int sdp_mline_index,
864 const std::string& msg) {
865 if (signaling_message_receiver_) {
866 signaling_message_receiver_->ReceiveIceMessage(sdp_mid, sdp_mline_index,
867 msg);
868 }
869 }
870
871 // SignalingMessageReceiver callbacks.
Steve Antona3a92c22017-12-07 10:27:41 -0800872 void ReceiveSdpMessage(SdpType type, const std::string& msg) override {
873 if (type == SdpType::kOffer) {
deadbeef1dcb1642017-03-29 21:08:16 -0700874 HandleIncomingOffer(msg);
875 } else {
876 HandleIncomingAnswer(msg);
877 }
878 }
879
880 void ReceiveIceMessage(const std::string& sdp_mid,
881 int sdp_mline_index,
882 const std::string& msg) override {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100883 RTC_LOG(LS_INFO) << debug_name_ << ": ReceiveIceMessage";
deadbeef1dcb1642017-03-29 21:08:16 -0700884 std::unique_ptr<webrtc::IceCandidateInterface> candidate(
885 webrtc::CreateIceCandidate(sdp_mid, sdp_mline_index, msg, nullptr));
886 EXPECT_TRUE(pc()->AddIceCandidate(candidate.get()));
887 }
888
889 // PeerConnectionObserver callbacks.
890 void OnSignalingChange(
891 webrtc::PeerConnectionInterface::SignalingState new_state) override {
892 EXPECT_EQ(pc()->signaling_state(), new_state);
893 }
Steve Anton15324772018-01-16 10:26:49 -0800894 void OnAddTrack(rtc::scoped_refptr<RtpReceiverInterface> receiver,
895 const std::vector<rtc::scoped_refptr<MediaStreamInterface>>&
896 streams) override {
897 if (receiver->media_type() == cricket::MEDIA_TYPE_VIDEO) {
898 rtc::scoped_refptr<VideoTrackInterface> video_track(
899 static_cast<VideoTrackInterface*>(receiver->track().get()));
900 ASSERT_TRUE(fake_video_renderers_.find(video_track->id()) ==
deadbeef1dcb1642017-03-29 21:08:16 -0700901 fake_video_renderers_.end());
Steve Anton15324772018-01-16 10:26:49 -0800902 fake_video_renderers_[video_track->id()] =
Karl Wiberg918f50c2018-07-05 11:40:33 +0200903 absl::make_unique<FakeVideoTrackRenderer>(video_track);
deadbeef1dcb1642017-03-29 21:08:16 -0700904 }
905 }
Steve Anton15324772018-01-16 10:26:49 -0800906 void OnRemoveTrack(
907 rtc::scoped_refptr<RtpReceiverInterface> receiver) override {
908 if (receiver->media_type() == cricket::MEDIA_TYPE_VIDEO) {
909 auto it = fake_video_renderers_.find(receiver->track()->id());
910 RTC_DCHECK(it != fake_video_renderers_.end());
911 fake_video_renderers_.erase(it);
912 }
913 }
deadbeef1dcb1642017-03-29 21:08:16 -0700914 void OnRenegotiationNeeded() override {}
915 void OnIceConnectionChange(
916 webrtc::PeerConnectionInterface::IceConnectionState new_state) override {
917 EXPECT_EQ(pc()->ice_connection_state(), new_state);
Steve Antonede9ca52017-10-16 13:04:27 -0700918 ice_connection_state_history_.push_back(new_state);
deadbeef1dcb1642017-03-29 21:08:16 -0700919 }
Jonas Olssonacd8ae72019-02-25 15:26:24 +0100920 void OnStandardizedIceConnectionChange(
921 webrtc::PeerConnectionInterface::IceConnectionState new_state) override {
922 standardized_ice_connection_state_history_.push_back(new_state);
923 }
Jonas Olsson635474e2018-10-18 15:58:17 +0200924 void OnConnectionChange(
925 webrtc::PeerConnectionInterface::PeerConnectionState new_state) override {
926 peer_connection_state_history_.push_back(new_state);
927 }
928
deadbeef1dcb1642017-03-29 21:08:16 -0700929 void OnIceGatheringChange(
930 webrtc::PeerConnectionInterface::IceGatheringState new_state) override {
deadbeef1dcb1642017-03-29 21:08:16 -0700931 EXPECT_EQ(pc()->ice_gathering_state(), new_state);
Steve Antonede9ca52017-10-16 13:04:27 -0700932 ice_gathering_state_history_.push_back(new_state);
deadbeef1dcb1642017-03-29 21:08:16 -0700933 }
934 void OnIceCandidate(const webrtc::IceCandidateInterface* candidate) override {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100935 RTC_LOG(LS_INFO) << debug_name_ << ": OnIceCandidate";
deadbeef1dcb1642017-03-29 21:08:16 -0700936
Qingsi Wang1dac6d82018-12-12 15:28:47 -0800937 if (remote_async_resolver_) {
938 const auto& local_candidate = candidate->candidate();
Qingsi Wang1dac6d82018-12-12 15:28:47 -0800939 if (local_candidate.address().IsUnresolvedIP()) {
940 RTC_DCHECK(local_candidate.type() == cricket::LOCAL_PORT_TYPE);
941 rtc::SocketAddress resolved_addr(local_candidate.address());
Qingsi Wangecd30542019-05-22 14:34:56 -0700942 const auto resolved_ip = mdns_responder_->GetMappedAddressForName(
Qingsi Wang1dac6d82018-12-12 15:28:47 -0800943 local_candidate.address().hostname());
944 RTC_DCHECK(!resolved_ip.IsNil());
945 resolved_addr.SetResolvedIP(resolved_ip);
946 EXPECT_CALL(*remote_async_resolver_, GetResolvedAddress(_, _))
947 .WillOnce(DoAll(SetArgPointee<1>(resolved_addr), Return(true)));
948 EXPECT_CALL(*remote_async_resolver_, Destroy(_));
Zach Stein6fcdc2f2018-08-23 16:25:55 -0700949 }
Zach Stein6fcdc2f2018-08-23 16:25:55 -0700950 }
951
deadbeef1dcb1642017-03-29 21:08:16 -0700952 std::string ice_sdp;
Qingsi Wang1dac6d82018-12-12 15:28:47 -0800953 EXPECT_TRUE(candidate->ToString(&ice_sdp));
Steve Antonede9ca52017-10-16 13:04:27 -0700954 if (signaling_message_receiver_ == nullptr || !signal_ice_candidates_) {
deadbeef1dcb1642017-03-29 21:08:16 -0700955 // Remote party may be deleted.
956 return;
957 }
Qingsi Wang1dac6d82018-12-12 15:28:47 -0800958 SendIceMessage(candidate->sdp_mid(), candidate->sdp_mline_index(), ice_sdp);
Qingsi Wangc129c352019-04-18 10:41:58 -0700959 last_candidate_gathered_ = candidate->candidate();
deadbeef1dcb1642017-03-29 21:08:16 -0700960 }
Eldar Relloda13ea22019-06-01 12:23:43 +0300961 void OnIceCandidateError(const std::string& host_candidate,
962 const std::string& url,
963 int error_code,
964 const std::string& error_text) override {
965 error_event_ = cricket::IceCandidateErrorEvent(host_candidate, url,
966 error_code, error_text);
967 }
deadbeef1dcb1642017-03-29 21:08:16 -0700968 void OnDataChannel(
969 rtc::scoped_refptr<DataChannelInterface> data_channel) override {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100970 RTC_LOG(LS_INFO) << debug_name_ << ": OnDataChannel";
deadbeef1dcb1642017-03-29 21:08:16 -0700971 data_channel_ = data_channel;
972 data_observer_.reset(new MockDataChannelObserver(data_channel));
973 }
974
deadbeef1dcb1642017-03-29 21:08:16 -0700975 std::string debug_name_;
976
977 std::unique_ptr<rtc::FakeNetworkManager> fake_network_manager_;
Qingsi Wangecd30542019-05-22 14:34:56 -0700978 // Reference to the mDNS responder owned by |fake_network_manager_| after set.
979 webrtc::FakeMdnsResponder* mdns_responder_ = nullptr;
deadbeef1dcb1642017-03-29 21:08:16 -0700980
981 rtc::scoped_refptr<webrtc::PeerConnectionInterface> peer_connection_;
982 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface>
983 peer_connection_factory_;
984
Steve Antonede9ca52017-10-16 13:04:27 -0700985 cricket::PortAllocator* port_allocator_;
deadbeef1dcb1642017-03-29 21:08:16 -0700986 // Needed to keep track of number of frames sent.
987 rtc::scoped_refptr<FakeAudioCaptureModule> fake_audio_capture_module_;
988 // Needed to keep track of number of frames received.
989 std::map<std::string, std::unique_ptr<webrtc::FakeVideoTrackRenderer>>
990 fake_video_renderers_;
991 // Needed to ensure frames aren't received for removed tracks.
992 std::vector<std::unique_ptr<webrtc::FakeVideoTrackRenderer>>
993 removed_fake_video_renderers_;
deadbeef1dcb1642017-03-29 21:08:16 -0700994
995 // For remote peer communication.
996 SignalingMessageReceiver* signaling_message_receiver_ = nullptr;
997 int signaling_delay_ms_ = 0;
Steve Antonede9ca52017-10-16 13:04:27 -0700998 bool signal_ice_candidates_ = true;
Qingsi Wangc129c352019-04-18 10:41:58 -0700999 cricket::Candidate last_candidate_gathered_;
Eldar Relloda13ea22019-06-01 12:23:43 +03001000 cricket::IceCandidateErrorEvent error_event_;
deadbeef1dcb1642017-03-29 21:08:16 -07001001
Niels Möller5c7efe72018-05-11 10:34:46 +02001002 // Store references to the video sources we've created, so that we can stop
deadbeef1dcb1642017-03-29 21:08:16 -07001003 // them, if required.
Niels Möller5c7efe72018-05-11 10:34:46 +02001004 std::vector<rtc::scoped_refptr<webrtc::VideoTrackSource>>
1005 video_track_sources_;
deadbeef1dcb1642017-03-29 21:08:16 -07001006 // |local_video_renderer_| attached to the first created local video track.
1007 std::unique_ptr<webrtc::FakeVideoTrackRenderer> local_video_renderer_;
1008
Seth Hampson2f0d7022018-02-20 11:54:42 -08001009 SdpSemantics sdp_semantics_;
deadbeef1dcb1642017-03-29 21:08:16 -07001010 PeerConnectionInterface::RTCOfferAnswerOptions offer_answer_options_;
1011 std::function<void(cricket::SessionDescription*)> received_sdp_munger_;
1012 std::function<void(cricket::SessionDescription*)> generated_sdp_munger_;
Seth Hampson2f0d7022018-02-20 11:54:42 -08001013 std::function<void()> remote_offer_handler_;
Qingsi Wang1dac6d82018-12-12 15:28:47 -08001014 rtc::MockAsyncResolver* remote_async_resolver_ = nullptr;
deadbeef1dcb1642017-03-29 21:08:16 -07001015 rtc::scoped_refptr<DataChannelInterface> data_channel_;
1016 std::unique_ptr<MockDataChannelObserver> data_observer_;
1017
1018 std::vector<std::unique_ptr<MockRtpReceiverObserver>> rtp_receiver_observers_;
1019
Steve Antonede9ca52017-10-16 13:04:27 -07001020 std::vector<PeerConnectionInterface::IceConnectionState>
1021 ice_connection_state_history_;
Jonas Olssonacd8ae72019-02-25 15:26:24 +01001022 std::vector<PeerConnectionInterface::IceConnectionState>
1023 standardized_ice_connection_state_history_;
Jonas Olsson635474e2018-10-18 15:58:17 +02001024 std::vector<PeerConnectionInterface::PeerConnectionState>
1025 peer_connection_state_history_;
Steve Antonede9ca52017-10-16 13:04:27 -07001026 std::vector<PeerConnectionInterface::IceGatheringState>
1027 ice_gathering_state_history_;
deadbeef1dcb1642017-03-29 21:08:16 -07001028
Qingsi Wang7685e862018-06-11 20:15:46 -07001029 webrtc::FakeRtcEventLogFactory* event_log_factory_;
1030
deadbeef1dcb1642017-03-29 21:08:16 -07001031 rtc::AsyncInvoker invoker_;
1032
Seth Hampson2f0d7022018-02-20 11:54:42 -08001033 friend class PeerConnectionIntegrationBaseTest;
deadbeef1dcb1642017-03-29 21:08:16 -07001034};
1035
Elad Alon99c3fe52017-10-13 16:29:40 +02001036class MockRtcEventLogOutput : public webrtc::RtcEventLogOutput {
1037 public:
1038 virtual ~MockRtcEventLogOutput() = default;
1039 MOCK_CONST_METHOD0(IsActive, bool());
1040 MOCK_METHOD1(Write, bool(const std::string&));
1041};
1042
Seth Hampson2f0d7022018-02-20 11:54:42 -08001043// This helper object is used for both specifying how many audio/video frames
1044// are expected to be received for a caller/callee. It provides helper functions
1045// to specify these expectations. The object initially starts in a state of no
1046// expectations.
1047class MediaExpectations {
1048 public:
1049 enum ExpectFrames {
1050 kExpectSomeFrames,
1051 kExpectNoFrames,
1052 kNoExpectation,
1053 };
1054
1055 void ExpectBidirectionalAudioAndVideo() {
1056 ExpectBidirectionalAudio();
1057 ExpectBidirectionalVideo();
1058 }
1059
1060 void ExpectBidirectionalAudio() {
1061 CallerExpectsSomeAudio();
1062 CalleeExpectsSomeAudio();
1063 }
1064
1065 void ExpectNoAudio() {
1066 CallerExpectsNoAudio();
1067 CalleeExpectsNoAudio();
1068 }
1069
1070 void ExpectBidirectionalVideo() {
1071 CallerExpectsSomeVideo();
1072 CalleeExpectsSomeVideo();
1073 }
1074
1075 void ExpectNoVideo() {
1076 CallerExpectsNoVideo();
1077 CalleeExpectsNoVideo();
1078 }
1079
1080 void CallerExpectsSomeAudioAndVideo() {
1081 CallerExpectsSomeAudio();
1082 CallerExpectsSomeVideo();
1083 }
1084
1085 void CalleeExpectsSomeAudioAndVideo() {
1086 CalleeExpectsSomeAudio();
1087 CalleeExpectsSomeVideo();
1088 }
1089
1090 // Caller's audio functions.
1091 void CallerExpectsSomeAudio(
1092 int expected_audio_frames = kDefaultExpectedAudioFrameCount) {
1093 caller_audio_expectation_ = kExpectSomeFrames;
1094 caller_audio_frames_expected_ = expected_audio_frames;
1095 }
1096
1097 void CallerExpectsNoAudio() {
1098 caller_audio_expectation_ = kExpectNoFrames;
1099 caller_audio_frames_expected_ = 0;
1100 }
1101
1102 // Caller's video functions.
1103 void CallerExpectsSomeVideo(
1104 int expected_video_frames = kDefaultExpectedVideoFrameCount) {
1105 caller_video_expectation_ = kExpectSomeFrames;
1106 caller_video_frames_expected_ = expected_video_frames;
1107 }
1108
1109 void CallerExpectsNoVideo() {
1110 caller_video_expectation_ = kExpectNoFrames;
1111 caller_video_frames_expected_ = 0;
1112 }
1113
1114 // Callee's audio functions.
1115 void CalleeExpectsSomeAudio(
1116 int expected_audio_frames = kDefaultExpectedAudioFrameCount) {
1117 callee_audio_expectation_ = kExpectSomeFrames;
1118 callee_audio_frames_expected_ = expected_audio_frames;
1119 }
1120
1121 void CalleeExpectsNoAudio() {
1122 callee_audio_expectation_ = kExpectNoFrames;
1123 callee_audio_frames_expected_ = 0;
1124 }
1125
1126 // Callee's video functions.
1127 void CalleeExpectsSomeVideo(
1128 int expected_video_frames = kDefaultExpectedVideoFrameCount) {
1129 callee_video_expectation_ = kExpectSomeFrames;
1130 callee_video_frames_expected_ = expected_video_frames;
1131 }
1132
1133 void CalleeExpectsNoVideo() {
1134 callee_video_expectation_ = kExpectNoFrames;
1135 callee_video_frames_expected_ = 0;
1136 }
1137
1138 ExpectFrames caller_audio_expectation_ = kNoExpectation;
1139 ExpectFrames caller_video_expectation_ = kNoExpectation;
1140 ExpectFrames callee_audio_expectation_ = kNoExpectation;
1141 ExpectFrames callee_video_expectation_ = kNoExpectation;
1142 int caller_audio_frames_expected_ = 0;
1143 int caller_video_frames_expected_ = 0;
1144 int callee_audio_frames_expected_ = 0;
1145 int callee_video_frames_expected_ = 0;
1146};
1147
deadbeef1dcb1642017-03-29 21:08:16 -07001148// Tests two PeerConnections connecting to each other end-to-end, using a
1149// virtual network, fake A/V capture and fake encoder/decoders. The
1150// PeerConnections share the threads/socket servers, but use separate versions
1151// of everything else (including "PeerConnectionFactory"s).
Mirko Bonadei6a489f22019-04-09 15:11:12 +02001152class PeerConnectionIntegrationBaseTest : public ::testing::Test {
deadbeef1dcb1642017-03-29 21:08:16 -07001153 public:
Seth Hampson2f0d7022018-02-20 11:54:42 -08001154 explicit PeerConnectionIntegrationBaseTest(SdpSemantics sdp_semantics)
1155 : sdp_semantics_(sdp_semantics),
1156 ss_(new rtc::VirtualSocketServer()),
Steve Antonede9ca52017-10-16 13:04:27 -07001157 fss_(new rtc::FirewallSocketServer(ss_.get())),
1158 network_thread_(new rtc::Thread(fss_.get())),
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001159 worker_thread_(rtc::Thread::Create()),
1160 loopback_media_transports_(network_thread_.get()) {
Sebastian Jansson8a793a02018-03-13 15:21:48 +01001161 network_thread_->SetName("PCNetworkThread", this);
1162 worker_thread_->SetName("PCWorkerThread", this);
deadbeef1dcb1642017-03-29 21:08:16 -07001163 RTC_CHECK(network_thread_->Start());
1164 RTC_CHECK(worker_thread_->Start());
Qingsi Wang7fc821d2018-07-12 12:54:53 -07001165 webrtc::metrics::Reset();
deadbeef1dcb1642017-03-29 21:08:16 -07001166 }
1167
Seth Hampson2f0d7022018-02-20 11:54:42 -08001168 ~PeerConnectionIntegrationBaseTest() {
Seth Hampsonaed71642018-06-11 07:41:32 -07001169 // The PeerConnections should deleted before the TurnCustomizers.
1170 // A TurnPort is created with a raw pointer to a TurnCustomizer. The
1171 // TurnPort has the same lifetime as the PeerConnection, so it's expected
1172 // that the TurnCustomizer outlives the life of the PeerConnection or else
1173 // when Send() is called it will hit a seg fault.
deadbeef1dcb1642017-03-29 21:08:16 -07001174 if (caller_) {
1175 caller_->set_signaling_message_receiver(nullptr);
Seth Hampsonaed71642018-06-11 07:41:32 -07001176 delete SetCallerPcWrapperAndReturnCurrent(nullptr);
deadbeef1dcb1642017-03-29 21:08:16 -07001177 }
1178 if (callee_) {
1179 callee_->set_signaling_message_receiver(nullptr);
Seth Hampsonaed71642018-06-11 07:41:32 -07001180 delete SetCalleePcWrapperAndReturnCurrent(nullptr);
deadbeef1dcb1642017-03-29 21:08:16 -07001181 }
Seth Hampsonaed71642018-06-11 07:41:32 -07001182
1183 // If turn servers were created for the test they need to be destroyed on
1184 // the network thread.
1185 network_thread()->Invoke<void>(RTC_FROM_HERE, [this] {
1186 turn_servers_.clear();
1187 turn_customizers_.clear();
1188 });
deadbeef1dcb1642017-03-29 21:08:16 -07001189 }
1190
1191 bool SignalingStateStable() {
1192 return caller_->SignalingStateStable() && callee_->SignalingStateStable();
1193 }
1194
deadbeef71452802017-05-07 17:21:01 -07001195 bool DtlsConnected() {
Alex Loiko9289eda2018-11-23 16:18:59 +00001196 // TODO(deadbeef): kIceConnectionConnected currently means both ICE and DTLS
1197 // are connected. This is an important distinction. Once we have separate
1198 // ICE and DTLS state, this check needs to use the DTLS state.
1199 return (callee()->ice_connection_state() ==
1200 webrtc::PeerConnectionInterface::kIceConnectionConnected ||
1201 callee()->ice_connection_state() ==
1202 webrtc::PeerConnectionInterface::kIceConnectionCompleted) &&
1203 (caller()->ice_connection_state() ==
1204 webrtc::PeerConnectionInterface::kIceConnectionConnected ||
1205 caller()->ice_connection_state() ==
1206 webrtc::PeerConnectionInterface::kIceConnectionCompleted);
deadbeef71452802017-05-07 17:21:01 -07001207 }
1208
Qingsi Wang7685e862018-06-11 20:15:46 -07001209 // When |event_log_factory| is null, the default implementation of the event
1210 // log factory will be used.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001211 std::unique_ptr<PeerConnectionWrapper> CreatePeerConnectionWrapper(
1212 const std::string& debug_name,
Seth Hampson2f0d7022018-02-20 11:54:42 -08001213 const PeerConnectionFactory::Options* options,
1214 const RTCConfiguration* config,
Qingsi Wang7685e862018-06-11 20:15:46 -07001215 webrtc::PeerConnectionDependencies dependencies,
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001216 std::unique_ptr<webrtc::FakeRtcEventLogFactory> event_log_factory,
1217 std::unique_ptr<webrtc::MediaTransportFactory> media_transport_factory) {
Seth Hampson2f0d7022018-02-20 11:54:42 -08001218 RTCConfiguration modified_config;
1219 if (config) {
1220 modified_config = *config;
1221 }
Steve Anton3acffc32018-04-12 17:21:03 -07001222 modified_config.sdp_semantics = sdp_semantics_;
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001223 if (!dependencies.cert_generator) {
1224 dependencies.cert_generator =
Karl Wiberg918f50c2018-07-05 11:40:33 +02001225 absl::make_unique<FakeRTCCertificateGenerator>();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001226 }
1227 std::unique_ptr<PeerConnectionWrapper> client(
1228 new PeerConnectionWrapper(debug_name));
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001229
Niels Möllerf06f9232018-08-07 12:32:18 +02001230 if (!client->Init(options, &modified_config, std::move(dependencies),
1231 network_thread_.get(), worker_thread_.get(),
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001232 std::move(event_log_factory),
1233 std::move(media_transport_factory))) {
Seth Hampson2f0d7022018-02-20 11:54:42 -08001234 return nullptr;
1235 }
1236 return client;
1237 }
1238
Qingsi Wang7685e862018-06-11 20:15:46 -07001239 std::unique_ptr<PeerConnectionWrapper>
1240 CreatePeerConnectionWrapperWithFakeRtcEventLog(
1241 const std::string& debug_name,
Qingsi Wang7685e862018-06-11 20:15:46 -07001242 const PeerConnectionFactory::Options* options,
1243 const RTCConfiguration* config,
1244 webrtc::PeerConnectionDependencies dependencies) {
1245 std::unique_ptr<webrtc::FakeRtcEventLogFactory> event_log_factory(
1246 new webrtc::FakeRtcEventLogFactory(rtc::Thread::Current()));
Niels Möllerf06f9232018-08-07 12:32:18 +02001247 return CreatePeerConnectionWrapper(debug_name, options, config,
Qingsi Wang7685e862018-06-11 20:15:46 -07001248 std::move(dependencies),
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001249 std::move(event_log_factory),
1250 /*media_transport_factory=*/nullptr);
Qingsi Wang7685e862018-06-11 20:15:46 -07001251 }
1252
deadbeef1dcb1642017-03-29 21:08:16 -07001253 bool CreatePeerConnectionWrappers() {
1254 return CreatePeerConnectionWrappersWithConfig(
1255 PeerConnectionInterface::RTCConfiguration(),
1256 PeerConnectionInterface::RTCConfiguration());
1257 }
1258
Steve Anton3acffc32018-04-12 17:21:03 -07001259 bool CreatePeerConnectionWrappersWithSdpSemantics(
1260 SdpSemantics caller_semantics,
1261 SdpSemantics callee_semantics) {
1262 // Can't specify the sdp_semantics in the passed-in configuration since it
1263 // will be overwritten by CreatePeerConnectionWrapper with whatever is
1264 // stored in sdp_semantics_. So get around this by modifying the instance
1265 // variable before calling CreatePeerConnectionWrapper for the caller and
1266 // callee PeerConnections.
1267 SdpSemantics original_semantics = sdp_semantics_;
1268 sdp_semantics_ = caller_semantics;
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001269 caller_ = CreatePeerConnectionWrapper(
Niels Möllerf06f9232018-08-07 12:32:18 +02001270 "Caller", nullptr, nullptr, webrtc::PeerConnectionDependencies(nullptr),
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001271 nullptr, /*media_transport_factory=*/nullptr);
Steve Anton3acffc32018-04-12 17:21:03 -07001272 sdp_semantics_ = callee_semantics;
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001273 callee_ = CreatePeerConnectionWrapper(
Niels Möllerf06f9232018-08-07 12:32:18 +02001274 "Callee", nullptr, nullptr, webrtc::PeerConnectionDependencies(nullptr),
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001275 nullptr, /*media_transport_factory=*/nullptr);
Steve Anton3acffc32018-04-12 17:21:03 -07001276 sdp_semantics_ = original_semantics;
1277 return caller_ && callee_;
1278 }
1279
deadbeef1dcb1642017-03-29 21:08:16 -07001280 bool CreatePeerConnectionWrappersWithConfig(
1281 const PeerConnectionInterface::RTCConfiguration& caller_config,
1282 const PeerConnectionInterface::RTCConfiguration& callee_config) {
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001283 caller_ = CreatePeerConnectionWrapper(
Niels Möllerf06f9232018-08-07 12:32:18 +02001284 "Caller", nullptr, &caller_config,
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001285 webrtc::PeerConnectionDependencies(nullptr), nullptr,
1286 /*media_transport_factory=*/nullptr);
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001287 callee_ = CreatePeerConnectionWrapper(
Niels Möllerf06f9232018-08-07 12:32:18 +02001288 "Callee", nullptr, &callee_config,
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001289 webrtc::PeerConnectionDependencies(nullptr), nullptr,
1290 /*media_transport_factory=*/nullptr);
1291 return caller_ && callee_;
1292 }
1293
1294 bool CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
1295 const PeerConnectionInterface::RTCConfiguration& caller_config,
1296 const PeerConnectionInterface::RTCConfiguration& callee_config,
1297 std::unique_ptr<webrtc::MediaTransportFactory> caller_factory,
1298 std::unique_ptr<webrtc::MediaTransportFactory> callee_factory) {
1299 caller_ =
1300 CreatePeerConnectionWrapper("Caller", nullptr, &caller_config,
1301 webrtc::PeerConnectionDependencies(nullptr),
1302 nullptr, std::move(caller_factory));
1303 callee_ =
1304 CreatePeerConnectionWrapper("Callee", nullptr, &callee_config,
1305 webrtc::PeerConnectionDependencies(nullptr),
1306 nullptr, std::move(callee_factory));
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001307 return caller_ && callee_;
1308 }
1309
1310 bool CreatePeerConnectionWrappersWithConfigAndDeps(
1311 const PeerConnectionInterface::RTCConfiguration& caller_config,
1312 webrtc::PeerConnectionDependencies caller_dependencies,
1313 const PeerConnectionInterface::RTCConfiguration& callee_config,
1314 webrtc::PeerConnectionDependencies callee_dependencies) {
1315 caller_ =
Niels Möllerf06f9232018-08-07 12:32:18 +02001316 CreatePeerConnectionWrapper("Caller", nullptr, &caller_config,
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001317 std::move(caller_dependencies), nullptr,
1318 /*media_transport_factory=*/nullptr);
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001319 callee_ =
Niels Möllerf06f9232018-08-07 12:32:18 +02001320 CreatePeerConnectionWrapper("Callee", nullptr, &callee_config,
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001321 std::move(callee_dependencies), nullptr,
1322 /*media_transport_factory=*/nullptr);
deadbeef1dcb1642017-03-29 21:08:16 -07001323 return caller_ && callee_;
1324 }
1325
1326 bool CreatePeerConnectionWrappersWithOptions(
1327 const PeerConnectionFactory::Options& caller_options,
1328 const PeerConnectionFactory::Options& callee_options) {
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001329 caller_ = CreatePeerConnectionWrapper(
Niels Möllerf06f9232018-08-07 12:32:18 +02001330 "Caller", &caller_options, nullptr,
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001331 webrtc::PeerConnectionDependencies(nullptr), nullptr,
1332 /*media_transport_factory=*/nullptr);
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001333 callee_ = CreatePeerConnectionWrapper(
Niels Möllerf06f9232018-08-07 12:32:18 +02001334 "Callee", &callee_options, nullptr,
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001335 webrtc::PeerConnectionDependencies(nullptr), nullptr,
1336 /*media_transport_factory=*/nullptr);
Qingsi Wang7685e862018-06-11 20:15:46 -07001337 return caller_ && callee_;
1338 }
1339
1340 bool CreatePeerConnectionWrappersWithFakeRtcEventLog() {
1341 PeerConnectionInterface::RTCConfiguration default_config;
1342 caller_ = CreatePeerConnectionWrapperWithFakeRtcEventLog(
Niels Möllerf06f9232018-08-07 12:32:18 +02001343 "Caller", nullptr, &default_config,
Qingsi Wang7685e862018-06-11 20:15:46 -07001344 webrtc::PeerConnectionDependencies(nullptr));
1345 callee_ = CreatePeerConnectionWrapperWithFakeRtcEventLog(
Niels Möllerf06f9232018-08-07 12:32:18 +02001346 "Callee", nullptr, &default_config,
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001347 webrtc::PeerConnectionDependencies(nullptr));
deadbeef1dcb1642017-03-29 21:08:16 -07001348 return caller_ && callee_;
1349 }
1350
Seth Hampson2f0d7022018-02-20 11:54:42 -08001351 std::unique_ptr<PeerConnectionWrapper>
1352 CreatePeerConnectionWrapperWithAlternateKey() {
deadbeef1dcb1642017-03-29 21:08:16 -07001353 std::unique_ptr<FakeRTCCertificateGenerator> cert_generator(
1354 new FakeRTCCertificateGenerator());
1355 cert_generator->use_alternate_key();
1356
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001357 webrtc::PeerConnectionDependencies dependencies(nullptr);
1358 dependencies.cert_generator = std::move(cert_generator);
Niels Möllerf06f9232018-08-07 12:32:18 +02001359 return CreatePeerConnectionWrapper("New Peer", nullptr, nullptr,
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001360 std::move(dependencies), nullptr,
1361 /*media_transport_factory=*/nullptr);
deadbeef1dcb1642017-03-29 21:08:16 -07001362 }
1363
Seth Hampsonaed71642018-06-11 07:41:32 -07001364 cricket::TestTurnServer* CreateTurnServer(
1365 rtc::SocketAddress internal_address,
1366 rtc::SocketAddress external_address,
1367 cricket::ProtocolType type = cricket::ProtocolType::PROTO_UDP,
1368 const std::string& common_name = "test turn server") {
1369 rtc::Thread* thread = network_thread();
1370 std::unique_ptr<cricket::TestTurnServer> turn_server =
1371 network_thread()->Invoke<std::unique_ptr<cricket::TestTurnServer>>(
1372 RTC_FROM_HERE,
1373 [thread, internal_address, external_address, type, common_name] {
Karl Wiberg918f50c2018-07-05 11:40:33 +02001374 return absl::make_unique<cricket::TestTurnServer>(
Seth Hampsonaed71642018-06-11 07:41:32 -07001375 thread, internal_address, external_address, type,
1376 /*ignore_bad_certs=*/true, common_name);
1377 });
1378 turn_servers_.push_back(std::move(turn_server));
1379 // Interactions with the turn server should be done on the network thread.
1380 return turn_servers_.back().get();
1381 }
1382
1383 cricket::TestTurnCustomizer* CreateTurnCustomizer() {
1384 std::unique_ptr<cricket::TestTurnCustomizer> turn_customizer =
1385 network_thread()->Invoke<std::unique_ptr<cricket::TestTurnCustomizer>>(
1386 RTC_FROM_HERE,
Karl Wiberg918f50c2018-07-05 11:40:33 +02001387 [] { return absl::make_unique<cricket::TestTurnCustomizer>(); });
Seth Hampsonaed71642018-06-11 07:41:32 -07001388 turn_customizers_.push_back(std::move(turn_customizer));
1389 // Interactions with the turn customizer should be done on the network
1390 // thread.
1391 return turn_customizers_.back().get();
1392 }
1393
1394 // Checks that the function counters for a TestTurnCustomizer are greater than
1395 // 0.
1396 void ExpectTurnCustomizerCountersIncremented(
1397 cricket::TestTurnCustomizer* turn_customizer) {
1398 unsigned int allow_channel_data_counter =
1399 network_thread()->Invoke<unsigned int>(
1400 RTC_FROM_HERE, [turn_customizer] {
1401 return turn_customizer->allow_channel_data_cnt_;
1402 });
1403 EXPECT_GT(allow_channel_data_counter, 0u);
1404 unsigned int modify_counter = network_thread()->Invoke<unsigned int>(
1405 RTC_FROM_HERE,
1406 [turn_customizer] { return turn_customizer->modify_cnt_; });
1407 EXPECT_GT(modify_counter, 0u);
1408 }
1409
deadbeef1dcb1642017-03-29 21:08:16 -07001410 // Once called, SDP blobs and ICE candidates will be automatically signaled
1411 // between PeerConnections.
1412 void ConnectFakeSignaling() {
1413 caller_->set_signaling_message_receiver(callee_.get());
1414 callee_->set_signaling_message_receiver(caller_.get());
1415 }
1416
Steve Antonede9ca52017-10-16 13:04:27 -07001417 // Once called, SDP blobs will be automatically signaled between
1418 // PeerConnections. Note that ICE candidates will not be signaled unless they
1419 // are in the exchanged SDP blobs.
1420 void ConnectFakeSignalingForSdpOnly() {
1421 ConnectFakeSignaling();
1422 SetSignalIceCandidates(false);
1423 }
1424
deadbeef1dcb1642017-03-29 21:08:16 -07001425 void SetSignalingDelayMs(int delay_ms) {
1426 caller_->set_signaling_delay_ms(delay_ms);
1427 callee_->set_signaling_delay_ms(delay_ms);
1428 }
1429
Steve Antonede9ca52017-10-16 13:04:27 -07001430 void SetSignalIceCandidates(bool signal) {
1431 caller_->set_signal_ice_candidates(signal);
1432 callee_->set_signal_ice_candidates(signal);
1433 }
1434
deadbeef1dcb1642017-03-29 21:08:16 -07001435 // Messages may get lost on the unreliable DataChannel, so we send multiple
1436 // times to avoid test flakiness.
1437 void SendRtpDataWithRetries(webrtc::DataChannelInterface* dc,
1438 const std::string& data,
1439 int retries) {
1440 for (int i = 0; i < retries; ++i) {
1441 dc->Send(DataBuffer(data));
1442 }
1443 }
1444
1445 rtc::Thread* network_thread() { return network_thread_.get(); }
1446
1447 rtc::VirtualSocketServer* virtual_socket_server() { return ss_.get(); }
1448
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001449 webrtc::MediaTransportPair* loopback_media_transports() {
1450 return &loopback_media_transports_;
1451 }
1452
deadbeef1dcb1642017-03-29 21:08:16 -07001453 PeerConnectionWrapper* caller() { return caller_.get(); }
1454
1455 // Set the |caller_| to the |wrapper| passed in and return the
1456 // original |caller_|.
1457 PeerConnectionWrapper* SetCallerPcWrapperAndReturnCurrent(
1458 PeerConnectionWrapper* wrapper) {
1459 PeerConnectionWrapper* old = caller_.release();
1460 caller_.reset(wrapper);
1461 return old;
1462 }
1463
1464 PeerConnectionWrapper* callee() { return callee_.get(); }
1465
1466 // Set the |callee_| to the |wrapper| passed in and return the
1467 // original |callee_|.
1468 PeerConnectionWrapper* SetCalleePcWrapperAndReturnCurrent(
1469 PeerConnectionWrapper* wrapper) {
1470 PeerConnectionWrapper* old = callee_.release();
1471 callee_.reset(wrapper);
1472 return old;
1473 }
1474
Qingsi Wang1dac6d82018-12-12 15:28:47 -08001475 void SetPortAllocatorFlags(uint32_t caller_flags, uint32_t callee_flags) {
1476 network_thread()->Invoke<void>(
1477 RTC_FROM_HERE, rtc::Bind(&cricket::PortAllocator::set_flags,
1478 caller()->port_allocator(), caller_flags));
1479 network_thread()->Invoke<void>(
1480 RTC_FROM_HERE, rtc::Bind(&cricket::PortAllocator::set_flags,
1481 callee()->port_allocator(), callee_flags));
1482 }
1483
Steve Antonede9ca52017-10-16 13:04:27 -07001484 rtc::FirewallSocketServer* firewall() const { return fss_.get(); }
1485
Seth Hampson2f0d7022018-02-20 11:54:42 -08001486 // Expects the provided number of new frames to be received within
1487 // kMaxWaitForFramesMs. The new expected frames are specified in
1488 // |media_expectations|. Returns false if any of the expectations were
1489 // not met.
1490 bool ExpectNewFrames(const MediaExpectations& media_expectations) {
1491 // First initialize the expected frame counts based upon the current
1492 // frame count.
1493 int total_caller_audio_frames_expected = caller()->audio_frames_received();
1494 if (media_expectations.caller_audio_expectation_ ==
1495 MediaExpectations::kExpectSomeFrames) {
1496 total_caller_audio_frames_expected +=
1497 media_expectations.caller_audio_frames_expected_;
1498 }
1499 int total_caller_video_frames_expected =
deadbeef1dcb1642017-03-29 21:08:16 -07001500 caller()->min_video_frames_received_per_track();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001501 if (media_expectations.caller_video_expectation_ ==
1502 MediaExpectations::kExpectSomeFrames) {
1503 total_caller_video_frames_expected +=
1504 media_expectations.caller_video_frames_expected_;
1505 }
1506 int total_callee_audio_frames_expected = callee()->audio_frames_received();
1507 if (media_expectations.callee_audio_expectation_ ==
1508 MediaExpectations::kExpectSomeFrames) {
1509 total_callee_audio_frames_expected +=
1510 media_expectations.callee_audio_frames_expected_;
1511 }
1512 int total_callee_video_frames_expected =
deadbeef1dcb1642017-03-29 21:08:16 -07001513 callee()->min_video_frames_received_per_track();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001514 if (media_expectations.callee_video_expectation_ ==
1515 MediaExpectations::kExpectSomeFrames) {
1516 total_callee_video_frames_expected +=
1517 media_expectations.callee_video_frames_expected_;
1518 }
deadbeef1dcb1642017-03-29 21:08:16 -07001519
Seth Hampson2f0d7022018-02-20 11:54:42 -08001520 // Wait for the expected frames.
deadbeef1dcb1642017-03-29 21:08:16 -07001521 EXPECT_TRUE_WAIT(caller()->audio_frames_received() >=
Seth Hampson2f0d7022018-02-20 11:54:42 -08001522 total_caller_audio_frames_expected &&
deadbeef1dcb1642017-03-29 21:08:16 -07001523 caller()->min_video_frames_received_per_track() >=
Seth Hampson2f0d7022018-02-20 11:54:42 -08001524 total_caller_video_frames_expected &&
deadbeef1dcb1642017-03-29 21:08:16 -07001525 callee()->audio_frames_received() >=
Seth Hampson2f0d7022018-02-20 11:54:42 -08001526 total_callee_audio_frames_expected &&
deadbeef1dcb1642017-03-29 21:08:16 -07001527 callee()->min_video_frames_received_per_track() >=
Seth Hampson2f0d7022018-02-20 11:54:42 -08001528 total_callee_video_frames_expected,
1529 kMaxWaitForFramesMs);
1530 bool expectations_correct =
1531 caller()->audio_frames_received() >=
1532 total_caller_audio_frames_expected &&
1533 caller()->min_video_frames_received_per_track() >=
1534 total_caller_video_frames_expected &&
1535 callee()->audio_frames_received() >=
1536 total_callee_audio_frames_expected &&
1537 callee()->min_video_frames_received_per_track() >=
1538 total_callee_video_frames_expected;
deadbeef1dcb1642017-03-29 21:08:16 -07001539
Seth Hampson2f0d7022018-02-20 11:54:42 -08001540 // After the combined wait, print out a more detailed message upon
1541 // failure.
deadbeef1dcb1642017-03-29 21:08:16 -07001542 EXPECT_GE(caller()->audio_frames_received(),
Seth Hampson2f0d7022018-02-20 11:54:42 -08001543 total_caller_audio_frames_expected);
deadbeef1dcb1642017-03-29 21:08:16 -07001544 EXPECT_GE(caller()->min_video_frames_received_per_track(),
Seth Hampson2f0d7022018-02-20 11:54:42 -08001545 total_caller_video_frames_expected);
deadbeef1dcb1642017-03-29 21:08:16 -07001546 EXPECT_GE(callee()->audio_frames_received(),
Seth Hampson2f0d7022018-02-20 11:54:42 -08001547 total_callee_audio_frames_expected);
deadbeef1dcb1642017-03-29 21:08:16 -07001548 EXPECT_GE(callee()->min_video_frames_received_per_track(),
Seth Hampson2f0d7022018-02-20 11:54:42 -08001549 total_callee_video_frames_expected);
1550
1551 // We want to make sure nothing unexpected was received.
1552 if (media_expectations.caller_audio_expectation_ ==
1553 MediaExpectations::kExpectNoFrames) {
1554 EXPECT_EQ(caller()->audio_frames_received(),
1555 total_caller_audio_frames_expected);
1556 if (caller()->audio_frames_received() !=
1557 total_caller_audio_frames_expected) {
1558 expectations_correct = false;
1559 }
1560 }
1561 if (media_expectations.caller_video_expectation_ ==
1562 MediaExpectations::kExpectNoFrames) {
1563 EXPECT_EQ(caller()->min_video_frames_received_per_track(),
1564 total_caller_video_frames_expected);
1565 if (caller()->min_video_frames_received_per_track() !=
1566 total_caller_video_frames_expected) {
1567 expectations_correct = false;
1568 }
1569 }
1570 if (media_expectations.callee_audio_expectation_ ==
1571 MediaExpectations::kExpectNoFrames) {
1572 EXPECT_EQ(callee()->audio_frames_received(),
1573 total_callee_audio_frames_expected);
1574 if (callee()->audio_frames_received() !=
1575 total_callee_audio_frames_expected) {
1576 expectations_correct = false;
1577 }
1578 }
1579 if (media_expectations.callee_video_expectation_ ==
1580 MediaExpectations::kExpectNoFrames) {
1581 EXPECT_EQ(callee()->min_video_frames_received_per_track(),
1582 total_callee_video_frames_expected);
1583 if (callee()->min_video_frames_received_per_track() !=
1584 total_callee_video_frames_expected) {
1585 expectations_correct = false;
1586 }
1587 }
1588 return expectations_correct;
deadbeef1dcb1642017-03-29 21:08:16 -07001589 }
1590
Steve Antond91969e2019-05-30 12:27:03 -07001591 void ClosePeerConnections() {
1592 caller()->pc()->Close();
1593 callee()->pc()->Close();
1594 }
1595
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07001596 void TestNegotiatedCipherSuite(
1597 const PeerConnectionFactory::Options& caller_options,
1598 const PeerConnectionFactory::Options& callee_options,
1599 int expected_cipher_suite) {
deadbeef1dcb1642017-03-29 21:08:16 -07001600 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(caller_options,
1601 callee_options));
deadbeef1dcb1642017-03-29 21:08:16 -07001602 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001603 caller()->AddAudioVideoTracks();
1604 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001605 caller()->CreateAndSetAndSignalOffer();
Qingsi Wang7fc821d2018-07-12 12:54:53 -07001606 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07001607 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(expected_cipher_suite),
deadbeefd8ad7882017-04-18 16:01:17 -07001608 caller()->OldGetStats()->SrtpCipher(), kDefaultTimeout);
Qingsi Wang7fc821d2018-07-12 12:54:53 -07001609 // TODO(bugs.webrtc.org/9456): Fix it.
Alex Loiko9289eda2018-11-23 16:18:59 +00001610 EXPECT_EQ(1, webrtc::metrics::NumEvents(
Qingsi Wang7fc821d2018-07-12 12:54:53 -07001611 "WebRTC.PeerConnection.SrtpCryptoSuite.Audio",
1612 expected_cipher_suite));
deadbeef1dcb1642017-03-29 21:08:16 -07001613 }
1614
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07001615 void TestGcmNegotiationUsesCipherSuite(bool local_gcm_enabled,
1616 bool remote_gcm_enabled,
1617 int expected_cipher_suite) {
1618 PeerConnectionFactory::Options caller_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07001619 caller_options.crypto_options.srtp.enable_gcm_crypto_suites =
1620 local_gcm_enabled;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07001621 PeerConnectionFactory::Options callee_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07001622 callee_options.crypto_options.srtp.enable_gcm_crypto_suites =
1623 remote_gcm_enabled;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07001624 TestNegotiatedCipherSuite(caller_options, callee_options,
1625 expected_cipher_suite);
1626 }
1627
Seth Hampson2f0d7022018-02-20 11:54:42 -08001628 protected:
Steve Anton3acffc32018-04-12 17:21:03 -07001629 SdpSemantics sdp_semantics_;
Seth Hampson2f0d7022018-02-20 11:54:42 -08001630
deadbeef1dcb1642017-03-29 21:08:16 -07001631 private:
1632 // |ss_| is used by |network_thread_| so it must be destroyed later.
deadbeef1dcb1642017-03-29 21:08:16 -07001633 std::unique_ptr<rtc::VirtualSocketServer> ss_;
Steve Antonede9ca52017-10-16 13:04:27 -07001634 std::unique_ptr<rtc::FirewallSocketServer> fss_;
deadbeef1dcb1642017-03-29 21:08:16 -07001635 // |network_thread_| and |worker_thread_| are used by both
1636 // |caller_| and |callee_| so they must be destroyed
1637 // later.
1638 std::unique_ptr<rtc::Thread> network_thread_;
1639 std::unique_ptr<rtc::Thread> worker_thread_;
Seth Hampsonaed71642018-06-11 07:41:32 -07001640 // The turn servers and turn customizers should be accessed & deleted on the
1641 // network thread to avoid a race with the socket read/write that occurs
1642 // on the network thread.
1643 std::vector<std::unique_ptr<cricket::TestTurnServer>> turn_servers_;
1644 std::vector<std::unique_ptr<cricket::TestTurnCustomizer>> turn_customizers_;
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001645 webrtc::MediaTransportPair loopback_media_transports_;
deadbeef1dcb1642017-03-29 21:08:16 -07001646 std::unique_ptr<PeerConnectionWrapper> caller_;
1647 std::unique_ptr<PeerConnectionWrapper> callee_;
1648};
1649
Seth Hampson2f0d7022018-02-20 11:54:42 -08001650class PeerConnectionIntegrationTest
1651 : public PeerConnectionIntegrationBaseTest,
1652 public ::testing::WithParamInterface<SdpSemantics> {
1653 protected:
1654 PeerConnectionIntegrationTest()
1655 : PeerConnectionIntegrationBaseTest(GetParam()) {}
1656};
1657
1658class PeerConnectionIntegrationTestPlanB
1659 : public PeerConnectionIntegrationBaseTest {
1660 protected:
1661 PeerConnectionIntegrationTestPlanB()
1662 : PeerConnectionIntegrationBaseTest(SdpSemantics::kPlanB) {}
1663};
1664
1665class PeerConnectionIntegrationTestUnifiedPlan
1666 : public PeerConnectionIntegrationBaseTest {
1667 protected:
1668 PeerConnectionIntegrationTestUnifiedPlan()
1669 : PeerConnectionIntegrationBaseTest(SdpSemantics::kUnifiedPlan) {}
1670};
1671
deadbeef1dcb1642017-03-29 21:08:16 -07001672// Test the OnFirstPacketReceived callback from audio/video RtpReceivers. This
1673// includes testing that the callback is invoked if an observer is connected
1674// after the first packet has already been received.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001675TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07001676 RtpReceiverObserverOnFirstPacketReceived) {
1677 ASSERT_TRUE(CreatePeerConnectionWrappers());
1678 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001679 caller()->AddAudioVideoTracks();
1680 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001681 // Start offer/answer exchange and wait for it to complete.
1682 caller()->CreateAndSetAndSignalOffer();
1683 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1684 // Should be one receiver each for audio/video.
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02001685 EXPECT_EQ(2U, caller()->rtp_receiver_observers().size());
1686 EXPECT_EQ(2U, callee()->rtp_receiver_observers().size());
deadbeef1dcb1642017-03-29 21:08:16 -07001687 // Wait for all "first packet received" callbacks to be fired.
1688 EXPECT_TRUE_WAIT(
Steve Anton64b626b2019-01-28 17:25:26 -08001689 absl::c_all_of(caller()->rtp_receiver_observers(),
1690 [](const std::unique_ptr<MockRtpReceiverObserver>& o) {
1691 return o->first_packet_received();
1692 }),
deadbeef1dcb1642017-03-29 21:08:16 -07001693 kMaxWaitForFramesMs);
1694 EXPECT_TRUE_WAIT(
Steve Anton64b626b2019-01-28 17:25:26 -08001695 absl::c_all_of(callee()->rtp_receiver_observers(),
1696 [](const std::unique_ptr<MockRtpReceiverObserver>& o) {
1697 return o->first_packet_received();
1698 }),
deadbeef1dcb1642017-03-29 21:08:16 -07001699 kMaxWaitForFramesMs);
1700 // If new observers are set after the first packet was already received, the
1701 // callback should still be invoked.
1702 caller()->ResetRtpReceiverObservers();
1703 callee()->ResetRtpReceiverObservers();
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02001704 EXPECT_EQ(2U, caller()->rtp_receiver_observers().size());
1705 EXPECT_EQ(2U, callee()->rtp_receiver_observers().size());
deadbeef1dcb1642017-03-29 21:08:16 -07001706 EXPECT_TRUE(
Steve Anton64b626b2019-01-28 17:25:26 -08001707 absl::c_all_of(caller()->rtp_receiver_observers(),
1708 [](const std::unique_ptr<MockRtpReceiverObserver>& o) {
1709 return o->first_packet_received();
1710 }));
deadbeef1dcb1642017-03-29 21:08:16 -07001711 EXPECT_TRUE(
Steve Anton64b626b2019-01-28 17:25:26 -08001712 absl::c_all_of(callee()->rtp_receiver_observers(),
1713 [](const std::unique_ptr<MockRtpReceiverObserver>& o) {
1714 return o->first_packet_received();
1715 }));
deadbeef1dcb1642017-03-29 21:08:16 -07001716}
1717
1718class DummyDtmfObserver : public DtmfSenderObserverInterface {
1719 public:
1720 DummyDtmfObserver() : completed_(false) {}
1721
1722 // Implements DtmfSenderObserverInterface.
1723 void OnToneChange(const std::string& tone) override {
1724 tones_.push_back(tone);
1725 if (tone.empty()) {
1726 completed_ = true;
1727 }
1728 }
1729
1730 const std::vector<std::string>& tones() const { return tones_; }
1731 bool completed() const { return completed_; }
1732
1733 private:
1734 bool completed_;
1735 std::vector<std::string> tones_;
1736};
1737
1738// Assumes |sender| already has an audio track added and the offer/answer
1739// exchange is done.
1740void TestDtmfFromSenderToReceiver(PeerConnectionWrapper* sender,
1741 PeerConnectionWrapper* receiver) {
Steve Anton15324772018-01-16 10:26:49 -08001742 // We should be able to get a DTMF sender from the local sender.
1743 rtc::scoped_refptr<DtmfSenderInterface> dtmf_sender =
1744 sender->pc()->GetSenders().at(0)->GetDtmfSender();
1745 ASSERT_TRUE(dtmf_sender);
deadbeef1dcb1642017-03-29 21:08:16 -07001746 DummyDtmfObserver observer;
deadbeef1dcb1642017-03-29 21:08:16 -07001747 dtmf_sender->RegisterObserver(&observer);
1748
1749 // Test the DtmfSender object just created.
1750 EXPECT_TRUE(dtmf_sender->CanInsertDtmf());
1751 EXPECT_TRUE(dtmf_sender->InsertDtmf("1a", 100, 50));
1752
1753 EXPECT_TRUE_WAIT(observer.completed(), kDefaultTimeout);
1754 std::vector<std::string> tones = {"1", "a", ""};
1755 EXPECT_EQ(tones, observer.tones());
1756 dtmf_sender->UnregisterObserver();
1757 // TODO(deadbeef): Verify the tones were actually received end-to-end.
1758}
1759
1760// Verifies the DtmfSenderObserver callbacks for a DtmfSender (one in each
1761// direction).
Seth Hampson2f0d7022018-02-20 11:54:42 -08001762TEST_P(PeerConnectionIntegrationTest, DtmfSenderObserver) {
deadbeef1dcb1642017-03-29 21:08:16 -07001763 ASSERT_TRUE(CreatePeerConnectionWrappers());
1764 ConnectFakeSignaling();
1765 // Only need audio for DTMF.
Steve Anton15324772018-01-16 10:26:49 -08001766 caller()->AddAudioTrack();
1767 callee()->AddAudioTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07001768 caller()->CreateAndSetAndSignalOffer();
1769 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
deadbeef71452802017-05-07 17:21:01 -07001770 // DTLS must finish before the DTMF sender can be used reliably.
1771 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07001772 TestDtmfFromSenderToReceiver(caller(), callee());
1773 TestDtmfFromSenderToReceiver(callee(), caller());
1774}
1775
1776// Basic end-to-end test, verifying media can be encoded/transmitted/decoded
1777// between two connections, using DTLS-SRTP.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001778TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithDtls) {
deadbeef1dcb1642017-03-29 21:08:16 -07001779 ASSERT_TRUE(CreatePeerConnectionWrappers());
1780 ConnectFakeSignaling();
Harald Alvestrand194939b2018-01-24 16:04:13 +01001781
deadbeef1dcb1642017-03-29 21:08:16 -07001782 // Do normal offer/answer and wait for some frames to be received in each
1783 // direction.
Steve Anton15324772018-01-16 10:26:49 -08001784 caller()->AddAudioVideoTracks();
1785 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001786 caller()->CreateAndSetAndSignalOffer();
1787 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001788 MediaExpectations media_expectations;
1789 media_expectations.ExpectBidirectionalAudioAndVideo();
1790 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Qingsi Wang7fc821d2018-07-12 12:54:53 -07001791 EXPECT_LE(2, webrtc::metrics::NumEvents("WebRTC.PeerConnection.KeyProtocol",
1792 webrtc::kEnumCounterKeyProtocolDtls));
1793 EXPECT_EQ(0, webrtc::metrics::NumEvents("WebRTC.PeerConnection.KeyProtocol",
1794 webrtc::kEnumCounterKeyProtocolSdes));
deadbeef1dcb1642017-03-29 21:08:16 -07001795}
1796
1797// Uses SDES instead of DTLS for key agreement.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001798TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithSdes) {
deadbeef1dcb1642017-03-29 21:08:16 -07001799 PeerConnectionInterface::RTCConfiguration sdes_config;
1800 sdes_config.enable_dtls_srtp.emplace(false);
1801 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(sdes_config, sdes_config));
1802 ConnectFakeSignaling();
1803
1804 // Do normal offer/answer and wait for some frames to be received in each
1805 // direction.
Steve Anton15324772018-01-16 10:26:49 -08001806 caller()->AddAudioVideoTracks();
1807 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001808 caller()->CreateAndSetAndSignalOffer();
1809 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001810 MediaExpectations media_expectations;
1811 media_expectations.ExpectBidirectionalAudioAndVideo();
1812 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Qingsi Wang7fc821d2018-07-12 12:54:53 -07001813 EXPECT_LE(2, webrtc::metrics::NumEvents("WebRTC.PeerConnection.KeyProtocol",
1814 webrtc::kEnumCounterKeyProtocolSdes));
1815 EXPECT_EQ(0, webrtc::metrics::NumEvents("WebRTC.PeerConnection.KeyProtocol",
1816 webrtc::kEnumCounterKeyProtocolDtls));
deadbeef1dcb1642017-03-29 21:08:16 -07001817}
1818
Steve Anton9a44b2d2019-07-12 12:58:30 -07001819// Basic end-to-end test specifying the |enable_encrypted_rtp_header_extensions|
1820// option to offer encrypted versions of all header extensions alongside the
1821// unencrypted versions.
1822TEST_P(PeerConnectionIntegrationTest,
1823 EndToEndCallWithEncryptedRtpHeaderExtensions) {
1824 CryptoOptions crypto_options;
1825 crypto_options.srtp.enable_encrypted_rtp_header_extensions = true;
1826 PeerConnectionInterface::RTCConfiguration config;
1827 config.crypto_options = crypto_options;
1828 // Note: This allows offering >14 RTP header extensions.
1829 config.offer_extmap_allow_mixed = true;
1830 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
1831 ConnectFakeSignaling();
1832
1833 // Do normal offer/answer and wait for some frames to be received in each
1834 // direction.
1835 caller()->AddAudioVideoTracks();
1836 callee()->AddAudioVideoTracks();
1837 caller()->CreateAndSetAndSignalOffer();
1838 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1839 MediaExpectations media_expectations;
1840 media_expectations.ExpectBidirectionalAudioAndVideo();
1841 ASSERT_TRUE(ExpectNewFrames(media_expectations));
1842}
1843
Steve Anton8c0f7a72017-10-03 10:03:10 -07001844// Tests that the GetRemoteAudioSSLCertificate method returns the remote DTLS
1845// certificate once the DTLS handshake has finished.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001846TEST_P(PeerConnectionIntegrationTest,
Steve Anton8c0f7a72017-10-03 10:03:10 -07001847 GetRemoteAudioSSLCertificateReturnsExchangedCertificate) {
1848 auto GetRemoteAudioSSLCertificate = [](PeerConnectionWrapper* wrapper) {
1849 auto pci = reinterpret_cast<PeerConnectionProxy*>(wrapper->pc());
1850 auto pc = reinterpret_cast<PeerConnection*>(pci->internal());
1851 return pc->GetRemoteAudioSSLCertificate();
1852 };
Zhi Huang70b820f2018-01-27 14:16:15 -08001853 auto GetRemoteAudioSSLCertChain = [](PeerConnectionWrapper* wrapper) {
1854 auto pci = reinterpret_cast<PeerConnectionProxy*>(wrapper->pc());
1855 auto pc = reinterpret_cast<PeerConnection*>(pci->internal());
1856 return pc->GetRemoteAudioSSLCertChain();
1857 };
Steve Anton8c0f7a72017-10-03 10:03:10 -07001858
1859 auto caller_cert = rtc::RTCCertificate::FromPEM(kRsaPems[0]);
1860 auto callee_cert = rtc::RTCCertificate::FromPEM(kRsaPems[1]);
1861
1862 // Configure each side with a known certificate so they can be compared later.
1863 PeerConnectionInterface::RTCConfiguration caller_config;
1864 caller_config.enable_dtls_srtp.emplace(true);
1865 caller_config.certificates.push_back(caller_cert);
1866 PeerConnectionInterface::RTCConfiguration callee_config;
1867 callee_config.enable_dtls_srtp.emplace(true);
1868 callee_config.certificates.push_back(callee_cert);
1869 ASSERT_TRUE(
1870 CreatePeerConnectionWrappersWithConfig(caller_config, callee_config));
1871 ConnectFakeSignaling();
1872
1873 // When first initialized, there should not be a remote SSL certificate (and
1874 // calling this method should not crash).
1875 EXPECT_EQ(nullptr, GetRemoteAudioSSLCertificate(caller()));
1876 EXPECT_EQ(nullptr, GetRemoteAudioSSLCertificate(callee()));
Zhi Huang70b820f2018-01-27 14:16:15 -08001877 EXPECT_EQ(nullptr, GetRemoteAudioSSLCertChain(caller()));
1878 EXPECT_EQ(nullptr, GetRemoteAudioSSLCertChain(callee()));
Steve Anton8c0f7a72017-10-03 10:03:10 -07001879
Steve Anton15324772018-01-16 10:26:49 -08001880 caller()->AddAudioTrack();
1881 callee()->AddAudioTrack();
Steve Anton8c0f7a72017-10-03 10:03:10 -07001882 caller()->CreateAndSetAndSignalOffer();
1883 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1884 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
1885
1886 // Once DTLS has been connected, each side should return the other's SSL
1887 // certificate when calling GetRemoteAudioSSLCertificate.
1888
1889 auto caller_remote_cert = GetRemoteAudioSSLCertificate(caller());
1890 ASSERT_TRUE(caller_remote_cert);
Benjamin Wright6c6c9df2018-10-25 01:16:26 -07001891 EXPECT_EQ(callee_cert->GetSSLCertificate().ToPEMString(),
Steve Anton8c0f7a72017-10-03 10:03:10 -07001892 caller_remote_cert->ToPEMString());
1893
1894 auto callee_remote_cert = GetRemoteAudioSSLCertificate(callee());
1895 ASSERT_TRUE(callee_remote_cert);
Benjamin Wright6c6c9df2018-10-25 01:16:26 -07001896 EXPECT_EQ(caller_cert->GetSSLCertificate().ToPEMString(),
Steve Anton8c0f7a72017-10-03 10:03:10 -07001897 callee_remote_cert->ToPEMString());
Zhi Huang70b820f2018-01-27 14:16:15 -08001898
1899 auto caller_remote_cert_chain = GetRemoteAudioSSLCertChain(caller());
1900 ASSERT_TRUE(caller_remote_cert_chain);
1901 ASSERT_EQ(1U, caller_remote_cert_chain->GetSize());
1902 auto remote_cert = &caller_remote_cert_chain->Get(0);
Benjamin Wright6c6c9df2018-10-25 01:16:26 -07001903 EXPECT_EQ(callee_cert->GetSSLCertificate().ToPEMString(),
Zhi Huang70b820f2018-01-27 14:16:15 -08001904 remote_cert->ToPEMString());
1905
1906 auto callee_remote_cert_chain = GetRemoteAudioSSLCertChain(callee());
1907 ASSERT_TRUE(callee_remote_cert_chain);
1908 ASSERT_EQ(1U, callee_remote_cert_chain->GetSize());
1909 remote_cert = &callee_remote_cert_chain->Get(0);
Benjamin Wright6c6c9df2018-10-25 01:16:26 -07001910 EXPECT_EQ(caller_cert->GetSSLCertificate().ToPEMString(),
Zhi Huang70b820f2018-01-27 14:16:15 -08001911 remote_cert->ToPEMString());
Steve Anton8c0f7a72017-10-03 10:03:10 -07001912}
1913
deadbeef1dcb1642017-03-29 21:08:16 -07001914// This test sets up a call between two parties with a source resolution of
1915// 1280x720 and verifies that a 16:9 aspect ratio is received.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001916TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07001917 Send1280By720ResolutionAndReceive16To9AspectRatio) {
1918 ASSERT_TRUE(CreatePeerConnectionWrappers());
1919 ConnectFakeSignaling();
1920
Niels Möller5c7efe72018-05-11 10:34:46 +02001921 // Add video tracks with 16:9 aspect ratio, size 1280 x 720.
1922 webrtc::FakePeriodicVideoSource::Config config;
1923 config.width = 1280;
1924 config.height = 720;
Johannes Kron965e7942018-09-13 15:36:20 +02001925 config.timestamp_offset_ms = rtc::TimeMillis();
Niels Möller5c7efe72018-05-11 10:34:46 +02001926 caller()->AddTrack(caller()->CreateLocalVideoTrackWithConfig(config));
1927 callee()->AddTrack(callee()->CreateLocalVideoTrackWithConfig(config));
deadbeef1dcb1642017-03-29 21:08:16 -07001928
1929 // Do normal offer/answer and wait for at least one frame to be received in
1930 // each direction.
1931 caller()->CreateAndSetAndSignalOffer();
1932 ASSERT_TRUE_WAIT(caller()->min_video_frames_received_per_track() > 0 &&
1933 callee()->min_video_frames_received_per_track() > 0,
1934 kMaxWaitForFramesMs);
1935
1936 // Check rendered aspect ratio.
1937 EXPECT_EQ(16.0 / 9, caller()->local_rendered_aspect_ratio());
1938 EXPECT_EQ(16.0 / 9, caller()->rendered_aspect_ratio());
1939 EXPECT_EQ(16.0 / 9, callee()->local_rendered_aspect_ratio());
1940 EXPECT_EQ(16.0 / 9, callee()->rendered_aspect_ratio());
1941}
1942
1943// This test sets up an one-way call, with media only from caller to
1944// callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001945TEST_P(PeerConnectionIntegrationTest, OneWayMediaCall) {
deadbeef1dcb1642017-03-29 21:08:16 -07001946 ASSERT_TRUE(CreatePeerConnectionWrappers());
1947 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001948 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001949 caller()->CreateAndSetAndSignalOffer();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001950 MediaExpectations media_expectations;
1951 media_expectations.CalleeExpectsSomeAudioAndVideo();
1952 media_expectations.CallerExpectsNoAudio();
1953 media_expectations.CallerExpectsNoVideo();
1954 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07001955}
1956
1957// This test sets up a audio call initially, with the callee rejecting video
1958// initially. Then later the callee decides to upgrade to audio/video, and
1959// initiates a new offer/answer exchange.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001960TEST_P(PeerConnectionIntegrationTest, AudioToVideoUpgrade) {
deadbeef1dcb1642017-03-29 21:08:16 -07001961 ASSERT_TRUE(CreatePeerConnectionWrappers());
1962 ConnectFakeSignaling();
1963 // Initially, offer an audio/video stream from the caller, but refuse to
1964 // send/receive video on the callee side.
Steve Anton15324772018-01-16 10:26:49 -08001965 caller()->AddAudioVideoTracks();
1966 callee()->AddAudioTrack();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001967 if (sdp_semantics_ == SdpSemantics::kPlanB) {
1968 PeerConnectionInterface::RTCOfferAnswerOptions options;
1969 options.offer_to_receive_video = 0;
1970 callee()->SetOfferAnswerOptions(options);
1971 } else {
1972 callee()->SetRemoteOfferHandler([this] {
1973 callee()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)->Stop();
1974 });
1975 }
deadbeef1dcb1642017-03-29 21:08:16 -07001976 // Do offer/answer and make sure audio is still received end-to-end.
1977 caller()->CreateAndSetAndSignalOffer();
1978 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001979 {
1980 MediaExpectations media_expectations;
1981 media_expectations.ExpectBidirectionalAudio();
1982 media_expectations.ExpectNoVideo();
1983 ASSERT_TRUE(ExpectNewFrames(media_expectations));
1984 }
deadbeef1dcb1642017-03-29 21:08:16 -07001985 // Sanity check that the callee's description has a rejected video section.
1986 ASSERT_NE(nullptr, callee()->pc()->local_description());
1987 const ContentInfo* callee_video_content =
1988 GetFirstVideoContent(callee()->pc()->local_description()->description());
1989 ASSERT_NE(nullptr, callee_video_content);
1990 EXPECT_TRUE(callee_video_content->rejected);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001991
deadbeef1dcb1642017-03-29 21:08:16 -07001992 // Now negotiate with video and ensure negotiation succeeds, with video
1993 // frames and additional audio frames being received.
Steve Anton15324772018-01-16 10:26:49 -08001994 callee()->AddVideoTrack();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001995 if (sdp_semantics_ == SdpSemantics::kPlanB) {
1996 PeerConnectionInterface::RTCOfferAnswerOptions options;
1997 options.offer_to_receive_video = 1;
1998 callee()->SetOfferAnswerOptions(options);
1999 } else {
2000 callee()->SetRemoteOfferHandler(nullptr);
2001 caller()->SetRemoteOfferHandler([this] {
2002 // The caller creates a new transceiver to receive video on when receiving
2003 // the offer, but by default it is send only.
2004 auto transceivers = caller()->pc()->GetTransceivers();
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02002005 ASSERT_EQ(3U, transceivers.size());
Seth Hampson2f0d7022018-02-20 11:54:42 -08002006 ASSERT_EQ(cricket::MEDIA_TYPE_VIDEO,
2007 transceivers[2]->receiver()->media_type());
2008 transceivers[2]->sender()->SetTrack(caller()->CreateLocalVideoTrack());
2009 transceivers[2]->SetDirection(RtpTransceiverDirection::kSendRecv);
2010 });
2011 }
deadbeef1dcb1642017-03-29 21:08:16 -07002012 callee()->CreateAndSetAndSignalOffer();
2013 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002014 {
2015 // Expect additional audio frames to be received after the upgrade.
2016 MediaExpectations media_expectations;
2017 media_expectations.ExpectBidirectionalAudioAndVideo();
2018 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2019 }
deadbeef1dcb1642017-03-29 21:08:16 -07002020}
2021
deadbeef4389b4d2017-09-07 09:07:36 -07002022// Simpler than the above test; just add an audio track to an established
2023// video-only connection.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002024TEST_P(PeerConnectionIntegrationTest, AddAudioToVideoOnlyCall) {
deadbeef4389b4d2017-09-07 09:07:36 -07002025 ASSERT_TRUE(CreatePeerConnectionWrappers());
2026 ConnectFakeSignaling();
2027 // Do initial offer/answer with just a video track.
Steve Anton15324772018-01-16 10:26:49 -08002028 caller()->AddVideoTrack();
2029 callee()->AddVideoTrack();
deadbeef4389b4d2017-09-07 09:07:36 -07002030 caller()->CreateAndSetAndSignalOffer();
2031 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2032 // Now add an audio track and do another offer/answer.
Steve Anton15324772018-01-16 10:26:49 -08002033 caller()->AddAudioTrack();
2034 callee()->AddAudioTrack();
deadbeef4389b4d2017-09-07 09:07:36 -07002035 caller()->CreateAndSetAndSignalOffer();
2036 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2037 // Ensure both audio and video frames are received end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002038 MediaExpectations media_expectations;
2039 media_expectations.ExpectBidirectionalAudioAndVideo();
2040 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef4389b4d2017-09-07 09:07:36 -07002041}
2042
deadbeef1dcb1642017-03-29 21:08:16 -07002043// This test sets up a call that's transferred to a new caller with a different
2044// DTLS fingerprint.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002045TEST_P(PeerConnectionIntegrationTest, CallTransferredForCallee) {
deadbeef1dcb1642017-03-29 21:08:16 -07002046 ASSERT_TRUE(CreatePeerConnectionWrappers());
2047 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002048 caller()->AddAudioVideoTracks();
2049 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002050 caller()->CreateAndSetAndSignalOffer();
2051 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2052
2053 // Keep the original peer around which will still send packets to the
2054 // receiving client. These SRTP packets will be dropped.
2055 std::unique_ptr<PeerConnectionWrapper> original_peer(
2056 SetCallerPcWrapperAndReturnCurrent(
Seth Hampson2f0d7022018-02-20 11:54:42 -08002057 CreatePeerConnectionWrapperWithAlternateKey().release()));
deadbeef1dcb1642017-03-29 21:08:16 -07002058 // TODO(deadbeef): Why do we call Close here? That goes against the comment
2059 // directly above.
2060 original_peer->pc()->Close();
2061
2062 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002063 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002064 caller()->CreateAndSetAndSignalOffer();
2065 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2066 // Wait for some additional frames to be transmitted end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002067 MediaExpectations media_expectations;
2068 media_expectations.ExpectBidirectionalAudioAndVideo();
2069 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002070}
2071
2072// This test sets up a call that's transferred to a new callee with a different
2073// DTLS fingerprint.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002074TEST_P(PeerConnectionIntegrationTest, CallTransferredForCaller) {
deadbeef1dcb1642017-03-29 21:08:16 -07002075 ASSERT_TRUE(CreatePeerConnectionWrappers());
2076 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002077 caller()->AddAudioVideoTracks();
2078 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002079 caller()->CreateAndSetAndSignalOffer();
2080 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2081
2082 // Keep the original peer around which will still send packets to the
2083 // receiving client. These SRTP packets will be dropped.
2084 std::unique_ptr<PeerConnectionWrapper> original_peer(
2085 SetCalleePcWrapperAndReturnCurrent(
Seth Hampson2f0d7022018-02-20 11:54:42 -08002086 CreatePeerConnectionWrapperWithAlternateKey().release()));
deadbeef1dcb1642017-03-29 21:08:16 -07002087 // TODO(deadbeef): Why do we call Close here? That goes against the comment
2088 // directly above.
2089 original_peer->pc()->Close();
2090
2091 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002092 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002093 caller()->SetOfferAnswerOptions(IceRestartOfferAnswerOptions());
2094 caller()->CreateAndSetAndSignalOffer();
2095 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2096 // Wait for some additional frames to be transmitted end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002097 MediaExpectations media_expectations;
2098 media_expectations.ExpectBidirectionalAudioAndVideo();
2099 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002100}
2101
2102// This test sets up a non-bundled call and negotiates bundling at the same
2103// time as starting an ICE restart. When bundling is in effect in the restart,
2104// the DTLS-SRTP context should be successfully reset.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002105TEST_P(PeerConnectionIntegrationTest, BundlingEnabledWhileIceRestartOccurs) {
deadbeef1dcb1642017-03-29 21:08:16 -07002106 ASSERT_TRUE(CreatePeerConnectionWrappers());
2107 ConnectFakeSignaling();
2108
Steve Anton15324772018-01-16 10:26:49 -08002109 caller()->AddAudioVideoTracks();
2110 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002111 // Remove the bundle group from the SDP received by the callee.
2112 callee()->SetReceivedSdpMunger([](cricket::SessionDescription* desc) {
2113 desc->RemoveGroupByName("BUNDLE");
2114 });
2115 caller()->CreateAndSetAndSignalOffer();
2116 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002117 {
2118 MediaExpectations media_expectations;
2119 media_expectations.ExpectBidirectionalAudioAndVideo();
2120 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2121 }
deadbeef1dcb1642017-03-29 21:08:16 -07002122 // Now stop removing the BUNDLE group, and trigger an ICE restart.
2123 callee()->SetReceivedSdpMunger(nullptr);
2124 caller()->SetOfferAnswerOptions(IceRestartOfferAnswerOptions());
2125 caller()->CreateAndSetAndSignalOffer();
2126 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2127
2128 // Expect additional frames to be received after the ICE restart.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002129 {
2130 MediaExpectations media_expectations;
2131 media_expectations.ExpectBidirectionalAudioAndVideo();
2132 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2133 }
deadbeef1dcb1642017-03-29 21:08:16 -07002134}
2135
2136// Test CVO (Coordination of Video Orientation). If a video source is rotated
2137// and both peers support the CVO RTP header extension, the actual video frames
2138// don't need to be encoded in different resolutions, since the rotation is
2139// communicated through the RTP header extension.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002140TEST_P(PeerConnectionIntegrationTest, RotatedVideoWithCVOExtension) {
deadbeef1dcb1642017-03-29 21:08:16 -07002141 ASSERT_TRUE(CreatePeerConnectionWrappers());
2142 ConnectFakeSignaling();
2143 // Add rotated video tracks.
Steve Anton15324772018-01-16 10:26:49 -08002144 caller()->AddTrack(
deadbeef1dcb1642017-03-29 21:08:16 -07002145 caller()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_90));
Steve Anton15324772018-01-16 10:26:49 -08002146 callee()->AddTrack(
deadbeef1dcb1642017-03-29 21:08:16 -07002147 callee()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_270));
2148
2149 // Wait for video frames to be received by both sides.
2150 caller()->CreateAndSetAndSignalOffer();
2151 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2152 ASSERT_TRUE_WAIT(caller()->min_video_frames_received_per_track() > 0 &&
2153 callee()->min_video_frames_received_per_track() > 0,
2154 kMaxWaitForFramesMs);
2155
2156 // Ensure that the aspect ratio is unmodified.
2157 // TODO(deadbeef): Where does 4:3 come from? Should be explicit in the test,
2158 // not just assumed.
2159 EXPECT_EQ(4.0 / 3, caller()->local_rendered_aspect_ratio());
2160 EXPECT_EQ(4.0 / 3, caller()->rendered_aspect_ratio());
2161 EXPECT_EQ(4.0 / 3, callee()->local_rendered_aspect_ratio());
2162 EXPECT_EQ(4.0 / 3, callee()->rendered_aspect_ratio());
2163 // Ensure that the CVO bits were surfaced to the renderer.
2164 EXPECT_EQ(webrtc::kVideoRotation_270, caller()->rendered_rotation());
2165 EXPECT_EQ(webrtc::kVideoRotation_90, callee()->rendered_rotation());
2166}
2167
2168// Test that when the CVO extension isn't supported, video is rotated the
2169// old-fashioned way, by encoding rotated frames.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002170TEST_P(PeerConnectionIntegrationTest, RotatedVideoWithoutCVOExtension) {
deadbeef1dcb1642017-03-29 21:08:16 -07002171 ASSERT_TRUE(CreatePeerConnectionWrappers());
2172 ConnectFakeSignaling();
2173 // Add rotated video tracks.
Steve Anton15324772018-01-16 10:26:49 -08002174 caller()->AddTrack(
deadbeef1dcb1642017-03-29 21:08:16 -07002175 caller()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_90));
Steve Anton15324772018-01-16 10:26:49 -08002176 callee()->AddTrack(
deadbeef1dcb1642017-03-29 21:08:16 -07002177 callee()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_270));
2178
2179 // Remove the CVO extension from the offered SDP.
2180 callee()->SetReceivedSdpMunger([](cricket::SessionDescription* desc) {
2181 cricket::VideoContentDescription* video =
2182 GetFirstVideoContentDescription(desc);
2183 video->ClearRtpHeaderExtensions();
2184 });
2185 // Wait for video frames to be received by both sides.
2186 caller()->CreateAndSetAndSignalOffer();
2187 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2188 ASSERT_TRUE_WAIT(caller()->min_video_frames_received_per_track() > 0 &&
2189 callee()->min_video_frames_received_per_track() > 0,
2190 kMaxWaitForFramesMs);
2191
2192 // Expect that the aspect ratio is inversed to account for the 90/270 degree
2193 // rotation.
2194 // TODO(deadbeef): Where does 4:3 come from? Should be explicit in the test,
2195 // not just assumed.
2196 EXPECT_EQ(3.0 / 4, caller()->local_rendered_aspect_ratio());
2197 EXPECT_EQ(3.0 / 4, caller()->rendered_aspect_ratio());
2198 EXPECT_EQ(3.0 / 4, callee()->local_rendered_aspect_ratio());
2199 EXPECT_EQ(3.0 / 4, callee()->rendered_aspect_ratio());
2200 // Expect that each endpoint is unaware of the rotation of the other endpoint.
2201 EXPECT_EQ(webrtc::kVideoRotation_0, caller()->rendered_rotation());
2202 EXPECT_EQ(webrtc::kVideoRotation_0, callee()->rendered_rotation());
2203}
2204
deadbeef1dcb1642017-03-29 21:08:16 -07002205// Test that if the answerer rejects the audio m= section, no audio is sent or
2206// received, but video still can be.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002207TEST_P(PeerConnectionIntegrationTest, AnswererRejectsAudioSection) {
deadbeef1dcb1642017-03-29 21:08:16 -07002208 ASSERT_TRUE(CreatePeerConnectionWrappers());
2209 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002210 caller()->AddAudioVideoTracks();
Seth Hampson2f0d7022018-02-20 11:54:42 -08002211 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2212 // Only add video track for callee, and set offer_to_receive_audio to 0, so
2213 // it will reject the audio m= section completely.
2214 PeerConnectionInterface::RTCOfferAnswerOptions options;
2215 options.offer_to_receive_audio = 0;
2216 callee()->SetOfferAnswerOptions(options);
2217 } else {
2218 // Stopping the audio RtpTransceiver will cause the media section to be
2219 // rejected in the answer.
2220 callee()->SetRemoteOfferHandler([this] {
2221 callee()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_AUDIO)->Stop();
2222 });
2223 }
Steve Anton15324772018-01-16 10:26:49 -08002224 callee()->AddTrack(callee()->CreateLocalVideoTrack());
deadbeef1dcb1642017-03-29 21:08:16 -07002225 // Do offer/answer and wait for successful end-to-end video frames.
2226 caller()->CreateAndSetAndSignalOffer();
2227 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002228 MediaExpectations media_expectations;
2229 media_expectations.ExpectBidirectionalVideo();
2230 media_expectations.ExpectNoAudio();
2231 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2232
deadbeef1dcb1642017-03-29 21:08:16 -07002233 // Sanity check that the callee's description has a rejected audio section.
2234 ASSERT_NE(nullptr, callee()->pc()->local_description());
2235 const ContentInfo* callee_audio_content =
2236 GetFirstAudioContent(callee()->pc()->local_description()->description());
2237 ASSERT_NE(nullptr, callee_audio_content);
2238 EXPECT_TRUE(callee_audio_content->rejected);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002239 if (sdp_semantics_ == SdpSemantics::kUnifiedPlan) {
2240 // The caller's transceiver should have stopped after receiving the answer.
2241 EXPECT_TRUE(caller()
2242 ->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_AUDIO)
2243 ->stopped());
2244 }
deadbeef1dcb1642017-03-29 21:08:16 -07002245}
2246
2247// Test that if the answerer rejects the video m= section, no video is sent or
2248// received, but audio still can be.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002249TEST_P(PeerConnectionIntegrationTest, AnswererRejectsVideoSection) {
deadbeef1dcb1642017-03-29 21:08:16 -07002250 ASSERT_TRUE(CreatePeerConnectionWrappers());
2251 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002252 caller()->AddAudioVideoTracks();
Seth Hampson2f0d7022018-02-20 11:54:42 -08002253 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2254 // Only add audio track for callee, and set offer_to_receive_video to 0, so
2255 // it will reject the video m= section completely.
2256 PeerConnectionInterface::RTCOfferAnswerOptions options;
2257 options.offer_to_receive_video = 0;
2258 callee()->SetOfferAnswerOptions(options);
2259 } else {
2260 // Stopping the video RtpTransceiver will cause the media section to be
2261 // rejected in the answer.
2262 callee()->SetRemoteOfferHandler([this] {
2263 callee()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)->Stop();
2264 });
2265 }
Steve Anton15324772018-01-16 10:26:49 -08002266 callee()->AddTrack(callee()->CreateLocalAudioTrack());
deadbeef1dcb1642017-03-29 21:08:16 -07002267 // Do offer/answer and wait for successful end-to-end audio frames.
2268 caller()->CreateAndSetAndSignalOffer();
2269 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002270 MediaExpectations media_expectations;
2271 media_expectations.ExpectBidirectionalAudio();
2272 media_expectations.ExpectNoVideo();
2273 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2274
deadbeef1dcb1642017-03-29 21:08:16 -07002275 // Sanity check that the callee's description has a rejected video section.
2276 ASSERT_NE(nullptr, callee()->pc()->local_description());
2277 const ContentInfo* callee_video_content =
2278 GetFirstVideoContent(callee()->pc()->local_description()->description());
2279 ASSERT_NE(nullptr, callee_video_content);
2280 EXPECT_TRUE(callee_video_content->rejected);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002281 if (sdp_semantics_ == SdpSemantics::kUnifiedPlan) {
2282 // The caller's transceiver should have stopped after receiving the answer.
2283 EXPECT_TRUE(caller()
2284 ->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)
2285 ->stopped());
2286 }
deadbeef1dcb1642017-03-29 21:08:16 -07002287}
2288
2289// Test that if the answerer rejects both audio and video m= sections, nothing
2290// bad happens.
2291// TODO(deadbeef): Test that a data channel still works. Currently this doesn't
2292// test anything but the fact that negotiation succeeds, which doesn't mean
2293// much.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002294TEST_P(PeerConnectionIntegrationTest, AnswererRejectsAudioAndVideoSections) {
deadbeef1dcb1642017-03-29 21:08:16 -07002295 ASSERT_TRUE(CreatePeerConnectionWrappers());
2296 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002297 caller()->AddAudioVideoTracks();
Seth Hampson2f0d7022018-02-20 11:54:42 -08002298 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2299 // Don't give the callee any tracks, and set offer_to_receive_X to 0, so it
2300 // will reject both audio and video m= sections.
2301 PeerConnectionInterface::RTCOfferAnswerOptions options;
2302 options.offer_to_receive_audio = 0;
2303 options.offer_to_receive_video = 0;
2304 callee()->SetOfferAnswerOptions(options);
2305 } else {
2306 callee()->SetRemoteOfferHandler([this] {
2307 // Stopping all transceivers will cause all media sections to be rejected.
Mirko Bonadei739baf02019-01-27 17:29:42 +01002308 for (const auto& transceiver : callee()->pc()->GetTransceivers()) {
Seth Hampson2f0d7022018-02-20 11:54:42 -08002309 transceiver->Stop();
2310 }
2311 });
2312 }
deadbeef1dcb1642017-03-29 21:08:16 -07002313 // Do offer/answer and wait for stable signaling state.
2314 caller()->CreateAndSetAndSignalOffer();
2315 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002316
deadbeef1dcb1642017-03-29 21:08:16 -07002317 // Sanity check that the callee's description has rejected m= sections.
2318 ASSERT_NE(nullptr, callee()->pc()->local_description());
2319 const ContentInfo* callee_audio_content =
2320 GetFirstAudioContent(callee()->pc()->local_description()->description());
2321 ASSERT_NE(nullptr, callee_audio_content);
2322 EXPECT_TRUE(callee_audio_content->rejected);
2323 const ContentInfo* callee_video_content =
2324 GetFirstVideoContent(callee()->pc()->local_description()->description());
2325 ASSERT_NE(nullptr, callee_video_content);
2326 EXPECT_TRUE(callee_video_content->rejected);
2327}
2328
2329// This test sets up an audio and video call between two parties. After the
2330// call runs for a while, the caller sends an updated offer with video being
2331// rejected. Once the re-negotiation is done, the video flow should stop and
2332// the audio flow should continue.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002333TEST_P(PeerConnectionIntegrationTest, VideoRejectedInSubsequentOffer) {
deadbeef1dcb1642017-03-29 21:08:16 -07002334 ASSERT_TRUE(CreatePeerConnectionWrappers());
2335 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002336 caller()->AddAudioVideoTracks();
2337 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002338 caller()->CreateAndSetAndSignalOffer();
2339 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002340 {
2341 MediaExpectations media_expectations;
2342 media_expectations.ExpectBidirectionalAudioAndVideo();
2343 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2344 }
deadbeef1dcb1642017-03-29 21:08:16 -07002345 // Renegotiate, rejecting the video m= section.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002346 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2347 caller()->SetGeneratedSdpMunger(
2348 [](cricket::SessionDescription* description) {
2349 for (cricket::ContentInfo& content : description->contents()) {
2350 if (cricket::IsVideoContent(&content)) {
2351 content.rejected = true;
2352 }
2353 }
2354 });
2355 } else {
2356 caller()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)->Stop();
2357 }
deadbeef1dcb1642017-03-29 21:08:16 -07002358 caller()->CreateAndSetAndSignalOffer();
2359 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
2360
2361 // Sanity check that the caller's description has a rejected video section.
2362 ASSERT_NE(nullptr, caller()->pc()->local_description());
2363 const ContentInfo* caller_video_content =
2364 GetFirstVideoContent(caller()->pc()->local_description()->description());
2365 ASSERT_NE(nullptr, caller_video_content);
2366 EXPECT_TRUE(caller_video_content->rejected);
deadbeef1dcb1642017-03-29 21:08:16 -07002367 // Wait for some additional audio frames to be received.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002368 {
2369 MediaExpectations media_expectations;
2370 media_expectations.ExpectBidirectionalAudio();
2371 media_expectations.ExpectNoVideo();
2372 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2373 }
deadbeef1dcb1642017-03-29 21:08:16 -07002374}
2375
Taylor Brandstetter60c8dc82018-04-11 15:20:27 -07002376// Do one offer/answer with audio, another that disables it (rejecting the m=
2377// section), and another that re-enables it. Regression test for:
2378// bugs.webrtc.org/6023
2379TEST_F(PeerConnectionIntegrationTestPlanB, EnableAudioAfterRejecting) {
2380 ASSERT_TRUE(CreatePeerConnectionWrappers());
2381 ConnectFakeSignaling();
2382
2383 // Add audio track, do normal offer/answer.
2384 rtc::scoped_refptr<webrtc::AudioTrackInterface> track =
2385 caller()->CreateLocalAudioTrack();
2386 rtc::scoped_refptr<webrtc::RtpSenderInterface> sender =
2387 caller()->pc()->AddTrack(track, {"stream"}).MoveValue();
2388 caller()->CreateAndSetAndSignalOffer();
2389 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2390
2391 // Remove audio track, and set offer_to_receive_audio to false to cause the
2392 // m= section to be completely disabled, not just "recvonly".
2393 caller()->pc()->RemoveTrack(sender);
2394 PeerConnectionInterface::RTCOfferAnswerOptions options;
2395 options.offer_to_receive_audio = 0;
2396 caller()->SetOfferAnswerOptions(options);
2397 caller()->CreateAndSetAndSignalOffer();
2398 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2399
2400 // Add the audio track again, expecting negotiation to succeed and frames to
2401 // flow.
2402 sender = caller()->pc()->AddTrack(track, {"stream"}).MoveValue();
2403 options.offer_to_receive_audio = 1;
2404 caller()->SetOfferAnswerOptions(options);
2405 caller()->CreateAndSetAndSignalOffer();
2406 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2407
2408 MediaExpectations media_expectations;
2409 media_expectations.CalleeExpectsSomeAudio();
2410 EXPECT_TRUE(ExpectNewFrames(media_expectations));
2411}
2412
deadbeef1dcb1642017-03-29 21:08:16 -07002413// Basic end-to-end test, but without SSRC/MSID signaling. This functionality
2414// is needed to support legacy endpoints.
2415// TODO(deadbeef): When we support the MID extension and demuxing on MID, also
2416// add a test for an end-to-end test without MID signaling either (basically,
2417// the minimum acceptable SDP).
Seth Hampson2f0d7022018-02-20 11:54:42 -08002418TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithoutSsrcOrMsidSignaling) {
deadbeef1dcb1642017-03-29 21:08:16 -07002419 ASSERT_TRUE(CreatePeerConnectionWrappers());
2420 ConnectFakeSignaling();
2421 // Add audio and video, testing that packets can be demuxed on payload type.
Steve Anton15324772018-01-16 10:26:49 -08002422 caller()->AddAudioVideoTracks();
2423 callee()->AddAudioVideoTracks();
deadbeefd8ad7882017-04-18 16:01:17 -07002424 // Remove SSRCs and MSIDs from the received offer SDP.
2425 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
deadbeef1dcb1642017-03-29 21:08:16 -07002426 caller()->CreateAndSetAndSignalOffer();
2427 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002428 MediaExpectations media_expectations;
2429 media_expectations.ExpectBidirectionalAudioAndVideo();
2430 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002431}
2432
Seth Hampson5897a6e2018-04-03 11:16:33 -07002433// Basic end-to-end test, without SSRC signaling. This means that the track
2434// was created properly and frames are delivered when the MSIDs are communicated
2435// with a=msid lines and no a=ssrc lines.
2436TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
2437 EndToEndCallWithoutSsrcSignaling) {
2438 const char kStreamId[] = "streamId";
2439 ASSERT_TRUE(CreatePeerConnectionWrappers());
2440 ConnectFakeSignaling();
2441 // Add just audio tracks.
2442 caller()->AddTrack(caller()->CreateLocalAudioTrack(), {kStreamId});
2443 callee()->AddAudioTrack();
2444
2445 // Remove SSRCs from the received offer SDP.
2446 callee()->SetReceivedSdpMunger(RemoveSsrcsAndKeepMsids);
2447 caller()->CreateAndSetAndSignalOffer();
2448 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2449 MediaExpectations media_expectations;
2450 media_expectations.ExpectBidirectionalAudio();
2451 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2452}
2453
Steve Antondf527fd2018-04-27 15:52:03 -07002454// Tests that video flows between multiple video tracks when SSRCs are not
2455// signaled. This exercises the MID RTP header extension which is needed to
2456// demux the incoming video tracks.
2457TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
2458 EndToEndCallWithTwoVideoTracksAndNoSignaledSsrc) {
2459 ASSERT_TRUE(CreatePeerConnectionWrappers());
2460 ConnectFakeSignaling();
2461 caller()->AddVideoTrack();
2462 caller()->AddVideoTrack();
2463 callee()->AddVideoTrack();
2464 callee()->AddVideoTrack();
2465
2466 caller()->SetReceivedSdpMunger(&RemoveSsrcsAndKeepMsids);
2467 callee()->SetReceivedSdpMunger(&RemoveSsrcsAndKeepMsids);
2468 caller()->CreateAndSetAndSignalOffer();
2469 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2470 ASSERT_EQ(2u, caller()->pc()->GetReceivers().size());
2471 ASSERT_EQ(2u, callee()->pc()->GetReceivers().size());
2472
2473 // Expect video to be received in both directions on both tracks.
2474 MediaExpectations media_expectations;
2475 media_expectations.ExpectBidirectionalVideo();
2476 EXPECT_TRUE(ExpectNewFrames(media_expectations));
2477}
2478
Henrik Boström5b147782018-12-04 11:25:05 +01002479TEST_F(PeerConnectionIntegrationTestUnifiedPlan, NoStreamsMsidLinePresent) {
2480 ASSERT_TRUE(CreatePeerConnectionWrappers());
2481 ConnectFakeSignaling();
2482 caller()->AddAudioTrack();
2483 caller()->AddVideoTrack();
2484 caller()->CreateAndSetAndSignalOffer();
2485 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2486 auto callee_receivers = callee()->pc()->GetReceivers();
2487 ASSERT_EQ(2u, callee_receivers.size());
2488 EXPECT_TRUE(callee_receivers[0]->stream_ids().empty());
2489 EXPECT_TRUE(callee_receivers[1]->stream_ids().empty());
2490}
2491
2492TEST_F(PeerConnectionIntegrationTestUnifiedPlan, NoStreamsMsidLineMissing) {
2493 ASSERT_TRUE(CreatePeerConnectionWrappers());
2494 ConnectFakeSignaling();
2495 caller()->AddAudioTrack();
2496 caller()->AddVideoTrack();
2497 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
2498 caller()->CreateAndSetAndSignalOffer();
2499 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2500 auto callee_receivers = callee()->pc()->GetReceivers();
2501 ASSERT_EQ(2u, callee_receivers.size());
2502 ASSERT_EQ(1u, callee_receivers[0]->stream_ids().size());
2503 ASSERT_EQ(1u, callee_receivers[1]->stream_ids().size());
2504 EXPECT_EQ(callee_receivers[0]->stream_ids()[0],
2505 callee_receivers[1]->stream_ids()[0]);
2506 EXPECT_EQ(callee_receivers[0]->streams()[0],
2507 callee_receivers[1]->streams()[0]);
2508}
2509
deadbeef1dcb1642017-03-29 21:08:16 -07002510// Test that if two video tracks are sent (from caller to callee, in this test),
2511// they're transmitted correctly end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002512TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithTwoVideoTracks) {
deadbeef1dcb1642017-03-29 21:08:16 -07002513 ASSERT_TRUE(CreatePeerConnectionWrappers());
2514 ConnectFakeSignaling();
2515 // Add one audio/video stream, and one video-only stream.
Steve Anton15324772018-01-16 10:26:49 -08002516 caller()->AddAudioVideoTracks();
2517 caller()->AddVideoTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07002518 caller()->CreateAndSetAndSignalOffer();
2519 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Steve Anton15324772018-01-16 10:26:49 -08002520 ASSERT_EQ(3u, callee()->pc()->GetReceivers().size());
Seth Hampson2f0d7022018-02-20 11:54:42 -08002521
2522 MediaExpectations media_expectations;
2523 media_expectations.CalleeExpectsSomeAudioAndVideo();
2524 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002525}
2526
2527static void MakeSpecCompliantMaxBundleOffer(cricket::SessionDescription* desc) {
2528 bool first = true;
2529 for (cricket::ContentInfo& content : desc->contents()) {
2530 if (first) {
2531 first = false;
2532 continue;
2533 }
2534 content.bundle_only = true;
2535 }
2536 first = true;
2537 for (cricket::TransportInfo& transport : desc->transport_infos()) {
2538 if (first) {
2539 first = false;
2540 continue;
2541 }
2542 transport.description.ice_ufrag.clear();
2543 transport.description.ice_pwd.clear();
2544 transport.description.connection_role = cricket::CONNECTIONROLE_NONE;
2545 transport.description.identity_fingerprint.reset(nullptr);
2546 }
2547}
2548
2549// Test that if applying a true "max bundle" offer, which uses ports of 0,
2550// "a=bundle-only", omitting "a=fingerprint", "a=setup", "a=ice-ufrag" and
2551// "a=ice-pwd" for all but the audio "m=" section, negotiation still completes
2552// successfully and media flows.
2553// TODO(deadbeef): Update this test to also omit "a=rtcp-mux", once that works.
2554// TODO(deadbeef): Won't need this test once we start generating actual
2555// standards-compliant SDP.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002556TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07002557 EndToEndCallWithSpecCompliantMaxBundleOffer) {
2558 ASSERT_TRUE(CreatePeerConnectionWrappers());
2559 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002560 caller()->AddAudioVideoTracks();
2561 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002562 // Do the equivalent of setting the port to 0, adding a=bundle-only, and
2563 // removing a=ice-ufrag, a=ice-pwd, a=fingerprint and a=setup from all
2564 // but the first m= section.
2565 callee()->SetReceivedSdpMunger(MakeSpecCompliantMaxBundleOffer);
2566 caller()->CreateAndSetAndSignalOffer();
2567 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002568 MediaExpectations media_expectations;
2569 media_expectations.ExpectBidirectionalAudioAndVideo();
2570 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002571}
2572
2573// Test that we can receive the audio output level from a remote audio track.
2574// TODO(deadbeef): Use a fake audio source and verify that the output level is
2575// exactly what the source on the other side was configured with.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002576TEST_P(PeerConnectionIntegrationTest, GetAudioOutputLevelStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-29 21:08:16 -07002577 ASSERT_TRUE(CreatePeerConnectionWrappers());
2578 ConnectFakeSignaling();
2579 // Just add an audio track.
Steve Anton15324772018-01-16 10:26:49 -08002580 caller()->AddAudioTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07002581 caller()->CreateAndSetAndSignalOffer();
2582 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2583
2584 // Get the audio output level stats. Note that the level is not available
2585 // until an RTCP packet has been received.
deadbeefd8ad7882017-04-18 16:01:17 -07002586 EXPECT_TRUE_WAIT(callee()->OldGetStats()->AudioOutputLevel() > 0,
deadbeef1dcb1642017-03-29 21:08:16 -07002587 kMaxWaitForFramesMs);
2588}
2589
2590// Test that an audio input level is reported.
2591// TODO(deadbeef): Use a fake audio source and verify that the input level is
2592// exactly what the source was configured with.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002593TEST_P(PeerConnectionIntegrationTest, GetAudioInputLevelStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-29 21:08:16 -07002594 ASSERT_TRUE(CreatePeerConnectionWrappers());
2595 ConnectFakeSignaling();
2596 // Just add an audio track.
Steve Anton15324772018-01-16 10:26:49 -08002597 caller()->AddAudioTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07002598 caller()->CreateAndSetAndSignalOffer();
2599 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2600
2601 // Get the audio input level stats. The level should be available very
2602 // soon after the test starts.
deadbeefd8ad7882017-04-18 16:01:17 -07002603 EXPECT_TRUE_WAIT(caller()->OldGetStats()->AudioInputLevel() > 0,
deadbeef1dcb1642017-03-29 21:08:16 -07002604 kMaxWaitForStatsMs);
2605}
2606
2607// Test that we can get incoming byte counts from both audio and video tracks.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002608TEST_P(PeerConnectionIntegrationTest, GetBytesReceivedStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-29 21:08:16 -07002609 ASSERT_TRUE(CreatePeerConnectionWrappers());
2610 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002611 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002612 // Do offer/answer, wait for the callee to receive some frames.
2613 caller()->CreateAndSetAndSignalOffer();
2614 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002615
2616 MediaExpectations media_expectations;
2617 media_expectations.CalleeExpectsSomeAudioAndVideo();
2618 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002619
2620 // Get a handle to the remote tracks created, so they can be used as GetStats
2621 // filters.
Mirko Bonadei739baf02019-01-27 17:29:42 +01002622 for (const auto& receiver : callee()->pc()->GetReceivers()) {
Steve Anton15324772018-01-16 10:26:49 -08002623 // We received frames, so we definitely should have nonzero "received bytes"
2624 // stats at this point.
2625 EXPECT_GT(callee()->OldGetStatsForTrack(receiver->track())->BytesReceived(),
2626 0);
2627 }
deadbeef1dcb1642017-03-29 21:08:16 -07002628}
2629
2630// Test that we can get outgoing byte counts from both audio and video tracks.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002631TEST_P(PeerConnectionIntegrationTest, GetBytesSentStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-29 21:08:16 -07002632 ASSERT_TRUE(CreatePeerConnectionWrappers());
2633 ConnectFakeSignaling();
2634 auto audio_track = caller()->CreateLocalAudioTrack();
2635 auto video_track = caller()->CreateLocalVideoTrack();
Steve Anton15324772018-01-16 10:26:49 -08002636 caller()->AddTrack(audio_track);
2637 caller()->AddTrack(video_track);
deadbeef1dcb1642017-03-29 21:08:16 -07002638 // Do offer/answer, wait for the callee to receive some frames.
2639 caller()->CreateAndSetAndSignalOffer();
2640 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002641 MediaExpectations media_expectations;
2642 media_expectations.CalleeExpectsSomeAudioAndVideo();
2643 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002644
2645 // The callee received frames, so we definitely should have nonzero "sent
2646 // bytes" stats at this point.
deadbeefd8ad7882017-04-18 16:01:17 -07002647 EXPECT_GT(caller()->OldGetStatsForTrack(audio_track)->BytesSent(), 0);
2648 EXPECT_GT(caller()->OldGetStatsForTrack(video_track)->BytesSent(), 0);
2649}
2650
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002651// Test that we can get capture start ntp time.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002652TEST_P(PeerConnectionIntegrationTest, GetCaptureStartNtpTimeWithOldStatsApi) {
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002653 ASSERT_TRUE(CreatePeerConnectionWrappers());
2654 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002655 caller()->AddAudioTrack();
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002656
Steve Anton15324772018-01-16 10:26:49 -08002657 callee()->AddAudioTrack();
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002658
2659 // Do offer/answer, wait for the callee to receive some frames.
2660 caller()->CreateAndSetAndSignalOffer();
2661 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2662
2663 // Get the remote audio track created on the receiver, so they can be used as
2664 // GetStats filters.
Steve Antonfc853712018-03-01 13:48:58 -08002665 auto receivers = callee()->pc()->GetReceivers();
2666 ASSERT_EQ(1u, receivers.size());
2667 auto remote_audio_track = receivers[0]->track();
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002668
2669 // Get the audio output level stats. Note that the level is not available
2670 // until an RTCP packet has been received.
Zhi Huange830e682018-03-30 10:48:35 -07002671 EXPECT_TRUE_WAIT(
2672 callee()->OldGetStatsForTrack(remote_audio_track)->CaptureStartNtpTime() >
2673 0,
2674 2 * kMaxWaitForFramesMs);
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002675}
2676
Steve Antona41959e2018-11-28 11:15:33 -08002677// Test that the track ID is associated with all local and remote SSRC stats
2678// using the old GetStats() and more than 1 audio and more than 1 video track.
2679// This is a regression test for crbug.com/906988
2680TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
2681 OldGetStatsAssociatesTrackIdForManyMediaSections) {
2682 ASSERT_TRUE(CreatePeerConnectionWrappers());
2683 ConnectFakeSignaling();
2684 auto audio_sender_1 = caller()->AddAudioTrack();
2685 auto video_sender_1 = caller()->AddVideoTrack();
2686 auto audio_sender_2 = caller()->AddAudioTrack();
2687 auto video_sender_2 = caller()->AddVideoTrack();
2688 caller()->CreateAndSetAndSignalOffer();
2689 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2690
2691 MediaExpectations media_expectations;
2692 media_expectations.CalleeExpectsSomeAudioAndVideo();
2693 ASSERT_TRUE_WAIT(ExpectNewFrames(media_expectations), kDefaultTimeout);
2694
2695 std::vector<std::string> track_ids = {
2696 audio_sender_1->track()->id(), video_sender_1->track()->id(),
2697 audio_sender_2->track()->id(), video_sender_2->track()->id()};
2698
2699 auto caller_stats = caller()->OldGetStats();
2700 EXPECT_THAT(caller_stats->TrackIds(), UnorderedElementsAreArray(track_ids));
2701 auto callee_stats = callee()->OldGetStats();
2702 EXPECT_THAT(callee_stats->TrackIds(), UnorderedElementsAreArray(track_ids));
2703}
2704
Steve Antonffa6ce42018-11-30 09:26:08 -08002705// Test that the new GetStats() returns stats for all outgoing/incoming streams
2706// with the correct track IDs if there are more than one audio and more than one
2707// video senders/receivers.
2708TEST_P(PeerConnectionIntegrationTest, NewGetStatsManyAudioAndManyVideoStreams) {
2709 ASSERT_TRUE(CreatePeerConnectionWrappers());
2710 ConnectFakeSignaling();
2711 auto audio_sender_1 = caller()->AddAudioTrack();
2712 auto video_sender_1 = caller()->AddVideoTrack();
2713 auto audio_sender_2 = caller()->AddAudioTrack();
2714 auto video_sender_2 = caller()->AddVideoTrack();
2715 caller()->CreateAndSetAndSignalOffer();
2716 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2717
2718 MediaExpectations media_expectations;
2719 media_expectations.CalleeExpectsSomeAudioAndVideo();
2720 ASSERT_TRUE_WAIT(ExpectNewFrames(media_expectations), kDefaultTimeout);
2721
2722 std::vector<std::string> track_ids = {
2723 audio_sender_1->track()->id(), video_sender_1->track()->id(),
2724 audio_sender_2->track()->id(), video_sender_2->track()->id()};
2725
2726 rtc::scoped_refptr<const webrtc::RTCStatsReport> caller_report =
2727 caller()->NewGetStats();
2728 ASSERT_TRUE(caller_report);
2729 auto outbound_stream_stats =
2730 caller_report->GetStatsOfType<webrtc::RTCOutboundRTPStreamStats>();
2731 ASSERT_EQ(4u, outbound_stream_stats.size());
2732 std::vector<std::string> outbound_track_ids;
2733 for (const auto& stat : outbound_stream_stats) {
2734 ASSERT_TRUE(stat->bytes_sent.is_defined());
2735 EXPECT_LT(0u, *stat->bytes_sent);
Rasmus Brandt2efae772019-06-27 14:29:34 +02002736 if (*stat->kind == "video") {
2737 ASSERT_TRUE(stat->key_frames_encoded.is_defined());
2738 EXPECT_GT(*stat->key_frames_encoded, 0u);
2739 ASSERT_TRUE(stat->frames_encoded.is_defined());
2740 EXPECT_GE(*stat->frames_encoded, *stat->key_frames_encoded);
2741 }
Steve Antonffa6ce42018-11-30 09:26:08 -08002742 ASSERT_TRUE(stat->track_id.is_defined());
2743 const auto* track_stat =
2744 caller_report->GetAs<webrtc::RTCMediaStreamTrackStats>(*stat->track_id);
2745 ASSERT_TRUE(track_stat);
2746 outbound_track_ids.push_back(*track_stat->track_identifier);
2747 }
2748 EXPECT_THAT(outbound_track_ids, UnorderedElementsAreArray(track_ids));
2749
2750 rtc::scoped_refptr<const webrtc::RTCStatsReport> callee_report =
2751 callee()->NewGetStats();
2752 ASSERT_TRUE(callee_report);
2753 auto inbound_stream_stats =
2754 callee_report->GetStatsOfType<webrtc::RTCInboundRTPStreamStats>();
2755 ASSERT_EQ(4u, inbound_stream_stats.size());
2756 std::vector<std::string> inbound_track_ids;
2757 for (const auto& stat : inbound_stream_stats) {
2758 ASSERT_TRUE(stat->bytes_received.is_defined());
2759 EXPECT_LT(0u, *stat->bytes_received);
Rasmus Brandt2efae772019-06-27 14:29:34 +02002760 if (*stat->kind == "video") {
2761 ASSERT_TRUE(stat->key_frames_decoded.is_defined());
2762 EXPECT_GT(*stat->key_frames_decoded, 0u);
2763 ASSERT_TRUE(stat->frames_decoded.is_defined());
2764 EXPECT_GE(*stat->frames_decoded, *stat->key_frames_decoded);
2765 }
Steve Antonffa6ce42018-11-30 09:26:08 -08002766 ASSERT_TRUE(stat->track_id.is_defined());
2767 const auto* track_stat =
2768 callee_report->GetAs<webrtc::RTCMediaStreamTrackStats>(*stat->track_id);
2769 ASSERT_TRUE(track_stat);
2770 inbound_track_ids.push_back(*track_stat->track_identifier);
2771 }
2772 EXPECT_THAT(inbound_track_ids, UnorderedElementsAreArray(track_ids));
2773}
2774
2775// Test that we can get stats (using the new stats implementation) for
deadbeefd8ad7882017-04-18 16:01:17 -07002776// unsignaled streams. Meaning when SSRCs/MSIDs aren't signaled explicitly in
2777// SDP.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002778TEST_P(PeerConnectionIntegrationTest,
deadbeefd8ad7882017-04-18 16:01:17 -07002779 GetStatsForUnsignaledStreamWithNewStatsApi) {
2780 ASSERT_TRUE(CreatePeerConnectionWrappers());
2781 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002782 caller()->AddAudioTrack();
deadbeefd8ad7882017-04-18 16:01:17 -07002783 // Remove SSRCs and MSIDs from the received offer SDP.
2784 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
2785 caller()->CreateAndSetAndSignalOffer();
2786 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002787 MediaExpectations media_expectations;
2788 media_expectations.CalleeExpectsSomeAudio(1);
2789 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeefd8ad7882017-04-18 16:01:17 -07002790
2791 // We received a frame, so we should have nonzero "bytes received" stats for
2792 // the unsignaled stream, if stats are working for it.
2793 rtc::scoped_refptr<const webrtc::RTCStatsReport> report =
2794 callee()->NewGetStats();
2795 ASSERT_NE(nullptr, report);
2796 auto inbound_stream_stats =
2797 report->GetStatsOfType<webrtc::RTCInboundRTPStreamStats>();
2798 ASSERT_EQ(1U, inbound_stream_stats.size());
2799 ASSERT_TRUE(inbound_stream_stats[0]->bytes_received.is_defined());
2800 ASSERT_GT(*inbound_stream_stats[0]->bytes_received, 0U);
zhihuangf8164932017-05-19 13:09:47 -07002801 ASSERT_TRUE(inbound_stream_stats[0]->track_id.is_defined());
2802}
2803
Taylor Brandstettera4653442018-06-19 09:44:26 -07002804// Same as above but for the legacy stats implementation.
2805TEST_P(PeerConnectionIntegrationTest,
2806 GetStatsForUnsignaledStreamWithOldStatsApi) {
2807 ASSERT_TRUE(CreatePeerConnectionWrappers());
2808 ConnectFakeSignaling();
2809 caller()->AddAudioTrack();
2810 // Remove SSRCs and MSIDs from the received offer SDP.
2811 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
2812 caller()->CreateAndSetAndSignalOffer();
2813 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2814
2815 // Note that, since the old stats implementation associates SSRCs with tracks
2816 // using SDP, when SSRCs aren't signaled in SDP these stats won't have an
2817 // associated track ID. So we can't use the track "selector" argument.
2818 //
2819 // Also, we use "EXPECT_TRUE_WAIT" because the stats collector may decide to
2820 // return cached stats if not enough time has passed since the last update.
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02002821 EXPECT_TRUE_WAIT(callee()->OldGetStats()->BytesReceived() > 0,
Taylor Brandstettera4653442018-06-19 09:44:26 -07002822 kDefaultTimeout);
2823}
2824
zhihuangf8164932017-05-19 13:09:47 -07002825// Test that we can successfully get the media related stats (audio level
2826// etc.) for the unsignaled stream.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002827TEST_P(PeerConnectionIntegrationTest,
zhihuangf8164932017-05-19 13:09:47 -07002828 GetMediaStatsForUnsignaledStreamWithNewStatsApi) {
2829 ASSERT_TRUE(CreatePeerConnectionWrappers());
2830 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002831 caller()->AddAudioVideoTracks();
zhihuangf8164932017-05-19 13:09:47 -07002832 // Remove SSRCs and MSIDs from the received offer SDP.
2833 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
2834 caller()->CreateAndSetAndSignalOffer();
2835 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002836 MediaExpectations media_expectations;
2837 media_expectations.CalleeExpectsSomeAudio(1);
2838 media_expectations.CalleeExpectsSomeVideo(1);
2839 ASSERT_TRUE(ExpectNewFrames(media_expectations));
zhihuangf8164932017-05-19 13:09:47 -07002840
2841 rtc::scoped_refptr<const webrtc::RTCStatsReport> report =
2842 callee()->NewGetStats();
2843 ASSERT_NE(nullptr, report);
2844
2845 auto media_stats = report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
2846 auto audio_index = FindFirstMediaStatsIndexByKind("audio", media_stats);
2847 ASSERT_GE(audio_index, 0);
2848 EXPECT_TRUE(media_stats[audio_index]->audio_level.is_defined());
deadbeef1dcb1642017-03-29 21:08:16 -07002849}
2850
deadbeef4e2deab2017-09-20 13:56:21 -07002851// Helper for test below.
2852void ModifySsrcs(cricket::SessionDescription* desc) {
2853 for (ContentInfo& content : desc->contents()) {
Steve Antondf527fd2018-04-27 15:52:03 -07002854 for (StreamParams& stream :
Steve Antonb1c1de12017-12-21 15:14:30 -08002855 content.media_description()->mutable_streams()) {
deadbeef4e2deab2017-09-20 13:56:21 -07002856 for (uint32_t& ssrc : stream.ssrcs) {
2857 ssrc = rtc::CreateRandomId();
2858 }
2859 }
2860 }
2861}
2862
2863// Test that the "RTCMediaSteamTrackStats" object is updated correctly when
2864// SSRCs are unsignaled, and the SSRC of the received (audio) stream changes.
2865// This should result in two "RTCInboundRTPStreamStats", but only one
2866// "RTCMediaStreamTrackStats", whose counters go up continuously rather than
2867// being reset to 0 once the SSRC change occurs.
2868//
2869// Regression test for this bug:
2870// https://bugs.chromium.org/p/webrtc/issues/detail?id=8158
2871//
2872// The bug causes the track stats to only represent one of the two streams:
2873// whichever one has the higher SSRC. So with this bug, there was a 50% chance
2874// that the track stat counters would reset to 0 when the new stream is
2875// received, and a 50% chance that they'll stop updating (while
2876// "concealed_samples" continues increasing, due to silence being generated for
2877// the inactive stream).
Seth Hampson2f0d7022018-02-20 11:54:42 -08002878TEST_P(PeerConnectionIntegrationTest,
Steve Anton83119dd2017-11-10 16:19:52 -08002879 TrackStatsUpdatedCorrectlyWhenUnsignaledSsrcChanges) {
deadbeef4e2deab2017-09-20 13:56:21 -07002880 ASSERT_TRUE(CreatePeerConnectionWrappers());
2881 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002882 caller()->AddAudioTrack();
deadbeef4e2deab2017-09-20 13:56:21 -07002883 // Remove SSRCs and MSIDs from the received offer SDP, simulating an endpoint
2884 // that doesn't signal SSRCs (from the callee's perspective).
2885 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
2886 caller()->CreateAndSetAndSignalOffer();
2887 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2888 // Wait for 50 audio frames (500ms of audio) to be received by the callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002889 {
2890 MediaExpectations media_expectations;
2891 media_expectations.CalleeExpectsSomeAudio(50);
2892 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2893 }
deadbeef4e2deab2017-09-20 13:56:21 -07002894 // Some audio frames were received, so we should have nonzero "samples
2895 // received" for the track.
2896 rtc::scoped_refptr<const webrtc::RTCStatsReport> report =
2897 callee()->NewGetStats();
2898 ASSERT_NE(nullptr, report);
2899 auto track_stats = report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
2900 ASSERT_EQ(1U, track_stats.size());
2901 ASSERT_TRUE(track_stats[0]->total_samples_received.is_defined());
2902 ASSERT_GT(*track_stats[0]->total_samples_received, 0U);
2903 // uint64_t prev_samples_received = *track_stats[0]->total_samples_received;
2904
2905 // Create a new offer and munge it to cause the caller to use a new SSRC.
2906 caller()->SetGeneratedSdpMunger(ModifySsrcs);
2907 caller()->CreateAndSetAndSignalOffer();
2908 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2909 // Wait for 25 more audio frames (250ms of audio) to be received, from the new
2910 // SSRC.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002911 {
2912 MediaExpectations media_expectations;
2913 media_expectations.CalleeExpectsSomeAudio(25);
2914 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2915 }
deadbeef4e2deab2017-09-20 13:56:21 -07002916
2917 report = callee()->NewGetStats();
2918 ASSERT_NE(nullptr, report);
2919 track_stats = report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
2920 ASSERT_EQ(1U, track_stats.size());
2921 ASSERT_TRUE(track_stats[0]->total_samples_received.is_defined());
2922 // The "total samples received" stat should only be greater than it was
2923 // before.
2924 // TODO(deadbeef): Uncomment this assertion once the bug is completely fixed.
2925 // Right now, the new SSRC will cause the counters to reset to 0.
2926 // EXPECT_GT(*track_stats[0]->total_samples_received, prev_samples_received);
2927
2928 // Additionally, the percentage of concealed samples (samples generated to
Steve Anton83119dd2017-11-10 16:19:52 -08002929 // conceal packet loss) should be less than 50%. If it's greater, that's a
deadbeef4e2deab2017-09-20 13:56:21 -07002930 // good sign that we're seeing stats from the old stream that's no longer
2931 // receiving packets, and is generating concealed samples of silence.
Steve Anton83119dd2017-11-10 16:19:52 -08002932 constexpr double kAcceptableConcealedSamplesPercentage = 0.50;
deadbeef4e2deab2017-09-20 13:56:21 -07002933 ASSERT_TRUE(track_stats[0]->concealed_samples.is_defined());
2934 EXPECT_LT(*track_stats[0]->concealed_samples,
2935 *track_stats[0]->total_samples_received *
2936 kAcceptableConcealedSamplesPercentage);
2937
2938 // Also ensure that we have two "RTCInboundRTPStreamStats" as expected, as a
2939 // sanity check that the SSRC really changed.
2940 // TODO(deadbeef): This isn't working right now, because we're not returning
2941 // *any* stats for the inactive stream. Uncomment when the bug is completely
2942 // fixed.
2943 // auto inbound_stream_stats =
2944 // report->GetStatsOfType<webrtc::RTCInboundRTPStreamStats>();
2945 // ASSERT_EQ(2U, inbound_stream_stats.size());
2946}
2947
deadbeef1dcb1642017-03-29 21:08:16 -07002948// Test that DTLS 1.0 is used if both sides only support DTLS 1.0.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002949TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithDtls10) {
deadbeef1dcb1642017-03-29 21:08:16 -07002950 PeerConnectionFactory::Options dtls_10_options;
2951 dtls_10_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
2952 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(dtls_10_options,
2953 dtls_10_options));
2954 ConnectFakeSignaling();
2955 // Do normal offer/answer and wait for some frames to be received in each
2956 // direction.
Steve Anton15324772018-01-16 10:26:49 -08002957 caller()->AddAudioVideoTracks();
2958 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002959 caller()->CreateAndSetAndSignalOffer();
2960 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002961 MediaExpectations media_expectations;
2962 media_expectations.ExpectBidirectionalAudioAndVideo();
2963 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002964}
2965
2966// Test getting cipher stats and UMA metrics when DTLS 1.0 is negotiated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002967TEST_P(PeerConnectionIntegrationTest, Dtls10CipherStatsAndUmaMetrics) {
deadbeef1dcb1642017-03-29 21:08:16 -07002968 PeerConnectionFactory::Options dtls_10_options;
2969 dtls_10_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
2970 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(dtls_10_options,
2971 dtls_10_options));
2972 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002973 caller()->AddAudioVideoTracks();
2974 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002975 caller()->CreateAndSetAndSignalOffer();
Qingsi Wang7fc821d2018-07-12 12:54:53 -07002976 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07002977 EXPECT_TRUE_WAIT(rtc::SSLStreamAdapter::IsAcceptableCipher(
deadbeefd8ad7882017-04-18 16:01:17 -07002978 caller()->OldGetStats()->DtlsCipher(), rtc::KT_DEFAULT),
deadbeef1dcb1642017-03-29 21:08:16 -07002979 kDefaultTimeout);
2980 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
deadbeefd8ad7882017-04-18 16:01:17 -07002981 caller()->OldGetStats()->SrtpCipher(), kDefaultTimeout);
Qingsi Wang7fc821d2018-07-12 12:54:53 -07002982 // TODO(bugs.webrtc.org/9456): Fix it.
Alex Loiko9289eda2018-11-23 16:18:59 +00002983 EXPECT_EQ(1, webrtc::metrics::NumEvents(
Qingsi Wang7fc821d2018-07-12 12:54:53 -07002984 "WebRTC.PeerConnection.SrtpCryptoSuite.Audio",
2985 kDefaultSrtpCryptoSuite));
deadbeef1dcb1642017-03-29 21:08:16 -07002986}
2987
2988// Test getting cipher stats and UMA metrics when DTLS 1.2 is negotiated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002989TEST_P(PeerConnectionIntegrationTest, Dtls12CipherStatsAndUmaMetrics) {
deadbeef1dcb1642017-03-29 21:08:16 -07002990 PeerConnectionFactory::Options dtls_12_options;
2991 dtls_12_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
2992 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(dtls_12_options,
2993 dtls_12_options));
2994 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002995 caller()->AddAudioVideoTracks();
2996 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002997 caller()->CreateAndSetAndSignalOffer();
Qingsi Wang7fc821d2018-07-12 12:54:53 -07002998 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07002999 EXPECT_TRUE_WAIT(rtc::SSLStreamAdapter::IsAcceptableCipher(
deadbeefd8ad7882017-04-18 16:01:17 -07003000 caller()->OldGetStats()->DtlsCipher(), rtc::KT_DEFAULT),
deadbeef1dcb1642017-03-29 21:08:16 -07003001 kDefaultTimeout);
3002 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
deadbeefd8ad7882017-04-18 16:01:17 -07003003 caller()->OldGetStats()->SrtpCipher(), kDefaultTimeout);
Qingsi Wang7fc821d2018-07-12 12:54:53 -07003004 // TODO(bugs.webrtc.org/9456): Fix it.
Alex Loiko9289eda2018-11-23 16:18:59 +00003005 EXPECT_EQ(1, webrtc::metrics::NumEvents(
Qingsi Wang7fc821d2018-07-12 12:54:53 -07003006 "WebRTC.PeerConnection.SrtpCryptoSuite.Audio",
3007 kDefaultSrtpCryptoSuite));
deadbeef1dcb1642017-03-29 21:08:16 -07003008}
3009
3010// Test that DTLS 1.0 can be used if the caller supports DTLS 1.2 and the
3011// callee only supports 1.0.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003012TEST_P(PeerConnectionIntegrationTest, CallerDtls12ToCalleeDtls10) {
deadbeef1dcb1642017-03-29 21:08:16 -07003013 PeerConnectionFactory::Options caller_options;
3014 caller_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
3015 PeerConnectionFactory::Options callee_options;
3016 callee_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
3017 ASSERT_TRUE(
3018 CreatePeerConnectionWrappersWithOptions(caller_options, callee_options));
3019 ConnectFakeSignaling();
3020 // Do normal offer/answer and wait for some frames to be received in each
3021 // direction.
Steve Anton15324772018-01-16 10:26:49 -08003022 caller()->AddAudioVideoTracks();
3023 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003024 caller()->CreateAndSetAndSignalOffer();
3025 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08003026 MediaExpectations media_expectations;
3027 media_expectations.ExpectBidirectionalAudioAndVideo();
3028 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003029}
3030
3031// Test that DTLS 1.0 can be used if the caller only supports DTLS 1.0 and the
3032// callee supports 1.2.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003033TEST_P(PeerConnectionIntegrationTest, CallerDtls10ToCalleeDtls12) {
deadbeef1dcb1642017-03-29 21:08:16 -07003034 PeerConnectionFactory::Options caller_options;
3035 caller_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
3036 PeerConnectionFactory::Options callee_options;
3037 callee_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
3038 ASSERT_TRUE(
3039 CreatePeerConnectionWrappersWithOptions(caller_options, callee_options));
3040 ConnectFakeSignaling();
3041 // Do normal offer/answer and wait for some frames to be received in each
3042 // direction.
Steve Anton15324772018-01-16 10:26:49 -08003043 caller()->AddAudioVideoTracks();
3044 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003045 caller()->CreateAndSetAndSignalOffer();
3046 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08003047 MediaExpectations media_expectations;
3048 media_expectations.ExpectBidirectionalAudioAndVideo();
3049 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003050}
3051
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07003052// The three tests below verify that "enable_aes128_sha1_32_crypto_cipher"
3053// works as expected; the cipher should only be used if enabled by both sides.
3054TEST_P(PeerConnectionIntegrationTest,
3055 Aes128Sha1_32_CipherNotUsedWhenOnlyCallerSupported) {
3056 PeerConnectionFactory::Options caller_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07003057 caller_options.crypto_options.srtp.enable_aes128_sha1_32_crypto_cipher = true;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07003058 PeerConnectionFactory::Options callee_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07003059 callee_options.crypto_options.srtp.enable_aes128_sha1_32_crypto_cipher =
3060 false;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07003061 int expected_cipher_suite = rtc::SRTP_AES128_CM_SHA1_80;
3062 TestNegotiatedCipherSuite(caller_options, callee_options,
3063 expected_cipher_suite);
3064}
3065
3066TEST_P(PeerConnectionIntegrationTest,
3067 Aes128Sha1_32_CipherNotUsedWhenOnlyCalleeSupported) {
3068 PeerConnectionFactory::Options caller_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07003069 caller_options.crypto_options.srtp.enable_aes128_sha1_32_crypto_cipher =
3070 false;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07003071 PeerConnectionFactory::Options callee_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07003072 callee_options.crypto_options.srtp.enable_aes128_sha1_32_crypto_cipher = true;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07003073 int expected_cipher_suite = rtc::SRTP_AES128_CM_SHA1_80;
3074 TestNegotiatedCipherSuite(caller_options, callee_options,
3075 expected_cipher_suite);
3076}
3077
3078TEST_P(PeerConnectionIntegrationTest, Aes128Sha1_32_CipherUsedWhenSupported) {
3079 PeerConnectionFactory::Options caller_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07003080 caller_options.crypto_options.srtp.enable_aes128_sha1_32_crypto_cipher = true;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07003081 PeerConnectionFactory::Options callee_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07003082 callee_options.crypto_options.srtp.enable_aes128_sha1_32_crypto_cipher = true;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07003083 int expected_cipher_suite = rtc::SRTP_AES128_CM_SHA1_32;
3084 TestNegotiatedCipherSuite(caller_options, callee_options,
3085 expected_cipher_suite);
3086}
3087
deadbeef1dcb1642017-03-29 21:08:16 -07003088// Test that a non-GCM cipher is used if both sides only support non-GCM.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003089TEST_P(PeerConnectionIntegrationTest, NonGcmCipherUsedWhenGcmNotSupported) {
deadbeef1dcb1642017-03-29 21:08:16 -07003090 bool local_gcm_enabled = false;
3091 bool remote_gcm_enabled = false;
3092 int expected_cipher_suite = kDefaultSrtpCryptoSuite;
3093 TestGcmNegotiationUsesCipherSuite(local_gcm_enabled, remote_gcm_enabled,
3094 expected_cipher_suite);
3095}
3096
3097// Test that a GCM cipher is used if both ends support it.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003098TEST_P(PeerConnectionIntegrationTest, GcmCipherUsedWhenGcmSupported) {
deadbeef1dcb1642017-03-29 21:08:16 -07003099 bool local_gcm_enabled = true;
3100 bool remote_gcm_enabled = true;
3101 int expected_cipher_suite = kDefaultSrtpCryptoSuiteGcm;
3102 TestGcmNegotiationUsesCipherSuite(local_gcm_enabled, remote_gcm_enabled,
3103 expected_cipher_suite);
3104}
3105
3106// Test that GCM isn't used if only the offerer supports it.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003107TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07003108 NonGcmCipherUsedWhenOnlyCallerSupportsGcm) {
3109 bool local_gcm_enabled = true;
3110 bool remote_gcm_enabled = false;
3111 int expected_cipher_suite = kDefaultSrtpCryptoSuite;
3112 TestGcmNegotiationUsesCipherSuite(local_gcm_enabled, remote_gcm_enabled,
3113 expected_cipher_suite);
3114}
3115
3116// Test that GCM isn't used if only the answerer supports it.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003117TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07003118 NonGcmCipherUsedWhenOnlyCalleeSupportsGcm) {
3119 bool local_gcm_enabled = false;
3120 bool remote_gcm_enabled = true;
3121 int expected_cipher_suite = kDefaultSrtpCryptoSuite;
3122 TestGcmNegotiationUsesCipherSuite(local_gcm_enabled, remote_gcm_enabled,
3123 expected_cipher_suite);
3124}
3125
deadbeef7914b8c2017-04-21 03:23:33 -07003126// Verify that media can be transmitted end-to-end when GCM crypto suites are
3127// enabled. Note that the above tests, such as GcmCipherUsedWhenGcmSupported,
3128// only verify that a GCM cipher is negotiated, and not necessarily that SRTP
3129// works with it.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003130TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithGcmCipher) {
deadbeef7914b8c2017-04-21 03:23:33 -07003131 PeerConnectionFactory::Options gcm_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07003132 gcm_options.crypto_options.srtp.enable_gcm_crypto_suites = true;
deadbeef7914b8c2017-04-21 03:23:33 -07003133 ASSERT_TRUE(
3134 CreatePeerConnectionWrappersWithOptions(gcm_options, gcm_options));
3135 ConnectFakeSignaling();
3136 // Do normal offer/answer and wait for some frames to be received in each
3137 // direction.
Steve Anton15324772018-01-16 10:26:49 -08003138 caller()->AddAudioVideoTracks();
3139 callee()->AddAudioVideoTracks();
deadbeef7914b8c2017-04-21 03:23:33 -07003140 caller()->CreateAndSetAndSignalOffer();
3141 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08003142 MediaExpectations media_expectations;
3143 media_expectations.ExpectBidirectionalAudioAndVideo();
3144 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef7914b8c2017-04-21 03:23:33 -07003145}
3146
deadbeef1dcb1642017-03-29 21:08:16 -07003147// This test sets up a call between two parties with audio, video and an RTP
3148// data channel.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003149TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithRtpDataChannel) {
Niels Möllerf06f9232018-08-07 12:32:18 +02003150 PeerConnectionInterface::RTCConfiguration rtc_config;
3151 rtc_config.enable_rtp_data_channel = true;
3152 rtc_config.enable_dtls_srtp = false;
3153 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(rtc_config, rtc_config));
deadbeef1dcb1642017-03-29 21:08:16 -07003154 ConnectFakeSignaling();
3155 // Expect that data channel created on caller side will show up for callee as
3156 // well.
3157 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08003158 caller()->AddAudioVideoTracks();
3159 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003160 caller()->CreateAndSetAndSignalOffer();
3161 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3162 // Ensure the existence of the RTP data channel didn't impede audio/video.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003163 MediaExpectations media_expectations;
3164 media_expectations.ExpectBidirectionalAudioAndVideo();
3165 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003166 ASSERT_NE(nullptr, caller()->data_channel());
3167 ASSERT_NE(nullptr, callee()->data_channel());
3168 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3169 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3170
3171 // Ensure data can be sent in both directions.
3172 std::string data = "hello world";
3173 SendRtpDataWithRetries(caller()->data_channel(), data, 5);
3174 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3175 kDefaultTimeout);
3176 SendRtpDataWithRetries(callee()->data_channel(), data, 5);
3177 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3178 kDefaultTimeout);
3179}
3180
3181// Ensure that an RTP data channel is signaled as closed for the caller when
3182// the callee rejects it in a subsequent offer.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003183TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07003184 RtpDataChannelSignaledClosedInCalleeOffer) {
3185 // Same procedure as above test.
Niels Möllerf06f9232018-08-07 12:32:18 +02003186 PeerConnectionInterface::RTCConfiguration rtc_config;
3187 rtc_config.enable_rtp_data_channel = true;
3188 rtc_config.enable_dtls_srtp = false;
3189 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(rtc_config, rtc_config));
deadbeef1dcb1642017-03-29 21:08:16 -07003190 ConnectFakeSignaling();
3191 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08003192 caller()->AddAudioVideoTracks();
3193 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003194 caller()->CreateAndSetAndSignalOffer();
3195 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3196 ASSERT_NE(nullptr, caller()->data_channel());
3197 ASSERT_NE(nullptr, callee()->data_channel());
3198 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3199 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3200
3201 // Close the data channel on the callee, and do an updated offer/answer.
3202 callee()->data_channel()->Close();
3203 callee()->CreateAndSetAndSignalOffer();
3204 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3205 EXPECT_FALSE(caller()->data_observer()->IsOpen());
3206 EXPECT_FALSE(callee()->data_observer()->IsOpen());
3207}
3208
3209// Tests that data is buffered in an RTP data channel until an observer is
3210// registered for it.
3211//
3212// NOTE: RTP data channels can receive data before the underlying
3213// transport has detected that a channel is writable and thus data can be
3214// received before the data channel state changes to open. That is hard to test
3215// but the same buffering is expected to be used in that case.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003216TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07003217 DataBufferedUntilRtpDataChannelObserverRegistered) {
3218 // Use fake clock and simulated network delay so that we predictably can wait
3219 // until an SCTP message has been delivered without "sleep()"ing.
3220 rtc::ScopedFakeClock fake_clock;
3221 // Some things use a time of "0" as a special value, so we need to start out
3222 // the fake clock at a nonzero time.
3223 // TODO(deadbeef): Fix this.
Sebastian Jansson5f83cf02018-05-08 14:52:22 +02003224 fake_clock.AdvanceTime(webrtc::TimeDelta::seconds(1));
deadbeef1dcb1642017-03-29 21:08:16 -07003225 virtual_socket_server()->set_delay_mean(5); // 5 ms per hop.
3226 virtual_socket_server()->UpdateDelayDistribution();
3227
Niels Möllerf06f9232018-08-07 12:32:18 +02003228 PeerConnectionInterface::RTCConfiguration rtc_config;
3229 rtc_config.enable_rtp_data_channel = true;
3230 rtc_config.enable_dtls_srtp = false;
3231 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(rtc_config, rtc_config));
deadbeef1dcb1642017-03-29 21:08:16 -07003232 ConnectFakeSignaling();
3233 caller()->CreateDataChannel();
3234 caller()->CreateAndSetAndSignalOffer();
3235 ASSERT_TRUE(caller()->data_channel() != nullptr);
3236 ASSERT_TRUE_SIMULATED_WAIT(callee()->data_channel() != nullptr,
3237 kDefaultTimeout, fake_clock);
3238 ASSERT_TRUE_SIMULATED_WAIT(caller()->data_observer()->IsOpen(),
3239 kDefaultTimeout, fake_clock);
3240 ASSERT_EQ_SIMULATED_WAIT(DataChannelInterface::kOpen,
3241 callee()->data_channel()->state(), kDefaultTimeout,
3242 fake_clock);
3243
3244 // Unregister the observer which is normally automatically registered.
3245 callee()->data_channel()->UnregisterObserver();
3246 // Send data and advance fake clock until it should have been received.
3247 std::string data = "hello world";
3248 caller()->data_channel()->Send(DataBuffer(data));
3249 SIMULATED_WAIT(false, 50, fake_clock);
3250
3251 // Attach data channel and expect data to be received immediately. Note that
3252 // EXPECT_EQ_WAIT is used, such that the simulated clock is not advanced any
3253 // further, but data can be received even if the callback is asynchronous.
3254 MockDataChannelObserver new_observer(callee()->data_channel());
3255 EXPECT_EQ_SIMULATED_WAIT(data, new_observer.last_message(), kDefaultTimeout,
3256 fake_clock);
Seth Hampson1d4a76d2018-06-19 14:31:41 -07003257 // Closing the PeerConnections destroys the ports before the ScopedFakeClock.
3258 // If this is not done a DCHECK can be hit in ports.cc, because a large
3259 // negative number is calculated for the rtt due to the global clock changing.
Steve Antond91969e2019-05-30 12:27:03 -07003260 ClosePeerConnections();
deadbeef1dcb1642017-03-29 21:08:16 -07003261}
3262
3263// This test sets up a call between two parties with audio, video and but only
3264// the caller client supports RTP data channels.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003265TEST_P(PeerConnectionIntegrationTest, RtpDataChannelsRejectedByCallee) {
Niels Möllerf06f9232018-08-07 12:32:18 +02003266 PeerConnectionInterface::RTCConfiguration rtc_config_1;
3267 rtc_config_1.enable_rtp_data_channel = true;
deadbeef1dcb1642017-03-29 21:08:16 -07003268 // Must disable DTLS to make negotiation succeed.
Niels Möllerf06f9232018-08-07 12:32:18 +02003269 rtc_config_1.enable_dtls_srtp = false;
3270 PeerConnectionInterface::RTCConfiguration rtc_config_2;
3271 rtc_config_2.enable_dtls_srtp = false;
3272 rtc_config_2.enable_dtls_srtp = false;
3273 ASSERT_TRUE(
3274 CreatePeerConnectionWrappersWithConfig(rtc_config_1, rtc_config_2));
deadbeef1dcb1642017-03-29 21:08:16 -07003275 ConnectFakeSignaling();
3276 caller()->CreateDataChannel();
Harald Alvestrandf3736ed2019-04-08 13:09:30 +02003277 ASSERT_TRUE(caller()->data_channel() != nullptr);
Steve Anton15324772018-01-16 10:26:49 -08003278 caller()->AddAudioVideoTracks();
3279 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003280 caller()->CreateAndSetAndSignalOffer();
3281 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3282 // The caller should still have a data channel, but it should be closed, and
3283 // one should ever have been created for the callee.
3284 EXPECT_TRUE(caller()->data_channel() != nullptr);
3285 EXPECT_FALSE(caller()->data_observer()->IsOpen());
3286 EXPECT_EQ(nullptr, callee()->data_channel());
3287}
3288
3289// This test sets up a call between two parties with audio, and video. When
3290// audio and video is setup and flowing, an RTP data channel is negotiated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003291TEST_P(PeerConnectionIntegrationTest, AddRtpDataChannelInSubsequentOffer) {
Niels Möllerf06f9232018-08-07 12:32:18 +02003292 PeerConnectionInterface::RTCConfiguration rtc_config;
3293 rtc_config.enable_rtp_data_channel = true;
3294 rtc_config.enable_dtls_srtp = false;
3295 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(rtc_config, rtc_config));
deadbeef1dcb1642017-03-29 21:08:16 -07003296 ConnectFakeSignaling();
3297 // Do initial offer/answer with audio/video.
Steve Anton15324772018-01-16 10:26:49 -08003298 caller()->AddAudioVideoTracks();
3299 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003300 caller()->CreateAndSetAndSignalOffer();
3301 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3302 // Create data channel and do new offer and answer.
3303 caller()->CreateDataChannel();
3304 caller()->CreateAndSetAndSignalOffer();
3305 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3306 ASSERT_NE(nullptr, caller()->data_channel());
3307 ASSERT_NE(nullptr, callee()->data_channel());
3308 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3309 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3310 // Ensure data can be sent in both directions.
3311 std::string data = "hello world";
3312 SendRtpDataWithRetries(caller()->data_channel(), data, 5);
3313 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3314 kDefaultTimeout);
3315 SendRtpDataWithRetries(callee()->data_channel(), data, 5);
3316 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3317 kDefaultTimeout);
3318}
3319
3320#ifdef HAVE_SCTP
3321
3322// This test sets up a call between two parties with audio, video and an SCTP
3323// data channel.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003324TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithSctpDataChannel) {
deadbeef1dcb1642017-03-29 21:08:16 -07003325 ASSERT_TRUE(CreatePeerConnectionWrappers());
3326 ConnectFakeSignaling();
3327 // Expect that data channel created on caller side will show up for callee as
3328 // well.
3329 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08003330 caller()->AddAudioVideoTracks();
3331 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003332 caller()->CreateAndSetAndSignalOffer();
3333 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3334 // Ensure the existence of the SCTP data channel didn't impede audio/video.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003335 MediaExpectations media_expectations;
3336 media_expectations.ExpectBidirectionalAudioAndVideo();
3337 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003338 // Caller data channel should already exist (it created one). Callee data
3339 // channel may not exist yet, since negotiation happens in-band, not in SDP.
3340 ASSERT_NE(nullptr, caller()->data_channel());
3341 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3342 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3343 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3344
3345 // Ensure data can be sent in both directions.
3346 std::string data = "hello world";
3347 caller()->data_channel()->Send(DataBuffer(data));
3348 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3349 kDefaultTimeout);
3350 callee()->data_channel()->Send(DataBuffer(data));
3351 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3352 kDefaultTimeout);
3353}
3354
3355// Ensure that when the callee closes an SCTP data channel, the closing
3356// procedure results in the data channel being closed for the caller as well.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003357TEST_P(PeerConnectionIntegrationTest, CalleeClosesSctpDataChannel) {
deadbeef1dcb1642017-03-29 21:08:16 -07003358 // Same procedure as above test.
3359 ASSERT_TRUE(CreatePeerConnectionWrappers());
3360 ConnectFakeSignaling();
3361 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08003362 caller()->AddAudioVideoTracks();
3363 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003364 caller()->CreateAndSetAndSignalOffer();
3365 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3366 ASSERT_NE(nullptr, caller()->data_channel());
3367 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3368 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3369 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3370
3371 // Close the data channel on the callee side, and wait for it to reach the
3372 // "closed" state on both sides.
3373 callee()->data_channel()->Close();
3374 EXPECT_TRUE_WAIT(!caller()->data_observer()->IsOpen(), kDefaultTimeout);
3375 EXPECT_TRUE_WAIT(!callee()->data_observer()->IsOpen(), kDefaultTimeout);
3376}
3377
Seth Hampson2f0d7022018-02-20 11:54:42 -08003378TEST_P(PeerConnectionIntegrationTest, SctpDataChannelConfigSentToOtherSide) {
Steve Antonda6c0952017-10-23 11:41:54 -07003379 ASSERT_TRUE(CreatePeerConnectionWrappers());
3380 ConnectFakeSignaling();
3381 webrtc::DataChannelInit init;
3382 init.id = 53;
3383 init.maxRetransmits = 52;
3384 caller()->CreateDataChannel("data-channel", &init);
Steve Anton15324772018-01-16 10:26:49 -08003385 caller()->AddAudioVideoTracks();
3386 callee()->AddAudioVideoTracks();
Steve Antonda6c0952017-10-23 11:41:54 -07003387 caller()->CreateAndSetAndSignalOffer();
3388 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Steve Anton074dece2017-10-24 13:04:12 -07003389 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3390 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
Harald Alvestrand5c4d2ee2019-04-01 12:58:15 +02003391 // Since "negotiated" is false, the "id" parameter should be ignored.
3392 EXPECT_NE(init.id, callee()->data_channel()->id());
Steve Antonda6c0952017-10-23 11:41:54 -07003393 EXPECT_EQ("data-channel", callee()->data_channel()->label());
3394 EXPECT_EQ(init.maxRetransmits, callee()->data_channel()->maxRetransmits());
3395 EXPECT_FALSE(callee()->data_channel()->negotiated());
3396}
3397
deadbeef1dcb1642017-03-29 21:08:16 -07003398// Test usrsctp's ability to process unordered data stream, where data actually
3399// arrives out of order using simulated delays. Previously there have been some
3400// bugs in this area.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003401TEST_P(PeerConnectionIntegrationTest, StressTestUnorderedSctpDataChannel) {
deadbeef1dcb1642017-03-29 21:08:16 -07003402 // Introduce random network delays.
3403 // Otherwise it's not a true "unordered" test.
3404 virtual_socket_server()->set_delay_mean(20);
3405 virtual_socket_server()->set_delay_stddev(5);
3406 virtual_socket_server()->UpdateDelayDistribution();
3407 // Normal procedure, but with unordered data channel config.
3408 ASSERT_TRUE(CreatePeerConnectionWrappers());
3409 ConnectFakeSignaling();
3410 webrtc::DataChannelInit init;
3411 init.ordered = false;
3412 caller()->CreateDataChannel(&init);
3413 caller()->CreateAndSetAndSignalOffer();
3414 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3415 ASSERT_NE(nullptr, caller()->data_channel());
3416 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3417 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3418 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3419
3420 static constexpr int kNumMessages = 100;
3421 // Deliberately chosen to be larger than the MTU so messages get fragmented.
3422 static constexpr size_t kMaxMessageSize = 4096;
3423 // Create and send random messages.
3424 std::vector<std::string> sent_messages;
3425 for (int i = 0; i < kNumMessages; ++i) {
3426 size_t length =
3427 (rand() % kMaxMessageSize) + 1; // NOLINT (rand_r instead of rand)
3428 std::string message;
3429 ASSERT_TRUE(rtc::CreateRandomString(length, &message));
3430 caller()->data_channel()->Send(DataBuffer(message));
3431 callee()->data_channel()->Send(DataBuffer(message));
3432 sent_messages.push_back(message);
3433 }
3434
3435 // Wait for all messages to be received.
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02003436 EXPECT_EQ_WAIT(rtc::checked_cast<size_t>(kNumMessages),
deadbeef1dcb1642017-03-29 21:08:16 -07003437 caller()->data_observer()->received_message_count(),
3438 kDefaultTimeout);
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02003439 EXPECT_EQ_WAIT(rtc::checked_cast<size_t>(kNumMessages),
deadbeef1dcb1642017-03-29 21:08:16 -07003440 callee()->data_observer()->received_message_count(),
3441 kDefaultTimeout);
3442
3443 // Sort and compare to make sure none of the messages were corrupted.
3444 std::vector<std::string> caller_received_messages =
3445 caller()->data_observer()->messages();
3446 std::vector<std::string> callee_received_messages =
3447 callee()->data_observer()->messages();
Steve Anton64b626b2019-01-28 17:25:26 -08003448 absl::c_sort(sent_messages);
3449 absl::c_sort(caller_received_messages);
3450 absl::c_sort(callee_received_messages);
deadbeef1dcb1642017-03-29 21:08:16 -07003451 EXPECT_EQ(sent_messages, caller_received_messages);
3452 EXPECT_EQ(sent_messages, callee_received_messages);
3453}
3454
3455// This test sets up a call between two parties with audio, and video. When
3456// audio and video are setup and flowing, an SCTP data channel is negotiated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003457TEST_P(PeerConnectionIntegrationTest, AddSctpDataChannelInSubsequentOffer) {
deadbeef1dcb1642017-03-29 21:08:16 -07003458 ASSERT_TRUE(CreatePeerConnectionWrappers());
3459 ConnectFakeSignaling();
3460 // Do initial offer/answer with audio/video.
Steve Anton15324772018-01-16 10:26:49 -08003461 caller()->AddAudioVideoTracks();
3462 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003463 caller()->CreateAndSetAndSignalOffer();
3464 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3465 // Create data channel and do new offer and answer.
3466 caller()->CreateDataChannel();
3467 caller()->CreateAndSetAndSignalOffer();
3468 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3469 // Caller data channel should already exist (it created one). Callee data
3470 // channel may not exist yet, since negotiation happens in-band, not in SDP.
3471 ASSERT_NE(nullptr, caller()->data_channel());
3472 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3473 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3474 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3475 // Ensure data can be sent in both directions.
3476 std::string data = "hello world";
3477 caller()->data_channel()->Send(DataBuffer(data));
3478 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3479 kDefaultTimeout);
3480 callee()->data_channel()->Send(DataBuffer(data));
3481 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3482 kDefaultTimeout);
3483}
3484
deadbeef7914b8c2017-04-21 03:23:33 -07003485// Set up a connection initially just using SCTP data channels, later upgrading
3486// to audio/video, ensuring frames are received end-to-end. Effectively the
3487// inverse of the test above.
3488// This was broken in M57; see https://crbug.com/711243
Seth Hampson2f0d7022018-02-20 11:54:42 -08003489TEST_P(PeerConnectionIntegrationTest, SctpDataChannelToAudioVideoUpgrade) {
deadbeef7914b8c2017-04-21 03:23:33 -07003490 ASSERT_TRUE(CreatePeerConnectionWrappers());
3491 ConnectFakeSignaling();
3492 // Do initial offer/answer with just data channel.
3493 caller()->CreateDataChannel();
3494 caller()->CreateAndSetAndSignalOffer();
3495 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3496 // Wait until data can be sent over the data channel.
3497 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3498 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3499 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3500
3501 // Do subsequent offer/answer with two-way audio and video. Audio and video
3502 // should end up bundled on the DTLS/ICE transport already used for data.
Steve Anton15324772018-01-16 10:26:49 -08003503 caller()->AddAudioVideoTracks();
3504 callee()->AddAudioVideoTracks();
deadbeef7914b8c2017-04-21 03:23:33 -07003505 caller()->CreateAndSetAndSignalOffer();
3506 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08003507 MediaExpectations media_expectations;
3508 media_expectations.ExpectBidirectionalAudioAndVideo();
3509 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef7914b8c2017-04-21 03:23:33 -07003510}
3511
deadbeef8b7e9ad2017-05-25 09:38:55 -07003512static void MakeSpecCompliantSctpOffer(cricket::SessionDescription* desc) {
Harald Alvestrand5fc28b12019-05-13 13:36:16 +02003513 cricket::SctpDataContentDescription* dcd_offer =
3514 GetFirstSctpDataContentDescription(desc);
Steve Antonb1c1de12017-12-21 15:14:30 -08003515 ASSERT_TRUE(dcd_offer);
deadbeef8b7e9ad2017-05-25 09:38:55 -07003516 dcd_offer->set_use_sctpmap(false);
3517 dcd_offer->set_protocol("UDP/DTLS/SCTP");
3518}
3519
3520// Test that the data channel works when a spec-compliant SCTP m= section is
3521// offered (using "a=sctp-port" instead of "a=sctpmap", and using
3522// "UDP/DTLS/SCTP" as the protocol).
Seth Hampson2f0d7022018-02-20 11:54:42 -08003523TEST_P(PeerConnectionIntegrationTest,
deadbeef8b7e9ad2017-05-25 09:38:55 -07003524 DataChannelWorksWhenSpecCompliantSctpOfferReceived) {
3525 ASSERT_TRUE(CreatePeerConnectionWrappers());
3526 ConnectFakeSignaling();
3527 caller()->CreateDataChannel();
3528 caller()->SetGeneratedSdpMunger(MakeSpecCompliantSctpOffer);
3529 caller()->CreateAndSetAndSignalOffer();
3530 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3531 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3532 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3533 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3534
3535 // Ensure data can be sent in both directions.
3536 std::string data = "hello world";
3537 caller()->data_channel()->Send(DataBuffer(data));
3538 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3539 kDefaultTimeout);
3540 callee()->data_channel()->Send(DataBuffer(data));
3541 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3542 kDefaultTimeout);
3543}
3544
deadbeef1dcb1642017-03-29 21:08:16 -07003545#endif // HAVE_SCTP
3546
Bjorn Mellema2eb0a72018-11-09 10:13:51 -08003547// This test sets up a call between two parties with a media transport data
3548// channel.
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08003549TEST_P(PeerConnectionIntegrationTest, MediaTransportDataChannelEndToEnd) {
3550 PeerConnectionInterface::RTCConfiguration rtc_config;
Piotr (Peter) Slatalab1ae10b2019-03-01 11:14:05 -08003551 rtc_config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyRequire;
3552 rtc_config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08003553 rtc_config.use_media_transport_for_data_channels = true;
3554 rtc_config.enable_dtls_srtp = false; // SDES is required for media transport.
3555 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
3556 rtc_config, rtc_config, loopback_media_transports()->first_factory(),
3557 loopback_media_transports()->second_factory()));
3558 ConnectFakeSignaling();
3559
3560 // Expect that data channel created on caller side will show up for callee as
3561 // well.
3562 caller()->CreateDataChannel();
3563 caller()->CreateAndSetAndSignalOffer();
3564 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3565
3566 // Ensure that the media transport is ready.
3567 loopback_media_transports()->SetState(webrtc::MediaTransportState::kWritable);
3568 loopback_media_transports()->FlushAsyncInvokes();
3569
3570 // Caller data channel should already exist (it created one). Callee data
3571 // channel may not exist yet, since negotiation happens in-band, not in SDP.
3572 ASSERT_NE(nullptr, caller()->data_channel());
3573 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3574 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3575 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3576
3577 // Ensure data can be sent in both directions.
3578 std::string data = "hello world";
3579 caller()->data_channel()->Send(DataBuffer(data));
3580 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3581 kDefaultTimeout);
3582 callee()->data_channel()->Send(DataBuffer(data));
3583 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3584 kDefaultTimeout);
3585}
3586
3587// Ensure that when the callee closes a media transport data channel, the
3588// closing procedure results in the data channel being closed for the caller
3589// as well.
3590TEST_P(PeerConnectionIntegrationTest, MediaTransportDataChannelCalleeCloses) {
3591 PeerConnectionInterface::RTCConfiguration rtc_config;
3592 rtc_config.use_media_transport_for_data_channels = true;
3593 rtc_config.enable_dtls_srtp = false; // SDES is required for media transport.
3594 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
3595 rtc_config, rtc_config, loopback_media_transports()->first_factory(),
3596 loopback_media_transports()->second_factory()));
3597 ConnectFakeSignaling();
3598
3599 // Create a data channel on the caller and signal it to the callee.
3600 caller()->CreateDataChannel();
3601 caller()->CreateAndSetAndSignalOffer();
3602 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3603
3604 // Ensure that the media transport is ready.
3605 loopback_media_transports()->SetState(webrtc::MediaTransportState::kWritable);
3606 loopback_media_transports()->FlushAsyncInvokes();
3607
3608 // Data channels exist and open on both ends of the connection.
3609 ASSERT_NE(nullptr, caller()->data_channel());
3610 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3611 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3612 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3613
3614 // Close the data channel on the callee side, and wait for it to reach the
3615 // "closed" state on both sides.
3616 callee()->data_channel()->Close();
3617 EXPECT_TRUE_WAIT(!caller()->data_observer()->IsOpen(), kDefaultTimeout);
3618 EXPECT_TRUE_WAIT(!callee()->data_observer()->IsOpen(), kDefaultTimeout);
3619}
3620
3621TEST_P(PeerConnectionIntegrationTest,
3622 MediaTransportDataChannelConfigSentToOtherSide) {
3623 PeerConnectionInterface::RTCConfiguration rtc_config;
3624 rtc_config.use_media_transport_for_data_channels = true;
3625 rtc_config.enable_dtls_srtp = false; // SDES is required for media transport.
3626 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
3627 rtc_config, rtc_config, loopback_media_transports()->first_factory(),
3628 loopback_media_transports()->second_factory()));
3629 ConnectFakeSignaling();
3630
3631 // Create a data channel with a non-default configuration and signal it to the
3632 // callee.
3633 webrtc::DataChannelInit init;
3634 init.id = 53;
3635 init.maxRetransmits = 52;
3636 caller()->CreateDataChannel("data-channel", &init);
3637 caller()->CreateAndSetAndSignalOffer();
3638 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3639
3640 // Ensure that the media transport is ready.
3641 loopback_media_transports()->SetState(webrtc::MediaTransportState::kWritable);
3642 loopback_media_transports()->FlushAsyncInvokes();
3643
3644 // Ensure that the data channel exists on the callee with the correct
3645 // configuration.
3646 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3647 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
Harald Alvestrand5c4d2ee2019-04-01 12:58:15 +02003648 // Since "negotiate" is false, the "id" parameter is ignored.
3649 EXPECT_NE(init.id, callee()->data_channel()->id());
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08003650 EXPECT_EQ("data-channel", callee()->data_channel()->label());
3651 EXPECT_EQ(init.maxRetransmits, callee()->data_channel()->maxRetransmits());
3652 EXPECT_FALSE(callee()->data_channel()->negotiated());
3653}
3654
Piotr (Peter) Slatalab1ae10b2019-03-01 11:14:05 -08003655TEST_P(PeerConnectionIntegrationTest, MediaTransportOfferUpgrade) {
3656 PeerConnectionInterface::RTCConfiguration rtc_config;
3657 rtc_config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyRequire;
3658 rtc_config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
3659 rtc_config.use_media_transport = true;
3660 rtc_config.enable_dtls_srtp = false; // SDES is required for media transport.
3661 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
3662 rtc_config, rtc_config, loopback_media_transports()->first_factory(),
3663 loopback_media_transports()->second_factory()));
3664 ConnectFakeSignaling();
3665
3666 // Do initial offer/answer with just a video track.
3667 caller()->AddVideoTrack();
3668 callee()->AddVideoTrack();
3669 caller()->CreateAndSetAndSignalOffer();
3670 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3671
3672 // Ensure that the media transport is ready.
3673 loopback_media_transports()->SetState(webrtc::MediaTransportState::kWritable);
3674 loopback_media_transports()->FlushAsyncInvokes();
3675
3676 // Now add an audio track and do another offer/answer.
3677 caller()->AddAudioTrack();
3678 callee()->AddAudioTrack();
3679 caller()->CreateAndSetAndSignalOffer();
3680 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3681
3682 // Ensure both audio and video frames are received end-to-end.
3683 MediaExpectations media_expectations;
3684 media_expectations.ExpectBidirectionalAudioAndVideo();
3685 ASSERT_TRUE(ExpectNewFrames(media_expectations));
3686
3687 // The second offer should not have generated another media transport.
3688 // Media transport was kept alive, and was not recreated.
3689 EXPECT_EQ(1, loopback_media_transports()->first_factory_transport_count());
3690 EXPECT_EQ(1, loopback_media_transports()->second_factory_transport_count());
3691}
3692
3693TEST_P(PeerConnectionIntegrationTest, MediaTransportOfferUpgradeOnTheCallee) {
3694 PeerConnectionInterface::RTCConfiguration rtc_config;
3695 rtc_config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyRequire;
3696 rtc_config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
3697 rtc_config.use_media_transport = true;
3698 rtc_config.enable_dtls_srtp = false; // SDES is required for media transport.
3699 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
3700 rtc_config, rtc_config, loopback_media_transports()->first_factory(),
3701 loopback_media_transports()->second_factory()));
3702 ConnectFakeSignaling();
3703
3704 // Do initial offer/answer with just a video track.
3705 caller()->AddVideoTrack();
3706 callee()->AddVideoTrack();
3707 caller()->CreateAndSetAndSignalOffer();
3708 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3709
3710 // Ensure that the media transport is ready.
3711 loopback_media_transports()->SetState(webrtc::MediaTransportState::kWritable);
3712 loopback_media_transports()->FlushAsyncInvokes();
3713
3714 // Now add an audio track and do another offer/answer.
3715 caller()->AddAudioTrack();
3716 callee()->AddAudioTrack();
3717 callee()->CreateAndSetAndSignalOffer();
3718 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3719
3720 // Ensure both audio and video frames are received end-to-end.
3721 MediaExpectations media_expectations;
3722 media_expectations.ExpectBidirectionalAudioAndVideo();
3723 ASSERT_TRUE(ExpectNewFrames(media_expectations));
3724
3725 // The second offer should not have generated another media transport.
3726 // Media transport was kept alive, and was not recreated.
3727 EXPECT_EQ(1, loopback_media_transports()->first_factory_transport_count());
3728 EXPECT_EQ(1, loopback_media_transports()->second_factory_transport_count());
3729}
3730
Niels Möllerc68d2822018-11-20 14:52:05 +01003731TEST_P(PeerConnectionIntegrationTest, MediaTransportBidirectionalAudio) {
3732 PeerConnectionInterface::RTCConfiguration rtc_config;
Piotr (Peter) Slatalab1ae10b2019-03-01 11:14:05 -08003733 rtc_config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyRequire;
3734 rtc_config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
Niels Möllerc68d2822018-11-20 14:52:05 +01003735 rtc_config.use_media_transport = true;
3736 rtc_config.enable_dtls_srtp = false; // SDES is required for media transport.
3737 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
3738 rtc_config, rtc_config, loopback_media_transports()->first_factory(),
3739 loopback_media_transports()->second_factory()));
3740 ConnectFakeSignaling();
3741
3742 caller()->AddAudioTrack();
3743 callee()->AddAudioTrack();
3744 // Start offer/answer exchange and wait for it to complete.
3745 caller()->CreateAndSetAndSignalOffer();
3746 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3747
3748 // Ensure that the media transport is ready.
3749 loopback_media_transports()->SetState(webrtc::MediaTransportState::kWritable);
3750 loopback_media_transports()->FlushAsyncInvokes();
3751
3752 MediaExpectations media_expectations;
3753 media_expectations.ExpectBidirectionalAudio();
3754 ASSERT_TRUE(ExpectNewFrames(media_expectations));
3755
3756 webrtc::MediaTransportPair::Stats first_stats =
3757 loopback_media_transports()->FirstStats();
3758 webrtc::MediaTransportPair::Stats second_stats =
3759 loopback_media_transports()->SecondStats();
3760
3761 EXPECT_GT(first_stats.received_audio_frames, 0);
3762 EXPECT_GE(second_stats.sent_audio_frames, first_stats.received_audio_frames);
3763
3764 EXPECT_GT(second_stats.received_audio_frames, 0);
3765 EXPECT_GE(first_stats.sent_audio_frames, second_stats.received_audio_frames);
3766}
3767
Niels Möller46879152019-01-07 15:54:47 +01003768TEST_P(PeerConnectionIntegrationTest, MediaTransportBidirectionalVideo) {
3769 PeerConnectionInterface::RTCConfiguration rtc_config;
3770 rtc_config.use_media_transport = true;
3771 rtc_config.enable_dtls_srtp = false; // SDES is required for media transport.
3772 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
3773 rtc_config, rtc_config, loopback_media_transports()->first_factory(),
3774 loopback_media_transports()->second_factory()));
3775 ConnectFakeSignaling();
3776
3777 caller()->AddVideoTrack();
3778 callee()->AddVideoTrack();
3779 // Start offer/answer exchange and wait for it to complete.
3780 caller()->CreateAndSetAndSignalOffer();
3781 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3782
3783 // Ensure that the media transport is ready.
3784 loopback_media_transports()->SetState(webrtc::MediaTransportState::kWritable);
3785 loopback_media_transports()->FlushAsyncInvokes();
3786
3787 MediaExpectations media_expectations;
3788 media_expectations.ExpectBidirectionalVideo();
3789 ASSERT_TRUE(ExpectNewFrames(media_expectations));
3790
3791 webrtc::MediaTransportPair::Stats first_stats =
3792 loopback_media_transports()->FirstStats();
3793 webrtc::MediaTransportPair::Stats second_stats =
3794 loopback_media_transports()->SecondStats();
3795
3796 EXPECT_GT(first_stats.received_video_frames, 0);
3797 EXPECT_GE(second_stats.sent_video_frames, first_stats.received_video_frames);
3798
3799 EXPECT_GT(second_stats.received_video_frames, 0);
3800 EXPECT_GE(first_stats.sent_video_frames, second_stats.received_video_frames);
3801}
3802
Piotr (Peter) Slatala55b91b92019-01-25 13:31:15 -08003803TEST_P(PeerConnectionIntegrationTest,
3804 MediaTransportDataChannelUsesRtpBidirectionalVideo) {
3805 PeerConnectionInterface::RTCConfiguration rtc_config;
3806 rtc_config.use_media_transport = false;
3807 rtc_config.use_media_transport_for_data_channels = true;
3808 rtc_config.enable_dtls_srtp = false; // SDES is required for media transport.
3809 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
3810 rtc_config, rtc_config, loopback_media_transports()->first_factory(),
3811 loopback_media_transports()->second_factory()));
3812 ConnectFakeSignaling();
3813
3814 caller()->AddVideoTrack();
3815 callee()->AddVideoTrack();
3816 // Start offer/answer exchange and wait for it to complete.
3817 caller()->CreateAndSetAndSignalOffer();
3818 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3819
3820 MediaExpectations media_expectations;
3821 media_expectations.ExpectBidirectionalVideo();
3822 ASSERT_TRUE(ExpectNewFrames(media_expectations));
3823}
3824
deadbeef1dcb1642017-03-29 21:08:16 -07003825// Test that the ICE connection and gathering states eventually reach
3826// "complete".
Seth Hampson2f0d7022018-02-20 11:54:42 -08003827TEST_P(PeerConnectionIntegrationTest, IceStatesReachCompletion) {
deadbeef1dcb1642017-03-29 21:08:16 -07003828 ASSERT_TRUE(CreatePeerConnectionWrappers());
3829 ConnectFakeSignaling();
3830 // Do normal offer/answer.
Steve Anton15324772018-01-16 10:26:49 -08003831 caller()->AddAudioVideoTracks();
3832 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003833 caller()->CreateAndSetAndSignalOffer();
3834 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3835 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceGatheringComplete,
3836 caller()->ice_gathering_state(), kMaxWaitForFramesMs);
3837 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceGatheringComplete,
3838 callee()->ice_gathering_state(), kMaxWaitForFramesMs);
3839 // After the best candidate pair is selected and all candidates are signaled,
3840 // the ICE connection state should reach "complete".
3841 // TODO(deadbeef): Currently, the ICE "controlled" agent (the
3842 // answerer/"callee" by default) only reaches "connected". When this is
3843 // fixed, this test should be updated.
3844 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
3845 caller()->ice_connection_state(), kDefaultTimeout);
Alex Loiko9289eda2018-11-23 16:18:59 +00003846 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
3847 callee()->ice_connection_state(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07003848}
3849
Qingsi Wang1dac6d82018-12-12 15:28:47 -08003850constexpr int kOnlyLocalPorts = cricket::PORTALLOCATOR_DISABLE_STUN |
3851 cricket::PORTALLOCATOR_DISABLE_RELAY |
3852 cricket::PORTALLOCATOR_DISABLE_TCP;
Zach Stein6fcdc2f2018-08-23 16:25:55 -07003853
Qingsi Wang1dac6d82018-12-12 15:28:47 -08003854// Use a mock resolver to resolve the hostname back to the original IP on both
3855// sides and check that the ICE connection connects.
Zach Stein6fcdc2f2018-08-23 16:25:55 -07003856TEST_P(PeerConnectionIntegrationTest,
3857 IceStatesReachCompletionWithRemoteHostname) {
Qingsi Wang1dac6d82018-12-12 15:28:47 -08003858 auto caller_resolver_factory =
3859 absl::make_unique<NiceMock<webrtc::MockAsyncResolverFactory>>();
3860 auto callee_resolver_factory =
3861 absl::make_unique<NiceMock<webrtc::MockAsyncResolverFactory>>();
3862 NiceMock<rtc::MockAsyncResolver> callee_async_resolver;
3863 NiceMock<rtc::MockAsyncResolver> caller_async_resolver;
Zach Stein6fcdc2f2018-08-23 16:25:55 -07003864
3865 // This also verifies that the injected AsyncResolverFactory is used by
3866 // P2PTransportChannel.
Qingsi Wang1dac6d82018-12-12 15:28:47 -08003867 EXPECT_CALL(*caller_resolver_factory, Create())
3868 .WillOnce(Return(&caller_async_resolver));
3869 webrtc::PeerConnectionDependencies caller_deps(nullptr);
3870 caller_deps.async_resolver_factory = std::move(caller_resolver_factory);
3871
3872 EXPECT_CALL(*callee_resolver_factory, Create())
3873 .WillOnce(Return(&callee_async_resolver));
3874 webrtc::PeerConnectionDependencies callee_deps(nullptr);
3875 callee_deps.async_resolver_factory = std::move(callee_resolver_factory);
3876
3877 PeerConnectionInterface::RTCConfiguration config;
3878 config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
3879 config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyRequire;
3880
3881 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndDeps(
3882 config, std::move(caller_deps), config, std::move(callee_deps)));
3883
3884 caller()->SetRemoteAsyncResolver(&callee_async_resolver);
3885 callee()->SetRemoteAsyncResolver(&caller_async_resolver);
3886
3887 // Enable hostname candidates with mDNS names.
Qingsi Wangecd30542019-05-22 14:34:56 -07003888 caller()->SetMdnsResponder(
3889 absl::make_unique<webrtc::FakeMdnsResponder>(network_thread()));
3890 callee()->SetMdnsResponder(
3891 absl::make_unique<webrtc::FakeMdnsResponder>(network_thread()));
Qingsi Wang1dac6d82018-12-12 15:28:47 -08003892
3893 SetPortAllocatorFlags(kOnlyLocalPorts, kOnlyLocalPorts);
Zach Stein6fcdc2f2018-08-23 16:25:55 -07003894
3895 ConnectFakeSignaling();
3896 caller()->AddAudioVideoTracks();
3897 callee()->AddAudioVideoTracks();
3898 caller()->CreateAndSetAndSignalOffer();
3899 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3900 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
3901 caller()->ice_connection_state(), kDefaultTimeout);
3902 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
3903 callee()->ice_connection_state(), kDefaultTimeout);
Jeroen de Borst833979f2018-12-13 08:25:54 -08003904
3905 EXPECT_EQ(1, webrtc::metrics::NumEvents(
3906 "WebRTC.PeerConnection.CandidatePairType_UDP",
3907 webrtc::kIceCandidatePairHostNameHostName));
Zach Stein6fcdc2f2018-08-23 16:25:55 -07003908}
3909
Steve Antonede9ca52017-10-16 13:04:27 -07003910// Test that firewalling the ICE connection causes the clients to identify the
3911// disconnected state and then removing the firewall causes them to reconnect.
3912class PeerConnectionIntegrationIceStatesTest
Seth Hampson2f0d7022018-02-20 11:54:42 -08003913 : public PeerConnectionIntegrationBaseTest,
3914 public ::testing::WithParamInterface<
3915 std::tuple<SdpSemantics, std::tuple<std::string, uint32_t>>> {
Steve Antonede9ca52017-10-16 13:04:27 -07003916 protected:
Seth Hampson2f0d7022018-02-20 11:54:42 -08003917 PeerConnectionIntegrationIceStatesTest()
3918 : PeerConnectionIntegrationBaseTest(std::get<0>(GetParam())) {
3919 port_allocator_flags_ = std::get<1>(std::get<1>(GetParam()));
Steve Antonede9ca52017-10-16 13:04:27 -07003920 }
3921
3922 void StartStunServer(const SocketAddress& server_address) {
3923 stun_server_.reset(
3924 cricket::TestStunServer::Create(network_thread(), server_address));
3925 }
3926
3927 bool TestIPv6() {
3928 return (port_allocator_flags_ & cricket::PORTALLOCATOR_ENABLE_IPV6);
3929 }
3930
3931 void SetPortAllocatorFlags() {
Qingsi Wang1dac6d82018-12-12 15:28:47 -08003932 PeerConnectionIntegrationBaseTest::SetPortAllocatorFlags(
3933 port_allocator_flags_, port_allocator_flags_);
Steve Antonede9ca52017-10-16 13:04:27 -07003934 }
3935
3936 std::vector<SocketAddress> CallerAddresses() {
3937 std::vector<SocketAddress> addresses;
3938 addresses.push_back(SocketAddress("1.1.1.1", 0));
3939 if (TestIPv6()) {
3940 addresses.push_back(SocketAddress("1111:0:a:b:c:d:e:f", 0));
3941 }
3942 return addresses;
3943 }
3944
3945 std::vector<SocketAddress> CalleeAddresses() {
3946 std::vector<SocketAddress> addresses;
3947 addresses.push_back(SocketAddress("2.2.2.2", 0));
3948 if (TestIPv6()) {
3949 addresses.push_back(SocketAddress("2222:0:a:b:c:d:e:f", 0));
3950 }
3951 return addresses;
3952 }
3953
3954 void SetUpNetworkInterfaces() {
3955 // Remove the default interfaces added by the test infrastructure.
Qingsi Wangecd30542019-05-22 14:34:56 -07003956 caller()->network_manager()->RemoveInterface(kDefaultLocalAddress);
3957 callee()->network_manager()->RemoveInterface(kDefaultLocalAddress);
Steve Antonede9ca52017-10-16 13:04:27 -07003958
3959 // Add network addresses for test.
3960 for (const auto& caller_address : CallerAddresses()) {
Qingsi Wangecd30542019-05-22 14:34:56 -07003961 caller()->network_manager()->AddInterface(caller_address);
Steve Antonede9ca52017-10-16 13:04:27 -07003962 }
3963 for (const auto& callee_address : CalleeAddresses()) {
Qingsi Wangecd30542019-05-22 14:34:56 -07003964 callee()->network_manager()->AddInterface(callee_address);
Steve Antonede9ca52017-10-16 13:04:27 -07003965 }
3966 }
3967
3968 private:
3969 uint32_t port_allocator_flags_;
3970 std::unique_ptr<cricket::TestStunServer> stun_server_;
3971};
3972
3973// Tests that the PeerConnection goes through all the ICE gathering/connection
3974// states over the duration of the call. This includes Disconnected and Failed
3975// states, induced by putting a firewall between the peers and waiting for them
3976// to time out.
Steve Anton83119dd2017-11-10 16:19:52 -08003977TEST_P(PeerConnectionIntegrationIceStatesTest, VerifyIceStates) {
Jonas Olssonb75d9e92019-02-22 10:33:29 +01003978 rtc::ScopedFakeClock fake_clock;
3979 // Some things use a time of "0" as a special value, so we need to start out
3980 // the fake clock at a nonzero time.
3981 fake_clock.AdvanceTime(TimeDelta::seconds(1));
Steve Antonede9ca52017-10-16 13:04:27 -07003982
3983 const SocketAddress kStunServerAddress =
3984 SocketAddress("99.99.99.1", cricket::STUN_SERVER_PORT);
3985 StartStunServer(kStunServerAddress);
3986
3987 PeerConnectionInterface::RTCConfiguration config;
3988 PeerConnectionInterface::IceServer ice_stun_server;
3989 ice_stun_server.urls.push_back(
3990 "stun:" + kStunServerAddress.HostAsURIString() + ":" +
3991 kStunServerAddress.PortAsString());
3992 config.servers.push_back(ice_stun_server);
3993
3994 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
3995 ConnectFakeSignaling();
3996 SetPortAllocatorFlags();
3997 SetUpNetworkInterfaces();
Steve Anton15324772018-01-16 10:26:49 -08003998 caller()->AddAudioVideoTracks();
3999 callee()->AddAudioVideoTracks();
Steve Antonede9ca52017-10-16 13:04:27 -07004000
4001 // Initial state before anything happens.
4002 ASSERT_EQ(PeerConnectionInterface::kIceGatheringNew,
4003 caller()->ice_gathering_state());
4004 ASSERT_EQ(PeerConnectionInterface::kIceConnectionNew,
4005 caller()->ice_connection_state());
Jonas Olsson7a6739e2019-01-15 16:31:55 +01004006 ASSERT_EQ(PeerConnectionInterface::kIceConnectionNew,
4007 caller()->standardized_ice_connection_state());
Steve Antonede9ca52017-10-16 13:04:27 -07004008
4009 // Start the call by creating the offer, setting it as the local description,
4010 // then sending it to the peer who will respond with an answer. This happens
4011 // asynchronously so that we can watch the states as it runs in the
4012 // background.
4013 caller()->CreateAndSetAndSignalOffer();
4014
Jonas Olsson7a6739e2019-01-15 16:31:55 +01004015 ASSERT_EQ(PeerConnectionInterface::kIceConnectionCompleted,
4016 caller()->ice_connection_state());
Jonas Olssonacd8ae72019-02-25 15:26:24 +01004017 ASSERT_EQ(PeerConnectionInterface::kIceConnectionCompleted,
Jonas Olsson7a6739e2019-01-15 16:31:55 +01004018 caller()->standardized_ice_connection_state());
Steve Antonede9ca52017-10-16 13:04:27 -07004019
4020 // Verify that the observer was notified of the intermediate transitions.
4021 EXPECT_THAT(caller()->ice_connection_state_history(),
4022 ElementsAre(PeerConnectionInterface::kIceConnectionChecking,
4023 PeerConnectionInterface::kIceConnectionConnected,
4024 PeerConnectionInterface::kIceConnectionCompleted));
Jonas Olssonacd8ae72019-02-25 15:26:24 +01004025 EXPECT_THAT(caller()->standardized_ice_connection_state_history(),
4026 ElementsAre(PeerConnectionInterface::kIceConnectionChecking,
4027 PeerConnectionInterface::kIceConnectionConnected,
4028 PeerConnectionInterface::kIceConnectionCompleted));
Jonas Olsson635474e2018-10-18 15:58:17 +02004029 EXPECT_THAT(
4030 caller()->peer_connection_state_history(),
4031 ElementsAre(PeerConnectionInterface::PeerConnectionState::kConnecting,
Jonas Olsson635474e2018-10-18 15:58:17 +02004032 PeerConnectionInterface::PeerConnectionState::kConnected));
Steve Antonede9ca52017-10-16 13:04:27 -07004033 EXPECT_THAT(caller()->ice_gathering_state_history(),
4034 ElementsAre(PeerConnectionInterface::kIceGatheringGathering,
4035 PeerConnectionInterface::kIceGatheringComplete));
4036
4037 // Block connections to/from the caller and wait for ICE to become
4038 // disconnected.
4039 for (const auto& caller_address : CallerAddresses()) {
4040 firewall()->AddRule(false, rtc::FP_ANY, rtc::FD_ANY, caller_address);
4041 }
Mirko Bonadei675513b2017-11-09 11:09:25 +01004042 RTC_LOG(LS_INFO) << "Firewall rules applied";
Jonas Olssonb75d9e92019-02-22 10:33:29 +01004043 ASSERT_EQ_SIMULATED_WAIT(PeerConnectionInterface::kIceConnectionDisconnected,
4044 caller()->ice_connection_state(), kDefaultTimeout,
4045 fake_clock);
4046 ASSERT_EQ_SIMULATED_WAIT(PeerConnectionInterface::kIceConnectionDisconnected,
4047 caller()->standardized_ice_connection_state(),
4048 kDefaultTimeout, fake_clock);
Steve Antonede9ca52017-10-16 13:04:27 -07004049
4050 // Let ICE re-establish by removing the firewall rules.
4051 firewall()->ClearRules();
Mirko Bonadei675513b2017-11-09 11:09:25 +01004052 RTC_LOG(LS_INFO) << "Firewall rules cleared";
Jonas Olssonb75d9e92019-02-22 10:33:29 +01004053 ASSERT_EQ_SIMULATED_WAIT(PeerConnectionInterface::kIceConnectionCompleted,
4054 caller()->ice_connection_state(), kDefaultTimeout,
4055 fake_clock);
Jonas Olssonacd8ae72019-02-25 15:26:24 +01004056 ASSERT_EQ_SIMULATED_WAIT(PeerConnectionInterface::kIceConnectionCompleted,
Jonas Olssonb75d9e92019-02-22 10:33:29 +01004057 caller()->standardized_ice_connection_state(),
4058 kDefaultTimeout, fake_clock);
Steve Antonede9ca52017-10-16 13:04:27 -07004059
4060 // According to RFC7675, if there is no response within 30 seconds then the
4061 // peer should consider the other side to have rejected the connection. This
Steve Anton83119dd2017-11-10 16:19:52 -08004062 // is signaled by the state transitioning to "failed".
Steve Antonede9ca52017-10-16 13:04:27 -07004063 constexpr int kConsentTimeout = 30000;
4064 for (const auto& caller_address : CallerAddresses()) {
4065 firewall()->AddRule(false, rtc::FP_ANY, rtc::FD_ANY, caller_address);
4066 }
Mirko Bonadei675513b2017-11-09 11:09:25 +01004067 RTC_LOG(LS_INFO) << "Firewall rules applied again";
Jonas Olssonb75d9e92019-02-22 10:33:29 +01004068 ASSERT_EQ_SIMULATED_WAIT(PeerConnectionInterface::kIceConnectionFailed,
4069 caller()->ice_connection_state(), kConsentTimeout,
4070 fake_clock);
4071 ASSERT_EQ_SIMULATED_WAIT(PeerConnectionInterface::kIceConnectionFailed,
4072 caller()->standardized_ice_connection_state(),
4073 kConsentTimeout, fake_clock);
4074
4075 // We need to manually close the peerconnections before the fake clock goes
4076 // out of scope, or we trigger a DCHECK in rtp_sender.cc when we briefly
4077 // return to using non-faked time.
4078 delete SetCallerPcWrapperAndReturnCurrent(nullptr);
4079 delete SetCalleePcWrapperAndReturnCurrent(nullptr);
4080}
4081
4082// Tests that if the connection doesn't get set up properly we eventually reach
4083// the "failed" iceConnectionState.
4084TEST_P(PeerConnectionIntegrationIceStatesTest, IceStateSetupFailure) {
4085 rtc::ScopedFakeClock fake_clock;
4086 // Some things use a time of "0" as a special value, so we need to start out
4087 // the fake clock at a nonzero time.
4088 fake_clock.AdvanceTime(TimeDelta::seconds(1));
4089
4090 // Block connections to/from the caller and wait for ICE to become
4091 // disconnected.
4092 for (const auto& caller_address : CallerAddresses()) {
4093 firewall()->AddRule(false, rtc::FP_ANY, rtc::FD_ANY, caller_address);
4094 }
4095
4096 ASSERT_TRUE(CreatePeerConnectionWrappers());
4097 ConnectFakeSignaling();
4098 SetPortAllocatorFlags();
4099 SetUpNetworkInterfaces();
4100 caller()->AddAudioVideoTracks();
4101 caller()->CreateAndSetAndSignalOffer();
4102
4103 // According to RFC7675, if there is no response within 30 seconds then the
4104 // peer should consider the other side to have rejected the connection. This
4105 // is signaled by the state transitioning to "failed".
4106 constexpr int kConsentTimeout = 30000;
4107 ASSERT_EQ_SIMULATED_WAIT(PeerConnectionInterface::kIceConnectionFailed,
4108 caller()->standardized_ice_connection_state(),
4109 kConsentTimeout, fake_clock);
4110
4111 // We need to manually close the peerconnections before the fake clock goes
4112 // out of scope, or we trigger a DCHECK in rtp_sender.cc when we briefly
4113 // return to using non-faked time.
4114 delete SetCallerPcWrapperAndReturnCurrent(nullptr);
4115 delete SetCalleePcWrapperAndReturnCurrent(nullptr);
Steve Antonede9ca52017-10-16 13:04:27 -07004116}
4117
4118// Tests that the best connection is set to the appropriate IPv4/IPv6 connection
4119// and that the statistics in the metric observers are updated correctly.
4120TEST_P(PeerConnectionIntegrationIceStatesTest, VerifyBestConnection) {
4121 ASSERT_TRUE(CreatePeerConnectionWrappers());
4122 ConnectFakeSignaling();
4123 SetPortAllocatorFlags();
4124 SetUpNetworkInterfaces();
Steve Anton15324772018-01-16 10:26:49 -08004125 caller()->AddAudioVideoTracks();
4126 callee()->AddAudioVideoTracks();
Steve Antonede9ca52017-10-16 13:04:27 -07004127 caller()->CreateAndSetAndSignalOffer();
4128
4129 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4130
Qingsi Wang7fc821d2018-07-12 12:54:53 -07004131 // TODO(bugs.webrtc.org/9456): Fix it.
4132 const int num_best_ipv4 = webrtc::metrics::NumEvents(
4133 "WebRTC.PeerConnection.IPMetrics", webrtc::kBestConnections_IPv4);
4134 const int num_best_ipv6 = webrtc::metrics::NumEvents(
4135 "WebRTC.PeerConnection.IPMetrics", webrtc::kBestConnections_IPv6);
Steve Antonede9ca52017-10-16 13:04:27 -07004136 if (TestIPv6()) {
4137 // When IPv6 is enabled, we should prefer an IPv6 connection over an IPv4
4138 // connection.
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02004139 EXPECT_EQ(0, num_best_ipv4);
4140 EXPECT_EQ(1, num_best_ipv6);
Steve Antonede9ca52017-10-16 13:04:27 -07004141 } else {
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02004142 EXPECT_EQ(1, num_best_ipv4);
4143 EXPECT_EQ(0, num_best_ipv6);
Steve Antonede9ca52017-10-16 13:04:27 -07004144 }
4145
Qingsi Wang7fc821d2018-07-12 12:54:53 -07004146 EXPECT_EQ(0, webrtc::metrics::NumEvents(
4147 "WebRTC.PeerConnection.CandidatePairType_UDP",
4148 webrtc::kIceCandidatePairHostHost));
4149 EXPECT_EQ(1, webrtc::metrics::NumEvents(
4150 "WebRTC.PeerConnection.CandidatePairType_UDP",
4151 webrtc::kIceCandidatePairHostPublicHostPublic));
Steve Antonede9ca52017-10-16 13:04:27 -07004152}
4153
4154constexpr uint32_t kFlagsIPv4NoStun = cricket::PORTALLOCATOR_DISABLE_TCP |
4155 cricket::PORTALLOCATOR_DISABLE_STUN |
4156 cricket::PORTALLOCATOR_DISABLE_RELAY;
4157constexpr uint32_t kFlagsIPv6NoStun =
4158 cricket::PORTALLOCATOR_DISABLE_TCP | cricket::PORTALLOCATOR_DISABLE_STUN |
4159 cricket::PORTALLOCATOR_ENABLE_IPV6 | cricket::PORTALLOCATOR_DISABLE_RELAY;
4160constexpr uint32_t kFlagsIPv4Stun =
4161 cricket::PORTALLOCATOR_DISABLE_TCP | cricket::PORTALLOCATOR_DISABLE_RELAY;
4162
Mirko Bonadeic84f6612019-01-31 12:20:57 +01004163INSTANTIATE_TEST_SUITE_P(
Seth Hampson2f0d7022018-02-20 11:54:42 -08004164 PeerConnectionIntegrationTest,
4165 PeerConnectionIntegrationIceStatesTest,
4166 Combine(Values(SdpSemantics::kPlanB, SdpSemantics::kUnifiedPlan),
4167 Values(std::make_pair("IPv4 no STUN", kFlagsIPv4NoStun),
4168 std::make_pair("IPv6 no STUN", kFlagsIPv6NoStun),
4169 std::make_pair("IPv4 with STUN", kFlagsIPv4Stun))));
Steve Antonede9ca52017-10-16 13:04:27 -07004170
deadbeef1dcb1642017-03-29 21:08:16 -07004171// This test sets up a call between two parties with audio and video.
4172// During the call, the caller restarts ICE and the test verifies that
4173// new ICE candidates are generated and audio and video still can flow, and the
4174// ICE state reaches completed again.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004175TEST_P(PeerConnectionIntegrationTest, MediaContinuesFlowingAfterIceRestart) {
deadbeef1dcb1642017-03-29 21:08:16 -07004176 ASSERT_TRUE(CreatePeerConnectionWrappers());
4177 ConnectFakeSignaling();
4178 // Do normal offer/answer and wait for ICE to complete.
Steve Anton15324772018-01-16 10:26:49 -08004179 caller()->AddAudioVideoTracks();
4180 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07004181 caller()->CreateAndSetAndSignalOffer();
4182 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4183 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
4184 caller()->ice_connection_state(), kMaxWaitForFramesMs);
Alex Loiko9289eda2018-11-23 16:18:59 +00004185 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
4186 callee()->ice_connection_state(), kMaxWaitForFramesMs);
deadbeef1dcb1642017-03-29 21:08:16 -07004187
4188 // To verify that the ICE restart actually occurs, get
4189 // ufrag/password/candidates before and after restart.
4190 // Create an SDP string of the first audio candidate for both clients.
4191 const webrtc::IceCandidateCollection* audio_candidates_caller =
4192 caller()->pc()->local_description()->candidates(0);
4193 const webrtc::IceCandidateCollection* audio_candidates_callee =
4194 callee()->pc()->local_description()->candidates(0);
4195 ASSERT_GT(audio_candidates_caller->count(), 0u);
4196 ASSERT_GT(audio_candidates_callee->count(), 0u);
4197 std::string caller_candidate_pre_restart;
4198 ASSERT_TRUE(
4199 audio_candidates_caller->at(0)->ToString(&caller_candidate_pre_restart));
4200 std::string callee_candidate_pre_restart;
4201 ASSERT_TRUE(
4202 audio_candidates_callee->at(0)->ToString(&callee_candidate_pre_restart));
4203 const cricket::SessionDescription* desc =
4204 caller()->pc()->local_description()->description();
4205 std::string caller_ufrag_pre_restart =
4206 desc->transport_infos()[0].description.ice_ufrag;
4207 desc = callee()->pc()->local_description()->description();
4208 std::string callee_ufrag_pre_restart =
4209 desc->transport_infos()[0].description.ice_ufrag;
4210
4211 // Have the caller initiate an ICE restart.
4212 caller()->SetOfferAnswerOptions(IceRestartOfferAnswerOptions());
4213 caller()->CreateAndSetAndSignalOffer();
4214 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4215 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
4216 caller()->ice_connection_state(), kMaxWaitForFramesMs);
Alex Loiko9289eda2018-11-23 16:18:59 +00004217 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
deadbeef1dcb1642017-03-29 21:08:16 -07004218 callee()->ice_connection_state(), kMaxWaitForFramesMs);
4219
4220 // Grab the ufrags/candidates again.
4221 audio_candidates_caller = caller()->pc()->local_description()->candidates(0);
4222 audio_candidates_callee = callee()->pc()->local_description()->candidates(0);
4223 ASSERT_GT(audio_candidates_caller->count(), 0u);
4224 ASSERT_GT(audio_candidates_callee->count(), 0u);
4225 std::string caller_candidate_post_restart;
4226 ASSERT_TRUE(
4227 audio_candidates_caller->at(0)->ToString(&caller_candidate_post_restart));
4228 std::string callee_candidate_post_restart;
4229 ASSERT_TRUE(
4230 audio_candidates_callee->at(0)->ToString(&callee_candidate_post_restart));
4231 desc = caller()->pc()->local_description()->description();
4232 std::string caller_ufrag_post_restart =
4233 desc->transport_infos()[0].description.ice_ufrag;
4234 desc = callee()->pc()->local_description()->description();
4235 std::string callee_ufrag_post_restart =
4236 desc->transport_infos()[0].description.ice_ufrag;
4237 // Sanity check that an ICE restart was actually negotiated in SDP.
4238 ASSERT_NE(caller_candidate_pre_restart, caller_candidate_post_restart);
4239 ASSERT_NE(callee_candidate_pre_restart, callee_candidate_post_restart);
4240 ASSERT_NE(caller_ufrag_pre_restart, caller_ufrag_post_restart);
4241 ASSERT_NE(callee_ufrag_pre_restart, callee_ufrag_post_restart);
4242
4243 // Ensure that additional frames are received after the ICE restart.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004244 MediaExpectations media_expectations;
4245 media_expectations.ExpectBidirectionalAudioAndVideo();
4246 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07004247}
4248
4249// Verify that audio/video can be received end-to-end when ICE renomination is
4250// enabled.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004251TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithIceRenomination) {
deadbeef1dcb1642017-03-29 21:08:16 -07004252 PeerConnectionInterface::RTCConfiguration config;
4253 config.enable_ice_renomination = true;
4254 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
4255 ConnectFakeSignaling();
4256 // Do normal offer/answer and wait for some frames to be received in each
4257 // direction.
Steve Anton15324772018-01-16 10:26:49 -08004258 caller()->AddAudioVideoTracks();
4259 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07004260 caller()->CreateAndSetAndSignalOffer();
4261 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4262 // Sanity check that ICE renomination was actually negotiated.
4263 const cricket::SessionDescription* desc =
4264 caller()->pc()->local_description()->description();
4265 for (const cricket::TransportInfo& info : desc->transport_infos()) {
Steve Anton64b626b2019-01-28 17:25:26 -08004266 ASSERT_THAT(info.description.transport_options, Contains("renomination"));
deadbeef1dcb1642017-03-29 21:08:16 -07004267 }
4268 desc = callee()->pc()->local_description()->description();
4269 for (const cricket::TransportInfo& info : desc->transport_infos()) {
Steve Anton64b626b2019-01-28 17:25:26 -08004270 ASSERT_THAT(info.description.transport_options, Contains("renomination"));
deadbeef1dcb1642017-03-29 21:08:16 -07004271 }
Seth Hampson2f0d7022018-02-20 11:54:42 -08004272 MediaExpectations media_expectations;
4273 media_expectations.ExpectBidirectionalAudioAndVideo();
4274 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07004275}
4276
Steve Anton6f25b092017-10-23 09:39:20 -07004277// With a max bundle policy and RTCP muxing, adding a new media description to
4278// the connection should not affect ICE at all because the new media will use
4279// the existing connection.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004280TEST_P(PeerConnectionIntegrationTest,
Steve Anton83119dd2017-11-10 16:19:52 -08004281 AddMediaToConnectedBundleDoesNotRestartIce) {
Steve Anton6f25b092017-10-23 09:39:20 -07004282 PeerConnectionInterface::RTCConfiguration config;
4283 config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
4284 config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyRequire;
4285 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(
4286 config, PeerConnectionInterface::RTCConfiguration()));
4287 ConnectFakeSignaling();
4288
Steve Anton15324772018-01-16 10:26:49 -08004289 caller()->AddAudioTrack();
Steve Anton6f25b092017-10-23 09:39:20 -07004290 caller()->CreateAndSetAndSignalOffer();
4291 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Steve Antonff52f1b2017-10-26 12:24:50 -07004292 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceConnectionCompleted,
4293 caller()->ice_connection_state(), kDefaultTimeout);
Steve Anton6f25b092017-10-23 09:39:20 -07004294
4295 caller()->clear_ice_connection_state_history();
4296
Steve Anton15324772018-01-16 10:26:49 -08004297 caller()->AddVideoTrack();
Steve Anton6f25b092017-10-23 09:39:20 -07004298 caller()->CreateAndSetAndSignalOffer();
4299 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4300
4301 EXPECT_EQ(0u, caller()->ice_connection_state_history().size());
4302}
4303
deadbeef1dcb1642017-03-29 21:08:16 -07004304// This test sets up a call between two parties with audio and video. It then
4305// renegotiates setting the video m-line to "port 0", then later renegotiates
4306// again, enabling video.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004307TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07004308 VideoFlowsAfterMediaSectionIsRejectedAndRecycled) {
4309 ASSERT_TRUE(CreatePeerConnectionWrappers());
4310 ConnectFakeSignaling();
4311
4312 // Do initial negotiation, only sending media from the caller. Will result in
4313 // video and audio recvonly "m=" sections.
Steve Anton15324772018-01-16 10:26:49 -08004314 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07004315 caller()->CreateAndSetAndSignalOffer();
4316 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4317
4318 // Negotiate again, disabling the video "m=" section (the callee will set the
4319 // port to 0 due to offer_to_receive_video = 0).
Seth Hampson2f0d7022018-02-20 11:54:42 -08004320 if (sdp_semantics_ == SdpSemantics::kPlanB) {
4321 PeerConnectionInterface::RTCOfferAnswerOptions options;
4322 options.offer_to_receive_video = 0;
4323 callee()->SetOfferAnswerOptions(options);
4324 } else {
4325 callee()->SetRemoteOfferHandler([this] {
4326 callee()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)->Stop();
4327 });
4328 }
deadbeef1dcb1642017-03-29 21:08:16 -07004329 caller()->CreateAndSetAndSignalOffer();
4330 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4331 // Sanity check that video "m=" section was actually rejected.
4332 const ContentInfo* answer_video_content = cricket::GetFirstVideoContent(
4333 callee()->pc()->local_description()->description());
4334 ASSERT_NE(nullptr, answer_video_content);
4335 ASSERT_TRUE(answer_video_content->rejected);
4336
4337 // Enable video and do negotiation again, making sure video is received
4338 // end-to-end, also adding media stream to callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004339 if (sdp_semantics_ == SdpSemantics::kPlanB) {
4340 PeerConnectionInterface::RTCOfferAnswerOptions options;
4341 options.offer_to_receive_video = 1;
4342 callee()->SetOfferAnswerOptions(options);
4343 } else {
4344 // The caller's transceiver is stopped, so we need to add another track.
4345 auto caller_transceiver =
4346 caller()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO);
4347 EXPECT_TRUE(caller_transceiver->stopped());
4348 caller()->AddVideoTrack();
4349 }
4350 callee()->AddVideoTrack();
4351 callee()->SetRemoteOfferHandler(nullptr);
deadbeef1dcb1642017-03-29 21:08:16 -07004352 caller()->CreateAndSetAndSignalOffer();
4353 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08004354
deadbeef1dcb1642017-03-29 21:08:16 -07004355 // Verify the caller receives frames from the newly added stream, and the
4356 // callee receives additional frames from the re-enabled video m= section.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004357 MediaExpectations media_expectations;
4358 media_expectations.CalleeExpectsSomeAudio();
4359 media_expectations.ExpectBidirectionalVideo();
4360 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07004361}
4362
deadbeef1dcb1642017-03-29 21:08:16 -07004363// This tests that if we negotiate after calling CreateSender but before we
4364// have a track, then set a track later, frames from the newly-set track are
4365// received end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004366TEST_F(PeerConnectionIntegrationTestPlanB,
deadbeef1dcb1642017-03-29 21:08:16 -07004367 MediaFlowsAfterEarlyWarmupWithCreateSender) {
4368 ASSERT_TRUE(CreatePeerConnectionWrappers());
4369 ConnectFakeSignaling();
4370 auto caller_audio_sender =
4371 caller()->pc()->CreateSender("audio", "caller_stream");
4372 auto caller_video_sender =
4373 caller()->pc()->CreateSender("video", "caller_stream");
4374 auto callee_audio_sender =
4375 callee()->pc()->CreateSender("audio", "callee_stream");
4376 auto callee_video_sender =
4377 callee()->pc()->CreateSender("video", "callee_stream");
4378 caller()->CreateAndSetAndSignalOffer();
4379 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
4380 // Wait for ICE to complete, without any tracks being set.
4381 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
4382 caller()->ice_connection_state(), kMaxWaitForFramesMs);
4383 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
4384 callee()->ice_connection_state(), kMaxWaitForFramesMs);
4385 // Now set the tracks, and expect frames to immediately start flowing.
4386 EXPECT_TRUE(caller_audio_sender->SetTrack(caller()->CreateLocalAudioTrack()));
4387 EXPECT_TRUE(caller_video_sender->SetTrack(caller()->CreateLocalVideoTrack()));
4388 EXPECT_TRUE(callee_audio_sender->SetTrack(callee()->CreateLocalAudioTrack()));
4389 EXPECT_TRUE(callee_video_sender->SetTrack(callee()->CreateLocalVideoTrack()));
Seth Hampson2f0d7022018-02-20 11:54:42 -08004390 MediaExpectations media_expectations;
4391 media_expectations.ExpectBidirectionalAudioAndVideo();
4392 ASSERT_TRUE(ExpectNewFrames(media_expectations));
4393}
4394
4395// This tests that if we negotiate after calling AddTransceiver but before we
4396// have a track, then set a track later, frames from the newly-set tracks are
4397// received end-to-end.
4398TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
4399 MediaFlowsAfterEarlyWarmupWithAddTransceiver) {
4400 ASSERT_TRUE(CreatePeerConnectionWrappers());
4401 ConnectFakeSignaling();
4402 auto audio_result = caller()->pc()->AddTransceiver(cricket::MEDIA_TYPE_AUDIO);
4403 ASSERT_EQ(RTCErrorType::NONE, audio_result.error().type());
4404 auto caller_audio_sender = audio_result.MoveValue()->sender();
4405 auto video_result = caller()->pc()->AddTransceiver(cricket::MEDIA_TYPE_VIDEO);
4406 ASSERT_EQ(RTCErrorType::NONE, video_result.error().type());
4407 auto caller_video_sender = video_result.MoveValue()->sender();
4408 callee()->SetRemoteOfferHandler([this] {
4409 ASSERT_EQ(2u, callee()->pc()->GetTransceivers().size());
4410 callee()->pc()->GetTransceivers()[0]->SetDirection(
4411 RtpTransceiverDirection::kSendRecv);
4412 callee()->pc()->GetTransceivers()[1]->SetDirection(
4413 RtpTransceiverDirection::kSendRecv);
4414 });
4415 caller()->CreateAndSetAndSignalOffer();
4416 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
4417 // Wait for ICE to complete, without any tracks being set.
4418 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
4419 caller()->ice_connection_state(), kMaxWaitForFramesMs);
4420 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
4421 callee()->ice_connection_state(), kMaxWaitForFramesMs);
4422 // Now set the tracks, and expect frames to immediately start flowing.
4423 auto callee_audio_sender = callee()->pc()->GetSenders()[0];
4424 auto callee_video_sender = callee()->pc()->GetSenders()[1];
4425 ASSERT_TRUE(caller_audio_sender->SetTrack(caller()->CreateLocalAudioTrack()));
4426 ASSERT_TRUE(caller_video_sender->SetTrack(caller()->CreateLocalVideoTrack()));
4427 ASSERT_TRUE(callee_audio_sender->SetTrack(callee()->CreateLocalAudioTrack()));
4428 ASSERT_TRUE(callee_video_sender->SetTrack(callee()->CreateLocalVideoTrack()));
4429 MediaExpectations media_expectations;
4430 media_expectations.ExpectBidirectionalAudioAndVideo();
4431 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07004432}
4433
4434// This test verifies that a remote video track can be added via AddStream,
4435// and sent end-to-end. For this particular test, it's simply echoed back
4436// from the caller to the callee, rather than being forwarded to a third
4437// PeerConnection.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004438TEST_F(PeerConnectionIntegrationTestPlanB, CanSendRemoteVideoTrack) {
deadbeef1dcb1642017-03-29 21:08:16 -07004439 ASSERT_TRUE(CreatePeerConnectionWrappers());
4440 ConnectFakeSignaling();
4441 // Just send a video track from the caller.
Steve Anton15324772018-01-16 10:26:49 -08004442 caller()->AddVideoTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07004443 caller()->CreateAndSetAndSignalOffer();
4444 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02004445 ASSERT_EQ(1U, callee()->remote_streams()->count());
deadbeef1dcb1642017-03-29 21:08:16 -07004446
4447 // Echo the stream back, and do a new offer/anwer (initiated by callee this
4448 // time).
4449 callee()->pc()->AddStream(callee()->remote_streams()->at(0));
4450 callee()->CreateAndSetAndSignalOffer();
4451 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
4452
Seth Hampson2f0d7022018-02-20 11:54:42 -08004453 MediaExpectations media_expectations;
4454 media_expectations.ExpectBidirectionalVideo();
4455 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07004456}
4457
4458// Test that we achieve the expected end-to-end connection time, using a
4459// fake clock and simulated latency on the media and signaling paths.
4460// We use a TURN<->TURN connection because this is usually the quickest to
4461// set up initially, especially when we're confident the connection will work
4462// and can start sending media before we get a STUN response.
4463//
4464// With various optimizations enabled, here are the network delays we expect to
4465// be on the critical path:
4466// 1. 2 signaling trips: Signaling offer and offerer's TURN candidate, then
4467// signaling answer (with DTLS fingerprint).
4468// 2. 9 media hops: Rest of the DTLS handshake. 3 hops in each direction when
4469// using TURN<->TURN pair, and DTLS exchange is 4 packets,
4470// the first of which should have arrived before the answer.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004471TEST_P(PeerConnectionIntegrationTest, EndToEndConnectionTimeWithTurnTurnPair) {
deadbeef1dcb1642017-03-29 21:08:16 -07004472 rtc::ScopedFakeClock fake_clock;
4473 // Some things use a time of "0" as a special value, so we need to start out
4474 // the fake clock at a nonzero time.
4475 // TODO(deadbeef): Fix this.
Sebastian Jansson5f83cf02018-05-08 14:52:22 +02004476 fake_clock.AdvanceTime(webrtc::TimeDelta::seconds(1));
deadbeef1dcb1642017-03-29 21:08:16 -07004477
4478 static constexpr int media_hop_delay_ms = 50;
4479 static constexpr int signaling_trip_delay_ms = 500;
4480 // For explanation of these values, see comment above.
4481 static constexpr int required_media_hops = 9;
4482 static constexpr int required_signaling_trips = 2;
4483 // For internal delays (such as posting an event asychronously).
4484 static constexpr int allowed_internal_delay_ms = 20;
4485 static constexpr int total_connection_time_ms =
4486 media_hop_delay_ms * required_media_hops +
4487 signaling_trip_delay_ms * required_signaling_trips +
4488 allowed_internal_delay_ms;
4489
4490 static const rtc::SocketAddress turn_server_1_internal_address{"88.88.88.0",
4491 3478};
4492 static const rtc::SocketAddress turn_server_1_external_address{"88.88.88.1",
4493 0};
4494 static const rtc::SocketAddress turn_server_2_internal_address{"99.99.99.0",
4495 3478};
4496 static const rtc::SocketAddress turn_server_2_external_address{"99.99.99.1",
4497 0};
Seth Hampsonaed71642018-06-11 07:41:32 -07004498 cricket::TestTurnServer* turn_server_1 = CreateTurnServer(
4499 turn_server_1_internal_address, turn_server_1_external_address);
Jonas Orelandbdcee282017-10-10 14:01:40 +02004500
Seth Hampsonaed71642018-06-11 07:41:32 -07004501 cricket::TestTurnServer* turn_server_2 = CreateTurnServer(
4502 turn_server_2_internal_address, turn_server_2_external_address);
deadbeef1dcb1642017-03-29 21:08:16 -07004503 // Bypass permission check on received packets so media can be sent before
4504 // the candidate is signaled.
Seth Hampsonaed71642018-06-11 07:41:32 -07004505 network_thread()->Invoke<void>(RTC_FROM_HERE, [turn_server_1] {
4506 turn_server_1->set_enable_permission_checks(false);
4507 });
4508 network_thread()->Invoke<void>(RTC_FROM_HERE, [turn_server_2] {
4509 turn_server_2->set_enable_permission_checks(false);
4510 });
deadbeef1dcb1642017-03-29 21:08:16 -07004511
4512 PeerConnectionInterface::RTCConfiguration client_1_config;
4513 webrtc::PeerConnectionInterface::IceServer ice_server_1;
4514 ice_server_1.urls.push_back("turn:88.88.88.0:3478");
4515 ice_server_1.username = "test";
4516 ice_server_1.password = "test";
4517 client_1_config.servers.push_back(ice_server_1);
4518 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
4519 client_1_config.presume_writable_when_fully_relayed = true;
4520
4521 PeerConnectionInterface::RTCConfiguration client_2_config;
4522 webrtc::PeerConnectionInterface::IceServer ice_server_2;
4523 ice_server_2.urls.push_back("turn:99.99.99.0:3478");
4524 ice_server_2.username = "test";
4525 ice_server_2.password = "test";
4526 client_2_config.servers.push_back(ice_server_2);
4527 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
4528 client_2_config.presume_writable_when_fully_relayed = true;
4529
4530 ASSERT_TRUE(
4531 CreatePeerConnectionWrappersWithConfig(client_1_config, client_2_config));
4532 // Set up the simulated delays.
4533 SetSignalingDelayMs(signaling_trip_delay_ms);
4534 ConnectFakeSignaling();
4535 virtual_socket_server()->set_delay_mean(media_hop_delay_ms);
4536 virtual_socket_server()->UpdateDelayDistribution();
4537
4538 // Set "offer to receive audio/video" without adding any tracks, so we just
4539 // set up ICE/DTLS with no media.
4540 PeerConnectionInterface::RTCOfferAnswerOptions options;
4541 options.offer_to_receive_audio = 1;
4542 options.offer_to_receive_video = 1;
4543 caller()->SetOfferAnswerOptions(options);
4544 caller()->CreateAndSetAndSignalOffer();
deadbeef71452802017-05-07 17:21:01 -07004545 EXPECT_TRUE_SIMULATED_WAIT(DtlsConnected(), total_connection_time_ms,
4546 fake_clock);
Seth Hampson1d4a76d2018-06-19 14:31:41 -07004547 // Closing the PeerConnections destroys the ports before the ScopedFakeClock.
4548 // If this is not done a DCHECK can be hit in ports.cc, because a large
4549 // negative number is calculated for the rtt due to the global clock changing.
Steve Antond91969e2019-05-30 12:27:03 -07004550 ClosePeerConnections();
deadbeef1dcb1642017-03-29 21:08:16 -07004551}
4552
Jonas Orelandbdcee282017-10-10 14:01:40 +02004553// Verify that a TurnCustomizer passed in through RTCConfiguration
4554// is actually used by the underlying TURN candidate pair.
4555// Note that turnport_unittest.cc contains more detailed, lower-level tests.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004556TEST_P(PeerConnectionIntegrationTest, TurnCustomizerUsedForTurnConnections) {
Jonas Orelandbdcee282017-10-10 14:01:40 +02004557 static const rtc::SocketAddress turn_server_1_internal_address{"88.88.88.0",
4558 3478};
4559 static const rtc::SocketAddress turn_server_1_external_address{"88.88.88.1",
4560 0};
4561 static const rtc::SocketAddress turn_server_2_internal_address{"99.99.99.0",
4562 3478};
4563 static const rtc::SocketAddress turn_server_2_external_address{"99.99.99.1",
4564 0};
Seth Hampsonaed71642018-06-11 07:41:32 -07004565 CreateTurnServer(turn_server_1_internal_address,
4566 turn_server_1_external_address);
4567 CreateTurnServer(turn_server_2_internal_address,
4568 turn_server_2_external_address);
Jonas Orelandbdcee282017-10-10 14:01:40 +02004569
4570 PeerConnectionInterface::RTCConfiguration client_1_config;
4571 webrtc::PeerConnectionInterface::IceServer ice_server_1;
4572 ice_server_1.urls.push_back("turn:88.88.88.0:3478");
4573 ice_server_1.username = "test";
4574 ice_server_1.password = "test";
4575 client_1_config.servers.push_back(ice_server_1);
4576 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
Seth Hampsonaed71642018-06-11 07:41:32 -07004577 auto* customizer1 = CreateTurnCustomizer();
4578 client_1_config.turn_customizer = customizer1;
Jonas Orelandbdcee282017-10-10 14:01:40 +02004579
4580 PeerConnectionInterface::RTCConfiguration client_2_config;
4581 webrtc::PeerConnectionInterface::IceServer ice_server_2;
4582 ice_server_2.urls.push_back("turn:99.99.99.0:3478");
4583 ice_server_2.username = "test";
4584 ice_server_2.password = "test";
4585 client_2_config.servers.push_back(ice_server_2);
4586 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
Seth Hampsonaed71642018-06-11 07:41:32 -07004587 auto* customizer2 = CreateTurnCustomizer();
4588 client_2_config.turn_customizer = customizer2;
Jonas Orelandbdcee282017-10-10 14:01:40 +02004589
4590 ASSERT_TRUE(
4591 CreatePeerConnectionWrappersWithConfig(client_1_config, client_2_config));
4592 ConnectFakeSignaling();
4593
4594 // Set "offer to receive audio/video" without adding any tracks, so we just
4595 // set up ICE/DTLS with no media.
4596 PeerConnectionInterface::RTCOfferAnswerOptions options;
4597 options.offer_to_receive_audio = 1;
4598 options.offer_to_receive_video = 1;
4599 caller()->SetOfferAnswerOptions(options);
4600 caller()->CreateAndSetAndSignalOffer();
4601 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
4602
Seth Hampsonaed71642018-06-11 07:41:32 -07004603 ExpectTurnCustomizerCountersIncremented(customizer1);
4604 ExpectTurnCustomizerCountersIncremented(customizer2);
Jonas Orelandbdcee282017-10-10 14:01:40 +02004605}
4606
Benjamin Wright2d5f3cb2018-05-22 14:46:06 -07004607// Verifies that you can use TCP instead of UDP to connect to a TURN server and
4608// send media between the caller and the callee.
4609TEST_P(PeerConnectionIntegrationTest, TCPUsedForTurnConnections) {
4610 static const rtc::SocketAddress turn_server_internal_address{"88.88.88.0",
4611 3478};
4612 static const rtc::SocketAddress turn_server_external_address{"88.88.88.1", 0};
4613
4614 // Enable TCP for the fake turn server.
Seth Hampsonaed71642018-06-11 07:41:32 -07004615 CreateTurnServer(turn_server_internal_address, turn_server_external_address,
4616 cricket::PROTO_TCP);
Benjamin Wright2d5f3cb2018-05-22 14:46:06 -07004617
4618 webrtc::PeerConnectionInterface::IceServer ice_server;
4619 ice_server.urls.push_back("turn:88.88.88.0:3478?transport=tcp");
4620 ice_server.username = "test";
4621 ice_server.password = "test";
4622
4623 PeerConnectionInterface::RTCConfiguration client_1_config;
4624 client_1_config.servers.push_back(ice_server);
4625 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
4626
4627 PeerConnectionInterface::RTCConfiguration client_2_config;
4628 client_2_config.servers.push_back(ice_server);
4629 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
4630
4631 ASSERT_TRUE(
4632 CreatePeerConnectionWrappersWithConfig(client_1_config, client_2_config));
4633
4634 // Do normal offer/answer and wait for ICE to complete.
4635 ConnectFakeSignaling();
4636 caller()->AddAudioVideoTracks();
4637 callee()->AddAudioVideoTracks();
4638 caller()->CreateAndSetAndSignalOffer();
4639 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4640 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
4641 callee()->ice_connection_state(), kMaxWaitForFramesMs);
4642
4643 MediaExpectations media_expectations;
4644 media_expectations.ExpectBidirectionalAudioAndVideo();
4645 EXPECT_TRUE(ExpectNewFrames(media_expectations));
4646}
4647
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07004648// Verify that a SSLCertificateVerifier passed in through
4649// PeerConnectionDependencies is actually used by the underlying SSL
4650// implementation to determine whether a certificate presented by the TURN
4651// server is accepted by the client. Note that openssladapter_unittest.cc
4652// contains more detailed, lower-level tests.
4653TEST_P(PeerConnectionIntegrationTest,
4654 SSLCertificateVerifierUsedForTurnConnections) {
4655 static const rtc::SocketAddress turn_server_internal_address{"88.88.88.0",
4656 3478};
4657 static const rtc::SocketAddress turn_server_external_address{"88.88.88.1", 0};
4658
4659 // Enable TCP-TLS for the fake turn server. We need to pass in 88.88.88.0 so
4660 // that host name verification passes on the fake certificate.
Seth Hampsonaed71642018-06-11 07:41:32 -07004661 CreateTurnServer(turn_server_internal_address, turn_server_external_address,
4662 cricket::PROTO_TLS, "88.88.88.0");
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07004663
4664 webrtc::PeerConnectionInterface::IceServer ice_server;
4665 ice_server.urls.push_back("turns:88.88.88.0:3478?transport=tcp");
4666 ice_server.username = "test";
4667 ice_server.password = "test";
4668
4669 PeerConnectionInterface::RTCConfiguration client_1_config;
4670 client_1_config.servers.push_back(ice_server);
4671 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
4672
4673 PeerConnectionInterface::RTCConfiguration client_2_config;
4674 client_2_config.servers.push_back(ice_server);
4675 // Setting the type to kRelay forces the connection to go through a TURN
4676 // server.
4677 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
4678
4679 // Get a copy to the pointer so we can verify calls later.
4680 rtc::TestCertificateVerifier* client_1_cert_verifier =
4681 new rtc::TestCertificateVerifier();
4682 client_1_cert_verifier->verify_certificate_ = true;
4683 rtc::TestCertificateVerifier* client_2_cert_verifier =
4684 new rtc::TestCertificateVerifier();
4685 client_2_cert_verifier->verify_certificate_ = true;
4686
4687 // Create the dependencies with the test certificate verifier.
4688 webrtc::PeerConnectionDependencies client_1_deps(nullptr);
4689 client_1_deps.tls_cert_verifier =
4690 std::unique_ptr<rtc::TestCertificateVerifier>(client_1_cert_verifier);
4691 webrtc::PeerConnectionDependencies client_2_deps(nullptr);
4692 client_2_deps.tls_cert_verifier =
4693 std::unique_ptr<rtc::TestCertificateVerifier>(client_2_cert_verifier);
4694
4695 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndDeps(
4696 client_1_config, std::move(client_1_deps), client_2_config,
4697 std::move(client_2_deps)));
4698 ConnectFakeSignaling();
4699
4700 // Set "offer to receive audio/video" without adding any tracks, so we just
4701 // set up ICE/DTLS with no media.
4702 PeerConnectionInterface::RTCOfferAnswerOptions options;
4703 options.offer_to_receive_audio = 1;
4704 options.offer_to_receive_video = 1;
4705 caller()->SetOfferAnswerOptions(options);
4706 caller()->CreateAndSetAndSignalOffer();
4707 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
4708
4709 EXPECT_GT(client_1_cert_verifier->call_count_, 0u);
4710 EXPECT_GT(client_2_cert_verifier->call_count_, 0u);
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07004711}
4712
4713TEST_P(PeerConnectionIntegrationTest,
4714 SSLCertificateVerifierFailureUsedForTurnConnectionsFailsConnection) {
4715 static const rtc::SocketAddress turn_server_internal_address{"88.88.88.0",
4716 3478};
4717 static const rtc::SocketAddress turn_server_external_address{"88.88.88.1", 0};
4718
4719 // Enable TCP-TLS for the fake turn server. We need to pass in 88.88.88.0 so
4720 // that host name verification passes on the fake certificate.
Seth Hampsonaed71642018-06-11 07:41:32 -07004721 CreateTurnServer(turn_server_internal_address, turn_server_external_address,
4722 cricket::PROTO_TLS, "88.88.88.0");
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07004723
4724 webrtc::PeerConnectionInterface::IceServer ice_server;
4725 ice_server.urls.push_back("turns:88.88.88.0:3478?transport=tcp");
4726 ice_server.username = "test";
4727 ice_server.password = "test";
4728
4729 PeerConnectionInterface::RTCConfiguration client_1_config;
4730 client_1_config.servers.push_back(ice_server);
4731 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
4732
4733 PeerConnectionInterface::RTCConfiguration client_2_config;
4734 client_2_config.servers.push_back(ice_server);
4735 // Setting the type to kRelay forces the connection to go through a TURN
4736 // server.
4737 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
4738
4739 // Get a copy to the pointer so we can verify calls later.
4740 rtc::TestCertificateVerifier* client_1_cert_verifier =
4741 new rtc::TestCertificateVerifier();
4742 client_1_cert_verifier->verify_certificate_ = false;
4743 rtc::TestCertificateVerifier* client_2_cert_verifier =
4744 new rtc::TestCertificateVerifier();
4745 client_2_cert_verifier->verify_certificate_ = false;
4746
4747 // Create the dependencies with the test certificate verifier.
4748 webrtc::PeerConnectionDependencies client_1_deps(nullptr);
4749 client_1_deps.tls_cert_verifier =
4750 std::unique_ptr<rtc::TestCertificateVerifier>(client_1_cert_verifier);
4751 webrtc::PeerConnectionDependencies client_2_deps(nullptr);
4752 client_2_deps.tls_cert_verifier =
4753 std::unique_ptr<rtc::TestCertificateVerifier>(client_2_cert_verifier);
4754
4755 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndDeps(
4756 client_1_config, std::move(client_1_deps), client_2_config,
4757 std::move(client_2_deps)));
4758 ConnectFakeSignaling();
4759
4760 // Set "offer to receive audio/video" without adding any tracks, so we just
4761 // set up ICE/DTLS with no media.
4762 PeerConnectionInterface::RTCOfferAnswerOptions options;
4763 options.offer_to_receive_audio = 1;
4764 options.offer_to_receive_video = 1;
4765 caller()->SetOfferAnswerOptions(options);
4766 caller()->CreateAndSetAndSignalOffer();
4767 bool wait_res = true;
4768 // TODO(bugs.webrtc.org/9219): When IceConnectionState is implemented
4769 // properly, should be able to just wait for a state of "failed" instead of
4770 // waiting a fixed 10 seconds.
4771 WAIT_(DtlsConnected(), kDefaultTimeout, wait_res);
4772 ASSERT_FALSE(wait_res);
4773
4774 EXPECT_GT(client_1_cert_verifier->call_count_, 0u);
4775 EXPECT_GT(client_2_cert_verifier->call_count_, 0u);
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07004776}
4777
deadbeefc964d0b2017-04-03 10:03:35 -07004778// Test that audio and video flow end-to-end when codec names don't use the
4779// expected casing, given that they're supposed to be case insensitive. To test
4780// this, all but one codec is removed from each media description, and its
4781// casing is changed.
4782//
4783// In the past, this has regressed and caused crashes/black video, due to the
4784// fact that code at some layers was doing case-insensitive comparisons and
4785// code at other layers was not.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004786TEST_P(PeerConnectionIntegrationTest, CodecNamesAreCaseInsensitive) {
deadbeefc964d0b2017-04-03 10:03:35 -07004787 ASSERT_TRUE(CreatePeerConnectionWrappers());
4788 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08004789 caller()->AddAudioVideoTracks();
4790 callee()->AddAudioVideoTracks();
deadbeefc964d0b2017-04-03 10:03:35 -07004791
4792 // Remove all but one audio/video codec (opus and VP8), and change the
4793 // casing of the caller's generated offer.
4794 caller()->SetGeneratedSdpMunger([](cricket::SessionDescription* description) {
4795 cricket::AudioContentDescription* audio =
4796 GetFirstAudioContentDescription(description);
4797 ASSERT_NE(nullptr, audio);
4798 auto audio_codecs = audio->codecs();
4799 audio_codecs.erase(std::remove_if(audio_codecs.begin(), audio_codecs.end(),
4800 [](const cricket::AudioCodec& codec) {
4801 return codec.name != "opus";
4802 }),
4803 audio_codecs.end());
4804 ASSERT_EQ(1u, audio_codecs.size());
4805 audio_codecs[0].name = "OpUs";
4806 audio->set_codecs(audio_codecs);
4807
4808 cricket::VideoContentDescription* video =
4809 GetFirstVideoContentDescription(description);
4810 ASSERT_NE(nullptr, video);
4811 auto video_codecs = video->codecs();
4812 video_codecs.erase(std::remove_if(video_codecs.begin(), video_codecs.end(),
4813 [](const cricket::VideoCodec& codec) {
4814 return codec.name != "VP8";
4815 }),
4816 video_codecs.end());
4817 ASSERT_EQ(1u, video_codecs.size());
4818 video_codecs[0].name = "vP8";
4819 video->set_codecs(video_codecs);
4820 });
4821
4822 caller()->CreateAndSetAndSignalOffer();
4823 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4824
4825 // Verify frames are still received end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004826 MediaExpectations media_expectations;
4827 media_expectations.ExpectBidirectionalAudioAndVideo();
4828 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeefc964d0b2017-04-03 10:03:35 -07004829}
4830
Jonas Oreland49ac5952018-09-26 16:04:32 +02004831TEST_P(PeerConnectionIntegrationTest, GetSourcesAudio) {
hbos8d609f62017-04-10 07:39:05 -07004832 ASSERT_TRUE(CreatePeerConnectionWrappers());
4833 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08004834 caller()->AddAudioTrack();
hbos8d609f62017-04-10 07:39:05 -07004835 caller()->CreateAndSetAndSignalOffer();
4836 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
deadbeefd8ad7882017-04-18 16:01:17 -07004837 // Wait for one audio frame to be received by the callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004838 MediaExpectations media_expectations;
4839 media_expectations.CalleeExpectsSomeAudio(1);
4840 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Jonas Oreland49ac5952018-09-26 16:04:32 +02004841 ASSERT_EQ(callee()->pc()->GetReceivers().size(), 1u);
hbos8d609f62017-04-10 07:39:05 -07004842 auto receiver = callee()->pc()->GetReceivers()[0];
4843 ASSERT_EQ(receiver->media_type(), cricket::MEDIA_TYPE_AUDIO);
Jonas Oreland49ac5952018-09-26 16:04:32 +02004844 auto sources = receiver->GetSources();
hbos8d609f62017-04-10 07:39:05 -07004845 ASSERT_GT(receiver->GetParameters().encodings.size(), 0u);
4846 EXPECT_EQ(receiver->GetParameters().encodings[0].ssrc,
Jonas Oreland49ac5952018-09-26 16:04:32 +02004847 sources[0].source_id());
4848 EXPECT_EQ(webrtc::RtpSourceType::SSRC, sources[0].source_type());
4849}
4850
4851TEST_P(PeerConnectionIntegrationTest, GetSourcesVideo) {
4852 ASSERT_TRUE(CreatePeerConnectionWrappers());
4853 ConnectFakeSignaling();
4854 caller()->AddVideoTrack();
4855 caller()->CreateAndSetAndSignalOffer();
4856 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4857 // Wait for one video frame to be received by the callee.
4858 MediaExpectations media_expectations;
4859 media_expectations.CalleeExpectsSomeVideo(1);
4860 ASSERT_TRUE(ExpectNewFrames(media_expectations));
4861 ASSERT_EQ(callee()->pc()->GetReceivers().size(), 1u);
4862 auto receiver = callee()->pc()->GetReceivers()[0];
4863 ASSERT_EQ(receiver->media_type(), cricket::MEDIA_TYPE_VIDEO);
4864 auto sources = receiver->GetSources();
4865 ASSERT_GT(receiver->GetParameters().encodings.size(), 0u);
Yves Gereyf781bb52019-07-23 19:15:39 +02004866 ASSERT_GT(sources.size(), 0u);
Jonas Oreland49ac5952018-09-26 16:04:32 +02004867 EXPECT_EQ(receiver->GetParameters().encodings[0].ssrc,
4868 sources[0].source_id());
4869 EXPECT_EQ(webrtc::RtpSourceType::SSRC, sources[0].source_type());
hbos8d609f62017-04-10 07:39:05 -07004870}
4871
deadbeef2f425aa2017-04-14 10:41:32 -07004872// Test that if a track is removed and added again with a different stream ID,
4873// the new stream ID is successfully communicated in SDP and media continues to
4874// flow end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004875// TODO(webrtc.bugs.org/8734): This test does not work for Unified Plan because
4876// it will not reuse a transceiver that has already been sending. After creating
4877// a new transceiver it tries to create an offer with two senders of the same
4878// track ids and it fails.
4879TEST_F(PeerConnectionIntegrationTestPlanB, RemoveAndAddTrackWithNewStreamId) {
deadbeef2f425aa2017-04-14 10:41:32 -07004880 ASSERT_TRUE(CreatePeerConnectionWrappers());
4881 ConnectFakeSignaling();
4882
deadbeef2f425aa2017-04-14 10:41:32 -07004883 // Add track using stream 1, do offer/answer.
4884 rtc::scoped_refptr<webrtc::AudioTrackInterface> track =
4885 caller()->CreateLocalAudioTrack();
4886 rtc::scoped_refptr<webrtc::RtpSenderInterface> sender =
Steve Antond78323f2018-07-11 11:13:44 -07004887 caller()->AddTrack(track, {"stream_1"});
deadbeef2f425aa2017-04-14 10:41:32 -07004888 caller()->CreateAndSetAndSignalOffer();
4889 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08004890 {
4891 MediaExpectations media_expectations;
4892 media_expectations.CalleeExpectsSomeAudio(1);
4893 ASSERT_TRUE(ExpectNewFrames(media_expectations));
4894 }
deadbeef2f425aa2017-04-14 10:41:32 -07004895 // Remove the sender, and create a new one with the new stream.
4896 caller()->pc()->RemoveTrack(sender);
Steve Antond78323f2018-07-11 11:13:44 -07004897 sender = caller()->AddTrack(track, {"stream_2"});
deadbeef2f425aa2017-04-14 10:41:32 -07004898 caller()->CreateAndSetAndSignalOffer();
4899 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4900 // Wait for additional audio frames to be received by the callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004901 {
4902 MediaExpectations media_expectations;
4903 media_expectations.CalleeExpectsSomeAudio();
4904 ASSERT_TRUE(ExpectNewFrames(media_expectations));
4905 }
deadbeef2f425aa2017-04-14 10:41:32 -07004906}
4907
Seth Hampson2f0d7022018-02-20 11:54:42 -08004908TEST_P(PeerConnectionIntegrationTest, RtcEventLogOutputWriteCalled) {
Elad Alon99c3fe52017-10-13 16:29:40 +02004909 ASSERT_TRUE(CreatePeerConnectionWrappers());
4910 ConnectFakeSignaling();
4911
Karl Wiberg918f50c2018-07-05 11:40:33 +02004912 auto output = absl::make_unique<testing::NiceMock<MockRtcEventLogOutput>>();
Mirko Bonadei6a489f22019-04-09 15:11:12 +02004913 ON_CALL(*output, IsActive()).WillByDefault(::testing::Return(true));
4914 ON_CALL(*output, Write(::testing::_)).WillByDefault(::testing::Return(true));
Elad Alon99c3fe52017-10-13 16:29:40 +02004915 EXPECT_CALL(*output, Write(::testing::_)).Times(::testing::AtLeast(1));
Bjorn Tereliusde939432017-11-20 17:38:14 +01004916 EXPECT_TRUE(caller()->pc()->StartRtcEventLog(
4917 std::move(output), webrtc::RtcEventLog::kImmediateOutput));
Elad Alon99c3fe52017-10-13 16:29:40 +02004918
Steve Anton15324772018-01-16 10:26:49 -08004919 caller()->AddAudioVideoTracks();
Elad Alon99c3fe52017-10-13 16:29:40 +02004920 caller()->CreateAndSetAndSignalOffer();
4921 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4922}
4923
Steve Antonede9ca52017-10-16 13:04:27 -07004924// Test that if candidates are only signaled by applying full session
4925// descriptions (instead of using AddIceCandidate), the peers can connect to
4926// each other and exchange media.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004927TEST_P(PeerConnectionIntegrationTest, MediaFlowsWhenCandidatesSetOnlyInSdp) {
Steve Antonede9ca52017-10-16 13:04:27 -07004928 ASSERT_TRUE(CreatePeerConnectionWrappers());
4929 // Each side will signal the session descriptions but not candidates.
4930 ConnectFakeSignalingForSdpOnly();
4931
4932 // Add audio video track and exchange the initial offer/answer with media
4933 // information only. This will start ICE gathering on each side.
Steve Anton15324772018-01-16 10:26:49 -08004934 caller()->AddAudioVideoTracks();
4935 callee()->AddAudioVideoTracks();
Steve Antonede9ca52017-10-16 13:04:27 -07004936 caller()->CreateAndSetAndSignalOffer();
4937
4938 // Wait for all candidates to be gathered on both the caller and callee.
4939 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceGatheringComplete,
4940 caller()->ice_gathering_state(), kDefaultTimeout);
4941 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceGatheringComplete,
4942 callee()->ice_gathering_state(), kDefaultTimeout);
4943
4944 // The candidates will now be included in the session description, so
4945 // signaling them will start the ICE connection.
4946 caller()->CreateAndSetAndSignalOffer();
4947 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4948
4949 // Ensure that media flows in both directions.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004950 MediaExpectations media_expectations;
4951 media_expectations.ExpectBidirectionalAudioAndVideo();
4952 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Antonede9ca52017-10-16 13:04:27 -07004953}
4954
henrika5f6bf242017-11-01 11:06:56 +01004955// Test that SetAudioPlayout can be used to disable audio playout from the
4956// start, then later enable it. This may be useful, for example, if the caller
4957// needs to play a local ringtone until some event occurs, after which it
4958// switches to playing the received audio.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004959TEST_P(PeerConnectionIntegrationTest, DisableAndEnableAudioPlayout) {
henrika5f6bf242017-11-01 11:06:56 +01004960 ASSERT_TRUE(CreatePeerConnectionWrappers());
4961 ConnectFakeSignaling();
4962
4963 // Set up audio-only call where audio playout is disabled on caller's side.
4964 caller()->pc()->SetAudioPlayout(false);
Steve Anton15324772018-01-16 10:26:49 -08004965 caller()->AddAudioTrack();
4966 callee()->AddAudioTrack();
henrika5f6bf242017-11-01 11:06:56 +01004967 caller()->CreateAndSetAndSignalOffer();
4968 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4969
4970 // Pump messages for a second.
4971 WAIT(false, 1000);
4972 // Since audio playout is disabled, the caller shouldn't have received
4973 // anything (at the playout level, at least).
4974 EXPECT_EQ(0, caller()->audio_frames_received());
4975 // As a sanity check, make sure the callee (for which playout isn't disabled)
4976 // did still see frames on its audio level.
4977 ASSERT_GT(callee()->audio_frames_received(), 0);
4978
4979 // Enable playout again, and ensure audio starts flowing.
4980 caller()->pc()->SetAudioPlayout(true);
Seth Hampson2f0d7022018-02-20 11:54:42 -08004981 MediaExpectations media_expectations;
4982 media_expectations.ExpectBidirectionalAudio();
4983 ASSERT_TRUE(ExpectNewFrames(media_expectations));
henrika5f6bf242017-11-01 11:06:56 +01004984}
4985
4986double GetAudioEnergyStat(PeerConnectionWrapper* pc) {
4987 auto report = pc->NewGetStats();
4988 auto track_stats_list =
4989 report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
4990 const webrtc::RTCMediaStreamTrackStats* remote_track_stats = nullptr;
4991 for (const auto* track_stats : track_stats_list) {
4992 if (track_stats->remote_source.is_defined() &&
4993 *track_stats->remote_source) {
4994 remote_track_stats = track_stats;
4995 break;
4996 }
4997 }
4998
4999 if (!remote_track_stats->total_audio_energy.is_defined()) {
5000 return 0.0;
5001 }
5002 return *remote_track_stats->total_audio_energy;
5003}
5004
5005// Test that if audio playout is disabled via the SetAudioPlayout() method, then
5006// incoming audio is still processed and statistics are generated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08005007TEST_P(PeerConnectionIntegrationTest,
henrika5f6bf242017-11-01 11:06:56 +01005008 DisableAudioPlayoutStillGeneratesAudioStats) {
5009 ASSERT_TRUE(CreatePeerConnectionWrappers());
5010 ConnectFakeSignaling();
5011
5012 // Set up audio-only call where playout is disabled but audio-processing is
5013 // still active.
Steve Anton15324772018-01-16 10:26:49 -08005014 caller()->AddAudioTrack();
5015 callee()->AddAudioTrack();
henrika5f6bf242017-11-01 11:06:56 +01005016 caller()->pc()->SetAudioPlayout(false);
5017
5018 caller()->CreateAndSetAndSignalOffer();
5019 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5020
5021 // Wait for the callee to receive audio stats.
5022 EXPECT_TRUE_WAIT(GetAudioEnergyStat(caller()) > 0, kMaxWaitForFramesMs);
5023}
5024
henrika4f167df2017-11-01 14:45:55 +01005025// Test that SetAudioRecording can be used to disable audio recording from the
5026// start, then later enable it. This may be useful, for example, if the caller
5027// wants to ensure that no audio resources are active before a certain state
5028// is reached.
Seth Hampson2f0d7022018-02-20 11:54:42 -08005029TEST_P(PeerConnectionIntegrationTest, DisableAndEnableAudioRecording) {
henrika4f167df2017-11-01 14:45:55 +01005030 ASSERT_TRUE(CreatePeerConnectionWrappers());
5031 ConnectFakeSignaling();
5032
5033 // Set up audio-only call where audio recording is disabled on caller's side.
5034 caller()->pc()->SetAudioRecording(false);
Steve Anton15324772018-01-16 10:26:49 -08005035 caller()->AddAudioTrack();
5036 callee()->AddAudioTrack();
henrika4f167df2017-11-01 14:45:55 +01005037 caller()->CreateAndSetAndSignalOffer();
5038 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5039
5040 // Pump messages for a second.
5041 WAIT(false, 1000);
5042 // Since caller has disabled audio recording, the callee shouldn't have
5043 // received anything.
5044 EXPECT_EQ(0, callee()->audio_frames_received());
5045 // As a sanity check, make sure the caller did still see frames on its
5046 // audio level since audio recording is enabled on the calle side.
5047 ASSERT_GT(caller()->audio_frames_received(), 0);
5048
5049 // Enable audio recording again, and ensure audio starts flowing.
5050 caller()->pc()->SetAudioRecording(true);
Seth Hampson2f0d7022018-02-20 11:54:42 -08005051 MediaExpectations media_expectations;
5052 media_expectations.ExpectBidirectionalAudio();
5053 ASSERT_TRUE(ExpectNewFrames(media_expectations));
henrika4f167df2017-11-01 14:45:55 +01005054}
5055
Taylor Brandstetter389a97c2018-01-03 16:26:06 -08005056// Test that after closing PeerConnections, they stop sending any packets (ICE,
5057// DTLS, RTP...).
Seth Hampson2f0d7022018-02-20 11:54:42 -08005058TEST_P(PeerConnectionIntegrationTest, ClosingConnectionStopsPacketFlow) {
Taylor Brandstetter389a97c2018-01-03 16:26:06 -08005059 // Set up audio/video/data, wait for some frames to be received.
5060 ASSERT_TRUE(CreatePeerConnectionWrappers());
5061 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08005062 caller()->AddAudioVideoTracks();
Taylor Brandstetter389a97c2018-01-03 16:26:06 -08005063#ifdef HAVE_SCTP
5064 caller()->CreateDataChannel();
5065#endif
5066 caller()->CreateAndSetAndSignalOffer();
5067 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08005068 MediaExpectations media_expectations;
5069 media_expectations.CalleeExpectsSomeAudioAndVideo();
5070 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Taylor Brandstetter389a97c2018-01-03 16:26:06 -08005071 // Close PeerConnections.
Steve Antond91969e2019-05-30 12:27:03 -07005072 ClosePeerConnections();
Taylor Brandstetter389a97c2018-01-03 16:26:06 -08005073 // Pump messages for a second, and ensure no new packets end up sent.
5074 uint32_t sent_packets_a = virtual_socket_server()->sent_packets();
5075 WAIT(false, 1000);
5076 uint32_t sent_packets_b = virtual_socket_server()->sent_packets();
5077 EXPECT_EQ(sent_packets_a, sent_packets_b);
5078}
5079
Steve Anton7eca0932018-03-30 15:18:41 -07005080// Test that transport stats are generated by the RTCStatsCollector for a
5081// connection that only involves data channels. This is a regression test for
5082// crbug.com/826972.
5083#ifdef HAVE_SCTP
5084TEST_P(PeerConnectionIntegrationTest,
5085 TransportStatsReportedForDataChannelOnlyConnection) {
5086 ASSERT_TRUE(CreatePeerConnectionWrappers());
5087 ConnectFakeSignaling();
5088 caller()->CreateDataChannel();
5089
5090 caller()->CreateAndSetAndSignalOffer();
5091 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5092 ASSERT_TRUE_WAIT(callee()->data_channel(), kDefaultTimeout);
5093
5094 auto caller_report = caller()->NewGetStats();
5095 EXPECT_EQ(1u, caller_report->GetStatsOfType<RTCTransportStats>().size());
5096 auto callee_report = callee()->NewGetStats();
5097 EXPECT_EQ(1u, callee_report->GetStatsOfType<RTCTransportStats>().size());
5098}
5099#endif // HAVE_SCTP
5100
Qingsi Wang7685e862018-06-11 20:15:46 -07005101TEST_P(PeerConnectionIntegrationTest,
5102 IceEventsGeneratedAndLoggedInRtcEventLog) {
5103 ASSERT_TRUE(CreatePeerConnectionWrappersWithFakeRtcEventLog());
5104 ConnectFakeSignaling();
5105 PeerConnectionInterface::RTCOfferAnswerOptions options;
5106 options.offer_to_receive_audio = 1;
5107 caller()->SetOfferAnswerOptions(options);
5108 caller()->CreateAndSetAndSignalOffer();
5109 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
5110 ASSERT_NE(nullptr, caller()->event_log_factory());
5111 ASSERT_NE(nullptr, callee()->event_log_factory());
5112 webrtc::FakeRtcEventLog* caller_event_log =
5113 static_cast<webrtc::FakeRtcEventLog*>(
5114 caller()->event_log_factory()->last_log_created());
5115 webrtc::FakeRtcEventLog* callee_event_log =
5116 static_cast<webrtc::FakeRtcEventLog*>(
5117 callee()->event_log_factory()->last_log_created());
5118 ASSERT_NE(nullptr, caller_event_log);
5119 ASSERT_NE(nullptr, callee_event_log);
5120 int caller_ice_config_count = caller_event_log->GetEventCount(
5121 webrtc::RtcEvent::Type::IceCandidatePairConfig);
5122 int caller_ice_event_count = caller_event_log->GetEventCount(
5123 webrtc::RtcEvent::Type::IceCandidatePairEvent);
5124 int callee_ice_config_count = callee_event_log->GetEventCount(
5125 webrtc::RtcEvent::Type::IceCandidatePairConfig);
5126 int callee_ice_event_count = callee_event_log->GetEventCount(
5127 webrtc::RtcEvent::Type::IceCandidatePairEvent);
5128 EXPECT_LT(0, caller_ice_config_count);
5129 EXPECT_LT(0, caller_ice_event_count);
5130 EXPECT_LT(0, callee_ice_config_count);
5131 EXPECT_LT(0, callee_ice_event_count);
5132}
5133
Qingsi Wangc129c352019-04-18 10:41:58 -07005134TEST_P(PeerConnectionIntegrationTest, RegatherAfterChangingIceTransportType) {
Qingsi Wangc129c352019-04-18 10:41:58 -07005135 static const rtc::SocketAddress turn_server_internal_address{"88.88.88.0",
5136 3478};
5137 static const rtc::SocketAddress turn_server_external_address{"88.88.88.1", 0};
5138
5139 CreateTurnServer(turn_server_internal_address, turn_server_external_address);
5140
5141 webrtc::PeerConnectionInterface::IceServer ice_server;
5142 ice_server.urls.push_back("turn:88.88.88.0:3478");
5143 ice_server.username = "test";
5144 ice_server.password = "test";
5145
5146 PeerConnectionInterface::RTCConfiguration caller_config;
5147 caller_config.servers.push_back(ice_server);
5148 caller_config.type = webrtc::PeerConnectionInterface::kRelay;
5149 caller_config.continual_gathering_policy = PeerConnection::GATHER_CONTINUALLY;
Qingsi Wang1fe119f2019-05-31 16:55:33 -07005150 caller_config.surface_ice_candidates_on_ice_transport_type_changed = true;
Qingsi Wangc129c352019-04-18 10:41:58 -07005151
5152 PeerConnectionInterface::RTCConfiguration callee_config;
5153 callee_config.servers.push_back(ice_server);
5154 callee_config.type = webrtc::PeerConnectionInterface::kRelay;
5155 callee_config.continual_gathering_policy = PeerConnection::GATHER_CONTINUALLY;
Qingsi Wang1fe119f2019-05-31 16:55:33 -07005156 callee_config.surface_ice_candidates_on_ice_transport_type_changed = true;
Qingsi Wangc129c352019-04-18 10:41:58 -07005157
5158 ASSERT_TRUE(
5159 CreatePeerConnectionWrappersWithConfig(caller_config, callee_config));
5160
5161 // Do normal offer/answer and wait for ICE to complete.
5162 ConnectFakeSignaling();
5163 caller()->AddAudioVideoTracks();
5164 callee()->AddAudioVideoTracks();
5165 caller()->CreateAndSetAndSignalOffer();
5166 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5167 // Since we are doing continual gathering, the ICE transport does not reach
5168 // kIceGatheringComplete (see
5169 // P2PTransportChannel::OnCandidatesAllocationDone), and consequently not
5170 // kIceConnectionComplete.
5171 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
5172 caller()->ice_connection_state(), kDefaultTimeout);
5173 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
5174 callee()->ice_connection_state(), kDefaultTimeout);
5175 // Note that we cannot use the metric
5176 // |WebRTC.PeerConnection.CandidatePairType_UDP| in this test since this
5177 // metric is only populated when we reach kIceConnectionComplete in the
5178 // current implementation.
5179 EXPECT_EQ(cricket::RELAY_PORT_TYPE,
5180 caller()->last_candidate_gathered().type());
5181 EXPECT_EQ(cricket::RELAY_PORT_TYPE,
5182 callee()->last_candidate_gathered().type());
5183
5184 // Loosen the caller's candidate filter.
5185 caller_config = caller()->pc()->GetConfiguration();
5186 caller_config.type = webrtc::PeerConnectionInterface::kAll;
5187 caller()->pc()->SetConfiguration(caller_config);
5188 // We should have gathered a new host candidate.
5189 EXPECT_EQ_WAIT(cricket::LOCAL_PORT_TYPE,
5190 caller()->last_candidate_gathered().type(), kDefaultTimeout);
5191
5192 // Loosen the callee's candidate filter.
5193 callee_config = callee()->pc()->GetConfiguration();
5194 callee_config.type = webrtc::PeerConnectionInterface::kAll;
5195 callee()->pc()->SetConfiguration(callee_config);
5196 EXPECT_EQ_WAIT(cricket::LOCAL_PORT_TYPE,
5197 callee()->last_candidate_gathered().type(), kDefaultTimeout);
5198}
5199
Eldar Relloda13ea22019-06-01 12:23:43 +03005200TEST_P(PeerConnectionIntegrationTest, OnIceCandidateError) {
Eldar Relloda13ea22019-06-01 12:23:43 +03005201 static const rtc::SocketAddress turn_server_internal_address{"88.88.88.0",
5202 3478};
5203 static const rtc::SocketAddress turn_server_external_address{"88.88.88.1", 0};
5204
5205 CreateTurnServer(turn_server_internal_address, turn_server_external_address);
5206
5207 webrtc::PeerConnectionInterface::IceServer ice_server;
5208 ice_server.urls.push_back("turn:88.88.88.0:3478");
5209 ice_server.username = "test";
5210 ice_server.password = "123";
5211
5212 PeerConnectionInterface::RTCConfiguration caller_config;
5213 caller_config.servers.push_back(ice_server);
5214 caller_config.type = webrtc::PeerConnectionInterface::kRelay;
5215 caller_config.continual_gathering_policy = PeerConnection::GATHER_CONTINUALLY;
5216
5217 PeerConnectionInterface::RTCConfiguration callee_config;
5218 callee_config.servers.push_back(ice_server);
5219 callee_config.type = webrtc::PeerConnectionInterface::kRelay;
5220 callee_config.continual_gathering_policy = PeerConnection::GATHER_CONTINUALLY;
5221
5222 ASSERT_TRUE(
5223 CreatePeerConnectionWrappersWithConfig(caller_config, callee_config));
5224
5225 // Do normal offer/answer and wait for ICE to complete.
5226 ConnectFakeSignaling();
5227 caller()->AddAudioVideoTracks();
5228 callee()->AddAudioVideoTracks();
5229 caller()->CreateAndSetAndSignalOffer();
5230 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5231 EXPECT_EQ_WAIT(401, caller()->error_event().error_code, kDefaultTimeout);
5232 EXPECT_EQ("Unauthorized", caller()->error_event().error_text);
5233 EXPECT_EQ("turn:88.88.88.0:3478?transport=udp", caller()->error_event().url);
5234 EXPECT_NE(std::string::npos,
5235 caller()->error_event().host_candidate.find(":"));
5236}
5237
Mirko Bonadeic84f6612019-01-31 12:20:57 +01005238INSTANTIATE_TEST_SUITE_P(PeerConnectionIntegrationTest,
5239 PeerConnectionIntegrationTest,
5240 Values(SdpSemantics::kPlanB,
5241 SdpSemantics::kUnifiedPlan));
Steve Antond3679212018-01-17 17:41:02 -08005242
Steve Anton74255ff2018-01-24 18:32:57 -08005243// Tests that verify interoperability between Plan B and Unified Plan
5244// PeerConnections.
5245class PeerConnectionIntegrationInteropTest
Seth Hampson2f0d7022018-02-20 11:54:42 -08005246 : public PeerConnectionIntegrationBaseTest,
Steve Anton74255ff2018-01-24 18:32:57 -08005247 public ::testing::WithParamInterface<
5248 std::tuple<SdpSemantics, SdpSemantics>> {
5249 protected:
Seth Hampson2f0d7022018-02-20 11:54:42 -08005250 // Setting the SdpSemantics for the base test to kDefault does not matter
5251 // because we specify not to use the test semantics when creating
5252 // PeerConnectionWrappers.
Steve Anton74255ff2018-01-24 18:32:57 -08005253 PeerConnectionIntegrationInteropTest()
Steve Anton3acffc32018-04-12 17:21:03 -07005254 : PeerConnectionIntegrationBaseTest(SdpSemantics::kPlanB),
Seth Hampson2f0d7022018-02-20 11:54:42 -08005255 caller_semantics_(std::get<0>(GetParam())),
Steve Anton74255ff2018-01-24 18:32:57 -08005256 callee_semantics_(std::get<1>(GetParam())) {}
5257
5258 bool CreatePeerConnectionWrappersWithSemantics() {
Steve Anton3acffc32018-04-12 17:21:03 -07005259 return CreatePeerConnectionWrappersWithSdpSemantics(caller_semantics_,
5260 callee_semantics_);
Steve Anton74255ff2018-01-24 18:32:57 -08005261 }
5262
5263 const SdpSemantics caller_semantics_;
5264 const SdpSemantics callee_semantics_;
5265};
5266
5267TEST_P(PeerConnectionIntegrationInteropTest, NoMediaLocalToNoMediaRemote) {
5268 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
5269 ConnectFakeSignaling();
5270
5271 caller()->CreateAndSetAndSignalOffer();
5272 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5273}
5274
5275TEST_P(PeerConnectionIntegrationInteropTest, OneAudioLocalToNoMediaRemote) {
5276 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
5277 ConnectFakeSignaling();
5278 auto audio_sender = caller()->AddAudioTrack();
5279
5280 caller()->CreateAndSetAndSignalOffer();
5281 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5282
5283 // Verify that one audio receiver has been created on the remote and that it
5284 // has the same track ID as the sending track.
5285 auto receivers = callee()->pc()->GetReceivers();
5286 ASSERT_EQ(1u, receivers.size());
5287 EXPECT_EQ(cricket::MEDIA_TYPE_AUDIO, receivers[0]->media_type());
5288 EXPECT_EQ(receivers[0]->track()->id(), audio_sender->track()->id());
5289
Seth Hampson2f0d7022018-02-20 11:54:42 -08005290 MediaExpectations media_expectations;
5291 media_expectations.CalleeExpectsSomeAudio();
5292 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08005293}
5294
5295TEST_P(PeerConnectionIntegrationInteropTest, OneAudioOneVideoToNoMediaRemote) {
5296 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
5297 ConnectFakeSignaling();
5298 auto video_sender = caller()->AddVideoTrack();
5299 auto audio_sender = caller()->AddAudioTrack();
5300
5301 caller()->CreateAndSetAndSignalOffer();
5302 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5303
5304 // Verify that one audio and one video receiver have been created on the
5305 // remote and that they have the same track IDs as the sending tracks.
5306 auto audio_receivers =
5307 callee()->GetReceiversOfType(cricket::MEDIA_TYPE_AUDIO);
5308 ASSERT_EQ(1u, audio_receivers.size());
5309 EXPECT_EQ(audio_receivers[0]->track()->id(), audio_sender->track()->id());
5310 auto video_receivers =
5311 callee()->GetReceiversOfType(cricket::MEDIA_TYPE_VIDEO);
5312 ASSERT_EQ(1u, video_receivers.size());
5313 EXPECT_EQ(video_receivers[0]->track()->id(), video_sender->track()->id());
5314
Seth Hampson2f0d7022018-02-20 11:54:42 -08005315 MediaExpectations media_expectations;
5316 media_expectations.CalleeExpectsSomeAudioAndVideo();
5317 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08005318}
5319
5320TEST_P(PeerConnectionIntegrationInteropTest,
5321 OneAudioOneVideoLocalToOneAudioOneVideoRemote) {
5322 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
5323 ConnectFakeSignaling();
5324 caller()->AddAudioVideoTracks();
5325 callee()->AddAudioVideoTracks();
5326
5327 caller()->CreateAndSetAndSignalOffer();
5328 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5329
Seth Hampson2f0d7022018-02-20 11:54:42 -08005330 MediaExpectations media_expectations;
5331 media_expectations.ExpectBidirectionalAudioAndVideo();
5332 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08005333}
5334
5335TEST_P(PeerConnectionIntegrationInteropTest,
5336 ReverseRolesOneAudioLocalToOneVideoRemote) {
5337 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
5338 ConnectFakeSignaling();
5339 caller()->AddAudioTrack();
5340 callee()->AddVideoTrack();
5341
5342 caller()->CreateAndSetAndSignalOffer();
5343 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5344
5345 // Verify that only the audio track has been negotiated.
5346 EXPECT_EQ(0u, caller()->GetReceiversOfType(cricket::MEDIA_TYPE_VIDEO).size());
5347 // Might also check that the callee's NegotiationNeeded flag is set.
5348
5349 // Reverse roles.
5350 callee()->CreateAndSetAndSignalOffer();
5351 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5352
Seth Hampson2f0d7022018-02-20 11:54:42 -08005353 MediaExpectations media_expectations;
5354 media_expectations.CallerExpectsSomeVideo();
5355 media_expectations.CalleeExpectsSomeAudio();
5356 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08005357}
5358
Mirko Bonadeic84f6612019-01-31 12:20:57 +01005359INSTANTIATE_TEST_SUITE_P(
Steve Antonba42e992018-04-09 14:10:01 -07005360 PeerConnectionIntegrationTest,
5361 PeerConnectionIntegrationInteropTest,
5362 Values(std::make_tuple(SdpSemantics::kPlanB, SdpSemantics::kUnifiedPlan),
5363 std::make_tuple(SdpSemantics::kUnifiedPlan, SdpSemantics::kPlanB)));
5364
5365// Test that if the Unified Plan side offers two video tracks then the Plan B
5366// side will only see the first one and ignore the second.
5367TEST_F(PeerConnectionIntegrationTestPlanB, TwoVideoUnifiedPlanToNoMediaPlanB) {
Steve Anton3acffc32018-04-12 17:21:03 -07005368 ASSERT_TRUE(CreatePeerConnectionWrappersWithSdpSemantics(
5369 SdpSemantics::kUnifiedPlan, SdpSemantics::kPlanB));
Steve Anton74255ff2018-01-24 18:32:57 -08005370 ConnectFakeSignaling();
5371 auto first_sender = caller()->AddVideoTrack();
5372 caller()->AddVideoTrack();
5373
5374 caller()->CreateAndSetAndSignalOffer();
5375 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5376
5377 // Verify that there is only one receiver and it corresponds to the first
5378 // added track.
5379 auto receivers = callee()->pc()->GetReceivers();
5380 ASSERT_EQ(1u, receivers.size());
5381 EXPECT_TRUE(receivers[0]->track()->enabled());
5382 EXPECT_EQ(first_sender->track()->id(), receivers[0]->track()->id());
5383
Seth Hampson2f0d7022018-02-20 11:54:42 -08005384 MediaExpectations media_expectations;
5385 media_expectations.CalleeExpectsSomeVideo();
5386 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08005387}
5388
Steve Anton2bed3972019-01-04 17:04:30 -08005389// Test that if the initial offer tagged BUNDLE section is rejected due to its
5390// associated RtpTransceiver being stopped and another transceiver is added,
5391// then renegotiation causes the callee to receive the new video track without
5392// error.
5393// This is a regression test for bugs.webrtc.org/9954
5394TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
5395 ReOfferWithStoppedBundleTaggedTransceiver) {
5396 RTCConfiguration config;
5397 config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
5398 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
5399 ConnectFakeSignaling();
5400 auto audio_transceiver_or_error =
5401 caller()->pc()->AddTransceiver(caller()->CreateLocalAudioTrack());
5402 ASSERT_TRUE(audio_transceiver_or_error.ok());
5403 auto audio_transceiver = audio_transceiver_or_error.MoveValue();
5404
5405 caller()->CreateAndSetAndSignalOffer();
5406 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5407 {
5408 MediaExpectations media_expectations;
5409 media_expectations.CalleeExpectsSomeAudio();
5410 ASSERT_TRUE(ExpectNewFrames(media_expectations));
5411 }
5412
5413 audio_transceiver->Stop();
5414 caller()->pc()->AddTransceiver(caller()->CreateLocalVideoTrack());
5415
5416 caller()->CreateAndSetAndSignalOffer();
5417 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5418 {
5419 MediaExpectations media_expectations;
5420 media_expectations.CalleeExpectsSomeVideo();
5421 ASSERT_TRUE(ExpectNewFrames(media_expectations));
5422 }
5423}
5424
Harald Alvestrandd61f2a72019-05-08 20:20:59 +02005425#ifdef HAVE_SCTP
5426
5427TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
5428 EndToEndCallWithBundledSctpDataChannel) {
5429 ASSERT_TRUE(CreatePeerConnectionWrappers());
5430 ConnectFakeSignaling();
5431 caller()->CreateDataChannel();
5432 caller()->AddAudioVideoTracks();
5433 callee()->AddAudioVideoTracks();
5434 caller()->SetGeneratedSdpMunger(MakeSpecCompliantSctpOffer);
5435 caller()->CreateAndSetAndSignalOffer();
5436 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5437 // Ensure that media and data are multiplexed on the same DTLS transport.
5438 // This only works on Unified Plan, because transports are not exposed in plan
5439 // B.
5440 auto sctp_info = caller()->pc()->GetSctpTransport()->Information();
5441 EXPECT_EQ(sctp_info.dtls_transport(),
5442 caller()->pc()->GetSenders()[0]->dtls_transport());
5443}
5444
5445#endif // HAVE_SCTP
5446
deadbeef1dcb1642017-03-29 21:08:16 -07005447} // namespace
Mirko Bonadeiab64e8a2018-12-12 12:10:18 +01005448} // namespace webrtc
deadbeef1dcb1642017-03-29 21:08:16 -07005449
5450#endif // if !defined(THREAD_SANITIZER)