blob: 2ac3580ae951b2b826bdeebcfdebd3e6e3550ad5 [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
Karl Wiberg5bdc82a2018-03-22 00:07:39 +010079 void CreatePcs(
80 const MediaConstraintsInterface* pc_constraints,
81 rtc::scoped_refptr<webrtc::AudioEncoderFactory> audio_encoder_factory1,
82 rtc::scoped_refptr<webrtc::AudioDecoderFactory> audio_decoder_factory1,
83 rtc::scoped_refptr<webrtc::AudioEncoderFactory> audio_encoder_factory2,
84 rtc::scoped_refptr<webrtc::AudioDecoderFactory> audio_decoder_factory2) {
85 EXPECT_TRUE(caller_->CreatePc(pc_constraints, config_,
86 audio_encoder_factory1,
87 audio_decoder_factory1));
88 EXPECT_TRUE(callee_->CreatePc(pc_constraints, config_,
89 audio_encoder_factory2,
90 audio_decoder_factory2));
wu@webrtc.org364f2042013-11-20 21:49:41 +000091 PeerConnectionTestWrapper::Connect(caller_.get(), callee_.get());
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +000092
93 caller_->SignalOnDataChannel.connect(
Steve Anton191c39f2018-01-24 19:35:55 -080094 this, &PeerConnectionEndToEndBaseTest::OnCallerAddedDataChanel);
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +000095 callee_->SignalOnDataChannel.connect(
Steve Anton191c39f2018-01-24 19:35:55 -080096 this, &PeerConnectionEndToEndBaseTest::OnCalleeAddedDataChannel);
wu@webrtc.org364f2042013-11-20 21:49:41 +000097 }
98
Karl Wiberg5bdc82a2018-03-22 00:07:39 +010099 void CreatePcs(
100 const MediaConstraintsInterface* pc_constraints,
101 rtc::scoped_refptr<webrtc::AudioEncoderFactory> audio_encoder_factory,
102 rtc::scoped_refptr<webrtc::AudioDecoderFactory> audio_decoder_factory) {
103 CreatePcs(pc_constraints, audio_encoder_factory, audio_decoder_factory,
104 audio_encoder_factory, audio_decoder_factory);
105 }
106
wu@webrtc.org364f2042013-11-20 21:49:41 +0000107 void GetAndAddUserMedia() {
108 FakeConstraints audio_constraints;
109 FakeConstraints video_constraints;
110 GetAndAddUserMedia(true, audio_constraints, true, video_constraints);
111 }
112
Mirko Bonadeic61ce0d2017-11-21 17:04:20 +0100113 void GetAndAddUserMedia(bool audio,
114 const FakeConstraints& audio_constraints,
115 bool video,
116 const FakeConstraints& video_constraints) {
wu@webrtc.org364f2042013-11-20 21:49:41 +0000117 caller_->GetAndAddUserMedia(audio, audio_constraints,
118 video, video_constraints);
119 callee_->GetAndAddUserMedia(audio, audio_constraints,
120 video, video_constraints);
121 }
122
123 void Negotiate() {
124 caller_->CreateOffer(NULL);
125 }
126
127 void WaitForCallEstablished() {
128 caller_->WaitForCallEstablished();
129 callee_->WaitForCallEstablished();
130 }
131
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000132 void WaitForConnection() {
133 caller_->WaitForConnection();
134 callee_->WaitForConnection();
135 }
136
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000137 void OnCallerAddedDataChanel(DataChannelInterface* dc) {
138 caller_signaled_data_channels_.push_back(dc);
139 }
140
141 void OnCalleeAddedDataChannel(DataChannelInterface* dc) {
142 callee_signaled_data_channels_.push_back(dc);
143 }
144
145 // Tests that |dc1| and |dc2| can send to and receive from each other.
146 void TestDataChannelSendAndReceive(
147 DataChannelInterface* dc1, DataChannelInterface* dc2) {
kwibergd1fe2812016-04-27 06:47:29 -0700148 std::unique_ptr<webrtc::MockDataChannelObserver> dc1_observer(
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000149 new webrtc::MockDataChannelObserver(dc1));
150
kwibergd1fe2812016-04-27 06:47:29 -0700151 std::unique_ptr<webrtc::MockDataChannelObserver> dc2_observer(
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000152 new webrtc::MockDataChannelObserver(dc2));
153
154 static const std::string kDummyData = "abcdefg";
155 webrtc::DataBuffer buffer(kDummyData);
156 EXPECT_TRUE(dc1->Send(buffer));
157 EXPECT_EQ_WAIT(kDummyData, dc2_observer->last_message(), kMaxWait);
158
159 EXPECT_TRUE(dc2->Send(buffer));
160 EXPECT_EQ_WAIT(kDummyData, dc1_observer->last_message(), kMaxWait);
161
162 EXPECT_EQ(1U, dc1_observer->received_message_count());
163 EXPECT_EQ(1U, dc2_observer->received_message_count());
164 }
165
166 void WaitForDataChannelsToOpen(DataChannelInterface* local_dc,
167 const DataChannelList& remote_dc_list,
168 size_t remote_dc_index) {
169 EXPECT_EQ_WAIT(DataChannelInterface::kOpen, local_dc->state(), kMaxWait);
170
171 EXPECT_TRUE_WAIT(remote_dc_list.size() > remote_dc_index, kMaxWait);
172 EXPECT_EQ_WAIT(DataChannelInterface::kOpen,
173 remote_dc_list[remote_dc_index]->state(),
174 kMaxWait);
175 EXPECT_EQ(local_dc->id(), remote_dc_list[remote_dc_index]->id());
176 }
177
178 void CloseDataChannels(DataChannelInterface* local_dc,
179 const DataChannelList& remote_dc_list,
180 size_t remote_dc_index) {
181 local_dc->Close();
182 EXPECT_EQ_WAIT(DataChannelInterface::kClosed, local_dc->state(), kMaxWait);
183 EXPECT_EQ_WAIT(DataChannelInterface::kClosed,
184 remote_dc_list[remote_dc_index]->state(),
185 kMaxWait);
186 }
187
wu@webrtc.org364f2042013-11-20 21:49:41 +0000188 protected:
tommie7251592017-07-14 14:44:46 -0700189 std::unique_ptr<rtc::Thread> network_thread_;
190 std::unique_ptr<rtc::Thread> worker_thread_;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000191 rtc::scoped_refptr<PeerConnectionTestWrapper> caller_;
192 rtc::scoped_refptr<PeerConnectionTestWrapper> callee_;
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000193 DataChannelList caller_signaled_data_channels_;
194 DataChannelList callee_signaled_data_channels_;
zhihuang9763d562016-08-05 11:14:50 -0700195 webrtc::PeerConnectionInterface::RTCConfiguration config_;
wu@webrtc.org364f2042013-11-20 21:49:41 +0000196};
197
Steve Anton191c39f2018-01-24 19:35:55 -0800198class PeerConnectionEndToEndTest
199 : public PeerConnectionEndToEndBaseTest,
200 public ::testing::WithParamInterface<SdpSemantics> {
201 protected:
202 PeerConnectionEndToEndTest() : PeerConnectionEndToEndBaseTest(GetParam()) {}
203};
204
Karl Wibergc5bb00b2017-10-10 23:17:17 +0200205namespace {
206
kwiberg9e5b11e2017-04-19 03:47:57 -0700207std::unique_ptr<webrtc::AudioDecoder> CreateForwardingMockDecoder(
208 std::unique_ptr<webrtc::AudioDecoder> real_decoder) {
209 class ForwardingMockDecoder : public StrictMock<webrtc::MockAudioDecoder> {
210 public:
Steve Anton36b29d12017-10-30 09:57:42 -0700211 explicit ForwardingMockDecoder(std::unique_ptr<AudioDecoder> decoder)
kwiberg9e5b11e2017-04-19 03:47:57 -0700212 : decoder_(std::move(decoder)) {}
213
214 private:
215 std::unique_ptr<AudioDecoder> decoder_;
216 };
217
218 const auto dec = real_decoder.get(); // For lambda capturing.
219 auto mock_decoder =
220 rtc::MakeUnique<ForwardingMockDecoder>(std::move(real_decoder));
221 EXPECT_CALL(*mock_decoder, Channels())
222 .Times(AtLeast(1))
223 .WillRepeatedly(Invoke([dec] { return dec->Channels(); }));
224 EXPECT_CALL(*mock_decoder, DecodeInternal(_, _, _, _, _))
225 .Times(AtLeast(1))
226 .WillRepeatedly(
227 Invoke([dec](const uint8_t* encoded, size_t encoded_len,
228 int sample_rate_hz, int16_t* decoded,
229 webrtc::AudioDecoder::SpeechType* speech_type) {
230 return dec->Decode(encoded, encoded_len, sample_rate_hz,
231 std::numeric_limits<size_t>::max(), decoded,
232 speech_type);
233 }));
234 EXPECT_CALL(*mock_decoder, Die());
235 EXPECT_CALL(*mock_decoder, HasDecodePlc()).WillRepeatedly(Invoke([dec] {
236 return dec->HasDecodePlc();
237 }));
238 EXPECT_CALL(*mock_decoder, IncomingPacket(_, _, _, _, _))
239 .Times(AtLeast(1))
240 .WillRepeatedly(Invoke([dec](const uint8_t* payload, size_t payload_len,
241 uint16_t rtp_sequence_number,
242 uint32_t rtp_timestamp,
243 uint32_t arrival_timestamp) {
244 return dec->IncomingPacket(payload, payload_len, rtp_sequence_number,
245 rtp_timestamp, arrival_timestamp);
246 }));
247 EXPECT_CALL(*mock_decoder, PacketDuration(_, _))
248 .Times(AtLeast(1))
249 .WillRepeatedly(Invoke([dec](const uint8_t* encoded, size_t encoded_len) {
250 return dec->PacketDuration(encoded, encoded_len);
251 }));
252 EXPECT_CALL(*mock_decoder, SampleRateHz())
253 .Times(AtLeast(1))
254 .WillRepeatedly(Invoke([dec] { return dec->SampleRateHz(); }));
255
256 return std::move(mock_decoder);
257}
258
259rtc::scoped_refptr<webrtc::AudioDecoderFactory>
260CreateForwardingMockDecoderFactory(
261 webrtc::AudioDecoderFactory* real_decoder_factory) {
262 rtc::scoped_refptr<webrtc::MockAudioDecoderFactory> mock_decoder_factory =
263 new rtc::RefCountedObject<StrictMock<webrtc::MockAudioDecoderFactory>>;
264 EXPECT_CALL(*mock_decoder_factory, GetSupportedDecoders())
265 .Times(AtLeast(1))
266 .WillRepeatedly(Invoke([real_decoder_factory] {
267 return real_decoder_factory->GetSupportedDecoders();
268 }));
269 EXPECT_CALL(*mock_decoder_factory, IsSupportedDecoder(_))
270 .Times(AtLeast(1))
271 .WillRepeatedly(
272 Invoke([real_decoder_factory](const webrtc::SdpAudioFormat& format) {
273 return real_decoder_factory->IsSupportedDecoder(format);
274 }));
Karl Wibergd6fbf2a2018-02-27 13:37:31 +0100275 EXPECT_CALL(*mock_decoder_factory, MakeAudioDecoderMock(_, _, _))
kwiberg9e5b11e2017-04-19 03:47:57 -0700276 .Times(AtLeast(2))
277 .WillRepeatedly(
278 Invoke([real_decoder_factory](
279 const webrtc::SdpAudioFormat& format,
Karl Wibergd6fbf2a2018-02-27 13:37:31 +0100280 rtc::Optional<webrtc::AudioCodecPairId> codec_pair_id,
kwiberg9e5b11e2017-04-19 03:47:57 -0700281 std::unique_ptr<webrtc::AudioDecoder>* return_value) {
Karl Wibergd6fbf2a2018-02-27 13:37:31 +0100282 auto real_decoder =
283 real_decoder_factory->MakeAudioDecoder(format, codec_pair_id);
kwiberg9e5b11e2017-04-19 03:47:57 -0700284 *return_value =
285 real_decoder
286 ? CreateForwardingMockDecoder(std::move(real_decoder))
287 : nullptr;
288 }));
289 return mock_decoder_factory;
290}
291
Karl Wibergc5bb00b2017-10-10 23:17:17 +0200292struct AudioEncoderUnicornSparklesRainbow {
293 using Config = webrtc::AudioEncoderL16::Config;
294 static rtc::Optional<Config> SdpToConfig(webrtc::SdpAudioFormat format) {
295 if (STR_CASE_CMP(format.name.c_str(), "UnicornSparklesRainbow") == 0) {
296 const webrtc::SdpAudioFormat::Parameters expected_params = {
297 {"num_horns", "1"}};
298 EXPECT_EQ(expected_params, format.parameters);
299 format.parameters.clear();
300 format.name = "L16";
301 return webrtc::AudioEncoderL16::SdpToConfig(format);
302 } else {
Oskar Sundbom63e232a2017-11-16 10:57:03 +0100303 return rtc::nullopt;
Karl Wibergc5bb00b2017-10-10 23:17:17 +0200304 }
305 }
306 static void AppendSupportedEncoders(
307 std::vector<webrtc::AudioCodecSpec>* specs) {
308 std::vector<webrtc::AudioCodecSpec> new_specs;
309 webrtc::AudioEncoderL16::AppendSupportedEncoders(&new_specs);
310 for (auto& spec : new_specs) {
311 spec.format.name = "UnicornSparklesRainbow";
312 EXPECT_TRUE(spec.format.parameters.empty());
313 spec.format.parameters.emplace("num_horns", "1");
314 specs->push_back(spec);
315 }
316 }
317 static webrtc::AudioCodecInfo QueryAudioEncoder(const Config& config) {
318 return webrtc::AudioEncoderL16::QueryAudioEncoder(config);
319 }
320 static std::unique_ptr<webrtc::AudioEncoder> MakeAudioEncoder(
321 const Config& config,
Karl Wiberg17668ec2018-03-01 15:13:27 +0100322 int payload_type,
323 rtc::Optional<webrtc::AudioCodecPairId> codec_pair_id = rtc::nullopt) {
324 return webrtc::AudioEncoderL16::MakeAudioEncoder(config, payload_type,
325 codec_pair_id);
Karl Wibergc5bb00b2017-10-10 23:17:17 +0200326 }
327};
328
329struct AudioDecoderUnicornSparklesRainbow {
330 using Config = webrtc::AudioDecoderL16::Config;
331 static rtc::Optional<Config> SdpToConfig(webrtc::SdpAudioFormat format) {
332 if (STR_CASE_CMP(format.name.c_str(), "UnicornSparklesRainbow") == 0) {
333 const webrtc::SdpAudioFormat::Parameters expected_params = {
334 {"num_horns", "1"}};
335 EXPECT_EQ(expected_params, format.parameters);
336 format.parameters.clear();
337 format.name = "L16";
338 return webrtc::AudioDecoderL16::SdpToConfig(format);
339 } else {
Oskar Sundbom63e232a2017-11-16 10:57:03 +0100340 return rtc::nullopt;
Karl Wibergc5bb00b2017-10-10 23:17:17 +0200341 }
342 }
343 static void AppendSupportedDecoders(
344 std::vector<webrtc::AudioCodecSpec>* specs) {
345 std::vector<webrtc::AudioCodecSpec> new_specs;
346 webrtc::AudioDecoderL16::AppendSupportedDecoders(&new_specs);
347 for (auto& spec : new_specs) {
348 spec.format.name = "UnicornSparklesRainbow";
349 EXPECT_TRUE(spec.format.parameters.empty());
350 spec.format.parameters.emplace("num_horns", "1");
351 specs->push_back(spec);
352 }
353 }
354 static std::unique_ptr<webrtc::AudioDecoder> MakeAudioDecoder(
Karl Wiberg17668ec2018-03-01 15:13:27 +0100355 const Config& config,
356 rtc::Optional<webrtc::AudioCodecPairId> codec_pair_id = rtc::nullopt) {
357 return webrtc::AudioDecoderL16::MakeAudioDecoder(config, codec_pair_id);
Karl Wibergc5bb00b2017-10-10 23:17:17 +0200358 }
359};
360
361} // namespace
362
kjellander@webrtc.org70c0e292015-11-30 21:45:35 +0100363// Disabled for TSan v2, see
364// https://bugs.chromium.org/p/webrtc/issues/detail?id=4719 for details.
kjellander@webrtc.org3c28d0d2015-12-02 22:53:26 +0100365// Disabled for Mac, see
366// https://bugs.chromium.org/p/webrtc/issues/detail?id=5231 for details.
Karl Wibergc5bb00b2017-10-10 23:17:17 +0200367#if defined(THREAD_SANITIZER) || defined(WEBRTC_MAC)
Steve Anton36da6ff2018-02-16 16:04:20 -0800368TEST_P(PeerConnectionEndToEndTest, DISABLED_Call) {
Karl Wibergc5bb00b2017-10-10 23:17:17 +0200369#else
Steve Anton36da6ff2018-02-16 16:04:20 -0800370TEST_P(PeerConnectionEndToEndTest, Call) {
371#endif // defined(THREAD_SANITIZER) || defined(WEBRTC_MAC)
kwiberg9e5b11e2017-04-19 03:47:57 -0700372 rtc::scoped_refptr<webrtc::AudioDecoderFactory> real_decoder_factory =
373 webrtc::CreateBuiltinAudioDecoderFactory();
374 CreatePcs(nullptr, webrtc::CreateBuiltinAudioEncoderFactory(),
375 CreateForwardingMockDecoderFactory(real_decoder_factory.get()));
wu@webrtc.org364f2042013-11-20 21:49:41 +0000376 GetAndAddUserMedia();
377 Negotiate();
378 WaitForCallEstablished();
379}
380
philipel7703b272016-11-28 16:23:12 +0100381#if !defined(ADDRESS_SANITIZER)
Steve Anton191c39f2018-01-24 19:35:55 -0800382TEST_P(PeerConnectionEndToEndTest, CallWithLegacySdp) {
wu@webrtc.org364f2042013-11-20 21:49:41 +0000383 FakeConstraints pc_constraints;
384 pc_constraints.AddMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
385 false);
kwiberg9e5b11e2017-04-19 03:47:57 -0700386 CreatePcs(&pc_constraints, webrtc::CreateBuiltinAudioEncoderFactory(),
387 webrtc::CreateBuiltinAudioDecoderFactory());
wu@webrtc.org364f2042013-11-20 21:49:41 +0000388 GetAndAddUserMedia();
389 Negotiate();
390 WaitForCallEstablished();
391}
philipel7703b272016-11-28 16:23:12 +0100392#endif // !defined(ADDRESS_SANITIZER)
wu@webrtc.orgb43202d2013-11-22 19:14:25 +0000393
Steve Anton191c39f2018-01-24 19:35:55 -0800394TEST_P(PeerConnectionEndToEndTest, CallWithCustomCodec) {
Karl Wiberg5bdc82a2018-03-22 00:07:39 +0100395 class IdLoggingAudioEncoderFactory : public webrtc::AudioEncoderFactory {
396 public:
397 IdLoggingAudioEncoderFactory(
398 rtc::scoped_refptr<AudioEncoderFactory> real_factory,
399 std::vector<webrtc::AudioCodecPairId>* const codec_ids)
400 : fact_(real_factory), codec_ids_(codec_ids) {}
401 std::vector<webrtc::AudioCodecSpec> GetSupportedEncoders() override {
402 return fact_->GetSupportedEncoders();
403 }
404 rtc::Optional<webrtc::AudioCodecInfo> QueryAudioEncoder(
405 const webrtc::SdpAudioFormat& format) override {
406 return fact_->QueryAudioEncoder(format);
407 }
408 std::unique_ptr<webrtc::AudioEncoder> MakeAudioEncoder(
409 int payload_type,
410 const webrtc::SdpAudioFormat& format,
411 rtc::Optional<webrtc::AudioCodecPairId> codec_pair_id) override {
412 EXPECT_TRUE(codec_pair_id.has_value());
413 codec_ids_->push_back(*codec_pair_id);
414 return fact_->MakeAudioEncoder(payload_type, format, codec_pair_id);
415 }
416
417 private:
418 const rtc::scoped_refptr<webrtc::AudioEncoderFactory> fact_;
419 std::vector<webrtc::AudioCodecPairId>* const codec_ids_;
420 };
421
422 class IdLoggingAudioDecoderFactory : public webrtc::AudioDecoderFactory {
423 public:
424 IdLoggingAudioDecoderFactory(
425 rtc::scoped_refptr<AudioDecoderFactory> real_factory,
426 std::vector<webrtc::AudioCodecPairId>* const codec_ids)
427 : fact_(real_factory), codec_ids_(codec_ids) {}
428 std::vector<webrtc::AudioCodecSpec> GetSupportedDecoders() override {
429 return fact_->GetSupportedDecoders();
430 }
431 bool IsSupportedDecoder(const webrtc::SdpAudioFormat& format) override {
432 return fact_->IsSupportedDecoder(format);
433 }
434 std::unique_ptr<webrtc::AudioDecoder> MakeAudioDecoder(
435 const webrtc::SdpAudioFormat& format,
436 rtc::Optional<webrtc::AudioCodecPairId> codec_pair_id) override {
437 EXPECT_TRUE(codec_pair_id.has_value());
438 codec_ids_->push_back(*codec_pair_id);
439 return fact_->MakeAudioDecoder(format, codec_pair_id);
440 }
441
442 private:
443 const rtc::scoped_refptr<webrtc::AudioDecoderFactory> fact_;
444 std::vector<webrtc::AudioCodecPairId>* const codec_ids_;
445 };
446
447 std::vector<webrtc::AudioCodecPairId> encoder_id1, encoder_id2, decoder_id1,
448 decoder_id2;
449 CreatePcs(nullptr,
450 rtc::scoped_refptr<webrtc::AudioEncoderFactory>(
451 new rtc::RefCountedObject<IdLoggingAudioEncoderFactory>(
452 webrtc::CreateAudioEncoderFactory<
453 AudioEncoderUnicornSparklesRainbow>(),
454 &encoder_id1)),
455 rtc::scoped_refptr<webrtc::AudioDecoderFactory>(
456 new rtc::RefCountedObject<IdLoggingAudioDecoderFactory>(
457 webrtc::CreateAudioDecoderFactory<
458 AudioDecoderUnicornSparklesRainbow>(),
459 &decoder_id1)),
460 rtc::scoped_refptr<webrtc::AudioEncoderFactory>(
461 new rtc::RefCountedObject<IdLoggingAudioEncoderFactory>(
462 webrtc::CreateAudioEncoderFactory<
463 AudioEncoderUnicornSparklesRainbow>(),
464 &encoder_id2)),
465 rtc::scoped_refptr<webrtc::AudioDecoderFactory>(
466 new rtc::RefCountedObject<IdLoggingAudioDecoderFactory>(
467 webrtc::CreateAudioDecoderFactory<
468 AudioDecoderUnicornSparklesRainbow>(),
469 &decoder_id2)));
Karl Wibergc5bb00b2017-10-10 23:17:17 +0200470 GetAndAddUserMedia();
471 Negotiate();
472 WaitForCallEstablished();
Karl Wiberg5bdc82a2018-03-22 00:07:39 +0100473
474 // Each codec factory has been used to create one codec. The first pair got
475 // the same ID because they were passed to the same PeerConnectionFactory,
476 // and the second pair got the same ID---but these two IDs are not equal,
477 // because each PeerConnectionFactory has its own ID.
478 EXPECT_EQ(1, encoder_id1.size());
479 EXPECT_EQ(1, encoder_id2.size());
480 EXPECT_EQ(encoder_id1, decoder_id1);
481 EXPECT_EQ(encoder_id2, decoder_id2);
482 EXPECT_NE(encoder_id1, encoder_id2);
Karl Wibergc5bb00b2017-10-10 23:17:17 +0200483}
484
deadbeef40610e22016-12-22 10:53:38 -0800485#ifdef HAVE_SCTP
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000486// Verifies that a DataChannel created before the negotiation can transition to
487// "OPEN" and transfer data.
Steve Anton191c39f2018-01-24 19:35:55 -0800488TEST_P(PeerConnectionEndToEndTest, CreateDataChannelBeforeNegotiate) {
kwiberg9e5b11e2017-04-19 03:47:57 -0700489 CreatePcs(nullptr, webrtc::CreateBuiltinAudioEncoderFactory(),
kwiberg7a12b5a2017-04-27 03:55:57 -0700490 webrtc::MockAudioDecoderFactory::CreateEmptyFactory());
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000491
492 webrtc::DataChannelInit init;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000493 rtc::scoped_refptr<DataChannelInterface> caller_dc(
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000494 caller_->CreateDataChannel("data", init));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000495 rtc::scoped_refptr<DataChannelInterface> callee_dc(
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000496 callee_->CreateDataChannel("data", init));
497
498 Negotiate();
499 WaitForConnection();
500
501 WaitForDataChannelsToOpen(caller_dc, callee_signaled_data_channels_, 0);
502 WaitForDataChannelsToOpen(callee_dc, caller_signaled_data_channels_, 0);
503
504 TestDataChannelSendAndReceive(caller_dc, callee_signaled_data_channels_[0]);
505 TestDataChannelSendAndReceive(callee_dc, caller_signaled_data_channels_[0]);
506
507 CloseDataChannels(caller_dc, callee_signaled_data_channels_, 0);
508 CloseDataChannels(callee_dc, caller_signaled_data_channels_, 0);
509}
510
511// Verifies that a DataChannel created after the negotiation can transition to
512// "OPEN" and transfer data.
Steve Anton191c39f2018-01-24 19:35:55 -0800513TEST_P(PeerConnectionEndToEndTest, CreateDataChannelAfterNegotiate) {
kwiberg9e5b11e2017-04-19 03:47:57 -0700514 CreatePcs(nullptr, webrtc::CreateBuiltinAudioEncoderFactory(),
kwiberg7a12b5a2017-04-27 03:55:57 -0700515 webrtc::MockAudioDecoderFactory::CreateEmptyFactory());
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000516
517 webrtc::DataChannelInit init;
518
519 // This DataChannel is for creating the data content in the negotiation.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000520 rtc::scoped_refptr<DataChannelInterface> dummy(
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000521 caller_->CreateDataChannel("data", init));
522 Negotiate();
523 WaitForConnection();
524
Taylor Brandstetterbf2f5692016-06-29 11:22:47 -0700525 // Wait for the data channel created pre-negotiation to be opened.
526 WaitForDataChannelsToOpen(dummy, callee_signaled_data_channels_, 0);
527
528 // Create new DataChannels after the negotiation and verify their states.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000529 rtc::scoped_refptr<DataChannelInterface> caller_dc(
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000530 caller_->CreateDataChannel("hello", init));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000531 rtc::scoped_refptr<DataChannelInterface> callee_dc(
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000532 callee_->CreateDataChannel("hello", init));
533
534 WaitForDataChannelsToOpen(caller_dc, callee_signaled_data_channels_, 1);
535 WaitForDataChannelsToOpen(callee_dc, caller_signaled_data_channels_, 0);
536
537 TestDataChannelSendAndReceive(caller_dc, callee_signaled_data_channels_[1]);
538 TestDataChannelSendAndReceive(callee_dc, caller_signaled_data_channels_[0]);
539
540 CloseDataChannels(caller_dc, callee_signaled_data_channels_, 1);
541 CloseDataChannels(callee_dc, caller_signaled_data_channels_, 0);
542}
543
544// Verifies that DataChannel IDs are even/odd based on the DTLS roles.
Steve Anton191c39f2018-01-24 19:35:55 -0800545TEST_P(PeerConnectionEndToEndTest, DataChannelIdAssignment) {
kwiberg9e5b11e2017-04-19 03:47:57 -0700546 CreatePcs(nullptr, webrtc::CreateBuiltinAudioEncoderFactory(),
kwiberg7a12b5a2017-04-27 03:55:57 -0700547 webrtc::MockAudioDecoderFactory::CreateEmptyFactory());
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000548
549 webrtc::DataChannelInit init;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000550 rtc::scoped_refptr<DataChannelInterface> caller_dc_1(
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000551 caller_->CreateDataChannel("data", init));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000552 rtc::scoped_refptr<DataChannelInterface> callee_dc_1(
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000553 callee_->CreateDataChannel("data", init));
554
555 Negotiate();
556 WaitForConnection();
557
558 EXPECT_EQ(1U, caller_dc_1->id() % 2);
559 EXPECT_EQ(0U, callee_dc_1->id() % 2);
560
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000561 rtc::scoped_refptr<DataChannelInterface> caller_dc_2(
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000562 caller_->CreateDataChannel("data", init));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000563 rtc::scoped_refptr<DataChannelInterface> callee_dc_2(
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000564 callee_->CreateDataChannel("data", init));
565
566 EXPECT_EQ(1U, caller_dc_2->id() % 2);
567 EXPECT_EQ(0U, callee_dc_2->id() % 2);
568}
569
570// Verifies that the message is received by the right remote DataChannel when
571// there are multiple DataChannels.
Steve Anton191c39f2018-01-24 19:35:55 -0800572TEST_P(PeerConnectionEndToEndTest,
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000573 MessageTransferBetweenTwoPairsOfDataChannels) {
kwiberg9e5b11e2017-04-19 03:47:57 -0700574 CreatePcs(nullptr, webrtc::CreateBuiltinAudioEncoderFactory(),
kwiberg7a12b5a2017-04-27 03:55:57 -0700575 webrtc::MockAudioDecoderFactory::CreateEmptyFactory());
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000576
577 webrtc::DataChannelInit init;
578
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> caller_dc_2(
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000582 caller_->CreateDataChannel("data", init));
583
584 Negotiate();
585 WaitForConnection();
586 WaitForDataChannelsToOpen(caller_dc_1, callee_signaled_data_channels_, 0);
587 WaitForDataChannelsToOpen(caller_dc_2, callee_signaled_data_channels_, 1);
588
kwibergd1fe2812016-04-27 06:47:29 -0700589 std::unique_ptr<webrtc::MockDataChannelObserver> dc_1_observer(
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000590 new webrtc::MockDataChannelObserver(callee_signaled_data_channels_[0]));
591
kwibergd1fe2812016-04-27 06:47:29 -0700592 std::unique_ptr<webrtc::MockDataChannelObserver> dc_2_observer(
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000593 new webrtc::MockDataChannelObserver(callee_signaled_data_channels_[1]));
594
595 const std::string message_1 = "hello 1";
596 const std::string message_2 = "hello 2";
597
598 caller_dc_1->Send(webrtc::DataBuffer(message_1));
599 EXPECT_EQ_WAIT(message_1, dc_1_observer->last_message(), kMaxWait);
600
601 caller_dc_2->Send(webrtc::DataBuffer(message_2));
602 EXPECT_EQ_WAIT(message_2, dc_2_observer->last_message(), kMaxWait);
603
604 EXPECT_EQ(1U, dc_1_observer->received_message_count());
605 EXPECT_EQ(1U, dc_2_observer->received_message_count());
606}
deadbeefab9b2d12015-10-14 11:33:11 -0700607
608// Verifies that a DataChannel added from an OPEN message functions after
609// a channel has been previously closed (webrtc issue 3778).
610// This previously failed because the new channel re-uses the ID of the closed
611// channel, and the closed channel was incorrectly still assigned to the id.
612// TODO(deadbeef): This is disabled because there's currently a race condition
613// caused by the fact that a data channel signals that it's closed before it
614// really is. Re-enable this test once that's fixed.
deadbeefe2213ce2016-11-03 16:01:57 -0700615// See: https://bugs.chromium.org/p/webrtc/issues/detail?id=4453
Steve Anton191c39f2018-01-24 19:35:55 -0800616TEST_P(PeerConnectionEndToEndTest,
deadbeefab9b2d12015-10-14 11:33:11 -0700617 DISABLED_DataChannelFromOpenWorksAfterClose) {
kwiberg9e5b11e2017-04-19 03:47:57 -0700618 CreatePcs(nullptr, webrtc::CreateBuiltinAudioEncoderFactory(),
kwiberg7a12b5a2017-04-27 03:55:57 -0700619 webrtc::MockAudioDecoderFactory::CreateEmptyFactory());
deadbeefab9b2d12015-10-14 11:33:11 -0700620
621 webrtc::DataChannelInit init;
622 rtc::scoped_refptr<DataChannelInterface> caller_dc(
623 caller_->CreateDataChannel("data", init));
624
625 Negotiate();
626 WaitForConnection();
627
628 WaitForDataChannelsToOpen(caller_dc, callee_signaled_data_channels_, 0);
629 CloseDataChannels(caller_dc, callee_signaled_data_channels_, 0);
630
631 // Create a new channel and ensure it works after closing the previous one.
632 caller_dc = caller_->CreateDataChannel("data2", init);
633
634 WaitForDataChannelsToOpen(caller_dc, callee_signaled_data_channels_, 1);
635 TestDataChannelSendAndReceive(caller_dc, callee_signaled_data_channels_[1]);
636
637 CloseDataChannels(caller_dc, callee_signaled_data_channels_, 1);
638}
deadbeefbd292462015-12-14 18:15:29 -0800639
640// This tests that if a data channel is closed remotely while not referenced
641// by the application (meaning only the PeerConnection contributes to its
642// reference count), no memory access violation will occur.
643// See: https://code.google.com/p/chromium/issues/detail?id=565048
Steve Anton191c39f2018-01-24 19:35:55 -0800644TEST_P(PeerConnectionEndToEndTest, CloseDataChannelRemotelyWhileNotReferenced) {
kwiberg9e5b11e2017-04-19 03:47:57 -0700645 CreatePcs(nullptr, webrtc::CreateBuiltinAudioEncoderFactory(),
kwiberg7a12b5a2017-04-27 03:55:57 -0700646 webrtc::MockAudioDecoderFactory::CreateEmptyFactory());
deadbeefbd292462015-12-14 18:15:29 -0800647
648 webrtc::DataChannelInit init;
649 rtc::scoped_refptr<DataChannelInterface> caller_dc(
650 caller_->CreateDataChannel("data", init));
651
652 Negotiate();
653 WaitForConnection();
654
655 WaitForDataChannelsToOpen(caller_dc, callee_signaled_data_channels_, 0);
656 // This removes the reference to the remote data channel that we hold.
657 callee_signaled_data_channels_.clear();
658 caller_dc->Close();
659 EXPECT_EQ_WAIT(DataChannelInterface::kClosed, caller_dc->state(), kMaxWait);
660
661 // Wait for a bit longer so the remote data channel will receive the
662 // close message and be destroyed.
663 rtc::Thread::Current()->ProcessMessages(100);
664}
deadbeef40610e22016-12-22 10:53:38 -0800665#endif // HAVE_SCTP
Steve Anton191c39f2018-01-24 19:35:55 -0800666
667INSTANTIATE_TEST_CASE_P(PeerConnectionEndToEndTest,
668 PeerConnectionEndToEndTest,
669 Values(SdpSemantics::kPlanB,
670 SdpSemantics::kUnifiedPlan));