blob: 406427bc7fb5b8246f1b63c0f44a9af308288556 [file] [log] [blame]
wu@webrtc.org364f2042013-11-20 21:49:41 +00001/*
kjellanderb24317b2016-02-10 07:54:43 -08002 * Copyright 2013 The WebRTC project authors. All Rights Reserved.
wu@webrtc.org364f2042013-11-20 21:49:41 +00003 *
kjellanderb24317b2016-02-10 07:54:43 -08004 * 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.
wu@webrtc.org364f2042013-11-20 21:49:41 +00009 */
10
kwibergd1fe2812016-04-27 06:47:29 -070011#include <memory>
12
Karl Wibergc5bb00b2017-10-10 23:17:17 +020013#include "api/audio_codecs/L16/audio_decoder_L16.h"
14#include "api/audio_codecs/L16/audio_encoder_L16.h"
Karl Wiberg17668ec2018-03-01 15:13:27 +010015#include "api/audio_codecs/audio_codec_pair_id.h"
Karl Wibergc5bb00b2017-10-10 23:17:17 +020016#include "api/audio_codecs/audio_decoder_factory_template.h"
17#include "api/audio_codecs/audio_encoder_factory_template.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "api/audio_codecs/builtin_audio_decoder_factory.h"
19#include "api/audio_codecs/builtin_audio_encoder_factory.h"
20#include "rtc_base/gunit.h"
21#include "rtc_base/logging.h"
22#include "rtc_base/ptr_util.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020023#include "rtc_base/stringencode.h"
24#include "rtc_base/stringutils.h"
Patrik Höglund563934e2017-09-15 09:04:28 +020025
ossu7bb87ee2017-01-23 04:56:25 -080026#ifdef WEBRTC_ANDROID
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020027#include "pc/test/androidtestinitializer.h"
ossu7bb87ee2017-01-23 04:56:25 -080028#endif
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020029#include "pc/test/peerconnectiontestwrapper.h"
ossu7bb87ee2017-01-23 04:56:25 -080030// Notice that mockpeerconnectionobservers.h must be included after the above!
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020031#include "pc/test/mockpeerconnectionobservers.h"
32#include "test/mock_audio_decoder.h"
33#include "test/mock_audio_decoder_factory.h"
kwiberg9e5b11e2017-04-19 03:47:57 -070034
35using testing::AtLeast;
36using testing::Invoke;
37using testing::StrictMock;
Steve Anton191c39f2018-01-24 19:35:55 -080038using testing::Values;
kwiberg9e5b11e2017-04-19 03:47:57 -070039using testing::_;
wu@webrtc.org364f2042013-11-20 21:49:41 +000040
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +000041using webrtc::DataChannelInterface;
wu@webrtc.org364f2042013-11-20 21:49:41 +000042using webrtc::FakeConstraints;
43using webrtc::MediaConstraintsInterface;
44using webrtc::MediaStreamInterface;
45using webrtc::PeerConnectionInterface;
Steve Anton191c39f2018-01-24 19:35:55 -080046using webrtc::SdpSemantics;
wu@webrtc.org364f2042013-11-20 21:49:41 +000047
48namespace {
49
Honghai Zhang82d78622016-05-06 11:29:15 -070050const int kMaxWait = 10000;
wu@webrtc.org364f2042013-11-20 21:49:41 +000051
wu@webrtc.org364f2042013-11-20 21:49:41 +000052} // namespace
53
Steve Anton191c39f2018-01-24 19:35:55 -080054class PeerConnectionEndToEndBaseTest : public sigslot::has_slots<>,
55 public testing::Test {
wu@webrtc.org364f2042013-11-20 21:49:41 +000056 public:
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000057 typedef std::vector<rtc::scoped_refptr<DataChannelInterface> >
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +000058 DataChannelList;
59
Steve Anton191c39f2018-01-24 19:35:55 -080060 explicit PeerConnectionEndToEndBaseTest(SdpSemantics sdp_semantics) {
tommie7251592017-07-14 14:44:46 -070061 network_thread_ = rtc::Thread::CreateWithSocketServer();
62 worker_thread_ = rtc::Thread::Create();
63 RTC_CHECK(network_thread_->Start());
64 RTC_CHECK(worker_thread_->Start());
perkj57db6522016-04-08 08:16:33 -070065 caller_ = new rtc::RefCountedObject<PeerConnectionTestWrapper>(
tommie7251592017-07-14 14:44:46 -070066 "caller", network_thread_.get(), worker_thread_.get());
perkj57db6522016-04-08 08:16:33 -070067 callee_ = new rtc::RefCountedObject<PeerConnectionTestWrapper>(
tommie7251592017-07-14 14:44:46 -070068 "callee", network_thread_.get(), worker_thread_.get());
zhihuang9763d562016-08-05 11:14:50 -070069 webrtc::PeerConnectionInterface::IceServer ice_server;
70 ice_server.uri = "stun:stun.l.google.com:19302";
71 config_.servers.push_back(ice_server);
Steve Anton191c39f2018-01-24 19:35:55 -080072 config_.sdp_semantics = sdp_semantics;
zhihuang9763d562016-08-05 11:14:50 -070073
phoglund37ebcf02016-01-08 05:04:57 -080074#ifdef WEBRTC_ANDROID
75 webrtc::InitializeAndroidObjects();
76#endif
wu@webrtc.org364f2042013-11-20 21:49:41 +000077 }
78
Mirko Bonadeic61ce0d2017-11-21 17:04:20 +010079 void CreatePcs(const MediaConstraintsInterface* pc_constraints,
80 const rtc::scoped_refptr<webrtc::AudioEncoderFactory>&
81 audio_encoder_factory,
82 const rtc::scoped_refptr<webrtc::AudioDecoderFactory>&
83 audio_decoder_factory) {
kwiberg9e5b11e2017-04-19 03:47:57 -070084 EXPECT_TRUE(caller_->CreatePc(
85 pc_constraints, config_, audio_encoder_factory, audio_decoder_factory));
86 EXPECT_TRUE(callee_->CreatePc(
87 pc_constraints, config_, audio_encoder_factory, audio_decoder_factory));
wu@webrtc.org364f2042013-11-20 21:49:41 +000088 PeerConnectionTestWrapper::Connect(caller_.get(), callee_.get());
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +000089
90 caller_->SignalOnDataChannel.connect(
Steve Anton191c39f2018-01-24 19:35:55 -080091 this, &PeerConnectionEndToEndBaseTest::OnCallerAddedDataChanel);
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +000092 callee_->SignalOnDataChannel.connect(
Steve Anton191c39f2018-01-24 19:35:55 -080093 this, &PeerConnectionEndToEndBaseTest::OnCalleeAddedDataChannel);
wu@webrtc.org364f2042013-11-20 21:49:41 +000094 }
95
96 void GetAndAddUserMedia() {
97 FakeConstraints audio_constraints;
98 FakeConstraints video_constraints;
99 GetAndAddUserMedia(true, audio_constraints, true, video_constraints);
100 }
101
Mirko Bonadeic61ce0d2017-11-21 17:04:20 +0100102 void GetAndAddUserMedia(bool audio,
103 const FakeConstraints& audio_constraints,
104 bool video,
105 const FakeConstraints& video_constraints) {
wu@webrtc.org364f2042013-11-20 21:49:41 +0000106 caller_->GetAndAddUserMedia(audio, audio_constraints,
107 video, video_constraints);
108 callee_->GetAndAddUserMedia(audio, audio_constraints,
109 video, video_constraints);
110 }
111
112 void Negotiate() {
113 caller_->CreateOffer(NULL);
114 }
115
116 void WaitForCallEstablished() {
117 caller_->WaitForCallEstablished();
118 callee_->WaitForCallEstablished();
119 }
120
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000121 void WaitForConnection() {
122 caller_->WaitForConnection();
123 callee_->WaitForConnection();
124 }
125
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000126 void OnCallerAddedDataChanel(DataChannelInterface* dc) {
127 caller_signaled_data_channels_.push_back(dc);
128 }
129
130 void OnCalleeAddedDataChannel(DataChannelInterface* dc) {
131 callee_signaled_data_channels_.push_back(dc);
132 }
133
134 // Tests that |dc1| and |dc2| can send to and receive from each other.
135 void TestDataChannelSendAndReceive(
136 DataChannelInterface* dc1, DataChannelInterface* dc2) {
kwibergd1fe2812016-04-27 06:47:29 -0700137 std::unique_ptr<webrtc::MockDataChannelObserver> dc1_observer(
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000138 new webrtc::MockDataChannelObserver(dc1));
139
kwibergd1fe2812016-04-27 06:47:29 -0700140 std::unique_ptr<webrtc::MockDataChannelObserver> dc2_observer(
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000141 new webrtc::MockDataChannelObserver(dc2));
142
143 static const std::string kDummyData = "abcdefg";
144 webrtc::DataBuffer buffer(kDummyData);
145 EXPECT_TRUE(dc1->Send(buffer));
146 EXPECT_EQ_WAIT(kDummyData, dc2_observer->last_message(), kMaxWait);
147
148 EXPECT_TRUE(dc2->Send(buffer));
149 EXPECT_EQ_WAIT(kDummyData, dc1_observer->last_message(), kMaxWait);
150
151 EXPECT_EQ(1U, dc1_observer->received_message_count());
152 EXPECT_EQ(1U, dc2_observer->received_message_count());
153 }
154
155 void WaitForDataChannelsToOpen(DataChannelInterface* local_dc,
156 const DataChannelList& remote_dc_list,
157 size_t remote_dc_index) {
158 EXPECT_EQ_WAIT(DataChannelInterface::kOpen, local_dc->state(), kMaxWait);
159
160 EXPECT_TRUE_WAIT(remote_dc_list.size() > remote_dc_index, kMaxWait);
161 EXPECT_EQ_WAIT(DataChannelInterface::kOpen,
162 remote_dc_list[remote_dc_index]->state(),
163 kMaxWait);
164 EXPECT_EQ(local_dc->id(), remote_dc_list[remote_dc_index]->id());
165 }
166
167 void CloseDataChannels(DataChannelInterface* local_dc,
168 const DataChannelList& remote_dc_list,
169 size_t remote_dc_index) {
170 local_dc->Close();
171 EXPECT_EQ_WAIT(DataChannelInterface::kClosed, local_dc->state(), kMaxWait);
172 EXPECT_EQ_WAIT(DataChannelInterface::kClosed,
173 remote_dc_list[remote_dc_index]->state(),
174 kMaxWait);
175 }
176
wu@webrtc.org364f2042013-11-20 21:49:41 +0000177 protected:
tommie7251592017-07-14 14:44:46 -0700178 std::unique_ptr<rtc::Thread> network_thread_;
179 std::unique_ptr<rtc::Thread> worker_thread_;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000180 rtc::scoped_refptr<PeerConnectionTestWrapper> caller_;
181 rtc::scoped_refptr<PeerConnectionTestWrapper> callee_;
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000182 DataChannelList caller_signaled_data_channels_;
183 DataChannelList callee_signaled_data_channels_;
zhihuang9763d562016-08-05 11:14:50 -0700184 webrtc::PeerConnectionInterface::RTCConfiguration config_;
wu@webrtc.org364f2042013-11-20 21:49:41 +0000185};
186
Steve Anton191c39f2018-01-24 19:35:55 -0800187class PeerConnectionEndToEndTest
188 : public PeerConnectionEndToEndBaseTest,
189 public ::testing::WithParamInterface<SdpSemantics> {
190 protected:
191 PeerConnectionEndToEndTest() : PeerConnectionEndToEndBaseTest(GetParam()) {}
192};
193
Karl Wibergc5bb00b2017-10-10 23:17:17 +0200194namespace {
195
kwiberg9e5b11e2017-04-19 03:47:57 -0700196std::unique_ptr<webrtc::AudioDecoder> CreateForwardingMockDecoder(
197 std::unique_ptr<webrtc::AudioDecoder> real_decoder) {
198 class ForwardingMockDecoder : public StrictMock<webrtc::MockAudioDecoder> {
199 public:
Steve Anton36b29d12017-10-30 09:57:42 -0700200 explicit ForwardingMockDecoder(std::unique_ptr<AudioDecoder> decoder)
kwiberg9e5b11e2017-04-19 03:47:57 -0700201 : decoder_(std::move(decoder)) {}
202
203 private:
204 std::unique_ptr<AudioDecoder> decoder_;
205 };
206
207 const auto dec = real_decoder.get(); // For lambda capturing.
208 auto mock_decoder =
209 rtc::MakeUnique<ForwardingMockDecoder>(std::move(real_decoder));
210 EXPECT_CALL(*mock_decoder, Channels())
211 .Times(AtLeast(1))
212 .WillRepeatedly(Invoke([dec] { return dec->Channels(); }));
213 EXPECT_CALL(*mock_decoder, DecodeInternal(_, _, _, _, _))
214 .Times(AtLeast(1))
215 .WillRepeatedly(
216 Invoke([dec](const uint8_t* encoded, size_t encoded_len,
217 int sample_rate_hz, int16_t* decoded,
218 webrtc::AudioDecoder::SpeechType* speech_type) {
219 return dec->Decode(encoded, encoded_len, sample_rate_hz,
220 std::numeric_limits<size_t>::max(), decoded,
221 speech_type);
222 }));
223 EXPECT_CALL(*mock_decoder, Die());
224 EXPECT_CALL(*mock_decoder, HasDecodePlc()).WillRepeatedly(Invoke([dec] {
225 return dec->HasDecodePlc();
226 }));
227 EXPECT_CALL(*mock_decoder, IncomingPacket(_, _, _, _, _))
228 .Times(AtLeast(1))
229 .WillRepeatedly(Invoke([dec](const uint8_t* payload, size_t payload_len,
230 uint16_t rtp_sequence_number,
231 uint32_t rtp_timestamp,
232 uint32_t arrival_timestamp) {
233 return dec->IncomingPacket(payload, payload_len, rtp_sequence_number,
234 rtp_timestamp, arrival_timestamp);
235 }));
236 EXPECT_CALL(*mock_decoder, PacketDuration(_, _))
237 .Times(AtLeast(1))
238 .WillRepeatedly(Invoke([dec](const uint8_t* encoded, size_t encoded_len) {
239 return dec->PacketDuration(encoded, encoded_len);
240 }));
241 EXPECT_CALL(*mock_decoder, SampleRateHz())
242 .Times(AtLeast(1))
243 .WillRepeatedly(Invoke([dec] { return dec->SampleRateHz(); }));
244
245 return std::move(mock_decoder);
246}
247
248rtc::scoped_refptr<webrtc::AudioDecoderFactory>
249CreateForwardingMockDecoderFactory(
250 webrtc::AudioDecoderFactory* real_decoder_factory) {
251 rtc::scoped_refptr<webrtc::MockAudioDecoderFactory> mock_decoder_factory =
252 new rtc::RefCountedObject<StrictMock<webrtc::MockAudioDecoderFactory>>;
253 EXPECT_CALL(*mock_decoder_factory, GetSupportedDecoders())
254 .Times(AtLeast(1))
255 .WillRepeatedly(Invoke([real_decoder_factory] {
256 return real_decoder_factory->GetSupportedDecoders();
257 }));
258 EXPECT_CALL(*mock_decoder_factory, IsSupportedDecoder(_))
259 .Times(AtLeast(1))
260 .WillRepeatedly(
261 Invoke([real_decoder_factory](const webrtc::SdpAudioFormat& format) {
262 return real_decoder_factory->IsSupportedDecoder(format);
263 }));
264 EXPECT_CALL(*mock_decoder_factory, MakeAudioDecoderMock(_, _))
265 .Times(AtLeast(2))
266 .WillRepeatedly(
267 Invoke([real_decoder_factory](
268 const webrtc::SdpAudioFormat& format,
269 std::unique_ptr<webrtc::AudioDecoder>* return_value) {
270 auto real_decoder = real_decoder_factory->MakeAudioDecoder(format);
271 *return_value =
272 real_decoder
273 ? CreateForwardingMockDecoder(std::move(real_decoder))
274 : nullptr;
275 }));
276 return mock_decoder_factory;
277}
278
Karl Wibergc5bb00b2017-10-10 23:17:17 +0200279struct AudioEncoderUnicornSparklesRainbow {
280 using Config = webrtc::AudioEncoderL16::Config;
281 static rtc::Optional<Config> SdpToConfig(webrtc::SdpAudioFormat format) {
282 if (STR_CASE_CMP(format.name.c_str(), "UnicornSparklesRainbow") == 0) {
283 const webrtc::SdpAudioFormat::Parameters expected_params = {
284 {"num_horns", "1"}};
285 EXPECT_EQ(expected_params, format.parameters);
286 format.parameters.clear();
287 format.name = "L16";
288 return webrtc::AudioEncoderL16::SdpToConfig(format);
289 } else {
Oskar Sundbom63e232a2017-11-16 10:57:03 +0100290 return rtc::nullopt;
Karl Wibergc5bb00b2017-10-10 23:17:17 +0200291 }
292 }
293 static void AppendSupportedEncoders(
294 std::vector<webrtc::AudioCodecSpec>* specs) {
295 std::vector<webrtc::AudioCodecSpec> new_specs;
296 webrtc::AudioEncoderL16::AppendSupportedEncoders(&new_specs);
297 for (auto& spec : new_specs) {
298 spec.format.name = "UnicornSparklesRainbow";
299 EXPECT_TRUE(spec.format.parameters.empty());
300 spec.format.parameters.emplace("num_horns", "1");
301 specs->push_back(spec);
302 }
303 }
304 static webrtc::AudioCodecInfo QueryAudioEncoder(const Config& config) {
305 return webrtc::AudioEncoderL16::QueryAudioEncoder(config);
306 }
307 static std::unique_ptr<webrtc::AudioEncoder> MakeAudioEncoder(
308 const Config& config,
Karl Wiberg17668ec2018-03-01 15:13:27 +0100309 int payload_type,
310 rtc::Optional<webrtc::AudioCodecPairId> codec_pair_id = rtc::nullopt) {
311 return webrtc::AudioEncoderL16::MakeAudioEncoder(config, payload_type,
312 codec_pair_id);
Karl Wibergc5bb00b2017-10-10 23:17:17 +0200313 }
314};
315
316struct AudioDecoderUnicornSparklesRainbow {
317 using Config = webrtc::AudioDecoderL16::Config;
318 static rtc::Optional<Config> SdpToConfig(webrtc::SdpAudioFormat format) {
319 if (STR_CASE_CMP(format.name.c_str(), "UnicornSparklesRainbow") == 0) {
320 const webrtc::SdpAudioFormat::Parameters expected_params = {
321 {"num_horns", "1"}};
322 EXPECT_EQ(expected_params, format.parameters);
323 format.parameters.clear();
324 format.name = "L16";
325 return webrtc::AudioDecoderL16::SdpToConfig(format);
326 } else {
Oskar Sundbom63e232a2017-11-16 10:57:03 +0100327 return rtc::nullopt;
Karl Wibergc5bb00b2017-10-10 23:17:17 +0200328 }
329 }
330 static void AppendSupportedDecoders(
331 std::vector<webrtc::AudioCodecSpec>* specs) {
332 std::vector<webrtc::AudioCodecSpec> new_specs;
333 webrtc::AudioDecoderL16::AppendSupportedDecoders(&new_specs);
334 for (auto& spec : new_specs) {
335 spec.format.name = "UnicornSparklesRainbow";
336 EXPECT_TRUE(spec.format.parameters.empty());
337 spec.format.parameters.emplace("num_horns", "1");
338 specs->push_back(spec);
339 }
340 }
341 static std::unique_ptr<webrtc::AudioDecoder> MakeAudioDecoder(
Karl Wiberg17668ec2018-03-01 15:13:27 +0100342 const Config& config,
343 rtc::Optional<webrtc::AudioCodecPairId> codec_pair_id = rtc::nullopt) {
344 return webrtc::AudioDecoderL16::MakeAudioDecoder(config, codec_pair_id);
Karl Wibergc5bb00b2017-10-10 23:17:17 +0200345 }
346};
347
348} // namespace
349
kjellander@webrtc.org70c0e292015-11-30 21:45:35 +0100350// Disabled for TSan v2, see
351// https://bugs.chromium.org/p/webrtc/issues/detail?id=4719 for details.
kjellander@webrtc.org3c28d0d2015-12-02 22:53:26 +0100352// Disabled for Mac, see
353// https://bugs.chromium.org/p/webrtc/issues/detail?id=5231 for details.
Karl Wibergc5bb00b2017-10-10 23:17:17 +0200354#if defined(THREAD_SANITIZER) || defined(WEBRTC_MAC)
Steve Anton36da6ff2018-02-16 16:04:20 -0800355TEST_P(PeerConnectionEndToEndTest, DISABLED_Call) {
Karl Wibergc5bb00b2017-10-10 23:17:17 +0200356#else
Steve Anton36da6ff2018-02-16 16:04:20 -0800357TEST_P(PeerConnectionEndToEndTest, Call) {
358#endif // defined(THREAD_SANITIZER) || defined(WEBRTC_MAC)
kwiberg9e5b11e2017-04-19 03:47:57 -0700359 rtc::scoped_refptr<webrtc::AudioDecoderFactory> real_decoder_factory =
360 webrtc::CreateBuiltinAudioDecoderFactory();
361 CreatePcs(nullptr, webrtc::CreateBuiltinAudioEncoderFactory(),
362 CreateForwardingMockDecoderFactory(real_decoder_factory.get()));
wu@webrtc.org364f2042013-11-20 21:49:41 +0000363 GetAndAddUserMedia();
364 Negotiate();
365 WaitForCallEstablished();
366}
367
philipel7703b272016-11-28 16:23:12 +0100368#if !defined(ADDRESS_SANITIZER)
Steve Anton191c39f2018-01-24 19:35:55 -0800369TEST_P(PeerConnectionEndToEndTest, CallWithLegacySdp) {
wu@webrtc.org364f2042013-11-20 21:49:41 +0000370 FakeConstraints pc_constraints;
371 pc_constraints.AddMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
372 false);
kwiberg9e5b11e2017-04-19 03:47:57 -0700373 CreatePcs(&pc_constraints, webrtc::CreateBuiltinAudioEncoderFactory(),
374 webrtc::CreateBuiltinAudioDecoderFactory());
wu@webrtc.org364f2042013-11-20 21:49:41 +0000375 GetAndAddUserMedia();
376 Negotiate();
377 WaitForCallEstablished();
378}
philipel7703b272016-11-28 16:23:12 +0100379#endif // !defined(ADDRESS_SANITIZER)
wu@webrtc.orgb43202d2013-11-22 19:14:25 +0000380
Steve Anton191c39f2018-01-24 19:35:55 -0800381TEST_P(PeerConnectionEndToEndTest, CallWithCustomCodec) {
Karl Wibergc5bb00b2017-10-10 23:17:17 +0200382 CreatePcs(
383 nullptr,
384 webrtc::CreateAudioEncoderFactory<AudioEncoderUnicornSparklesRainbow>(),
385 webrtc::CreateAudioDecoderFactory<AudioDecoderUnicornSparklesRainbow>());
386 GetAndAddUserMedia();
387 Negotiate();
388 WaitForCallEstablished();
389}
390
deadbeef40610e22016-12-22 10:53:38 -0800391#ifdef HAVE_SCTP
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000392// Verifies that a DataChannel created before the negotiation can transition to
393// "OPEN" and transfer data.
Steve Anton191c39f2018-01-24 19:35:55 -0800394TEST_P(PeerConnectionEndToEndTest, CreateDataChannelBeforeNegotiate) {
kwiberg9e5b11e2017-04-19 03:47:57 -0700395 CreatePcs(nullptr, webrtc::CreateBuiltinAudioEncoderFactory(),
kwiberg7a12b5a2017-04-27 03:55:57 -0700396 webrtc::MockAudioDecoderFactory::CreateEmptyFactory());
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000397
398 webrtc::DataChannelInit init;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000399 rtc::scoped_refptr<DataChannelInterface> caller_dc(
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000400 caller_->CreateDataChannel("data", init));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000401 rtc::scoped_refptr<DataChannelInterface> callee_dc(
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000402 callee_->CreateDataChannel("data", init));
403
404 Negotiate();
405 WaitForConnection();
406
407 WaitForDataChannelsToOpen(caller_dc, callee_signaled_data_channels_, 0);
408 WaitForDataChannelsToOpen(callee_dc, caller_signaled_data_channels_, 0);
409
410 TestDataChannelSendAndReceive(caller_dc, callee_signaled_data_channels_[0]);
411 TestDataChannelSendAndReceive(callee_dc, caller_signaled_data_channels_[0]);
412
413 CloseDataChannels(caller_dc, callee_signaled_data_channels_, 0);
414 CloseDataChannels(callee_dc, caller_signaled_data_channels_, 0);
415}
416
417// Verifies that a DataChannel created after the negotiation can transition to
418// "OPEN" and transfer data.
Steve Anton191c39f2018-01-24 19:35:55 -0800419TEST_P(PeerConnectionEndToEndTest, CreateDataChannelAfterNegotiate) {
kwiberg9e5b11e2017-04-19 03:47:57 -0700420 CreatePcs(nullptr, webrtc::CreateBuiltinAudioEncoderFactory(),
kwiberg7a12b5a2017-04-27 03:55:57 -0700421 webrtc::MockAudioDecoderFactory::CreateEmptyFactory());
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000422
423 webrtc::DataChannelInit init;
424
425 // This DataChannel is for creating the data content in the negotiation.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000426 rtc::scoped_refptr<DataChannelInterface> dummy(
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000427 caller_->CreateDataChannel("data", init));
428 Negotiate();
429 WaitForConnection();
430
Taylor Brandstetterbf2f5692016-06-29 11:22:47 -0700431 // Wait for the data channel created pre-negotiation to be opened.
432 WaitForDataChannelsToOpen(dummy, callee_signaled_data_channels_, 0);
433
434 // Create new DataChannels after the negotiation and verify their states.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000435 rtc::scoped_refptr<DataChannelInterface> caller_dc(
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000436 caller_->CreateDataChannel("hello", init));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000437 rtc::scoped_refptr<DataChannelInterface> callee_dc(
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000438 callee_->CreateDataChannel("hello", init));
439
440 WaitForDataChannelsToOpen(caller_dc, callee_signaled_data_channels_, 1);
441 WaitForDataChannelsToOpen(callee_dc, caller_signaled_data_channels_, 0);
442
443 TestDataChannelSendAndReceive(caller_dc, callee_signaled_data_channels_[1]);
444 TestDataChannelSendAndReceive(callee_dc, caller_signaled_data_channels_[0]);
445
446 CloseDataChannels(caller_dc, callee_signaled_data_channels_, 1);
447 CloseDataChannels(callee_dc, caller_signaled_data_channels_, 0);
448}
449
450// Verifies that DataChannel IDs are even/odd based on the DTLS roles.
Steve Anton191c39f2018-01-24 19:35:55 -0800451TEST_P(PeerConnectionEndToEndTest, DataChannelIdAssignment) {
kwiberg9e5b11e2017-04-19 03:47:57 -0700452 CreatePcs(nullptr, webrtc::CreateBuiltinAudioEncoderFactory(),
kwiberg7a12b5a2017-04-27 03:55:57 -0700453 webrtc::MockAudioDecoderFactory::CreateEmptyFactory());
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000454
455 webrtc::DataChannelInit init;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000456 rtc::scoped_refptr<DataChannelInterface> caller_dc_1(
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000457 caller_->CreateDataChannel("data", init));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000458 rtc::scoped_refptr<DataChannelInterface> callee_dc_1(
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000459 callee_->CreateDataChannel("data", init));
460
461 Negotiate();
462 WaitForConnection();
463
464 EXPECT_EQ(1U, caller_dc_1->id() % 2);
465 EXPECT_EQ(0U, callee_dc_1->id() % 2);
466
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000467 rtc::scoped_refptr<DataChannelInterface> caller_dc_2(
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000468 caller_->CreateDataChannel("data", init));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000469 rtc::scoped_refptr<DataChannelInterface> callee_dc_2(
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000470 callee_->CreateDataChannel("data", init));
471
472 EXPECT_EQ(1U, caller_dc_2->id() % 2);
473 EXPECT_EQ(0U, callee_dc_2->id() % 2);
474}
475
476// Verifies that the message is received by the right remote DataChannel when
477// there are multiple DataChannels.
Steve Anton191c39f2018-01-24 19:35:55 -0800478TEST_P(PeerConnectionEndToEndTest,
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000479 MessageTransferBetweenTwoPairsOfDataChannels) {
kwiberg9e5b11e2017-04-19 03:47:57 -0700480 CreatePcs(nullptr, webrtc::CreateBuiltinAudioEncoderFactory(),
kwiberg7a12b5a2017-04-27 03:55:57 -0700481 webrtc::MockAudioDecoderFactory::CreateEmptyFactory());
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000482
483 webrtc::DataChannelInit init;
484
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000485 rtc::scoped_refptr<DataChannelInterface> caller_dc_1(
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000486 caller_->CreateDataChannel("data", init));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000487 rtc::scoped_refptr<DataChannelInterface> caller_dc_2(
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000488 caller_->CreateDataChannel("data", init));
489
490 Negotiate();
491 WaitForConnection();
492 WaitForDataChannelsToOpen(caller_dc_1, callee_signaled_data_channels_, 0);
493 WaitForDataChannelsToOpen(caller_dc_2, callee_signaled_data_channels_, 1);
494
kwibergd1fe2812016-04-27 06:47:29 -0700495 std::unique_ptr<webrtc::MockDataChannelObserver> dc_1_observer(
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000496 new webrtc::MockDataChannelObserver(callee_signaled_data_channels_[0]));
497
kwibergd1fe2812016-04-27 06:47:29 -0700498 std::unique_ptr<webrtc::MockDataChannelObserver> dc_2_observer(
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000499 new webrtc::MockDataChannelObserver(callee_signaled_data_channels_[1]));
500
501 const std::string message_1 = "hello 1";
502 const std::string message_2 = "hello 2";
503
504 caller_dc_1->Send(webrtc::DataBuffer(message_1));
505 EXPECT_EQ_WAIT(message_1, dc_1_observer->last_message(), kMaxWait);
506
507 caller_dc_2->Send(webrtc::DataBuffer(message_2));
508 EXPECT_EQ_WAIT(message_2, dc_2_observer->last_message(), kMaxWait);
509
510 EXPECT_EQ(1U, dc_1_observer->received_message_count());
511 EXPECT_EQ(1U, dc_2_observer->received_message_count());
512}
deadbeefab9b2d12015-10-14 11:33:11 -0700513
514// Verifies that a DataChannel added from an OPEN message functions after
515// a channel has been previously closed (webrtc issue 3778).
516// This previously failed because the new channel re-uses the ID of the closed
517// channel, and the closed channel was incorrectly still assigned to the id.
518// TODO(deadbeef): This is disabled because there's currently a race condition
519// caused by the fact that a data channel signals that it's closed before it
520// really is. Re-enable this test once that's fixed.
deadbeefe2213ce2016-11-03 16:01:57 -0700521// See: https://bugs.chromium.org/p/webrtc/issues/detail?id=4453
Steve Anton191c39f2018-01-24 19:35:55 -0800522TEST_P(PeerConnectionEndToEndTest,
deadbeefab9b2d12015-10-14 11:33:11 -0700523 DISABLED_DataChannelFromOpenWorksAfterClose) {
kwiberg9e5b11e2017-04-19 03:47:57 -0700524 CreatePcs(nullptr, webrtc::CreateBuiltinAudioEncoderFactory(),
kwiberg7a12b5a2017-04-27 03:55:57 -0700525 webrtc::MockAudioDecoderFactory::CreateEmptyFactory());
deadbeefab9b2d12015-10-14 11:33:11 -0700526
527 webrtc::DataChannelInit init;
528 rtc::scoped_refptr<DataChannelInterface> caller_dc(
529 caller_->CreateDataChannel("data", init));
530
531 Negotiate();
532 WaitForConnection();
533
534 WaitForDataChannelsToOpen(caller_dc, callee_signaled_data_channels_, 0);
535 CloseDataChannels(caller_dc, callee_signaled_data_channels_, 0);
536
537 // Create a new channel and ensure it works after closing the previous one.
538 caller_dc = caller_->CreateDataChannel("data2", init);
539
540 WaitForDataChannelsToOpen(caller_dc, callee_signaled_data_channels_, 1);
541 TestDataChannelSendAndReceive(caller_dc, callee_signaled_data_channels_[1]);
542
543 CloseDataChannels(caller_dc, callee_signaled_data_channels_, 1);
544}
deadbeefbd292462015-12-14 18:15:29 -0800545
546// This tests that if a data channel is closed remotely while not referenced
547// by the application (meaning only the PeerConnection contributes to its
548// reference count), no memory access violation will occur.
549// See: https://code.google.com/p/chromium/issues/detail?id=565048
Steve Anton191c39f2018-01-24 19:35:55 -0800550TEST_P(PeerConnectionEndToEndTest, CloseDataChannelRemotelyWhileNotReferenced) {
kwiberg9e5b11e2017-04-19 03:47:57 -0700551 CreatePcs(nullptr, webrtc::CreateBuiltinAudioEncoderFactory(),
kwiberg7a12b5a2017-04-27 03:55:57 -0700552 webrtc::MockAudioDecoderFactory::CreateEmptyFactory());
deadbeefbd292462015-12-14 18:15:29 -0800553
554 webrtc::DataChannelInit init;
555 rtc::scoped_refptr<DataChannelInterface> caller_dc(
556 caller_->CreateDataChannel("data", init));
557
558 Negotiate();
559 WaitForConnection();
560
561 WaitForDataChannelsToOpen(caller_dc, callee_signaled_data_channels_, 0);
562 // This removes the reference to the remote data channel that we hold.
563 callee_signaled_data_channels_.clear();
564 caller_dc->Close();
565 EXPECT_EQ_WAIT(DataChannelInterface::kClosed, caller_dc->state(), kMaxWait);
566
567 // Wait for a bit longer so the remote data channel will receive the
568 // close message and be destroyed.
569 rtc::Thread::Current()->ProcessMessages(100);
570}
deadbeef40610e22016-12-22 10:53:38 -0800571#endif // HAVE_SCTP
Steve Anton191c39f2018-01-24 19:35:55 -0800572
573INSTANTIATE_TEST_CASE_P(PeerConnectionEndToEndTest,
574 PeerConnectionEndToEndTest,
575 Values(SdpSemantics::kPlanB,
576 SdpSemantics::kUnifiedPlan));