blob: b15126416af6f5414b4dce2f42138b58d041f5ad [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020013#include "api/audio_codecs/builtin_audio_decoder_factory.h"
14#include "api/audio_codecs/builtin_audio_encoder_factory.h"
15#include "rtc_base/gunit.h"
16#include "rtc_base/logging.h"
17#include "rtc_base/ptr_util.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "rtc_base/stringencode.h"
19#include "rtc_base/stringutils.h"
Patrik Höglund563934e2017-09-15 09:04:28 +020020
ossu7bb87ee2017-01-23 04:56:25 -080021#ifdef WEBRTC_ANDROID
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#include "pc/test/androidtestinitializer.h"
ossu7bb87ee2017-01-23 04:56:25 -080023#endif
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020024#include "pc/test/peerconnectiontestwrapper.h"
ossu7bb87ee2017-01-23 04:56:25 -080025// Notice that mockpeerconnectionobservers.h must be included after the above!
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020026#include "pc/test/mockpeerconnectionobservers.h"
27#include "test/mock_audio_decoder.h"
28#include "test/mock_audio_decoder_factory.h"
kwiberg9e5b11e2017-04-19 03:47:57 -070029
30using testing::AtLeast;
31using testing::Invoke;
32using testing::StrictMock;
33using testing::_;
wu@webrtc.org364f2042013-11-20 21:49:41 +000034
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +000035using webrtc::DataChannelInterface;
wu@webrtc.org364f2042013-11-20 21:49:41 +000036using webrtc::FakeConstraints;
37using webrtc::MediaConstraintsInterface;
38using webrtc::MediaStreamInterface;
39using webrtc::PeerConnectionInterface;
40
41namespace {
42
Honghai Zhang82d78622016-05-06 11:29:15 -070043const int kMaxWait = 10000;
wu@webrtc.org364f2042013-11-20 21:49:41 +000044
wu@webrtc.org364f2042013-11-20 21:49:41 +000045} // namespace
46
47class PeerConnectionEndToEndTest
48 : public sigslot::has_slots<>,
49 public testing::Test {
50 public:
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000051 typedef std::vector<rtc::scoped_refptr<DataChannelInterface> >
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +000052 DataChannelList;
53
perkj57db6522016-04-08 08:16:33 -070054 PeerConnectionEndToEndTest() {
tommie7251592017-07-14 14:44:46 -070055 network_thread_ = rtc::Thread::CreateWithSocketServer();
56 worker_thread_ = rtc::Thread::Create();
57 RTC_CHECK(network_thread_->Start());
58 RTC_CHECK(worker_thread_->Start());
perkj57db6522016-04-08 08:16:33 -070059 caller_ = new rtc::RefCountedObject<PeerConnectionTestWrapper>(
tommie7251592017-07-14 14:44:46 -070060 "caller", network_thread_.get(), worker_thread_.get());
perkj57db6522016-04-08 08:16:33 -070061 callee_ = new rtc::RefCountedObject<PeerConnectionTestWrapper>(
tommie7251592017-07-14 14:44:46 -070062 "callee", network_thread_.get(), worker_thread_.get());
zhihuang9763d562016-08-05 11:14:50 -070063 webrtc::PeerConnectionInterface::IceServer ice_server;
64 ice_server.uri = "stun:stun.l.google.com:19302";
65 config_.servers.push_back(ice_server);
66
phoglund37ebcf02016-01-08 05:04:57 -080067#ifdef WEBRTC_ANDROID
68 webrtc::InitializeAndroidObjects();
69#endif
wu@webrtc.org364f2042013-11-20 21:49:41 +000070 }
71
kwiberg9e5b11e2017-04-19 03:47:57 -070072 void CreatePcs(
73 const MediaConstraintsInterface* pc_constraints,
74 rtc::scoped_refptr<webrtc::AudioEncoderFactory> audio_encoder_factory,
75 rtc::scoped_refptr<webrtc::AudioDecoderFactory> audio_decoder_factory) {
76 EXPECT_TRUE(caller_->CreatePc(
77 pc_constraints, config_, audio_encoder_factory, audio_decoder_factory));
78 EXPECT_TRUE(callee_->CreatePc(
79 pc_constraints, config_, audio_encoder_factory, audio_decoder_factory));
wu@webrtc.org364f2042013-11-20 21:49:41 +000080 PeerConnectionTestWrapper::Connect(caller_.get(), callee_.get());
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +000081
82 caller_->SignalOnDataChannel.connect(
83 this, &PeerConnectionEndToEndTest::OnCallerAddedDataChanel);
84 callee_->SignalOnDataChannel.connect(
85 this, &PeerConnectionEndToEndTest::OnCalleeAddedDataChannel);
wu@webrtc.org364f2042013-11-20 21:49:41 +000086 }
87
88 void GetAndAddUserMedia() {
89 FakeConstraints audio_constraints;
90 FakeConstraints video_constraints;
91 GetAndAddUserMedia(true, audio_constraints, true, video_constraints);
92 }
93
94 void GetAndAddUserMedia(bool audio, FakeConstraints audio_constraints,
95 bool video, FakeConstraints video_constraints) {
96 caller_->GetAndAddUserMedia(audio, audio_constraints,
97 video, video_constraints);
98 callee_->GetAndAddUserMedia(audio, audio_constraints,
99 video, video_constraints);
100 }
101
102 void Negotiate() {
103 caller_->CreateOffer(NULL);
104 }
105
106 void WaitForCallEstablished() {
107 caller_->WaitForCallEstablished();
108 callee_->WaitForCallEstablished();
109 }
110
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000111 void WaitForConnection() {
112 caller_->WaitForConnection();
113 callee_->WaitForConnection();
114 }
115
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000116 void OnCallerAddedDataChanel(DataChannelInterface* dc) {
117 caller_signaled_data_channels_.push_back(dc);
118 }
119
120 void OnCalleeAddedDataChannel(DataChannelInterface* dc) {
121 callee_signaled_data_channels_.push_back(dc);
122 }
123
124 // Tests that |dc1| and |dc2| can send to and receive from each other.
125 void TestDataChannelSendAndReceive(
126 DataChannelInterface* dc1, DataChannelInterface* dc2) {
kwibergd1fe2812016-04-27 06:47:29 -0700127 std::unique_ptr<webrtc::MockDataChannelObserver> dc1_observer(
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000128 new webrtc::MockDataChannelObserver(dc1));
129
kwibergd1fe2812016-04-27 06:47:29 -0700130 std::unique_ptr<webrtc::MockDataChannelObserver> dc2_observer(
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000131 new webrtc::MockDataChannelObserver(dc2));
132
133 static const std::string kDummyData = "abcdefg";
134 webrtc::DataBuffer buffer(kDummyData);
135 EXPECT_TRUE(dc1->Send(buffer));
136 EXPECT_EQ_WAIT(kDummyData, dc2_observer->last_message(), kMaxWait);
137
138 EXPECT_TRUE(dc2->Send(buffer));
139 EXPECT_EQ_WAIT(kDummyData, dc1_observer->last_message(), kMaxWait);
140
141 EXPECT_EQ(1U, dc1_observer->received_message_count());
142 EXPECT_EQ(1U, dc2_observer->received_message_count());
143 }
144
145 void WaitForDataChannelsToOpen(DataChannelInterface* local_dc,
146 const DataChannelList& remote_dc_list,
147 size_t remote_dc_index) {
148 EXPECT_EQ_WAIT(DataChannelInterface::kOpen, local_dc->state(), kMaxWait);
149
150 EXPECT_TRUE_WAIT(remote_dc_list.size() > remote_dc_index, kMaxWait);
151 EXPECT_EQ_WAIT(DataChannelInterface::kOpen,
152 remote_dc_list[remote_dc_index]->state(),
153 kMaxWait);
154 EXPECT_EQ(local_dc->id(), remote_dc_list[remote_dc_index]->id());
155 }
156
157 void CloseDataChannels(DataChannelInterface* local_dc,
158 const DataChannelList& remote_dc_list,
159 size_t remote_dc_index) {
160 local_dc->Close();
161 EXPECT_EQ_WAIT(DataChannelInterface::kClosed, local_dc->state(), kMaxWait);
162 EXPECT_EQ_WAIT(DataChannelInterface::kClosed,
163 remote_dc_list[remote_dc_index]->state(),
164 kMaxWait);
165 }
166
wu@webrtc.org364f2042013-11-20 21:49:41 +0000167 protected:
tommie7251592017-07-14 14:44:46 -0700168 std::unique_ptr<rtc::Thread> network_thread_;
169 std::unique_ptr<rtc::Thread> worker_thread_;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000170 rtc::scoped_refptr<PeerConnectionTestWrapper> caller_;
171 rtc::scoped_refptr<PeerConnectionTestWrapper> callee_;
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000172 DataChannelList caller_signaled_data_channels_;
173 DataChannelList callee_signaled_data_channels_;
zhihuang9763d562016-08-05 11:14:50 -0700174 webrtc::PeerConnectionInterface::RTCConfiguration config_;
wu@webrtc.org364f2042013-11-20 21:49:41 +0000175};
176
kwiberg9e5b11e2017-04-19 03:47:57 -0700177std::unique_ptr<webrtc::AudioDecoder> CreateForwardingMockDecoder(
178 std::unique_ptr<webrtc::AudioDecoder> real_decoder) {
179 class ForwardingMockDecoder : public StrictMock<webrtc::MockAudioDecoder> {
180 public:
181 ForwardingMockDecoder(std::unique_ptr<AudioDecoder> decoder)
182 : decoder_(std::move(decoder)) {}
183
184 private:
185 std::unique_ptr<AudioDecoder> decoder_;
186 };
187
188 const auto dec = real_decoder.get(); // For lambda capturing.
189 auto mock_decoder =
190 rtc::MakeUnique<ForwardingMockDecoder>(std::move(real_decoder));
191 EXPECT_CALL(*mock_decoder, Channels())
192 .Times(AtLeast(1))
193 .WillRepeatedly(Invoke([dec] { return dec->Channels(); }));
194 EXPECT_CALL(*mock_decoder, DecodeInternal(_, _, _, _, _))
195 .Times(AtLeast(1))
196 .WillRepeatedly(
197 Invoke([dec](const uint8_t* encoded, size_t encoded_len,
198 int sample_rate_hz, int16_t* decoded,
199 webrtc::AudioDecoder::SpeechType* speech_type) {
200 return dec->Decode(encoded, encoded_len, sample_rate_hz,
201 std::numeric_limits<size_t>::max(), decoded,
202 speech_type);
203 }));
204 EXPECT_CALL(*mock_decoder, Die());
205 EXPECT_CALL(*mock_decoder, HasDecodePlc()).WillRepeatedly(Invoke([dec] {
206 return dec->HasDecodePlc();
207 }));
208 EXPECT_CALL(*mock_decoder, IncomingPacket(_, _, _, _, _))
209 .Times(AtLeast(1))
210 .WillRepeatedly(Invoke([dec](const uint8_t* payload, size_t payload_len,
211 uint16_t rtp_sequence_number,
212 uint32_t rtp_timestamp,
213 uint32_t arrival_timestamp) {
214 return dec->IncomingPacket(payload, payload_len, rtp_sequence_number,
215 rtp_timestamp, arrival_timestamp);
216 }));
217 EXPECT_CALL(*mock_decoder, PacketDuration(_, _))
218 .Times(AtLeast(1))
219 .WillRepeatedly(Invoke([dec](const uint8_t* encoded, size_t encoded_len) {
220 return dec->PacketDuration(encoded, encoded_len);
221 }));
222 EXPECT_CALL(*mock_decoder, SampleRateHz())
223 .Times(AtLeast(1))
224 .WillRepeatedly(Invoke([dec] { return dec->SampleRateHz(); }));
225
226 return std::move(mock_decoder);
227}
228
229rtc::scoped_refptr<webrtc::AudioDecoderFactory>
230CreateForwardingMockDecoderFactory(
231 webrtc::AudioDecoderFactory* real_decoder_factory) {
232 rtc::scoped_refptr<webrtc::MockAudioDecoderFactory> mock_decoder_factory =
233 new rtc::RefCountedObject<StrictMock<webrtc::MockAudioDecoderFactory>>;
234 EXPECT_CALL(*mock_decoder_factory, GetSupportedDecoders())
235 .Times(AtLeast(1))
236 .WillRepeatedly(Invoke([real_decoder_factory] {
237 return real_decoder_factory->GetSupportedDecoders();
238 }));
239 EXPECT_CALL(*mock_decoder_factory, IsSupportedDecoder(_))
240 .Times(AtLeast(1))
241 .WillRepeatedly(
242 Invoke([real_decoder_factory](const webrtc::SdpAudioFormat& format) {
243 return real_decoder_factory->IsSupportedDecoder(format);
244 }));
245 EXPECT_CALL(*mock_decoder_factory, MakeAudioDecoderMock(_, _))
246 .Times(AtLeast(2))
247 .WillRepeatedly(
248 Invoke([real_decoder_factory](
249 const webrtc::SdpAudioFormat& format,
250 std::unique_ptr<webrtc::AudioDecoder>* return_value) {
251 auto real_decoder = real_decoder_factory->MakeAudioDecoder(format);
252 *return_value =
253 real_decoder
254 ? CreateForwardingMockDecoder(std::move(real_decoder))
255 : nullptr;
256 }));
257 return mock_decoder_factory;
258}
259
kjellander@webrtc.org70c0e292015-11-30 21:45:35 +0100260// Disabled for TSan v2, see
261// https://bugs.chromium.org/p/webrtc/issues/detail?id=4719 for details.
kjellander@webrtc.org3c28d0d2015-12-02 22:53:26 +0100262// Disabled for Mac, see
263// https://bugs.chromium.org/p/webrtc/issues/detail?id=5231 for details.
264#if !defined(THREAD_SANITIZER) && !defined(WEBRTC_MAC)
deadbeefee8c6d32015-08-13 14:27:18 -0700265TEST_F(PeerConnectionEndToEndTest, Call) {
kwiberg9e5b11e2017-04-19 03:47:57 -0700266 rtc::scoped_refptr<webrtc::AudioDecoderFactory> real_decoder_factory =
267 webrtc::CreateBuiltinAudioDecoderFactory();
268 CreatePcs(nullptr, webrtc::CreateBuiltinAudioEncoderFactory(),
269 CreateForwardingMockDecoderFactory(real_decoder_factory.get()));
wu@webrtc.org364f2042013-11-20 21:49:41 +0000270 GetAndAddUserMedia();
271 Negotiate();
272 WaitForCallEstablished();
273}
kjellander@webrtc.org3c28d0d2015-12-02 22:53:26 +0100274#endif // if !defined(THREAD_SANITIZER) && !defined(WEBRTC_MAC)
wu@webrtc.org364f2042013-11-20 21:49:41 +0000275
philipel7703b272016-11-28 16:23:12 +0100276#if !defined(ADDRESS_SANITIZER)
deadbeefc9be0072015-12-14 18:27:57 -0800277TEST_F(PeerConnectionEndToEndTest, CallWithLegacySdp) {
wu@webrtc.org364f2042013-11-20 21:49:41 +0000278 FakeConstraints pc_constraints;
279 pc_constraints.AddMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
280 false);
kwiberg9e5b11e2017-04-19 03:47:57 -0700281 CreatePcs(&pc_constraints, webrtc::CreateBuiltinAudioEncoderFactory(),
282 webrtc::CreateBuiltinAudioDecoderFactory());
wu@webrtc.org364f2042013-11-20 21:49:41 +0000283 GetAndAddUserMedia();
284 Negotiate();
285 WaitForCallEstablished();
286}
philipel7703b272016-11-28 16:23:12 +0100287#endif // !defined(ADDRESS_SANITIZER)
wu@webrtc.orgb43202d2013-11-22 19:14:25 +0000288
deadbeef40610e22016-12-22 10:53:38 -0800289#ifdef HAVE_SCTP
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000290// Verifies that a DataChannel created before the negotiation can transition to
291// "OPEN" and transfer data.
292TEST_F(PeerConnectionEndToEndTest, CreateDataChannelBeforeNegotiate) {
kwiberg9e5b11e2017-04-19 03:47:57 -0700293 CreatePcs(nullptr, webrtc::CreateBuiltinAudioEncoderFactory(),
kwiberg7a12b5a2017-04-27 03:55:57 -0700294 webrtc::MockAudioDecoderFactory::CreateEmptyFactory());
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000295
296 webrtc::DataChannelInit init;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000297 rtc::scoped_refptr<DataChannelInterface> caller_dc(
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000298 caller_->CreateDataChannel("data", init));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000299 rtc::scoped_refptr<DataChannelInterface> callee_dc(
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000300 callee_->CreateDataChannel("data", init));
301
302 Negotiate();
303 WaitForConnection();
304
305 WaitForDataChannelsToOpen(caller_dc, callee_signaled_data_channels_, 0);
306 WaitForDataChannelsToOpen(callee_dc, caller_signaled_data_channels_, 0);
307
308 TestDataChannelSendAndReceive(caller_dc, callee_signaled_data_channels_[0]);
309 TestDataChannelSendAndReceive(callee_dc, caller_signaled_data_channels_[0]);
310
311 CloseDataChannels(caller_dc, callee_signaled_data_channels_, 0);
312 CloseDataChannels(callee_dc, caller_signaled_data_channels_, 0);
313}
314
315// Verifies that a DataChannel created after the negotiation can transition to
316// "OPEN" and transfer data.
Taylor Brandstetterbf2f5692016-06-29 11:22:47 -0700317TEST_F(PeerConnectionEndToEndTest, CreateDataChannelAfterNegotiate) {
kwiberg9e5b11e2017-04-19 03:47:57 -0700318 CreatePcs(nullptr, webrtc::CreateBuiltinAudioEncoderFactory(),
kwiberg7a12b5a2017-04-27 03:55:57 -0700319 webrtc::MockAudioDecoderFactory::CreateEmptyFactory());
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000320
321 webrtc::DataChannelInit init;
322
323 // This DataChannel is for creating the data content in the negotiation.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000324 rtc::scoped_refptr<DataChannelInterface> dummy(
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000325 caller_->CreateDataChannel("data", init));
326 Negotiate();
327 WaitForConnection();
328
Taylor Brandstetterbf2f5692016-06-29 11:22:47 -0700329 // Wait for the data channel created pre-negotiation to be opened.
330 WaitForDataChannelsToOpen(dummy, callee_signaled_data_channels_, 0);
331
332 // Create new DataChannels after the negotiation and verify their states.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000333 rtc::scoped_refptr<DataChannelInterface> caller_dc(
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000334 caller_->CreateDataChannel("hello", init));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000335 rtc::scoped_refptr<DataChannelInterface> callee_dc(
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000336 callee_->CreateDataChannel("hello", init));
337
338 WaitForDataChannelsToOpen(caller_dc, callee_signaled_data_channels_, 1);
339 WaitForDataChannelsToOpen(callee_dc, caller_signaled_data_channels_, 0);
340
341 TestDataChannelSendAndReceive(caller_dc, callee_signaled_data_channels_[1]);
342 TestDataChannelSendAndReceive(callee_dc, caller_signaled_data_channels_[0]);
343
344 CloseDataChannels(caller_dc, callee_signaled_data_channels_, 1);
345 CloseDataChannels(callee_dc, caller_signaled_data_channels_, 0);
346}
347
348// Verifies that DataChannel IDs are even/odd based on the DTLS roles.
349TEST_F(PeerConnectionEndToEndTest, DataChannelIdAssignment) {
kwiberg9e5b11e2017-04-19 03:47:57 -0700350 CreatePcs(nullptr, webrtc::CreateBuiltinAudioEncoderFactory(),
kwiberg7a12b5a2017-04-27 03:55:57 -0700351 webrtc::MockAudioDecoderFactory::CreateEmptyFactory());
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000352
353 webrtc::DataChannelInit init;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000354 rtc::scoped_refptr<DataChannelInterface> caller_dc_1(
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000355 caller_->CreateDataChannel("data", init));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000356 rtc::scoped_refptr<DataChannelInterface> callee_dc_1(
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000357 callee_->CreateDataChannel("data", init));
358
359 Negotiate();
360 WaitForConnection();
361
362 EXPECT_EQ(1U, caller_dc_1->id() % 2);
363 EXPECT_EQ(0U, callee_dc_1->id() % 2);
364
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000365 rtc::scoped_refptr<DataChannelInterface> caller_dc_2(
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000366 caller_->CreateDataChannel("data", init));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000367 rtc::scoped_refptr<DataChannelInterface> callee_dc_2(
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000368 callee_->CreateDataChannel("data", init));
369
370 EXPECT_EQ(1U, caller_dc_2->id() % 2);
371 EXPECT_EQ(0U, callee_dc_2->id() % 2);
372}
373
374// Verifies that the message is received by the right remote DataChannel when
375// there are multiple DataChannels.
376TEST_F(PeerConnectionEndToEndTest,
377 MessageTransferBetweenTwoPairsOfDataChannels) {
kwiberg9e5b11e2017-04-19 03:47:57 -0700378 CreatePcs(nullptr, webrtc::CreateBuiltinAudioEncoderFactory(),
kwiberg7a12b5a2017-04-27 03:55:57 -0700379 webrtc::MockAudioDecoderFactory::CreateEmptyFactory());
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000380
381 webrtc::DataChannelInit init;
382
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000383 rtc::scoped_refptr<DataChannelInterface> caller_dc_1(
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000384 caller_->CreateDataChannel("data", init));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000385 rtc::scoped_refptr<DataChannelInterface> caller_dc_2(
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000386 caller_->CreateDataChannel("data", init));
387
388 Negotiate();
389 WaitForConnection();
390 WaitForDataChannelsToOpen(caller_dc_1, callee_signaled_data_channels_, 0);
391 WaitForDataChannelsToOpen(caller_dc_2, callee_signaled_data_channels_, 1);
392
kwibergd1fe2812016-04-27 06:47:29 -0700393 std::unique_ptr<webrtc::MockDataChannelObserver> dc_1_observer(
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000394 new webrtc::MockDataChannelObserver(callee_signaled_data_channels_[0]));
395
kwibergd1fe2812016-04-27 06:47:29 -0700396 std::unique_ptr<webrtc::MockDataChannelObserver> dc_2_observer(
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000397 new webrtc::MockDataChannelObserver(callee_signaled_data_channels_[1]));
398
399 const std::string message_1 = "hello 1";
400 const std::string message_2 = "hello 2";
401
402 caller_dc_1->Send(webrtc::DataBuffer(message_1));
403 EXPECT_EQ_WAIT(message_1, dc_1_observer->last_message(), kMaxWait);
404
405 caller_dc_2->Send(webrtc::DataBuffer(message_2));
406 EXPECT_EQ_WAIT(message_2, dc_2_observer->last_message(), kMaxWait);
407
408 EXPECT_EQ(1U, dc_1_observer->received_message_count());
409 EXPECT_EQ(1U, dc_2_observer->received_message_count());
410}
deadbeef40610e22016-12-22 10:53:38 -0800411#endif // HAVE_SCTP
deadbeefab9b2d12015-10-14 11:33:11 -0700412
zhihuang9763d562016-08-05 11:14:50 -0700413#ifdef HAVE_QUIC
414// Test that QUIC data channels can be used and that messages go to the correct
415// remote data channel when both peers want to use QUIC. It is assumed that the
416// application has externally negotiated the data channel parameters.
417TEST_F(PeerConnectionEndToEndTest, MessageTransferBetweenQuicDataChannels) {
418 config_.enable_quic = true;
419 CreatePcs();
420
421 webrtc::DataChannelInit init_1;
422 init_1.id = 0;
423 init_1.ordered = false;
424 init_1.reliable = true;
425
426 webrtc::DataChannelInit init_2;
427 init_2.id = 1;
428 init_2.ordered = false;
429 init_2.reliable = true;
430
431 rtc::scoped_refptr<DataChannelInterface> caller_dc_1(
432 caller_->CreateDataChannel("data", init_1));
433 ASSERT_NE(nullptr, caller_dc_1);
434 rtc::scoped_refptr<DataChannelInterface> caller_dc_2(
435 caller_->CreateDataChannel("data", init_2));
436 ASSERT_NE(nullptr, caller_dc_2);
437 rtc::scoped_refptr<DataChannelInterface> callee_dc_1(
438 callee_->CreateDataChannel("data", init_1));
439 ASSERT_NE(nullptr, callee_dc_1);
440 rtc::scoped_refptr<DataChannelInterface> callee_dc_2(
441 callee_->CreateDataChannel("data", init_2));
442 ASSERT_NE(nullptr, callee_dc_2);
443
444 Negotiate();
445 WaitForConnection();
446 EXPECT_TRUE_WAIT(caller_dc_1->state() == webrtc::DataChannelInterface::kOpen,
447 kMaxWait);
448 EXPECT_TRUE_WAIT(callee_dc_1->state() == webrtc::DataChannelInterface::kOpen,
449 kMaxWait);
450 EXPECT_TRUE_WAIT(caller_dc_2->state() == webrtc::DataChannelInterface::kOpen,
451 kMaxWait);
452 EXPECT_TRUE_WAIT(callee_dc_2->state() == webrtc::DataChannelInterface::kOpen,
453 kMaxWait);
454
455 std::unique_ptr<webrtc::MockDataChannelObserver> dc_1_observer(
456 new webrtc::MockDataChannelObserver(callee_dc_1.get()));
457
458 std::unique_ptr<webrtc::MockDataChannelObserver> dc_2_observer(
459 new webrtc::MockDataChannelObserver(callee_dc_2.get()));
460
461 const std::string message_1 = "hello 1";
462 const std::string message_2 = "hello 2";
463
464 // Send data from caller to callee.
465 caller_dc_1->Send(webrtc::DataBuffer(message_1));
466 EXPECT_EQ_WAIT(message_1, dc_1_observer->last_message(), kMaxWait);
467
468 caller_dc_2->Send(webrtc::DataBuffer(message_2));
469 EXPECT_EQ_WAIT(message_2, dc_2_observer->last_message(), kMaxWait);
470
471 EXPECT_EQ(1U, dc_1_observer->received_message_count());
472 EXPECT_EQ(1U, dc_2_observer->received_message_count());
473
474 // Send data from callee to caller.
475 dc_1_observer.reset(new webrtc::MockDataChannelObserver(caller_dc_1.get()));
476 dc_2_observer.reset(new webrtc::MockDataChannelObserver(caller_dc_2.get()));
477
478 callee_dc_1->Send(webrtc::DataBuffer(message_1));
479 EXPECT_EQ_WAIT(message_1, dc_1_observer->last_message(), kMaxWait);
480
481 callee_dc_2->Send(webrtc::DataBuffer(message_2));
482 EXPECT_EQ_WAIT(message_2, dc_2_observer->last_message(), kMaxWait);
483
484 EXPECT_EQ(1U, dc_1_observer->received_message_count());
485 EXPECT_EQ(1U, dc_2_observer->received_message_count());
486}
487#endif // HAVE_QUIC
488
deadbeef40610e22016-12-22 10:53:38 -0800489#ifdef HAVE_SCTP
deadbeefab9b2d12015-10-14 11:33:11 -0700490// Verifies that a DataChannel added from an OPEN message functions after
491// a channel has been previously closed (webrtc issue 3778).
492// This previously failed because the new channel re-uses the ID of the closed
493// channel, and the closed channel was incorrectly still assigned to the id.
494// TODO(deadbeef): This is disabled because there's currently a race condition
495// caused by the fact that a data channel signals that it's closed before it
496// really is. Re-enable this test once that's fixed.
deadbeefe2213ce2016-11-03 16:01:57 -0700497// See: https://bugs.chromium.org/p/webrtc/issues/detail?id=4453
deadbeefab9b2d12015-10-14 11:33:11 -0700498TEST_F(PeerConnectionEndToEndTest,
499 DISABLED_DataChannelFromOpenWorksAfterClose) {
kwiberg9e5b11e2017-04-19 03:47:57 -0700500 CreatePcs(nullptr, webrtc::CreateBuiltinAudioEncoderFactory(),
kwiberg7a12b5a2017-04-27 03:55:57 -0700501 webrtc::MockAudioDecoderFactory::CreateEmptyFactory());
deadbeefab9b2d12015-10-14 11:33:11 -0700502
503 webrtc::DataChannelInit init;
504 rtc::scoped_refptr<DataChannelInterface> caller_dc(
505 caller_->CreateDataChannel("data", init));
506
507 Negotiate();
508 WaitForConnection();
509
510 WaitForDataChannelsToOpen(caller_dc, callee_signaled_data_channels_, 0);
511 CloseDataChannels(caller_dc, callee_signaled_data_channels_, 0);
512
513 // Create a new channel and ensure it works after closing the previous one.
514 caller_dc = caller_->CreateDataChannel("data2", init);
515
516 WaitForDataChannelsToOpen(caller_dc, callee_signaled_data_channels_, 1);
517 TestDataChannelSendAndReceive(caller_dc, callee_signaled_data_channels_[1]);
518
519 CloseDataChannels(caller_dc, callee_signaled_data_channels_, 1);
520}
deadbeefbd292462015-12-14 18:15:29 -0800521
522// This tests that if a data channel is closed remotely while not referenced
523// by the application (meaning only the PeerConnection contributes to its
524// reference count), no memory access violation will occur.
525// See: https://code.google.com/p/chromium/issues/detail?id=565048
526TEST_F(PeerConnectionEndToEndTest, CloseDataChannelRemotelyWhileNotReferenced) {
kwiberg9e5b11e2017-04-19 03:47:57 -0700527 CreatePcs(nullptr, webrtc::CreateBuiltinAudioEncoderFactory(),
kwiberg7a12b5a2017-04-27 03:55:57 -0700528 webrtc::MockAudioDecoderFactory::CreateEmptyFactory());
deadbeefbd292462015-12-14 18:15:29 -0800529
530 webrtc::DataChannelInit init;
531 rtc::scoped_refptr<DataChannelInterface> caller_dc(
532 caller_->CreateDataChannel("data", init));
533
534 Negotiate();
535 WaitForConnection();
536
537 WaitForDataChannelsToOpen(caller_dc, callee_signaled_data_channels_, 0);
538 // This removes the reference to the remote data channel that we hold.
539 callee_signaled_data_channels_.clear();
540 caller_dc->Close();
541 EXPECT_EQ_WAIT(DataChannelInterface::kClosed, caller_dc->state(), kMaxWait);
542
543 // Wait for a bit longer so the remote data channel will receive the
544 // close message and be destroyed.
545 rtc::Thread::Current()->ProcessMessages(100);
546}
deadbeef40610e22016-12-22 10:53:38 -0800547#endif // HAVE_SCTP