blob: 583765d9a64da925cccf8d8f2666a86abc8c3cbd [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.
Zhi Huang365381f2018-04-13 16:44:34 -07002276// TODO(zhihuang): Enable this test in Unified Plan mode once the MID-based
2277// demuxing is ready.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002278TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithTwoVideoTracks) {
deadbeef1dcb1642017-03-29 21:08:16 -07002279 ASSERT_TRUE(CreatePeerConnectionWrappers());
2280 ConnectFakeSignaling();
2281 // Add one audio/video stream, and one video-only stream.
Steve Anton15324772018-01-16 10:26:49 -08002282 caller()->AddAudioVideoTracks();
2283 caller()->AddVideoTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07002284 caller()->CreateAndSetAndSignalOffer();
2285 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Steve Anton15324772018-01-16 10:26:49 -08002286 ASSERT_EQ(3u, callee()->pc()->GetReceivers().size());
Seth Hampson2f0d7022018-02-20 11:54:42 -08002287
2288 MediaExpectations media_expectations;
2289 media_expectations.CalleeExpectsSomeAudioAndVideo();
2290 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002291}
2292
2293static void MakeSpecCompliantMaxBundleOffer(cricket::SessionDescription* desc) {
2294 bool first = true;
2295 for (cricket::ContentInfo& content : desc->contents()) {
2296 if (first) {
2297 first = false;
2298 continue;
2299 }
2300 content.bundle_only = true;
2301 }
2302 first = true;
2303 for (cricket::TransportInfo& transport : desc->transport_infos()) {
2304 if (first) {
2305 first = false;
2306 continue;
2307 }
2308 transport.description.ice_ufrag.clear();
2309 transport.description.ice_pwd.clear();
2310 transport.description.connection_role = cricket::CONNECTIONROLE_NONE;
2311 transport.description.identity_fingerprint.reset(nullptr);
2312 }
2313}
2314
2315// Test that if applying a true "max bundle" offer, which uses ports of 0,
2316// "a=bundle-only", omitting "a=fingerprint", "a=setup", "a=ice-ufrag" and
2317// "a=ice-pwd" for all but the audio "m=" section, negotiation still completes
2318// successfully and media flows.
2319// TODO(deadbeef): Update this test to also omit "a=rtcp-mux", once that works.
2320// TODO(deadbeef): Won't need this test once we start generating actual
2321// standards-compliant SDP.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002322TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07002323 EndToEndCallWithSpecCompliantMaxBundleOffer) {
2324 ASSERT_TRUE(CreatePeerConnectionWrappers());
2325 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002326 caller()->AddAudioVideoTracks();
2327 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002328 // Do the equivalent of setting the port to 0, adding a=bundle-only, and
2329 // removing a=ice-ufrag, a=ice-pwd, a=fingerprint and a=setup from all
2330 // but the first m= section.
2331 callee()->SetReceivedSdpMunger(MakeSpecCompliantMaxBundleOffer);
2332 caller()->CreateAndSetAndSignalOffer();
2333 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002334 MediaExpectations media_expectations;
2335 media_expectations.ExpectBidirectionalAudioAndVideo();
2336 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002337}
2338
2339// Test that we can receive the audio output level from a remote audio track.
2340// TODO(deadbeef): Use a fake audio source and verify that the output level is
2341// exactly what the source on the other side was configured with.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002342TEST_P(PeerConnectionIntegrationTest, GetAudioOutputLevelStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-29 21:08:16 -07002343 ASSERT_TRUE(CreatePeerConnectionWrappers());
2344 ConnectFakeSignaling();
2345 // Just add an audio track.
Steve Anton15324772018-01-16 10:26:49 -08002346 caller()->AddAudioTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07002347 caller()->CreateAndSetAndSignalOffer();
2348 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2349
2350 // Get the audio output level stats. Note that the level is not available
2351 // until an RTCP packet has been received.
deadbeefd8ad7882017-04-18 16:01:17 -07002352 EXPECT_TRUE_WAIT(callee()->OldGetStats()->AudioOutputLevel() > 0,
deadbeef1dcb1642017-03-29 21:08:16 -07002353 kMaxWaitForFramesMs);
2354}
2355
2356// Test that an audio input level is reported.
2357// TODO(deadbeef): Use a fake audio source and verify that the input level is
2358// exactly what the source was configured with.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002359TEST_P(PeerConnectionIntegrationTest, GetAudioInputLevelStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-29 21:08:16 -07002360 ASSERT_TRUE(CreatePeerConnectionWrappers());
2361 ConnectFakeSignaling();
2362 // Just add an audio track.
Steve Anton15324772018-01-16 10:26:49 -08002363 caller()->AddAudioTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07002364 caller()->CreateAndSetAndSignalOffer();
2365 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2366
2367 // Get the audio input level stats. The level should be available very
2368 // soon after the test starts.
deadbeefd8ad7882017-04-18 16:01:17 -07002369 EXPECT_TRUE_WAIT(caller()->OldGetStats()->AudioInputLevel() > 0,
deadbeef1dcb1642017-03-29 21:08:16 -07002370 kMaxWaitForStatsMs);
2371}
2372
2373// Test that we can get incoming byte counts from both audio and video tracks.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002374TEST_P(PeerConnectionIntegrationTest, GetBytesReceivedStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-29 21:08:16 -07002375 ASSERT_TRUE(CreatePeerConnectionWrappers());
2376 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002377 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002378 // Do offer/answer, wait for the callee to receive some frames.
2379 caller()->CreateAndSetAndSignalOffer();
2380 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002381
2382 MediaExpectations media_expectations;
2383 media_expectations.CalleeExpectsSomeAudioAndVideo();
2384 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002385
2386 // Get a handle to the remote tracks created, so they can be used as GetStats
2387 // filters.
Steve Anton15324772018-01-16 10:26:49 -08002388 for (auto receiver : callee()->pc()->GetReceivers()) {
2389 // We received frames, so we definitely should have nonzero "received bytes"
2390 // stats at this point.
2391 EXPECT_GT(callee()->OldGetStatsForTrack(receiver->track())->BytesReceived(),
2392 0);
2393 }
deadbeef1dcb1642017-03-29 21:08:16 -07002394}
2395
2396// Test that we can get outgoing byte counts from both audio and video tracks.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002397TEST_P(PeerConnectionIntegrationTest, GetBytesSentStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-29 21:08:16 -07002398 ASSERT_TRUE(CreatePeerConnectionWrappers());
2399 ConnectFakeSignaling();
2400 auto audio_track = caller()->CreateLocalAudioTrack();
2401 auto video_track = caller()->CreateLocalVideoTrack();
Steve Anton15324772018-01-16 10:26:49 -08002402 caller()->AddTrack(audio_track);
2403 caller()->AddTrack(video_track);
deadbeef1dcb1642017-03-29 21:08:16 -07002404 // Do offer/answer, wait for the callee to receive some frames.
2405 caller()->CreateAndSetAndSignalOffer();
2406 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002407 MediaExpectations media_expectations;
2408 media_expectations.CalleeExpectsSomeAudioAndVideo();
2409 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002410
2411 // The callee received frames, so we definitely should have nonzero "sent
2412 // bytes" stats at this point.
deadbeefd8ad7882017-04-18 16:01:17 -07002413 EXPECT_GT(caller()->OldGetStatsForTrack(audio_track)->BytesSent(), 0);
2414 EXPECT_GT(caller()->OldGetStatsForTrack(video_track)->BytesSent(), 0);
2415}
2416
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002417// Test that we can get capture start ntp time.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002418TEST_P(PeerConnectionIntegrationTest, GetCaptureStartNtpTimeWithOldStatsApi) {
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002419 ASSERT_TRUE(CreatePeerConnectionWrappers());
2420 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002421 caller()->AddAudioTrack();
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002422
Steve Anton15324772018-01-16 10:26:49 -08002423 callee()->AddAudioTrack();
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002424
2425 // Do offer/answer, wait for the callee to receive some frames.
2426 caller()->CreateAndSetAndSignalOffer();
2427 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2428
2429 // Get the remote audio track created on the receiver, so they can be used as
2430 // GetStats filters.
Steve Antonfc853712018-03-01 13:48:58 -08002431 auto receivers = callee()->pc()->GetReceivers();
2432 ASSERT_EQ(1u, receivers.size());
2433 auto remote_audio_track = receivers[0]->track();
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002434
2435 // Get the audio output level stats. Note that the level is not available
2436 // until an RTCP packet has been received.
Zhi Huange830e682018-03-30 10:48:35 -07002437 EXPECT_TRUE_WAIT(
2438 callee()->OldGetStatsForTrack(remote_audio_track)->CaptureStartNtpTime() >
2439 0,
2440 2 * kMaxWaitForFramesMs);
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002441}
2442
deadbeefd8ad7882017-04-18 16:01:17 -07002443// Test that we can get stats (using the new stats implemnetation) for
2444// unsignaled streams. Meaning when SSRCs/MSIDs aren't signaled explicitly in
2445// SDP.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002446TEST_P(PeerConnectionIntegrationTest,
deadbeefd8ad7882017-04-18 16:01:17 -07002447 GetStatsForUnsignaledStreamWithNewStatsApi) {
2448 ASSERT_TRUE(CreatePeerConnectionWrappers());
2449 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002450 caller()->AddAudioTrack();
deadbeefd8ad7882017-04-18 16:01:17 -07002451 // Remove SSRCs and MSIDs from the received offer SDP.
2452 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
2453 caller()->CreateAndSetAndSignalOffer();
2454 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002455 MediaExpectations media_expectations;
2456 media_expectations.CalleeExpectsSomeAudio(1);
2457 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeefd8ad7882017-04-18 16:01:17 -07002458
2459 // We received a frame, so we should have nonzero "bytes received" stats for
2460 // the unsignaled stream, if stats are working for it.
2461 rtc::scoped_refptr<const webrtc::RTCStatsReport> report =
2462 callee()->NewGetStats();
2463 ASSERT_NE(nullptr, report);
2464 auto inbound_stream_stats =
2465 report->GetStatsOfType<webrtc::RTCInboundRTPStreamStats>();
2466 ASSERT_EQ(1U, inbound_stream_stats.size());
2467 ASSERT_TRUE(inbound_stream_stats[0]->bytes_received.is_defined());
2468 ASSERT_GT(*inbound_stream_stats[0]->bytes_received, 0U);
zhihuangf8164932017-05-19 13:09:47 -07002469 ASSERT_TRUE(inbound_stream_stats[0]->track_id.is_defined());
2470}
2471
2472// Test that we can successfully get the media related stats (audio level
2473// etc.) for the unsignaled stream.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002474TEST_P(PeerConnectionIntegrationTest,
zhihuangf8164932017-05-19 13:09:47 -07002475 GetMediaStatsForUnsignaledStreamWithNewStatsApi) {
2476 ASSERT_TRUE(CreatePeerConnectionWrappers());
2477 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002478 caller()->AddAudioVideoTracks();
zhihuangf8164932017-05-19 13:09:47 -07002479 // Remove SSRCs and MSIDs from the received offer SDP.
2480 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
2481 caller()->CreateAndSetAndSignalOffer();
2482 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002483 MediaExpectations media_expectations;
2484 media_expectations.CalleeExpectsSomeAudio(1);
2485 media_expectations.CalleeExpectsSomeVideo(1);
2486 ASSERT_TRUE(ExpectNewFrames(media_expectations));
zhihuangf8164932017-05-19 13:09:47 -07002487
2488 rtc::scoped_refptr<const webrtc::RTCStatsReport> report =
2489 callee()->NewGetStats();
2490 ASSERT_NE(nullptr, report);
2491
2492 auto media_stats = report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
2493 auto audio_index = FindFirstMediaStatsIndexByKind("audio", media_stats);
2494 ASSERT_GE(audio_index, 0);
2495 EXPECT_TRUE(media_stats[audio_index]->audio_level.is_defined());
deadbeef1dcb1642017-03-29 21:08:16 -07002496}
2497
deadbeef4e2deab2017-09-20 13:56:21 -07002498// Helper for test below.
2499void ModifySsrcs(cricket::SessionDescription* desc) {
2500 for (ContentInfo& content : desc->contents()) {
Steve Antonb1c1de12017-12-21 15:14:30 -08002501 for (cricket::StreamParams& stream :
2502 content.media_description()->mutable_streams()) {
deadbeef4e2deab2017-09-20 13:56:21 -07002503 for (uint32_t& ssrc : stream.ssrcs) {
2504 ssrc = rtc::CreateRandomId();
2505 }
2506 }
2507 }
2508}
2509
2510// Test that the "RTCMediaSteamTrackStats" object is updated correctly when
2511// SSRCs are unsignaled, and the SSRC of the received (audio) stream changes.
2512// This should result in two "RTCInboundRTPStreamStats", but only one
2513// "RTCMediaStreamTrackStats", whose counters go up continuously rather than
2514// being reset to 0 once the SSRC change occurs.
2515//
2516// Regression test for this bug:
2517// https://bugs.chromium.org/p/webrtc/issues/detail?id=8158
2518//
2519// The bug causes the track stats to only represent one of the two streams:
2520// whichever one has the higher SSRC. So with this bug, there was a 50% chance
2521// that the track stat counters would reset to 0 when the new stream is
2522// received, and a 50% chance that they'll stop updating (while
2523// "concealed_samples" continues increasing, due to silence being generated for
2524// the inactive stream).
Seth Hampson2f0d7022018-02-20 11:54:42 -08002525TEST_P(PeerConnectionIntegrationTest,
Steve Anton83119dd2017-11-10 16:19:52 -08002526 TrackStatsUpdatedCorrectlyWhenUnsignaledSsrcChanges) {
deadbeef4e2deab2017-09-20 13:56:21 -07002527 ASSERT_TRUE(CreatePeerConnectionWrappers());
2528 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002529 caller()->AddAudioTrack();
deadbeef4e2deab2017-09-20 13:56:21 -07002530 // Remove SSRCs and MSIDs from the received offer SDP, simulating an endpoint
2531 // that doesn't signal SSRCs (from the callee's perspective).
2532 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
2533 caller()->CreateAndSetAndSignalOffer();
2534 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2535 // Wait for 50 audio frames (500ms of audio) to be received by the callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002536 {
2537 MediaExpectations media_expectations;
2538 media_expectations.CalleeExpectsSomeAudio(50);
2539 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2540 }
deadbeef4e2deab2017-09-20 13:56:21 -07002541 // Some audio frames were received, so we should have nonzero "samples
2542 // received" for the track.
2543 rtc::scoped_refptr<const webrtc::RTCStatsReport> report =
2544 callee()->NewGetStats();
2545 ASSERT_NE(nullptr, report);
2546 auto track_stats = report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
2547 ASSERT_EQ(1U, track_stats.size());
2548 ASSERT_TRUE(track_stats[0]->total_samples_received.is_defined());
2549 ASSERT_GT(*track_stats[0]->total_samples_received, 0U);
2550 // uint64_t prev_samples_received = *track_stats[0]->total_samples_received;
2551
2552 // Create a new offer and munge it to cause the caller to use a new SSRC.
2553 caller()->SetGeneratedSdpMunger(ModifySsrcs);
2554 caller()->CreateAndSetAndSignalOffer();
2555 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2556 // Wait for 25 more audio frames (250ms of audio) to be received, from the new
2557 // SSRC.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002558 {
2559 MediaExpectations media_expectations;
2560 media_expectations.CalleeExpectsSomeAudio(25);
2561 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2562 }
deadbeef4e2deab2017-09-20 13:56:21 -07002563
2564 report = callee()->NewGetStats();
2565 ASSERT_NE(nullptr, report);
2566 track_stats = report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
2567 ASSERT_EQ(1U, track_stats.size());
2568 ASSERT_TRUE(track_stats[0]->total_samples_received.is_defined());
2569 // The "total samples received" stat should only be greater than it was
2570 // before.
2571 // TODO(deadbeef): Uncomment this assertion once the bug is completely fixed.
2572 // Right now, the new SSRC will cause the counters to reset to 0.
2573 // EXPECT_GT(*track_stats[0]->total_samples_received, prev_samples_received);
2574
2575 // Additionally, the percentage of concealed samples (samples generated to
Steve Anton83119dd2017-11-10 16:19:52 -08002576 // conceal packet loss) should be less than 50%. If it's greater, that's a
deadbeef4e2deab2017-09-20 13:56:21 -07002577 // good sign that we're seeing stats from the old stream that's no longer
2578 // receiving packets, and is generating concealed samples of silence.
Steve Anton83119dd2017-11-10 16:19:52 -08002579 constexpr double kAcceptableConcealedSamplesPercentage = 0.50;
deadbeef4e2deab2017-09-20 13:56:21 -07002580 ASSERT_TRUE(track_stats[0]->concealed_samples.is_defined());
2581 EXPECT_LT(*track_stats[0]->concealed_samples,
2582 *track_stats[0]->total_samples_received *
2583 kAcceptableConcealedSamplesPercentage);
2584
2585 // Also ensure that we have two "RTCInboundRTPStreamStats" as expected, as a
2586 // sanity check that the SSRC really changed.
2587 // TODO(deadbeef): This isn't working right now, because we're not returning
2588 // *any* stats for the inactive stream. Uncomment when the bug is completely
2589 // fixed.
2590 // auto inbound_stream_stats =
2591 // report->GetStatsOfType<webrtc::RTCInboundRTPStreamStats>();
2592 // ASSERT_EQ(2U, inbound_stream_stats.size());
2593}
2594
deadbeef1dcb1642017-03-29 21:08:16 -07002595// Test that DTLS 1.0 is used if both sides only support DTLS 1.0.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002596TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithDtls10) {
deadbeef1dcb1642017-03-29 21:08:16 -07002597 PeerConnectionFactory::Options dtls_10_options;
2598 dtls_10_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
2599 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(dtls_10_options,
2600 dtls_10_options));
2601 ConnectFakeSignaling();
2602 // Do normal offer/answer and wait for some frames to be received in each
2603 // direction.
Steve Anton15324772018-01-16 10:26:49 -08002604 caller()->AddAudioVideoTracks();
2605 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002606 caller()->CreateAndSetAndSignalOffer();
2607 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002608 MediaExpectations media_expectations;
2609 media_expectations.ExpectBidirectionalAudioAndVideo();
2610 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002611}
2612
2613// Test getting cipher stats and UMA metrics when DTLS 1.0 is negotiated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002614TEST_P(PeerConnectionIntegrationTest, Dtls10CipherStatsAndUmaMetrics) {
deadbeef1dcb1642017-03-29 21:08:16 -07002615 PeerConnectionFactory::Options dtls_10_options;
2616 dtls_10_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
2617 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(dtls_10_options,
2618 dtls_10_options));
2619 ConnectFakeSignaling();
2620 // Register UMA observer before signaling begins.
2621 rtc::scoped_refptr<webrtc::FakeMetricsObserver> caller_observer =
2622 new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
2623 caller()->pc()->RegisterUMAObserver(caller_observer);
Steve Anton15324772018-01-16 10:26:49 -08002624 caller()->AddAudioVideoTracks();
2625 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002626 caller()->CreateAndSetAndSignalOffer();
2627 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2628 EXPECT_TRUE_WAIT(rtc::SSLStreamAdapter::IsAcceptableCipher(
deadbeefd8ad7882017-04-18 16:01:17 -07002629 caller()->OldGetStats()->DtlsCipher(), rtc::KT_DEFAULT),
deadbeef1dcb1642017-03-29 21:08:16 -07002630 kDefaultTimeout);
2631 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
deadbeefd8ad7882017-04-18 16:01:17 -07002632 caller()->OldGetStats()->SrtpCipher(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07002633 EXPECT_EQ(1,
2634 caller_observer->GetEnumCounter(webrtc::kEnumCounterAudioSrtpCipher,
2635 kDefaultSrtpCryptoSuite));
2636}
2637
2638// Test getting cipher stats and UMA metrics when DTLS 1.2 is negotiated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002639TEST_P(PeerConnectionIntegrationTest, Dtls12CipherStatsAndUmaMetrics) {
deadbeef1dcb1642017-03-29 21:08:16 -07002640 PeerConnectionFactory::Options dtls_12_options;
2641 dtls_12_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
2642 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(dtls_12_options,
2643 dtls_12_options));
2644 ConnectFakeSignaling();
2645 // Register UMA observer before signaling begins.
2646 rtc::scoped_refptr<webrtc::FakeMetricsObserver> caller_observer =
2647 new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
2648 caller()->pc()->RegisterUMAObserver(caller_observer);
Steve Anton15324772018-01-16 10:26:49 -08002649 caller()->AddAudioVideoTracks();
2650 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002651 caller()->CreateAndSetAndSignalOffer();
2652 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2653 EXPECT_TRUE_WAIT(rtc::SSLStreamAdapter::IsAcceptableCipher(
deadbeefd8ad7882017-04-18 16:01:17 -07002654 caller()->OldGetStats()->DtlsCipher(), rtc::KT_DEFAULT),
deadbeef1dcb1642017-03-29 21:08:16 -07002655 kDefaultTimeout);
2656 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
deadbeefd8ad7882017-04-18 16:01:17 -07002657 caller()->OldGetStats()->SrtpCipher(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07002658 EXPECT_EQ(1,
2659 caller_observer->GetEnumCounter(webrtc::kEnumCounterAudioSrtpCipher,
2660 kDefaultSrtpCryptoSuite));
2661}
2662
2663// Test that DTLS 1.0 can be used if the caller supports DTLS 1.2 and the
2664// callee only supports 1.0.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002665TEST_P(PeerConnectionIntegrationTest, CallerDtls12ToCalleeDtls10) {
deadbeef1dcb1642017-03-29 21:08:16 -07002666 PeerConnectionFactory::Options caller_options;
2667 caller_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
2668 PeerConnectionFactory::Options callee_options;
2669 callee_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
2670 ASSERT_TRUE(
2671 CreatePeerConnectionWrappersWithOptions(caller_options, callee_options));
2672 ConnectFakeSignaling();
2673 // Do normal offer/answer and wait for some frames to be received in each
2674 // direction.
Steve Anton15324772018-01-16 10:26:49 -08002675 caller()->AddAudioVideoTracks();
2676 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002677 caller()->CreateAndSetAndSignalOffer();
2678 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002679 MediaExpectations media_expectations;
2680 media_expectations.ExpectBidirectionalAudioAndVideo();
2681 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002682}
2683
2684// Test that DTLS 1.0 can be used if the caller only supports DTLS 1.0 and the
2685// callee supports 1.2.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002686TEST_P(PeerConnectionIntegrationTest, CallerDtls10ToCalleeDtls12) {
deadbeef1dcb1642017-03-29 21:08:16 -07002687 PeerConnectionFactory::Options caller_options;
2688 caller_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
2689 PeerConnectionFactory::Options callee_options;
2690 callee_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
2691 ASSERT_TRUE(
2692 CreatePeerConnectionWrappersWithOptions(caller_options, callee_options));
2693 ConnectFakeSignaling();
2694 // Do normal offer/answer and wait for some frames to be received in each
2695 // direction.
Steve Anton15324772018-01-16 10:26:49 -08002696 caller()->AddAudioVideoTracks();
2697 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002698 caller()->CreateAndSetAndSignalOffer();
2699 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002700 MediaExpectations media_expectations;
2701 media_expectations.ExpectBidirectionalAudioAndVideo();
2702 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002703}
2704
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07002705// The three tests below verify that "enable_aes128_sha1_32_crypto_cipher"
2706// works as expected; the cipher should only be used if enabled by both sides.
2707TEST_P(PeerConnectionIntegrationTest,
2708 Aes128Sha1_32_CipherNotUsedWhenOnlyCallerSupported) {
2709 PeerConnectionFactory::Options caller_options;
2710 caller_options.crypto_options.enable_aes128_sha1_32_crypto_cipher = true;
2711 PeerConnectionFactory::Options callee_options;
2712 callee_options.crypto_options.enable_aes128_sha1_32_crypto_cipher = false;
2713 int expected_cipher_suite = rtc::SRTP_AES128_CM_SHA1_80;
2714 TestNegotiatedCipherSuite(caller_options, callee_options,
2715 expected_cipher_suite);
2716}
2717
2718TEST_P(PeerConnectionIntegrationTest,
2719 Aes128Sha1_32_CipherNotUsedWhenOnlyCalleeSupported) {
2720 PeerConnectionFactory::Options caller_options;
2721 caller_options.crypto_options.enable_aes128_sha1_32_crypto_cipher = false;
2722 PeerConnectionFactory::Options callee_options;
2723 callee_options.crypto_options.enable_aes128_sha1_32_crypto_cipher = true;
2724 int expected_cipher_suite = rtc::SRTP_AES128_CM_SHA1_80;
2725 TestNegotiatedCipherSuite(caller_options, callee_options,
2726 expected_cipher_suite);
2727}
2728
2729TEST_P(PeerConnectionIntegrationTest, Aes128Sha1_32_CipherUsedWhenSupported) {
2730 PeerConnectionFactory::Options caller_options;
2731 caller_options.crypto_options.enable_aes128_sha1_32_crypto_cipher = true;
2732 PeerConnectionFactory::Options callee_options;
2733 callee_options.crypto_options.enable_aes128_sha1_32_crypto_cipher = true;
2734 int expected_cipher_suite = rtc::SRTP_AES128_CM_SHA1_32;
2735 TestNegotiatedCipherSuite(caller_options, callee_options,
2736 expected_cipher_suite);
2737}
2738
deadbeef1dcb1642017-03-29 21:08:16 -07002739// Test that a non-GCM cipher is used if both sides only support non-GCM.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002740TEST_P(PeerConnectionIntegrationTest, NonGcmCipherUsedWhenGcmNotSupported) {
deadbeef1dcb1642017-03-29 21:08:16 -07002741 bool local_gcm_enabled = false;
2742 bool remote_gcm_enabled = false;
2743 int expected_cipher_suite = kDefaultSrtpCryptoSuite;
2744 TestGcmNegotiationUsesCipherSuite(local_gcm_enabled, remote_gcm_enabled,
2745 expected_cipher_suite);
2746}
2747
2748// Test that a GCM cipher is used if both ends support it.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002749TEST_P(PeerConnectionIntegrationTest, GcmCipherUsedWhenGcmSupported) {
deadbeef1dcb1642017-03-29 21:08:16 -07002750 bool local_gcm_enabled = true;
2751 bool remote_gcm_enabled = true;
2752 int expected_cipher_suite = kDefaultSrtpCryptoSuiteGcm;
2753 TestGcmNegotiationUsesCipherSuite(local_gcm_enabled, remote_gcm_enabled,
2754 expected_cipher_suite);
2755}
2756
2757// Test that GCM isn't used if only the offerer supports it.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002758TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07002759 NonGcmCipherUsedWhenOnlyCallerSupportsGcm) {
2760 bool local_gcm_enabled = true;
2761 bool remote_gcm_enabled = false;
2762 int expected_cipher_suite = kDefaultSrtpCryptoSuite;
2763 TestGcmNegotiationUsesCipherSuite(local_gcm_enabled, remote_gcm_enabled,
2764 expected_cipher_suite);
2765}
2766
2767// Test that GCM isn't used if only the answerer supports it.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002768TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07002769 NonGcmCipherUsedWhenOnlyCalleeSupportsGcm) {
2770 bool local_gcm_enabled = false;
2771 bool remote_gcm_enabled = true;
2772 int expected_cipher_suite = kDefaultSrtpCryptoSuite;
2773 TestGcmNegotiationUsesCipherSuite(local_gcm_enabled, remote_gcm_enabled,
2774 expected_cipher_suite);
2775}
2776
deadbeef7914b8c2017-04-21 03:23:33 -07002777// Verify that media can be transmitted end-to-end when GCM crypto suites are
2778// enabled. Note that the above tests, such as GcmCipherUsedWhenGcmSupported,
2779// only verify that a GCM cipher is negotiated, and not necessarily that SRTP
2780// works with it.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002781TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithGcmCipher) {
deadbeef7914b8c2017-04-21 03:23:33 -07002782 PeerConnectionFactory::Options gcm_options;
2783 gcm_options.crypto_options.enable_gcm_crypto_suites = true;
2784 ASSERT_TRUE(
2785 CreatePeerConnectionWrappersWithOptions(gcm_options, gcm_options));
2786 ConnectFakeSignaling();
2787 // Do normal offer/answer and wait for some frames to be received in each
2788 // direction.
Steve Anton15324772018-01-16 10:26:49 -08002789 caller()->AddAudioVideoTracks();
2790 callee()->AddAudioVideoTracks();
deadbeef7914b8c2017-04-21 03:23:33 -07002791 caller()->CreateAndSetAndSignalOffer();
2792 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002793 MediaExpectations media_expectations;
2794 media_expectations.ExpectBidirectionalAudioAndVideo();
2795 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef7914b8c2017-04-21 03:23:33 -07002796}
2797
deadbeef1dcb1642017-03-29 21:08:16 -07002798// This test sets up a call between two parties with audio, video and an RTP
2799// data channel.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002800TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithRtpDataChannel) {
deadbeef1dcb1642017-03-29 21:08:16 -07002801 FakeConstraints setup_constraints;
2802 setup_constraints.SetAllowRtpDataChannels();
2803 ASSERT_TRUE(CreatePeerConnectionWrappersWithConstraints(&setup_constraints,
2804 &setup_constraints));
2805 ConnectFakeSignaling();
2806 // Expect that data channel created on caller side will show up for callee as
2807 // well.
2808 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08002809 caller()->AddAudioVideoTracks();
2810 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002811 caller()->CreateAndSetAndSignalOffer();
2812 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2813 // Ensure the existence of the RTP data channel didn't impede audio/video.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002814 MediaExpectations media_expectations;
2815 media_expectations.ExpectBidirectionalAudioAndVideo();
2816 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002817 ASSERT_NE(nullptr, caller()->data_channel());
2818 ASSERT_NE(nullptr, callee()->data_channel());
2819 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
2820 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
2821
2822 // Ensure data can be sent in both directions.
2823 std::string data = "hello world";
2824 SendRtpDataWithRetries(caller()->data_channel(), data, 5);
2825 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
2826 kDefaultTimeout);
2827 SendRtpDataWithRetries(callee()->data_channel(), data, 5);
2828 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
2829 kDefaultTimeout);
2830}
2831
2832// Ensure that an RTP data channel is signaled as closed for the caller when
2833// the callee rejects it in a subsequent offer.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002834TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07002835 RtpDataChannelSignaledClosedInCalleeOffer) {
2836 // Same procedure as above test.
2837 FakeConstraints setup_constraints;
2838 setup_constraints.SetAllowRtpDataChannels();
2839 ASSERT_TRUE(CreatePeerConnectionWrappersWithConstraints(&setup_constraints,
2840 &setup_constraints));
2841 ConnectFakeSignaling();
2842 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08002843 caller()->AddAudioVideoTracks();
2844 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002845 caller()->CreateAndSetAndSignalOffer();
2846 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2847 ASSERT_NE(nullptr, caller()->data_channel());
2848 ASSERT_NE(nullptr, callee()->data_channel());
2849 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
2850 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
2851
2852 // Close the data channel on the callee, and do an updated offer/answer.
2853 callee()->data_channel()->Close();
2854 callee()->CreateAndSetAndSignalOffer();
2855 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2856 EXPECT_FALSE(caller()->data_observer()->IsOpen());
2857 EXPECT_FALSE(callee()->data_observer()->IsOpen());
2858}
2859
2860// Tests that data is buffered in an RTP data channel until an observer is
2861// registered for it.
2862//
2863// NOTE: RTP data channels can receive data before the underlying
2864// transport has detected that a channel is writable and thus data can be
2865// received before the data channel state changes to open. That is hard to test
2866// but the same buffering is expected to be used in that case.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002867TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07002868 DataBufferedUntilRtpDataChannelObserverRegistered) {
2869 // Use fake clock and simulated network delay so that we predictably can wait
2870 // until an SCTP message has been delivered without "sleep()"ing.
2871 rtc::ScopedFakeClock fake_clock;
2872 // Some things use a time of "0" as a special value, so we need to start out
2873 // the fake clock at a nonzero time.
2874 // TODO(deadbeef): Fix this.
2875 fake_clock.AdvanceTime(rtc::TimeDelta::FromSeconds(1));
2876 virtual_socket_server()->set_delay_mean(5); // 5 ms per hop.
2877 virtual_socket_server()->UpdateDelayDistribution();
2878
2879 FakeConstraints constraints;
2880 constraints.SetAllowRtpDataChannels();
2881 ASSERT_TRUE(
2882 CreatePeerConnectionWrappersWithConstraints(&constraints, &constraints));
2883 ConnectFakeSignaling();
2884 caller()->CreateDataChannel();
2885 caller()->CreateAndSetAndSignalOffer();
2886 ASSERT_TRUE(caller()->data_channel() != nullptr);
2887 ASSERT_TRUE_SIMULATED_WAIT(callee()->data_channel() != nullptr,
2888 kDefaultTimeout, fake_clock);
2889 ASSERT_TRUE_SIMULATED_WAIT(caller()->data_observer()->IsOpen(),
2890 kDefaultTimeout, fake_clock);
2891 ASSERT_EQ_SIMULATED_WAIT(DataChannelInterface::kOpen,
2892 callee()->data_channel()->state(), kDefaultTimeout,
2893 fake_clock);
2894
2895 // Unregister the observer which is normally automatically registered.
2896 callee()->data_channel()->UnregisterObserver();
2897 // Send data and advance fake clock until it should have been received.
2898 std::string data = "hello world";
2899 caller()->data_channel()->Send(DataBuffer(data));
2900 SIMULATED_WAIT(false, 50, fake_clock);
2901
2902 // Attach data channel and expect data to be received immediately. Note that
2903 // EXPECT_EQ_WAIT is used, such that the simulated clock is not advanced any
2904 // further, but data can be received even if the callback is asynchronous.
2905 MockDataChannelObserver new_observer(callee()->data_channel());
2906 EXPECT_EQ_SIMULATED_WAIT(data, new_observer.last_message(), kDefaultTimeout,
2907 fake_clock);
2908}
2909
2910// This test sets up a call between two parties with audio, video and but only
2911// the caller client supports RTP data channels.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002912TEST_P(PeerConnectionIntegrationTest, RtpDataChannelsRejectedByCallee) {
deadbeef1dcb1642017-03-29 21:08:16 -07002913 FakeConstraints setup_constraints_1;
2914 setup_constraints_1.SetAllowRtpDataChannels();
2915 // Must disable DTLS to make negotiation succeed.
2916 setup_constraints_1.SetMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
2917 false);
2918 FakeConstraints setup_constraints_2;
2919 setup_constraints_2.SetMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
2920 false);
2921 ASSERT_TRUE(CreatePeerConnectionWrappersWithConstraints(
2922 &setup_constraints_1, &setup_constraints_2));
2923 ConnectFakeSignaling();
2924 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08002925 caller()->AddAudioVideoTracks();
2926 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002927 caller()->CreateAndSetAndSignalOffer();
2928 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2929 // The caller should still have a data channel, but it should be closed, and
2930 // one should ever have been created for the callee.
2931 EXPECT_TRUE(caller()->data_channel() != nullptr);
2932 EXPECT_FALSE(caller()->data_observer()->IsOpen());
2933 EXPECT_EQ(nullptr, callee()->data_channel());
2934}
2935
2936// This test sets up a call between two parties with audio, and video. When
2937// audio and video is setup and flowing, an RTP data channel is negotiated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002938TEST_P(PeerConnectionIntegrationTest, AddRtpDataChannelInSubsequentOffer) {
deadbeef1dcb1642017-03-29 21:08:16 -07002939 FakeConstraints setup_constraints;
2940 setup_constraints.SetAllowRtpDataChannels();
2941 ASSERT_TRUE(CreatePeerConnectionWrappersWithConstraints(&setup_constraints,
2942 &setup_constraints));
2943 ConnectFakeSignaling();
2944 // Do initial offer/answer with audio/video.
Steve Anton15324772018-01-16 10:26:49 -08002945 caller()->AddAudioVideoTracks();
2946 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002947 caller()->CreateAndSetAndSignalOffer();
2948 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2949 // Create data channel and do new offer and answer.
2950 caller()->CreateDataChannel();
2951 caller()->CreateAndSetAndSignalOffer();
2952 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2953 ASSERT_NE(nullptr, caller()->data_channel());
2954 ASSERT_NE(nullptr, callee()->data_channel());
2955 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
2956 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
2957 // Ensure data can be sent in both directions.
2958 std::string data = "hello world";
2959 SendRtpDataWithRetries(caller()->data_channel(), data, 5);
2960 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
2961 kDefaultTimeout);
2962 SendRtpDataWithRetries(callee()->data_channel(), data, 5);
2963 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
2964 kDefaultTimeout);
2965}
2966
2967#ifdef HAVE_SCTP
2968
2969// This test sets up a call between two parties with audio, video and an SCTP
2970// data channel.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002971TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithSctpDataChannel) {
deadbeef1dcb1642017-03-29 21:08:16 -07002972 ASSERT_TRUE(CreatePeerConnectionWrappers());
2973 ConnectFakeSignaling();
2974 // Expect that data channel created on caller side will show up for callee as
2975 // well.
2976 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08002977 caller()->AddAudioVideoTracks();
2978 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002979 caller()->CreateAndSetAndSignalOffer();
2980 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2981 // Ensure the existence of the SCTP data channel didn't impede audio/video.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002982 MediaExpectations media_expectations;
2983 media_expectations.ExpectBidirectionalAudioAndVideo();
2984 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002985 // Caller data channel should already exist (it created one). Callee data
2986 // channel may not exist yet, since negotiation happens in-band, not in SDP.
2987 ASSERT_NE(nullptr, caller()->data_channel());
2988 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
2989 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
2990 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
2991
2992 // Ensure data can be sent in both directions.
2993 std::string data = "hello world";
2994 caller()->data_channel()->Send(DataBuffer(data));
2995 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
2996 kDefaultTimeout);
2997 callee()->data_channel()->Send(DataBuffer(data));
2998 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
2999 kDefaultTimeout);
3000}
3001
3002// Ensure that when the callee closes an SCTP data channel, the closing
3003// procedure results in the data channel being closed for the caller as well.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003004TEST_P(PeerConnectionIntegrationTest, CalleeClosesSctpDataChannel) {
deadbeef1dcb1642017-03-29 21:08:16 -07003005 // Same procedure as above test.
3006 ASSERT_TRUE(CreatePeerConnectionWrappers());
3007 ConnectFakeSignaling();
3008 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08003009 caller()->AddAudioVideoTracks();
3010 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003011 caller()->CreateAndSetAndSignalOffer();
3012 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3013 ASSERT_NE(nullptr, caller()->data_channel());
3014 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3015 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3016 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3017
3018 // Close the data channel on the callee side, and wait for it to reach the
3019 // "closed" state on both sides.
3020 callee()->data_channel()->Close();
3021 EXPECT_TRUE_WAIT(!caller()->data_observer()->IsOpen(), kDefaultTimeout);
3022 EXPECT_TRUE_WAIT(!callee()->data_observer()->IsOpen(), kDefaultTimeout);
3023}
3024
Seth Hampson2f0d7022018-02-20 11:54:42 -08003025TEST_P(PeerConnectionIntegrationTest, SctpDataChannelConfigSentToOtherSide) {
Steve Antonda6c0952017-10-23 11:41:54 -07003026 ASSERT_TRUE(CreatePeerConnectionWrappers());
3027 ConnectFakeSignaling();
3028 webrtc::DataChannelInit init;
3029 init.id = 53;
3030 init.maxRetransmits = 52;
3031 caller()->CreateDataChannel("data-channel", &init);
Steve Anton15324772018-01-16 10:26:49 -08003032 caller()->AddAudioVideoTracks();
3033 callee()->AddAudioVideoTracks();
Steve Antonda6c0952017-10-23 11:41:54 -07003034 caller()->CreateAndSetAndSignalOffer();
3035 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Steve Anton074dece2017-10-24 13:04:12 -07003036 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3037 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
Steve Antonda6c0952017-10-23 11:41:54 -07003038 EXPECT_EQ(init.id, callee()->data_channel()->id());
3039 EXPECT_EQ("data-channel", callee()->data_channel()->label());
3040 EXPECT_EQ(init.maxRetransmits, callee()->data_channel()->maxRetransmits());
3041 EXPECT_FALSE(callee()->data_channel()->negotiated());
3042}
3043
deadbeef1dcb1642017-03-29 21:08:16 -07003044// Test usrsctp's ability to process unordered data stream, where data actually
3045// arrives out of order using simulated delays. Previously there have been some
3046// bugs in this area.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003047TEST_P(PeerConnectionIntegrationTest, StressTestUnorderedSctpDataChannel) {
deadbeef1dcb1642017-03-29 21:08:16 -07003048 // Introduce random network delays.
3049 // Otherwise it's not a true "unordered" test.
3050 virtual_socket_server()->set_delay_mean(20);
3051 virtual_socket_server()->set_delay_stddev(5);
3052 virtual_socket_server()->UpdateDelayDistribution();
3053 // Normal procedure, but with unordered data channel config.
3054 ASSERT_TRUE(CreatePeerConnectionWrappers());
3055 ConnectFakeSignaling();
3056 webrtc::DataChannelInit init;
3057 init.ordered = false;
3058 caller()->CreateDataChannel(&init);
3059 caller()->CreateAndSetAndSignalOffer();
3060 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3061 ASSERT_NE(nullptr, caller()->data_channel());
3062 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3063 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3064 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3065
3066 static constexpr int kNumMessages = 100;
3067 // Deliberately chosen to be larger than the MTU so messages get fragmented.
3068 static constexpr size_t kMaxMessageSize = 4096;
3069 // Create and send random messages.
3070 std::vector<std::string> sent_messages;
3071 for (int i = 0; i < kNumMessages; ++i) {
3072 size_t length =
3073 (rand() % kMaxMessageSize) + 1; // NOLINT (rand_r instead of rand)
3074 std::string message;
3075 ASSERT_TRUE(rtc::CreateRandomString(length, &message));
3076 caller()->data_channel()->Send(DataBuffer(message));
3077 callee()->data_channel()->Send(DataBuffer(message));
3078 sent_messages.push_back(message);
3079 }
3080
3081 // Wait for all messages to be received.
3082 EXPECT_EQ_WAIT(kNumMessages,
3083 caller()->data_observer()->received_message_count(),
3084 kDefaultTimeout);
3085 EXPECT_EQ_WAIT(kNumMessages,
3086 callee()->data_observer()->received_message_count(),
3087 kDefaultTimeout);
3088
3089 // Sort and compare to make sure none of the messages were corrupted.
3090 std::vector<std::string> caller_received_messages =
3091 caller()->data_observer()->messages();
3092 std::vector<std::string> callee_received_messages =
3093 callee()->data_observer()->messages();
3094 std::sort(sent_messages.begin(), sent_messages.end());
3095 std::sort(caller_received_messages.begin(), caller_received_messages.end());
3096 std::sort(callee_received_messages.begin(), callee_received_messages.end());
3097 EXPECT_EQ(sent_messages, caller_received_messages);
3098 EXPECT_EQ(sent_messages, callee_received_messages);
3099}
3100
3101// This test sets up a call between two parties with audio, and video. When
3102// audio and video are setup and flowing, an SCTP data channel is negotiated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003103TEST_P(PeerConnectionIntegrationTest, AddSctpDataChannelInSubsequentOffer) {
deadbeef1dcb1642017-03-29 21:08:16 -07003104 ASSERT_TRUE(CreatePeerConnectionWrappers());
3105 ConnectFakeSignaling();
3106 // Do initial offer/answer with audio/video.
Steve Anton15324772018-01-16 10:26:49 -08003107 caller()->AddAudioVideoTracks();
3108 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003109 caller()->CreateAndSetAndSignalOffer();
3110 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3111 // Create data channel and do new offer and answer.
3112 caller()->CreateDataChannel();
3113 caller()->CreateAndSetAndSignalOffer();
3114 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3115 // Caller data channel should already exist (it created one). Callee data
3116 // channel may not exist yet, since negotiation happens in-band, not in SDP.
3117 ASSERT_NE(nullptr, caller()->data_channel());
3118 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3119 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3120 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3121 // Ensure data can be sent in both directions.
3122 std::string data = "hello world";
3123 caller()->data_channel()->Send(DataBuffer(data));
3124 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3125 kDefaultTimeout);
3126 callee()->data_channel()->Send(DataBuffer(data));
3127 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3128 kDefaultTimeout);
3129}
3130
deadbeef7914b8c2017-04-21 03:23:33 -07003131// Set up a connection initially just using SCTP data channels, later upgrading
3132// to audio/video, ensuring frames are received end-to-end. Effectively the
3133// inverse of the test above.
3134// This was broken in M57; see https://crbug.com/711243
Seth Hampson2f0d7022018-02-20 11:54:42 -08003135TEST_P(PeerConnectionIntegrationTest, SctpDataChannelToAudioVideoUpgrade) {
deadbeef7914b8c2017-04-21 03:23:33 -07003136 ASSERT_TRUE(CreatePeerConnectionWrappers());
3137 ConnectFakeSignaling();
3138 // Do initial offer/answer with just data channel.
3139 caller()->CreateDataChannel();
3140 caller()->CreateAndSetAndSignalOffer();
3141 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3142 // Wait until data can be sent over the data channel.
3143 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3144 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3145 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3146
3147 // Do subsequent offer/answer with two-way audio and video. Audio and video
3148 // should end up bundled on the DTLS/ICE transport already used for data.
Steve Anton15324772018-01-16 10:26:49 -08003149 caller()->AddAudioVideoTracks();
3150 callee()->AddAudioVideoTracks();
deadbeef7914b8c2017-04-21 03:23:33 -07003151 caller()->CreateAndSetAndSignalOffer();
3152 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08003153 MediaExpectations media_expectations;
3154 media_expectations.ExpectBidirectionalAudioAndVideo();
3155 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef7914b8c2017-04-21 03:23:33 -07003156}
3157
deadbeef8b7e9ad2017-05-25 09:38:55 -07003158static void MakeSpecCompliantSctpOffer(cricket::SessionDescription* desc) {
deadbeef8b7e9ad2017-05-25 09:38:55 -07003159 cricket::DataContentDescription* dcd_offer =
Steve Antonb1c1de12017-12-21 15:14:30 -08003160 GetFirstDataContentDescription(desc);
3161 ASSERT_TRUE(dcd_offer);
deadbeef8b7e9ad2017-05-25 09:38:55 -07003162 dcd_offer->set_use_sctpmap(false);
3163 dcd_offer->set_protocol("UDP/DTLS/SCTP");
3164}
3165
3166// Test that the data channel works when a spec-compliant SCTP m= section is
3167// offered (using "a=sctp-port" instead of "a=sctpmap", and using
3168// "UDP/DTLS/SCTP" as the protocol).
Seth Hampson2f0d7022018-02-20 11:54:42 -08003169TEST_P(PeerConnectionIntegrationTest,
deadbeef8b7e9ad2017-05-25 09:38:55 -07003170 DataChannelWorksWhenSpecCompliantSctpOfferReceived) {
3171 ASSERT_TRUE(CreatePeerConnectionWrappers());
3172 ConnectFakeSignaling();
3173 caller()->CreateDataChannel();
3174 caller()->SetGeneratedSdpMunger(MakeSpecCompliantSctpOffer);
3175 caller()->CreateAndSetAndSignalOffer();
3176 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3177 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3178 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3179 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3180
3181 // Ensure data can be sent in both directions.
3182 std::string data = "hello world";
3183 caller()->data_channel()->Send(DataBuffer(data));
3184 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3185 kDefaultTimeout);
3186 callee()->data_channel()->Send(DataBuffer(data));
3187 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3188 kDefaultTimeout);
3189}
3190
deadbeef1dcb1642017-03-29 21:08:16 -07003191#endif // HAVE_SCTP
3192
3193// Test that the ICE connection and gathering states eventually reach
3194// "complete".
Seth Hampson2f0d7022018-02-20 11:54:42 -08003195TEST_P(PeerConnectionIntegrationTest, IceStatesReachCompletion) {
deadbeef1dcb1642017-03-29 21:08:16 -07003196 ASSERT_TRUE(CreatePeerConnectionWrappers());
3197 ConnectFakeSignaling();
3198 // Do normal offer/answer.
Steve Anton15324772018-01-16 10:26:49 -08003199 caller()->AddAudioVideoTracks();
3200 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003201 caller()->CreateAndSetAndSignalOffer();
3202 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3203 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceGatheringComplete,
3204 caller()->ice_gathering_state(), kMaxWaitForFramesMs);
3205 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceGatheringComplete,
3206 callee()->ice_gathering_state(), kMaxWaitForFramesMs);
3207 // After the best candidate pair is selected and all candidates are signaled,
3208 // the ICE connection state should reach "complete".
3209 // TODO(deadbeef): Currently, the ICE "controlled" agent (the
3210 // answerer/"callee" by default) only reaches "connected". When this is
3211 // fixed, this test should be updated.
3212 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
3213 caller()->ice_connection_state(), kDefaultTimeout);
3214 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
3215 callee()->ice_connection_state(), kDefaultTimeout);
3216}
3217
Steve Antonede9ca52017-10-16 13:04:27 -07003218// Test that firewalling the ICE connection causes the clients to identify the
3219// disconnected state and then removing the firewall causes them to reconnect.
3220class PeerConnectionIntegrationIceStatesTest
Seth Hampson2f0d7022018-02-20 11:54:42 -08003221 : public PeerConnectionIntegrationBaseTest,
3222 public ::testing::WithParamInterface<
3223 std::tuple<SdpSemantics, std::tuple<std::string, uint32_t>>> {
Steve Antonede9ca52017-10-16 13:04:27 -07003224 protected:
Seth Hampson2f0d7022018-02-20 11:54:42 -08003225 PeerConnectionIntegrationIceStatesTest()
3226 : PeerConnectionIntegrationBaseTest(std::get<0>(GetParam())) {
3227 port_allocator_flags_ = std::get<1>(std::get<1>(GetParam()));
Steve Antonede9ca52017-10-16 13:04:27 -07003228 }
3229
3230 void StartStunServer(const SocketAddress& server_address) {
3231 stun_server_.reset(
3232 cricket::TestStunServer::Create(network_thread(), server_address));
3233 }
3234
3235 bool TestIPv6() {
3236 return (port_allocator_flags_ & cricket::PORTALLOCATOR_ENABLE_IPV6);
3237 }
3238
3239 void SetPortAllocatorFlags() {
Patrik Höglund3dc41062018-04-11 11:13:57 +00003240 caller()->port_allocator()->set_flags(port_allocator_flags_);
3241 callee()->port_allocator()->set_flags(port_allocator_flags_);
Steve Antonede9ca52017-10-16 13:04:27 -07003242 }
3243
3244 std::vector<SocketAddress> CallerAddresses() {
3245 std::vector<SocketAddress> addresses;
3246 addresses.push_back(SocketAddress("1.1.1.1", 0));
3247 if (TestIPv6()) {
3248 addresses.push_back(SocketAddress("1111:0:a:b:c:d:e:f", 0));
3249 }
3250 return addresses;
3251 }
3252
3253 std::vector<SocketAddress> CalleeAddresses() {
3254 std::vector<SocketAddress> addresses;
3255 addresses.push_back(SocketAddress("2.2.2.2", 0));
3256 if (TestIPv6()) {
3257 addresses.push_back(SocketAddress("2222:0:a:b:c:d:e:f", 0));
3258 }
3259 return addresses;
3260 }
3261
3262 void SetUpNetworkInterfaces() {
3263 // Remove the default interfaces added by the test infrastructure.
3264 caller()->network()->RemoveInterface(kDefaultLocalAddress);
3265 callee()->network()->RemoveInterface(kDefaultLocalAddress);
3266
3267 // Add network addresses for test.
3268 for (const auto& caller_address : CallerAddresses()) {
3269 caller()->network()->AddInterface(caller_address);
3270 }
3271 for (const auto& callee_address : CalleeAddresses()) {
3272 callee()->network()->AddInterface(callee_address);
3273 }
3274 }
3275
3276 private:
3277 uint32_t port_allocator_flags_;
3278 std::unique_ptr<cricket::TestStunServer> stun_server_;
3279};
3280
3281// Tests that the PeerConnection goes through all the ICE gathering/connection
3282// states over the duration of the call. This includes Disconnected and Failed
3283// states, induced by putting a firewall between the peers and waiting for them
3284// to time out.
Steve Anton83119dd2017-11-10 16:19:52 -08003285TEST_P(PeerConnectionIntegrationIceStatesTest, VerifyIceStates) {
3286 // TODO(bugs.webrtc.org/8295): When using a ScopedFakeClock, this test will
3287 // sometimes hit a DCHECK in platform_thread.cc about the PacerThread being
3288 // too busy. For now, revert to running without a fake clock.
Steve Antonede9ca52017-10-16 13:04:27 -07003289
3290 const SocketAddress kStunServerAddress =
3291 SocketAddress("99.99.99.1", cricket::STUN_SERVER_PORT);
3292 StartStunServer(kStunServerAddress);
3293
3294 PeerConnectionInterface::RTCConfiguration config;
3295 PeerConnectionInterface::IceServer ice_stun_server;
3296 ice_stun_server.urls.push_back(
3297 "stun:" + kStunServerAddress.HostAsURIString() + ":" +
3298 kStunServerAddress.PortAsString());
3299 config.servers.push_back(ice_stun_server);
3300
3301 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
3302 ConnectFakeSignaling();
3303 SetPortAllocatorFlags();
3304 SetUpNetworkInterfaces();
Steve Anton15324772018-01-16 10:26:49 -08003305 caller()->AddAudioVideoTracks();
3306 callee()->AddAudioVideoTracks();
Steve Antonede9ca52017-10-16 13:04:27 -07003307
3308 // Initial state before anything happens.
3309 ASSERT_EQ(PeerConnectionInterface::kIceGatheringNew,
3310 caller()->ice_gathering_state());
3311 ASSERT_EQ(PeerConnectionInterface::kIceConnectionNew,
3312 caller()->ice_connection_state());
3313
3314 // Start the call by creating the offer, setting it as the local description,
3315 // then sending it to the peer who will respond with an answer. This happens
3316 // asynchronously so that we can watch the states as it runs in the
3317 // background.
3318 caller()->CreateAndSetAndSignalOffer();
3319
Steve Anton83119dd2017-11-10 16:19:52 -08003320 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceConnectionCompleted,
3321 caller()->ice_connection_state(), kDefaultTimeout);
Steve Antonede9ca52017-10-16 13:04:27 -07003322
3323 // Verify that the observer was notified of the intermediate transitions.
3324 EXPECT_THAT(caller()->ice_connection_state_history(),
3325 ElementsAre(PeerConnectionInterface::kIceConnectionChecking,
3326 PeerConnectionInterface::kIceConnectionConnected,
3327 PeerConnectionInterface::kIceConnectionCompleted));
3328 EXPECT_THAT(caller()->ice_gathering_state_history(),
3329 ElementsAre(PeerConnectionInterface::kIceGatheringGathering,
3330 PeerConnectionInterface::kIceGatheringComplete));
3331
3332 // Block connections to/from the caller and wait for ICE to become
3333 // disconnected.
3334 for (const auto& caller_address : CallerAddresses()) {
3335 firewall()->AddRule(false, rtc::FP_ANY, rtc::FD_ANY, caller_address);
3336 }
Mirko Bonadei675513b2017-11-09 11:09:25 +01003337 RTC_LOG(LS_INFO) << "Firewall rules applied";
Steve Anton83119dd2017-11-10 16:19:52 -08003338 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceConnectionDisconnected,
3339 caller()->ice_connection_state(), kDefaultTimeout);
Steve Antonede9ca52017-10-16 13:04:27 -07003340
3341 // Let ICE re-establish by removing the firewall rules.
3342 firewall()->ClearRules();
Mirko Bonadei675513b2017-11-09 11:09:25 +01003343 RTC_LOG(LS_INFO) << "Firewall rules cleared";
Steve Anton83119dd2017-11-10 16:19:52 -08003344 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceConnectionCompleted,
3345 caller()->ice_connection_state(), kDefaultTimeout);
Steve Antonede9ca52017-10-16 13:04:27 -07003346
3347 // According to RFC7675, if there is no response within 30 seconds then the
3348 // peer should consider the other side to have rejected the connection. This
Steve Anton83119dd2017-11-10 16:19:52 -08003349 // is signaled by the state transitioning to "failed".
Steve Antonede9ca52017-10-16 13:04:27 -07003350 constexpr int kConsentTimeout = 30000;
3351 for (const auto& caller_address : CallerAddresses()) {
3352 firewall()->AddRule(false, rtc::FP_ANY, rtc::FD_ANY, caller_address);
3353 }
Mirko Bonadei675513b2017-11-09 11:09:25 +01003354 RTC_LOG(LS_INFO) << "Firewall rules applied again";
Steve Anton83119dd2017-11-10 16:19:52 -08003355 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceConnectionFailed,
3356 caller()->ice_connection_state(), kConsentTimeout);
Steve Antonede9ca52017-10-16 13:04:27 -07003357}
3358
3359// Tests that the best connection is set to the appropriate IPv4/IPv6 connection
3360// and that the statistics in the metric observers are updated correctly.
3361TEST_P(PeerConnectionIntegrationIceStatesTest, VerifyBestConnection) {
3362 ASSERT_TRUE(CreatePeerConnectionWrappers());
3363 ConnectFakeSignaling();
3364 SetPortAllocatorFlags();
3365 SetUpNetworkInterfaces();
Steve Anton15324772018-01-16 10:26:49 -08003366 caller()->AddAudioVideoTracks();
3367 callee()->AddAudioVideoTracks();
Steve Antonede9ca52017-10-16 13:04:27 -07003368
3369 rtc::scoped_refptr<webrtc::FakeMetricsObserver> metrics_observer(
3370 new rtc::RefCountedObject<webrtc::FakeMetricsObserver>());
3371 caller()->pc()->RegisterUMAObserver(metrics_observer.get());
3372
3373 caller()->CreateAndSetAndSignalOffer();
3374
3375 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3376
3377 const int num_best_ipv4 = metrics_observer->GetEnumCounter(
3378 webrtc::kEnumCounterAddressFamily, webrtc::kBestConnections_IPv4);
3379 const int num_best_ipv6 = metrics_observer->GetEnumCounter(
3380 webrtc::kEnumCounterAddressFamily, webrtc::kBestConnections_IPv6);
3381 if (TestIPv6()) {
3382 // When IPv6 is enabled, we should prefer an IPv6 connection over an IPv4
3383 // connection.
3384 EXPECT_EQ(0u, num_best_ipv4);
3385 EXPECT_EQ(1u, num_best_ipv6);
3386 } else {
3387 EXPECT_EQ(1u, num_best_ipv4);
3388 EXPECT_EQ(0u, num_best_ipv6);
3389 }
3390
3391 EXPECT_EQ(0u, metrics_observer->GetEnumCounter(
3392 webrtc::kEnumCounterIceCandidatePairTypeUdp,
3393 webrtc::kIceCandidatePairHostHost));
3394 EXPECT_EQ(1u, metrics_observer->GetEnumCounter(
3395 webrtc::kEnumCounterIceCandidatePairTypeUdp,
3396 webrtc::kIceCandidatePairHostPublicHostPublic));
3397}
3398
3399constexpr uint32_t kFlagsIPv4NoStun = cricket::PORTALLOCATOR_DISABLE_TCP |
3400 cricket::PORTALLOCATOR_DISABLE_STUN |
3401 cricket::PORTALLOCATOR_DISABLE_RELAY;
3402constexpr uint32_t kFlagsIPv6NoStun =
3403 cricket::PORTALLOCATOR_DISABLE_TCP | cricket::PORTALLOCATOR_DISABLE_STUN |
3404 cricket::PORTALLOCATOR_ENABLE_IPV6 | cricket::PORTALLOCATOR_DISABLE_RELAY;
3405constexpr uint32_t kFlagsIPv4Stun =
3406 cricket::PORTALLOCATOR_DISABLE_TCP | cricket::PORTALLOCATOR_DISABLE_RELAY;
3407
Seth Hampson2f0d7022018-02-20 11:54:42 -08003408INSTANTIATE_TEST_CASE_P(
3409 PeerConnectionIntegrationTest,
3410 PeerConnectionIntegrationIceStatesTest,
3411 Combine(Values(SdpSemantics::kPlanB, SdpSemantics::kUnifiedPlan),
3412 Values(std::make_pair("IPv4 no STUN", kFlagsIPv4NoStun),
3413 std::make_pair("IPv6 no STUN", kFlagsIPv6NoStun),
3414 std::make_pair("IPv4 with STUN", kFlagsIPv4Stun))));
Steve Antonede9ca52017-10-16 13:04:27 -07003415
deadbeef1dcb1642017-03-29 21:08:16 -07003416// This test sets up a call between two parties with audio and video.
3417// During the call, the caller restarts ICE and the test verifies that
3418// new ICE candidates are generated and audio and video still can flow, and the
3419// ICE state reaches completed again.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003420TEST_P(PeerConnectionIntegrationTest, MediaContinuesFlowingAfterIceRestart) {
deadbeef1dcb1642017-03-29 21:08:16 -07003421 ASSERT_TRUE(CreatePeerConnectionWrappers());
3422 ConnectFakeSignaling();
3423 // Do normal offer/answer and wait for ICE to complete.
Steve Anton15324772018-01-16 10:26:49 -08003424 caller()->AddAudioVideoTracks();
3425 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003426 caller()->CreateAndSetAndSignalOffer();
3427 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3428 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
3429 caller()->ice_connection_state(), kMaxWaitForFramesMs);
3430 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
3431 callee()->ice_connection_state(), kMaxWaitForFramesMs);
3432
3433 // To verify that the ICE restart actually occurs, get
3434 // ufrag/password/candidates before and after restart.
3435 // Create an SDP string of the first audio candidate for both clients.
3436 const webrtc::IceCandidateCollection* audio_candidates_caller =
3437 caller()->pc()->local_description()->candidates(0);
3438 const webrtc::IceCandidateCollection* audio_candidates_callee =
3439 callee()->pc()->local_description()->candidates(0);
3440 ASSERT_GT(audio_candidates_caller->count(), 0u);
3441 ASSERT_GT(audio_candidates_callee->count(), 0u);
3442 std::string caller_candidate_pre_restart;
3443 ASSERT_TRUE(
3444 audio_candidates_caller->at(0)->ToString(&caller_candidate_pre_restart));
3445 std::string callee_candidate_pre_restart;
3446 ASSERT_TRUE(
3447 audio_candidates_callee->at(0)->ToString(&callee_candidate_pre_restart));
3448 const cricket::SessionDescription* desc =
3449 caller()->pc()->local_description()->description();
3450 std::string caller_ufrag_pre_restart =
3451 desc->transport_infos()[0].description.ice_ufrag;
3452 desc = callee()->pc()->local_description()->description();
3453 std::string callee_ufrag_pre_restart =
3454 desc->transport_infos()[0].description.ice_ufrag;
3455
3456 // Have the caller initiate an ICE restart.
3457 caller()->SetOfferAnswerOptions(IceRestartOfferAnswerOptions());
3458 caller()->CreateAndSetAndSignalOffer();
3459 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3460 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
3461 caller()->ice_connection_state(), kMaxWaitForFramesMs);
3462 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
3463 callee()->ice_connection_state(), kMaxWaitForFramesMs);
3464
3465 // Grab the ufrags/candidates again.
3466 audio_candidates_caller = caller()->pc()->local_description()->candidates(0);
3467 audio_candidates_callee = callee()->pc()->local_description()->candidates(0);
3468 ASSERT_GT(audio_candidates_caller->count(), 0u);
3469 ASSERT_GT(audio_candidates_callee->count(), 0u);
3470 std::string caller_candidate_post_restart;
3471 ASSERT_TRUE(
3472 audio_candidates_caller->at(0)->ToString(&caller_candidate_post_restart));
3473 std::string callee_candidate_post_restart;
3474 ASSERT_TRUE(
3475 audio_candidates_callee->at(0)->ToString(&callee_candidate_post_restart));
3476 desc = caller()->pc()->local_description()->description();
3477 std::string caller_ufrag_post_restart =
3478 desc->transport_infos()[0].description.ice_ufrag;
3479 desc = callee()->pc()->local_description()->description();
3480 std::string callee_ufrag_post_restart =
3481 desc->transport_infos()[0].description.ice_ufrag;
3482 // Sanity check that an ICE restart was actually negotiated in SDP.
3483 ASSERT_NE(caller_candidate_pre_restart, caller_candidate_post_restart);
3484 ASSERT_NE(callee_candidate_pre_restart, callee_candidate_post_restart);
3485 ASSERT_NE(caller_ufrag_pre_restart, caller_ufrag_post_restart);
3486 ASSERT_NE(callee_ufrag_pre_restart, callee_ufrag_post_restart);
3487
3488 // Ensure that additional frames are received after the ICE restart.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003489 MediaExpectations media_expectations;
3490 media_expectations.ExpectBidirectionalAudioAndVideo();
3491 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003492}
3493
3494// Verify that audio/video can be received end-to-end when ICE renomination is
3495// enabled.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003496TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithIceRenomination) {
deadbeef1dcb1642017-03-29 21:08:16 -07003497 PeerConnectionInterface::RTCConfiguration config;
3498 config.enable_ice_renomination = true;
3499 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
3500 ConnectFakeSignaling();
3501 // Do normal offer/answer and wait for some frames to be received in each
3502 // direction.
Steve Anton15324772018-01-16 10:26:49 -08003503 caller()->AddAudioVideoTracks();
3504 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003505 caller()->CreateAndSetAndSignalOffer();
3506 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3507 // Sanity check that ICE renomination was actually negotiated.
3508 const cricket::SessionDescription* desc =
3509 caller()->pc()->local_description()->description();
3510 for (const cricket::TransportInfo& info : desc->transport_infos()) {
deadbeef30952b42017-04-21 02:41:29 -07003511 ASSERT_NE(
3512 info.description.transport_options.end(),
3513 std::find(info.description.transport_options.begin(),
3514 info.description.transport_options.end(), "renomination"));
deadbeef1dcb1642017-03-29 21:08:16 -07003515 }
3516 desc = callee()->pc()->local_description()->description();
3517 for (const cricket::TransportInfo& info : desc->transport_infos()) {
deadbeef30952b42017-04-21 02:41:29 -07003518 ASSERT_NE(
3519 info.description.transport_options.end(),
3520 std::find(info.description.transport_options.begin(),
3521 info.description.transport_options.end(), "renomination"));
deadbeef1dcb1642017-03-29 21:08:16 -07003522 }
Seth Hampson2f0d7022018-02-20 11:54:42 -08003523 MediaExpectations media_expectations;
3524 media_expectations.ExpectBidirectionalAudioAndVideo();
3525 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003526}
3527
Steve Anton6f25b092017-10-23 09:39:20 -07003528// With a max bundle policy and RTCP muxing, adding a new media description to
3529// the connection should not affect ICE at all because the new media will use
3530// the existing connection.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003531TEST_P(PeerConnectionIntegrationTest,
Steve Anton83119dd2017-11-10 16:19:52 -08003532 AddMediaToConnectedBundleDoesNotRestartIce) {
Steve Anton6f25b092017-10-23 09:39:20 -07003533 PeerConnectionInterface::RTCConfiguration config;
3534 config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
3535 config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyRequire;
3536 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(
3537 config, PeerConnectionInterface::RTCConfiguration()));
3538 ConnectFakeSignaling();
3539
Steve Anton15324772018-01-16 10:26:49 -08003540 caller()->AddAudioTrack();
Steve Anton6f25b092017-10-23 09:39:20 -07003541 caller()->CreateAndSetAndSignalOffer();
3542 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Steve Antonff52f1b2017-10-26 12:24:50 -07003543 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceConnectionCompleted,
3544 caller()->ice_connection_state(), kDefaultTimeout);
Steve Anton6f25b092017-10-23 09:39:20 -07003545
3546 caller()->clear_ice_connection_state_history();
3547
Steve Anton15324772018-01-16 10:26:49 -08003548 caller()->AddVideoTrack();
Steve Anton6f25b092017-10-23 09:39:20 -07003549 caller()->CreateAndSetAndSignalOffer();
3550 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3551
3552 EXPECT_EQ(0u, caller()->ice_connection_state_history().size());
3553}
3554
deadbeef1dcb1642017-03-29 21:08:16 -07003555// This test sets up a call between two parties with audio and video. It then
3556// renegotiates setting the video m-line to "port 0", then later renegotiates
3557// again, enabling video.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003558TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07003559 VideoFlowsAfterMediaSectionIsRejectedAndRecycled) {
3560 ASSERT_TRUE(CreatePeerConnectionWrappers());
3561 ConnectFakeSignaling();
3562
3563 // Do initial negotiation, only sending media from the caller. Will result in
3564 // video and audio recvonly "m=" sections.
Steve Anton15324772018-01-16 10:26:49 -08003565 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003566 caller()->CreateAndSetAndSignalOffer();
3567 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3568
3569 // Negotiate again, disabling the video "m=" section (the callee will set the
3570 // port to 0 due to offer_to_receive_video = 0).
Seth Hampson2f0d7022018-02-20 11:54:42 -08003571 if (sdp_semantics_ == SdpSemantics::kPlanB) {
3572 PeerConnectionInterface::RTCOfferAnswerOptions options;
3573 options.offer_to_receive_video = 0;
3574 callee()->SetOfferAnswerOptions(options);
3575 } else {
3576 callee()->SetRemoteOfferHandler([this] {
3577 callee()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)->Stop();
3578 });
3579 }
deadbeef1dcb1642017-03-29 21:08:16 -07003580 caller()->CreateAndSetAndSignalOffer();
3581 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3582 // Sanity check that video "m=" section was actually rejected.
3583 const ContentInfo* answer_video_content = cricket::GetFirstVideoContent(
3584 callee()->pc()->local_description()->description());
3585 ASSERT_NE(nullptr, answer_video_content);
3586 ASSERT_TRUE(answer_video_content->rejected);
3587
3588 // Enable video and do negotiation again, making sure video is received
3589 // end-to-end, also adding media stream to callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003590 if (sdp_semantics_ == SdpSemantics::kPlanB) {
3591 PeerConnectionInterface::RTCOfferAnswerOptions options;
3592 options.offer_to_receive_video = 1;
3593 callee()->SetOfferAnswerOptions(options);
3594 } else {
3595 // The caller's transceiver is stopped, so we need to add another track.
3596 auto caller_transceiver =
3597 caller()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO);
3598 EXPECT_TRUE(caller_transceiver->stopped());
3599 caller()->AddVideoTrack();
3600 }
3601 callee()->AddVideoTrack();
3602 callee()->SetRemoteOfferHandler(nullptr);
deadbeef1dcb1642017-03-29 21:08:16 -07003603 caller()->CreateAndSetAndSignalOffer();
3604 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08003605
deadbeef1dcb1642017-03-29 21:08:16 -07003606 // Verify the caller receives frames from the newly added stream, and the
3607 // callee receives additional frames from the re-enabled video m= section.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003608 MediaExpectations media_expectations;
3609 media_expectations.CalleeExpectsSomeAudio();
3610 media_expectations.ExpectBidirectionalVideo();
3611 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003612}
3613
3614// This test sets up a Jsep call between two parties with external
3615// VideoDecoderFactory.
3616// TODO(holmer): Disabled due to sometimes crashing on buildbots.
3617// See issue webrtc/2378.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003618TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07003619 DISABLED_EndToEndCallWithVideoDecoderFactory) {
3620 ASSERT_TRUE(CreatePeerConnectionWrappers());
3621 EnableVideoDecoderFactory();
3622 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08003623 caller()->AddAudioVideoTracks();
3624 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003625 caller()->CreateAndSetAndSignalOffer();
3626 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08003627 MediaExpectations media_expectations;
3628 media_expectations.ExpectBidirectionalAudioAndVideo();
3629 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003630}
3631
3632// This tests that if we negotiate after calling CreateSender but before we
3633// have a track, then set a track later, frames from the newly-set track are
3634// received end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003635TEST_F(PeerConnectionIntegrationTestPlanB,
deadbeef1dcb1642017-03-29 21:08:16 -07003636 MediaFlowsAfterEarlyWarmupWithCreateSender) {
3637 ASSERT_TRUE(CreatePeerConnectionWrappers());
3638 ConnectFakeSignaling();
3639 auto caller_audio_sender =
3640 caller()->pc()->CreateSender("audio", "caller_stream");
3641 auto caller_video_sender =
3642 caller()->pc()->CreateSender("video", "caller_stream");
3643 auto callee_audio_sender =
3644 callee()->pc()->CreateSender("audio", "callee_stream");
3645 auto callee_video_sender =
3646 callee()->pc()->CreateSender("video", "callee_stream");
3647 caller()->CreateAndSetAndSignalOffer();
3648 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
3649 // Wait for ICE to complete, without any tracks being set.
3650 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
3651 caller()->ice_connection_state(), kMaxWaitForFramesMs);
3652 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
3653 callee()->ice_connection_state(), kMaxWaitForFramesMs);
3654 // Now set the tracks, and expect frames to immediately start flowing.
3655 EXPECT_TRUE(caller_audio_sender->SetTrack(caller()->CreateLocalAudioTrack()));
3656 EXPECT_TRUE(caller_video_sender->SetTrack(caller()->CreateLocalVideoTrack()));
3657 EXPECT_TRUE(callee_audio_sender->SetTrack(callee()->CreateLocalAudioTrack()));
3658 EXPECT_TRUE(callee_video_sender->SetTrack(callee()->CreateLocalVideoTrack()));
Seth Hampson2f0d7022018-02-20 11:54:42 -08003659 MediaExpectations media_expectations;
3660 media_expectations.ExpectBidirectionalAudioAndVideo();
3661 ASSERT_TRUE(ExpectNewFrames(media_expectations));
3662}
3663
3664// This tests that if we negotiate after calling AddTransceiver but before we
3665// have a track, then set a track later, frames from the newly-set tracks are
3666// received end-to-end.
3667TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
3668 MediaFlowsAfterEarlyWarmupWithAddTransceiver) {
3669 ASSERT_TRUE(CreatePeerConnectionWrappers());
3670 ConnectFakeSignaling();
3671 auto audio_result = caller()->pc()->AddTransceiver(cricket::MEDIA_TYPE_AUDIO);
3672 ASSERT_EQ(RTCErrorType::NONE, audio_result.error().type());
3673 auto caller_audio_sender = audio_result.MoveValue()->sender();
3674 auto video_result = caller()->pc()->AddTransceiver(cricket::MEDIA_TYPE_VIDEO);
3675 ASSERT_EQ(RTCErrorType::NONE, video_result.error().type());
3676 auto caller_video_sender = video_result.MoveValue()->sender();
3677 callee()->SetRemoteOfferHandler([this] {
3678 ASSERT_EQ(2u, callee()->pc()->GetTransceivers().size());
3679 callee()->pc()->GetTransceivers()[0]->SetDirection(
3680 RtpTransceiverDirection::kSendRecv);
3681 callee()->pc()->GetTransceivers()[1]->SetDirection(
3682 RtpTransceiverDirection::kSendRecv);
3683 });
3684 caller()->CreateAndSetAndSignalOffer();
3685 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
3686 // Wait for ICE to complete, without any tracks being set.
3687 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
3688 caller()->ice_connection_state(), kMaxWaitForFramesMs);
3689 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
3690 callee()->ice_connection_state(), kMaxWaitForFramesMs);
3691 // Now set the tracks, and expect frames to immediately start flowing.
3692 auto callee_audio_sender = callee()->pc()->GetSenders()[0];
3693 auto callee_video_sender = callee()->pc()->GetSenders()[1];
3694 ASSERT_TRUE(caller_audio_sender->SetTrack(caller()->CreateLocalAudioTrack()));
3695 ASSERT_TRUE(caller_video_sender->SetTrack(caller()->CreateLocalVideoTrack()));
3696 ASSERT_TRUE(callee_audio_sender->SetTrack(callee()->CreateLocalAudioTrack()));
3697 ASSERT_TRUE(callee_video_sender->SetTrack(callee()->CreateLocalVideoTrack()));
3698 MediaExpectations media_expectations;
3699 media_expectations.ExpectBidirectionalAudioAndVideo();
3700 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003701}
3702
3703// This test verifies that a remote video track can be added via AddStream,
3704// and sent end-to-end. For this particular test, it's simply echoed back
3705// from the caller to the callee, rather than being forwarded to a third
3706// PeerConnection.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003707TEST_F(PeerConnectionIntegrationTestPlanB, CanSendRemoteVideoTrack) {
deadbeef1dcb1642017-03-29 21:08:16 -07003708 ASSERT_TRUE(CreatePeerConnectionWrappers());
3709 ConnectFakeSignaling();
3710 // Just send a video track from the caller.
Steve Anton15324772018-01-16 10:26:49 -08003711 caller()->AddVideoTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07003712 caller()->CreateAndSetAndSignalOffer();
3713 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
3714 ASSERT_EQ(1, callee()->remote_streams()->count());
3715
3716 // Echo the stream back, and do a new offer/anwer (initiated by callee this
3717 // time).
3718 callee()->pc()->AddStream(callee()->remote_streams()->at(0));
3719 callee()->CreateAndSetAndSignalOffer();
3720 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
3721
Seth Hampson2f0d7022018-02-20 11:54:42 -08003722 MediaExpectations media_expectations;
3723 media_expectations.ExpectBidirectionalVideo();
3724 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003725}
3726
3727// Test that we achieve the expected end-to-end connection time, using a
3728// fake clock and simulated latency on the media and signaling paths.
3729// We use a TURN<->TURN connection because this is usually the quickest to
3730// set up initially, especially when we're confident the connection will work
3731// and can start sending media before we get a STUN response.
3732//
3733// With various optimizations enabled, here are the network delays we expect to
3734// be on the critical path:
3735// 1. 2 signaling trips: Signaling offer and offerer's TURN candidate, then
3736// signaling answer (with DTLS fingerprint).
3737// 2. 9 media hops: Rest of the DTLS handshake. 3 hops in each direction when
3738// using TURN<->TURN pair, and DTLS exchange is 4 packets,
3739// the first of which should have arrived before the answer.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003740TEST_P(PeerConnectionIntegrationTest, EndToEndConnectionTimeWithTurnTurnPair) {
deadbeef1dcb1642017-03-29 21:08:16 -07003741 rtc::ScopedFakeClock fake_clock;
3742 // Some things use a time of "0" as a special value, so we need to start out
3743 // the fake clock at a nonzero time.
3744 // TODO(deadbeef): Fix this.
3745 fake_clock.AdvanceTime(rtc::TimeDelta::FromSeconds(1));
3746
3747 static constexpr int media_hop_delay_ms = 50;
3748 static constexpr int signaling_trip_delay_ms = 500;
3749 // For explanation of these values, see comment above.
3750 static constexpr int required_media_hops = 9;
3751 static constexpr int required_signaling_trips = 2;
3752 // For internal delays (such as posting an event asychronously).
3753 static constexpr int allowed_internal_delay_ms = 20;
3754 static constexpr int total_connection_time_ms =
3755 media_hop_delay_ms * required_media_hops +
3756 signaling_trip_delay_ms * required_signaling_trips +
3757 allowed_internal_delay_ms;
3758
3759 static const rtc::SocketAddress turn_server_1_internal_address{"88.88.88.0",
3760 3478};
3761 static const rtc::SocketAddress turn_server_1_external_address{"88.88.88.1",
3762 0};
3763 static const rtc::SocketAddress turn_server_2_internal_address{"99.99.99.0",
3764 3478};
3765 static const rtc::SocketAddress turn_server_2_external_address{"99.99.99.1",
3766 0};
3767 cricket::TestTurnServer turn_server_1(network_thread(),
3768 turn_server_1_internal_address,
3769 turn_server_1_external_address);
3770 cricket::TestTurnServer turn_server_2(network_thread(),
3771 turn_server_2_internal_address,
3772 turn_server_2_external_address);
Jonas Orelandbdcee282017-10-10 14:01:40 +02003773
deadbeef1dcb1642017-03-29 21:08:16 -07003774 // Bypass permission check on received packets so media can be sent before
3775 // the candidate is signaled.
3776 turn_server_1.set_enable_permission_checks(false);
3777 turn_server_2.set_enable_permission_checks(false);
3778
3779 PeerConnectionInterface::RTCConfiguration client_1_config;
3780 webrtc::PeerConnectionInterface::IceServer ice_server_1;
3781 ice_server_1.urls.push_back("turn:88.88.88.0:3478");
3782 ice_server_1.username = "test";
3783 ice_server_1.password = "test";
3784 client_1_config.servers.push_back(ice_server_1);
3785 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
3786 client_1_config.presume_writable_when_fully_relayed = true;
3787
3788 PeerConnectionInterface::RTCConfiguration client_2_config;
3789 webrtc::PeerConnectionInterface::IceServer ice_server_2;
3790 ice_server_2.urls.push_back("turn:99.99.99.0:3478");
3791 ice_server_2.username = "test";
3792 ice_server_2.password = "test";
3793 client_2_config.servers.push_back(ice_server_2);
3794 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
3795 client_2_config.presume_writable_when_fully_relayed = true;
3796
3797 ASSERT_TRUE(
3798 CreatePeerConnectionWrappersWithConfig(client_1_config, client_2_config));
3799 // Set up the simulated delays.
3800 SetSignalingDelayMs(signaling_trip_delay_ms);
3801 ConnectFakeSignaling();
3802 virtual_socket_server()->set_delay_mean(media_hop_delay_ms);
3803 virtual_socket_server()->UpdateDelayDistribution();
3804
3805 // Set "offer to receive audio/video" without adding any tracks, so we just
3806 // set up ICE/DTLS with no media.
3807 PeerConnectionInterface::RTCOfferAnswerOptions options;
3808 options.offer_to_receive_audio = 1;
3809 options.offer_to_receive_video = 1;
3810 caller()->SetOfferAnswerOptions(options);
3811 caller()->CreateAndSetAndSignalOffer();
deadbeef71452802017-05-07 17:21:01 -07003812 EXPECT_TRUE_SIMULATED_WAIT(DtlsConnected(), total_connection_time_ms,
3813 fake_clock);
deadbeef1dcb1642017-03-29 21:08:16 -07003814 // Need to free the clients here since they're using things we created on
3815 // the stack.
3816 delete SetCallerPcWrapperAndReturnCurrent(nullptr);
3817 delete SetCalleePcWrapperAndReturnCurrent(nullptr);
3818}
3819
Jonas Orelandbdcee282017-10-10 14:01:40 +02003820// Verify that a TurnCustomizer passed in through RTCConfiguration
3821// is actually used by the underlying TURN candidate pair.
3822// Note that turnport_unittest.cc contains more detailed, lower-level tests.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003823TEST_P(PeerConnectionIntegrationTest, TurnCustomizerUsedForTurnConnections) {
Jonas Orelandbdcee282017-10-10 14:01:40 +02003824 static const rtc::SocketAddress turn_server_1_internal_address{"88.88.88.0",
3825 3478};
3826 static const rtc::SocketAddress turn_server_1_external_address{"88.88.88.1",
3827 0};
3828 static const rtc::SocketAddress turn_server_2_internal_address{"99.99.99.0",
3829 3478};
3830 static const rtc::SocketAddress turn_server_2_external_address{"99.99.99.1",
3831 0};
3832 cricket::TestTurnServer turn_server_1(network_thread(),
3833 turn_server_1_internal_address,
3834 turn_server_1_external_address);
3835 cricket::TestTurnServer turn_server_2(network_thread(),
3836 turn_server_2_internal_address,
3837 turn_server_2_external_address);
3838
3839 PeerConnectionInterface::RTCConfiguration client_1_config;
3840 webrtc::PeerConnectionInterface::IceServer ice_server_1;
3841 ice_server_1.urls.push_back("turn:88.88.88.0:3478");
3842 ice_server_1.username = "test";
3843 ice_server_1.password = "test";
3844 client_1_config.servers.push_back(ice_server_1);
3845 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
3846 auto customizer1 = rtc::MakeUnique<cricket::TestTurnCustomizer>();
3847 client_1_config.turn_customizer = customizer1.get();
3848
3849 PeerConnectionInterface::RTCConfiguration client_2_config;
3850 webrtc::PeerConnectionInterface::IceServer ice_server_2;
3851 ice_server_2.urls.push_back("turn:99.99.99.0:3478");
3852 ice_server_2.username = "test";
3853 ice_server_2.password = "test";
3854 client_2_config.servers.push_back(ice_server_2);
3855 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
3856 auto customizer2 = rtc::MakeUnique<cricket::TestTurnCustomizer>();
3857 client_2_config.turn_customizer = customizer2.get();
3858
3859 ASSERT_TRUE(
3860 CreatePeerConnectionWrappersWithConfig(client_1_config, client_2_config));
3861 ConnectFakeSignaling();
3862
3863 // Set "offer to receive audio/video" without adding any tracks, so we just
3864 // set up ICE/DTLS with no media.
3865 PeerConnectionInterface::RTCOfferAnswerOptions options;
3866 options.offer_to_receive_audio = 1;
3867 options.offer_to_receive_video = 1;
3868 caller()->SetOfferAnswerOptions(options);
3869 caller()->CreateAndSetAndSignalOffer();
3870 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
3871
3872 EXPECT_GT(customizer1->allow_channel_data_cnt_, 0u);
3873 EXPECT_GT(customizer1->modify_cnt_, 0u);
3874
3875 EXPECT_GT(customizer2->allow_channel_data_cnt_, 0u);
3876 EXPECT_GT(customizer2->modify_cnt_, 0u);
3877
3878 // Need to free the clients here since they're using things we created on
3879 // the stack.
3880 delete SetCallerPcWrapperAndReturnCurrent(nullptr);
3881 delete SetCalleePcWrapperAndReturnCurrent(nullptr);
3882}
3883
deadbeefc964d0b2017-04-03 10:03:35 -07003884// Test that audio and video flow end-to-end when codec names don't use the
3885// expected casing, given that they're supposed to be case insensitive. To test
3886// this, all but one codec is removed from each media description, and its
3887// casing is changed.
3888//
3889// In the past, this has regressed and caused crashes/black video, due to the
3890// fact that code at some layers was doing case-insensitive comparisons and
3891// code at other layers was not.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003892TEST_P(PeerConnectionIntegrationTest, CodecNamesAreCaseInsensitive) {
deadbeefc964d0b2017-04-03 10:03:35 -07003893 ASSERT_TRUE(CreatePeerConnectionWrappers());
3894 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08003895 caller()->AddAudioVideoTracks();
3896 callee()->AddAudioVideoTracks();
deadbeefc964d0b2017-04-03 10:03:35 -07003897
3898 // Remove all but one audio/video codec (opus and VP8), and change the
3899 // casing of the caller's generated offer.
3900 caller()->SetGeneratedSdpMunger([](cricket::SessionDescription* description) {
3901 cricket::AudioContentDescription* audio =
3902 GetFirstAudioContentDescription(description);
3903 ASSERT_NE(nullptr, audio);
3904 auto audio_codecs = audio->codecs();
3905 audio_codecs.erase(std::remove_if(audio_codecs.begin(), audio_codecs.end(),
3906 [](const cricket::AudioCodec& codec) {
3907 return codec.name != "opus";
3908 }),
3909 audio_codecs.end());
3910 ASSERT_EQ(1u, audio_codecs.size());
3911 audio_codecs[0].name = "OpUs";
3912 audio->set_codecs(audio_codecs);
3913
3914 cricket::VideoContentDescription* video =
3915 GetFirstVideoContentDescription(description);
3916 ASSERT_NE(nullptr, video);
3917 auto video_codecs = video->codecs();
3918 video_codecs.erase(std::remove_if(video_codecs.begin(), video_codecs.end(),
3919 [](const cricket::VideoCodec& codec) {
3920 return codec.name != "VP8";
3921 }),
3922 video_codecs.end());
3923 ASSERT_EQ(1u, video_codecs.size());
3924 video_codecs[0].name = "vP8";
3925 video->set_codecs(video_codecs);
3926 });
3927
3928 caller()->CreateAndSetAndSignalOffer();
3929 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3930
3931 // Verify frames are still received end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003932 MediaExpectations media_expectations;
3933 media_expectations.ExpectBidirectionalAudioAndVideo();
3934 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeefc964d0b2017-04-03 10:03:35 -07003935}
3936
Seth Hampson2f0d7022018-02-20 11:54:42 -08003937TEST_P(PeerConnectionIntegrationTest, GetSources) {
hbos8d609f62017-04-10 07:39:05 -07003938 ASSERT_TRUE(CreatePeerConnectionWrappers());
3939 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08003940 caller()->AddAudioTrack();
hbos8d609f62017-04-10 07:39:05 -07003941 caller()->CreateAndSetAndSignalOffer();
3942 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
deadbeefd8ad7882017-04-18 16:01:17 -07003943 // Wait for one audio frame to be received by the callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003944 MediaExpectations media_expectations;
3945 media_expectations.CalleeExpectsSomeAudio(1);
3946 ASSERT_TRUE(ExpectNewFrames(media_expectations));
hbos8d609f62017-04-10 07:39:05 -07003947 ASSERT_GT(callee()->pc()->GetReceivers().size(), 0u);
3948 auto receiver = callee()->pc()->GetReceivers()[0];
3949 ASSERT_EQ(receiver->media_type(), cricket::MEDIA_TYPE_AUDIO);
3950
3951 auto contributing_sources = receiver->GetSources();
3952 ASSERT_GT(receiver->GetParameters().encodings.size(), 0u);
3953 EXPECT_EQ(receiver->GetParameters().encodings[0].ssrc,
3954 contributing_sources[0].source_id());
3955}
3956
deadbeef2f425aa2017-04-14 10:41:32 -07003957// Test that if a track is removed and added again with a different stream ID,
3958// the new stream ID is successfully communicated in SDP and media continues to
3959// flow end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003960// TODO(webrtc.bugs.org/8734): This test does not work for Unified Plan because
3961// it will not reuse a transceiver that has already been sending. After creating
3962// a new transceiver it tries to create an offer with two senders of the same
3963// track ids and it fails.
3964TEST_F(PeerConnectionIntegrationTestPlanB, RemoveAndAddTrackWithNewStreamId) {
deadbeef2f425aa2017-04-14 10:41:32 -07003965 ASSERT_TRUE(CreatePeerConnectionWrappers());
3966 ConnectFakeSignaling();
3967
3968 rtc::scoped_refptr<MediaStreamInterface> stream_1 =
3969 caller()->pc_factory()->CreateLocalMediaStream("stream_1");
3970 rtc::scoped_refptr<MediaStreamInterface> stream_2 =
3971 caller()->pc_factory()->CreateLocalMediaStream("stream_2");
3972
3973 // Add track using stream 1, do offer/answer.
3974 rtc::scoped_refptr<webrtc::AudioTrackInterface> track =
3975 caller()->CreateLocalAudioTrack();
3976 rtc::scoped_refptr<webrtc::RtpSenderInterface> sender =
3977 caller()->pc()->AddTrack(track, {stream_1.get()});
3978 caller()->CreateAndSetAndSignalOffer();
3979 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08003980 {
3981 MediaExpectations media_expectations;
3982 media_expectations.CalleeExpectsSomeAudio(1);
3983 ASSERT_TRUE(ExpectNewFrames(media_expectations));
3984 }
deadbeef2f425aa2017-04-14 10:41:32 -07003985 // Remove the sender, and create a new one with the new stream.
3986 caller()->pc()->RemoveTrack(sender);
3987 sender = caller()->pc()->AddTrack(track, {stream_2.get()});
3988 caller()->CreateAndSetAndSignalOffer();
3989 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3990 // Wait for additional audio frames to be received by the callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003991 {
3992 MediaExpectations media_expectations;
3993 media_expectations.CalleeExpectsSomeAudio();
3994 ASSERT_TRUE(ExpectNewFrames(media_expectations));
3995 }
deadbeef2f425aa2017-04-14 10:41:32 -07003996}
3997
Seth Hampson2f0d7022018-02-20 11:54:42 -08003998TEST_P(PeerConnectionIntegrationTest, RtcEventLogOutputWriteCalled) {
Elad Alon99c3fe52017-10-13 16:29:40 +02003999 ASSERT_TRUE(CreatePeerConnectionWrappers());
4000 ConnectFakeSignaling();
4001
4002 auto output = rtc::MakeUnique<testing::NiceMock<MockRtcEventLogOutput>>();
4003 ON_CALL(*output, IsActive()).WillByDefault(testing::Return(true));
4004 ON_CALL(*output, Write(::testing::_)).WillByDefault(testing::Return(true));
4005 EXPECT_CALL(*output, Write(::testing::_)).Times(::testing::AtLeast(1));
Bjorn Tereliusde939432017-11-20 17:38:14 +01004006 EXPECT_TRUE(caller()->pc()->StartRtcEventLog(
4007 std::move(output), webrtc::RtcEventLog::kImmediateOutput));
Elad Alon99c3fe52017-10-13 16:29:40 +02004008
Steve Anton15324772018-01-16 10:26:49 -08004009 caller()->AddAudioVideoTracks();
Elad Alon99c3fe52017-10-13 16:29:40 +02004010 caller()->CreateAndSetAndSignalOffer();
4011 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4012}
4013
Steve Antonede9ca52017-10-16 13:04:27 -07004014// Test that if candidates are only signaled by applying full session
4015// descriptions (instead of using AddIceCandidate), the peers can connect to
4016// each other and exchange media.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004017TEST_P(PeerConnectionIntegrationTest, MediaFlowsWhenCandidatesSetOnlyInSdp) {
Steve Antonede9ca52017-10-16 13:04:27 -07004018 ASSERT_TRUE(CreatePeerConnectionWrappers());
4019 // Each side will signal the session descriptions but not candidates.
4020 ConnectFakeSignalingForSdpOnly();
4021
4022 // Add audio video track and exchange the initial offer/answer with media
4023 // information only. This will start ICE gathering on each side.
Steve Anton15324772018-01-16 10:26:49 -08004024 caller()->AddAudioVideoTracks();
4025 callee()->AddAudioVideoTracks();
Steve Antonede9ca52017-10-16 13:04:27 -07004026 caller()->CreateAndSetAndSignalOffer();
4027
4028 // Wait for all candidates to be gathered on both the caller and callee.
4029 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceGatheringComplete,
4030 caller()->ice_gathering_state(), kDefaultTimeout);
4031 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceGatheringComplete,
4032 callee()->ice_gathering_state(), kDefaultTimeout);
4033
4034 // The candidates will now be included in the session description, so
4035 // signaling them will start the ICE connection.
4036 caller()->CreateAndSetAndSignalOffer();
4037 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4038
4039 // Ensure that media flows in both directions.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004040 MediaExpectations media_expectations;
4041 media_expectations.ExpectBidirectionalAudioAndVideo();
4042 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Antonede9ca52017-10-16 13:04:27 -07004043}
4044
henrika5f6bf242017-11-01 11:06:56 +01004045// Test that SetAudioPlayout can be used to disable audio playout from the
4046// start, then later enable it. This may be useful, for example, if the caller
4047// needs to play a local ringtone until some event occurs, after which it
4048// switches to playing the received audio.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004049TEST_P(PeerConnectionIntegrationTest, DisableAndEnableAudioPlayout) {
henrika5f6bf242017-11-01 11:06:56 +01004050 ASSERT_TRUE(CreatePeerConnectionWrappers());
4051 ConnectFakeSignaling();
4052
4053 // Set up audio-only call where audio playout is disabled on caller's side.
4054 caller()->pc()->SetAudioPlayout(false);
Steve Anton15324772018-01-16 10:26:49 -08004055 caller()->AddAudioTrack();
4056 callee()->AddAudioTrack();
henrika5f6bf242017-11-01 11:06:56 +01004057 caller()->CreateAndSetAndSignalOffer();
4058 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4059
4060 // Pump messages for a second.
4061 WAIT(false, 1000);
4062 // Since audio playout is disabled, the caller shouldn't have received
4063 // anything (at the playout level, at least).
4064 EXPECT_EQ(0, caller()->audio_frames_received());
4065 // As a sanity check, make sure the callee (for which playout isn't disabled)
4066 // did still see frames on its audio level.
4067 ASSERT_GT(callee()->audio_frames_received(), 0);
4068
4069 // Enable playout again, and ensure audio starts flowing.
4070 caller()->pc()->SetAudioPlayout(true);
Seth Hampson2f0d7022018-02-20 11:54:42 -08004071 MediaExpectations media_expectations;
4072 media_expectations.ExpectBidirectionalAudio();
4073 ASSERT_TRUE(ExpectNewFrames(media_expectations));
henrika5f6bf242017-11-01 11:06:56 +01004074}
4075
4076double GetAudioEnergyStat(PeerConnectionWrapper* pc) {
4077 auto report = pc->NewGetStats();
4078 auto track_stats_list =
4079 report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
4080 const webrtc::RTCMediaStreamTrackStats* remote_track_stats = nullptr;
4081 for (const auto* track_stats : track_stats_list) {
4082 if (track_stats->remote_source.is_defined() &&
4083 *track_stats->remote_source) {
4084 remote_track_stats = track_stats;
4085 break;
4086 }
4087 }
4088
4089 if (!remote_track_stats->total_audio_energy.is_defined()) {
4090 return 0.0;
4091 }
4092 return *remote_track_stats->total_audio_energy;
4093}
4094
4095// Test that if audio playout is disabled via the SetAudioPlayout() method, then
4096// incoming audio is still processed and statistics are generated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004097TEST_P(PeerConnectionIntegrationTest,
henrika5f6bf242017-11-01 11:06:56 +01004098 DisableAudioPlayoutStillGeneratesAudioStats) {
4099 ASSERT_TRUE(CreatePeerConnectionWrappers());
4100 ConnectFakeSignaling();
4101
4102 // Set up audio-only call where playout is disabled but audio-processing is
4103 // still active.
Steve Anton15324772018-01-16 10:26:49 -08004104 caller()->AddAudioTrack();
4105 callee()->AddAudioTrack();
henrika5f6bf242017-11-01 11:06:56 +01004106 caller()->pc()->SetAudioPlayout(false);
4107
4108 caller()->CreateAndSetAndSignalOffer();
4109 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4110
4111 // Wait for the callee to receive audio stats.
4112 EXPECT_TRUE_WAIT(GetAudioEnergyStat(caller()) > 0, kMaxWaitForFramesMs);
4113}
4114
henrika4f167df2017-11-01 14:45:55 +01004115// Test that SetAudioRecording can be used to disable audio recording from the
4116// start, then later enable it. This may be useful, for example, if the caller
4117// wants to ensure that no audio resources are active before a certain state
4118// is reached.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004119TEST_P(PeerConnectionIntegrationTest, DisableAndEnableAudioRecording) {
henrika4f167df2017-11-01 14:45:55 +01004120 ASSERT_TRUE(CreatePeerConnectionWrappers());
4121 ConnectFakeSignaling();
4122
4123 // Set up audio-only call where audio recording is disabled on caller's side.
4124 caller()->pc()->SetAudioRecording(false);
Steve Anton15324772018-01-16 10:26:49 -08004125 caller()->AddAudioTrack();
4126 callee()->AddAudioTrack();
henrika4f167df2017-11-01 14:45:55 +01004127 caller()->CreateAndSetAndSignalOffer();
4128 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4129
4130 // Pump messages for a second.
4131 WAIT(false, 1000);
4132 // Since caller has disabled audio recording, the callee shouldn't have
4133 // received anything.
4134 EXPECT_EQ(0, callee()->audio_frames_received());
4135 // As a sanity check, make sure the caller did still see frames on its
4136 // audio level since audio recording is enabled on the calle side.
4137 ASSERT_GT(caller()->audio_frames_received(), 0);
4138
4139 // Enable audio recording again, and ensure audio starts flowing.
4140 caller()->pc()->SetAudioRecording(true);
Seth Hampson2f0d7022018-02-20 11:54:42 -08004141 MediaExpectations media_expectations;
4142 media_expectations.ExpectBidirectionalAudio();
4143 ASSERT_TRUE(ExpectNewFrames(media_expectations));
henrika4f167df2017-11-01 14:45:55 +01004144}
4145
Taylor Brandstetter389a97c2018-01-03 16:26:06 -08004146// Test that after closing PeerConnections, they stop sending any packets (ICE,
4147// DTLS, RTP...).
Seth Hampson2f0d7022018-02-20 11:54:42 -08004148TEST_P(PeerConnectionIntegrationTest, ClosingConnectionStopsPacketFlow) {
Taylor Brandstetter389a97c2018-01-03 16:26:06 -08004149 // Set up audio/video/data, wait for some frames to be received.
4150 ASSERT_TRUE(CreatePeerConnectionWrappers());
4151 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08004152 caller()->AddAudioVideoTracks();
Taylor Brandstetter389a97c2018-01-03 16:26:06 -08004153#ifdef HAVE_SCTP
4154 caller()->CreateDataChannel();
4155#endif
4156 caller()->CreateAndSetAndSignalOffer();
4157 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08004158 MediaExpectations media_expectations;
4159 media_expectations.CalleeExpectsSomeAudioAndVideo();
4160 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Taylor Brandstetter389a97c2018-01-03 16:26:06 -08004161 // Close PeerConnections.
4162 caller()->pc()->Close();
4163 callee()->pc()->Close();
4164 // Pump messages for a second, and ensure no new packets end up sent.
4165 uint32_t sent_packets_a = virtual_socket_server()->sent_packets();
4166 WAIT(false, 1000);
4167 uint32_t sent_packets_b = virtual_socket_server()->sent_packets();
4168 EXPECT_EQ(sent_packets_a, sent_packets_b);
4169}
4170
Steve Anton7eca0932018-03-30 15:18:41 -07004171// Test that transport stats are generated by the RTCStatsCollector for a
4172// connection that only involves data channels. This is a regression test for
4173// crbug.com/826972.
4174#ifdef HAVE_SCTP
4175TEST_P(PeerConnectionIntegrationTest,
4176 TransportStatsReportedForDataChannelOnlyConnection) {
4177 ASSERT_TRUE(CreatePeerConnectionWrappers());
4178 ConnectFakeSignaling();
4179 caller()->CreateDataChannel();
4180
4181 caller()->CreateAndSetAndSignalOffer();
4182 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4183 ASSERT_TRUE_WAIT(callee()->data_channel(), kDefaultTimeout);
4184
4185 auto caller_report = caller()->NewGetStats();
4186 EXPECT_EQ(1u, caller_report->GetStatsOfType<RTCTransportStats>().size());
4187 auto callee_report = callee()->NewGetStats();
4188 EXPECT_EQ(1u, callee_report->GetStatsOfType<RTCTransportStats>().size());
4189}
4190#endif // HAVE_SCTP
4191
Seth Hampson2f0d7022018-02-20 11:54:42 -08004192INSTANTIATE_TEST_CASE_P(PeerConnectionIntegrationTest,
4193 PeerConnectionIntegrationTest,
4194 Values(SdpSemantics::kPlanB,
4195 SdpSemantics::kUnifiedPlan));
Steve Antond3679212018-01-17 17:41:02 -08004196
Steve Anton74255ff2018-01-24 18:32:57 -08004197// Tests that verify interoperability between Plan B and Unified Plan
4198// PeerConnections.
4199class PeerConnectionIntegrationInteropTest
Seth Hampson2f0d7022018-02-20 11:54:42 -08004200 : public PeerConnectionIntegrationBaseTest,
Steve Anton74255ff2018-01-24 18:32:57 -08004201 public ::testing::WithParamInterface<
4202 std::tuple<SdpSemantics, SdpSemantics>> {
4203 protected:
Seth Hampson2f0d7022018-02-20 11:54:42 -08004204 // Setting the SdpSemantics for the base test to kDefault does not matter
4205 // because we specify not to use the test semantics when creating
4206 // PeerConnectionWrappers.
Steve Anton74255ff2018-01-24 18:32:57 -08004207 PeerConnectionIntegrationInteropTest()
Steve Anton3acffc32018-04-12 17:21:03 -07004208 : PeerConnectionIntegrationBaseTest(SdpSemantics::kPlanB),
Seth Hampson2f0d7022018-02-20 11:54:42 -08004209 caller_semantics_(std::get<0>(GetParam())),
Steve Anton74255ff2018-01-24 18:32:57 -08004210 callee_semantics_(std::get<1>(GetParam())) {}
4211
4212 bool CreatePeerConnectionWrappersWithSemantics() {
Steve Anton3acffc32018-04-12 17:21:03 -07004213 return CreatePeerConnectionWrappersWithSdpSemantics(caller_semantics_,
4214 callee_semantics_);
Steve Anton74255ff2018-01-24 18:32:57 -08004215 }
4216
4217 const SdpSemantics caller_semantics_;
4218 const SdpSemantics callee_semantics_;
4219};
4220
4221TEST_P(PeerConnectionIntegrationInteropTest, NoMediaLocalToNoMediaRemote) {
4222 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
4223 ConnectFakeSignaling();
4224
4225 caller()->CreateAndSetAndSignalOffer();
4226 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4227}
4228
4229TEST_P(PeerConnectionIntegrationInteropTest, OneAudioLocalToNoMediaRemote) {
4230 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
4231 ConnectFakeSignaling();
4232 auto audio_sender = caller()->AddAudioTrack();
4233
4234 caller()->CreateAndSetAndSignalOffer();
4235 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4236
4237 // Verify that one audio receiver has been created on the remote and that it
4238 // has the same track ID as the sending track.
4239 auto receivers = callee()->pc()->GetReceivers();
4240 ASSERT_EQ(1u, receivers.size());
4241 EXPECT_EQ(cricket::MEDIA_TYPE_AUDIO, receivers[0]->media_type());
4242 EXPECT_EQ(receivers[0]->track()->id(), audio_sender->track()->id());
4243
Seth Hampson2f0d7022018-02-20 11:54:42 -08004244 MediaExpectations media_expectations;
4245 media_expectations.CalleeExpectsSomeAudio();
4246 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08004247}
4248
4249TEST_P(PeerConnectionIntegrationInteropTest, OneAudioOneVideoToNoMediaRemote) {
4250 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
4251 ConnectFakeSignaling();
4252 auto video_sender = caller()->AddVideoTrack();
4253 auto audio_sender = caller()->AddAudioTrack();
4254
4255 caller()->CreateAndSetAndSignalOffer();
4256 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4257
4258 // Verify that one audio and one video receiver have been created on the
4259 // remote and that they have the same track IDs as the sending tracks.
4260 auto audio_receivers =
4261 callee()->GetReceiversOfType(cricket::MEDIA_TYPE_AUDIO);
4262 ASSERT_EQ(1u, audio_receivers.size());
4263 EXPECT_EQ(audio_receivers[0]->track()->id(), audio_sender->track()->id());
4264 auto video_receivers =
4265 callee()->GetReceiversOfType(cricket::MEDIA_TYPE_VIDEO);
4266 ASSERT_EQ(1u, video_receivers.size());
4267 EXPECT_EQ(video_receivers[0]->track()->id(), video_sender->track()->id());
4268
Seth Hampson2f0d7022018-02-20 11:54:42 -08004269 MediaExpectations media_expectations;
4270 media_expectations.CalleeExpectsSomeAudioAndVideo();
4271 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08004272}
4273
4274TEST_P(PeerConnectionIntegrationInteropTest,
4275 OneAudioOneVideoLocalToOneAudioOneVideoRemote) {
4276 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
4277 ConnectFakeSignaling();
4278 caller()->AddAudioVideoTracks();
4279 callee()->AddAudioVideoTracks();
4280
4281 caller()->CreateAndSetAndSignalOffer();
4282 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4283
Seth Hampson2f0d7022018-02-20 11:54:42 -08004284 MediaExpectations media_expectations;
4285 media_expectations.ExpectBidirectionalAudioAndVideo();
4286 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08004287}
4288
4289TEST_P(PeerConnectionIntegrationInteropTest,
4290 ReverseRolesOneAudioLocalToOneVideoRemote) {
4291 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
4292 ConnectFakeSignaling();
4293 caller()->AddAudioTrack();
4294 callee()->AddVideoTrack();
4295
4296 caller()->CreateAndSetAndSignalOffer();
4297 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4298
4299 // Verify that only the audio track has been negotiated.
4300 EXPECT_EQ(0u, caller()->GetReceiversOfType(cricket::MEDIA_TYPE_VIDEO).size());
4301 // Might also check that the callee's NegotiationNeeded flag is set.
4302
4303 // Reverse roles.
4304 callee()->CreateAndSetAndSignalOffer();
4305 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4306
Seth Hampson2f0d7022018-02-20 11:54:42 -08004307 MediaExpectations media_expectations;
4308 media_expectations.CallerExpectsSomeVideo();
4309 media_expectations.CalleeExpectsSomeAudio();
4310 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08004311}
4312
Steve Antonba42e992018-04-09 14:10:01 -07004313INSTANTIATE_TEST_CASE_P(
4314 PeerConnectionIntegrationTest,
4315 PeerConnectionIntegrationInteropTest,
4316 Values(std::make_tuple(SdpSemantics::kPlanB, SdpSemantics::kUnifiedPlan),
4317 std::make_tuple(SdpSemantics::kUnifiedPlan, SdpSemantics::kPlanB)));
4318
4319// Test that if the Unified Plan side offers two video tracks then the Plan B
4320// side will only see the first one and ignore the second.
4321TEST_F(PeerConnectionIntegrationTestPlanB, TwoVideoUnifiedPlanToNoMediaPlanB) {
Steve Anton3acffc32018-04-12 17:21:03 -07004322 ASSERT_TRUE(CreatePeerConnectionWrappersWithSdpSemantics(
4323 SdpSemantics::kUnifiedPlan, SdpSemantics::kPlanB));
Steve Anton74255ff2018-01-24 18:32:57 -08004324 ConnectFakeSignaling();
4325 auto first_sender = caller()->AddVideoTrack();
4326 caller()->AddVideoTrack();
4327
4328 caller()->CreateAndSetAndSignalOffer();
4329 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4330
4331 // Verify that there is only one receiver and it corresponds to the first
4332 // added track.
4333 auto receivers = callee()->pc()->GetReceivers();
4334 ASSERT_EQ(1u, receivers.size());
4335 EXPECT_TRUE(receivers[0]->track()->enabled());
4336 EXPECT_EQ(first_sender->track()->id(), receivers[0]->track()->id());
4337
Seth Hampson2f0d7022018-02-20 11:54:42 -08004338 MediaExpectations media_expectations;
4339 media_expectations.CalleeExpectsSomeVideo();
4340 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08004341}
4342
deadbeef1dcb1642017-03-29 21:08:16 -07004343} // namespace
4344
4345#endif // if !defined(THREAD_SANITIZER)