Bug 1864652 - Expose settings for Global Privacy Control. r=geckoview-reviewers,ohall...
[gecko.git] / third_party / libwebrtc / pc / peer_connection_end_to_end_unittest.cc
bloba21d455ec5a8b3a93a8d400cbd81b12ddbe1560e
1 /*
2 * Copyright 2013 The WebRTC project authors. All Rights Reserved.
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.
9 */
11 #include <stdint.h>
13 #include <cstddef>
14 #include <limits>
15 #include <memory>
16 #include <string>
17 #include <type_traits>
18 #include <utility>
19 #include <vector>
21 #include "absl/strings/match.h"
22 #include "absl/types/optional.h"
23 #include "api/audio_codecs/L16/audio_decoder_L16.h"
24 #include "api/audio_codecs/L16/audio_encoder_L16.h"
25 #include "api/audio_codecs/audio_codec_pair_id.h"
26 #include "api/audio_codecs/audio_decoder.h"
27 #include "api/audio_codecs/audio_decoder_factory.h"
28 #include "api/audio_codecs/audio_decoder_factory_template.h"
29 #include "api/audio_codecs/audio_encoder.h"
30 #include "api/audio_codecs/audio_encoder_factory.h"
31 #include "api/audio_codecs/audio_encoder_factory_template.h"
32 #include "api/audio_codecs/audio_format.h"
33 #include "api/audio_codecs/opus_audio_decoder_factory.h"
34 #include "api/audio_codecs/opus_audio_encoder_factory.h"
35 #include "api/audio_options.h"
36 #include "api/data_channel_interface.h"
37 #include "api/media_stream_interface.h"
38 #include "api/peer_connection_interface.h"
39 #include "api/rtc_error.h"
40 #include "api/scoped_refptr.h"
41 #include "media/sctp/sctp_transport_internal.h"
42 #include "rtc_base/checks.h"
43 #include "rtc_base/copy_on_write_buffer.h"
44 #include "rtc_base/gunit.h"
45 #include "rtc_base/physical_socket_server.h"
46 #include "rtc_base/third_party/sigslot/sigslot.h"
47 #include "rtc_base/thread.h"
48 #include "test/gmock.h"
49 #include "test/gtest.h"
51 #ifdef WEBRTC_ANDROID
52 #include "pc/test/android_test_initializer.h"
53 #endif
54 #include "pc/test/peer_connection_test_wrapper.h"
55 // Notice that mockpeerconnectionobservers.h must be included after the above!
56 #include "pc/test/mock_peer_connection_observers.h"
57 #include "test/mock_audio_decoder.h"
58 #include "test/mock_audio_decoder_factory.h"
59 #include "test/mock_audio_encoder_factory.h"
61 using ::testing::_;
62 using ::testing::AtLeast;
63 using ::testing::Invoke;
64 using ::testing::StrictMock;
65 using ::testing::Values;
67 using webrtc::DataChannelInterface;
68 using webrtc::MediaStreamInterface;
69 using webrtc::PeerConnectionInterface;
70 using webrtc::SdpSemantics;
72 namespace {
74 const int kMaxWait = 25000;
76 } // namespace
78 class PeerConnectionEndToEndBaseTest : public sigslot::has_slots<>,
79 public ::testing::Test {
80 public:
81 typedef std::vector<rtc::scoped_refptr<DataChannelInterface>> DataChannelList;
83 explicit PeerConnectionEndToEndBaseTest(SdpSemantics sdp_semantics)
84 : network_thread_(std::make_unique<rtc::Thread>(&pss_)),
85 worker_thread_(rtc::Thread::Create()) {
86 RTC_CHECK(network_thread_->Start());
87 RTC_CHECK(worker_thread_->Start());
88 caller_ = rtc::make_ref_counted<PeerConnectionTestWrapper>(
89 "caller", &pss_, network_thread_.get(), worker_thread_.get());
90 callee_ = rtc::make_ref_counted<PeerConnectionTestWrapper>(
91 "callee", &pss_, network_thread_.get(), worker_thread_.get());
92 webrtc::PeerConnectionInterface::IceServer ice_server;
93 ice_server.uri = "stun:stun.l.google.com:19302";
94 config_.servers.push_back(ice_server);
95 config_.sdp_semantics = sdp_semantics;
97 #ifdef WEBRTC_ANDROID
98 webrtc::InitializeAndroidObjects();
99 #endif
102 void CreatePcs(
103 rtc::scoped_refptr<webrtc::AudioEncoderFactory> audio_encoder_factory1,
104 rtc::scoped_refptr<webrtc::AudioDecoderFactory> audio_decoder_factory1,
105 rtc::scoped_refptr<webrtc::AudioEncoderFactory> audio_encoder_factory2,
106 rtc::scoped_refptr<webrtc::AudioDecoderFactory> audio_decoder_factory2) {
107 EXPECT_TRUE(caller_->CreatePc(config_, audio_encoder_factory1,
108 audio_decoder_factory1));
109 EXPECT_TRUE(callee_->CreatePc(config_, audio_encoder_factory2,
110 audio_decoder_factory2));
111 PeerConnectionTestWrapper::Connect(caller_.get(), callee_.get());
113 caller_->SignalOnDataChannel.connect(
114 this, &PeerConnectionEndToEndBaseTest::OnCallerAddedDataChanel);
115 callee_->SignalOnDataChannel.connect(
116 this, &PeerConnectionEndToEndBaseTest::OnCalleeAddedDataChannel);
119 void CreatePcs(
120 rtc::scoped_refptr<webrtc::AudioEncoderFactory> audio_encoder_factory,
121 rtc::scoped_refptr<webrtc::AudioDecoderFactory> audio_decoder_factory) {
122 CreatePcs(audio_encoder_factory, audio_decoder_factory,
123 audio_encoder_factory, audio_decoder_factory);
126 void GetAndAddUserMedia() {
127 cricket::AudioOptions audio_options;
128 GetAndAddUserMedia(true, audio_options, true);
131 void GetAndAddUserMedia(bool audio,
132 const cricket::AudioOptions& audio_options,
133 bool video) {
134 caller_->GetAndAddUserMedia(audio, audio_options, video);
135 callee_->GetAndAddUserMedia(audio, audio_options, video);
138 void Negotiate() {
139 caller_->CreateOffer(
140 webrtc::PeerConnectionInterface::RTCOfferAnswerOptions());
143 void WaitForCallEstablished() {
144 caller_->WaitForCallEstablished();
145 callee_->WaitForCallEstablished();
148 void WaitForConnection() {
149 caller_->WaitForConnection();
150 callee_->WaitForConnection();
153 void OnCallerAddedDataChanel(DataChannelInterface* dc) {
154 caller_signaled_data_channels_.push_back(
155 rtc::scoped_refptr<DataChannelInterface>(dc));
158 void OnCalleeAddedDataChannel(DataChannelInterface* dc) {
159 callee_signaled_data_channels_.push_back(
160 rtc::scoped_refptr<DataChannelInterface>(dc));
163 // Tests that `dc1` and `dc2` can send to and receive from each other.
164 void TestDataChannelSendAndReceive(DataChannelInterface* dc1,
165 DataChannelInterface* dc2,
166 size_t size = 6) {
167 std::unique_ptr<webrtc::MockDataChannelObserver> dc1_observer(
168 new webrtc::MockDataChannelObserver(dc1));
170 std::unique_ptr<webrtc::MockDataChannelObserver> dc2_observer(
171 new webrtc::MockDataChannelObserver(dc2));
173 static const std::string kDummyData =
174 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
175 webrtc::DataBuffer buffer("");
177 size_t sizeLeft = size;
178 while (sizeLeft > 0) {
179 size_t chunkSize =
180 sizeLeft > kDummyData.length() ? kDummyData.length() : sizeLeft;
181 buffer.data.AppendData(kDummyData.data(), chunkSize);
182 sizeLeft -= chunkSize;
185 EXPECT_TRUE(dc1->Send(buffer));
186 EXPECT_EQ_WAIT(buffer.data,
187 rtc::CopyOnWriteBuffer(dc2_observer->last_message()),
188 kMaxWait);
190 EXPECT_TRUE(dc2->Send(buffer));
191 EXPECT_EQ_WAIT(buffer.data,
192 rtc::CopyOnWriteBuffer(dc1_observer->last_message()),
193 kMaxWait);
195 EXPECT_EQ(1U, dc1_observer->received_message_count());
196 EXPECT_EQ(size, dc1_observer->last_message().length());
197 EXPECT_EQ(1U, dc2_observer->received_message_count());
198 EXPECT_EQ(size, dc2_observer->last_message().length());
201 void WaitForDataChannelsToOpen(DataChannelInterface* local_dc,
202 const DataChannelList& remote_dc_list,
203 size_t remote_dc_index) {
204 EXPECT_EQ_WAIT(DataChannelInterface::kOpen, local_dc->state(), kMaxWait);
206 ASSERT_TRUE_WAIT(remote_dc_list.size() > remote_dc_index, kMaxWait);
207 EXPECT_EQ_WAIT(DataChannelInterface::kOpen,
208 remote_dc_list[remote_dc_index]->state(), kMaxWait);
209 EXPECT_EQ(local_dc->id(), remote_dc_list[remote_dc_index]->id());
212 void CloseDataChannels(DataChannelInterface* local_dc,
213 const DataChannelList& remote_dc_list,
214 size_t remote_dc_index) {
215 local_dc->Close();
216 EXPECT_EQ_WAIT(DataChannelInterface::kClosed, local_dc->state(), kMaxWait);
217 EXPECT_EQ_WAIT(DataChannelInterface::kClosed,
218 remote_dc_list[remote_dc_index]->state(), kMaxWait);
221 protected:
222 rtc::AutoThread main_thread_;
223 rtc::PhysicalSocketServer pss_;
224 std::unique_ptr<rtc::Thread> network_thread_;
225 std::unique_ptr<rtc::Thread> worker_thread_;
226 rtc::scoped_refptr<PeerConnectionTestWrapper> caller_;
227 rtc::scoped_refptr<PeerConnectionTestWrapper> callee_;
228 DataChannelList caller_signaled_data_channels_;
229 DataChannelList callee_signaled_data_channels_;
230 webrtc::PeerConnectionInterface::RTCConfiguration config_;
233 class PeerConnectionEndToEndTest
234 : public PeerConnectionEndToEndBaseTest,
235 public ::testing::WithParamInterface<SdpSemantics> {
236 protected:
237 PeerConnectionEndToEndTest() : PeerConnectionEndToEndBaseTest(GetParam()) {}
240 namespace {
242 std::unique_ptr<webrtc::AudioDecoder> CreateForwardingMockDecoder(
243 std::unique_ptr<webrtc::AudioDecoder> real_decoder) {
244 class ForwardingMockDecoder : public StrictMock<webrtc::MockAudioDecoder> {
245 public:
246 explicit ForwardingMockDecoder(std::unique_ptr<AudioDecoder> decoder)
247 : decoder_(std::move(decoder)) {}
249 private:
250 std::unique_ptr<AudioDecoder> decoder_;
253 const auto dec = real_decoder.get(); // For lambda capturing.
254 auto mock_decoder =
255 std::make_unique<ForwardingMockDecoder>(std::move(real_decoder));
256 EXPECT_CALL(*mock_decoder, Channels())
257 .Times(AtLeast(1))
258 .WillRepeatedly(Invoke([dec] { return dec->Channels(); }));
259 EXPECT_CALL(*mock_decoder, DecodeInternal(_, _, _, _, _))
260 .Times(AtLeast(1))
261 .WillRepeatedly(
262 Invoke([dec](const uint8_t* encoded, size_t encoded_len,
263 int sample_rate_hz, int16_t* decoded,
264 webrtc::AudioDecoder::SpeechType* speech_type) {
265 return dec->Decode(encoded, encoded_len, sample_rate_hz,
266 std::numeric_limits<size_t>::max(), decoded,
267 speech_type);
268 }));
269 EXPECT_CALL(*mock_decoder, Die());
270 EXPECT_CALL(*mock_decoder, HasDecodePlc()).WillRepeatedly(Invoke([dec] {
271 return dec->HasDecodePlc();
272 }));
273 EXPECT_CALL(*mock_decoder, PacketDuration(_, _))
274 .Times(AtLeast(1))
275 .WillRepeatedly(Invoke([dec](const uint8_t* encoded, size_t encoded_len) {
276 return dec->PacketDuration(encoded, encoded_len);
277 }));
278 EXPECT_CALL(*mock_decoder, SampleRateHz())
279 .Times(AtLeast(1))
280 .WillRepeatedly(Invoke([dec] { return dec->SampleRateHz(); }));
282 return std::move(mock_decoder);
285 rtc::scoped_refptr<webrtc::AudioDecoderFactory>
286 CreateForwardingMockDecoderFactory(
287 webrtc::AudioDecoderFactory* real_decoder_factory) {
288 rtc::scoped_refptr<webrtc::MockAudioDecoderFactory> mock_decoder_factory =
289 rtc::make_ref_counted<StrictMock<webrtc::MockAudioDecoderFactory>>();
290 EXPECT_CALL(*mock_decoder_factory, GetSupportedDecoders())
291 .Times(AtLeast(1))
292 .WillRepeatedly(Invoke([real_decoder_factory] {
293 return real_decoder_factory->GetSupportedDecoders();
294 }));
295 EXPECT_CALL(*mock_decoder_factory, IsSupportedDecoder(_))
296 .Times(AtLeast(1))
297 .WillRepeatedly(
298 Invoke([real_decoder_factory](const webrtc::SdpAudioFormat& format) {
299 return real_decoder_factory->IsSupportedDecoder(format);
300 }));
301 EXPECT_CALL(*mock_decoder_factory, MakeAudioDecoderMock(_, _, _))
302 .Times(AtLeast(2))
303 .WillRepeatedly(
304 Invoke([real_decoder_factory](
305 const webrtc::SdpAudioFormat& format,
306 absl::optional<webrtc::AudioCodecPairId> codec_pair_id,
307 std::unique_ptr<webrtc::AudioDecoder>* return_value) {
308 auto real_decoder =
309 real_decoder_factory->MakeAudioDecoder(format, codec_pair_id);
310 *return_value =
311 real_decoder
312 ? CreateForwardingMockDecoder(std::move(real_decoder))
313 : nullptr;
314 }));
315 return mock_decoder_factory;
318 struct AudioEncoderUnicornSparklesRainbow {
319 using Config = webrtc::AudioEncoderL16::Config;
320 static absl::optional<Config> SdpToConfig(webrtc::SdpAudioFormat format) {
321 if (absl::EqualsIgnoreCase(format.name, "UnicornSparklesRainbow")) {
322 const webrtc::SdpAudioFormat::Parameters expected_params = {
323 {"num_horns", "1"}};
324 EXPECT_EQ(expected_params, format.parameters);
325 format.parameters.clear();
326 format.name = "L16";
327 return webrtc::AudioEncoderL16::SdpToConfig(format);
328 } else {
329 return absl::nullopt;
332 static void AppendSupportedEncoders(
333 std::vector<webrtc::AudioCodecSpec>* specs) {
334 std::vector<webrtc::AudioCodecSpec> new_specs;
335 webrtc::AudioEncoderL16::AppendSupportedEncoders(&new_specs);
336 for (auto& spec : new_specs) {
337 spec.format.name = "UnicornSparklesRainbow";
338 EXPECT_TRUE(spec.format.parameters.empty());
339 spec.format.parameters.emplace("num_horns", "1");
340 specs->push_back(spec);
343 static webrtc::AudioCodecInfo QueryAudioEncoder(const Config& config) {
344 return webrtc::AudioEncoderL16::QueryAudioEncoder(config);
346 static std::unique_ptr<webrtc::AudioEncoder> MakeAudioEncoder(
347 const Config& config,
348 int payload_type,
349 absl::optional<webrtc::AudioCodecPairId> codec_pair_id = absl::nullopt) {
350 return webrtc::AudioEncoderL16::MakeAudioEncoder(config, payload_type,
351 codec_pair_id);
355 struct AudioDecoderUnicornSparklesRainbow {
356 using Config = webrtc::AudioDecoderL16::Config;
357 static absl::optional<Config> SdpToConfig(webrtc::SdpAudioFormat format) {
358 if (absl::EqualsIgnoreCase(format.name, "UnicornSparklesRainbow")) {
359 const webrtc::SdpAudioFormat::Parameters expected_params = {
360 {"num_horns", "1"}};
361 EXPECT_EQ(expected_params, format.parameters);
362 format.parameters.clear();
363 format.name = "L16";
364 return webrtc::AudioDecoderL16::SdpToConfig(format);
365 } else {
366 return absl::nullopt;
369 static void AppendSupportedDecoders(
370 std::vector<webrtc::AudioCodecSpec>* specs) {
371 std::vector<webrtc::AudioCodecSpec> new_specs;
372 webrtc::AudioDecoderL16::AppendSupportedDecoders(&new_specs);
373 for (auto& spec : new_specs) {
374 spec.format.name = "UnicornSparklesRainbow";
375 EXPECT_TRUE(spec.format.parameters.empty());
376 spec.format.parameters.emplace("num_horns", "1");
377 specs->push_back(spec);
380 static std::unique_ptr<webrtc::AudioDecoder> MakeAudioDecoder(
381 const Config& config,
382 absl::optional<webrtc::AudioCodecPairId> codec_pair_id = absl::nullopt) {
383 return webrtc::AudioDecoderL16::MakeAudioDecoder(config, codec_pair_id);
387 } // namespace
389 TEST_P(PeerConnectionEndToEndTest, Call) {
390 rtc::scoped_refptr<webrtc::AudioDecoderFactory> real_decoder_factory =
391 webrtc::CreateOpusAudioDecoderFactory();
392 CreatePcs(webrtc::CreateOpusAudioEncoderFactory(),
393 CreateForwardingMockDecoderFactory(real_decoder_factory.get()));
394 GetAndAddUserMedia();
395 Negotiate();
396 WaitForCallEstablished();
399 #if defined(IS_FUCHSIA)
400 TEST_P(PeerConnectionEndToEndTest, CallWithSdesKeyNegotiation) {
401 config_.enable_dtls_srtp = false;
402 CreatePcs(webrtc::CreateOpusAudioEncoderFactory(),
403 webrtc::CreateOpusAudioDecoderFactory());
404 GetAndAddUserMedia();
405 Negotiate();
406 WaitForCallEstablished();
408 #endif
410 TEST_P(PeerConnectionEndToEndTest, CallWithCustomCodec) {
411 class IdLoggingAudioEncoderFactory : public webrtc::AudioEncoderFactory {
412 public:
413 IdLoggingAudioEncoderFactory(
414 rtc::scoped_refptr<AudioEncoderFactory> real_factory,
415 std::vector<webrtc::AudioCodecPairId>* const codec_ids)
416 : fact_(real_factory), codec_ids_(codec_ids) {}
417 std::vector<webrtc::AudioCodecSpec> GetSupportedEncoders() override {
418 return fact_->GetSupportedEncoders();
420 absl::optional<webrtc::AudioCodecInfo> QueryAudioEncoder(
421 const webrtc::SdpAudioFormat& format) override {
422 return fact_->QueryAudioEncoder(format);
424 std::unique_ptr<webrtc::AudioEncoder> MakeAudioEncoder(
425 int payload_type,
426 const webrtc::SdpAudioFormat& format,
427 absl::optional<webrtc::AudioCodecPairId> codec_pair_id) override {
428 EXPECT_TRUE(codec_pair_id.has_value());
429 codec_ids_->push_back(*codec_pair_id);
430 return fact_->MakeAudioEncoder(payload_type, format, codec_pair_id);
433 private:
434 const rtc::scoped_refptr<webrtc::AudioEncoderFactory> fact_;
435 std::vector<webrtc::AudioCodecPairId>* const codec_ids_;
438 class IdLoggingAudioDecoderFactory : public webrtc::AudioDecoderFactory {
439 public:
440 IdLoggingAudioDecoderFactory(
441 rtc::scoped_refptr<AudioDecoderFactory> real_factory,
442 std::vector<webrtc::AudioCodecPairId>* const codec_ids)
443 : fact_(real_factory), codec_ids_(codec_ids) {}
444 std::vector<webrtc::AudioCodecSpec> GetSupportedDecoders() override {
445 return fact_->GetSupportedDecoders();
447 bool IsSupportedDecoder(const webrtc::SdpAudioFormat& format) override {
448 return fact_->IsSupportedDecoder(format);
450 std::unique_ptr<webrtc::AudioDecoder> MakeAudioDecoder(
451 const webrtc::SdpAudioFormat& format,
452 absl::optional<webrtc::AudioCodecPairId> codec_pair_id) override {
453 EXPECT_TRUE(codec_pair_id.has_value());
454 codec_ids_->push_back(*codec_pair_id);
455 return fact_->MakeAudioDecoder(format, codec_pair_id);
458 private:
459 const rtc::scoped_refptr<webrtc::AudioDecoderFactory> fact_;
460 std::vector<webrtc::AudioCodecPairId>* const codec_ids_;
463 std::vector<webrtc::AudioCodecPairId> encoder_id1, encoder_id2, decoder_id1,
464 decoder_id2;
465 CreatePcs(rtc::make_ref_counted<IdLoggingAudioEncoderFactory>(
466 webrtc::CreateAudioEncoderFactory<
467 AudioEncoderUnicornSparklesRainbow>(),
468 &encoder_id1),
469 rtc::make_ref_counted<IdLoggingAudioDecoderFactory>(
470 webrtc::CreateAudioDecoderFactory<
471 AudioDecoderUnicornSparklesRainbow>(),
472 &decoder_id1),
473 rtc::make_ref_counted<IdLoggingAudioEncoderFactory>(
474 webrtc::CreateAudioEncoderFactory<
475 AudioEncoderUnicornSparklesRainbow>(),
476 &encoder_id2),
477 rtc::make_ref_counted<IdLoggingAudioDecoderFactory>(
478 webrtc::CreateAudioDecoderFactory<
479 AudioDecoderUnicornSparklesRainbow>(),
480 &decoder_id2));
481 GetAndAddUserMedia();
482 Negotiate();
483 WaitForCallEstablished();
485 // Each codec factory has been used to create one codec. The first pair got
486 // the same ID because they were passed to the same PeerConnectionFactory,
487 // and the second pair got the same ID---but these two IDs are not equal,
488 // because each PeerConnectionFactory has its own ID.
489 EXPECT_EQ(1U, encoder_id1.size());
490 EXPECT_EQ(1U, encoder_id2.size());
491 EXPECT_EQ(encoder_id1, decoder_id1);
492 EXPECT_EQ(encoder_id2, decoder_id2);
493 EXPECT_NE(encoder_id1, encoder_id2);
496 #ifdef WEBRTC_HAVE_SCTP
497 // Verifies that a DataChannel created before the negotiation can transition to
498 // "OPEN" and transfer data.
499 TEST_P(PeerConnectionEndToEndTest, CreateDataChannelBeforeNegotiate) {
500 CreatePcs(webrtc::MockAudioEncoderFactory::CreateEmptyFactory(),
501 webrtc::MockAudioDecoderFactory::CreateEmptyFactory());
503 webrtc::DataChannelInit init;
504 rtc::scoped_refptr<DataChannelInterface> caller_dc(
505 caller_->CreateDataChannel("data", init));
506 rtc::scoped_refptr<DataChannelInterface> callee_dc(
507 callee_->CreateDataChannel("data", init));
509 Negotiate();
510 WaitForConnection();
512 WaitForDataChannelsToOpen(caller_dc.get(), callee_signaled_data_channels_, 0);
513 WaitForDataChannelsToOpen(callee_dc.get(), caller_signaled_data_channels_, 0);
515 TestDataChannelSendAndReceive(caller_dc.get(),
516 callee_signaled_data_channels_[0].get());
517 TestDataChannelSendAndReceive(callee_dc.get(),
518 caller_signaled_data_channels_[0].get());
520 CloseDataChannels(caller_dc.get(), callee_signaled_data_channels_, 0);
521 CloseDataChannels(callee_dc.get(), caller_signaled_data_channels_, 0);
524 // Verifies that a DataChannel created after the negotiation can transition to
525 // "OPEN" and transfer data.
526 TEST_P(PeerConnectionEndToEndTest, CreateDataChannelAfterNegotiate) {
527 CreatePcs(webrtc::MockAudioEncoderFactory::CreateEmptyFactory(),
528 webrtc::MockAudioDecoderFactory::CreateEmptyFactory());
530 webrtc::DataChannelInit init;
532 // This DataChannel is for creating the data content in the negotiation.
533 rtc::scoped_refptr<DataChannelInterface> dummy(
534 caller_->CreateDataChannel("data", init));
535 Negotiate();
536 WaitForConnection();
538 // Wait for the data channel created pre-negotiation to be opened.
539 WaitForDataChannelsToOpen(dummy.get(), callee_signaled_data_channels_, 0);
541 // Create new DataChannels after the negotiation and verify their states.
542 rtc::scoped_refptr<DataChannelInterface> caller_dc(
543 caller_->CreateDataChannel("hello", init));
544 rtc::scoped_refptr<DataChannelInterface> callee_dc(
545 callee_->CreateDataChannel("hello", init));
547 WaitForDataChannelsToOpen(caller_dc.get(), callee_signaled_data_channels_, 1);
548 WaitForDataChannelsToOpen(callee_dc.get(), caller_signaled_data_channels_, 0);
550 TestDataChannelSendAndReceive(caller_dc.get(),
551 callee_signaled_data_channels_[1].get());
552 TestDataChannelSendAndReceive(callee_dc.get(),
553 caller_signaled_data_channels_[0].get());
555 CloseDataChannels(caller_dc.get(), callee_signaled_data_channels_, 1);
556 CloseDataChannels(callee_dc.get(), caller_signaled_data_channels_, 0);
559 // Verifies that a DataChannel created can transfer large messages.
560 TEST_P(PeerConnectionEndToEndTest, CreateDataChannelLargeTransfer) {
561 CreatePcs(webrtc::MockAudioEncoderFactory::CreateEmptyFactory(),
562 webrtc::MockAudioDecoderFactory::CreateEmptyFactory());
564 webrtc::DataChannelInit init;
566 // This DataChannel is for creating the data content in the negotiation.
567 rtc::scoped_refptr<DataChannelInterface> dummy(
568 caller_->CreateDataChannel("data", init));
569 Negotiate();
570 WaitForConnection();
572 // Wait for the data channel created pre-negotiation to be opened.
573 WaitForDataChannelsToOpen(dummy.get(), callee_signaled_data_channels_, 0);
575 // Create new DataChannels after the negotiation and verify their states.
576 rtc::scoped_refptr<DataChannelInterface> caller_dc(
577 caller_->CreateDataChannel("hello", init));
578 rtc::scoped_refptr<DataChannelInterface> callee_dc(
579 callee_->CreateDataChannel("hello", init));
581 WaitForDataChannelsToOpen(caller_dc.get(), callee_signaled_data_channels_, 1);
582 WaitForDataChannelsToOpen(callee_dc.get(), caller_signaled_data_channels_, 0);
584 TestDataChannelSendAndReceive(
585 caller_dc.get(), callee_signaled_data_channels_[1].get(), 256 * 1024);
586 TestDataChannelSendAndReceive(
587 callee_dc.get(), caller_signaled_data_channels_[0].get(), 256 * 1024);
589 CloseDataChannels(caller_dc.get(), callee_signaled_data_channels_, 1);
590 CloseDataChannels(callee_dc.get(), caller_signaled_data_channels_, 0);
593 // Verifies that DataChannel IDs are even/odd based on the DTLS roles.
594 TEST_P(PeerConnectionEndToEndTest, DataChannelIdAssignment) {
595 CreatePcs(webrtc::MockAudioEncoderFactory::CreateEmptyFactory(),
596 webrtc::MockAudioDecoderFactory::CreateEmptyFactory());
598 webrtc::DataChannelInit init;
599 rtc::scoped_refptr<DataChannelInterface> caller_dc_1(
600 caller_->CreateDataChannel("data", init));
601 rtc::scoped_refptr<DataChannelInterface> callee_dc_1(
602 callee_->CreateDataChannel("data", init));
604 Negotiate();
605 WaitForConnection();
607 EXPECT_EQ(1, caller_dc_1->id() % 2);
608 EXPECT_EQ(0, callee_dc_1->id() % 2);
610 rtc::scoped_refptr<DataChannelInterface> caller_dc_2(
611 caller_->CreateDataChannel("data", init));
612 rtc::scoped_refptr<DataChannelInterface> callee_dc_2(
613 callee_->CreateDataChannel("data", init));
615 EXPECT_EQ(1, caller_dc_2->id() % 2);
616 EXPECT_EQ(0, callee_dc_2->id() % 2);
619 // Verifies that the message is received by the right remote DataChannel when
620 // there are multiple DataChannels.
621 TEST_P(PeerConnectionEndToEndTest,
622 MessageTransferBetweenTwoPairsOfDataChannels) {
623 CreatePcs(webrtc::MockAudioEncoderFactory::CreateEmptyFactory(),
624 webrtc::MockAudioDecoderFactory::CreateEmptyFactory());
626 webrtc::DataChannelInit init;
628 rtc::scoped_refptr<DataChannelInterface> caller_dc_1(
629 caller_->CreateDataChannel("data", init));
630 rtc::scoped_refptr<DataChannelInterface> caller_dc_2(
631 caller_->CreateDataChannel("data", init));
633 Negotiate();
634 WaitForConnection();
635 WaitForDataChannelsToOpen(caller_dc_1.get(), callee_signaled_data_channels_,
637 WaitForDataChannelsToOpen(caller_dc_2.get(), callee_signaled_data_channels_,
640 std::unique_ptr<webrtc::MockDataChannelObserver> dc_1_observer(
641 new webrtc::MockDataChannelObserver(
642 callee_signaled_data_channels_[0].get()));
644 std::unique_ptr<webrtc::MockDataChannelObserver> dc_2_observer(
645 new webrtc::MockDataChannelObserver(
646 callee_signaled_data_channels_[1].get()));
648 const std::string message_1 = "hello 1";
649 const std::string message_2 = "hello 2";
651 caller_dc_1->Send(webrtc::DataBuffer(message_1));
652 EXPECT_EQ_WAIT(message_1, dc_1_observer->last_message(), kMaxWait);
654 caller_dc_2->Send(webrtc::DataBuffer(message_2));
655 EXPECT_EQ_WAIT(message_2, dc_2_observer->last_message(), kMaxWait);
657 EXPECT_EQ(1U, dc_1_observer->received_message_count());
658 EXPECT_EQ(1U, dc_2_observer->received_message_count());
661 // Verifies that a DataChannel added from an OPEN message functions after
662 // a channel has been previously closed (webrtc issue 3778).
663 // This previously failed because the new channel re-used the ID of the closed
664 // channel, and the closed channel was incorrectly still assigned to the ID.
665 TEST_P(PeerConnectionEndToEndTest,
666 DataChannelFromOpenWorksAfterPreviousChannelClosed) {
667 CreatePcs(webrtc::MockAudioEncoderFactory::CreateEmptyFactory(),
668 webrtc::MockAudioDecoderFactory::CreateEmptyFactory());
670 webrtc::DataChannelInit init;
671 rtc::scoped_refptr<DataChannelInterface> caller_dc(
672 caller_->CreateDataChannel("data", init));
674 Negotiate();
675 WaitForConnection();
677 WaitForDataChannelsToOpen(caller_dc.get(), callee_signaled_data_channels_, 0);
678 int first_channel_id = caller_dc->id();
679 // Wait for the local side to say it's closed, but not the remote side.
680 // Previously, the channel on which Close is called reported being closed
681 // prematurely, and this caused issues; see bugs.webrtc.org/4453.
682 caller_dc->Close();
683 EXPECT_EQ_WAIT(DataChannelInterface::kClosed, caller_dc->state(), kMaxWait);
685 // Create a new channel and ensure it works after closing the previous one.
686 caller_dc = caller_->CreateDataChannel("data2", init);
687 WaitForDataChannelsToOpen(caller_dc.get(), callee_signaled_data_channels_, 1);
688 // Since the second channel was created after the first finished closing, it
689 // should be able to re-use the first one's ID.
690 EXPECT_EQ(first_channel_id, caller_dc->id());
691 TestDataChannelSendAndReceive(caller_dc.get(),
692 callee_signaled_data_channels_[1].get());
694 CloseDataChannels(caller_dc.get(), callee_signaled_data_channels_, 1);
697 // This tests that if a data channel is closed remotely while not referenced
698 // by the application (meaning only the PeerConnection contributes to its
699 // reference count), no memory access violation will occur.
700 // See: https://code.google.com/p/chromium/issues/detail?id=565048
701 TEST_P(PeerConnectionEndToEndTest, CloseDataChannelRemotelyWhileNotReferenced) {
702 CreatePcs(webrtc::MockAudioEncoderFactory::CreateEmptyFactory(),
703 webrtc::MockAudioDecoderFactory::CreateEmptyFactory());
705 webrtc::DataChannelInit init;
706 rtc::scoped_refptr<DataChannelInterface> caller_dc(
707 caller_->CreateDataChannel("data", init));
709 Negotiate();
710 WaitForConnection();
712 WaitForDataChannelsToOpen(caller_dc.get(), callee_signaled_data_channels_, 0);
713 // This removes the reference to the remote data channel that we hold.
714 callee_signaled_data_channels_.clear();
715 caller_dc->Close();
716 EXPECT_EQ_WAIT(DataChannelInterface::kClosed, caller_dc->state(), kMaxWait);
718 // Wait for a bit longer so the remote data channel will receive the
719 // close message and be destroyed.
720 rtc::Thread::Current()->ProcessMessages(100);
723 // Test behavior of creating too many datachannels.
724 TEST_P(PeerConnectionEndToEndTest, TooManyDataChannelsOpenedBeforeConnecting) {
725 CreatePcs(webrtc::MockAudioEncoderFactory::CreateEmptyFactory(),
726 webrtc::MockAudioDecoderFactory::CreateEmptyFactory());
728 webrtc::DataChannelInit init;
729 std::vector<rtc::scoped_refptr<DataChannelInterface>> channels;
730 for (int i = 0; i <= cricket::kMaxSctpStreams / 2; i++) {
731 rtc::scoped_refptr<DataChannelInterface> caller_dc(
732 caller_->CreateDataChannel("data", init));
733 channels.push_back(std::move(caller_dc));
735 Negotiate();
736 WaitForConnection();
737 EXPECT_EQ_WAIT(callee_signaled_data_channels_.size(),
738 static_cast<size_t>(cricket::kMaxSctpStreams / 2), kMaxWait);
739 EXPECT_EQ(DataChannelInterface::kOpen,
740 channels[(cricket::kMaxSctpStreams / 2) - 1]->state());
741 EXPECT_EQ(DataChannelInterface::kClosed,
742 channels[cricket::kMaxSctpStreams / 2]->state());
745 #endif // WEBRTC_HAVE_SCTP
747 TEST_P(PeerConnectionEndToEndTest, CanRestartIce) {
748 rtc::scoped_refptr<webrtc::AudioDecoderFactory> real_decoder_factory =
749 webrtc::CreateOpusAudioDecoderFactory();
750 CreatePcs(webrtc::CreateOpusAudioEncoderFactory(),
751 CreateForwardingMockDecoderFactory(real_decoder_factory.get()));
752 GetAndAddUserMedia();
753 Negotiate();
754 WaitForCallEstablished();
755 // Cause ICE restart to be requested.
756 auto config = caller_->pc()->GetConfiguration();
757 ASSERT_NE(PeerConnectionInterface::kRelay, config.type);
758 config.type = PeerConnectionInterface::kRelay;
759 ASSERT_TRUE(caller_->pc()->SetConfiguration(config).ok());
760 // When solving https://crbug.com/webrtc/10504, all we need to check
761 // is that we do not crash. We should also be testing that restart happens.
764 INSTANTIATE_TEST_SUITE_P(PeerConnectionEndToEndTest,
765 PeerConnectionEndToEndTest,
766 Values(SdpSemantics::kPlanB_DEPRECATED,
767 SdpSemantics::kUnifiedPlan));