blob: 51ce29f7f280003dab7044f9ad46c1c0cf0a6320 [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 }));
Karl Wibergd6fbf2a2018-02-27 13:37:31 +0100264 EXPECT_CALL(*mock_decoder_factory, MakeAudioDecoderMock(_, _, _))
kwiberg9e5b11e2017-04-19 03:47:57 -0700265 .Times(AtLeast(2))
266 .WillRepeatedly(
267 Invoke([real_decoder_factory](
268 const webrtc::SdpAudioFormat& format,
Karl Wibergd6fbf2a2018-02-27 13:37:31 +0100269 rtc::Optional<webrtc::AudioCodecPairId> codec_pair_id,
kwiberg9e5b11e2017-04-19 03:47:57 -0700270 std::unique_ptr<webrtc::AudioDecoder>* return_value) {
Karl Wibergd6fbf2a2018-02-27 13:37:31 +0100271 auto real_decoder =
272 real_decoder_factory->MakeAudioDecoder(format, codec_pair_id);
kwiberg9e5b11e2017-04-19 03:47:57 -0700273 *return_value =
274 real_decoder
275 ? CreateForwardingMockDecoder(std::move(real_decoder))
276 : nullptr;
277 }));
278 return mock_decoder_factory;
279}
280
Karl Wibergc5bb00b2017-10-10 23:17:17 +0200281struct AudioEncoderUnicornSparklesRainbow {
282 using Config = webrtc::AudioEncoderL16::Config;
283 static rtc::Optional<Config> SdpToConfig(webrtc::SdpAudioFormat format) {
284 if (STR_CASE_CMP(format.name.c_str(), "UnicornSparklesRainbow") == 0) {
285 const webrtc::SdpAudioFormat::Parameters expected_params = {
286 {"num_horns", "1"}};
287 EXPECT_EQ(expected_params, format.parameters);
288 format.parameters.clear();
289 format.name = "L16";
290 return webrtc::AudioEncoderL16::SdpToConfig(format);
291 } else {
Oskar Sundbom63e232a2017-11-16 10:57:03 +0100292 return rtc::nullopt;
Karl Wibergc5bb00b2017-10-10 23:17:17 +0200293 }
294 }
295 static void AppendSupportedEncoders(
296 std::vector<webrtc::AudioCodecSpec>* specs) {
297 std::vector<webrtc::AudioCodecSpec> new_specs;
298 webrtc::AudioEncoderL16::AppendSupportedEncoders(&new_specs);
299 for (auto& spec : new_specs) {
300 spec.format.name = "UnicornSparklesRainbow";
301 EXPECT_TRUE(spec.format.parameters.empty());
302 spec.format.parameters.emplace("num_horns", "1");
303 specs->push_back(spec);
304 }
305 }
306 static webrtc::AudioCodecInfo QueryAudioEncoder(const Config& config) {
307 return webrtc::AudioEncoderL16::QueryAudioEncoder(config);
308 }
309 static std::unique_ptr<webrtc::AudioEncoder> MakeAudioEncoder(
310 const Config& config,
Karl Wiberg17668ec2018-03-01 15:13:27 +0100311 int payload_type,
312 rtc::Optional<webrtc::AudioCodecPairId> codec_pair_id = rtc::nullopt) {
313 return webrtc::AudioEncoderL16::MakeAudioEncoder(config, payload_type,
314 codec_pair_id);
Karl Wibergc5bb00b2017-10-10 23:17:17 +0200315 }
316};
317
318struct AudioDecoderUnicornSparklesRainbow {
319 using Config = webrtc::AudioDecoderL16::Config;
320 static rtc::Optional<Config> SdpToConfig(webrtc::SdpAudioFormat format) {
321 if (STR_CASE_CMP(format.name.c_str(), "UnicornSparklesRainbow") == 0) {
322 const webrtc::SdpAudioFormat::Parameters expected_params = {
323 {"num_horns", "1"}};
324 EXPECT_EQ(expected_params, format.parameters);
325 format.parameters.clear();
326 format.name = "L16";
327 return webrtc::AudioDecoderL16::SdpToConfig(format);
328 } else {
Oskar Sundbom63e232a2017-11-16 10:57:03 +0100329 return rtc::nullopt;
Karl Wibergc5bb00b2017-10-10 23:17:17 +0200330 }
331 }
332 static void AppendSupportedDecoders(
333 std::vector<webrtc::AudioCodecSpec>* specs) {
334 std::vector<webrtc::AudioCodecSpec> new_specs;
335 webrtc::AudioDecoderL16::AppendSupportedDecoders(&new_specs);
336 for (auto& spec : new_specs) {
337 spec.format.name = "UnicornSparklesRainbow";
338 EXPECT_TRUE(spec.format.parameters.empty());
339 spec.format.parameters.emplace("num_horns", "1");
340 specs->push_back(spec);
341 }
342 }
343 static std::unique_ptr<webrtc::AudioDecoder> MakeAudioDecoder(
Karl Wiberg17668ec2018-03-01 15:13:27 +0100344 const Config& config,
345 rtc::Optional<webrtc::AudioCodecPairId> codec_pair_id = rtc::nullopt) {
346 return webrtc::AudioDecoderL16::MakeAudioDecoder(config, codec_pair_id);
Karl Wibergc5bb00b2017-10-10 23:17:17 +0200347 }
348};
349
350} // namespace
351
kjellander@webrtc.org70c0e292015-11-30 21:45:35 +0100352// Disabled for TSan v2, see
353// https://bugs.chromium.org/p/webrtc/issues/detail?id=4719 for details.
kjellander@webrtc.org3c28d0d2015-12-02 22:53:26 +0100354// Disabled for Mac, see
355// https://bugs.chromium.org/p/webrtc/issues/detail?id=5231 for details.
Karl Wibergc5bb00b2017-10-10 23:17:17 +0200356#if defined(THREAD_SANITIZER) || defined(WEBRTC_MAC)
Steve Anton36da6ff2018-02-16 16:04:20 -0800357TEST_P(PeerConnectionEndToEndTest, DISABLED_Call) {
Karl Wibergc5bb00b2017-10-10 23:17:17 +0200358#else
Steve Anton36da6ff2018-02-16 16:04:20 -0800359TEST_P(PeerConnectionEndToEndTest, Call) {
360#endif // defined(THREAD_SANITIZER) || defined(WEBRTC_MAC)
kwiberg9e5b11e2017-04-19 03:47:57 -0700361 rtc::scoped_refptr<webrtc::AudioDecoderFactory> real_decoder_factory =
362 webrtc::CreateBuiltinAudioDecoderFactory();
363 CreatePcs(nullptr, webrtc::CreateBuiltinAudioEncoderFactory(),
364 CreateForwardingMockDecoderFactory(real_decoder_factory.get()));
wu@webrtc.org364f2042013-11-20 21:49:41 +0000365 GetAndAddUserMedia();
366 Negotiate();
367 WaitForCallEstablished();
368}
369
philipel7703b272016-11-28 16:23:12 +0100370#if !defined(ADDRESS_SANITIZER)
Steve Anton191c39f2018-01-24 19:35:55 -0800371TEST_P(PeerConnectionEndToEndTest, CallWithLegacySdp) {
wu@webrtc.org364f2042013-11-20 21:49:41 +0000372 FakeConstraints pc_constraints;
373 pc_constraints.AddMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
374 false);
kwiberg9e5b11e2017-04-19 03:47:57 -0700375 CreatePcs(&pc_constraints, webrtc::CreateBuiltinAudioEncoderFactory(),
376 webrtc::CreateBuiltinAudioDecoderFactory());
wu@webrtc.org364f2042013-11-20 21:49:41 +0000377 GetAndAddUserMedia();
378 Negotiate();
379 WaitForCallEstablished();
380}
philipel7703b272016-11-28 16:23:12 +0100381#endif // !defined(ADDRESS_SANITIZER)
wu@webrtc.orgb43202d2013-11-22 19:14:25 +0000382
Steve Anton191c39f2018-01-24 19:35:55 -0800383TEST_P(PeerConnectionEndToEndTest, CallWithCustomCodec) {
Karl Wibergc5bb00b2017-10-10 23:17:17 +0200384 CreatePcs(
385 nullptr,
386 webrtc::CreateAudioEncoderFactory<AudioEncoderUnicornSparklesRainbow>(),
387 webrtc::CreateAudioDecoderFactory<AudioDecoderUnicornSparklesRainbow>());
388 GetAndAddUserMedia();
389 Negotiate();
390 WaitForCallEstablished();
391}
392
deadbeef40610e22016-12-22 10:53:38 -0800393#ifdef HAVE_SCTP
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000394// Verifies that a DataChannel created before the negotiation can transition to
395// "OPEN" and transfer data.
Steve Anton191c39f2018-01-24 19:35:55 -0800396TEST_P(PeerConnectionEndToEndTest, CreateDataChannelBeforeNegotiate) {
kwiberg9e5b11e2017-04-19 03:47:57 -0700397 CreatePcs(nullptr, webrtc::CreateBuiltinAudioEncoderFactory(),
kwiberg7a12b5a2017-04-27 03:55:57 -0700398 webrtc::MockAudioDecoderFactory::CreateEmptyFactory());
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000399
400 webrtc::DataChannelInit init;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000401 rtc::scoped_refptr<DataChannelInterface> caller_dc(
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000402 caller_->CreateDataChannel("data", init));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000403 rtc::scoped_refptr<DataChannelInterface> callee_dc(
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000404 callee_->CreateDataChannel("data", init));
405
406 Negotiate();
407 WaitForConnection();
408
409 WaitForDataChannelsToOpen(caller_dc, callee_signaled_data_channels_, 0);
410 WaitForDataChannelsToOpen(callee_dc, caller_signaled_data_channels_, 0);
411
412 TestDataChannelSendAndReceive(caller_dc, callee_signaled_data_channels_[0]);
413 TestDataChannelSendAndReceive(callee_dc, caller_signaled_data_channels_[0]);
414
415 CloseDataChannels(caller_dc, callee_signaled_data_channels_, 0);
416 CloseDataChannels(callee_dc, caller_signaled_data_channels_, 0);
417}
418
419// Verifies that a DataChannel created after the negotiation can transition to
420// "OPEN" and transfer data.
Steve Anton191c39f2018-01-24 19:35:55 -0800421TEST_P(PeerConnectionEndToEndTest, CreateDataChannelAfterNegotiate) {
kwiberg9e5b11e2017-04-19 03:47:57 -0700422 CreatePcs(nullptr, webrtc::CreateBuiltinAudioEncoderFactory(),
kwiberg7a12b5a2017-04-27 03:55:57 -0700423 webrtc::MockAudioDecoderFactory::CreateEmptyFactory());
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000424
425 webrtc::DataChannelInit init;
426
427 // This DataChannel is for creating the data content in the negotiation.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000428 rtc::scoped_refptr<DataChannelInterface> dummy(
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000429 caller_->CreateDataChannel("data", init));
430 Negotiate();
431 WaitForConnection();
432
Taylor Brandstetterbf2f5692016-06-29 11:22:47 -0700433 // Wait for the data channel created pre-negotiation to be opened.
434 WaitForDataChannelsToOpen(dummy, callee_signaled_data_channels_, 0);
435
436 // Create new DataChannels after the negotiation and verify their states.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000437 rtc::scoped_refptr<DataChannelInterface> caller_dc(
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000438 caller_->CreateDataChannel("hello", init));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000439 rtc::scoped_refptr<DataChannelInterface> callee_dc(
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000440 callee_->CreateDataChannel("hello", init));
441
442 WaitForDataChannelsToOpen(caller_dc, callee_signaled_data_channels_, 1);
443 WaitForDataChannelsToOpen(callee_dc, caller_signaled_data_channels_, 0);
444
445 TestDataChannelSendAndReceive(caller_dc, callee_signaled_data_channels_[1]);
446 TestDataChannelSendAndReceive(callee_dc, caller_signaled_data_channels_[0]);
447
448 CloseDataChannels(caller_dc, callee_signaled_data_channels_, 1);
449 CloseDataChannels(callee_dc, caller_signaled_data_channels_, 0);
450}
451
452// Verifies that DataChannel IDs are even/odd based on the DTLS roles.
Steve Anton191c39f2018-01-24 19:35:55 -0800453TEST_P(PeerConnectionEndToEndTest, DataChannelIdAssignment) {
kwiberg9e5b11e2017-04-19 03:47:57 -0700454 CreatePcs(nullptr, webrtc::CreateBuiltinAudioEncoderFactory(),
kwiberg7a12b5a2017-04-27 03:55:57 -0700455 webrtc::MockAudioDecoderFactory::CreateEmptyFactory());
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000456
457 webrtc::DataChannelInit init;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000458 rtc::scoped_refptr<DataChannelInterface> caller_dc_1(
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000459 caller_->CreateDataChannel("data", init));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000460 rtc::scoped_refptr<DataChannelInterface> callee_dc_1(
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000461 callee_->CreateDataChannel("data", init));
462
463 Negotiate();
464 WaitForConnection();
465
466 EXPECT_EQ(1U, caller_dc_1->id() % 2);
467 EXPECT_EQ(0U, callee_dc_1->id() % 2);
468
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000469 rtc::scoped_refptr<DataChannelInterface> caller_dc_2(
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000470 caller_->CreateDataChannel("data", init));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000471 rtc::scoped_refptr<DataChannelInterface> callee_dc_2(
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000472 callee_->CreateDataChannel("data", init));
473
474 EXPECT_EQ(1U, caller_dc_2->id() % 2);
475 EXPECT_EQ(0U, callee_dc_2->id() % 2);
476}
477
478// Verifies that the message is received by the right remote DataChannel when
479// there are multiple DataChannels.
Steve Anton191c39f2018-01-24 19:35:55 -0800480TEST_P(PeerConnectionEndToEndTest,
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000481 MessageTransferBetweenTwoPairsOfDataChannels) {
kwiberg9e5b11e2017-04-19 03:47:57 -0700482 CreatePcs(nullptr, webrtc::CreateBuiltinAudioEncoderFactory(),
kwiberg7a12b5a2017-04-27 03:55:57 -0700483 webrtc::MockAudioDecoderFactory::CreateEmptyFactory());
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000484
485 webrtc::DataChannelInit init;
486
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000487 rtc::scoped_refptr<DataChannelInterface> caller_dc_1(
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000488 caller_->CreateDataChannel("data", init));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000489 rtc::scoped_refptr<DataChannelInterface> caller_dc_2(
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000490 caller_->CreateDataChannel("data", init));
491
492 Negotiate();
493 WaitForConnection();
494 WaitForDataChannelsToOpen(caller_dc_1, callee_signaled_data_channels_, 0);
495 WaitForDataChannelsToOpen(caller_dc_2, callee_signaled_data_channels_, 1);
496
kwibergd1fe2812016-04-27 06:47:29 -0700497 std::unique_ptr<webrtc::MockDataChannelObserver> dc_1_observer(
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000498 new webrtc::MockDataChannelObserver(callee_signaled_data_channels_[0]));
499
kwibergd1fe2812016-04-27 06:47:29 -0700500 std::unique_ptr<webrtc::MockDataChannelObserver> dc_2_observer(
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000501 new webrtc::MockDataChannelObserver(callee_signaled_data_channels_[1]));
502
503 const std::string message_1 = "hello 1";
504 const std::string message_2 = "hello 2";
505
506 caller_dc_1->Send(webrtc::DataBuffer(message_1));
507 EXPECT_EQ_WAIT(message_1, dc_1_observer->last_message(), kMaxWait);
508
509 caller_dc_2->Send(webrtc::DataBuffer(message_2));
510 EXPECT_EQ_WAIT(message_2, dc_2_observer->last_message(), kMaxWait);
511
512 EXPECT_EQ(1U, dc_1_observer->received_message_count());
513 EXPECT_EQ(1U, dc_2_observer->received_message_count());
514}
deadbeefab9b2d12015-10-14 11:33:11 -0700515
516// Verifies that a DataChannel added from an OPEN message functions after
517// a channel has been previously closed (webrtc issue 3778).
518// This previously failed because the new channel re-uses the ID of the closed
519// channel, and the closed channel was incorrectly still assigned to the id.
520// TODO(deadbeef): This is disabled because there's currently a race condition
521// caused by the fact that a data channel signals that it's closed before it
522// really is. Re-enable this test once that's fixed.
deadbeefe2213ce2016-11-03 16:01:57 -0700523// See: https://bugs.chromium.org/p/webrtc/issues/detail?id=4453
Steve Anton191c39f2018-01-24 19:35:55 -0800524TEST_P(PeerConnectionEndToEndTest,
deadbeefab9b2d12015-10-14 11:33:11 -0700525 DISABLED_DataChannelFromOpenWorksAfterClose) {
kwiberg9e5b11e2017-04-19 03:47:57 -0700526 CreatePcs(nullptr, webrtc::CreateBuiltinAudioEncoderFactory(),
kwiberg7a12b5a2017-04-27 03:55:57 -0700527 webrtc::MockAudioDecoderFactory::CreateEmptyFactory());
deadbeefab9b2d12015-10-14 11:33:11 -0700528
529 webrtc::DataChannelInit init;
530 rtc::scoped_refptr<DataChannelInterface> caller_dc(
531 caller_->CreateDataChannel("data", init));
532
533 Negotiate();
534 WaitForConnection();
535
536 WaitForDataChannelsToOpen(caller_dc, callee_signaled_data_channels_, 0);
537 CloseDataChannels(caller_dc, callee_signaled_data_channels_, 0);
538
539 // Create a new channel and ensure it works after closing the previous one.
540 caller_dc = caller_->CreateDataChannel("data2", init);
541
542 WaitForDataChannelsToOpen(caller_dc, callee_signaled_data_channels_, 1);
543 TestDataChannelSendAndReceive(caller_dc, callee_signaled_data_channels_[1]);
544
545 CloseDataChannels(caller_dc, callee_signaled_data_channels_, 1);
546}
deadbeefbd292462015-12-14 18:15:29 -0800547
548// This tests that if a data channel is closed remotely while not referenced
549// by the application (meaning only the PeerConnection contributes to its
550// reference count), no memory access violation will occur.
551// See: https://code.google.com/p/chromium/issues/detail?id=565048
Steve Anton191c39f2018-01-24 19:35:55 -0800552TEST_P(PeerConnectionEndToEndTest, CloseDataChannelRemotelyWhileNotReferenced) {
kwiberg9e5b11e2017-04-19 03:47:57 -0700553 CreatePcs(nullptr, webrtc::CreateBuiltinAudioEncoderFactory(),
kwiberg7a12b5a2017-04-27 03:55:57 -0700554 webrtc::MockAudioDecoderFactory::CreateEmptyFactory());
deadbeefbd292462015-12-14 18:15:29 -0800555
556 webrtc::DataChannelInit init;
557 rtc::scoped_refptr<DataChannelInterface> caller_dc(
558 caller_->CreateDataChannel("data", init));
559
560 Negotiate();
561 WaitForConnection();
562
563 WaitForDataChannelsToOpen(caller_dc, callee_signaled_data_channels_, 0);
564 // This removes the reference to the remote data channel that we hold.
565 callee_signaled_data_channels_.clear();
566 caller_dc->Close();
567 EXPECT_EQ_WAIT(DataChannelInterface::kClosed, caller_dc->state(), kMaxWait);
568
569 // Wait for a bit longer so the remote data channel will receive the
570 // close message and be destroyed.
571 rtc::Thread::Current()->ProcessMessages(100);
572}
deadbeef40610e22016-12-22 10:53:38 -0800573#endif // HAVE_SCTP
Steve Anton191c39f2018-01-24 19:35:55 -0800574
575INSTANTIATE_TEST_CASE_P(PeerConnectionEndToEndTest,
576 PeerConnectionEndToEndTest,
577 Values(SdpSemantics::kPlanB,
578 SdpSemantics::kUnifiedPlan));