blob: 2f5631ac0dee4529da8e84b5314e74c449ec45a5 [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
Taylor Brandstettercdd05f02018-05-31 13:23:32 -0700171 ASSERT_TRUE_WAIT(remote_dc_list.size() > remote_dc_index, kMaxWait);
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000172 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.
Kári Tristan Helgason983042b2018-04-09 15:07:54 +0200365#if defined(THREAD_SANITIZER)
Steve Anton36da6ff2018-02-16 16:04:20 -0800366TEST_P(PeerConnectionEndToEndTest, DISABLED_Call) {
Karl Wibergc5bb00b2017-10-10 23:17:17 +0200367#else
Steve Anton36da6ff2018-02-16 16:04:20 -0800368TEST_P(PeerConnectionEndToEndTest, Call) {
Kári Tristan Helgason983042b2018-04-09 15:07:54 +0200369#endif // defined(THREAD_SANITIZER)
kwiberg9e5b11e2017-04-19 03:47:57 -0700370 rtc::scoped_refptr<webrtc::AudioDecoderFactory> real_decoder_factory =
371 webrtc::CreateBuiltinAudioDecoderFactory();
372 CreatePcs(nullptr, webrtc::CreateBuiltinAudioEncoderFactory(),
373 CreateForwardingMockDecoderFactory(real_decoder_factory.get()));
wu@webrtc.org364f2042013-11-20 21:49:41 +0000374 GetAndAddUserMedia();
375 Negotiate();
376 WaitForCallEstablished();
377}
378
philipel7703b272016-11-28 16:23:12 +0100379#if !defined(ADDRESS_SANITIZER)
Steve Anton191c39f2018-01-24 19:35:55 -0800380TEST_P(PeerConnectionEndToEndTest, CallWithLegacySdp) {
wu@webrtc.org364f2042013-11-20 21:49:41 +0000381 FakeConstraints pc_constraints;
382 pc_constraints.AddMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
383 false);
kwiberg9e5b11e2017-04-19 03:47:57 -0700384 CreatePcs(&pc_constraints, webrtc::CreateBuiltinAudioEncoderFactory(),
385 webrtc::CreateBuiltinAudioDecoderFactory());
wu@webrtc.org364f2042013-11-20 21:49:41 +0000386 GetAndAddUserMedia();
387 Negotiate();
388 WaitForCallEstablished();
389}
philipel7703b272016-11-28 16:23:12 +0100390#endif // !defined(ADDRESS_SANITIZER)
wu@webrtc.orgb43202d2013-11-22 19:14:25 +0000391
Steve Anton191c39f2018-01-24 19:35:55 -0800392TEST_P(PeerConnectionEndToEndTest, CallWithCustomCodec) {
Karl Wiberg5bdc82a2018-03-22 00:07:39 +0100393 class IdLoggingAudioEncoderFactory : public webrtc::AudioEncoderFactory {
394 public:
395 IdLoggingAudioEncoderFactory(
396 rtc::scoped_refptr<AudioEncoderFactory> real_factory,
397 std::vector<webrtc::AudioCodecPairId>* const codec_ids)
398 : fact_(real_factory), codec_ids_(codec_ids) {}
399 std::vector<webrtc::AudioCodecSpec> GetSupportedEncoders() override {
400 return fact_->GetSupportedEncoders();
401 }
402 rtc::Optional<webrtc::AudioCodecInfo> QueryAudioEncoder(
403 const webrtc::SdpAudioFormat& format) override {
404 return fact_->QueryAudioEncoder(format);
405 }
406 std::unique_ptr<webrtc::AudioEncoder> MakeAudioEncoder(
407 int payload_type,
408 const webrtc::SdpAudioFormat& format,
409 rtc::Optional<webrtc::AudioCodecPairId> codec_pair_id) override {
410 EXPECT_TRUE(codec_pair_id.has_value());
411 codec_ids_->push_back(*codec_pair_id);
412 return fact_->MakeAudioEncoder(payload_type, format, codec_pair_id);
413 }
414
415 private:
416 const rtc::scoped_refptr<webrtc::AudioEncoderFactory> fact_;
417 std::vector<webrtc::AudioCodecPairId>* const codec_ids_;
418 };
419
420 class IdLoggingAudioDecoderFactory : public webrtc::AudioDecoderFactory {
421 public:
422 IdLoggingAudioDecoderFactory(
423 rtc::scoped_refptr<AudioDecoderFactory> real_factory,
424 std::vector<webrtc::AudioCodecPairId>* const codec_ids)
425 : fact_(real_factory), codec_ids_(codec_ids) {}
426 std::vector<webrtc::AudioCodecSpec> GetSupportedDecoders() override {
427 return fact_->GetSupportedDecoders();
428 }
429 bool IsSupportedDecoder(const webrtc::SdpAudioFormat& format) override {
430 return fact_->IsSupportedDecoder(format);
431 }
432 std::unique_ptr<webrtc::AudioDecoder> MakeAudioDecoder(
433 const webrtc::SdpAudioFormat& format,
434 rtc::Optional<webrtc::AudioCodecPairId> codec_pair_id) override {
435 EXPECT_TRUE(codec_pair_id.has_value());
436 codec_ids_->push_back(*codec_pair_id);
437 return fact_->MakeAudioDecoder(format, codec_pair_id);
438 }
439
440 private:
441 const rtc::scoped_refptr<webrtc::AudioDecoderFactory> fact_;
442 std::vector<webrtc::AudioCodecPairId>* const codec_ids_;
443 };
444
445 std::vector<webrtc::AudioCodecPairId> encoder_id1, encoder_id2, decoder_id1,
446 decoder_id2;
447 CreatePcs(nullptr,
448 rtc::scoped_refptr<webrtc::AudioEncoderFactory>(
449 new rtc::RefCountedObject<IdLoggingAudioEncoderFactory>(
450 webrtc::CreateAudioEncoderFactory<
451 AudioEncoderUnicornSparklesRainbow>(),
452 &encoder_id1)),
453 rtc::scoped_refptr<webrtc::AudioDecoderFactory>(
454 new rtc::RefCountedObject<IdLoggingAudioDecoderFactory>(
455 webrtc::CreateAudioDecoderFactory<
456 AudioDecoderUnicornSparklesRainbow>(),
457 &decoder_id1)),
458 rtc::scoped_refptr<webrtc::AudioEncoderFactory>(
459 new rtc::RefCountedObject<IdLoggingAudioEncoderFactory>(
460 webrtc::CreateAudioEncoderFactory<
461 AudioEncoderUnicornSparklesRainbow>(),
462 &encoder_id2)),
463 rtc::scoped_refptr<webrtc::AudioDecoderFactory>(
464 new rtc::RefCountedObject<IdLoggingAudioDecoderFactory>(
465 webrtc::CreateAudioDecoderFactory<
466 AudioDecoderUnicornSparklesRainbow>(),
467 &decoder_id2)));
Karl Wibergc5bb00b2017-10-10 23:17:17 +0200468 GetAndAddUserMedia();
469 Negotiate();
470 WaitForCallEstablished();
Karl Wiberg5bdc82a2018-03-22 00:07:39 +0100471
472 // Each codec factory has been used to create one codec. The first pair got
473 // the same ID because they were passed to the same PeerConnectionFactory,
474 // and the second pair got the same ID---but these two IDs are not equal,
475 // because each PeerConnectionFactory has its own ID.
476 EXPECT_EQ(1, encoder_id1.size());
477 EXPECT_EQ(1, encoder_id2.size());
478 EXPECT_EQ(encoder_id1, decoder_id1);
479 EXPECT_EQ(encoder_id2, decoder_id2);
480 EXPECT_NE(encoder_id1, encoder_id2);
Karl Wibergc5bb00b2017-10-10 23:17:17 +0200481}
482
deadbeef40610e22016-12-22 10:53:38 -0800483#ifdef HAVE_SCTP
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000484// Verifies that a DataChannel created before the negotiation can transition to
485// "OPEN" and transfer data.
Steve Anton191c39f2018-01-24 19:35:55 -0800486TEST_P(PeerConnectionEndToEndTest, CreateDataChannelBeforeNegotiate) {
kwiberg9e5b11e2017-04-19 03:47:57 -0700487 CreatePcs(nullptr, webrtc::CreateBuiltinAudioEncoderFactory(),
kwiberg7a12b5a2017-04-27 03:55:57 -0700488 webrtc::MockAudioDecoderFactory::CreateEmptyFactory());
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000489
490 webrtc::DataChannelInit init;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000491 rtc::scoped_refptr<DataChannelInterface> caller_dc(
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000492 caller_->CreateDataChannel("data", init));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000493 rtc::scoped_refptr<DataChannelInterface> callee_dc(
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000494 callee_->CreateDataChannel("data", init));
495
496 Negotiate();
497 WaitForConnection();
498
499 WaitForDataChannelsToOpen(caller_dc, callee_signaled_data_channels_, 0);
500 WaitForDataChannelsToOpen(callee_dc, caller_signaled_data_channels_, 0);
501
502 TestDataChannelSendAndReceive(caller_dc, callee_signaled_data_channels_[0]);
503 TestDataChannelSendAndReceive(callee_dc, caller_signaled_data_channels_[0]);
504
505 CloseDataChannels(caller_dc, callee_signaled_data_channels_, 0);
506 CloseDataChannels(callee_dc, caller_signaled_data_channels_, 0);
507}
508
509// Verifies that a DataChannel created after the negotiation can transition to
510// "OPEN" and transfer data.
Steve Anton191c39f2018-01-24 19:35:55 -0800511TEST_P(PeerConnectionEndToEndTest, CreateDataChannelAfterNegotiate) {
kwiberg9e5b11e2017-04-19 03:47:57 -0700512 CreatePcs(nullptr, webrtc::CreateBuiltinAudioEncoderFactory(),
kwiberg7a12b5a2017-04-27 03:55:57 -0700513 webrtc::MockAudioDecoderFactory::CreateEmptyFactory());
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000514
515 webrtc::DataChannelInit init;
516
517 // This DataChannel is for creating the data content in the negotiation.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000518 rtc::scoped_refptr<DataChannelInterface> dummy(
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000519 caller_->CreateDataChannel("data", init));
520 Negotiate();
521 WaitForConnection();
522
Taylor Brandstetterbf2f5692016-06-29 11:22:47 -0700523 // Wait for the data channel created pre-negotiation to be opened.
524 WaitForDataChannelsToOpen(dummy, callee_signaled_data_channels_, 0);
525
526 // Create new DataChannels after the negotiation and verify their states.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000527 rtc::scoped_refptr<DataChannelInterface> caller_dc(
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000528 caller_->CreateDataChannel("hello", init));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000529 rtc::scoped_refptr<DataChannelInterface> callee_dc(
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000530 callee_->CreateDataChannel("hello", init));
531
532 WaitForDataChannelsToOpen(caller_dc, callee_signaled_data_channels_, 1);
533 WaitForDataChannelsToOpen(callee_dc, caller_signaled_data_channels_, 0);
534
535 TestDataChannelSendAndReceive(caller_dc, callee_signaled_data_channels_[1]);
536 TestDataChannelSendAndReceive(callee_dc, caller_signaled_data_channels_[0]);
537
538 CloseDataChannels(caller_dc, callee_signaled_data_channels_, 1);
539 CloseDataChannels(callee_dc, caller_signaled_data_channels_, 0);
540}
541
542// Verifies that DataChannel IDs are even/odd based on the DTLS roles.
Steve Anton191c39f2018-01-24 19:35:55 -0800543TEST_P(PeerConnectionEndToEndTest, DataChannelIdAssignment) {
kwiberg9e5b11e2017-04-19 03:47:57 -0700544 CreatePcs(nullptr, webrtc::CreateBuiltinAudioEncoderFactory(),
kwiberg7a12b5a2017-04-27 03:55:57 -0700545 webrtc::MockAudioDecoderFactory::CreateEmptyFactory());
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000546
547 webrtc::DataChannelInit init;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000548 rtc::scoped_refptr<DataChannelInterface> caller_dc_1(
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000549 caller_->CreateDataChannel("data", init));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000550 rtc::scoped_refptr<DataChannelInterface> callee_dc_1(
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000551 callee_->CreateDataChannel("data", init));
552
553 Negotiate();
554 WaitForConnection();
555
556 EXPECT_EQ(1U, caller_dc_1->id() % 2);
557 EXPECT_EQ(0U, callee_dc_1->id() % 2);
558
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000559 rtc::scoped_refptr<DataChannelInterface> caller_dc_2(
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000560 caller_->CreateDataChannel("data", init));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000561 rtc::scoped_refptr<DataChannelInterface> callee_dc_2(
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000562 callee_->CreateDataChannel("data", init));
563
564 EXPECT_EQ(1U, caller_dc_2->id() % 2);
565 EXPECT_EQ(0U, callee_dc_2->id() % 2);
566}
567
568// Verifies that the message is received by the right remote DataChannel when
569// there are multiple DataChannels.
Steve Anton191c39f2018-01-24 19:35:55 -0800570TEST_P(PeerConnectionEndToEndTest,
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000571 MessageTransferBetweenTwoPairsOfDataChannels) {
kwiberg9e5b11e2017-04-19 03:47:57 -0700572 CreatePcs(nullptr, webrtc::CreateBuiltinAudioEncoderFactory(),
kwiberg7a12b5a2017-04-27 03:55:57 -0700573 webrtc::MockAudioDecoderFactory::CreateEmptyFactory());
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000574
575 webrtc::DataChannelInit init;
576
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000577 rtc::scoped_refptr<DataChannelInterface> caller_dc_1(
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000578 caller_->CreateDataChannel("data", init));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000579 rtc::scoped_refptr<DataChannelInterface> caller_dc_2(
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000580 caller_->CreateDataChannel("data", init));
581
582 Negotiate();
583 WaitForConnection();
584 WaitForDataChannelsToOpen(caller_dc_1, callee_signaled_data_channels_, 0);
585 WaitForDataChannelsToOpen(caller_dc_2, callee_signaled_data_channels_, 1);
586
kwibergd1fe2812016-04-27 06:47:29 -0700587 std::unique_ptr<webrtc::MockDataChannelObserver> dc_1_observer(
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000588 new webrtc::MockDataChannelObserver(callee_signaled_data_channels_[0]));
589
kwibergd1fe2812016-04-27 06:47:29 -0700590 std::unique_ptr<webrtc::MockDataChannelObserver> dc_2_observer(
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000591 new webrtc::MockDataChannelObserver(callee_signaled_data_channels_[1]));
592
593 const std::string message_1 = "hello 1";
594 const std::string message_2 = "hello 2";
595
596 caller_dc_1->Send(webrtc::DataBuffer(message_1));
597 EXPECT_EQ_WAIT(message_1, dc_1_observer->last_message(), kMaxWait);
598
599 caller_dc_2->Send(webrtc::DataBuffer(message_2));
600 EXPECT_EQ_WAIT(message_2, dc_2_observer->last_message(), kMaxWait);
601
602 EXPECT_EQ(1U, dc_1_observer->received_message_count());
603 EXPECT_EQ(1U, dc_2_observer->received_message_count());
604}
deadbeefab9b2d12015-10-14 11:33:11 -0700605
606// Verifies that a DataChannel added from an OPEN message functions after
607// a channel has been previously closed (webrtc issue 3778).
Taylor Brandstettercdd05f02018-05-31 13:23:32 -0700608// This previously failed because the new channel re-used the ID of the closed
609// channel, and the closed channel was incorrectly still assigned to the ID.
Steve Anton191c39f2018-01-24 19:35:55 -0800610TEST_P(PeerConnectionEndToEndTest,
Taylor Brandstettercdd05f02018-05-31 13:23:32 -0700611 DataChannelFromOpenWorksAfterPreviousChannelClosed) {
kwiberg9e5b11e2017-04-19 03:47:57 -0700612 CreatePcs(nullptr, webrtc::CreateBuiltinAudioEncoderFactory(),
kwiberg7a12b5a2017-04-27 03:55:57 -0700613 webrtc::MockAudioDecoderFactory::CreateEmptyFactory());
deadbeefab9b2d12015-10-14 11:33:11 -0700614
615 webrtc::DataChannelInit init;
616 rtc::scoped_refptr<DataChannelInterface> caller_dc(
617 caller_->CreateDataChannel("data", init));
618
619 Negotiate();
620 WaitForConnection();
621
622 WaitForDataChannelsToOpen(caller_dc, callee_signaled_data_channels_, 0);
Taylor Brandstettercdd05f02018-05-31 13:23:32 -0700623 int first_channel_id = caller_dc->id();
624 // Wait for the local side to say it's closed, but not the remote side.
625 // Previously, the channel on which Close is called reported being closed
626 // prematurely, and this caused issues; see bugs.webrtc.org/4453.
627 caller_dc->Close();
628 EXPECT_EQ_WAIT(DataChannelInterface::kClosed, caller_dc->state(), kMaxWait);
deadbeefab9b2d12015-10-14 11:33:11 -0700629
630 // Create a new channel and ensure it works after closing the previous one.
631 caller_dc = caller_->CreateDataChannel("data2", init);
deadbeefab9b2d12015-10-14 11:33:11 -0700632 WaitForDataChannelsToOpen(caller_dc, callee_signaled_data_channels_, 1);
Taylor Brandstettercdd05f02018-05-31 13:23:32 -0700633 // Since the second channel was created after the first finished closing, it
634 // should be able to re-use the first one's ID.
635 EXPECT_EQ(first_channel_id, caller_dc->id());
636 TestDataChannelSendAndReceive(caller_dc, callee_signaled_data_channels_[1]);
637
638 CloseDataChannels(caller_dc, callee_signaled_data_channels_, 1);
639}
640
641// Similar to the above test, but don't wait for the first channel to finish
642// closing before creating the second one.
643TEST_P(PeerConnectionEndToEndTest,
644 DataChannelFromOpenWorksWhilePreviousChannelClosing) {
645 CreatePcs(nullptr, webrtc::CreateBuiltinAudioEncoderFactory(),
646 webrtc::MockAudioDecoderFactory::CreateEmptyFactory());
647
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 int first_channel_id = caller_dc->id();
657 caller_dc->Close();
658
659 // Immediately create a new channel, before waiting for the previous one to
660 // transition to "closed".
661 caller_dc = caller_->CreateDataChannel("data2", init);
662 WaitForDataChannelsToOpen(caller_dc, callee_signaled_data_channels_, 1);
663 // Since the second channel was created while the first was still closing,
664 // it should have been assigned a different ID.
665 EXPECT_NE(first_channel_id, caller_dc->id());
deadbeefab9b2d12015-10-14 11:33:11 -0700666 TestDataChannelSendAndReceive(caller_dc, callee_signaled_data_channels_[1]);
667
668 CloseDataChannels(caller_dc, callee_signaled_data_channels_, 1);
669}
deadbeefbd292462015-12-14 18:15:29 -0800670
671// This tests that if a data channel is closed remotely while not referenced
672// by the application (meaning only the PeerConnection contributes to its
673// reference count), no memory access violation will occur.
674// See: https://code.google.com/p/chromium/issues/detail?id=565048
Steve Anton191c39f2018-01-24 19:35:55 -0800675TEST_P(PeerConnectionEndToEndTest, CloseDataChannelRemotelyWhileNotReferenced) {
kwiberg9e5b11e2017-04-19 03:47:57 -0700676 CreatePcs(nullptr, webrtc::CreateBuiltinAudioEncoderFactory(),
kwiberg7a12b5a2017-04-27 03:55:57 -0700677 webrtc::MockAudioDecoderFactory::CreateEmptyFactory());
deadbeefbd292462015-12-14 18:15:29 -0800678
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 // This removes the reference to the remote data channel that we hold.
688 callee_signaled_data_channels_.clear();
689 caller_dc->Close();
690 EXPECT_EQ_WAIT(DataChannelInterface::kClosed, caller_dc->state(), kMaxWait);
691
692 // Wait for a bit longer so the remote data channel will receive the
693 // close message and be destroyed.
694 rtc::Thread::Current()->ProcessMessages(100);
695}
deadbeef40610e22016-12-22 10:53:38 -0800696#endif // HAVE_SCTP
Steve Anton191c39f2018-01-24 19:35:55 -0800697
698INSTANTIATE_TEST_CASE_P(PeerConnectionEndToEndTest,
699 PeerConnectionEndToEndTest,
700 Values(SdpSemantics::kPlanB,
701 SdpSemantics::kUnifiedPlan));