blob: f2a9641c11714cef3faa89b6e6e5db9d6e893bf0 [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
kwiberg9e5b11e2017-04-19 03:47:57 -070013#include "webrtc/api/audio_codecs/builtin_audio_decoder_factory.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000014#include "webrtc/base/gunit.h"
15#include "webrtc/base/logging.h"
kwiberg9e5b11e2017-04-19 03:47:57 -070016#include "webrtc/base/ptr_util.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000017#include "webrtc/base/ssladapter.h"
18#include "webrtc/base/sslstreamadapter.h"
19#include "webrtc/base/stringencode.h"
20#include "webrtc/base/stringutils.h"
kwiberg9e5b11e2017-04-19 03:47:57 -070021#include "webrtc/base/thread.h"
ossu7bb87ee2017-01-23 04:56:25 -080022#ifdef WEBRTC_ANDROID
23#include "webrtc/pc/test/androidtestinitializer.h"
24#endif
25#include "webrtc/pc/test/peerconnectiontestwrapper.h"
26// Notice that mockpeerconnectionobservers.h must be included after the above!
27#include "webrtc/pc/test/mockpeerconnectionobservers.h"
kwiberg9e5b11e2017-04-19 03:47:57 -070028#include "webrtc/test/mock_audio_decoder.h"
29#include "webrtc/test/mock_audio_decoder_factory.h"
30
31using testing::AtLeast;
32using testing::Invoke;
33using testing::StrictMock;
34using testing::_;
wu@webrtc.org364f2042013-11-20 21:49:41 +000035
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +000036using webrtc::DataChannelInterface;
wu@webrtc.org364f2042013-11-20 21:49:41 +000037using webrtc::FakeConstraints;
38using webrtc::MediaConstraintsInterface;
39using webrtc::MediaStreamInterface;
40using webrtc::PeerConnectionInterface;
41
42namespace {
43
Honghai Zhang82d78622016-05-06 11:29:15 -070044const int kMaxWait = 10000;
wu@webrtc.org364f2042013-11-20 21:49:41 +000045
wu@webrtc.org364f2042013-11-20 21:49:41 +000046} // namespace
47
48class PeerConnectionEndToEndTest
49 : public sigslot::has_slots<>,
50 public testing::Test {
51 public:
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000052 typedef std::vector<rtc::scoped_refptr<DataChannelInterface> >
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +000053 DataChannelList;
54
perkj57db6522016-04-08 08:16:33 -070055 PeerConnectionEndToEndTest() {
danilchape9021a32016-05-17 01:52:02 -070056 RTC_CHECK(network_thread_.Start());
perkj57db6522016-04-08 08:16:33 -070057 RTC_CHECK(worker_thread_.Start());
58 caller_ = new rtc::RefCountedObject<PeerConnectionTestWrapper>(
danilchape9021a32016-05-17 01:52:02 -070059 "caller", &network_thread_, &worker_thread_);
perkj57db6522016-04-08 08:16:33 -070060 callee_ = new rtc::RefCountedObject<PeerConnectionTestWrapper>(
danilchape9021a32016-05-17 01:52:02 -070061 "callee", &network_thread_, &worker_thread_);
zhihuang9763d562016-08-05 11:14:50 -070062 webrtc::PeerConnectionInterface::IceServer ice_server;
63 ice_server.uri = "stun:stun.l.google.com:19302";
64 config_.servers.push_back(ice_server);
65
phoglund37ebcf02016-01-08 05:04:57 -080066#ifdef WEBRTC_ANDROID
67 webrtc::InitializeAndroidObjects();
68#endif
wu@webrtc.org364f2042013-11-20 21:49:41 +000069 }
70
kwiberg9e5b11e2017-04-19 03:47:57 -070071 void CreatePcs(
72 const MediaConstraintsInterface* pc_constraints,
73 rtc::scoped_refptr<webrtc::AudioEncoderFactory> audio_encoder_factory,
74 rtc::scoped_refptr<webrtc::AudioDecoderFactory> audio_decoder_factory) {
75 EXPECT_TRUE(caller_->CreatePc(
76 pc_constraints, config_, audio_encoder_factory, audio_decoder_factory));
77 EXPECT_TRUE(callee_->CreatePc(
78 pc_constraints, config_, audio_encoder_factory, audio_decoder_factory));
wu@webrtc.org364f2042013-11-20 21:49:41 +000079 PeerConnectionTestWrapper::Connect(caller_.get(), callee_.get());
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +000080
81 caller_->SignalOnDataChannel.connect(
82 this, &PeerConnectionEndToEndTest::OnCallerAddedDataChanel);
83 callee_->SignalOnDataChannel.connect(
84 this, &PeerConnectionEndToEndTest::OnCalleeAddedDataChannel);
wu@webrtc.org364f2042013-11-20 21:49:41 +000085 }
86
87 void GetAndAddUserMedia() {
88 FakeConstraints audio_constraints;
89 FakeConstraints video_constraints;
90 GetAndAddUserMedia(true, audio_constraints, true, video_constraints);
91 }
92
93 void GetAndAddUserMedia(bool audio, FakeConstraints audio_constraints,
94 bool video, FakeConstraints video_constraints) {
95 caller_->GetAndAddUserMedia(audio, audio_constraints,
96 video, video_constraints);
97 callee_->GetAndAddUserMedia(audio, audio_constraints,
98 video, video_constraints);
99 }
100
101 void Negotiate() {
102 caller_->CreateOffer(NULL);
103 }
104
105 void WaitForCallEstablished() {
106 caller_->WaitForCallEstablished();
107 callee_->WaitForCallEstablished();
108 }
109
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000110 void WaitForConnection() {
111 caller_->WaitForConnection();
112 callee_->WaitForConnection();
113 }
114
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000115 void OnCallerAddedDataChanel(DataChannelInterface* dc) {
116 caller_signaled_data_channels_.push_back(dc);
117 }
118
119 void OnCalleeAddedDataChannel(DataChannelInterface* dc) {
120 callee_signaled_data_channels_.push_back(dc);
121 }
122
123 // Tests that |dc1| and |dc2| can send to and receive from each other.
124 void TestDataChannelSendAndReceive(
125 DataChannelInterface* dc1, DataChannelInterface* dc2) {
kwibergd1fe2812016-04-27 06:47:29 -0700126 std::unique_ptr<webrtc::MockDataChannelObserver> dc1_observer(
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000127 new webrtc::MockDataChannelObserver(dc1));
128
kwibergd1fe2812016-04-27 06:47:29 -0700129 std::unique_ptr<webrtc::MockDataChannelObserver> dc2_observer(
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000130 new webrtc::MockDataChannelObserver(dc2));
131
132 static const std::string kDummyData = "abcdefg";
133 webrtc::DataBuffer buffer(kDummyData);
134 EXPECT_TRUE(dc1->Send(buffer));
135 EXPECT_EQ_WAIT(kDummyData, dc2_observer->last_message(), kMaxWait);
136
137 EXPECT_TRUE(dc2->Send(buffer));
138 EXPECT_EQ_WAIT(kDummyData, dc1_observer->last_message(), kMaxWait);
139
140 EXPECT_EQ(1U, dc1_observer->received_message_count());
141 EXPECT_EQ(1U, dc2_observer->received_message_count());
142 }
143
144 void WaitForDataChannelsToOpen(DataChannelInterface* local_dc,
145 const DataChannelList& remote_dc_list,
146 size_t remote_dc_index) {
147 EXPECT_EQ_WAIT(DataChannelInterface::kOpen, local_dc->state(), kMaxWait);
148
149 EXPECT_TRUE_WAIT(remote_dc_list.size() > remote_dc_index, kMaxWait);
150 EXPECT_EQ_WAIT(DataChannelInterface::kOpen,
151 remote_dc_list[remote_dc_index]->state(),
152 kMaxWait);
153 EXPECT_EQ(local_dc->id(), remote_dc_list[remote_dc_index]->id());
154 }
155
156 void CloseDataChannels(DataChannelInterface* local_dc,
157 const DataChannelList& remote_dc_list,
158 size_t remote_dc_index) {
159 local_dc->Close();
160 EXPECT_EQ_WAIT(DataChannelInterface::kClosed, local_dc->state(), kMaxWait);
161 EXPECT_EQ_WAIT(DataChannelInterface::kClosed,
162 remote_dc_list[remote_dc_index]->state(),
163 kMaxWait);
164 }
165
wu@webrtc.org364f2042013-11-20 21:49:41 +0000166 protected:
danilchape9021a32016-05-17 01:52:02 -0700167 rtc::Thread network_thread_;
perkj57db6522016-04-08 08:16:33 -0700168 rtc::Thread worker_thread_;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000169 rtc::scoped_refptr<PeerConnectionTestWrapper> caller_;
170 rtc::scoped_refptr<PeerConnectionTestWrapper> callee_;
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000171 DataChannelList caller_signaled_data_channels_;
172 DataChannelList callee_signaled_data_channels_;
zhihuang9763d562016-08-05 11:14:50 -0700173 webrtc::PeerConnectionInterface::RTCConfiguration config_;
wu@webrtc.org364f2042013-11-20 21:49:41 +0000174};
175
kwiberg9e5b11e2017-04-19 03:47:57 -0700176std::unique_ptr<webrtc::AudioDecoder> CreateForwardingMockDecoder(
177 std::unique_ptr<webrtc::AudioDecoder> real_decoder) {
178 class ForwardingMockDecoder : public StrictMock<webrtc::MockAudioDecoder> {
179 public:
180 ForwardingMockDecoder(std::unique_ptr<AudioDecoder> decoder)
181 : decoder_(std::move(decoder)) {}
182
183 private:
184 std::unique_ptr<AudioDecoder> decoder_;
185 };
186
187 const auto dec = real_decoder.get(); // For lambda capturing.
188 auto mock_decoder =
189 rtc::MakeUnique<ForwardingMockDecoder>(std::move(real_decoder));
190 EXPECT_CALL(*mock_decoder, Channels())
191 .Times(AtLeast(1))
192 .WillRepeatedly(Invoke([dec] { return dec->Channels(); }));
193 EXPECT_CALL(*mock_decoder, DecodeInternal(_, _, _, _, _))
194 .Times(AtLeast(1))
195 .WillRepeatedly(
196 Invoke([dec](const uint8_t* encoded, size_t encoded_len,
197 int sample_rate_hz, int16_t* decoded,
198 webrtc::AudioDecoder::SpeechType* speech_type) {
199 return dec->Decode(encoded, encoded_len, sample_rate_hz,
200 std::numeric_limits<size_t>::max(), decoded,
201 speech_type);
202 }));
203 EXPECT_CALL(*mock_decoder, Die());
204 EXPECT_CALL(*mock_decoder, HasDecodePlc()).WillRepeatedly(Invoke([dec] {
205 return dec->HasDecodePlc();
206 }));
207 EXPECT_CALL(*mock_decoder, IncomingPacket(_, _, _, _, _))
208 .Times(AtLeast(1))
209 .WillRepeatedly(Invoke([dec](const uint8_t* payload, size_t payload_len,
210 uint16_t rtp_sequence_number,
211 uint32_t rtp_timestamp,
212 uint32_t arrival_timestamp) {
213 return dec->IncomingPacket(payload, payload_len, rtp_sequence_number,
214 rtp_timestamp, arrival_timestamp);
215 }));
216 EXPECT_CALL(*mock_decoder, PacketDuration(_, _))
217 .Times(AtLeast(1))
218 .WillRepeatedly(Invoke([dec](const uint8_t* encoded, size_t encoded_len) {
219 return dec->PacketDuration(encoded, encoded_len);
220 }));
221 EXPECT_CALL(*mock_decoder, SampleRateHz())
222 .Times(AtLeast(1))
223 .WillRepeatedly(Invoke([dec] { return dec->SampleRateHz(); }));
224
225 return std::move(mock_decoder);
226}
227
228rtc::scoped_refptr<webrtc::AudioDecoderFactory>
229CreateForwardingMockDecoderFactory(
230 webrtc::AudioDecoderFactory* real_decoder_factory) {
231 rtc::scoped_refptr<webrtc::MockAudioDecoderFactory> mock_decoder_factory =
232 new rtc::RefCountedObject<StrictMock<webrtc::MockAudioDecoderFactory>>;
233 EXPECT_CALL(*mock_decoder_factory, GetSupportedDecoders())
234 .Times(AtLeast(1))
235 .WillRepeatedly(Invoke([real_decoder_factory] {
236 return real_decoder_factory->GetSupportedDecoders();
237 }));
238 EXPECT_CALL(*mock_decoder_factory, IsSupportedDecoder(_))
239 .Times(AtLeast(1))
240 .WillRepeatedly(
241 Invoke([real_decoder_factory](const webrtc::SdpAudioFormat& format) {
242 return real_decoder_factory->IsSupportedDecoder(format);
243 }));
244 EXPECT_CALL(*mock_decoder_factory, MakeAudioDecoderMock(_, _))
245 .Times(AtLeast(2))
246 .WillRepeatedly(
247 Invoke([real_decoder_factory](
248 const webrtc::SdpAudioFormat& format,
249 std::unique_ptr<webrtc::AudioDecoder>* return_value) {
250 auto real_decoder = real_decoder_factory->MakeAudioDecoder(format);
251 *return_value =
252 real_decoder
253 ? CreateForwardingMockDecoder(std::move(real_decoder))
254 : nullptr;
255 }));
256 return mock_decoder_factory;
257}
258
kjellander@webrtc.org70c0e292015-11-30 21:45:35 +0100259// Disabled for TSan v2, see
260// https://bugs.chromium.org/p/webrtc/issues/detail?id=4719 for details.
kjellander@webrtc.org3c28d0d2015-12-02 22:53:26 +0100261// Disabled for Mac, see
262// https://bugs.chromium.org/p/webrtc/issues/detail?id=5231 for details.
263#if !defined(THREAD_SANITIZER) && !defined(WEBRTC_MAC)
deadbeefee8c6d32015-08-13 14:27:18 -0700264TEST_F(PeerConnectionEndToEndTest, Call) {
kwiberg9e5b11e2017-04-19 03:47:57 -0700265 rtc::scoped_refptr<webrtc::AudioDecoderFactory> real_decoder_factory =
266 webrtc::CreateBuiltinAudioDecoderFactory();
267 CreatePcs(nullptr, webrtc::CreateBuiltinAudioEncoderFactory(),
268 CreateForwardingMockDecoderFactory(real_decoder_factory.get()));
wu@webrtc.org364f2042013-11-20 21:49:41 +0000269 GetAndAddUserMedia();
270 Negotiate();
271 WaitForCallEstablished();
272}
kjellander@webrtc.org3c28d0d2015-12-02 22:53:26 +0100273#endif // if !defined(THREAD_SANITIZER) && !defined(WEBRTC_MAC)
wu@webrtc.org364f2042013-11-20 21:49:41 +0000274
philipel7703b272016-11-28 16:23:12 +0100275#if !defined(ADDRESS_SANITIZER)
deadbeefc9be0072015-12-14 18:27:57 -0800276TEST_F(PeerConnectionEndToEndTest, CallWithLegacySdp) {
wu@webrtc.org364f2042013-11-20 21:49:41 +0000277 FakeConstraints pc_constraints;
278 pc_constraints.AddMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
279 false);
kwiberg9e5b11e2017-04-19 03:47:57 -0700280 CreatePcs(&pc_constraints, webrtc::CreateBuiltinAudioEncoderFactory(),
281 webrtc::CreateBuiltinAudioDecoderFactory());
wu@webrtc.org364f2042013-11-20 21:49:41 +0000282 GetAndAddUserMedia();
283 Negotiate();
284 WaitForCallEstablished();
285}
philipel7703b272016-11-28 16:23:12 +0100286#endif // !defined(ADDRESS_SANITIZER)
wu@webrtc.orgb43202d2013-11-22 19:14:25 +0000287
deadbeef40610e22016-12-22 10:53:38 -0800288#ifdef HAVE_SCTP
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000289// Verifies that a DataChannel created before the negotiation can transition to
290// "OPEN" and transfer data.
291TEST_F(PeerConnectionEndToEndTest, CreateDataChannelBeforeNegotiate) {
kwiberg9e5b11e2017-04-19 03:47:57 -0700292 CreatePcs(nullptr, webrtc::CreateBuiltinAudioEncoderFactory(),
293 webrtc::CreateBuiltinAudioDecoderFactory());
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000294
295 webrtc::DataChannelInit init;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000296 rtc::scoped_refptr<DataChannelInterface> caller_dc(
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000297 caller_->CreateDataChannel("data", init));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000298 rtc::scoped_refptr<DataChannelInterface> callee_dc(
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000299 callee_->CreateDataChannel("data", init));
300
301 Negotiate();
302 WaitForConnection();
303
304 WaitForDataChannelsToOpen(caller_dc, callee_signaled_data_channels_, 0);
305 WaitForDataChannelsToOpen(callee_dc, caller_signaled_data_channels_, 0);
306
307 TestDataChannelSendAndReceive(caller_dc, callee_signaled_data_channels_[0]);
308 TestDataChannelSendAndReceive(callee_dc, caller_signaled_data_channels_[0]);
309
310 CloseDataChannels(caller_dc, callee_signaled_data_channels_, 0);
311 CloseDataChannels(callee_dc, caller_signaled_data_channels_, 0);
312}
313
314// Verifies that a DataChannel created after the negotiation can transition to
315// "OPEN" and transfer data.
Taylor Brandstetterbf2f5692016-06-29 11:22:47 -0700316TEST_F(PeerConnectionEndToEndTest, CreateDataChannelAfterNegotiate) {
kwiberg9e5b11e2017-04-19 03:47:57 -0700317 CreatePcs(nullptr, webrtc::CreateBuiltinAudioEncoderFactory(),
318 webrtc::CreateBuiltinAudioDecoderFactory());
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000319
320 webrtc::DataChannelInit init;
321
322 // This DataChannel is for creating the data content in the negotiation.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000323 rtc::scoped_refptr<DataChannelInterface> dummy(
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000324 caller_->CreateDataChannel("data", init));
325 Negotiate();
326 WaitForConnection();
327
Taylor Brandstetterbf2f5692016-06-29 11:22:47 -0700328 // Wait for the data channel created pre-negotiation to be opened.
329 WaitForDataChannelsToOpen(dummy, callee_signaled_data_channels_, 0);
330
331 // Create new DataChannels after the negotiation and verify their states.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000332 rtc::scoped_refptr<DataChannelInterface> caller_dc(
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000333 caller_->CreateDataChannel("hello", init));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000334 rtc::scoped_refptr<DataChannelInterface> callee_dc(
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000335 callee_->CreateDataChannel("hello", init));
336
337 WaitForDataChannelsToOpen(caller_dc, callee_signaled_data_channels_, 1);
338 WaitForDataChannelsToOpen(callee_dc, caller_signaled_data_channels_, 0);
339
340 TestDataChannelSendAndReceive(caller_dc, callee_signaled_data_channels_[1]);
341 TestDataChannelSendAndReceive(callee_dc, caller_signaled_data_channels_[0]);
342
343 CloseDataChannels(caller_dc, callee_signaled_data_channels_, 1);
344 CloseDataChannels(callee_dc, caller_signaled_data_channels_, 0);
345}
346
347// Verifies that DataChannel IDs are even/odd based on the DTLS roles.
348TEST_F(PeerConnectionEndToEndTest, DataChannelIdAssignment) {
kwiberg9e5b11e2017-04-19 03:47:57 -0700349 CreatePcs(nullptr, webrtc::CreateBuiltinAudioEncoderFactory(),
350 webrtc::CreateBuiltinAudioDecoderFactory());
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000351
352 webrtc::DataChannelInit init;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000353 rtc::scoped_refptr<DataChannelInterface> caller_dc_1(
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000354 caller_->CreateDataChannel("data", init));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000355 rtc::scoped_refptr<DataChannelInterface> callee_dc_1(
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000356 callee_->CreateDataChannel("data", init));
357
358 Negotiate();
359 WaitForConnection();
360
361 EXPECT_EQ(1U, caller_dc_1->id() % 2);
362 EXPECT_EQ(0U, callee_dc_1->id() % 2);
363
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000364 rtc::scoped_refptr<DataChannelInterface> caller_dc_2(
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000365 caller_->CreateDataChannel("data", init));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000366 rtc::scoped_refptr<DataChannelInterface> callee_dc_2(
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000367 callee_->CreateDataChannel("data", init));
368
369 EXPECT_EQ(1U, caller_dc_2->id() % 2);
370 EXPECT_EQ(0U, callee_dc_2->id() % 2);
371}
372
373// Verifies that the message is received by the right remote DataChannel when
374// there are multiple DataChannels.
375TEST_F(PeerConnectionEndToEndTest,
376 MessageTransferBetweenTwoPairsOfDataChannels) {
kwiberg9e5b11e2017-04-19 03:47:57 -0700377 CreatePcs(nullptr, webrtc::CreateBuiltinAudioEncoderFactory(),
378 webrtc::CreateBuiltinAudioDecoderFactory());
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000379
380 webrtc::DataChannelInit init;
381
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000382 rtc::scoped_refptr<DataChannelInterface> caller_dc_1(
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000383 caller_->CreateDataChannel("data", init));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000384 rtc::scoped_refptr<DataChannelInterface> caller_dc_2(
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000385 caller_->CreateDataChannel("data", init));
386
387 Negotiate();
388 WaitForConnection();
389 WaitForDataChannelsToOpen(caller_dc_1, callee_signaled_data_channels_, 0);
390 WaitForDataChannelsToOpen(caller_dc_2, callee_signaled_data_channels_, 1);
391
kwibergd1fe2812016-04-27 06:47:29 -0700392 std::unique_ptr<webrtc::MockDataChannelObserver> dc_1_observer(
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000393 new webrtc::MockDataChannelObserver(callee_signaled_data_channels_[0]));
394
kwibergd1fe2812016-04-27 06:47:29 -0700395 std::unique_ptr<webrtc::MockDataChannelObserver> dc_2_observer(
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000396 new webrtc::MockDataChannelObserver(callee_signaled_data_channels_[1]));
397
398 const std::string message_1 = "hello 1";
399 const std::string message_2 = "hello 2";
400
401 caller_dc_1->Send(webrtc::DataBuffer(message_1));
402 EXPECT_EQ_WAIT(message_1, dc_1_observer->last_message(), kMaxWait);
403
404 caller_dc_2->Send(webrtc::DataBuffer(message_2));
405 EXPECT_EQ_WAIT(message_2, dc_2_observer->last_message(), kMaxWait);
406
407 EXPECT_EQ(1U, dc_1_observer->received_message_count());
408 EXPECT_EQ(1U, dc_2_observer->received_message_count());
409}
deadbeef40610e22016-12-22 10:53:38 -0800410#endif // HAVE_SCTP
deadbeefab9b2d12015-10-14 11:33:11 -0700411
zhihuang9763d562016-08-05 11:14:50 -0700412#ifdef HAVE_QUIC
413// Test that QUIC data channels can be used and that messages go to the correct
414// remote data channel when both peers want to use QUIC. It is assumed that the
415// application has externally negotiated the data channel parameters.
416TEST_F(PeerConnectionEndToEndTest, MessageTransferBetweenQuicDataChannels) {
417 config_.enable_quic = true;
418 CreatePcs();
419
420 webrtc::DataChannelInit init_1;
421 init_1.id = 0;
422 init_1.ordered = false;
423 init_1.reliable = true;
424
425 webrtc::DataChannelInit init_2;
426 init_2.id = 1;
427 init_2.ordered = false;
428 init_2.reliable = true;
429
430 rtc::scoped_refptr<DataChannelInterface> caller_dc_1(
431 caller_->CreateDataChannel("data", init_1));
432 ASSERT_NE(nullptr, caller_dc_1);
433 rtc::scoped_refptr<DataChannelInterface> caller_dc_2(
434 caller_->CreateDataChannel("data", init_2));
435 ASSERT_NE(nullptr, caller_dc_2);
436 rtc::scoped_refptr<DataChannelInterface> callee_dc_1(
437 callee_->CreateDataChannel("data", init_1));
438 ASSERT_NE(nullptr, callee_dc_1);
439 rtc::scoped_refptr<DataChannelInterface> callee_dc_2(
440 callee_->CreateDataChannel("data", init_2));
441 ASSERT_NE(nullptr, callee_dc_2);
442
443 Negotiate();
444 WaitForConnection();
445 EXPECT_TRUE_WAIT(caller_dc_1->state() == webrtc::DataChannelInterface::kOpen,
446 kMaxWait);
447 EXPECT_TRUE_WAIT(callee_dc_1->state() == webrtc::DataChannelInterface::kOpen,
448 kMaxWait);
449 EXPECT_TRUE_WAIT(caller_dc_2->state() == webrtc::DataChannelInterface::kOpen,
450 kMaxWait);
451 EXPECT_TRUE_WAIT(callee_dc_2->state() == webrtc::DataChannelInterface::kOpen,
452 kMaxWait);
453
454 std::unique_ptr<webrtc::MockDataChannelObserver> dc_1_observer(
455 new webrtc::MockDataChannelObserver(callee_dc_1.get()));
456
457 std::unique_ptr<webrtc::MockDataChannelObserver> dc_2_observer(
458 new webrtc::MockDataChannelObserver(callee_dc_2.get()));
459
460 const std::string message_1 = "hello 1";
461 const std::string message_2 = "hello 2";
462
463 // Send data from caller to callee.
464 caller_dc_1->Send(webrtc::DataBuffer(message_1));
465 EXPECT_EQ_WAIT(message_1, dc_1_observer->last_message(), kMaxWait);
466
467 caller_dc_2->Send(webrtc::DataBuffer(message_2));
468 EXPECT_EQ_WAIT(message_2, dc_2_observer->last_message(), kMaxWait);
469
470 EXPECT_EQ(1U, dc_1_observer->received_message_count());
471 EXPECT_EQ(1U, dc_2_observer->received_message_count());
472
473 // Send data from callee to caller.
474 dc_1_observer.reset(new webrtc::MockDataChannelObserver(caller_dc_1.get()));
475 dc_2_observer.reset(new webrtc::MockDataChannelObserver(caller_dc_2.get()));
476
477 callee_dc_1->Send(webrtc::DataBuffer(message_1));
478 EXPECT_EQ_WAIT(message_1, dc_1_observer->last_message(), kMaxWait);
479
480 callee_dc_2->Send(webrtc::DataBuffer(message_2));
481 EXPECT_EQ_WAIT(message_2, dc_2_observer->last_message(), kMaxWait);
482
483 EXPECT_EQ(1U, dc_1_observer->received_message_count());
484 EXPECT_EQ(1U, dc_2_observer->received_message_count());
485}
486#endif // HAVE_QUIC
487
deadbeef40610e22016-12-22 10:53:38 -0800488#ifdef HAVE_SCTP
deadbeefab9b2d12015-10-14 11:33:11 -0700489// Verifies that a DataChannel added from an OPEN message functions after
490// a channel has been previously closed (webrtc issue 3778).
491// This previously failed because the new channel re-uses the ID of the closed
492// channel, and the closed channel was incorrectly still assigned to the id.
493// TODO(deadbeef): This is disabled because there's currently a race condition
494// caused by the fact that a data channel signals that it's closed before it
495// really is. Re-enable this test once that's fixed.
deadbeefe2213ce2016-11-03 16:01:57 -0700496// See: https://bugs.chromium.org/p/webrtc/issues/detail?id=4453
deadbeefab9b2d12015-10-14 11:33:11 -0700497TEST_F(PeerConnectionEndToEndTest,
498 DISABLED_DataChannelFromOpenWorksAfterClose) {
kwiberg9e5b11e2017-04-19 03:47:57 -0700499 CreatePcs(nullptr, webrtc::CreateBuiltinAudioEncoderFactory(),
500 webrtc::CreateBuiltinAudioDecoderFactory());
deadbeefab9b2d12015-10-14 11:33:11 -0700501
502 webrtc::DataChannelInit init;
503 rtc::scoped_refptr<DataChannelInterface> caller_dc(
504 caller_->CreateDataChannel("data", init));
505
506 Negotiate();
507 WaitForConnection();
508
509 WaitForDataChannelsToOpen(caller_dc, callee_signaled_data_channels_, 0);
510 CloseDataChannels(caller_dc, callee_signaled_data_channels_, 0);
511
512 // Create a new channel and ensure it works after closing the previous one.
513 caller_dc = caller_->CreateDataChannel("data2", init);
514
515 WaitForDataChannelsToOpen(caller_dc, callee_signaled_data_channels_, 1);
516 TestDataChannelSendAndReceive(caller_dc, callee_signaled_data_channels_[1]);
517
518 CloseDataChannels(caller_dc, callee_signaled_data_channels_, 1);
519}
deadbeefbd292462015-12-14 18:15:29 -0800520
521// This tests that if a data channel is closed remotely while not referenced
522// by the application (meaning only the PeerConnection contributes to its
523// reference count), no memory access violation will occur.
524// See: https://code.google.com/p/chromium/issues/detail?id=565048
525TEST_F(PeerConnectionEndToEndTest, CloseDataChannelRemotelyWhileNotReferenced) {
kwiberg9e5b11e2017-04-19 03:47:57 -0700526 CreatePcs(nullptr, webrtc::CreateBuiltinAudioEncoderFactory(),
527 webrtc::CreateBuiltinAudioDecoderFactory());
deadbeefbd292462015-12-14 18:15:29 -0800528
529 webrtc::DataChannelInit init;
530 rtc::scoped_refptr<DataChannelInterface> caller_dc(
531 caller_->CreateDataChannel("data", init));
532
533 Negotiate();
534 WaitForConnection();
535
536 WaitForDataChannelsToOpen(caller_dc, callee_signaled_data_channels_, 0);
537 // This removes the reference to the remote data channel that we hold.
538 callee_signaled_data_channels_.clear();
539 caller_dc->Close();
540 EXPECT_EQ_WAIT(DataChannelInterface::kClosed, caller_dc->state(), kMaxWait);
541
542 // Wait for a bit longer so the remote data channel will receive the
543 // close message and be destroyed.
544 rtc::Thread::Current()->ProcessMessages(100);
545}
deadbeef40610e22016-12-22 10:53:38 -0800546#endif // HAVE_SCTP