blob: 7379c711cffee1a10d8725ce514cef7c9b9a9729 [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 }
Steve Anton3acffc32018-04-12 17:21:03 -07001149 modified_config.sdp_semantics = sdp_semantics_;
Seth Hampson2f0d7022018-02-20 11:54:42 -08001150 if (!cert_generator) {
1151 cert_generator = rtc::MakeUnique<FakeRTCCertificateGenerator>();
1152 }
1153 std::unique_ptr<PeerConnectionWrapper> client(
1154 new PeerConnectionWrapper(debug_name));
1155 if (!client->Init(constraints, options, &modified_config,
1156 std::move(cert_generator), network_thread_.get(),
1157 worker_thread_.get())) {
1158 return nullptr;
1159 }
1160 return client;
1161 }
1162
deadbeef1dcb1642017-03-29 21:08:16 -07001163 bool CreatePeerConnectionWrappers() {
1164 return CreatePeerConnectionWrappersWithConfig(
1165 PeerConnectionInterface::RTCConfiguration(),
1166 PeerConnectionInterface::RTCConfiguration());
1167 }
1168
Steve Anton3acffc32018-04-12 17:21:03 -07001169 bool CreatePeerConnectionWrappersWithSdpSemantics(
1170 SdpSemantics caller_semantics,
1171 SdpSemantics callee_semantics) {
1172 // Can't specify the sdp_semantics in the passed-in configuration since it
1173 // will be overwritten by CreatePeerConnectionWrapper with whatever is
1174 // stored in sdp_semantics_. So get around this by modifying the instance
1175 // variable before calling CreatePeerConnectionWrapper for the caller and
1176 // callee PeerConnections.
1177 SdpSemantics original_semantics = sdp_semantics_;
1178 sdp_semantics_ = caller_semantics;
1179 caller_ = CreatePeerConnectionWrapper("Caller", nullptr, nullptr, nullptr,
1180 nullptr);
1181 sdp_semantics_ = callee_semantics;
1182 callee_ = CreatePeerConnectionWrapper("Callee", nullptr, nullptr, nullptr,
1183 nullptr);
1184 sdp_semantics_ = original_semantics;
1185 return caller_ && callee_;
1186 }
1187
deadbeef1dcb1642017-03-29 21:08:16 -07001188 bool CreatePeerConnectionWrappersWithConstraints(
1189 MediaConstraintsInterface* caller_constraints,
1190 MediaConstraintsInterface* callee_constraints) {
Seth Hampson2f0d7022018-02-20 11:54:42 -08001191 caller_ = CreatePeerConnectionWrapper("Caller", caller_constraints, nullptr,
1192 nullptr, nullptr);
1193 callee_ = CreatePeerConnectionWrapper("Callee", callee_constraints, nullptr,
1194 nullptr, nullptr);
deadbeef1dcb1642017-03-29 21:08:16 -07001195 return caller_ && callee_;
1196 }
1197
1198 bool CreatePeerConnectionWrappersWithConfig(
1199 const PeerConnectionInterface::RTCConfiguration& caller_config,
1200 const PeerConnectionInterface::RTCConfiguration& callee_config) {
Seth Hampson2f0d7022018-02-20 11:54:42 -08001201 caller_ = CreatePeerConnectionWrapper("Caller", nullptr, nullptr,
1202 &caller_config, nullptr);
1203 callee_ = CreatePeerConnectionWrapper("Callee", nullptr, nullptr,
1204 &callee_config, nullptr);
deadbeef1dcb1642017-03-29 21:08:16 -07001205 return caller_ && callee_;
1206 }
1207
1208 bool CreatePeerConnectionWrappersWithOptions(
1209 const PeerConnectionFactory::Options& caller_options,
1210 const PeerConnectionFactory::Options& callee_options) {
Seth Hampson2f0d7022018-02-20 11:54:42 -08001211 caller_ = CreatePeerConnectionWrapper("Caller", nullptr, &caller_options,
1212 nullptr, nullptr);
1213 callee_ = CreatePeerConnectionWrapper("Callee", nullptr, &callee_options,
1214 nullptr, nullptr);
deadbeef1dcb1642017-03-29 21:08:16 -07001215 return caller_ && callee_;
1216 }
1217
Seth Hampson2f0d7022018-02-20 11:54:42 -08001218 std::unique_ptr<PeerConnectionWrapper>
1219 CreatePeerConnectionWrapperWithAlternateKey() {
deadbeef1dcb1642017-03-29 21:08:16 -07001220 std::unique_ptr<FakeRTCCertificateGenerator> cert_generator(
1221 new FakeRTCCertificateGenerator());
1222 cert_generator->use_alternate_key();
1223
Seth Hampson2f0d7022018-02-20 11:54:42 -08001224 return CreatePeerConnectionWrapper("New Peer", nullptr, nullptr, nullptr,
1225 std::move(cert_generator));
deadbeef1dcb1642017-03-29 21:08:16 -07001226 }
1227
1228 // Once called, SDP blobs and ICE candidates will be automatically signaled
1229 // between PeerConnections.
1230 void ConnectFakeSignaling() {
1231 caller_->set_signaling_message_receiver(callee_.get());
1232 callee_->set_signaling_message_receiver(caller_.get());
1233 }
1234
Steve Antonede9ca52017-10-16 13:04:27 -07001235 // Once called, SDP blobs will be automatically signaled between
1236 // PeerConnections. Note that ICE candidates will not be signaled unless they
1237 // are in the exchanged SDP blobs.
1238 void ConnectFakeSignalingForSdpOnly() {
1239 ConnectFakeSignaling();
1240 SetSignalIceCandidates(false);
1241 }
1242
deadbeef1dcb1642017-03-29 21:08:16 -07001243 void SetSignalingDelayMs(int delay_ms) {
1244 caller_->set_signaling_delay_ms(delay_ms);
1245 callee_->set_signaling_delay_ms(delay_ms);
1246 }
1247
Steve Antonede9ca52017-10-16 13:04:27 -07001248 void SetSignalIceCandidates(bool signal) {
1249 caller_->set_signal_ice_candidates(signal);
1250 callee_->set_signal_ice_candidates(signal);
1251 }
1252
deadbeef1dcb1642017-03-29 21:08:16 -07001253 void EnableVideoDecoderFactory() {
1254 caller_->EnableVideoDecoderFactory();
1255 callee_->EnableVideoDecoderFactory();
1256 }
1257
1258 // Messages may get lost on the unreliable DataChannel, so we send multiple
1259 // times to avoid test flakiness.
1260 void SendRtpDataWithRetries(webrtc::DataChannelInterface* dc,
1261 const std::string& data,
1262 int retries) {
1263 for (int i = 0; i < retries; ++i) {
1264 dc->Send(DataBuffer(data));
1265 }
1266 }
1267
1268 rtc::Thread* network_thread() { return network_thread_.get(); }
1269
1270 rtc::VirtualSocketServer* virtual_socket_server() { return ss_.get(); }
1271
1272 PeerConnectionWrapper* caller() { return caller_.get(); }
1273
1274 // Set the |caller_| to the |wrapper| passed in and return the
1275 // original |caller_|.
1276 PeerConnectionWrapper* SetCallerPcWrapperAndReturnCurrent(
1277 PeerConnectionWrapper* wrapper) {
1278 PeerConnectionWrapper* old = caller_.release();
1279 caller_.reset(wrapper);
1280 return old;
1281 }
1282
1283 PeerConnectionWrapper* callee() { return callee_.get(); }
1284
1285 // Set the |callee_| to the |wrapper| passed in and return the
1286 // original |callee_|.
1287 PeerConnectionWrapper* SetCalleePcWrapperAndReturnCurrent(
1288 PeerConnectionWrapper* wrapper) {
1289 PeerConnectionWrapper* old = callee_.release();
1290 callee_.reset(wrapper);
1291 return old;
1292 }
1293
Steve Antonede9ca52017-10-16 13:04:27 -07001294 rtc::FirewallSocketServer* firewall() const { return fss_.get(); }
1295
Seth Hampson2f0d7022018-02-20 11:54:42 -08001296 // Expects the provided number of new frames to be received within
1297 // kMaxWaitForFramesMs. The new expected frames are specified in
1298 // |media_expectations|. Returns false if any of the expectations were
1299 // not met.
1300 bool ExpectNewFrames(const MediaExpectations& media_expectations) {
1301 // First initialize the expected frame counts based upon the current
1302 // frame count.
1303 int total_caller_audio_frames_expected = caller()->audio_frames_received();
1304 if (media_expectations.caller_audio_expectation_ ==
1305 MediaExpectations::kExpectSomeFrames) {
1306 total_caller_audio_frames_expected +=
1307 media_expectations.caller_audio_frames_expected_;
1308 }
1309 int total_caller_video_frames_expected =
deadbeef1dcb1642017-03-29 21:08:16 -07001310 caller()->min_video_frames_received_per_track();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001311 if (media_expectations.caller_video_expectation_ ==
1312 MediaExpectations::kExpectSomeFrames) {
1313 total_caller_video_frames_expected +=
1314 media_expectations.caller_video_frames_expected_;
1315 }
1316 int total_callee_audio_frames_expected = callee()->audio_frames_received();
1317 if (media_expectations.callee_audio_expectation_ ==
1318 MediaExpectations::kExpectSomeFrames) {
1319 total_callee_audio_frames_expected +=
1320 media_expectations.callee_audio_frames_expected_;
1321 }
1322 int total_callee_video_frames_expected =
deadbeef1dcb1642017-03-29 21:08:16 -07001323 callee()->min_video_frames_received_per_track();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001324 if (media_expectations.callee_video_expectation_ ==
1325 MediaExpectations::kExpectSomeFrames) {
1326 total_callee_video_frames_expected +=
1327 media_expectations.callee_video_frames_expected_;
1328 }
deadbeef1dcb1642017-03-29 21:08:16 -07001329
Seth Hampson2f0d7022018-02-20 11:54:42 -08001330 // Wait for the expected frames.
deadbeef1dcb1642017-03-29 21:08:16 -07001331 EXPECT_TRUE_WAIT(caller()->audio_frames_received() >=
Seth Hampson2f0d7022018-02-20 11:54:42 -08001332 total_caller_audio_frames_expected &&
deadbeef1dcb1642017-03-29 21:08:16 -07001333 caller()->min_video_frames_received_per_track() >=
Seth Hampson2f0d7022018-02-20 11:54:42 -08001334 total_caller_video_frames_expected &&
deadbeef1dcb1642017-03-29 21:08:16 -07001335 callee()->audio_frames_received() >=
Seth Hampson2f0d7022018-02-20 11:54:42 -08001336 total_callee_audio_frames_expected &&
deadbeef1dcb1642017-03-29 21:08:16 -07001337 callee()->min_video_frames_received_per_track() >=
Seth Hampson2f0d7022018-02-20 11:54:42 -08001338 total_callee_video_frames_expected,
1339 kMaxWaitForFramesMs);
1340 bool expectations_correct =
1341 caller()->audio_frames_received() >=
1342 total_caller_audio_frames_expected &&
1343 caller()->min_video_frames_received_per_track() >=
1344 total_caller_video_frames_expected &&
1345 callee()->audio_frames_received() >=
1346 total_callee_audio_frames_expected &&
1347 callee()->min_video_frames_received_per_track() >=
1348 total_callee_video_frames_expected;
deadbeef1dcb1642017-03-29 21:08:16 -07001349
Seth Hampson2f0d7022018-02-20 11:54:42 -08001350 // After the combined wait, print out a more detailed message upon
1351 // failure.
deadbeef1dcb1642017-03-29 21:08:16 -07001352 EXPECT_GE(caller()->audio_frames_received(),
Seth Hampson2f0d7022018-02-20 11:54:42 -08001353 total_caller_audio_frames_expected);
deadbeef1dcb1642017-03-29 21:08:16 -07001354 EXPECT_GE(caller()->min_video_frames_received_per_track(),
Seth Hampson2f0d7022018-02-20 11:54:42 -08001355 total_caller_video_frames_expected);
deadbeef1dcb1642017-03-29 21:08:16 -07001356 EXPECT_GE(callee()->audio_frames_received(),
Seth Hampson2f0d7022018-02-20 11:54:42 -08001357 total_callee_audio_frames_expected);
deadbeef1dcb1642017-03-29 21:08:16 -07001358 EXPECT_GE(callee()->min_video_frames_received_per_track(),
Seth Hampson2f0d7022018-02-20 11:54:42 -08001359 total_callee_video_frames_expected);
1360
1361 // We want to make sure nothing unexpected was received.
1362 if (media_expectations.caller_audio_expectation_ ==
1363 MediaExpectations::kExpectNoFrames) {
1364 EXPECT_EQ(caller()->audio_frames_received(),
1365 total_caller_audio_frames_expected);
1366 if (caller()->audio_frames_received() !=
1367 total_caller_audio_frames_expected) {
1368 expectations_correct = false;
1369 }
1370 }
1371 if (media_expectations.caller_video_expectation_ ==
1372 MediaExpectations::kExpectNoFrames) {
1373 EXPECT_EQ(caller()->min_video_frames_received_per_track(),
1374 total_caller_video_frames_expected);
1375 if (caller()->min_video_frames_received_per_track() !=
1376 total_caller_video_frames_expected) {
1377 expectations_correct = false;
1378 }
1379 }
1380 if (media_expectations.callee_audio_expectation_ ==
1381 MediaExpectations::kExpectNoFrames) {
1382 EXPECT_EQ(callee()->audio_frames_received(),
1383 total_callee_audio_frames_expected);
1384 if (callee()->audio_frames_received() !=
1385 total_callee_audio_frames_expected) {
1386 expectations_correct = false;
1387 }
1388 }
1389 if (media_expectations.callee_video_expectation_ ==
1390 MediaExpectations::kExpectNoFrames) {
1391 EXPECT_EQ(callee()->min_video_frames_received_per_track(),
1392 total_callee_video_frames_expected);
1393 if (callee()->min_video_frames_received_per_track() !=
1394 total_callee_video_frames_expected) {
1395 expectations_correct = false;
1396 }
1397 }
1398 return expectations_correct;
deadbeef1dcb1642017-03-29 21:08:16 -07001399 }
1400
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07001401 void TestNegotiatedCipherSuite(
1402 const PeerConnectionFactory::Options& caller_options,
1403 const PeerConnectionFactory::Options& callee_options,
1404 int expected_cipher_suite) {
deadbeef1dcb1642017-03-29 21:08:16 -07001405 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(caller_options,
1406 callee_options));
1407 rtc::scoped_refptr<webrtc::FakeMetricsObserver> caller_observer =
1408 new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
1409 caller()->pc()->RegisterUMAObserver(caller_observer);
1410 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001411 caller()->AddAudioVideoTracks();
1412 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001413 caller()->CreateAndSetAndSignalOffer();
1414 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1415 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(expected_cipher_suite),
deadbeefd8ad7882017-04-18 16:01:17 -07001416 caller()->OldGetStats()->SrtpCipher(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07001417 EXPECT_EQ(
1418 1, caller_observer->GetEnumCounter(webrtc::kEnumCounterAudioSrtpCipher,
1419 expected_cipher_suite));
1420 caller()->pc()->RegisterUMAObserver(nullptr);
1421 }
1422
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07001423 void TestGcmNegotiationUsesCipherSuite(bool local_gcm_enabled,
1424 bool remote_gcm_enabled,
1425 int expected_cipher_suite) {
1426 PeerConnectionFactory::Options caller_options;
1427 caller_options.crypto_options.enable_gcm_crypto_suites = local_gcm_enabled;
1428 PeerConnectionFactory::Options callee_options;
1429 callee_options.crypto_options.enable_gcm_crypto_suites = remote_gcm_enabled;
1430 TestNegotiatedCipherSuite(caller_options, callee_options,
1431 expected_cipher_suite);
1432 }
1433
Seth Hampson2f0d7022018-02-20 11:54:42 -08001434 protected:
Steve Anton3acffc32018-04-12 17:21:03 -07001435 SdpSemantics sdp_semantics_;
Seth Hampson2f0d7022018-02-20 11:54:42 -08001436
deadbeef1dcb1642017-03-29 21:08:16 -07001437 private:
1438 // |ss_| is used by |network_thread_| so it must be destroyed later.
deadbeef1dcb1642017-03-29 21:08:16 -07001439 std::unique_ptr<rtc::VirtualSocketServer> ss_;
Steve Antonede9ca52017-10-16 13:04:27 -07001440 std::unique_ptr<rtc::FirewallSocketServer> fss_;
deadbeef1dcb1642017-03-29 21:08:16 -07001441 // |network_thread_| and |worker_thread_| are used by both
1442 // |caller_| and |callee_| so they must be destroyed
1443 // later.
1444 std::unique_ptr<rtc::Thread> network_thread_;
1445 std::unique_ptr<rtc::Thread> worker_thread_;
1446 std::unique_ptr<PeerConnectionWrapper> caller_;
1447 std::unique_ptr<PeerConnectionWrapper> callee_;
1448};
1449
Seth Hampson2f0d7022018-02-20 11:54:42 -08001450class PeerConnectionIntegrationTest
1451 : public PeerConnectionIntegrationBaseTest,
1452 public ::testing::WithParamInterface<SdpSemantics> {
1453 protected:
1454 PeerConnectionIntegrationTest()
1455 : PeerConnectionIntegrationBaseTest(GetParam()) {}
1456};
1457
1458class PeerConnectionIntegrationTestPlanB
1459 : public PeerConnectionIntegrationBaseTest {
1460 protected:
1461 PeerConnectionIntegrationTestPlanB()
1462 : PeerConnectionIntegrationBaseTest(SdpSemantics::kPlanB) {}
1463};
1464
1465class PeerConnectionIntegrationTestUnifiedPlan
1466 : public PeerConnectionIntegrationBaseTest {
1467 protected:
1468 PeerConnectionIntegrationTestUnifiedPlan()
1469 : PeerConnectionIntegrationBaseTest(SdpSemantics::kUnifiedPlan) {}
1470};
1471
deadbeef1dcb1642017-03-29 21:08:16 -07001472// Test the OnFirstPacketReceived callback from audio/video RtpReceivers. This
1473// includes testing that the callback is invoked if an observer is connected
1474// after the first packet has already been received.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001475TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07001476 RtpReceiverObserverOnFirstPacketReceived) {
1477 ASSERT_TRUE(CreatePeerConnectionWrappers());
1478 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001479 caller()->AddAudioVideoTracks();
1480 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001481 // Start offer/answer exchange and wait for it to complete.
1482 caller()->CreateAndSetAndSignalOffer();
1483 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1484 // Should be one receiver each for audio/video.
1485 EXPECT_EQ(2, caller()->rtp_receiver_observers().size());
1486 EXPECT_EQ(2, callee()->rtp_receiver_observers().size());
1487 // Wait for all "first packet received" callbacks to be fired.
1488 EXPECT_TRUE_WAIT(
1489 std::all_of(caller()->rtp_receiver_observers().begin(),
1490 caller()->rtp_receiver_observers().end(),
1491 [](const std::unique_ptr<MockRtpReceiverObserver>& o) {
1492 return o->first_packet_received();
1493 }),
1494 kMaxWaitForFramesMs);
1495 EXPECT_TRUE_WAIT(
1496 std::all_of(callee()->rtp_receiver_observers().begin(),
1497 callee()->rtp_receiver_observers().end(),
1498 [](const std::unique_ptr<MockRtpReceiverObserver>& o) {
1499 return o->first_packet_received();
1500 }),
1501 kMaxWaitForFramesMs);
1502 // If new observers are set after the first packet was already received, the
1503 // callback should still be invoked.
1504 caller()->ResetRtpReceiverObservers();
1505 callee()->ResetRtpReceiverObservers();
1506 EXPECT_EQ(2, caller()->rtp_receiver_observers().size());
1507 EXPECT_EQ(2, callee()->rtp_receiver_observers().size());
1508 EXPECT_TRUE(
1509 std::all_of(caller()->rtp_receiver_observers().begin(),
1510 caller()->rtp_receiver_observers().end(),
1511 [](const std::unique_ptr<MockRtpReceiverObserver>& o) {
1512 return o->first_packet_received();
1513 }));
1514 EXPECT_TRUE(
1515 std::all_of(callee()->rtp_receiver_observers().begin(),
1516 callee()->rtp_receiver_observers().end(),
1517 [](const std::unique_ptr<MockRtpReceiverObserver>& o) {
1518 return o->first_packet_received();
1519 }));
1520}
1521
1522class DummyDtmfObserver : public DtmfSenderObserverInterface {
1523 public:
1524 DummyDtmfObserver() : completed_(false) {}
1525
1526 // Implements DtmfSenderObserverInterface.
1527 void OnToneChange(const std::string& tone) override {
1528 tones_.push_back(tone);
1529 if (tone.empty()) {
1530 completed_ = true;
1531 }
1532 }
1533
1534 const std::vector<std::string>& tones() const { return tones_; }
1535 bool completed() const { return completed_; }
1536
1537 private:
1538 bool completed_;
1539 std::vector<std::string> tones_;
1540};
1541
1542// Assumes |sender| already has an audio track added and the offer/answer
1543// exchange is done.
1544void TestDtmfFromSenderToReceiver(PeerConnectionWrapper* sender,
1545 PeerConnectionWrapper* receiver) {
Steve Anton15324772018-01-16 10:26:49 -08001546 // We should be able to get a DTMF sender from the local sender.
1547 rtc::scoped_refptr<DtmfSenderInterface> dtmf_sender =
1548 sender->pc()->GetSenders().at(0)->GetDtmfSender();
1549 ASSERT_TRUE(dtmf_sender);
deadbeef1dcb1642017-03-29 21:08:16 -07001550 DummyDtmfObserver observer;
deadbeef1dcb1642017-03-29 21:08:16 -07001551 dtmf_sender->RegisterObserver(&observer);
1552
1553 // Test the DtmfSender object just created.
1554 EXPECT_TRUE(dtmf_sender->CanInsertDtmf());
1555 EXPECT_TRUE(dtmf_sender->InsertDtmf("1a", 100, 50));
1556
1557 EXPECT_TRUE_WAIT(observer.completed(), kDefaultTimeout);
1558 std::vector<std::string> tones = {"1", "a", ""};
1559 EXPECT_EQ(tones, observer.tones());
1560 dtmf_sender->UnregisterObserver();
1561 // TODO(deadbeef): Verify the tones were actually received end-to-end.
1562}
1563
1564// Verifies the DtmfSenderObserver callbacks for a DtmfSender (one in each
1565// direction).
Seth Hampson2f0d7022018-02-20 11:54:42 -08001566TEST_P(PeerConnectionIntegrationTest, DtmfSenderObserver) {
deadbeef1dcb1642017-03-29 21:08:16 -07001567 ASSERT_TRUE(CreatePeerConnectionWrappers());
1568 ConnectFakeSignaling();
1569 // Only need audio for DTMF.
Steve Anton15324772018-01-16 10:26:49 -08001570 caller()->AddAudioTrack();
1571 callee()->AddAudioTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07001572 caller()->CreateAndSetAndSignalOffer();
1573 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
deadbeef71452802017-05-07 17:21:01 -07001574 // DTLS must finish before the DTMF sender can be used reliably.
1575 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07001576 TestDtmfFromSenderToReceiver(caller(), callee());
1577 TestDtmfFromSenderToReceiver(callee(), caller());
1578}
1579
1580// Basic end-to-end test, verifying media can be encoded/transmitted/decoded
1581// between two connections, using DTLS-SRTP.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001582TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithDtls) {
deadbeef1dcb1642017-03-29 21:08:16 -07001583 ASSERT_TRUE(CreatePeerConnectionWrappers());
1584 ConnectFakeSignaling();
Harald Alvestrand194939b2018-01-24 16:04:13 +01001585 rtc::scoped_refptr<webrtc::FakeMetricsObserver> caller_observer =
1586 new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
1587 caller()->pc()->RegisterUMAObserver(caller_observer);
1588
deadbeef1dcb1642017-03-29 21:08:16 -07001589 // Do normal offer/answer and wait for some frames to be received in each
1590 // direction.
Steve Anton15324772018-01-16 10:26:49 -08001591 caller()->AddAudioVideoTracks();
1592 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001593 caller()->CreateAndSetAndSignalOffer();
1594 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001595 MediaExpectations media_expectations;
1596 media_expectations.ExpectBidirectionalAudioAndVideo();
1597 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Harald Alvestrand194939b2018-01-24 16:04:13 +01001598 EXPECT_LE(
1599 1, caller_observer->GetEnumCounter(webrtc::kEnumCounterKeyProtocol,
1600 webrtc::kEnumCounterKeyProtocolDtls));
1601 EXPECT_EQ(
1602 0, caller_observer->GetEnumCounter(webrtc::kEnumCounterKeyProtocol,
1603 webrtc::kEnumCounterKeyProtocolSdes));
deadbeef1dcb1642017-03-29 21:08:16 -07001604}
1605
1606// Uses SDES instead of DTLS for key agreement.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001607TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithSdes) {
deadbeef1dcb1642017-03-29 21:08:16 -07001608 PeerConnectionInterface::RTCConfiguration sdes_config;
1609 sdes_config.enable_dtls_srtp.emplace(false);
1610 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(sdes_config, sdes_config));
1611 ConnectFakeSignaling();
Harald Alvestrand194939b2018-01-24 16:04:13 +01001612 rtc::scoped_refptr<webrtc::FakeMetricsObserver> caller_observer =
1613 new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
1614 caller()->pc()->RegisterUMAObserver(caller_observer);
deadbeef1dcb1642017-03-29 21:08:16 -07001615
1616 // Do normal offer/answer and wait for some frames to be received in each
1617 // direction.
Steve Anton15324772018-01-16 10:26:49 -08001618 caller()->AddAudioVideoTracks();
1619 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001620 caller()->CreateAndSetAndSignalOffer();
1621 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001622 MediaExpectations media_expectations;
1623 media_expectations.ExpectBidirectionalAudioAndVideo();
1624 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Harald Alvestrand194939b2018-01-24 16:04:13 +01001625 EXPECT_LE(
1626 1, caller_observer->GetEnumCounter(webrtc::kEnumCounterKeyProtocol,
1627 webrtc::kEnumCounterKeyProtocolSdes));
1628 EXPECT_EQ(
1629 0, caller_observer->GetEnumCounter(webrtc::kEnumCounterKeyProtocol,
1630 webrtc::kEnumCounterKeyProtocolDtls));
deadbeef1dcb1642017-03-29 21:08:16 -07001631}
1632
Steve Anton8c0f7a72017-10-03 10:03:10 -07001633// Tests that the GetRemoteAudioSSLCertificate method returns the remote DTLS
1634// certificate once the DTLS handshake has finished.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001635TEST_P(PeerConnectionIntegrationTest,
Steve Anton8c0f7a72017-10-03 10:03:10 -07001636 GetRemoteAudioSSLCertificateReturnsExchangedCertificate) {
1637 auto GetRemoteAudioSSLCertificate = [](PeerConnectionWrapper* wrapper) {
1638 auto pci = reinterpret_cast<PeerConnectionProxy*>(wrapper->pc());
1639 auto pc = reinterpret_cast<PeerConnection*>(pci->internal());
1640 return pc->GetRemoteAudioSSLCertificate();
1641 };
Zhi Huang70b820f2018-01-27 14:16:15 -08001642 auto GetRemoteAudioSSLCertChain = [](PeerConnectionWrapper* wrapper) {
1643 auto pci = reinterpret_cast<PeerConnectionProxy*>(wrapper->pc());
1644 auto pc = reinterpret_cast<PeerConnection*>(pci->internal());
1645 return pc->GetRemoteAudioSSLCertChain();
1646 };
Steve Anton8c0f7a72017-10-03 10:03:10 -07001647
1648 auto caller_cert = rtc::RTCCertificate::FromPEM(kRsaPems[0]);
1649 auto callee_cert = rtc::RTCCertificate::FromPEM(kRsaPems[1]);
1650
1651 // Configure each side with a known certificate so they can be compared later.
1652 PeerConnectionInterface::RTCConfiguration caller_config;
1653 caller_config.enable_dtls_srtp.emplace(true);
1654 caller_config.certificates.push_back(caller_cert);
1655 PeerConnectionInterface::RTCConfiguration callee_config;
1656 callee_config.enable_dtls_srtp.emplace(true);
1657 callee_config.certificates.push_back(callee_cert);
1658 ASSERT_TRUE(
1659 CreatePeerConnectionWrappersWithConfig(caller_config, callee_config));
1660 ConnectFakeSignaling();
1661
1662 // When first initialized, there should not be a remote SSL certificate (and
1663 // calling this method should not crash).
1664 EXPECT_EQ(nullptr, GetRemoteAudioSSLCertificate(caller()));
1665 EXPECT_EQ(nullptr, GetRemoteAudioSSLCertificate(callee()));
Zhi Huang70b820f2018-01-27 14:16:15 -08001666 EXPECT_EQ(nullptr, GetRemoteAudioSSLCertChain(caller()));
1667 EXPECT_EQ(nullptr, GetRemoteAudioSSLCertChain(callee()));
Steve Anton8c0f7a72017-10-03 10:03:10 -07001668
Steve Anton15324772018-01-16 10:26:49 -08001669 caller()->AddAudioTrack();
1670 callee()->AddAudioTrack();
Steve Anton8c0f7a72017-10-03 10:03:10 -07001671 caller()->CreateAndSetAndSignalOffer();
1672 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1673 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
1674
1675 // Once DTLS has been connected, each side should return the other's SSL
1676 // certificate when calling GetRemoteAudioSSLCertificate.
1677
1678 auto caller_remote_cert = GetRemoteAudioSSLCertificate(caller());
1679 ASSERT_TRUE(caller_remote_cert);
1680 EXPECT_EQ(callee_cert->ssl_certificate().ToPEMString(),
1681 caller_remote_cert->ToPEMString());
1682
1683 auto callee_remote_cert = GetRemoteAudioSSLCertificate(callee());
1684 ASSERT_TRUE(callee_remote_cert);
1685 EXPECT_EQ(caller_cert->ssl_certificate().ToPEMString(),
1686 callee_remote_cert->ToPEMString());
Zhi Huang70b820f2018-01-27 14:16:15 -08001687
1688 auto caller_remote_cert_chain = GetRemoteAudioSSLCertChain(caller());
1689 ASSERT_TRUE(caller_remote_cert_chain);
1690 ASSERT_EQ(1U, caller_remote_cert_chain->GetSize());
1691 auto remote_cert = &caller_remote_cert_chain->Get(0);
1692 EXPECT_EQ(callee_cert->ssl_certificate().ToPEMString(),
1693 remote_cert->ToPEMString());
1694
1695 auto callee_remote_cert_chain = GetRemoteAudioSSLCertChain(callee());
1696 ASSERT_TRUE(callee_remote_cert_chain);
1697 ASSERT_EQ(1U, callee_remote_cert_chain->GetSize());
1698 remote_cert = &callee_remote_cert_chain->Get(0);
1699 EXPECT_EQ(caller_cert->ssl_certificate().ToPEMString(),
1700 remote_cert->ToPEMString());
Steve Anton8c0f7a72017-10-03 10:03:10 -07001701}
1702
deadbeef1dcb1642017-03-29 21:08:16 -07001703// This test sets up a call between two parties (using DTLS) and tests that we
1704// can get a video aspect ratio of 16:9.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001705TEST_P(PeerConnectionIntegrationTest, SendAndReceive16To9AspectRatio) {
deadbeef1dcb1642017-03-29 21:08:16 -07001706 ASSERT_TRUE(CreatePeerConnectionWrappers());
1707 ConnectFakeSignaling();
1708
1709 // Add video tracks with 16:9 constraint.
1710 FakeConstraints constraints;
1711 double requested_ratio = 16.0 / 9;
1712 constraints.SetMandatoryMinAspectRatio(requested_ratio);
Steve Anton15324772018-01-16 10:26:49 -08001713 caller()->AddTrack(
1714 caller()->CreateLocalVideoTrackWithConstraints(constraints));
1715 callee()->AddTrack(
1716 callee()->CreateLocalVideoTrackWithConstraints(constraints));
deadbeef1dcb1642017-03-29 21:08:16 -07001717
1718 // Do normal offer/answer and wait for at least one frame to be received in
1719 // each direction.
1720 caller()->CreateAndSetAndSignalOffer();
1721 ASSERT_TRUE_WAIT(caller()->min_video_frames_received_per_track() > 0 &&
1722 callee()->min_video_frames_received_per_track() > 0,
1723 kMaxWaitForFramesMs);
1724
1725 // Check rendered aspect ratio.
1726 EXPECT_EQ(requested_ratio, caller()->local_rendered_aspect_ratio());
1727 EXPECT_EQ(requested_ratio, caller()->rendered_aspect_ratio());
1728 EXPECT_EQ(requested_ratio, callee()->local_rendered_aspect_ratio());
1729 EXPECT_EQ(requested_ratio, callee()->rendered_aspect_ratio());
1730}
1731
1732// This test sets up a call between two parties with a source resolution of
1733// 1280x720 and verifies that a 16:9 aspect ratio is received.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001734TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07001735 Send1280By720ResolutionAndReceive16To9AspectRatio) {
1736 ASSERT_TRUE(CreatePeerConnectionWrappers());
1737 ConnectFakeSignaling();
1738
1739 // Similar to above test, but uses MandatoryMin[Width/Height] constraint
1740 // instead of aspect ratio constraint.
1741 FakeConstraints constraints;
1742 constraints.SetMandatoryMinWidth(1280);
1743 constraints.SetMandatoryMinHeight(720);
Steve Anton15324772018-01-16 10:26:49 -08001744 caller()->AddTrack(
1745 caller()->CreateLocalVideoTrackWithConstraints(constraints));
1746 callee()->AddTrack(
1747 callee()->CreateLocalVideoTrackWithConstraints(constraints));
deadbeef1dcb1642017-03-29 21:08:16 -07001748
1749 // Do normal offer/answer and wait for at least one frame to be received in
1750 // each direction.
1751 caller()->CreateAndSetAndSignalOffer();
1752 ASSERT_TRUE_WAIT(caller()->min_video_frames_received_per_track() > 0 &&
1753 callee()->min_video_frames_received_per_track() > 0,
1754 kMaxWaitForFramesMs);
1755
1756 // Check rendered aspect ratio.
1757 EXPECT_EQ(16.0 / 9, caller()->local_rendered_aspect_ratio());
1758 EXPECT_EQ(16.0 / 9, caller()->rendered_aspect_ratio());
1759 EXPECT_EQ(16.0 / 9, callee()->local_rendered_aspect_ratio());
1760 EXPECT_EQ(16.0 / 9, callee()->rendered_aspect_ratio());
1761}
1762
1763// This test sets up an one-way call, with media only from caller to
1764// callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001765TEST_P(PeerConnectionIntegrationTest, OneWayMediaCall) {
deadbeef1dcb1642017-03-29 21:08:16 -07001766 ASSERT_TRUE(CreatePeerConnectionWrappers());
1767 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001768 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001769 caller()->CreateAndSetAndSignalOffer();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001770 MediaExpectations media_expectations;
1771 media_expectations.CalleeExpectsSomeAudioAndVideo();
1772 media_expectations.CallerExpectsNoAudio();
1773 media_expectations.CallerExpectsNoVideo();
1774 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07001775}
1776
1777// This test sets up a audio call initially, with the callee rejecting video
1778// initially. Then later the callee decides to upgrade to audio/video, and
1779// initiates a new offer/answer exchange.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001780TEST_P(PeerConnectionIntegrationTest, AudioToVideoUpgrade) {
deadbeef1dcb1642017-03-29 21:08:16 -07001781 ASSERT_TRUE(CreatePeerConnectionWrappers());
1782 ConnectFakeSignaling();
1783 // Initially, offer an audio/video stream from the caller, but refuse to
1784 // send/receive video on the callee side.
Steve Anton15324772018-01-16 10:26:49 -08001785 caller()->AddAudioVideoTracks();
1786 callee()->AddAudioTrack();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001787 if (sdp_semantics_ == SdpSemantics::kPlanB) {
1788 PeerConnectionInterface::RTCOfferAnswerOptions options;
1789 options.offer_to_receive_video = 0;
1790 callee()->SetOfferAnswerOptions(options);
1791 } else {
1792 callee()->SetRemoteOfferHandler([this] {
1793 callee()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)->Stop();
1794 });
1795 }
deadbeef1dcb1642017-03-29 21:08:16 -07001796 // Do offer/answer and make sure audio is still received end-to-end.
1797 caller()->CreateAndSetAndSignalOffer();
1798 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001799 {
1800 MediaExpectations media_expectations;
1801 media_expectations.ExpectBidirectionalAudio();
1802 media_expectations.ExpectNoVideo();
1803 ASSERT_TRUE(ExpectNewFrames(media_expectations));
1804 }
deadbeef1dcb1642017-03-29 21:08:16 -07001805 // Sanity check that the callee's description has a rejected video section.
1806 ASSERT_NE(nullptr, callee()->pc()->local_description());
1807 const ContentInfo* callee_video_content =
1808 GetFirstVideoContent(callee()->pc()->local_description()->description());
1809 ASSERT_NE(nullptr, callee_video_content);
1810 EXPECT_TRUE(callee_video_content->rejected);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001811
deadbeef1dcb1642017-03-29 21:08:16 -07001812 // Now negotiate with video and ensure negotiation succeeds, with video
1813 // frames and additional audio frames being received.
Steve Anton15324772018-01-16 10:26:49 -08001814 callee()->AddVideoTrack();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001815 if (sdp_semantics_ == SdpSemantics::kPlanB) {
1816 PeerConnectionInterface::RTCOfferAnswerOptions options;
1817 options.offer_to_receive_video = 1;
1818 callee()->SetOfferAnswerOptions(options);
1819 } else {
1820 callee()->SetRemoteOfferHandler(nullptr);
1821 caller()->SetRemoteOfferHandler([this] {
1822 // The caller creates a new transceiver to receive video on when receiving
1823 // the offer, but by default it is send only.
1824 auto transceivers = caller()->pc()->GetTransceivers();
1825 ASSERT_EQ(3, transceivers.size());
1826 ASSERT_EQ(cricket::MEDIA_TYPE_VIDEO,
1827 transceivers[2]->receiver()->media_type());
1828 transceivers[2]->sender()->SetTrack(caller()->CreateLocalVideoTrack());
1829 transceivers[2]->SetDirection(RtpTransceiverDirection::kSendRecv);
1830 });
1831 }
deadbeef1dcb1642017-03-29 21:08:16 -07001832 callee()->CreateAndSetAndSignalOffer();
1833 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001834 {
1835 // Expect additional audio frames to be received after the upgrade.
1836 MediaExpectations media_expectations;
1837 media_expectations.ExpectBidirectionalAudioAndVideo();
1838 ASSERT_TRUE(ExpectNewFrames(media_expectations));
1839 }
deadbeef1dcb1642017-03-29 21:08:16 -07001840}
1841
deadbeef4389b4d2017-09-07 09:07:36 -07001842// Simpler than the above test; just add an audio track to an established
1843// video-only connection.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001844TEST_P(PeerConnectionIntegrationTest, AddAudioToVideoOnlyCall) {
deadbeef4389b4d2017-09-07 09:07:36 -07001845 ASSERT_TRUE(CreatePeerConnectionWrappers());
1846 ConnectFakeSignaling();
1847 // Do initial offer/answer with just a video track.
Steve Anton15324772018-01-16 10:26:49 -08001848 caller()->AddVideoTrack();
1849 callee()->AddVideoTrack();
deadbeef4389b4d2017-09-07 09:07:36 -07001850 caller()->CreateAndSetAndSignalOffer();
1851 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1852 // Now add an audio track and do another offer/answer.
Steve Anton15324772018-01-16 10:26:49 -08001853 caller()->AddAudioTrack();
1854 callee()->AddAudioTrack();
deadbeef4389b4d2017-09-07 09:07:36 -07001855 caller()->CreateAndSetAndSignalOffer();
1856 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1857 // Ensure both audio and video frames are received end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001858 MediaExpectations media_expectations;
1859 media_expectations.ExpectBidirectionalAudioAndVideo();
1860 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef4389b4d2017-09-07 09:07:36 -07001861}
1862
deadbeef1dcb1642017-03-29 21:08:16 -07001863// This test sets up a call that's transferred to a new caller with a different
1864// DTLS fingerprint.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001865TEST_P(PeerConnectionIntegrationTest, CallTransferredForCallee) {
deadbeef1dcb1642017-03-29 21:08:16 -07001866 ASSERT_TRUE(CreatePeerConnectionWrappers());
1867 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001868 caller()->AddAudioVideoTracks();
1869 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001870 caller()->CreateAndSetAndSignalOffer();
1871 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1872
1873 // Keep the original peer around which will still send packets to the
1874 // receiving client. These SRTP packets will be dropped.
1875 std::unique_ptr<PeerConnectionWrapper> original_peer(
1876 SetCallerPcWrapperAndReturnCurrent(
Seth Hampson2f0d7022018-02-20 11:54:42 -08001877 CreatePeerConnectionWrapperWithAlternateKey().release()));
deadbeef1dcb1642017-03-29 21:08:16 -07001878 // TODO(deadbeef): Why do we call Close here? That goes against the comment
1879 // directly above.
1880 original_peer->pc()->Close();
1881
1882 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001883 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001884 caller()->CreateAndSetAndSignalOffer();
1885 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1886 // Wait for some additional frames to be transmitted end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001887 MediaExpectations media_expectations;
1888 media_expectations.ExpectBidirectionalAudioAndVideo();
1889 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07001890}
1891
1892// This test sets up a call that's transferred to a new callee with a different
1893// DTLS fingerprint.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001894TEST_P(PeerConnectionIntegrationTest, CallTransferredForCaller) {
deadbeef1dcb1642017-03-29 21:08:16 -07001895 ASSERT_TRUE(CreatePeerConnectionWrappers());
1896 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001897 caller()->AddAudioVideoTracks();
1898 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001899 caller()->CreateAndSetAndSignalOffer();
1900 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1901
1902 // Keep the original peer around which will still send packets to the
1903 // receiving client. These SRTP packets will be dropped.
1904 std::unique_ptr<PeerConnectionWrapper> original_peer(
1905 SetCalleePcWrapperAndReturnCurrent(
Seth Hampson2f0d7022018-02-20 11:54:42 -08001906 CreatePeerConnectionWrapperWithAlternateKey().release()));
deadbeef1dcb1642017-03-29 21:08:16 -07001907 // TODO(deadbeef): Why do we call Close here? That goes against the comment
1908 // directly above.
1909 original_peer->pc()->Close();
1910
1911 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001912 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001913 caller()->SetOfferAnswerOptions(IceRestartOfferAnswerOptions());
1914 caller()->CreateAndSetAndSignalOffer();
1915 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1916 // Wait for some additional frames to be transmitted end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001917 MediaExpectations media_expectations;
1918 media_expectations.ExpectBidirectionalAudioAndVideo();
1919 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07001920}
1921
1922// This test sets up a non-bundled call and negotiates bundling at the same
1923// time as starting an ICE restart. When bundling is in effect in the restart,
1924// the DTLS-SRTP context should be successfully reset.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001925TEST_P(PeerConnectionIntegrationTest, BundlingEnabledWhileIceRestartOccurs) {
deadbeef1dcb1642017-03-29 21:08:16 -07001926 ASSERT_TRUE(CreatePeerConnectionWrappers());
1927 ConnectFakeSignaling();
1928
Steve Anton15324772018-01-16 10:26:49 -08001929 caller()->AddAudioVideoTracks();
1930 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001931 // Remove the bundle group from the SDP received by the callee.
1932 callee()->SetReceivedSdpMunger([](cricket::SessionDescription* desc) {
1933 desc->RemoveGroupByName("BUNDLE");
1934 });
1935 caller()->CreateAndSetAndSignalOffer();
1936 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001937 {
1938 MediaExpectations media_expectations;
1939 media_expectations.ExpectBidirectionalAudioAndVideo();
1940 ASSERT_TRUE(ExpectNewFrames(media_expectations));
1941 }
deadbeef1dcb1642017-03-29 21:08:16 -07001942 // Now stop removing the BUNDLE group, and trigger an ICE restart.
1943 callee()->SetReceivedSdpMunger(nullptr);
1944 caller()->SetOfferAnswerOptions(IceRestartOfferAnswerOptions());
1945 caller()->CreateAndSetAndSignalOffer();
1946 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1947
1948 // Expect additional frames to be received after the ICE restart.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001949 {
1950 MediaExpectations media_expectations;
1951 media_expectations.ExpectBidirectionalAudioAndVideo();
1952 ASSERT_TRUE(ExpectNewFrames(media_expectations));
1953 }
deadbeef1dcb1642017-03-29 21:08:16 -07001954}
1955
1956// Test CVO (Coordination of Video Orientation). If a video source is rotated
1957// and both peers support the CVO RTP header extension, the actual video frames
1958// don't need to be encoded in different resolutions, since the rotation is
1959// communicated through the RTP header extension.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001960TEST_P(PeerConnectionIntegrationTest, RotatedVideoWithCVOExtension) {
deadbeef1dcb1642017-03-29 21:08:16 -07001961 ASSERT_TRUE(CreatePeerConnectionWrappers());
1962 ConnectFakeSignaling();
1963 // Add rotated video tracks.
Steve Anton15324772018-01-16 10:26:49 -08001964 caller()->AddTrack(
deadbeef1dcb1642017-03-29 21:08:16 -07001965 caller()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_90));
Steve Anton15324772018-01-16 10:26:49 -08001966 callee()->AddTrack(
deadbeef1dcb1642017-03-29 21:08:16 -07001967 callee()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_270));
1968
1969 // Wait for video frames to be received by both sides.
1970 caller()->CreateAndSetAndSignalOffer();
1971 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1972 ASSERT_TRUE_WAIT(caller()->min_video_frames_received_per_track() > 0 &&
1973 callee()->min_video_frames_received_per_track() > 0,
1974 kMaxWaitForFramesMs);
1975
1976 // Ensure that the aspect ratio is unmodified.
1977 // TODO(deadbeef): Where does 4:3 come from? Should be explicit in the test,
1978 // not just assumed.
1979 EXPECT_EQ(4.0 / 3, caller()->local_rendered_aspect_ratio());
1980 EXPECT_EQ(4.0 / 3, caller()->rendered_aspect_ratio());
1981 EXPECT_EQ(4.0 / 3, callee()->local_rendered_aspect_ratio());
1982 EXPECT_EQ(4.0 / 3, callee()->rendered_aspect_ratio());
1983 // Ensure that the CVO bits were surfaced to the renderer.
1984 EXPECT_EQ(webrtc::kVideoRotation_270, caller()->rendered_rotation());
1985 EXPECT_EQ(webrtc::kVideoRotation_90, callee()->rendered_rotation());
1986}
1987
1988// Test that when the CVO extension isn't supported, video is rotated the
1989// old-fashioned way, by encoding rotated frames.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001990TEST_P(PeerConnectionIntegrationTest, RotatedVideoWithoutCVOExtension) {
deadbeef1dcb1642017-03-29 21:08:16 -07001991 ASSERT_TRUE(CreatePeerConnectionWrappers());
1992 ConnectFakeSignaling();
1993 // Add rotated video tracks.
Steve Anton15324772018-01-16 10:26:49 -08001994 caller()->AddTrack(
deadbeef1dcb1642017-03-29 21:08:16 -07001995 caller()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_90));
Steve Anton15324772018-01-16 10:26:49 -08001996 callee()->AddTrack(
deadbeef1dcb1642017-03-29 21:08:16 -07001997 callee()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_270));
1998
1999 // Remove the CVO extension from the offered SDP.
2000 callee()->SetReceivedSdpMunger([](cricket::SessionDescription* desc) {
2001 cricket::VideoContentDescription* video =
2002 GetFirstVideoContentDescription(desc);
2003 video->ClearRtpHeaderExtensions();
2004 });
2005 // Wait for video frames to be received by both sides.
2006 caller()->CreateAndSetAndSignalOffer();
2007 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2008 ASSERT_TRUE_WAIT(caller()->min_video_frames_received_per_track() > 0 &&
2009 callee()->min_video_frames_received_per_track() > 0,
2010 kMaxWaitForFramesMs);
2011
2012 // Expect that the aspect ratio is inversed to account for the 90/270 degree
2013 // rotation.
2014 // TODO(deadbeef): Where does 4:3 come from? Should be explicit in the test,
2015 // not just assumed.
2016 EXPECT_EQ(3.0 / 4, caller()->local_rendered_aspect_ratio());
2017 EXPECT_EQ(3.0 / 4, caller()->rendered_aspect_ratio());
2018 EXPECT_EQ(3.0 / 4, callee()->local_rendered_aspect_ratio());
2019 EXPECT_EQ(3.0 / 4, callee()->rendered_aspect_ratio());
2020 // Expect that each endpoint is unaware of the rotation of the other endpoint.
2021 EXPECT_EQ(webrtc::kVideoRotation_0, caller()->rendered_rotation());
2022 EXPECT_EQ(webrtc::kVideoRotation_0, callee()->rendered_rotation());
2023}
2024
deadbeef1dcb1642017-03-29 21:08:16 -07002025// Test that if the answerer rejects the audio m= section, no audio is sent or
2026// received, but video still can be.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002027TEST_P(PeerConnectionIntegrationTest, AnswererRejectsAudioSection) {
deadbeef1dcb1642017-03-29 21:08:16 -07002028 ASSERT_TRUE(CreatePeerConnectionWrappers());
2029 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002030 caller()->AddAudioVideoTracks();
Seth Hampson2f0d7022018-02-20 11:54:42 -08002031 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2032 // Only add video track for callee, and set offer_to_receive_audio to 0, so
2033 // it will reject the audio m= section completely.
2034 PeerConnectionInterface::RTCOfferAnswerOptions options;
2035 options.offer_to_receive_audio = 0;
2036 callee()->SetOfferAnswerOptions(options);
2037 } else {
2038 // Stopping the audio RtpTransceiver will cause the media section to be
2039 // rejected in the answer.
2040 callee()->SetRemoteOfferHandler([this] {
2041 callee()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_AUDIO)->Stop();
2042 });
2043 }
Steve Anton15324772018-01-16 10:26:49 -08002044 callee()->AddTrack(callee()->CreateLocalVideoTrack());
deadbeef1dcb1642017-03-29 21:08:16 -07002045 // Do offer/answer and wait for successful end-to-end video frames.
2046 caller()->CreateAndSetAndSignalOffer();
2047 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002048 MediaExpectations media_expectations;
2049 media_expectations.ExpectBidirectionalVideo();
2050 media_expectations.ExpectNoAudio();
2051 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2052
deadbeef1dcb1642017-03-29 21:08:16 -07002053 // Sanity check that the callee's description has a rejected audio section.
2054 ASSERT_NE(nullptr, callee()->pc()->local_description());
2055 const ContentInfo* callee_audio_content =
2056 GetFirstAudioContent(callee()->pc()->local_description()->description());
2057 ASSERT_NE(nullptr, callee_audio_content);
2058 EXPECT_TRUE(callee_audio_content->rejected);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002059 if (sdp_semantics_ == SdpSemantics::kUnifiedPlan) {
2060 // The caller's transceiver should have stopped after receiving the answer.
2061 EXPECT_TRUE(caller()
2062 ->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_AUDIO)
2063 ->stopped());
2064 }
deadbeef1dcb1642017-03-29 21:08:16 -07002065}
2066
2067// Test that if the answerer rejects the video m= section, no video is sent or
2068// received, but audio still can be.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002069TEST_P(PeerConnectionIntegrationTest, AnswererRejectsVideoSection) {
deadbeef1dcb1642017-03-29 21:08:16 -07002070 ASSERT_TRUE(CreatePeerConnectionWrappers());
2071 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002072 caller()->AddAudioVideoTracks();
Seth Hampson2f0d7022018-02-20 11:54:42 -08002073 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2074 // Only add audio track for callee, and set offer_to_receive_video to 0, so
2075 // it will reject the video m= section completely.
2076 PeerConnectionInterface::RTCOfferAnswerOptions options;
2077 options.offer_to_receive_video = 0;
2078 callee()->SetOfferAnswerOptions(options);
2079 } else {
2080 // Stopping the video RtpTransceiver will cause the media section to be
2081 // rejected in the answer.
2082 callee()->SetRemoteOfferHandler([this] {
2083 callee()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)->Stop();
2084 });
2085 }
Steve Anton15324772018-01-16 10:26:49 -08002086 callee()->AddTrack(callee()->CreateLocalAudioTrack());
deadbeef1dcb1642017-03-29 21:08:16 -07002087 // Do offer/answer and wait for successful end-to-end audio frames.
2088 caller()->CreateAndSetAndSignalOffer();
2089 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002090 MediaExpectations media_expectations;
2091 media_expectations.ExpectBidirectionalAudio();
2092 media_expectations.ExpectNoVideo();
2093 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2094
deadbeef1dcb1642017-03-29 21:08:16 -07002095 // Sanity check that the callee's description has a rejected video section.
2096 ASSERT_NE(nullptr, callee()->pc()->local_description());
2097 const ContentInfo* callee_video_content =
2098 GetFirstVideoContent(callee()->pc()->local_description()->description());
2099 ASSERT_NE(nullptr, callee_video_content);
2100 EXPECT_TRUE(callee_video_content->rejected);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002101 if (sdp_semantics_ == SdpSemantics::kUnifiedPlan) {
2102 // The caller's transceiver should have stopped after receiving the answer.
2103 EXPECT_TRUE(caller()
2104 ->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)
2105 ->stopped());
2106 }
deadbeef1dcb1642017-03-29 21:08:16 -07002107}
2108
2109// Test that if the answerer rejects both audio and video m= sections, nothing
2110// bad happens.
2111// TODO(deadbeef): Test that a data channel still works. Currently this doesn't
2112// test anything but the fact that negotiation succeeds, which doesn't mean
2113// much.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002114TEST_P(PeerConnectionIntegrationTest, AnswererRejectsAudioAndVideoSections) {
deadbeef1dcb1642017-03-29 21:08:16 -07002115 ASSERT_TRUE(CreatePeerConnectionWrappers());
2116 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002117 caller()->AddAudioVideoTracks();
Seth Hampson2f0d7022018-02-20 11:54:42 -08002118 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2119 // Don't give the callee any tracks, and set offer_to_receive_X to 0, so it
2120 // will reject both audio and video m= sections.
2121 PeerConnectionInterface::RTCOfferAnswerOptions options;
2122 options.offer_to_receive_audio = 0;
2123 options.offer_to_receive_video = 0;
2124 callee()->SetOfferAnswerOptions(options);
2125 } else {
2126 callee()->SetRemoteOfferHandler([this] {
2127 // Stopping all transceivers will cause all media sections to be rejected.
2128 for (auto transceiver : callee()->pc()->GetTransceivers()) {
2129 transceiver->Stop();
2130 }
2131 });
2132 }
deadbeef1dcb1642017-03-29 21:08:16 -07002133 // Do offer/answer and wait for stable signaling state.
2134 caller()->CreateAndSetAndSignalOffer();
2135 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002136
deadbeef1dcb1642017-03-29 21:08:16 -07002137 // Sanity check that the callee's description has rejected m= sections.
2138 ASSERT_NE(nullptr, callee()->pc()->local_description());
2139 const ContentInfo* callee_audio_content =
2140 GetFirstAudioContent(callee()->pc()->local_description()->description());
2141 ASSERT_NE(nullptr, callee_audio_content);
2142 EXPECT_TRUE(callee_audio_content->rejected);
2143 const ContentInfo* callee_video_content =
2144 GetFirstVideoContent(callee()->pc()->local_description()->description());
2145 ASSERT_NE(nullptr, callee_video_content);
2146 EXPECT_TRUE(callee_video_content->rejected);
2147}
2148
2149// This test sets up an audio and video call between two parties. After the
2150// call runs for a while, the caller sends an updated offer with video being
2151// rejected. Once the re-negotiation is done, the video flow should stop and
2152// the audio flow should continue.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002153TEST_P(PeerConnectionIntegrationTest, VideoRejectedInSubsequentOffer) {
deadbeef1dcb1642017-03-29 21:08:16 -07002154 ASSERT_TRUE(CreatePeerConnectionWrappers());
2155 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002156 caller()->AddAudioVideoTracks();
2157 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002158 caller()->CreateAndSetAndSignalOffer();
2159 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002160 {
2161 MediaExpectations media_expectations;
2162 media_expectations.ExpectBidirectionalAudioAndVideo();
2163 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2164 }
deadbeef1dcb1642017-03-29 21:08:16 -07002165 // Renegotiate, rejecting the video m= section.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002166 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2167 caller()->SetGeneratedSdpMunger(
2168 [](cricket::SessionDescription* description) {
2169 for (cricket::ContentInfo& content : description->contents()) {
2170 if (cricket::IsVideoContent(&content)) {
2171 content.rejected = true;
2172 }
2173 }
2174 });
2175 } else {
2176 caller()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)->Stop();
2177 }
deadbeef1dcb1642017-03-29 21:08:16 -07002178 caller()->CreateAndSetAndSignalOffer();
2179 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
2180
2181 // Sanity check that the caller's description has a rejected video section.
2182 ASSERT_NE(nullptr, caller()->pc()->local_description());
2183 const ContentInfo* caller_video_content =
2184 GetFirstVideoContent(caller()->pc()->local_description()->description());
2185 ASSERT_NE(nullptr, caller_video_content);
2186 EXPECT_TRUE(caller_video_content->rejected);
deadbeef1dcb1642017-03-29 21:08:16 -07002187 // Wait for some additional audio frames to be received.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002188 {
2189 MediaExpectations media_expectations;
2190 media_expectations.ExpectBidirectionalAudio();
2191 media_expectations.ExpectNoVideo();
2192 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2193 }
deadbeef1dcb1642017-03-29 21:08:16 -07002194}
2195
Taylor Brandstetter60c8dc82018-04-11 15:20:27 -07002196// Do one offer/answer with audio, another that disables it (rejecting the m=
2197// section), and another that re-enables it. Regression test for:
2198// bugs.webrtc.org/6023
2199TEST_F(PeerConnectionIntegrationTestPlanB, EnableAudioAfterRejecting) {
2200 ASSERT_TRUE(CreatePeerConnectionWrappers());
2201 ConnectFakeSignaling();
2202
2203 // Add audio track, do normal offer/answer.
2204 rtc::scoped_refptr<webrtc::AudioTrackInterface> track =
2205 caller()->CreateLocalAudioTrack();
2206 rtc::scoped_refptr<webrtc::RtpSenderInterface> sender =
2207 caller()->pc()->AddTrack(track, {"stream"}).MoveValue();
2208 caller()->CreateAndSetAndSignalOffer();
2209 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2210
2211 // Remove audio track, and set offer_to_receive_audio to false to cause the
2212 // m= section to be completely disabled, not just "recvonly".
2213 caller()->pc()->RemoveTrack(sender);
2214 PeerConnectionInterface::RTCOfferAnswerOptions options;
2215 options.offer_to_receive_audio = 0;
2216 caller()->SetOfferAnswerOptions(options);
2217 caller()->CreateAndSetAndSignalOffer();
2218 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2219
2220 // Add the audio track again, expecting negotiation to succeed and frames to
2221 // flow.
2222 sender = caller()->pc()->AddTrack(track, {"stream"}).MoveValue();
2223 options.offer_to_receive_audio = 1;
2224 caller()->SetOfferAnswerOptions(options);
2225 caller()->CreateAndSetAndSignalOffer();
2226 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2227
2228 MediaExpectations media_expectations;
2229 media_expectations.CalleeExpectsSomeAudio();
2230 EXPECT_TRUE(ExpectNewFrames(media_expectations));
2231}
2232
deadbeef1dcb1642017-03-29 21:08:16 -07002233// Basic end-to-end test, but without SSRC/MSID signaling. This functionality
2234// is needed to support legacy endpoints.
2235// TODO(deadbeef): When we support the MID extension and demuxing on MID, also
2236// add a test for an end-to-end test without MID signaling either (basically,
2237// the minimum acceptable SDP).
Seth Hampson2f0d7022018-02-20 11:54:42 -08002238TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithoutSsrcOrMsidSignaling) {
deadbeef1dcb1642017-03-29 21:08:16 -07002239 ASSERT_TRUE(CreatePeerConnectionWrappers());
2240 ConnectFakeSignaling();
2241 // Add audio and video, testing that packets can be demuxed on payload type.
Steve Anton15324772018-01-16 10:26:49 -08002242 caller()->AddAudioVideoTracks();
2243 callee()->AddAudioVideoTracks();
deadbeefd8ad7882017-04-18 16:01:17 -07002244 // Remove SSRCs and MSIDs from the received offer SDP.
2245 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
deadbeef1dcb1642017-03-29 21:08:16 -07002246 caller()->CreateAndSetAndSignalOffer();
2247 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002248 MediaExpectations media_expectations;
2249 media_expectations.ExpectBidirectionalAudioAndVideo();
2250 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002251}
2252
Seth Hampson5897a6e2018-04-03 11:16:33 -07002253// Basic end-to-end test, without SSRC signaling. This means that the track
2254// was created properly and frames are delivered when the MSIDs are communicated
2255// with a=msid lines and no a=ssrc lines.
2256TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
2257 EndToEndCallWithoutSsrcSignaling) {
2258 const char kStreamId[] = "streamId";
2259 ASSERT_TRUE(CreatePeerConnectionWrappers());
2260 ConnectFakeSignaling();
2261 // Add just audio tracks.
2262 caller()->AddTrack(caller()->CreateLocalAudioTrack(), {kStreamId});
2263 callee()->AddAudioTrack();
2264
2265 // Remove SSRCs from the received offer SDP.
2266 callee()->SetReceivedSdpMunger(RemoveSsrcsAndKeepMsids);
2267 caller()->CreateAndSetAndSignalOffer();
2268 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2269 MediaExpectations media_expectations;
2270 media_expectations.ExpectBidirectionalAudio();
2271 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2272}
2273
deadbeef1dcb1642017-03-29 21:08:16 -07002274// Test that if two video tracks are sent (from caller to callee, in this test),
2275// they're transmitted correctly end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002276TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithTwoVideoTracks) {
deadbeef1dcb1642017-03-29 21:08:16 -07002277 ASSERT_TRUE(CreatePeerConnectionWrappers());
2278 ConnectFakeSignaling();
2279 // Add one audio/video stream, and one video-only stream.
Steve Anton15324772018-01-16 10:26:49 -08002280 caller()->AddAudioVideoTracks();
2281 caller()->AddVideoTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07002282 caller()->CreateAndSetAndSignalOffer();
2283 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Steve Anton15324772018-01-16 10:26:49 -08002284 ASSERT_EQ(3u, callee()->pc()->GetReceivers().size());
Seth Hampson2f0d7022018-02-20 11:54:42 -08002285
2286 MediaExpectations media_expectations;
2287 media_expectations.CalleeExpectsSomeAudioAndVideo();
2288 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002289}
2290
2291static void MakeSpecCompliantMaxBundleOffer(cricket::SessionDescription* desc) {
2292 bool first = true;
2293 for (cricket::ContentInfo& content : desc->contents()) {
2294 if (first) {
2295 first = false;
2296 continue;
2297 }
2298 content.bundle_only = true;
2299 }
2300 first = true;
2301 for (cricket::TransportInfo& transport : desc->transport_infos()) {
2302 if (first) {
2303 first = false;
2304 continue;
2305 }
2306 transport.description.ice_ufrag.clear();
2307 transport.description.ice_pwd.clear();
2308 transport.description.connection_role = cricket::CONNECTIONROLE_NONE;
2309 transport.description.identity_fingerprint.reset(nullptr);
2310 }
2311}
2312
2313// Test that if applying a true "max bundle" offer, which uses ports of 0,
2314// "a=bundle-only", omitting "a=fingerprint", "a=setup", "a=ice-ufrag" and
2315// "a=ice-pwd" for all but the audio "m=" section, negotiation still completes
2316// successfully and media flows.
2317// TODO(deadbeef): Update this test to also omit "a=rtcp-mux", once that works.
2318// TODO(deadbeef): Won't need this test once we start generating actual
2319// standards-compliant SDP.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002320TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07002321 EndToEndCallWithSpecCompliantMaxBundleOffer) {
2322 ASSERT_TRUE(CreatePeerConnectionWrappers());
2323 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002324 caller()->AddAudioVideoTracks();
2325 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002326 // Do the equivalent of setting the port to 0, adding a=bundle-only, and
2327 // removing a=ice-ufrag, a=ice-pwd, a=fingerprint and a=setup from all
2328 // but the first m= section.
2329 callee()->SetReceivedSdpMunger(MakeSpecCompliantMaxBundleOffer);
2330 caller()->CreateAndSetAndSignalOffer();
2331 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002332 MediaExpectations media_expectations;
2333 media_expectations.ExpectBidirectionalAudioAndVideo();
2334 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002335}
2336
2337// Test that we can receive the audio output level from a remote audio track.
2338// TODO(deadbeef): Use a fake audio source and verify that the output level is
2339// exactly what the source on the other side was configured with.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002340TEST_P(PeerConnectionIntegrationTest, GetAudioOutputLevelStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-29 21:08:16 -07002341 ASSERT_TRUE(CreatePeerConnectionWrappers());
2342 ConnectFakeSignaling();
2343 // Just add an audio track.
Steve Anton15324772018-01-16 10:26:49 -08002344 caller()->AddAudioTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07002345 caller()->CreateAndSetAndSignalOffer();
2346 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2347
2348 // Get the audio output level stats. Note that the level is not available
2349 // until an RTCP packet has been received.
deadbeefd8ad7882017-04-18 16:01:17 -07002350 EXPECT_TRUE_WAIT(callee()->OldGetStats()->AudioOutputLevel() > 0,
deadbeef1dcb1642017-03-29 21:08:16 -07002351 kMaxWaitForFramesMs);
2352}
2353
2354// Test that an audio input level is reported.
2355// TODO(deadbeef): Use a fake audio source and verify that the input level is
2356// exactly what the source was configured with.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002357TEST_P(PeerConnectionIntegrationTest, GetAudioInputLevelStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-29 21:08:16 -07002358 ASSERT_TRUE(CreatePeerConnectionWrappers());
2359 ConnectFakeSignaling();
2360 // Just add an audio track.
Steve Anton15324772018-01-16 10:26:49 -08002361 caller()->AddAudioTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07002362 caller()->CreateAndSetAndSignalOffer();
2363 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2364
2365 // Get the audio input level stats. The level should be available very
2366 // soon after the test starts.
deadbeefd8ad7882017-04-18 16:01:17 -07002367 EXPECT_TRUE_WAIT(caller()->OldGetStats()->AudioInputLevel() > 0,
deadbeef1dcb1642017-03-29 21:08:16 -07002368 kMaxWaitForStatsMs);
2369}
2370
2371// Test that we can get incoming byte counts from both audio and video tracks.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002372TEST_P(PeerConnectionIntegrationTest, GetBytesReceivedStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-29 21:08:16 -07002373 ASSERT_TRUE(CreatePeerConnectionWrappers());
2374 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002375 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002376 // Do offer/answer, wait for the callee to receive some frames.
2377 caller()->CreateAndSetAndSignalOffer();
2378 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002379
2380 MediaExpectations media_expectations;
2381 media_expectations.CalleeExpectsSomeAudioAndVideo();
2382 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002383
2384 // Get a handle to the remote tracks created, so they can be used as GetStats
2385 // filters.
Steve Anton15324772018-01-16 10:26:49 -08002386 for (auto receiver : callee()->pc()->GetReceivers()) {
2387 // We received frames, so we definitely should have nonzero "received bytes"
2388 // stats at this point.
2389 EXPECT_GT(callee()->OldGetStatsForTrack(receiver->track())->BytesReceived(),
2390 0);
2391 }
deadbeef1dcb1642017-03-29 21:08:16 -07002392}
2393
2394// Test that we can get outgoing byte counts from both audio and video tracks.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002395TEST_P(PeerConnectionIntegrationTest, GetBytesSentStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-29 21:08:16 -07002396 ASSERT_TRUE(CreatePeerConnectionWrappers());
2397 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002398 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002399 auto audio_track = caller()->CreateLocalAudioTrack();
2400 auto video_track = caller()->CreateLocalVideoTrack();
Steve Anton15324772018-01-16 10:26:49 -08002401 caller()->AddTrack(audio_track);
2402 caller()->AddTrack(video_track);
deadbeef1dcb1642017-03-29 21:08:16 -07002403 // Do offer/answer, wait for the callee to receive some frames.
2404 caller()->CreateAndSetAndSignalOffer();
2405 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002406 MediaExpectations media_expectations;
2407 media_expectations.CalleeExpectsSomeAudioAndVideo();
2408 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002409
2410 // The callee received frames, so we definitely should have nonzero "sent
2411 // bytes" stats at this point.
deadbeefd8ad7882017-04-18 16:01:17 -07002412 EXPECT_GT(caller()->OldGetStatsForTrack(audio_track)->BytesSent(), 0);
2413 EXPECT_GT(caller()->OldGetStatsForTrack(video_track)->BytesSent(), 0);
2414}
2415
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002416// Test that we can get capture start ntp time.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002417TEST_P(PeerConnectionIntegrationTest, GetCaptureStartNtpTimeWithOldStatsApi) {
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002418 ASSERT_TRUE(CreatePeerConnectionWrappers());
2419 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002420 caller()->AddAudioTrack();
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002421
Steve Anton15324772018-01-16 10:26:49 -08002422 callee()->AddAudioTrack();
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002423
2424 // Do offer/answer, wait for the callee to receive some frames.
2425 caller()->CreateAndSetAndSignalOffer();
2426 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2427
2428 // Get the remote audio track created on the receiver, so they can be used as
2429 // GetStats filters.
Steve Antonfc853712018-03-01 13:48:58 -08002430 auto receivers = callee()->pc()->GetReceivers();
2431 ASSERT_EQ(1u, receivers.size());
2432 auto remote_audio_track = receivers[0]->track();
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002433
2434 // Get the audio output level stats. Note that the level is not available
2435 // until an RTCP packet has been received.
Zhi Huange830e682018-03-30 10:48:35 -07002436 EXPECT_TRUE_WAIT(
2437 callee()->OldGetStatsForTrack(remote_audio_track)->CaptureStartNtpTime() >
2438 0,
2439 2 * kMaxWaitForFramesMs);
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002440}
2441
deadbeefd8ad7882017-04-18 16:01:17 -07002442// Test that we can get stats (using the new stats implemnetation) for
2443// unsignaled streams. Meaning when SSRCs/MSIDs aren't signaled explicitly in
2444// SDP.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002445TEST_P(PeerConnectionIntegrationTest,
deadbeefd8ad7882017-04-18 16:01:17 -07002446 GetStatsForUnsignaledStreamWithNewStatsApi) {
2447 ASSERT_TRUE(CreatePeerConnectionWrappers());
2448 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002449 caller()->AddAudioTrack();
deadbeefd8ad7882017-04-18 16:01:17 -07002450 // Remove SSRCs and MSIDs from the received offer SDP.
2451 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
2452 caller()->CreateAndSetAndSignalOffer();
2453 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002454 MediaExpectations media_expectations;
2455 media_expectations.CalleeExpectsSomeAudio(1);
2456 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeefd8ad7882017-04-18 16:01:17 -07002457
2458 // We received a frame, so we should have nonzero "bytes received" stats for
2459 // the unsignaled stream, if stats are working for it.
2460 rtc::scoped_refptr<const webrtc::RTCStatsReport> report =
2461 callee()->NewGetStats();
2462 ASSERT_NE(nullptr, report);
2463 auto inbound_stream_stats =
2464 report->GetStatsOfType<webrtc::RTCInboundRTPStreamStats>();
2465 ASSERT_EQ(1U, inbound_stream_stats.size());
2466 ASSERT_TRUE(inbound_stream_stats[0]->bytes_received.is_defined());
2467 ASSERT_GT(*inbound_stream_stats[0]->bytes_received, 0U);
zhihuangf8164932017-05-19 13:09:47 -07002468 ASSERT_TRUE(inbound_stream_stats[0]->track_id.is_defined());
2469}
2470
2471// Test that we can successfully get the media related stats (audio level
2472// etc.) for the unsignaled stream.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002473TEST_P(PeerConnectionIntegrationTest,
zhihuangf8164932017-05-19 13:09:47 -07002474 GetMediaStatsForUnsignaledStreamWithNewStatsApi) {
2475 ASSERT_TRUE(CreatePeerConnectionWrappers());
2476 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002477 caller()->AddAudioVideoTracks();
zhihuangf8164932017-05-19 13:09:47 -07002478 // Remove SSRCs and MSIDs from the received offer SDP.
2479 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
2480 caller()->CreateAndSetAndSignalOffer();
2481 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002482 MediaExpectations media_expectations;
2483 media_expectations.CalleeExpectsSomeAudio(1);
2484 media_expectations.CalleeExpectsSomeVideo(1);
2485 ASSERT_TRUE(ExpectNewFrames(media_expectations));
zhihuangf8164932017-05-19 13:09:47 -07002486
2487 rtc::scoped_refptr<const webrtc::RTCStatsReport> report =
2488 callee()->NewGetStats();
2489 ASSERT_NE(nullptr, report);
2490
2491 auto media_stats = report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
2492 auto audio_index = FindFirstMediaStatsIndexByKind("audio", media_stats);
2493 ASSERT_GE(audio_index, 0);
2494 EXPECT_TRUE(media_stats[audio_index]->audio_level.is_defined());
deadbeef1dcb1642017-03-29 21:08:16 -07002495}
2496
deadbeef4e2deab2017-09-20 13:56:21 -07002497// Helper for test below.
2498void ModifySsrcs(cricket::SessionDescription* desc) {
2499 for (ContentInfo& content : desc->contents()) {
Steve Antonb1c1de12017-12-21 15:14:30 -08002500 for (cricket::StreamParams& stream :
2501 content.media_description()->mutable_streams()) {
deadbeef4e2deab2017-09-20 13:56:21 -07002502 for (uint32_t& ssrc : stream.ssrcs) {
2503 ssrc = rtc::CreateRandomId();
2504 }
2505 }
2506 }
2507}
2508
2509// Test that the "RTCMediaSteamTrackStats" object is updated correctly when
2510// SSRCs are unsignaled, and the SSRC of the received (audio) stream changes.
2511// This should result in two "RTCInboundRTPStreamStats", but only one
2512// "RTCMediaStreamTrackStats", whose counters go up continuously rather than
2513// being reset to 0 once the SSRC change occurs.
2514//
2515// Regression test for this bug:
2516// https://bugs.chromium.org/p/webrtc/issues/detail?id=8158
2517//
2518// The bug causes the track stats to only represent one of the two streams:
2519// whichever one has the higher SSRC. So with this bug, there was a 50% chance
2520// that the track stat counters would reset to 0 when the new stream is
2521// received, and a 50% chance that they'll stop updating (while
2522// "concealed_samples" continues increasing, due to silence being generated for
2523// the inactive stream).
Seth Hampson2f0d7022018-02-20 11:54:42 -08002524TEST_P(PeerConnectionIntegrationTest,
Steve Anton83119dd2017-11-10 16:19:52 -08002525 TrackStatsUpdatedCorrectlyWhenUnsignaledSsrcChanges) {
deadbeef4e2deab2017-09-20 13:56:21 -07002526 ASSERT_TRUE(CreatePeerConnectionWrappers());
2527 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002528 caller()->AddAudioTrack();
deadbeef4e2deab2017-09-20 13:56:21 -07002529 // Remove SSRCs and MSIDs from the received offer SDP, simulating an endpoint
2530 // that doesn't signal SSRCs (from the callee's perspective).
2531 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
2532 caller()->CreateAndSetAndSignalOffer();
2533 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2534 // Wait for 50 audio frames (500ms of audio) to be received by the callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002535 {
2536 MediaExpectations media_expectations;
2537 media_expectations.CalleeExpectsSomeAudio(50);
2538 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2539 }
deadbeef4e2deab2017-09-20 13:56:21 -07002540 // Some audio frames were received, so we should have nonzero "samples
2541 // received" for the track.
2542 rtc::scoped_refptr<const webrtc::RTCStatsReport> report =
2543 callee()->NewGetStats();
2544 ASSERT_NE(nullptr, report);
2545 auto track_stats = report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
2546 ASSERT_EQ(1U, track_stats.size());
2547 ASSERT_TRUE(track_stats[0]->total_samples_received.is_defined());
2548 ASSERT_GT(*track_stats[0]->total_samples_received, 0U);
2549 // uint64_t prev_samples_received = *track_stats[0]->total_samples_received;
2550
2551 // Create a new offer and munge it to cause the caller to use a new SSRC.
2552 caller()->SetGeneratedSdpMunger(ModifySsrcs);
2553 caller()->CreateAndSetAndSignalOffer();
2554 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2555 // Wait for 25 more audio frames (250ms of audio) to be received, from the new
2556 // SSRC.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002557 {
2558 MediaExpectations media_expectations;
2559 media_expectations.CalleeExpectsSomeAudio(25);
2560 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2561 }
deadbeef4e2deab2017-09-20 13:56:21 -07002562
2563 report = callee()->NewGetStats();
2564 ASSERT_NE(nullptr, report);
2565 track_stats = report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
2566 ASSERT_EQ(1U, track_stats.size());
2567 ASSERT_TRUE(track_stats[0]->total_samples_received.is_defined());
2568 // The "total samples received" stat should only be greater than it was
2569 // before.
2570 // TODO(deadbeef): Uncomment this assertion once the bug is completely fixed.
2571 // Right now, the new SSRC will cause the counters to reset to 0.
2572 // EXPECT_GT(*track_stats[0]->total_samples_received, prev_samples_received);
2573
2574 // Additionally, the percentage of concealed samples (samples generated to
Steve Anton83119dd2017-11-10 16:19:52 -08002575 // conceal packet loss) should be less than 50%. If it's greater, that's a
deadbeef4e2deab2017-09-20 13:56:21 -07002576 // good sign that we're seeing stats from the old stream that's no longer
2577 // receiving packets, and is generating concealed samples of silence.
Steve Anton83119dd2017-11-10 16:19:52 -08002578 constexpr double kAcceptableConcealedSamplesPercentage = 0.50;
deadbeef4e2deab2017-09-20 13:56:21 -07002579 ASSERT_TRUE(track_stats[0]->concealed_samples.is_defined());
2580 EXPECT_LT(*track_stats[0]->concealed_samples,
2581 *track_stats[0]->total_samples_received *
2582 kAcceptableConcealedSamplesPercentage);
2583
2584 // Also ensure that we have two "RTCInboundRTPStreamStats" as expected, as a
2585 // sanity check that the SSRC really changed.
2586 // TODO(deadbeef): This isn't working right now, because we're not returning
2587 // *any* stats for the inactive stream. Uncomment when the bug is completely
2588 // fixed.
2589 // auto inbound_stream_stats =
2590 // report->GetStatsOfType<webrtc::RTCInboundRTPStreamStats>();
2591 // ASSERT_EQ(2U, inbound_stream_stats.size());
2592}
2593
deadbeef1dcb1642017-03-29 21:08:16 -07002594// Test that DTLS 1.0 is used if both sides only support DTLS 1.0.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002595TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithDtls10) {
deadbeef1dcb1642017-03-29 21:08:16 -07002596 PeerConnectionFactory::Options dtls_10_options;
2597 dtls_10_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
2598 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(dtls_10_options,
2599 dtls_10_options));
2600 ConnectFakeSignaling();
2601 // Do normal offer/answer and wait for some frames to be received in each
2602 // direction.
Steve Anton15324772018-01-16 10:26:49 -08002603 caller()->AddAudioVideoTracks();
2604 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002605 caller()->CreateAndSetAndSignalOffer();
2606 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002607 MediaExpectations media_expectations;
2608 media_expectations.ExpectBidirectionalAudioAndVideo();
2609 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002610}
2611
2612// Test getting cipher stats and UMA metrics when DTLS 1.0 is negotiated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002613TEST_P(PeerConnectionIntegrationTest, Dtls10CipherStatsAndUmaMetrics) {
deadbeef1dcb1642017-03-29 21:08:16 -07002614 PeerConnectionFactory::Options dtls_10_options;
2615 dtls_10_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
2616 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(dtls_10_options,
2617 dtls_10_options));
2618 ConnectFakeSignaling();
2619 // Register UMA observer before signaling begins.
2620 rtc::scoped_refptr<webrtc::FakeMetricsObserver> caller_observer =
2621 new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
2622 caller()->pc()->RegisterUMAObserver(caller_observer);
Steve Anton15324772018-01-16 10:26:49 -08002623 caller()->AddAudioVideoTracks();
2624 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002625 caller()->CreateAndSetAndSignalOffer();
2626 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2627 EXPECT_TRUE_WAIT(rtc::SSLStreamAdapter::IsAcceptableCipher(
deadbeefd8ad7882017-04-18 16:01:17 -07002628 caller()->OldGetStats()->DtlsCipher(), rtc::KT_DEFAULT),
deadbeef1dcb1642017-03-29 21:08:16 -07002629 kDefaultTimeout);
2630 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
deadbeefd8ad7882017-04-18 16:01:17 -07002631 caller()->OldGetStats()->SrtpCipher(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07002632 EXPECT_EQ(1,
2633 caller_observer->GetEnumCounter(webrtc::kEnumCounterAudioSrtpCipher,
2634 kDefaultSrtpCryptoSuite));
2635}
2636
2637// Test getting cipher stats and UMA metrics when DTLS 1.2 is negotiated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002638TEST_P(PeerConnectionIntegrationTest, Dtls12CipherStatsAndUmaMetrics) {
deadbeef1dcb1642017-03-29 21:08:16 -07002639 PeerConnectionFactory::Options dtls_12_options;
2640 dtls_12_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
2641 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(dtls_12_options,
2642 dtls_12_options));
2643 ConnectFakeSignaling();
2644 // Register UMA observer before signaling begins.
2645 rtc::scoped_refptr<webrtc::FakeMetricsObserver> caller_observer =
2646 new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
2647 caller()->pc()->RegisterUMAObserver(caller_observer);
Steve Anton15324772018-01-16 10:26:49 -08002648 caller()->AddAudioVideoTracks();
2649 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002650 caller()->CreateAndSetAndSignalOffer();
2651 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2652 EXPECT_TRUE_WAIT(rtc::SSLStreamAdapter::IsAcceptableCipher(
deadbeefd8ad7882017-04-18 16:01:17 -07002653 caller()->OldGetStats()->DtlsCipher(), rtc::KT_DEFAULT),
deadbeef1dcb1642017-03-29 21:08:16 -07002654 kDefaultTimeout);
2655 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
deadbeefd8ad7882017-04-18 16:01:17 -07002656 caller()->OldGetStats()->SrtpCipher(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07002657 EXPECT_EQ(1,
2658 caller_observer->GetEnumCounter(webrtc::kEnumCounterAudioSrtpCipher,
2659 kDefaultSrtpCryptoSuite));
2660}
2661
2662// Test that DTLS 1.0 can be used if the caller supports DTLS 1.2 and the
2663// callee only supports 1.0.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002664TEST_P(PeerConnectionIntegrationTest, CallerDtls12ToCalleeDtls10) {
deadbeef1dcb1642017-03-29 21:08:16 -07002665 PeerConnectionFactory::Options caller_options;
2666 caller_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
2667 PeerConnectionFactory::Options callee_options;
2668 callee_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
2669 ASSERT_TRUE(
2670 CreatePeerConnectionWrappersWithOptions(caller_options, callee_options));
2671 ConnectFakeSignaling();
2672 // Do normal offer/answer and wait for some frames to be received in each
2673 // direction.
Steve Anton15324772018-01-16 10:26:49 -08002674 caller()->AddAudioVideoTracks();
2675 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002676 caller()->CreateAndSetAndSignalOffer();
2677 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002678 MediaExpectations media_expectations;
2679 media_expectations.ExpectBidirectionalAudioAndVideo();
2680 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002681}
2682
2683// Test that DTLS 1.0 can be used if the caller only supports DTLS 1.0 and the
2684// callee supports 1.2.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002685TEST_P(PeerConnectionIntegrationTest, CallerDtls10ToCalleeDtls12) {
deadbeef1dcb1642017-03-29 21:08:16 -07002686 PeerConnectionFactory::Options caller_options;
2687 caller_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
2688 PeerConnectionFactory::Options callee_options;
2689 callee_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
2690 ASSERT_TRUE(
2691 CreatePeerConnectionWrappersWithOptions(caller_options, callee_options));
2692 ConnectFakeSignaling();
2693 // Do normal offer/answer and wait for some frames to be received in each
2694 // direction.
Steve Anton15324772018-01-16 10:26:49 -08002695 caller()->AddAudioVideoTracks();
2696 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002697 caller()->CreateAndSetAndSignalOffer();
2698 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002699 MediaExpectations media_expectations;
2700 media_expectations.ExpectBidirectionalAudioAndVideo();
2701 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002702}
2703
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07002704// The three tests below verify that "enable_aes128_sha1_32_crypto_cipher"
2705// works as expected; the cipher should only be used if enabled by both sides.
2706TEST_P(PeerConnectionIntegrationTest,
2707 Aes128Sha1_32_CipherNotUsedWhenOnlyCallerSupported) {
2708 PeerConnectionFactory::Options caller_options;
2709 caller_options.crypto_options.enable_aes128_sha1_32_crypto_cipher = true;
2710 PeerConnectionFactory::Options callee_options;
2711 callee_options.crypto_options.enable_aes128_sha1_32_crypto_cipher = false;
2712 int expected_cipher_suite = rtc::SRTP_AES128_CM_SHA1_80;
2713 TestNegotiatedCipherSuite(caller_options, callee_options,
2714 expected_cipher_suite);
2715}
2716
2717TEST_P(PeerConnectionIntegrationTest,
2718 Aes128Sha1_32_CipherNotUsedWhenOnlyCalleeSupported) {
2719 PeerConnectionFactory::Options caller_options;
2720 caller_options.crypto_options.enable_aes128_sha1_32_crypto_cipher = false;
2721 PeerConnectionFactory::Options callee_options;
2722 callee_options.crypto_options.enable_aes128_sha1_32_crypto_cipher = true;
2723 int expected_cipher_suite = rtc::SRTP_AES128_CM_SHA1_80;
2724 TestNegotiatedCipherSuite(caller_options, callee_options,
2725 expected_cipher_suite);
2726}
2727
2728TEST_P(PeerConnectionIntegrationTest, Aes128Sha1_32_CipherUsedWhenSupported) {
2729 PeerConnectionFactory::Options caller_options;
2730 caller_options.crypto_options.enable_aes128_sha1_32_crypto_cipher = true;
2731 PeerConnectionFactory::Options callee_options;
2732 callee_options.crypto_options.enable_aes128_sha1_32_crypto_cipher = true;
2733 int expected_cipher_suite = rtc::SRTP_AES128_CM_SHA1_32;
2734 TestNegotiatedCipherSuite(caller_options, callee_options,
2735 expected_cipher_suite);
2736}
2737
deadbeef1dcb1642017-03-29 21:08:16 -07002738// Test that a non-GCM cipher is used if both sides only support non-GCM.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002739TEST_P(PeerConnectionIntegrationTest, NonGcmCipherUsedWhenGcmNotSupported) {
deadbeef1dcb1642017-03-29 21:08:16 -07002740 bool local_gcm_enabled = false;
2741 bool remote_gcm_enabled = false;
2742 int expected_cipher_suite = kDefaultSrtpCryptoSuite;
2743 TestGcmNegotiationUsesCipherSuite(local_gcm_enabled, remote_gcm_enabled,
2744 expected_cipher_suite);
2745}
2746
2747// Test that a GCM cipher is used if both ends support it.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002748TEST_P(PeerConnectionIntegrationTest, GcmCipherUsedWhenGcmSupported) {
deadbeef1dcb1642017-03-29 21:08:16 -07002749 bool local_gcm_enabled = true;
2750 bool remote_gcm_enabled = true;
2751 int expected_cipher_suite = kDefaultSrtpCryptoSuiteGcm;
2752 TestGcmNegotiationUsesCipherSuite(local_gcm_enabled, remote_gcm_enabled,
2753 expected_cipher_suite);
2754}
2755
2756// Test that GCM isn't used if only the offerer supports it.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002757TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07002758 NonGcmCipherUsedWhenOnlyCallerSupportsGcm) {
2759 bool local_gcm_enabled = true;
2760 bool remote_gcm_enabled = false;
2761 int expected_cipher_suite = kDefaultSrtpCryptoSuite;
2762 TestGcmNegotiationUsesCipherSuite(local_gcm_enabled, remote_gcm_enabled,
2763 expected_cipher_suite);
2764}
2765
2766// Test that GCM isn't used if only the answerer supports it.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002767TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07002768 NonGcmCipherUsedWhenOnlyCalleeSupportsGcm) {
2769 bool local_gcm_enabled = false;
2770 bool remote_gcm_enabled = true;
2771 int expected_cipher_suite = kDefaultSrtpCryptoSuite;
2772 TestGcmNegotiationUsesCipherSuite(local_gcm_enabled, remote_gcm_enabled,
2773 expected_cipher_suite);
2774}
2775
deadbeef7914b8c2017-04-21 03:23:33 -07002776// Verify that media can be transmitted end-to-end when GCM crypto suites are
2777// enabled. Note that the above tests, such as GcmCipherUsedWhenGcmSupported,
2778// only verify that a GCM cipher is negotiated, and not necessarily that SRTP
2779// works with it.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002780TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithGcmCipher) {
deadbeef7914b8c2017-04-21 03:23:33 -07002781 PeerConnectionFactory::Options gcm_options;
2782 gcm_options.crypto_options.enable_gcm_crypto_suites = true;
2783 ASSERT_TRUE(
2784 CreatePeerConnectionWrappersWithOptions(gcm_options, gcm_options));
2785 ConnectFakeSignaling();
2786 // Do normal offer/answer and wait for some frames to be received in each
2787 // direction.
Steve Anton15324772018-01-16 10:26:49 -08002788 caller()->AddAudioVideoTracks();
2789 callee()->AddAudioVideoTracks();
deadbeef7914b8c2017-04-21 03:23:33 -07002790 caller()->CreateAndSetAndSignalOffer();
2791 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002792 MediaExpectations media_expectations;
2793 media_expectations.ExpectBidirectionalAudioAndVideo();
2794 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef7914b8c2017-04-21 03:23:33 -07002795}
2796
deadbeef1dcb1642017-03-29 21:08:16 -07002797// This test sets up a call between two parties with audio, video and an RTP
2798// data channel.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002799TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithRtpDataChannel) {
deadbeef1dcb1642017-03-29 21:08:16 -07002800 FakeConstraints setup_constraints;
2801 setup_constraints.SetAllowRtpDataChannels();
2802 ASSERT_TRUE(CreatePeerConnectionWrappersWithConstraints(&setup_constraints,
2803 &setup_constraints));
2804 ConnectFakeSignaling();
2805 // Expect that data channel created on caller side will show up for callee as
2806 // well.
2807 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08002808 caller()->AddAudioVideoTracks();
2809 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002810 caller()->CreateAndSetAndSignalOffer();
2811 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2812 // Ensure the existence of the RTP data channel didn't impede audio/video.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002813 MediaExpectations media_expectations;
2814 media_expectations.ExpectBidirectionalAudioAndVideo();
2815 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002816 ASSERT_NE(nullptr, caller()->data_channel());
2817 ASSERT_NE(nullptr, callee()->data_channel());
2818 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
2819 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
2820
2821 // Ensure data can be sent in both directions.
2822 std::string data = "hello world";
2823 SendRtpDataWithRetries(caller()->data_channel(), data, 5);
2824 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
2825 kDefaultTimeout);
2826 SendRtpDataWithRetries(callee()->data_channel(), data, 5);
2827 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
2828 kDefaultTimeout);
2829}
2830
2831// Ensure that an RTP data channel is signaled as closed for the caller when
2832// the callee rejects it in a subsequent offer.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002833TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07002834 RtpDataChannelSignaledClosedInCalleeOffer) {
2835 // Same procedure as above test.
2836 FakeConstraints setup_constraints;
2837 setup_constraints.SetAllowRtpDataChannels();
2838 ASSERT_TRUE(CreatePeerConnectionWrappersWithConstraints(&setup_constraints,
2839 &setup_constraints));
2840 ConnectFakeSignaling();
2841 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08002842 caller()->AddAudioVideoTracks();
2843 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002844 caller()->CreateAndSetAndSignalOffer();
2845 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2846 ASSERT_NE(nullptr, caller()->data_channel());
2847 ASSERT_NE(nullptr, callee()->data_channel());
2848 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
2849 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
2850
2851 // Close the data channel on the callee, and do an updated offer/answer.
2852 callee()->data_channel()->Close();
2853 callee()->CreateAndSetAndSignalOffer();
2854 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2855 EXPECT_FALSE(caller()->data_observer()->IsOpen());
2856 EXPECT_FALSE(callee()->data_observer()->IsOpen());
2857}
2858
2859// Tests that data is buffered in an RTP data channel until an observer is
2860// registered for it.
2861//
2862// NOTE: RTP data channels can receive data before the underlying
2863// transport has detected that a channel is writable and thus data can be
2864// received before the data channel state changes to open. That is hard to test
2865// but the same buffering is expected to be used in that case.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002866TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07002867 DataBufferedUntilRtpDataChannelObserverRegistered) {
2868 // Use fake clock and simulated network delay so that we predictably can wait
2869 // until an SCTP message has been delivered without "sleep()"ing.
2870 rtc::ScopedFakeClock fake_clock;
2871 // Some things use a time of "0" as a special value, so we need to start out
2872 // the fake clock at a nonzero time.
2873 // TODO(deadbeef): Fix this.
2874 fake_clock.AdvanceTime(rtc::TimeDelta::FromSeconds(1));
2875 virtual_socket_server()->set_delay_mean(5); // 5 ms per hop.
2876 virtual_socket_server()->UpdateDelayDistribution();
2877
2878 FakeConstraints constraints;
2879 constraints.SetAllowRtpDataChannels();
2880 ASSERT_TRUE(
2881 CreatePeerConnectionWrappersWithConstraints(&constraints, &constraints));
2882 ConnectFakeSignaling();
2883 caller()->CreateDataChannel();
2884 caller()->CreateAndSetAndSignalOffer();
2885 ASSERT_TRUE(caller()->data_channel() != nullptr);
2886 ASSERT_TRUE_SIMULATED_WAIT(callee()->data_channel() != nullptr,
2887 kDefaultTimeout, fake_clock);
2888 ASSERT_TRUE_SIMULATED_WAIT(caller()->data_observer()->IsOpen(),
2889 kDefaultTimeout, fake_clock);
2890 ASSERT_EQ_SIMULATED_WAIT(DataChannelInterface::kOpen,
2891 callee()->data_channel()->state(), kDefaultTimeout,
2892 fake_clock);
2893
2894 // Unregister the observer which is normally automatically registered.
2895 callee()->data_channel()->UnregisterObserver();
2896 // Send data and advance fake clock until it should have been received.
2897 std::string data = "hello world";
2898 caller()->data_channel()->Send(DataBuffer(data));
2899 SIMULATED_WAIT(false, 50, fake_clock);
2900
2901 // Attach data channel and expect data to be received immediately. Note that
2902 // EXPECT_EQ_WAIT is used, such that the simulated clock is not advanced any
2903 // further, but data can be received even if the callback is asynchronous.
2904 MockDataChannelObserver new_observer(callee()->data_channel());
2905 EXPECT_EQ_SIMULATED_WAIT(data, new_observer.last_message(), kDefaultTimeout,
2906 fake_clock);
2907}
2908
2909// This test sets up a call between two parties with audio, video and but only
2910// the caller client supports RTP data channels.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002911TEST_P(PeerConnectionIntegrationTest, RtpDataChannelsRejectedByCallee) {
deadbeef1dcb1642017-03-29 21:08:16 -07002912 FakeConstraints setup_constraints_1;
2913 setup_constraints_1.SetAllowRtpDataChannels();
2914 // Must disable DTLS to make negotiation succeed.
2915 setup_constraints_1.SetMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
2916 false);
2917 FakeConstraints setup_constraints_2;
2918 setup_constraints_2.SetMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
2919 false);
2920 ASSERT_TRUE(CreatePeerConnectionWrappersWithConstraints(
2921 &setup_constraints_1, &setup_constraints_2));
2922 ConnectFakeSignaling();
2923 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08002924 caller()->AddAudioVideoTracks();
2925 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002926 caller()->CreateAndSetAndSignalOffer();
2927 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2928 // The caller should still have a data channel, but it should be closed, and
2929 // one should ever have been created for the callee.
2930 EXPECT_TRUE(caller()->data_channel() != nullptr);
2931 EXPECT_FALSE(caller()->data_observer()->IsOpen());
2932 EXPECT_EQ(nullptr, callee()->data_channel());
2933}
2934
2935// This test sets up a call between two parties with audio, and video. When
2936// audio and video is setup and flowing, an RTP data channel is negotiated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002937TEST_P(PeerConnectionIntegrationTest, AddRtpDataChannelInSubsequentOffer) {
deadbeef1dcb1642017-03-29 21:08:16 -07002938 FakeConstraints setup_constraints;
2939 setup_constraints.SetAllowRtpDataChannels();
2940 ASSERT_TRUE(CreatePeerConnectionWrappersWithConstraints(&setup_constraints,
2941 &setup_constraints));
2942 ConnectFakeSignaling();
2943 // Do initial offer/answer with audio/video.
Steve Anton15324772018-01-16 10:26:49 -08002944 caller()->AddAudioVideoTracks();
2945 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002946 caller()->CreateAndSetAndSignalOffer();
2947 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2948 // Create data channel and do new offer and answer.
2949 caller()->CreateDataChannel();
2950 caller()->CreateAndSetAndSignalOffer();
2951 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2952 ASSERT_NE(nullptr, caller()->data_channel());
2953 ASSERT_NE(nullptr, callee()->data_channel());
2954 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
2955 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
2956 // Ensure data can be sent in both directions.
2957 std::string data = "hello world";
2958 SendRtpDataWithRetries(caller()->data_channel(), data, 5);
2959 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
2960 kDefaultTimeout);
2961 SendRtpDataWithRetries(callee()->data_channel(), data, 5);
2962 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
2963 kDefaultTimeout);
2964}
2965
2966#ifdef HAVE_SCTP
2967
2968// This test sets up a call between two parties with audio, video and an SCTP
2969// data channel.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002970TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithSctpDataChannel) {
deadbeef1dcb1642017-03-29 21:08:16 -07002971 ASSERT_TRUE(CreatePeerConnectionWrappers());
2972 ConnectFakeSignaling();
2973 // Expect that data channel created on caller side will show up for callee as
2974 // well.
2975 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08002976 caller()->AddAudioVideoTracks();
2977 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002978 caller()->CreateAndSetAndSignalOffer();
2979 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2980 // Ensure the existence of the SCTP data channel didn't impede audio/video.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002981 MediaExpectations media_expectations;
2982 media_expectations.ExpectBidirectionalAudioAndVideo();
2983 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002984 // Caller data channel should already exist (it created one). Callee data
2985 // channel may not exist yet, since negotiation happens in-band, not in SDP.
2986 ASSERT_NE(nullptr, caller()->data_channel());
2987 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
2988 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
2989 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
2990
2991 // Ensure data can be sent in both directions.
2992 std::string data = "hello world";
2993 caller()->data_channel()->Send(DataBuffer(data));
2994 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
2995 kDefaultTimeout);
2996 callee()->data_channel()->Send(DataBuffer(data));
2997 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
2998 kDefaultTimeout);
2999}
3000
3001// Ensure that when the callee closes an SCTP data channel, the closing
3002// procedure results in the data channel being closed for the caller as well.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003003TEST_P(PeerConnectionIntegrationTest, CalleeClosesSctpDataChannel) {
deadbeef1dcb1642017-03-29 21:08:16 -07003004 // Same procedure as above test.
3005 ASSERT_TRUE(CreatePeerConnectionWrappers());
3006 ConnectFakeSignaling();
3007 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08003008 caller()->AddAudioVideoTracks();
3009 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003010 caller()->CreateAndSetAndSignalOffer();
3011 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3012 ASSERT_NE(nullptr, caller()->data_channel());
3013 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3014 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3015 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3016
3017 // Close the data channel on the callee side, and wait for it to reach the
3018 // "closed" state on both sides.
3019 callee()->data_channel()->Close();
3020 EXPECT_TRUE_WAIT(!caller()->data_observer()->IsOpen(), kDefaultTimeout);
3021 EXPECT_TRUE_WAIT(!callee()->data_observer()->IsOpen(), kDefaultTimeout);
3022}
3023
Seth Hampson2f0d7022018-02-20 11:54:42 -08003024TEST_P(PeerConnectionIntegrationTest, SctpDataChannelConfigSentToOtherSide) {
Steve Antonda6c0952017-10-23 11:41:54 -07003025 ASSERT_TRUE(CreatePeerConnectionWrappers());
3026 ConnectFakeSignaling();
3027 webrtc::DataChannelInit init;
3028 init.id = 53;
3029 init.maxRetransmits = 52;
3030 caller()->CreateDataChannel("data-channel", &init);
Steve Anton15324772018-01-16 10:26:49 -08003031 caller()->AddAudioVideoTracks();
3032 callee()->AddAudioVideoTracks();
Steve Antonda6c0952017-10-23 11:41:54 -07003033 caller()->CreateAndSetAndSignalOffer();
3034 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Steve Anton074dece2017-10-24 13:04:12 -07003035 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3036 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
Steve Antonda6c0952017-10-23 11:41:54 -07003037 EXPECT_EQ(init.id, callee()->data_channel()->id());
3038 EXPECT_EQ("data-channel", callee()->data_channel()->label());
3039 EXPECT_EQ(init.maxRetransmits, callee()->data_channel()->maxRetransmits());
3040 EXPECT_FALSE(callee()->data_channel()->negotiated());
3041}
3042
deadbeef1dcb1642017-03-29 21:08:16 -07003043// Test usrsctp's ability to process unordered data stream, where data actually
3044// arrives out of order using simulated delays. Previously there have been some
3045// bugs in this area.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003046TEST_P(PeerConnectionIntegrationTest, StressTestUnorderedSctpDataChannel) {
deadbeef1dcb1642017-03-29 21:08:16 -07003047 // Introduce random network delays.
3048 // Otherwise it's not a true "unordered" test.
3049 virtual_socket_server()->set_delay_mean(20);
3050 virtual_socket_server()->set_delay_stddev(5);
3051 virtual_socket_server()->UpdateDelayDistribution();
3052 // Normal procedure, but with unordered data channel config.
3053 ASSERT_TRUE(CreatePeerConnectionWrappers());
3054 ConnectFakeSignaling();
3055 webrtc::DataChannelInit init;
3056 init.ordered = false;
3057 caller()->CreateDataChannel(&init);
3058 caller()->CreateAndSetAndSignalOffer();
3059 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3060 ASSERT_NE(nullptr, caller()->data_channel());
3061 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3062 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3063 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3064
3065 static constexpr int kNumMessages = 100;
3066 // Deliberately chosen to be larger than the MTU so messages get fragmented.
3067 static constexpr size_t kMaxMessageSize = 4096;
3068 // Create and send random messages.
3069 std::vector<std::string> sent_messages;
3070 for (int i = 0; i < kNumMessages; ++i) {
3071 size_t length =
3072 (rand() % kMaxMessageSize) + 1; // NOLINT (rand_r instead of rand)
3073 std::string message;
3074 ASSERT_TRUE(rtc::CreateRandomString(length, &message));
3075 caller()->data_channel()->Send(DataBuffer(message));
3076 callee()->data_channel()->Send(DataBuffer(message));
3077 sent_messages.push_back(message);
3078 }
3079
3080 // Wait for all messages to be received.
3081 EXPECT_EQ_WAIT(kNumMessages,
3082 caller()->data_observer()->received_message_count(),
3083 kDefaultTimeout);
3084 EXPECT_EQ_WAIT(kNumMessages,
3085 callee()->data_observer()->received_message_count(),
3086 kDefaultTimeout);
3087
3088 // Sort and compare to make sure none of the messages were corrupted.
3089 std::vector<std::string> caller_received_messages =
3090 caller()->data_observer()->messages();
3091 std::vector<std::string> callee_received_messages =
3092 callee()->data_observer()->messages();
3093 std::sort(sent_messages.begin(), sent_messages.end());
3094 std::sort(caller_received_messages.begin(), caller_received_messages.end());
3095 std::sort(callee_received_messages.begin(), callee_received_messages.end());
3096 EXPECT_EQ(sent_messages, caller_received_messages);
3097 EXPECT_EQ(sent_messages, callee_received_messages);
3098}
3099
3100// This test sets up a call between two parties with audio, and video. When
3101// audio and video are setup and flowing, an SCTP data channel is negotiated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003102TEST_P(PeerConnectionIntegrationTest, AddSctpDataChannelInSubsequentOffer) {
deadbeef1dcb1642017-03-29 21:08:16 -07003103 ASSERT_TRUE(CreatePeerConnectionWrappers());
3104 ConnectFakeSignaling();
3105 // Do initial offer/answer with audio/video.
Steve Anton15324772018-01-16 10:26:49 -08003106 caller()->AddAudioVideoTracks();
3107 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003108 caller()->CreateAndSetAndSignalOffer();
3109 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3110 // Create data channel and do new offer and answer.
3111 caller()->CreateDataChannel();
3112 caller()->CreateAndSetAndSignalOffer();
3113 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3114 // Caller data channel should already exist (it created one). Callee data
3115 // channel may not exist yet, since negotiation happens in-band, not in SDP.
3116 ASSERT_NE(nullptr, caller()->data_channel());
3117 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3118 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3119 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3120 // Ensure data can be sent in both directions.
3121 std::string data = "hello world";
3122 caller()->data_channel()->Send(DataBuffer(data));
3123 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3124 kDefaultTimeout);
3125 callee()->data_channel()->Send(DataBuffer(data));
3126 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3127 kDefaultTimeout);
3128}
3129
deadbeef7914b8c2017-04-21 03:23:33 -07003130// Set up a connection initially just using SCTP data channels, later upgrading
3131// to audio/video, ensuring frames are received end-to-end. Effectively the
3132// inverse of the test above.
3133// This was broken in M57; see https://crbug.com/711243
Seth Hampson2f0d7022018-02-20 11:54:42 -08003134TEST_P(PeerConnectionIntegrationTest, SctpDataChannelToAudioVideoUpgrade) {
deadbeef7914b8c2017-04-21 03:23:33 -07003135 ASSERT_TRUE(CreatePeerConnectionWrappers());
3136 ConnectFakeSignaling();
3137 // Do initial offer/answer with just data channel.
3138 caller()->CreateDataChannel();
3139 caller()->CreateAndSetAndSignalOffer();
3140 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3141 // Wait until data can be sent over the data channel.
3142 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3143 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3144 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3145
3146 // Do subsequent offer/answer with two-way audio and video. Audio and video
3147 // should end up bundled on the DTLS/ICE transport already used for data.
Steve Anton15324772018-01-16 10:26:49 -08003148 caller()->AddAudioVideoTracks();
3149 callee()->AddAudioVideoTracks();
deadbeef7914b8c2017-04-21 03:23:33 -07003150 caller()->CreateAndSetAndSignalOffer();
3151 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08003152 MediaExpectations media_expectations;
3153 media_expectations.ExpectBidirectionalAudioAndVideo();
3154 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef7914b8c2017-04-21 03:23:33 -07003155}
3156
deadbeef8b7e9ad2017-05-25 09:38:55 -07003157static void MakeSpecCompliantSctpOffer(cricket::SessionDescription* desc) {
deadbeef8b7e9ad2017-05-25 09:38:55 -07003158 cricket::DataContentDescription* dcd_offer =
Steve Antonb1c1de12017-12-21 15:14:30 -08003159 GetFirstDataContentDescription(desc);
3160 ASSERT_TRUE(dcd_offer);
deadbeef8b7e9ad2017-05-25 09:38:55 -07003161 dcd_offer->set_use_sctpmap(false);
3162 dcd_offer->set_protocol("UDP/DTLS/SCTP");
3163}
3164
3165// Test that the data channel works when a spec-compliant SCTP m= section is
3166// offered (using "a=sctp-port" instead of "a=sctpmap", and using
3167// "UDP/DTLS/SCTP" as the protocol).
Seth Hampson2f0d7022018-02-20 11:54:42 -08003168TEST_P(PeerConnectionIntegrationTest,
deadbeef8b7e9ad2017-05-25 09:38:55 -07003169 DataChannelWorksWhenSpecCompliantSctpOfferReceived) {
3170 ASSERT_TRUE(CreatePeerConnectionWrappers());
3171 ConnectFakeSignaling();
3172 caller()->CreateDataChannel();
3173 caller()->SetGeneratedSdpMunger(MakeSpecCompliantSctpOffer);
3174 caller()->CreateAndSetAndSignalOffer();
3175 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3176 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3177 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3178 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3179
3180 // Ensure data can be sent in both directions.
3181 std::string data = "hello world";
3182 caller()->data_channel()->Send(DataBuffer(data));
3183 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3184 kDefaultTimeout);
3185 callee()->data_channel()->Send(DataBuffer(data));
3186 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3187 kDefaultTimeout);
3188}
3189
deadbeef1dcb1642017-03-29 21:08:16 -07003190#endif // HAVE_SCTP
3191
3192// Test that the ICE connection and gathering states eventually reach
3193// "complete".
Seth Hampson2f0d7022018-02-20 11:54:42 -08003194TEST_P(PeerConnectionIntegrationTest, IceStatesReachCompletion) {
deadbeef1dcb1642017-03-29 21:08:16 -07003195 ASSERT_TRUE(CreatePeerConnectionWrappers());
3196 ConnectFakeSignaling();
3197 // Do normal offer/answer.
Steve Anton15324772018-01-16 10:26:49 -08003198 caller()->AddAudioVideoTracks();
3199 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003200 caller()->CreateAndSetAndSignalOffer();
3201 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3202 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceGatheringComplete,
3203 caller()->ice_gathering_state(), kMaxWaitForFramesMs);
3204 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceGatheringComplete,
3205 callee()->ice_gathering_state(), kMaxWaitForFramesMs);
3206 // After the best candidate pair is selected and all candidates are signaled,
3207 // the ICE connection state should reach "complete".
3208 // TODO(deadbeef): Currently, the ICE "controlled" agent (the
3209 // answerer/"callee" by default) only reaches "connected". When this is
3210 // fixed, this test should be updated.
3211 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
3212 caller()->ice_connection_state(), kDefaultTimeout);
3213 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
3214 callee()->ice_connection_state(), kDefaultTimeout);
3215}
3216
Steve Antonede9ca52017-10-16 13:04:27 -07003217// Test that firewalling the ICE connection causes the clients to identify the
3218// disconnected state and then removing the firewall causes them to reconnect.
3219class PeerConnectionIntegrationIceStatesTest
Seth Hampson2f0d7022018-02-20 11:54:42 -08003220 : public PeerConnectionIntegrationBaseTest,
3221 public ::testing::WithParamInterface<
3222 std::tuple<SdpSemantics, std::tuple<std::string, uint32_t>>> {
Steve Antonede9ca52017-10-16 13:04:27 -07003223 protected:
Seth Hampson2f0d7022018-02-20 11:54:42 -08003224 PeerConnectionIntegrationIceStatesTest()
3225 : PeerConnectionIntegrationBaseTest(std::get<0>(GetParam())) {
3226 port_allocator_flags_ = std::get<1>(std::get<1>(GetParam()));
Steve Antonede9ca52017-10-16 13:04:27 -07003227 }
3228
3229 void StartStunServer(const SocketAddress& server_address) {
3230 stun_server_.reset(
3231 cricket::TestStunServer::Create(network_thread(), server_address));
3232 }
3233
3234 bool TestIPv6() {
3235 return (port_allocator_flags_ & cricket::PORTALLOCATOR_ENABLE_IPV6);
3236 }
3237
3238 void SetPortAllocatorFlags() {
Patrik Höglund3dc41062018-04-11 11:13:57 +00003239 caller()->port_allocator()->set_flags(port_allocator_flags_);
3240 callee()->port_allocator()->set_flags(port_allocator_flags_);
Steve Antonede9ca52017-10-16 13:04:27 -07003241 }
3242
3243 std::vector<SocketAddress> CallerAddresses() {
3244 std::vector<SocketAddress> addresses;
3245 addresses.push_back(SocketAddress("1.1.1.1", 0));
3246 if (TestIPv6()) {
3247 addresses.push_back(SocketAddress("1111:0:a:b:c:d:e:f", 0));
3248 }
3249 return addresses;
3250 }
3251
3252 std::vector<SocketAddress> CalleeAddresses() {
3253 std::vector<SocketAddress> addresses;
3254 addresses.push_back(SocketAddress("2.2.2.2", 0));
3255 if (TestIPv6()) {
3256 addresses.push_back(SocketAddress("2222:0:a:b:c:d:e:f", 0));
3257 }
3258 return addresses;
3259 }
3260
3261 void SetUpNetworkInterfaces() {
3262 // Remove the default interfaces added by the test infrastructure.
3263 caller()->network()->RemoveInterface(kDefaultLocalAddress);
3264 callee()->network()->RemoveInterface(kDefaultLocalAddress);
3265
3266 // Add network addresses for test.
3267 for (const auto& caller_address : CallerAddresses()) {
3268 caller()->network()->AddInterface(caller_address);
3269 }
3270 for (const auto& callee_address : CalleeAddresses()) {
3271 callee()->network()->AddInterface(callee_address);
3272 }
3273 }
3274
3275 private:
3276 uint32_t port_allocator_flags_;
3277 std::unique_ptr<cricket::TestStunServer> stun_server_;
3278};
3279
3280// Tests that the PeerConnection goes through all the ICE gathering/connection
3281// states over the duration of the call. This includes Disconnected and Failed
3282// states, induced by putting a firewall between the peers and waiting for them
3283// to time out.
Steve Anton83119dd2017-11-10 16:19:52 -08003284TEST_P(PeerConnectionIntegrationIceStatesTest, VerifyIceStates) {
3285 // TODO(bugs.webrtc.org/8295): When using a ScopedFakeClock, this test will
3286 // sometimes hit a DCHECK in platform_thread.cc about the PacerThread being
3287 // too busy. For now, revert to running without a fake clock.
Steve Antonede9ca52017-10-16 13:04:27 -07003288
3289 const SocketAddress kStunServerAddress =
3290 SocketAddress("99.99.99.1", cricket::STUN_SERVER_PORT);
3291 StartStunServer(kStunServerAddress);
3292
3293 PeerConnectionInterface::RTCConfiguration config;
3294 PeerConnectionInterface::IceServer ice_stun_server;
3295 ice_stun_server.urls.push_back(
3296 "stun:" + kStunServerAddress.HostAsURIString() + ":" +
3297 kStunServerAddress.PortAsString());
3298 config.servers.push_back(ice_stun_server);
3299
3300 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
3301 ConnectFakeSignaling();
3302 SetPortAllocatorFlags();
3303 SetUpNetworkInterfaces();
Steve Anton15324772018-01-16 10:26:49 -08003304 caller()->AddAudioVideoTracks();
3305 callee()->AddAudioVideoTracks();
Steve Antonede9ca52017-10-16 13:04:27 -07003306
3307 // Initial state before anything happens.
3308 ASSERT_EQ(PeerConnectionInterface::kIceGatheringNew,
3309 caller()->ice_gathering_state());
3310 ASSERT_EQ(PeerConnectionInterface::kIceConnectionNew,
3311 caller()->ice_connection_state());
3312
3313 // Start the call by creating the offer, setting it as the local description,
3314 // then sending it to the peer who will respond with an answer. This happens
3315 // asynchronously so that we can watch the states as it runs in the
3316 // background.
3317 caller()->CreateAndSetAndSignalOffer();
3318
Steve Anton83119dd2017-11-10 16:19:52 -08003319 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceConnectionCompleted,
3320 caller()->ice_connection_state(), kDefaultTimeout);
Steve Antonede9ca52017-10-16 13:04:27 -07003321
3322 // Verify that the observer was notified of the intermediate transitions.
3323 EXPECT_THAT(caller()->ice_connection_state_history(),
3324 ElementsAre(PeerConnectionInterface::kIceConnectionChecking,
3325 PeerConnectionInterface::kIceConnectionConnected,
3326 PeerConnectionInterface::kIceConnectionCompleted));
3327 EXPECT_THAT(caller()->ice_gathering_state_history(),
3328 ElementsAre(PeerConnectionInterface::kIceGatheringGathering,
3329 PeerConnectionInterface::kIceGatheringComplete));
3330
3331 // Block connections to/from the caller and wait for ICE to become
3332 // disconnected.
3333 for (const auto& caller_address : CallerAddresses()) {
3334 firewall()->AddRule(false, rtc::FP_ANY, rtc::FD_ANY, caller_address);
3335 }
Mirko Bonadei675513b2017-11-09 11:09:25 +01003336 RTC_LOG(LS_INFO) << "Firewall rules applied";
Steve Anton83119dd2017-11-10 16:19:52 -08003337 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceConnectionDisconnected,
3338 caller()->ice_connection_state(), kDefaultTimeout);
Steve Antonede9ca52017-10-16 13:04:27 -07003339
3340 // Let ICE re-establish by removing the firewall rules.
3341 firewall()->ClearRules();
Mirko Bonadei675513b2017-11-09 11:09:25 +01003342 RTC_LOG(LS_INFO) << "Firewall rules cleared";
Steve Anton83119dd2017-11-10 16:19:52 -08003343 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceConnectionCompleted,
3344 caller()->ice_connection_state(), kDefaultTimeout);
Steve Antonede9ca52017-10-16 13:04:27 -07003345
3346 // According to RFC7675, if there is no response within 30 seconds then the
3347 // peer should consider the other side to have rejected the connection. This
Steve Anton83119dd2017-11-10 16:19:52 -08003348 // is signaled by the state transitioning to "failed".
Steve Antonede9ca52017-10-16 13:04:27 -07003349 constexpr int kConsentTimeout = 30000;
3350 for (const auto& caller_address : CallerAddresses()) {
3351 firewall()->AddRule(false, rtc::FP_ANY, rtc::FD_ANY, caller_address);
3352 }
Mirko Bonadei675513b2017-11-09 11:09:25 +01003353 RTC_LOG(LS_INFO) << "Firewall rules applied again";
Steve Anton83119dd2017-11-10 16:19:52 -08003354 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceConnectionFailed,
3355 caller()->ice_connection_state(), kConsentTimeout);
Steve Antonede9ca52017-10-16 13:04:27 -07003356}
3357
3358// Tests that the best connection is set to the appropriate IPv4/IPv6 connection
3359// and that the statistics in the metric observers are updated correctly.
3360TEST_P(PeerConnectionIntegrationIceStatesTest, VerifyBestConnection) {
3361 ASSERT_TRUE(CreatePeerConnectionWrappers());
3362 ConnectFakeSignaling();
3363 SetPortAllocatorFlags();
3364 SetUpNetworkInterfaces();
Steve Anton15324772018-01-16 10:26:49 -08003365 caller()->AddAudioVideoTracks();
3366 callee()->AddAudioVideoTracks();
Steve Antonede9ca52017-10-16 13:04:27 -07003367
3368 rtc::scoped_refptr<webrtc::FakeMetricsObserver> metrics_observer(
3369 new rtc::RefCountedObject<webrtc::FakeMetricsObserver>());
3370 caller()->pc()->RegisterUMAObserver(metrics_observer.get());
3371
3372 caller()->CreateAndSetAndSignalOffer();
3373
3374 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3375
3376 const int num_best_ipv4 = metrics_observer->GetEnumCounter(
3377 webrtc::kEnumCounterAddressFamily, webrtc::kBestConnections_IPv4);
3378 const int num_best_ipv6 = metrics_observer->GetEnumCounter(
3379 webrtc::kEnumCounterAddressFamily, webrtc::kBestConnections_IPv6);
3380 if (TestIPv6()) {
3381 // When IPv6 is enabled, we should prefer an IPv6 connection over an IPv4
3382 // connection.
3383 EXPECT_EQ(0u, num_best_ipv4);
3384 EXPECT_EQ(1u, num_best_ipv6);
3385 } else {
3386 EXPECT_EQ(1u, num_best_ipv4);
3387 EXPECT_EQ(0u, num_best_ipv6);
3388 }
3389
3390 EXPECT_EQ(0u, metrics_observer->GetEnumCounter(
3391 webrtc::kEnumCounterIceCandidatePairTypeUdp,
3392 webrtc::kIceCandidatePairHostHost));
3393 EXPECT_EQ(1u, metrics_observer->GetEnumCounter(
3394 webrtc::kEnumCounterIceCandidatePairTypeUdp,
3395 webrtc::kIceCandidatePairHostPublicHostPublic));
3396}
3397
3398constexpr uint32_t kFlagsIPv4NoStun = cricket::PORTALLOCATOR_DISABLE_TCP |
3399 cricket::PORTALLOCATOR_DISABLE_STUN |
3400 cricket::PORTALLOCATOR_DISABLE_RELAY;
3401constexpr uint32_t kFlagsIPv6NoStun =
3402 cricket::PORTALLOCATOR_DISABLE_TCP | cricket::PORTALLOCATOR_DISABLE_STUN |
3403 cricket::PORTALLOCATOR_ENABLE_IPV6 | cricket::PORTALLOCATOR_DISABLE_RELAY;
3404constexpr uint32_t kFlagsIPv4Stun =
3405 cricket::PORTALLOCATOR_DISABLE_TCP | cricket::PORTALLOCATOR_DISABLE_RELAY;
3406
Seth Hampson2f0d7022018-02-20 11:54:42 -08003407INSTANTIATE_TEST_CASE_P(
3408 PeerConnectionIntegrationTest,
3409 PeerConnectionIntegrationIceStatesTest,
3410 Combine(Values(SdpSemantics::kPlanB, SdpSemantics::kUnifiedPlan),
3411 Values(std::make_pair("IPv4 no STUN", kFlagsIPv4NoStun),
3412 std::make_pair("IPv6 no STUN", kFlagsIPv6NoStun),
3413 std::make_pair("IPv4 with STUN", kFlagsIPv4Stun))));
Steve Antonede9ca52017-10-16 13:04:27 -07003414
deadbeef1dcb1642017-03-29 21:08:16 -07003415// This test sets up a call between two parties with audio and video.
3416// During the call, the caller restarts ICE and the test verifies that
3417// new ICE candidates are generated and audio and video still can flow, and the
3418// ICE state reaches completed again.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003419TEST_P(PeerConnectionIntegrationTest, MediaContinuesFlowingAfterIceRestart) {
deadbeef1dcb1642017-03-29 21:08:16 -07003420 ASSERT_TRUE(CreatePeerConnectionWrappers());
3421 ConnectFakeSignaling();
3422 // Do normal offer/answer and wait for ICE to complete.
Steve Anton15324772018-01-16 10:26:49 -08003423 caller()->AddAudioVideoTracks();
3424 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003425 caller()->CreateAndSetAndSignalOffer();
3426 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3427 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
3428 caller()->ice_connection_state(), kMaxWaitForFramesMs);
3429 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
3430 callee()->ice_connection_state(), kMaxWaitForFramesMs);
3431
3432 // To verify that the ICE restart actually occurs, get
3433 // ufrag/password/candidates before and after restart.
3434 // Create an SDP string of the first audio candidate for both clients.
3435 const webrtc::IceCandidateCollection* audio_candidates_caller =
3436 caller()->pc()->local_description()->candidates(0);
3437 const webrtc::IceCandidateCollection* audio_candidates_callee =
3438 callee()->pc()->local_description()->candidates(0);
3439 ASSERT_GT(audio_candidates_caller->count(), 0u);
3440 ASSERT_GT(audio_candidates_callee->count(), 0u);
3441 std::string caller_candidate_pre_restart;
3442 ASSERT_TRUE(
3443 audio_candidates_caller->at(0)->ToString(&caller_candidate_pre_restart));
3444 std::string callee_candidate_pre_restart;
3445 ASSERT_TRUE(
3446 audio_candidates_callee->at(0)->ToString(&callee_candidate_pre_restart));
3447 const cricket::SessionDescription* desc =
3448 caller()->pc()->local_description()->description();
3449 std::string caller_ufrag_pre_restart =
3450 desc->transport_infos()[0].description.ice_ufrag;
3451 desc = callee()->pc()->local_description()->description();
3452 std::string callee_ufrag_pre_restart =
3453 desc->transport_infos()[0].description.ice_ufrag;
3454
3455 // Have the caller initiate an ICE restart.
3456 caller()->SetOfferAnswerOptions(IceRestartOfferAnswerOptions());
3457 caller()->CreateAndSetAndSignalOffer();
3458 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3459 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
3460 caller()->ice_connection_state(), kMaxWaitForFramesMs);
3461 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
3462 callee()->ice_connection_state(), kMaxWaitForFramesMs);
3463
3464 // Grab the ufrags/candidates again.
3465 audio_candidates_caller = caller()->pc()->local_description()->candidates(0);
3466 audio_candidates_callee = callee()->pc()->local_description()->candidates(0);
3467 ASSERT_GT(audio_candidates_caller->count(), 0u);
3468 ASSERT_GT(audio_candidates_callee->count(), 0u);
3469 std::string caller_candidate_post_restart;
3470 ASSERT_TRUE(
3471 audio_candidates_caller->at(0)->ToString(&caller_candidate_post_restart));
3472 std::string callee_candidate_post_restart;
3473 ASSERT_TRUE(
3474 audio_candidates_callee->at(0)->ToString(&callee_candidate_post_restart));
3475 desc = caller()->pc()->local_description()->description();
3476 std::string caller_ufrag_post_restart =
3477 desc->transport_infos()[0].description.ice_ufrag;
3478 desc = callee()->pc()->local_description()->description();
3479 std::string callee_ufrag_post_restart =
3480 desc->transport_infos()[0].description.ice_ufrag;
3481 // Sanity check that an ICE restart was actually negotiated in SDP.
3482 ASSERT_NE(caller_candidate_pre_restart, caller_candidate_post_restart);
3483 ASSERT_NE(callee_candidate_pre_restart, callee_candidate_post_restart);
3484 ASSERT_NE(caller_ufrag_pre_restart, caller_ufrag_post_restart);
3485 ASSERT_NE(callee_ufrag_pre_restart, callee_ufrag_post_restart);
3486
3487 // Ensure that additional frames are received after the ICE restart.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003488 MediaExpectations media_expectations;
3489 media_expectations.ExpectBidirectionalAudioAndVideo();
3490 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003491}
3492
3493// Verify that audio/video can be received end-to-end when ICE renomination is
3494// enabled.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003495TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithIceRenomination) {
deadbeef1dcb1642017-03-29 21:08:16 -07003496 PeerConnectionInterface::RTCConfiguration config;
3497 config.enable_ice_renomination = true;
3498 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
3499 ConnectFakeSignaling();
3500 // Do normal offer/answer and wait for some frames to be received in each
3501 // direction.
Steve Anton15324772018-01-16 10:26:49 -08003502 caller()->AddAudioVideoTracks();
3503 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003504 caller()->CreateAndSetAndSignalOffer();
3505 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3506 // Sanity check that ICE renomination was actually negotiated.
3507 const cricket::SessionDescription* desc =
3508 caller()->pc()->local_description()->description();
3509 for (const cricket::TransportInfo& info : desc->transport_infos()) {
deadbeef30952b42017-04-21 02:41:29 -07003510 ASSERT_NE(
3511 info.description.transport_options.end(),
3512 std::find(info.description.transport_options.begin(),
3513 info.description.transport_options.end(), "renomination"));
deadbeef1dcb1642017-03-29 21:08:16 -07003514 }
3515 desc = callee()->pc()->local_description()->description();
3516 for (const cricket::TransportInfo& info : desc->transport_infos()) {
deadbeef30952b42017-04-21 02:41:29 -07003517 ASSERT_NE(
3518 info.description.transport_options.end(),
3519 std::find(info.description.transport_options.begin(),
3520 info.description.transport_options.end(), "renomination"));
deadbeef1dcb1642017-03-29 21:08:16 -07003521 }
Seth Hampson2f0d7022018-02-20 11:54:42 -08003522 MediaExpectations media_expectations;
3523 media_expectations.ExpectBidirectionalAudioAndVideo();
3524 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003525}
3526
Steve Anton6f25b092017-10-23 09:39:20 -07003527// With a max bundle policy and RTCP muxing, adding a new media description to
3528// the connection should not affect ICE at all because the new media will use
3529// the existing connection.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003530TEST_P(PeerConnectionIntegrationTest,
Steve Anton83119dd2017-11-10 16:19:52 -08003531 AddMediaToConnectedBundleDoesNotRestartIce) {
Steve Anton6f25b092017-10-23 09:39:20 -07003532 PeerConnectionInterface::RTCConfiguration config;
3533 config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
3534 config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyRequire;
3535 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(
3536 config, PeerConnectionInterface::RTCConfiguration()));
3537 ConnectFakeSignaling();
3538
Steve Anton15324772018-01-16 10:26:49 -08003539 caller()->AddAudioTrack();
Steve Anton6f25b092017-10-23 09:39:20 -07003540 caller()->CreateAndSetAndSignalOffer();
3541 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Steve Antonff52f1b2017-10-26 12:24:50 -07003542 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceConnectionCompleted,
3543 caller()->ice_connection_state(), kDefaultTimeout);
Steve Anton6f25b092017-10-23 09:39:20 -07003544
3545 caller()->clear_ice_connection_state_history();
3546
Steve Anton15324772018-01-16 10:26:49 -08003547 caller()->AddVideoTrack();
Steve Anton6f25b092017-10-23 09:39:20 -07003548 caller()->CreateAndSetAndSignalOffer();
3549 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3550
3551 EXPECT_EQ(0u, caller()->ice_connection_state_history().size());
3552}
3553
deadbeef1dcb1642017-03-29 21:08:16 -07003554// This test sets up a call between two parties with audio and video. It then
3555// renegotiates setting the video m-line to "port 0", then later renegotiates
3556// again, enabling video.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003557TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07003558 VideoFlowsAfterMediaSectionIsRejectedAndRecycled) {
3559 ASSERT_TRUE(CreatePeerConnectionWrappers());
3560 ConnectFakeSignaling();
3561
3562 // Do initial negotiation, only sending media from the caller. Will result in
3563 // video and audio recvonly "m=" sections.
Steve Anton15324772018-01-16 10:26:49 -08003564 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003565 caller()->CreateAndSetAndSignalOffer();
3566 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3567
3568 // Negotiate again, disabling the video "m=" section (the callee will set the
3569 // port to 0 due to offer_to_receive_video = 0).
Seth Hampson2f0d7022018-02-20 11:54:42 -08003570 if (sdp_semantics_ == SdpSemantics::kPlanB) {
3571 PeerConnectionInterface::RTCOfferAnswerOptions options;
3572 options.offer_to_receive_video = 0;
3573 callee()->SetOfferAnswerOptions(options);
3574 } else {
3575 callee()->SetRemoteOfferHandler([this] {
3576 callee()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)->Stop();
3577 });
3578 }
deadbeef1dcb1642017-03-29 21:08:16 -07003579 caller()->CreateAndSetAndSignalOffer();
3580 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3581 // Sanity check that video "m=" section was actually rejected.
3582 const ContentInfo* answer_video_content = cricket::GetFirstVideoContent(
3583 callee()->pc()->local_description()->description());
3584 ASSERT_NE(nullptr, answer_video_content);
3585 ASSERT_TRUE(answer_video_content->rejected);
3586
3587 // Enable video and do negotiation again, making sure video is received
3588 // end-to-end, also adding media stream to callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003589 if (sdp_semantics_ == SdpSemantics::kPlanB) {
3590 PeerConnectionInterface::RTCOfferAnswerOptions options;
3591 options.offer_to_receive_video = 1;
3592 callee()->SetOfferAnswerOptions(options);
3593 } else {
3594 // The caller's transceiver is stopped, so we need to add another track.
3595 auto caller_transceiver =
3596 caller()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO);
3597 EXPECT_TRUE(caller_transceiver->stopped());
3598 caller()->AddVideoTrack();
3599 }
3600 callee()->AddVideoTrack();
3601 callee()->SetRemoteOfferHandler(nullptr);
deadbeef1dcb1642017-03-29 21:08:16 -07003602 caller()->CreateAndSetAndSignalOffer();
3603 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08003604
deadbeef1dcb1642017-03-29 21:08:16 -07003605 // Verify the caller receives frames from the newly added stream, and the
3606 // callee receives additional frames from the re-enabled video m= section.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003607 MediaExpectations media_expectations;
3608 media_expectations.CalleeExpectsSomeAudio();
3609 media_expectations.ExpectBidirectionalVideo();
3610 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003611}
3612
3613// This test sets up a Jsep call between two parties with external
3614// VideoDecoderFactory.
3615// TODO(holmer): Disabled due to sometimes crashing on buildbots.
3616// See issue webrtc/2378.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003617TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07003618 DISABLED_EndToEndCallWithVideoDecoderFactory) {
3619 ASSERT_TRUE(CreatePeerConnectionWrappers());
3620 EnableVideoDecoderFactory();
3621 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08003622 caller()->AddAudioVideoTracks();
3623 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003624 caller()->CreateAndSetAndSignalOffer();
3625 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08003626 MediaExpectations media_expectations;
3627 media_expectations.ExpectBidirectionalAudioAndVideo();
3628 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003629}
3630
3631// This tests that if we negotiate after calling CreateSender but before we
3632// have a track, then set a track later, frames from the newly-set track are
3633// received end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003634TEST_F(PeerConnectionIntegrationTestPlanB,
deadbeef1dcb1642017-03-29 21:08:16 -07003635 MediaFlowsAfterEarlyWarmupWithCreateSender) {
3636 ASSERT_TRUE(CreatePeerConnectionWrappers());
3637 ConnectFakeSignaling();
3638 auto caller_audio_sender =
3639 caller()->pc()->CreateSender("audio", "caller_stream");
3640 auto caller_video_sender =
3641 caller()->pc()->CreateSender("video", "caller_stream");
3642 auto callee_audio_sender =
3643 callee()->pc()->CreateSender("audio", "callee_stream");
3644 auto callee_video_sender =
3645 callee()->pc()->CreateSender("video", "callee_stream");
3646 caller()->CreateAndSetAndSignalOffer();
3647 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
3648 // Wait for ICE to complete, without any tracks being set.
3649 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
3650 caller()->ice_connection_state(), kMaxWaitForFramesMs);
3651 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
3652 callee()->ice_connection_state(), kMaxWaitForFramesMs);
3653 // Now set the tracks, and expect frames to immediately start flowing.
3654 EXPECT_TRUE(caller_audio_sender->SetTrack(caller()->CreateLocalAudioTrack()));
3655 EXPECT_TRUE(caller_video_sender->SetTrack(caller()->CreateLocalVideoTrack()));
3656 EXPECT_TRUE(callee_audio_sender->SetTrack(callee()->CreateLocalAudioTrack()));
3657 EXPECT_TRUE(callee_video_sender->SetTrack(callee()->CreateLocalVideoTrack()));
Seth Hampson2f0d7022018-02-20 11:54:42 -08003658 MediaExpectations media_expectations;
3659 media_expectations.ExpectBidirectionalAudioAndVideo();
3660 ASSERT_TRUE(ExpectNewFrames(media_expectations));
3661}
3662
3663// This tests that if we negotiate after calling AddTransceiver but before we
3664// have a track, then set a track later, frames from the newly-set tracks are
3665// received end-to-end.
3666TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
3667 MediaFlowsAfterEarlyWarmupWithAddTransceiver) {
3668 ASSERT_TRUE(CreatePeerConnectionWrappers());
3669 ConnectFakeSignaling();
3670 auto audio_result = caller()->pc()->AddTransceiver(cricket::MEDIA_TYPE_AUDIO);
3671 ASSERT_EQ(RTCErrorType::NONE, audio_result.error().type());
3672 auto caller_audio_sender = audio_result.MoveValue()->sender();
3673 auto video_result = caller()->pc()->AddTransceiver(cricket::MEDIA_TYPE_VIDEO);
3674 ASSERT_EQ(RTCErrorType::NONE, video_result.error().type());
3675 auto caller_video_sender = video_result.MoveValue()->sender();
3676 callee()->SetRemoteOfferHandler([this] {
3677 ASSERT_EQ(2u, callee()->pc()->GetTransceivers().size());
3678 callee()->pc()->GetTransceivers()[0]->SetDirection(
3679 RtpTransceiverDirection::kSendRecv);
3680 callee()->pc()->GetTransceivers()[1]->SetDirection(
3681 RtpTransceiverDirection::kSendRecv);
3682 });
3683 caller()->CreateAndSetAndSignalOffer();
3684 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
3685 // Wait for ICE to complete, without any tracks being set.
3686 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
3687 caller()->ice_connection_state(), kMaxWaitForFramesMs);
3688 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
3689 callee()->ice_connection_state(), kMaxWaitForFramesMs);
3690 // Now set the tracks, and expect frames to immediately start flowing.
3691 auto callee_audio_sender = callee()->pc()->GetSenders()[0];
3692 auto callee_video_sender = callee()->pc()->GetSenders()[1];
3693 ASSERT_TRUE(caller_audio_sender->SetTrack(caller()->CreateLocalAudioTrack()));
3694 ASSERT_TRUE(caller_video_sender->SetTrack(caller()->CreateLocalVideoTrack()));
3695 ASSERT_TRUE(callee_audio_sender->SetTrack(callee()->CreateLocalAudioTrack()));
3696 ASSERT_TRUE(callee_video_sender->SetTrack(callee()->CreateLocalVideoTrack()));
3697 MediaExpectations media_expectations;
3698 media_expectations.ExpectBidirectionalAudioAndVideo();
3699 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003700}
3701
3702// This test verifies that a remote video track can be added via AddStream,
3703// and sent end-to-end. For this particular test, it's simply echoed back
3704// from the caller to the callee, rather than being forwarded to a third
3705// PeerConnection.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003706TEST_F(PeerConnectionIntegrationTestPlanB, CanSendRemoteVideoTrack) {
deadbeef1dcb1642017-03-29 21:08:16 -07003707 ASSERT_TRUE(CreatePeerConnectionWrappers());
3708 ConnectFakeSignaling();
3709 // Just send a video track from the caller.
Steve Anton15324772018-01-16 10:26:49 -08003710 caller()->AddVideoTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07003711 caller()->CreateAndSetAndSignalOffer();
3712 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
3713 ASSERT_EQ(1, callee()->remote_streams()->count());
3714
3715 // Echo the stream back, and do a new offer/anwer (initiated by callee this
3716 // time).
3717 callee()->pc()->AddStream(callee()->remote_streams()->at(0));
3718 callee()->CreateAndSetAndSignalOffer();
3719 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
3720
Seth Hampson2f0d7022018-02-20 11:54:42 -08003721 MediaExpectations media_expectations;
3722 media_expectations.ExpectBidirectionalVideo();
3723 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003724}
3725
3726// Test that we achieve the expected end-to-end connection time, using a
3727// fake clock and simulated latency on the media and signaling paths.
3728// We use a TURN<->TURN connection because this is usually the quickest to
3729// set up initially, especially when we're confident the connection will work
3730// and can start sending media before we get a STUN response.
3731//
3732// With various optimizations enabled, here are the network delays we expect to
3733// be on the critical path:
3734// 1. 2 signaling trips: Signaling offer and offerer's TURN candidate, then
3735// signaling answer (with DTLS fingerprint).
3736// 2. 9 media hops: Rest of the DTLS handshake. 3 hops in each direction when
3737// using TURN<->TURN pair, and DTLS exchange is 4 packets,
3738// the first of which should have arrived before the answer.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003739TEST_P(PeerConnectionIntegrationTest, EndToEndConnectionTimeWithTurnTurnPair) {
deadbeef1dcb1642017-03-29 21:08:16 -07003740 rtc::ScopedFakeClock fake_clock;
3741 // Some things use a time of "0" as a special value, so we need to start out
3742 // the fake clock at a nonzero time.
3743 // TODO(deadbeef): Fix this.
3744 fake_clock.AdvanceTime(rtc::TimeDelta::FromSeconds(1));
3745
3746 static constexpr int media_hop_delay_ms = 50;
3747 static constexpr int signaling_trip_delay_ms = 500;
3748 // For explanation of these values, see comment above.
3749 static constexpr int required_media_hops = 9;
3750 static constexpr int required_signaling_trips = 2;
3751 // For internal delays (such as posting an event asychronously).
3752 static constexpr int allowed_internal_delay_ms = 20;
3753 static constexpr int total_connection_time_ms =
3754 media_hop_delay_ms * required_media_hops +
3755 signaling_trip_delay_ms * required_signaling_trips +
3756 allowed_internal_delay_ms;
3757
3758 static const rtc::SocketAddress turn_server_1_internal_address{"88.88.88.0",
3759 3478};
3760 static const rtc::SocketAddress turn_server_1_external_address{"88.88.88.1",
3761 0};
3762 static const rtc::SocketAddress turn_server_2_internal_address{"99.99.99.0",
3763 3478};
3764 static const rtc::SocketAddress turn_server_2_external_address{"99.99.99.1",
3765 0};
3766 cricket::TestTurnServer turn_server_1(network_thread(),
3767 turn_server_1_internal_address,
3768 turn_server_1_external_address);
3769 cricket::TestTurnServer turn_server_2(network_thread(),
3770 turn_server_2_internal_address,
3771 turn_server_2_external_address);
Jonas Orelandbdcee282017-10-10 14:01:40 +02003772
deadbeef1dcb1642017-03-29 21:08:16 -07003773 // Bypass permission check on received packets so media can be sent before
3774 // the candidate is signaled.
3775 turn_server_1.set_enable_permission_checks(false);
3776 turn_server_2.set_enable_permission_checks(false);
3777
3778 PeerConnectionInterface::RTCConfiguration client_1_config;
3779 webrtc::PeerConnectionInterface::IceServer ice_server_1;
3780 ice_server_1.urls.push_back("turn:88.88.88.0:3478");
3781 ice_server_1.username = "test";
3782 ice_server_1.password = "test";
3783 client_1_config.servers.push_back(ice_server_1);
3784 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
3785 client_1_config.presume_writable_when_fully_relayed = true;
3786
3787 PeerConnectionInterface::RTCConfiguration client_2_config;
3788 webrtc::PeerConnectionInterface::IceServer ice_server_2;
3789 ice_server_2.urls.push_back("turn:99.99.99.0:3478");
3790 ice_server_2.username = "test";
3791 ice_server_2.password = "test";
3792 client_2_config.servers.push_back(ice_server_2);
3793 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
3794 client_2_config.presume_writable_when_fully_relayed = true;
3795
3796 ASSERT_TRUE(
3797 CreatePeerConnectionWrappersWithConfig(client_1_config, client_2_config));
3798 // Set up the simulated delays.
3799 SetSignalingDelayMs(signaling_trip_delay_ms);
3800 ConnectFakeSignaling();
3801 virtual_socket_server()->set_delay_mean(media_hop_delay_ms);
3802 virtual_socket_server()->UpdateDelayDistribution();
3803
3804 // Set "offer to receive audio/video" without adding any tracks, so we just
3805 // set up ICE/DTLS with no media.
3806 PeerConnectionInterface::RTCOfferAnswerOptions options;
3807 options.offer_to_receive_audio = 1;
3808 options.offer_to_receive_video = 1;
3809 caller()->SetOfferAnswerOptions(options);
3810 caller()->CreateAndSetAndSignalOffer();
deadbeef71452802017-05-07 17:21:01 -07003811 EXPECT_TRUE_SIMULATED_WAIT(DtlsConnected(), total_connection_time_ms,
3812 fake_clock);
deadbeef1dcb1642017-03-29 21:08:16 -07003813 // Need to free the clients here since they're using things we created on
3814 // the stack.
3815 delete SetCallerPcWrapperAndReturnCurrent(nullptr);
3816 delete SetCalleePcWrapperAndReturnCurrent(nullptr);
3817}
3818
Jonas Orelandbdcee282017-10-10 14:01:40 +02003819// Verify that a TurnCustomizer passed in through RTCConfiguration
3820// is actually used by the underlying TURN candidate pair.
3821// Note that turnport_unittest.cc contains more detailed, lower-level tests.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003822TEST_P(PeerConnectionIntegrationTest, TurnCustomizerUsedForTurnConnections) {
Jonas Orelandbdcee282017-10-10 14:01:40 +02003823 static const rtc::SocketAddress turn_server_1_internal_address{"88.88.88.0",
3824 3478};
3825 static const rtc::SocketAddress turn_server_1_external_address{"88.88.88.1",
3826 0};
3827 static const rtc::SocketAddress turn_server_2_internal_address{"99.99.99.0",
3828 3478};
3829 static const rtc::SocketAddress turn_server_2_external_address{"99.99.99.1",
3830 0};
3831 cricket::TestTurnServer turn_server_1(network_thread(),
3832 turn_server_1_internal_address,
3833 turn_server_1_external_address);
3834 cricket::TestTurnServer turn_server_2(network_thread(),
3835 turn_server_2_internal_address,
3836 turn_server_2_external_address);
3837
3838 PeerConnectionInterface::RTCConfiguration client_1_config;
3839 webrtc::PeerConnectionInterface::IceServer ice_server_1;
3840 ice_server_1.urls.push_back("turn:88.88.88.0:3478");
3841 ice_server_1.username = "test";
3842 ice_server_1.password = "test";
3843 client_1_config.servers.push_back(ice_server_1);
3844 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
3845 auto customizer1 = rtc::MakeUnique<cricket::TestTurnCustomizer>();
3846 client_1_config.turn_customizer = customizer1.get();
3847
3848 PeerConnectionInterface::RTCConfiguration client_2_config;
3849 webrtc::PeerConnectionInterface::IceServer ice_server_2;
3850 ice_server_2.urls.push_back("turn:99.99.99.0:3478");
3851 ice_server_2.username = "test";
3852 ice_server_2.password = "test";
3853 client_2_config.servers.push_back(ice_server_2);
3854 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
3855 auto customizer2 = rtc::MakeUnique<cricket::TestTurnCustomizer>();
3856 client_2_config.turn_customizer = customizer2.get();
3857
3858 ASSERT_TRUE(
3859 CreatePeerConnectionWrappersWithConfig(client_1_config, client_2_config));
3860 ConnectFakeSignaling();
3861
3862 // Set "offer to receive audio/video" without adding any tracks, so we just
3863 // set up ICE/DTLS with no media.
3864 PeerConnectionInterface::RTCOfferAnswerOptions options;
3865 options.offer_to_receive_audio = 1;
3866 options.offer_to_receive_video = 1;
3867 caller()->SetOfferAnswerOptions(options);
3868 caller()->CreateAndSetAndSignalOffer();
3869 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
3870
3871 EXPECT_GT(customizer1->allow_channel_data_cnt_, 0u);
3872 EXPECT_GT(customizer1->modify_cnt_, 0u);
3873
3874 EXPECT_GT(customizer2->allow_channel_data_cnt_, 0u);
3875 EXPECT_GT(customizer2->modify_cnt_, 0u);
3876
3877 // Need to free the clients here since they're using things we created on
3878 // the stack.
3879 delete SetCallerPcWrapperAndReturnCurrent(nullptr);
3880 delete SetCalleePcWrapperAndReturnCurrent(nullptr);
3881}
3882
deadbeefc964d0b2017-04-03 10:03:35 -07003883// Test that audio and video flow end-to-end when codec names don't use the
3884// expected casing, given that they're supposed to be case insensitive. To test
3885// this, all but one codec is removed from each media description, and its
3886// casing is changed.
3887//
3888// In the past, this has regressed and caused crashes/black video, due to the
3889// fact that code at some layers was doing case-insensitive comparisons and
3890// code at other layers was not.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003891TEST_P(PeerConnectionIntegrationTest, CodecNamesAreCaseInsensitive) {
deadbeefc964d0b2017-04-03 10:03:35 -07003892 ASSERT_TRUE(CreatePeerConnectionWrappers());
3893 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08003894 caller()->AddAudioVideoTracks();
3895 callee()->AddAudioVideoTracks();
deadbeefc964d0b2017-04-03 10:03:35 -07003896
3897 // Remove all but one audio/video codec (opus and VP8), and change the
3898 // casing of the caller's generated offer.
3899 caller()->SetGeneratedSdpMunger([](cricket::SessionDescription* description) {
3900 cricket::AudioContentDescription* audio =
3901 GetFirstAudioContentDescription(description);
3902 ASSERT_NE(nullptr, audio);
3903 auto audio_codecs = audio->codecs();
3904 audio_codecs.erase(std::remove_if(audio_codecs.begin(), audio_codecs.end(),
3905 [](const cricket::AudioCodec& codec) {
3906 return codec.name != "opus";
3907 }),
3908 audio_codecs.end());
3909 ASSERT_EQ(1u, audio_codecs.size());
3910 audio_codecs[0].name = "OpUs";
3911 audio->set_codecs(audio_codecs);
3912
3913 cricket::VideoContentDescription* video =
3914 GetFirstVideoContentDescription(description);
3915 ASSERT_NE(nullptr, video);
3916 auto video_codecs = video->codecs();
3917 video_codecs.erase(std::remove_if(video_codecs.begin(), video_codecs.end(),
3918 [](const cricket::VideoCodec& codec) {
3919 return codec.name != "VP8";
3920 }),
3921 video_codecs.end());
3922 ASSERT_EQ(1u, video_codecs.size());
3923 video_codecs[0].name = "vP8";
3924 video->set_codecs(video_codecs);
3925 });
3926
3927 caller()->CreateAndSetAndSignalOffer();
3928 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3929
3930 // Verify frames are still received end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003931 MediaExpectations media_expectations;
3932 media_expectations.ExpectBidirectionalAudioAndVideo();
3933 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeefc964d0b2017-04-03 10:03:35 -07003934}
3935
Seth Hampson2f0d7022018-02-20 11:54:42 -08003936TEST_P(PeerConnectionIntegrationTest, GetSources) {
hbos8d609f62017-04-10 07:39:05 -07003937 ASSERT_TRUE(CreatePeerConnectionWrappers());
3938 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08003939 caller()->AddAudioTrack();
hbos8d609f62017-04-10 07:39:05 -07003940 caller()->CreateAndSetAndSignalOffer();
3941 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
deadbeefd8ad7882017-04-18 16:01:17 -07003942 // Wait for one audio frame to be received by the callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003943 MediaExpectations media_expectations;
3944 media_expectations.CalleeExpectsSomeAudio(1);
3945 ASSERT_TRUE(ExpectNewFrames(media_expectations));
hbos8d609f62017-04-10 07:39:05 -07003946 ASSERT_GT(callee()->pc()->GetReceivers().size(), 0u);
3947 auto receiver = callee()->pc()->GetReceivers()[0];
3948 ASSERT_EQ(receiver->media_type(), cricket::MEDIA_TYPE_AUDIO);
3949
3950 auto contributing_sources = receiver->GetSources();
3951 ASSERT_GT(receiver->GetParameters().encodings.size(), 0u);
3952 EXPECT_EQ(receiver->GetParameters().encodings[0].ssrc,
3953 contributing_sources[0].source_id());
3954}
3955
deadbeef2f425aa2017-04-14 10:41:32 -07003956// Test that if a track is removed and added again with a different stream ID,
3957// the new stream ID is successfully communicated in SDP and media continues to
3958// flow end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003959// TODO(webrtc.bugs.org/8734): This test does not work for Unified Plan because
3960// it will not reuse a transceiver that has already been sending. After creating
3961// a new transceiver it tries to create an offer with two senders of the same
3962// track ids and it fails.
3963TEST_F(PeerConnectionIntegrationTestPlanB, RemoveAndAddTrackWithNewStreamId) {
deadbeef2f425aa2017-04-14 10:41:32 -07003964 ASSERT_TRUE(CreatePeerConnectionWrappers());
3965 ConnectFakeSignaling();
3966
3967 rtc::scoped_refptr<MediaStreamInterface> stream_1 =
3968 caller()->pc_factory()->CreateLocalMediaStream("stream_1");
3969 rtc::scoped_refptr<MediaStreamInterface> stream_2 =
3970 caller()->pc_factory()->CreateLocalMediaStream("stream_2");
3971
3972 // Add track using stream 1, do offer/answer.
3973 rtc::scoped_refptr<webrtc::AudioTrackInterface> track =
3974 caller()->CreateLocalAudioTrack();
3975 rtc::scoped_refptr<webrtc::RtpSenderInterface> sender =
3976 caller()->pc()->AddTrack(track, {stream_1.get()});
3977 caller()->CreateAndSetAndSignalOffer();
3978 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08003979 {
3980 MediaExpectations media_expectations;
3981 media_expectations.CalleeExpectsSomeAudio(1);
3982 ASSERT_TRUE(ExpectNewFrames(media_expectations));
3983 }
deadbeef2f425aa2017-04-14 10:41:32 -07003984 // Remove the sender, and create a new one with the new stream.
3985 caller()->pc()->RemoveTrack(sender);
3986 sender = caller()->pc()->AddTrack(track, {stream_2.get()});
3987 caller()->CreateAndSetAndSignalOffer();
3988 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3989 // Wait for additional audio frames to be received by the callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003990 {
3991 MediaExpectations media_expectations;
3992 media_expectations.CalleeExpectsSomeAudio();
3993 ASSERT_TRUE(ExpectNewFrames(media_expectations));
3994 }
deadbeef2f425aa2017-04-14 10:41:32 -07003995}
3996
Seth Hampson2f0d7022018-02-20 11:54:42 -08003997TEST_P(PeerConnectionIntegrationTest, RtcEventLogOutputWriteCalled) {
Elad Alon99c3fe52017-10-13 16:29:40 +02003998 ASSERT_TRUE(CreatePeerConnectionWrappers());
3999 ConnectFakeSignaling();
4000
4001 auto output = rtc::MakeUnique<testing::NiceMock<MockRtcEventLogOutput>>();
4002 ON_CALL(*output, IsActive()).WillByDefault(testing::Return(true));
4003 ON_CALL(*output, Write(::testing::_)).WillByDefault(testing::Return(true));
4004 EXPECT_CALL(*output, Write(::testing::_)).Times(::testing::AtLeast(1));
Bjorn Tereliusde939432017-11-20 17:38:14 +01004005 EXPECT_TRUE(caller()->pc()->StartRtcEventLog(
4006 std::move(output), webrtc::RtcEventLog::kImmediateOutput));
Elad Alon99c3fe52017-10-13 16:29:40 +02004007
Steve Anton15324772018-01-16 10:26:49 -08004008 caller()->AddAudioVideoTracks();
Elad Alon99c3fe52017-10-13 16:29:40 +02004009 caller()->CreateAndSetAndSignalOffer();
4010 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4011}
4012
Steve Antonede9ca52017-10-16 13:04:27 -07004013// Test that if candidates are only signaled by applying full session
4014// descriptions (instead of using AddIceCandidate), the peers can connect to
4015// each other and exchange media.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004016TEST_P(PeerConnectionIntegrationTest, MediaFlowsWhenCandidatesSetOnlyInSdp) {
Steve Antonede9ca52017-10-16 13:04:27 -07004017 ASSERT_TRUE(CreatePeerConnectionWrappers());
4018 // Each side will signal the session descriptions but not candidates.
4019 ConnectFakeSignalingForSdpOnly();
4020
4021 // Add audio video track and exchange the initial offer/answer with media
4022 // information only. This will start ICE gathering on each side.
Steve Anton15324772018-01-16 10:26:49 -08004023 caller()->AddAudioVideoTracks();
4024 callee()->AddAudioVideoTracks();
Steve Antonede9ca52017-10-16 13:04:27 -07004025 caller()->CreateAndSetAndSignalOffer();
4026
4027 // Wait for all candidates to be gathered on both the caller and callee.
4028 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceGatheringComplete,
4029 caller()->ice_gathering_state(), kDefaultTimeout);
4030 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceGatheringComplete,
4031 callee()->ice_gathering_state(), kDefaultTimeout);
4032
4033 // The candidates will now be included in the session description, so
4034 // signaling them will start the ICE connection.
4035 caller()->CreateAndSetAndSignalOffer();
4036 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4037
4038 // Ensure that media flows in both directions.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004039 MediaExpectations media_expectations;
4040 media_expectations.ExpectBidirectionalAudioAndVideo();
4041 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Antonede9ca52017-10-16 13:04:27 -07004042}
4043
henrika5f6bf242017-11-01 11:06:56 +01004044// Test that SetAudioPlayout can be used to disable audio playout from the
4045// start, then later enable it. This may be useful, for example, if the caller
4046// needs to play a local ringtone until some event occurs, after which it
4047// switches to playing the received audio.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004048TEST_P(PeerConnectionIntegrationTest, DisableAndEnableAudioPlayout) {
henrika5f6bf242017-11-01 11:06:56 +01004049 ASSERT_TRUE(CreatePeerConnectionWrappers());
4050 ConnectFakeSignaling();
4051
4052 // Set up audio-only call where audio playout is disabled on caller's side.
4053 caller()->pc()->SetAudioPlayout(false);
Steve Anton15324772018-01-16 10:26:49 -08004054 caller()->AddAudioTrack();
4055 callee()->AddAudioTrack();
henrika5f6bf242017-11-01 11:06:56 +01004056 caller()->CreateAndSetAndSignalOffer();
4057 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4058
4059 // Pump messages for a second.
4060 WAIT(false, 1000);
4061 // Since audio playout is disabled, the caller shouldn't have received
4062 // anything (at the playout level, at least).
4063 EXPECT_EQ(0, caller()->audio_frames_received());
4064 // As a sanity check, make sure the callee (for which playout isn't disabled)
4065 // did still see frames on its audio level.
4066 ASSERT_GT(callee()->audio_frames_received(), 0);
4067
4068 // Enable playout again, and ensure audio starts flowing.
4069 caller()->pc()->SetAudioPlayout(true);
Seth Hampson2f0d7022018-02-20 11:54:42 -08004070 MediaExpectations media_expectations;
4071 media_expectations.ExpectBidirectionalAudio();
4072 ASSERT_TRUE(ExpectNewFrames(media_expectations));
henrika5f6bf242017-11-01 11:06:56 +01004073}
4074
4075double GetAudioEnergyStat(PeerConnectionWrapper* pc) {
4076 auto report = pc->NewGetStats();
4077 auto track_stats_list =
4078 report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
4079 const webrtc::RTCMediaStreamTrackStats* remote_track_stats = nullptr;
4080 for (const auto* track_stats : track_stats_list) {
4081 if (track_stats->remote_source.is_defined() &&
4082 *track_stats->remote_source) {
4083 remote_track_stats = track_stats;
4084 break;
4085 }
4086 }
4087
4088 if (!remote_track_stats->total_audio_energy.is_defined()) {
4089 return 0.0;
4090 }
4091 return *remote_track_stats->total_audio_energy;
4092}
4093
4094// Test that if audio playout is disabled via the SetAudioPlayout() method, then
4095// incoming audio is still processed and statistics are generated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004096TEST_P(PeerConnectionIntegrationTest,
henrika5f6bf242017-11-01 11:06:56 +01004097 DisableAudioPlayoutStillGeneratesAudioStats) {
4098 ASSERT_TRUE(CreatePeerConnectionWrappers());
4099 ConnectFakeSignaling();
4100
4101 // Set up audio-only call where playout is disabled but audio-processing is
4102 // still active.
Steve Anton15324772018-01-16 10:26:49 -08004103 caller()->AddAudioTrack();
4104 callee()->AddAudioTrack();
henrika5f6bf242017-11-01 11:06:56 +01004105 caller()->pc()->SetAudioPlayout(false);
4106
4107 caller()->CreateAndSetAndSignalOffer();
4108 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4109
4110 // Wait for the callee to receive audio stats.
4111 EXPECT_TRUE_WAIT(GetAudioEnergyStat(caller()) > 0, kMaxWaitForFramesMs);
4112}
4113
henrika4f167df2017-11-01 14:45:55 +01004114// Test that SetAudioRecording can be used to disable audio recording from the
4115// start, then later enable it. This may be useful, for example, if the caller
4116// wants to ensure that no audio resources are active before a certain state
4117// is reached.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004118TEST_P(PeerConnectionIntegrationTest, DisableAndEnableAudioRecording) {
henrika4f167df2017-11-01 14:45:55 +01004119 ASSERT_TRUE(CreatePeerConnectionWrappers());
4120 ConnectFakeSignaling();
4121
4122 // Set up audio-only call where audio recording is disabled on caller's side.
4123 caller()->pc()->SetAudioRecording(false);
Steve Anton15324772018-01-16 10:26:49 -08004124 caller()->AddAudioTrack();
4125 callee()->AddAudioTrack();
henrika4f167df2017-11-01 14:45:55 +01004126 caller()->CreateAndSetAndSignalOffer();
4127 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4128
4129 // Pump messages for a second.
4130 WAIT(false, 1000);
4131 // Since caller has disabled audio recording, the callee shouldn't have
4132 // received anything.
4133 EXPECT_EQ(0, callee()->audio_frames_received());
4134 // As a sanity check, make sure the caller did still see frames on its
4135 // audio level since audio recording is enabled on the calle side.
4136 ASSERT_GT(caller()->audio_frames_received(), 0);
4137
4138 // Enable audio recording again, and ensure audio starts flowing.
4139 caller()->pc()->SetAudioRecording(true);
Seth Hampson2f0d7022018-02-20 11:54:42 -08004140 MediaExpectations media_expectations;
4141 media_expectations.ExpectBidirectionalAudio();
4142 ASSERT_TRUE(ExpectNewFrames(media_expectations));
henrika4f167df2017-11-01 14:45:55 +01004143}
4144
Taylor Brandstetter389a97c2018-01-03 16:26:06 -08004145// Test that after closing PeerConnections, they stop sending any packets (ICE,
4146// DTLS, RTP...).
Seth Hampson2f0d7022018-02-20 11:54:42 -08004147TEST_P(PeerConnectionIntegrationTest, ClosingConnectionStopsPacketFlow) {
Taylor Brandstetter389a97c2018-01-03 16:26:06 -08004148 // Set up audio/video/data, wait for some frames to be received.
4149 ASSERT_TRUE(CreatePeerConnectionWrappers());
4150 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08004151 caller()->AddAudioVideoTracks();
Taylor Brandstetter389a97c2018-01-03 16:26:06 -08004152#ifdef HAVE_SCTP
4153 caller()->CreateDataChannel();
4154#endif
4155 caller()->CreateAndSetAndSignalOffer();
4156 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08004157 MediaExpectations media_expectations;
4158 media_expectations.CalleeExpectsSomeAudioAndVideo();
4159 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Taylor Brandstetter389a97c2018-01-03 16:26:06 -08004160 // Close PeerConnections.
4161 caller()->pc()->Close();
4162 callee()->pc()->Close();
4163 // Pump messages for a second, and ensure no new packets end up sent.
4164 uint32_t sent_packets_a = virtual_socket_server()->sent_packets();
4165 WAIT(false, 1000);
4166 uint32_t sent_packets_b = virtual_socket_server()->sent_packets();
4167 EXPECT_EQ(sent_packets_a, sent_packets_b);
4168}
4169
Steve Anton7eca0932018-03-30 15:18:41 -07004170// Test that transport stats are generated by the RTCStatsCollector for a
4171// connection that only involves data channels. This is a regression test for
4172// crbug.com/826972.
4173#ifdef HAVE_SCTP
4174TEST_P(PeerConnectionIntegrationTest,
4175 TransportStatsReportedForDataChannelOnlyConnection) {
4176 ASSERT_TRUE(CreatePeerConnectionWrappers());
4177 ConnectFakeSignaling();
4178 caller()->CreateDataChannel();
4179
4180 caller()->CreateAndSetAndSignalOffer();
4181 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4182 ASSERT_TRUE_WAIT(callee()->data_channel(), kDefaultTimeout);
4183
4184 auto caller_report = caller()->NewGetStats();
4185 EXPECT_EQ(1u, caller_report->GetStatsOfType<RTCTransportStats>().size());
4186 auto callee_report = callee()->NewGetStats();
4187 EXPECT_EQ(1u, callee_report->GetStatsOfType<RTCTransportStats>().size());
4188}
4189#endif // HAVE_SCTP
4190
Seth Hampson2f0d7022018-02-20 11:54:42 -08004191INSTANTIATE_TEST_CASE_P(PeerConnectionIntegrationTest,
4192 PeerConnectionIntegrationTest,
4193 Values(SdpSemantics::kPlanB,
4194 SdpSemantics::kUnifiedPlan));
Steve Antond3679212018-01-17 17:41:02 -08004195
Steve Anton74255ff2018-01-24 18:32:57 -08004196// Tests that verify interoperability between Plan B and Unified Plan
4197// PeerConnections.
4198class PeerConnectionIntegrationInteropTest
Seth Hampson2f0d7022018-02-20 11:54:42 -08004199 : public PeerConnectionIntegrationBaseTest,
Steve Anton74255ff2018-01-24 18:32:57 -08004200 public ::testing::WithParamInterface<
4201 std::tuple<SdpSemantics, SdpSemantics>> {
4202 protected:
Seth Hampson2f0d7022018-02-20 11:54:42 -08004203 // Setting the SdpSemantics for the base test to kDefault does not matter
4204 // because we specify not to use the test semantics when creating
4205 // PeerConnectionWrappers.
Steve Anton74255ff2018-01-24 18:32:57 -08004206 PeerConnectionIntegrationInteropTest()
Steve Anton3acffc32018-04-12 17:21:03 -07004207 : PeerConnectionIntegrationBaseTest(SdpSemantics::kPlanB),
Seth Hampson2f0d7022018-02-20 11:54:42 -08004208 caller_semantics_(std::get<0>(GetParam())),
Steve Anton74255ff2018-01-24 18:32:57 -08004209 callee_semantics_(std::get<1>(GetParam())) {}
4210
4211 bool CreatePeerConnectionWrappersWithSemantics() {
Steve Anton3acffc32018-04-12 17:21:03 -07004212 return CreatePeerConnectionWrappersWithSdpSemantics(caller_semantics_,
4213 callee_semantics_);
Steve Anton74255ff2018-01-24 18:32:57 -08004214 }
4215
4216 const SdpSemantics caller_semantics_;
4217 const SdpSemantics callee_semantics_;
4218};
4219
4220TEST_P(PeerConnectionIntegrationInteropTest, NoMediaLocalToNoMediaRemote) {
4221 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
4222 ConnectFakeSignaling();
4223
4224 caller()->CreateAndSetAndSignalOffer();
4225 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4226}
4227
4228TEST_P(PeerConnectionIntegrationInteropTest, OneAudioLocalToNoMediaRemote) {
4229 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
4230 ConnectFakeSignaling();
4231 auto audio_sender = caller()->AddAudioTrack();
4232
4233 caller()->CreateAndSetAndSignalOffer();
4234 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4235
4236 // Verify that one audio receiver has been created on the remote and that it
4237 // has the same track ID as the sending track.
4238 auto receivers = callee()->pc()->GetReceivers();
4239 ASSERT_EQ(1u, receivers.size());
4240 EXPECT_EQ(cricket::MEDIA_TYPE_AUDIO, receivers[0]->media_type());
4241 EXPECT_EQ(receivers[0]->track()->id(), audio_sender->track()->id());
4242
Seth Hampson2f0d7022018-02-20 11:54:42 -08004243 MediaExpectations media_expectations;
4244 media_expectations.CalleeExpectsSomeAudio();
4245 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08004246}
4247
4248TEST_P(PeerConnectionIntegrationInteropTest, OneAudioOneVideoToNoMediaRemote) {
4249 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
4250 ConnectFakeSignaling();
4251 auto video_sender = caller()->AddVideoTrack();
4252 auto audio_sender = caller()->AddAudioTrack();
4253
4254 caller()->CreateAndSetAndSignalOffer();
4255 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4256
4257 // Verify that one audio and one video receiver have been created on the
4258 // remote and that they have the same track IDs as the sending tracks.
4259 auto audio_receivers =
4260 callee()->GetReceiversOfType(cricket::MEDIA_TYPE_AUDIO);
4261 ASSERT_EQ(1u, audio_receivers.size());
4262 EXPECT_EQ(audio_receivers[0]->track()->id(), audio_sender->track()->id());
4263 auto video_receivers =
4264 callee()->GetReceiversOfType(cricket::MEDIA_TYPE_VIDEO);
4265 ASSERT_EQ(1u, video_receivers.size());
4266 EXPECT_EQ(video_receivers[0]->track()->id(), video_sender->track()->id());
4267
Seth Hampson2f0d7022018-02-20 11:54:42 -08004268 MediaExpectations media_expectations;
4269 media_expectations.CalleeExpectsSomeAudioAndVideo();
4270 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08004271}
4272
4273TEST_P(PeerConnectionIntegrationInteropTest,
4274 OneAudioOneVideoLocalToOneAudioOneVideoRemote) {
4275 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
4276 ConnectFakeSignaling();
4277 caller()->AddAudioVideoTracks();
4278 callee()->AddAudioVideoTracks();
4279
4280 caller()->CreateAndSetAndSignalOffer();
4281 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4282
Seth Hampson2f0d7022018-02-20 11:54:42 -08004283 MediaExpectations media_expectations;
4284 media_expectations.ExpectBidirectionalAudioAndVideo();
4285 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08004286}
4287
4288TEST_P(PeerConnectionIntegrationInteropTest,
4289 ReverseRolesOneAudioLocalToOneVideoRemote) {
4290 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
4291 ConnectFakeSignaling();
4292 caller()->AddAudioTrack();
4293 callee()->AddVideoTrack();
4294
4295 caller()->CreateAndSetAndSignalOffer();
4296 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4297
4298 // Verify that only the audio track has been negotiated.
4299 EXPECT_EQ(0u, caller()->GetReceiversOfType(cricket::MEDIA_TYPE_VIDEO).size());
4300 // Might also check that the callee's NegotiationNeeded flag is set.
4301
4302 // Reverse roles.
4303 callee()->CreateAndSetAndSignalOffer();
4304 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4305
Seth Hampson2f0d7022018-02-20 11:54:42 -08004306 MediaExpectations media_expectations;
4307 media_expectations.CallerExpectsSomeVideo();
4308 media_expectations.CalleeExpectsSomeAudio();
4309 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08004310}
4311
Steve Antonba42e992018-04-09 14:10:01 -07004312INSTANTIATE_TEST_CASE_P(
4313 PeerConnectionIntegrationTest,
4314 PeerConnectionIntegrationInteropTest,
4315 Values(std::make_tuple(SdpSemantics::kPlanB, SdpSemantics::kUnifiedPlan),
4316 std::make_tuple(SdpSemantics::kUnifiedPlan, SdpSemantics::kPlanB)));
4317
4318// Test that if the Unified Plan side offers two video tracks then the Plan B
4319// side will only see the first one and ignore the second.
4320TEST_F(PeerConnectionIntegrationTestPlanB, TwoVideoUnifiedPlanToNoMediaPlanB) {
Steve Anton3acffc32018-04-12 17:21:03 -07004321 ASSERT_TRUE(CreatePeerConnectionWrappersWithSdpSemantics(
4322 SdpSemantics::kUnifiedPlan, SdpSemantics::kPlanB));
Steve Anton74255ff2018-01-24 18:32:57 -08004323 ConnectFakeSignaling();
4324 auto first_sender = caller()->AddVideoTrack();
4325 caller()->AddVideoTrack();
4326
4327 caller()->CreateAndSetAndSignalOffer();
4328 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4329
4330 // Verify that there is only one receiver and it corresponds to the first
4331 // added track.
4332 auto receivers = callee()->pc()->GetReceivers();
4333 ASSERT_EQ(1u, receivers.size());
4334 EXPECT_TRUE(receivers[0]->track()->enabled());
4335 EXPECT_EQ(first_sender->track()->id(), receivers[0]->track()->id());
4336
Seth Hampson2f0d7022018-02-20 11:54:42 -08004337 MediaExpectations media_expectations;
4338 media_expectations.CalleeExpectsSomeVideo();
4339 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08004340}
4341
deadbeef1dcb1642017-03-29 21:08:16 -07004342} // namespace
4343
4344#endif // if !defined(THREAD_SANITIZER)