blob: 12492982caa2e6e5f869ce8ab6f19ef0bd5501ea [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
17#include <algorithm>
18#include <functional>
19#include <list>
20#include <map>
21#include <memory>
22#include <utility>
23#include <vector>
24
Karl Wiberg1b0eae32017-10-17 14:48:54 +020025#include "api/audio_codecs/builtin_audio_decoder_factory.h"
26#include "api/audio_codecs/builtin_audio_encoder_factory.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020027#include "api/fakemetricsobserver.h"
28#include "api/mediastreaminterface.h"
29#include "api/peerconnectioninterface.h"
Steve Anton8c0f7a72017-10-03 10:03:10 -070030#include "api/peerconnectionproxy.h"
Mirko Bonadeic61ce0d2017-11-21 17:04:20 +010031#include "api/rtpreceiverinterface.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020032#include "api/test/fakeconstraints.h"
33#include "media/engine/fakewebrtcvideoengine.h"
34#include "p2p/base/p2pconstants.h"
35#include "p2p/base/portinterface.h"
Steve Antonede9ca52017-10-16 13:04:27 -070036#include "p2p/base/teststunserver.h"
Jonas Orelandbdcee282017-10-10 14:01:40 +020037#include "p2p/base/testturncustomizer.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020038#include "p2p/base/testturnserver.h"
39#include "p2p/client/basicportallocator.h"
40#include "pc/dtmfsender.h"
41#include "pc/localaudiosource.h"
42#include "pc/mediasession.h"
43#include "pc/peerconnection.h"
44#include "pc/peerconnectionfactory.h"
Seth Hampson2f0d7022018-02-20 11:54:42 -080045#include "pc/rtpmediautils.h"
Steve Anton4ab68ee2017-12-19 14:26:11 -080046#include "pc/sessiondescription.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020047#include "pc/test/fakeaudiocapturemodule.h"
48#include "pc/test/fakeperiodicvideocapturer.h"
49#include "pc/test/fakertccertificategenerator.h"
50#include "pc/test/fakevideotrackrenderer.h"
51#include "pc/test/mockpeerconnectionobservers.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020052#include "rtc_base/fakenetwork.h"
Steve Antonede9ca52017-10-16 13:04:27 -070053#include "rtc_base/firewallsocketserver.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020054#include "rtc_base/gunit.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020055#include "rtc_base/virtualsocketserver.h"
Elad Alon99c3fe52017-10-13 16:29:40 +020056#include "test/gmock.h"
deadbeef1dcb1642017-03-29 21:08:16 -070057
58using cricket::ContentInfo;
59using cricket::FakeWebRtcVideoDecoder;
60using cricket::FakeWebRtcVideoDecoderFactory;
61using cricket::FakeWebRtcVideoEncoder;
62using cricket::FakeWebRtcVideoEncoderFactory;
63using cricket::MediaContentDescription;
Steve Antonede9ca52017-10-16 13:04:27 -070064using rtc::SocketAddress;
Seth Hampson2f0d7022018-02-20 11:54:42 -080065using ::testing::Combine;
Steve Antonede9ca52017-10-16 13:04:27 -070066using ::testing::ElementsAre;
67using ::testing::Values;
deadbeef1dcb1642017-03-29 21:08:16 -070068using webrtc::DataBuffer;
69using webrtc::DataChannelInterface;
70using webrtc::DtmfSender;
71using webrtc::DtmfSenderInterface;
72using webrtc::DtmfSenderObserverInterface;
73using webrtc::FakeConstraints;
Steve Anton15324772018-01-16 10:26:49 -080074using webrtc::FakeVideoTrackRenderer;
deadbeef1dcb1642017-03-29 21:08:16 -070075using webrtc::MediaConstraintsInterface;
76using webrtc::MediaStreamInterface;
77using webrtc::MediaStreamTrackInterface;
78using webrtc::MockCreateSessionDescriptionObserver;
79using webrtc::MockDataChannelObserver;
80using webrtc::MockSetSessionDescriptionObserver;
81using webrtc::MockStatsObserver;
82using webrtc::ObserverInterface;
Steve Anton8c0f7a72017-10-03 10:03:10 -070083using webrtc::PeerConnection;
deadbeef1dcb1642017-03-29 21:08:16 -070084using webrtc::PeerConnectionInterface;
Steve Anton74255ff2018-01-24 18:32:57 -080085using RTCConfiguration = PeerConnectionInterface::RTCConfiguration;
deadbeef1dcb1642017-03-29 21:08:16 -070086using webrtc::PeerConnectionFactory;
Steve Anton8c0f7a72017-10-03 10:03:10 -070087using webrtc::PeerConnectionProxy;
Steve Anton15324772018-01-16 10:26:49 -080088using webrtc::RTCErrorType;
Steve Anton7eca0932018-03-30 15:18:41 -070089using webrtc::RTCTransportStats;
Steve Anton74255ff2018-01-24 18:32:57 -080090using webrtc::RtpSenderInterface;
Mirko Bonadeic61ce0d2017-11-21 17:04:20 +010091using webrtc::RtpReceiverInterface;
Seth Hampson2f0d7022018-02-20 11:54:42 -080092using webrtc::RtpSenderInterface;
93using webrtc::RtpTransceiverDirection;
94using webrtc::RtpTransceiverInit;
95using webrtc::RtpTransceiverInterface;
Steve Antond3679212018-01-17 17:41:02 -080096using webrtc::SdpSemantics;
Steve Antona3a92c22017-12-07 10:27:41 -080097using webrtc::SdpType;
deadbeef1dcb1642017-03-29 21:08:16 -070098using webrtc::SessionDescriptionInterface;
99using webrtc::StreamCollectionInterface;
Steve Anton15324772018-01-16 10:26:49 -0800100using webrtc::VideoTrackInterface;
deadbeef1dcb1642017-03-29 21:08:16 -0700101
102namespace {
103
104static const int kDefaultTimeout = 10000;
105static const int kMaxWaitForStatsMs = 3000;
106static const int kMaxWaitForActivationMs = 5000;
107static const int kMaxWaitForFramesMs = 10000;
108// Default number of audio/video frames to wait for before considering a test
109// successful.
110static const int kDefaultExpectedAudioFrameCount = 3;
111static const int kDefaultExpectedVideoFrameCount = 3;
112
deadbeef1dcb1642017-03-29 21:08:16 -0700113static const char kDataChannelLabel[] = "data_channel";
114
115// SRTP cipher name negotiated by the tests. This must be updated if the
116// default changes.
Taylor Brandstetterfd350d72018-04-03 16:29:26 -0700117static const int kDefaultSrtpCryptoSuite = rtc::SRTP_AES128_CM_SHA1_80;
deadbeef1dcb1642017-03-29 21:08:16 -0700118static const int kDefaultSrtpCryptoSuiteGcm = rtc::SRTP_AEAD_AES_256_GCM;
119
Steve Antonede9ca52017-10-16 13:04:27 -0700120static const SocketAddress kDefaultLocalAddress("192.168.1.1", 0);
121
deadbeef1dcb1642017-03-29 21:08:16 -0700122// Helper function for constructing offer/answer options to initiate an ICE
123// restart.
124PeerConnectionInterface::RTCOfferAnswerOptions IceRestartOfferAnswerOptions() {
125 PeerConnectionInterface::RTCOfferAnswerOptions options;
126 options.ice_restart = true;
127 return options;
128}
129
deadbeefd8ad7882017-04-18 16:01:17 -0700130// Remove all stream information (SSRCs, track IDs, etc.) and "msid-semantic"
131// attribute from received SDP, simulating a legacy endpoint.
132void RemoveSsrcsAndMsids(cricket::SessionDescription* desc) {
133 for (ContentInfo& content : desc->contents()) {
Steve Antonb1c1de12017-12-21 15:14:30 -0800134 content.media_description()->mutable_streams().clear();
deadbeefd8ad7882017-04-18 16:01:17 -0700135 }
136 desc->set_msid_supported(false);
137}
138
Seth Hampson5897a6e2018-04-03 11:16:33 -0700139// Removes all stream information besides the stream ids, simulating an
140// endpoint that only signals a=msid lines to convey stream_ids.
141void RemoveSsrcsAndKeepMsids(cricket::SessionDescription* desc) {
142 for (ContentInfo& content : desc->contents()) {
143 std::vector<std::string> stream_ids;
144 if (!content.media_description()->streams().empty()) {
145 stream_ids = content.media_description()->streams()[0].stream_ids();
146 }
147 content.media_description()->mutable_streams().clear();
148 cricket::StreamParams new_stream;
149 new_stream.set_stream_ids(stream_ids);
150 content.media_description()->AddStream(new_stream);
151 }
152}
153
zhihuangf8164932017-05-19 13:09:47 -0700154int FindFirstMediaStatsIndexByKind(
155 const std::string& kind,
156 const std::vector<const webrtc::RTCMediaStreamTrackStats*>&
157 media_stats_vec) {
158 for (size_t i = 0; i < media_stats_vec.size(); i++) {
159 if (media_stats_vec[i]->kind.ValueToString() == kind) {
160 return i;
161 }
162 }
163 return -1;
164}
165
deadbeef1dcb1642017-03-29 21:08:16 -0700166class SignalingMessageReceiver {
167 public:
Steve Antona3a92c22017-12-07 10:27:41 -0800168 virtual void ReceiveSdpMessage(SdpType type, const std::string& msg) = 0;
deadbeef1dcb1642017-03-29 21:08:16 -0700169 virtual void ReceiveIceMessage(const std::string& sdp_mid,
170 int sdp_mline_index,
171 const std::string& msg) = 0;
172
173 protected:
174 SignalingMessageReceiver() {}
175 virtual ~SignalingMessageReceiver() {}
176};
177
178class MockRtpReceiverObserver : public webrtc::RtpReceiverObserverInterface {
179 public:
180 explicit MockRtpReceiverObserver(cricket::MediaType media_type)
181 : expected_media_type_(media_type) {}
182
183 void OnFirstPacketReceived(cricket::MediaType media_type) override {
184 ASSERT_EQ(expected_media_type_, media_type);
185 first_packet_received_ = true;
186 }
187
188 bool first_packet_received() const { return first_packet_received_; }
189
190 virtual ~MockRtpReceiverObserver() {}
191
192 private:
193 bool first_packet_received_ = false;
194 cricket::MediaType expected_media_type_;
195};
196
197// Helper class that wraps a peer connection, observes it, and can accept
198// signaling messages from another wrapper.
199//
200// Uses a fake network, fake A/V capture, and optionally fake
201// encoders/decoders, though they aren't used by default since they don't
202// advertise support of any codecs.
Steve Anton94286cb2017-09-26 16:20:19 -0700203// TODO(steveanton): See how this could become a subclass of
Seth Hampson2f0d7022018-02-20 11:54:42 -0800204// PeerConnectionWrapper defined in peerconnectionwrapper.h.
deadbeef1dcb1642017-03-29 21:08:16 -0700205class PeerConnectionWrapper : public webrtc::PeerConnectionObserver,
Steve Anton15324772018-01-16 10:26:49 -0800206 public SignalingMessageReceiver {
deadbeef1dcb1642017-03-29 21:08:16 -0700207 public:
208 // Different factory methods for convenience.
209 // TODO(deadbeef): Could use the pattern of:
210 //
211 // PeerConnectionWrapper =
212 // WrapperBuilder.WithConfig(...).WithOptions(...).build();
213 //
214 // To reduce some code duplication.
215 static PeerConnectionWrapper* CreateWithDtlsIdentityStore(
216 const std::string& debug_name,
217 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator,
218 rtc::Thread* network_thread,
219 rtc::Thread* worker_thread) {
220 PeerConnectionWrapper* client(new PeerConnectionWrapper(debug_name));
221 if (!client->Init(nullptr, nullptr, nullptr, std::move(cert_generator),
222 network_thread, worker_thread)) {
223 delete client;
224 return nullptr;
225 }
226 return client;
227 }
228
deadbeef2f425aa2017-04-14 10:41:32 -0700229 webrtc::PeerConnectionFactoryInterface* pc_factory() const {
230 return peer_connection_factory_.get();
231 }
232
deadbeef1dcb1642017-03-29 21:08:16 -0700233 webrtc::PeerConnectionInterface* pc() const { return peer_connection_.get(); }
234
235 // If a signaling message receiver is set (via ConnectFakeSignaling), this
236 // will set the whole offer/answer exchange in motion. Just need to wait for
237 // the signaling state to reach "stable".
238 void CreateAndSetAndSignalOffer() {
239 auto offer = CreateOffer();
240 ASSERT_NE(nullptr, offer);
241 EXPECT_TRUE(SetLocalDescriptionAndSendSdpMessage(std::move(offer)));
242 }
243
244 // Sets the options to be used when CreateAndSetAndSignalOffer is called, or
245 // when a remote offer is received (via fake signaling) and an answer is
246 // generated. By default, uses default options.
247 void SetOfferAnswerOptions(
248 const PeerConnectionInterface::RTCOfferAnswerOptions& options) {
249 offer_answer_options_ = options;
250 }
251
252 // Set a callback to be invoked when SDP is received via the fake signaling
253 // channel, which provides an opportunity to munge (modify) the SDP. This is
254 // used to test SDP being applied that a PeerConnection would normally not
255 // generate, but a non-JSEP endpoint might.
256 void SetReceivedSdpMunger(
257 std::function<void(cricket::SessionDescription*)> munger) {
Mirko Bonadeic61ce0d2017-11-21 17:04:20 +0100258 received_sdp_munger_ = std::move(munger);
deadbeef1dcb1642017-03-29 21:08:16 -0700259 }
260
deadbeefc964d0b2017-04-03 10:03:35 -0700261 // Similar to the above, but this is run on SDP immediately after it's
deadbeef1dcb1642017-03-29 21:08:16 -0700262 // generated.
263 void SetGeneratedSdpMunger(
264 std::function<void(cricket::SessionDescription*)> munger) {
Mirko Bonadeic61ce0d2017-11-21 17:04:20 +0100265 generated_sdp_munger_ = std::move(munger);
deadbeef1dcb1642017-03-29 21:08:16 -0700266 }
267
Seth Hampson2f0d7022018-02-20 11:54:42 -0800268 // Set a callback to be invoked when a remote offer is received via the fake
269 // signaling channel. This provides an opportunity to change the
270 // PeerConnection state before an answer is created and sent to the caller.
271 void SetRemoteOfferHandler(std::function<void()> handler) {
272 remote_offer_handler_ = std::move(handler);
273 }
274
Steve Antonede9ca52017-10-16 13:04:27 -0700275 // Every ICE connection state in order that has been seen by the observer.
276 std::vector<PeerConnectionInterface::IceConnectionState>
277 ice_connection_state_history() const {
278 return ice_connection_state_history_;
279 }
Steve Anton6f25b092017-10-23 09:39:20 -0700280 void clear_ice_connection_state_history() {
281 ice_connection_state_history_.clear();
282 }
Steve Antonede9ca52017-10-16 13:04:27 -0700283
284 // Every ICE gathering state in order that has been seen by the observer.
285 std::vector<PeerConnectionInterface::IceGatheringState>
286 ice_gathering_state_history() const {
287 return ice_gathering_state_history_;
deadbeef1dcb1642017-03-29 21:08:16 -0700288 }
289
Steve Anton15324772018-01-16 10:26:49 -0800290 void AddAudioVideoTracks() {
291 AddAudioTrack();
292 AddVideoTrack();
deadbeef1dcb1642017-03-29 21:08:16 -0700293 }
294
Steve Anton74255ff2018-01-24 18:32:57 -0800295 rtc::scoped_refptr<RtpSenderInterface> AddAudioTrack() {
296 return AddTrack(CreateLocalAudioTrack());
297 }
deadbeef1dcb1642017-03-29 21:08:16 -0700298
Steve Anton74255ff2018-01-24 18:32:57 -0800299 rtc::scoped_refptr<RtpSenderInterface> AddVideoTrack() {
300 return AddTrack(CreateLocalVideoTrack());
301 }
deadbeef1dcb1642017-03-29 21:08:16 -0700302
303 rtc::scoped_refptr<webrtc::AudioTrackInterface> CreateLocalAudioTrack() {
304 FakeConstraints constraints;
305 // Disable highpass filter so that we can get all the test audio frames.
306 constraints.AddMandatory(MediaConstraintsInterface::kHighpassFilter, false);
307 rtc::scoped_refptr<webrtc::AudioSourceInterface> source =
308 peer_connection_factory_->CreateAudioSource(&constraints);
309 // TODO(perkj): Test audio source when it is implemented. Currently audio
310 // always use the default input.
deadbeefb1a15d72017-09-07 14:12:05 -0700311 return peer_connection_factory_->CreateAudioTrack(rtc::CreateRandomUuid(),
deadbeef1dcb1642017-03-29 21:08:16 -0700312 source);
313 }
314
315 rtc::scoped_refptr<webrtc::VideoTrackInterface> CreateLocalVideoTrack() {
deadbeefb1a15d72017-09-07 14:12:05 -0700316 return CreateLocalVideoTrackInternal(FakeConstraints(),
317 webrtc::kVideoRotation_0);
deadbeef1dcb1642017-03-29 21:08:16 -0700318 }
319
320 rtc::scoped_refptr<webrtc::VideoTrackInterface>
321 CreateLocalVideoTrackWithConstraints(const FakeConstraints& constraints) {
deadbeefb1a15d72017-09-07 14:12:05 -0700322 return CreateLocalVideoTrackInternal(constraints, webrtc::kVideoRotation_0);
deadbeef1dcb1642017-03-29 21:08:16 -0700323 }
324
325 rtc::scoped_refptr<webrtc::VideoTrackInterface>
326 CreateLocalVideoTrackWithRotation(webrtc::VideoRotation rotation) {
deadbeefb1a15d72017-09-07 14:12:05 -0700327 return CreateLocalVideoTrackInternal(FakeConstraints(), rotation);
deadbeef1dcb1642017-03-29 21:08:16 -0700328 }
329
Steve Anton74255ff2018-01-24 18:32:57 -0800330 rtc::scoped_refptr<RtpSenderInterface> AddTrack(
331 rtc::scoped_refptr<MediaStreamTrackInterface> track,
Seth Hampson845e8782018-03-02 11:34:10 -0800332 const std::vector<std::string>& stream_ids = {}) {
333 auto result = pc()->AddTrack(track, stream_ids);
Steve Anton15324772018-01-16 10:26:49 -0800334 EXPECT_EQ(RTCErrorType::NONE, result.error().type());
Steve Anton74255ff2018-01-24 18:32:57 -0800335 return result.MoveValue();
336 }
337
338 std::vector<rtc::scoped_refptr<RtpReceiverInterface>> GetReceiversOfType(
339 cricket::MediaType media_type) {
340 std::vector<rtc::scoped_refptr<RtpReceiverInterface>> receivers;
341 for (auto receiver : pc()->GetReceivers()) {
342 if (receiver->media_type() == media_type) {
343 receivers.push_back(receiver);
344 }
345 }
346 return receivers;
deadbeef1dcb1642017-03-29 21:08:16 -0700347 }
348
Seth Hampson2f0d7022018-02-20 11:54:42 -0800349 rtc::scoped_refptr<RtpTransceiverInterface> GetFirstTransceiverOfType(
350 cricket::MediaType media_type) {
351 for (auto transceiver : pc()->GetTransceivers()) {
352 if (transceiver->receiver()->media_type() == media_type) {
353 return transceiver;
354 }
355 }
356 return nullptr;
357 }
358
deadbeef1dcb1642017-03-29 21:08:16 -0700359 bool SignalingStateStable() {
360 return pc()->signaling_state() == webrtc::PeerConnectionInterface::kStable;
361 }
362
363 void CreateDataChannel() { CreateDataChannel(nullptr); }
364
365 void CreateDataChannel(const webrtc::DataChannelInit* init) {
Steve Antonda6c0952017-10-23 11:41:54 -0700366 CreateDataChannel(kDataChannelLabel, init);
367 }
368
369 void CreateDataChannel(const std::string& label,
370 const webrtc::DataChannelInit* init) {
371 data_channel_ = pc()->CreateDataChannel(label, init);
deadbeef1dcb1642017-03-29 21:08:16 -0700372 ASSERT_TRUE(data_channel_.get() != nullptr);
373 data_observer_.reset(new MockDataChannelObserver(data_channel_));
374 }
375
376 DataChannelInterface* data_channel() { return data_channel_; }
377 const MockDataChannelObserver* data_observer() const {
378 return data_observer_.get();
379 }
380
381 int audio_frames_received() const {
382 return fake_audio_capture_module_->frames_received();
383 }
384
385 // Takes minimum of video frames received for each track.
386 //
387 // Can be used like:
388 // EXPECT_GE(expected_frames, min_video_frames_received_per_track());
389 //
390 // To ensure that all video tracks received at least a certain number of
391 // frames.
392 int min_video_frames_received_per_track() const {
393 int min_frames = INT_MAX;
394 if (video_decoder_factory_enabled_) {
395 const std::vector<FakeWebRtcVideoDecoder*>& decoders =
396 fake_video_decoder_factory_->decoders();
397 if (decoders.empty()) {
398 return 0;
399 }
400 for (FakeWebRtcVideoDecoder* decoder : decoders) {
401 min_frames = std::min(min_frames, decoder->GetNumFramesReceived());
402 }
403 return min_frames;
404 } else {
405 if (fake_video_renderers_.empty()) {
406 return 0;
407 }
408
409 for (const auto& pair : fake_video_renderers_) {
410 min_frames = std::min(min_frames, pair.second->num_rendered_frames());
411 }
412 return min_frames;
413 }
414 }
415
416 // In contrast to the above, sums the video frames received for all tracks.
417 // Can be used to verify that no video frames were received, or that the
418 // counts didn't increase.
419 int total_video_frames_received() const {
420 int total = 0;
421 if (video_decoder_factory_enabled_) {
422 const std::vector<FakeWebRtcVideoDecoder*>& decoders =
423 fake_video_decoder_factory_->decoders();
424 for (const FakeWebRtcVideoDecoder* decoder : decoders) {
425 total += decoder->GetNumFramesReceived();
426 }
427 } else {
428 for (const auto& pair : fake_video_renderers_) {
429 total += pair.second->num_rendered_frames();
430 }
431 for (const auto& renderer : removed_fake_video_renderers_) {
432 total += renderer->num_rendered_frames();
433 }
434 }
435 return total;
436 }
437
438 // Returns a MockStatsObserver in a state after stats gathering finished,
439 // which can be used to access the gathered stats.
deadbeefd8ad7882017-04-18 16:01:17 -0700440 rtc::scoped_refptr<MockStatsObserver> OldGetStatsForTrack(
deadbeef1dcb1642017-03-29 21:08:16 -0700441 webrtc::MediaStreamTrackInterface* track) {
442 rtc::scoped_refptr<MockStatsObserver> observer(
443 new rtc::RefCountedObject<MockStatsObserver>());
444 EXPECT_TRUE(peer_connection_->GetStats(
445 observer, nullptr, PeerConnectionInterface::kStatsOutputLevelStandard));
446 EXPECT_TRUE_WAIT(observer->called(), kDefaultTimeout);
447 return observer;
448 }
449
450 // Version that doesn't take a track "filter", and gathers all stats.
deadbeefd8ad7882017-04-18 16:01:17 -0700451 rtc::scoped_refptr<MockStatsObserver> OldGetStats() {
452 return OldGetStatsForTrack(nullptr);
453 }
454
455 // Synchronously gets stats and returns them. If it times out, fails the test
456 // and returns null.
457 rtc::scoped_refptr<const webrtc::RTCStatsReport> NewGetStats() {
458 rtc::scoped_refptr<webrtc::MockRTCStatsCollectorCallback> callback(
459 new rtc::RefCountedObject<webrtc::MockRTCStatsCollectorCallback>());
460 peer_connection_->GetStats(callback);
461 EXPECT_TRUE_WAIT(callback->called(), kDefaultTimeout);
462 return callback->report();
deadbeef1dcb1642017-03-29 21:08:16 -0700463 }
464
465 int rendered_width() {
466 EXPECT_FALSE(fake_video_renderers_.empty());
467 return fake_video_renderers_.empty()
468 ? 0
469 : fake_video_renderers_.begin()->second->width();
470 }
471
472 int rendered_height() {
473 EXPECT_FALSE(fake_video_renderers_.empty());
474 return fake_video_renderers_.empty()
475 ? 0
476 : fake_video_renderers_.begin()->second->height();
477 }
478
479 double rendered_aspect_ratio() {
480 if (rendered_height() == 0) {
481 return 0.0;
482 }
483 return static_cast<double>(rendered_width()) / rendered_height();
484 }
485
486 webrtc::VideoRotation rendered_rotation() {
487 EXPECT_FALSE(fake_video_renderers_.empty());
488 return fake_video_renderers_.empty()
489 ? webrtc::kVideoRotation_0
490 : fake_video_renderers_.begin()->second->rotation();
491 }
492
493 int local_rendered_width() {
494 return local_video_renderer_ ? local_video_renderer_->width() : 0;
495 }
496
497 int local_rendered_height() {
498 return local_video_renderer_ ? local_video_renderer_->height() : 0;
499 }
500
501 double local_rendered_aspect_ratio() {
502 if (local_rendered_height() == 0) {
503 return 0.0;
504 }
505 return static_cast<double>(local_rendered_width()) /
506 local_rendered_height();
507 }
508
509 size_t number_of_remote_streams() {
510 if (!pc()) {
511 return 0;
512 }
513 return pc()->remote_streams()->count();
514 }
515
516 StreamCollectionInterface* remote_streams() const {
517 if (!pc()) {
518 ADD_FAILURE();
519 return nullptr;
520 }
521 return pc()->remote_streams();
522 }
523
524 StreamCollectionInterface* local_streams() {
525 if (!pc()) {
526 ADD_FAILURE();
527 return nullptr;
528 }
529 return pc()->local_streams();
530 }
531
532 webrtc::PeerConnectionInterface::SignalingState signaling_state() {
533 return pc()->signaling_state();
534 }
535
536 webrtc::PeerConnectionInterface::IceConnectionState ice_connection_state() {
537 return pc()->ice_connection_state();
538 }
539
540 webrtc::PeerConnectionInterface::IceGatheringState ice_gathering_state() {
541 return pc()->ice_gathering_state();
542 }
543
544 // Returns a MockRtpReceiverObserver for each RtpReceiver returned by
545 // GetReceivers. They're updated automatically when a remote offer/answer
546 // from the fake signaling channel is applied, or when
547 // ResetRtpReceiverObservers below is called.
548 const std::vector<std::unique_ptr<MockRtpReceiverObserver>>&
549 rtp_receiver_observers() {
550 return rtp_receiver_observers_;
551 }
552
553 void ResetRtpReceiverObservers() {
554 rtp_receiver_observers_.clear();
Mirko Bonadeic61ce0d2017-11-21 17:04:20 +0100555 for (const rtc::scoped_refptr<RtpReceiverInterface>& receiver :
556 pc()->GetReceivers()) {
deadbeef1dcb1642017-03-29 21:08:16 -0700557 std::unique_ptr<MockRtpReceiverObserver> observer(
558 new MockRtpReceiverObserver(receiver->media_type()));
559 receiver->SetObserver(observer.get());
560 rtp_receiver_observers_.push_back(std::move(observer));
561 }
562 }
563
Steve Antonede9ca52017-10-16 13:04:27 -0700564 rtc::FakeNetworkManager* network() const {
565 return fake_network_manager_.get();
566 }
567 cricket::PortAllocator* port_allocator() const { return port_allocator_; }
568
deadbeef1dcb1642017-03-29 21:08:16 -0700569 private:
570 explicit PeerConnectionWrapper(const std::string& debug_name)
571 : debug_name_(debug_name) {}
572
573 bool Init(
574 const MediaConstraintsInterface* constraints,
575 const PeerConnectionFactory::Options* options,
576 const PeerConnectionInterface::RTCConfiguration* config,
577 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator,
578 rtc::Thread* network_thread,
579 rtc::Thread* worker_thread) {
580 // There's an error in this test code if Init ends up being called twice.
581 RTC_DCHECK(!peer_connection_);
582 RTC_DCHECK(!peer_connection_factory_);
583
584 fake_network_manager_.reset(new rtc::FakeNetworkManager());
Steve Antonede9ca52017-10-16 13:04:27 -0700585 fake_network_manager_->AddInterface(kDefaultLocalAddress);
deadbeef1dcb1642017-03-29 21:08:16 -0700586
587 std::unique_ptr<cricket::PortAllocator> port_allocator(
588 new cricket::BasicPortAllocator(fake_network_manager_.get()));
Steve Antonede9ca52017-10-16 13:04:27 -0700589 port_allocator_ = port_allocator.get();
deadbeef1dcb1642017-03-29 21:08:16 -0700590 fake_audio_capture_module_ = FakeAudioCaptureModule::Create();
591 if (!fake_audio_capture_module_) {
592 return false;
593 }
594 // Note that these factories don't end up getting used unless supported
595 // codecs are added to them.
596 fake_video_decoder_factory_ = new FakeWebRtcVideoDecoderFactory();
597 fake_video_encoder_factory_ = new FakeWebRtcVideoEncoderFactory();
598 rtc::Thread* const signaling_thread = rtc::Thread::Current();
599 peer_connection_factory_ = webrtc::CreatePeerConnectionFactory(
600 network_thread, worker_thread, signaling_thread,
Karl Wiberg1b0eae32017-10-17 14:48:54 +0200601 fake_audio_capture_module_, webrtc::CreateBuiltinAudioEncoderFactory(),
602 webrtc::CreateBuiltinAudioDecoderFactory(), fake_video_encoder_factory_,
deadbeef1dcb1642017-03-29 21:08:16 -0700603 fake_video_decoder_factory_);
604 if (!peer_connection_factory_) {
605 return false;
606 }
607 if (options) {
608 peer_connection_factory_->SetOptions(*options);
609 }
Seth Hampson2f0d7022018-02-20 11:54:42 -0800610 if (config) {
611 sdp_semantics_ = config->sdp_semantics;
612 }
deadbeef1dcb1642017-03-29 21:08:16 -0700613 peer_connection_ =
614 CreatePeerConnection(std::move(port_allocator), constraints, config,
615 std::move(cert_generator));
616 return peer_connection_.get() != nullptr;
617 }
618
619 rtc::scoped_refptr<webrtc::PeerConnectionInterface> CreatePeerConnection(
620 std::unique_ptr<cricket::PortAllocator> port_allocator,
621 const MediaConstraintsInterface* constraints,
622 const PeerConnectionInterface::RTCConfiguration* config,
623 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator) {
624 PeerConnectionInterface::RTCConfiguration modified_config;
625 // If |config| is null, this will result in a default configuration being
626 // used.
627 if (config) {
628 modified_config = *config;
629 }
630 // Disable resolution adaptation; we don't want it interfering with the
631 // test results.
632 // TODO(deadbeef): Do something more robust. Since we're testing for aspect
633 // ratios and not specific resolutions, is this even necessary?
634 modified_config.set_cpu_adaptation(false);
635
636 return peer_connection_factory_->CreatePeerConnection(
637 modified_config, constraints, std::move(port_allocator),
638 std::move(cert_generator), this);
639 }
640
641 void set_signaling_message_receiver(
642 SignalingMessageReceiver* signaling_message_receiver) {
643 signaling_message_receiver_ = signaling_message_receiver;
644 }
645
646 void set_signaling_delay_ms(int delay_ms) { signaling_delay_ms_ = delay_ms; }
647
Steve Antonede9ca52017-10-16 13:04:27 -0700648 void set_signal_ice_candidates(bool signal) {
649 signal_ice_candidates_ = signal;
650 }
651
deadbeef1dcb1642017-03-29 21:08:16 -0700652 void EnableVideoDecoderFactory() {
653 video_decoder_factory_enabled_ = true;
654 fake_video_decoder_factory_->AddSupportedVideoCodecType(
655 webrtc::kVideoCodecVP8);
656 }
657
658 rtc::scoped_refptr<webrtc::VideoTrackInterface> CreateLocalVideoTrackInternal(
deadbeef1dcb1642017-03-29 21:08:16 -0700659 const FakeConstraints& constraints,
660 webrtc::VideoRotation rotation) {
661 // Set max frame rate to 10fps to reduce the risk of test flakiness.
662 // TODO(deadbeef): Do something more robust.
663 FakeConstraints source_constraints = constraints;
664 source_constraints.SetMandatoryMaxFrameRate(10);
665
666 cricket::FakeVideoCapturer* fake_capturer =
667 new webrtc::FakePeriodicVideoCapturer();
668 fake_capturer->SetRotation(rotation);
669 video_capturers_.push_back(fake_capturer);
670 rtc::scoped_refptr<webrtc::VideoTrackSourceInterface> source =
671 peer_connection_factory_->CreateVideoSource(fake_capturer,
672 &source_constraints);
673 rtc::scoped_refptr<webrtc::VideoTrackInterface> track(
deadbeefb1a15d72017-09-07 14:12:05 -0700674 peer_connection_factory_->CreateVideoTrack(rtc::CreateRandomUuid(),
675 source));
deadbeef1dcb1642017-03-29 21:08:16 -0700676 if (!local_video_renderer_) {
677 local_video_renderer_.reset(new webrtc::FakeVideoTrackRenderer(track));
678 }
679 return track;
680 }
681
682 void HandleIncomingOffer(const std::string& msg) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100683 RTC_LOG(LS_INFO) << debug_name_ << ": HandleIncomingOffer";
Steve Antona3a92c22017-12-07 10:27:41 -0800684 std::unique_ptr<SessionDescriptionInterface> desc =
685 webrtc::CreateSessionDescription(SdpType::kOffer, msg);
deadbeef1dcb1642017-03-29 21:08:16 -0700686 if (received_sdp_munger_) {
687 received_sdp_munger_(desc->description());
688 }
689
690 EXPECT_TRUE(SetRemoteDescription(std::move(desc)));
691 // Setting a remote description may have changed the number of receivers,
692 // so reset the receiver observers.
693 ResetRtpReceiverObservers();
Seth Hampson2f0d7022018-02-20 11:54:42 -0800694 if (remote_offer_handler_) {
695 remote_offer_handler_();
696 }
deadbeef1dcb1642017-03-29 21:08:16 -0700697 auto answer = CreateAnswer();
698 ASSERT_NE(nullptr, answer);
699 EXPECT_TRUE(SetLocalDescriptionAndSendSdpMessage(std::move(answer)));
700 }
701
702 void HandleIncomingAnswer(const std::string& msg) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100703 RTC_LOG(LS_INFO) << debug_name_ << ": HandleIncomingAnswer";
Steve Antona3a92c22017-12-07 10:27:41 -0800704 std::unique_ptr<SessionDescriptionInterface> desc =
705 webrtc::CreateSessionDescription(SdpType::kAnswer, msg);
deadbeef1dcb1642017-03-29 21:08:16 -0700706 if (received_sdp_munger_) {
707 received_sdp_munger_(desc->description());
708 }
709
710 EXPECT_TRUE(SetRemoteDescription(std::move(desc)));
711 // Set the RtpReceiverObserver after receivers are created.
712 ResetRtpReceiverObservers();
713 }
714
715 // Returns null on failure.
716 std::unique_ptr<SessionDescriptionInterface> CreateOffer() {
717 rtc::scoped_refptr<MockCreateSessionDescriptionObserver> observer(
718 new rtc::RefCountedObject<MockCreateSessionDescriptionObserver>());
719 pc()->CreateOffer(observer, offer_answer_options_);
720 return WaitForDescriptionFromObserver(observer);
721 }
722
723 // Returns null on failure.
724 std::unique_ptr<SessionDescriptionInterface> CreateAnswer() {
725 rtc::scoped_refptr<MockCreateSessionDescriptionObserver> observer(
726 new rtc::RefCountedObject<MockCreateSessionDescriptionObserver>());
727 pc()->CreateAnswer(observer, offer_answer_options_);
728 return WaitForDescriptionFromObserver(observer);
729 }
730
731 std::unique_ptr<SessionDescriptionInterface> WaitForDescriptionFromObserver(
Mirko Bonadeic61ce0d2017-11-21 17:04:20 +0100732 MockCreateSessionDescriptionObserver* observer) {
deadbeef1dcb1642017-03-29 21:08:16 -0700733 EXPECT_EQ_WAIT(true, observer->called(), kDefaultTimeout);
734 if (!observer->result()) {
735 return nullptr;
736 }
737 auto description = observer->MoveDescription();
738 if (generated_sdp_munger_) {
739 generated_sdp_munger_(description->description());
740 }
741 return description;
742 }
743
744 // Setting the local description and sending the SDP message over the fake
745 // signaling channel are combined into the same method because the SDP
746 // message needs to be sent as soon as SetLocalDescription finishes, without
747 // waiting for the observer to be called. This ensures that ICE candidates
748 // don't outrace the description.
749 bool SetLocalDescriptionAndSendSdpMessage(
750 std::unique_ptr<SessionDescriptionInterface> desc) {
751 rtc::scoped_refptr<MockSetSessionDescriptionObserver> observer(
752 new rtc::RefCountedObject<MockSetSessionDescriptionObserver>());
Mirko Bonadei675513b2017-11-09 11:09:25 +0100753 RTC_LOG(LS_INFO) << debug_name_ << ": SetLocalDescriptionAndSendSdpMessage";
Steve Antona3a92c22017-12-07 10:27:41 -0800754 SdpType type = desc->GetType();
deadbeef1dcb1642017-03-29 21:08:16 -0700755 std::string sdp;
756 EXPECT_TRUE(desc->ToString(&sdp));
757 pc()->SetLocalDescription(observer, desc.release());
Seth Hampson2f0d7022018-02-20 11:54:42 -0800758 if (sdp_semantics_ == SdpSemantics::kUnifiedPlan) {
759 RemoveUnusedVideoRenderers();
760 }
deadbeef1dcb1642017-03-29 21:08:16 -0700761 // As mentioned above, we need to send the message immediately after
762 // SetLocalDescription.
763 SendSdpMessage(type, sdp);
764 EXPECT_TRUE_WAIT(observer->called(), kDefaultTimeout);
765 return true;
766 }
767
768 bool SetRemoteDescription(std::unique_ptr<SessionDescriptionInterface> desc) {
769 rtc::scoped_refptr<MockSetSessionDescriptionObserver> observer(
770 new rtc::RefCountedObject<MockSetSessionDescriptionObserver>());
Mirko Bonadei675513b2017-11-09 11:09:25 +0100771 RTC_LOG(LS_INFO) << debug_name_ << ": SetRemoteDescription";
deadbeef1dcb1642017-03-29 21:08:16 -0700772 pc()->SetRemoteDescription(observer, desc.release());
Seth Hampson2f0d7022018-02-20 11:54:42 -0800773 if (sdp_semantics_ == SdpSemantics::kUnifiedPlan) {
774 RemoveUnusedVideoRenderers();
775 }
deadbeef1dcb1642017-03-29 21:08:16 -0700776 EXPECT_TRUE_WAIT(observer->called(), kDefaultTimeout);
777 return observer->result();
778 }
779
Seth Hampson2f0d7022018-02-20 11:54:42 -0800780 // This is a work around to remove unused fake_video_renderers from
781 // transceivers that have either stopped or are no longer receiving.
782 void RemoveUnusedVideoRenderers() {
783 auto transceivers = pc()->GetTransceivers();
784 for (auto& transceiver : transceivers) {
785 if (transceiver->receiver()->media_type() != cricket::MEDIA_TYPE_VIDEO) {
786 continue;
787 }
788 // Remove fake video renderers from any stopped transceivers.
789 if (transceiver->stopped()) {
790 auto it =
791 fake_video_renderers_.find(transceiver->receiver()->track()->id());
792 if (it != fake_video_renderers_.end()) {
793 fake_video_renderers_.erase(it);
794 }
795 }
796 // Remove fake video renderers from any transceivers that are no longer
797 // receiving.
798 if ((transceiver->current_direction() &&
799 !webrtc::RtpTransceiverDirectionHasRecv(
800 *transceiver->current_direction()))) {
801 auto it =
802 fake_video_renderers_.find(transceiver->receiver()->track()->id());
803 if (it != fake_video_renderers_.end()) {
804 fake_video_renderers_.erase(it);
805 }
806 }
807 }
808 }
809
deadbeef1dcb1642017-03-29 21:08:16 -0700810 // Simulate sending a blob of SDP with delay |signaling_delay_ms_| (0 by
811 // default).
Steve Antona3a92c22017-12-07 10:27:41 -0800812 void SendSdpMessage(SdpType type, const std::string& msg) {
deadbeef1dcb1642017-03-29 21:08:16 -0700813 if (signaling_delay_ms_ == 0) {
814 RelaySdpMessageIfReceiverExists(type, msg);
815 } else {
816 invoker_.AsyncInvokeDelayed<void>(
817 RTC_FROM_HERE, rtc::Thread::Current(),
818 rtc::Bind(&PeerConnectionWrapper::RelaySdpMessageIfReceiverExists,
819 this, type, msg),
820 signaling_delay_ms_);
821 }
822 }
823
Steve Antona3a92c22017-12-07 10:27:41 -0800824 void RelaySdpMessageIfReceiverExists(SdpType type, const std::string& msg) {
deadbeef1dcb1642017-03-29 21:08:16 -0700825 if (signaling_message_receiver_) {
826 signaling_message_receiver_->ReceiveSdpMessage(type, msg);
827 }
828 }
829
830 // Simulate trickling an ICE candidate with delay |signaling_delay_ms_| (0 by
831 // default).
832 void SendIceMessage(const std::string& sdp_mid,
833 int sdp_mline_index,
834 const std::string& msg) {
835 if (signaling_delay_ms_ == 0) {
836 RelayIceMessageIfReceiverExists(sdp_mid, sdp_mline_index, msg);
837 } else {
838 invoker_.AsyncInvokeDelayed<void>(
839 RTC_FROM_HERE, rtc::Thread::Current(),
840 rtc::Bind(&PeerConnectionWrapper::RelayIceMessageIfReceiverExists,
841 this, sdp_mid, sdp_mline_index, msg),
842 signaling_delay_ms_);
843 }
844 }
845
846 void RelayIceMessageIfReceiverExists(const std::string& sdp_mid,
847 int sdp_mline_index,
848 const std::string& msg) {
849 if (signaling_message_receiver_) {
850 signaling_message_receiver_->ReceiveIceMessage(sdp_mid, sdp_mline_index,
851 msg);
852 }
853 }
854
855 // SignalingMessageReceiver callbacks.
Steve Antona3a92c22017-12-07 10:27:41 -0800856 void ReceiveSdpMessage(SdpType type, const std::string& msg) override {
857 if (type == SdpType::kOffer) {
deadbeef1dcb1642017-03-29 21:08:16 -0700858 HandleIncomingOffer(msg);
859 } else {
860 HandleIncomingAnswer(msg);
861 }
862 }
863
864 void ReceiveIceMessage(const std::string& sdp_mid,
865 int sdp_mline_index,
866 const std::string& msg) override {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100867 RTC_LOG(LS_INFO) << debug_name_ << ": ReceiveIceMessage";
deadbeef1dcb1642017-03-29 21:08:16 -0700868 std::unique_ptr<webrtc::IceCandidateInterface> candidate(
869 webrtc::CreateIceCandidate(sdp_mid, sdp_mline_index, msg, nullptr));
870 EXPECT_TRUE(pc()->AddIceCandidate(candidate.get()));
871 }
872
873 // PeerConnectionObserver callbacks.
874 void OnSignalingChange(
875 webrtc::PeerConnectionInterface::SignalingState new_state) override {
876 EXPECT_EQ(pc()->signaling_state(), new_state);
877 }
Steve Anton15324772018-01-16 10:26:49 -0800878 void OnAddTrack(rtc::scoped_refptr<RtpReceiverInterface> receiver,
879 const std::vector<rtc::scoped_refptr<MediaStreamInterface>>&
880 streams) override {
881 if (receiver->media_type() == cricket::MEDIA_TYPE_VIDEO) {
882 rtc::scoped_refptr<VideoTrackInterface> video_track(
883 static_cast<VideoTrackInterface*>(receiver->track().get()));
884 ASSERT_TRUE(fake_video_renderers_.find(video_track->id()) ==
deadbeef1dcb1642017-03-29 21:08:16 -0700885 fake_video_renderers_.end());
Steve Anton15324772018-01-16 10:26:49 -0800886 fake_video_renderers_[video_track->id()] =
887 rtc::MakeUnique<FakeVideoTrackRenderer>(video_track);
deadbeef1dcb1642017-03-29 21:08:16 -0700888 }
889 }
Steve Anton15324772018-01-16 10:26:49 -0800890 void OnRemoveTrack(
891 rtc::scoped_refptr<RtpReceiverInterface> receiver) override {
892 if (receiver->media_type() == cricket::MEDIA_TYPE_VIDEO) {
893 auto it = fake_video_renderers_.find(receiver->track()->id());
894 RTC_DCHECK(it != fake_video_renderers_.end());
895 fake_video_renderers_.erase(it);
896 }
897 }
deadbeef1dcb1642017-03-29 21:08:16 -0700898 void OnRenegotiationNeeded() override {}
899 void OnIceConnectionChange(
900 webrtc::PeerConnectionInterface::IceConnectionState new_state) override {
901 EXPECT_EQ(pc()->ice_connection_state(), new_state);
Steve Antonede9ca52017-10-16 13:04:27 -0700902 ice_connection_state_history_.push_back(new_state);
deadbeef1dcb1642017-03-29 21:08:16 -0700903 }
904 void OnIceGatheringChange(
905 webrtc::PeerConnectionInterface::IceGatheringState new_state) override {
deadbeef1dcb1642017-03-29 21:08:16 -0700906 EXPECT_EQ(pc()->ice_gathering_state(), new_state);
Steve Antonede9ca52017-10-16 13:04:27 -0700907 ice_gathering_state_history_.push_back(new_state);
deadbeef1dcb1642017-03-29 21:08:16 -0700908 }
909 void OnIceCandidate(const webrtc::IceCandidateInterface* candidate) override {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100910 RTC_LOG(LS_INFO) << debug_name_ << ": OnIceCandidate";
deadbeef1dcb1642017-03-29 21:08:16 -0700911
912 std::string ice_sdp;
913 EXPECT_TRUE(candidate->ToString(&ice_sdp));
Steve Antonede9ca52017-10-16 13:04:27 -0700914 if (signaling_message_receiver_ == nullptr || !signal_ice_candidates_) {
deadbeef1dcb1642017-03-29 21:08:16 -0700915 // Remote party may be deleted.
916 return;
917 }
918 SendIceMessage(candidate->sdp_mid(), candidate->sdp_mline_index(), ice_sdp);
919 }
920 void OnDataChannel(
921 rtc::scoped_refptr<DataChannelInterface> data_channel) override {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100922 RTC_LOG(LS_INFO) << debug_name_ << ": OnDataChannel";
deadbeef1dcb1642017-03-29 21:08:16 -0700923 data_channel_ = data_channel;
924 data_observer_.reset(new MockDataChannelObserver(data_channel));
925 }
926
deadbeef1dcb1642017-03-29 21:08:16 -0700927 std::string debug_name_;
928
929 std::unique_ptr<rtc::FakeNetworkManager> fake_network_manager_;
930
931 rtc::scoped_refptr<webrtc::PeerConnectionInterface> peer_connection_;
932 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface>
933 peer_connection_factory_;
934
Steve Antonede9ca52017-10-16 13:04:27 -0700935 cricket::PortAllocator* port_allocator_;
deadbeef1dcb1642017-03-29 21:08:16 -0700936 // Needed to keep track of number of frames sent.
937 rtc::scoped_refptr<FakeAudioCaptureModule> fake_audio_capture_module_;
938 // Needed to keep track of number of frames received.
939 std::map<std::string, std::unique_ptr<webrtc::FakeVideoTrackRenderer>>
940 fake_video_renderers_;
941 // Needed to ensure frames aren't received for removed tracks.
942 std::vector<std::unique_ptr<webrtc::FakeVideoTrackRenderer>>
943 removed_fake_video_renderers_;
944 // Needed to keep track of number of frames received when external decoder
945 // used.
946 FakeWebRtcVideoDecoderFactory* fake_video_decoder_factory_ = nullptr;
947 FakeWebRtcVideoEncoderFactory* fake_video_encoder_factory_ = nullptr;
948 bool video_decoder_factory_enabled_ = false;
949
950 // For remote peer communication.
951 SignalingMessageReceiver* signaling_message_receiver_ = nullptr;
952 int signaling_delay_ms_ = 0;
Steve Antonede9ca52017-10-16 13:04:27 -0700953 bool signal_ice_candidates_ = true;
deadbeef1dcb1642017-03-29 21:08:16 -0700954
955 // Store references to the video capturers we've created, so that we can stop
956 // them, if required.
957 std::vector<cricket::FakeVideoCapturer*> video_capturers_;
958 // |local_video_renderer_| attached to the first created local video track.
959 std::unique_ptr<webrtc::FakeVideoTrackRenderer> local_video_renderer_;
960
Seth Hampson2f0d7022018-02-20 11:54:42 -0800961 SdpSemantics sdp_semantics_;
deadbeef1dcb1642017-03-29 21:08:16 -0700962 PeerConnectionInterface::RTCOfferAnswerOptions offer_answer_options_;
963 std::function<void(cricket::SessionDescription*)> received_sdp_munger_;
964 std::function<void(cricket::SessionDescription*)> generated_sdp_munger_;
Seth Hampson2f0d7022018-02-20 11:54:42 -0800965 std::function<void()> remote_offer_handler_;
deadbeef1dcb1642017-03-29 21:08:16 -0700966
967 rtc::scoped_refptr<DataChannelInterface> data_channel_;
968 std::unique_ptr<MockDataChannelObserver> data_observer_;
969
970 std::vector<std::unique_ptr<MockRtpReceiverObserver>> rtp_receiver_observers_;
971
Steve Antonede9ca52017-10-16 13:04:27 -0700972 std::vector<PeerConnectionInterface::IceConnectionState>
973 ice_connection_state_history_;
974 std::vector<PeerConnectionInterface::IceGatheringState>
975 ice_gathering_state_history_;
deadbeef1dcb1642017-03-29 21:08:16 -0700976
977 rtc::AsyncInvoker invoker_;
978
Seth Hampson2f0d7022018-02-20 11:54:42 -0800979 friend class PeerConnectionIntegrationBaseTest;
deadbeef1dcb1642017-03-29 21:08:16 -0700980};
981
Elad Alon99c3fe52017-10-13 16:29:40 +0200982class MockRtcEventLogOutput : public webrtc::RtcEventLogOutput {
983 public:
984 virtual ~MockRtcEventLogOutput() = default;
985 MOCK_CONST_METHOD0(IsActive, bool());
986 MOCK_METHOD1(Write, bool(const std::string&));
987};
988
Seth Hampson2f0d7022018-02-20 11:54:42 -0800989// This helper object is used for both specifying how many audio/video frames
990// are expected to be received for a caller/callee. It provides helper functions
991// to specify these expectations. The object initially starts in a state of no
992// expectations.
993class MediaExpectations {
994 public:
995 enum ExpectFrames {
996 kExpectSomeFrames,
997 kExpectNoFrames,
998 kNoExpectation,
999 };
1000
1001 void ExpectBidirectionalAudioAndVideo() {
1002 ExpectBidirectionalAudio();
1003 ExpectBidirectionalVideo();
1004 }
1005
1006 void ExpectBidirectionalAudio() {
1007 CallerExpectsSomeAudio();
1008 CalleeExpectsSomeAudio();
1009 }
1010
1011 void ExpectNoAudio() {
1012 CallerExpectsNoAudio();
1013 CalleeExpectsNoAudio();
1014 }
1015
1016 void ExpectBidirectionalVideo() {
1017 CallerExpectsSomeVideo();
1018 CalleeExpectsSomeVideo();
1019 }
1020
1021 void ExpectNoVideo() {
1022 CallerExpectsNoVideo();
1023 CalleeExpectsNoVideo();
1024 }
1025
1026 void CallerExpectsSomeAudioAndVideo() {
1027 CallerExpectsSomeAudio();
1028 CallerExpectsSomeVideo();
1029 }
1030
1031 void CalleeExpectsSomeAudioAndVideo() {
1032 CalleeExpectsSomeAudio();
1033 CalleeExpectsSomeVideo();
1034 }
1035
1036 // Caller's audio functions.
1037 void CallerExpectsSomeAudio(
1038 int expected_audio_frames = kDefaultExpectedAudioFrameCount) {
1039 caller_audio_expectation_ = kExpectSomeFrames;
1040 caller_audio_frames_expected_ = expected_audio_frames;
1041 }
1042
1043 void CallerExpectsNoAudio() {
1044 caller_audio_expectation_ = kExpectNoFrames;
1045 caller_audio_frames_expected_ = 0;
1046 }
1047
1048 // Caller's video functions.
1049 void CallerExpectsSomeVideo(
1050 int expected_video_frames = kDefaultExpectedVideoFrameCount) {
1051 caller_video_expectation_ = kExpectSomeFrames;
1052 caller_video_frames_expected_ = expected_video_frames;
1053 }
1054
1055 void CallerExpectsNoVideo() {
1056 caller_video_expectation_ = kExpectNoFrames;
1057 caller_video_frames_expected_ = 0;
1058 }
1059
1060 // Callee's audio functions.
1061 void CalleeExpectsSomeAudio(
1062 int expected_audio_frames = kDefaultExpectedAudioFrameCount) {
1063 callee_audio_expectation_ = kExpectSomeFrames;
1064 callee_audio_frames_expected_ = expected_audio_frames;
1065 }
1066
1067 void CalleeExpectsNoAudio() {
1068 callee_audio_expectation_ = kExpectNoFrames;
1069 callee_audio_frames_expected_ = 0;
1070 }
1071
1072 // Callee's video functions.
1073 void CalleeExpectsSomeVideo(
1074 int expected_video_frames = kDefaultExpectedVideoFrameCount) {
1075 callee_video_expectation_ = kExpectSomeFrames;
1076 callee_video_frames_expected_ = expected_video_frames;
1077 }
1078
1079 void CalleeExpectsNoVideo() {
1080 callee_video_expectation_ = kExpectNoFrames;
1081 callee_video_frames_expected_ = 0;
1082 }
1083
1084 ExpectFrames caller_audio_expectation_ = kNoExpectation;
1085 ExpectFrames caller_video_expectation_ = kNoExpectation;
1086 ExpectFrames callee_audio_expectation_ = kNoExpectation;
1087 ExpectFrames callee_video_expectation_ = kNoExpectation;
1088 int caller_audio_frames_expected_ = 0;
1089 int caller_video_frames_expected_ = 0;
1090 int callee_audio_frames_expected_ = 0;
1091 int callee_video_frames_expected_ = 0;
1092};
1093
deadbeef1dcb1642017-03-29 21:08:16 -07001094// Tests two PeerConnections connecting to each other end-to-end, using a
1095// virtual network, fake A/V capture and fake encoder/decoders. The
1096// PeerConnections share the threads/socket servers, but use separate versions
1097// of everything else (including "PeerConnectionFactory"s).
Seth Hampson2f0d7022018-02-20 11:54:42 -08001098class PeerConnectionIntegrationBaseTest : public testing::Test {
deadbeef1dcb1642017-03-29 21:08:16 -07001099 public:
Seth Hampson2f0d7022018-02-20 11:54:42 -08001100 explicit PeerConnectionIntegrationBaseTest(SdpSemantics sdp_semantics)
1101 : sdp_semantics_(sdp_semantics),
1102 ss_(new rtc::VirtualSocketServer()),
Steve Antonede9ca52017-10-16 13:04:27 -07001103 fss_(new rtc::FirewallSocketServer(ss_.get())),
1104 network_thread_(new rtc::Thread(fss_.get())),
deadbeef1dcb1642017-03-29 21:08:16 -07001105 worker_thread_(rtc::Thread::Create()) {
Sebastian Jansson8a793a02018-03-13 15:21:48 +01001106 network_thread_->SetName("PCNetworkThread", this);
1107 worker_thread_->SetName("PCWorkerThread", this);
deadbeef1dcb1642017-03-29 21:08:16 -07001108 RTC_CHECK(network_thread_->Start());
1109 RTC_CHECK(worker_thread_->Start());
1110 }
1111
Seth Hampson2f0d7022018-02-20 11:54:42 -08001112 ~PeerConnectionIntegrationBaseTest() {
deadbeef1dcb1642017-03-29 21:08:16 -07001113 if (caller_) {
1114 caller_->set_signaling_message_receiver(nullptr);
1115 }
1116 if (callee_) {
1117 callee_->set_signaling_message_receiver(nullptr);
1118 }
1119 }
1120
1121 bool SignalingStateStable() {
1122 return caller_->SignalingStateStable() && callee_->SignalingStateStable();
1123 }
1124
deadbeef71452802017-05-07 17:21:01 -07001125 bool DtlsConnected() {
1126 // TODO(deadbeef): kIceConnectionConnected currently means both ICE and DTLS
1127 // are connected. This is an important distinction. Once we have separate
1128 // ICE and DTLS state, this check needs to use the DTLS state.
1129 return (callee()->ice_connection_state() ==
1130 webrtc::PeerConnectionInterface::kIceConnectionConnected ||
1131 callee()->ice_connection_state() ==
1132 webrtc::PeerConnectionInterface::kIceConnectionCompleted) &&
1133 (caller()->ice_connection_state() ==
1134 webrtc::PeerConnectionInterface::kIceConnectionConnected ||
1135 caller()->ice_connection_state() ==
1136 webrtc::PeerConnectionInterface::kIceConnectionCompleted);
1137 }
1138
Seth Hampson2f0d7022018-02-20 11:54:42 -08001139 std::unique_ptr<PeerConnectionWrapper> CreatePeerConnectionWrapper(
1140 const std::string& debug_name,
1141 const MediaConstraintsInterface* constraints,
1142 const PeerConnectionFactory::Options* options,
1143 const RTCConfiguration* config,
1144 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator) {
1145 RTCConfiguration modified_config;
1146 if (config) {
1147 modified_config = *config;
1148 }
1149 if (modified_config.sdp_semantics == SdpSemantics::kDefault) {
1150 modified_config.sdp_semantics = sdp_semantics_;
1151 }
1152 if (!cert_generator) {
1153 cert_generator = rtc::MakeUnique<FakeRTCCertificateGenerator>();
1154 }
1155 std::unique_ptr<PeerConnectionWrapper> client(
1156 new PeerConnectionWrapper(debug_name));
1157 if (!client->Init(constraints, options, &modified_config,
1158 std::move(cert_generator), network_thread_.get(),
1159 worker_thread_.get())) {
1160 return nullptr;
1161 }
1162 return client;
1163 }
1164
deadbeef1dcb1642017-03-29 21:08:16 -07001165 bool CreatePeerConnectionWrappers() {
1166 return CreatePeerConnectionWrappersWithConfig(
1167 PeerConnectionInterface::RTCConfiguration(),
1168 PeerConnectionInterface::RTCConfiguration());
1169 }
1170
1171 bool CreatePeerConnectionWrappersWithConstraints(
1172 MediaConstraintsInterface* caller_constraints,
1173 MediaConstraintsInterface* callee_constraints) {
Seth Hampson2f0d7022018-02-20 11:54:42 -08001174 caller_ = CreatePeerConnectionWrapper("Caller", caller_constraints, nullptr,
1175 nullptr, nullptr);
1176 callee_ = CreatePeerConnectionWrapper("Callee", callee_constraints, nullptr,
1177 nullptr, nullptr);
deadbeef1dcb1642017-03-29 21:08:16 -07001178 return caller_ && callee_;
1179 }
1180
1181 bool CreatePeerConnectionWrappersWithConfig(
1182 const PeerConnectionInterface::RTCConfiguration& caller_config,
1183 const PeerConnectionInterface::RTCConfiguration& callee_config) {
Seth Hampson2f0d7022018-02-20 11:54:42 -08001184 caller_ = CreatePeerConnectionWrapper("Caller", nullptr, nullptr,
1185 &caller_config, nullptr);
1186 callee_ = CreatePeerConnectionWrapper("Callee", nullptr, nullptr,
1187 &callee_config, nullptr);
deadbeef1dcb1642017-03-29 21:08:16 -07001188 return caller_ && callee_;
1189 }
1190
1191 bool CreatePeerConnectionWrappersWithOptions(
1192 const PeerConnectionFactory::Options& caller_options,
1193 const PeerConnectionFactory::Options& callee_options) {
Seth Hampson2f0d7022018-02-20 11:54:42 -08001194 caller_ = CreatePeerConnectionWrapper("Caller", nullptr, &caller_options,
1195 nullptr, nullptr);
1196 callee_ = CreatePeerConnectionWrapper("Callee", nullptr, &callee_options,
1197 nullptr, nullptr);
deadbeef1dcb1642017-03-29 21:08:16 -07001198 return caller_ && callee_;
1199 }
1200
Seth Hampson2f0d7022018-02-20 11:54:42 -08001201 std::unique_ptr<PeerConnectionWrapper>
1202 CreatePeerConnectionWrapperWithAlternateKey() {
deadbeef1dcb1642017-03-29 21:08:16 -07001203 std::unique_ptr<FakeRTCCertificateGenerator> cert_generator(
1204 new FakeRTCCertificateGenerator());
1205 cert_generator->use_alternate_key();
1206
Seth Hampson2f0d7022018-02-20 11:54:42 -08001207 return CreatePeerConnectionWrapper("New Peer", nullptr, nullptr, nullptr,
1208 std::move(cert_generator));
deadbeef1dcb1642017-03-29 21:08:16 -07001209 }
1210
1211 // Once called, SDP blobs and ICE candidates will be automatically signaled
1212 // between PeerConnections.
1213 void ConnectFakeSignaling() {
1214 caller_->set_signaling_message_receiver(callee_.get());
1215 callee_->set_signaling_message_receiver(caller_.get());
1216 }
1217
Steve Antonede9ca52017-10-16 13:04:27 -07001218 // Once called, SDP blobs will be automatically signaled between
1219 // PeerConnections. Note that ICE candidates will not be signaled unless they
1220 // are in the exchanged SDP blobs.
1221 void ConnectFakeSignalingForSdpOnly() {
1222 ConnectFakeSignaling();
1223 SetSignalIceCandidates(false);
1224 }
1225
deadbeef1dcb1642017-03-29 21:08:16 -07001226 void SetSignalingDelayMs(int delay_ms) {
1227 caller_->set_signaling_delay_ms(delay_ms);
1228 callee_->set_signaling_delay_ms(delay_ms);
1229 }
1230
Steve Antonede9ca52017-10-16 13:04:27 -07001231 void SetSignalIceCandidates(bool signal) {
1232 caller_->set_signal_ice_candidates(signal);
1233 callee_->set_signal_ice_candidates(signal);
1234 }
1235
deadbeef1dcb1642017-03-29 21:08:16 -07001236 void EnableVideoDecoderFactory() {
1237 caller_->EnableVideoDecoderFactory();
1238 callee_->EnableVideoDecoderFactory();
1239 }
1240
1241 // Messages may get lost on the unreliable DataChannel, so we send multiple
1242 // times to avoid test flakiness.
1243 void SendRtpDataWithRetries(webrtc::DataChannelInterface* dc,
1244 const std::string& data,
1245 int retries) {
1246 for (int i = 0; i < retries; ++i) {
1247 dc->Send(DataBuffer(data));
1248 }
1249 }
1250
1251 rtc::Thread* network_thread() { return network_thread_.get(); }
1252
1253 rtc::VirtualSocketServer* virtual_socket_server() { return ss_.get(); }
1254
1255 PeerConnectionWrapper* caller() { return caller_.get(); }
1256
1257 // Set the |caller_| to the |wrapper| passed in and return the
1258 // original |caller_|.
1259 PeerConnectionWrapper* SetCallerPcWrapperAndReturnCurrent(
1260 PeerConnectionWrapper* wrapper) {
1261 PeerConnectionWrapper* old = caller_.release();
1262 caller_.reset(wrapper);
1263 return old;
1264 }
1265
1266 PeerConnectionWrapper* callee() { return callee_.get(); }
1267
1268 // Set the |callee_| to the |wrapper| passed in and return the
1269 // original |callee_|.
1270 PeerConnectionWrapper* SetCalleePcWrapperAndReturnCurrent(
1271 PeerConnectionWrapper* wrapper) {
1272 PeerConnectionWrapper* old = callee_.release();
1273 callee_.reset(wrapper);
1274 return old;
1275 }
1276
Steve Antonede9ca52017-10-16 13:04:27 -07001277 rtc::FirewallSocketServer* firewall() const { return fss_.get(); }
1278
Seth Hampson2f0d7022018-02-20 11:54:42 -08001279 // Expects the provided number of new frames to be received within
1280 // kMaxWaitForFramesMs. The new expected frames are specified in
1281 // |media_expectations|. Returns false if any of the expectations were
1282 // not met.
1283 bool ExpectNewFrames(const MediaExpectations& media_expectations) {
1284 // First initialize the expected frame counts based upon the current
1285 // frame count.
1286 int total_caller_audio_frames_expected = caller()->audio_frames_received();
1287 if (media_expectations.caller_audio_expectation_ ==
1288 MediaExpectations::kExpectSomeFrames) {
1289 total_caller_audio_frames_expected +=
1290 media_expectations.caller_audio_frames_expected_;
1291 }
1292 int total_caller_video_frames_expected =
deadbeef1dcb1642017-03-29 21:08:16 -07001293 caller()->min_video_frames_received_per_track();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001294 if (media_expectations.caller_video_expectation_ ==
1295 MediaExpectations::kExpectSomeFrames) {
1296 total_caller_video_frames_expected +=
1297 media_expectations.caller_video_frames_expected_;
1298 }
1299 int total_callee_audio_frames_expected = callee()->audio_frames_received();
1300 if (media_expectations.callee_audio_expectation_ ==
1301 MediaExpectations::kExpectSomeFrames) {
1302 total_callee_audio_frames_expected +=
1303 media_expectations.callee_audio_frames_expected_;
1304 }
1305 int total_callee_video_frames_expected =
deadbeef1dcb1642017-03-29 21:08:16 -07001306 callee()->min_video_frames_received_per_track();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001307 if (media_expectations.callee_video_expectation_ ==
1308 MediaExpectations::kExpectSomeFrames) {
1309 total_callee_video_frames_expected +=
1310 media_expectations.callee_video_frames_expected_;
1311 }
deadbeef1dcb1642017-03-29 21:08:16 -07001312
Seth Hampson2f0d7022018-02-20 11:54:42 -08001313 // Wait for the expected frames.
deadbeef1dcb1642017-03-29 21:08:16 -07001314 EXPECT_TRUE_WAIT(caller()->audio_frames_received() >=
Seth Hampson2f0d7022018-02-20 11:54:42 -08001315 total_caller_audio_frames_expected &&
deadbeef1dcb1642017-03-29 21:08:16 -07001316 caller()->min_video_frames_received_per_track() >=
Seth Hampson2f0d7022018-02-20 11:54:42 -08001317 total_caller_video_frames_expected &&
deadbeef1dcb1642017-03-29 21:08:16 -07001318 callee()->audio_frames_received() >=
Seth Hampson2f0d7022018-02-20 11:54:42 -08001319 total_callee_audio_frames_expected &&
deadbeef1dcb1642017-03-29 21:08:16 -07001320 callee()->min_video_frames_received_per_track() >=
Seth Hampson2f0d7022018-02-20 11:54:42 -08001321 total_callee_video_frames_expected,
1322 kMaxWaitForFramesMs);
1323 bool expectations_correct =
1324 caller()->audio_frames_received() >=
1325 total_caller_audio_frames_expected &&
1326 caller()->min_video_frames_received_per_track() >=
1327 total_caller_video_frames_expected &&
1328 callee()->audio_frames_received() >=
1329 total_callee_audio_frames_expected &&
1330 callee()->min_video_frames_received_per_track() >=
1331 total_callee_video_frames_expected;
deadbeef1dcb1642017-03-29 21:08:16 -07001332
Seth Hampson2f0d7022018-02-20 11:54:42 -08001333 // After the combined wait, print out a more detailed message upon
1334 // failure.
deadbeef1dcb1642017-03-29 21:08:16 -07001335 EXPECT_GE(caller()->audio_frames_received(),
Seth Hampson2f0d7022018-02-20 11:54:42 -08001336 total_caller_audio_frames_expected);
deadbeef1dcb1642017-03-29 21:08:16 -07001337 EXPECT_GE(caller()->min_video_frames_received_per_track(),
Seth Hampson2f0d7022018-02-20 11:54:42 -08001338 total_caller_video_frames_expected);
deadbeef1dcb1642017-03-29 21:08:16 -07001339 EXPECT_GE(callee()->audio_frames_received(),
Seth Hampson2f0d7022018-02-20 11:54:42 -08001340 total_callee_audio_frames_expected);
deadbeef1dcb1642017-03-29 21:08:16 -07001341 EXPECT_GE(callee()->min_video_frames_received_per_track(),
Seth Hampson2f0d7022018-02-20 11:54:42 -08001342 total_callee_video_frames_expected);
1343
1344 // We want to make sure nothing unexpected was received.
1345 if (media_expectations.caller_audio_expectation_ ==
1346 MediaExpectations::kExpectNoFrames) {
1347 EXPECT_EQ(caller()->audio_frames_received(),
1348 total_caller_audio_frames_expected);
1349 if (caller()->audio_frames_received() !=
1350 total_caller_audio_frames_expected) {
1351 expectations_correct = false;
1352 }
1353 }
1354 if (media_expectations.caller_video_expectation_ ==
1355 MediaExpectations::kExpectNoFrames) {
1356 EXPECT_EQ(caller()->min_video_frames_received_per_track(),
1357 total_caller_video_frames_expected);
1358 if (caller()->min_video_frames_received_per_track() !=
1359 total_caller_video_frames_expected) {
1360 expectations_correct = false;
1361 }
1362 }
1363 if (media_expectations.callee_audio_expectation_ ==
1364 MediaExpectations::kExpectNoFrames) {
1365 EXPECT_EQ(callee()->audio_frames_received(),
1366 total_callee_audio_frames_expected);
1367 if (callee()->audio_frames_received() !=
1368 total_callee_audio_frames_expected) {
1369 expectations_correct = false;
1370 }
1371 }
1372 if (media_expectations.callee_video_expectation_ ==
1373 MediaExpectations::kExpectNoFrames) {
1374 EXPECT_EQ(callee()->min_video_frames_received_per_track(),
1375 total_callee_video_frames_expected);
1376 if (callee()->min_video_frames_received_per_track() !=
1377 total_callee_video_frames_expected) {
1378 expectations_correct = false;
1379 }
1380 }
1381 return expectations_correct;
deadbeef1dcb1642017-03-29 21:08:16 -07001382 }
1383
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07001384 void TestNegotiatedCipherSuite(
1385 const PeerConnectionFactory::Options& caller_options,
1386 const PeerConnectionFactory::Options& callee_options,
1387 int expected_cipher_suite) {
deadbeef1dcb1642017-03-29 21:08:16 -07001388 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(caller_options,
1389 callee_options));
1390 rtc::scoped_refptr<webrtc::FakeMetricsObserver> caller_observer =
1391 new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
1392 caller()->pc()->RegisterUMAObserver(caller_observer);
1393 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001394 caller()->AddAudioVideoTracks();
1395 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001396 caller()->CreateAndSetAndSignalOffer();
1397 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1398 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(expected_cipher_suite),
deadbeefd8ad7882017-04-18 16:01:17 -07001399 caller()->OldGetStats()->SrtpCipher(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07001400 EXPECT_EQ(
1401 1, caller_observer->GetEnumCounter(webrtc::kEnumCounterAudioSrtpCipher,
1402 expected_cipher_suite));
1403 caller()->pc()->RegisterUMAObserver(nullptr);
1404 }
1405
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07001406 void TestGcmNegotiationUsesCipherSuite(bool local_gcm_enabled,
1407 bool remote_gcm_enabled,
1408 int expected_cipher_suite) {
1409 PeerConnectionFactory::Options caller_options;
1410 caller_options.crypto_options.enable_gcm_crypto_suites = local_gcm_enabled;
1411 PeerConnectionFactory::Options callee_options;
1412 callee_options.crypto_options.enable_gcm_crypto_suites = remote_gcm_enabled;
1413 TestNegotiatedCipherSuite(caller_options, callee_options,
1414 expected_cipher_suite);
1415 }
1416
Seth Hampson2f0d7022018-02-20 11:54:42 -08001417 protected:
1418 const SdpSemantics sdp_semantics_;
1419
deadbeef1dcb1642017-03-29 21:08:16 -07001420 private:
1421 // |ss_| is used by |network_thread_| so it must be destroyed later.
deadbeef1dcb1642017-03-29 21:08:16 -07001422 std::unique_ptr<rtc::VirtualSocketServer> ss_;
Steve Antonede9ca52017-10-16 13:04:27 -07001423 std::unique_ptr<rtc::FirewallSocketServer> fss_;
deadbeef1dcb1642017-03-29 21:08:16 -07001424 // |network_thread_| and |worker_thread_| are used by both
1425 // |caller_| and |callee_| so they must be destroyed
1426 // later.
1427 std::unique_ptr<rtc::Thread> network_thread_;
1428 std::unique_ptr<rtc::Thread> worker_thread_;
1429 std::unique_ptr<PeerConnectionWrapper> caller_;
1430 std::unique_ptr<PeerConnectionWrapper> callee_;
1431};
1432
Seth Hampson2f0d7022018-02-20 11:54:42 -08001433class PeerConnectionIntegrationTest
1434 : public PeerConnectionIntegrationBaseTest,
1435 public ::testing::WithParamInterface<SdpSemantics> {
1436 protected:
1437 PeerConnectionIntegrationTest()
1438 : PeerConnectionIntegrationBaseTest(GetParam()) {}
1439};
1440
1441class PeerConnectionIntegrationTestPlanB
1442 : public PeerConnectionIntegrationBaseTest {
1443 protected:
1444 PeerConnectionIntegrationTestPlanB()
1445 : PeerConnectionIntegrationBaseTest(SdpSemantics::kPlanB) {}
1446};
1447
1448class PeerConnectionIntegrationTestUnifiedPlan
1449 : public PeerConnectionIntegrationBaseTest {
1450 protected:
1451 PeerConnectionIntegrationTestUnifiedPlan()
1452 : PeerConnectionIntegrationBaseTest(SdpSemantics::kUnifiedPlan) {}
1453};
1454
deadbeef1dcb1642017-03-29 21:08:16 -07001455// Test the OnFirstPacketReceived callback from audio/video RtpReceivers. This
1456// includes testing that the callback is invoked if an observer is connected
1457// after the first packet has already been received.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001458TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07001459 RtpReceiverObserverOnFirstPacketReceived) {
1460 ASSERT_TRUE(CreatePeerConnectionWrappers());
1461 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001462 caller()->AddAudioVideoTracks();
1463 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001464 // Start offer/answer exchange and wait for it to complete.
1465 caller()->CreateAndSetAndSignalOffer();
1466 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1467 // Should be one receiver each for audio/video.
1468 EXPECT_EQ(2, caller()->rtp_receiver_observers().size());
1469 EXPECT_EQ(2, callee()->rtp_receiver_observers().size());
1470 // Wait for all "first packet received" callbacks to be fired.
1471 EXPECT_TRUE_WAIT(
1472 std::all_of(caller()->rtp_receiver_observers().begin(),
1473 caller()->rtp_receiver_observers().end(),
1474 [](const std::unique_ptr<MockRtpReceiverObserver>& o) {
1475 return o->first_packet_received();
1476 }),
1477 kMaxWaitForFramesMs);
1478 EXPECT_TRUE_WAIT(
1479 std::all_of(callee()->rtp_receiver_observers().begin(),
1480 callee()->rtp_receiver_observers().end(),
1481 [](const std::unique_ptr<MockRtpReceiverObserver>& o) {
1482 return o->first_packet_received();
1483 }),
1484 kMaxWaitForFramesMs);
1485 // If new observers are set after the first packet was already received, the
1486 // callback should still be invoked.
1487 caller()->ResetRtpReceiverObservers();
1488 callee()->ResetRtpReceiverObservers();
1489 EXPECT_EQ(2, caller()->rtp_receiver_observers().size());
1490 EXPECT_EQ(2, callee()->rtp_receiver_observers().size());
1491 EXPECT_TRUE(
1492 std::all_of(caller()->rtp_receiver_observers().begin(),
1493 caller()->rtp_receiver_observers().end(),
1494 [](const std::unique_ptr<MockRtpReceiverObserver>& o) {
1495 return o->first_packet_received();
1496 }));
1497 EXPECT_TRUE(
1498 std::all_of(callee()->rtp_receiver_observers().begin(),
1499 callee()->rtp_receiver_observers().end(),
1500 [](const std::unique_ptr<MockRtpReceiverObserver>& o) {
1501 return o->first_packet_received();
1502 }));
1503}
1504
1505class DummyDtmfObserver : public DtmfSenderObserverInterface {
1506 public:
1507 DummyDtmfObserver() : completed_(false) {}
1508
1509 // Implements DtmfSenderObserverInterface.
1510 void OnToneChange(const std::string& tone) override {
1511 tones_.push_back(tone);
1512 if (tone.empty()) {
1513 completed_ = true;
1514 }
1515 }
1516
1517 const std::vector<std::string>& tones() const { return tones_; }
1518 bool completed() const { return completed_; }
1519
1520 private:
1521 bool completed_;
1522 std::vector<std::string> tones_;
1523};
1524
1525// Assumes |sender| already has an audio track added and the offer/answer
1526// exchange is done.
1527void TestDtmfFromSenderToReceiver(PeerConnectionWrapper* sender,
1528 PeerConnectionWrapper* receiver) {
Steve Anton15324772018-01-16 10:26:49 -08001529 // We should be able to get a DTMF sender from the local sender.
1530 rtc::scoped_refptr<DtmfSenderInterface> dtmf_sender =
1531 sender->pc()->GetSenders().at(0)->GetDtmfSender();
1532 ASSERT_TRUE(dtmf_sender);
deadbeef1dcb1642017-03-29 21:08:16 -07001533 DummyDtmfObserver observer;
deadbeef1dcb1642017-03-29 21:08:16 -07001534 dtmf_sender->RegisterObserver(&observer);
1535
1536 // Test the DtmfSender object just created.
1537 EXPECT_TRUE(dtmf_sender->CanInsertDtmf());
1538 EXPECT_TRUE(dtmf_sender->InsertDtmf("1a", 100, 50));
1539
1540 EXPECT_TRUE_WAIT(observer.completed(), kDefaultTimeout);
1541 std::vector<std::string> tones = {"1", "a", ""};
1542 EXPECT_EQ(tones, observer.tones());
1543 dtmf_sender->UnregisterObserver();
1544 // TODO(deadbeef): Verify the tones were actually received end-to-end.
1545}
1546
1547// Verifies the DtmfSenderObserver callbacks for a DtmfSender (one in each
1548// direction).
Seth Hampson2f0d7022018-02-20 11:54:42 -08001549TEST_P(PeerConnectionIntegrationTest, DtmfSenderObserver) {
deadbeef1dcb1642017-03-29 21:08:16 -07001550 ASSERT_TRUE(CreatePeerConnectionWrappers());
1551 ConnectFakeSignaling();
1552 // Only need audio for DTMF.
Steve Anton15324772018-01-16 10:26:49 -08001553 caller()->AddAudioTrack();
1554 callee()->AddAudioTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07001555 caller()->CreateAndSetAndSignalOffer();
1556 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
deadbeef71452802017-05-07 17:21:01 -07001557 // DTLS must finish before the DTMF sender can be used reliably.
1558 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07001559 TestDtmfFromSenderToReceiver(caller(), callee());
1560 TestDtmfFromSenderToReceiver(callee(), caller());
1561}
1562
1563// Basic end-to-end test, verifying media can be encoded/transmitted/decoded
1564// between two connections, using DTLS-SRTP.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001565TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithDtls) {
deadbeef1dcb1642017-03-29 21:08:16 -07001566 ASSERT_TRUE(CreatePeerConnectionWrappers());
1567 ConnectFakeSignaling();
Harald Alvestrand194939b2018-01-24 16:04:13 +01001568 rtc::scoped_refptr<webrtc::FakeMetricsObserver> caller_observer =
1569 new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
1570 caller()->pc()->RegisterUMAObserver(caller_observer);
1571
deadbeef1dcb1642017-03-29 21:08:16 -07001572 // Do normal offer/answer and wait for some frames to be received in each
1573 // direction.
Steve Anton15324772018-01-16 10:26:49 -08001574 caller()->AddAudioVideoTracks();
1575 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001576 caller()->CreateAndSetAndSignalOffer();
1577 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001578 MediaExpectations media_expectations;
1579 media_expectations.ExpectBidirectionalAudioAndVideo();
1580 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Harald Alvestrand194939b2018-01-24 16:04:13 +01001581 EXPECT_LE(
1582 1, caller_observer->GetEnumCounter(webrtc::kEnumCounterKeyProtocol,
1583 webrtc::kEnumCounterKeyProtocolDtls));
1584 EXPECT_EQ(
1585 0, caller_observer->GetEnumCounter(webrtc::kEnumCounterKeyProtocol,
1586 webrtc::kEnumCounterKeyProtocolSdes));
deadbeef1dcb1642017-03-29 21:08:16 -07001587}
1588
1589// Uses SDES instead of DTLS for key agreement.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001590TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithSdes) {
deadbeef1dcb1642017-03-29 21:08:16 -07001591 PeerConnectionInterface::RTCConfiguration sdes_config;
1592 sdes_config.enable_dtls_srtp.emplace(false);
1593 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(sdes_config, sdes_config));
1594 ConnectFakeSignaling();
Harald Alvestrand194939b2018-01-24 16:04:13 +01001595 rtc::scoped_refptr<webrtc::FakeMetricsObserver> caller_observer =
1596 new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
1597 caller()->pc()->RegisterUMAObserver(caller_observer);
deadbeef1dcb1642017-03-29 21:08:16 -07001598
1599 // Do normal offer/answer and wait for some frames to be received in each
1600 // direction.
Steve Anton15324772018-01-16 10:26:49 -08001601 caller()->AddAudioVideoTracks();
1602 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001603 caller()->CreateAndSetAndSignalOffer();
1604 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001605 MediaExpectations media_expectations;
1606 media_expectations.ExpectBidirectionalAudioAndVideo();
1607 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Harald Alvestrand194939b2018-01-24 16:04:13 +01001608 EXPECT_LE(
1609 1, caller_observer->GetEnumCounter(webrtc::kEnumCounterKeyProtocol,
1610 webrtc::kEnumCounterKeyProtocolSdes));
1611 EXPECT_EQ(
1612 0, caller_observer->GetEnumCounter(webrtc::kEnumCounterKeyProtocol,
1613 webrtc::kEnumCounterKeyProtocolDtls));
deadbeef1dcb1642017-03-29 21:08:16 -07001614}
1615
Steve Anton8c0f7a72017-10-03 10:03:10 -07001616// Tests that the GetRemoteAudioSSLCertificate method returns the remote DTLS
1617// certificate once the DTLS handshake has finished.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001618TEST_P(PeerConnectionIntegrationTest,
Steve Anton8c0f7a72017-10-03 10:03:10 -07001619 GetRemoteAudioSSLCertificateReturnsExchangedCertificate) {
1620 auto GetRemoteAudioSSLCertificate = [](PeerConnectionWrapper* wrapper) {
1621 auto pci = reinterpret_cast<PeerConnectionProxy*>(wrapper->pc());
1622 auto pc = reinterpret_cast<PeerConnection*>(pci->internal());
1623 return pc->GetRemoteAudioSSLCertificate();
1624 };
Zhi Huang70b820f2018-01-27 14:16:15 -08001625 auto GetRemoteAudioSSLCertChain = [](PeerConnectionWrapper* wrapper) {
1626 auto pci = reinterpret_cast<PeerConnectionProxy*>(wrapper->pc());
1627 auto pc = reinterpret_cast<PeerConnection*>(pci->internal());
1628 return pc->GetRemoteAudioSSLCertChain();
1629 };
Steve Anton8c0f7a72017-10-03 10:03:10 -07001630
1631 auto caller_cert = rtc::RTCCertificate::FromPEM(kRsaPems[0]);
1632 auto callee_cert = rtc::RTCCertificate::FromPEM(kRsaPems[1]);
1633
1634 // Configure each side with a known certificate so they can be compared later.
1635 PeerConnectionInterface::RTCConfiguration caller_config;
1636 caller_config.enable_dtls_srtp.emplace(true);
1637 caller_config.certificates.push_back(caller_cert);
1638 PeerConnectionInterface::RTCConfiguration callee_config;
1639 callee_config.enable_dtls_srtp.emplace(true);
1640 callee_config.certificates.push_back(callee_cert);
1641 ASSERT_TRUE(
1642 CreatePeerConnectionWrappersWithConfig(caller_config, callee_config));
1643 ConnectFakeSignaling();
1644
1645 // When first initialized, there should not be a remote SSL certificate (and
1646 // calling this method should not crash).
1647 EXPECT_EQ(nullptr, GetRemoteAudioSSLCertificate(caller()));
1648 EXPECT_EQ(nullptr, GetRemoteAudioSSLCertificate(callee()));
Zhi Huang70b820f2018-01-27 14:16:15 -08001649 EXPECT_EQ(nullptr, GetRemoteAudioSSLCertChain(caller()));
1650 EXPECT_EQ(nullptr, GetRemoteAudioSSLCertChain(callee()));
Steve Anton8c0f7a72017-10-03 10:03:10 -07001651
Steve Anton15324772018-01-16 10:26:49 -08001652 caller()->AddAudioTrack();
1653 callee()->AddAudioTrack();
Steve Anton8c0f7a72017-10-03 10:03:10 -07001654 caller()->CreateAndSetAndSignalOffer();
1655 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1656 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
1657
1658 // Once DTLS has been connected, each side should return the other's SSL
1659 // certificate when calling GetRemoteAudioSSLCertificate.
1660
1661 auto caller_remote_cert = GetRemoteAudioSSLCertificate(caller());
1662 ASSERT_TRUE(caller_remote_cert);
1663 EXPECT_EQ(callee_cert->ssl_certificate().ToPEMString(),
1664 caller_remote_cert->ToPEMString());
1665
1666 auto callee_remote_cert = GetRemoteAudioSSLCertificate(callee());
1667 ASSERT_TRUE(callee_remote_cert);
1668 EXPECT_EQ(caller_cert->ssl_certificate().ToPEMString(),
1669 callee_remote_cert->ToPEMString());
Zhi Huang70b820f2018-01-27 14:16:15 -08001670
1671 auto caller_remote_cert_chain = GetRemoteAudioSSLCertChain(caller());
1672 ASSERT_TRUE(caller_remote_cert_chain);
1673 ASSERT_EQ(1U, caller_remote_cert_chain->GetSize());
1674 auto remote_cert = &caller_remote_cert_chain->Get(0);
1675 EXPECT_EQ(callee_cert->ssl_certificate().ToPEMString(),
1676 remote_cert->ToPEMString());
1677
1678 auto callee_remote_cert_chain = GetRemoteAudioSSLCertChain(callee());
1679 ASSERT_TRUE(callee_remote_cert_chain);
1680 ASSERT_EQ(1U, callee_remote_cert_chain->GetSize());
1681 remote_cert = &callee_remote_cert_chain->Get(0);
1682 EXPECT_EQ(caller_cert->ssl_certificate().ToPEMString(),
1683 remote_cert->ToPEMString());
Steve Anton8c0f7a72017-10-03 10:03:10 -07001684}
1685
deadbeef1dcb1642017-03-29 21:08:16 -07001686// This test sets up a call between two parties (using DTLS) and tests that we
1687// can get a video aspect ratio of 16:9.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001688TEST_P(PeerConnectionIntegrationTest, SendAndReceive16To9AspectRatio) {
deadbeef1dcb1642017-03-29 21:08:16 -07001689 ASSERT_TRUE(CreatePeerConnectionWrappers());
1690 ConnectFakeSignaling();
1691
1692 // Add video tracks with 16:9 constraint.
1693 FakeConstraints constraints;
1694 double requested_ratio = 16.0 / 9;
1695 constraints.SetMandatoryMinAspectRatio(requested_ratio);
Steve Anton15324772018-01-16 10:26:49 -08001696 caller()->AddTrack(
1697 caller()->CreateLocalVideoTrackWithConstraints(constraints));
1698 callee()->AddTrack(
1699 callee()->CreateLocalVideoTrackWithConstraints(constraints));
deadbeef1dcb1642017-03-29 21:08:16 -07001700
1701 // Do normal offer/answer and wait for at least one frame to be received in
1702 // each direction.
1703 caller()->CreateAndSetAndSignalOffer();
1704 ASSERT_TRUE_WAIT(caller()->min_video_frames_received_per_track() > 0 &&
1705 callee()->min_video_frames_received_per_track() > 0,
1706 kMaxWaitForFramesMs);
1707
1708 // Check rendered aspect ratio.
1709 EXPECT_EQ(requested_ratio, caller()->local_rendered_aspect_ratio());
1710 EXPECT_EQ(requested_ratio, caller()->rendered_aspect_ratio());
1711 EXPECT_EQ(requested_ratio, callee()->local_rendered_aspect_ratio());
1712 EXPECT_EQ(requested_ratio, callee()->rendered_aspect_ratio());
1713}
1714
1715// This test sets up a call between two parties with a source resolution of
1716// 1280x720 and verifies that a 16:9 aspect ratio is received.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001717TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07001718 Send1280By720ResolutionAndReceive16To9AspectRatio) {
1719 ASSERT_TRUE(CreatePeerConnectionWrappers());
1720 ConnectFakeSignaling();
1721
1722 // Similar to above test, but uses MandatoryMin[Width/Height] constraint
1723 // instead of aspect ratio constraint.
1724 FakeConstraints constraints;
1725 constraints.SetMandatoryMinWidth(1280);
1726 constraints.SetMandatoryMinHeight(720);
Steve Anton15324772018-01-16 10:26:49 -08001727 caller()->AddTrack(
1728 caller()->CreateLocalVideoTrackWithConstraints(constraints));
1729 callee()->AddTrack(
1730 callee()->CreateLocalVideoTrackWithConstraints(constraints));
deadbeef1dcb1642017-03-29 21:08:16 -07001731
1732 // Do normal offer/answer and wait for at least one frame to be received in
1733 // each direction.
1734 caller()->CreateAndSetAndSignalOffer();
1735 ASSERT_TRUE_WAIT(caller()->min_video_frames_received_per_track() > 0 &&
1736 callee()->min_video_frames_received_per_track() > 0,
1737 kMaxWaitForFramesMs);
1738
1739 // Check rendered aspect ratio.
1740 EXPECT_EQ(16.0 / 9, caller()->local_rendered_aspect_ratio());
1741 EXPECT_EQ(16.0 / 9, caller()->rendered_aspect_ratio());
1742 EXPECT_EQ(16.0 / 9, callee()->local_rendered_aspect_ratio());
1743 EXPECT_EQ(16.0 / 9, callee()->rendered_aspect_ratio());
1744}
1745
1746// This test sets up an one-way call, with media only from caller to
1747// callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001748TEST_P(PeerConnectionIntegrationTest, OneWayMediaCall) {
deadbeef1dcb1642017-03-29 21:08:16 -07001749 ASSERT_TRUE(CreatePeerConnectionWrappers());
1750 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001751 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001752 caller()->CreateAndSetAndSignalOffer();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001753 MediaExpectations media_expectations;
1754 media_expectations.CalleeExpectsSomeAudioAndVideo();
1755 media_expectations.CallerExpectsNoAudio();
1756 media_expectations.CallerExpectsNoVideo();
1757 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07001758}
1759
1760// This test sets up a audio call initially, with the callee rejecting video
1761// initially. Then later the callee decides to upgrade to audio/video, and
1762// initiates a new offer/answer exchange.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001763TEST_P(PeerConnectionIntegrationTest, AudioToVideoUpgrade) {
deadbeef1dcb1642017-03-29 21:08:16 -07001764 ASSERT_TRUE(CreatePeerConnectionWrappers());
1765 ConnectFakeSignaling();
1766 // Initially, offer an audio/video stream from the caller, but refuse to
1767 // send/receive video on the callee side.
Steve Anton15324772018-01-16 10:26:49 -08001768 caller()->AddAudioVideoTracks();
1769 callee()->AddAudioTrack();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001770 if (sdp_semantics_ == SdpSemantics::kPlanB) {
1771 PeerConnectionInterface::RTCOfferAnswerOptions options;
1772 options.offer_to_receive_video = 0;
1773 callee()->SetOfferAnswerOptions(options);
1774 } else {
1775 callee()->SetRemoteOfferHandler([this] {
1776 callee()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)->Stop();
1777 });
1778 }
deadbeef1dcb1642017-03-29 21:08:16 -07001779 // Do offer/answer and make sure audio is still received end-to-end.
1780 caller()->CreateAndSetAndSignalOffer();
1781 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001782 {
1783 MediaExpectations media_expectations;
1784 media_expectations.ExpectBidirectionalAudio();
1785 media_expectations.ExpectNoVideo();
1786 ASSERT_TRUE(ExpectNewFrames(media_expectations));
1787 }
deadbeef1dcb1642017-03-29 21:08:16 -07001788 // Sanity check that the callee's description has a rejected video section.
1789 ASSERT_NE(nullptr, callee()->pc()->local_description());
1790 const ContentInfo* callee_video_content =
1791 GetFirstVideoContent(callee()->pc()->local_description()->description());
1792 ASSERT_NE(nullptr, callee_video_content);
1793 EXPECT_TRUE(callee_video_content->rejected);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001794
deadbeef1dcb1642017-03-29 21:08:16 -07001795 // Now negotiate with video and ensure negotiation succeeds, with video
1796 // frames and additional audio frames being received.
Steve Anton15324772018-01-16 10:26:49 -08001797 callee()->AddVideoTrack();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001798 if (sdp_semantics_ == SdpSemantics::kPlanB) {
1799 PeerConnectionInterface::RTCOfferAnswerOptions options;
1800 options.offer_to_receive_video = 1;
1801 callee()->SetOfferAnswerOptions(options);
1802 } else {
1803 callee()->SetRemoteOfferHandler(nullptr);
1804 caller()->SetRemoteOfferHandler([this] {
1805 // The caller creates a new transceiver to receive video on when receiving
1806 // the offer, but by default it is send only.
1807 auto transceivers = caller()->pc()->GetTransceivers();
1808 ASSERT_EQ(3, transceivers.size());
1809 ASSERT_EQ(cricket::MEDIA_TYPE_VIDEO,
1810 transceivers[2]->receiver()->media_type());
1811 transceivers[2]->sender()->SetTrack(caller()->CreateLocalVideoTrack());
1812 transceivers[2]->SetDirection(RtpTransceiverDirection::kSendRecv);
1813 });
1814 }
deadbeef1dcb1642017-03-29 21:08:16 -07001815 callee()->CreateAndSetAndSignalOffer();
1816 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001817 {
1818 // Expect additional audio frames to be received after the upgrade.
1819 MediaExpectations media_expectations;
1820 media_expectations.ExpectBidirectionalAudioAndVideo();
1821 ASSERT_TRUE(ExpectNewFrames(media_expectations));
1822 }
deadbeef1dcb1642017-03-29 21:08:16 -07001823}
1824
deadbeef4389b4d2017-09-07 09:07:36 -07001825// Simpler than the above test; just add an audio track to an established
1826// video-only connection.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001827TEST_P(PeerConnectionIntegrationTest, AddAudioToVideoOnlyCall) {
deadbeef4389b4d2017-09-07 09:07:36 -07001828 ASSERT_TRUE(CreatePeerConnectionWrappers());
1829 ConnectFakeSignaling();
1830 // Do initial offer/answer with just a video track.
Steve Anton15324772018-01-16 10:26:49 -08001831 caller()->AddVideoTrack();
1832 callee()->AddVideoTrack();
deadbeef4389b4d2017-09-07 09:07:36 -07001833 caller()->CreateAndSetAndSignalOffer();
1834 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1835 // Now add an audio track and do another offer/answer.
Steve Anton15324772018-01-16 10:26:49 -08001836 caller()->AddAudioTrack();
1837 callee()->AddAudioTrack();
deadbeef4389b4d2017-09-07 09:07:36 -07001838 caller()->CreateAndSetAndSignalOffer();
1839 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1840 // Ensure both audio and video frames are received end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001841 MediaExpectations media_expectations;
1842 media_expectations.ExpectBidirectionalAudioAndVideo();
1843 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef4389b4d2017-09-07 09:07:36 -07001844}
1845
deadbeef1dcb1642017-03-29 21:08:16 -07001846// This test sets up a call that's transferred to a new caller with a different
1847// DTLS fingerprint.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001848TEST_P(PeerConnectionIntegrationTest, CallTransferredForCallee) {
deadbeef1dcb1642017-03-29 21:08:16 -07001849 ASSERT_TRUE(CreatePeerConnectionWrappers());
1850 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001851 caller()->AddAudioVideoTracks();
1852 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001853 caller()->CreateAndSetAndSignalOffer();
1854 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1855
1856 // Keep the original peer around which will still send packets to the
1857 // receiving client. These SRTP packets will be dropped.
1858 std::unique_ptr<PeerConnectionWrapper> original_peer(
1859 SetCallerPcWrapperAndReturnCurrent(
Seth Hampson2f0d7022018-02-20 11:54:42 -08001860 CreatePeerConnectionWrapperWithAlternateKey().release()));
deadbeef1dcb1642017-03-29 21:08:16 -07001861 // TODO(deadbeef): Why do we call Close here? That goes against the comment
1862 // directly above.
1863 original_peer->pc()->Close();
1864
1865 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001866 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001867 caller()->CreateAndSetAndSignalOffer();
1868 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1869 // Wait for some additional frames to be transmitted end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001870 MediaExpectations media_expectations;
1871 media_expectations.ExpectBidirectionalAudioAndVideo();
1872 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07001873}
1874
1875// This test sets up a call that's transferred to a new callee with a different
1876// DTLS fingerprint.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001877TEST_P(PeerConnectionIntegrationTest, CallTransferredForCaller) {
deadbeef1dcb1642017-03-29 21:08:16 -07001878 ASSERT_TRUE(CreatePeerConnectionWrappers());
1879 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001880 caller()->AddAudioVideoTracks();
1881 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001882 caller()->CreateAndSetAndSignalOffer();
1883 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1884
1885 // Keep the original peer around which will still send packets to the
1886 // receiving client. These SRTP packets will be dropped.
1887 std::unique_ptr<PeerConnectionWrapper> original_peer(
1888 SetCalleePcWrapperAndReturnCurrent(
Seth Hampson2f0d7022018-02-20 11:54:42 -08001889 CreatePeerConnectionWrapperWithAlternateKey().release()));
deadbeef1dcb1642017-03-29 21:08:16 -07001890 // TODO(deadbeef): Why do we call Close here? That goes against the comment
1891 // directly above.
1892 original_peer->pc()->Close();
1893
1894 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001895 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001896 caller()->SetOfferAnswerOptions(IceRestartOfferAnswerOptions());
1897 caller()->CreateAndSetAndSignalOffer();
1898 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1899 // Wait for some additional frames to be transmitted end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001900 MediaExpectations media_expectations;
1901 media_expectations.ExpectBidirectionalAudioAndVideo();
1902 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07001903}
1904
1905// This test sets up a non-bundled call and negotiates bundling at the same
1906// time as starting an ICE restart. When bundling is in effect in the restart,
1907// the DTLS-SRTP context should be successfully reset.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001908TEST_P(PeerConnectionIntegrationTest, BundlingEnabledWhileIceRestartOccurs) {
deadbeef1dcb1642017-03-29 21:08:16 -07001909 ASSERT_TRUE(CreatePeerConnectionWrappers());
1910 ConnectFakeSignaling();
1911
Steve Anton15324772018-01-16 10:26:49 -08001912 caller()->AddAudioVideoTracks();
1913 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001914 // Remove the bundle group from the SDP received by the callee.
1915 callee()->SetReceivedSdpMunger([](cricket::SessionDescription* desc) {
1916 desc->RemoveGroupByName("BUNDLE");
1917 });
1918 caller()->CreateAndSetAndSignalOffer();
1919 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001920 {
1921 MediaExpectations media_expectations;
1922 media_expectations.ExpectBidirectionalAudioAndVideo();
1923 ASSERT_TRUE(ExpectNewFrames(media_expectations));
1924 }
deadbeef1dcb1642017-03-29 21:08:16 -07001925 // Now stop removing the BUNDLE group, and trigger an ICE restart.
1926 callee()->SetReceivedSdpMunger(nullptr);
1927 caller()->SetOfferAnswerOptions(IceRestartOfferAnswerOptions());
1928 caller()->CreateAndSetAndSignalOffer();
1929 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1930
1931 // Expect additional frames to be received after the ICE restart.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001932 {
1933 MediaExpectations media_expectations;
1934 media_expectations.ExpectBidirectionalAudioAndVideo();
1935 ASSERT_TRUE(ExpectNewFrames(media_expectations));
1936 }
deadbeef1dcb1642017-03-29 21:08:16 -07001937}
1938
1939// Test CVO (Coordination of Video Orientation). If a video source is rotated
1940// and both peers support the CVO RTP header extension, the actual video frames
1941// don't need to be encoded in different resolutions, since the rotation is
1942// communicated through the RTP header extension.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001943TEST_P(PeerConnectionIntegrationTest, RotatedVideoWithCVOExtension) {
deadbeef1dcb1642017-03-29 21:08:16 -07001944 ASSERT_TRUE(CreatePeerConnectionWrappers());
1945 ConnectFakeSignaling();
1946 // Add rotated video tracks.
Steve Anton15324772018-01-16 10:26:49 -08001947 caller()->AddTrack(
deadbeef1dcb1642017-03-29 21:08:16 -07001948 caller()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_90));
Steve Anton15324772018-01-16 10:26:49 -08001949 callee()->AddTrack(
deadbeef1dcb1642017-03-29 21:08:16 -07001950 callee()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_270));
1951
1952 // Wait for video frames to be received by both sides.
1953 caller()->CreateAndSetAndSignalOffer();
1954 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1955 ASSERT_TRUE_WAIT(caller()->min_video_frames_received_per_track() > 0 &&
1956 callee()->min_video_frames_received_per_track() > 0,
1957 kMaxWaitForFramesMs);
1958
1959 // Ensure that the aspect ratio is unmodified.
1960 // TODO(deadbeef): Where does 4:3 come from? Should be explicit in the test,
1961 // not just assumed.
1962 EXPECT_EQ(4.0 / 3, caller()->local_rendered_aspect_ratio());
1963 EXPECT_EQ(4.0 / 3, caller()->rendered_aspect_ratio());
1964 EXPECT_EQ(4.0 / 3, callee()->local_rendered_aspect_ratio());
1965 EXPECT_EQ(4.0 / 3, callee()->rendered_aspect_ratio());
1966 // Ensure that the CVO bits were surfaced to the renderer.
1967 EXPECT_EQ(webrtc::kVideoRotation_270, caller()->rendered_rotation());
1968 EXPECT_EQ(webrtc::kVideoRotation_90, callee()->rendered_rotation());
1969}
1970
1971// Test that when the CVO extension isn't supported, video is rotated the
1972// old-fashioned way, by encoding rotated frames.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001973TEST_P(PeerConnectionIntegrationTest, RotatedVideoWithoutCVOExtension) {
deadbeef1dcb1642017-03-29 21:08:16 -07001974 ASSERT_TRUE(CreatePeerConnectionWrappers());
1975 ConnectFakeSignaling();
1976 // Add rotated video tracks.
Steve Anton15324772018-01-16 10:26:49 -08001977 caller()->AddTrack(
deadbeef1dcb1642017-03-29 21:08:16 -07001978 caller()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_90));
Steve Anton15324772018-01-16 10:26:49 -08001979 callee()->AddTrack(
deadbeef1dcb1642017-03-29 21:08:16 -07001980 callee()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_270));
1981
1982 // Remove the CVO extension from the offered SDP.
1983 callee()->SetReceivedSdpMunger([](cricket::SessionDescription* desc) {
1984 cricket::VideoContentDescription* video =
1985 GetFirstVideoContentDescription(desc);
1986 video->ClearRtpHeaderExtensions();
1987 });
1988 // Wait for video frames to be received by both sides.
1989 caller()->CreateAndSetAndSignalOffer();
1990 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1991 ASSERT_TRUE_WAIT(caller()->min_video_frames_received_per_track() > 0 &&
1992 callee()->min_video_frames_received_per_track() > 0,
1993 kMaxWaitForFramesMs);
1994
1995 // Expect that the aspect ratio is inversed to account for the 90/270 degree
1996 // rotation.
1997 // TODO(deadbeef): Where does 4:3 come from? Should be explicit in the test,
1998 // not just assumed.
1999 EXPECT_EQ(3.0 / 4, caller()->local_rendered_aspect_ratio());
2000 EXPECT_EQ(3.0 / 4, caller()->rendered_aspect_ratio());
2001 EXPECT_EQ(3.0 / 4, callee()->local_rendered_aspect_ratio());
2002 EXPECT_EQ(3.0 / 4, callee()->rendered_aspect_ratio());
2003 // Expect that each endpoint is unaware of the rotation of the other endpoint.
2004 EXPECT_EQ(webrtc::kVideoRotation_0, caller()->rendered_rotation());
2005 EXPECT_EQ(webrtc::kVideoRotation_0, callee()->rendered_rotation());
2006}
2007
deadbeef1dcb1642017-03-29 21:08:16 -07002008// Test that if the answerer rejects the audio m= section, no audio is sent or
2009// received, but video still can be.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002010TEST_P(PeerConnectionIntegrationTest, AnswererRejectsAudioSection) {
deadbeef1dcb1642017-03-29 21:08:16 -07002011 ASSERT_TRUE(CreatePeerConnectionWrappers());
2012 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002013 caller()->AddAudioVideoTracks();
Seth Hampson2f0d7022018-02-20 11:54:42 -08002014 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2015 // Only add video track for callee, and set offer_to_receive_audio to 0, so
2016 // it will reject the audio m= section completely.
2017 PeerConnectionInterface::RTCOfferAnswerOptions options;
2018 options.offer_to_receive_audio = 0;
2019 callee()->SetOfferAnswerOptions(options);
2020 } else {
2021 // Stopping the audio RtpTransceiver will cause the media section to be
2022 // rejected in the answer.
2023 callee()->SetRemoteOfferHandler([this] {
2024 callee()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_AUDIO)->Stop();
2025 });
2026 }
Steve Anton15324772018-01-16 10:26:49 -08002027 callee()->AddTrack(callee()->CreateLocalVideoTrack());
deadbeef1dcb1642017-03-29 21:08:16 -07002028 // Do offer/answer and wait for successful end-to-end video frames.
2029 caller()->CreateAndSetAndSignalOffer();
2030 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002031 MediaExpectations media_expectations;
2032 media_expectations.ExpectBidirectionalVideo();
2033 media_expectations.ExpectNoAudio();
2034 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2035
deadbeef1dcb1642017-03-29 21:08:16 -07002036 // Sanity check that the callee's description has a rejected audio section.
2037 ASSERT_NE(nullptr, callee()->pc()->local_description());
2038 const ContentInfo* callee_audio_content =
2039 GetFirstAudioContent(callee()->pc()->local_description()->description());
2040 ASSERT_NE(nullptr, callee_audio_content);
2041 EXPECT_TRUE(callee_audio_content->rejected);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002042 if (sdp_semantics_ == SdpSemantics::kUnifiedPlan) {
2043 // The caller's transceiver should have stopped after receiving the answer.
2044 EXPECT_TRUE(caller()
2045 ->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_AUDIO)
2046 ->stopped());
2047 }
deadbeef1dcb1642017-03-29 21:08:16 -07002048}
2049
2050// Test that if the answerer rejects the video m= section, no video is sent or
2051// received, but audio still can be.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002052TEST_P(PeerConnectionIntegrationTest, AnswererRejectsVideoSection) {
deadbeef1dcb1642017-03-29 21:08:16 -07002053 ASSERT_TRUE(CreatePeerConnectionWrappers());
2054 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002055 caller()->AddAudioVideoTracks();
Seth Hampson2f0d7022018-02-20 11:54:42 -08002056 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2057 // Only add audio track for callee, and set offer_to_receive_video to 0, so
2058 // it will reject the video m= section completely.
2059 PeerConnectionInterface::RTCOfferAnswerOptions options;
2060 options.offer_to_receive_video = 0;
2061 callee()->SetOfferAnswerOptions(options);
2062 } else {
2063 // Stopping the video RtpTransceiver will cause the media section to be
2064 // rejected in the answer.
2065 callee()->SetRemoteOfferHandler([this] {
2066 callee()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)->Stop();
2067 });
2068 }
Steve Anton15324772018-01-16 10:26:49 -08002069 callee()->AddTrack(callee()->CreateLocalAudioTrack());
deadbeef1dcb1642017-03-29 21:08:16 -07002070 // Do offer/answer and wait for successful end-to-end audio frames.
2071 caller()->CreateAndSetAndSignalOffer();
2072 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002073 MediaExpectations media_expectations;
2074 media_expectations.ExpectBidirectionalAudio();
2075 media_expectations.ExpectNoVideo();
2076 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2077
deadbeef1dcb1642017-03-29 21:08:16 -07002078 // Sanity check that the callee's description has a rejected video section.
2079 ASSERT_NE(nullptr, callee()->pc()->local_description());
2080 const ContentInfo* callee_video_content =
2081 GetFirstVideoContent(callee()->pc()->local_description()->description());
2082 ASSERT_NE(nullptr, callee_video_content);
2083 EXPECT_TRUE(callee_video_content->rejected);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002084 if (sdp_semantics_ == SdpSemantics::kUnifiedPlan) {
2085 // The caller's transceiver should have stopped after receiving the answer.
2086 EXPECT_TRUE(caller()
2087 ->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)
2088 ->stopped());
2089 }
deadbeef1dcb1642017-03-29 21:08:16 -07002090}
2091
2092// Test that if the answerer rejects both audio and video m= sections, nothing
2093// bad happens.
2094// TODO(deadbeef): Test that a data channel still works. Currently this doesn't
2095// test anything but the fact that negotiation succeeds, which doesn't mean
2096// much.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002097TEST_P(PeerConnectionIntegrationTest, AnswererRejectsAudioAndVideoSections) {
deadbeef1dcb1642017-03-29 21:08:16 -07002098 ASSERT_TRUE(CreatePeerConnectionWrappers());
2099 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002100 caller()->AddAudioVideoTracks();
Seth Hampson2f0d7022018-02-20 11:54:42 -08002101 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2102 // Don't give the callee any tracks, and set offer_to_receive_X to 0, so it
2103 // will reject both audio and video m= sections.
2104 PeerConnectionInterface::RTCOfferAnswerOptions options;
2105 options.offer_to_receive_audio = 0;
2106 options.offer_to_receive_video = 0;
2107 callee()->SetOfferAnswerOptions(options);
2108 } else {
2109 callee()->SetRemoteOfferHandler([this] {
2110 // Stopping all transceivers will cause all media sections to be rejected.
2111 for (auto transceiver : callee()->pc()->GetTransceivers()) {
2112 transceiver->Stop();
2113 }
2114 });
2115 }
deadbeef1dcb1642017-03-29 21:08:16 -07002116 // Do offer/answer and wait for stable signaling state.
2117 caller()->CreateAndSetAndSignalOffer();
2118 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002119
deadbeef1dcb1642017-03-29 21:08:16 -07002120 // Sanity check that the callee's description has rejected m= sections.
2121 ASSERT_NE(nullptr, callee()->pc()->local_description());
2122 const ContentInfo* callee_audio_content =
2123 GetFirstAudioContent(callee()->pc()->local_description()->description());
2124 ASSERT_NE(nullptr, callee_audio_content);
2125 EXPECT_TRUE(callee_audio_content->rejected);
2126 const ContentInfo* callee_video_content =
2127 GetFirstVideoContent(callee()->pc()->local_description()->description());
2128 ASSERT_NE(nullptr, callee_video_content);
2129 EXPECT_TRUE(callee_video_content->rejected);
2130}
2131
2132// This test sets up an audio and video call between two parties. After the
2133// call runs for a while, the caller sends an updated offer with video being
2134// rejected. Once the re-negotiation is done, the video flow should stop and
2135// the audio flow should continue.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002136TEST_P(PeerConnectionIntegrationTest, VideoRejectedInSubsequentOffer) {
deadbeef1dcb1642017-03-29 21:08:16 -07002137 ASSERT_TRUE(CreatePeerConnectionWrappers());
2138 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002139 caller()->AddAudioVideoTracks();
2140 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002141 caller()->CreateAndSetAndSignalOffer();
2142 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002143 {
2144 MediaExpectations media_expectations;
2145 media_expectations.ExpectBidirectionalAudioAndVideo();
2146 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2147 }
deadbeef1dcb1642017-03-29 21:08:16 -07002148 // Renegotiate, rejecting the video m= section.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002149 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2150 caller()->SetGeneratedSdpMunger(
2151 [](cricket::SessionDescription* description) {
2152 for (cricket::ContentInfo& content : description->contents()) {
2153 if (cricket::IsVideoContent(&content)) {
2154 content.rejected = true;
2155 }
2156 }
2157 });
2158 } else {
2159 caller()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)->Stop();
2160 }
deadbeef1dcb1642017-03-29 21:08:16 -07002161 caller()->CreateAndSetAndSignalOffer();
2162 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
2163
2164 // Sanity check that the caller's description has a rejected video section.
2165 ASSERT_NE(nullptr, caller()->pc()->local_description());
2166 const ContentInfo* caller_video_content =
2167 GetFirstVideoContent(caller()->pc()->local_description()->description());
2168 ASSERT_NE(nullptr, caller_video_content);
2169 EXPECT_TRUE(caller_video_content->rejected);
deadbeef1dcb1642017-03-29 21:08:16 -07002170 // Wait for some additional audio frames to be received.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002171 {
2172 MediaExpectations media_expectations;
2173 media_expectations.ExpectBidirectionalAudio();
2174 media_expectations.ExpectNoVideo();
2175 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2176 }
deadbeef1dcb1642017-03-29 21:08:16 -07002177}
2178
Taylor Brandstetter60c8dc82018-04-11 15:20:27 -07002179// Do one offer/answer with audio, another that disables it (rejecting the m=
2180// section), and another that re-enables it. Regression test for:
2181// bugs.webrtc.org/6023
2182TEST_F(PeerConnectionIntegrationTestPlanB, EnableAudioAfterRejecting) {
2183 ASSERT_TRUE(CreatePeerConnectionWrappers());
2184 ConnectFakeSignaling();
2185
2186 // Add audio track, do normal offer/answer.
2187 rtc::scoped_refptr<webrtc::AudioTrackInterface> track =
2188 caller()->CreateLocalAudioTrack();
2189 rtc::scoped_refptr<webrtc::RtpSenderInterface> sender =
2190 caller()->pc()->AddTrack(track, {"stream"}).MoveValue();
2191 caller()->CreateAndSetAndSignalOffer();
2192 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2193
2194 // Remove audio track, and set offer_to_receive_audio to false to cause the
2195 // m= section to be completely disabled, not just "recvonly".
2196 caller()->pc()->RemoveTrack(sender);
2197 PeerConnectionInterface::RTCOfferAnswerOptions options;
2198 options.offer_to_receive_audio = 0;
2199 caller()->SetOfferAnswerOptions(options);
2200 caller()->CreateAndSetAndSignalOffer();
2201 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2202
2203 // Add the audio track again, expecting negotiation to succeed and frames to
2204 // flow.
2205 sender = caller()->pc()->AddTrack(track, {"stream"}).MoveValue();
2206 options.offer_to_receive_audio = 1;
2207 caller()->SetOfferAnswerOptions(options);
2208 caller()->CreateAndSetAndSignalOffer();
2209 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2210
2211 MediaExpectations media_expectations;
2212 media_expectations.CalleeExpectsSomeAudio();
2213 EXPECT_TRUE(ExpectNewFrames(media_expectations));
2214}
2215
deadbeef1dcb1642017-03-29 21:08:16 -07002216// Basic end-to-end test, but without SSRC/MSID signaling. This functionality
2217// is needed to support legacy endpoints.
2218// TODO(deadbeef): When we support the MID extension and demuxing on MID, also
2219// add a test for an end-to-end test without MID signaling either (basically,
2220// the minimum acceptable SDP).
Seth Hampson2f0d7022018-02-20 11:54:42 -08002221TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithoutSsrcOrMsidSignaling) {
deadbeef1dcb1642017-03-29 21:08:16 -07002222 ASSERT_TRUE(CreatePeerConnectionWrappers());
2223 ConnectFakeSignaling();
2224 // Add audio and video, testing that packets can be demuxed on payload type.
Steve Anton15324772018-01-16 10:26:49 -08002225 caller()->AddAudioVideoTracks();
2226 callee()->AddAudioVideoTracks();
deadbeefd8ad7882017-04-18 16:01:17 -07002227 // Remove SSRCs and MSIDs from the received offer SDP.
2228 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
deadbeef1dcb1642017-03-29 21:08:16 -07002229 caller()->CreateAndSetAndSignalOffer();
2230 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002231 MediaExpectations media_expectations;
2232 media_expectations.ExpectBidirectionalAudioAndVideo();
2233 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002234}
2235
Seth Hampson5897a6e2018-04-03 11:16:33 -07002236// Basic end-to-end test, without SSRC signaling. This means that the track
2237// was created properly and frames are delivered when the MSIDs are communicated
2238// with a=msid lines and no a=ssrc lines.
2239TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
2240 EndToEndCallWithoutSsrcSignaling) {
2241 const char kStreamId[] = "streamId";
2242 ASSERT_TRUE(CreatePeerConnectionWrappers());
2243 ConnectFakeSignaling();
2244 // Add just audio tracks.
2245 caller()->AddTrack(caller()->CreateLocalAudioTrack(), {kStreamId});
2246 callee()->AddAudioTrack();
2247
2248 // Remove SSRCs from the received offer SDP.
2249 callee()->SetReceivedSdpMunger(RemoveSsrcsAndKeepMsids);
2250 caller()->CreateAndSetAndSignalOffer();
2251 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2252 MediaExpectations media_expectations;
2253 media_expectations.ExpectBidirectionalAudio();
2254 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2255}
2256
deadbeef1dcb1642017-03-29 21:08:16 -07002257// Test that if two video tracks are sent (from caller to callee, in this test),
2258// they're transmitted correctly end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002259TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithTwoVideoTracks) {
deadbeef1dcb1642017-03-29 21:08:16 -07002260 ASSERT_TRUE(CreatePeerConnectionWrappers());
2261 ConnectFakeSignaling();
2262 // Add one audio/video stream, and one video-only stream.
Steve Anton15324772018-01-16 10:26:49 -08002263 caller()->AddAudioVideoTracks();
2264 caller()->AddVideoTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07002265 caller()->CreateAndSetAndSignalOffer();
2266 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Steve Anton15324772018-01-16 10:26:49 -08002267 ASSERT_EQ(3u, callee()->pc()->GetReceivers().size());
Seth Hampson2f0d7022018-02-20 11:54:42 -08002268
2269 MediaExpectations media_expectations;
2270 media_expectations.CalleeExpectsSomeAudioAndVideo();
2271 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002272}
2273
2274static void MakeSpecCompliantMaxBundleOffer(cricket::SessionDescription* desc) {
2275 bool first = true;
2276 for (cricket::ContentInfo& content : desc->contents()) {
2277 if (first) {
2278 first = false;
2279 continue;
2280 }
2281 content.bundle_only = true;
2282 }
2283 first = true;
2284 for (cricket::TransportInfo& transport : desc->transport_infos()) {
2285 if (first) {
2286 first = false;
2287 continue;
2288 }
2289 transport.description.ice_ufrag.clear();
2290 transport.description.ice_pwd.clear();
2291 transport.description.connection_role = cricket::CONNECTIONROLE_NONE;
2292 transport.description.identity_fingerprint.reset(nullptr);
2293 }
2294}
2295
2296// Test that if applying a true "max bundle" offer, which uses ports of 0,
2297// "a=bundle-only", omitting "a=fingerprint", "a=setup", "a=ice-ufrag" and
2298// "a=ice-pwd" for all but the audio "m=" section, negotiation still completes
2299// successfully and media flows.
2300// TODO(deadbeef): Update this test to also omit "a=rtcp-mux", once that works.
2301// TODO(deadbeef): Won't need this test once we start generating actual
2302// standards-compliant SDP.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002303TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07002304 EndToEndCallWithSpecCompliantMaxBundleOffer) {
2305 ASSERT_TRUE(CreatePeerConnectionWrappers());
2306 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002307 caller()->AddAudioVideoTracks();
2308 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002309 // Do the equivalent of setting the port to 0, adding a=bundle-only, and
2310 // removing a=ice-ufrag, a=ice-pwd, a=fingerprint and a=setup from all
2311 // but the first m= section.
2312 callee()->SetReceivedSdpMunger(MakeSpecCompliantMaxBundleOffer);
2313 caller()->CreateAndSetAndSignalOffer();
2314 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002315 MediaExpectations media_expectations;
2316 media_expectations.ExpectBidirectionalAudioAndVideo();
2317 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002318}
2319
2320// Test that we can receive the audio output level from a remote audio track.
2321// TODO(deadbeef): Use a fake audio source and verify that the output level is
2322// exactly what the source on the other side was configured with.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002323TEST_P(PeerConnectionIntegrationTest, GetAudioOutputLevelStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-29 21:08:16 -07002324 ASSERT_TRUE(CreatePeerConnectionWrappers());
2325 ConnectFakeSignaling();
2326 // Just add an audio track.
Steve Anton15324772018-01-16 10:26:49 -08002327 caller()->AddAudioTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07002328 caller()->CreateAndSetAndSignalOffer();
2329 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2330
2331 // Get the audio output level stats. Note that the level is not available
2332 // until an RTCP packet has been received.
deadbeefd8ad7882017-04-18 16:01:17 -07002333 EXPECT_TRUE_WAIT(callee()->OldGetStats()->AudioOutputLevel() > 0,
deadbeef1dcb1642017-03-29 21:08:16 -07002334 kMaxWaitForFramesMs);
2335}
2336
2337// Test that an audio input level is reported.
2338// TODO(deadbeef): Use a fake audio source and verify that the input level is
2339// exactly what the source was configured with.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002340TEST_P(PeerConnectionIntegrationTest, GetAudioInputLevelStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-29 21:08:16 -07002341 ASSERT_TRUE(CreatePeerConnectionWrappers());
2342 ConnectFakeSignaling();
2343 // Just add an audio track.
Steve Anton15324772018-01-16 10:26:49 -08002344 caller()->AddAudioTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07002345 caller()->CreateAndSetAndSignalOffer();
2346 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2347
2348 // Get the audio input level stats. The level should be available very
2349 // soon after the test starts.
deadbeefd8ad7882017-04-18 16:01:17 -07002350 EXPECT_TRUE_WAIT(caller()->OldGetStats()->AudioInputLevel() > 0,
deadbeef1dcb1642017-03-29 21:08:16 -07002351 kMaxWaitForStatsMs);
2352}
2353
2354// Test that we can get incoming byte counts from both audio and video tracks.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002355TEST_P(PeerConnectionIntegrationTest, GetBytesReceivedStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-29 21:08:16 -07002356 ASSERT_TRUE(CreatePeerConnectionWrappers());
2357 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002358 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002359 // Do offer/answer, wait for the callee to receive some frames.
2360 caller()->CreateAndSetAndSignalOffer();
2361 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002362
2363 MediaExpectations media_expectations;
2364 media_expectations.CalleeExpectsSomeAudioAndVideo();
2365 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002366
2367 // Get a handle to the remote tracks created, so they can be used as GetStats
2368 // filters.
Steve Anton15324772018-01-16 10:26:49 -08002369 for (auto receiver : callee()->pc()->GetReceivers()) {
2370 // We received frames, so we definitely should have nonzero "received bytes"
2371 // stats at this point.
2372 EXPECT_GT(callee()->OldGetStatsForTrack(receiver->track())->BytesReceived(),
2373 0);
2374 }
deadbeef1dcb1642017-03-29 21:08:16 -07002375}
2376
2377// Test that we can get outgoing byte counts from both audio and video tracks.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002378TEST_P(PeerConnectionIntegrationTest, GetBytesSentStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-29 21:08:16 -07002379 ASSERT_TRUE(CreatePeerConnectionWrappers());
2380 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002381 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002382 auto audio_track = caller()->CreateLocalAudioTrack();
2383 auto video_track = caller()->CreateLocalVideoTrack();
Steve Anton15324772018-01-16 10:26:49 -08002384 caller()->AddTrack(audio_track);
2385 caller()->AddTrack(video_track);
deadbeef1dcb1642017-03-29 21:08:16 -07002386 // Do offer/answer, wait for the callee to receive some frames.
2387 caller()->CreateAndSetAndSignalOffer();
2388 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002389 MediaExpectations media_expectations;
2390 media_expectations.CalleeExpectsSomeAudioAndVideo();
2391 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002392
2393 // The callee received frames, so we definitely should have nonzero "sent
2394 // bytes" stats at this point.
deadbeefd8ad7882017-04-18 16:01:17 -07002395 EXPECT_GT(caller()->OldGetStatsForTrack(audio_track)->BytesSent(), 0);
2396 EXPECT_GT(caller()->OldGetStatsForTrack(video_track)->BytesSent(), 0);
2397}
2398
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002399// Test that we can get capture start ntp time.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002400TEST_P(PeerConnectionIntegrationTest, GetCaptureStartNtpTimeWithOldStatsApi) {
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002401 ASSERT_TRUE(CreatePeerConnectionWrappers());
2402 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002403 caller()->AddAudioTrack();
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002404
Steve Anton15324772018-01-16 10:26:49 -08002405 callee()->AddAudioTrack();
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002406
2407 // Do offer/answer, wait for the callee to receive some frames.
2408 caller()->CreateAndSetAndSignalOffer();
2409 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2410
2411 // Get the remote audio track created on the receiver, so they can be used as
2412 // GetStats filters.
Steve Antonfc853712018-03-01 13:48:58 -08002413 auto receivers = callee()->pc()->GetReceivers();
2414 ASSERT_EQ(1u, receivers.size());
2415 auto remote_audio_track = receivers[0]->track();
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002416
2417 // Get the audio output level stats. Note that the level is not available
2418 // until an RTCP packet has been received.
Zhi Huange830e682018-03-30 10:48:35 -07002419 EXPECT_TRUE_WAIT(
2420 callee()->OldGetStatsForTrack(remote_audio_track)->CaptureStartNtpTime() >
2421 0,
2422 2 * kMaxWaitForFramesMs);
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002423}
2424
deadbeefd8ad7882017-04-18 16:01:17 -07002425// Test that we can get stats (using the new stats implemnetation) for
2426// unsignaled streams. Meaning when SSRCs/MSIDs aren't signaled explicitly in
2427// SDP.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002428TEST_P(PeerConnectionIntegrationTest,
deadbeefd8ad7882017-04-18 16:01:17 -07002429 GetStatsForUnsignaledStreamWithNewStatsApi) {
2430 ASSERT_TRUE(CreatePeerConnectionWrappers());
2431 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002432 caller()->AddAudioTrack();
deadbeefd8ad7882017-04-18 16:01:17 -07002433 // Remove SSRCs and MSIDs from the received offer SDP.
2434 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
2435 caller()->CreateAndSetAndSignalOffer();
2436 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002437 MediaExpectations media_expectations;
2438 media_expectations.CalleeExpectsSomeAudio(1);
2439 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeefd8ad7882017-04-18 16:01:17 -07002440
2441 // We received a frame, so we should have nonzero "bytes received" stats for
2442 // the unsignaled stream, if stats are working for it.
2443 rtc::scoped_refptr<const webrtc::RTCStatsReport> report =
2444 callee()->NewGetStats();
2445 ASSERT_NE(nullptr, report);
2446 auto inbound_stream_stats =
2447 report->GetStatsOfType<webrtc::RTCInboundRTPStreamStats>();
2448 ASSERT_EQ(1U, inbound_stream_stats.size());
2449 ASSERT_TRUE(inbound_stream_stats[0]->bytes_received.is_defined());
2450 ASSERT_GT(*inbound_stream_stats[0]->bytes_received, 0U);
zhihuangf8164932017-05-19 13:09:47 -07002451 ASSERT_TRUE(inbound_stream_stats[0]->track_id.is_defined());
2452}
2453
2454// Test that we can successfully get the media related stats (audio level
2455// etc.) for the unsignaled stream.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002456TEST_P(PeerConnectionIntegrationTest,
zhihuangf8164932017-05-19 13:09:47 -07002457 GetMediaStatsForUnsignaledStreamWithNewStatsApi) {
2458 ASSERT_TRUE(CreatePeerConnectionWrappers());
2459 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002460 caller()->AddAudioVideoTracks();
zhihuangf8164932017-05-19 13:09:47 -07002461 // Remove SSRCs and MSIDs from the received offer SDP.
2462 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
2463 caller()->CreateAndSetAndSignalOffer();
2464 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002465 MediaExpectations media_expectations;
2466 media_expectations.CalleeExpectsSomeAudio(1);
2467 media_expectations.CalleeExpectsSomeVideo(1);
2468 ASSERT_TRUE(ExpectNewFrames(media_expectations));
zhihuangf8164932017-05-19 13:09:47 -07002469
2470 rtc::scoped_refptr<const webrtc::RTCStatsReport> report =
2471 callee()->NewGetStats();
2472 ASSERT_NE(nullptr, report);
2473
2474 auto media_stats = report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
2475 auto audio_index = FindFirstMediaStatsIndexByKind("audio", media_stats);
2476 ASSERT_GE(audio_index, 0);
2477 EXPECT_TRUE(media_stats[audio_index]->audio_level.is_defined());
deadbeef1dcb1642017-03-29 21:08:16 -07002478}
2479
deadbeef4e2deab2017-09-20 13:56:21 -07002480// Helper for test below.
2481void ModifySsrcs(cricket::SessionDescription* desc) {
2482 for (ContentInfo& content : desc->contents()) {
Steve Antonb1c1de12017-12-21 15:14:30 -08002483 for (cricket::StreamParams& stream :
2484 content.media_description()->mutable_streams()) {
deadbeef4e2deab2017-09-20 13:56:21 -07002485 for (uint32_t& ssrc : stream.ssrcs) {
2486 ssrc = rtc::CreateRandomId();
2487 }
2488 }
2489 }
2490}
2491
2492// Test that the "RTCMediaSteamTrackStats" object is updated correctly when
2493// SSRCs are unsignaled, and the SSRC of the received (audio) stream changes.
2494// This should result in two "RTCInboundRTPStreamStats", but only one
2495// "RTCMediaStreamTrackStats", whose counters go up continuously rather than
2496// being reset to 0 once the SSRC change occurs.
2497//
2498// Regression test for this bug:
2499// https://bugs.chromium.org/p/webrtc/issues/detail?id=8158
2500//
2501// The bug causes the track stats to only represent one of the two streams:
2502// whichever one has the higher SSRC. So with this bug, there was a 50% chance
2503// that the track stat counters would reset to 0 when the new stream is
2504// received, and a 50% chance that they'll stop updating (while
2505// "concealed_samples" continues increasing, due to silence being generated for
2506// the inactive stream).
Seth Hampson2f0d7022018-02-20 11:54:42 -08002507TEST_P(PeerConnectionIntegrationTest,
Steve Anton83119dd2017-11-10 16:19:52 -08002508 TrackStatsUpdatedCorrectlyWhenUnsignaledSsrcChanges) {
deadbeef4e2deab2017-09-20 13:56:21 -07002509 ASSERT_TRUE(CreatePeerConnectionWrappers());
2510 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002511 caller()->AddAudioTrack();
deadbeef4e2deab2017-09-20 13:56:21 -07002512 // Remove SSRCs and MSIDs from the received offer SDP, simulating an endpoint
2513 // that doesn't signal SSRCs (from the callee's perspective).
2514 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
2515 caller()->CreateAndSetAndSignalOffer();
2516 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2517 // Wait for 50 audio frames (500ms of audio) to be received by the callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002518 {
2519 MediaExpectations media_expectations;
2520 media_expectations.CalleeExpectsSomeAudio(50);
2521 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2522 }
deadbeef4e2deab2017-09-20 13:56:21 -07002523 // Some audio frames were received, so we should have nonzero "samples
2524 // received" for the track.
2525 rtc::scoped_refptr<const webrtc::RTCStatsReport> report =
2526 callee()->NewGetStats();
2527 ASSERT_NE(nullptr, report);
2528 auto track_stats = report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
2529 ASSERT_EQ(1U, track_stats.size());
2530 ASSERT_TRUE(track_stats[0]->total_samples_received.is_defined());
2531 ASSERT_GT(*track_stats[0]->total_samples_received, 0U);
2532 // uint64_t prev_samples_received = *track_stats[0]->total_samples_received;
2533
2534 // Create a new offer and munge it to cause the caller to use a new SSRC.
2535 caller()->SetGeneratedSdpMunger(ModifySsrcs);
2536 caller()->CreateAndSetAndSignalOffer();
2537 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2538 // Wait for 25 more audio frames (250ms of audio) to be received, from the new
2539 // SSRC.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002540 {
2541 MediaExpectations media_expectations;
2542 media_expectations.CalleeExpectsSomeAudio(25);
2543 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2544 }
deadbeef4e2deab2017-09-20 13:56:21 -07002545
2546 report = callee()->NewGetStats();
2547 ASSERT_NE(nullptr, report);
2548 track_stats = report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
2549 ASSERT_EQ(1U, track_stats.size());
2550 ASSERT_TRUE(track_stats[0]->total_samples_received.is_defined());
2551 // The "total samples received" stat should only be greater than it was
2552 // before.
2553 // TODO(deadbeef): Uncomment this assertion once the bug is completely fixed.
2554 // Right now, the new SSRC will cause the counters to reset to 0.
2555 // EXPECT_GT(*track_stats[0]->total_samples_received, prev_samples_received);
2556
2557 // Additionally, the percentage of concealed samples (samples generated to
Steve Anton83119dd2017-11-10 16:19:52 -08002558 // conceal packet loss) should be less than 50%. If it's greater, that's a
deadbeef4e2deab2017-09-20 13:56:21 -07002559 // good sign that we're seeing stats from the old stream that's no longer
2560 // receiving packets, and is generating concealed samples of silence.
Steve Anton83119dd2017-11-10 16:19:52 -08002561 constexpr double kAcceptableConcealedSamplesPercentage = 0.50;
deadbeef4e2deab2017-09-20 13:56:21 -07002562 ASSERT_TRUE(track_stats[0]->concealed_samples.is_defined());
2563 EXPECT_LT(*track_stats[0]->concealed_samples,
2564 *track_stats[0]->total_samples_received *
2565 kAcceptableConcealedSamplesPercentage);
2566
2567 // Also ensure that we have two "RTCInboundRTPStreamStats" as expected, as a
2568 // sanity check that the SSRC really changed.
2569 // TODO(deadbeef): This isn't working right now, because we're not returning
2570 // *any* stats for the inactive stream. Uncomment when the bug is completely
2571 // fixed.
2572 // auto inbound_stream_stats =
2573 // report->GetStatsOfType<webrtc::RTCInboundRTPStreamStats>();
2574 // ASSERT_EQ(2U, inbound_stream_stats.size());
2575}
2576
deadbeef1dcb1642017-03-29 21:08:16 -07002577// Test that DTLS 1.0 is used if both sides only support DTLS 1.0.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002578TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithDtls10) {
deadbeef1dcb1642017-03-29 21:08:16 -07002579 PeerConnectionFactory::Options dtls_10_options;
2580 dtls_10_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
2581 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(dtls_10_options,
2582 dtls_10_options));
2583 ConnectFakeSignaling();
2584 // Do normal offer/answer and wait for some frames to be received in each
2585 // direction.
Steve Anton15324772018-01-16 10:26:49 -08002586 caller()->AddAudioVideoTracks();
2587 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002588 caller()->CreateAndSetAndSignalOffer();
2589 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002590 MediaExpectations media_expectations;
2591 media_expectations.ExpectBidirectionalAudioAndVideo();
2592 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002593}
2594
2595// Test getting cipher stats and UMA metrics when DTLS 1.0 is negotiated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002596TEST_P(PeerConnectionIntegrationTest, Dtls10CipherStatsAndUmaMetrics) {
deadbeef1dcb1642017-03-29 21:08:16 -07002597 PeerConnectionFactory::Options dtls_10_options;
2598 dtls_10_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
2599 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(dtls_10_options,
2600 dtls_10_options));
2601 ConnectFakeSignaling();
2602 // Register UMA observer before signaling begins.
2603 rtc::scoped_refptr<webrtc::FakeMetricsObserver> caller_observer =
2604 new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
2605 caller()->pc()->RegisterUMAObserver(caller_observer);
Steve Anton15324772018-01-16 10:26:49 -08002606 caller()->AddAudioVideoTracks();
2607 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002608 caller()->CreateAndSetAndSignalOffer();
2609 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2610 EXPECT_TRUE_WAIT(rtc::SSLStreamAdapter::IsAcceptableCipher(
deadbeefd8ad7882017-04-18 16:01:17 -07002611 caller()->OldGetStats()->DtlsCipher(), rtc::KT_DEFAULT),
deadbeef1dcb1642017-03-29 21:08:16 -07002612 kDefaultTimeout);
2613 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
deadbeefd8ad7882017-04-18 16:01:17 -07002614 caller()->OldGetStats()->SrtpCipher(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07002615 EXPECT_EQ(1,
2616 caller_observer->GetEnumCounter(webrtc::kEnumCounterAudioSrtpCipher,
2617 kDefaultSrtpCryptoSuite));
2618}
2619
2620// Test getting cipher stats and UMA metrics when DTLS 1.2 is negotiated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002621TEST_P(PeerConnectionIntegrationTest, Dtls12CipherStatsAndUmaMetrics) {
deadbeef1dcb1642017-03-29 21:08:16 -07002622 PeerConnectionFactory::Options dtls_12_options;
2623 dtls_12_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
2624 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(dtls_12_options,
2625 dtls_12_options));
2626 ConnectFakeSignaling();
2627 // Register UMA observer before signaling begins.
2628 rtc::scoped_refptr<webrtc::FakeMetricsObserver> caller_observer =
2629 new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
2630 caller()->pc()->RegisterUMAObserver(caller_observer);
Steve Anton15324772018-01-16 10:26:49 -08002631 caller()->AddAudioVideoTracks();
2632 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002633 caller()->CreateAndSetAndSignalOffer();
2634 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2635 EXPECT_TRUE_WAIT(rtc::SSLStreamAdapter::IsAcceptableCipher(
deadbeefd8ad7882017-04-18 16:01:17 -07002636 caller()->OldGetStats()->DtlsCipher(), rtc::KT_DEFAULT),
deadbeef1dcb1642017-03-29 21:08:16 -07002637 kDefaultTimeout);
2638 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
deadbeefd8ad7882017-04-18 16:01:17 -07002639 caller()->OldGetStats()->SrtpCipher(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07002640 EXPECT_EQ(1,
2641 caller_observer->GetEnumCounter(webrtc::kEnumCounterAudioSrtpCipher,
2642 kDefaultSrtpCryptoSuite));
2643}
2644
2645// Test that DTLS 1.0 can be used if the caller supports DTLS 1.2 and the
2646// callee only supports 1.0.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002647TEST_P(PeerConnectionIntegrationTest, CallerDtls12ToCalleeDtls10) {
deadbeef1dcb1642017-03-29 21:08:16 -07002648 PeerConnectionFactory::Options caller_options;
2649 caller_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
2650 PeerConnectionFactory::Options callee_options;
2651 callee_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
2652 ASSERT_TRUE(
2653 CreatePeerConnectionWrappersWithOptions(caller_options, callee_options));
2654 ConnectFakeSignaling();
2655 // Do normal offer/answer and wait for some frames to be received in each
2656 // direction.
Steve Anton15324772018-01-16 10:26:49 -08002657 caller()->AddAudioVideoTracks();
2658 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002659 caller()->CreateAndSetAndSignalOffer();
2660 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002661 MediaExpectations media_expectations;
2662 media_expectations.ExpectBidirectionalAudioAndVideo();
2663 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002664}
2665
2666// Test that DTLS 1.0 can be used if the caller only supports DTLS 1.0 and the
2667// callee supports 1.2.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002668TEST_P(PeerConnectionIntegrationTest, CallerDtls10ToCalleeDtls12) {
deadbeef1dcb1642017-03-29 21:08:16 -07002669 PeerConnectionFactory::Options caller_options;
2670 caller_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
2671 PeerConnectionFactory::Options callee_options;
2672 callee_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
2673 ASSERT_TRUE(
2674 CreatePeerConnectionWrappersWithOptions(caller_options, callee_options));
2675 ConnectFakeSignaling();
2676 // Do normal offer/answer and wait for some frames to be received in each
2677 // direction.
Steve Anton15324772018-01-16 10:26:49 -08002678 caller()->AddAudioVideoTracks();
2679 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002680 caller()->CreateAndSetAndSignalOffer();
2681 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002682 MediaExpectations media_expectations;
2683 media_expectations.ExpectBidirectionalAudioAndVideo();
2684 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002685}
2686
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07002687// The three tests below verify that "enable_aes128_sha1_32_crypto_cipher"
2688// works as expected; the cipher should only be used if enabled by both sides.
2689TEST_P(PeerConnectionIntegrationTest,
2690 Aes128Sha1_32_CipherNotUsedWhenOnlyCallerSupported) {
2691 PeerConnectionFactory::Options caller_options;
2692 caller_options.crypto_options.enable_aes128_sha1_32_crypto_cipher = true;
2693 PeerConnectionFactory::Options callee_options;
2694 callee_options.crypto_options.enable_aes128_sha1_32_crypto_cipher = false;
2695 int expected_cipher_suite = rtc::SRTP_AES128_CM_SHA1_80;
2696 TestNegotiatedCipherSuite(caller_options, callee_options,
2697 expected_cipher_suite);
2698}
2699
2700TEST_P(PeerConnectionIntegrationTest,
2701 Aes128Sha1_32_CipherNotUsedWhenOnlyCalleeSupported) {
2702 PeerConnectionFactory::Options caller_options;
2703 caller_options.crypto_options.enable_aes128_sha1_32_crypto_cipher = false;
2704 PeerConnectionFactory::Options callee_options;
2705 callee_options.crypto_options.enable_aes128_sha1_32_crypto_cipher = true;
2706 int expected_cipher_suite = rtc::SRTP_AES128_CM_SHA1_80;
2707 TestNegotiatedCipherSuite(caller_options, callee_options,
2708 expected_cipher_suite);
2709}
2710
2711TEST_P(PeerConnectionIntegrationTest, Aes128Sha1_32_CipherUsedWhenSupported) {
2712 PeerConnectionFactory::Options caller_options;
2713 caller_options.crypto_options.enable_aes128_sha1_32_crypto_cipher = true;
2714 PeerConnectionFactory::Options callee_options;
2715 callee_options.crypto_options.enable_aes128_sha1_32_crypto_cipher = true;
2716 int expected_cipher_suite = rtc::SRTP_AES128_CM_SHA1_32;
2717 TestNegotiatedCipherSuite(caller_options, callee_options,
2718 expected_cipher_suite);
2719}
2720
deadbeef1dcb1642017-03-29 21:08:16 -07002721// Test that a non-GCM cipher is used if both sides only support non-GCM.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002722TEST_P(PeerConnectionIntegrationTest, NonGcmCipherUsedWhenGcmNotSupported) {
deadbeef1dcb1642017-03-29 21:08:16 -07002723 bool local_gcm_enabled = false;
2724 bool remote_gcm_enabled = false;
2725 int expected_cipher_suite = kDefaultSrtpCryptoSuite;
2726 TestGcmNegotiationUsesCipherSuite(local_gcm_enabled, remote_gcm_enabled,
2727 expected_cipher_suite);
2728}
2729
2730// Test that a GCM cipher is used if both ends support it.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002731TEST_P(PeerConnectionIntegrationTest, GcmCipherUsedWhenGcmSupported) {
deadbeef1dcb1642017-03-29 21:08:16 -07002732 bool local_gcm_enabled = true;
2733 bool remote_gcm_enabled = true;
2734 int expected_cipher_suite = kDefaultSrtpCryptoSuiteGcm;
2735 TestGcmNegotiationUsesCipherSuite(local_gcm_enabled, remote_gcm_enabled,
2736 expected_cipher_suite);
2737}
2738
2739// Test that GCM isn't used if only the offerer supports it.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002740TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07002741 NonGcmCipherUsedWhenOnlyCallerSupportsGcm) {
2742 bool local_gcm_enabled = true;
2743 bool remote_gcm_enabled = false;
2744 int expected_cipher_suite = kDefaultSrtpCryptoSuite;
2745 TestGcmNegotiationUsesCipherSuite(local_gcm_enabled, remote_gcm_enabled,
2746 expected_cipher_suite);
2747}
2748
2749// Test that GCM isn't used if only the answerer supports it.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002750TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07002751 NonGcmCipherUsedWhenOnlyCalleeSupportsGcm) {
2752 bool local_gcm_enabled = false;
2753 bool remote_gcm_enabled = true;
2754 int expected_cipher_suite = kDefaultSrtpCryptoSuite;
2755 TestGcmNegotiationUsesCipherSuite(local_gcm_enabled, remote_gcm_enabled,
2756 expected_cipher_suite);
2757}
2758
deadbeef7914b8c2017-04-21 03:23:33 -07002759// Verify that media can be transmitted end-to-end when GCM crypto suites are
2760// enabled. Note that the above tests, such as GcmCipherUsedWhenGcmSupported,
2761// only verify that a GCM cipher is negotiated, and not necessarily that SRTP
2762// works with it.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002763TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithGcmCipher) {
deadbeef7914b8c2017-04-21 03:23:33 -07002764 PeerConnectionFactory::Options gcm_options;
2765 gcm_options.crypto_options.enable_gcm_crypto_suites = true;
2766 ASSERT_TRUE(
2767 CreatePeerConnectionWrappersWithOptions(gcm_options, gcm_options));
2768 ConnectFakeSignaling();
2769 // Do normal offer/answer and wait for some frames to be received in each
2770 // direction.
Steve Anton15324772018-01-16 10:26:49 -08002771 caller()->AddAudioVideoTracks();
2772 callee()->AddAudioVideoTracks();
deadbeef7914b8c2017-04-21 03:23:33 -07002773 caller()->CreateAndSetAndSignalOffer();
2774 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002775 MediaExpectations media_expectations;
2776 media_expectations.ExpectBidirectionalAudioAndVideo();
2777 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef7914b8c2017-04-21 03:23:33 -07002778}
2779
deadbeef1dcb1642017-03-29 21:08:16 -07002780// This test sets up a call between two parties with audio, video and an RTP
2781// data channel.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002782TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithRtpDataChannel) {
deadbeef1dcb1642017-03-29 21:08:16 -07002783 FakeConstraints setup_constraints;
2784 setup_constraints.SetAllowRtpDataChannels();
2785 ASSERT_TRUE(CreatePeerConnectionWrappersWithConstraints(&setup_constraints,
2786 &setup_constraints));
2787 ConnectFakeSignaling();
2788 // Expect that data channel created on caller side will show up for callee as
2789 // well.
2790 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08002791 caller()->AddAudioVideoTracks();
2792 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002793 caller()->CreateAndSetAndSignalOffer();
2794 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2795 // Ensure the existence of the RTP data channel didn't impede audio/video.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002796 MediaExpectations media_expectations;
2797 media_expectations.ExpectBidirectionalAudioAndVideo();
2798 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002799 ASSERT_NE(nullptr, caller()->data_channel());
2800 ASSERT_NE(nullptr, callee()->data_channel());
2801 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
2802 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
2803
2804 // Ensure data can be sent in both directions.
2805 std::string data = "hello world";
2806 SendRtpDataWithRetries(caller()->data_channel(), data, 5);
2807 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
2808 kDefaultTimeout);
2809 SendRtpDataWithRetries(callee()->data_channel(), data, 5);
2810 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
2811 kDefaultTimeout);
2812}
2813
2814// Ensure that an RTP data channel is signaled as closed for the caller when
2815// the callee rejects it in a subsequent offer.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002816TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07002817 RtpDataChannelSignaledClosedInCalleeOffer) {
2818 // Same procedure as above test.
2819 FakeConstraints setup_constraints;
2820 setup_constraints.SetAllowRtpDataChannels();
2821 ASSERT_TRUE(CreatePeerConnectionWrappersWithConstraints(&setup_constraints,
2822 &setup_constraints));
2823 ConnectFakeSignaling();
2824 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08002825 caller()->AddAudioVideoTracks();
2826 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002827 caller()->CreateAndSetAndSignalOffer();
2828 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2829 ASSERT_NE(nullptr, caller()->data_channel());
2830 ASSERT_NE(nullptr, callee()->data_channel());
2831 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
2832 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
2833
2834 // Close the data channel on the callee, and do an updated offer/answer.
2835 callee()->data_channel()->Close();
2836 callee()->CreateAndSetAndSignalOffer();
2837 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2838 EXPECT_FALSE(caller()->data_observer()->IsOpen());
2839 EXPECT_FALSE(callee()->data_observer()->IsOpen());
2840}
2841
2842// Tests that data is buffered in an RTP data channel until an observer is
2843// registered for it.
2844//
2845// NOTE: RTP data channels can receive data before the underlying
2846// transport has detected that a channel is writable and thus data can be
2847// received before the data channel state changes to open. That is hard to test
2848// but the same buffering is expected to be used in that case.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002849TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07002850 DataBufferedUntilRtpDataChannelObserverRegistered) {
2851 // Use fake clock and simulated network delay so that we predictably can wait
2852 // until an SCTP message has been delivered without "sleep()"ing.
2853 rtc::ScopedFakeClock fake_clock;
2854 // Some things use a time of "0" as a special value, so we need to start out
2855 // the fake clock at a nonzero time.
2856 // TODO(deadbeef): Fix this.
2857 fake_clock.AdvanceTime(rtc::TimeDelta::FromSeconds(1));
2858 virtual_socket_server()->set_delay_mean(5); // 5 ms per hop.
2859 virtual_socket_server()->UpdateDelayDistribution();
2860
2861 FakeConstraints constraints;
2862 constraints.SetAllowRtpDataChannels();
2863 ASSERT_TRUE(
2864 CreatePeerConnectionWrappersWithConstraints(&constraints, &constraints));
2865 ConnectFakeSignaling();
2866 caller()->CreateDataChannel();
2867 caller()->CreateAndSetAndSignalOffer();
2868 ASSERT_TRUE(caller()->data_channel() != nullptr);
2869 ASSERT_TRUE_SIMULATED_WAIT(callee()->data_channel() != nullptr,
2870 kDefaultTimeout, fake_clock);
2871 ASSERT_TRUE_SIMULATED_WAIT(caller()->data_observer()->IsOpen(),
2872 kDefaultTimeout, fake_clock);
2873 ASSERT_EQ_SIMULATED_WAIT(DataChannelInterface::kOpen,
2874 callee()->data_channel()->state(), kDefaultTimeout,
2875 fake_clock);
2876
2877 // Unregister the observer which is normally automatically registered.
2878 callee()->data_channel()->UnregisterObserver();
2879 // Send data and advance fake clock until it should have been received.
2880 std::string data = "hello world";
2881 caller()->data_channel()->Send(DataBuffer(data));
2882 SIMULATED_WAIT(false, 50, fake_clock);
2883
2884 // Attach data channel and expect data to be received immediately. Note that
2885 // EXPECT_EQ_WAIT is used, such that the simulated clock is not advanced any
2886 // further, but data can be received even if the callback is asynchronous.
2887 MockDataChannelObserver new_observer(callee()->data_channel());
2888 EXPECT_EQ_SIMULATED_WAIT(data, new_observer.last_message(), kDefaultTimeout,
2889 fake_clock);
2890}
2891
2892// This test sets up a call between two parties with audio, video and but only
2893// the caller client supports RTP data channels.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002894TEST_P(PeerConnectionIntegrationTest, RtpDataChannelsRejectedByCallee) {
deadbeef1dcb1642017-03-29 21:08:16 -07002895 FakeConstraints setup_constraints_1;
2896 setup_constraints_1.SetAllowRtpDataChannels();
2897 // Must disable DTLS to make negotiation succeed.
2898 setup_constraints_1.SetMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
2899 false);
2900 FakeConstraints setup_constraints_2;
2901 setup_constraints_2.SetMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
2902 false);
2903 ASSERT_TRUE(CreatePeerConnectionWrappersWithConstraints(
2904 &setup_constraints_1, &setup_constraints_2));
2905 ConnectFakeSignaling();
2906 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08002907 caller()->AddAudioVideoTracks();
2908 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002909 caller()->CreateAndSetAndSignalOffer();
2910 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2911 // The caller should still have a data channel, but it should be closed, and
2912 // one should ever have been created for the callee.
2913 EXPECT_TRUE(caller()->data_channel() != nullptr);
2914 EXPECT_FALSE(caller()->data_observer()->IsOpen());
2915 EXPECT_EQ(nullptr, callee()->data_channel());
2916}
2917
2918// This test sets up a call between two parties with audio, and video. When
2919// audio and video is setup and flowing, an RTP data channel is negotiated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002920TEST_P(PeerConnectionIntegrationTest, AddRtpDataChannelInSubsequentOffer) {
deadbeef1dcb1642017-03-29 21:08:16 -07002921 FakeConstraints setup_constraints;
2922 setup_constraints.SetAllowRtpDataChannels();
2923 ASSERT_TRUE(CreatePeerConnectionWrappersWithConstraints(&setup_constraints,
2924 &setup_constraints));
2925 ConnectFakeSignaling();
2926 // Do initial offer/answer with audio/video.
Steve Anton15324772018-01-16 10:26:49 -08002927 caller()->AddAudioVideoTracks();
2928 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002929 caller()->CreateAndSetAndSignalOffer();
2930 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2931 // Create data channel and do new offer and answer.
2932 caller()->CreateDataChannel();
2933 caller()->CreateAndSetAndSignalOffer();
2934 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2935 ASSERT_NE(nullptr, caller()->data_channel());
2936 ASSERT_NE(nullptr, callee()->data_channel());
2937 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
2938 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
2939 // Ensure data can be sent in both directions.
2940 std::string data = "hello world";
2941 SendRtpDataWithRetries(caller()->data_channel(), data, 5);
2942 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
2943 kDefaultTimeout);
2944 SendRtpDataWithRetries(callee()->data_channel(), data, 5);
2945 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
2946 kDefaultTimeout);
2947}
2948
2949#ifdef HAVE_SCTP
2950
2951// This test sets up a call between two parties with audio, video and an SCTP
2952// data channel.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002953TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithSctpDataChannel) {
deadbeef1dcb1642017-03-29 21:08:16 -07002954 ASSERT_TRUE(CreatePeerConnectionWrappers());
2955 ConnectFakeSignaling();
2956 // Expect that data channel created on caller side will show up for callee as
2957 // well.
2958 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08002959 caller()->AddAudioVideoTracks();
2960 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002961 caller()->CreateAndSetAndSignalOffer();
2962 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2963 // Ensure the existence of the SCTP data channel didn't impede audio/video.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002964 MediaExpectations media_expectations;
2965 media_expectations.ExpectBidirectionalAudioAndVideo();
2966 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002967 // Caller data channel should already exist (it created one). Callee data
2968 // channel may not exist yet, since negotiation happens in-band, not in SDP.
2969 ASSERT_NE(nullptr, caller()->data_channel());
2970 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
2971 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
2972 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
2973
2974 // Ensure data can be sent in both directions.
2975 std::string data = "hello world";
2976 caller()->data_channel()->Send(DataBuffer(data));
2977 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
2978 kDefaultTimeout);
2979 callee()->data_channel()->Send(DataBuffer(data));
2980 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
2981 kDefaultTimeout);
2982}
2983
2984// Ensure that when the callee closes an SCTP data channel, the closing
2985// procedure results in the data channel being closed for the caller as well.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002986TEST_P(PeerConnectionIntegrationTest, CalleeClosesSctpDataChannel) {
deadbeef1dcb1642017-03-29 21:08:16 -07002987 // Same procedure as above test.
2988 ASSERT_TRUE(CreatePeerConnectionWrappers());
2989 ConnectFakeSignaling();
2990 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08002991 caller()->AddAudioVideoTracks();
2992 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002993 caller()->CreateAndSetAndSignalOffer();
2994 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2995 ASSERT_NE(nullptr, caller()->data_channel());
2996 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
2997 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
2998 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
2999
3000 // Close the data channel on the callee side, and wait for it to reach the
3001 // "closed" state on both sides.
3002 callee()->data_channel()->Close();
3003 EXPECT_TRUE_WAIT(!caller()->data_observer()->IsOpen(), kDefaultTimeout);
3004 EXPECT_TRUE_WAIT(!callee()->data_observer()->IsOpen(), kDefaultTimeout);
3005}
3006
Seth Hampson2f0d7022018-02-20 11:54:42 -08003007TEST_P(PeerConnectionIntegrationTest, SctpDataChannelConfigSentToOtherSide) {
Steve Antonda6c0952017-10-23 11:41:54 -07003008 ASSERT_TRUE(CreatePeerConnectionWrappers());
3009 ConnectFakeSignaling();
3010 webrtc::DataChannelInit init;
3011 init.id = 53;
3012 init.maxRetransmits = 52;
3013 caller()->CreateDataChannel("data-channel", &init);
Steve Anton15324772018-01-16 10:26:49 -08003014 caller()->AddAudioVideoTracks();
3015 callee()->AddAudioVideoTracks();
Steve Antonda6c0952017-10-23 11:41:54 -07003016 caller()->CreateAndSetAndSignalOffer();
3017 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Steve Anton074dece2017-10-24 13:04:12 -07003018 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3019 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
Steve Antonda6c0952017-10-23 11:41:54 -07003020 EXPECT_EQ(init.id, callee()->data_channel()->id());
3021 EXPECT_EQ("data-channel", callee()->data_channel()->label());
3022 EXPECT_EQ(init.maxRetransmits, callee()->data_channel()->maxRetransmits());
3023 EXPECT_FALSE(callee()->data_channel()->negotiated());
3024}
3025
deadbeef1dcb1642017-03-29 21:08:16 -07003026// Test usrsctp's ability to process unordered data stream, where data actually
3027// arrives out of order using simulated delays. Previously there have been some
3028// bugs in this area.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003029TEST_P(PeerConnectionIntegrationTest, StressTestUnorderedSctpDataChannel) {
deadbeef1dcb1642017-03-29 21:08:16 -07003030 // Introduce random network delays.
3031 // Otherwise it's not a true "unordered" test.
3032 virtual_socket_server()->set_delay_mean(20);
3033 virtual_socket_server()->set_delay_stddev(5);
3034 virtual_socket_server()->UpdateDelayDistribution();
3035 // Normal procedure, but with unordered data channel config.
3036 ASSERT_TRUE(CreatePeerConnectionWrappers());
3037 ConnectFakeSignaling();
3038 webrtc::DataChannelInit init;
3039 init.ordered = false;
3040 caller()->CreateDataChannel(&init);
3041 caller()->CreateAndSetAndSignalOffer();
3042 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3043 ASSERT_NE(nullptr, caller()->data_channel());
3044 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3045 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3046 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3047
3048 static constexpr int kNumMessages = 100;
3049 // Deliberately chosen to be larger than the MTU so messages get fragmented.
3050 static constexpr size_t kMaxMessageSize = 4096;
3051 // Create and send random messages.
3052 std::vector<std::string> sent_messages;
3053 for (int i = 0; i < kNumMessages; ++i) {
3054 size_t length =
3055 (rand() % kMaxMessageSize) + 1; // NOLINT (rand_r instead of rand)
3056 std::string message;
3057 ASSERT_TRUE(rtc::CreateRandomString(length, &message));
3058 caller()->data_channel()->Send(DataBuffer(message));
3059 callee()->data_channel()->Send(DataBuffer(message));
3060 sent_messages.push_back(message);
3061 }
3062
3063 // Wait for all messages to be received.
3064 EXPECT_EQ_WAIT(kNumMessages,
3065 caller()->data_observer()->received_message_count(),
3066 kDefaultTimeout);
3067 EXPECT_EQ_WAIT(kNumMessages,
3068 callee()->data_observer()->received_message_count(),
3069 kDefaultTimeout);
3070
3071 // Sort and compare to make sure none of the messages were corrupted.
3072 std::vector<std::string> caller_received_messages =
3073 caller()->data_observer()->messages();
3074 std::vector<std::string> callee_received_messages =
3075 callee()->data_observer()->messages();
3076 std::sort(sent_messages.begin(), sent_messages.end());
3077 std::sort(caller_received_messages.begin(), caller_received_messages.end());
3078 std::sort(callee_received_messages.begin(), callee_received_messages.end());
3079 EXPECT_EQ(sent_messages, caller_received_messages);
3080 EXPECT_EQ(sent_messages, callee_received_messages);
3081}
3082
3083// This test sets up a call between two parties with audio, and video. When
3084// audio and video are setup and flowing, an SCTP data channel is negotiated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003085TEST_P(PeerConnectionIntegrationTest, AddSctpDataChannelInSubsequentOffer) {
deadbeef1dcb1642017-03-29 21:08:16 -07003086 ASSERT_TRUE(CreatePeerConnectionWrappers());
3087 ConnectFakeSignaling();
3088 // Do initial offer/answer with audio/video.
Steve Anton15324772018-01-16 10:26:49 -08003089 caller()->AddAudioVideoTracks();
3090 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003091 caller()->CreateAndSetAndSignalOffer();
3092 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3093 // Create data channel and do new offer and answer.
3094 caller()->CreateDataChannel();
3095 caller()->CreateAndSetAndSignalOffer();
3096 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3097 // Caller data channel should already exist (it created one). Callee data
3098 // channel may not exist yet, since negotiation happens in-band, not in SDP.
3099 ASSERT_NE(nullptr, caller()->data_channel());
3100 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3101 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3102 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3103 // Ensure data can be sent in both directions.
3104 std::string data = "hello world";
3105 caller()->data_channel()->Send(DataBuffer(data));
3106 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3107 kDefaultTimeout);
3108 callee()->data_channel()->Send(DataBuffer(data));
3109 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3110 kDefaultTimeout);
3111}
3112
deadbeef7914b8c2017-04-21 03:23:33 -07003113// Set up a connection initially just using SCTP data channels, later upgrading
3114// to audio/video, ensuring frames are received end-to-end. Effectively the
3115// inverse of the test above.
3116// This was broken in M57; see https://crbug.com/711243
Seth Hampson2f0d7022018-02-20 11:54:42 -08003117TEST_P(PeerConnectionIntegrationTest, SctpDataChannelToAudioVideoUpgrade) {
deadbeef7914b8c2017-04-21 03:23:33 -07003118 ASSERT_TRUE(CreatePeerConnectionWrappers());
3119 ConnectFakeSignaling();
3120 // Do initial offer/answer with just data channel.
3121 caller()->CreateDataChannel();
3122 caller()->CreateAndSetAndSignalOffer();
3123 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3124 // Wait until data can be sent over the data channel.
3125 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3126 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3127 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3128
3129 // Do subsequent offer/answer with two-way audio and video. Audio and video
3130 // should end up bundled on the DTLS/ICE transport already used for data.
Steve Anton15324772018-01-16 10:26:49 -08003131 caller()->AddAudioVideoTracks();
3132 callee()->AddAudioVideoTracks();
deadbeef7914b8c2017-04-21 03:23:33 -07003133 caller()->CreateAndSetAndSignalOffer();
3134 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08003135 MediaExpectations media_expectations;
3136 media_expectations.ExpectBidirectionalAudioAndVideo();
3137 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef7914b8c2017-04-21 03:23:33 -07003138}
3139
deadbeef8b7e9ad2017-05-25 09:38:55 -07003140static void MakeSpecCompliantSctpOffer(cricket::SessionDescription* desc) {
deadbeef8b7e9ad2017-05-25 09:38:55 -07003141 cricket::DataContentDescription* dcd_offer =
Steve Antonb1c1de12017-12-21 15:14:30 -08003142 GetFirstDataContentDescription(desc);
3143 ASSERT_TRUE(dcd_offer);
deadbeef8b7e9ad2017-05-25 09:38:55 -07003144 dcd_offer->set_use_sctpmap(false);
3145 dcd_offer->set_protocol("UDP/DTLS/SCTP");
3146}
3147
3148// Test that the data channel works when a spec-compliant SCTP m= section is
3149// offered (using "a=sctp-port" instead of "a=sctpmap", and using
3150// "UDP/DTLS/SCTP" as the protocol).
Seth Hampson2f0d7022018-02-20 11:54:42 -08003151TEST_P(PeerConnectionIntegrationTest,
deadbeef8b7e9ad2017-05-25 09:38:55 -07003152 DataChannelWorksWhenSpecCompliantSctpOfferReceived) {
3153 ASSERT_TRUE(CreatePeerConnectionWrappers());
3154 ConnectFakeSignaling();
3155 caller()->CreateDataChannel();
3156 caller()->SetGeneratedSdpMunger(MakeSpecCompliantSctpOffer);
3157 caller()->CreateAndSetAndSignalOffer();
3158 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3159 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3160 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3161 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3162
3163 // Ensure data can be sent in both directions.
3164 std::string data = "hello world";
3165 caller()->data_channel()->Send(DataBuffer(data));
3166 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3167 kDefaultTimeout);
3168 callee()->data_channel()->Send(DataBuffer(data));
3169 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3170 kDefaultTimeout);
3171}
3172
deadbeef1dcb1642017-03-29 21:08:16 -07003173#endif // HAVE_SCTP
3174
3175// Test that the ICE connection and gathering states eventually reach
3176// "complete".
Seth Hampson2f0d7022018-02-20 11:54:42 -08003177TEST_P(PeerConnectionIntegrationTest, IceStatesReachCompletion) {
deadbeef1dcb1642017-03-29 21:08:16 -07003178 ASSERT_TRUE(CreatePeerConnectionWrappers());
3179 ConnectFakeSignaling();
3180 // Do normal offer/answer.
Steve Anton15324772018-01-16 10:26:49 -08003181 caller()->AddAudioVideoTracks();
3182 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003183 caller()->CreateAndSetAndSignalOffer();
3184 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3185 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceGatheringComplete,
3186 caller()->ice_gathering_state(), kMaxWaitForFramesMs);
3187 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceGatheringComplete,
3188 callee()->ice_gathering_state(), kMaxWaitForFramesMs);
3189 // After the best candidate pair is selected and all candidates are signaled,
3190 // the ICE connection state should reach "complete".
3191 // TODO(deadbeef): Currently, the ICE "controlled" agent (the
3192 // answerer/"callee" by default) only reaches "connected". When this is
3193 // fixed, this test should be updated.
3194 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
3195 caller()->ice_connection_state(), kDefaultTimeout);
3196 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
3197 callee()->ice_connection_state(), kDefaultTimeout);
3198}
3199
Steve Antonede9ca52017-10-16 13:04:27 -07003200// Test that firewalling the ICE connection causes the clients to identify the
3201// disconnected state and then removing the firewall causes them to reconnect.
3202class PeerConnectionIntegrationIceStatesTest
Seth Hampson2f0d7022018-02-20 11:54:42 -08003203 : public PeerConnectionIntegrationBaseTest,
3204 public ::testing::WithParamInterface<
3205 std::tuple<SdpSemantics, std::tuple<std::string, uint32_t>>> {
Steve Antonede9ca52017-10-16 13:04:27 -07003206 protected:
Seth Hampson2f0d7022018-02-20 11:54:42 -08003207 PeerConnectionIntegrationIceStatesTest()
3208 : PeerConnectionIntegrationBaseTest(std::get<0>(GetParam())) {
3209 port_allocator_flags_ = std::get<1>(std::get<1>(GetParam()));
Steve Antonede9ca52017-10-16 13:04:27 -07003210 }
3211
3212 void StartStunServer(const SocketAddress& server_address) {
3213 stun_server_.reset(
3214 cricket::TestStunServer::Create(network_thread(), server_address));
3215 }
3216
3217 bool TestIPv6() {
3218 return (port_allocator_flags_ & cricket::PORTALLOCATOR_ENABLE_IPV6);
3219 }
3220
3221 void SetPortAllocatorFlags() {
Patrik Höglund3dc41062018-04-11 11:13:57 +00003222 caller()->port_allocator()->set_flags(port_allocator_flags_);
3223 callee()->port_allocator()->set_flags(port_allocator_flags_);
Steve Antonede9ca52017-10-16 13:04:27 -07003224 }
3225
3226 std::vector<SocketAddress> CallerAddresses() {
3227 std::vector<SocketAddress> addresses;
3228 addresses.push_back(SocketAddress("1.1.1.1", 0));
3229 if (TestIPv6()) {
3230 addresses.push_back(SocketAddress("1111:0:a:b:c:d:e:f", 0));
3231 }
3232 return addresses;
3233 }
3234
3235 std::vector<SocketAddress> CalleeAddresses() {
3236 std::vector<SocketAddress> addresses;
3237 addresses.push_back(SocketAddress("2.2.2.2", 0));
3238 if (TestIPv6()) {
3239 addresses.push_back(SocketAddress("2222:0:a:b:c:d:e:f", 0));
3240 }
3241 return addresses;
3242 }
3243
3244 void SetUpNetworkInterfaces() {
3245 // Remove the default interfaces added by the test infrastructure.
3246 caller()->network()->RemoveInterface(kDefaultLocalAddress);
3247 callee()->network()->RemoveInterface(kDefaultLocalAddress);
3248
3249 // Add network addresses for test.
3250 for (const auto& caller_address : CallerAddresses()) {
3251 caller()->network()->AddInterface(caller_address);
3252 }
3253 for (const auto& callee_address : CalleeAddresses()) {
3254 callee()->network()->AddInterface(callee_address);
3255 }
3256 }
3257
3258 private:
3259 uint32_t port_allocator_flags_;
3260 std::unique_ptr<cricket::TestStunServer> stun_server_;
3261};
3262
3263// Tests that the PeerConnection goes through all the ICE gathering/connection
3264// states over the duration of the call. This includes Disconnected and Failed
3265// states, induced by putting a firewall between the peers and waiting for them
3266// to time out.
Steve Anton83119dd2017-11-10 16:19:52 -08003267TEST_P(PeerConnectionIntegrationIceStatesTest, VerifyIceStates) {
3268 // TODO(bugs.webrtc.org/8295): When using a ScopedFakeClock, this test will
3269 // sometimes hit a DCHECK in platform_thread.cc about the PacerThread being
3270 // too busy. For now, revert to running without a fake clock.
Steve Antonede9ca52017-10-16 13:04:27 -07003271
3272 const SocketAddress kStunServerAddress =
3273 SocketAddress("99.99.99.1", cricket::STUN_SERVER_PORT);
3274 StartStunServer(kStunServerAddress);
3275
3276 PeerConnectionInterface::RTCConfiguration config;
3277 PeerConnectionInterface::IceServer ice_stun_server;
3278 ice_stun_server.urls.push_back(
3279 "stun:" + kStunServerAddress.HostAsURIString() + ":" +
3280 kStunServerAddress.PortAsString());
3281 config.servers.push_back(ice_stun_server);
3282
3283 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
3284 ConnectFakeSignaling();
3285 SetPortAllocatorFlags();
3286 SetUpNetworkInterfaces();
Steve Anton15324772018-01-16 10:26:49 -08003287 caller()->AddAudioVideoTracks();
3288 callee()->AddAudioVideoTracks();
Steve Antonede9ca52017-10-16 13:04:27 -07003289
3290 // Initial state before anything happens.
3291 ASSERT_EQ(PeerConnectionInterface::kIceGatheringNew,
3292 caller()->ice_gathering_state());
3293 ASSERT_EQ(PeerConnectionInterface::kIceConnectionNew,
3294 caller()->ice_connection_state());
3295
3296 // Start the call by creating the offer, setting it as the local description,
3297 // then sending it to the peer who will respond with an answer. This happens
3298 // asynchronously so that we can watch the states as it runs in the
3299 // background.
3300 caller()->CreateAndSetAndSignalOffer();
3301
Steve Anton83119dd2017-11-10 16:19:52 -08003302 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceConnectionCompleted,
3303 caller()->ice_connection_state(), kDefaultTimeout);
Steve Antonede9ca52017-10-16 13:04:27 -07003304
3305 // Verify that the observer was notified of the intermediate transitions.
3306 EXPECT_THAT(caller()->ice_connection_state_history(),
3307 ElementsAre(PeerConnectionInterface::kIceConnectionChecking,
3308 PeerConnectionInterface::kIceConnectionConnected,
3309 PeerConnectionInterface::kIceConnectionCompleted));
3310 EXPECT_THAT(caller()->ice_gathering_state_history(),
3311 ElementsAre(PeerConnectionInterface::kIceGatheringGathering,
3312 PeerConnectionInterface::kIceGatheringComplete));
3313
3314 // Block connections to/from the caller and wait for ICE to become
3315 // disconnected.
3316 for (const auto& caller_address : CallerAddresses()) {
3317 firewall()->AddRule(false, rtc::FP_ANY, rtc::FD_ANY, caller_address);
3318 }
Mirko Bonadei675513b2017-11-09 11:09:25 +01003319 RTC_LOG(LS_INFO) << "Firewall rules applied";
Steve Anton83119dd2017-11-10 16:19:52 -08003320 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceConnectionDisconnected,
3321 caller()->ice_connection_state(), kDefaultTimeout);
Steve Antonede9ca52017-10-16 13:04:27 -07003322
3323 // Let ICE re-establish by removing the firewall rules.
3324 firewall()->ClearRules();
Mirko Bonadei675513b2017-11-09 11:09:25 +01003325 RTC_LOG(LS_INFO) << "Firewall rules cleared";
Steve Anton83119dd2017-11-10 16:19:52 -08003326 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceConnectionCompleted,
3327 caller()->ice_connection_state(), kDefaultTimeout);
Steve Antonede9ca52017-10-16 13:04:27 -07003328
3329 // According to RFC7675, if there is no response within 30 seconds then the
3330 // peer should consider the other side to have rejected the connection. This
Steve Anton83119dd2017-11-10 16:19:52 -08003331 // is signaled by the state transitioning to "failed".
Steve Antonede9ca52017-10-16 13:04:27 -07003332 constexpr int kConsentTimeout = 30000;
3333 for (const auto& caller_address : CallerAddresses()) {
3334 firewall()->AddRule(false, rtc::FP_ANY, rtc::FD_ANY, caller_address);
3335 }
Mirko Bonadei675513b2017-11-09 11:09:25 +01003336 RTC_LOG(LS_INFO) << "Firewall rules applied again";
Steve Anton83119dd2017-11-10 16:19:52 -08003337 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceConnectionFailed,
3338 caller()->ice_connection_state(), kConsentTimeout);
Steve Antonede9ca52017-10-16 13:04:27 -07003339}
3340
3341// Tests that the best connection is set to the appropriate IPv4/IPv6 connection
3342// and that the statistics in the metric observers are updated correctly.
3343TEST_P(PeerConnectionIntegrationIceStatesTest, VerifyBestConnection) {
3344 ASSERT_TRUE(CreatePeerConnectionWrappers());
3345 ConnectFakeSignaling();
3346 SetPortAllocatorFlags();
3347 SetUpNetworkInterfaces();
Steve Anton15324772018-01-16 10:26:49 -08003348 caller()->AddAudioVideoTracks();
3349 callee()->AddAudioVideoTracks();
Steve Antonede9ca52017-10-16 13:04:27 -07003350
3351 rtc::scoped_refptr<webrtc::FakeMetricsObserver> metrics_observer(
3352 new rtc::RefCountedObject<webrtc::FakeMetricsObserver>());
3353 caller()->pc()->RegisterUMAObserver(metrics_observer.get());
3354
3355 caller()->CreateAndSetAndSignalOffer();
3356
3357 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3358
3359 const int num_best_ipv4 = metrics_observer->GetEnumCounter(
3360 webrtc::kEnumCounterAddressFamily, webrtc::kBestConnections_IPv4);
3361 const int num_best_ipv6 = metrics_observer->GetEnumCounter(
3362 webrtc::kEnumCounterAddressFamily, webrtc::kBestConnections_IPv6);
3363 if (TestIPv6()) {
3364 // When IPv6 is enabled, we should prefer an IPv6 connection over an IPv4
3365 // connection.
3366 EXPECT_EQ(0u, num_best_ipv4);
3367 EXPECT_EQ(1u, num_best_ipv6);
3368 } else {
3369 EXPECT_EQ(1u, num_best_ipv4);
3370 EXPECT_EQ(0u, num_best_ipv6);
3371 }
3372
3373 EXPECT_EQ(0u, metrics_observer->GetEnumCounter(
3374 webrtc::kEnumCounterIceCandidatePairTypeUdp,
3375 webrtc::kIceCandidatePairHostHost));
3376 EXPECT_EQ(1u, metrics_observer->GetEnumCounter(
3377 webrtc::kEnumCounterIceCandidatePairTypeUdp,
3378 webrtc::kIceCandidatePairHostPublicHostPublic));
3379}
3380
3381constexpr uint32_t kFlagsIPv4NoStun = cricket::PORTALLOCATOR_DISABLE_TCP |
3382 cricket::PORTALLOCATOR_DISABLE_STUN |
3383 cricket::PORTALLOCATOR_DISABLE_RELAY;
3384constexpr uint32_t kFlagsIPv6NoStun =
3385 cricket::PORTALLOCATOR_DISABLE_TCP | cricket::PORTALLOCATOR_DISABLE_STUN |
3386 cricket::PORTALLOCATOR_ENABLE_IPV6 | cricket::PORTALLOCATOR_DISABLE_RELAY;
3387constexpr uint32_t kFlagsIPv4Stun =
3388 cricket::PORTALLOCATOR_DISABLE_TCP | cricket::PORTALLOCATOR_DISABLE_RELAY;
3389
Seth Hampson2f0d7022018-02-20 11:54:42 -08003390INSTANTIATE_TEST_CASE_P(
3391 PeerConnectionIntegrationTest,
3392 PeerConnectionIntegrationIceStatesTest,
3393 Combine(Values(SdpSemantics::kPlanB, SdpSemantics::kUnifiedPlan),
3394 Values(std::make_pair("IPv4 no STUN", kFlagsIPv4NoStun),
3395 std::make_pair("IPv6 no STUN", kFlagsIPv6NoStun),
3396 std::make_pair("IPv4 with STUN", kFlagsIPv4Stun))));
Steve Antonede9ca52017-10-16 13:04:27 -07003397
deadbeef1dcb1642017-03-29 21:08:16 -07003398// This test sets up a call between two parties with audio and video.
3399// During the call, the caller restarts ICE and the test verifies that
3400// new ICE candidates are generated and audio and video still can flow, and the
3401// ICE state reaches completed again.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003402TEST_P(PeerConnectionIntegrationTest, MediaContinuesFlowingAfterIceRestart) {
deadbeef1dcb1642017-03-29 21:08:16 -07003403 ASSERT_TRUE(CreatePeerConnectionWrappers());
3404 ConnectFakeSignaling();
3405 // Do normal offer/answer and wait for ICE to complete.
Steve Anton15324772018-01-16 10:26:49 -08003406 caller()->AddAudioVideoTracks();
3407 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003408 caller()->CreateAndSetAndSignalOffer();
3409 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3410 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
3411 caller()->ice_connection_state(), kMaxWaitForFramesMs);
3412 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
3413 callee()->ice_connection_state(), kMaxWaitForFramesMs);
3414
3415 // To verify that the ICE restart actually occurs, get
3416 // ufrag/password/candidates before and after restart.
3417 // Create an SDP string of the first audio candidate for both clients.
3418 const webrtc::IceCandidateCollection* audio_candidates_caller =
3419 caller()->pc()->local_description()->candidates(0);
3420 const webrtc::IceCandidateCollection* audio_candidates_callee =
3421 callee()->pc()->local_description()->candidates(0);
3422 ASSERT_GT(audio_candidates_caller->count(), 0u);
3423 ASSERT_GT(audio_candidates_callee->count(), 0u);
3424 std::string caller_candidate_pre_restart;
3425 ASSERT_TRUE(
3426 audio_candidates_caller->at(0)->ToString(&caller_candidate_pre_restart));
3427 std::string callee_candidate_pre_restart;
3428 ASSERT_TRUE(
3429 audio_candidates_callee->at(0)->ToString(&callee_candidate_pre_restart));
3430 const cricket::SessionDescription* desc =
3431 caller()->pc()->local_description()->description();
3432 std::string caller_ufrag_pre_restart =
3433 desc->transport_infos()[0].description.ice_ufrag;
3434 desc = callee()->pc()->local_description()->description();
3435 std::string callee_ufrag_pre_restart =
3436 desc->transport_infos()[0].description.ice_ufrag;
3437
3438 // Have the caller initiate an ICE restart.
3439 caller()->SetOfferAnswerOptions(IceRestartOfferAnswerOptions());
3440 caller()->CreateAndSetAndSignalOffer();
3441 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3442 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
3443 caller()->ice_connection_state(), kMaxWaitForFramesMs);
3444 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
3445 callee()->ice_connection_state(), kMaxWaitForFramesMs);
3446
3447 // Grab the ufrags/candidates again.
3448 audio_candidates_caller = caller()->pc()->local_description()->candidates(0);
3449 audio_candidates_callee = callee()->pc()->local_description()->candidates(0);
3450 ASSERT_GT(audio_candidates_caller->count(), 0u);
3451 ASSERT_GT(audio_candidates_callee->count(), 0u);
3452 std::string caller_candidate_post_restart;
3453 ASSERT_TRUE(
3454 audio_candidates_caller->at(0)->ToString(&caller_candidate_post_restart));
3455 std::string callee_candidate_post_restart;
3456 ASSERT_TRUE(
3457 audio_candidates_callee->at(0)->ToString(&callee_candidate_post_restart));
3458 desc = caller()->pc()->local_description()->description();
3459 std::string caller_ufrag_post_restart =
3460 desc->transport_infos()[0].description.ice_ufrag;
3461 desc = callee()->pc()->local_description()->description();
3462 std::string callee_ufrag_post_restart =
3463 desc->transport_infos()[0].description.ice_ufrag;
3464 // Sanity check that an ICE restart was actually negotiated in SDP.
3465 ASSERT_NE(caller_candidate_pre_restart, caller_candidate_post_restart);
3466 ASSERT_NE(callee_candidate_pre_restart, callee_candidate_post_restart);
3467 ASSERT_NE(caller_ufrag_pre_restart, caller_ufrag_post_restart);
3468 ASSERT_NE(callee_ufrag_pre_restart, callee_ufrag_post_restart);
3469
3470 // Ensure that additional frames are received after the ICE restart.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003471 MediaExpectations media_expectations;
3472 media_expectations.ExpectBidirectionalAudioAndVideo();
3473 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003474}
3475
3476// Verify that audio/video can be received end-to-end when ICE renomination is
3477// enabled.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003478TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithIceRenomination) {
deadbeef1dcb1642017-03-29 21:08:16 -07003479 PeerConnectionInterface::RTCConfiguration config;
3480 config.enable_ice_renomination = true;
3481 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
3482 ConnectFakeSignaling();
3483 // Do normal offer/answer and wait for some frames to be received in each
3484 // direction.
Steve Anton15324772018-01-16 10:26:49 -08003485 caller()->AddAudioVideoTracks();
3486 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003487 caller()->CreateAndSetAndSignalOffer();
3488 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3489 // Sanity check that ICE renomination was actually negotiated.
3490 const cricket::SessionDescription* desc =
3491 caller()->pc()->local_description()->description();
3492 for (const cricket::TransportInfo& info : desc->transport_infos()) {
deadbeef30952b42017-04-21 02:41:29 -07003493 ASSERT_NE(
3494 info.description.transport_options.end(),
3495 std::find(info.description.transport_options.begin(),
3496 info.description.transport_options.end(), "renomination"));
deadbeef1dcb1642017-03-29 21:08:16 -07003497 }
3498 desc = callee()->pc()->local_description()->description();
3499 for (const cricket::TransportInfo& info : desc->transport_infos()) {
deadbeef30952b42017-04-21 02:41:29 -07003500 ASSERT_NE(
3501 info.description.transport_options.end(),
3502 std::find(info.description.transport_options.begin(),
3503 info.description.transport_options.end(), "renomination"));
deadbeef1dcb1642017-03-29 21:08:16 -07003504 }
Seth Hampson2f0d7022018-02-20 11:54:42 -08003505 MediaExpectations media_expectations;
3506 media_expectations.ExpectBidirectionalAudioAndVideo();
3507 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003508}
3509
Steve Anton6f25b092017-10-23 09:39:20 -07003510// With a max bundle policy and RTCP muxing, adding a new media description to
3511// the connection should not affect ICE at all because the new media will use
3512// the existing connection.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003513TEST_P(PeerConnectionIntegrationTest,
Steve Anton83119dd2017-11-10 16:19:52 -08003514 AddMediaToConnectedBundleDoesNotRestartIce) {
Steve Anton6f25b092017-10-23 09:39:20 -07003515 PeerConnectionInterface::RTCConfiguration config;
3516 config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
3517 config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyRequire;
3518 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(
3519 config, PeerConnectionInterface::RTCConfiguration()));
3520 ConnectFakeSignaling();
3521
Steve Anton15324772018-01-16 10:26:49 -08003522 caller()->AddAudioTrack();
Steve Anton6f25b092017-10-23 09:39:20 -07003523 caller()->CreateAndSetAndSignalOffer();
3524 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Steve Antonff52f1b2017-10-26 12:24:50 -07003525 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceConnectionCompleted,
3526 caller()->ice_connection_state(), kDefaultTimeout);
Steve Anton6f25b092017-10-23 09:39:20 -07003527
3528 caller()->clear_ice_connection_state_history();
3529
Steve Anton15324772018-01-16 10:26:49 -08003530 caller()->AddVideoTrack();
Steve Anton6f25b092017-10-23 09:39:20 -07003531 caller()->CreateAndSetAndSignalOffer();
3532 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3533
3534 EXPECT_EQ(0u, caller()->ice_connection_state_history().size());
3535}
3536
deadbeef1dcb1642017-03-29 21:08:16 -07003537// This test sets up a call between two parties with audio and video. It then
3538// renegotiates setting the video m-line to "port 0", then later renegotiates
3539// again, enabling video.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003540TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07003541 VideoFlowsAfterMediaSectionIsRejectedAndRecycled) {
3542 ASSERT_TRUE(CreatePeerConnectionWrappers());
3543 ConnectFakeSignaling();
3544
3545 // Do initial negotiation, only sending media from the caller. Will result in
3546 // video and audio recvonly "m=" sections.
Steve Anton15324772018-01-16 10:26:49 -08003547 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003548 caller()->CreateAndSetAndSignalOffer();
3549 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3550
3551 // Negotiate again, disabling the video "m=" section (the callee will set the
3552 // port to 0 due to offer_to_receive_video = 0).
Seth Hampson2f0d7022018-02-20 11:54:42 -08003553 if (sdp_semantics_ == SdpSemantics::kPlanB) {
3554 PeerConnectionInterface::RTCOfferAnswerOptions options;
3555 options.offer_to_receive_video = 0;
3556 callee()->SetOfferAnswerOptions(options);
3557 } else {
3558 callee()->SetRemoteOfferHandler([this] {
3559 callee()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)->Stop();
3560 });
3561 }
deadbeef1dcb1642017-03-29 21:08:16 -07003562 caller()->CreateAndSetAndSignalOffer();
3563 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3564 // Sanity check that video "m=" section was actually rejected.
3565 const ContentInfo* answer_video_content = cricket::GetFirstVideoContent(
3566 callee()->pc()->local_description()->description());
3567 ASSERT_NE(nullptr, answer_video_content);
3568 ASSERT_TRUE(answer_video_content->rejected);
3569
3570 // Enable video and do negotiation again, making sure video is received
3571 // end-to-end, also adding media stream to callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003572 if (sdp_semantics_ == SdpSemantics::kPlanB) {
3573 PeerConnectionInterface::RTCOfferAnswerOptions options;
3574 options.offer_to_receive_video = 1;
3575 callee()->SetOfferAnswerOptions(options);
3576 } else {
3577 // The caller's transceiver is stopped, so we need to add another track.
3578 auto caller_transceiver =
3579 caller()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO);
3580 EXPECT_TRUE(caller_transceiver->stopped());
3581 caller()->AddVideoTrack();
3582 }
3583 callee()->AddVideoTrack();
3584 callee()->SetRemoteOfferHandler(nullptr);
deadbeef1dcb1642017-03-29 21:08:16 -07003585 caller()->CreateAndSetAndSignalOffer();
3586 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08003587
deadbeef1dcb1642017-03-29 21:08:16 -07003588 // Verify the caller receives frames from the newly added stream, and the
3589 // callee receives additional frames from the re-enabled video m= section.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003590 MediaExpectations media_expectations;
3591 media_expectations.CalleeExpectsSomeAudio();
3592 media_expectations.ExpectBidirectionalVideo();
3593 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003594}
3595
3596// This test sets up a Jsep call between two parties with external
3597// VideoDecoderFactory.
3598// TODO(holmer): Disabled due to sometimes crashing on buildbots.
3599// See issue webrtc/2378.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003600TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07003601 DISABLED_EndToEndCallWithVideoDecoderFactory) {
3602 ASSERT_TRUE(CreatePeerConnectionWrappers());
3603 EnableVideoDecoderFactory();
3604 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08003605 caller()->AddAudioVideoTracks();
3606 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003607 caller()->CreateAndSetAndSignalOffer();
3608 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08003609 MediaExpectations media_expectations;
3610 media_expectations.ExpectBidirectionalAudioAndVideo();
3611 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003612}
3613
3614// This tests that if we negotiate after calling CreateSender but before we
3615// have a track, then set a track later, frames from the newly-set track are
3616// received end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003617TEST_F(PeerConnectionIntegrationTestPlanB,
deadbeef1dcb1642017-03-29 21:08:16 -07003618 MediaFlowsAfterEarlyWarmupWithCreateSender) {
3619 ASSERT_TRUE(CreatePeerConnectionWrappers());
3620 ConnectFakeSignaling();
3621 auto caller_audio_sender =
3622 caller()->pc()->CreateSender("audio", "caller_stream");
3623 auto caller_video_sender =
3624 caller()->pc()->CreateSender("video", "caller_stream");
3625 auto callee_audio_sender =
3626 callee()->pc()->CreateSender("audio", "callee_stream");
3627 auto callee_video_sender =
3628 callee()->pc()->CreateSender("video", "callee_stream");
3629 caller()->CreateAndSetAndSignalOffer();
3630 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
3631 // Wait for ICE to complete, without any tracks being set.
3632 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
3633 caller()->ice_connection_state(), kMaxWaitForFramesMs);
3634 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
3635 callee()->ice_connection_state(), kMaxWaitForFramesMs);
3636 // Now set the tracks, and expect frames to immediately start flowing.
3637 EXPECT_TRUE(caller_audio_sender->SetTrack(caller()->CreateLocalAudioTrack()));
3638 EXPECT_TRUE(caller_video_sender->SetTrack(caller()->CreateLocalVideoTrack()));
3639 EXPECT_TRUE(callee_audio_sender->SetTrack(callee()->CreateLocalAudioTrack()));
3640 EXPECT_TRUE(callee_video_sender->SetTrack(callee()->CreateLocalVideoTrack()));
Seth Hampson2f0d7022018-02-20 11:54:42 -08003641 MediaExpectations media_expectations;
3642 media_expectations.ExpectBidirectionalAudioAndVideo();
3643 ASSERT_TRUE(ExpectNewFrames(media_expectations));
3644}
3645
3646// This tests that if we negotiate after calling AddTransceiver but before we
3647// have a track, then set a track later, frames from the newly-set tracks are
3648// received end-to-end.
3649TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
3650 MediaFlowsAfterEarlyWarmupWithAddTransceiver) {
3651 ASSERT_TRUE(CreatePeerConnectionWrappers());
3652 ConnectFakeSignaling();
3653 auto audio_result = caller()->pc()->AddTransceiver(cricket::MEDIA_TYPE_AUDIO);
3654 ASSERT_EQ(RTCErrorType::NONE, audio_result.error().type());
3655 auto caller_audio_sender = audio_result.MoveValue()->sender();
3656 auto video_result = caller()->pc()->AddTransceiver(cricket::MEDIA_TYPE_VIDEO);
3657 ASSERT_EQ(RTCErrorType::NONE, video_result.error().type());
3658 auto caller_video_sender = video_result.MoveValue()->sender();
3659 callee()->SetRemoteOfferHandler([this] {
3660 ASSERT_EQ(2u, callee()->pc()->GetTransceivers().size());
3661 callee()->pc()->GetTransceivers()[0]->SetDirection(
3662 RtpTransceiverDirection::kSendRecv);
3663 callee()->pc()->GetTransceivers()[1]->SetDirection(
3664 RtpTransceiverDirection::kSendRecv);
3665 });
3666 caller()->CreateAndSetAndSignalOffer();
3667 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
3668 // Wait for ICE to complete, without any tracks being set.
3669 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
3670 caller()->ice_connection_state(), kMaxWaitForFramesMs);
3671 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
3672 callee()->ice_connection_state(), kMaxWaitForFramesMs);
3673 // Now set the tracks, and expect frames to immediately start flowing.
3674 auto callee_audio_sender = callee()->pc()->GetSenders()[0];
3675 auto callee_video_sender = callee()->pc()->GetSenders()[1];
3676 ASSERT_TRUE(caller_audio_sender->SetTrack(caller()->CreateLocalAudioTrack()));
3677 ASSERT_TRUE(caller_video_sender->SetTrack(caller()->CreateLocalVideoTrack()));
3678 ASSERT_TRUE(callee_audio_sender->SetTrack(callee()->CreateLocalAudioTrack()));
3679 ASSERT_TRUE(callee_video_sender->SetTrack(callee()->CreateLocalVideoTrack()));
3680 MediaExpectations media_expectations;
3681 media_expectations.ExpectBidirectionalAudioAndVideo();
3682 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003683}
3684
3685// This test verifies that a remote video track can be added via AddStream,
3686// and sent end-to-end. For this particular test, it's simply echoed back
3687// from the caller to the callee, rather than being forwarded to a third
3688// PeerConnection.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003689TEST_F(PeerConnectionIntegrationTestPlanB, CanSendRemoteVideoTrack) {
deadbeef1dcb1642017-03-29 21:08:16 -07003690 ASSERT_TRUE(CreatePeerConnectionWrappers());
3691 ConnectFakeSignaling();
3692 // Just send a video track from the caller.
Steve Anton15324772018-01-16 10:26:49 -08003693 caller()->AddVideoTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07003694 caller()->CreateAndSetAndSignalOffer();
3695 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
3696 ASSERT_EQ(1, callee()->remote_streams()->count());
3697
3698 // Echo the stream back, and do a new offer/anwer (initiated by callee this
3699 // time).
3700 callee()->pc()->AddStream(callee()->remote_streams()->at(0));
3701 callee()->CreateAndSetAndSignalOffer();
3702 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
3703
Seth Hampson2f0d7022018-02-20 11:54:42 -08003704 MediaExpectations media_expectations;
3705 media_expectations.ExpectBidirectionalVideo();
3706 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003707}
3708
3709// Test that we achieve the expected end-to-end connection time, using a
3710// fake clock and simulated latency on the media and signaling paths.
3711// We use a TURN<->TURN connection because this is usually the quickest to
3712// set up initially, especially when we're confident the connection will work
3713// and can start sending media before we get a STUN response.
3714//
3715// With various optimizations enabled, here are the network delays we expect to
3716// be on the critical path:
3717// 1. 2 signaling trips: Signaling offer and offerer's TURN candidate, then
3718// signaling answer (with DTLS fingerprint).
3719// 2. 9 media hops: Rest of the DTLS handshake. 3 hops in each direction when
3720// using TURN<->TURN pair, and DTLS exchange is 4 packets,
3721// the first of which should have arrived before the answer.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003722TEST_P(PeerConnectionIntegrationTest, EndToEndConnectionTimeWithTurnTurnPair) {
deadbeef1dcb1642017-03-29 21:08:16 -07003723 rtc::ScopedFakeClock fake_clock;
3724 // Some things use a time of "0" as a special value, so we need to start out
3725 // the fake clock at a nonzero time.
3726 // TODO(deadbeef): Fix this.
3727 fake_clock.AdvanceTime(rtc::TimeDelta::FromSeconds(1));
3728
3729 static constexpr int media_hop_delay_ms = 50;
3730 static constexpr int signaling_trip_delay_ms = 500;
3731 // For explanation of these values, see comment above.
3732 static constexpr int required_media_hops = 9;
3733 static constexpr int required_signaling_trips = 2;
3734 // For internal delays (such as posting an event asychronously).
3735 static constexpr int allowed_internal_delay_ms = 20;
3736 static constexpr int total_connection_time_ms =
3737 media_hop_delay_ms * required_media_hops +
3738 signaling_trip_delay_ms * required_signaling_trips +
3739 allowed_internal_delay_ms;
3740
3741 static const rtc::SocketAddress turn_server_1_internal_address{"88.88.88.0",
3742 3478};
3743 static const rtc::SocketAddress turn_server_1_external_address{"88.88.88.1",
3744 0};
3745 static const rtc::SocketAddress turn_server_2_internal_address{"99.99.99.0",
3746 3478};
3747 static const rtc::SocketAddress turn_server_2_external_address{"99.99.99.1",
3748 0};
3749 cricket::TestTurnServer turn_server_1(network_thread(),
3750 turn_server_1_internal_address,
3751 turn_server_1_external_address);
3752 cricket::TestTurnServer turn_server_2(network_thread(),
3753 turn_server_2_internal_address,
3754 turn_server_2_external_address);
Jonas Orelandbdcee282017-10-10 14:01:40 +02003755
deadbeef1dcb1642017-03-29 21:08:16 -07003756 // Bypass permission check on received packets so media can be sent before
3757 // the candidate is signaled.
3758 turn_server_1.set_enable_permission_checks(false);
3759 turn_server_2.set_enable_permission_checks(false);
3760
3761 PeerConnectionInterface::RTCConfiguration client_1_config;
3762 webrtc::PeerConnectionInterface::IceServer ice_server_1;
3763 ice_server_1.urls.push_back("turn:88.88.88.0:3478");
3764 ice_server_1.username = "test";
3765 ice_server_1.password = "test";
3766 client_1_config.servers.push_back(ice_server_1);
3767 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
3768 client_1_config.presume_writable_when_fully_relayed = true;
3769
3770 PeerConnectionInterface::RTCConfiguration client_2_config;
3771 webrtc::PeerConnectionInterface::IceServer ice_server_2;
3772 ice_server_2.urls.push_back("turn:99.99.99.0:3478");
3773 ice_server_2.username = "test";
3774 ice_server_2.password = "test";
3775 client_2_config.servers.push_back(ice_server_2);
3776 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
3777 client_2_config.presume_writable_when_fully_relayed = true;
3778
3779 ASSERT_TRUE(
3780 CreatePeerConnectionWrappersWithConfig(client_1_config, client_2_config));
3781 // Set up the simulated delays.
3782 SetSignalingDelayMs(signaling_trip_delay_ms);
3783 ConnectFakeSignaling();
3784 virtual_socket_server()->set_delay_mean(media_hop_delay_ms);
3785 virtual_socket_server()->UpdateDelayDistribution();
3786
3787 // Set "offer to receive audio/video" without adding any tracks, so we just
3788 // set up ICE/DTLS with no media.
3789 PeerConnectionInterface::RTCOfferAnswerOptions options;
3790 options.offer_to_receive_audio = 1;
3791 options.offer_to_receive_video = 1;
3792 caller()->SetOfferAnswerOptions(options);
3793 caller()->CreateAndSetAndSignalOffer();
deadbeef71452802017-05-07 17:21:01 -07003794 EXPECT_TRUE_SIMULATED_WAIT(DtlsConnected(), total_connection_time_ms,
3795 fake_clock);
deadbeef1dcb1642017-03-29 21:08:16 -07003796 // Need to free the clients here since they're using things we created on
3797 // the stack.
3798 delete SetCallerPcWrapperAndReturnCurrent(nullptr);
3799 delete SetCalleePcWrapperAndReturnCurrent(nullptr);
3800}
3801
Jonas Orelandbdcee282017-10-10 14:01:40 +02003802// Verify that a TurnCustomizer passed in through RTCConfiguration
3803// is actually used by the underlying TURN candidate pair.
3804// Note that turnport_unittest.cc contains more detailed, lower-level tests.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003805TEST_P(PeerConnectionIntegrationTest, TurnCustomizerUsedForTurnConnections) {
Jonas Orelandbdcee282017-10-10 14:01:40 +02003806 static const rtc::SocketAddress turn_server_1_internal_address{"88.88.88.0",
3807 3478};
3808 static const rtc::SocketAddress turn_server_1_external_address{"88.88.88.1",
3809 0};
3810 static const rtc::SocketAddress turn_server_2_internal_address{"99.99.99.0",
3811 3478};
3812 static const rtc::SocketAddress turn_server_2_external_address{"99.99.99.1",
3813 0};
3814 cricket::TestTurnServer turn_server_1(network_thread(),
3815 turn_server_1_internal_address,
3816 turn_server_1_external_address);
3817 cricket::TestTurnServer turn_server_2(network_thread(),
3818 turn_server_2_internal_address,
3819 turn_server_2_external_address);
3820
3821 PeerConnectionInterface::RTCConfiguration client_1_config;
3822 webrtc::PeerConnectionInterface::IceServer ice_server_1;
3823 ice_server_1.urls.push_back("turn:88.88.88.0:3478");
3824 ice_server_1.username = "test";
3825 ice_server_1.password = "test";
3826 client_1_config.servers.push_back(ice_server_1);
3827 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
3828 auto customizer1 = rtc::MakeUnique<cricket::TestTurnCustomizer>();
3829 client_1_config.turn_customizer = customizer1.get();
3830
3831 PeerConnectionInterface::RTCConfiguration client_2_config;
3832 webrtc::PeerConnectionInterface::IceServer ice_server_2;
3833 ice_server_2.urls.push_back("turn:99.99.99.0:3478");
3834 ice_server_2.username = "test";
3835 ice_server_2.password = "test";
3836 client_2_config.servers.push_back(ice_server_2);
3837 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
3838 auto customizer2 = rtc::MakeUnique<cricket::TestTurnCustomizer>();
3839 client_2_config.turn_customizer = customizer2.get();
3840
3841 ASSERT_TRUE(
3842 CreatePeerConnectionWrappersWithConfig(client_1_config, client_2_config));
3843 ConnectFakeSignaling();
3844
3845 // Set "offer to receive audio/video" without adding any tracks, so we just
3846 // set up ICE/DTLS with no media.
3847 PeerConnectionInterface::RTCOfferAnswerOptions options;
3848 options.offer_to_receive_audio = 1;
3849 options.offer_to_receive_video = 1;
3850 caller()->SetOfferAnswerOptions(options);
3851 caller()->CreateAndSetAndSignalOffer();
3852 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
3853
3854 EXPECT_GT(customizer1->allow_channel_data_cnt_, 0u);
3855 EXPECT_GT(customizer1->modify_cnt_, 0u);
3856
3857 EXPECT_GT(customizer2->allow_channel_data_cnt_, 0u);
3858 EXPECT_GT(customizer2->modify_cnt_, 0u);
3859
3860 // Need to free the clients here since they're using things we created on
3861 // the stack.
3862 delete SetCallerPcWrapperAndReturnCurrent(nullptr);
3863 delete SetCalleePcWrapperAndReturnCurrent(nullptr);
3864}
3865
deadbeefc964d0b2017-04-03 10:03:35 -07003866// Test that audio and video flow end-to-end when codec names don't use the
3867// expected casing, given that they're supposed to be case insensitive. To test
3868// this, all but one codec is removed from each media description, and its
3869// casing is changed.
3870//
3871// In the past, this has regressed and caused crashes/black video, due to the
3872// fact that code at some layers was doing case-insensitive comparisons and
3873// code at other layers was not.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003874TEST_P(PeerConnectionIntegrationTest, CodecNamesAreCaseInsensitive) {
deadbeefc964d0b2017-04-03 10:03:35 -07003875 ASSERT_TRUE(CreatePeerConnectionWrappers());
3876 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08003877 caller()->AddAudioVideoTracks();
3878 callee()->AddAudioVideoTracks();
deadbeefc964d0b2017-04-03 10:03:35 -07003879
3880 // Remove all but one audio/video codec (opus and VP8), and change the
3881 // casing of the caller's generated offer.
3882 caller()->SetGeneratedSdpMunger([](cricket::SessionDescription* description) {
3883 cricket::AudioContentDescription* audio =
3884 GetFirstAudioContentDescription(description);
3885 ASSERT_NE(nullptr, audio);
3886 auto audio_codecs = audio->codecs();
3887 audio_codecs.erase(std::remove_if(audio_codecs.begin(), audio_codecs.end(),
3888 [](const cricket::AudioCodec& codec) {
3889 return codec.name != "opus";
3890 }),
3891 audio_codecs.end());
3892 ASSERT_EQ(1u, audio_codecs.size());
3893 audio_codecs[0].name = "OpUs";
3894 audio->set_codecs(audio_codecs);
3895
3896 cricket::VideoContentDescription* video =
3897 GetFirstVideoContentDescription(description);
3898 ASSERT_NE(nullptr, video);
3899 auto video_codecs = video->codecs();
3900 video_codecs.erase(std::remove_if(video_codecs.begin(), video_codecs.end(),
3901 [](const cricket::VideoCodec& codec) {
3902 return codec.name != "VP8";
3903 }),
3904 video_codecs.end());
3905 ASSERT_EQ(1u, video_codecs.size());
3906 video_codecs[0].name = "vP8";
3907 video->set_codecs(video_codecs);
3908 });
3909
3910 caller()->CreateAndSetAndSignalOffer();
3911 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3912
3913 // Verify frames are still received end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003914 MediaExpectations media_expectations;
3915 media_expectations.ExpectBidirectionalAudioAndVideo();
3916 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeefc964d0b2017-04-03 10:03:35 -07003917}
3918
Seth Hampson2f0d7022018-02-20 11:54:42 -08003919TEST_P(PeerConnectionIntegrationTest, GetSources) {
hbos8d609f62017-04-10 07:39:05 -07003920 ASSERT_TRUE(CreatePeerConnectionWrappers());
3921 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08003922 caller()->AddAudioTrack();
hbos8d609f62017-04-10 07:39:05 -07003923 caller()->CreateAndSetAndSignalOffer();
3924 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
deadbeefd8ad7882017-04-18 16:01:17 -07003925 // Wait for one audio frame to be received by the callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003926 MediaExpectations media_expectations;
3927 media_expectations.CalleeExpectsSomeAudio(1);
3928 ASSERT_TRUE(ExpectNewFrames(media_expectations));
hbos8d609f62017-04-10 07:39:05 -07003929 ASSERT_GT(callee()->pc()->GetReceivers().size(), 0u);
3930 auto receiver = callee()->pc()->GetReceivers()[0];
3931 ASSERT_EQ(receiver->media_type(), cricket::MEDIA_TYPE_AUDIO);
3932
3933 auto contributing_sources = receiver->GetSources();
3934 ASSERT_GT(receiver->GetParameters().encodings.size(), 0u);
3935 EXPECT_EQ(receiver->GetParameters().encodings[0].ssrc,
3936 contributing_sources[0].source_id());
3937}
3938
deadbeef2f425aa2017-04-14 10:41:32 -07003939// Test that if a track is removed and added again with a different stream ID,
3940// the new stream ID is successfully communicated in SDP and media continues to
3941// flow end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003942// TODO(webrtc.bugs.org/8734): This test does not work for Unified Plan because
3943// it will not reuse a transceiver that has already been sending. After creating
3944// a new transceiver it tries to create an offer with two senders of the same
3945// track ids and it fails.
3946TEST_F(PeerConnectionIntegrationTestPlanB, RemoveAndAddTrackWithNewStreamId) {
deadbeef2f425aa2017-04-14 10:41:32 -07003947 ASSERT_TRUE(CreatePeerConnectionWrappers());
3948 ConnectFakeSignaling();
3949
3950 rtc::scoped_refptr<MediaStreamInterface> stream_1 =
3951 caller()->pc_factory()->CreateLocalMediaStream("stream_1");
3952 rtc::scoped_refptr<MediaStreamInterface> stream_2 =
3953 caller()->pc_factory()->CreateLocalMediaStream("stream_2");
3954
3955 // Add track using stream 1, do offer/answer.
3956 rtc::scoped_refptr<webrtc::AudioTrackInterface> track =
3957 caller()->CreateLocalAudioTrack();
3958 rtc::scoped_refptr<webrtc::RtpSenderInterface> sender =
3959 caller()->pc()->AddTrack(track, {stream_1.get()});
3960 caller()->CreateAndSetAndSignalOffer();
3961 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08003962 {
3963 MediaExpectations media_expectations;
3964 media_expectations.CalleeExpectsSomeAudio(1);
3965 ASSERT_TRUE(ExpectNewFrames(media_expectations));
3966 }
deadbeef2f425aa2017-04-14 10:41:32 -07003967 // Remove the sender, and create a new one with the new stream.
3968 caller()->pc()->RemoveTrack(sender);
3969 sender = caller()->pc()->AddTrack(track, {stream_2.get()});
3970 caller()->CreateAndSetAndSignalOffer();
3971 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3972 // Wait for additional audio frames to be received by the callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003973 {
3974 MediaExpectations media_expectations;
3975 media_expectations.CalleeExpectsSomeAudio();
3976 ASSERT_TRUE(ExpectNewFrames(media_expectations));
3977 }
deadbeef2f425aa2017-04-14 10:41:32 -07003978}
3979
Seth Hampson2f0d7022018-02-20 11:54:42 -08003980TEST_P(PeerConnectionIntegrationTest, RtcEventLogOutputWriteCalled) {
Elad Alon99c3fe52017-10-13 16:29:40 +02003981 ASSERT_TRUE(CreatePeerConnectionWrappers());
3982 ConnectFakeSignaling();
3983
3984 auto output = rtc::MakeUnique<testing::NiceMock<MockRtcEventLogOutput>>();
3985 ON_CALL(*output, IsActive()).WillByDefault(testing::Return(true));
3986 ON_CALL(*output, Write(::testing::_)).WillByDefault(testing::Return(true));
3987 EXPECT_CALL(*output, Write(::testing::_)).Times(::testing::AtLeast(1));
Bjorn Tereliusde939432017-11-20 17:38:14 +01003988 EXPECT_TRUE(caller()->pc()->StartRtcEventLog(
3989 std::move(output), webrtc::RtcEventLog::kImmediateOutput));
Elad Alon99c3fe52017-10-13 16:29:40 +02003990
Steve Anton15324772018-01-16 10:26:49 -08003991 caller()->AddAudioVideoTracks();
Elad Alon99c3fe52017-10-13 16:29:40 +02003992 caller()->CreateAndSetAndSignalOffer();
3993 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3994}
3995
Steve Antonede9ca52017-10-16 13:04:27 -07003996// Test that if candidates are only signaled by applying full session
3997// descriptions (instead of using AddIceCandidate), the peers can connect to
3998// each other and exchange media.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003999TEST_P(PeerConnectionIntegrationTest, MediaFlowsWhenCandidatesSetOnlyInSdp) {
Steve Antonede9ca52017-10-16 13:04:27 -07004000 ASSERT_TRUE(CreatePeerConnectionWrappers());
4001 // Each side will signal the session descriptions but not candidates.
4002 ConnectFakeSignalingForSdpOnly();
4003
4004 // Add audio video track and exchange the initial offer/answer with media
4005 // information only. This will start ICE gathering on each side.
Steve Anton15324772018-01-16 10:26:49 -08004006 caller()->AddAudioVideoTracks();
4007 callee()->AddAudioVideoTracks();
Steve Antonede9ca52017-10-16 13:04:27 -07004008 caller()->CreateAndSetAndSignalOffer();
4009
4010 // Wait for all candidates to be gathered on both the caller and callee.
4011 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceGatheringComplete,
4012 caller()->ice_gathering_state(), kDefaultTimeout);
4013 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceGatheringComplete,
4014 callee()->ice_gathering_state(), kDefaultTimeout);
4015
4016 // The candidates will now be included in the session description, so
4017 // signaling them will start the ICE connection.
4018 caller()->CreateAndSetAndSignalOffer();
4019 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4020
4021 // Ensure that media flows in both directions.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004022 MediaExpectations media_expectations;
4023 media_expectations.ExpectBidirectionalAudioAndVideo();
4024 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Antonede9ca52017-10-16 13:04:27 -07004025}
4026
henrika5f6bf242017-11-01 11:06:56 +01004027// Test that SetAudioPlayout can be used to disable audio playout from the
4028// start, then later enable it. This may be useful, for example, if the caller
4029// needs to play a local ringtone until some event occurs, after which it
4030// switches to playing the received audio.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004031TEST_P(PeerConnectionIntegrationTest, DisableAndEnableAudioPlayout) {
henrika5f6bf242017-11-01 11:06:56 +01004032 ASSERT_TRUE(CreatePeerConnectionWrappers());
4033 ConnectFakeSignaling();
4034
4035 // Set up audio-only call where audio playout is disabled on caller's side.
4036 caller()->pc()->SetAudioPlayout(false);
Steve Anton15324772018-01-16 10:26:49 -08004037 caller()->AddAudioTrack();
4038 callee()->AddAudioTrack();
henrika5f6bf242017-11-01 11:06:56 +01004039 caller()->CreateAndSetAndSignalOffer();
4040 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4041
4042 // Pump messages for a second.
4043 WAIT(false, 1000);
4044 // Since audio playout is disabled, the caller shouldn't have received
4045 // anything (at the playout level, at least).
4046 EXPECT_EQ(0, caller()->audio_frames_received());
4047 // As a sanity check, make sure the callee (for which playout isn't disabled)
4048 // did still see frames on its audio level.
4049 ASSERT_GT(callee()->audio_frames_received(), 0);
4050
4051 // Enable playout again, and ensure audio starts flowing.
4052 caller()->pc()->SetAudioPlayout(true);
Seth Hampson2f0d7022018-02-20 11:54:42 -08004053 MediaExpectations media_expectations;
4054 media_expectations.ExpectBidirectionalAudio();
4055 ASSERT_TRUE(ExpectNewFrames(media_expectations));
henrika5f6bf242017-11-01 11:06:56 +01004056}
4057
4058double GetAudioEnergyStat(PeerConnectionWrapper* pc) {
4059 auto report = pc->NewGetStats();
4060 auto track_stats_list =
4061 report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
4062 const webrtc::RTCMediaStreamTrackStats* remote_track_stats = nullptr;
4063 for (const auto* track_stats : track_stats_list) {
4064 if (track_stats->remote_source.is_defined() &&
4065 *track_stats->remote_source) {
4066 remote_track_stats = track_stats;
4067 break;
4068 }
4069 }
4070
4071 if (!remote_track_stats->total_audio_energy.is_defined()) {
4072 return 0.0;
4073 }
4074 return *remote_track_stats->total_audio_energy;
4075}
4076
4077// Test that if audio playout is disabled via the SetAudioPlayout() method, then
4078// incoming audio is still processed and statistics are generated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004079TEST_P(PeerConnectionIntegrationTest,
henrika5f6bf242017-11-01 11:06:56 +01004080 DisableAudioPlayoutStillGeneratesAudioStats) {
4081 ASSERT_TRUE(CreatePeerConnectionWrappers());
4082 ConnectFakeSignaling();
4083
4084 // Set up audio-only call where playout is disabled but audio-processing is
4085 // still active.
Steve Anton15324772018-01-16 10:26:49 -08004086 caller()->AddAudioTrack();
4087 callee()->AddAudioTrack();
henrika5f6bf242017-11-01 11:06:56 +01004088 caller()->pc()->SetAudioPlayout(false);
4089
4090 caller()->CreateAndSetAndSignalOffer();
4091 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4092
4093 // Wait for the callee to receive audio stats.
4094 EXPECT_TRUE_WAIT(GetAudioEnergyStat(caller()) > 0, kMaxWaitForFramesMs);
4095}
4096
henrika4f167df2017-11-01 14:45:55 +01004097// Test that SetAudioRecording can be used to disable audio recording from the
4098// start, then later enable it. This may be useful, for example, if the caller
4099// wants to ensure that no audio resources are active before a certain state
4100// is reached.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004101TEST_P(PeerConnectionIntegrationTest, DisableAndEnableAudioRecording) {
henrika4f167df2017-11-01 14:45:55 +01004102 ASSERT_TRUE(CreatePeerConnectionWrappers());
4103 ConnectFakeSignaling();
4104
4105 // Set up audio-only call where audio recording is disabled on caller's side.
4106 caller()->pc()->SetAudioRecording(false);
Steve Anton15324772018-01-16 10:26:49 -08004107 caller()->AddAudioTrack();
4108 callee()->AddAudioTrack();
henrika4f167df2017-11-01 14:45:55 +01004109 caller()->CreateAndSetAndSignalOffer();
4110 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4111
4112 // Pump messages for a second.
4113 WAIT(false, 1000);
4114 // Since caller has disabled audio recording, the callee shouldn't have
4115 // received anything.
4116 EXPECT_EQ(0, callee()->audio_frames_received());
4117 // As a sanity check, make sure the caller did still see frames on its
4118 // audio level since audio recording is enabled on the calle side.
4119 ASSERT_GT(caller()->audio_frames_received(), 0);
4120
4121 // Enable audio recording again, and ensure audio starts flowing.
4122 caller()->pc()->SetAudioRecording(true);
Seth Hampson2f0d7022018-02-20 11:54:42 -08004123 MediaExpectations media_expectations;
4124 media_expectations.ExpectBidirectionalAudio();
4125 ASSERT_TRUE(ExpectNewFrames(media_expectations));
henrika4f167df2017-11-01 14:45:55 +01004126}
4127
Taylor Brandstetter389a97c2018-01-03 16:26:06 -08004128// Test that after closing PeerConnections, they stop sending any packets (ICE,
4129// DTLS, RTP...).
Seth Hampson2f0d7022018-02-20 11:54:42 -08004130TEST_P(PeerConnectionIntegrationTest, ClosingConnectionStopsPacketFlow) {
Taylor Brandstetter389a97c2018-01-03 16:26:06 -08004131 // Set up audio/video/data, wait for some frames to be received.
4132 ASSERT_TRUE(CreatePeerConnectionWrappers());
4133 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08004134 caller()->AddAudioVideoTracks();
Taylor Brandstetter389a97c2018-01-03 16:26:06 -08004135#ifdef HAVE_SCTP
4136 caller()->CreateDataChannel();
4137#endif
4138 caller()->CreateAndSetAndSignalOffer();
4139 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08004140 MediaExpectations media_expectations;
4141 media_expectations.CalleeExpectsSomeAudioAndVideo();
4142 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Taylor Brandstetter389a97c2018-01-03 16:26:06 -08004143 // Close PeerConnections.
4144 caller()->pc()->Close();
4145 callee()->pc()->Close();
4146 // Pump messages for a second, and ensure no new packets end up sent.
4147 uint32_t sent_packets_a = virtual_socket_server()->sent_packets();
4148 WAIT(false, 1000);
4149 uint32_t sent_packets_b = virtual_socket_server()->sent_packets();
4150 EXPECT_EQ(sent_packets_a, sent_packets_b);
4151}
4152
Steve Anton7eca0932018-03-30 15:18:41 -07004153// Test that transport stats are generated by the RTCStatsCollector for a
4154// connection that only involves data channels. This is a regression test for
4155// crbug.com/826972.
4156#ifdef HAVE_SCTP
4157TEST_P(PeerConnectionIntegrationTest,
4158 TransportStatsReportedForDataChannelOnlyConnection) {
4159 ASSERT_TRUE(CreatePeerConnectionWrappers());
4160 ConnectFakeSignaling();
4161 caller()->CreateDataChannel();
4162
4163 caller()->CreateAndSetAndSignalOffer();
4164 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4165 ASSERT_TRUE_WAIT(callee()->data_channel(), kDefaultTimeout);
4166
4167 auto caller_report = caller()->NewGetStats();
4168 EXPECT_EQ(1u, caller_report->GetStatsOfType<RTCTransportStats>().size());
4169 auto callee_report = callee()->NewGetStats();
4170 EXPECT_EQ(1u, callee_report->GetStatsOfType<RTCTransportStats>().size());
4171}
4172#endif // HAVE_SCTP
4173
Seth Hampson2f0d7022018-02-20 11:54:42 -08004174INSTANTIATE_TEST_CASE_P(PeerConnectionIntegrationTest,
4175 PeerConnectionIntegrationTest,
4176 Values(SdpSemantics::kPlanB,
4177 SdpSemantics::kUnifiedPlan));
Steve Antond3679212018-01-17 17:41:02 -08004178
Steve Anton74255ff2018-01-24 18:32:57 -08004179// Tests that verify interoperability between Plan B and Unified Plan
4180// PeerConnections.
4181class PeerConnectionIntegrationInteropTest
Seth Hampson2f0d7022018-02-20 11:54:42 -08004182 : public PeerConnectionIntegrationBaseTest,
Steve Anton74255ff2018-01-24 18:32:57 -08004183 public ::testing::WithParamInterface<
4184 std::tuple<SdpSemantics, SdpSemantics>> {
4185 protected:
Seth Hampson2f0d7022018-02-20 11:54:42 -08004186 // Setting the SdpSemantics for the base test to kDefault does not matter
4187 // because we specify not to use the test semantics when creating
4188 // PeerConnectionWrappers.
Steve Anton74255ff2018-01-24 18:32:57 -08004189 PeerConnectionIntegrationInteropTest()
Seth Hampson2f0d7022018-02-20 11:54:42 -08004190 : PeerConnectionIntegrationBaseTest(SdpSemantics::kDefault),
4191 caller_semantics_(std::get<0>(GetParam())),
Steve Anton74255ff2018-01-24 18:32:57 -08004192 callee_semantics_(std::get<1>(GetParam())) {}
4193
4194 bool CreatePeerConnectionWrappersWithSemantics() {
4195 RTCConfiguration caller_config;
4196 caller_config.sdp_semantics = caller_semantics_;
4197 RTCConfiguration callee_config;
4198 callee_config.sdp_semantics = callee_semantics_;
4199 return CreatePeerConnectionWrappersWithConfig(caller_config, callee_config);
4200 }
4201
4202 const SdpSemantics caller_semantics_;
4203 const SdpSemantics callee_semantics_;
4204};
4205
4206TEST_P(PeerConnectionIntegrationInteropTest, NoMediaLocalToNoMediaRemote) {
4207 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
4208 ConnectFakeSignaling();
4209
4210 caller()->CreateAndSetAndSignalOffer();
4211 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4212}
4213
4214TEST_P(PeerConnectionIntegrationInteropTest, OneAudioLocalToNoMediaRemote) {
4215 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
4216 ConnectFakeSignaling();
4217 auto audio_sender = caller()->AddAudioTrack();
4218
4219 caller()->CreateAndSetAndSignalOffer();
4220 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4221
4222 // Verify that one audio receiver has been created on the remote and that it
4223 // has the same track ID as the sending track.
4224 auto receivers = callee()->pc()->GetReceivers();
4225 ASSERT_EQ(1u, receivers.size());
4226 EXPECT_EQ(cricket::MEDIA_TYPE_AUDIO, receivers[0]->media_type());
4227 EXPECT_EQ(receivers[0]->track()->id(), audio_sender->track()->id());
4228
Seth Hampson2f0d7022018-02-20 11:54:42 -08004229 MediaExpectations media_expectations;
4230 media_expectations.CalleeExpectsSomeAudio();
4231 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08004232}
4233
4234TEST_P(PeerConnectionIntegrationInteropTest, OneAudioOneVideoToNoMediaRemote) {
4235 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
4236 ConnectFakeSignaling();
4237 auto video_sender = caller()->AddVideoTrack();
4238 auto audio_sender = caller()->AddAudioTrack();
4239
4240 caller()->CreateAndSetAndSignalOffer();
4241 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4242
4243 // Verify that one audio and one video receiver have been created on the
4244 // remote and that they have the same track IDs as the sending tracks.
4245 auto audio_receivers =
4246 callee()->GetReceiversOfType(cricket::MEDIA_TYPE_AUDIO);
4247 ASSERT_EQ(1u, audio_receivers.size());
4248 EXPECT_EQ(audio_receivers[0]->track()->id(), audio_sender->track()->id());
4249 auto video_receivers =
4250 callee()->GetReceiversOfType(cricket::MEDIA_TYPE_VIDEO);
4251 ASSERT_EQ(1u, video_receivers.size());
4252 EXPECT_EQ(video_receivers[0]->track()->id(), video_sender->track()->id());
4253
Seth Hampson2f0d7022018-02-20 11:54:42 -08004254 MediaExpectations media_expectations;
4255 media_expectations.CalleeExpectsSomeAudioAndVideo();
4256 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08004257}
4258
4259TEST_P(PeerConnectionIntegrationInteropTest,
4260 OneAudioOneVideoLocalToOneAudioOneVideoRemote) {
4261 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
4262 ConnectFakeSignaling();
4263 caller()->AddAudioVideoTracks();
4264 callee()->AddAudioVideoTracks();
4265
4266 caller()->CreateAndSetAndSignalOffer();
4267 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4268
Seth Hampson2f0d7022018-02-20 11:54:42 -08004269 MediaExpectations media_expectations;
4270 media_expectations.ExpectBidirectionalAudioAndVideo();
4271 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08004272}
4273
4274TEST_P(PeerConnectionIntegrationInteropTest,
4275 ReverseRolesOneAudioLocalToOneVideoRemote) {
4276 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
4277 ConnectFakeSignaling();
4278 caller()->AddAudioTrack();
4279 callee()->AddVideoTrack();
4280
4281 caller()->CreateAndSetAndSignalOffer();
4282 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4283
4284 // Verify that only the audio track has been negotiated.
4285 EXPECT_EQ(0u, caller()->GetReceiversOfType(cricket::MEDIA_TYPE_VIDEO).size());
4286 // Might also check that the callee's NegotiationNeeded flag is set.
4287
4288 // Reverse roles.
4289 callee()->CreateAndSetAndSignalOffer();
4290 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4291
Seth Hampson2f0d7022018-02-20 11:54:42 -08004292 MediaExpectations media_expectations;
4293 media_expectations.CallerExpectsSomeVideo();
4294 media_expectations.CalleeExpectsSomeAudio();
4295 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08004296}
4297
Steve Antonba42e992018-04-09 14:10:01 -07004298INSTANTIATE_TEST_CASE_P(
4299 PeerConnectionIntegrationTest,
4300 PeerConnectionIntegrationInteropTest,
4301 Values(std::make_tuple(SdpSemantics::kPlanB, SdpSemantics::kUnifiedPlan),
4302 std::make_tuple(SdpSemantics::kUnifiedPlan, SdpSemantics::kPlanB)));
4303
4304// Test that if the Unified Plan side offers two video tracks then the Plan B
4305// side will only see the first one and ignore the second.
4306TEST_F(PeerConnectionIntegrationTestPlanB, TwoVideoUnifiedPlanToNoMediaPlanB) {
4307 RTCConfiguration caller_config;
4308 caller_config.sdp_semantics = SdpSemantics::kUnifiedPlan;
4309 RTCConfiguration callee_config;
4310 callee_config.sdp_semantics = SdpSemantics::kPlanB;
4311 ASSERT_TRUE(
4312 CreatePeerConnectionWrappersWithConfig(caller_config, callee_config));
4313
Steve Anton74255ff2018-01-24 18:32:57 -08004314 ConnectFakeSignaling();
4315 auto first_sender = caller()->AddVideoTrack();
4316 caller()->AddVideoTrack();
4317
4318 caller()->CreateAndSetAndSignalOffer();
4319 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4320
4321 // Verify that there is only one receiver and it corresponds to the first
4322 // added track.
4323 auto receivers = callee()->pc()->GetReceivers();
4324 ASSERT_EQ(1u, receivers.size());
4325 EXPECT_TRUE(receivers[0]->track()->enabled());
4326 EXPECT_EQ(first_sender->track()->id(), receivers[0]->track()->id());
4327
Seth Hampson2f0d7022018-02-20 11:54:42 -08004328 MediaExpectations media_expectations;
4329 media_expectations.CalleeExpectsSomeVideo();
4330 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08004331}
4332
deadbeef1dcb1642017-03-29 21:08:16 -07004333} // namespace
4334
4335#endif // if !defined(THREAD_SANITIZER)