From 16e7b515ee50074461c128dbac4843bcfa960c22 Mon Sep 17 00:00:00 2001 From: Tim Na Date: Tue, 6 Oct 2020 09:24:33 -0700 Subject: [PATCH] Unit test around ProcessThread usage MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Bug: webrtc:11989 Change-Id: Ic631e80c4e5db6e3558ff714cc105e5a4874f744 Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/186421 Commit-Queue: Tim Na Reviewed-by: Mirko Bonadei Reviewed-by: Per Ã…hgren Cr-Commit-Position: refs/heads/master@{#32331} --- audio/voip/test/BUILD.gn | 1 + audio/voip/test/voip_core_unittest.cc | 37 ++++++++++++++++++++++++++- audio/voip/voip_core.cc | 8 ++++-- audio/voip/voip_core.h | 6 ++++- 4 files changed, 48 insertions(+), 4 deletions(-) diff --git a/audio/voip/test/BUILD.gn b/audio/voip/test/BUILD.gn index d698b3321d..ade10764f2 100644 --- a/audio/voip/test/BUILD.gn +++ b/audio/voip/test/BUILD.gn @@ -19,6 +19,7 @@ if (rtc_include_tests) { "../../../api/task_queue:default_task_queue_factory", "../../../modules/audio_device:mock_audio_device", "../../../modules/audio_processing:mocks", + "../../../modules/utility:mock_process_thread", "../../../test:audio_codec_mocks", "../../../test:mock_transport", "../../../test:test_support", diff --git a/audio/voip/test/voip_core_unittest.cc b/audio/voip/test/voip_core_unittest.cc index 713f7f65b2..930d10baa8 100644 --- a/audio/voip/test/voip_core_unittest.cc +++ b/audio/voip/test/voip_core_unittest.cc @@ -14,6 +14,7 @@ #include "api/task_queue/default_task_queue_factory.h" #include "modules/audio_device/include/mock_audio_device.h" #include "modules/audio_processing/include/mock_audio_processing.h" +#include "modules/utility/include/mock/mock_process_thread.h" #include "test/gtest.h" #include "test/mock_transport.h" @@ -40,15 +41,20 @@ class VoipCoreTest : public ::testing::Test { rtc::scoped_refptr audio_processing = new rtc::RefCountedObject(); + auto process_thread = std::make_unique>(); + // Hold the pointer to use for testing. + process_thread_ = process_thread.get(); + voip_core_ = std::make_unique(); voip_core_->Init(std::move(encoder_factory), std::move(decoder_factory), CreateDefaultTaskQueueFactory(), audio_device_, - std::move(audio_processing)); + std::move(audio_processing), std::move(process_thread)); } std::unique_ptr voip_core_; NiceMock transport_; rtc::scoped_refptr audio_device_; + NiceMock* process_thread_; }; // Validate expected API calls that involves with VoipCore. Some verification is @@ -176,5 +182,34 @@ TEST_F(VoipCoreTest, StopSendAndPlayoutWithoutStarting) { voip_core_->ReleaseChannel(*channel); } +// This tests correctness on ProcessThread usage where we expect the first/last +// channel creation/release triggers its Start/Stop method once only. +TEST_F(VoipCoreTest, TestProcessThreadOperation) { + EXPECT_CALL(*process_thread_, Start); + EXPECT_CALL(*process_thread_, RegisterModule).Times(2); + + auto channel_one = voip_core_->CreateChannel(&transport_, 0xdeadc0de); + auto channel_two = voip_core_->CreateChannel(&transport_, 0xdeadbeef); + EXPECT_TRUE(channel_one); + EXPECT_TRUE(channel_two); + + EXPECT_CALL(*process_thread_, Stop); + EXPECT_CALL(*process_thread_, DeRegisterModule).Times(2); + + voip_core_->ReleaseChannel(*channel_one); + voip_core_->ReleaseChannel(*channel_two); + + EXPECT_CALL(*process_thread_, Start); + EXPECT_CALL(*process_thread_, RegisterModule); + + auto channel_three = voip_core_->CreateChannel(&transport_, absl::nullopt); + EXPECT_TRUE(channel_three); + + EXPECT_CALL(*process_thread_, Stop); + EXPECT_CALL(*process_thread_, DeRegisterModule); + + voip_core_->ReleaseChannel(*channel_three); +} + } // namespace } // namespace webrtc diff --git a/audio/voip/voip_core.cc b/audio/voip/voip_core.cc index 551d941861..2d752aca58 100644 --- a/audio/voip/voip_core.cc +++ b/audio/voip/voip_core.cc @@ -41,14 +41,18 @@ bool VoipCore::Init(rtc::scoped_refptr encoder_factory, rtc::scoped_refptr decoder_factory, std::unique_ptr task_queue_factory, rtc::scoped_refptr audio_device_module, - rtc::scoped_refptr audio_processing) { + rtc::scoped_refptr audio_processing, + std::unique_ptr process_thread) { encoder_factory_ = std::move(encoder_factory); decoder_factory_ = std::move(decoder_factory); task_queue_factory_ = std::move(task_queue_factory); audio_device_module_ = std::move(audio_device_module); audio_processing_ = std::move(audio_processing); + process_thread_ = std::move(process_thread); - process_thread_ = ProcessThread::Create("ModuleProcessThread"); + if (!process_thread_) { + process_thread_ = ProcessThread::Create("ModuleProcessThread"); + } audio_mixer_ = AudioMixerImpl::Create(); // AudioTransportImpl depends on audio mixer and audio processing instances. diff --git a/audio/voip/voip_core.h b/audio/voip/voip_core.h index 6654ff7d95..11ac6166f0 100644 --- a/audio/voip/voip_core.h +++ b/audio/voip/voip_core.h @@ -54,12 +54,16 @@ class VoipCore : public VoipEngine, // Initialize VoipCore components with provided arguments. // Returns false only when |audio_device_module| fails to initialize which // would presumably render further processing useless. + // ProcessThread implementation can be injected by |process_thread| + // (mainly for testing purpose) and when set to nullptr, default + // implementation will be used. // TODO(natim@webrtc.org): Need to report audio device errors to user layer. bool Init(rtc::scoped_refptr encoder_factory, rtc::scoped_refptr decoder_factory, std::unique_ptr task_queue_factory, rtc::scoped_refptr audio_device_module, - rtc::scoped_refptr audio_processing); + rtc::scoped_refptr audio_processing, + std::unique_ptr process_thread = nullptr); // Implements VoipEngine interfaces. VoipBase& Base() override { return *this; }