blob: b9a0227c7672425ec80996ed3ec86324b0d18bb2 [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
2179// Basic end-to-end test, but without SSRC/MSID signaling. This functionality
2180// is needed to support legacy endpoints.
2181// TODO(deadbeef): When we support the MID extension and demuxing on MID, also
2182// add a test for an end-to-end test without MID signaling either (basically,
2183// the minimum acceptable SDP).
Seth Hampson2f0d7022018-02-20 11:54:42 -08002184TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithoutSsrcOrMsidSignaling) {
deadbeef1dcb1642017-03-29 21:08:16 -07002185 ASSERT_TRUE(CreatePeerConnectionWrappers());
2186 ConnectFakeSignaling();
2187 // Add audio and video, testing that packets can be demuxed on payload type.
Steve Anton15324772018-01-16 10:26:49 -08002188 caller()->AddAudioVideoTracks();
2189 callee()->AddAudioVideoTracks();
deadbeefd8ad7882017-04-18 16:01:17 -07002190 // Remove SSRCs and MSIDs from the received offer SDP.
2191 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
deadbeef1dcb1642017-03-29 21:08:16 -07002192 caller()->CreateAndSetAndSignalOffer();
2193 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002194 MediaExpectations media_expectations;
2195 media_expectations.ExpectBidirectionalAudioAndVideo();
2196 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002197}
2198
Seth Hampson5897a6e2018-04-03 11:16:33 -07002199// Basic end-to-end test, without SSRC signaling. This means that the track
2200// was created properly and frames are delivered when the MSIDs are communicated
2201// with a=msid lines and no a=ssrc lines.
2202TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
2203 EndToEndCallWithoutSsrcSignaling) {
2204 const char kStreamId[] = "streamId";
2205 ASSERT_TRUE(CreatePeerConnectionWrappers());
2206 ConnectFakeSignaling();
2207 // Add just audio tracks.
2208 caller()->AddTrack(caller()->CreateLocalAudioTrack(), {kStreamId});
2209 callee()->AddAudioTrack();
2210
2211 // Remove SSRCs from the received offer SDP.
2212 callee()->SetReceivedSdpMunger(RemoveSsrcsAndKeepMsids);
2213 caller()->CreateAndSetAndSignalOffer();
2214 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2215 MediaExpectations media_expectations;
2216 media_expectations.ExpectBidirectionalAudio();
2217 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2218}
2219
deadbeef1dcb1642017-03-29 21:08:16 -07002220// Test that if two video tracks are sent (from caller to callee, in this test),
2221// they're transmitted correctly end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002222TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithTwoVideoTracks) {
deadbeef1dcb1642017-03-29 21:08:16 -07002223 ASSERT_TRUE(CreatePeerConnectionWrappers());
2224 ConnectFakeSignaling();
2225 // Add one audio/video stream, and one video-only stream.
Steve Anton15324772018-01-16 10:26:49 -08002226 caller()->AddAudioVideoTracks();
2227 caller()->AddVideoTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07002228 caller()->CreateAndSetAndSignalOffer();
2229 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Steve Anton15324772018-01-16 10:26:49 -08002230 ASSERT_EQ(3u, callee()->pc()->GetReceivers().size());
Seth Hampson2f0d7022018-02-20 11:54:42 -08002231
2232 MediaExpectations media_expectations;
2233 media_expectations.CalleeExpectsSomeAudioAndVideo();
2234 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002235}
2236
2237static void MakeSpecCompliantMaxBundleOffer(cricket::SessionDescription* desc) {
2238 bool first = true;
2239 for (cricket::ContentInfo& content : desc->contents()) {
2240 if (first) {
2241 first = false;
2242 continue;
2243 }
2244 content.bundle_only = true;
2245 }
2246 first = true;
2247 for (cricket::TransportInfo& transport : desc->transport_infos()) {
2248 if (first) {
2249 first = false;
2250 continue;
2251 }
2252 transport.description.ice_ufrag.clear();
2253 transport.description.ice_pwd.clear();
2254 transport.description.connection_role = cricket::CONNECTIONROLE_NONE;
2255 transport.description.identity_fingerprint.reset(nullptr);
2256 }
2257}
2258
2259// Test that if applying a true "max bundle" offer, which uses ports of 0,
2260// "a=bundle-only", omitting "a=fingerprint", "a=setup", "a=ice-ufrag" and
2261// "a=ice-pwd" for all but the audio "m=" section, negotiation still completes
2262// successfully and media flows.
2263// TODO(deadbeef): Update this test to also omit "a=rtcp-mux", once that works.
2264// TODO(deadbeef): Won't need this test once we start generating actual
2265// standards-compliant SDP.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002266TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07002267 EndToEndCallWithSpecCompliantMaxBundleOffer) {
2268 ASSERT_TRUE(CreatePeerConnectionWrappers());
2269 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002270 caller()->AddAudioVideoTracks();
2271 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002272 // Do the equivalent of setting the port to 0, adding a=bundle-only, and
2273 // removing a=ice-ufrag, a=ice-pwd, a=fingerprint and a=setup from all
2274 // but the first m= section.
2275 callee()->SetReceivedSdpMunger(MakeSpecCompliantMaxBundleOffer);
2276 caller()->CreateAndSetAndSignalOffer();
2277 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002278 MediaExpectations media_expectations;
2279 media_expectations.ExpectBidirectionalAudioAndVideo();
2280 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002281}
2282
2283// Test that we can receive the audio output level from a remote audio track.
2284// TODO(deadbeef): Use a fake audio source and verify that the output level is
2285// exactly what the source on the other side was configured with.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002286TEST_P(PeerConnectionIntegrationTest, GetAudioOutputLevelStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-29 21:08:16 -07002287 ASSERT_TRUE(CreatePeerConnectionWrappers());
2288 ConnectFakeSignaling();
2289 // Just add an audio track.
Steve Anton15324772018-01-16 10:26:49 -08002290 caller()->AddAudioTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07002291 caller()->CreateAndSetAndSignalOffer();
2292 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2293
2294 // Get the audio output level stats. Note that the level is not available
2295 // until an RTCP packet has been received.
deadbeefd8ad7882017-04-18 16:01:17 -07002296 EXPECT_TRUE_WAIT(callee()->OldGetStats()->AudioOutputLevel() > 0,
deadbeef1dcb1642017-03-29 21:08:16 -07002297 kMaxWaitForFramesMs);
2298}
2299
2300// Test that an audio input level is reported.
2301// TODO(deadbeef): Use a fake audio source and verify that the input level is
2302// exactly what the source was configured with.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002303TEST_P(PeerConnectionIntegrationTest, GetAudioInputLevelStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-29 21:08:16 -07002304 ASSERT_TRUE(CreatePeerConnectionWrappers());
2305 ConnectFakeSignaling();
2306 // Just add an audio track.
Steve Anton15324772018-01-16 10:26:49 -08002307 caller()->AddAudioTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07002308 caller()->CreateAndSetAndSignalOffer();
2309 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2310
2311 // Get the audio input level stats. The level should be available very
2312 // soon after the test starts.
deadbeefd8ad7882017-04-18 16:01:17 -07002313 EXPECT_TRUE_WAIT(caller()->OldGetStats()->AudioInputLevel() > 0,
deadbeef1dcb1642017-03-29 21:08:16 -07002314 kMaxWaitForStatsMs);
2315}
2316
2317// Test that we can get incoming byte counts from both audio and video tracks.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002318TEST_P(PeerConnectionIntegrationTest, GetBytesReceivedStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-29 21:08:16 -07002319 ASSERT_TRUE(CreatePeerConnectionWrappers());
2320 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002321 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002322 // Do offer/answer, wait for the callee to receive some frames.
2323 caller()->CreateAndSetAndSignalOffer();
2324 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002325
2326 MediaExpectations media_expectations;
2327 media_expectations.CalleeExpectsSomeAudioAndVideo();
2328 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002329
2330 // Get a handle to the remote tracks created, so they can be used as GetStats
2331 // filters.
Steve Anton15324772018-01-16 10:26:49 -08002332 for (auto receiver : callee()->pc()->GetReceivers()) {
2333 // We received frames, so we definitely should have nonzero "received bytes"
2334 // stats at this point.
2335 EXPECT_GT(callee()->OldGetStatsForTrack(receiver->track())->BytesReceived(),
2336 0);
2337 }
deadbeef1dcb1642017-03-29 21:08:16 -07002338}
2339
2340// Test that we can get outgoing byte counts from both audio and video tracks.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002341TEST_P(PeerConnectionIntegrationTest, GetBytesSentStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-29 21:08:16 -07002342 ASSERT_TRUE(CreatePeerConnectionWrappers());
2343 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002344 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002345 auto audio_track = caller()->CreateLocalAudioTrack();
2346 auto video_track = caller()->CreateLocalVideoTrack();
Steve Anton15324772018-01-16 10:26:49 -08002347 caller()->AddTrack(audio_track);
2348 caller()->AddTrack(video_track);
deadbeef1dcb1642017-03-29 21:08:16 -07002349 // Do offer/answer, wait for the callee to receive some frames.
2350 caller()->CreateAndSetAndSignalOffer();
2351 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002352 MediaExpectations media_expectations;
2353 media_expectations.CalleeExpectsSomeAudioAndVideo();
2354 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002355
2356 // The callee received frames, so we definitely should have nonzero "sent
2357 // bytes" stats at this point.
deadbeefd8ad7882017-04-18 16:01:17 -07002358 EXPECT_GT(caller()->OldGetStatsForTrack(audio_track)->BytesSent(), 0);
2359 EXPECT_GT(caller()->OldGetStatsForTrack(video_track)->BytesSent(), 0);
2360}
2361
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002362// Test that we can get capture start ntp time.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002363TEST_P(PeerConnectionIntegrationTest, GetCaptureStartNtpTimeWithOldStatsApi) {
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002364 ASSERT_TRUE(CreatePeerConnectionWrappers());
2365 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002366 caller()->AddAudioTrack();
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002367
Steve Anton15324772018-01-16 10:26:49 -08002368 callee()->AddAudioTrack();
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002369
2370 // Do offer/answer, wait for the callee to receive some frames.
2371 caller()->CreateAndSetAndSignalOffer();
2372 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2373
2374 // Get the remote audio track created on the receiver, so they can be used as
2375 // GetStats filters.
Steve Antonfc853712018-03-01 13:48:58 -08002376 auto receivers = callee()->pc()->GetReceivers();
2377 ASSERT_EQ(1u, receivers.size());
2378 auto remote_audio_track = receivers[0]->track();
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002379
2380 // Get the audio output level stats. Note that the level is not available
2381 // until an RTCP packet has been received.
Zhi Huange830e682018-03-30 10:48:35 -07002382 EXPECT_TRUE_WAIT(
2383 callee()->OldGetStatsForTrack(remote_audio_track)->CaptureStartNtpTime() >
2384 0,
2385 2 * kMaxWaitForFramesMs);
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002386}
2387
deadbeefd8ad7882017-04-18 16:01:17 -07002388// Test that we can get stats (using the new stats implemnetation) for
2389// unsignaled streams. Meaning when SSRCs/MSIDs aren't signaled explicitly in
2390// SDP.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002391TEST_P(PeerConnectionIntegrationTest,
deadbeefd8ad7882017-04-18 16:01:17 -07002392 GetStatsForUnsignaledStreamWithNewStatsApi) {
2393 ASSERT_TRUE(CreatePeerConnectionWrappers());
2394 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002395 caller()->AddAudioTrack();
deadbeefd8ad7882017-04-18 16:01:17 -07002396 // Remove SSRCs and MSIDs from the received offer SDP.
2397 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
2398 caller()->CreateAndSetAndSignalOffer();
2399 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002400 MediaExpectations media_expectations;
2401 media_expectations.CalleeExpectsSomeAudio(1);
2402 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeefd8ad7882017-04-18 16:01:17 -07002403
2404 // We received a frame, so we should have nonzero "bytes received" stats for
2405 // the unsignaled stream, if stats are working for it.
2406 rtc::scoped_refptr<const webrtc::RTCStatsReport> report =
2407 callee()->NewGetStats();
2408 ASSERT_NE(nullptr, report);
2409 auto inbound_stream_stats =
2410 report->GetStatsOfType<webrtc::RTCInboundRTPStreamStats>();
2411 ASSERT_EQ(1U, inbound_stream_stats.size());
2412 ASSERT_TRUE(inbound_stream_stats[0]->bytes_received.is_defined());
2413 ASSERT_GT(*inbound_stream_stats[0]->bytes_received, 0U);
zhihuangf8164932017-05-19 13:09:47 -07002414 ASSERT_TRUE(inbound_stream_stats[0]->track_id.is_defined());
2415}
2416
2417// Test that we can successfully get the media related stats (audio level
2418// etc.) for the unsignaled stream.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002419TEST_P(PeerConnectionIntegrationTest,
zhihuangf8164932017-05-19 13:09:47 -07002420 GetMediaStatsForUnsignaledStreamWithNewStatsApi) {
2421 ASSERT_TRUE(CreatePeerConnectionWrappers());
2422 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002423 caller()->AddAudioVideoTracks();
zhihuangf8164932017-05-19 13:09:47 -07002424 // Remove SSRCs and MSIDs from the received offer SDP.
2425 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
2426 caller()->CreateAndSetAndSignalOffer();
2427 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002428 MediaExpectations media_expectations;
2429 media_expectations.CalleeExpectsSomeAudio(1);
2430 media_expectations.CalleeExpectsSomeVideo(1);
2431 ASSERT_TRUE(ExpectNewFrames(media_expectations));
zhihuangf8164932017-05-19 13:09:47 -07002432
2433 rtc::scoped_refptr<const webrtc::RTCStatsReport> report =
2434 callee()->NewGetStats();
2435 ASSERT_NE(nullptr, report);
2436
2437 auto media_stats = report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
2438 auto audio_index = FindFirstMediaStatsIndexByKind("audio", media_stats);
2439 ASSERT_GE(audio_index, 0);
2440 EXPECT_TRUE(media_stats[audio_index]->audio_level.is_defined());
deadbeef1dcb1642017-03-29 21:08:16 -07002441}
2442
deadbeef4e2deab2017-09-20 13:56:21 -07002443// Helper for test below.
2444void ModifySsrcs(cricket::SessionDescription* desc) {
2445 for (ContentInfo& content : desc->contents()) {
Steve Antonb1c1de12017-12-21 15:14:30 -08002446 for (cricket::StreamParams& stream :
2447 content.media_description()->mutable_streams()) {
deadbeef4e2deab2017-09-20 13:56:21 -07002448 for (uint32_t& ssrc : stream.ssrcs) {
2449 ssrc = rtc::CreateRandomId();
2450 }
2451 }
2452 }
2453}
2454
2455// Test that the "RTCMediaSteamTrackStats" object is updated correctly when
2456// SSRCs are unsignaled, and the SSRC of the received (audio) stream changes.
2457// This should result in two "RTCInboundRTPStreamStats", but only one
2458// "RTCMediaStreamTrackStats", whose counters go up continuously rather than
2459// being reset to 0 once the SSRC change occurs.
2460//
2461// Regression test for this bug:
2462// https://bugs.chromium.org/p/webrtc/issues/detail?id=8158
2463//
2464// The bug causes the track stats to only represent one of the two streams:
2465// whichever one has the higher SSRC. So with this bug, there was a 50% chance
2466// that the track stat counters would reset to 0 when the new stream is
2467// received, and a 50% chance that they'll stop updating (while
2468// "concealed_samples" continues increasing, due to silence being generated for
2469// the inactive stream).
Seth Hampson2f0d7022018-02-20 11:54:42 -08002470TEST_P(PeerConnectionIntegrationTest,
Steve Anton83119dd2017-11-10 16:19:52 -08002471 TrackStatsUpdatedCorrectlyWhenUnsignaledSsrcChanges) {
deadbeef4e2deab2017-09-20 13:56:21 -07002472 ASSERT_TRUE(CreatePeerConnectionWrappers());
2473 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002474 caller()->AddAudioTrack();
deadbeef4e2deab2017-09-20 13:56:21 -07002475 // Remove SSRCs and MSIDs from the received offer SDP, simulating an endpoint
2476 // that doesn't signal SSRCs (from the callee's perspective).
2477 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
2478 caller()->CreateAndSetAndSignalOffer();
2479 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2480 // Wait for 50 audio frames (500ms of audio) to be received by the callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002481 {
2482 MediaExpectations media_expectations;
2483 media_expectations.CalleeExpectsSomeAudio(50);
2484 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2485 }
deadbeef4e2deab2017-09-20 13:56:21 -07002486 // Some audio frames were received, so we should have nonzero "samples
2487 // received" for the track.
2488 rtc::scoped_refptr<const webrtc::RTCStatsReport> report =
2489 callee()->NewGetStats();
2490 ASSERT_NE(nullptr, report);
2491 auto track_stats = report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
2492 ASSERT_EQ(1U, track_stats.size());
2493 ASSERT_TRUE(track_stats[0]->total_samples_received.is_defined());
2494 ASSERT_GT(*track_stats[0]->total_samples_received, 0U);
2495 // uint64_t prev_samples_received = *track_stats[0]->total_samples_received;
2496
2497 // Create a new offer and munge it to cause the caller to use a new SSRC.
2498 caller()->SetGeneratedSdpMunger(ModifySsrcs);
2499 caller()->CreateAndSetAndSignalOffer();
2500 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2501 // Wait for 25 more audio frames (250ms of audio) to be received, from the new
2502 // SSRC.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002503 {
2504 MediaExpectations media_expectations;
2505 media_expectations.CalleeExpectsSomeAudio(25);
2506 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2507 }
deadbeef4e2deab2017-09-20 13:56:21 -07002508
2509 report = callee()->NewGetStats();
2510 ASSERT_NE(nullptr, report);
2511 track_stats = report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
2512 ASSERT_EQ(1U, track_stats.size());
2513 ASSERT_TRUE(track_stats[0]->total_samples_received.is_defined());
2514 // The "total samples received" stat should only be greater than it was
2515 // before.
2516 // TODO(deadbeef): Uncomment this assertion once the bug is completely fixed.
2517 // Right now, the new SSRC will cause the counters to reset to 0.
2518 // EXPECT_GT(*track_stats[0]->total_samples_received, prev_samples_received);
2519
2520 // Additionally, the percentage of concealed samples (samples generated to
Steve Anton83119dd2017-11-10 16:19:52 -08002521 // conceal packet loss) should be less than 50%. If it's greater, that's a
deadbeef4e2deab2017-09-20 13:56:21 -07002522 // good sign that we're seeing stats from the old stream that's no longer
2523 // receiving packets, and is generating concealed samples of silence.
Steve Anton83119dd2017-11-10 16:19:52 -08002524 constexpr double kAcceptableConcealedSamplesPercentage = 0.50;
deadbeef4e2deab2017-09-20 13:56:21 -07002525 ASSERT_TRUE(track_stats[0]->concealed_samples.is_defined());
2526 EXPECT_LT(*track_stats[0]->concealed_samples,
2527 *track_stats[0]->total_samples_received *
2528 kAcceptableConcealedSamplesPercentage);
2529
2530 // Also ensure that we have two "RTCInboundRTPStreamStats" as expected, as a
2531 // sanity check that the SSRC really changed.
2532 // TODO(deadbeef): This isn't working right now, because we're not returning
2533 // *any* stats for the inactive stream. Uncomment when the bug is completely
2534 // fixed.
2535 // auto inbound_stream_stats =
2536 // report->GetStatsOfType<webrtc::RTCInboundRTPStreamStats>();
2537 // ASSERT_EQ(2U, inbound_stream_stats.size());
2538}
2539
deadbeef1dcb1642017-03-29 21:08:16 -07002540// Test that DTLS 1.0 is used if both sides only support DTLS 1.0.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002541TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithDtls10) {
deadbeef1dcb1642017-03-29 21:08:16 -07002542 PeerConnectionFactory::Options dtls_10_options;
2543 dtls_10_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
2544 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(dtls_10_options,
2545 dtls_10_options));
2546 ConnectFakeSignaling();
2547 // Do normal offer/answer and wait for some frames to be received in each
2548 // direction.
Steve Anton15324772018-01-16 10:26:49 -08002549 caller()->AddAudioVideoTracks();
2550 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002551 caller()->CreateAndSetAndSignalOffer();
2552 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002553 MediaExpectations media_expectations;
2554 media_expectations.ExpectBidirectionalAudioAndVideo();
2555 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002556}
2557
2558// Test getting cipher stats and UMA metrics when DTLS 1.0 is negotiated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002559TEST_P(PeerConnectionIntegrationTest, Dtls10CipherStatsAndUmaMetrics) {
deadbeef1dcb1642017-03-29 21:08:16 -07002560 PeerConnectionFactory::Options dtls_10_options;
2561 dtls_10_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
2562 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(dtls_10_options,
2563 dtls_10_options));
2564 ConnectFakeSignaling();
2565 // Register UMA observer before signaling begins.
2566 rtc::scoped_refptr<webrtc::FakeMetricsObserver> caller_observer =
2567 new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
2568 caller()->pc()->RegisterUMAObserver(caller_observer);
Steve Anton15324772018-01-16 10:26:49 -08002569 caller()->AddAudioVideoTracks();
2570 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002571 caller()->CreateAndSetAndSignalOffer();
2572 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2573 EXPECT_TRUE_WAIT(rtc::SSLStreamAdapter::IsAcceptableCipher(
deadbeefd8ad7882017-04-18 16:01:17 -07002574 caller()->OldGetStats()->DtlsCipher(), rtc::KT_DEFAULT),
deadbeef1dcb1642017-03-29 21:08:16 -07002575 kDefaultTimeout);
2576 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
deadbeefd8ad7882017-04-18 16:01:17 -07002577 caller()->OldGetStats()->SrtpCipher(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07002578 EXPECT_EQ(1,
2579 caller_observer->GetEnumCounter(webrtc::kEnumCounterAudioSrtpCipher,
2580 kDefaultSrtpCryptoSuite));
2581}
2582
2583// Test getting cipher stats and UMA metrics when DTLS 1.2 is negotiated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002584TEST_P(PeerConnectionIntegrationTest, Dtls12CipherStatsAndUmaMetrics) {
deadbeef1dcb1642017-03-29 21:08:16 -07002585 PeerConnectionFactory::Options dtls_12_options;
2586 dtls_12_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
2587 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(dtls_12_options,
2588 dtls_12_options));
2589 ConnectFakeSignaling();
2590 // Register UMA observer before signaling begins.
2591 rtc::scoped_refptr<webrtc::FakeMetricsObserver> caller_observer =
2592 new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
2593 caller()->pc()->RegisterUMAObserver(caller_observer);
Steve Anton15324772018-01-16 10:26:49 -08002594 caller()->AddAudioVideoTracks();
2595 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002596 caller()->CreateAndSetAndSignalOffer();
2597 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2598 EXPECT_TRUE_WAIT(rtc::SSLStreamAdapter::IsAcceptableCipher(
deadbeefd8ad7882017-04-18 16:01:17 -07002599 caller()->OldGetStats()->DtlsCipher(), rtc::KT_DEFAULT),
deadbeef1dcb1642017-03-29 21:08:16 -07002600 kDefaultTimeout);
2601 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
deadbeefd8ad7882017-04-18 16:01:17 -07002602 caller()->OldGetStats()->SrtpCipher(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07002603 EXPECT_EQ(1,
2604 caller_observer->GetEnumCounter(webrtc::kEnumCounterAudioSrtpCipher,
2605 kDefaultSrtpCryptoSuite));
2606}
2607
2608// Test that DTLS 1.0 can be used if the caller supports DTLS 1.2 and the
2609// callee only supports 1.0.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002610TEST_P(PeerConnectionIntegrationTest, CallerDtls12ToCalleeDtls10) {
deadbeef1dcb1642017-03-29 21:08:16 -07002611 PeerConnectionFactory::Options caller_options;
2612 caller_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
2613 PeerConnectionFactory::Options callee_options;
2614 callee_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
2615 ASSERT_TRUE(
2616 CreatePeerConnectionWrappersWithOptions(caller_options, callee_options));
2617 ConnectFakeSignaling();
2618 // Do normal offer/answer and wait for some frames to be received in each
2619 // direction.
Steve Anton15324772018-01-16 10:26:49 -08002620 caller()->AddAudioVideoTracks();
2621 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002622 caller()->CreateAndSetAndSignalOffer();
2623 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002624 MediaExpectations media_expectations;
2625 media_expectations.ExpectBidirectionalAudioAndVideo();
2626 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002627}
2628
2629// Test that DTLS 1.0 can be used if the caller only supports DTLS 1.0 and the
2630// callee supports 1.2.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002631TEST_P(PeerConnectionIntegrationTest, CallerDtls10ToCalleeDtls12) {
deadbeef1dcb1642017-03-29 21:08:16 -07002632 PeerConnectionFactory::Options caller_options;
2633 caller_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
2634 PeerConnectionFactory::Options callee_options;
2635 callee_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
2636 ASSERT_TRUE(
2637 CreatePeerConnectionWrappersWithOptions(caller_options, callee_options));
2638 ConnectFakeSignaling();
2639 // Do normal offer/answer and wait for some frames to be received in each
2640 // direction.
Steve Anton15324772018-01-16 10:26:49 -08002641 caller()->AddAudioVideoTracks();
2642 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002643 caller()->CreateAndSetAndSignalOffer();
2644 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002645 MediaExpectations media_expectations;
2646 media_expectations.ExpectBidirectionalAudioAndVideo();
2647 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002648}
2649
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07002650// The three tests below verify that "enable_aes128_sha1_32_crypto_cipher"
2651// works as expected; the cipher should only be used if enabled by both sides.
2652TEST_P(PeerConnectionIntegrationTest,
2653 Aes128Sha1_32_CipherNotUsedWhenOnlyCallerSupported) {
2654 PeerConnectionFactory::Options caller_options;
2655 caller_options.crypto_options.enable_aes128_sha1_32_crypto_cipher = true;
2656 PeerConnectionFactory::Options callee_options;
2657 callee_options.crypto_options.enable_aes128_sha1_32_crypto_cipher = false;
2658 int expected_cipher_suite = rtc::SRTP_AES128_CM_SHA1_80;
2659 TestNegotiatedCipherSuite(caller_options, callee_options,
2660 expected_cipher_suite);
2661}
2662
2663TEST_P(PeerConnectionIntegrationTest,
2664 Aes128Sha1_32_CipherNotUsedWhenOnlyCalleeSupported) {
2665 PeerConnectionFactory::Options caller_options;
2666 caller_options.crypto_options.enable_aes128_sha1_32_crypto_cipher = false;
2667 PeerConnectionFactory::Options callee_options;
2668 callee_options.crypto_options.enable_aes128_sha1_32_crypto_cipher = true;
2669 int expected_cipher_suite = rtc::SRTP_AES128_CM_SHA1_80;
2670 TestNegotiatedCipherSuite(caller_options, callee_options,
2671 expected_cipher_suite);
2672}
2673
2674TEST_P(PeerConnectionIntegrationTest, Aes128Sha1_32_CipherUsedWhenSupported) {
2675 PeerConnectionFactory::Options caller_options;
2676 caller_options.crypto_options.enable_aes128_sha1_32_crypto_cipher = true;
2677 PeerConnectionFactory::Options callee_options;
2678 callee_options.crypto_options.enable_aes128_sha1_32_crypto_cipher = true;
2679 int expected_cipher_suite = rtc::SRTP_AES128_CM_SHA1_32;
2680 TestNegotiatedCipherSuite(caller_options, callee_options,
2681 expected_cipher_suite);
2682}
2683
deadbeef1dcb1642017-03-29 21:08:16 -07002684// Test that a non-GCM cipher is used if both sides only support non-GCM.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002685TEST_P(PeerConnectionIntegrationTest, NonGcmCipherUsedWhenGcmNotSupported) {
deadbeef1dcb1642017-03-29 21:08:16 -07002686 bool local_gcm_enabled = false;
2687 bool remote_gcm_enabled = false;
2688 int expected_cipher_suite = kDefaultSrtpCryptoSuite;
2689 TestGcmNegotiationUsesCipherSuite(local_gcm_enabled, remote_gcm_enabled,
2690 expected_cipher_suite);
2691}
2692
2693// Test that a GCM cipher is used if both ends support it.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002694TEST_P(PeerConnectionIntegrationTest, GcmCipherUsedWhenGcmSupported) {
deadbeef1dcb1642017-03-29 21:08:16 -07002695 bool local_gcm_enabled = true;
2696 bool remote_gcm_enabled = true;
2697 int expected_cipher_suite = kDefaultSrtpCryptoSuiteGcm;
2698 TestGcmNegotiationUsesCipherSuite(local_gcm_enabled, remote_gcm_enabled,
2699 expected_cipher_suite);
2700}
2701
2702// Test that GCM isn't used if only the offerer supports it.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002703TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07002704 NonGcmCipherUsedWhenOnlyCallerSupportsGcm) {
2705 bool local_gcm_enabled = true;
2706 bool remote_gcm_enabled = false;
2707 int expected_cipher_suite = kDefaultSrtpCryptoSuite;
2708 TestGcmNegotiationUsesCipherSuite(local_gcm_enabled, remote_gcm_enabled,
2709 expected_cipher_suite);
2710}
2711
2712// Test that GCM isn't used if only the answerer supports it.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002713TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07002714 NonGcmCipherUsedWhenOnlyCalleeSupportsGcm) {
2715 bool local_gcm_enabled = false;
2716 bool remote_gcm_enabled = true;
2717 int expected_cipher_suite = kDefaultSrtpCryptoSuite;
2718 TestGcmNegotiationUsesCipherSuite(local_gcm_enabled, remote_gcm_enabled,
2719 expected_cipher_suite);
2720}
2721
deadbeef7914b8c2017-04-21 03:23:33 -07002722// Verify that media can be transmitted end-to-end when GCM crypto suites are
2723// enabled. Note that the above tests, such as GcmCipherUsedWhenGcmSupported,
2724// only verify that a GCM cipher is negotiated, and not necessarily that SRTP
2725// works with it.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002726TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithGcmCipher) {
deadbeef7914b8c2017-04-21 03:23:33 -07002727 PeerConnectionFactory::Options gcm_options;
2728 gcm_options.crypto_options.enable_gcm_crypto_suites = true;
2729 ASSERT_TRUE(
2730 CreatePeerConnectionWrappersWithOptions(gcm_options, gcm_options));
2731 ConnectFakeSignaling();
2732 // Do normal offer/answer and wait for some frames to be received in each
2733 // direction.
Steve Anton15324772018-01-16 10:26:49 -08002734 caller()->AddAudioVideoTracks();
2735 callee()->AddAudioVideoTracks();
deadbeef7914b8c2017-04-21 03:23:33 -07002736 caller()->CreateAndSetAndSignalOffer();
2737 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002738 MediaExpectations media_expectations;
2739 media_expectations.ExpectBidirectionalAudioAndVideo();
2740 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef7914b8c2017-04-21 03:23:33 -07002741}
2742
deadbeef1dcb1642017-03-29 21:08:16 -07002743// This test sets up a call between two parties with audio, video and an RTP
2744// data channel.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002745TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithRtpDataChannel) {
deadbeef1dcb1642017-03-29 21:08:16 -07002746 FakeConstraints setup_constraints;
2747 setup_constraints.SetAllowRtpDataChannels();
2748 ASSERT_TRUE(CreatePeerConnectionWrappersWithConstraints(&setup_constraints,
2749 &setup_constraints));
2750 ConnectFakeSignaling();
2751 // Expect that data channel created on caller side will show up for callee as
2752 // well.
2753 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08002754 caller()->AddAudioVideoTracks();
2755 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002756 caller()->CreateAndSetAndSignalOffer();
2757 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2758 // Ensure the existence of the RTP data channel didn't impede audio/video.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002759 MediaExpectations media_expectations;
2760 media_expectations.ExpectBidirectionalAudioAndVideo();
2761 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002762 ASSERT_NE(nullptr, caller()->data_channel());
2763 ASSERT_NE(nullptr, callee()->data_channel());
2764 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
2765 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
2766
2767 // Ensure data can be sent in both directions.
2768 std::string data = "hello world";
2769 SendRtpDataWithRetries(caller()->data_channel(), data, 5);
2770 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
2771 kDefaultTimeout);
2772 SendRtpDataWithRetries(callee()->data_channel(), data, 5);
2773 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
2774 kDefaultTimeout);
2775}
2776
2777// Ensure that an RTP data channel is signaled as closed for the caller when
2778// the callee rejects it in a subsequent offer.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002779TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07002780 RtpDataChannelSignaledClosedInCalleeOffer) {
2781 // Same procedure as above test.
2782 FakeConstraints setup_constraints;
2783 setup_constraints.SetAllowRtpDataChannels();
2784 ASSERT_TRUE(CreatePeerConnectionWrappersWithConstraints(&setup_constraints,
2785 &setup_constraints));
2786 ConnectFakeSignaling();
2787 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08002788 caller()->AddAudioVideoTracks();
2789 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002790 caller()->CreateAndSetAndSignalOffer();
2791 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2792 ASSERT_NE(nullptr, caller()->data_channel());
2793 ASSERT_NE(nullptr, callee()->data_channel());
2794 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
2795 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
2796
2797 // Close the data channel on the callee, and do an updated offer/answer.
2798 callee()->data_channel()->Close();
2799 callee()->CreateAndSetAndSignalOffer();
2800 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2801 EXPECT_FALSE(caller()->data_observer()->IsOpen());
2802 EXPECT_FALSE(callee()->data_observer()->IsOpen());
2803}
2804
2805// Tests that data is buffered in an RTP data channel until an observer is
2806// registered for it.
2807//
2808// NOTE: RTP data channels can receive data before the underlying
2809// transport has detected that a channel is writable and thus data can be
2810// received before the data channel state changes to open. That is hard to test
2811// but the same buffering is expected to be used in that case.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002812TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07002813 DataBufferedUntilRtpDataChannelObserverRegistered) {
2814 // Use fake clock and simulated network delay so that we predictably can wait
2815 // until an SCTP message has been delivered without "sleep()"ing.
2816 rtc::ScopedFakeClock fake_clock;
2817 // Some things use a time of "0" as a special value, so we need to start out
2818 // the fake clock at a nonzero time.
2819 // TODO(deadbeef): Fix this.
2820 fake_clock.AdvanceTime(rtc::TimeDelta::FromSeconds(1));
2821 virtual_socket_server()->set_delay_mean(5); // 5 ms per hop.
2822 virtual_socket_server()->UpdateDelayDistribution();
2823
2824 FakeConstraints constraints;
2825 constraints.SetAllowRtpDataChannels();
2826 ASSERT_TRUE(
2827 CreatePeerConnectionWrappersWithConstraints(&constraints, &constraints));
2828 ConnectFakeSignaling();
2829 caller()->CreateDataChannel();
2830 caller()->CreateAndSetAndSignalOffer();
2831 ASSERT_TRUE(caller()->data_channel() != nullptr);
2832 ASSERT_TRUE_SIMULATED_WAIT(callee()->data_channel() != nullptr,
2833 kDefaultTimeout, fake_clock);
2834 ASSERT_TRUE_SIMULATED_WAIT(caller()->data_observer()->IsOpen(),
2835 kDefaultTimeout, fake_clock);
2836 ASSERT_EQ_SIMULATED_WAIT(DataChannelInterface::kOpen,
2837 callee()->data_channel()->state(), kDefaultTimeout,
2838 fake_clock);
2839
2840 // Unregister the observer which is normally automatically registered.
2841 callee()->data_channel()->UnregisterObserver();
2842 // Send data and advance fake clock until it should have been received.
2843 std::string data = "hello world";
2844 caller()->data_channel()->Send(DataBuffer(data));
2845 SIMULATED_WAIT(false, 50, fake_clock);
2846
2847 // Attach data channel and expect data to be received immediately. Note that
2848 // EXPECT_EQ_WAIT is used, such that the simulated clock is not advanced any
2849 // further, but data can be received even if the callback is asynchronous.
2850 MockDataChannelObserver new_observer(callee()->data_channel());
2851 EXPECT_EQ_SIMULATED_WAIT(data, new_observer.last_message(), kDefaultTimeout,
2852 fake_clock);
2853}
2854
2855// This test sets up a call between two parties with audio, video and but only
2856// the caller client supports RTP data channels.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002857TEST_P(PeerConnectionIntegrationTest, RtpDataChannelsRejectedByCallee) {
deadbeef1dcb1642017-03-29 21:08:16 -07002858 FakeConstraints setup_constraints_1;
2859 setup_constraints_1.SetAllowRtpDataChannels();
2860 // Must disable DTLS to make negotiation succeed.
2861 setup_constraints_1.SetMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
2862 false);
2863 FakeConstraints setup_constraints_2;
2864 setup_constraints_2.SetMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
2865 false);
2866 ASSERT_TRUE(CreatePeerConnectionWrappersWithConstraints(
2867 &setup_constraints_1, &setup_constraints_2));
2868 ConnectFakeSignaling();
2869 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08002870 caller()->AddAudioVideoTracks();
2871 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002872 caller()->CreateAndSetAndSignalOffer();
2873 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2874 // The caller should still have a data channel, but it should be closed, and
2875 // one should ever have been created for the callee.
2876 EXPECT_TRUE(caller()->data_channel() != nullptr);
2877 EXPECT_FALSE(caller()->data_observer()->IsOpen());
2878 EXPECT_EQ(nullptr, callee()->data_channel());
2879}
2880
2881// This test sets up a call between two parties with audio, and video. When
2882// audio and video is setup and flowing, an RTP data channel is negotiated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002883TEST_P(PeerConnectionIntegrationTest, AddRtpDataChannelInSubsequentOffer) {
deadbeef1dcb1642017-03-29 21:08:16 -07002884 FakeConstraints setup_constraints;
2885 setup_constraints.SetAllowRtpDataChannels();
2886 ASSERT_TRUE(CreatePeerConnectionWrappersWithConstraints(&setup_constraints,
2887 &setup_constraints));
2888 ConnectFakeSignaling();
2889 // Do initial offer/answer with audio/video.
Steve Anton15324772018-01-16 10:26:49 -08002890 caller()->AddAudioVideoTracks();
2891 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002892 caller()->CreateAndSetAndSignalOffer();
2893 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2894 // Create data channel and do new offer and answer.
2895 caller()->CreateDataChannel();
2896 caller()->CreateAndSetAndSignalOffer();
2897 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2898 ASSERT_NE(nullptr, caller()->data_channel());
2899 ASSERT_NE(nullptr, callee()->data_channel());
2900 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
2901 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
2902 // Ensure data can be sent in both directions.
2903 std::string data = "hello world";
2904 SendRtpDataWithRetries(caller()->data_channel(), data, 5);
2905 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
2906 kDefaultTimeout);
2907 SendRtpDataWithRetries(callee()->data_channel(), data, 5);
2908 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
2909 kDefaultTimeout);
2910}
2911
2912#ifdef HAVE_SCTP
2913
2914// This test sets up a call between two parties with audio, video and an SCTP
2915// data channel.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002916TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithSctpDataChannel) {
deadbeef1dcb1642017-03-29 21:08:16 -07002917 ASSERT_TRUE(CreatePeerConnectionWrappers());
2918 ConnectFakeSignaling();
2919 // Expect that data channel created on caller side will show up for callee as
2920 // well.
2921 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08002922 caller()->AddAudioVideoTracks();
2923 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002924 caller()->CreateAndSetAndSignalOffer();
2925 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2926 // Ensure the existence of the SCTP data channel didn't impede audio/video.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002927 MediaExpectations media_expectations;
2928 media_expectations.ExpectBidirectionalAudioAndVideo();
2929 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002930 // Caller data channel should already exist (it created one). Callee data
2931 // channel may not exist yet, since negotiation happens in-band, not in SDP.
2932 ASSERT_NE(nullptr, caller()->data_channel());
2933 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
2934 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
2935 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
2936
2937 // Ensure data can be sent in both directions.
2938 std::string data = "hello world";
2939 caller()->data_channel()->Send(DataBuffer(data));
2940 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
2941 kDefaultTimeout);
2942 callee()->data_channel()->Send(DataBuffer(data));
2943 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
2944 kDefaultTimeout);
2945}
2946
2947// Ensure that when the callee closes an SCTP data channel, the closing
2948// procedure results in the data channel being closed for the caller as well.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002949TEST_P(PeerConnectionIntegrationTest, CalleeClosesSctpDataChannel) {
deadbeef1dcb1642017-03-29 21:08:16 -07002950 // Same procedure as above test.
2951 ASSERT_TRUE(CreatePeerConnectionWrappers());
2952 ConnectFakeSignaling();
2953 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08002954 caller()->AddAudioVideoTracks();
2955 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002956 caller()->CreateAndSetAndSignalOffer();
2957 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2958 ASSERT_NE(nullptr, caller()->data_channel());
2959 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
2960 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
2961 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
2962
2963 // Close the data channel on the callee side, and wait for it to reach the
2964 // "closed" state on both sides.
2965 callee()->data_channel()->Close();
2966 EXPECT_TRUE_WAIT(!caller()->data_observer()->IsOpen(), kDefaultTimeout);
2967 EXPECT_TRUE_WAIT(!callee()->data_observer()->IsOpen(), kDefaultTimeout);
2968}
2969
Seth Hampson2f0d7022018-02-20 11:54:42 -08002970TEST_P(PeerConnectionIntegrationTest, SctpDataChannelConfigSentToOtherSide) {
Steve Antonda6c0952017-10-23 11:41:54 -07002971 ASSERT_TRUE(CreatePeerConnectionWrappers());
2972 ConnectFakeSignaling();
2973 webrtc::DataChannelInit init;
2974 init.id = 53;
2975 init.maxRetransmits = 52;
2976 caller()->CreateDataChannel("data-channel", &init);
Steve Anton15324772018-01-16 10:26:49 -08002977 caller()->AddAudioVideoTracks();
2978 callee()->AddAudioVideoTracks();
Steve Antonda6c0952017-10-23 11:41:54 -07002979 caller()->CreateAndSetAndSignalOffer();
2980 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Steve Anton074dece2017-10-24 13:04:12 -07002981 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
2982 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
Steve Antonda6c0952017-10-23 11:41:54 -07002983 EXPECT_EQ(init.id, callee()->data_channel()->id());
2984 EXPECT_EQ("data-channel", callee()->data_channel()->label());
2985 EXPECT_EQ(init.maxRetransmits, callee()->data_channel()->maxRetransmits());
2986 EXPECT_FALSE(callee()->data_channel()->negotiated());
2987}
2988
deadbeef1dcb1642017-03-29 21:08:16 -07002989// Test usrsctp's ability to process unordered data stream, where data actually
2990// arrives out of order using simulated delays. Previously there have been some
2991// bugs in this area.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002992TEST_P(PeerConnectionIntegrationTest, StressTestUnorderedSctpDataChannel) {
deadbeef1dcb1642017-03-29 21:08:16 -07002993 // Introduce random network delays.
2994 // Otherwise it's not a true "unordered" test.
2995 virtual_socket_server()->set_delay_mean(20);
2996 virtual_socket_server()->set_delay_stddev(5);
2997 virtual_socket_server()->UpdateDelayDistribution();
2998 // Normal procedure, but with unordered data channel config.
2999 ASSERT_TRUE(CreatePeerConnectionWrappers());
3000 ConnectFakeSignaling();
3001 webrtc::DataChannelInit init;
3002 init.ordered = false;
3003 caller()->CreateDataChannel(&init);
3004 caller()->CreateAndSetAndSignalOffer();
3005 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3006 ASSERT_NE(nullptr, caller()->data_channel());
3007 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3008 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3009 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3010
3011 static constexpr int kNumMessages = 100;
3012 // Deliberately chosen to be larger than the MTU so messages get fragmented.
3013 static constexpr size_t kMaxMessageSize = 4096;
3014 // Create and send random messages.
3015 std::vector<std::string> sent_messages;
3016 for (int i = 0; i < kNumMessages; ++i) {
3017 size_t length =
3018 (rand() % kMaxMessageSize) + 1; // NOLINT (rand_r instead of rand)
3019 std::string message;
3020 ASSERT_TRUE(rtc::CreateRandomString(length, &message));
3021 caller()->data_channel()->Send(DataBuffer(message));
3022 callee()->data_channel()->Send(DataBuffer(message));
3023 sent_messages.push_back(message);
3024 }
3025
3026 // Wait for all messages to be received.
3027 EXPECT_EQ_WAIT(kNumMessages,
3028 caller()->data_observer()->received_message_count(),
3029 kDefaultTimeout);
3030 EXPECT_EQ_WAIT(kNumMessages,
3031 callee()->data_observer()->received_message_count(),
3032 kDefaultTimeout);
3033
3034 // Sort and compare to make sure none of the messages were corrupted.
3035 std::vector<std::string> caller_received_messages =
3036 caller()->data_observer()->messages();
3037 std::vector<std::string> callee_received_messages =
3038 callee()->data_observer()->messages();
3039 std::sort(sent_messages.begin(), sent_messages.end());
3040 std::sort(caller_received_messages.begin(), caller_received_messages.end());
3041 std::sort(callee_received_messages.begin(), callee_received_messages.end());
3042 EXPECT_EQ(sent_messages, caller_received_messages);
3043 EXPECT_EQ(sent_messages, callee_received_messages);
3044}
3045
3046// This test sets up a call between two parties with audio, and video. When
3047// audio and video are setup and flowing, an SCTP data channel is negotiated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003048TEST_P(PeerConnectionIntegrationTest, AddSctpDataChannelInSubsequentOffer) {
deadbeef1dcb1642017-03-29 21:08:16 -07003049 ASSERT_TRUE(CreatePeerConnectionWrappers());
3050 ConnectFakeSignaling();
3051 // Do initial offer/answer with audio/video.
Steve Anton15324772018-01-16 10:26:49 -08003052 caller()->AddAudioVideoTracks();
3053 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003054 caller()->CreateAndSetAndSignalOffer();
3055 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3056 // Create data channel and do new offer and answer.
3057 caller()->CreateDataChannel();
3058 caller()->CreateAndSetAndSignalOffer();
3059 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3060 // Caller data channel should already exist (it created one). Callee data
3061 // channel may not exist yet, since negotiation happens in-band, not in SDP.
3062 ASSERT_NE(nullptr, caller()->data_channel());
3063 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3064 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3065 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3066 // Ensure data can be sent in both directions.
3067 std::string data = "hello world";
3068 caller()->data_channel()->Send(DataBuffer(data));
3069 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3070 kDefaultTimeout);
3071 callee()->data_channel()->Send(DataBuffer(data));
3072 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3073 kDefaultTimeout);
3074}
3075
deadbeef7914b8c2017-04-21 03:23:33 -07003076// Set up a connection initially just using SCTP data channels, later upgrading
3077// to audio/video, ensuring frames are received end-to-end. Effectively the
3078// inverse of the test above.
3079// This was broken in M57; see https://crbug.com/711243
Seth Hampson2f0d7022018-02-20 11:54:42 -08003080TEST_P(PeerConnectionIntegrationTest, SctpDataChannelToAudioVideoUpgrade) {
deadbeef7914b8c2017-04-21 03:23:33 -07003081 ASSERT_TRUE(CreatePeerConnectionWrappers());
3082 ConnectFakeSignaling();
3083 // Do initial offer/answer with just data channel.
3084 caller()->CreateDataChannel();
3085 caller()->CreateAndSetAndSignalOffer();
3086 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3087 // Wait until data can be sent over the data channel.
3088 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3089 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3090 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3091
3092 // Do subsequent offer/answer with two-way audio and video. Audio and video
3093 // should end up bundled on the DTLS/ICE transport already used for data.
Steve Anton15324772018-01-16 10:26:49 -08003094 caller()->AddAudioVideoTracks();
3095 callee()->AddAudioVideoTracks();
deadbeef7914b8c2017-04-21 03:23:33 -07003096 caller()->CreateAndSetAndSignalOffer();
3097 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08003098 MediaExpectations media_expectations;
3099 media_expectations.ExpectBidirectionalAudioAndVideo();
3100 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef7914b8c2017-04-21 03:23:33 -07003101}
3102
deadbeef8b7e9ad2017-05-25 09:38:55 -07003103static void MakeSpecCompliantSctpOffer(cricket::SessionDescription* desc) {
deadbeef8b7e9ad2017-05-25 09:38:55 -07003104 cricket::DataContentDescription* dcd_offer =
Steve Antonb1c1de12017-12-21 15:14:30 -08003105 GetFirstDataContentDescription(desc);
3106 ASSERT_TRUE(dcd_offer);
deadbeef8b7e9ad2017-05-25 09:38:55 -07003107 dcd_offer->set_use_sctpmap(false);
3108 dcd_offer->set_protocol("UDP/DTLS/SCTP");
3109}
3110
3111// Test that the data channel works when a spec-compliant SCTP m= section is
3112// offered (using "a=sctp-port" instead of "a=sctpmap", and using
3113// "UDP/DTLS/SCTP" as the protocol).
Seth Hampson2f0d7022018-02-20 11:54:42 -08003114TEST_P(PeerConnectionIntegrationTest,
deadbeef8b7e9ad2017-05-25 09:38:55 -07003115 DataChannelWorksWhenSpecCompliantSctpOfferReceived) {
3116 ASSERT_TRUE(CreatePeerConnectionWrappers());
3117 ConnectFakeSignaling();
3118 caller()->CreateDataChannel();
3119 caller()->SetGeneratedSdpMunger(MakeSpecCompliantSctpOffer);
3120 caller()->CreateAndSetAndSignalOffer();
3121 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3122 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3123 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3124 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3125
3126 // Ensure data can be sent in both directions.
3127 std::string data = "hello world";
3128 caller()->data_channel()->Send(DataBuffer(data));
3129 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3130 kDefaultTimeout);
3131 callee()->data_channel()->Send(DataBuffer(data));
3132 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3133 kDefaultTimeout);
3134}
3135
deadbeef1dcb1642017-03-29 21:08:16 -07003136#endif // HAVE_SCTP
3137
3138// Test that the ICE connection and gathering states eventually reach
3139// "complete".
Seth Hampson2f0d7022018-02-20 11:54:42 -08003140TEST_P(PeerConnectionIntegrationTest, IceStatesReachCompletion) {
deadbeef1dcb1642017-03-29 21:08:16 -07003141 ASSERT_TRUE(CreatePeerConnectionWrappers());
3142 ConnectFakeSignaling();
3143 // Do normal offer/answer.
Steve Anton15324772018-01-16 10:26:49 -08003144 caller()->AddAudioVideoTracks();
3145 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003146 caller()->CreateAndSetAndSignalOffer();
3147 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3148 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceGatheringComplete,
3149 caller()->ice_gathering_state(), kMaxWaitForFramesMs);
3150 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceGatheringComplete,
3151 callee()->ice_gathering_state(), kMaxWaitForFramesMs);
3152 // After the best candidate pair is selected and all candidates are signaled,
3153 // the ICE connection state should reach "complete".
3154 // TODO(deadbeef): Currently, the ICE "controlled" agent (the
3155 // answerer/"callee" by default) only reaches "connected". When this is
3156 // fixed, this test should be updated.
3157 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
3158 caller()->ice_connection_state(), kDefaultTimeout);
3159 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
3160 callee()->ice_connection_state(), kDefaultTimeout);
3161}
3162
Steve Antonede9ca52017-10-16 13:04:27 -07003163// Test that firewalling the ICE connection causes the clients to identify the
3164// disconnected state and then removing the firewall causes them to reconnect.
3165class PeerConnectionIntegrationIceStatesTest
Seth Hampson2f0d7022018-02-20 11:54:42 -08003166 : public PeerConnectionIntegrationBaseTest,
3167 public ::testing::WithParamInterface<
3168 std::tuple<SdpSemantics, std::tuple<std::string, uint32_t>>> {
Steve Antonede9ca52017-10-16 13:04:27 -07003169 protected:
Seth Hampson2f0d7022018-02-20 11:54:42 -08003170 PeerConnectionIntegrationIceStatesTest()
3171 : PeerConnectionIntegrationBaseTest(std::get<0>(GetParam())) {
3172 port_allocator_flags_ = std::get<1>(std::get<1>(GetParam()));
Steve Antonede9ca52017-10-16 13:04:27 -07003173 }
3174
3175 void StartStunServer(const SocketAddress& server_address) {
3176 stun_server_.reset(
3177 cricket::TestStunServer::Create(network_thread(), server_address));
3178 }
3179
3180 bool TestIPv6() {
3181 return (port_allocator_flags_ & cricket::PORTALLOCATOR_ENABLE_IPV6);
3182 }
3183
3184 void SetPortAllocatorFlags() {
Patrik Höglund3dc41062018-04-11 11:13:57 +00003185 caller()->port_allocator()->set_flags(port_allocator_flags_);
3186 callee()->port_allocator()->set_flags(port_allocator_flags_);
Steve Antonede9ca52017-10-16 13:04:27 -07003187 }
3188
3189 std::vector<SocketAddress> CallerAddresses() {
3190 std::vector<SocketAddress> addresses;
3191 addresses.push_back(SocketAddress("1.1.1.1", 0));
3192 if (TestIPv6()) {
3193 addresses.push_back(SocketAddress("1111:0:a:b:c:d:e:f", 0));
3194 }
3195 return addresses;
3196 }
3197
3198 std::vector<SocketAddress> CalleeAddresses() {
3199 std::vector<SocketAddress> addresses;
3200 addresses.push_back(SocketAddress("2.2.2.2", 0));
3201 if (TestIPv6()) {
3202 addresses.push_back(SocketAddress("2222:0:a:b:c:d:e:f", 0));
3203 }
3204 return addresses;
3205 }
3206
3207 void SetUpNetworkInterfaces() {
3208 // Remove the default interfaces added by the test infrastructure.
3209 caller()->network()->RemoveInterface(kDefaultLocalAddress);
3210 callee()->network()->RemoveInterface(kDefaultLocalAddress);
3211
3212 // Add network addresses for test.
3213 for (const auto& caller_address : CallerAddresses()) {
3214 caller()->network()->AddInterface(caller_address);
3215 }
3216 for (const auto& callee_address : CalleeAddresses()) {
3217 callee()->network()->AddInterface(callee_address);
3218 }
3219 }
3220
3221 private:
3222 uint32_t port_allocator_flags_;
3223 std::unique_ptr<cricket::TestStunServer> stun_server_;
3224};
3225
3226// Tests that the PeerConnection goes through all the ICE gathering/connection
3227// states over the duration of the call. This includes Disconnected and Failed
3228// states, induced by putting a firewall between the peers and waiting for them
3229// to time out.
Steve Anton83119dd2017-11-10 16:19:52 -08003230TEST_P(PeerConnectionIntegrationIceStatesTest, VerifyIceStates) {
3231 // TODO(bugs.webrtc.org/8295): When using a ScopedFakeClock, this test will
3232 // sometimes hit a DCHECK in platform_thread.cc about the PacerThread being
3233 // too busy. For now, revert to running without a fake clock.
Steve Antonede9ca52017-10-16 13:04:27 -07003234
3235 const SocketAddress kStunServerAddress =
3236 SocketAddress("99.99.99.1", cricket::STUN_SERVER_PORT);
3237 StartStunServer(kStunServerAddress);
3238
3239 PeerConnectionInterface::RTCConfiguration config;
3240 PeerConnectionInterface::IceServer ice_stun_server;
3241 ice_stun_server.urls.push_back(
3242 "stun:" + kStunServerAddress.HostAsURIString() + ":" +
3243 kStunServerAddress.PortAsString());
3244 config.servers.push_back(ice_stun_server);
3245
3246 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
3247 ConnectFakeSignaling();
3248 SetPortAllocatorFlags();
3249 SetUpNetworkInterfaces();
Steve Anton15324772018-01-16 10:26:49 -08003250 caller()->AddAudioVideoTracks();
3251 callee()->AddAudioVideoTracks();
Steve Antonede9ca52017-10-16 13:04:27 -07003252
3253 // Initial state before anything happens.
3254 ASSERT_EQ(PeerConnectionInterface::kIceGatheringNew,
3255 caller()->ice_gathering_state());
3256 ASSERT_EQ(PeerConnectionInterface::kIceConnectionNew,
3257 caller()->ice_connection_state());
3258
3259 // Start the call by creating the offer, setting it as the local description,
3260 // then sending it to the peer who will respond with an answer. This happens
3261 // asynchronously so that we can watch the states as it runs in the
3262 // background.
3263 caller()->CreateAndSetAndSignalOffer();
3264
Steve Anton83119dd2017-11-10 16:19:52 -08003265 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceConnectionCompleted,
3266 caller()->ice_connection_state(), kDefaultTimeout);
Steve Antonede9ca52017-10-16 13:04:27 -07003267
3268 // Verify that the observer was notified of the intermediate transitions.
3269 EXPECT_THAT(caller()->ice_connection_state_history(),
3270 ElementsAre(PeerConnectionInterface::kIceConnectionChecking,
3271 PeerConnectionInterface::kIceConnectionConnected,
3272 PeerConnectionInterface::kIceConnectionCompleted));
3273 EXPECT_THAT(caller()->ice_gathering_state_history(),
3274 ElementsAre(PeerConnectionInterface::kIceGatheringGathering,
3275 PeerConnectionInterface::kIceGatheringComplete));
3276
3277 // Block connections to/from the caller and wait for ICE to become
3278 // disconnected.
3279 for (const auto& caller_address : CallerAddresses()) {
3280 firewall()->AddRule(false, rtc::FP_ANY, rtc::FD_ANY, caller_address);
3281 }
Mirko Bonadei675513b2017-11-09 11:09:25 +01003282 RTC_LOG(LS_INFO) << "Firewall rules applied";
Steve Anton83119dd2017-11-10 16:19:52 -08003283 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceConnectionDisconnected,
3284 caller()->ice_connection_state(), kDefaultTimeout);
Steve Antonede9ca52017-10-16 13:04:27 -07003285
3286 // Let ICE re-establish by removing the firewall rules.
3287 firewall()->ClearRules();
Mirko Bonadei675513b2017-11-09 11:09:25 +01003288 RTC_LOG(LS_INFO) << "Firewall rules cleared";
Steve Anton83119dd2017-11-10 16:19:52 -08003289 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceConnectionCompleted,
3290 caller()->ice_connection_state(), kDefaultTimeout);
Steve Antonede9ca52017-10-16 13:04:27 -07003291
3292 // According to RFC7675, if there is no response within 30 seconds then the
3293 // peer should consider the other side to have rejected the connection. This
Steve Anton83119dd2017-11-10 16:19:52 -08003294 // is signaled by the state transitioning to "failed".
Steve Antonede9ca52017-10-16 13:04:27 -07003295 constexpr int kConsentTimeout = 30000;
3296 for (const auto& caller_address : CallerAddresses()) {
3297 firewall()->AddRule(false, rtc::FP_ANY, rtc::FD_ANY, caller_address);
3298 }
Mirko Bonadei675513b2017-11-09 11:09:25 +01003299 RTC_LOG(LS_INFO) << "Firewall rules applied again";
Steve Anton83119dd2017-11-10 16:19:52 -08003300 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceConnectionFailed,
3301 caller()->ice_connection_state(), kConsentTimeout);
Steve Antonede9ca52017-10-16 13:04:27 -07003302}
3303
3304// Tests that the best connection is set to the appropriate IPv4/IPv6 connection
3305// and that the statistics in the metric observers are updated correctly.
3306TEST_P(PeerConnectionIntegrationIceStatesTest, VerifyBestConnection) {
3307 ASSERT_TRUE(CreatePeerConnectionWrappers());
3308 ConnectFakeSignaling();
3309 SetPortAllocatorFlags();
3310 SetUpNetworkInterfaces();
Steve Anton15324772018-01-16 10:26:49 -08003311 caller()->AddAudioVideoTracks();
3312 callee()->AddAudioVideoTracks();
Steve Antonede9ca52017-10-16 13:04:27 -07003313
3314 rtc::scoped_refptr<webrtc::FakeMetricsObserver> metrics_observer(
3315 new rtc::RefCountedObject<webrtc::FakeMetricsObserver>());
3316 caller()->pc()->RegisterUMAObserver(metrics_observer.get());
3317
3318 caller()->CreateAndSetAndSignalOffer();
3319
3320 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3321
3322 const int num_best_ipv4 = metrics_observer->GetEnumCounter(
3323 webrtc::kEnumCounterAddressFamily, webrtc::kBestConnections_IPv4);
3324 const int num_best_ipv6 = metrics_observer->GetEnumCounter(
3325 webrtc::kEnumCounterAddressFamily, webrtc::kBestConnections_IPv6);
3326 if (TestIPv6()) {
3327 // When IPv6 is enabled, we should prefer an IPv6 connection over an IPv4
3328 // connection.
3329 EXPECT_EQ(0u, num_best_ipv4);
3330 EXPECT_EQ(1u, num_best_ipv6);
3331 } else {
3332 EXPECT_EQ(1u, num_best_ipv4);
3333 EXPECT_EQ(0u, num_best_ipv6);
3334 }
3335
3336 EXPECT_EQ(0u, metrics_observer->GetEnumCounter(
3337 webrtc::kEnumCounterIceCandidatePairTypeUdp,
3338 webrtc::kIceCandidatePairHostHost));
3339 EXPECT_EQ(1u, metrics_observer->GetEnumCounter(
3340 webrtc::kEnumCounterIceCandidatePairTypeUdp,
3341 webrtc::kIceCandidatePairHostPublicHostPublic));
3342}
3343
3344constexpr uint32_t kFlagsIPv4NoStun = cricket::PORTALLOCATOR_DISABLE_TCP |
3345 cricket::PORTALLOCATOR_DISABLE_STUN |
3346 cricket::PORTALLOCATOR_DISABLE_RELAY;
3347constexpr uint32_t kFlagsIPv6NoStun =
3348 cricket::PORTALLOCATOR_DISABLE_TCP | cricket::PORTALLOCATOR_DISABLE_STUN |
3349 cricket::PORTALLOCATOR_ENABLE_IPV6 | cricket::PORTALLOCATOR_DISABLE_RELAY;
3350constexpr uint32_t kFlagsIPv4Stun =
3351 cricket::PORTALLOCATOR_DISABLE_TCP | cricket::PORTALLOCATOR_DISABLE_RELAY;
3352
Seth Hampson2f0d7022018-02-20 11:54:42 -08003353INSTANTIATE_TEST_CASE_P(
3354 PeerConnectionIntegrationTest,
3355 PeerConnectionIntegrationIceStatesTest,
3356 Combine(Values(SdpSemantics::kPlanB, SdpSemantics::kUnifiedPlan),
3357 Values(std::make_pair("IPv4 no STUN", kFlagsIPv4NoStun),
3358 std::make_pair("IPv6 no STUN", kFlagsIPv6NoStun),
3359 std::make_pair("IPv4 with STUN", kFlagsIPv4Stun))));
Steve Antonede9ca52017-10-16 13:04:27 -07003360
deadbeef1dcb1642017-03-29 21:08:16 -07003361// This test sets up a call between two parties with audio and video.
3362// During the call, the caller restarts ICE and the test verifies that
3363// new ICE candidates are generated and audio and video still can flow, and the
3364// ICE state reaches completed again.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003365TEST_P(PeerConnectionIntegrationTest, MediaContinuesFlowingAfterIceRestart) {
deadbeef1dcb1642017-03-29 21:08:16 -07003366 ASSERT_TRUE(CreatePeerConnectionWrappers());
3367 ConnectFakeSignaling();
3368 // Do normal offer/answer and wait for ICE to complete.
Steve Anton15324772018-01-16 10:26:49 -08003369 caller()->AddAudioVideoTracks();
3370 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003371 caller()->CreateAndSetAndSignalOffer();
3372 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3373 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
3374 caller()->ice_connection_state(), kMaxWaitForFramesMs);
3375 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
3376 callee()->ice_connection_state(), kMaxWaitForFramesMs);
3377
3378 // To verify that the ICE restart actually occurs, get
3379 // ufrag/password/candidates before and after restart.
3380 // Create an SDP string of the first audio candidate for both clients.
3381 const webrtc::IceCandidateCollection* audio_candidates_caller =
3382 caller()->pc()->local_description()->candidates(0);
3383 const webrtc::IceCandidateCollection* audio_candidates_callee =
3384 callee()->pc()->local_description()->candidates(0);
3385 ASSERT_GT(audio_candidates_caller->count(), 0u);
3386 ASSERT_GT(audio_candidates_callee->count(), 0u);
3387 std::string caller_candidate_pre_restart;
3388 ASSERT_TRUE(
3389 audio_candidates_caller->at(0)->ToString(&caller_candidate_pre_restart));
3390 std::string callee_candidate_pre_restart;
3391 ASSERT_TRUE(
3392 audio_candidates_callee->at(0)->ToString(&callee_candidate_pre_restart));
3393 const cricket::SessionDescription* desc =
3394 caller()->pc()->local_description()->description();
3395 std::string caller_ufrag_pre_restart =
3396 desc->transport_infos()[0].description.ice_ufrag;
3397 desc = callee()->pc()->local_description()->description();
3398 std::string callee_ufrag_pre_restart =
3399 desc->transport_infos()[0].description.ice_ufrag;
3400
3401 // Have the caller initiate an ICE restart.
3402 caller()->SetOfferAnswerOptions(IceRestartOfferAnswerOptions());
3403 caller()->CreateAndSetAndSignalOffer();
3404 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3405 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
3406 caller()->ice_connection_state(), kMaxWaitForFramesMs);
3407 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
3408 callee()->ice_connection_state(), kMaxWaitForFramesMs);
3409
3410 // Grab the ufrags/candidates again.
3411 audio_candidates_caller = caller()->pc()->local_description()->candidates(0);
3412 audio_candidates_callee = callee()->pc()->local_description()->candidates(0);
3413 ASSERT_GT(audio_candidates_caller->count(), 0u);
3414 ASSERT_GT(audio_candidates_callee->count(), 0u);
3415 std::string caller_candidate_post_restart;
3416 ASSERT_TRUE(
3417 audio_candidates_caller->at(0)->ToString(&caller_candidate_post_restart));
3418 std::string callee_candidate_post_restart;
3419 ASSERT_TRUE(
3420 audio_candidates_callee->at(0)->ToString(&callee_candidate_post_restart));
3421 desc = caller()->pc()->local_description()->description();
3422 std::string caller_ufrag_post_restart =
3423 desc->transport_infos()[0].description.ice_ufrag;
3424 desc = callee()->pc()->local_description()->description();
3425 std::string callee_ufrag_post_restart =
3426 desc->transport_infos()[0].description.ice_ufrag;
3427 // Sanity check that an ICE restart was actually negotiated in SDP.
3428 ASSERT_NE(caller_candidate_pre_restart, caller_candidate_post_restart);
3429 ASSERT_NE(callee_candidate_pre_restart, callee_candidate_post_restart);
3430 ASSERT_NE(caller_ufrag_pre_restart, caller_ufrag_post_restart);
3431 ASSERT_NE(callee_ufrag_pre_restart, callee_ufrag_post_restart);
3432
3433 // Ensure that additional frames are received after the ICE restart.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003434 MediaExpectations media_expectations;
3435 media_expectations.ExpectBidirectionalAudioAndVideo();
3436 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003437}
3438
3439// Verify that audio/video can be received end-to-end when ICE renomination is
3440// enabled.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003441TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithIceRenomination) {
deadbeef1dcb1642017-03-29 21:08:16 -07003442 PeerConnectionInterface::RTCConfiguration config;
3443 config.enable_ice_renomination = true;
3444 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
3445 ConnectFakeSignaling();
3446 // Do normal offer/answer and wait for some frames to be received in each
3447 // direction.
Steve Anton15324772018-01-16 10:26:49 -08003448 caller()->AddAudioVideoTracks();
3449 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003450 caller()->CreateAndSetAndSignalOffer();
3451 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3452 // Sanity check that ICE renomination was actually negotiated.
3453 const cricket::SessionDescription* desc =
3454 caller()->pc()->local_description()->description();
3455 for (const cricket::TransportInfo& info : desc->transport_infos()) {
deadbeef30952b42017-04-21 02:41:29 -07003456 ASSERT_NE(
3457 info.description.transport_options.end(),
3458 std::find(info.description.transport_options.begin(),
3459 info.description.transport_options.end(), "renomination"));
deadbeef1dcb1642017-03-29 21:08:16 -07003460 }
3461 desc = callee()->pc()->local_description()->description();
3462 for (const cricket::TransportInfo& info : desc->transport_infos()) {
deadbeef30952b42017-04-21 02:41:29 -07003463 ASSERT_NE(
3464 info.description.transport_options.end(),
3465 std::find(info.description.transport_options.begin(),
3466 info.description.transport_options.end(), "renomination"));
deadbeef1dcb1642017-03-29 21:08:16 -07003467 }
Seth Hampson2f0d7022018-02-20 11:54:42 -08003468 MediaExpectations media_expectations;
3469 media_expectations.ExpectBidirectionalAudioAndVideo();
3470 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003471}
3472
Steve Anton6f25b092017-10-23 09:39:20 -07003473// With a max bundle policy and RTCP muxing, adding a new media description to
3474// the connection should not affect ICE at all because the new media will use
3475// the existing connection.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003476TEST_P(PeerConnectionIntegrationTest,
Steve Anton83119dd2017-11-10 16:19:52 -08003477 AddMediaToConnectedBundleDoesNotRestartIce) {
Steve Anton6f25b092017-10-23 09:39:20 -07003478 PeerConnectionInterface::RTCConfiguration config;
3479 config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
3480 config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyRequire;
3481 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(
3482 config, PeerConnectionInterface::RTCConfiguration()));
3483 ConnectFakeSignaling();
3484
Steve Anton15324772018-01-16 10:26:49 -08003485 caller()->AddAudioTrack();
Steve Anton6f25b092017-10-23 09:39:20 -07003486 caller()->CreateAndSetAndSignalOffer();
3487 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Steve Antonff52f1b2017-10-26 12:24:50 -07003488 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceConnectionCompleted,
3489 caller()->ice_connection_state(), kDefaultTimeout);
Steve Anton6f25b092017-10-23 09:39:20 -07003490
3491 caller()->clear_ice_connection_state_history();
3492
Steve Anton15324772018-01-16 10:26:49 -08003493 caller()->AddVideoTrack();
Steve Anton6f25b092017-10-23 09:39:20 -07003494 caller()->CreateAndSetAndSignalOffer();
3495 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3496
3497 EXPECT_EQ(0u, caller()->ice_connection_state_history().size());
3498}
3499
deadbeef1dcb1642017-03-29 21:08:16 -07003500// This test sets up a call between two parties with audio and video. It then
3501// renegotiates setting the video m-line to "port 0", then later renegotiates
3502// again, enabling video.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003503TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07003504 VideoFlowsAfterMediaSectionIsRejectedAndRecycled) {
3505 ASSERT_TRUE(CreatePeerConnectionWrappers());
3506 ConnectFakeSignaling();
3507
3508 // Do initial negotiation, only sending media from the caller. Will result in
3509 // video and audio recvonly "m=" sections.
Steve Anton15324772018-01-16 10:26:49 -08003510 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003511 caller()->CreateAndSetAndSignalOffer();
3512 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3513
3514 // Negotiate again, disabling the video "m=" section (the callee will set the
3515 // port to 0 due to offer_to_receive_video = 0).
Seth Hampson2f0d7022018-02-20 11:54:42 -08003516 if (sdp_semantics_ == SdpSemantics::kPlanB) {
3517 PeerConnectionInterface::RTCOfferAnswerOptions options;
3518 options.offer_to_receive_video = 0;
3519 callee()->SetOfferAnswerOptions(options);
3520 } else {
3521 callee()->SetRemoteOfferHandler([this] {
3522 callee()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)->Stop();
3523 });
3524 }
deadbeef1dcb1642017-03-29 21:08:16 -07003525 caller()->CreateAndSetAndSignalOffer();
3526 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3527 // Sanity check that video "m=" section was actually rejected.
3528 const ContentInfo* answer_video_content = cricket::GetFirstVideoContent(
3529 callee()->pc()->local_description()->description());
3530 ASSERT_NE(nullptr, answer_video_content);
3531 ASSERT_TRUE(answer_video_content->rejected);
3532
3533 // Enable video and do negotiation again, making sure video is received
3534 // end-to-end, also adding media stream to callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003535 if (sdp_semantics_ == SdpSemantics::kPlanB) {
3536 PeerConnectionInterface::RTCOfferAnswerOptions options;
3537 options.offer_to_receive_video = 1;
3538 callee()->SetOfferAnswerOptions(options);
3539 } else {
3540 // The caller's transceiver is stopped, so we need to add another track.
3541 auto caller_transceiver =
3542 caller()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO);
3543 EXPECT_TRUE(caller_transceiver->stopped());
3544 caller()->AddVideoTrack();
3545 }
3546 callee()->AddVideoTrack();
3547 callee()->SetRemoteOfferHandler(nullptr);
deadbeef1dcb1642017-03-29 21:08:16 -07003548 caller()->CreateAndSetAndSignalOffer();
3549 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08003550
deadbeef1dcb1642017-03-29 21:08:16 -07003551 // Verify the caller receives frames from the newly added stream, and the
3552 // callee receives additional frames from the re-enabled video m= section.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003553 MediaExpectations media_expectations;
3554 media_expectations.CalleeExpectsSomeAudio();
3555 media_expectations.ExpectBidirectionalVideo();
3556 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003557}
3558
3559// This test sets up a Jsep call between two parties with external
3560// VideoDecoderFactory.
3561// TODO(holmer): Disabled due to sometimes crashing on buildbots.
3562// See issue webrtc/2378.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003563TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07003564 DISABLED_EndToEndCallWithVideoDecoderFactory) {
3565 ASSERT_TRUE(CreatePeerConnectionWrappers());
3566 EnableVideoDecoderFactory();
3567 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08003568 caller()->AddAudioVideoTracks();
3569 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003570 caller()->CreateAndSetAndSignalOffer();
3571 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08003572 MediaExpectations media_expectations;
3573 media_expectations.ExpectBidirectionalAudioAndVideo();
3574 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003575}
3576
3577// This tests that if we negotiate after calling CreateSender but before we
3578// have a track, then set a track later, frames from the newly-set track are
3579// received end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003580TEST_F(PeerConnectionIntegrationTestPlanB,
deadbeef1dcb1642017-03-29 21:08:16 -07003581 MediaFlowsAfterEarlyWarmupWithCreateSender) {
3582 ASSERT_TRUE(CreatePeerConnectionWrappers());
3583 ConnectFakeSignaling();
3584 auto caller_audio_sender =
3585 caller()->pc()->CreateSender("audio", "caller_stream");
3586 auto caller_video_sender =
3587 caller()->pc()->CreateSender("video", "caller_stream");
3588 auto callee_audio_sender =
3589 callee()->pc()->CreateSender("audio", "callee_stream");
3590 auto callee_video_sender =
3591 callee()->pc()->CreateSender("video", "callee_stream");
3592 caller()->CreateAndSetAndSignalOffer();
3593 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
3594 // Wait for ICE to complete, without any tracks being set.
3595 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
3596 caller()->ice_connection_state(), kMaxWaitForFramesMs);
3597 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
3598 callee()->ice_connection_state(), kMaxWaitForFramesMs);
3599 // Now set the tracks, and expect frames to immediately start flowing.
3600 EXPECT_TRUE(caller_audio_sender->SetTrack(caller()->CreateLocalAudioTrack()));
3601 EXPECT_TRUE(caller_video_sender->SetTrack(caller()->CreateLocalVideoTrack()));
3602 EXPECT_TRUE(callee_audio_sender->SetTrack(callee()->CreateLocalAudioTrack()));
3603 EXPECT_TRUE(callee_video_sender->SetTrack(callee()->CreateLocalVideoTrack()));
Seth Hampson2f0d7022018-02-20 11:54:42 -08003604 MediaExpectations media_expectations;
3605 media_expectations.ExpectBidirectionalAudioAndVideo();
3606 ASSERT_TRUE(ExpectNewFrames(media_expectations));
3607}
3608
3609// This tests that if we negotiate after calling AddTransceiver but before we
3610// have a track, then set a track later, frames from the newly-set tracks are
3611// received end-to-end.
3612TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
3613 MediaFlowsAfterEarlyWarmupWithAddTransceiver) {
3614 ASSERT_TRUE(CreatePeerConnectionWrappers());
3615 ConnectFakeSignaling();
3616 auto audio_result = caller()->pc()->AddTransceiver(cricket::MEDIA_TYPE_AUDIO);
3617 ASSERT_EQ(RTCErrorType::NONE, audio_result.error().type());
3618 auto caller_audio_sender = audio_result.MoveValue()->sender();
3619 auto video_result = caller()->pc()->AddTransceiver(cricket::MEDIA_TYPE_VIDEO);
3620 ASSERT_EQ(RTCErrorType::NONE, video_result.error().type());
3621 auto caller_video_sender = video_result.MoveValue()->sender();
3622 callee()->SetRemoteOfferHandler([this] {
3623 ASSERT_EQ(2u, callee()->pc()->GetTransceivers().size());
3624 callee()->pc()->GetTransceivers()[0]->SetDirection(
3625 RtpTransceiverDirection::kSendRecv);
3626 callee()->pc()->GetTransceivers()[1]->SetDirection(
3627 RtpTransceiverDirection::kSendRecv);
3628 });
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 auto callee_audio_sender = callee()->pc()->GetSenders()[0];
3638 auto callee_video_sender = callee()->pc()->GetSenders()[1];
3639 ASSERT_TRUE(caller_audio_sender->SetTrack(caller()->CreateLocalAudioTrack()));
3640 ASSERT_TRUE(caller_video_sender->SetTrack(caller()->CreateLocalVideoTrack()));
3641 ASSERT_TRUE(callee_audio_sender->SetTrack(callee()->CreateLocalAudioTrack()));
3642 ASSERT_TRUE(callee_video_sender->SetTrack(callee()->CreateLocalVideoTrack()));
3643 MediaExpectations media_expectations;
3644 media_expectations.ExpectBidirectionalAudioAndVideo();
3645 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003646}
3647
3648// This test verifies that a remote video track can be added via AddStream,
3649// and sent end-to-end. For this particular test, it's simply echoed back
3650// from the caller to the callee, rather than being forwarded to a third
3651// PeerConnection.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003652TEST_F(PeerConnectionIntegrationTestPlanB, CanSendRemoteVideoTrack) {
deadbeef1dcb1642017-03-29 21:08:16 -07003653 ASSERT_TRUE(CreatePeerConnectionWrappers());
3654 ConnectFakeSignaling();
3655 // Just send a video track from the caller.
Steve Anton15324772018-01-16 10:26:49 -08003656 caller()->AddVideoTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07003657 caller()->CreateAndSetAndSignalOffer();
3658 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
3659 ASSERT_EQ(1, callee()->remote_streams()->count());
3660
3661 // Echo the stream back, and do a new offer/anwer (initiated by callee this
3662 // time).
3663 callee()->pc()->AddStream(callee()->remote_streams()->at(0));
3664 callee()->CreateAndSetAndSignalOffer();
3665 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
3666
Seth Hampson2f0d7022018-02-20 11:54:42 -08003667 MediaExpectations media_expectations;
3668 media_expectations.ExpectBidirectionalVideo();
3669 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003670}
3671
3672// Test that we achieve the expected end-to-end connection time, using a
3673// fake clock and simulated latency on the media and signaling paths.
3674// We use a TURN<->TURN connection because this is usually the quickest to
3675// set up initially, especially when we're confident the connection will work
3676// and can start sending media before we get a STUN response.
3677//
3678// With various optimizations enabled, here are the network delays we expect to
3679// be on the critical path:
3680// 1. 2 signaling trips: Signaling offer and offerer's TURN candidate, then
3681// signaling answer (with DTLS fingerprint).
3682// 2. 9 media hops: Rest of the DTLS handshake. 3 hops in each direction when
3683// using TURN<->TURN pair, and DTLS exchange is 4 packets,
3684// the first of which should have arrived before the answer.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003685TEST_P(PeerConnectionIntegrationTest, EndToEndConnectionTimeWithTurnTurnPair) {
deadbeef1dcb1642017-03-29 21:08:16 -07003686 rtc::ScopedFakeClock fake_clock;
3687 // Some things use a time of "0" as a special value, so we need to start out
3688 // the fake clock at a nonzero time.
3689 // TODO(deadbeef): Fix this.
3690 fake_clock.AdvanceTime(rtc::TimeDelta::FromSeconds(1));
3691
3692 static constexpr int media_hop_delay_ms = 50;
3693 static constexpr int signaling_trip_delay_ms = 500;
3694 // For explanation of these values, see comment above.
3695 static constexpr int required_media_hops = 9;
3696 static constexpr int required_signaling_trips = 2;
3697 // For internal delays (such as posting an event asychronously).
3698 static constexpr int allowed_internal_delay_ms = 20;
3699 static constexpr int total_connection_time_ms =
3700 media_hop_delay_ms * required_media_hops +
3701 signaling_trip_delay_ms * required_signaling_trips +
3702 allowed_internal_delay_ms;
3703
3704 static const rtc::SocketAddress turn_server_1_internal_address{"88.88.88.0",
3705 3478};
3706 static const rtc::SocketAddress turn_server_1_external_address{"88.88.88.1",
3707 0};
3708 static const rtc::SocketAddress turn_server_2_internal_address{"99.99.99.0",
3709 3478};
3710 static const rtc::SocketAddress turn_server_2_external_address{"99.99.99.1",
3711 0};
3712 cricket::TestTurnServer turn_server_1(network_thread(),
3713 turn_server_1_internal_address,
3714 turn_server_1_external_address);
3715 cricket::TestTurnServer turn_server_2(network_thread(),
3716 turn_server_2_internal_address,
3717 turn_server_2_external_address);
Jonas Orelandbdcee282017-10-10 14:01:40 +02003718
deadbeef1dcb1642017-03-29 21:08:16 -07003719 // Bypass permission check on received packets so media can be sent before
3720 // the candidate is signaled.
3721 turn_server_1.set_enable_permission_checks(false);
3722 turn_server_2.set_enable_permission_checks(false);
3723
3724 PeerConnectionInterface::RTCConfiguration client_1_config;
3725 webrtc::PeerConnectionInterface::IceServer ice_server_1;
3726 ice_server_1.urls.push_back("turn:88.88.88.0:3478");
3727 ice_server_1.username = "test";
3728 ice_server_1.password = "test";
3729 client_1_config.servers.push_back(ice_server_1);
3730 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
3731 client_1_config.presume_writable_when_fully_relayed = true;
3732
3733 PeerConnectionInterface::RTCConfiguration client_2_config;
3734 webrtc::PeerConnectionInterface::IceServer ice_server_2;
3735 ice_server_2.urls.push_back("turn:99.99.99.0:3478");
3736 ice_server_2.username = "test";
3737 ice_server_2.password = "test";
3738 client_2_config.servers.push_back(ice_server_2);
3739 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
3740 client_2_config.presume_writable_when_fully_relayed = true;
3741
3742 ASSERT_TRUE(
3743 CreatePeerConnectionWrappersWithConfig(client_1_config, client_2_config));
3744 // Set up the simulated delays.
3745 SetSignalingDelayMs(signaling_trip_delay_ms);
3746 ConnectFakeSignaling();
3747 virtual_socket_server()->set_delay_mean(media_hop_delay_ms);
3748 virtual_socket_server()->UpdateDelayDistribution();
3749
3750 // Set "offer to receive audio/video" without adding any tracks, so we just
3751 // set up ICE/DTLS with no media.
3752 PeerConnectionInterface::RTCOfferAnswerOptions options;
3753 options.offer_to_receive_audio = 1;
3754 options.offer_to_receive_video = 1;
3755 caller()->SetOfferAnswerOptions(options);
3756 caller()->CreateAndSetAndSignalOffer();
deadbeef71452802017-05-07 17:21:01 -07003757 EXPECT_TRUE_SIMULATED_WAIT(DtlsConnected(), total_connection_time_ms,
3758 fake_clock);
deadbeef1dcb1642017-03-29 21:08:16 -07003759 // Need to free the clients here since they're using things we created on
3760 // the stack.
3761 delete SetCallerPcWrapperAndReturnCurrent(nullptr);
3762 delete SetCalleePcWrapperAndReturnCurrent(nullptr);
3763}
3764
Jonas Orelandbdcee282017-10-10 14:01:40 +02003765// Verify that a TurnCustomizer passed in through RTCConfiguration
3766// is actually used by the underlying TURN candidate pair.
3767// Note that turnport_unittest.cc contains more detailed, lower-level tests.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003768TEST_P(PeerConnectionIntegrationTest, TurnCustomizerUsedForTurnConnections) {
Jonas Orelandbdcee282017-10-10 14:01:40 +02003769 static const rtc::SocketAddress turn_server_1_internal_address{"88.88.88.0",
3770 3478};
3771 static const rtc::SocketAddress turn_server_1_external_address{"88.88.88.1",
3772 0};
3773 static const rtc::SocketAddress turn_server_2_internal_address{"99.99.99.0",
3774 3478};
3775 static const rtc::SocketAddress turn_server_2_external_address{"99.99.99.1",
3776 0};
3777 cricket::TestTurnServer turn_server_1(network_thread(),
3778 turn_server_1_internal_address,
3779 turn_server_1_external_address);
3780 cricket::TestTurnServer turn_server_2(network_thread(),
3781 turn_server_2_internal_address,
3782 turn_server_2_external_address);
3783
3784 PeerConnectionInterface::RTCConfiguration client_1_config;
3785 webrtc::PeerConnectionInterface::IceServer ice_server_1;
3786 ice_server_1.urls.push_back("turn:88.88.88.0:3478");
3787 ice_server_1.username = "test";
3788 ice_server_1.password = "test";
3789 client_1_config.servers.push_back(ice_server_1);
3790 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
3791 auto customizer1 = rtc::MakeUnique<cricket::TestTurnCustomizer>();
3792 client_1_config.turn_customizer = customizer1.get();
3793
3794 PeerConnectionInterface::RTCConfiguration client_2_config;
3795 webrtc::PeerConnectionInterface::IceServer ice_server_2;
3796 ice_server_2.urls.push_back("turn:99.99.99.0:3478");
3797 ice_server_2.username = "test";
3798 ice_server_2.password = "test";
3799 client_2_config.servers.push_back(ice_server_2);
3800 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
3801 auto customizer2 = rtc::MakeUnique<cricket::TestTurnCustomizer>();
3802 client_2_config.turn_customizer = customizer2.get();
3803
3804 ASSERT_TRUE(
3805 CreatePeerConnectionWrappersWithConfig(client_1_config, client_2_config));
3806 ConnectFakeSignaling();
3807
3808 // Set "offer to receive audio/video" without adding any tracks, so we just
3809 // set up ICE/DTLS with no media.
3810 PeerConnectionInterface::RTCOfferAnswerOptions options;
3811 options.offer_to_receive_audio = 1;
3812 options.offer_to_receive_video = 1;
3813 caller()->SetOfferAnswerOptions(options);
3814 caller()->CreateAndSetAndSignalOffer();
3815 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
3816
3817 EXPECT_GT(customizer1->allow_channel_data_cnt_, 0u);
3818 EXPECT_GT(customizer1->modify_cnt_, 0u);
3819
3820 EXPECT_GT(customizer2->allow_channel_data_cnt_, 0u);
3821 EXPECT_GT(customizer2->modify_cnt_, 0u);
3822
3823 // Need to free the clients here since they're using things we created on
3824 // the stack.
3825 delete SetCallerPcWrapperAndReturnCurrent(nullptr);
3826 delete SetCalleePcWrapperAndReturnCurrent(nullptr);
3827}
3828
deadbeefc964d0b2017-04-03 10:03:35 -07003829// Test that audio and video flow end-to-end when codec names don't use the
3830// expected casing, given that they're supposed to be case insensitive. To test
3831// this, all but one codec is removed from each media description, and its
3832// casing is changed.
3833//
3834// In the past, this has regressed and caused crashes/black video, due to the
3835// fact that code at some layers was doing case-insensitive comparisons and
3836// code at other layers was not.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003837TEST_P(PeerConnectionIntegrationTest, CodecNamesAreCaseInsensitive) {
deadbeefc964d0b2017-04-03 10:03:35 -07003838 ASSERT_TRUE(CreatePeerConnectionWrappers());
3839 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08003840 caller()->AddAudioVideoTracks();
3841 callee()->AddAudioVideoTracks();
deadbeefc964d0b2017-04-03 10:03:35 -07003842
3843 // Remove all but one audio/video codec (opus and VP8), and change the
3844 // casing of the caller's generated offer.
3845 caller()->SetGeneratedSdpMunger([](cricket::SessionDescription* description) {
3846 cricket::AudioContentDescription* audio =
3847 GetFirstAudioContentDescription(description);
3848 ASSERT_NE(nullptr, audio);
3849 auto audio_codecs = audio->codecs();
3850 audio_codecs.erase(std::remove_if(audio_codecs.begin(), audio_codecs.end(),
3851 [](const cricket::AudioCodec& codec) {
3852 return codec.name != "opus";
3853 }),
3854 audio_codecs.end());
3855 ASSERT_EQ(1u, audio_codecs.size());
3856 audio_codecs[0].name = "OpUs";
3857 audio->set_codecs(audio_codecs);
3858
3859 cricket::VideoContentDescription* video =
3860 GetFirstVideoContentDescription(description);
3861 ASSERT_NE(nullptr, video);
3862 auto video_codecs = video->codecs();
3863 video_codecs.erase(std::remove_if(video_codecs.begin(), video_codecs.end(),
3864 [](const cricket::VideoCodec& codec) {
3865 return codec.name != "VP8";
3866 }),
3867 video_codecs.end());
3868 ASSERT_EQ(1u, video_codecs.size());
3869 video_codecs[0].name = "vP8";
3870 video->set_codecs(video_codecs);
3871 });
3872
3873 caller()->CreateAndSetAndSignalOffer();
3874 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3875
3876 // Verify frames are still received end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003877 MediaExpectations media_expectations;
3878 media_expectations.ExpectBidirectionalAudioAndVideo();
3879 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeefc964d0b2017-04-03 10:03:35 -07003880}
3881
Seth Hampson2f0d7022018-02-20 11:54:42 -08003882TEST_P(PeerConnectionIntegrationTest, GetSources) {
hbos8d609f62017-04-10 07:39:05 -07003883 ASSERT_TRUE(CreatePeerConnectionWrappers());
3884 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08003885 caller()->AddAudioTrack();
hbos8d609f62017-04-10 07:39:05 -07003886 caller()->CreateAndSetAndSignalOffer();
3887 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
deadbeefd8ad7882017-04-18 16:01:17 -07003888 // Wait for one audio frame to be received by the callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003889 MediaExpectations media_expectations;
3890 media_expectations.CalleeExpectsSomeAudio(1);
3891 ASSERT_TRUE(ExpectNewFrames(media_expectations));
hbos8d609f62017-04-10 07:39:05 -07003892 ASSERT_GT(callee()->pc()->GetReceivers().size(), 0u);
3893 auto receiver = callee()->pc()->GetReceivers()[0];
3894 ASSERT_EQ(receiver->media_type(), cricket::MEDIA_TYPE_AUDIO);
3895
3896 auto contributing_sources = receiver->GetSources();
3897 ASSERT_GT(receiver->GetParameters().encodings.size(), 0u);
3898 EXPECT_EQ(receiver->GetParameters().encodings[0].ssrc,
3899 contributing_sources[0].source_id());
3900}
3901
deadbeef2f425aa2017-04-14 10:41:32 -07003902// Test that if a track is removed and added again with a different stream ID,
3903// the new stream ID is successfully communicated in SDP and media continues to
3904// flow end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003905// TODO(webrtc.bugs.org/8734): This test does not work for Unified Plan because
3906// it will not reuse a transceiver that has already been sending. After creating
3907// a new transceiver it tries to create an offer with two senders of the same
3908// track ids and it fails.
3909TEST_F(PeerConnectionIntegrationTestPlanB, RemoveAndAddTrackWithNewStreamId) {
deadbeef2f425aa2017-04-14 10:41:32 -07003910 ASSERT_TRUE(CreatePeerConnectionWrappers());
3911 ConnectFakeSignaling();
3912
3913 rtc::scoped_refptr<MediaStreamInterface> stream_1 =
3914 caller()->pc_factory()->CreateLocalMediaStream("stream_1");
3915 rtc::scoped_refptr<MediaStreamInterface> stream_2 =
3916 caller()->pc_factory()->CreateLocalMediaStream("stream_2");
3917
3918 // Add track using stream 1, do offer/answer.
3919 rtc::scoped_refptr<webrtc::AudioTrackInterface> track =
3920 caller()->CreateLocalAudioTrack();
3921 rtc::scoped_refptr<webrtc::RtpSenderInterface> sender =
3922 caller()->pc()->AddTrack(track, {stream_1.get()});
3923 caller()->CreateAndSetAndSignalOffer();
3924 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08003925 {
3926 MediaExpectations media_expectations;
3927 media_expectations.CalleeExpectsSomeAudio(1);
3928 ASSERT_TRUE(ExpectNewFrames(media_expectations));
3929 }
deadbeef2f425aa2017-04-14 10:41:32 -07003930 // Remove the sender, and create a new one with the new stream.
3931 caller()->pc()->RemoveTrack(sender);
3932 sender = caller()->pc()->AddTrack(track, {stream_2.get()});
3933 caller()->CreateAndSetAndSignalOffer();
3934 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3935 // Wait for additional audio frames to be received by the callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003936 {
3937 MediaExpectations media_expectations;
3938 media_expectations.CalleeExpectsSomeAudio();
3939 ASSERT_TRUE(ExpectNewFrames(media_expectations));
3940 }
deadbeef2f425aa2017-04-14 10:41:32 -07003941}
3942
Seth Hampson2f0d7022018-02-20 11:54:42 -08003943TEST_P(PeerConnectionIntegrationTest, RtcEventLogOutputWriteCalled) {
Elad Alon99c3fe52017-10-13 16:29:40 +02003944 ASSERT_TRUE(CreatePeerConnectionWrappers());
3945 ConnectFakeSignaling();
3946
3947 auto output = rtc::MakeUnique<testing::NiceMock<MockRtcEventLogOutput>>();
3948 ON_CALL(*output, IsActive()).WillByDefault(testing::Return(true));
3949 ON_CALL(*output, Write(::testing::_)).WillByDefault(testing::Return(true));
3950 EXPECT_CALL(*output, Write(::testing::_)).Times(::testing::AtLeast(1));
Bjorn Tereliusde939432017-11-20 17:38:14 +01003951 EXPECT_TRUE(caller()->pc()->StartRtcEventLog(
3952 std::move(output), webrtc::RtcEventLog::kImmediateOutput));
Elad Alon99c3fe52017-10-13 16:29:40 +02003953
Steve Anton15324772018-01-16 10:26:49 -08003954 caller()->AddAudioVideoTracks();
Elad Alon99c3fe52017-10-13 16:29:40 +02003955 caller()->CreateAndSetAndSignalOffer();
3956 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3957}
3958
Steve Antonede9ca52017-10-16 13:04:27 -07003959// Test that if candidates are only signaled by applying full session
3960// descriptions (instead of using AddIceCandidate), the peers can connect to
3961// each other and exchange media.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003962TEST_P(PeerConnectionIntegrationTest, MediaFlowsWhenCandidatesSetOnlyInSdp) {
Steve Antonede9ca52017-10-16 13:04:27 -07003963 ASSERT_TRUE(CreatePeerConnectionWrappers());
3964 // Each side will signal the session descriptions but not candidates.
3965 ConnectFakeSignalingForSdpOnly();
3966
3967 // Add audio video track and exchange the initial offer/answer with media
3968 // information only. This will start ICE gathering on each side.
Steve Anton15324772018-01-16 10:26:49 -08003969 caller()->AddAudioVideoTracks();
3970 callee()->AddAudioVideoTracks();
Steve Antonede9ca52017-10-16 13:04:27 -07003971 caller()->CreateAndSetAndSignalOffer();
3972
3973 // Wait for all candidates to be gathered on both the caller and callee.
3974 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceGatheringComplete,
3975 caller()->ice_gathering_state(), kDefaultTimeout);
3976 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceGatheringComplete,
3977 callee()->ice_gathering_state(), kDefaultTimeout);
3978
3979 // The candidates will now be included in the session description, so
3980 // signaling them will start the ICE connection.
3981 caller()->CreateAndSetAndSignalOffer();
3982 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3983
3984 // Ensure that media flows in both directions.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003985 MediaExpectations media_expectations;
3986 media_expectations.ExpectBidirectionalAudioAndVideo();
3987 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Antonede9ca52017-10-16 13:04:27 -07003988}
3989
henrika5f6bf242017-11-01 11:06:56 +01003990// Test that SetAudioPlayout can be used to disable audio playout from the
3991// start, then later enable it. This may be useful, for example, if the caller
3992// needs to play a local ringtone until some event occurs, after which it
3993// switches to playing the received audio.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003994TEST_P(PeerConnectionIntegrationTest, DisableAndEnableAudioPlayout) {
henrika5f6bf242017-11-01 11:06:56 +01003995 ASSERT_TRUE(CreatePeerConnectionWrappers());
3996 ConnectFakeSignaling();
3997
3998 // Set up audio-only call where audio playout is disabled on caller's side.
3999 caller()->pc()->SetAudioPlayout(false);
Steve Anton15324772018-01-16 10:26:49 -08004000 caller()->AddAudioTrack();
4001 callee()->AddAudioTrack();
henrika5f6bf242017-11-01 11:06:56 +01004002 caller()->CreateAndSetAndSignalOffer();
4003 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4004
4005 // Pump messages for a second.
4006 WAIT(false, 1000);
4007 // Since audio playout is disabled, the caller shouldn't have received
4008 // anything (at the playout level, at least).
4009 EXPECT_EQ(0, caller()->audio_frames_received());
4010 // As a sanity check, make sure the callee (for which playout isn't disabled)
4011 // did still see frames on its audio level.
4012 ASSERT_GT(callee()->audio_frames_received(), 0);
4013
4014 // Enable playout again, and ensure audio starts flowing.
4015 caller()->pc()->SetAudioPlayout(true);
Seth Hampson2f0d7022018-02-20 11:54:42 -08004016 MediaExpectations media_expectations;
4017 media_expectations.ExpectBidirectionalAudio();
4018 ASSERT_TRUE(ExpectNewFrames(media_expectations));
henrika5f6bf242017-11-01 11:06:56 +01004019}
4020
4021double GetAudioEnergyStat(PeerConnectionWrapper* pc) {
4022 auto report = pc->NewGetStats();
4023 auto track_stats_list =
4024 report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
4025 const webrtc::RTCMediaStreamTrackStats* remote_track_stats = nullptr;
4026 for (const auto* track_stats : track_stats_list) {
4027 if (track_stats->remote_source.is_defined() &&
4028 *track_stats->remote_source) {
4029 remote_track_stats = track_stats;
4030 break;
4031 }
4032 }
4033
4034 if (!remote_track_stats->total_audio_energy.is_defined()) {
4035 return 0.0;
4036 }
4037 return *remote_track_stats->total_audio_energy;
4038}
4039
4040// Test that if audio playout is disabled via the SetAudioPlayout() method, then
4041// incoming audio is still processed and statistics are generated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004042TEST_P(PeerConnectionIntegrationTest,
henrika5f6bf242017-11-01 11:06:56 +01004043 DisableAudioPlayoutStillGeneratesAudioStats) {
4044 ASSERT_TRUE(CreatePeerConnectionWrappers());
4045 ConnectFakeSignaling();
4046
4047 // Set up audio-only call where playout is disabled but audio-processing is
4048 // still active.
Steve Anton15324772018-01-16 10:26:49 -08004049 caller()->AddAudioTrack();
4050 callee()->AddAudioTrack();
henrika5f6bf242017-11-01 11:06:56 +01004051 caller()->pc()->SetAudioPlayout(false);
4052
4053 caller()->CreateAndSetAndSignalOffer();
4054 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4055
4056 // Wait for the callee to receive audio stats.
4057 EXPECT_TRUE_WAIT(GetAudioEnergyStat(caller()) > 0, kMaxWaitForFramesMs);
4058}
4059
henrika4f167df2017-11-01 14:45:55 +01004060// Test that SetAudioRecording can be used to disable audio recording from the
4061// start, then later enable it. This may be useful, for example, if the caller
4062// wants to ensure that no audio resources are active before a certain state
4063// is reached.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004064TEST_P(PeerConnectionIntegrationTest, DisableAndEnableAudioRecording) {
henrika4f167df2017-11-01 14:45:55 +01004065 ASSERT_TRUE(CreatePeerConnectionWrappers());
4066 ConnectFakeSignaling();
4067
4068 // Set up audio-only call where audio recording is disabled on caller's side.
4069 caller()->pc()->SetAudioRecording(false);
Steve Anton15324772018-01-16 10:26:49 -08004070 caller()->AddAudioTrack();
4071 callee()->AddAudioTrack();
henrika4f167df2017-11-01 14:45:55 +01004072 caller()->CreateAndSetAndSignalOffer();
4073 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4074
4075 // Pump messages for a second.
4076 WAIT(false, 1000);
4077 // Since caller has disabled audio recording, the callee shouldn't have
4078 // received anything.
4079 EXPECT_EQ(0, callee()->audio_frames_received());
4080 // As a sanity check, make sure the caller did still see frames on its
4081 // audio level since audio recording is enabled on the calle side.
4082 ASSERT_GT(caller()->audio_frames_received(), 0);
4083
4084 // Enable audio recording again, and ensure audio starts flowing.
4085 caller()->pc()->SetAudioRecording(true);
Seth Hampson2f0d7022018-02-20 11:54:42 -08004086 MediaExpectations media_expectations;
4087 media_expectations.ExpectBidirectionalAudio();
4088 ASSERT_TRUE(ExpectNewFrames(media_expectations));
henrika4f167df2017-11-01 14:45:55 +01004089}
4090
Taylor Brandstetter389a97c2018-01-03 16:26:06 -08004091// Test that after closing PeerConnections, they stop sending any packets (ICE,
4092// DTLS, RTP...).
Seth Hampson2f0d7022018-02-20 11:54:42 -08004093TEST_P(PeerConnectionIntegrationTest, ClosingConnectionStopsPacketFlow) {
Taylor Brandstetter389a97c2018-01-03 16:26:06 -08004094 // Set up audio/video/data, wait for some frames to be received.
4095 ASSERT_TRUE(CreatePeerConnectionWrappers());
4096 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08004097 caller()->AddAudioVideoTracks();
Taylor Brandstetter389a97c2018-01-03 16:26:06 -08004098#ifdef HAVE_SCTP
4099 caller()->CreateDataChannel();
4100#endif
4101 caller()->CreateAndSetAndSignalOffer();
4102 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08004103 MediaExpectations media_expectations;
4104 media_expectations.CalleeExpectsSomeAudioAndVideo();
4105 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Taylor Brandstetter389a97c2018-01-03 16:26:06 -08004106 // Close PeerConnections.
4107 caller()->pc()->Close();
4108 callee()->pc()->Close();
4109 // Pump messages for a second, and ensure no new packets end up sent.
4110 uint32_t sent_packets_a = virtual_socket_server()->sent_packets();
4111 WAIT(false, 1000);
4112 uint32_t sent_packets_b = virtual_socket_server()->sent_packets();
4113 EXPECT_EQ(sent_packets_a, sent_packets_b);
4114}
4115
Steve Anton7eca0932018-03-30 15:18:41 -07004116// Test that transport stats are generated by the RTCStatsCollector for a
4117// connection that only involves data channels. This is a regression test for
4118// crbug.com/826972.
4119#ifdef HAVE_SCTP
4120TEST_P(PeerConnectionIntegrationTest,
4121 TransportStatsReportedForDataChannelOnlyConnection) {
4122 ASSERT_TRUE(CreatePeerConnectionWrappers());
4123 ConnectFakeSignaling();
4124 caller()->CreateDataChannel();
4125
4126 caller()->CreateAndSetAndSignalOffer();
4127 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4128 ASSERT_TRUE_WAIT(callee()->data_channel(), kDefaultTimeout);
4129
4130 auto caller_report = caller()->NewGetStats();
4131 EXPECT_EQ(1u, caller_report->GetStatsOfType<RTCTransportStats>().size());
4132 auto callee_report = callee()->NewGetStats();
4133 EXPECT_EQ(1u, callee_report->GetStatsOfType<RTCTransportStats>().size());
4134}
4135#endif // HAVE_SCTP
4136
Seth Hampson2f0d7022018-02-20 11:54:42 -08004137INSTANTIATE_TEST_CASE_P(PeerConnectionIntegrationTest,
4138 PeerConnectionIntegrationTest,
4139 Values(SdpSemantics::kPlanB,
4140 SdpSemantics::kUnifiedPlan));
Steve Antond3679212018-01-17 17:41:02 -08004141
Steve Anton74255ff2018-01-24 18:32:57 -08004142// Tests that verify interoperability between Plan B and Unified Plan
4143// PeerConnections.
4144class PeerConnectionIntegrationInteropTest
Seth Hampson2f0d7022018-02-20 11:54:42 -08004145 : public PeerConnectionIntegrationBaseTest,
Steve Anton74255ff2018-01-24 18:32:57 -08004146 public ::testing::WithParamInterface<
4147 std::tuple<SdpSemantics, SdpSemantics>> {
4148 protected:
Seth Hampson2f0d7022018-02-20 11:54:42 -08004149 // Setting the SdpSemantics for the base test to kDefault does not matter
4150 // because we specify not to use the test semantics when creating
4151 // PeerConnectionWrappers.
Steve Anton74255ff2018-01-24 18:32:57 -08004152 PeerConnectionIntegrationInteropTest()
Seth Hampson2f0d7022018-02-20 11:54:42 -08004153 : PeerConnectionIntegrationBaseTest(SdpSemantics::kDefault),
4154 caller_semantics_(std::get<0>(GetParam())),
Steve Anton74255ff2018-01-24 18:32:57 -08004155 callee_semantics_(std::get<1>(GetParam())) {}
4156
4157 bool CreatePeerConnectionWrappersWithSemantics() {
4158 RTCConfiguration caller_config;
4159 caller_config.sdp_semantics = caller_semantics_;
4160 RTCConfiguration callee_config;
4161 callee_config.sdp_semantics = callee_semantics_;
4162 return CreatePeerConnectionWrappersWithConfig(caller_config, callee_config);
4163 }
4164
4165 const SdpSemantics caller_semantics_;
4166 const SdpSemantics callee_semantics_;
4167};
4168
4169TEST_P(PeerConnectionIntegrationInteropTest, NoMediaLocalToNoMediaRemote) {
4170 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
4171 ConnectFakeSignaling();
4172
4173 caller()->CreateAndSetAndSignalOffer();
4174 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4175}
4176
4177TEST_P(PeerConnectionIntegrationInteropTest, OneAudioLocalToNoMediaRemote) {
4178 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
4179 ConnectFakeSignaling();
4180 auto audio_sender = caller()->AddAudioTrack();
4181
4182 caller()->CreateAndSetAndSignalOffer();
4183 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4184
4185 // Verify that one audio receiver has been created on the remote and that it
4186 // has the same track ID as the sending track.
4187 auto receivers = callee()->pc()->GetReceivers();
4188 ASSERT_EQ(1u, receivers.size());
4189 EXPECT_EQ(cricket::MEDIA_TYPE_AUDIO, receivers[0]->media_type());
4190 EXPECT_EQ(receivers[0]->track()->id(), audio_sender->track()->id());
4191
Seth Hampson2f0d7022018-02-20 11:54:42 -08004192 MediaExpectations media_expectations;
4193 media_expectations.CalleeExpectsSomeAudio();
4194 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08004195}
4196
4197TEST_P(PeerConnectionIntegrationInteropTest, OneAudioOneVideoToNoMediaRemote) {
4198 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
4199 ConnectFakeSignaling();
4200 auto video_sender = caller()->AddVideoTrack();
4201 auto audio_sender = caller()->AddAudioTrack();
4202
4203 caller()->CreateAndSetAndSignalOffer();
4204 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4205
4206 // Verify that one audio and one video receiver have been created on the
4207 // remote and that they have the same track IDs as the sending tracks.
4208 auto audio_receivers =
4209 callee()->GetReceiversOfType(cricket::MEDIA_TYPE_AUDIO);
4210 ASSERT_EQ(1u, audio_receivers.size());
4211 EXPECT_EQ(audio_receivers[0]->track()->id(), audio_sender->track()->id());
4212 auto video_receivers =
4213 callee()->GetReceiversOfType(cricket::MEDIA_TYPE_VIDEO);
4214 ASSERT_EQ(1u, video_receivers.size());
4215 EXPECT_EQ(video_receivers[0]->track()->id(), video_sender->track()->id());
4216
Seth Hampson2f0d7022018-02-20 11:54:42 -08004217 MediaExpectations media_expectations;
4218 media_expectations.CalleeExpectsSomeAudioAndVideo();
4219 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08004220}
4221
4222TEST_P(PeerConnectionIntegrationInteropTest,
4223 OneAudioOneVideoLocalToOneAudioOneVideoRemote) {
4224 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
4225 ConnectFakeSignaling();
4226 caller()->AddAudioVideoTracks();
4227 callee()->AddAudioVideoTracks();
4228
4229 caller()->CreateAndSetAndSignalOffer();
4230 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4231
Seth Hampson2f0d7022018-02-20 11:54:42 -08004232 MediaExpectations media_expectations;
4233 media_expectations.ExpectBidirectionalAudioAndVideo();
4234 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08004235}
4236
4237TEST_P(PeerConnectionIntegrationInteropTest,
4238 ReverseRolesOneAudioLocalToOneVideoRemote) {
4239 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
4240 ConnectFakeSignaling();
4241 caller()->AddAudioTrack();
4242 callee()->AddVideoTrack();
4243
4244 caller()->CreateAndSetAndSignalOffer();
4245 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4246
4247 // Verify that only the audio track has been negotiated.
4248 EXPECT_EQ(0u, caller()->GetReceiversOfType(cricket::MEDIA_TYPE_VIDEO).size());
4249 // Might also check that the callee's NegotiationNeeded flag is set.
4250
4251 // Reverse roles.
4252 callee()->CreateAndSetAndSignalOffer();
4253 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4254
Seth Hampson2f0d7022018-02-20 11:54:42 -08004255 MediaExpectations media_expectations;
4256 media_expectations.CallerExpectsSomeVideo();
4257 media_expectations.CalleeExpectsSomeAudio();
4258 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08004259}
4260
Steve Antonba42e992018-04-09 14:10:01 -07004261INSTANTIATE_TEST_CASE_P(
4262 PeerConnectionIntegrationTest,
4263 PeerConnectionIntegrationInteropTest,
4264 Values(std::make_tuple(SdpSemantics::kPlanB, SdpSemantics::kUnifiedPlan),
4265 std::make_tuple(SdpSemantics::kUnifiedPlan, SdpSemantics::kPlanB)));
4266
4267// Test that if the Unified Plan side offers two video tracks then the Plan B
4268// side will only see the first one and ignore the second.
4269TEST_F(PeerConnectionIntegrationTestPlanB, TwoVideoUnifiedPlanToNoMediaPlanB) {
4270 RTCConfiguration caller_config;
4271 caller_config.sdp_semantics = SdpSemantics::kUnifiedPlan;
4272 RTCConfiguration callee_config;
4273 callee_config.sdp_semantics = SdpSemantics::kPlanB;
4274 ASSERT_TRUE(
4275 CreatePeerConnectionWrappersWithConfig(caller_config, callee_config));
4276
Steve Anton74255ff2018-01-24 18:32:57 -08004277 ConnectFakeSignaling();
4278 auto first_sender = caller()->AddVideoTrack();
4279 caller()->AddVideoTrack();
4280
4281 caller()->CreateAndSetAndSignalOffer();
4282 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4283
4284 // Verify that there is only one receiver and it corresponds to the first
4285 // added track.
4286 auto receivers = callee()->pc()->GetReceivers();
4287 ASSERT_EQ(1u, receivers.size());
4288 EXPECT_TRUE(receivers[0]->track()->enabled());
4289 EXPECT_EQ(first_sender->track()->id(), receivers[0]->track()->id());
4290
Seth Hampson2f0d7022018-02-20 11:54:42 -08004291 MediaExpectations media_expectations;
4292 media_expectations.CalleeExpectsSomeVideo();
4293 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08004294}
4295
deadbeef1dcb1642017-03-29 21:08:16 -07004296} // namespace
4297
4298#endif // if !defined(THREAD_SANITIZER)