blob: 40218e72d20679667de1cc5183f8483af15b99d7 [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 Antondf527fd2018-04-27 15:52:03 -070064using cricket::StreamParams;
Steve Antonede9ca52017-10-16 13:04:27 -070065using rtc::SocketAddress;
Seth Hampson2f0d7022018-02-20 11:54:42 -080066using ::testing::Combine;
Steve Antonede9ca52017-10-16 13:04:27 -070067using ::testing::ElementsAre;
68using ::testing::Values;
deadbeef1dcb1642017-03-29 21:08:16 -070069using webrtc::DataBuffer;
70using webrtc::DataChannelInterface;
71using webrtc::DtmfSender;
72using webrtc::DtmfSenderInterface;
73using webrtc::DtmfSenderObserverInterface;
74using webrtc::FakeConstraints;
Steve Anton15324772018-01-16 10:26:49 -080075using webrtc::FakeVideoTrackRenderer;
deadbeef1dcb1642017-03-29 21:08:16 -070076using webrtc::MediaConstraintsInterface;
77using webrtc::MediaStreamInterface;
78using webrtc::MediaStreamTrackInterface;
79using webrtc::MockCreateSessionDescriptionObserver;
80using webrtc::MockDataChannelObserver;
81using webrtc::MockSetSessionDescriptionObserver;
82using webrtc::MockStatsObserver;
83using webrtc::ObserverInterface;
Steve Anton8c0f7a72017-10-03 10:03:10 -070084using webrtc::PeerConnection;
deadbeef1dcb1642017-03-29 21:08:16 -070085using webrtc::PeerConnectionInterface;
Steve Anton74255ff2018-01-24 18:32:57 -080086using RTCConfiguration = PeerConnectionInterface::RTCConfiguration;
deadbeef1dcb1642017-03-29 21:08:16 -070087using webrtc::PeerConnectionFactory;
Steve Anton8c0f7a72017-10-03 10:03:10 -070088using webrtc::PeerConnectionProxy;
Steve Anton15324772018-01-16 10:26:49 -080089using webrtc::RTCErrorType;
Steve Anton7eca0932018-03-30 15:18:41 -070090using webrtc::RTCTransportStats;
Steve Anton74255ff2018-01-24 18:32:57 -080091using webrtc::RtpSenderInterface;
Mirko Bonadeic61ce0d2017-11-21 17:04:20 +010092using webrtc::RtpReceiverInterface;
Seth Hampson2f0d7022018-02-20 11:54:42 -080093using webrtc::RtpSenderInterface;
94using webrtc::RtpTransceiverDirection;
95using webrtc::RtpTransceiverInit;
96using webrtc::RtpTransceiverInterface;
Steve Antond3679212018-01-17 17:41:02 -080097using webrtc::SdpSemantics;
Steve Antona3a92c22017-12-07 10:27:41 -080098using webrtc::SdpType;
deadbeef1dcb1642017-03-29 21:08:16 -070099using webrtc::SessionDescriptionInterface;
100using webrtc::StreamCollectionInterface;
Steve Anton15324772018-01-16 10:26:49 -0800101using webrtc::VideoTrackInterface;
deadbeef1dcb1642017-03-29 21:08:16 -0700102
103namespace {
104
105static const int kDefaultTimeout = 10000;
106static const int kMaxWaitForStatsMs = 3000;
107static const int kMaxWaitForActivationMs = 5000;
108static const int kMaxWaitForFramesMs = 10000;
109// Default number of audio/video frames to wait for before considering a test
110// successful.
111static const int kDefaultExpectedAudioFrameCount = 3;
112static const int kDefaultExpectedVideoFrameCount = 3;
113
deadbeef1dcb1642017-03-29 21:08:16 -0700114static const char kDataChannelLabel[] = "data_channel";
115
116// SRTP cipher name negotiated by the tests. This must be updated if the
117// default changes.
Taylor Brandstetterfd350d72018-04-03 16:29:26 -0700118static const int kDefaultSrtpCryptoSuite = rtc::SRTP_AES128_CM_SHA1_80;
deadbeef1dcb1642017-03-29 21:08:16 -0700119static const int kDefaultSrtpCryptoSuiteGcm = rtc::SRTP_AEAD_AES_256_GCM;
120
Steve Antonede9ca52017-10-16 13:04:27 -0700121static const SocketAddress kDefaultLocalAddress("192.168.1.1", 0);
122
deadbeef1dcb1642017-03-29 21:08:16 -0700123// Helper function for constructing offer/answer options to initiate an ICE
124// restart.
125PeerConnectionInterface::RTCOfferAnswerOptions IceRestartOfferAnswerOptions() {
126 PeerConnectionInterface::RTCOfferAnswerOptions options;
127 options.ice_restart = true;
128 return options;
129}
130
deadbeefd8ad7882017-04-18 16:01:17 -0700131// Remove all stream information (SSRCs, track IDs, etc.) and "msid-semantic"
132// attribute from received SDP, simulating a legacy endpoint.
133void RemoveSsrcsAndMsids(cricket::SessionDescription* desc) {
134 for (ContentInfo& content : desc->contents()) {
Steve Antonb1c1de12017-12-21 15:14:30 -0800135 content.media_description()->mutable_streams().clear();
deadbeefd8ad7882017-04-18 16:01:17 -0700136 }
137 desc->set_msid_supported(false);
138}
139
Seth Hampson5897a6e2018-04-03 11:16:33 -0700140// Removes all stream information besides the stream ids, simulating an
141// endpoint that only signals a=msid lines to convey stream_ids.
142void RemoveSsrcsAndKeepMsids(cricket::SessionDescription* desc) {
143 for (ContentInfo& content : desc->contents()) {
Steve Antondf527fd2018-04-27 15:52:03 -0700144 std::string track_id;
Seth Hampson5897a6e2018-04-03 11:16:33 -0700145 std::vector<std::string> stream_ids;
146 if (!content.media_description()->streams().empty()) {
Steve Antondf527fd2018-04-27 15:52:03 -0700147 const StreamParams& first_stream =
148 content.media_description()->streams()[0];
149 track_id = first_stream.id;
150 stream_ids = first_stream.stream_ids();
Seth Hampson5897a6e2018-04-03 11:16:33 -0700151 }
152 content.media_description()->mutable_streams().clear();
Steve Antondf527fd2018-04-27 15:52:03 -0700153 StreamParams new_stream;
154 new_stream.id = track_id;
Seth Hampson5897a6e2018-04-03 11:16:33 -0700155 new_stream.set_stream_ids(stream_ids);
156 content.media_description()->AddStream(new_stream);
157 }
158}
159
zhihuangf8164932017-05-19 13:09:47 -0700160int FindFirstMediaStatsIndexByKind(
161 const std::string& kind,
162 const std::vector<const webrtc::RTCMediaStreamTrackStats*>&
163 media_stats_vec) {
164 for (size_t i = 0; i < media_stats_vec.size(); i++) {
165 if (media_stats_vec[i]->kind.ValueToString() == kind) {
166 return i;
167 }
168 }
169 return -1;
170}
171
deadbeef1dcb1642017-03-29 21:08:16 -0700172class SignalingMessageReceiver {
173 public:
Steve Antona3a92c22017-12-07 10:27:41 -0800174 virtual void ReceiveSdpMessage(SdpType type, const std::string& msg) = 0;
deadbeef1dcb1642017-03-29 21:08:16 -0700175 virtual void ReceiveIceMessage(const std::string& sdp_mid,
176 int sdp_mline_index,
177 const std::string& msg) = 0;
178
179 protected:
180 SignalingMessageReceiver() {}
181 virtual ~SignalingMessageReceiver() {}
182};
183
184class MockRtpReceiverObserver : public webrtc::RtpReceiverObserverInterface {
185 public:
186 explicit MockRtpReceiverObserver(cricket::MediaType media_type)
187 : expected_media_type_(media_type) {}
188
189 void OnFirstPacketReceived(cricket::MediaType media_type) override {
190 ASSERT_EQ(expected_media_type_, media_type);
191 first_packet_received_ = true;
192 }
193
194 bool first_packet_received() const { return first_packet_received_; }
195
196 virtual ~MockRtpReceiverObserver() {}
197
198 private:
199 bool first_packet_received_ = false;
200 cricket::MediaType expected_media_type_;
201};
202
203// Helper class that wraps a peer connection, observes it, and can accept
204// signaling messages from another wrapper.
205//
206// Uses a fake network, fake A/V capture, and optionally fake
207// encoders/decoders, though they aren't used by default since they don't
208// advertise support of any codecs.
Steve Anton94286cb2017-09-26 16:20:19 -0700209// TODO(steveanton): See how this could become a subclass of
Seth Hampson2f0d7022018-02-20 11:54:42 -0800210// PeerConnectionWrapper defined in peerconnectionwrapper.h.
deadbeef1dcb1642017-03-29 21:08:16 -0700211class PeerConnectionWrapper : public webrtc::PeerConnectionObserver,
Steve Anton15324772018-01-16 10:26:49 -0800212 public SignalingMessageReceiver {
deadbeef1dcb1642017-03-29 21:08:16 -0700213 public:
214 // Different factory methods for convenience.
215 // TODO(deadbeef): Could use the pattern of:
216 //
217 // PeerConnectionWrapper =
218 // WrapperBuilder.WithConfig(...).WithOptions(...).build();
219 //
220 // To reduce some code duplication.
221 static PeerConnectionWrapper* CreateWithDtlsIdentityStore(
222 const std::string& debug_name,
223 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator,
224 rtc::Thread* network_thread,
225 rtc::Thread* worker_thread) {
226 PeerConnectionWrapper* client(new PeerConnectionWrapper(debug_name));
227 if (!client->Init(nullptr, nullptr, nullptr, std::move(cert_generator),
228 network_thread, worker_thread)) {
229 delete client;
230 return nullptr;
231 }
232 return client;
233 }
234
deadbeef2f425aa2017-04-14 10:41:32 -0700235 webrtc::PeerConnectionFactoryInterface* pc_factory() const {
236 return peer_connection_factory_.get();
237 }
238
deadbeef1dcb1642017-03-29 21:08:16 -0700239 webrtc::PeerConnectionInterface* pc() const { return peer_connection_.get(); }
240
241 // If a signaling message receiver is set (via ConnectFakeSignaling), this
242 // will set the whole offer/answer exchange in motion. Just need to wait for
243 // the signaling state to reach "stable".
244 void CreateAndSetAndSignalOffer() {
245 auto offer = CreateOffer();
246 ASSERT_NE(nullptr, offer);
247 EXPECT_TRUE(SetLocalDescriptionAndSendSdpMessage(std::move(offer)));
248 }
249
250 // Sets the options to be used when CreateAndSetAndSignalOffer is called, or
251 // when a remote offer is received (via fake signaling) and an answer is
252 // generated. By default, uses default options.
253 void SetOfferAnswerOptions(
254 const PeerConnectionInterface::RTCOfferAnswerOptions& options) {
255 offer_answer_options_ = options;
256 }
257
258 // Set a callback to be invoked when SDP is received via the fake signaling
259 // channel, which provides an opportunity to munge (modify) the SDP. This is
260 // used to test SDP being applied that a PeerConnection would normally not
261 // generate, but a non-JSEP endpoint might.
262 void SetReceivedSdpMunger(
263 std::function<void(cricket::SessionDescription*)> munger) {
Mirko Bonadeic61ce0d2017-11-21 17:04:20 +0100264 received_sdp_munger_ = std::move(munger);
deadbeef1dcb1642017-03-29 21:08:16 -0700265 }
266
deadbeefc964d0b2017-04-03 10:03:35 -0700267 // Similar to the above, but this is run on SDP immediately after it's
deadbeef1dcb1642017-03-29 21:08:16 -0700268 // generated.
269 void SetGeneratedSdpMunger(
270 std::function<void(cricket::SessionDescription*)> munger) {
Mirko Bonadeic61ce0d2017-11-21 17:04:20 +0100271 generated_sdp_munger_ = std::move(munger);
deadbeef1dcb1642017-03-29 21:08:16 -0700272 }
273
Seth Hampson2f0d7022018-02-20 11:54:42 -0800274 // Set a callback to be invoked when a remote offer is received via the fake
275 // signaling channel. This provides an opportunity to change the
276 // PeerConnection state before an answer is created and sent to the caller.
277 void SetRemoteOfferHandler(std::function<void()> handler) {
278 remote_offer_handler_ = std::move(handler);
279 }
280
Steve Antonede9ca52017-10-16 13:04:27 -0700281 // Every ICE connection state in order that has been seen by the observer.
282 std::vector<PeerConnectionInterface::IceConnectionState>
283 ice_connection_state_history() const {
284 return ice_connection_state_history_;
285 }
Steve Anton6f25b092017-10-23 09:39:20 -0700286 void clear_ice_connection_state_history() {
287 ice_connection_state_history_.clear();
288 }
Steve Antonede9ca52017-10-16 13:04:27 -0700289
290 // Every ICE gathering state in order that has been seen by the observer.
291 std::vector<PeerConnectionInterface::IceGatheringState>
292 ice_gathering_state_history() const {
293 return ice_gathering_state_history_;
deadbeef1dcb1642017-03-29 21:08:16 -0700294 }
295
Steve Anton15324772018-01-16 10:26:49 -0800296 void AddAudioVideoTracks() {
297 AddAudioTrack();
298 AddVideoTrack();
deadbeef1dcb1642017-03-29 21:08:16 -0700299 }
300
Steve Anton74255ff2018-01-24 18:32:57 -0800301 rtc::scoped_refptr<RtpSenderInterface> AddAudioTrack() {
302 return AddTrack(CreateLocalAudioTrack());
303 }
deadbeef1dcb1642017-03-29 21:08:16 -0700304
Steve Anton74255ff2018-01-24 18:32:57 -0800305 rtc::scoped_refptr<RtpSenderInterface> AddVideoTrack() {
306 return AddTrack(CreateLocalVideoTrack());
307 }
deadbeef1dcb1642017-03-29 21:08:16 -0700308
309 rtc::scoped_refptr<webrtc::AudioTrackInterface> CreateLocalAudioTrack() {
310 FakeConstraints constraints;
311 // Disable highpass filter so that we can get all the test audio frames.
312 constraints.AddMandatory(MediaConstraintsInterface::kHighpassFilter, false);
313 rtc::scoped_refptr<webrtc::AudioSourceInterface> source =
314 peer_connection_factory_->CreateAudioSource(&constraints);
315 // TODO(perkj): Test audio source when it is implemented. Currently audio
316 // always use the default input.
deadbeefb1a15d72017-09-07 14:12:05 -0700317 return peer_connection_factory_->CreateAudioTrack(rtc::CreateRandomUuid(),
deadbeef1dcb1642017-03-29 21:08:16 -0700318 source);
319 }
320
321 rtc::scoped_refptr<webrtc::VideoTrackInterface> CreateLocalVideoTrack() {
deadbeefb1a15d72017-09-07 14:12:05 -0700322 return CreateLocalVideoTrackInternal(FakeConstraints(),
323 webrtc::kVideoRotation_0);
deadbeef1dcb1642017-03-29 21:08:16 -0700324 }
325
326 rtc::scoped_refptr<webrtc::VideoTrackInterface>
327 CreateLocalVideoTrackWithConstraints(const FakeConstraints& constraints) {
deadbeefb1a15d72017-09-07 14:12:05 -0700328 return CreateLocalVideoTrackInternal(constraints, webrtc::kVideoRotation_0);
deadbeef1dcb1642017-03-29 21:08:16 -0700329 }
330
331 rtc::scoped_refptr<webrtc::VideoTrackInterface>
332 CreateLocalVideoTrackWithRotation(webrtc::VideoRotation rotation) {
deadbeefb1a15d72017-09-07 14:12:05 -0700333 return CreateLocalVideoTrackInternal(FakeConstraints(), rotation);
deadbeef1dcb1642017-03-29 21:08:16 -0700334 }
335
Steve Anton74255ff2018-01-24 18:32:57 -0800336 rtc::scoped_refptr<RtpSenderInterface> AddTrack(
337 rtc::scoped_refptr<MediaStreamTrackInterface> track,
Seth Hampson845e8782018-03-02 11:34:10 -0800338 const std::vector<std::string>& stream_ids = {}) {
339 auto result = pc()->AddTrack(track, stream_ids);
Steve Anton15324772018-01-16 10:26:49 -0800340 EXPECT_EQ(RTCErrorType::NONE, result.error().type());
Steve Anton74255ff2018-01-24 18:32:57 -0800341 return result.MoveValue();
342 }
343
344 std::vector<rtc::scoped_refptr<RtpReceiverInterface>> GetReceiversOfType(
345 cricket::MediaType media_type) {
346 std::vector<rtc::scoped_refptr<RtpReceiverInterface>> receivers;
347 for (auto receiver : pc()->GetReceivers()) {
348 if (receiver->media_type() == media_type) {
349 receivers.push_back(receiver);
350 }
351 }
352 return receivers;
deadbeef1dcb1642017-03-29 21:08:16 -0700353 }
354
Seth Hampson2f0d7022018-02-20 11:54:42 -0800355 rtc::scoped_refptr<RtpTransceiverInterface> GetFirstTransceiverOfType(
356 cricket::MediaType media_type) {
357 for (auto transceiver : pc()->GetTransceivers()) {
358 if (transceiver->receiver()->media_type() == media_type) {
359 return transceiver;
360 }
361 }
362 return nullptr;
363 }
364
deadbeef1dcb1642017-03-29 21:08:16 -0700365 bool SignalingStateStable() {
366 return pc()->signaling_state() == webrtc::PeerConnectionInterface::kStable;
367 }
368
369 void CreateDataChannel() { CreateDataChannel(nullptr); }
370
371 void CreateDataChannel(const webrtc::DataChannelInit* init) {
Steve Antonda6c0952017-10-23 11:41:54 -0700372 CreateDataChannel(kDataChannelLabel, init);
373 }
374
375 void CreateDataChannel(const std::string& label,
376 const webrtc::DataChannelInit* init) {
377 data_channel_ = pc()->CreateDataChannel(label, init);
deadbeef1dcb1642017-03-29 21:08:16 -0700378 ASSERT_TRUE(data_channel_.get() != nullptr);
379 data_observer_.reset(new MockDataChannelObserver(data_channel_));
380 }
381
382 DataChannelInterface* data_channel() { return data_channel_; }
383 const MockDataChannelObserver* data_observer() const {
384 return data_observer_.get();
385 }
386
387 int audio_frames_received() const {
388 return fake_audio_capture_module_->frames_received();
389 }
390
391 // Takes minimum of video frames received for each track.
392 //
393 // Can be used like:
394 // EXPECT_GE(expected_frames, min_video_frames_received_per_track());
395 //
396 // To ensure that all video tracks received at least a certain number of
397 // frames.
398 int min_video_frames_received_per_track() const {
399 int min_frames = INT_MAX;
400 if (video_decoder_factory_enabled_) {
401 const std::vector<FakeWebRtcVideoDecoder*>& decoders =
402 fake_video_decoder_factory_->decoders();
403 if (decoders.empty()) {
404 return 0;
405 }
406 for (FakeWebRtcVideoDecoder* decoder : decoders) {
407 min_frames = std::min(min_frames, decoder->GetNumFramesReceived());
408 }
409 return min_frames;
410 } else {
411 if (fake_video_renderers_.empty()) {
412 return 0;
413 }
414
415 for (const auto& pair : fake_video_renderers_) {
416 min_frames = std::min(min_frames, pair.second->num_rendered_frames());
417 }
418 return min_frames;
419 }
420 }
421
422 // In contrast to the above, sums the video frames received for all tracks.
423 // Can be used to verify that no video frames were received, or that the
424 // counts didn't increase.
425 int total_video_frames_received() const {
426 int total = 0;
427 if (video_decoder_factory_enabled_) {
428 const std::vector<FakeWebRtcVideoDecoder*>& decoders =
429 fake_video_decoder_factory_->decoders();
430 for (const FakeWebRtcVideoDecoder* decoder : decoders) {
431 total += decoder->GetNumFramesReceived();
432 }
433 } else {
434 for (const auto& pair : fake_video_renderers_) {
435 total += pair.second->num_rendered_frames();
436 }
437 for (const auto& renderer : removed_fake_video_renderers_) {
438 total += renderer->num_rendered_frames();
439 }
440 }
441 return total;
442 }
443
444 // Returns a MockStatsObserver in a state after stats gathering finished,
445 // which can be used to access the gathered stats.
deadbeefd8ad7882017-04-18 16:01:17 -0700446 rtc::scoped_refptr<MockStatsObserver> OldGetStatsForTrack(
deadbeef1dcb1642017-03-29 21:08:16 -0700447 webrtc::MediaStreamTrackInterface* track) {
448 rtc::scoped_refptr<MockStatsObserver> observer(
449 new rtc::RefCountedObject<MockStatsObserver>());
450 EXPECT_TRUE(peer_connection_->GetStats(
451 observer, nullptr, PeerConnectionInterface::kStatsOutputLevelStandard));
452 EXPECT_TRUE_WAIT(observer->called(), kDefaultTimeout);
453 return observer;
454 }
455
456 // Version that doesn't take a track "filter", and gathers all stats.
deadbeefd8ad7882017-04-18 16:01:17 -0700457 rtc::scoped_refptr<MockStatsObserver> OldGetStats() {
458 return OldGetStatsForTrack(nullptr);
459 }
460
461 // Synchronously gets stats and returns them. If it times out, fails the test
462 // and returns null.
463 rtc::scoped_refptr<const webrtc::RTCStatsReport> NewGetStats() {
464 rtc::scoped_refptr<webrtc::MockRTCStatsCollectorCallback> callback(
465 new rtc::RefCountedObject<webrtc::MockRTCStatsCollectorCallback>());
466 peer_connection_->GetStats(callback);
467 EXPECT_TRUE_WAIT(callback->called(), kDefaultTimeout);
468 return callback->report();
deadbeef1dcb1642017-03-29 21:08:16 -0700469 }
470
471 int rendered_width() {
472 EXPECT_FALSE(fake_video_renderers_.empty());
473 return fake_video_renderers_.empty()
474 ? 0
475 : fake_video_renderers_.begin()->second->width();
476 }
477
478 int rendered_height() {
479 EXPECT_FALSE(fake_video_renderers_.empty());
480 return fake_video_renderers_.empty()
481 ? 0
482 : fake_video_renderers_.begin()->second->height();
483 }
484
485 double rendered_aspect_ratio() {
486 if (rendered_height() == 0) {
487 return 0.0;
488 }
489 return static_cast<double>(rendered_width()) / rendered_height();
490 }
491
492 webrtc::VideoRotation rendered_rotation() {
493 EXPECT_FALSE(fake_video_renderers_.empty());
494 return fake_video_renderers_.empty()
495 ? webrtc::kVideoRotation_0
496 : fake_video_renderers_.begin()->second->rotation();
497 }
498
499 int local_rendered_width() {
500 return local_video_renderer_ ? local_video_renderer_->width() : 0;
501 }
502
503 int local_rendered_height() {
504 return local_video_renderer_ ? local_video_renderer_->height() : 0;
505 }
506
507 double local_rendered_aspect_ratio() {
508 if (local_rendered_height() == 0) {
509 return 0.0;
510 }
511 return static_cast<double>(local_rendered_width()) /
512 local_rendered_height();
513 }
514
515 size_t number_of_remote_streams() {
516 if (!pc()) {
517 return 0;
518 }
519 return pc()->remote_streams()->count();
520 }
521
522 StreamCollectionInterface* remote_streams() const {
523 if (!pc()) {
524 ADD_FAILURE();
525 return nullptr;
526 }
527 return pc()->remote_streams();
528 }
529
530 StreamCollectionInterface* local_streams() {
531 if (!pc()) {
532 ADD_FAILURE();
533 return nullptr;
534 }
535 return pc()->local_streams();
536 }
537
538 webrtc::PeerConnectionInterface::SignalingState signaling_state() {
539 return pc()->signaling_state();
540 }
541
542 webrtc::PeerConnectionInterface::IceConnectionState ice_connection_state() {
543 return pc()->ice_connection_state();
544 }
545
546 webrtc::PeerConnectionInterface::IceGatheringState ice_gathering_state() {
547 return pc()->ice_gathering_state();
548 }
549
550 // Returns a MockRtpReceiverObserver for each RtpReceiver returned by
551 // GetReceivers. They're updated automatically when a remote offer/answer
552 // from the fake signaling channel is applied, or when
553 // ResetRtpReceiverObservers below is called.
554 const std::vector<std::unique_ptr<MockRtpReceiverObserver>>&
555 rtp_receiver_observers() {
556 return rtp_receiver_observers_;
557 }
558
559 void ResetRtpReceiverObservers() {
560 rtp_receiver_observers_.clear();
Mirko Bonadeic61ce0d2017-11-21 17:04:20 +0100561 for (const rtc::scoped_refptr<RtpReceiverInterface>& receiver :
562 pc()->GetReceivers()) {
deadbeef1dcb1642017-03-29 21:08:16 -0700563 std::unique_ptr<MockRtpReceiverObserver> observer(
564 new MockRtpReceiverObserver(receiver->media_type()));
565 receiver->SetObserver(observer.get());
566 rtp_receiver_observers_.push_back(std::move(observer));
567 }
568 }
569
Steve Antonede9ca52017-10-16 13:04:27 -0700570 rtc::FakeNetworkManager* network() const {
571 return fake_network_manager_.get();
572 }
573 cricket::PortAllocator* port_allocator() const { return port_allocator_; }
574
deadbeef1dcb1642017-03-29 21:08:16 -0700575 private:
576 explicit PeerConnectionWrapper(const std::string& debug_name)
577 : debug_name_(debug_name) {}
578
579 bool Init(
580 const MediaConstraintsInterface* constraints,
581 const PeerConnectionFactory::Options* options,
582 const PeerConnectionInterface::RTCConfiguration* config,
583 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator,
584 rtc::Thread* network_thread,
585 rtc::Thread* worker_thread) {
586 // There's an error in this test code if Init ends up being called twice.
587 RTC_DCHECK(!peer_connection_);
588 RTC_DCHECK(!peer_connection_factory_);
589
590 fake_network_manager_.reset(new rtc::FakeNetworkManager());
Steve Antonede9ca52017-10-16 13:04:27 -0700591 fake_network_manager_->AddInterface(kDefaultLocalAddress);
deadbeef1dcb1642017-03-29 21:08:16 -0700592
593 std::unique_ptr<cricket::PortAllocator> port_allocator(
594 new cricket::BasicPortAllocator(fake_network_manager_.get()));
Steve Antonede9ca52017-10-16 13:04:27 -0700595 port_allocator_ = port_allocator.get();
deadbeef1dcb1642017-03-29 21:08:16 -0700596 fake_audio_capture_module_ = FakeAudioCaptureModule::Create();
597 if (!fake_audio_capture_module_) {
598 return false;
599 }
600 // Note that these factories don't end up getting used unless supported
601 // codecs are added to them.
602 fake_video_decoder_factory_ = new FakeWebRtcVideoDecoderFactory();
603 fake_video_encoder_factory_ = new FakeWebRtcVideoEncoderFactory();
604 rtc::Thread* const signaling_thread = rtc::Thread::Current();
605 peer_connection_factory_ = webrtc::CreatePeerConnectionFactory(
606 network_thread, worker_thread, signaling_thread,
Karl Wiberg1b0eae32017-10-17 14:48:54 +0200607 fake_audio_capture_module_, webrtc::CreateBuiltinAudioEncoderFactory(),
608 webrtc::CreateBuiltinAudioDecoderFactory(), fake_video_encoder_factory_,
deadbeef1dcb1642017-03-29 21:08:16 -0700609 fake_video_decoder_factory_);
610 if (!peer_connection_factory_) {
611 return false;
612 }
613 if (options) {
614 peer_connection_factory_->SetOptions(*options);
615 }
Seth Hampson2f0d7022018-02-20 11:54:42 -0800616 if (config) {
617 sdp_semantics_ = config->sdp_semantics;
618 }
deadbeef1dcb1642017-03-29 21:08:16 -0700619 peer_connection_ =
620 CreatePeerConnection(std::move(port_allocator), constraints, config,
621 std::move(cert_generator));
622 return peer_connection_.get() != nullptr;
623 }
624
625 rtc::scoped_refptr<webrtc::PeerConnectionInterface> CreatePeerConnection(
626 std::unique_ptr<cricket::PortAllocator> port_allocator,
627 const MediaConstraintsInterface* constraints,
628 const PeerConnectionInterface::RTCConfiguration* config,
629 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator) {
630 PeerConnectionInterface::RTCConfiguration modified_config;
631 // If |config| is null, this will result in a default configuration being
632 // used.
633 if (config) {
634 modified_config = *config;
635 }
636 // Disable resolution adaptation; we don't want it interfering with the
637 // test results.
638 // TODO(deadbeef): Do something more robust. Since we're testing for aspect
639 // ratios and not specific resolutions, is this even necessary?
640 modified_config.set_cpu_adaptation(false);
641
642 return peer_connection_factory_->CreatePeerConnection(
643 modified_config, constraints, std::move(port_allocator),
644 std::move(cert_generator), this);
645 }
646
647 void set_signaling_message_receiver(
648 SignalingMessageReceiver* signaling_message_receiver) {
649 signaling_message_receiver_ = signaling_message_receiver;
650 }
651
652 void set_signaling_delay_ms(int delay_ms) { signaling_delay_ms_ = delay_ms; }
653
Steve Antonede9ca52017-10-16 13:04:27 -0700654 void set_signal_ice_candidates(bool signal) {
655 signal_ice_candidates_ = signal;
656 }
657
deadbeef1dcb1642017-03-29 21:08:16 -0700658 void EnableVideoDecoderFactory() {
659 video_decoder_factory_enabled_ = true;
660 fake_video_decoder_factory_->AddSupportedVideoCodecType(
661 webrtc::kVideoCodecVP8);
662 }
663
664 rtc::scoped_refptr<webrtc::VideoTrackInterface> CreateLocalVideoTrackInternal(
deadbeef1dcb1642017-03-29 21:08:16 -0700665 const FakeConstraints& constraints,
666 webrtc::VideoRotation rotation) {
667 // Set max frame rate to 10fps to reduce the risk of test flakiness.
668 // TODO(deadbeef): Do something more robust.
669 FakeConstraints source_constraints = constraints;
670 source_constraints.SetMandatoryMaxFrameRate(10);
671
672 cricket::FakeVideoCapturer* fake_capturer =
673 new webrtc::FakePeriodicVideoCapturer();
674 fake_capturer->SetRotation(rotation);
675 video_capturers_.push_back(fake_capturer);
676 rtc::scoped_refptr<webrtc::VideoTrackSourceInterface> source =
677 peer_connection_factory_->CreateVideoSource(fake_capturer,
678 &source_constraints);
679 rtc::scoped_refptr<webrtc::VideoTrackInterface> track(
deadbeefb1a15d72017-09-07 14:12:05 -0700680 peer_connection_factory_->CreateVideoTrack(rtc::CreateRandomUuid(),
681 source));
deadbeef1dcb1642017-03-29 21:08:16 -0700682 if (!local_video_renderer_) {
683 local_video_renderer_.reset(new webrtc::FakeVideoTrackRenderer(track));
684 }
685 return track;
686 }
687
688 void HandleIncomingOffer(const std::string& msg) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100689 RTC_LOG(LS_INFO) << debug_name_ << ": HandleIncomingOffer";
Steve Antona3a92c22017-12-07 10:27:41 -0800690 std::unique_ptr<SessionDescriptionInterface> desc =
691 webrtc::CreateSessionDescription(SdpType::kOffer, msg);
deadbeef1dcb1642017-03-29 21:08:16 -0700692 if (received_sdp_munger_) {
693 received_sdp_munger_(desc->description());
694 }
695
696 EXPECT_TRUE(SetRemoteDescription(std::move(desc)));
697 // Setting a remote description may have changed the number of receivers,
698 // so reset the receiver observers.
699 ResetRtpReceiverObservers();
Seth Hampson2f0d7022018-02-20 11:54:42 -0800700 if (remote_offer_handler_) {
701 remote_offer_handler_();
702 }
deadbeef1dcb1642017-03-29 21:08:16 -0700703 auto answer = CreateAnswer();
704 ASSERT_NE(nullptr, answer);
705 EXPECT_TRUE(SetLocalDescriptionAndSendSdpMessage(std::move(answer)));
706 }
707
708 void HandleIncomingAnswer(const std::string& msg) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100709 RTC_LOG(LS_INFO) << debug_name_ << ": HandleIncomingAnswer";
Steve Antona3a92c22017-12-07 10:27:41 -0800710 std::unique_ptr<SessionDescriptionInterface> desc =
711 webrtc::CreateSessionDescription(SdpType::kAnswer, msg);
deadbeef1dcb1642017-03-29 21:08:16 -0700712 if (received_sdp_munger_) {
713 received_sdp_munger_(desc->description());
714 }
715
716 EXPECT_TRUE(SetRemoteDescription(std::move(desc)));
717 // Set the RtpReceiverObserver after receivers are created.
718 ResetRtpReceiverObservers();
719 }
720
721 // Returns null on failure.
722 std::unique_ptr<SessionDescriptionInterface> CreateOffer() {
723 rtc::scoped_refptr<MockCreateSessionDescriptionObserver> observer(
724 new rtc::RefCountedObject<MockCreateSessionDescriptionObserver>());
725 pc()->CreateOffer(observer, offer_answer_options_);
726 return WaitForDescriptionFromObserver(observer);
727 }
728
729 // Returns null on failure.
730 std::unique_ptr<SessionDescriptionInterface> CreateAnswer() {
731 rtc::scoped_refptr<MockCreateSessionDescriptionObserver> observer(
732 new rtc::RefCountedObject<MockCreateSessionDescriptionObserver>());
733 pc()->CreateAnswer(observer, offer_answer_options_);
734 return WaitForDescriptionFromObserver(observer);
735 }
736
737 std::unique_ptr<SessionDescriptionInterface> WaitForDescriptionFromObserver(
Mirko Bonadeic61ce0d2017-11-21 17:04:20 +0100738 MockCreateSessionDescriptionObserver* observer) {
deadbeef1dcb1642017-03-29 21:08:16 -0700739 EXPECT_EQ_WAIT(true, observer->called(), kDefaultTimeout);
740 if (!observer->result()) {
741 return nullptr;
742 }
743 auto description = observer->MoveDescription();
744 if (generated_sdp_munger_) {
745 generated_sdp_munger_(description->description());
746 }
747 return description;
748 }
749
750 // Setting the local description and sending the SDP message over the fake
751 // signaling channel are combined into the same method because the SDP
752 // message needs to be sent as soon as SetLocalDescription finishes, without
753 // waiting for the observer to be called. This ensures that ICE candidates
754 // don't outrace the description.
755 bool SetLocalDescriptionAndSendSdpMessage(
756 std::unique_ptr<SessionDescriptionInterface> desc) {
757 rtc::scoped_refptr<MockSetSessionDescriptionObserver> observer(
758 new rtc::RefCountedObject<MockSetSessionDescriptionObserver>());
Mirko Bonadei675513b2017-11-09 11:09:25 +0100759 RTC_LOG(LS_INFO) << debug_name_ << ": SetLocalDescriptionAndSendSdpMessage";
Steve Antona3a92c22017-12-07 10:27:41 -0800760 SdpType type = desc->GetType();
deadbeef1dcb1642017-03-29 21:08:16 -0700761 std::string sdp;
762 EXPECT_TRUE(desc->ToString(&sdp));
763 pc()->SetLocalDescription(observer, desc.release());
Seth Hampson2f0d7022018-02-20 11:54:42 -0800764 if (sdp_semantics_ == SdpSemantics::kUnifiedPlan) {
765 RemoveUnusedVideoRenderers();
766 }
deadbeef1dcb1642017-03-29 21:08:16 -0700767 // As mentioned above, we need to send the message immediately after
768 // SetLocalDescription.
769 SendSdpMessage(type, sdp);
770 EXPECT_TRUE_WAIT(observer->called(), kDefaultTimeout);
771 return true;
772 }
773
774 bool SetRemoteDescription(std::unique_ptr<SessionDescriptionInterface> desc) {
775 rtc::scoped_refptr<MockSetSessionDescriptionObserver> observer(
776 new rtc::RefCountedObject<MockSetSessionDescriptionObserver>());
Mirko Bonadei675513b2017-11-09 11:09:25 +0100777 RTC_LOG(LS_INFO) << debug_name_ << ": SetRemoteDescription";
deadbeef1dcb1642017-03-29 21:08:16 -0700778 pc()->SetRemoteDescription(observer, desc.release());
Seth Hampson2f0d7022018-02-20 11:54:42 -0800779 if (sdp_semantics_ == SdpSemantics::kUnifiedPlan) {
780 RemoveUnusedVideoRenderers();
781 }
deadbeef1dcb1642017-03-29 21:08:16 -0700782 EXPECT_TRUE_WAIT(observer->called(), kDefaultTimeout);
783 return observer->result();
784 }
785
Seth Hampson2f0d7022018-02-20 11:54:42 -0800786 // This is a work around to remove unused fake_video_renderers from
787 // transceivers that have either stopped or are no longer receiving.
788 void RemoveUnusedVideoRenderers() {
789 auto transceivers = pc()->GetTransceivers();
790 for (auto& transceiver : transceivers) {
791 if (transceiver->receiver()->media_type() != cricket::MEDIA_TYPE_VIDEO) {
792 continue;
793 }
794 // Remove fake video renderers from any stopped transceivers.
795 if (transceiver->stopped()) {
796 auto it =
797 fake_video_renderers_.find(transceiver->receiver()->track()->id());
798 if (it != fake_video_renderers_.end()) {
799 fake_video_renderers_.erase(it);
800 }
801 }
802 // Remove fake video renderers from any transceivers that are no longer
803 // receiving.
804 if ((transceiver->current_direction() &&
805 !webrtc::RtpTransceiverDirectionHasRecv(
806 *transceiver->current_direction()))) {
807 auto it =
808 fake_video_renderers_.find(transceiver->receiver()->track()->id());
809 if (it != fake_video_renderers_.end()) {
810 fake_video_renderers_.erase(it);
811 }
812 }
813 }
814 }
815
deadbeef1dcb1642017-03-29 21:08:16 -0700816 // Simulate sending a blob of SDP with delay |signaling_delay_ms_| (0 by
817 // default).
Steve Antona3a92c22017-12-07 10:27:41 -0800818 void SendSdpMessage(SdpType type, const std::string& msg) {
deadbeef1dcb1642017-03-29 21:08:16 -0700819 if (signaling_delay_ms_ == 0) {
820 RelaySdpMessageIfReceiverExists(type, msg);
821 } else {
822 invoker_.AsyncInvokeDelayed<void>(
823 RTC_FROM_HERE, rtc::Thread::Current(),
824 rtc::Bind(&PeerConnectionWrapper::RelaySdpMessageIfReceiverExists,
825 this, type, msg),
826 signaling_delay_ms_);
827 }
828 }
829
Steve Antona3a92c22017-12-07 10:27:41 -0800830 void RelaySdpMessageIfReceiverExists(SdpType type, const std::string& msg) {
deadbeef1dcb1642017-03-29 21:08:16 -0700831 if (signaling_message_receiver_) {
832 signaling_message_receiver_->ReceiveSdpMessage(type, msg);
833 }
834 }
835
836 // Simulate trickling an ICE candidate with delay |signaling_delay_ms_| (0 by
837 // default).
838 void SendIceMessage(const std::string& sdp_mid,
839 int sdp_mline_index,
840 const std::string& msg) {
841 if (signaling_delay_ms_ == 0) {
842 RelayIceMessageIfReceiverExists(sdp_mid, sdp_mline_index, msg);
843 } else {
844 invoker_.AsyncInvokeDelayed<void>(
845 RTC_FROM_HERE, rtc::Thread::Current(),
846 rtc::Bind(&PeerConnectionWrapper::RelayIceMessageIfReceiverExists,
847 this, sdp_mid, sdp_mline_index, msg),
848 signaling_delay_ms_);
849 }
850 }
851
852 void RelayIceMessageIfReceiverExists(const std::string& sdp_mid,
853 int sdp_mline_index,
854 const std::string& msg) {
855 if (signaling_message_receiver_) {
856 signaling_message_receiver_->ReceiveIceMessage(sdp_mid, sdp_mline_index,
857 msg);
858 }
859 }
860
861 // SignalingMessageReceiver callbacks.
Steve Antona3a92c22017-12-07 10:27:41 -0800862 void ReceiveSdpMessage(SdpType type, const std::string& msg) override {
863 if (type == SdpType::kOffer) {
deadbeef1dcb1642017-03-29 21:08:16 -0700864 HandleIncomingOffer(msg);
865 } else {
866 HandleIncomingAnswer(msg);
867 }
868 }
869
870 void ReceiveIceMessage(const std::string& sdp_mid,
871 int sdp_mline_index,
872 const std::string& msg) override {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100873 RTC_LOG(LS_INFO) << debug_name_ << ": ReceiveIceMessage";
deadbeef1dcb1642017-03-29 21:08:16 -0700874 std::unique_ptr<webrtc::IceCandidateInterface> candidate(
875 webrtc::CreateIceCandidate(sdp_mid, sdp_mline_index, msg, nullptr));
876 EXPECT_TRUE(pc()->AddIceCandidate(candidate.get()));
877 }
878
879 // PeerConnectionObserver callbacks.
880 void OnSignalingChange(
881 webrtc::PeerConnectionInterface::SignalingState new_state) override {
882 EXPECT_EQ(pc()->signaling_state(), new_state);
883 }
Steve Anton15324772018-01-16 10:26:49 -0800884 void OnAddTrack(rtc::scoped_refptr<RtpReceiverInterface> receiver,
885 const std::vector<rtc::scoped_refptr<MediaStreamInterface>>&
886 streams) override {
887 if (receiver->media_type() == cricket::MEDIA_TYPE_VIDEO) {
888 rtc::scoped_refptr<VideoTrackInterface> video_track(
889 static_cast<VideoTrackInterface*>(receiver->track().get()));
890 ASSERT_TRUE(fake_video_renderers_.find(video_track->id()) ==
deadbeef1dcb1642017-03-29 21:08:16 -0700891 fake_video_renderers_.end());
Steve Anton15324772018-01-16 10:26:49 -0800892 fake_video_renderers_[video_track->id()] =
893 rtc::MakeUnique<FakeVideoTrackRenderer>(video_track);
deadbeef1dcb1642017-03-29 21:08:16 -0700894 }
895 }
Steve Anton15324772018-01-16 10:26:49 -0800896 void OnRemoveTrack(
897 rtc::scoped_refptr<RtpReceiverInterface> receiver) override {
898 if (receiver->media_type() == cricket::MEDIA_TYPE_VIDEO) {
899 auto it = fake_video_renderers_.find(receiver->track()->id());
900 RTC_DCHECK(it != fake_video_renderers_.end());
901 fake_video_renderers_.erase(it);
902 }
903 }
deadbeef1dcb1642017-03-29 21:08:16 -0700904 void OnRenegotiationNeeded() override {}
905 void OnIceConnectionChange(
906 webrtc::PeerConnectionInterface::IceConnectionState new_state) override {
907 EXPECT_EQ(pc()->ice_connection_state(), new_state);
Steve Antonede9ca52017-10-16 13:04:27 -0700908 ice_connection_state_history_.push_back(new_state);
deadbeef1dcb1642017-03-29 21:08:16 -0700909 }
910 void OnIceGatheringChange(
911 webrtc::PeerConnectionInterface::IceGatheringState new_state) override {
deadbeef1dcb1642017-03-29 21:08:16 -0700912 EXPECT_EQ(pc()->ice_gathering_state(), new_state);
Steve Antonede9ca52017-10-16 13:04:27 -0700913 ice_gathering_state_history_.push_back(new_state);
deadbeef1dcb1642017-03-29 21:08:16 -0700914 }
915 void OnIceCandidate(const webrtc::IceCandidateInterface* candidate) override {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100916 RTC_LOG(LS_INFO) << debug_name_ << ": OnIceCandidate";
deadbeef1dcb1642017-03-29 21:08:16 -0700917
918 std::string ice_sdp;
919 EXPECT_TRUE(candidate->ToString(&ice_sdp));
Steve Antonede9ca52017-10-16 13:04:27 -0700920 if (signaling_message_receiver_ == nullptr || !signal_ice_candidates_) {
deadbeef1dcb1642017-03-29 21:08:16 -0700921 // Remote party may be deleted.
922 return;
923 }
924 SendIceMessage(candidate->sdp_mid(), candidate->sdp_mline_index(), ice_sdp);
925 }
926 void OnDataChannel(
927 rtc::scoped_refptr<DataChannelInterface> data_channel) override {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100928 RTC_LOG(LS_INFO) << debug_name_ << ": OnDataChannel";
deadbeef1dcb1642017-03-29 21:08:16 -0700929 data_channel_ = data_channel;
930 data_observer_.reset(new MockDataChannelObserver(data_channel));
931 }
932
deadbeef1dcb1642017-03-29 21:08:16 -0700933 std::string debug_name_;
934
935 std::unique_ptr<rtc::FakeNetworkManager> fake_network_manager_;
936
937 rtc::scoped_refptr<webrtc::PeerConnectionInterface> peer_connection_;
938 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface>
939 peer_connection_factory_;
940
Steve Antonede9ca52017-10-16 13:04:27 -0700941 cricket::PortAllocator* port_allocator_;
deadbeef1dcb1642017-03-29 21:08:16 -0700942 // Needed to keep track of number of frames sent.
943 rtc::scoped_refptr<FakeAudioCaptureModule> fake_audio_capture_module_;
944 // Needed to keep track of number of frames received.
945 std::map<std::string, std::unique_ptr<webrtc::FakeVideoTrackRenderer>>
946 fake_video_renderers_;
947 // Needed to ensure frames aren't received for removed tracks.
948 std::vector<std::unique_ptr<webrtc::FakeVideoTrackRenderer>>
949 removed_fake_video_renderers_;
950 // Needed to keep track of number of frames received when external decoder
951 // used.
952 FakeWebRtcVideoDecoderFactory* fake_video_decoder_factory_ = nullptr;
953 FakeWebRtcVideoEncoderFactory* fake_video_encoder_factory_ = nullptr;
954 bool video_decoder_factory_enabled_ = false;
955
956 // For remote peer communication.
957 SignalingMessageReceiver* signaling_message_receiver_ = nullptr;
958 int signaling_delay_ms_ = 0;
Steve Antonede9ca52017-10-16 13:04:27 -0700959 bool signal_ice_candidates_ = true;
deadbeef1dcb1642017-03-29 21:08:16 -0700960
961 // Store references to the video capturers we've created, so that we can stop
962 // them, if required.
963 std::vector<cricket::FakeVideoCapturer*> video_capturers_;
964 // |local_video_renderer_| attached to the first created local video track.
965 std::unique_ptr<webrtc::FakeVideoTrackRenderer> local_video_renderer_;
966
Seth Hampson2f0d7022018-02-20 11:54:42 -0800967 SdpSemantics sdp_semantics_;
deadbeef1dcb1642017-03-29 21:08:16 -0700968 PeerConnectionInterface::RTCOfferAnswerOptions offer_answer_options_;
969 std::function<void(cricket::SessionDescription*)> received_sdp_munger_;
970 std::function<void(cricket::SessionDescription*)> generated_sdp_munger_;
Seth Hampson2f0d7022018-02-20 11:54:42 -0800971 std::function<void()> remote_offer_handler_;
deadbeef1dcb1642017-03-29 21:08:16 -0700972
973 rtc::scoped_refptr<DataChannelInterface> data_channel_;
974 std::unique_ptr<MockDataChannelObserver> data_observer_;
975
976 std::vector<std::unique_ptr<MockRtpReceiverObserver>> rtp_receiver_observers_;
977
Steve Antonede9ca52017-10-16 13:04:27 -0700978 std::vector<PeerConnectionInterface::IceConnectionState>
979 ice_connection_state_history_;
980 std::vector<PeerConnectionInterface::IceGatheringState>
981 ice_gathering_state_history_;
deadbeef1dcb1642017-03-29 21:08:16 -0700982
983 rtc::AsyncInvoker invoker_;
984
Seth Hampson2f0d7022018-02-20 11:54:42 -0800985 friend class PeerConnectionIntegrationBaseTest;
deadbeef1dcb1642017-03-29 21:08:16 -0700986};
987
Elad Alon99c3fe52017-10-13 16:29:40 +0200988class MockRtcEventLogOutput : public webrtc::RtcEventLogOutput {
989 public:
990 virtual ~MockRtcEventLogOutput() = default;
991 MOCK_CONST_METHOD0(IsActive, bool());
992 MOCK_METHOD1(Write, bool(const std::string&));
993};
994
Seth Hampson2f0d7022018-02-20 11:54:42 -0800995// This helper object is used for both specifying how many audio/video frames
996// are expected to be received for a caller/callee. It provides helper functions
997// to specify these expectations. The object initially starts in a state of no
998// expectations.
999class MediaExpectations {
1000 public:
1001 enum ExpectFrames {
1002 kExpectSomeFrames,
1003 kExpectNoFrames,
1004 kNoExpectation,
1005 };
1006
1007 void ExpectBidirectionalAudioAndVideo() {
1008 ExpectBidirectionalAudio();
1009 ExpectBidirectionalVideo();
1010 }
1011
1012 void ExpectBidirectionalAudio() {
1013 CallerExpectsSomeAudio();
1014 CalleeExpectsSomeAudio();
1015 }
1016
1017 void ExpectNoAudio() {
1018 CallerExpectsNoAudio();
1019 CalleeExpectsNoAudio();
1020 }
1021
1022 void ExpectBidirectionalVideo() {
1023 CallerExpectsSomeVideo();
1024 CalleeExpectsSomeVideo();
1025 }
1026
1027 void ExpectNoVideo() {
1028 CallerExpectsNoVideo();
1029 CalleeExpectsNoVideo();
1030 }
1031
1032 void CallerExpectsSomeAudioAndVideo() {
1033 CallerExpectsSomeAudio();
1034 CallerExpectsSomeVideo();
1035 }
1036
1037 void CalleeExpectsSomeAudioAndVideo() {
1038 CalleeExpectsSomeAudio();
1039 CalleeExpectsSomeVideo();
1040 }
1041
1042 // Caller's audio functions.
1043 void CallerExpectsSomeAudio(
1044 int expected_audio_frames = kDefaultExpectedAudioFrameCount) {
1045 caller_audio_expectation_ = kExpectSomeFrames;
1046 caller_audio_frames_expected_ = expected_audio_frames;
1047 }
1048
1049 void CallerExpectsNoAudio() {
1050 caller_audio_expectation_ = kExpectNoFrames;
1051 caller_audio_frames_expected_ = 0;
1052 }
1053
1054 // Caller's video functions.
1055 void CallerExpectsSomeVideo(
1056 int expected_video_frames = kDefaultExpectedVideoFrameCount) {
1057 caller_video_expectation_ = kExpectSomeFrames;
1058 caller_video_frames_expected_ = expected_video_frames;
1059 }
1060
1061 void CallerExpectsNoVideo() {
1062 caller_video_expectation_ = kExpectNoFrames;
1063 caller_video_frames_expected_ = 0;
1064 }
1065
1066 // Callee's audio functions.
1067 void CalleeExpectsSomeAudio(
1068 int expected_audio_frames = kDefaultExpectedAudioFrameCount) {
1069 callee_audio_expectation_ = kExpectSomeFrames;
1070 callee_audio_frames_expected_ = expected_audio_frames;
1071 }
1072
1073 void CalleeExpectsNoAudio() {
1074 callee_audio_expectation_ = kExpectNoFrames;
1075 callee_audio_frames_expected_ = 0;
1076 }
1077
1078 // Callee's video functions.
1079 void CalleeExpectsSomeVideo(
1080 int expected_video_frames = kDefaultExpectedVideoFrameCount) {
1081 callee_video_expectation_ = kExpectSomeFrames;
1082 callee_video_frames_expected_ = expected_video_frames;
1083 }
1084
1085 void CalleeExpectsNoVideo() {
1086 callee_video_expectation_ = kExpectNoFrames;
1087 callee_video_frames_expected_ = 0;
1088 }
1089
1090 ExpectFrames caller_audio_expectation_ = kNoExpectation;
1091 ExpectFrames caller_video_expectation_ = kNoExpectation;
1092 ExpectFrames callee_audio_expectation_ = kNoExpectation;
1093 ExpectFrames callee_video_expectation_ = kNoExpectation;
1094 int caller_audio_frames_expected_ = 0;
1095 int caller_video_frames_expected_ = 0;
1096 int callee_audio_frames_expected_ = 0;
1097 int callee_video_frames_expected_ = 0;
1098};
1099
deadbeef1dcb1642017-03-29 21:08:16 -07001100// Tests two PeerConnections connecting to each other end-to-end, using a
1101// virtual network, fake A/V capture and fake encoder/decoders. The
1102// PeerConnections share the threads/socket servers, but use separate versions
1103// of everything else (including "PeerConnectionFactory"s).
Seth Hampson2f0d7022018-02-20 11:54:42 -08001104class PeerConnectionIntegrationBaseTest : public testing::Test {
deadbeef1dcb1642017-03-29 21:08:16 -07001105 public:
Seth Hampson2f0d7022018-02-20 11:54:42 -08001106 explicit PeerConnectionIntegrationBaseTest(SdpSemantics sdp_semantics)
1107 : sdp_semantics_(sdp_semantics),
1108 ss_(new rtc::VirtualSocketServer()),
Steve Antonede9ca52017-10-16 13:04:27 -07001109 fss_(new rtc::FirewallSocketServer(ss_.get())),
1110 network_thread_(new rtc::Thread(fss_.get())),
deadbeef1dcb1642017-03-29 21:08:16 -07001111 worker_thread_(rtc::Thread::Create()) {
Sebastian Jansson8a793a02018-03-13 15:21:48 +01001112 network_thread_->SetName("PCNetworkThread", this);
1113 worker_thread_->SetName("PCWorkerThread", this);
deadbeef1dcb1642017-03-29 21:08:16 -07001114 RTC_CHECK(network_thread_->Start());
1115 RTC_CHECK(worker_thread_->Start());
1116 }
1117
Seth Hampson2f0d7022018-02-20 11:54:42 -08001118 ~PeerConnectionIntegrationBaseTest() {
deadbeef1dcb1642017-03-29 21:08:16 -07001119 if (caller_) {
1120 caller_->set_signaling_message_receiver(nullptr);
1121 }
1122 if (callee_) {
1123 callee_->set_signaling_message_receiver(nullptr);
1124 }
1125 }
1126
1127 bool SignalingStateStable() {
1128 return caller_->SignalingStateStable() && callee_->SignalingStateStable();
1129 }
1130
deadbeef71452802017-05-07 17:21:01 -07001131 bool DtlsConnected() {
1132 // TODO(deadbeef): kIceConnectionConnected currently means both ICE and DTLS
1133 // are connected. This is an important distinction. Once we have separate
1134 // ICE and DTLS state, this check needs to use the DTLS state.
1135 return (callee()->ice_connection_state() ==
1136 webrtc::PeerConnectionInterface::kIceConnectionConnected ||
1137 callee()->ice_connection_state() ==
1138 webrtc::PeerConnectionInterface::kIceConnectionCompleted) &&
1139 (caller()->ice_connection_state() ==
1140 webrtc::PeerConnectionInterface::kIceConnectionConnected ||
1141 caller()->ice_connection_state() ==
1142 webrtc::PeerConnectionInterface::kIceConnectionCompleted);
1143 }
1144
Seth Hampson2f0d7022018-02-20 11:54:42 -08001145 std::unique_ptr<PeerConnectionWrapper> CreatePeerConnectionWrapper(
1146 const std::string& debug_name,
1147 const MediaConstraintsInterface* constraints,
1148 const PeerConnectionFactory::Options* options,
1149 const RTCConfiguration* config,
1150 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator) {
1151 RTCConfiguration modified_config;
1152 if (config) {
1153 modified_config = *config;
1154 }
Steve Anton3acffc32018-04-12 17:21:03 -07001155 modified_config.sdp_semantics = sdp_semantics_;
Seth Hampson2f0d7022018-02-20 11:54:42 -08001156 if (!cert_generator) {
1157 cert_generator = rtc::MakeUnique<FakeRTCCertificateGenerator>();
1158 }
1159 std::unique_ptr<PeerConnectionWrapper> client(
1160 new PeerConnectionWrapper(debug_name));
1161 if (!client->Init(constraints, options, &modified_config,
1162 std::move(cert_generator), network_thread_.get(),
1163 worker_thread_.get())) {
1164 return nullptr;
1165 }
1166 return client;
1167 }
1168
deadbeef1dcb1642017-03-29 21:08:16 -07001169 bool CreatePeerConnectionWrappers() {
1170 return CreatePeerConnectionWrappersWithConfig(
1171 PeerConnectionInterface::RTCConfiguration(),
1172 PeerConnectionInterface::RTCConfiguration());
1173 }
1174
Steve Anton3acffc32018-04-12 17:21:03 -07001175 bool CreatePeerConnectionWrappersWithSdpSemantics(
1176 SdpSemantics caller_semantics,
1177 SdpSemantics callee_semantics) {
1178 // Can't specify the sdp_semantics in the passed-in configuration since it
1179 // will be overwritten by CreatePeerConnectionWrapper with whatever is
1180 // stored in sdp_semantics_. So get around this by modifying the instance
1181 // variable before calling CreatePeerConnectionWrapper for the caller and
1182 // callee PeerConnections.
1183 SdpSemantics original_semantics = sdp_semantics_;
1184 sdp_semantics_ = caller_semantics;
1185 caller_ = CreatePeerConnectionWrapper("Caller", nullptr, nullptr, nullptr,
1186 nullptr);
1187 sdp_semantics_ = callee_semantics;
1188 callee_ = CreatePeerConnectionWrapper("Callee", nullptr, nullptr, nullptr,
1189 nullptr);
1190 sdp_semantics_ = original_semantics;
1191 return caller_ && callee_;
1192 }
1193
deadbeef1dcb1642017-03-29 21:08:16 -07001194 bool CreatePeerConnectionWrappersWithConstraints(
1195 MediaConstraintsInterface* caller_constraints,
1196 MediaConstraintsInterface* callee_constraints) {
Seth Hampson2f0d7022018-02-20 11:54:42 -08001197 caller_ = CreatePeerConnectionWrapper("Caller", caller_constraints, nullptr,
1198 nullptr, nullptr);
1199 callee_ = CreatePeerConnectionWrapper("Callee", callee_constraints, nullptr,
1200 nullptr, nullptr);
deadbeef1dcb1642017-03-29 21:08:16 -07001201 return caller_ && callee_;
1202 }
1203
1204 bool CreatePeerConnectionWrappersWithConfig(
1205 const PeerConnectionInterface::RTCConfiguration& caller_config,
1206 const PeerConnectionInterface::RTCConfiguration& callee_config) {
Seth Hampson2f0d7022018-02-20 11:54:42 -08001207 caller_ = CreatePeerConnectionWrapper("Caller", nullptr, nullptr,
1208 &caller_config, nullptr);
1209 callee_ = CreatePeerConnectionWrapper("Callee", nullptr, nullptr,
1210 &callee_config, nullptr);
deadbeef1dcb1642017-03-29 21:08:16 -07001211 return caller_ && callee_;
1212 }
1213
1214 bool CreatePeerConnectionWrappersWithOptions(
1215 const PeerConnectionFactory::Options& caller_options,
1216 const PeerConnectionFactory::Options& callee_options) {
Seth Hampson2f0d7022018-02-20 11:54:42 -08001217 caller_ = CreatePeerConnectionWrapper("Caller", nullptr, &caller_options,
1218 nullptr, nullptr);
1219 callee_ = CreatePeerConnectionWrapper("Callee", nullptr, &callee_options,
1220 nullptr, nullptr);
deadbeef1dcb1642017-03-29 21:08:16 -07001221 return caller_ && callee_;
1222 }
1223
Seth Hampson2f0d7022018-02-20 11:54:42 -08001224 std::unique_ptr<PeerConnectionWrapper>
1225 CreatePeerConnectionWrapperWithAlternateKey() {
deadbeef1dcb1642017-03-29 21:08:16 -07001226 std::unique_ptr<FakeRTCCertificateGenerator> cert_generator(
1227 new FakeRTCCertificateGenerator());
1228 cert_generator->use_alternate_key();
1229
Seth Hampson2f0d7022018-02-20 11:54:42 -08001230 return CreatePeerConnectionWrapper("New Peer", nullptr, nullptr, nullptr,
1231 std::move(cert_generator));
deadbeef1dcb1642017-03-29 21:08:16 -07001232 }
1233
1234 // Once called, SDP blobs and ICE candidates will be automatically signaled
1235 // between PeerConnections.
1236 void ConnectFakeSignaling() {
1237 caller_->set_signaling_message_receiver(callee_.get());
1238 callee_->set_signaling_message_receiver(caller_.get());
1239 }
1240
Steve Antonede9ca52017-10-16 13:04:27 -07001241 // Once called, SDP blobs will be automatically signaled between
1242 // PeerConnections. Note that ICE candidates will not be signaled unless they
1243 // are in the exchanged SDP blobs.
1244 void ConnectFakeSignalingForSdpOnly() {
1245 ConnectFakeSignaling();
1246 SetSignalIceCandidates(false);
1247 }
1248
deadbeef1dcb1642017-03-29 21:08:16 -07001249 void SetSignalingDelayMs(int delay_ms) {
1250 caller_->set_signaling_delay_ms(delay_ms);
1251 callee_->set_signaling_delay_ms(delay_ms);
1252 }
1253
Steve Antonede9ca52017-10-16 13:04:27 -07001254 void SetSignalIceCandidates(bool signal) {
1255 caller_->set_signal_ice_candidates(signal);
1256 callee_->set_signal_ice_candidates(signal);
1257 }
1258
deadbeef1dcb1642017-03-29 21:08:16 -07001259 void EnableVideoDecoderFactory() {
1260 caller_->EnableVideoDecoderFactory();
1261 callee_->EnableVideoDecoderFactory();
1262 }
1263
1264 // Messages may get lost on the unreliable DataChannel, so we send multiple
1265 // times to avoid test flakiness.
1266 void SendRtpDataWithRetries(webrtc::DataChannelInterface* dc,
1267 const std::string& data,
1268 int retries) {
1269 for (int i = 0; i < retries; ++i) {
1270 dc->Send(DataBuffer(data));
1271 }
1272 }
1273
1274 rtc::Thread* network_thread() { return network_thread_.get(); }
1275
1276 rtc::VirtualSocketServer* virtual_socket_server() { return ss_.get(); }
1277
1278 PeerConnectionWrapper* caller() { return caller_.get(); }
1279
1280 // Set the |caller_| to the |wrapper| passed in and return the
1281 // original |caller_|.
1282 PeerConnectionWrapper* SetCallerPcWrapperAndReturnCurrent(
1283 PeerConnectionWrapper* wrapper) {
1284 PeerConnectionWrapper* old = caller_.release();
1285 caller_.reset(wrapper);
1286 return old;
1287 }
1288
1289 PeerConnectionWrapper* callee() { return callee_.get(); }
1290
1291 // Set the |callee_| to the |wrapper| passed in and return the
1292 // original |callee_|.
1293 PeerConnectionWrapper* SetCalleePcWrapperAndReturnCurrent(
1294 PeerConnectionWrapper* wrapper) {
1295 PeerConnectionWrapper* old = callee_.release();
1296 callee_.reset(wrapper);
1297 return old;
1298 }
1299
Steve Antonede9ca52017-10-16 13:04:27 -07001300 rtc::FirewallSocketServer* firewall() const { return fss_.get(); }
1301
Seth Hampson2f0d7022018-02-20 11:54:42 -08001302 // Expects the provided number of new frames to be received within
1303 // kMaxWaitForFramesMs. The new expected frames are specified in
1304 // |media_expectations|. Returns false if any of the expectations were
1305 // not met.
1306 bool ExpectNewFrames(const MediaExpectations& media_expectations) {
1307 // First initialize the expected frame counts based upon the current
1308 // frame count.
1309 int total_caller_audio_frames_expected = caller()->audio_frames_received();
1310 if (media_expectations.caller_audio_expectation_ ==
1311 MediaExpectations::kExpectSomeFrames) {
1312 total_caller_audio_frames_expected +=
1313 media_expectations.caller_audio_frames_expected_;
1314 }
1315 int total_caller_video_frames_expected =
deadbeef1dcb1642017-03-29 21:08:16 -07001316 caller()->min_video_frames_received_per_track();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001317 if (media_expectations.caller_video_expectation_ ==
1318 MediaExpectations::kExpectSomeFrames) {
1319 total_caller_video_frames_expected +=
1320 media_expectations.caller_video_frames_expected_;
1321 }
1322 int total_callee_audio_frames_expected = callee()->audio_frames_received();
1323 if (media_expectations.callee_audio_expectation_ ==
1324 MediaExpectations::kExpectSomeFrames) {
1325 total_callee_audio_frames_expected +=
1326 media_expectations.callee_audio_frames_expected_;
1327 }
1328 int total_callee_video_frames_expected =
deadbeef1dcb1642017-03-29 21:08:16 -07001329 callee()->min_video_frames_received_per_track();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001330 if (media_expectations.callee_video_expectation_ ==
1331 MediaExpectations::kExpectSomeFrames) {
1332 total_callee_video_frames_expected +=
1333 media_expectations.callee_video_frames_expected_;
1334 }
deadbeef1dcb1642017-03-29 21:08:16 -07001335
Seth Hampson2f0d7022018-02-20 11:54:42 -08001336 // Wait for the expected frames.
deadbeef1dcb1642017-03-29 21:08:16 -07001337 EXPECT_TRUE_WAIT(caller()->audio_frames_received() >=
Seth Hampson2f0d7022018-02-20 11:54:42 -08001338 total_caller_audio_frames_expected &&
deadbeef1dcb1642017-03-29 21:08:16 -07001339 caller()->min_video_frames_received_per_track() >=
Seth Hampson2f0d7022018-02-20 11:54:42 -08001340 total_caller_video_frames_expected &&
deadbeef1dcb1642017-03-29 21:08:16 -07001341 callee()->audio_frames_received() >=
Seth Hampson2f0d7022018-02-20 11:54:42 -08001342 total_callee_audio_frames_expected &&
deadbeef1dcb1642017-03-29 21:08:16 -07001343 callee()->min_video_frames_received_per_track() >=
Seth Hampson2f0d7022018-02-20 11:54:42 -08001344 total_callee_video_frames_expected,
1345 kMaxWaitForFramesMs);
1346 bool expectations_correct =
1347 caller()->audio_frames_received() >=
1348 total_caller_audio_frames_expected &&
1349 caller()->min_video_frames_received_per_track() >=
1350 total_caller_video_frames_expected &&
1351 callee()->audio_frames_received() >=
1352 total_callee_audio_frames_expected &&
1353 callee()->min_video_frames_received_per_track() >=
1354 total_callee_video_frames_expected;
deadbeef1dcb1642017-03-29 21:08:16 -07001355
Seth Hampson2f0d7022018-02-20 11:54:42 -08001356 // After the combined wait, print out a more detailed message upon
1357 // failure.
deadbeef1dcb1642017-03-29 21:08:16 -07001358 EXPECT_GE(caller()->audio_frames_received(),
Seth Hampson2f0d7022018-02-20 11:54:42 -08001359 total_caller_audio_frames_expected);
deadbeef1dcb1642017-03-29 21:08:16 -07001360 EXPECT_GE(caller()->min_video_frames_received_per_track(),
Seth Hampson2f0d7022018-02-20 11:54:42 -08001361 total_caller_video_frames_expected);
deadbeef1dcb1642017-03-29 21:08:16 -07001362 EXPECT_GE(callee()->audio_frames_received(),
Seth Hampson2f0d7022018-02-20 11:54:42 -08001363 total_callee_audio_frames_expected);
deadbeef1dcb1642017-03-29 21:08:16 -07001364 EXPECT_GE(callee()->min_video_frames_received_per_track(),
Seth Hampson2f0d7022018-02-20 11:54:42 -08001365 total_callee_video_frames_expected);
1366
1367 // We want to make sure nothing unexpected was received.
1368 if (media_expectations.caller_audio_expectation_ ==
1369 MediaExpectations::kExpectNoFrames) {
1370 EXPECT_EQ(caller()->audio_frames_received(),
1371 total_caller_audio_frames_expected);
1372 if (caller()->audio_frames_received() !=
1373 total_caller_audio_frames_expected) {
1374 expectations_correct = false;
1375 }
1376 }
1377 if (media_expectations.caller_video_expectation_ ==
1378 MediaExpectations::kExpectNoFrames) {
1379 EXPECT_EQ(caller()->min_video_frames_received_per_track(),
1380 total_caller_video_frames_expected);
1381 if (caller()->min_video_frames_received_per_track() !=
1382 total_caller_video_frames_expected) {
1383 expectations_correct = false;
1384 }
1385 }
1386 if (media_expectations.callee_audio_expectation_ ==
1387 MediaExpectations::kExpectNoFrames) {
1388 EXPECT_EQ(callee()->audio_frames_received(),
1389 total_callee_audio_frames_expected);
1390 if (callee()->audio_frames_received() !=
1391 total_callee_audio_frames_expected) {
1392 expectations_correct = false;
1393 }
1394 }
1395 if (media_expectations.callee_video_expectation_ ==
1396 MediaExpectations::kExpectNoFrames) {
1397 EXPECT_EQ(callee()->min_video_frames_received_per_track(),
1398 total_callee_video_frames_expected);
1399 if (callee()->min_video_frames_received_per_track() !=
1400 total_callee_video_frames_expected) {
1401 expectations_correct = false;
1402 }
1403 }
1404 return expectations_correct;
deadbeef1dcb1642017-03-29 21:08:16 -07001405 }
1406
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07001407 void TestNegotiatedCipherSuite(
1408 const PeerConnectionFactory::Options& caller_options,
1409 const PeerConnectionFactory::Options& callee_options,
1410 int expected_cipher_suite) {
deadbeef1dcb1642017-03-29 21:08:16 -07001411 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(caller_options,
1412 callee_options));
1413 rtc::scoped_refptr<webrtc::FakeMetricsObserver> caller_observer =
1414 new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
1415 caller()->pc()->RegisterUMAObserver(caller_observer);
1416 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001417 caller()->AddAudioVideoTracks();
1418 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001419 caller()->CreateAndSetAndSignalOffer();
1420 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1421 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(expected_cipher_suite),
deadbeefd8ad7882017-04-18 16:01:17 -07001422 caller()->OldGetStats()->SrtpCipher(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07001423 EXPECT_EQ(
1424 1, caller_observer->GetEnumCounter(webrtc::kEnumCounterAudioSrtpCipher,
1425 expected_cipher_suite));
1426 caller()->pc()->RegisterUMAObserver(nullptr);
1427 }
1428
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07001429 void TestGcmNegotiationUsesCipherSuite(bool local_gcm_enabled,
1430 bool remote_gcm_enabled,
1431 int expected_cipher_suite) {
1432 PeerConnectionFactory::Options caller_options;
1433 caller_options.crypto_options.enable_gcm_crypto_suites = local_gcm_enabled;
1434 PeerConnectionFactory::Options callee_options;
1435 callee_options.crypto_options.enable_gcm_crypto_suites = remote_gcm_enabled;
1436 TestNegotiatedCipherSuite(caller_options, callee_options,
1437 expected_cipher_suite);
1438 }
1439
Seth Hampson2f0d7022018-02-20 11:54:42 -08001440 protected:
Steve Anton3acffc32018-04-12 17:21:03 -07001441 SdpSemantics sdp_semantics_;
Seth Hampson2f0d7022018-02-20 11:54:42 -08001442
deadbeef1dcb1642017-03-29 21:08:16 -07001443 private:
1444 // |ss_| is used by |network_thread_| so it must be destroyed later.
deadbeef1dcb1642017-03-29 21:08:16 -07001445 std::unique_ptr<rtc::VirtualSocketServer> ss_;
Steve Antonede9ca52017-10-16 13:04:27 -07001446 std::unique_ptr<rtc::FirewallSocketServer> fss_;
deadbeef1dcb1642017-03-29 21:08:16 -07001447 // |network_thread_| and |worker_thread_| are used by both
1448 // |caller_| and |callee_| so they must be destroyed
1449 // later.
1450 std::unique_ptr<rtc::Thread> network_thread_;
1451 std::unique_ptr<rtc::Thread> worker_thread_;
1452 std::unique_ptr<PeerConnectionWrapper> caller_;
1453 std::unique_ptr<PeerConnectionWrapper> callee_;
1454};
1455
Seth Hampson2f0d7022018-02-20 11:54:42 -08001456class PeerConnectionIntegrationTest
1457 : public PeerConnectionIntegrationBaseTest,
1458 public ::testing::WithParamInterface<SdpSemantics> {
1459 protected:
1460 PeerConnectionIntegrationTest()
1461 : PeerConnectionIntegrationBaseTest(GetParam()) {}
1462};
1463
1464class PeerConnectionIntegrationTestPlanB
1465 : public PeerConnectionIntegrationBaseTest {
1466 protected:
1467 PeerConnectionIntegrationTestPlanB()
1468 : PeerConnectionIntegrationBaseTest(SdpSemantics::kPlanB) {}
1469};
1470
1471class PeerConnectionIntegrationTestUnifiedPlan
1472 : public PeerConnectionIntegrationBaseTest {
1473 protected:
1474 PeerConnectionIntegrationTestUnifiedPlan()
1475 : PeerConnectionIntegrationBaseTest(SdpSemantics::kUnifiedPlan) {}
1476};
1477
deadbeef1dcb1642017-03-29 21:08:16 -07001478// Test the OnFirstPacketReceived callback from audio/video RtpReceivers. This
1479// includes testing that the callback is invoked if an observer is connected
1480// after the first packet has already been received.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001481TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07001482 RtpReceiverObserverOnFirstPacketReceived) {
1483 ASSERT_TRUE(CreatePeerConnectionWrappers());
1484 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001485 caller()->AddAudioVideoTracks();
1486 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001487 // Start offer/answer exchange and wait for it to complete.
1488 caller()->CreateAndSetAndSignalOffer();
1489 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1490 // Should be one receiver each for audio/video.
1491 EXPECT_EQ(2, caller()->rtp_receiver_observers().size());
1492 EXPECT_EQ(2, callee()->rtp_receiver_observers().size());
1493 // Wait for all "first packet received" callbacks to be fired.
1494 EXPECT_TRUE_WAIT(
1495 std::all_of(caller()->rtp_receiver_observers().begin(),
1496 caller()->rtp_receiver_observers().end(),
1497 [](const std::unique_ptr<MockRtpReceiverObserver>& o) {
1498 return o->first_packet_received();
1499 }),
1500 kMaxWaitForFramesMs);
1501 EXPECT_TRUE_WAIT(
1502 std::all_of(callee()->rtp_receiver_observers().begin(),
1503 callee()->rtp_receiver_observers().end(),
1504 [](const std::unique_ptr<MockRtpReceiverObserver>& o) {
1505 return o->first_packet_received();
1506 }),
1507 kMaxWaitForFramesMs);
1508 // If new observers are set after the first packet was already received, the
1509 // callback should still be invoked.
1510 caller()->ResetRtpReceiverObservers();
1511 callee()->ResetRtpReceiverObservers();
1512 EXPECT_EQ(2, caller()->rtp_receiver_observers().size());
1513 EXPECT_EQ(2, callee()->rtp_receiver_observers().size());
1514 EXPECT_TRUE(
1515 std::all_of(caller()->rtp_receiver_observers().begin(),
1516 caller()->rtp_receiver_observers().end(),
1517 [](const std::unique_ptr<MockRtpReceiverObserver>& o) {
1518 return o->first_packet_received();
1519 }));
1520 EXPECT_TRUE(
1521 std::all_of(callee()->rtp_receiver_observers().begin(),
1522 callee()->rtp_receiver_observers().end(),
1523 [](const std::unique_ptr<MockRtpReceiverObserver>& o) {
1524 return o->first_packet_received();
1525 }));
1526}
1527
1528class DummyDtmfObserver : public DtmfSenderObserverInterface {
1529 public:
1530 DummyDtmfObserver() : completed_(false) {}
1531
1532 // Implements DtmfSenderObserverInterface.
1533 void OnToneChange(const std::string& tone) override {
1534 tones_.push_back(tone);
1535 if (tone.empty()) {
1536 completed_ = true;
1537 }
1538 }
1539
1540 const std::vector<std::string>& tones() const { return tones_; }
1541 bool completed() const { return completed_; }
1542
1543 private:
1544 bool completed_;
1545 std::vector<std::string> tones_;
1546};
1547
1548// Assumes |sender| already has an audio track added and the offer/answer
1549// exchange is done.
1550void TestDtmfFromSenderToReceiver(PeerConnectionWrapper* sender,
1551 PeerConnectionWrapper* receiver) {
Steve Anton15324772018-01-16 10:26:49 -08001552 // We should be able to get a DTMF sender from the local sender.
1553 rtc::scoped_refptr<DtmfSenderInterface> dtmf_sender =
1554 sender->pc()->GetSenders().at(0)->GetDtmfSender();
1555 ASSERT_TRUE(dtmf_sender);
deadbeef1dcb1642017-03-29 21:08:16 -07001556 DummyDtmfObserver observer;
deadbeef1dcb1642017-03-29 21:08:16 -07001557 dtmf_sender->RegisterObserver(&observer);
1558
1559 // Test the DtmfSender object just created.
1560 EXPECT_TRUE(dtmf_sender->CanInsertDtmf());
1561 EXPECT_TRUE(dtmf_sender->InsertDtmf("1a", 100, 50));
1562
1563 EXPECT_TRUE_WAIT(observer.completed(), kDefaultTimeout);
1564 std::vector<std::string> tones = {"1", "a", ""};
1565 EXPECT_EQ(tones, observer.tones());
1566 dtmf_sender->UnregisterObserver();
1567 // TODO(deadbeef): Verify the tones were actually received end-to-end.
1568}
1569
1570// Verifies the DtmfSenderObserver callbacks for a DtmfSender (one in each
1571// direction).
Seth Hampson2f0d7022018-02-20 11:54:42 -08001572TEST_P(PeerConnectionIntegrationTest, DtmfSenderObserver) {
deadbeef1dcb1642017-03-29 21:08:16 -07001573 ASSERT_TRUE(CreatePeerConnectionWrappers());
1574 ConnectFakeSignaling();
1575 // Only need audio for DTMF.
Steve Anton15324772018-01-16 10:26:49 -08001576 caller()->AddAudioTrack();
1577 callee()->AddAudioTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07001578 caller()->CreateAndSetAndSignalOffer();
1579 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
deadbeef71452802017-05-07 17:21:01 -07001580 // DTLS must finish before the DTMF sender can be used reliably.
1581 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07001582 TestDtmfFromSenderToReceiver(caller(), callee());
1583 TestDtmfFromSenderToReceiver(callee(), caller());
1584}
1585
1586// Basic end-to-end test, verifying media can be encoded/transmitted/decoded
1587// between two connections, using DTLS-SRTP.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001588TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithDtls) {
deadbeef1dcb1642017-03-29 21:08:16 -07001589 ASSERT_TRUE(CreatePeerConnectionWrappers());
1590 ConnectFakeSignaling();
Harald Alvestrand194939b2018-01-24 16:04:13 +01001591 rtc::scoped_refptr<webrtc::FakeMetricsObserver> caller_observer =
1592 new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
1593 caller()->pc()->RegisterUMAObserver(caller_observer);
1594
deadbeef1dcb1642017-03-29 21:08:16 -07001595 // Do normal offer/answer and wait for some frames to be received in each
1596 // direction.
Steve Anton15324772018-01-16 10:26:49 -08001597 caller()->AddAudioVideoTracks();
1598 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001599 caller()->CreateAndSetAndSignalOffer();
1600 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001601 MediaExpectations media_expectations;
1602 media_expectations.ExpectBidirectionalAudioAndVideo();
1603 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Harald Alvestrand194939b2018-01-24 16:04:13 +01001604 EXPECT_LE(
1605 1, caller_observer->GetEnumCounter(webrtc::kEnumCounterKeyProtocol,
1606 webrtc::kEnumCounterKeyProtocolDtls));
1607 EXPECT_EQ(
1608 0, caller_observer->GetEnumCounter(webrtc::kEnumCounterKeyProtocol,
1609 webrtc::kEnumCounterKeyProtocolSdes));
deadbeef1dcb1642017-03-29 21:08:16 -07001610}
1611
1612// Uses SDES instead of DTLS for key agreement.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001613TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithSdes) {
deadbeef1dcb1642017-03-29 21:08:16 -07001614 PeerConnectionInterface::RTCConfiguration sdes_config;
1615 sdes_config.enable_dtls_srtp.emplace(false);
1616 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(sdes_config, sdes_config));
1617 ConnectFakeSignaling();
Harald Alvestrand194939b2018-01-24 16:04:13 +01001618 rtc::scoped_refptr<webrtc::FakeMetricsObserver> caller_observer =
1619 new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
1620 caller()->pc()->RegisterUMAObserver(caller_observer);
deadbeef1dcb1642017-03-29 21:08:16 -07001621
1622 // Do normal offer/answer and wait for some frames to be received in each
1623 // direction.
Steve Anton15324772018-01-16 10:26:49 -08001624 caller()->AddAudioVideoTracks();
1625 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001626 caller()->CreateAndSetAndSignalOffer();
1627 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001628 MediaExpectations media_expectations;
1629 media_expectations.ExpectBidirectionalAudioAndVideo();
1630 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Harald Alvestrand194939b2018-01-24 16:04:13 +01001631 EXPECT_LE(
1632 1, caller_observer->GetEnumCounter(webrtc::kEnumCounterKeyProtocol,
1633 webrtc::kEnumCounterKeyProtocolSdes));
1634 EXPECT_EQ(
1635 0, caller_observer->GetEnumCounter(webrtc::kEnumCounterKeyProtocol,
1636 webrtc::kEnumCounterKeyProtocolDtls));
deadbeef1dcb1642017-03-29 21:08:16 -07001637}
1638
Steve Anton8c0f7a72017-10-03 10:03:10 -07001639// Tests that the GetRemoteAudioSSLCertificate method returns the remote DTLS
1640// certificate once the DTLS handshake has finished.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001641TEST_P(PeerConnectionIntegrationTest,
Steve Anton8c0f7a72017-10-03 10:03:10 -07001642 GetRemoteAudioSSLCertificateReturnsExchangedCertificate) {
1643 auto GetRemoteAudioSSLCertificate = [](PeerConnectionWrapper* wrapper) {
1644 auto pci = reinterpret_cast<PeerConnectionProxy*>(wrapper->pc());
1645 auto pc = reinterpret_cast<PeerConnection*>(pci->internal());
1646 return pc->GetRemoteAudioSSLCertificate();
1647 };
Zhi Huang70b820f2018-01-27 14:16:15 -08001648 auto GetRemoteAudioSSLCertChain = [](PeerConnectionWrapper* wrapper) {
1649 auto pci = reinterpret_cast<PeerConnectionProxy*>(wrapper->pc());
1650 auto pc = reinterpret_cast<PeerConnection*>(pci->internal());
1651 return pc->GetRemoteAudioSSLCertChain();
1652 };
Steve Anton8c0f7a72017-10-03 10:03:10 -07001653
1654 auto caller_cert = rtc::RTCCertificate::FromPEM(kRsaPems[0]);
1655 auto callee_cert = rtc::RTCCertificate::FromPEM(kRsaPems[1]);
1656
1657 // Configure each side with a known certificate so they can be compared later.
1658 PeerConnectionInterface::RTCConfiguration caller_config;
1659 caller_config.enable_dtls_srtp.emplace(true);
1660 caller_config.certificates.push_back(caller_cert);
1661 PeerConnectionInterface::RTCConfiguration callee_config;
1662 callee_config.enable_dtls_srtp.emplace(true);
1663 callee_config.certificates.push_back(callee_cert);
1664 ASSERT_TRUE(
1665 CreatePeerConnectionWrappersWithConfig(caller_config, callee_config));
1666 ConnectFakeSignaling();
1667
1668 // When first initialized, there should not be a remote SSL certificate (and
1669 // calling this method should not crash).
1670 EXPECT_EQ(nullptr, GetRemoteAudioSSLCertificate(caller()));
1671 EXPECT_EQ(nullptr, GetRemoteAudioSSLCertificate(callee()));
Zhi Huang70b820f2018-01-27 14:16:15 -08001672 EXPECT_EQ(nullptr, GetRemoteAudioSSLCertChain(caller()));
1673 EXPECT_EQ(nullptr, GetRemoteAudioSSLCertChain(callee()));
Steve Anton8c0f7a72017-10-03 10:03:10 -07001674
Steve Anton15324772018-01-16 10:26:49 -08001675 caller()->AddAudioTrack();
1676 callee()->AddAudioTrack();
Steve Anton8c0f7a72017-10-03 10:03:10 -07001677 caller()->CreateAndSetAndSignalOffer();
1678 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1679 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
1680
1681 // Once DTLS has been connected, each side should return the other's SSL
1682 // certificate when calling GetRemoteAudioSSLCertificate.
1683
1684 auto caller_remote_cert = GetRemoteAudioSSLCertificate(caller());
1685 ASSERT_TRUE(caller_remote_cert);
1686 EXPECT_EQ(callee_cert->ssl_certificate().ToPEMString(),
1687 caller_remote_cert->ToPEMString());
1688
1689 auto callee_remote_cert = GetRemoteAudioSSLCertificate(callee());
1690 ASSERT_TRUE(callee_remote_cert);
1691 EXPECT_EQ(caller_cert->ssl_certificate().ToPEMString(),
1692 callee_remote_cert->ToPEMString());
Zhi Huang70b820f2018-01-27 14:16:15 -08001693
1694 auto caller_remote_cert_chain = GetRemoteAudioSSLCertChain(caller());
1695 ASSERT_TRUE(caller_remote_cert_chain);
1696 ASSERT_EQ(1U, caller_remote_cert_chain->GetSize());
1697 auto remote_cert = &caller_remote_cert_chain->Get(0);
1698 EXPECT_EQ(callee_cert->ssl_certificate().ToPEMString(),
1699 remote_cert->ToPEMString());
1700
1701 auto callee_remote_cert_chain = GetRemoteAudioSSLCertChain(callee());
1702 ASSERT_TRUE(callee_remote_cert_chain);
1703 ASSERT_EQ(1U, callee_remote_cert_chain->GetSize());
1704 remote_cert = &callee_remote_cert_chain->Get(0);
1705 EXPECT_EQ(caller_cert->ssl_certificate().ToPEMString(),
1706 remote_cert->ToPEMString());
Steve Anton8c0f7a72017-10-03 10:03:10 -07001707}
1708
deadbeef1dcb1642017-03-29 21:08:16 -07001709// This test sets up a call between two parties (using DTLS) and tests that we
1710// can get a video aspect ratio of 16:9.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001711TEST_P(PeerConnectionIntegrationTest, SendAndReceive16To9AspectRatio) {
deadbeef1dcb1642017-03-29 21:08:16 -07001712 ASSERT_TRUE(CreatePeerConnectionWrappers());
1713 ConnectFakeSignaling();
1714
1715 // Add video tracks with 16:9 constraint.
1716 FakeConstraints constraints;
1717 double requested_ratio = 16.0 / 9;
1718 constraints.SetMandatoryMinAspectRatio(requested_ratio);
Steve Anton15324772018-01-16 10:26:49 -08001719 caller()->AddTrack(
1720 caller()->CreateLocalVideoTrackWithConstraints(constraints));
1721 callee()->AddTrack(
1722 callee()->CreateLocalVideoTrackWithConstraints(constraints));
deadbeef1dcb1642017-03-29 21:08:16 -07001723
1724 // Do normal offer/answer and wait for at least one frame to be received in
1725 // each direction.
1726 caller()->CreateAndSetAndSignalOffer();
1727 ASSERT_TRUE_WAIT(caller()->min_video_frames_received_per_track() > 0 &&
1728 callee()->min_video_frames_received_per_track() > 0,
1729 kMaxWaitForFramesMs);
1730
1731 // Check rendered aspect ratio.
1732 EXPECT_EQ(requested_ratio, caller()->local_rendered_aspect_ratio());
1733 EXPECT_EQ(requested_ratio, caller()->rendered_aspect_ratio());
1734 EXPECT_EQ(requested_ratio, callee()->local_rendered_aspect_ratio());
1735 EXPECT_EQ(requested_ratio, callee()->rendered_aspect_ratio());
1736}
1737
1738// This test sets up a call between two parties with a source resolution of
1739// 1280x720 and verifies that a 16:9 aspect ratio is received.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001740TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07001741 Send1280By720ResolutionAndReceive16To9AspectRatio) {
1742 ASSERT_TRUE(CreatePeerConnectionWrappers());
1743 ConnectFakeSignaling();
1744
1745 // Similar to above test, but uses MandatoryMin[Width/Height] constraint
1746 // instead of aspect ratio constraint.
1747 FakeConstraints constraints;
1748 constraints.SetMandatoryMinWidth(1280);
1749 constraints.SetMandatoryMinHeight(720);
Steve Anton15324772018-01-16 10:26:49 -08001750 caller()->AddTrack(
1751 caller()->CreateLocalVideoTrackWithConstraints(constraints));
1752 callee()->AddTrack(
1753 callee()->CreateLocalVideoTrackWithConstraints(constraints));
deadbeef1dcb1642017-03-29 21:08:16 -07001754
1755 // Do normal offer/answer and wait for at least one frame to be received in
1756 // each direction.
1757 caller()->CreateAndSetAndSignalOffer();
1758 ASSERT_TRUE_WAIT(caller()->min_video_frames_received_per_track() > 0 &&
1759 callee()->min_video_frames_received_per_track() > 0,
1760 kMaxWaitForFramesMs);
1761
1762 // Check rendered aspect ratio.
1763 EXPECT_EQ(16.0 / 9, caller()->local_rendered_aspect_ratio());
1764 EXPECT_EQ(16.0 / 9, caller()->rendered_aspect_ratio());
1765 EXPECT_EQ(16.0 / 9, callee()->local_rendered_aspect_ratio());
1766 EXPECT_EQ(16.0 / 9, callee()->rendered_aspect_ratio());
1767}
1768
1769// This test sets up an one-way call, with media only from caller to
1770// callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001771TEST_P(PeerConnectionIntegrationTest, OneWayMediaCall) {
deadbeef1dcb1642017-03-29 21:08:16 -07001772 ASSERT_TRUE(CreatePeerConnectionWrappers());
1773 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001774 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001775 caller()->CreateAndSetAndSignalOffer();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001776 MediaExpectations media_expectations;
1777 media_expectations.CalleeExpectsSomeAudioAndVideo();
1778 media_expectations.CallerExpectsNoAudio();
1779 media_expectations.CallerExpectsNoVideo();
1780 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07001781}
1782
1783// This test sets up a audio call initially, with the callee rejecting video
1784// initially. Then later the callee decides to upgrade to audio/video, and
1785// initiates a new offer/answer exchange.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001786TEST_P(PeerConnectionIntegrationTest, AudioToVideoUpgrade) {
deadbeef1dcb1642017-03-29 21:08:16 -07001787 ASSERT_TRUE(CreatePeerConnectionWrappers());
1788 ConnectFakeSignaling();
1789 // Initially, offer an audio/video stream from the caller, but refuse to
1790 // send/receive video on the callee side.
Steve Anton15324772018-01-16 10:26:49 -08001791 caller()->AddAudioVideoTracks();
1792 callee()->AddAudioTrack();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001793 if (sdp_semantics_ == SdpSemantics::kPlanB) {
1794 PeerConnectionInterface::RTCOfferAnswerOptions options;
1795 options.offer_to_receive_video = 0;
1796 callee()->SetOfferAnswerOptions(options);
1797 } else {
1798 callee()->SetRemoteOfferHandler([this] {
1799 callee()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)->Stop();
1800 });
1801 }
deadbeef1dcb1642017-03-29 21:08:16 -07001802 // Do offer/answer and make sure audio is still received end-to-end.
1803 caller()->CreateAndSetAndSignalOffer();
1804 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001805 {
1806 MediaExpectations media_expectations;
1807 media_expectations.ExpectBidirectionalAudio();
1808 media_expectations.ExpectNoVideo();
1809 ASSERT_TRUE(ExpectNewFrames(media_expectations));
1810 }
deadbeef1dcb1642017-03-29 21:08:16 -07001811 // Sanity check that the callee's description has a rejected video section.
1812 ASSERT_NE(nullptr, callee()->pc()->local_description());
1813 const ContentInfo* callee_video_content =
1814 GetFirstVideoContent(callee()->pc()->local_description()->description());
1815 ASSERT_NE(nullptr, callee_video_content);
1816 EXPECT_TRUE(callee_video_content->rejected);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001817
deadbeef1dcb1642017-03-29 21:08:16 -07001818 // Now negotiate with video and ensure negotiation succeeds, with video
1819 // frames and additional audio frames being received.
Steve Anton15324772018-01-16 10:26:49 -08001820 callee()->AddVideoTrack();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001821 if (sdp_semantics_ == SdpSemantics::kPlanB) {
1822 PeerConnectionInterface::RTCOfferAnswerOptions options;
1823 options.offer_to_receive_video = 1;
1824 callee()->SetOfferAnswerOptions(options);
1825 } else {
1826 callee()->SetRemoteOfferHandler(nullptr);
1827 caller()->SetRemoteOfferHandler([this] {
1828 // The caller creates a new transceiver to receive video on when receiving
1829 // the offer, but by default it is send only.
1830 auto transceivers = caller()->pc()->GetTransceivers();
1831 ASSERT_EQ(3, transceivers.size());
1832 ASSERT_EQ(cricket::MEDIA_TYPE_VIDEO,
1833 transceivers[2]->receiver()->media_type());
1834 transceivers[2]->sender()->SetTrack(caller()->CreateLocalVideoTrack());
1835 transceivers[2]->SetDirection(RtpTransceiverDirection::kSendRecv);
1836 });
1837 }
deadbeef1dcb1642017-03-29 21:08:16 -07001838 callee()->CreateAndSetAndSignalOffer();
1839 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001840 {
1841 // Expect additional audio frames to be received after the upgrade.
1842 MediaExpectations media_expectations;
1843 media_expectations.ExpectBidirectionalAudioAndVideo();
1844 ASSERT_TRUE(ExpectNewFrames(media_expectations));
1845 }
deadbeef1dcb1642017-03-29 21:08:16 -07001846}
1847
deadbeef4389b4d2017-09-07 09:07:36 -07001848// Simpler than the above test; just add an audio track to an established
1849// video-only connection.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001850TEST_P(PeerConnectionIntegrationTest, AddAudioToVideoOnlyCall) {
deadbeef4389b4d2017-09-07 09:07:36 -07001851 ASSERT_TRUE(CreatePeerConnectionWrappers());
1852 ConnectFakeSignaling();
1853 // Do initial offer/answer with just a video track.
Steve Anton15324772018-01-16 10:26:49 -08001854 caller()->AddVideoTrack();
1855 callee()->AddVideoTrack();
deadbeef4389b4d2017-09-07 09:07:36 -07001856 caller()->CreateAndSetAndSignalOffer();
1857 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1858 // Now add an audio track and do another offer/answer.
Steve Anton15324772018-01-16 10:26:49 -08001859 caller()->AddAudioTrack();
1860 callee()->AddAudioTrack();
deadbeef4389b4d2017-09-07 09:07:36 -07001861 caller()->CreateAndSetAndSignalOffer();
1862 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1863 // Ensure both audio and video frames are received end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001864 MediaExpectations media_expectations;
1865 media_expectations.ExpectBidirectionalAudioAndVideo();
1866 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef4389b4d2017-09-07 09:07:36 -07001867}
1868
deadbeef1dcb1642017-03-29 21:08:16 -07001869// This test sets up a call that's transferred to a new caller with a different
1870// DTLS fingerprint.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001871TEST_P(PeerConnectionIntegrationTest, CallTransferredForCallee) {
deadbeef1dcb1642017-03-29 21:08:16 -07001872 ASSERT_TRUE(CreatePeerConnectionWrappers());
1873 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001874 caller()->AddAudioVideoTracks();
1875 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001876 caller()->CreateAndSetAndSignalOffer();
1877 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1878
1879 // Keep the original peer around which will still send packets to the
1880 // receiving client. These SRTP packets will be dropped.
1881 std::unique_ptr<PeerConnectionWrapper> original_peer(
1882 SetCallerPcWrapperAndReturnCurrent(
Seth Hampson2f0d7022018-02-20 11:54:42 -08001883 CreatePeerConnectionWrapperWithAlternateKey().release()));
deadbeef1dcb1642017-03-29 21:08:16 -07001884 // TODO(deadbeef): Why do we call Close here? That goes against the comment
1885 // directly above.
1886 original_peer->pc()->Close();
1887
1888 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001889 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001890 caller()->CreateAndSetAndSignalOffer();
1891 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1892 // Wait for some additional frames to be transmitted end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001893 MediaExpectations media_expectations;
1894 media_expectations.ExpectBidirectionalAudioAndVideo();
1895 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07001896}
1897
1898// This test sets up a call that's transferred to a new callee with a different
1899// DTLS fingerprint.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001900TEST_P(PeerConnectionIntegrationTest, CallTransferredForCaller) {
deadbeef1dcb1642017-03-29 21:08:16 -07001901 ASSERT_TRUE(CreatePeerConnectionWrappers());
1902 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001903 caller()->AddAudioVideoTracks();
1904 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001905 caller()->CreateAndSetAndSignalOffer();
1906 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1907
1908 // Keep the original peer around which will still send packets to the
1909 // receiving client. These SRTP packets will be dropped.
1910 std::unique_ptr<PeerConnectionWrapper> original_peer(
1911 SetCalleePcWrapperAndReturnCurrent(
Seth Hampson2f0d7022018-02-20 11:54:42 -08001912 CreatePeerConnectionWrapperWithAlternateKey().release()));
deadbeef1dcb1642017-03-29 21:08:16 -07001913 // TODO(deadbeef): Why do we call Close here? That goes against the comment
1914 // directly above.
1915 original_peer->pc()->Close();
1916
1917 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001918 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001919 caller()->SetOfferAnswerOptions(IceRestartOfferAnswerOptions());
1920 caller()->CreateAndSetAndSignalOffer();
1921 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1922 // Wait for some additional frames to be transmitted end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001923 MediaExpectations media_expectations;
1924 media_expectations.ExpectBidirectionalAudioAndVideo();
1925 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07001926}
1927
1928// This test sets up a non-bundled call and negotiates bundling at the same
1929// time as starting an ICE restart. When bundling is in effect in the restart,
1930// the DTLS-SRTP context should be successfully reset.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001931TEST_P(PeerConnectionIntegrationTest, BundlingEnabledWhileIceRestartOccurs) {
deadbeef1dcb1642017-03-29 21:08:16 -07001932 ASSERT_TRUE(CreatePeerConnectionWrappers());
1933 ConnectFakeSignaling();
1934
Steve Anton15324772018-01-16 10:26:49 -08001935 caller()->AddAudioVideoTracks();
1936 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001937 // Remove the bundle group from the SDP received by the callee.
1938 callee()->SetReceivedSdpMunger([](cricket::SessionDescription* desc) {
1939 desc->RemoveGroupByName("BUNDLE");
1940 });
1941 caller()->CreateAndSetAndSignalOffer();
1942 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001943 {
1944 MediaExpectations media_expectations;
1945 media_expectations.ExpectBidirectionalAudioAndVideo();
1946 ASSERT_TRUE(ExpectNewFrames(media_expectations));
1947 }
deadbeef1dcb1642017-03-29 21:08:16 -07001948 // Now stop removing the BUNDLE group, and trigger an ICE restart.
1949 callee()->SetReceivedSdpMunger(nullptr);
1950 caller()->SetOfferAnswerOptions(IceRestartOfferAnswerOptions());
1951 caller()->CreateAndSetAndSignalOffer();
1952 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1953
1954 // Expect additional frames to be received after the ICE restart.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001955 {
1956 MediaExpectations media_expectations;
1957 media_expectations.ExpectBidirectionalAudioAndVideo();
1958 ASSERT_TRUE(ExpectNewFrames(media_expectations));
1959 }
deadbeef1dcb1642017-03-29 21:08:16 -07001960}
1961
1962// Test CVO (Coordination of Video Orientation). If a video source is rotated
1963// and both peers support the CVO RTP header extension, the actual video frames
1964// don't need to be encoded in different resolutions, since the rotation is
1965// communicated through the RTP header extension.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001966TEST_P(PeerConnectionIntegrationTest, RotatedVideoWithCVOExtension) {
deadbeef1dcb1642017-03-29 21:08:16 -07001967 ASSERT_TRUE(CreatePeerConnectionWrappers());
1968 ConnectFakeSignaling();
1969 // Add rotated video tracks.
Steve Anton15324772018-01-16 10:26:49 -08001970 caller()->AddTrack(
deadbeef1dcb1642017-03-29 21:08:16 -07001971 caller()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_90));
Steve Anton15324772018-01-16 10:26:49 -08001972 callee()->AddTrack(
deadbeef1dcb1642017-03-29 21:08:16 -07001973 callee()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_270));
1974
1975 // Wait for video frames to be received by both sides.
1976 caller()->CreateAndSetAndSignalOffer();
1977 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1978 ASSERT_TRUE_WAIT(caller()->min_video_frames_received_per_track() > 0 &&
1979 callee()->min_video_frames_received_per_track() > 0,
1980 kMaxWaitForFramesMs);
1981
1982 // Ensure that the aspect ratio is unmodified.
1983 // TODO(deadbeef): Where does 4:3 come from? Should be explicit in the test,
1984 // not just assumed.
1985 EXPECT_EQ(4.0 / 3, caller()->local_rendered_aspect_ratio());
1986 EXPECT_EQ(4.0 / 3, caller()->rendered_aspect_ratio());
1987 EXPECT_EQ(4.0 / 3, callee()->local_rendered_aspect_ratio());
1988 EXPECT_EQ(4.0 / 3, callee()->rendered_aspect_ratio());
1989 // Ensure that the CVO bits were surfaced to the renderer.
1990 EXPECT_EQ(webrtc::kVideoRotation_270, caller()->rendered_rotation());
1991 EXPECT_EQ(webrtc::kVideoRotation_90, callee()->rendered_rotation());
1992}
1993
1994// Test that when the CVO extension isn't supported, video is rotated the
1995// old-fashioned way, by encoding rotated frames.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001996TEST_P(PeerConnectionIntegrationTest, RotatedVideoWithoutCVOExtension) {
deadbeef1dcb1642017-03-29 21:08:16 -07001997 ASSERT_TRUE(CreatePeerConnectionWrappers());
1998 ConnectFakeSignaling();
1999 // Add rotated video tracks.
Steve Anton15324772018-01-16 10:26:49 -08002000 caller()->AddTrack(
deadbeef1dcb1642017-03-29 21:08:16 -07002001 caller()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_90));
Steve Anton15324772018-01-16 10:26:49 -08002002 callee()->AddTrack(
deadbeef1dcb1642017-03-29 21:08:16 -07002003 callee()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_270));
2004
2005 // Remove the CVO extension from the offered SDP.
2006 callee()->SetReceivedSdpMunger([](cricket::SessionDescription* desc) {
2007 cricket::VideoContentDescription* video =
2008 GetFirstVideoContentDescription(desc);
2009 video->ClearRtpHeaderExtensions();
2010 });
2011 // Wait for video frames to be received by both sides.
2012 caller()->CreateAndSetAndSignalOffer();
2013 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2014 ASSERT_TRUE_WAIT(caller()->min_video_frames_received_per_track() > 0 &&
2015 callee()->min_video_frames_received_per_track() > 0,
2016 kMaxWaitForFramesMs);
2017
2018 // Expect that the aspect ratio is inversed to account for the 90/270 degree
2019 // rotation.
2020 // TODO(deadbeef): Where does 4:3 come from? Should be explicit in the test,
2021 // not just assumed.
2022 EXPECT_EQ(3.0 / 4, caller()->local_rendered_aspect_ratio());
2023 EXPECT_EQ(3.0 / 4, caller()->rendered_aspect_ratio());
2024 EXPECT_EQ(3.0 / 4, callee()->local_rendered_aspect_ratio());
2025 EXPECT_EQ(3.0 / 4, callee()->rendered_aspect_ratio());
2026 // Expect that each endpoint is unaware of the rotation of the other endpoint.
2027 EXPECT_EQ(webrtc::kVideoRotation_0, caller()->rendered_rotation());
2028 EXPECT_EQ(webrtc::kVideoRotation_0, callee()->rendered_rotation());
2029}
2030
deadbeef1dcb1642017-03-29 21:08:16 -07002031// Test that if the answerer rejects the audio m= section, no audio is sent or
2032// received, but video still can be.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002033TEST_P(PeerConnectionIntegrationTest, AnswererRejectsAudioSection) {
deadbeef1dcb1642017-03-29 21:08:16 -07002034 ASSERT_TRUE(CreatePeerConnectionWrappers());
2035 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002036 caller()->AddAudioVideoTracks();
Seth Hampson2f0d7022018-02-20 11:54:42 -08002037 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2038 // Only add video track for callee, and set offer_to_receive_audio to 0, so
2039 // it will reject the audio m= section completely.
2040 PeerConnectionInterface::RTCOfferAnswerOptions options;
2041 options.offer_to_receive_audio = 0;
2042 callee()->SetOfferAnswerOptions(options);
2043 } else {
2044 // Stopping the audio RtpTransceiver will cause the media section to be
2045 // rejected in the answer.
2046 callee()->SetRemoteOfferHandler([this] {
2047 callee()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_AUDIO)->Stop();
2048 });
2049 }
Steve Anton15324772018-01-16 10:26:49 -08002050 callee()->AddTrack(callee()->CreateLocalVideoTrack());
deadbeef1dcb1642017-03-29 21:08:16 -07002051 // Do offer/answer and wait for successful end-to-end video frames.
2052 caller()->CreateAndSetAndSignalOffer();
2053 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002054 MediaExpectations media_expectations;
2055 media_expectations.ExpectBidirectionalVideo();
2056 media_expectations.ExpectNoAudio();
2057 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2058
deadbeef1dcb1642017-03-29 21:08:16 -07002059 // Sanity check that the callee's description has a rejected audio section.
2060 ASSERT_NE(nullptr, callee()->pc()->local_description());
2061 const ContentInfo* callee_audio_content =
2062 GetFirstAudioContent(callee()->pc()->local_description()->description());
2063 ASSERT_NE(nullptr, callee_audio_content);
2064 EXPECT_TRUE(callee_audio_content->rejected);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002065 if (sdp_semantics_ == SdpSemantics::kUnifiedPlan) {
2066 // The caller's transceiver should have stopped after receiving the answer.
2067 EXPECT_TRUE(caller()
2068 ->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_AUDIO)
2069 ->stopped());
2070 }
deadbeef1dcb1642017-03-29 21:08:16 -07002071}
2072
2073// Test that if the answerer rejects the video m= section, no video is sent or
2074// received, but audio still can be.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002075TEST_P(PeerConnectionIntegrationTest, AnswererRejectsVideoSection) {
deadbeef1dcb1642017-03-29 21:08:16 -07002076 ASSERT_TRUE(CreatePeerConnectionWrappers());
2077 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002078 caller()->AddAudioVideoTracks();
Seth Hampson2f0d7022018-02-20 11:54:42 -08002079 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2080 // Only add audio track for callee, and set offer_to_receive_video to 0, so
2081 // it will reject the video m= section completely.
2082 PeerConnectionInterface::RTCOfferAnswerOptions options;
2083 options.offer_to_receive_video = 0;
2084 callee()->SetOfferAnswerOptions(options);
2085 } else {
2086 // Stopping the video RtpTransceiver will cause the media section to be
2087 // rejected in the answer.
2088 callee()->SetRemoteOfferHandler([this] {
2089 callee()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)->Stop();
2090 });
2091 }
Steve Anton15324772018-01-16 10:26:49 -08002092 callee()->AddTrack(callee()->CreateLocalAudioTrack());
deadbeef1dcb1642017-03-29 21:08:16 -07002093 // Do offer/answer and wait for successful end-to-end audio frames.
2094 caller()->CreateAndSetAndSignalOffer();
2095 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002096 MediaExpectations media_expectations;
2097 media_expectations.ExpectBidirectionalAudio();
2098 media_expectations.ExpectNoVideo();
2099 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2100
deadbeef1dcb1642017-03-29 21:08:16 -07002101 // Sanity check that the callee's description has a rejected video section.
2102 ASSERT_NE(nullptr, callee()->pc()->local_description());
2103 const ContentInfo* callee_video_content =
2104 GetFirstVideoContent(callee()->pc()->local_description()->description());
2105 ASSERT_NE(nullptr, callee_video_content);
2106 EXPECT_TRUE(callee_video_content->rejected);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002107 if (sdp_semantics_ == SdpSemantics::kUnifiedPlan) {
2108 // The caller's transceiver should have stopped after receiving the answer.
2109 EXPECT_TRUE(caller()
2110 ->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)
2111 ->stopped());
2112 }
deadbeef1dcb1642017-03-29 21:08:16 -07002113}
2114
2115// Test that if the answerer rejects both audio and video m= sections, nothing
2116// bad happens.
2117// TODO(deadbeef): Test that a data channel still works. Currently this doesn't
2118// test anything but the fact that negotiation succeeds, which doesn't mean
2119// much.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002120TEST_P(PeerConnectionIntegrationTest, AnswererRejectsAudioAndVideoSections) {
deadbeef1dcb1642017-03-29 21:08:16 -07002121 ASSERT_TRUE(CreatePeerConnectionWrappers());
2122 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002123 caller()->AddAudioVideoTracks();
Seth Hampson2f0d7022018-02-20 11:54:42 -08002124 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2125 // Don't give the callee any tracks, and set offer_to_receive_X to 0, so it
2126 // will reject both audio and video m= sections.
2127 PeerConnectionInterface::RTCOfferAnswerOptions options;
2128 options.offer_to_receive_audio = 0;
2129 options.offer_to_receive_video = 0;
2130 callee()->SetOfferAnswerOptions(options);
2131 } else {
2132 callee()->SetRemoteOfferHandler([this] {
2133 // Stopping all transceivers will cause all media sections to be rejected.
2134 for (auto transceiver : callee()->pc()->GetTransceivers()) {
2135 transceiver->Stop();
2136 }
2137 });
2138 }
deadbeef1dcb1642017-03-29 21:08:16 -07002139 // Do offer/answer and wait for stable signaling state.
2140 caller()->CreateAndSetAndSignalOffer();
2141 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002142
deadbeef1dcb1642017-03-29 21:08:16 -07002143 // Sanity check that the callee's description has rejected m= sections.
2144 ASSERT_NE(nullptr, callee()->pc()->local_description());
2145 const ContentInfo* callee_audio_content =
2146 GetFirstAudioContent(callee()->pc()->local_description()->description());
2147 ASSERT_NE(nullptr, callee_audio_content);
2148 EXPECT_TRUE(callee_audio_content->rejected);
2149 const ContentInfo* callee_video_content =
2150 GetFirstVideoContent(callee()->pc()->local_description()->description());
2151 ASSERT_NE(nullptr, callee_video_content);
2152 EXPECT_TRUE(callee_video_content->rejected);
2153}
2154
2155// This test sets up an audio and video call between two parties. After the
2156// call runs for a while, the caller sends an updated offer with video being
2157// rejected. Once the re-negotiation is done, the video flow should stop and
2158// the audio flow should continue.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002159TEST_P(PeerConnectionIntegrationTest, VideoRejectedInSubsequentOffer) {
deadbeef1dcb1642017-03-29 21:08:16 -07002160 ASSERT_TRUE(CreatePeerConnectionWrappers());
2161 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002162 caller()->AddAudioVideoTracks();
2163 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002164 caller()->CreateAndSetAndSignalOffer();
2165 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002166 {
2167 MediaExpectations media_expectations;
2168 media_expectations.ExpectBidirectionalAudioAndVideo();
2169 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2170 }
deadbeef1dcb1642017-03-29 21:08:16 -07002171 // Renegotiate, rejecting the video m= section.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002172 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2173 caller()->SetGeneratedSdpMunger(
2174 [](cricket::SessionDescription* description) {
2175 for (cricket::ContentInfo& content : description->contents()) {
2176 if (cricket::IsVideoContent(&content)) {
2177 content.rejected = true;
2178 }
2179 }
2180 });
2181 } else {
2182 caller()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)->Stop();
2183 }
deadbeef1dcb1642017-03-29 21:08:16 -07002184 caller()->CreateAndSetAndSignalOffer();
2185 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
2186
2187 // Sanity check that the caller's description has a rejected video section.
2188 ASSERT_NE(nullptr, caller()->pc()->local_description());
2189 const ContentInfo* caller_video_content =
2190 GetFirstVideoContent(caller()->pc()->local_description()->description());
2191 ASSERT_NE(nullptr, caller_video_content);
2192 EXPECT_TRUE(caller_video_content->rejected);
deadbeef1dcb1642017-03-29 21:08:16 -07002193 // Wait for some additional audio frames to be received.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002194 {
2195 MediaExpectations media_expectations;
2196 media_expectations.ExpectBidirectionalAudio();
2197 media_expectations.ExpectNoVideo();
2198 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2199 }
deadbeef1dcb1642017-03-29 21:08:16 -07002200}
2201
Taylor Brandstetter60c8dc82018-04-11 15:20:27 -07002202// Do one offer/answer with audio, another that disables it (rejecting the m=
2203// section), and another that re-enables it. Regression test for:
2204// bugs.webrtc.org/6023
2205TEST_F(PeerConnectionIntegrationTestPlanB, EnableAudioAfterRejecting) {
2206 ASSERT_TRUE(CreatePeerConnectionWrappers());
2207 ConnectFakeSignaling();
2208
2209 // Add audio track, do normal offer/answer.
2210 rtc::scoped_refptr<webrtc::AudioTrackInterface> track =
2211 caller()->CreateLocalAudioTrack();
2212 rtc::scoped_refptr<webrtc::RtpSenderInterface> sender =
2213 caller()->pc()->AddTrack(track, {"stream"}).MoveValue();
2214 caller()->CreateAndSetAndSignalOffer();
2215 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2216
2217 // Remove audio track, and set offer_to_receive_audio to false to cause the
2218 // m= section to be completely disabled, not just "recvonly".
2219 caller()->pc()->RemoveTrack(sender);
2220 PeerConnectionInterface::RTCOfferAnswerOptions options;
2221 options.offer_to_receive_audio = 0;
2222 caller()->SetOfferAnswerOptions(options);
2223 caller()->CreateAndSetAndSignalOffer();
2224 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2225
2226 // Add the audio track again, expecting negotiation to succeed and frames to
2227 // flow.
2228 sender = caller()->pc()->AddTrack(track, {"stream"}).MoveValue();
2229 options.offer_to_receive_audio = 1;
2230 caller()->SetOfferAnswerOptions(options);
2231 caller()->CreateAndSetAndSignalOffer();
2232 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2233
2234 MediaExpectations media_expectations;
2235 media_expectations.CalleeExpectsSomeAudio();
2236 EXPECT_TRUE(ExpectNewFrames(media_expectations));
2237}
2238
deadbeef1dcb1642017-03-29 21:08:16 -07002239// Basic end-to-end test, but without SSRC/MSID signaling. This functionality
2240// is needed to support legacy endpoints.
2241// TODO(deadbeef): When we support the MID extension and demuxing on MID, also
2242// add a test for an end-to-end test without MID signaling either (basically,
2243// the minimum acceptable SDP).
Seth Hampson2f0d7022018-02-20 11:54:42 -08002244TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithoutSsrcOrMsidSignaling) {
deadbeef1dcb1642017-03-29 21:08:16 -07002245 ASSERT_TRUE(CreatePeerConnectionWrappers());
2246 ConnectFakeSignaling();
2247 // Add audio and video, testing that packets can be demuxed on payload type.
Steve Anton15324772018-01-16 10:26:49 -08002248 caller()->AddAudioVideoTracks();
2249 callee()->AddAudioVideoTracks();
deadbeefd8ad7882017-04-18 16:01:17 -07002250 // Remove SSRCs and MSIDs from the received offer SDP.
2251 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
deadbeef1dcb1642017-03-29 21:08:16 -07002252 caller()->CreateAndSetAndSignalOffer();
2253 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002254 MediaExpectations media_expectations;
2255 media_expectations.ExpectBidirectionalAudioAndVideo();
2256 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002257}
2258
Seth Hampson5897a6e2018-04-03 11:16:33 -07002259// Basic end-to-end test, without SSRC signaling. This means that the track
2260// was created properly and frames are delivered when the MSIDs are communicated
2261// with a=msid lines and no a=ssrc lines.
2262TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
2263 EndToEndCallWithoutSsrcSignaling) {
2264 const char kStreamId[] = "streamId";
2265 ASSERT_TRUE(CreatePeerConnectionWrappers());
2266 ConnectFakeSignaling();
2267 // Add just audio tracks.
2268 caller()->AddTrack(caller()->CreateLocalAudioTrack(), {kStreamId});
2269 callee()->AddAudioTrack();
2270
2271 // Remove SSRCs from the received offer SDP.
2272 callee()->SetReceivedSdpMunger(RemoveSsrcsAndKeepMsids);
2273 caller()->CreateAndSetAndSignalOffer();
2274 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2275 MediaExpectations media_expectations;
2276 media_expectations.ExpectBidirectionalAudio();
2277 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2278}
2279
Steve Antondf527fd2018-04-27 15:52:03 -07002280// Tests that video flows between multiple video tracks when SSRCs are not
2281// signaled. This exercises the MID RTP header extension which is needed to
2282// demux the incoming video tracks.
2283TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
2284 EndToEndCallWithTwoVideoTracksAndNoSignaledSsrc) {
2285 ASSERT_TRUE(CreatePeerConnectionWrappers());
2286 ConnectFakeSignaling();
2287 caller()->AddVideoTrack();
2288 caller()->AddVideoTrack();
2289 callee()->AddVideoTrack();
2290 callee()->AddVideoTrack();
2291
2292 caller()->SetReceivedSdpMunger(&RemoveSsrcsAndKeepMsids);
2293 callee()->SetReceivedSdpMunger(&RemoveSsrcsAndKeepMsids);
2294 caller()->CreateAndSetAndSignalOffer();
2295 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2296 ASSERT_EQ(2u, caller()->pc()->GetReceivers().size());
2297 ASSERT_EQ(2u, callee()->pc()->GetReceivers().size());
2298
2299 // Expect video to be received in both directions on both tracks.
2300 MediaExpectations media_expectations;
2301 media_expectations.ExpectBidirectionalVideo();
2302 EXPECT_TRUE(ExpectNewFrames(media_expectations));
2303}
2304
deadbeef1dcb1642017-03-29 21:08:16 -07002305// Test that if two video tracks are sent (from caller to callee, in this test),
2306// they're transmitted correctly end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002307TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithTwoVideoTracks) {
deadbeef1dcb1642017-03-29 21:08:16 -07002308 ASSERT_TRUE(CreatePeerConnectionWrappers());
2309 ConnectFakeSignaling();
2310 // Add one audio/video stream, and one video-only stream.
Steve Anton15324772018-01-16 10:26:49 -08002311 caller()->AddAudioVideoTracks();
2312 caller()->AddVideoTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07002313 caller()->CreateAndSetAndSignalOffer();
2314 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Steve Anton15324772018-01-16 10:26:49 -08002315 ASSERT_EQ(3u, callee()->pc()->GetReceivers().size());
Seth Hampson2f0d7022018-02-20 11:54:42 -08002316
2317 MediaExpectations media_expectations;
2318 media_expectations.CalleeExpectsSomeAudioAndVideo();
2319 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002320}
2321
2322static void MakeSpecCompliantMaxBundleOffer(cricket::SessionDescription* desc) {
2323 bool first = true;
2324 for (cricket::ContentInfo& content : desc->contents()) {
2325 if (first) {
2326 first = false;
2327 continue;
2328 }
2329 content.bundle_only = true;
2330 }
2331 first = true;
2332 for (cricket::TransportInfo& transport : desc->transport_infos()) {
2333 if (first) {
2334 first = false;
2335 continue;
2336 }
2337 transport.description.ice_ufrag.clear();
2338 transport.description.ice_pwd.clear();
2339 transport.description.connection_role = cricket::CONNECTIONROLE_NONE;
2340 transport.description.identity_fingerprint.reset(nullptr);
2341 }
2342}
2343
2344// Test that if applying a true "max bundle" offer, which uses ports of 0,
2345// "a=bundle-only", omitting "a=fingerprint", "a=setup", "a=ice-ufrag" and
2346// "a=ice-pwd" for all but the audio "m=" section, negotiation still completes
2347// successfully and media flows.
2348// TODO(deadbeef): Update this test to also omit "a=rtcp-mux", once that works.
2349// TODO(deadbeef): Won't need this test once we start generating actual
2350// standards-compliant SDP.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002351TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07002352 EndToEndCallWithSpecCompliantMaxBundleOffer) {
2353 ASSERT_TRUE(CreatePeerConnectionWrappers());
2354 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002355 caller()->AddAudioVideoTracks();
2356 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002357 // Do the equivalent of setting the port to 0, adding a=bundle-only, and
2358 // removing a=ice-ufrag, a=ice-pwd, a=fingerprint and a=setup from all
2359 // but the first m= section.
2360 callee()->SetReceivedSdpMunger(MakeSpecCompliantMaxBundleOffer);
2361 caller()->CreateAndSetAndSignalOffer();
2362 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002363 MediaExpectations media_expectations;
2364 media_expectations.ExpectBidirectionalAudioAndVideo();
2365 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002366}
2367
2368// Test that we can receive the audio output level from a remote audio track.
2369// TODO(deadbeef): Use a fake audio source and verify that the output level is
2370// exactly what the source on the other side was configured with.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002371TEST_P(PeerConnectionIntegrationTest, GetAudioOutputLevelStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-29 21:08:16 -07002372 ASSERT_TRUE(CreatePeerConnectionWrappers());
2373 ConnectFakeSignaling();
2374 // Just add an audio track.
Steve Anton15324772018-01-16 10:26:49 -08002375 caller()->AddAudioTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07002376 caller()->CreateAndSetAndSignalOffer();
2377 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2378
2379 // Get the audio output level stats. Note that the level is not available
2380 // until an RTCP packet has been received.
deadbeefd8ad7882017-04-18 16:01:17 -07002381 EXPECT_TRUE_WAIT(callee()->OldGetStats()->AudioOutputLevel() > 0,
deadbeef1dcb1642017-03-29 21:08:16 -07002382 kMaxWaitForFramesMs);
2383}
2384
2385// Test that an audio input level is reported.
2386// TODO(deadbeef): Use a fake audio source and verify that the input level is
2387// exactly what the source was configured with.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002388TEST_P(PeerConnectionIntegrationTest, GetAudioInputLevelStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-29 21:08:16 -07002389 ASSERT_TRUE(CreatePeerConnectionWrappers());
2390 ConnectFakeSignaling();
2391 // Just add an audio track.
Steve Anton15324772018-01-16 10:26:49 -08002392 caller()->AddAudioTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07002393 caller()->CreateAndSetAndSignalOffer();
2394 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2395
2396 // Get the audio input level stats. The level should be available very
2397 // soon after the test starts.
deadbeefd8ad7882017-04-18 16:01:17 -07002398 EXPECT_TRUE_WAIT(caller()->OldGetStats()->AudioInputLevel() > 0,
deadbeef1dcb1642017-03-29 21:08:16 -07002399 kMaxWaitForStatsMs);
2400}
2401
2402// Test that we can get incoming byte counts from both audio and video tracks.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002403TEST_P(PeerConnectionIntegrationTest, GetBytesReceivedStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-29 21:08:16 -07002404 ASSERT_TRUE(CreatePeerConnectionWrappers());
2405 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002406 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002407 // Do offer/answer, wait for the callee to receive some frames.
2408 caller()->CreateAndSetAndSignalOffer();
2409 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002410
2411 MediaExpectations media_expectations;
2412 media_expectations.CalleeExpectsSomeAudioAndVideo();
2413 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002414
2415 // Get a handle to the remote tracks created, so they can be used as GetStats
2416 // filters.
Steve Anton15324772018-01-16 10:26:49 -08002417 for (auto receiver : callee()->pc()->GetReceivers()) {
2418 // We received frames, so we definitely should have nonzero "received bytes"
2419 // stats at this point.
2420 EXPECT_GT(callee()->OldGetStatsForTrack(receiver->track())->BytesReceived(),
2421 0);
2422 }
deadbeef1dcb1642017-03-29 21:08:16 -07002423}
2424
2425// Test that we can get outgoing byte counts from both audio and video tracks.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002426TEST_P(PeerConnectionIntegrationTest, GetBytesSentStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-29 21:08:16 -07002427 ASSERT_TRUE(CreatePeerConnectionWrappers());
2428 ConnectFakeSignaling();
2429 auto audio_track = caller()->CreateLocalAudioTrack();
2430 auto video_track = caller()->CreateLocalVideoTrack();
Steve Anton15324772018-01-16 10:26:49 -08002431 caller()->AddTrack(audio_track);
2432 caller()->AddTrack(video_track);
deadbeef1dcb1642017-03-29 21:08:16 -07002433 // Do offer/answer, wait for the callee to receive some frames.
2434 caller()->CreateAndSetAndSignalOffer();
2435 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002436 MediaExpectations media_expectations;
2437 media_expectations.CalleeExpectsSomeAudioAndVideo();
2438 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002439
2440 // The callee received frames, so we definitely should have nonzero "sent
2441 // bytes" stats at this point.
deadbeefd8ad7882017-04-18 16:01:17 -07002442 EXPECT_GT(caller()->OldGetStatsForTrack(audio_track)->BytesSent(), 0);
2443 EXPECT_GT(caller()->OldGetStatsForTrack(video_track)->BytesSent(), 0);
2444}
2445
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002446// Test that we can get capture start ntp time.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002447TEST_P(PeerConnectionIntegrationTest, GetCaptureStartNtpTimeWithOldStatsApi) {
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002448 ASSERT_TRUE(CreatePeerConnectionWrappers());
2449 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002450 caller()->AddAudioTrack();
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002451
Steve Anton15324772018-01-16 10:26:49 -08002452 callee()->AddAudioTrack();
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002453
2454 // Do offer/answer, wait for the callee to receive some frames.
2455 caller()->CreateAndSetAndSignalOffer();
2456 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2457
2458 // Get the remote audio track created on the receiver, so they can be used as
2459 // GetStats filters.
Steve Antonfc853712018-03-01 13:48:58 -08002460 auto receivers = callee()->pc()->GetReceivers();
2461 ASSERT_EQ(1u, receivers.size());
2462 auto remote_audio_track = receivers[0]->track();
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002463
2464 // Get the audio output level stats. Note that the level is not available
2465 // until an RTCP packet has been received.
Zhi Huange830e682018-03-30 10:48:35 -07002466 EXPECT_TRUE_WAIT(
2467 callee()->OldGetStatsForTrack(remote_audio_track)->CaptureStartNtpTime() >
2468 0,
2469 2 * kMaxWaitForFramesMs);
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002470}
2471
deadbeefd8ad7882017-04-18 16:01:17 -07002472// Test that we can get stats (using the new stats implemnetation) for
2473// unsignaled streams. Meaning when SSRCs/MSIDs aren't signaled explicitly in
2474// SDP.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002475TEST_P(PeerConnectionIntegrationTest,
deadbeefd8ad7882017-04-18 16:01:17 -07002476 GetStatsForUnsignaledStreamWithNewStatsApi) {
2477 ASSERT_TRUE(CreatePeerConnectionWrappers());
2478 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002479 caller()->AddAudioTrack();
deadbeefd8ad7882017-04-18 16:01:17 -07002480 // Remove SSRCs and MSIDs from the received offer SDP.
2481 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
2482 caller()->CreateAndSetAndSignalOffer();
2483 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002484 MediaExpectations media_expectations;
2485 media_expectations.CalleeExpectsSomeAudio(1);
2486 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeefd8ad7882017-04-18 16:01:17 -07002487
2488 // We received a frame, so we should have nonzero "bytes received" stats for
2489 // the unsignaled stream, if stats are working for it.
2490 rtc::scoped_refptr<const webrtc::RTCStatsReport> report =
2491 callee()->NewGetStats();
2492 ASSERT_NE(nullptr, report);
2493 auto inbound_stream_stats =
2494 report->GetStatsOfType<webrtc::RTCInboundRTPStreamStats>();
2495 ASSERT_EQ(1U, inbound_stream_stats.size());
2496 ASSERT_TRUE(inbound_stream_stats[0]->bytes_received.is_defined());
2497 ASSERT_GT(*inbound_stream_stats[0]->bytes_received, 0U);
zhihuangf8164932017-05-19 13:09:47 -07002498 ASSERT_TRUE(inbound_stream_stats[0]->track_id.is_defined());
2499}
2500
2501// Test that we can successfully get the media related stats (audio level
2502// etc.) for the unsignaled stream.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002503TEST_P(PeerConnectionIntegrationTest,
zhihuangf8164932017-05-19 13:09:47 -07002504 GetMediaStatsForUnsignaledStreamWithNewStatsApi) {
2505 ASSERT_TRUE(CreatePeerConnectionWrappers());
2506 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002507 caller()->AddAudioVideoTracks();
zhihuangf8164932017-05-19 13:09:47 -07002508 // Remove SSRCs and MSIDs from the received offer SDP.
2509 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
2510 caller()->CreateAndSetAndSignalOffer();
2511 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002512 MediaExpectations media_expectations;
2513 media_expectations.CalleeExpectsSomeAudio(1);
2514 media_expectations.CalleeExpectsSomeVideo(1);
2515 ASSERT_TRUE(ExpectNewFrames(media_expectations));
zhihuangf8164932017-05-19 13:09:47 -07002516
2517 rtc::scoped_refptr<const webrtc::RTCStatsReport> report =
2518 callee()->NewGetStats();
2519 ASSERT_NE(nullptr, report);
2520
2521 auto media_stats = report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
2522 auto audio_index = FindFirstMediaStatsIndexByKind("audio", media_stats);
2523 ASSERT_GE(audio_index, 0);
2524 EXPECT_TRUE(media_stats[audio_index]->audio_level.is_defined());
deadbeef1dcb1642017-03-29 21:08:16 -07002525}
2526
deadbeef4e2deab2017-09-20 13:56:21 -07002527// Helper for test below.
2528void ModifySsrcs(cricket::SessionDescription* desc) {
2529 for (ContentInfo& content : desc->contents()) {
Steve Antondf527fd2018-04-27 15:52:03 -07002530 for (StreamParams& stream :
Steve Antonb1c1de12017-12-21 15:14:30 -08002531 content.media_description()->mutable_streams()) {
deadbeef4e2deab2017-09-20 13:56:21 -07002532 for (uint32_t& ssrc : stream.ssrcs) {
2533 ssrc = rtc::CreateRandomId();
2534 }
2535 }
2536 }
2537}
2538
2539// Test that the "RTCMediaSteamTrackStats" object is updated correctly when
2540// SSRCs are unsignaled, and the SSRC of the received (audio) stream changes.
2541// This should result in two "RTCInboundRTPStreamStats", but only one
2542// "RTCMediaStreamTrackStats", whose counters go up continuously rather than
2543// being reset to 0 once the SSRC change occurs.
2544//
2545// Regression test for this bug:
2546// https://bugs.chromium.org/p/webrtc/issues/detail?id=8158
2547//
2548// The bug causes the track stats to only represent one of the two streams:
2549// whichever one has the higher SSRC. So with this bug, there was a 50% chance
2550// that the track stat counters would reset to 0 when the new stream is
2551// received, and a 50% chance that they'll stop updating (while
2552// "concealed_samples" continues increasing, due to silence being generated for
2553// the inactive stream).
Seth Hampson2f0d7022018-02-20 11:54:42 -08002554TEST_P(PeerConnectionIntegrationTest,
Steve Anton83119dd2017-11-10 16:19:52 -08002555 TrackStatsUpdatedCorrectlyWhenUnsignaledSsrcChanges) {
deadbeef4e2deab2017-09-20 13:56:21 -07002556 ASSERT_TRUE(CreatePeerConnectionWrappers());
2557 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002558 caller()->AddAudioTrack();
deadbeef4e2deab2017-09-20 13:56:21 -07002559 // Remove SSRCs and MSIDs from the received offer SDP, simulating an endpoint
2560 // that doesn't signal SSRCs (from the callee's perspective).
2561 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
2562 caller()->CreateAndSetAndSignalOffer();
2563 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2564 // Wait for 50 audio frames (500ms of audio) to be received by the callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002565 {
2566 MediaExpectations media_expectations;
2567 media_expectations.CalleeExpectsSomeAudio(50);
2568 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2569 }
deadbeef4e2deab2017-09-20 13:56:21 -07002570 // Some audio frames were received, so we should have nonzero "samples
2571 // received" for the track.
2572 rtc::scoped_refptr<const webrtc::RTCStatsReport> report =
2573 callee()->NewGetStats();
2574 ASSERT_NE(nullptr, report);
2575 auto track_stats = report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
2576 ASSERT_EQ(1U, track_stats.size());
2577 ASSERT_TRUE(track_stats[0]->total_samples_received.is_defined());
2578 ASSERT_GT(*track_stats[0]->total_samples_received, 0U);
2579 // uint64_t prev_samples_received = *track_stats[0]->total_samples_received;
2580
2581 // Create a new offer and munge it to cause the caller to use a new SSRC.
2582 caller()->SetGeneratedSdpMunger(ModifySsrcs);
2583 caller()->CreateAndSetAndSignalOffer();
2584 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2585 // Wait for 25 more audio frames (250ms of audio) to be received, from the new
2586 // SSRC.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002587 {
2588 MediaExpectations media_expectations;
2589 media_expectations.CalleeExpectsSomeAudio(25);
2590 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2591 }
deadbeef4e2deab2017-09-20 13:56:21 -07002592
2593 report = callee()->NewGetStats();
2594 ASSERT_NE(nullptr, report);
2595 track_stats = report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
2596 ASSERT_EQ(1U, track_stats.size());
2597 ASSERT_TRUE(track_stats[0]->total_samples_received.is_defined());
2598 // The "total samples received" stat should only be greater than it was
2599 // before.
2600 // TODO(deadbeef): Uncomment this assertion once the bug is completely fixed.
2601 // Right now, the new SSRC will cause the counters to reset to 0.
2602 // EXPECT_GT(*track_stats[0]->total_samples_received, prev_samples_received);
2603
2604 // Additionally, the percentage of concealed samples (samples generated to
Steve Anton83119dd2017-11-10 16:19:52 -08002605 // conceal packet loss) should be less than 50%. If it's greater, that's a
deadbeef4e2deab2017-09-20 13:56:21 -07002606 // good sign that we're seeing stats from the old stream that's no longer
2607 // receiving packets, and is generating concealed samples of silence.
Steve Anton83119dd2017-11-10 16:19:52 -08002608 constexpr double kAcceptableConcealedSamplesPercentage = 0.50;
deadbeef4e2deab2017-09-20 13:56:21 -07002609 ASSERT_TRUE(track_stats[0]->concealed_samples.is_defined());
2610 EXPECT_LT(*track_stats[0]->concealed_samples,
2611 *track_stats[0]->total_samples_received *
2612 kAcceptableConcealedSamplesPercentage);
2613
2614 // Also ensure that we have two "RTCInboundRTPStreamStats" as expected, as a
2615 // sanity check that the SSRC really changed.
2616 // TODO(deadbeef): This isn't working right now, because we're not returning
2617 // *any* stats for the inactive stream. Uncomment when the bug is completely
2618 // fixed.
2619 // auto inbound_stream_stats =
2620 // report->GetStatsOfType<webrtc::RTCInboundRTPStreamStats>();
2621 // ASSERT_EQ(2U, inbound_stream_stats.size());
2622}
2623
deadbeef1dcb1642017-03-29 21:08:16 -07002624// Test that DTLS 1.0 is used if both sides only support DTLS 1.0.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002625TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithDtls10) {
deadbeef1dcb1642017-03-29 21:08:16 -07002626 PeerConnectionFactory::Options dtls_10_options;
2627 dtls_10_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
2628 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(dtls_10_options,
2629 dtls_10_options));
2630 ConnectFakeSignaling();
2631 // Do normal offer/answer and wait for some frames to be received in each
2632 // direction.
Steve Anton15324772018-01-16 10:26:49 -08002633 caller()->AddAudioVideoTracks();
2634 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002635 caller()->CreateAndSetAndSignalOffer();
2636 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002637 MediaExpectations media_expectations;
2638 media_expectations.ExpectBidirectionalAudioAndVideo();
2639 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002640}
2641
2642// Test getting cipher stats and UMA metrics when DTLS 1.0 is negotiated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002643TEST_P(PeerConnectionIntegrationTest, Dtls10CipherStatsAndUmaMetrics) {
deadbeef1dcb1642017-03-29 21:08:16 -07002644 PeerConnectionFactory::Options dtls_10_options;
2645 dtls_10_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
2646 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(dtls_10_options,
2647 dtls_10_options));
2648 ConnectFakeSignaling();
2649 // Register UMA observer before signaling begins.
2650 rtc::scoped_refptr<webrtc::FakeMetricsObserver> caller_observer =
2651 new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
2652 caller()->pc()->RegisterUMAObserver(caller_observer);
Steve Anton15324772018-01-16 10:26:49 -08002653 caller()->AddAudioVideoTracks();
2654 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002655 caller()->CreateAndSetAndSignalOffer();
2656 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2657 EXPECT_TRUE_WAIT(rtc::SSLStreamAdapter::IsAcceptableCipher(
deadbeefd8ad7882017-04-18 16:01:17 -07002658 caller()->OldGetStats()->DtlsCipher(), rtc::KT_DEFAULT),
deadbeef1dcb1642017-03-29 21:08:16 -07002659 kDefaultTimeout);
2660 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
deadbeefd8ad7882017-04-18 16:01:17 -07002661 caller()->OldGetStats()->SrtpCipher(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07002662 EXPECT_EQ(1,
2663 caller_observer->GetEnumCounter(webrtc::kEnumCounterAudioSrtpCipher,
2664 kDefaultSrtpCryptoSuite));
2665}
2666
2667// Test getting cipher stats and UMA metrics when DTLS 1.2 is negotiated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002668TEST_P(PeerConnectionIntegrationTest, Dtls12CipherStatsAndUmaMetrics) {
deadbeef1dcb1642017-03-29 21:08:16 -07002669 PeerConnectionFactory::Options dtls_12_options;
2670 dtls_12_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
2671 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(dtls_12_options,
2672 dtls_12_options));
2673 ConnectFakeSignaling();
2674 // Register UMA observer before signaling begins.
2675 rtc::scoped_refptr<webrtc::FakeMetricsObserver> caller_observer =
2676 new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
2677 caller()->pc()->RegisterUMAObserver(caller_observer);
Steve Anton15324772018-01-16 10:26:49 -08002678 caller()->AddAudioVideoTracks();
2679 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002680 caller()->CreateAndSetAndSignalOffer();
2681 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2682 EXPECT_TRUE_WAIT(rtc::SSLStreamAdapter::IsAcceptableCipher(
deadbeefd8ad7882017-04-18 16:01:17 -07002683 caller()->OldGetStats()->DtlsCipher(), rtc::KT_DEFAULT),
deadbeef1dcb1642017-03-29 21:08:16 -07002684 kDefaultTimeout);
2685 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
deadbeefd8ad7882017-04-18 16:01:17 -07002686 caller()->OldGetStats()->SrtpCipher(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07002687 EXPECT_EQ(1,
2688 caller_observer->GetEnumCounter(webrtc::kEnumCounterAudioSrtpCipher,
2689 kDefaultSrtpCryptoSuite));
2690}
2691
2692// Test that DTLS 1.0 can be used if the caller supports DTLS 1.2 and the
2693// callee only supports 1.0.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002694TEST_P(PeerConnectionIntegrationTest, CallerDtls12ToCalleeDtls10) {
deadbeef1dcb1642017-03-29 21:08:16 -07002695 PeerConnectionFactory::Options caller_options;
2696 caller_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
2697 PeerConnectionFactory::Options callee_options;
2698 callee_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
2699 ASSERT_TRUE(
2700 CreatePeerConnectionWrappersWithOptions(caller_options, callee_options));
2701 ConnectFakeSignaling();
2702 // Do normal offer/answer and wait for some frames to be received in each
2703 // direction.
Steve Anton15324772018-01-16 10:26:49 -08002704 caller()->AddAudioVideoTracks();
2705 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002706 caller()->CreateAndSetAndSignalOffer();
2707 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002708 MediaExpectations media_expectations;
2709 media_expectations.ExpectBidirectionalAudioAndVideo();
2710 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002711}
2712
2713// Test that DTLS 1.0 can be used if the caller only supports DTLS 1.0 and the
2714// callee supports 1.2.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002715TEST_P(PeerConnectionIntegrationTest, CallerDtls10ToCalleeDtls12) {
deadbeef1dcb1642017-03-29 21:08:16 -07002716 PeerConnectionFactory::Options caller_options;
2717 caller_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
2718 PeerConnectionFactory::Options callee_options;
2719 callee_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
2720 ASSERT_TRUE(
2721 CreatePeerConnectionWrappersWithOptions(caller_options, callee_options));
2722 ConnectFakeSignaling();
2723 // Do normal offer/answer and wait for some frames to be received in each
2724 // direction.
Steve Anton15324772018-01-16 10:26:49 -08002725 caller()->AddAudioVideoTracks();
2726 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002727 caller()->CreateAndSetAndSignalOffer();
2728 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002729 MediaExpectations media_expectations;
2730 media_expectations.ExpectBidirectionalAudioAndVideo();
2731 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002732}
2733
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07002734// The three tests below verify that "enable_aes128_sha1_32_crypto_cipher"
2735// works as expected; the cipher should only be used if enabled by both sides.
2736TEST_P(PeerConnectionIntegrationTest,
2737 Aes128Sha1_32_CipherNotUsedWhenOnlyCallerSupported) {
2738 PeerConnectionFactory::Options caller_options;
2739 caller_options.crypto_options.enable_aes128_sha1_32_crypto_cipher = true;
2740 PeerConnectionFactory::Options callee_options;
2741 callee_options.crypto_options.enable_aes128_sha1_32_crypto_cipher = false;
2742 int expected_cipher_suite = rtc::SRTP_AES128_CM_SHA1_80;
2743 TestNegotiatedCipherSuite(caller_options, callee_options,
2744 expected_cipher_suite);
2745}
2746
2747TEST_P(PeerConnectionIntegrationTest,
2748 Aes128Sha1_32_CipherNotUsedWhenOnlyCalleeSupported) {
2749 PeerConnectionFactory::Options caller_options;
2750 caller_options.crypto_options.enable_aes128_sha1_32_crypto_cipher = false;
2751 PeerConnectionFactory::Options callee_options;
2752 callee_options.crypto_options.enable_aes128_sha1_32_crypto_cipher = true;
2753 int expected_cipher_suite = rtc::SRTP_AES128_CM_SHA1_80;
2754 TestNegotiatedCipherSuite(caller_options, callee_options,
2755 expected_cipher_suite);
2756}
2757
2758TEST_P(PeerConnectionIntegrationTest, Aes128Sha1_32_CipherUsedWhenSupported) {
2759 PeerConnectionFactory::Options caller_options;
2760 caller_options.crypto_options.enable_aes128_sha1_32_crypto_cipher = true;
2761 PeerConnectionFactory::Options callee_options;
2762 callee_options.crypto_options.enable_aes128_sha1_32_crypto_cipher = true;
2763 int expected_cipher_suite = rtc::SRTP_AES128_CM_SHA1_32;
2764 TestNegotiatedCipherSuite(caller_options, callee_options,
2765 expected_cipher_suite);
2766}
2767
deadbeef1dcb1642017-03-29 21:08:16 -07002768// Test that a non-GCM cipher is used if both sides only support non-GCM.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002769TEST_P(PeerConnectionIntegrationTest, NonGcmCipherUsedWhenGcmNotSupported) {
deadbeef1dcb1642017-03-29 21:08:16 -07002770 bool local_gcm_enabled = false;
2771 bool remote_gcm_enabled = false;
2772 int expected_cipher_suite = kDefaultSrtpCryptoSuite;
2773 TestGcmNegotiationUsesCipherSuite(local_gcm_enabled, remote_gcm_enabled,
2774 expected_cipher_suite);
2775}
2776
2777// Test that a GCM cipher is used if both ends support it.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002778TEST_P(PeerConnectionIntegrationTest, GcmCipherUsedWhenGcmSupported) {
deadbeef1dcb1642017-03-29 21:08:16 -07002779 bool local_gcm_enabled = true;
2780 bool remote_gcm_enabled = true;
2781 int expected_cipher_suite = kDefaultSrtpCryptoSuiteGcm;
2782 TestGcmNegotiationUsesCipherSuite(local_gcm_enabled, remote_gcm_enabled,
2783 expected_cipher_suite);
2784}
2785
2786// Test that GCM isn't used if only the offerer supports it.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002787TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07002788 NonGcmCipherUsedWhenOnlyCallerSupportsGcm) {
2789 bool local_gcm_enabled = true;
2790 bool remote_gcm_enabled = false;
2791 int expected_cipher_suite = kDefaultSrtpCryptoSuite;
2792 TestGcmNegotiationUsesCipherSuite(local_gcm_enabled, remote_gcm_enabled,
2793 expected_cipher_suite);
2794}
2795
2796// Test that GCM isn't used if only the answerer supports it.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002797TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07002798 NonGcmCipherUsedWhenOnlyCalleeSupportsGcm) {
2799 bool local_gcm_enabled = false;
2800 bool remote_gcm_enabled = true;
2801 int expected_cipher_suite = kDefaultSrtpCryptoSuite;
2802 TestGcmNegotiationUsesCipherSuite(local_gcm_enabled, remote_gcm_enabled,
2803 expected_cipher_suite);
2804}
2805
deadbeef7914b8c2017-04-21 03:23:33 -07002806// Verify that media can be transmitted end-to-end when GCM crypto suites are
2807// enabled. Note that the above tests, such as GcmCipherUsedWhenGcmSupported,
2808// only verify that a GCM cipher is negotiated, and not necessarily that SRTP
2809// works with it.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002810TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithGcmCipher) {
deadbeef7914b8c2017-04-21 03:23:33 -07002811 PeerConnectionFactory::Options gcm_options;
2812 gcm_options.crypto_options.enable_gcm_crypto_suites = true;
2813 ASSERT_TRUE(
2814 CreatePeerConnectionWrappersWithOptions(gcm_options, gcm_options));
2815 ConnectFakeSignaling();
2816 // Do normal offer/answer and wait for some frames to be received in each
2817 // direction.
Steve Anton15324772018-01-16 10:26:49 -08002818 caller()->AddAudioVideoTracks();
2819 callee()->AddAudioVideoTracks();
deadbeef7914b8c2017-04-21 03:23:33 -07002820 caller()->CreateAndSetAndSignalOffer();
2821 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002822 MediaExpectations media_expectations;
2823 media_expectations.ExpectBidirectionalAudioAndVideo();
2824 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef7914b8c2017-04-21 03:23:33 -07002825}
2826
deadbeef1dcb1642017-03-29 21:08:16 -07002827// This test sets up a call between two parties with audio, video and an RTP
2828// data channel.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002829TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithRtpDataChannel) {
deadbeef1dcb1642017-03-29 21:08:16 -07002830 FakeConstraints setup_constraints;
2831 setup_constraints.SetAllowRtpDataChannels();
2832 ASSERT_TRUE(CreatePeerConnectionWrappersWithConstraints(&setup_constraints,
2833 &setup_constraints));
2834 ConnectFakeSignaling();
2835 // Expect that data channel created on caller side will show up for callee as
2836 // well.
2837 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08002838 caller()->AddAudioVideoTracks();
2839 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002840 caller()->CreateAndSetAndSignalOffer();
2841 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2842 // Ensure the existence of the RTP data channel didn't impede audio/video.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002843 MediaExpectations media_expectations;
2844 media_expectations.ExpectBidirectionalAudioAndVideo();
2845 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002846 ASSERT_NE(nullptr, caller()->data_channel());
2847 ASSERT_NE(nullptr, callee()->data_channel());
2848 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
2849 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
2850
2851 // Ensure data can be sent in both directions.
2852 std::string data = "hello world";
2853 SendRtpDataWithRetries(caller()->data_channel(), data, 5);
2854 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
2855 kDefaultTimeout);
2856 SendRtpDataWithRetries(callee()->data_channel(), data, 5);
2857 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
2858 kDefaultTimeout);
2859}
2860
2861// Ensure that an RTP data channel is signaled as closed for the caller when
2862// the callee rejects it in a subsequent offer.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002863TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07002864 RtpDataChannelSignaledClosedInCalleeOffer) {
2865 // Same procedure as above test.
2866 FakeConstraints setup_constraints;
2867 setup_constraints.SetAllowRtpDataChannels();
2868 ASSERT_TRUE(CreatePeerConnectionWrappersWithConstraints(&setup_constraints,
2869 &setup_constraints));
2870 ConnectFakeSignaling();
2871 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08002872 caller()->AddAudioVideoTracks();
2873 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002874 caller()->CreateAndSetAndSignalOffer();
2875 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2876 ASSERT_NE(nullptr, caller()->data_channel());
2877 ASSERT_NE(nullptr, callee()->data_channel());
2878 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
2879 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
2880
2881 // Close the data channel on the callee, and do an updated offer/answer.
2882 callee()->data_channel()->Close();
2883 callee()->CreateAndSetAndSignalOffer();
2884 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2885 EXPECT_FALSE(caller()->data_observer()->IsOpen());
2886 EXPECT_FALSE(callee()->data_observer()->IsOpen());
2887}
2888
2889// Tests that data is buffered in an RTP data channel until an observer is
2890// registered for it.
2891//
2892// NOTE: RTP data channels can receive data before the underlying
2893// transport has detected that a channel is writable and thus data can be
2894// received before the data channel state changes to open. That is hard to test
2895// but the same buffering is expected to be used in that case.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002896TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07002897 DataBufferedUntilRtpDataChannelObserverRegistered) {
2898 // Use fake clock and simulated network delay so that we predictably can wait
2899 // until an SCTP message has been delivered without "sleep()"ing.
2900 rtc::ScopedFakeClock fake_clock;
2901 // Some things use a time of "0" as a special value, so we need to start out
2902 // the fake clock at a nonzero time.
2903 // TODO(deadbeef): Fix this.
2904 fake_clock.AdvanceTime(rtc::TimeDelta::FromSeconds(1));
2905 virtual_socket_server()->set_delay_mean(5); // 5 ms per hop.
2906 virtual_socket_server()->UpdateDelayDistribution();
2907
2908 FakeConstraints constraints;
2909 constraints.SetAllowRtpDataChannels();
2910 ASSERT_TRUE(
2911 CreatePeerConnectionWrappersWithConstraints(&constraints, &constraints));
2912 ConnectFakeSignaling();
2913 caller()->CreateDataChannel();
2914 caller()->CreateAndSetAndSignalOffer();
2915 ASSERT_TRUE(caller()->data_channel() != nullptr);
2916 ASSERT_TRUE_SIMULATED_WAIT(callee()->data_channel() != nullptr,
2917 kDefaultTimeout, fake_clock);
2918 ASSERT_TRUE_SIMULATED_WAIT(caller()->data_observer()->IsOpen(),
2919 kDefaultTimeout, fake_clock);
2920 ASSERT_EQ_SIMULATED_WAIT(DataChannelInterface::kOpen,
2921 callee()->data_channel()->state(), kDefaultTimeout,
2922 fake_clock);
2923
2924 // Unregister the observer which is normally automatically registered.
2925 callee()->data_channel()->UnregisterObserver();
2926 // Send data and advance fake clock until it should have been received.
2927 std::string data = "hello world";
2928 caller()->data_channel()->Send(DataBuffer(data));
2929 SIMULATED_WAIT(false, 50, fake_clock);
2930
2931 // Attach data channel and expect data to be received immediately. Note that
2932 // EXPECT_EQ_WAIT is used, such that the simulated clock is not advanced any
2933 // further, but data can be received even if the callback is asynchronous.
2934 MockDataChannelObserver new_observer(callee()->data_channel());
2935 EXPECT_EQ_SIMULATED_WAIT(data, new_observer.last_message(), kDefaultTimeout,
2936 fake_clock);
2937}
2938
2939// This test sets up a call between two parties with audio, video and but only
2940// the caller client supports RTP data channels.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002941TEST_P(PeerConnectionIntegrationTest, RtpDataChannelsRejectedByCallee) {
deadbeef1dcb1642017-03-29 21:08:16 -07002942 FakeConstraints setup_constraints_1;
2943 setup_constraints_1.SetAllowRtpDataChannels();
2944 // Must disable DTLS to make negotiation succeed.
2945 setup_constraints_1.SetMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
2946 false);
2947 FakeConstraints setup_constraints_2;
2948 setup_constraints_2.SetMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
2949 false);
2950 ASSERT_TRUE(CreatePeerConnectionWrappersWithConstraints(
2951 &setup_constraints_1, &setup_constraints_2));
2952 ConnectFakeSignaling();
2953 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08002954 caller()->AddAudioVideoTracks();
2955 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002956 caller()->CreateAndSetAndSignalOffer();
2957 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2958 // The caller should still have a data channel, but it should be closed, and
2959 // one should ever have been created for the callee.
2960 EXPECT_TRUE(caller()->data_channel() != nullptr);
2961 EXPECT_FALSE(caller()->data_observer()->IsOpen());
2962 EXPECT_EQ(nullptr, callee()->data_channel());
2963}
2964
2965// This test sets up a call between two parties with audio, and video. When
2966// audio and video is setup and flowing, an RTP data channel is negotiated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002967TEST_P(PeerConnectionIntegrationTest, AddRtpDataChannelInSubsequentOffer) {
deadbeef1dcb1642017-03-29 21:08:16 -07002968 FakeConstraints setup_constraints;
2969 setup_constraints.SetAllowRtpDataChannels();
2970 ASSERT_TRUE(CreatePeerConnectionWrappersWithConstraints(&setup_constraints,
2971 &setup_constraints));
2972 ConnectFakeSignaling();
2973 // Do initial offer/answer with audio/video.
Steve Anton15324772018-01-16 10:26:49 -08002974 caller()->AddAudioVideoTracks();
2975 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002976 caller()->CreateAndSetAndSignalOffer();
2977 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2978 // Create data channel and do new offer and answer.
2979 caller()->CreateDataChannel();
2980 caller()->CreateAndSetAndSignalOffer();
2981 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2982 ASSERT_NE(nullptr, caller()->data_channel());
2983 ASSERT_NE(nullptr, callee()->data_channel());
2984 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
2985 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
2986 // Ensure data can be sent in both directions.
2987 std::string data = "hello world";
2988 SendRtpDataWithRetries(caller()->data_channel(), data, 5);
2989 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
2990 kDefaultTimeout);
2991 SendRtpDataWithRetries(callee()->data_channel(), data, 5);
2992 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
2993 kDefaultTimeout);
2994}
2995
2996#ifdef HAVE_SCTP
2997
2998// This test sets up a call between two parties with audio, video and an SCTP
2999// data channel.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003000TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithSctpDataChannel) {
deadbeef1dcb1642017-03-29 21:08:16 -07003001 ASSERT_TRUE(CreatePeerConnectionWrappers());
3002 ConnectFakeSignaling();
3003 // Expect that data channel created on caller side will show up for callee as
3004 // well.
3005 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08003006 caller()->AddAudioVideoTracks();
3007 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003008 caller()->CreateAndSetAndSignalOffer();
3009 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3010 // Ensure the existence of the SCTP data channel didn't impede audio/video.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003011 MediaExpectations media_expectations;
3012 media_expectations.ExpectBidirectionalAudioAndVideo();
3013 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003014 // Caller data channel should already exist (it created one). Callee data
3015 // channel may not exist yet, since negotiation happens in-band, not in SDP.
3016 ASSERT_NE(nullptr, caller()->data_channel());
3017 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3018 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3019 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3020
3021 // Ensure data can be sent in both directions.
3022 std::string data = "hello world";
3023 caller()->data_channel()->Send(DataBuffer(data));
3024 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3025 kDefaultTimeout);
3026 callee()->data_channel()->Send(DataBuffer(data));
3027 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3028 kDefaultTimeout);
3029}
3030
3031// Ensure that when the callee closes an SCTP data channel, the closing
3032// procedure results in the data channel being closed for the caller as well.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003033TEST_P(PeerConnectionIntegrationTest, CalleeClosesSctpDataChannel) {
deadbeef1dcb1642017-03-29 21:08:16 -07003034 // Same procedure as above test.
3035 ASSERT_TRUE(CreatePeerConnectionWrappers());
3036 ConnectFakeSignaling();
3037 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08003038 caller()->AddAudioVideoTracks();
3039 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003040 caller()->CreateAndSetAndSignalOffer();
3041 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3042 ASSERT_NE(nullptr, caller()->data_channel());
3043 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3044 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3045 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3046
3047 // Close the data channel on the callee side, and wait for it to reach the
3048 // "closed" state on both sides.
3049 callee()->data_channel()->Close();
3050 EXPECT_TRUE_WAIT(!caller()->data_observer()->IsOpen(), kDefaultTimeout);
3051 EXPECT_TRUE_WAIT(!callee()->data_observer()->IsOpen(), kDefaultTimeout);
3052}
3053
Seth Hampson2f0d7022018-02-20 11:54:42 -08003054TEST_P(PeerConnectionIntegrationTest, SctpDataChannelConfigSentToOtherSide) {
Steve Antonda6c0952017-10-23 11:41:54 -07003055 ASSERT_TRUE(CreatePeerConnectionWrappers());
3056 ConnectFakeSignaling();
3057 webrtc::DataChannelInit init;
3058 init.id = 53;
3059 init.maxRetransmits = 52;
3060 caller()->CreateDataChannel("data-channel", &init);
Steve Anton15324772018-01-16 10:26:49 -08003061 caller()->AddAudioVideoTracks();
3062 callee()->AddAudioVideoTracks();
Steve Antonda6c0952017-10-23 11:41:54 -07003063 caller()->CreateAndSetAndSignalOffer();
3064 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Steve Anton074dece2017-10-24 13:04:12 -07003065 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3066 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
Steve Antonda6c0952017-10-23 11:41:54 -07003067 EXPECT_EQ(init.id, callee()->data_channel()->id());
3068 EXPECT_EQ("data-channel", callee()->data_channel()->label());
3069 EXPECT_EQ(init.maxRetransmits, callee()->data_channel()->maxRetransmits());
3070 EXPECT_FALSE(callee()->data_channel()->negotiated());
3071}
3072
deadbeef1dcb1642017-03-29 21:08:16 -07003073// Test usrsctp's ability to process unordered data stream, where data actually
3074// arrives out of order using simulated delays. Previously there have been some
3075// bugs in this area.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003076TEST_P(PeerConnectionIntegrationTest, StressTestUnorderedSctpDataChannel) {
deadbeef1dcb1642017-03-29 21:08:16 -07003077 // Introduce random network delays.
3078 // Otherwise it's not a true "unordered" test.
3079 virtual_socket_server()->set_delay_mean(20);
3080 virtual_socket_server()->set_delay_stddev(5);
3081 virtual_socket_server()->UpdateDelayDistribution();
3082 // Normal procedure, but with unordered data channel config.
3083 ASSERT_TRUE(CreatePeerConnectionWrappers());
3084 ConnectFakeSignaling();
3085 webrtc::DataChannelInit init;
3086 init.ordered = false;
3087 caller()->CreateDataChannel(&init);
3088 caller()->CreateAndSetAndSignalOffer();
3089 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3090 ASSERT_NE(nullptr, caller()->data_channel());
3091 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3092 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3093 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3094
3095 static constexpr int kNumMessages = 100;
3096 // Deliberately chosen to be larger than the MTU so messages get fragmented.
3097 static constexpr size_t kMaxMessageSize = 4096;
3098 // Create and send random messages.
3099 std::vector<std::string> sent_messages;
3100 for (int i = 0; i < kNumMessages; ++i) {
3101 size_t length =
3102 (rand() % kMaxMessageSize) + 1; // NOLINT (rand_r instead of rand)
3103 std::string message;
3104 ASSERT_TRUE(rtc::CreateRandomString(length, &message));
3105 caller()->data_channel()->Send(DataBuffer(message));
3106 callee()->data_channel()->Send(DataBuffer(message));
3107 sent_messages.push_back(message);
3108 }
3109
3110 // Wait for all messages to be received.
3111 EXPECT_EQ_WAIT(kNumMessages,
3112 caller()->data_observer()->received_message_count(),
3113 kDefaultTimeout);
3114 EXPECT_EQ_WAIT(kNumMessages,
3115 callee()->data_observer()->received_message_count(),
3116 kDefaultTimeout);
3117
3118 // Sort and compare to make sure none of the messages were corrupted.
3119 std::vector<std::string> caller_received_messages =
3120 caller()->data_observer()->messages();
3121 std::vector<std::string> callee_received_messages =
3122 callee()->data_observer()->messages();
3123 std::sort(sent_messages.begin(), sent_messages.end());
3124 std::sort(caller_received_messages.begin(), caller_received_messages.end());
3125 std::sort(callee_received_messages.begin(), callee_received_messages.end());
3126 EXPECT_EQ(sent_messages, caller_received_messages);
3127 EXPECT_EQ(sent_messages, callee_received_messages);
3128}
3129
3130// This test sets up a call between two parties with audio, and video. When
3131// audio and video are setup and flowing, an SCTP data channel is negotiated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003132TEST_P(PeerConnectionIntegrationTest, AddSctpDataChannelInSubsequentOffer) {
deadbeef1dcb1642017-03-29 21:08:16 -07003133 ASSERT_TRUE(CreatePeerConnectionWrappers());
3134 ConnectFakeSignaling();
3135 // Do initial offer/answer with audio/video.
Steve Anton15324772018-01-16 10:26:49 -08003136 caller()->AddAudioVideoTracks();
3137 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003138 caller()->CreateAndSetAndSignalOffer();
3139 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3140 // Create data channel and do new offer and answer.
3141 caller()->CreateDataChannel();
3142 caller()->CreateAndSetAndSignalOffer();
3143 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3144 // Caller data channel should already exist (it created one). Callee data
3145 // channel may not exist yet, since negotiation happens in-band, not in SDP.
3146 ASSERT_NE(nullptr, caller()->data_channel());
3147 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3148 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3149 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3150 // Ensure data can be sent in both directions.
3151 std::string data = "hello world";
3152 caller()->data_channel()->Send(DataBuffer(data));
3153 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3154 kDefaultTimeout);
3155 callee()->data_channel()->Send(DataBuffer(data));
3156 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3157 kDefaultTimeout);
3158}
3159
deadbeef7914b8c2017-04-21 03:23:33 -07003160// Set up a connection initially just using SCTP data channels, later upgrading
3161// to audio/video, ensuring frames are received end-to-end. Effectively the
3162// inverse of the test above.
3163// This was broken in M57; see https://crbug.com/711243
Seth Hampson2f0d7022018-02-20 11:54:42 -08003164TEST_P(PeerConnectionIntegrationTest, SctpDataChannelToAudioVideoUpgrade) {
deadbeef7914b8c2017-04-21 03:23:33 -07003165 ASSERT_TRUE(CreatePeerConnectionWrappers());
3166 ConnectFakeSignaling();
3167 // Do initial offer/answer with just data channel.
3168 caller()->CreateDataChannel();
3169 caller()->CreateAndSetAndSignalOffer();
3170 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3171 // Wait until data can be sent over the data channel.
3172 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3173 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3174 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3175
3176 // Do subsequent offer/answer with two-way audio and video. Audio and video
3177 // should end up bundled on the DTLS/ICE transport already used for data.
Steve Anton15324772018-01-16 10:26:49 -08003178 caller()->AddAudioVideoTracks();
3179 callee()->AddAudioVideoTracks();
deadbeef7914b8c2017-04-21 03:23:33 -07003180 caller()->CreateAndSetAndSignalOffer();
3181 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08003182 MediaExpectations media_expectations;
3183 media_expectations.ExpectBidirectionalAudioAndVideo();
3184 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef7914b8c2017-04-21 03:23:33 -07003185}
3186
deadbeef8b7e9ad2017-05-25 09:38:55 -07003187static void MakeSpecCompliantSctpOffer(cricket::SessionDescription* desc) {
deadbeef8b7e9ad2017-05-25 09:38:55 -07003188 cricket::DataContentDescription* dcd_offer =
Steve Antonb1c1de12017-12-21 15:14:30 -08003189 GetFirstDataContentDescription(desc);
3190 ASSERT_TRUE(dcd_offer);
deadbeef8b7e9ad2017-05-25 09:38:55 -07003191 dcd_offer->set_use_sctpmap(false);
3192 dcd_offer->set_protocol("UDP/DTLS/SCTP");
3193}
3194
3195// Test that the data channel works when a spec-compliant SCTP m= section is
3196// offered (using "a=sctp-port" instead of "a=sctpmap", and using
3197// "UDP/DTLS/SCTP" as the protocol).
Seth Hampson2f0d7022018-02-20 11:54:42 -08003198TEST_P(PeerConnectionIntegrationTest,
deadbeef8b7e9ad2017-05-25 09:38:55 -07003199 DataChannelWorksWhenSpecCompliantSctpOfferReceived) {
3200 ASSERT_TRUE(CreatePeerConnectionWrappers());
3201 ConnectFakeSignaling();
3202 caller()->CreateDataChannel();
3203 caller()->SetGeneratedSdpMunger(MakeSpecCompliantSctpOffer);
3204 caller()->CreateAndSetAndSignalOffer();
3205 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3206 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3207 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3208 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3209
3210 // Ensure data can be sent in both directions.
3211 std::string data = "hello world";
3212 caller()->data_channel()->Send(DataBuffer(data));
3213 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3214 kDefaultTimeout);
3215 callee()->data_channel()->Send(DataBuffer(data));
3216 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3217 kDefaultTimeout);
3218}
3219
deadbeef1dcb1642017-03-29 21:08:16 -07003220#endif // HAVE_SCTP
3221
3222// Test that the ICE connection and gathering states eventually reach
3223// "complete".
Seth Hampson2f0d7022018-02-20 11:54:42 -08003224TEST_P(PeerConnectionIntegrationTest, IceStatesReachCompletion) {
deadbeef1dcb1642017-03-29 21:08:16 -07003225 ASSERT_TRUE(CreatePeerConnectionWrappers());
3226 ConnectFakeSignaling();
3227 // Do normal offer/answer.
Steve Anton15324772018-01-16 10:26:49 -08003228 caller()->AddAudioVideoTracks();
3229 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003230 caller()->CreateAndSetAndSignalOffer();
3231 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3232 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceGatheringComplete,
3233 caller()->ice_gathering_state(), kMaxWaitForFramesMs);
3234 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceGatheringComplete,
3235 callee()->ice_gathering_state(), kMaxWaitForFramesMs);
3236 // After the best candidate pair is selected and all candidates are signaled,
3237 // the ICE connection state should reach "complete".
3238 // TODO(deadbeef): Currently, the ICE "controlled" agent (the
3239 // answerer/"callee" by default) only reaches "connected". When this is
3240 // fixed, this test should be updated.
3241 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
3242 caller()->ice_connection_state(), kDefaultTimeout);
3243 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
3244 callee()->ice_connection_state(), kDefaultTimeout);
3245}
3246
Steve Antonede9ca52017-10-16 13:04:27 -07003247// Test that firewalling the ICE connection causes the clients to identify the
3248// disconnected state and then removing the firewall causes them to reconnect.
3249class PeerConnectionIntegrationIceStatesTest
Seth Hampson2f0d7022018-02-20 11:54:42 -08003250 : public PeerConnectionIntegrationBaseTest,
3251 public ::testing::WithParamInterface<
3252 std::tuple<SdpSemantics, std::tuple<std::string, uint32_t>>> {
Steve Antonede9ca52017-10-16 13:04:27 -07003253 protected:
Seth Hampson2f0d7022018-02-20 11:54:42 -08003254 PeerConnectionIntegrationIceStatesTest()
3255 : PeerConnectionIntegrationBaseTest(std::get<0>(GetParam())) {
3256 port_allocator_flags_ = std::get<1>(std::get<1>(GetParam()));
Steve Antonede9ca52017-10-16 13:04:27 -07003257 }
3258
3259 void StartStunServer(const SocketAddress& server_address) {
3260 stun_server_.reset(
3261 cricket::TestStunServer::Create(network_thread(), server_address));
3262 }
3263
3264 bool TestIPv6() {
3265 return (port_allocator_flags_ & cricket::PORTALLOCATOR_ENABLE_IPV6);
3266 }
3267
3268 void SetPortAllocatorFlags() {
Qingsi Wanga2d60672018-04-11 16:57:45 -07003269 network_thread()->Invoke<void>(
3270 RTC_FROM_HERE,
3271 rtc::Bind(&cricket::PortAllocator::set_flags,
3272 caller()->port_allocator(), port_allocator_flags_));
3273 network_thread()->Invoke<void>(
3274 RTC_FROM_HERE,
3275 rtc::Bind(&cricket::PortAllocator::set_flags,
3276 callee()->port_allocator(), port_allocator_flags_));
Steve Antonede9ca52017-10-16 13:04:27 -07003277 }
3278
3279 std::vector<SocketAddress> CallerAddresses() {
3280 std::vector<SocketAddress> addresses;
3281 addresses.push_back(SocketAddress("1.1.1.1", 0));
3282 if (TestIPv6()) {
3283 addresses.push_back(SocketAddress("1111:0:a:b:c:d:e:f", 0));
3284 }
3285 return addresses;
3286 }
3287
3288 std::vector<SocketAddress> CalleeAddresses() {
3289 std::vector<SocketAddress> addresses;
3290 addresses.push_back(SocketAddress("2.2.2.2", 0));
3291 if (TestIPv6()) {
3292 addresses.push_back(SocketAddress("2222:0:a:b:c:d:e:f", 0));
3293 }
3294 return addresses;
3295 }
3296
3297 void SetUpNetworkInterfaces() {
3298 // Remove the default interfaces added by the test infrastructure.
3299 caller()->network()->RemoveInterface(kDefaultLocalAddress);
3300 callee()->network()->RemoveInterface(kDefaultLocalAddress);
3301
3302 // Add network addresses for test.
3303 for (const auto& caller_address : CallerAddresses()) {
3304 caller()->network()->AddInterface(caller_address);
3305 }
3306 for (const auto& callee_address : CalleeAddresses()) {
3307 callee()->network()->AddInterface(callee_address);
3308 }
3309 }
3310
3311 private:
3312 uint32_t port_allocator_flags_;
3313 std::unique_ptr<cricket::TestStunServer> stun_server_;
3314};
3315
3316// Tests that the PeerConnection goes through all the ICE gathering/connection
3317// states over the duration of the call. This includes Disconnected and Failed
3318// states, induced by putting a firewall between the peers and waiting for them
3319// to time out.
Steve Anton83119dd2017-11-10 16:19:52 -08003320TEST_P(PeerConnectionIntegrationIceStatesTest, VerifyIceStates) {
3321 // TODO(bugs.webrtc.org/8295): When using a ScopedFakeClock, this test will
3322 // sometimes hit a DCHECK in platform_thread.cc about the PacerThread being
3323 // too busy. For now, revert to running without a fake clock.
Steve Antonede9ca52017-10-16 13:04:27 -07003324
3325 const SocketAddress kStunServerAddress =
3326 SocketAddress("99.99.99.1", cricket::STUN_SERVER_PORT);
3327 StartStunServer(kStunServerAddress);
3328
3329 PeerConnectionInterface::RTCConfiguration config;
3330 PeerConnectionInterface::IceServer ice_stun_server;
3331 ice_stun_server.urls.push_back(
3332 "stun:" + kStunServerAddress.HostAsURIString() + ":" +
3333 kStunServerAddress.PortAsString());
3334 config.servers.push_back(ice_stun_server);
3335
3336 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
3337 ConnectFakeSignaling();
3338 SetPortAllocatorFlags();
3339 SetUpNetworkInterfaces();
Steve Anton15324772018-01-16 10:26:49 -08003340 caller()->AddAudioVideoTracks();
3341 callee()->AddAudioVideoTracks();
Steve Antonede9ca52017-10-16 13:04:27 -07003342
3343 // Initial state before anything happens.
3344 ASSERT_EQ(PeerConnectionInterface::kIceGatheringNew,
3345 caller()->ice_gathering_state());
3346 ASSERT_EQ(PeerConnectionInterface::kIceConnectionNew,
3347 caller()->ice_connection_state());
3348
3349 // Start the call by creating the offer, setting it as the local description,
3350 // then sending it to the peer who will respond with an answer. This happens
3351 // asynchronously so that we can watch the states as it runs in the
3352 // background.
3353 caller()->CreateAndSetAndSignalOffer();
3354
Steve Anton83119dd2017-11-10 16:19:52 -08003355 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceConnectionCompleted,
3356 caller()->ice_connection_state(), kDefaultTimeout);
Steve Antonede9ca52017-10-16 13:04:27 -07003357
3358 // Verify that the observer was notified of the intermediate transitions.
3359 EXPECT_THAT(caller()->ice_connection_state_history(),
3360 ElementsAre(PeerConnectionInterface::kIceConnectionChecking,
3361 PeerConnectionInterface::kIceConnectionConnected,
3362 PeerConnectionInterface::kIceConnectionCompleted));
3363 EXPECT_THAT(caller()->ice_gathering_state_history(),
3364 ElementsAre(PeerConnectionInterface::kIceGatheringGathering,
3365 PeerConnectionInterface::kIceGatheringComplete));
3366
3367 // Block connections to/from the caller and wait for ICE to become
3368 // disconnected.
3369 for (const auto& caller_address : CallerAddresses()) {
3370 firewall()->AddRule(false, rtc::FP_ANY, rtc::FD_ANY, caller_address);
3371 }
Mirko Bonadei675513b2017-11-09 11:09:25 +01003372 RTC_LOG(LS_INFO) << "Firewall rules applied";
Steve Anton83119dd2017-11-10 16:19:52 -08003373 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceConnectionDisconnected,
3374 caller()->ice_connection_state(), kDefaultTimeout);
Steve Antonede9ca52017-10-16 13:04:27 -07003375
3376 // Let ICE re-establish by removing the firewall rules.
3377 firewall()->ClearRules();
Mirko Bonadei675513b2017-11-09 11:09:25 +01003378 RTC_LOG(LS_INFO) << "Firewall rules cleared";
Steve Anton83119dd2017-11-10 16:19:52 -08003379 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceConnectionCompleted,
3380 caller()->ice_connection_state(), kDefaultTimeout);
Steve Antonede9ca52017-10-16 13:04:27 -07003381
3382 // According to RFC7675, if there is no response within 30 seconds then the
3383 // peer should consider the other side to have rejected the connection. This
Steve Anton83119dd2017-11-10 16:19:52 -08003384 // is signaled by the state transitioning to "failed".
Steve Antonede9ca52017-10-16 13:04:27 -07003385 constexpr int kConsentTimeout = 30000;
3386 for (const auto& caller_address : CallerAddresses()) {
3387 firewall()->AddRule(false, rtc::FP_ANY, rtc::FD_ANY, caller_address);
3388 }
Mirko Bonadei675513b2017-11-09 11:09:25 +01003389 RTC_LOG(LS_INFO) << "Firewall rules applied again";
Steve Anton83119dd2017-11-10 16:19:52 -08003390 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceConnectionFailed,
3391 caller()->ice_connection_state(), kConsentTimeout);
Steve Antonede9ca52017-10-16 13:04:27 -07003392}
3393
3394// Tests that the best connection is set to the appropriate IPv4/IPv6 connection
3395// and that the statistics in the metric observers are updated correctly.
3396TEST_P(PeerConnectionIntegrationIceStatesTest, VerifyBestConnection) {
3397 ASSERT_TRUE(CreatePeerConnectionWrappers());
3398 ConnectFakeSignaling();
3399 SetPortAllocatorFlags();
3400 SetUpNetworkInterfaces();
Steve Anton15324772018-01-16 10:26:49 -08003401 caller()->AddAudioVideoTracks();
3402 callee()->AddAudioVideoTracks();
Steve Antonede9ca52017-10-16 13:04:27 -07003403
3404 rtc::scoped_refptr<webrtc::FakeMetricsObserver> metrics_observer(
3405 new rtc::RefCountedObject<webrtc::FakeMetricsObserver>());
3406 caller()->pc()->RegisterUMAObserver(metrics_observer.get());
3407
3408 caller()->CreateAndSetAndSignalOffer();
3409
3410 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3411
3412 const int num_best_ipv4 = metrics_observer->GetEnumCounter(
3413 webrtc::kEnumCounterAddressFamily, webrtc::kBestConnections_IPv4);
3414 const int num_best_ipv6 = metrics_observer->GetEnumCounter(
3415 webrtc::kEnumCounterAddressFamily, webrtc::kBestConnections_IPv6);
3416 if (TestIPv6()) {
3417 // When IPv6 is enabled, we should prefer an IPv6 connection over an IPv4
3418 // connection.
3419 EXPECT_EQ(0u, num_best_ipv4);
3420 EXPECT_EQ(1u, num_best_ipv6);
3421 } else {
3422 EXPECT_EQ(1u, num_best_ipv4);
3423 EXPECT_EQ(0u, num_best_ipv6);
3424 }
3425
3426 EXPECT_EQ(0u, metrics_observer->GetEnumCounter(
3427 webrtc::kEnumCounterIceCandidatePairTypeUdp,
3428 webrtc::kIceCandidatePairHostHost));
3429 EXPECT_EQ(1u, metrics_observer->GetEnumCounter(
3430 webrtc::kEnumCounterIceCandidatePairTypeUdp,
3431 webrtc::kIceCandidatePairHostPublicHostPublic));
3432}
3433
3434constexpr uint32_t kFlagsIPv4NoStun = cricket::PORTALLOCATOR_DISABLE_TCP |
3435 cricket::PORTALLOCATOR_DISABLE_STUN |
3436 cricket::PORTALLOCATOR_DISABLE_RELAY;
3437constexpr uint32_t kFlagsIPv6NoStun =
3438 cricket::PORTALLOCATOR_DISABLE_TCP | cricket::PORTALLOCATOR_DISABLE_STUN |
3439 cricket::PORTALLOCATOR_ENABLE_IPV6 | cricket::PORTALLOCATOR_DISABLE_RELAY;
3440constexpr uint32_t kFlagsIPv4Stun =
3441 cricket::PORTALLOCATOR_DISABLE_TCP | cricket::PORTALLOCATOR_DISABLE_RELAY;
3442
Seth Hampson2f0d7022018-02-20 11:54:42 -08003443INSTANTIATE_TEST_CASE_P(
3444 PeerConnectionIntegrationTest,
3445 PeerConnectionIntegrationIceStatesTest,
3446 Combine(Values(SdpSemantics::kPlanB, SdpSemantics::kUnifiedPlan),
3447 Values(std::make_pair("IPv4 no STUN", kFlagsIPv4NoStun),
3448 std::make_pair("IPv6 no STUN", kFlagsIPv6NoStun),
3449 std::make_pair("IPv4 with STUN", kFlagsIPv4Stun))));
Steve Antonede9ca52017-10-16 13:04:27 -07003450
deadbeef1dcb1642017-03-29 21:08:16 -07003451// This test sets up a call between two parties with audio and video.
3452// During the call, the caller restarts ICE and the test verifies that
3453// new ICE candidates are generated and audio and video still can flow, and the
3454// ICE state reaches completed again.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003455TEST_P(PeerConnectionIntegrationTest, MediaContinuesFlowingAfterIceRestart) {
deadbeef1dcb1642017-03-29 21:08:16 -07003456 ASSERT_TRUE(CreatePeerConnectionWrappers());
3457 ConnectFakeSignaling();
3458 // Do normal offer/answer and wait for ICE to complete.
Steve Anton15324772018-01-16 10:26:49 -08003459 caller()->AddAudioVideoTracks();
3460 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003461 caller()->CreateAndSetAndSignalOffer();
3462 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3463 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
3464 caller()->ice_connection_state(), kMaxWaitForFramesMs);
3465 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
3466 callee()->ice_connection_state(), kMaxWaitForFramesMs);
3467
3468 // To verify that the ICE restart actually occurs, get
3469 // ufrag/password/candidates before and after restart.
3470 // Create an SDP string of the first audio candidate for both clients.
3471 const webrtc::IceCandidateCollection* audio_candidates_caller =
3472 caller()->pc()->local_description()->candidates(0);
3473 const webrtc::IceCandidateCollection* audio_candidates_callee =
3474 callee()->pc()->local_description()->candidates(0);
3475 ASSERT_GT(audio_candidates_caller->count(), 0u);
3476 ASSERT_GT(audio_candidates_callee->count(), 0u);
3477 std::string caller_candidate_pre_restart;
3478 ASSERT_TRUE(
3479 audio_candidates_caller->at(0)->ToString(&caller_candidate_pre_restart));
3480 std::string callee_candidate_pre_restart;
3481 ASSERT_TRUE(
3482 audio_candidates_callee->at(0)->ToString(&callee_candidate_pre_restart));
3483 const cricket::SessionDescription* desc =
3484 caller()->pc()->local_description()->description();
3485 std::string caller_ufrag_pre_restart =
3486 desc->transport_infos()[0].description.ice_ufrag;
3487 desc = callee()->pc()->local_description()->description();
3488 std::string callee_ufrag_pre_restart =
3489 desc->transport_infos()[0].description.ice_ufrag;
3490
3491 // Have the caller initiate an ICE restart.
3492 caller()->SetOfferAnswerOptions(IceRestartOfferAnswerOptions());
3493 caller()->CreateAndSetAndSignalOffer();
3494 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3495 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
3496 caller()->ice_connection_state(), kMaxWaitForFramesMs);
3497 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
3498 callee()->ice_connection_state(), kMaxWaitForFramesMs);
3499
3500 // Grab the ufrags/candidates again.
3501 audio_candidates_caller = caller()->pc()->local_description()->candidates(0);
3502 audio_candidates_callee = callee()->pc()->local_description()->candidates(0);
3503 ASSERT_GT(audio_candidates_caller->count(), 0u);
3504 ASSERT_GT(audio_candidates_callee->count(), 0u);
3505 std::string caller_candidate_post_restart;
3506 ASSERT_TRUE(
3507 audio_candidates_caller->at(0)->ToString(&caller_candidate_post_restart));
3508 std::string callee_candidate_post_restart;
3509 ASSERT_TRUE(
3510 audio_candidates_callee->at(0)->ToString(&callee_candidate_post_restart));
3511 desc = caller()->pc()->local_description()->description();
3512 std::string caller_ufrag_post_restart =
3513 desc->transport_infos()[0].description.ice_ufrag;
3514 desc = callee()->pc()->local_description()->description();
3515 std::string callee_ufrag_post_restart =
3516 desc->transport_infos()[0].description.ice_ufrag;
3517 // Sanity check that an ICE restart was actually negotiated in SDP.
3518 ASSERT_NE(caller_candidate_pre_restart, caller_candidate_post_restart);
3519 ASSERT_NE(callee_candidate_pre_restart, callee_candidate_post_restart);
3520 ASSERT_NE(caller_ufrag_pre_restart, caller_ufrag_post_restart);
3521 ASSERT_NE(callee_ufrag_pre_restart, callee_ufrag_post_restart);
3522
3523 // Ensure that additional frames are received after the ICE restart.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003524 MediaExpectations media_expectations;
3525 media_expectations.ExpectBidirectionalAudioAndVideo();
3526 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003527}
3528
3529// Verify that audio/video can be received end-to-end when ICE renomination is
3530// enabled.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003531TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithIceRenomination) {
deadbeef1dcb1642017-03-29 21:08:16 -07003532 PeerConnectionInterface::RTCConfiguration config;
3533 config.enable_ice_renomination = true;
3534 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
3535 ConnectFakeSignaling();
3536 // Do normal offer/answer and wait for some frames to be received in each
3537 // direction.
Steve Anton15324772018-01-16 10:26:49 -08003538 caller()->AddAudioVideoTracks();
3539 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003540 caller()->CreateAndSetAndSignalOffer();
3541 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3542 // Sanity check that ICE renomination was actually negotiated.
3543 const cricket::SessionDescription* desc =
3544 caller()->pc()->local_description()->description();
3545 for (const cricket::TransportInfo& info : desc->transport_infos()) {
deadbeef30952b42017-04-21 02:41:29 -07003546 ASSERT_NE(
3547 info.description.transport_options.end(),
3548 std::find(info.description.transport_options.begin(),
3549 info.description.transport_options.end(), "renomination"));
deadbeef1dcb1642017-03-29 21:08:16 -07003550 }
3551 desc = callee()->pc()->local_description()->description();
3552 for (const cricket::TransportInfo& info : desc->transport_infos()) {
deadbeef30952b42017-04-21 02:41:29 -07003553 ASSERT_NE(
3554 info.description.transport_options.end(),
3555 std::find(info.description.transport_options.begin(),
3556 info.description.transport_options.end(), "renomination"));
deadbeef1dcb1642017-03-29 21:08:16 -07003557 }
Seth Hampson2f0d7022018-02-20 11:54:42 -08003558 MediaExpectations media_expectations;
3559 media_expectations.ExpectBidirectionalAudioAndVideo();
3560 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003561}
3562
Steve Anton6f25b092017-10-23 09:39:20 -07003563// With a max bundle policy and RTCP muxing, adding a new media description to
3564// the connection should not affect ICE at all because the new media will use
3565// the existing connection.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003566TEST_P(PeerConnectionIntegrationTest,
Steve Anton83119dd2017-11-10 16:19:52 -08003567 AddMediaToConnectedBundleDoesNotRestartIce) {
Steve Anton6f25b092017-10-23 09:39:20 -07003568 PeerConnectionInterface::RTCConfiguration config;
3569 config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
3570 config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyRequire;
3571 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(
3572 config, PeerConnectionInterface::RTCConfiguration()));
3573 ConnectFakeSignaling();
3574
Steve Anton15324772018-01-16 10:26:49 -08003575 caller()->AddAudioTrack();
Steve Anton6f25b092017-10-23 09:39:20 -07003576 caller()->CreateAndSetAndSignalOffer();
3577 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Steve Antonff52f1b2017-10-26 12:24:50 -07003578 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceConnectionCompleted,
3579 caller()->ice_connection_state(), kDefaultTimeout);
Steve Anton6f25b092017-10-23 09:39:20 -07003580
3581 caller()->clear_ice_connection_state_history();
3582
Steve Anton15324772018-01-16 10:26:49 -08003583 caller()->AddVideoTrack();
Steve Anton6f25b092017-10-23 09:39:20 -07003584 caller()->CreateAndSetAndSignalOffer();
3585 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3586
3587 EXPECT_EQ(0u, caller()->ice_connection_state_history().size());
3588}
3589
deadbeef1dcb1642017-03-29 21:08:16 -07003590// This test sets up a call between two parties with audio and video. It then
3591// renegotiates setting the video m-line to "port 0", then later renegotiates
3592// again, enabling video.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003593TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07003594 VideoFlowsAfterMediaSectionIsRejectedAndRecycled) {
3595 ASSERT_TRUE(CreatePeerConnectionWrappers());
3596 ConnectFakeSignaling();
3597
3598 // Do initial negotiation, only sending media from the caller. Will result in
3599 // video and audio recvonly "m=" sections.
Steve Anton15324772018-01-16 10:26:49 -08003600 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003601 caller()->CreateAndSetAndSignalOffer();
3602 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3603
3604 // Negotiate again, disabling the video "m=" section (the callee will set the
3605 // port to 0 due to offer_to_receive_video = 0).
Seth Hampson2f0d7022018-02-20 11:54:42 -08003606 if (sdp_semantics_ == SdpSemantics::kPlanB) {
3607 PeerConnectionInterface::RTCOfferAnswerOptions options;
3608 options.offer_to_receive_video = 0;
3609 callee()->SetOfferAnswerOptions(options);
3610 } else {
3611 callee()->SetRemoteOfferHandler([this] {
3612 callee()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)->Stop();
3613 });
3614 }
deadbeef1dcb1642017-03-29 21:08:16 -07003615 caller()->CreateAndSetAndSignalOffer();
3616 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3617 // Sanity check that video "m=" section was actually rejected.
3618 const ContentInfo* answer_video_content = cricket::GetFirstVideoContent(
3619 callee()->pc()->local_description()->description());
3620 ASSERT_NE(nullptr, answer_video_content);
3621 ASSERT_TRUE(answer_video_content->rejected);
3622
3623 // Enable video and do negotiation again, making sure video is received
3624 // end-to-end, also adding media stream to callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003625 if (sdp_semantics_ == SdpSemantics::kPlanB) {
3626 PeerConnectionInterface::RTCOfferAnswerOptions options;
3627 options.offer_to_receive_video = 1;
3628 callee()->SetOfferAnswerOptions(options);
3629 } else {
3630 // The caller's transceiver is stopped, so we need to add another track.
3631 auto caller_transceiver =
3632 caller()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO);
3633 EXPECT_TRUE(caller_transceiver->stopped());
3634 caller()->AddVideoTrack();
3635 }
3636 callee()->AddVideoTrack();
3637 callee()->SetRemoteOfferHandler(nullptr);
deadbeef1dcb1642017-03-29 21:08:16 -07003638 caller()->CreateAndSetAndSignalOffer();
3639 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08003640
deadbeef1dcb1642017-03-29 21:08:16 -07003641 // Verify the caller receives frames from the newly added stream, and the
3642 // callee receives additional frames from the re-enabled video m= section.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003643 MediaExpectations media_expectations;
3644 media_expectations.CalleeExpectsSomeAudio();
3645 media_expectations.ExpectBidirectionalVideo();
3646 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003647}
3648
3649// This test sets up a Jsep call between two parties with external
3650// VideoDecoderFactory.
3651// TODO(holmer): Disabled due to sometimes crashing on buildbots.
3652// See issue webrtc/2378.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003653TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07003654 DISABLED_EndToEndCallWithVideoDecoderFactory) {
3655 ASSERT_TRUE(CreatePeerConnectionWrappers());
3656 EnableVideoDecoderFactory();
3657 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08003658 caller()->AddAudioVideoTracks();
3659 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003660 caller()->CreateAndSetAndSignalOffer();
3661 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08003662 MediaExpectations media_expectations;
3663 media_expectations.ExpectBidirectionalAudioAndVideo();
3664 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003665}
3666
3667// This tests that if we negotiate after calling CreateSender but before we
3668// have a track, then set a track later, frames from the newly-set track are
3669// received end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003670TEST_F(PeerConnectionIntegrationTestPlanB,
deadbeef1dcb1642017-03-29 21:08:16 -07003671 MediaFlowsAfterEarlyWarmupWithCreateSender) {
3672 ASSERT_TRUE(CreatePeerConnectionWrappers());
3673 ConnectFakeSignaling();
3674 auto caller_audio_sender =
3675 caller()->pc()->CreateSender("audio", "caller_stream");
3676 auto caller_video_sender =
3677 caller()->pc()->CreateSender("video", "caller_stream");
3678 auto callee_audio_sender =
3679 callee()->pc()->CreateSender("audio", "callee_stream");
3680 auto callee_video_sender =
3681 callee()->pc()->CreateSender("video", "callee_stream");
3682 caller()->CreateAndSetAndSignalOffer();
3683 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
3684 // Wait for ICE to complete, without any tracks being set.
3685 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
3686 caller()->ice_connection_state(), kMaxWaitForFramesMs);
3687 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
3688 callee()->ice_connection_state(), kMaxWaitForFramesMs);
3689 // Now set the tracks, and expect frames to immediately start flowing.
3690 EXPECT_TRUE(caller_audio_sender->SetTrack(caller()->CreateLocalAudioTrack()));
3691 EXPECT_TRUE(caller_video_sender->SetTrack(caller()->CreateLocalVideoTrack()));
3692 EXPECT_TRUE(callee_audio_sender->SetTrack(callee()->CreateLocalAudioTrack()));
3693 EXPECT_TRUE(callee_video_sender->SetTrack(callee()->CreateLocalVideoTrack()));
Seth Hampson2f0d7022018-02-20 11:54:42 -08003694 MediaExpectations media_expectations;
3695 media_expectations.ExpectBidirectionalAudioAndVideo();
3696 ASSERT_TRUE(ExpectNewFrames(media_expectations));
3697}
3698
3699// This tests that if we negotiate after calling AddTransceiver but before we
3700// have a track, then set a track later, frames from the newly-set tracks are
3701// received end-to-end.
3702TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
3703 MediaFlowsAfterEarlyWarmupWithAddTransceiver) {
3704 ASSERT_TRUE(CreatePeerConnectionWrappers());
3705 ConnectFakeSignaling();
3706 auto audio_result = caller()->pc()->AddTransceiver(cricket::MEDIA_TYPE_AUDIO);
3707 ASSERT_EQ(RTCErrorType::NONE, audio_result.error().type());
3708 auto caller_audio_sender = audio_result.MoveValue()->sender();
3709 auto video_result = caller()->pc()->AddTransceiver(cricket::MEDIA_TYPE_VIDEO);
3710 ASSERT_EQ(RTCErrorType::NONE, video_result.error().type());
3711 auto caller_video_sender = video_result.MoveValue()->sender();
3712 callee()->SetRemoteOfferHandler([this] {
3713 ASSERT_EQ(2u, callee()->pc()->GetTransceivers().size());
3714 callee()->pc()->GetTransceivers()[0]->SetDirection(
3715 RtpTransceiverDirection::kSendRecv);
3716 callee()->pc()->GetTransceivers()[1]->SetDirection(
3717 RtpTransceiverDirection::kSendRecv);
3718 });
3719 caller()->CreateAndSetAndSignalOffer();
3720 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
3721 // Wait for ICE to complete, without any tracks being set.
3722 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
3723 caller()->ice_connection_state(), kMaxWaitForFramesMs);
3724 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
3725 callee()->ice_connection_state(), kMaxWaitForFramesMs);
3726 // Now set the tracks, and expect frames to immediately start flowing.
3727 auto callee_audio_sender = callee()->pc()->GetSenders()[0];
3728 auto callee_video_sender = callee()->pc()->GetSenders()[1];
3729 ASSERT_TRUE(caller_audio_sender->SetTrack(caller()->CreateLocalAudioTrack()));
3730 ASSERT_TRUE(caller_video_sender->SetTrack(caller()->CreateLocalVideoTrack()));
3731 ASSERT_TRUE(callee_audio_sender->SetTrack(callee()->CreateLocalAudioTrack()));
3732 ASSERT_TRUE(callee_video_sender->SetTrack(callee()->CreateLocalVideoTrack()));
3733 MediaExpectations media_expectations;
3734 media_expectations.ExpectBidirectionalAudioAndVideo();
3735 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003736}
3737
3738// This test verifies that a remote video track can be added via AddStream,
3739// and sent end-to-end. For this particular test, it's simply echoed back
3740// from the caller to the callee, rather than being forwarded to a third
3741// PeerConnection.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003742TEST_F(PeerConnectionIntegrationTestPlanB, CanSendRemoteVideoTrack) {
deadbeef1dcb1642017-03-29 21:08:16 -07003743 ASSERT_TRUE(CreatePeerConnectionWrappers());
3744 ConnectFakeSignaling();
3745 // Just send a video track from the caller.
Steve Anton15324772018-01-16 10:26:49 -08003746 caller()->AddVideoTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07003747 caller()->CreateAndSetAndSignalOffer();
3748 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
3749 ASSERT_EQ(1, callee()->remote_streams()->count());
3750
3751 // Echo the stream back, and do a new offer/anwer (initiated by callee this
3752 // time).
3753 callee()->pc()->AddStream(callee()->remote_streams()->at(0));
3754 callee()->CreateAndSetAndSignalOffer();
3755 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
3756
Seth Hampson2f0d7022018-02-20 11:54:42 -08003757 MediaExpectations media_expectations;
3758 media_expectations.ExpectBidirectionalVideo();
3759 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003760}
3761
3762// Test that we achieve the expected end-to-end connection time, using a
3763// fake clock and simulated latency on the media and signaling paths.
3764// We use a TURN<->TURN connection because this is usually the quickest to
3765// set up initially, especially when we're confident the connection will work
3766// and can start sending media before we get a STUN response.
3767//
3768// With various optimizations enabled, here are the network delays we expect to
3769// be on the critical path:
3770// 1. 2 signaling trips: Signaling offer and offerer's TURN candidate, then
3771// signaling answer (with DTLS fingerprint).
3772// 2. 9 media hops: Rest of the DTLS handshake. 3 hops in each direction when
3773// using TURN<->TURN pair, and DTLS exchange is 4 packets,
3774// the first of which should have arrived before the answer.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003775TEST_P(PeerConnectionIntegrationTest, EndToEndConnectionTimeWithTurnTurnPair) {
deadbeef1dcb1642017-03-29 21:08:16 -07003776 rtc::ScopedFakeClock fake_clock;
3777 // Some things use a time of "0" as a special value, so we need to start out
3778 // the fake clock at a nonzero time.
3779 // TODO(deadbeef): Fix this.
3780 fake_clock.AdvanceTime(rtc::TimeDelta::FromSeconds(1));
3781
3782 static constexpr int media_hop_delay_ms = 50;
3783 static constexpr int signaling_trip_delay_ms = 500;
3784 // For explanation of these values, see comment above.
3785 static constexpr int required_media_hops = 9;
3786 static constexpr int required_signaling_trips = 2;
3787 // For internal delays (such as posting an event asychronously).
3788 static constexpr int allowed_internal_delay_ms = 20;
3789 static constexpr int total_connection_time_ms =
3790 media_hop_delay_ms * required_media_hops +
3791 signaling_trip_delay_ms * required_signaling_trips +
3792 allowed_internal_delay_ms;
3793
3794 static const rtc::SocketAddress turn_server_1_internal_address{"88.88.88.0",
3795 3478};
3796 static const rtc::SocketAddress turn_server_1_external_address{"88.88.88.1",
3797 0};
3798 static const rtc::SocketAddress turn_server_2_internal_address{"99.99.99.0",
3799 3478};
3800 static const rtc::SocketAddress turn_server_2_external_address{"99.99.99.1",
3801 0};
3802 cricket::TestTurnServer turn_server_1(network_thread(),
3803 turn_server_1_internal_address,
3804 turn_server_1_external_address);
3805 cricket::TestTurnServer turn_server_2(network_thread(),
3806 turn_server_2_internal_address,
3807 turn_server_2_external_address);
Jonas Orelandbdcee282017-10-10 14:01:40 +02003808
deadbeef1dcb1642017-03-29 21:08:16 -07003809 // Bypass permission check on received packets so media can be sent before
3810 // the candidate is signaled.
3811 turn_server_1.set_enable_permission_checks(false);
3812 turn_server_2.set_enable_permission_checks(false);
3813
3814 PeerConnectionInterface::RTCConfiguration client_1_config;
3815 webrtc::PeerConnectionInterface::IceServer ice_server_1;
3816 ice_server_1.urls.push_back("turn:88.88.88.0:3478");
3817 ice_server_1.username = "test";
3818 ice_server_1.password = "test";
3819 client_1_config.servers.push_back(ice_server_1);
3820 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
3821 client_1_config.presume_writable_when_fully_relayed = true;
3822
3823 PeerConnectionInterface::RTCConfiguration client_2_config;
3824 webrtc::PeerConnectionInterface::IceServer ice_server_2;
3825 ice_server_2.urls.push_back("turn:99.99.99.0:3478");
3826 ice_server_2.username = "test";
3827 ice_server_2.password = "test";
3828 client_2_config.servers.push_back(ice_server_2);
3829 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
3830 client_2_config.presume_writable_when_fully_relayed = true;
3831
3832 ASSERT_TRUE(
3833 CreatePeerConnectionWrappersWithConfig(client_1_config, client_2_config));
3834 // Set up the simulated delays.
3835 SetSignalingDelayMs(signaling_trip_delay_ms);
3836 ConnectFakeSignaling();
3837 virtual_socket_server()->set_delay_mean(media_hop_delay_ms);
3838 virtual_socket_server()->UpdateDelayDistribution();
3839
3840 // Set "offer to receive audio/video" without adding any tracks, so we just
3841 // set up ICE/DTLS with no media.
3842 PeerConnectionInterface::RTCOfferAnswerOptions options;
3843 options.offer_to_receive_audio = 1;
3844 options.offer_to_receive_video = 1;
3845 caller()->SetOfferAnswerOptions(options);
3846 caller()->CreateAndSetAndSignalOffer();
deadbeef71452802017-05-07 17:21:01 -07003847 EXPECT_TRUE_SIMULATED_WAIT(DtlsConnected(), total_connection_time_ms,
3848 fake_clock);
deadbeef1dcb1642017-03-29 21:08:16 -07003849 // Need to free the clients here since they're using things we created on
3850 // the stack.
3851 delete SetCallerPcWrapperAndReturnCurrent(nullptr);
3852 delete SetCalleePcWrapperAndReturnCurrent(nullptr);
3853}
3854
Jonas Orelandbdcee282017-10-10 14:01:40 +02003855// Verify that a TurnCustomizer passed in through RTCConfiguration
3856// is actually used by the underlying TURN candidate pair.
3857// Note that turnport_unittest.cc contains more detailed, lower-level tests.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003858TEST_P(PeerConnectionIntegrationTest, TurnCustomizerUsedForTurnConnections) {
Jonas Orelandbdcee282017-10-10 14:01:40 +02003859 static const rtc::SocketAddress turn_server_1_internal_address{"88.88.88.0",
3860 3478};
3861 static const rtc::SocketAddress turn_server_1_external_address{"88.88.88.1",
3862 0};
3863 static const rtc::SocketAddress turn_server_2_internal_address{"99.99.99.0",
3864 3478};
3865 static const rtc::SocketAddress turn_server_2_external_address{"99.99.99.1",
3866 0};
3867 cricket::TestTurnServer turn_server_1(network_thread(),
3868 turn_server_1_internal_address,
3869 turn_server_1_external_address);
3870 cricket::TestTurnServer turn_server_2(network_thread(),
3871 turn_server_2_internal_address,
3872 turn_server_2_external_address);
3873
3874 PeerConnectionInterface::RTCConfiguration client_1_config;
3875 webrtc::PeerConnectionInterface::IceServer ice_server_1;
3876 ice_server_1.urls.push_back("turn:88.88.88.0:3478");
3877 ice_server_1.username = "test";
3878 ice_server_1.password = "test";
3879 client_1_config.servers.push_back(ice_server_1);
3880 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
3881 auto customizer1 = rtc::MakeUnique<cricket::TestTurnCustomizer>();
3882 client_1_config.turn_customizer = customizer1.get();
3883
3884 PeerConnectionInterface::RTCConfiguration client_2_config;
3885 webrtc::PeerConnectionInterface::IceServer ice_server_2;
3886 ice_server_2.urls.push_back("turn:99.99.99.0:3478");
3887 ice_server_2.username = "test";
3888 ice_server_2.password = "test";
3889 client_2_config.servers.push_back(ice_server_2);
3890 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
3891 auto customizer2 = rtc::MakeUnique<cricket::TestTurnCustomizer>();
3892 client_2_config.turn_customizer = customizer2.get();
3893
3894 ASSERT_TRUE(
3895 CreatePeerConnectionWrappersWithConfig(client_1_config, client_2_config));
3896 ConnectFakeSignaling();
3897
3898 // Set "offer to receive audio/video" without adding any tracks, so we just
3899 // set up ICE/DTLS with no media.
3900 PeerConnectionInterface::RTCOfferAnswerOptions options;
3901 options.offer_to_receive_audio = 1;
3902 options.offer_to_receive_video = 1;
3903 caller()->SetOfferAnswerOptions(options);
3904 caller()->CreateAndSetAndSignalOffer();
3905 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
3906
3907 EXPECT_GT(customizer1->allow_channel_data_cnt_, 0u);
3908 EXPECT_GT(customizer1->modify_cnt_, 0u);
3909
3910 EXPECT_GT(customizer2->allow_channel_data_cnt_, 0u);
3911 EXPECT_GT(customizer2->modify_cnt_, 0u);
3912
3913 // Need to free the clients here since they're using things we created on
3914 // the stack.
3915 delete SetCallerPcWrapperAndReturnCurrent(nullptr);
3916 delete SetCalleePcWrapperAndReturnCurrent(nullptr);
3917}
3918
deadbeefc964d0b2017-04-03 10:03:35 -07003919// Test that audio and video flow end-to-end when codec names don't use the
3920// expected casing, given that they're supposed to be case insensitive. To test
3921// this, all but one codec is removed from each media description, and its
3922// casing is changed.
3923//
3924// In the past, this has regressed and caused crashes/black video, due to the
3925// fact that code at some layers was doing case-insensitive comparisons and
3926// code at other layers was not.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003927TEST_P(PeerConnectionIntegrationTest, CodecNamesAreCaseInsensitive) {
deadbeefc964d0b2017-04-03 10:03:35 -07003928 ASSERT_TRUE(CreatePeerConnectionWrappers());
3929 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08003930 caller()->AddAudioVideoTracks();
3931 callee()->AddAudioVideoTracks();
deadbeefc964d0b2017-04-03 10:03:35 -07003932
3933 // Remove all but one audio/video codec (opus and VP8), and change the
3934 // casing of the caller's generated offer.
3935 caller()->SetGeneratedSdpMunger([](cricket::SessionDescription* description) {
3936 cricket::AudioContentDescription* audio =
3937 GetFirstAudioContentDescription(description);
3938 ASSERT_NE(nullptr, audio);
3939 auto audio_codecs = audio->codecs();
3940 audio_codecs.erase(std::remove_if(audio_codecs.begin(), audio_codecs.end(),
3941 [](const cricket::AudioCodec& codec) {
3942 return codec.name != "opus";
3943 }),
3944 audio_codecs.end());
3945 ASSERT_EQ(1u, audio_codecs.size());
3946 audio_codecs[0].name = "OpUs";
3947 audio->set_codecs(audio_codecs);
3948
3949 cricket::VideoContentDescription* video =
3950 GetFirstVideoContentDescription(description);
3951 ASSERT_NE(nullptr, video);
3952 auto video_codecs = video->codecs();
3953 video_codecs.erase(std::remove_if(video_codecs.begin(), video_codecs.end(),
3954 [](const cricket::VideoCodec& codec) {
3955 return codec.name != "VP8";
3956 }),
3957 video_codecs.end());
3958 ASSERT_EQ(1u, video_codecs.size());
3959 video_codecs[0].name = "vP8";
3960 video->set_codecs(video_codecs);
3961 });
3962
3963 caller()->CreateAndSetAndSignalOffer();
3964 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3965
3966 // Verify frames are still received end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003967 MediaExpectations media_expectations;
3968 media_expectations.ExpectBidirectionalAudioAndVideo();
3969 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeefc964d0b2017-04-03 10:03:35 -07003970}
3971
Seth Hampson2f0d7022018-02-20 11:54:42 -08003972TEST_P(PeerConnectionIntegrationTest, GetSources) {
hbos8d609f62017-04-10 07:39:05 -07003973 ASSERT_TRUE(CreatePeerConnectionWrappers());
3974 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08003975 caller()->AddAudioTrack();
hbos8d609f62017-04-10 07:39:05 -07003976 caller()->CreateAndSetAndSignalOffer();
3977 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
deadbeefd8ad7882017-04-18 16:01:17 -07003978 // Wait for one audio frame to be received by the callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003979 MediaExpectations media_expectations;
3980 media_expectations.CalleeExpectsSomeAudio(1);
3981 ASSERT_TRUE(ExpectNewFrames(media_expectations));
hbos8d609f62017-04-10 07:39:05 -07003982 ASSERT_GT(callee()->pc()->GetReceivers().size(), 0u);
3983 auto receiver = callee()->pc()->GetReceivers()[0];
3984 ASSERT_EQ(receiver->media_type(), cricket::MEDIA_TYPE_AUDIO);
3985
3986 auto contributing_sources = receiver->GetSources();
3987 ASSERT_GT(receiver->GetParameters().encodings.size(), 0u);
3988 EXPECT_EQ(receiver->GetParameters().encodings[0].ssrc,
3989 contributing_sources[0].source_id());
3990}
3991
deadbeef2f425aa2017-04-14 10:41:32 -07003992// Test that if a track is removed and added again with a different stream ID,
3993// the new stream ID is successfully communicated in SDP and media continues to
3994// flow end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003995// TODO(webrtc.bugs.org/8734): This test does not work for Unified Plan because
3996// it will not reuse a transceiver that has already been sending. After creating
3997// a new transceiver it tries to create an offer with two senders of the same
3998// track ids and it fails.
3999TEST_F(PeerConnectionIntegrationTestPlanB, RemoveAndAddTrackWithNewStreamId) {
deadbeef2f425aa2017-04-14 10:41:32 -07004000 ASSERT_TRUE(CreatePeerConnectionWrappers());
4001 ConnectFakeSignaling();
4002
4003 rtc::scoped_refptr<MediaStreamInterface> stream_1 =
4004 caller()->pc_factory()->CreateLocalMediaStream("stream_1");
4005 rtc::scoped_refptr<MediaStreamInterface> stream_2 =
4006 caller()->pc_factory()->CreateLocalMediaStream("stream_2");
4007
4008 // Add track using stream 1, do offer/answer.
4009 rtc::scoped_refptr<webrtc::AudioTrackInterface> track =
4010 caller()->CreateLocalAudioTrack();
4011 rtc::scoped_refptr<webrtc::RtpSenderInterface> sender =
4012 caller()->pc()->AddTrack(track, {stream_1.get()});
4013 caller()->CreateAndSetAndSignalOffer();
4014 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08004015 {
4016 MediaExpectations media_expectations;
4017 media_expectations.CalleeExpectsSomeAudio(1);
4018 ASSERT_TRUE(ExpectNewFrames(media_expectations));
4019 }
deadbeef2f425aa2017-04-14 10:41:32 -07004020 // Remove the sender, and create a new one with the new stream.
4021 caller()->pc()->RemoveTrack(sender);
4022 sender = caller()->pc()->AddTrack(track, {stream_2.get()});
4023 caller()->CreateAndSetAndSignalOffer();
4024 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4025 // Wait for additional audio frames to be received by the callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004026 {
4027 MediaExpectations media_expectations;
4028 media_expectations.CalleeExpectsSomeAudio();
4029 ASSERT_TRUE(ExpectNewFrames(media_expectations));
4030 }
deadbeef2f425aa2017-04-14 10:41:32 -07004031}
4032
Seth Hampson2f0d7022018-02-20 11:54:42 -08004033TEST_P(PeerConnectionIntegrationTest, RtcEventLogOutputWriteCalled) {
Elad Alon99c3fe52017-10-13 16:29:40 +02004034 ASSERT_TRUE(CreatePeerConnectionWrappers());
4035 ConnectFakeSignaling();
4036
4037 auto output = rtc::MakeUnique<testing::NiceMock<MockRtcEventLogOutput>>();
4038 ON_CALL(*output, IsActive()).WillByDefault(testing::Return(true));
4039 ON_CALL(*output, Write(::testing::_)).WillByDefault(testing::Return(true));
4040 EXPECT_CALL(*output, Write(::testing::_)).Times(::testing::AtLeast(1));
Bjorn Tereliusde939432017-11-20 17:38:14 +01004041 EXPECT_TRUE(caller()->pc()->StartRtcEventLog(
4042 std::move(output), webrtc::RtcEventLog::kImmediateOutput));
Elad Alon99c3fe52017-10-13 16:29:40 +02004043
Steve Anton15324772018-01-16 10:26:49 -08004044 caller()->AddAudioVideoTracks();
Elad Alon99c3fe52017-10-13 16:29:40 +02004045 caller()->CreateAndSetAndSignalOffer();
4046 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4047}
4048
Steve Antonede9ca52017-10-16 13:04:27 -07004049// Test that if candidates are only signaled by applying full session
4050// descriptions (instead of using AddIceCandidate), the peers can connect to
4051// each other and exchange media.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004052TEST_P(PeerConnectionIntegrationTest, MediaFlowsWhenCandidatesSetOnlyInSdp) {
Steve Antonede9ca52017-10-16 13:04:27 -07004053 ASSERT_TRUE(CreatePeerConnectionWrappers());
4054 // Each side will signal the session descriptions but not candidates.
4055 ConnectFakeSignalingForSdpOnly();
4056
4057 // Add audio video track and exchange the initial offer/answer with media
4058 // information only. This will start ICE gathering on each side.
Steve Anton15324772018-01-16 10:26:49 -08004059 caller()->AddAudioVideoTracks();
4060 callee()->AddAudioVideoTracks();
Steve Antonede9ca52017-10-16 13:04:27 -07004061 caller()->CreateAndSetAndSignalOffer();
4062
4063 // Wait for all candidates to be gathered on both the caller and callee.
4064 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceGatheringComplete,
4065 caller()->ice_gathering_state(), kDefaultTimeout);
4066 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceGatheringComplete,
4067 callee()->ice_gathering_state(), kDefaultTimeout);
4068
4069 // The candidates will now be included in the session description, so
4070 // signaling them will start the ICE connection.
4071 caller()->CreateAndSetAndSignalOffer();
4072 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4073
4074 // Ensure that media flows in both directions.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004075 MediaExpectations media_expectations;
4076 media_expectations.ExpectBidirectionalAudioAndVideo();
4077 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Antonede9ca52017-10-16 13:04:27 -07004078}
4079
henrika5f6bf242017-11-01 11:06:56 +01004080// Test that SetAudioPlayout can be used to disable audio playout from the
4081// start, then later enable it. This may be useful, for example, if the caller
4082// needs to play a local ringtone until some event occurs, after which it
4083// switches to playing the received audio.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004084TEST_P(PeerConnectionIntegrationTest, DisableAndEnableAudioPlayout) {
henrika5f6bf242017-11-01 11:06:56 +01004085 ASSERT_TRUE(CreatePeerConnectionWrappers());
4086 ConnectFakeSignaling();
4087
4088 // Set up audio-only call where audio playout is disabled on caller's side.
4089 caller()->pc()->SetAudioPlayout(false);
Steve Anton15324772018-01-16 10:26:49 -08004090 caller()->AddAudioTrack();
4091 callee()->AddAudioTrack();
henrika5f6bf242017-11-01 11:06:56 +01004092 caller()->CreateAndSetAndSignalOffer();
4093 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4094
4095 // Pump messages for a second.
4096 WAIT(false, 1000);
4097 // Since audio playout is disabled, the caller shouldn't have received
4098 // anything (at the playout level, at least).
4099 EXPECT_EQ(0, caller()->audio_frames_received());
4100 // As a sanity check, make sure the callee (for which playout isn't disabled)
4101 // did still see frames on its audio level.
4102 ASSERT_GT(callee()->audio_frames_received(), 0);
4103
4104 // Enable playout again, and ensure audio starts flowing.
4105 caller()->pc()->SetAudioPlayout(true);
Seth Hampson2f0d7022018-02-20 11:54:42 -08004106 MediaExpectations media_expectations;
4107 media_expectations.ExpectBidirectionalAudio();
4108 ASSERT_TRUE(ExpectNewFrames(media_expectations));
henrika5f6bf242017-11-01 11:06:56 +01004109}
4110
4111double GetAudioEnergyStat(PeerConnectionWrapper* pc) {
4112 auto report = pc->NewGetStats();
4113 auto track_stats_list =
4114 report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
4115 const webrtc::RTCMediaStreamTrackStats* remote_track_stats = nullptr;
4116 for (const auto* track_stats : track_stats_list) {
4117 if (track_stats->remote_source.is_defined() &&
4118 *track_stats->remote_source) {
4119 remote_track_stats = track_stats;
4120 break;
4121 }
4122 }
4123
4124 if (!remote_track_stats->total_audio_energy.is_defined()) {
4125 return 0.0;
4126 }
4127 return *remote_track_stats->total_audio_energy;
4128}
4129
4130// Test that if audio playout is disabled via the SetAudioPlayout() method, then
4131// incoming audio is still processed and statistics are generated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004132TEST_P(PeerConnectionIntegrationTest,
henrika5f6bf242017-11-01 11:06:56 +01004133 DisableAudioPlayoutStillGeneratesAudioStats) {
4134 ASSERT_TRUE(CreatePeerConnectionWrappers());
4135 ConnectFakeSignaling();
4136
4137 // Set up audio-only call where playout is disabled but audio-processing is
4138 // still active.
Steve Anton15324772018-01-16 10:26:49 -08004139 caller()->AddAudioTrack();
4140 callee()->AddAudioTrack();
henrika5f6bf242017-11-01 11:06:56 +01004141 caller()->pc()->SetAudioPlayout(false);
4142
4143 caller()->CreateAndSetAndSignalOffer();
4144 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4145
4146 // Wait for the callee to receive audio stats.
4147 EXPECT_TRUE_WAIT(GetAudioEnergyStat(caller()) > 0, kMaxWaitForFramesMs);
4148}
4149
henrika4f167df2017-11-01 14:45:55 +01004150// Test that SetAudioRecording can be used to disable audio recording from the
4151// start, then later enable it. This may be useful, for example, if the caller
4152// wants to ensure that no audio resources are active before a certain state
4153// is reached.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004154TEST_P(PeerConnectionIntegrationTest, DisableAndEnableAudioRecording) {
henrika4f167df2017-11-01 14:45:55 +01004155 ASSERT_TRUE(CreatePeerConnectionWrappers());
4156 ConnectFakeSignaling();
4157
4158 // Set up audio-only call where audio recording is disabled on caller's side.
4159 caller()->pc()->SetAudioRecording(false);
Steve Anton15324772018-01-16 10:26:49 -08004160 caller()->AddAudioTrack();
4161 callee()->AddAudioTrack();
henrika4f167df2017-11-01 14:45:55 +01004162 caller()->CreateAndSetAndSignalOffer();
4163 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4164
4165 // Pump messages for a second.
4166 WAIT(false, 1000);
4167 // Since caller has disabled audio recording, the callee shouldn't have
4168 // received anything.
4169 EXPECT_EQ(0, callee()->audio_frames_received());
4170 // As a sanity check, make sure the caller did still see frames on its
4171 // audio level since audio recording is enabled on the calle side.
4172 ASSERT_GT(caller()->audio_frames_received(), 0);
4173
4174 // Enable audio recording again, and ensure audio starts flowing.
4175 caller()->pc()->SetAudioRecording(true);
Seth Hampson2f0d7022018-02-20 11:54:42 -08004176 MediaExpectations media_expectations;
4177 media_expectations.ExpectBidirectionalAudio();
4178 ASSERT_TRUE(ExpectNewFrames(media_expectations));
henrika4f167df2017-11-01 14:45:55 +01004179}
4180
Taylor Brandstetter389a97c2018-01-03 16:26:06 -08004181// Test that after closing PeerConnections, they stop sending any packets (ICE,
4182// DTLS, RTP...).
Seth Hampson2f0d7022018-02-20 11:54:42 -08004183TEST_P(PeerConnectionIntegrationTest, ClosingConnectionStopsPacketFlow) {
Taylor Brandstetter389a97c2018-01-03 16:26:06 -08004184 // Set up audio/video/data, wait for some frames to be received.
4185 ASSERT_TRUE(CreatePeerConnectionWrappers());
4186 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08004187 caller()->AddAudioVideoTracks();
Taylor Brandstetter389a97c2018-01-03 16:26:06 -08004188#ifdef HAVE_SCTP
4189 caller()->CreateDataChannel();
4190#endif
4191 caller()->CreateAndSetAndSignalOffer();
4192 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08004193 MediaExpectations media_expectations;
4194 media_expectations.CalleeExpectsSomeAudioAndVideo();
4195 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Taylor Brandstetter389a97c2018-01-03 16:26:06 -08004196 // Close PeerConnections.
4197 caller()->pc()->Close();
4198 callee()->pc()->Close();
4199 // Pump messages for a second, and ensure no new packets end up sent.
4200 uint32_t sent_packets_a = virtual_socket_server()->sent_packets();
4201 WAIT(false, 1000);
4202 uint32_t sent_packets_b = virtual_socket_server()->sent_packets();
4203 EXPECT_EQ(sent_packets_a, sent_packets_b);
4204}
4205
Steve Anton7eca0932018-03-30 15:18:41 -07004206// Test that transport stats are generated by the RTCStatsCollector for a
4207// connection that only involves data channels. This is a regression test for
4208// crbug.com/826972.
4209#ifdef HAVE_SCTP
4210TEST_P(PeerConnectionIntegrationTest,
4211 TransportStatsReportedForDataChannelOnlyConnection) {
4212 ASSERT_TRUE(CreatePeerConnectionWrappers());
4213 ConnectFakeSignaling();
4214 caller()->CreateDataChannel();
4215
4216 caller()->CreateAndSetAndSignalOffer();
4217 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4218 ASSERT_TRUE_WAIT(callee()->data_channel(), kDefaultTimeout);
4219
4220 auto caller_report = caller()->NewGetStats();
4221 EXPECT_EQ(1u, caller_report->GetStatsOfType<RTCTransportStats>().size());
4222 auto callee_report = callee()->NewGetStats();
4223 EXPECT_EQ(1u, callee_report->GetStatsOfType<RTCTransportStats>().size());
4224}
4225#endif // HAVE_SCTP
4226
Seth Hampson2f0d7022018-02-20 11:54:42 -08004227INSTANTIATE_TEST_CASE_P(PeerConnectionIntegrationTest,
4228 PeerConnectionIntegrationTest,
4229 Values(SdpSemantics::kPlanB,
4230 SdpSemantics::kUnifiedPlan));
Steve Antond3679212018-01-17 17:41:02 -08004231
Steve Anton74255ff2018-01-24 18:32:57 -08004232// Tests that verify interoperability between Plan B and Unified Plan
4233// PeerConnections.
4234class PeerConnectionIntegrationInteropTest
Seth Hampson2f0d7022018-02-20 11:54:42 -08004235 : public PeerConnectionIntegrationBaseTest,
Steve Anton74255ff2018-01-24 18:32:57 -08004236 public ::testing::WithParamInterface<
4237 std::tuple<SdpSemantics, SdpSemantics>> {
4238 protected:
Seth Hampson2f0d7022018-02-20 11:54:42 -08004239 // Setting the SdpSemantics for the base test to kDefault does not matter
4240 // because we specify not to use the test semantics when creating
4241 // PeerConnectionWrappers.
Steve Anton74255ff2018-01-24 18:32:57 -08004242 PeerConnectionIntegrationInteropTest()
Steve Anton3acffc32018-04-12 17:21:03 -07004243 : PeerConnectionIntegrationBaseTest(SdpSemantics::kPlanB),
Seth Hampson2f0d7022018-02-20 11:54:42 -08004244 caller_semantics_(std::get<0>(GetParam())),
Steve Anton74255ff2018-01-24 18:32:57 -08004245 callee_semantics_(std::get<1>(GetParam())) {}
4246
4247 bool CreatePeerConnectionWrappersWithSemantics() {
Steve Anton3acffc32018-04-12 17:21:03 -07004248 return CreatePeerConnectionWrappersWithSdpSemantics(caller_semantics_,
4249 callee_semantics_);
Steve Anton74255ff2018-01-24 18:32:57 -08004250 }
4251
4252 const SdpSemantics caller_semantics_;
4253 const SdpSemantics callee_semantics_;
4254};
4255
4256TEST_P(PeerConnectionIntegrationInteropTest, NoMediaLocalToNoMediaRemote) {
4257 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
4258 ConnectFakeSignaling();
4259
4260 caller()->CreateAndSetAndSignalOffer();
4261 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4262}
4263
4264TEST_P(PeerConnectionIntegrationInteropTest, OneAudioLocalToNoMediaRemote) {
4265 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
4266 ConnectFakeSignaling();
4267 auto audio_sender = caller()->AddAudioTrack();
4268
4269 caller()->CreateAndSetAndSignalOffer();
4270 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4271
4272 // Verify that one audio receiver has been created on the remote and that it
4273 // has the same track ID as the sending track.
4274 auto receivers = callee()->pc()->GetReceivers();
4275 ASSERT_EQ(1u, receivers.size());
4276 EXPECT_EQ(cricket::MEDIA_TYPE_AUDIO, receivers[0]->media_type());
4277 EXPECT_EQ(receivers[0]->track()->id(), audio_sender->track()->id());
4278
Seth Hampson2f0d7022018-02-20 11:54:42 -08004279 MediaExpectations media_expectations;
4280 media_expectations.CalleeExpectsSomeAudio();
4281 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08004282}
4283
4284TEST_P(PeerConnectionIntegrationInteropTest, OneAudioOneVideoToNoMediaRemote) {
4285 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
4286 ConnectFakeSignaling();
4287 auto video_sender = caller()->AddVideoTrack();
4288 auto audio_sender = caller()->AddAudioTrack();
4289
4290 caller()->CreateAndSetAndSignalOffer();
4291 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4292
4293 // Verify that one audio and one video receiver have been created on the
4294 // remote and that they have the same track IDs as the sending tracks.
4295 auto audio_receivers =
4296 callee()->GetReceiversOfType(cricket::MEDIA_TYPE_AUDIO);
4297 ASSERT_EQ(1u, audio_receivers.size());
4298 EXPECT_EQ(audio_receivers[0]->track()->id(), audio_sender->track()->id());
4299 auto video_receivers =
4300 callee()->GetReceiversOfType(cricket::MEDIA_TYPE_VIDEO);
4301 ASSERT_EQ(1u, video_receivers.size());
4302 EXPECT_EQ(video_receivers[0]->track()->id(), video_sender->track()->id());
4303
Seth Hampson2f0d7022018-02-20 11:54:42 -08004304 MediaExpectations media_expectations;
4305 media_expectations.CalleeExpectsSomeAudioAndVideo();
4306 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08004307}
4308
4309TEST_P(PeerConnectionIntegrationInteropTest,
4310 OneAudioOneVideoLocalToOneAudioOneVideoRemote) {
4311 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
4312 ConnectFakeSignaling();
4313 caller()->AddAudioVideoTracks();
4314 callee()->AddAudioVideoTracks();
4315
4316 caller()->CreateAndSetAndSignalOffer();
4317 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4318
Seth Hampson2f0d7022018-02-20 11:54:42 -08004319 MediaExpectations media_expectations;
4320 media_expectations.ExpectBidirectionalAudioAndVideo();
4321 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08004322}
4323
4324TEST_P(PeerConnectionIntegrationInteropTest,
4325 ReverseRolesOneAudioLocalToOneVideoRemote) {
4326 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
4327 ConnectFakeSignaling();
4328 caller()->AddAudioTrack();
4329 callee()->AddVideoTrack();
4330
4331 caller()->CreateAndSetAndSignalOffer();
4332 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4333
4334 // Verify that only the audio track has been negotiated.
4335 EXPECT_EQ(0u, caller()->GetReceiversOfType(cricket::MEDIA_TYPE_VIDEO).size());
4336 // Might also check that the callee's NegotiationNeeded flag is set.
4337
4338 // Reverse roles.
4339 callee()->CreateAndSetAndSignalOffer();
4340 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4341
Seth Hampson2f0d7022018-02-20 11:54:42 -08004342 MediaExpectations media_expectations;
4343 media_expectations.CallerExpectsSomeVideo();
4344 media_expectations.CalleeExpectsSomeAudio();
4345 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08004346}
4347
Steve Antonba42e992018-04-09 14:10:01 -07004348INSTANTIATE_TEST_CASE_P(
4349 PeerConnectionIntegrationTest,
4350 PeerConnectionIntegrationInteropTest,
4351 Values(std::make_tuple(SdpSemantics::kPlanB, SdpSemantics::kUnifiedPlan),
4352 std::make_tuple(SdpSemantics::kUnifiedPlan, SdpSemantics::kPlanB)));
4353
4354// Test that if the Unified Plan side offers two video tracks then the Plan B
4355// side will only see the first one and ignore the second.
4356TEST_F(PeerConnectionIntegrationTestPlanB, TwoVideoUnifiedPlanToNoMediaPlanB) {
Steve Anton3acffc32018-04-12 17:21:03 -07004357 ASSERT_TRUE(CreatePeerConnectionWrappersWithSdpSemantics(
4358 SdpSemantics::kUnifiedPlan, SdpSemantics::kPlanB));
Steve Anton74255ff2018-01-24 18:32:57 -08004359 ConnectFakeSignaling();
4360 auto first_sender = caller()->AddVideoTrack();
4361 caller()->AddVideoTrack();
4362
4363 caller()->CreateAndSetAndSignalOffer();
4364 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4365
4366 // Verify that there is only one receiver and it corresponds to the first
4367 // added track.
4368 auto receivers = callee()->pc()->GetReceivers();
4369 ASSERT_EQ(1u, receivers.size());
4370 EXPECT_TRUE(receivers[0]->track()->enabled());
4371 EXPECT_EQ(first_sender->track()->id(), receivers[0]->track()->id());
4372
Seth Hampson2f0d7022018-02-20 11:54:42 -08004373 MediaExpectations media_expectations;
4374 media_expectations.CalleeExpectsSomeVideo();
4375 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08004376}
4377
deadbeef1dcb1642017-03-29 21:08:16 -07004378} // namespace
4379
4380#endif // if !defined(THREAD_SANITIZER)