blob: dce69dabcc5bbb3f9094cb9ff9a8cbe60fc71b58 [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"
15#include "api/audio_codecs/audio_decoder_factory_template.h"
16#include "api/audio_codecs/audio_encoder_factory_template.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020017#include "api/audio_codecs/builtin_audio_decoder_factory.h"
18#include "api/audio_codecs/builtin_audio_encoder_factory.h"
19#include "rtc_base/gunit.h"
20#include "rtc_base/logging.h"
21#include "rtc_base/ptr_util.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#include "rtc_base/stringencode.h"
23#include "rtc_base/stringutils.h"
Patrik Höglund563934e2017-09-15 09:04:28 +020024
ossu7bb87ee2017-01-23 04:56:25 -080025#ifdef WEBRTC_ANDROID
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020026#include "pc/test/androidtestinitializer.h"
ossu7bb87ee2017-01-23 04:56:25 -080027#endif
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020028#include "pc/test/peerconnectiontestwrapper.h"
ossu7bb87ee2017-01-23 04:56:25 -080029// Notice that mockpeerconnectionobservers.h must be included after the above!
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020030#include "pc/test/mockpeerconnectionobservers.h"
31#include "test/mock_audio_decoder.h"
32#include "test/mock_audio_decoder_factory.h"
kwiberg9e5b11e2017-04-19 03:47:57 -070033
34using testing::AtLeast;
35using testing::Invoke;
36using testing::StrictMock;
37using testing::_;
wu@webrtc.org364f2042013-11-20 21:49:41 +000038
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +000039using webrtc::DataChannelInterface;
wu@webrtc.org364f2042013-11-20 21:49:41 +000040using webrtc::FakeConstraints;
41using webrtc::MediaConstraintsInterface;
42using webrtc::MediaStreamInterface;
43using webrtc::PeerConnectionInterface;
44
45namespace {
46
Honghai Zhang82d78622016-05-06 11:29:15 -070047const int kMaxWait = 10000;
wu@webrtc.org364f2042013-11-20 21:49:41 +000048
wu@webrtc.org364f2042013-11-20 21:49:41 +000049} // namespace
50
51class PeerConnectionEndToEndTest
52 : public sigslot::has_slots<>,
53 public testing::Test {
54 public:
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000055 typedef std::vector<rtc::scoped_refptr<DataChannelInterface> >
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +000056 DataChannelList;
57
perkj57db6522016-04-08 08:16:33 -070058 PeerConnectionEndToEndTest() {
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);
70
phoglund37ebcf02016-01-08 05:04:57 -080071#ifdef WEBRTC_ANDROID
72 webrtc::InitializeAndroidObjects();
73#endif
wu@webrtc.org364f2042013-11-20 21:49:41 +000074 }
75
kwiberg9e5b11e2017-04-19 03:47:57 -070076 void CreatePcs(
77 const MediaConstraintsInterface* pc_constraints,
78 rtc::scoped_refptr<webrtc::AudioEncoderFactory> audio_encoder_factory,
79 rtc::scoped_refptr<webrtc::AudioDecoderFactory> audio_decoder_factory) {
80 EXPECT_TRUE(caller_->CreatePc(
81 pc_constraints, config_, audio_encoder_factory, audio_decoder_factory));
82 EXPECT_TRUE(callee_->CreatePc(
83 pc_constraints, config_, audio_encoder_factory, audio_decoder_factory));
wu@webrtc.org364f2042013-11-20 21:49:41 +000084 PeerConnectionTestWrapper::Connect(caller_.get(), callee_.get());
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +000085
86 caller_->SignalOnDataChannel.connect(
87 this, &PeerConnectionEndToEndTest::OnCallerAddedDataChanel);
88 callee_->SignalOnDataChannel.connect(
89 this, &PeerConnectionEndToEndTest::OnCalleeAddedDataChannel);
wu@webrtc.org364f2042013-11-20 21:49:41 +000090 }
91
92 void GetAndAddUserMedia() {
93 FakeConstraints audio_constraints;
94 FakeConstraints video_constraints;
95 GetAndAddUserMedia(true, audio_constraints, true, video_constraints);
96 }
97
98 void GetAndAddUserMedia(bool audio, FakeConstraints audio_constraints,
99 bool video, FakeConstraints video_constraints) {
100 caller_->GetAndAddUserMedia(audio, audio_constraints,
101 video, video_constraints);
102 callee_->GetAndAddUserMedia(audio, audio_constraints,
103 video, video_constraints);
104 }
105
106 void Negotiate() {
107 caller_->CreateOffer(NULL);
108 }
109
110 void WaitForCallEstablished() {
111 caller_->WaitForCallEstablished();
112 callee_->WaitForCallEstablished();
113 }
114
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000115 void WaitForConnection() {
116 caller_->WaitForConnection();
117 callee_->WaitForConnection();
118 }
119
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000120 void OnCallerAddedDataChanel(DataChannelInterface* dc) {
121 caller_signaled_data_channels_.push_back(dc);
122 }
123
124 void OnCalleeAddedDataChannel(DataChannelInterface* dc) {
125 callee_signaled_data_channels_.push_back(dc);
126 }
127
128 // Tests that |dc1| and |dc2| can send to and receive from each other.
129 void TestDataChannelSendAndReceive(
130 DataChannelInterface* dc1, DataChannelInterface* dc2) {
kwibergd1fe2812016-04-27 06:47:29 -0700131 std::unique_ptr<webrtc::MockDataChannelObserver> dc1_observer(
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000132 new webrtc::MockDataChannelObserver(dc1));
133
kwibergd1fe2812016-04-27 06:47:29 -0700134 std::unique_ptr<webrtc::MockDataChannelObserver> dc2_observer(
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000135 new webrtc::MockDataChannelObserver(dc2));
136
137 static const std::string kDummyData = "abcdefg";
138 webrtc::DataBuffer buffer(kDummyData);
139 EXPECT_TRUE(dc1->Send(buffer));
140 EXPECT_EQ_WAIT(kDummyData, dc2_observer->last_message(), kMaxWait);
141
142 EXPECT_TRUE(dc2->Send(buffer));
143 EXPECT_EQ_WAIT(kDummyData, dc1_observer->last_message(), kMaxWait);
144
145 EXPECT_EQ(1U, dc1_observer->received_message_count());
146 EXPECT_EQ(1U, dc2_observer->received_message_count());
147 }
148
149 void WaitForDataChannelsToOpen(DataChannelInterface* local_dc,
150 const DataChannelList& remote_dc_list,
151 size_t remote_dc_index) {
152 EXPECT_EQ_WAIT(DataChannelInterface::kOpen, local_dc->state(), kMaxWait);
153
154 EXPECT_TRUE_WAIT(remote_dc_list.size() > remote_dc_index, kMaxWait);
155 EXPECT_EQ_WAIT(DataChannelInterface::kOpen,
156 remote_dc_list[remote_dc_index]->state(),
157 kMaxWait);
158 EXPECT_EQ(local_dc->id(), remote_dc_list[remote_dc_index]->id());
159 }
160
161 void CloseDataChannels(DataChannelInterface* local_dc,
162 const DataChannelList& remote_dc_list,
163 size_t remote_dc_index) {
164 local_dc->Close();
165 EXPECT_EQ_WAIT(DataChannelInterface::kClosed, local_dc->state(), kMaxWait);
166 EXPECT_EQ_WAIT(DataChannelInterface::kClosed,
167 remote_dc_list[remote_dc_index]->state(),
168 kMaxWait);
169 }
170
wu@webrtc.org364f2042013-11-20 21:49:41 +0000171 protected:
tommie7251592017-07-14 14:44:46 -0700172 std::unique_ptr<rtc::Thread> network_thread_;
173 std::unique_ptr<rtc::Thread> worker_thread_;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000174 rtc::scoped_refptr<PeerConnectionTestWrapper> caller_;
175 rtc::scoped_refptr<PeerConnectionTestWrapper> callee_;
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000176 DataChannelList caller_signaled_data_channels_;
177 DataChannelList callee_signaled_data_channels_;
zhihuang9763d562016-08-05 11:14:50 -0700178 webrtc::PeerConnectionInterface::RTCConfiguration config_;
wu@webrtc.org364f2042013-11-20 21:49:41 +0000179};
180
Karl Wibergc5bb00b2017-10-10 23:17:17 +0200181namespace {
182
kwiberg9e5b11e2017-04-19 03:47:57 -0700183std::unique_ptr<webrtc::AudioDecoder> CreateForwardingMockDecoder(
184 std::unique_ptr<webrtc::AudioDecoder> real_decoder) {
185 class ForwardingMockDecoder : public StrictMock<webrtc::MockAudioDecoder> {
186 public:
Steve Anton36b29d12017-10-30 09:57:42 -0700187 explicit ForwardingMockDecoder(std::unique_ptr<AudioDecoder> decoder)
kwiberg9e5b11e2017-04-19 03:47:57 -0700188 : decoder_(std::move(decoder)) {}
189
190 private:
191 std::unique_ptr<AudioDecoder> decoder_;
192 };
193
194 const auto dec = real_decoder.get(); // For lambda capturing.
195 auto mock_decoder =
196 rtc::MakeUnique<ForwardingMockDecoder>(std::move(real_decoder));
197 EXPECT_CALL(*mock_decoder, Channels())
198 .Times(AtLeast(1))
199 .WillRepeatedly(Invoke([dec] { return dec->Channels(); }));
200 EXPECT_CALL(*mock_decoder, DecodeInternal(_, _, _, _, _))
201 .Times(AtLeast(1))
202 .WillRepeatedly(
203 Invoke([dec](const uint8_t* encoded, size_t encoded_len,
204 int sample_rate_hz, int16_t* decoded,
205 webrtc::AudioDecoder::SpeechType* speech_type) {
206 return dec->Decode(encoded, encoded_len, sample_rate_hz,
207 std::numeric_limits<size_t>::max(), decoded,
208 speech_type);
209 }));
210 EXPECT_CALL(*mock_decoder, Die());
211 EXPECT_CALL(*mock_decoder, HasDecodePlc()).WillRepeatedly(Invoke([dec] {
212 return dec->HasDecodePlc();
213 }));
214 EXPECT_CALL(*mock_decoder, IncomingPacket(_, _, _, _, _))
215 .Times(AtLeast(1))
216 .WillRepeatedly(Invoke([dec](const uint8_t* payload, size_t payload_len,
217 uint16_t rtp_sequence_number,
218 uint32_t rtp_timestamp,
219 uint32_t arrival_timestamp) {
220 return dec->IncomingPacket(payload, payload_len, rtp_sequence_number,
221 rtp_timestamp, arrival_timestamp);
222 }));
223 EXPECT_CALL(*mock_decoder, PacketDuration(_, _))
224 .Times(AtLeast(1))
225 .WillRepeatedly(Invoke([dec](const uint8_t* encoded, size_t encoded_len) {
226 return dec->PacketDuration(encoded, encoded_len);
227 }));
228 EXPECT_CALL(*mock_decoder, SampleRateHz())
229 .Times(AtLeast(1))
230 .WillRepeatedly(Invoke([dec] { return dec->SampleRateHz(); }));
231
232 return std::move(mock_decoder);
233}
234
235rtc::scoped_refptr<webrtc::AudioDecoderFactory>
236CreateForwardingMockDecoderFactory(
237 webrtc::AudioDecoderFactory* real_decoder_factory) {
238 rtc::scoped_refptr<webrtc::MockAudioDecoderFactory> mock_decoder_factory =
239 new rtc::RefCountedObject<StrictMock<webrtc::MockAudioDecoderFactory>>;
240 EXPECT_CALL(*mock_decoder_factory, GetSupportedDecoders())
241 .Times(AtLeast(1))
242 .WillRepeatedly(Invoke([real_decoder_factory] {
243 return real_decoder_factory->GetSupportedDecoders();
244 }));
245 EXPECT_CALL(*mock_decoder_factory, IsSupportedDecoder(_))
246 .Times(AtLeast(1))
247 .WillRepeatedly(
248 Invoke([real_decoder_factory](const webrtc::SdpAudioFormat& format) {
249 return real_decoder_factory->IsSupportedDecoder(format);
250 }));
251 EXPECT_CALL(*mock_decoder_factory, MakeAudioDecoderMock(_, _))
252 .Times(AtLeast(2))
253 .WillRepeatedly(
254 Invoke([real_decoder_factory](
255 const webrtc::SdpAudioFormat& format,
256 std::unique_ptr<webrtc::AudioDecoder>* return_value) {
257 auto real_decoder = real_decoder_factory->MakeAudioDecoder(format);
258 *return_value =
259 real_decoder
260 ? CreateForwardingMockDecoder(std::move(real_decoder))
261 : nullptr;
262 }));
263 return mock_decoder_factory;
264}
265
Karl Wibergc5bb00b2017-10-10 23:17:17 +0200266struct AudioEncoderUnicornSparklesRainbow {
267 using Config = webrtc::AudioEncoderL16::Config;
268 static rtc::Optional<Config> SdpToConfig(webrtc::SdpAudioFormat format) {
269 if (STR_CASE_CMP(format.name.c_str(), "UnicornSparklesRainbow") == 0) {
270 const webrtc::SdpAudioFormat::Parameters expected_params = {
271 {"num_horns", "1"}};
272 EXPECT_EQ(expected_params, format.parameters);
273 format.parameters.clear();
274 format.name = "L16";
275 return webrtc::AudioEncoderL16::SdpToConfig(format);
276 } else {
277 return rtc::Optional<Config>();
278 }
279 }
280 static void AppendSupportedEncoders(
281 std::vector<webrtc::AudioCodecSpec>* specs) {
282 std::vector<webrtc::AudioCodecSpec> new_specs;
283 webrtc::AudioEncoderL16::AppendSupportedEncoders(&new_specs);
284 for (auto& spec : new_specs) {
285 spec.format.name = "UnicornSparklesRainbow";
286 EXPECT_TRUE(spec.format.parameters.empty());
287 spec.format.parameters.emplace("num_horns", "1");
288 specs->push_back(spec);
289 }
290 }
291 static webrtc::AudioCodecInfo QueryAudioEncoder(const Config& config) {
292 return webrtc::AudioEncoderL16::QueryAudioEncoder(config);
293 }
294 static std::unique_ptr<webrtc::AudioEncoder> MakeAudioEncoder(
295 const Config& config,
296 int payload_type) {
297 return webrtc::AudioEncoderL16::MakeAudioEncoder(config, payload_type);
298 }
299};
300
301struct AudioDecoderUnicornSparklesRainbow {
302 using Config = webrtc::AudioDecoderL16::Config;
303 static rtc::Optional<Config> SdpToConfig(webrtc::SdpAudioFormat format) {
304 if (STR_CASE_CMP(format.name.c_str(), "UnicornSparklesRainbow") == 0) {
305 const webrtc::SdpAudioFormat::Parameters expected_params = {
306 {"num_horns", "1"}};
307 EXPECT_EQ(expected_params, format.parameters);
308 format.parameters.clear();
309 format.name = "L16";
310 return webrtc::AudioDecoderL16::SdpToConfig(format);
311 } else {
312 return rtc::Optional<Config>();
313 }
314 }
315 static void AppendSupportedDecoders(
316 std::vector<webrtc::AudioCodecSpec>* specs) {
317 std::vector<webrtc::AudioCodecSpec> new_specs;
318 webrtc::AudioDecoderL16::AppendSupportedDecoders(&new_specs);
319 for (auto& spec : new_specs) {
320 spec.format.name = "UnicornSparklesRainbow";
321 EXPECT_TRUE(spec.format.parameters.empty());
322 spec.format.parameters.emplace("num_horns", "1");
323 specs->push_back(spec);
324 }
325 }
326 static std::unique_ptr<webrtc::AudioDecoder> MakeAudioDecoder(
327 const Config& config) {
328 return webrtc::AudioDecoderL16::MakeAudioDecoder(config);
329 }
330};
331
332} // namespace
333
kjellander@webrtc.org70c0e292015-11-30 21:45:35 +0100334// Disabled for TSan v2, see
335// https://bugs.chromium.org/p/webrtc/issues/detail?id=4719 for details.
kjellander@webrtc.org3c28d0d2015-12-02 22:53:26 +0100336// Disabled for Mac, see
337// https://bugs.chromium.org/p/webrtc/issues/detail?id=5231 for details.
Karl Wibergc5bb00b2017-10-10 23:17:17 +0200338#if defined(THREAD_SANITIZER) || defined(WEBRTC_MAC)
339#define MAYBE_Call DISABLED_Call
340#else
341#define MAYBE_Call Call
342#endif
343TEST_F(PeerConnectionEndToEndTest, MAYBE_Call) {
kwiberg9e5b11e2017-04-19 03:47:57 -0700344 rtc::scoped_refptr<webrtc::AudioDecoderFactory> real_decoder_factory =
345 webrtc::CreateBuiltinAudioDecoderFactory();
346 CreatePcs(nullptr, webrtc::CreateBuiltinAudioEncoderFactory(),
347 CreateForwardingMockDecoderFactory(real_decoder_factory.get()));
wu@webrtc.org364f2042013-11-20 21:49:41 +0000348 GetAndAddUserMedia();
349 Negotiate();
350 WaitForCallEstablished();
351}
352
philipel7703b272016-11-28 16:23:12 +0100353#if !defined(ADDRESS_SANITIZER)
deadbeefc9be0072015-12-14 18:27:57 -0800354TEST_F(PeerConnectionEndToEndTest, CallWithLegacySdp) {
wu@webrtc.org364f2042013-11-20 21:49:41 +0000355 FakeConstraints pc_constraints;
356 pc_constraints.AddMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
357 false);
kwiberg9e5b11e2017-04-19 03:47:57 -0700358 CreatePcs(&pc_constraints, webrtc::CreateBuiltinAudioEncoderFactory(),
359 webrtc::CreateBuiltinAudioDecoderFactory());
wu@webrtc.org364f2042013-11-20 21:49:41 +0000360 GetAndAddUserMedia();
361 Negotiate();
362 WaitForCallEstablished();
363}
philipel7703b272016-11-28 16:23:12 +0100364#endif // !defined(ADDRESS_SANITIZER)
wu@webrtc.orgb43202d2013-11-22 19:14:25 +0000365
Karl Wibergc5bb00b2017-10-10 23:17:17 +0200366TEST_F(PeerConnectionEndToEndTest, CallWithCustomCodec) {
367 CreatePcs(
368 nullptr,
369 webrtc::CreateAudioEncoderFactory<AudioEncoderUnicornSparklesRainbow>(),
370 webrtc::CreateAudioDecoderFactory<AudioDecoderUnicornSparklesRainbow>());
371 GetAndAddUserMedia();
372 Negotiate();
373 WaitForCallEstablished();
374}
375
deadbeef40610e22016-12-22 10:53:38 -0800376#ifdef HAVE_SCTP
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000377// Verifies that a DataChannel created before the negotiation can transition to
378// "OPEN" and transfer data.
379TEST_F(PeerConnectionEndToEndTest, CreateDataChannelBeforeNegotiate) {
kwiberg9e5b11e2017-04-19 03:47:57 -0700380 CreatePcs(nullptr, webrtc::CreateBuiltinAudioEncoderFactory(),
kwiberg7a12b5a2017-04-27 03:55:57 -0700381 webrtc::MockAudioDecoderFactory::CreateEmptyFactory());
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000382
383 webrtc::DataChannelInit init;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000384 rtc::scoped_refptr<DataChannelInterface> caller_dc(
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000385 caller_->CreateDataChannel("data", init));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000386 rtc::scoped_refptr<DataChannelInterface> callee_dc(
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000387 callee_->CreateDataChannel("data", init));
388
389 Negotiate();
390 WaitForConnection();
391
392 WaitForDataChannelsToOpen(caller_dc, callee_signaled_data_channels_, 0);
393 WaitForDataChannelsToOpen(callee_dc, caller_signaled_data_channels_, 0);
394
395 TestDataChannelSendAndReceive(caller_dc, callee_signaled_data_channels_[0]);
396 TestDataChannelSendAndReceive(callee_dc, caller_signaled_data_channels_[0]);
397
398 CloseDataChannels(caller_dc, callee_signaled_data_channels_, 0);
399 CloseDataChannels(callee_dc, caller_signaled_data_channels_, 0);
400}
401
402// Verifies that a DataChannel created after the negotiation can transition to
403// "OPEN" and transfer data.
Taylor Brandstetterbf2f5692016-06-29 11:22:47 -0700404TEST_F(PeerConnectionEndToEndTest, CreateDataChannelAfterNegotiate) {
kwiberg9e5b11e2017-04-19 03:47:57 -0700405 CreatePcs(nullptr, webrtc::CreateBuiltinAudioEncoderFactory(),
kwiberg7a12b5a2017-04-27 03:55:57 -0700406 webrtc::MockAudioDecoderFactory::CreateEmptyFactory());
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000407
408 webrtc::DataChannelInit init;
409
410 // This DataChannel is for creating the data content in the negotiation.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000411 rtc::scoped_refptr<DataChannelInterface> dummy(
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000412 caller_->CreateDataChannel("data", init));
413 Negotiate();
414 WaitForConnection();
415
Taylor Brandstetterbf2f5692016-06-29 11:22:47 -0700416 // Wait for the data channel created pre-negotiation to be opened.
417 WaitForDataChannelsToOpen(dummy, callee_signaled_data_channels_, 0);
418
419 // Create new DataChannels after the negotiation and verify their states.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000420 rtc::scoped_refptr<DataChannelInterface> caller_dc(
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000421 caller_->CreateDataChannel("hello", init));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000422 rtc::scoped_refptr<DataChannelInterface> callee_dc(
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000423 callee_->CreateDataChannel("hello", init));
424
425 WaitForDataChannelsToOpen(caller_dc, callee_signaled_data_channels_, 1);
426 WaitForDataChannelsToOpen(callee_dc, caller_signaled_data_channels_, 0);
427
428 TestDataChannelSendAndReceive(caller_dc, callee_signaled_data_channels_[1]);
429 TestDataChannelSendAndReceive(callee_dc, caller_signaled_data_channels_[0]);
430
431 CloseDataChannels(caller_dc, callee_signaled_data_channels_, 1);
432 CloseDataChannels(callee_dc, caller_signaled_data_channels_, 0);
433}
434
435// Verifies that DataChannel IDs are even/odd based on the DTLS roles.
436TEST_F(PeerConnectionEndToEndTest, DataChannelIdAssignment) {
kwiberg9e5b11e2017-04-19 03:47:57 -0700437 CreatePcs(nullptr, webrtc::CreateBuiltinAudioEncoderFactory(),
kwiberg7a12b5a2017-04-27 03:55:57 -0700438 webrtc::MockAudioDecoderFactory::CreateEmptyFactory());
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000439
440 webrtc::DataChannelInit init;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000441 rtc::scoped_refptr<DataChannelInterface> caller_dc_1(
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000442 caller_->CreateDataChannel("data", init));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000443 rtc::scoped_refptr<DataChannelInterface> callee_dc_1(
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000444 callee_->CreateDataChannel("data", init));
445
446 Negotiate();
447 WaitForConnection();
448
449 EXPECT_EQ(1U, caller_dc_1->id() % 2);
450 EXPECT_EQ(0U, callee_dc_1->id() % 2);
451
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000452 rtc::scoped_refptr<DataChannelInterface> caller_dc_2(
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000453 caller_->CreateDataChannel("data", init));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000454 rtc::scoped_refptr<DataChannelInterface> callee_dc_2(
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000455 callee_->CreateDataChannel("data", init));
456
457 EXPECT_EQ(1U, caller_dc_2->id() % 2);
458 EXPECT_EQ(0U, callee_dc_2->id() % 2);
459}
460
461// Verifies that the message is received by the right remote DataChannel when
462// there are multiple DataChannels.
463TEST_F(PeerConnectionEndToEndTest,
464 MessageTransferBetweenTwoPairsOfDataChannels) {
kwiberg9e5b11e2017-04-19 03:47:57 -0700465 CreatePcs(nullptr, webrtc::CreateBuiltinAudioEncoderFactory(),
kwiberg7a12b5a2017-04-27 03:55:57 -0700466 webrtc::MockAudioDecoderFactory::CreateEmptyFactory());
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000467
468 webrtc::DataChannelInit init;
469
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000470 rtc::scoped_refptr<DataChannelInterface> caller_dc_1(
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000471 caller_->CreateDataChannel("data", init));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000472 rtc::scoped_refptr<DataChannelInterface> caller_dc_2(
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000473 caller_->CreateDataChannel("data", init));
474
475 Negotiate();
476 WaitForConnection();
477 WaitForDataChannelsToOpen(caller_dc_1, callee_signaled_data_channels_, 0);
478 WaitForDataChannelsToOpen(caller_dc_2, callee_signaled_data_channels_, 1);
479
kwibergd1fe2812016-04-27 06:47:29 -0700480 std::unique_ptr<webrtc::MockDataChannelObserver> dc_1_observer(
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000481 new webrtc::MockDataChannelObserver(callee_signaled_data_channels_[0]));
482
kwibergd1fe2812016-04-27 06:47:29 -0700483 std::unique_ptr<webrtc::MockDataChannelObserver> dc_2_observer(
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000484 new webrtc::MockDataChannelObserver(callee_signaled_data_channels_[1]));
485
486 const std::string message_1 = "hello 1";
487 const std::string message_2 = "hello 2";
488
489 caller_dc_1->Send(webrtc::DataBuffer(message_1));
490 EXPECT_EQ_WAIT(message_1, dc_1_observer->last_message(), kMaxWait);
491
492 caller_dc_2->Send(webrtc::DataBuffer(message_2));
493 EXPECT_EQ_WAIT(message_2, dc_2_observer->last_message(), kMaxWait);
494
495 EXPECT_EQ(1U, dc_1_observer->received_message_count());
496 EXPECT_EQ(1U, dc_2_observer->received_message_count());
497}
deadbeefab9b2d12015-10-14 11:33:11 -0700498
499// Verifies that a DataChannel added from an OPEN message functions after
500// a channel has been previously closed (webrtc issue 3778).
501// This previously failed because the new channel re-uses the ID of the closed
502// channel, and the closed channel was incorrectly still assigned to the id.
503// TODO(deadbeef): This is disabled because there's currently a race condition
504// caused by the fact that a data channel signals that it's closed before it
505// really is. Re-enable this test once that's fixed.
deadbeefe2213ce2016-11-03 16:01:57 -0700506// See: https://bugs.chromium.org/p/webrtc/issues/detail?id=4453
deadbeefab9b2d12015-10-14 11:33:11 -0700507TEST_F(PeerConnectionEndToEndTest,
508 DISABLED_DataChannelFromOpenWorksAfterClose) {
kwiberg9e5b11e2017-04-19 03:47:57 -0700509 CreatePcs(nullptr, webrtc::CreateBuiltinAudioEncoderFactory(),
kwiberg7a12b5a2017-04-27 03:55:57 -0700510 webrtc::MockAudioDecoderFactory::CreateEmptyFactory());
deadbeefab9b2d12015-10-14 11:33:11 -0700511
512 webrtc::DataChannelInit init;
513 rtc::scoped_refptr<DataChannelInterface> caller_dc(
514 caller_->CreateDataChannel("data", init));
515
516 Negotiate();
517 WaitForConnection();
518
519 WaitForDataChannelsToOpen(caller_dc, callee_signaled_data_channels_, 0);
520 CloseDataChannels(caller_dc, callee_signaled_data_channels_, 0);
521
522 // Create a new channel and ensure it works after closing the previous one.
523 caller_dc = caller_->CreateDataChannel("data2", init);
524
525 WaitForDataChannelsToOpen(caller_dc, callee_signaled_data_channels_, 1);
526 TestDataChannelSendAndReceive(caller_dc, callee_signaled_data_channels_[1]);
527
528 CloseDataChannels(caller_dc, callee_signaled_data_channels_, 1);
529}
deadbeefbd292462015-12-14 18:15:29 -0800530
531// This tests that if a data channel is closed remotely while not referenced
532// by the application (meaning only the PeerConnection contributes to its
533// reference count), no memory access violation will occur.
534// See: https://code.google.com/p/chromium/issues/detail?id=565048
535TEST_F(PeerConnectionEndToEndTest, CloseDataChannelRemotelyWhileNotReferenced) {
kwiberg9e5b11e2017-04-19 03:47:57 -0700536 CreatePcs(nullptr, webrtc::CreateBuiltinAudioEncoderFactory(),
kwiberg7a12b5a2017-04-27 03:55:57 -0700537 webrtc::MockAudioDecoderFactory::CreateEmptyFactory());
deadbeefbd292462015-12-14 18:15:29 -0800538
539 webrtc::DataChannelInit init;
540 rtc::scoped_refptr<DataChannelInterface> caller_dc(
541 caller_->CreateDataChannel("data", init));
542
543 Negotiate();
544 WaitForConnection();
545
546 WaitForDataChannelsToOpen(caller_dc, callee_signaled_data_channels_, 0);
547 // This removes the reference to the remote data channel that we hold.
548 callee_signaled_data_channels_.clear();
549 caller_dc->Close();
550 EXPECT_EQ_WAIT(DataChannelInterface::kClosed, caller_dc->state(), kMaxWait);
551
552 // Wait for a bit longer so the remote data channel will receive the
553 // close message and be destroyed.
554 rtc::Thread::Current()->ProcessMessages(100);
555}
deadbeef40610e22016-12-22 10:53:38 -0800556#endif // HAVE_SCTP