blob: b01a69b0a8afe47e0e32ce2bcc94a54b9854419d [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 Wiberg918f50c2018-07-05 11:40:33 +020013#include "absl/memory/memory.h"
Niels Möller2edab4c2018-10-22 09:48:08 +020014#include "absl/strings/match.h"
Karl Wibergc5bb00b2017-10-10 23:17:17 +020015#include "api/audio_codecs/L16/audio_decoder_L16.h"
16#include "api/audio_codecs/L16/audio_encoder_L16.h"
Karl Wiberg17668ec2018-03-01 15:13:27 +010017#include "api/audio_codecs/audio_codec_pair_id.h"
Karl Wibergc5bb00b2017-10-10 23:17:17 +020018#include "api/audio_codecs/audio_decoder_factory_template.h"
19#include "api/audio_codecs/audio_encoder_factory_template.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#include "api/audio_codecs/builtin_audio_decoder_factory.h"
21#include "api/audio_codecs/builtin_audio_encoder_factory.h"
22#include "rtc_base/gunit.h"
23#include "rtc_base/logging.h"
Patrik Höglund563934e2017-09-15 09:04:28 +020024
ossu7bb87ee2017-01-23 04:56:25 -080025#ifdef WEBRTC_ANDROID
Steve Anton10542f22019-01-11 09:11:00 -080026#include "pc/test/android_test_initializer.h"
ossu7bb87ee2017-01-23 04:56:25 -080027#endif
Steve Anton10542f22019-01-11 09:11:00 -080028#include "pc/test/peer_connection_test_wrapper.h"
ossu7bb87ee2017-01-23 04:56:25 -080029// Notice that mockpeerconnectionobservers.h must be included after the above!
Steve Anton10542f22019-01-11 09:11:00 -080030#include "pc/test/mock_peer_connection_observers.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020031#include "test/mock_audio_decoder.h"
32#include "test/mock_audio_decoder_factory.h"
Karl Wibergbc4cf892018-11-13 13:20:51 +010033#include "test/mock_audio_encoder_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;
wu@webrtc.org364f2042013-11-20 21:49:41 +000043using webrtc::MediaStreamInterface;
44using webrtc::PeerConnectionInterface;
Steve Anton191c39f2018-01-24 19:35:55 -080045using webrtc::SdpSemantics;
wu@webrtc.org364f2042013-11-20 21:49:41 +000046
47namespace {
48
Jeroen de Borst4f6d2332018-07-18 11:25:12 -070049const int kMaxWait = 25000;
wu@webrtc.org364f2042013-11-20 21:49:41 +000050
wu@webrtc.org364f2042013-11-20 21:49:41 +000051} // namespace
52
Steve Anton191c39f2018-01-24 19:35:55 -080053class PeerConnectionEndToEndBaseTest : public sigslot::has_slots<>,
54 public testing::Test {
wu@webrtc.org364f2042013-11-20 21:49:41 +000055 public:
Yves Gerey665174f2018-06-19 15:03:05 +020056 typedef std::vector<rtc::scoped_refptr<DataChannelInterface>> DataChannelList;
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +000057
Steve Anton191c39f2018-01-24 19:35:55 -080058 explicit PeerConnectionEndToEndBaseTest(SdpSemantics sdp_semantics) {
tommie7251592017-07-14 14:44:46 -070059 network_thread_ = rtc::Thread::CreateWithSocketServer();
60 worker_thread_ = rtc::Thread::Create();
61 RTC_CHECK(network_thread_->Start());
62 RTC_CHECK(worker_thread_->Start());
perkj57db6522016-04-08 08:16:33 -070063 caller_ = new rtc::RefCountedObject<PeerConnectionTestWrapper>(
tommie7251592017-07-14 14:44:46 -070064 "caller", network_thread_.get(), worker_thread_.get());
perkj57db6522016-04-08 08:16:33 -070065 callee_ = new rtc::RefCountedObject<PeerConnectionTestWrapper>(
tommie7251592017-07-14 14:44:46 -070066 "callee", network_thread_.get(), worker_thread_.get());
zhihuang9763d562016-08-05 11:14:50 -070067 webrtc::PeerConnectionInterface::IceServer ice_server;
68 ice_server.uri = "stun:stun.l.google.com:19302";
69 config_.servers.push_back(ice_server);
Steve Anton191c39f2018-01-24 19:35:55 -080070 config_.sdp_semantics = sdp_semantics;
zhihuang9763d562016-08-05 11:14:50 -070071
phoglund37ebcf02016-01-08 05:04:57 -080072#ifdef WEBRTC_ANDROID
73 webrtc::InitializeAndroidObjects();
74#endif
wu@webrtc.org364f2042013-11-20 21:49:41 +000075 }
76
Karl Wiberg5bdc82a2018-03-22 00:07:39 +010077 void CreatePcs(
Karl Wiberg5bdc82a2018-03-22 00:07:39 +010078 rtc::scoped_refptr<webrtc::AudioEncoderFactory> audio_encoder_factory1,
79 rtc::scoped_refptr<webrtc::AudioDecoderFactory> audio_decoder_factory1,
80 rtc::scoped_refptr<webrtc::AudioEncoderFactory> audio_encoder_factory2,
81 rtc::scoped_refptr<webrtc::AudioDecoderFactory> audio_decoder_factory2) {
Niels Möllerf06f9232018-08-07 12:32:18 +020082 EXPECT_TRUE(caller_->CreatePc(config_, audio_encoder_factory1,
Karl Wiberg5bdc82a2018-03-22 00:07:39 +010083 audio_decoder_factory1));
Niels Möllerf06f9232018-08-07 12:32:18 +020084 EXPECT_TRUE(callee_->CreatePc(config_, audio_encoder_factory2,
Karl Wiberg5bdc82a2018-03-22 00:07:39 +010085 audio_decoder_factory2));
wu@webrtc.org364f2042013-11-20 21:49:41 +000086 PeerConnectionTestWrapper::Connect(caller_.get(), callee_.get());
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +000087
88 caller_->SignalOnDataChannel.connect(
Steve Anton191c39f2018-01-24 19:35:55 -080089 this, &PeerConnectionEndToEndBaseTest::OnCallerAddedDataChanel);
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +000090 callee_->SignalOnDataChannel.connect(
Steve Anton191c39f2018-01-24 19:35:55 -080091 this, &PeerConnectionEndToEndBaseTest::OnCalleeAddedDataChannel);
wu@webrtc.org364f2042013-11-20 21:49:41 +000092 }
93
Karl Wiberg5bdc82a2018-03-22 00:07:39 +010094 void CreatePcs(
Karl Wiberg5bdc82a2018-03-22 00:07:39 +010095 rtc::scoped_refptr<webrtc::AudioEncoderFactory> audio_encoder_factory,
96 rtc::scoped_refptr<webrtc::AudioDecoderFactory> audio_decoder_factory) {
Niels Möllerf06f9232018-08-07 12:32:18 +020097 CreatePcs(audio_encoder_factory, audio_decoder_factory,
Karl Wiberg5bdc82a2018-03-22 00:07:39 +010098 audio_encoder_factory, audio_decoder_factory);
99 }
100
wu@webrtc.org364f2042013-11-20 21:49:41 +0000101 void GetAndAddUserMedia() {
Niels Möller2d02e082018-05-21 11:23:35 +0200102 cricket::AudioOptions audio_options;
wu@webrtc.org364f2042013-11-20 21:49:41 +0000103 FakeConstraints video_constraints;
Niels Möller2d02e082018-05-21 11:23:35 +0200104 GetAndAddUserMedia(true, audio_options, true, video_constraints);
wu@webrtc.org364f2042013-11-20 21:49:41 +0000105 }
106
Mirko Bonadeic61ce0d2017-11-21 17:04:20 +0100107 void GetAndAddUserMedia(bool audio,
Niels Möller2d02e082018-05-21 11:23:35 +0200108 const cricket::AudioOptions& audio_options,
Mirko Bonadeic61ce0d2017-11-21 17:04:20 +0100109 bool video,
110 const FakeConstraints& video_constraints) {
Yves Gerey665174f2018-06-19 15:03:05 +0200111 caller_->GetAndAddUserMedia(audio, audio_options, video, video_constraints);
112 callee_->GetAndAddUserMedia(audio, audio_options, video, video_constraints);
wu@webrtc.org364f2042013-11-20 21:49:41 +0000113 }
114
Niels Möllerf06f9232018-08-07 12:32:18 +0200115 void Negotiate() {
116 caller_->CreateOffer(
117 webrtc::PeerConnectionInterface::RTCOfferAnswerOptions());
118 }
wu@webrtc.org364f2042013-11-20 21:49:41 +0000119
120 void WaitForCallEstablished() {
121 caller_->WaitForCallEstablished();
122 callee_->WaitForCallEstablished();
123 }
124
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000125 void WaitForConnection() {
126 caller_->WaitForConnection();
127 callee_->WaitForConnection();
128 }
129
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000130 void OnCallerAddedDataChanel(DataChannelInterface* dc) {
131 caller_signaled_data_channels_.push_back(dc);
132 }
133
134 void OnCalleeAddedDataChannel(DataChannelInterface* dc) {
135 callee_signaled_data_channels_.push_back(dc);
136 }
137
138 // Tests that |dc1| and |dc2| can send to and receive from each other.
Yves Gerey665174f2018-06-19 15:03:05 +0200139 void TestDataChannelSendAndReceive(DataChannelInterface* dc1,
Jeroen de Borst4f6d2332018-07-18 11:25:12 -0700140 DataChannelInterface* dc2,
141 size_t size = 6) {
kwibergd1fe2812016-04-27 06:47:29 -0700142 std::unique_ptr<webrtc::MockDataChannelObserver> dc1_observer(
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000143 new webrtc::MockDataChannelObserver(dc1));
144
kwibergd1fe2812016-04-27 06:47:29 -0700145 std::unique_ptr<webrtc::MockDataChannelObserver> dc2_observer(
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000146 new webrtc::MockDataChannelObserver(dc2));
147
Jeroen de Borst4f6d2332018-07-18 11:25:12 -0700148 static const std::string kDummyData =
149 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
150 webrtc::DataBuffer buffer("");
151
152 size_t sizeLeft = size;
153 while (sizeLeft > 0) {
154 size_t chunkSize =
155 sizeLeft > kDummyData.length() ? kDummyData.length() : sizeLeft;
156 buffer.data.AppendData(kDummyData.data(), chunkSize);
157 sizeLeft -= chunkSize;
158 }
159
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000160 EXPECT_TRUE(dc1->Send(buffer));
Jeroen de Borst4f6d2332018-07-18 11:25:12 -0700161 EXPECT_EQ_WAIT(buffer.data,
162 rtc::CopyOnWriteBuffer(dc2_observer->last_message()),
163 kMaxWait);
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000164
165 EXPECT_TRUE(dc2->Send(buffer));
Jeroen de Borst4f6d2332018-07-18 11:25:12 -0700166 EXPECT_EQ_WAIT(buffer.data,
167 rtc::CopyOnWriteBuffer(dc1_observer->last_message()),
168 kMaxWait);
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000169
170 EXPECT_EQ(1U, dc1_observer->received_message_count());
Jeroen de Borst4f6d2332018-07-18 11:25:12 -0700171 EXPECT_EQ(size, dc1_observer->last_message().length());
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000172 EXPECT_EQ(1U, dc2_observer->received_message_count());
Jeroen de Borst4f6d2332018-07-18 11:25:12 -0700173 EXPECT_EQ(size, dc2_observer->last_message().length());
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000174 }
175
176 void WaitForDataChannelsToOpen(DataChannelInterface* local_dc,
177 const DataChannelList& remote_dc_list,
178 size_t remote_dc_index) {
179 EXPECT_EQ_WAIT(DataChannelInterface::kOpen, local_dc->state(), kMaxWait);
180
Taylor Brandstettercdd05f02018-05-31 13:23:32 -0700181 ASSERT_TRUE_WAIT(remote_dc_list.size() > remote_dc_index, kMaxWait);
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000182 EXPECT_EQ_WAIT(DataChannelInterface::kOpen,
Yves Gerey665174f2018-06-19 15:03:05 +0200183 remote_dc_list[remote_dc_index]->state(), kMaxWait);
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000184 EXPECT_EQ(local_dc->id(), remote_dc_list[remote_dc_index]->id());
185 }
186
187 void CloseDataChannels(DataChannelInterface* local_dc,
188 const DataChannelList& remote_dc_list,
189 size_t remote_dc_index) {
190 local_dc->Close();
191 EXPECT_EQ_WAIT(DataChannelInterface::kClosed, local_dc->state(), kMaxWait);
192 EXPECT_EQ_WAIT(DataChannelInterface::kClosed,
Yves Gerey665174f2018-06-19 15:03:05 +0200193 remote_dc_list[remote_dc_index]->state(), kMaxWait);
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000194 }
195
wu@webrtc.org364f2042013-11-20 21:49:41 +0000196 protected:
tommie7251592017-07-14 14:44:46 -0700197 std::unique_ptr<rtc::Thread> network_thread_;
198 std::unique_ptr<rtc::Thread> worker_thread_;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000199 rtc::scoped_refptr<PeerConnectionTestWrapper> caller_;
200 rtc::scoped_refptr<PeerConnectionTestWrapper> callee_;
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000201 DataChannelList caller_signaled_data_channels_;
202 DataChannelList callee_signaled_data_channels_;
zhihuang9763d562016-08-05 11:14:50 -0700203 webrtc::PeerConnectionInterface::RTCConfiguration config_;
wu@webrtc.org364f2042013-11-20 21:49:41 +0000204};
205
Steve Anton191c39f2018-01-24 19:35:55 -0800206class PeerConnectionEndToEndTest
207 : public PeerConnectionEndToEndBaseTest,
208 public ::testing::WithParamInterface<SdpSemantics> {
209 protected:
210 PeerConnectionEndToEndTest() : PeerConnectionEndToEndBaseTest(GetParam()) {}
211};
212
Karl Wibergc5bb00b2017-10-10 23:17:17 +0200213namespace {
214
kwiberg9e5b11e2017-04-19 03:47:57 -0700215std::unique_ptr<webrtc::AudioDecoder> CreateForwardingMockDecoder(
216 std::unique_ptr<webrtc::AudioDecoder> real_decoder) {
217 class ForwardingMockDecoder : public StrictMock<webrtc::MockAudioDecoder> {
218 public:
Steve Anton36b29d12017-10-30 09:57:42 -0700219 explicit ForwardingMockDecoder(std::unique_ptr<AudioDecoder> decoder)
kwiberg9e5b11e2017-04-19 03:47:57 -0700220 : decoder_(std::move(decoder)) {}
221
222 private:
223 std::unique_ptr<AudioDecoder> decoder_;
224 };
225
226 const auto dec = real_decoder.get(); // For lambda capturing.
227 auto mock_decoder =
Karl Wiberg918f50c2018-07-05 11:40:33 +0200228 absl::make_unique<ForwardingMockDecoder>(std::move(real_decoder));
kwiberg9e5b11e2017-04-19 03:47:57 -0700229 EXPECT_CALL(*mock_decoder, Channels())
230 .Times(AtLeast(1))
231 .WillRepeatedly(Invoke([dec] { return dec->Channels(); }));
232 EXPECT_CALL(*mock_decoder, DecodeInternal(_, _, _, _, _))
233 .Times(AtLeast(1))
234 .WillRepeatedly(
235 Invoke([dec](const uint8_t* encoded, size_t encoded_len,
236 int sample_rate_hz, int16_t* decoded,
237 webrtc::AudioDecoder::SpeechType* speech_type) {
238 return dec->Decode(encoded, encoded_len, sample_rate_hz,
239 std::numeric_limits<size_t>::max(), decoded,
240 speech_type);
241 }));
242 EXPECT_CALL(*mock_decoder, Die());
243 EXPECT_CALL(*mock_decoder, HasDecodePlc()).WillRepeatedly(Invoke([dec] {
244 return dec->HasDecodePlc();
245 }));
246 EXPECT_CALL(*mock_decoder, IncomingPacket(_, _, _, _, _))
247 .Times(AtLeast(1))
248 .WillRepeatedly(Invoke([dec](const uint8_t* payload, size_t payload_len,
249 uint16_t rtp_sequence_number,
250 uint32_t rtp_timestamp,
251 uint32_t arrival_timestamp) {
252 return dec->IncomingPacket(payload, payload_len, rtp_sequence_number,
253 rtp_timestamp, arrival_timestamp);
254 }));
255 EXPECT_CALL(*mock_decoder, PacketDuration(_, _))
256 .Times(AtLeast(1))
257 .WillRepeatedly(Invoke([dec](const uint8_t* encoded, size_t encoded_len) {
258 return dec->PacketDuration(encoded, encoded_len);
259 }));
260 EXPECT_CALL(*mock_decoder, SampleRateHz())
261 .Times(AtLeast(1))
262 .WillRepeatedly(Invoke([dec] { return dec->SampleRateHz(); }));
263
264 return std::move(mock_decoder);
265}
266
267rtc::scoped_refptr<webrtc::AudioDecoderFactory>
268CreateForwardingMockDecoderFactory(
269 webrtc::AudioDecoderFactory* real_decoder_factory) {
270 rtc::scoped_refptr<webrtc::MockAudioDecoderFactory> mock_decoder_factory =
271 new rtc::RefCountedObject<StrictMock<webrtc::MockAudioDecoderFactory>>;
272 EXPECT_CALL(*mock_decoder_factory, GetSupportedDecoders())
273 .Times(AtLeast(1))
274 .WillRepeatedly(Invoke([real_decoder_factory] {
275 return real_decoder_factory->GetSupportedDecoders();
276 }));
277 EXPECT_CALL(*mock_decoder_factory, IsSupportedDecoder(_))
278 .Times(AtLeast(1))
279 .WillRepeatedly(
280 Invoke([real_decoder_factory](const webrtc::SdpAudioFormat& format) {
281 return real_decoder_factory->IsSupportedDecoder(format);
282 }));
Karl Wibergd6fbf2a2018-02-27 13:37:31 +0100283 EXPECT_CALL(*mock_decoder_factory, MakeAudioDecoderMock(_, _, _))
kwiberg9e5b11e2017-04-19 03:47:57 -0700284 .Times(AtLeast(2))
285 .WillRepeatedly(
286 Invoke([real_decoder_factory](
287 const webrtc::SdpAudioFormat& format,
Danil Chapovalov66cadcc2018-06-19 16:47:43 +0200288 absl::optional<webrtc::AudioCodecPairId> codec_pair_id,
kwiberg9e5b11e2017-04-19 03:47:57 -0700289 std::unique_ptr<webrtc::AudioDecoder>* return_value) {
Karl Wibergd6fbf2a2018-02-27 13:37:31 +0100290 auto real_decoder =
291 real_decoder_factory->MakeAudioDecoder(format, codec_pair_id);
kwiberg9e5b11e2017-04-19 03:47:57 -0700292 *return_value =
293 real_decoder
294 ? CreateForwardingMockDecoder(std::move(real_decoder))
295 : nullptr;
296 }));
297 return mock_decoder_factory;
298}
299
Karl Wibergc5bb00b2017-10-10 23:17:17 +0200300struct AudioEncoderUnicornSparklesRainbow {
301 using Config = webrtc::AudioEncoderL16::Config;
Danil Chapovalov66cadcc2018-06-19 16:47:43 +0200302 static absl::optional<Config> SdpToConfig(webrtc::SdpAudioFormat format) {
Niels Möller2edab4c2018-10-22 09:48:08 +0200303 if (absl::EqualsIgnoreCase(format.name, "UnicornSparklesRainbow")) {
Karl Wibergc5bb00b2017-10-10 23:17:17 +0200304 const webrtc::SdpAudioFormat::Parameters expected_params = {
305 {"num_horns", "1"}};
306 EXPECT_EQ(expected_params, format.parameters);
307 format.parameters.clear();
308 format.name = "L16";
309 return webrtc::AudioEncoderL16::SdpToConfig(format);
310 } else {
Danil Chapovalov66cadcc2018-06-19 16:47:43 +0200311 return absl::nullopt;
Karl Wibergc5bb00b2017-10-10 23:17:17 +0200312 }
313 }
314 static void AppendSupportedEncoders(
315 std::vector<webrtc::AudioCodecSpec>* specs) {
316 std::vector<webrtc::AudioCodecSpec> new_specs;
317 webrtc::AudioEncoderL16::AppendSupportedEncoders(&new_specs);
318 for (auto& spec : new_specs) {
319 spec.format.name = "UnicornSparklesRainbow";
320 EXPECT_TRUE(spec.format.parameters.empty());
321 spec.format.parameters.emplace("num_horns", "1");
322 specs->push_back(spec);
323 }
324 }
325 static webrtc::AudioCodecInfo QueryAudioEncoder(const Config& config) {
326 return webrtc::AudioEncoderL16::QueryAudioEncoder(config);
327 }
328 static std::unique_ptr<webrtc::AudioEncoder> MakeAudioEncoder(
329 const Config& config,
Karl Wiberg17668ec2018-03-01 15:13:27 +0100330 int payload_type,
Danil Chapovalov66cadcc2018-06-19 16:47:43 +0200331 absl::optional<webrtc::AudioCodecPairId> codec_pair_id = absl::nullopt) {
Karl Wiberg17668ec2018-03-01 15:13:27 +0100332 return webrtc::AudioEncoderL16::MakeAudioEncoder(config, payload_type,
333 codec_pair_id);
Karl Wibergc5bb00b2017-10-10 23:17:17 +0200334 }
335};
336
337struct AudioDecoderUnicornSparklesRainbow {
338 using Config = webrtc::AudioDecoderL16::Config;
Danil Chapovalov66cadcc2018-06-19 16:47:43 +0200339 static absl::optional<Config> SdpToConfig(webrtc::SdpAudioFormat format) {
Niels Möller2edab4c2018-10-22 09:48:08 +0200340 if (absl::EqualsIgnoreCase(format.name, "UnicornSparklesRainbow")) {
Karl Wibergc5bb00b2017-10-10 23:17:17 +0200341 const webrtc::SdpAudioFormat::Parameters expected_params = {
342 {"num_horns", "1"}};
343 EXPECT_EQ(expected_params, format.parameters);
344 format.parameters.clear();
345 format.name = "L16";
346 return webrtc::AudioDecoderL16::SdpToConfig(format);
347 } else {
Danil Chapovalov66cadcc2018-06-19 16:47:43 +0200348 return absl::nullopt;
Karl Wibergc5bb00b2017-10-10 23:17:17 +0200349 }
350 }
351 static void AppendSupportedDecoders(
352 std::vector<webrtc::AudioCodecSpec>* specs) {
353 std::vector<webrtc::AudioCodecSpec> new_specs;
354 webrtc::AudioDecoderL16::AppendSupportedDecoders(&new_specs);
355 for (auto& spec : new_specs) {
356 spec.format.name = "UnicornSparklesRainbow";
357 EXPECT_TRUE(spec.format.parameters.empty());
358 spec.format.parameters.emplace("num_horns", "1");
359 specs->push_back(spec);
360 }
361 }
362 static std::unique_ptr<webrtc::AudioDecoder> MakeAudioDecoder(
Karl Wiberg17668ec2018-03-01 15:13:27 +0100363 const Config& config,
Danil Chapovalov66cadcc2018-06-19 16:47:43 +0200364 absl::optional<webrtc::AudioCodecPairId> codec_pair_id = absl::nullopt) {
Karl Wiberg17668ec2018-03-01 15:13:27 +0100365 return webrtc::AudioDecoderL16::MakeAudioDecoder(config, codec_pair_id);
Karl Wibergc5bb00b2017-10-10 23:17:17 +0200366 }
367};
368
369} // namespace
370
Steve Anton36da6ff2018-02-16 16:04:20 -0800371TEST_P(PeerConnectionEndToEndTest, Call) {
kwiberg9e5b11e2017-04-19 03:47:57 -0700372 rtc::scoped_refptr<webrtc::AudioDecoderFactory> real_decoder_factory =
373 webrtc::CreateBuiltinAudioDecoderFactory();
Niels Möllerf06f9232018-08-07 12:32:18 +0200374 CreatePcs(webrtc::CreateBuiltinAudioEncoderFactory(),
kwiberg9e5b11e2017-04-19 03:47:57 -0700375 CreateForwardingMockDecoderFactory(real_decoder_factory.get()));
wu@webrtc.org364f2042013-11-20 21:49:41 +0000376 GetAndAddUserMedia();
377 Negotiate();
378 WaitForCallEstablished();
379}
380
Niels Möllerf06f9232018-08-07 12:32:18 +0200381TEST_P(PeerConnectionEndToEndTest, CallWithSdesKeyNegotiation) {
382 config_.enable_dtls_srtp = false;
383 CreatePcs(webrtc::CreateBuiltinAudioEncoderFactory(),
kwiberg9e5b11e2017-04-19 03:47:57 -0700384 webrtc::CreateBuiltinAudioDecoderFactory());
wu@webrtc.org364f2042013-11-20 21:49:41 +0000385 GetAndAddUserMedia();
386 Negotiate();
387 WaitForCallEstablished();
388}
wu@webrtc.orgb43202d2013-11-22 19:14:25 +0000389
Steve Anton191c39f2018-01-24 19:35:55 -0800390TEST_P(PeerConnectionEndToEndTest, CallWithCustomCodec) {
Karl Wiberg5bdc82a2018-03-22 00:07:39 +0100391 class IdLoggingAudioEncoderFactory : public webrtc::AudioEncoderFactory {
392 public:
393 IdLoggingAudioEncoderFactory(
394 rtc::scoped_refptr<AudioEncoderFactory> real_factory,
395 std::vector<webrtc::AudioCodecPairId>* const codec_ids)
396 : fact_(real_factory), codec_ids_(codec_ids) {}
397 std::vector<webrtc::AudioCodecSpec> GetSupportedEncoders() override {
398 return fact_->GetSupportedEncoders();
399 }
Danil Chapovalov66cadcc2018-06-19 16:47:43 +0200400 absl::optional<webrtc::AudioCodecInfo> QueryAudioEncoder(
Karl Wiberg5bdc82a2018-03-22 00:07:39 +0100401 const webrtc::SdpAudioFormat& format) override {
402 return fact_->QueryAudioEncoder(format);
403 }
404 std::unique_ptr<webrtc::AudioEncoder> MakeAudioEncoder(
405 int payload_type,
406 const webrtc::SdpAudioFormat& format,
Danil Chapovalov66cadcc2018-06-19 16:47:43 +0200407 absl::optional<webrtc::AudioCodecPairId> codec_pair_id) override {
Karl Wiberg5bdc82a2018-03-22 00:07:39 +0100408 EXPECT_TRUE(codec_pair_id.has_value());
409 codec_ids_->push_back(*codec_pair_id);
410 return fact_->MakeAudioEncoder(payload_type, format, codec_pair_id);
411 }
412
413 private:
414 const rtc::scoped_refptr<webrtc::AudioEncoderFactory> fact_;
415 std::vector<webrtc::AudioCodecPairId>* const codec_ids_;
416 };
417
418 class IdLoggingAudioDecoderFactory : public webrtc::AudioDecoderFactory {
419 public:
420 IdLoggingAudioDecoderFactory(
421 rtc::scoped_refptr<AudioDecoderFactory> real_factory,
422 std::vector<webrtc::AudioCodecPairId>* const codec_ids)
423 : fact_(real_factory), codec_ids_(codec_ids) {}
424 std::vector<webrtc::AudioCodecSpec> GetSupportedDecoders() override {
425 return fact_->GetSupportedDecoders();
426 }
427 bool IsSupportedDecoder(const webrtc::SdpAudioFormat& format) override {
428 return fact_->IsSupportedDecoder(format);
429 }
430 std::unique_ptr<webrtc::AudioDecoder> MakeAudioDecoder(
431 const webrtc::SdpAudioFormat& format,
Danil Chapovalov66cadcc2018-06-19 16:47:43 +0200432 absl::optional<webrtc::AudioCodecPairId> codec_pair_id) override {
Karl Wiberg5bdc82a2018-03-22 00:07:39 +0100433 EXPECT_TRUE(codec_pair_id.has_value());
434 codec_ids_->push_back(*codec_pair_id);
435 return fact_->MakeAudioDecoder(format, codec_pair_id);
436 }
437
438 private:
439 const rtc::scoped_refptr<webrtc::AudioDecoderFactory> fact_;
440 std::vector<webrtc::AudioCodecPairId>* const codec_ids_;
441 };
442
443 std::vector<webrtc::AudioCodecPairId> encoder_id1, encoder_id2, decoder_id1,
444 decoder_id2;
Niels Möllerf06f9232018-08-07 12:32:18 +0200445 CreatePcs(rtc::scoped_refptr<webrtc::AudioEncoderFactory>(
Karl Wiberg5bdc82a2018-03-22 00:07:39 +0100446 new rtc::RefCountedObject<IdLoggingAudioEncoderFactory>(
447 webrtc::CreateAudioEncoderFactory<
448 AudioEncoderUnicornSparklesRainbow>(),
449 &encoder_id1)),
450 rtc::scoped_refptr<webrtc::AudioDecoderFactory>(
451 new rtc::RefCountedObject<IdLoggingAudioDecoderFactory>(
452 webrtc::CreateAudioDecoderFactory<
453 AudioDecoderUnicornSparklesRainbow>(),
454 &decoder_id1)),
455 rtc::scoped_refptr<webrtc::AudioEncoderFactory>(
456 new rtc::RefCountedObject<IdLoggingAudioEncoderFactory>(
457 webrtc::CreateAudioEncoderFactory<
458 AudioEncoderUnicornSparklesRainbow>(),
459 &encoder_id2)),
460 rtc::scoped_refptr<webrtc::AudioDecoderFactory>(
461 new rtc::RefCountedObject<IdLoggingAudioDecoderFactory>(
462 webrtc::CreateAudioDecoderFactory<
463 AudioDecoderUnicornSparklesRainbow>(),
464 &decoder_id2)));
Karl Wibergc5bb00b2017-10-10 23:17:17 +0200465 GetAndAddUserMedia();
466 Negotiate();
467 WaitForCallEstablished();
Karl Wiberg5bdc82a2018-03-22 00:07:39 +0100468
469 // Each codec factory has been used to create one codec. The first pair got
470 // the same ID because they were passed to the same PeerConnectionFactory,
471 // and the second pair got the same ID---but these two IDs are not equal,
472 // because each PeerConnectionFactory has its own ID.
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +0200473 EXPECT_EQ(1U, encoder_id1.size());
474 EXPECT_EQ(1U, encoder_id2.size());
Karl Wiberg5bdc82a2018-03-22 00:07:39 +0100475 EXPECT_EQ(encoder_id1, decoder_id1);
476 EXPECT_EQ(encoder_id2, decoder_id2);
477 EXPECT_NE(encoder_id1, encoder_id2);
Karl Wibergc5bb00b2017-10-10 23:17:17 +0200478}
479
deadbeef40610e22016-12-22 10:53:38 -0800480#ifdef HAVE_SCTP
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000481// Verifies that a DataChannel created before the negotiation can transition to
482// "OPEN" and transfer data.
Steve Anton191c39f2018-01-24 19:35:55 -0800483TEST_P(PeerConnectionEndToEndTest, CreateDataChannelBeforeNegotiate) {
Karl Wibergbc4cf892018-11-13 13:20:51 +0100484 CreatePcs(webrtc::MockAudioEncoderFactory::CreateEmptyFactory(),
kwiberg7a12b5a2017-04-27 03:55:57 -0700485 webrtc::MockAudioDecoderFactory::CreateEmptyFactory());
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000486
487 webrtc::DataChannelInit init;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000488 rtc::scoped_refptr<DataChannelInterface> caller_dc(
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000489 caller_->CreateDataChannel("data", init));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000490 rtc::scoped_refptr<DataChannelInterface> callee_dc(
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000491 callee_->CreateDataChannel("data", init));
492
493 Negotiate();
494 WaitForConnection();
495
496 WaitForDataChannelsToOpen(caller_dc, callee_signaled_data_channels_, 0);
497 WaitForDataChannelsToOpen(callee_dc, caller_signaled_data_channels_, 0);
498
499 TestDataChannelSendAndReceive(caller_dc, callee_signaled_data_channels_[0]);
500 TestDataChannelSendAndReceive(callee_dc, caller_signaled_data_channels_[0]);
501
502 CloseDataChannels(caller_dc, callee_signaled_data_channels_, 0);
503 CloseDataChannels(callee_dc, caller_signaled_data_channels_, 0);
504}
505
506// Verifies that a DataChannel created after the negotiation can transition to
507// "OPEN" and transfer data.
Steve Anton191c39f2018-01-24 19:35:55 -0800508TEST_P(PeerConnectionEndToEndTest, CreateDataChannelAfterNegotiate) {
Karl Wibergbc4cf892018-11-13 13:20:51 +0100509 CreatePcs(webrtc::MockAudioEncoderFactory::CreateEmptyFactory(),
kwiberg7a12b5a2017-04-27 03:55:57 -0700510 webrtc::MockAudioDecoderFactory::CreateEmptyFactory());
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000511
512 webrtc::DataChannelInit init;
513
514 // This DataChannel is for creating the data content in the negotiation.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000515 rtc::scoped_refptr<DataChannelInterface> dummy(
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000516 caller_->CreateDataChannel("data", init));
517 Negotiate();
518 WaitForConnection();
519
Taylor Brandstetterbf2f5692016-06-29 11:22:47 -0700520 // Wait for the data channel created pre-negotiation to be opened.
521 WaitForDataChannelsToOpen(dummy, callee_signaled_data_channels_, 0);
522
523 // Create new DataChannels after the negotiation and verify their states.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000524 rtc::scoped_refptr<DataChannelInterface> caller_dc(
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000525 caller_->CreateDataChannel("hello", init));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000526 rtc::scoped_refptr<DataChannelInterface> callee_dc(
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000527 callee_->CreateDataChannel("hello", init));
528
529 WaitForDataChannelsToOpen(caller_dc, callee_signaled_data_channels_, 1);
530 WaitForDataChannelsToOpen(callee_dc, caller_signaled_data_channels_, 0);
531
532 TestDataChannelSendAndReceive(caller_dc, callee_signaled_data_channels_[1]);
533 TestDataChannelSendAndReceive(callee_dc, caller_signaled_data_channels_[0]);
534
535 CloseDataChannels(caller_dc, callee_signaled_data_channels_, 1);
536 CloseDataChannels(callee_dc, caller_signaled_data_channels_, 0);
537}
538
Jeroen de Borst4f6d2332018-07-18 11:25:12 -0700539// Verifies that a DataChannel created can transfer large messages.
540TEST_P(PeerConnectionEndToEndTest, CreateDataChannelLargeTransfer) {
Karl Wibergbc4cf892018-11-13 13:20:51 +0100541 CreatePcs(webrtc::MockAudioEncoderFactory::CreateEmptyFactory(),
Jeroen de Borst4f6d2332018-07-18 11:25:12 -0700542 webrtc::MockAudioDecoderFactory::CreateEmptyFactory());
543
544 webrtc::DataChannelInit init;
545
546 // This DataChannel is for creating the data content in the negotiation.
547 rtc::scoped_refptr<DataChannelInterface> dummy(
548 caller_->CreateDataChannel("data", init));
549 Negotiate();
550 WaitForConnection();
551
552 // Wait for the data channel created pre-negotiation to be opened.
553 WaitForDataChannelsToOpen(dummy, callee_signaled_data_channels_, 0);
554
555 // Create new DataChannels after the negotiation and verify their states.
556 rtc::scoped_refptr<DataChannelInterface> caller_dc(
557 caller_->CreateDataChannel("hello", init));
558 rtc::scoped_refptr<DataChannelInterface> callee_dc(
559 callee_->CreateDataChannel("hello", init));
560
561 WaitForDataChannelsToOpen(caller_dc, callee_signaled_data_channels_, 1);
562 WaitForDataChannelsToOpen(callee_dc, caller_signaled_data_channels_, 0);
563
564 TestDataChannelSendAndReceive(caller_dc, callee_signaled_data_channels_[1],
565 256 * 1024);
566 TestDataChannelSendAndReceive(callee_dc, caller_signaled_data_channels_[0],
567 256 * 1024);
568
569 CloseDataChannels(caller_dc, callee_signaled_data_channels_, 1);
570 CloseDataChannels(callee_dc, caller_signaled_data_channels_, 0);
571}
572
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000573// Verifies that DataChannel IDs are even/odd based on the DTLS roles.
Steve Anton191c39f2018-01-24 19:35:55 -0800574TEST_P(PeerConnectionEndToEndTest, DataChannelIdAssignment) {
Karl Wibergbc4cf892018-11-13 13:20:51 +0100575 CreatePcs(webrtc::MockAudioEncoderFactory::CreateEmptyFactory(),
kwiberg7a12b5a2017-04-27 03:55:57 -0700576 webrtc::MockAudioDecoderFactory::CreateEmptyFactory());
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000577
578 webrtc::DataChannelInit init;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000579 rtc::scoped_refptr<DataChannelInterface> caller_dc_1(
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000580 caller_->CreateDataChannel("data", init));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000581 rtc::scoped_refptr<DataChannelInterface> callee_dc_1(
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000582 callee_->CreateDataChannel("data", init));
583
584 Negotiate();
585 WaitForConnection();
586
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +0200587 EXPECT_EQ(1, caller_dc_1->id() % 2);
588 EXPECT_EQ(0, callee_dc_1->id() % 2);
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000589
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000590 rtc::scoped_refptr<DataChannelInterface> caller_dc_2(
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000591 caller_->CreateDataChannel("data", init));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000592 rtc::scoped_refptr<DataChannelInterface> callee_dc_2(
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000593 callee_->CreateDataChannel("data", init));
594
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +0200595 EXPECT_EQ(1, caller_dc_2->id() % 2);
596 EXPECT_EQ(0, callee_dc_2->id() % 2);
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000597}
598
599// Verifies that the message is received by the right remote DataChannel when
600// there are multiple DataChannels.
Steve Anton191c39f2018-01-24 19:35:55 -0800601TEST_P(PeerConnectionEndToEndTest,
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000602 MessageTransferBetweenTwoPairsOfDataChannels) {
Karl Wibergbc4cf892018-11-13 13:20:51 +0100603 CreatePcs(webrtc::MockAudioEncoderFactory::CreateEmptyFactory(),
kwiberg7a12b5a2017-04-27 03:55:57 -0700604 webrtc::MockAudioDecoderFactory::CreateEmptyFactory());
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000605
606 webrtc::DataChannelInit init;
607
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000608 rtc::scoped_refptr<DataChannelInterface> caller_dc_1(
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000609 caller_->CreateDataChannel("data", init));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000610 rtc::scoped_refptr<DataChannelInterface> caller_dc_2(
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000611 caller_->CreateDataChannel("data", init));
612
613 Negotiate();
614 WaitForConnection();
615 WaitForDataChannelsToOpen(caller_dc_1, callee_signaled_data_channels_, 0);
616 WaitForDataChannelsToOpen(caller_dc_2, callee_signaled_data_channels_, 1);
617
kwibergd1fe2812016-04-27 06:47:29 -0700618 std::unique_ptr<webrtc::MockDataChannelObserver> dc_1_observer(
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000619 new webrtc::MockDataChannelObserver(callee_signaled_data_channels_[0]));
620
kwibergd1fe2812016-04-27 06:47:29 -0700621 std::unique_ptr<webrtc::MockDataChannelObserver> dc_2_observer(
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000622 new webrtc::MockDataChannelObserver(callee_signaled_data_channels_[1]));
623
624 const std::string message_1 = "hello 1";
625 const std::string message_2 = "hello 2";
626
627 caller_dc_1->Send(webrtc::DataBuffer(message_1));
628 EXPECT_EQ_WAIT(message_1, dc_1_observer->last_message(), kMaxWait);
629
630 caller_dc_2->Send(webrtc::DataBuffer(message_2));
631 EXPECT_EQ_WAIT(message_2, dc_2_observer->last_message(), kMaxWait);
632
633 EXPECT_EQ(1U, dc_1_observer->received_message_count());
634 EXPECT_EQ(1U, dc_2_observer->received_message_count());
635}
deadbeefab9b2d12015-10-14 11:33:11 -0700636
637// Verifies that a DataChannel added from an OPEN message functions after
638// a channel has been previously closed (webrtc issue 3778).
Taylor Brandstettercdd05f02018-05-31 13:23:32 -0700639// This previously failed because the new channel re-used the ID of the closed
640// channel, and the closed channel was incorrectly still assigned to the ID.
Steve Anton191c39f2018-01-24 19:35:55 -0800641TEST_P(PeerConnectionEndToEndTest,
Taylor Brandstettercdd05f02018-05-31 13:23:32 -0700642 DataChannelFromOpenWorksAfterPreviousChannelClosed) {
Karl Wibergbc4cf892018-11-13 13:20:51 +0100643 CreatePcs(webrtc::MockAudioEncoderFactory::CreateEmptyFactory(),
kwiberg7a12b5a2017-04-27 03:55:57 -0700644 webrtc::MockAudioDecoderFactory::CreateEmptyFactory());
deadbeefab9b2d12015-10-14 11:33:11 -0700645
646 webrtc::DataChannelInit init;
647 rtc::scoped_refptr<DataChannelInterface> caller_dc(
648 caller_->CreateDataChannel("data", init));
649
650 Negotiate();
651 WaitForConnection();
652
653 WaitForDataChannelsToOpen(caller_dc, callee_signaled_data_channels_, 0);
Taylor Brandstettercdd05f02018-05-31 13:23:32 -0700654 int first_channel_id = caller_dc->id();
655 // Wait for the local side to say it's closed, but not the remote side.
656 // Previously, the channel on which Close is called reported being closed
657 // prematurely, and this caused issues; see bugs.webrtc.org/4453.
658 caller_dc->Close();
659 EXPECT_EQ_WAIT(DataChannelInterface::kClosed, caller_dc->state(), kMaxWait);
deadbeefab9b2d12015-10-14 11:33:11 -0700660
661 // Create a new channel and ensure it works after closing the previous one.
662 caller_dc = caller_->CreateDataChannel("data2", init);
deadbeefab9b2d12015-10-14 11:33:11 -0700663 WaitForDataChannelsToOpen(caller_dc, callee_signaled_data_channels_, 1);
Taylor Brandstettercdd05f02018-05-31 13:23:32 -0700664 // Since the second channel was created after the first finished closing, it
665 // should be able to re-use the first one's ID.
666 EXPECT_EQ(first_channel_id, caller_dc->id());
667 TestDataChannelSendAndReceive(caller_dc, callee_signaled_data_channels_[1]);
668
669 CloseDataChannels(caller_dc, callee_signaled_data_channels_, 1);
670}
671
672// Similar to the above test, but don't wait for the first channel to finish
673// closing before creating the second one.
674TEST_P(PeerConnectionEndToEndTest,
675 DataChannelFromOpenWorksWhilePreviousChannelClosing) {
Karl Wibergbc4cf892018-11-13 13:20:51 +0100676 CreatePcs(webrtc::MockAudioEncoderFactory::CreateEmptyFactory(),
Taylor Brandstettercdd05f02018-05-31 13:23:32 -0700677 webrtc::MockAudioDecoderFactory::CreateEmptyFactory());
678
679 webrtc::DataChannelInit init;
680 rtc::scoped_refptr<DataChannelInterface> caller_dc(
681 caller_->CreateDataChannel("data", init));
682
683 Negotiate();
684 WaitForConnection();
685
686 WaitForDataChannelsToOpen(caller_dc, callee_signaled_data_channels_, 0);
687 int first_channel_id = caller_dc->id();
688 caller_dc->Close();
689
690 // Immediately create a new channel, before waiting for the previous one to
691 // transition to "closed".
692 caller_dc = caller_->CreateDataChannel("data2", init);
693 WaitForDataChannelsToOpen(caller_dc, callee_signaled_data_channels_, 1);
694 // Since the second channel was created while the first was still closing,
695 // it should have been assigned a different ID.
696 EXPECT_NE(first_channel_id, caller_dc->id());
deadbeefab9b2d12015-10-14 11:33:11 -0700697 TestDataChannelSendAndReceive(caller_dc, callee_signaled_data_channels_[1]);
698
699 CloseDataChannels(caller_dc, callee_signaled_data_channels_, 1);
700}
deadbeefbd292462015-12-14 18:15:29 -0800701
702// This tests that if a data channel is closed remotely while not referenced
703// by the application (meaning only the PeerConnection contributes to its
704// reference count), no memory access violation will occur.
705// See: https://code.google.com/p/chromium/issues/detail?id=565048
Steve Anton191c39f2018-01-24 19:35:55 -0800706TEST_P(PeerConnectionEndToEndTest, CloseDataChannelRemotelyWhileNotReferenced) {
Karl Wibergbc4cf892018-11-13 13:20:51 +0100707 CreatePcs(webrtc::MockAudioEncoderFactory::CreateEmptyFactory(),
kwiberg7a12b5a2017-04-27 03:55:57 -0700708 webrtc::MockAudioDecoderFactory::CreateEmptyFactory());
deadbeefbd292462015-12-14 18:15:29 -0800709
710 webrtc::DataChannelInit init;
711 rtc::scoped_refptr<DataChannelInterface> caller_dc(
712 caller_->CreateDataChannel("data", init));
713
714 Negotiate();
715 WaitForConnection();
716
717 WaitForDataChannelsToOpen(caller_dc, callee_signaled_data_channels_, 0);
718 // This removes the reference to the remote data channel that we hold.
719 callee_signaled_data_channels_.clear();
720 caller_dc->Close();
721 EXPECT_EQ_WAIT(DataChannelInterface::kClosed, caller_dc->state(), kMaxWait);
722
723 // Wait for a bit longer so the remote data channel will receive the
724 // close message and be destroyed.
725 rtc::Thread::Current()->ProcessMessages(100);
726}
deadbeef40610e22016-12-22 10:53:38 -0800727#endif // HAVE_SCTP
Steve Anton191c39f2018-01-24 19:35:55 -0800728
Mirko Bonadeic84f6612019-01-31 12:20:57 +0100729INSTANTIATE_TEST_SUITE_P(PeerConnectionEndToEndTest,
730 PeerConnectionEndToEndTest,
731 Values(SdpSemantics::kPlanB,
732 SdpSemantics::kUnifiedPlan));