wu@webrtc.org | 364f204 | 2013-11-20 21:49:41 +0000 | [diff] [blame] | 1 | /* |
kjellander | b24317b | 2016-02-10 07:54:43 -0800 | [diff] [blame] | 2 | * Copyright 2013 The WebRTC project authors. All Rights Reserved. |
wu@webrtc.org | 364f204 | 2013-11-20 21:49:41 +0000 | [diff] [blame] | 3 | * |
kjellander | b24317b | 2016-02-10 07:54:43 -0800 | [diff] [blame] | 4 | * 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.org | 364f204 | 2013-11-20 21:49:41 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
kwiberg | d1fe281 | 2016-04-27 06:47:29 -0700 | [diff] [blame] | 11 | #include <memory> |
| 12 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 13 | #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 Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 18 | #include "rtc_base/stringencode.h" |
| 19 | #include "rtc_base/stringutils.h" |
Patrik Höglund | 563934e | 2017-09-15 09:04:28 +0200 | [diff] [blame^] | 20 | |
ossu | 7bb87ee | 2017-01-23 04:56:25 -0800 | [diff] [blame] | 21 | #ifdef WEBRTC_ANDROID |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 22 | #include "pc/test/androidtestinitializer.h" |
ossu | 7bb87ee | 2017-01-23 04:56:25 -0800 | [diff] [blame] | 23 | #endif |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 24 | #include "pc/test/peerconnectiontestwrapper.h" |
ossu | 7bb87ee | 2017-01-23 04:56:25 -0800 | [diff] [blame] | 25 | // Notice that mockpeerconnectionobservers.h must be included after the above! |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 26 | #include "pc/test/mockpeerconnectionobservers.h" |
| 27 | #include "test/mock_audio_decoder.h" |
| 28 | #include "test/mock_audio_decoder_factory.h" |
kwiberg | 9e5b11e | 2017-04-19 03:47:57 -0700 | [diff] [blame] | 29 | |
| 30 | using testing::AtLeast; |
| 31 | using testing::Invoke; |
| 32 | using testing::StrictMock; |
| 33 | using testing::_; |
wu@webrtc.org | 364f204 | 2013-11-20 21:49:41 +0000 | [diff] [blame] | 34 | |
jiayl@webrtc.org | 1a6c628 | 2014-06-12 21:59:29 +0000 | [diff] [blame] | 35 | using webrtc::DataChannelInterface; |
wu@webrtc.org | 364f204 | 2013-11-20 21:49:41 +0000 | [diff] [blame] | 36 | using webrtc::FakeConstraints; |
| 37 | using webrtc::MediaConstraintsInterface; |
| 38 | using webrtc::MediaStreamInterface; |
| 39 | using webrtc::PeerConnectionInterface; |
| 40 | |
| 41 | namespace { |
| 42 | |
Honghai Zhang | 82d7862 | 2016-05-06 11:29:15 -0700 | [diff] [blame] | 43 | const int kMaxWait = 10000; |
wu@webrtc.org | 364f204 | 2013-11-20 21:49:41 +0000 | [diff] [blame] | 44 | |
wu@webrtc.org | 364f204 | 2013-11-20 21:49:41 +0000 | [diff] [blame] | 45 | } // namespace |
| 46 | |
| 47 | class PeerConnectionEndToEndTest |
| 48 | : public sigslot::has_slots<>, |
| 49 | public testing::Test { |
| 50 | public: |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 51 | typedef std::vector<rtc::scoped_refptr<DataChannelInterface> > |
jiayl@webrtc.org | 1a6c628 | 2014-06-12 21:59:29 +0000 | [diff] [blame] | 52 | DataChannelList; |
| 53 | |
perkj | 57db652 | 2016-04-08 08:16:33 -0700 | [diff] [blame] | 54 | PeerConnectionEndToEndTest() { |
tommi | e725159 | 2017-07-14 14:44:46 -0700 | [diff] [blame] | 55 | network_thread_ = rtc::Thread::CreateWithSocketServer(); |
| 56 | worker_thread_ = rtc::Thread::Create(); |
| 57 | RTC_CHECK(network_thread_->Start()); |
| 58 | RTC_CHECK(worker_thread_->Start()); |
perkj | 57db652 | 2016-04-08 08:16:33 -0700 | [diff] [blame] | 59 | caller_ = new rtc::RefCountedObject<PeerConnectionTestWrapper>( |
tommi | e725159 | 2017-07-14 14:44:46 -0700 | [diff] [blame] | 60 | "caller", network_thread_.get(), worker_thread_.get()); |
perkj | 57db652 | 2016-04-08 08:16:33 -0700 | [diff] [blame] | 61 | callee_ = new rtc::RefCountedObject<PeerConnectionTestWrapper>( |
tommi | e725159 | 2017-07-14 14:44:46 -0700 | [diff] [blame] | 62 | "callee", network_thread_.get(), worker_thread_.get()); |
zhihuang | 9763d56 | 2016-08-05 11:14:50 -0700 | [diff] [blame] | 63 | webrtc::PeerConnectionInterface::IceServer ice_server; |
| 64 | ice_server.uri = "stun:stun.l.google.com:19302"; |
| 65 | config_.servers.push_back(ice_server); |
| 66 | |
phoglund | 37ebcf0 | 2016-01-08 05:04:57 -0800 | [diff] [blame] | 67 | #ifdef WEBRTC_ANDROID |
| 68 | webrtc::InitializeAndroidObjects(); |
| 69 | #endif |
wu@webrtc.org | 364f204 | 2013-11-20 21:49:41 +0000 | [diff] [blame] | 70 | } |
| 71 | |
kwiberg | 9e5b11e | 2017-04-19 03:47:57 -0700 | [diff] [blame] | 72 | 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.org | 364f204 | 2013-11-20 21:49:41 +0000 | [diff] [blame] | 80 | PeerConnectionTestWrapper::Connect(caller_.get(), callee_.get()); |
jiayl@webrtc.org | 1a6c628 | 2014-06-12 21:59:29 +0000 | [diff] [blame] | 81 | |
| 82 | caller_->SignalOnDataChannel.connect( |
| 83 | this, &PeerConnectionEndToEndTest::OnCallerAddedDataChanel); |
| 84 | callee_->SignalOnDataChannel.connect( |
| 85 | this, &PeerConnectionEndToEndTest::OnCalleeAddedDataChannel); |
wu@webrtc.org | 364f204 | 2013-11-20 21:49:41 +0000 | [diff] [blame] | 86 | } |
| 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.org | 1a6c628 | 2014-06-12 21:59:29 +0000 | [diff] [blame] | 111 | void WaitForConnection() { |
| 112 | caller_->WaitForConnection(); |
| 113 | callee_->WaitForConnection(); |
| 114 | } |
| 115 | |
jiayl@webrtc.org | 1a6c628 | 2014-06-12 21:59:29 +0000 | [diff] [blame] | 116 | 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) { |
kwiberg | d1fe281 | 2016-04-27 06:47:29 -0700 | [diff] [blame] | 127 | std::unique_ptr<webrtc::MockDataChannelObserver> dc1_observer( |
jiayl@webrtc.org | 1a6c628 | 2014-06-12 21:59:29 +0000 | [diff] [blame] | 128 | new webrtc::MockDataChannelObserver(dc1)); |
| 129 | |
kwiberg | d1fe281 | 2016-04-27 06:47:29 -0700 | [diff] [blame] | 130 | std::unique_ptr<webrtc::MockDataChannelObserver> dc2_observer( |
jiayl@webrtc.org | 1a6c628 | 2014-06-12 21:59:29 +0000 | [diff] [blame] | 131 | 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.org | 364f204 | 2013-11-20 21:49:41 +0000 | [diff] [blame] | 167 | protected: |
tommi | e725159 | 2017-07-14 14:44:46 -0700 | [diff] [blame] | 168 | std::unique_ptr<rtc::Thread> network_thread_; |
| 169 | std::unique_ptr<rtc::Thread> worker_thread_; |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 170 | rtc::scoped_refptr<PeerConnectionTestWrapper> caller_; |
| 171 | rtc::scoped_refptr<PeerConnectionTestWrapper> callee_; |
jiayl@webrtc.org | 1a6c628 | 2014-06-12 21:59:29 +0000 | [diff] [blame] | 172 | DataChannelList caller_signaled_data_channels_; |
| 173 | DataChannelList callee_signaled_data_channels_; |
zhihuang | 9763d56 | 2016-08-05 11:14:50 -0700 | [diff] [blame] | 174 | webrtc::PeerConnectionInterface::RTCConfiguration config_; |
wu@webrtc.org | 364f204 | 2013-11-20 21:49:41 +0000 | [diff] [blame] | 175 | }; |
| 176 | |
kwiberg | 9e5b11e | 2017-04-19 03:47:57 -0700 | [diff] [blame] | 177 | std::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 | |
| 229 | rtc::scoped_refptr<webrtc::AudioDecoderFactory> |
| 230 | CreateForwardingMockDecoderFactory( |
| 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.org | 70c0e29 | 2015-11-30 21:45:35 +0100 | [diff] [blame] | 260 | // Disabled for TSan v2, see |
| 261 | // https://bugs.chromium.org/p/webrtc/issues/detail?id=4719 for details. |
kjellander@webrtc.org | 3c28d0d | 2015-12-02 22:53:26 +0100 | [diff] [blame] | 262 | // 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) |
deadbeef | ee8c6d3 | 2015-08-13 14:27:18 -0700 | [diff] [blame] | 265 | TEST_F(PeerConnectionEndToEndTest, Call) { |
kwiberg | 9e5b11e | 2017-04-19 03:47:57 -0700 | [diff] [blame] | 266 | rtc::scoped_refptr<webrtc::AudioDecoderFactory> real_decoder_factory = |
| 267 | webrtc::CreateBuiltinAudioDecoderFactory(); |
| 268 | CreatePcs(nullptr, webrtc::CreateBuiltinAudioEncoderFactory(), |
| 269 | CreateForwardingMockDecoderFactory(real_decoder_factory.get())); |
wu@webrtc.org | 364f204 | 2013-11-20 21:49:41 +0000 | [diff] [blame] | 270 | GetAndAddUserMedia(); |
| 271 | Negotiate(); |
| 272 | WaitForCallEstablished(); |
| 273 | } |
kjellander@webrtc.org | 3c28d0d | 2015-12-02 22:53:26 +0100 | [diff] [blame] | 274 | #endif // if !defined(THREAD_SANITIZER) && !defined(WEBRTC_MAC) |
wu@webrtc.org | 364f204 | 2013-11-20 21:49:41 +0000 | [diff] [blame] | 275 | |
philipel | 7703b27 | 2016-11-28 16:23:12 +0100 | [diff] [blame] | 276 | #if !defined(ADDRESS_SANITIZER) |
deadbeef | c9be007 | 2015-12-14 18:27:57 -0800 | [diff] [blame] | 277 | TEST_F(PeerConnectionEndToEndTest, CallWithLegacySdp) { |
wu@webrtc.org | 364f204 | 2013-11-20 21:49:41 +0000 | [diff] [blame] | 278 | FakeConstraints pc_constraints; |
| 279 | pc_constraints.AddMandatory(MediaConstraintsInterface::kEnableDtlsSrtp, |
| 280 | false); |
kwiberg | 9e5b11e | 2017-04-19 03:47:57 -0700 | [diff] [blame] | 281 | CreatePcs(&pc_constraints, webrtc::CreateBuiltinAudioEncoderFactory(), |
| 282 | webrtc::CreateBuiltinAudioDecoderFactory()); |
wu@webrtc.org | 364f204 | 2013-11-20 21:49:41 +0000 | [diff] [blame] | 283 | GetAndAddUserMedia(); |
| 284 | Negotiate(); |
| 285 | WaitForCallEstablished(); |
| 286 | } |
philipel | 7703b27 | 2016-11-28 16:23:12 +0100 | [diff] [blame] | 287 | #endif // !defined(ADDRESS_SANITIZER) |
wu@webrtc.org | b43202d | 2013-11-22 19:14:25 +0000 | [diff] [blame] | 288 | |
deadbeef | 40610e2 | 2016-12-22 10:53:38 -0800 | [diff] [blame] | 289 | #ifdef HAVE_SCTP |
jiayl@webrtc.org | 1a6c628 | 2014-06-12 21:59:29 +0000 | [diff] [blame] | 290 | // Verifies that a DataChannel created before the negotiation can transition to |
| 291 | // "OPEN" and transfer data. |
| 292 | TEST_F(PeerConnectionEndToEndTest, CreateDataChannelBeforeNegotiate) { |
kwiberg | 9e5b11e | 2017-04-19 03:47:57 -0700 | [diff] [blame] | 293 | CreatePcs(nullptr, webrtc::CreateBuiltinAudioEncoderFactory(), |
kwiberg | 7a12b5a | 2017-04-27 03:55:57 -0700 | [diff] [blame] | 294 | webrtc::MockAudioDecoderFactory::CreateEmptyFactory()); |
jiayl@webrtc.org | 1a6c628 | 2014-06-12 21:59:29 +0000 | [diff] [blame] | 295 | |
| 296 | webrtc::DataChannelInit init; |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 297 | rtc::scoped_refptr<DataChannelInterface> caller_dc( |
jiayl@webrtc.org | 1a6c628 | 2014-06-12 21:59:29 +0000 | [diff] [blame] | 298 | caller_->CreateDataChannel("data", init)); |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 299 | rtc::scoped_refptr<DataChannelInterface> callee_dc( |
jiayl@webrtc.org | 1a6c628 | 2014-06-12 21:59:29 +0000 | [diff] [blame] | 300 | 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 Brandstetter | bf2f569 | 2016-06-29 11:22:47 -0700 | [diff] [blame] | 317 | TEST_F(PeerConnectionEndToEndTest, CreateDataChannelAfterNegotiate) { |
kwiberg | 9e5b11e | 2017-04-19 03:47:57 -0700 | [diff] [blame] | 318 | CreatePcs(nullptr, webrtc::CreateBuiltinAudioEncoderFactory(), |
kwiberg | 7a12b5a | 2017-04-27 03:55:57 -0700 | [diff] [blame] | 319 | webrtc::MockAudioDecoderFactory::CreateEmptyFactory()); |
jiayl@webrtc.org | 1a6c628 | 2014-06-12 21:59:29 +0000 | [diff] [blame] | 320 | |
| 321 | webrtc::DataChannelInit init; |
| 322 | |
| 323 | // This DataChannel is for creating the data content in the negotiation. |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 324 | rtc::scoped_refptr<DataChannelInterface> dummy( |
jiayl@webrtc.org | 1a6c628 | 2014-06-12 21:59:29 +0000 | [diff] [blame] | 325 | caller_->CreateDataChannel("data", init)); |
| 326 | Negotiate(); |
| 327 | WaitForConnection(); |
| 328 | |
Taylor Brandstetter | bf2f569 | 2016-06-29 11:22:47 -0700 | [diff] [blame] | 329 | // 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.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 333 | rtc::scoped_refptr<DataChannelInterface> caller_dc( |
jiayl@webrtc.org | 1a6c628 | 2014-06-12 21:59:29 +0000 | [diff] [blame] | 334 | caller_->CreateDataChannel("hello", init)); |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 335 | rtc::scoped_refptr<DataChannelInterface> callee_dc( |
jiayl@webrtc.org | 1a6c628 | 2014-06-12 21:59:29 +0000 | [diff] [blame] | 336 | 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. |
| 349 | TEST_F(PeerConnectionEndToEndTest, DataChannelIdAssignment) { |
kwiberg | 9e5b11e | 2017-04-19 03:47:57 -0700 | [diff] [blame] | 350 | CreatePcs(nullptr, webrtc::CreateBuiltinAudioEncoderFactory(), |
kwiberg | 7a12b5a | 2017-04-27 03:55:57 -0700 | [diff] [blame] | 351 | webrtc::MockAudioDecoderFactory::CreateEmptyFactory()); |
jiayl@webrtc.org | 1a6c628 | 2014-06-12 21:59:29 +0000 | [diff] [blame] | 352 | |
| 353 | webrtc::DataChannelInit init; |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 354 | rtc::scoped_refptr<DataChannelInterface> caller_dc_1( |
jiayl@webrtc.org | 1a6c628 | 2014-06-12 21:59:29 +0000 | [diff] [blame] | 355 | caller_->CreateDataChannel("data", init)); |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 356 | rtc::scoped_refptr<DataChannelInterface> callee_dc_1( |
jiayl@webrtc.org | 1a6c628 | 2014-06-12 21:59:29 +0000 | [diff] [blame] | 357 | 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.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 365 | rtc::scoped_refptr<DataChannelInterface> caller_dc_2( |
jiayl@webrtc.org | 1a6c628 | 2014-06-12 21:59:29 +0000 | [diff] [blame] | 366 | caller_->CreateDataChannel("data", init)); |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 367 | rtc::scoped_refptr<DataChannelInterface> callee_dc_2( |
jiayl@webrtc.org | 1a6c628 | 2014-06-12 21:59:29 +0000 | [diff] [blame] | 368 | 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. |
| 376 | TEST_F(PeerConnectionEndToEndTest, |
| 377 | MessageTransferBetweenTwoPairsOfDataChannels) { |
kwiberg | 9e5b11e | 2017-04-19 03:47:57 -0700 | [diff] [blame] | 378 | CreatePcs(nullptr, webrtc::CreateBuiltinAudioEncoderFactory(), |
kwiberg | 7a12b5a | 2017-04-27 03:55:57 -0700 | [diff] [blame] | 379 | webrtc::MockAudioDecoderFactory::CreateEmptyFactory()); |
jiayl@webrtc.org | 1a6c628 | 2014-06-12 21:59:29 +0000 | [diff] [blame] | 380 | |
| 381 | webrtc::DataChannelInit init; |
| 382 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 383 | rtc::scoped_refptr<DataChannelInterface> caller_dc_1( |
jiayl@webrtc.org | 1a6c628 | 2014-06-12 21:59:29 +0000 | [diff] [blame] | 384 | caller_->CreateDataChannel("data", init)); |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 385 | rtc::scoped_refptr<DataChannelInterface> caller_dc_2( |
jiayl@webrtc.org | 1a6c628 | 2014-06-12 21:59:29 +0000 | [diff] [blame] | 386 | 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 | |
kwiberg | d1fe281 | 2016-04-27 06:47:29 -0700 | [diff] [blame] | 393 | std::unique_ptr<webrtc::MockDataChannelObserver> dc_1_observer( |
jiayl@webrtc.org | 1a6c628 | 2014-06-12 21:59:29 +0000 | [diff] [blame] | 394 | new webrtc::MockDataChannelObserver(callee_signaled_data_channels_[0])); |
| 395 | |
kwiberg | d1fe281 | 2016-04-27 06:47:29 -0700 | [diff] [blame] | 396 | std::unique_ptr<webrtc::MockDataChannelObserver> dc_2_observer( |
jiayl@webrtc.org | 1a6c628 | 2014-06-12 21:59:29 +0000 | [diff] [blame] | 397 | 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 | } |
deadbeef | 40610e2 | 2016-12-22 10:53:38 -0800 | [diff] [blame] | 411 | #endif // HAVE_SCTP |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 412 | |
zhihuang | 9763d56 | 2016-08-05 11:14:50 -0700 | [diff] [blame] | 413 | #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. |
| 417 | TEST_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 | |
deadbeef | 40610e2 | 2016-12-22 10:53:38 -0800 | [diff] [blame] | 489 | #ifdef HAVE_SCTP |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 490 | // 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. |
deadbeef | e2213ce | 2016-11-03 16:01:57 -0700 | [diff] [blame] | 497 | // See: https://bugs.chromium.org/p/webrtc/issues/detail?id=4453 |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 498 | TEST_F(PeerConnectionEndToEndTest, |
| 499 | DISABLED_DataChannelFromOpenWorksAfterClose) { |
kwiberg | 9e5b11e | 2017-04-19 03:47:57 -0700 | [diff] [blame] | 500 | CreatePcs(nullptr, webrtc::CreateBuiltinAudioEncoderFactory(), |
kwiberg | 7a12b5a | 2017-04-27 03:55:57 -0700 | [diff] [blame] | 501 | webrtc::MockAudioDecoderFactory::CreateEmptyFactory()); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 502 | |
| 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 | } |
deadbeef | bd29246 | 2015-12-14 18:15:29 -0800 | [diff] [blame] | 521 | |
| 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 |
| 526 | TEST_F(PeerConnectionEndToEndTest, CloseDataChannelRemotelyWhileNotReferenced) { |
kwiberg | 9e5b11e | 2017-04-19 03:47:57 -0700 | [diff] [blame] | 527 | CreatePcs(nullptr, webrtc::CreateBuiltinAudioEncoderFactory(), |
kwiberg | 7a12b5a | 2017-04-27 03:55:57 -0700 | [diff] [blame] | 528 | webrtc::MockAudioDecoderFactory::CreateEmptyFactory()); |
deadbeef | bd29246 | 2015-12-14 18:15:29 -0800 | [diff] [blame] | 529 | |
| 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 | } |
deadbeef | 40610e2 | 2016-12-22 10:53:38 -0800 | [diff] [blame] | 547 | #endif // HAVE_SCTP |