Skip to content
This repository was archived by the owner on Oct 25, 2024. It is now read-only.

Commit 16e7b51

Browse files
timnaCommit Bot
authored andcommitted
Unit test around ProcessThread usage
Bug: webrtc:11989 Change-Id: Ic631e80c4e5db6e3558ff714cc105e5a4874f744 Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/186421 Commit-Queue: Tim Na <[email protected]> Reviewed-by: Mirko Bonadei <[email protected]> Reviewed-by: Per Åhgren <[email protected]> Cr-Commit-Position: refs/heads/master@{#32331}
1 parent 4354686 commit 16e7b51

File tree

4 files changed

+48
-4
lines changed

4 files changed

+48
-4
lines changed

audio/voip/test/BUILD.gn

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ if (rtc_include_tests) {
1919
"../../../api/task_queue:default_task_queue_factory",
2020
"../../../modules/audio_device:mock_audio_device",
2121
"../../../modules/audio_processing:mocks",
22+
"../../../modules/utility:mock_process_thread",
2223
"../../../test:audio_codec_mocks",
2324
"../../../test:mock_transport",
2425
"../../../test:test_support",

audio/voip/test/voip_core_unittest.cc

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "api/task_queue/default_task_queue_factory.h"
1515
#include "modules/audio_device/include/mock_audio_device.h"
1616
#include "modules/audio_processing/include/mock_audio_processing.h"
17+
#include "modules/utility/include/mock/mock_process_thread.h"
1718
#include "test/gtest.h"
1819
#include "test/mock_transport.h"
1920

@@ -40,15 +41,20 @@ class VoipCoreTest : public ::testing::Test {
4041
rtc::scoped_refptr<AudioProcessing> audio_processing =
4142
new rtc::RefCountedObject<test::MockAudioProcessing>();
4243

44+
auto process_thread = std::make_unique<NiceMock<MockProcessThread>>();
45+
// Hold the pointer to use for testing.
46+
process_thread_ = process_thread.get();
47+
4348
voip_core_ = std::make_unique<VoipCore>();
4449
voip_core_->Init(std::move(encoder_factory), std::move(decoder_factory),
4550
CreateDefaultTaskQueueFactory(), audio_device_,
46-
std::move(audio_processing));
51+
std::move(audio_processing), std::move(process_thread));
4752
}
4853

4954
std::unique_ptr<VoipCore> voip_core_;
5055
NiceMock<MockTransport> transport_;
5156
rtc::scoped_refptr<test::MockAudioDeviceModule> audio_device_;
57+
NiceMock<MockProcessThread>* process_thread_;
5258
};
5359

5460
// Validate expected API calls that involves with VoipCore. Some verification is
@@ -176,5 +182,34 @@ TEST_F(VoipCoreTest, StopSendAndPlayoutWithoutStarting) {
176182
voip_core_->ReleaseChannel(*channel);
177183
}
178184

185+
// This tests correctness on ProcessThread usage where we expect the first/last
186+
// channel creation/release triggers its Start/Stop method once only.
187+
TEST_F(VoipCoreTest, TestProcessThreadOperation) {
188+
EXPECT_CALL(*process_thread_, Start);
189+
EXPECT_CALL(*process_thread_, RegisterModule).Times(2);
190+
191+
auto channel_one = voip_core_->CreateChannel(&transport_, 0xdeadc0de);
192+
auto channel_two = voip_core_->CreateChannel(&transport_, 0xdeadbeef);
193+
EXPECT_TRUE(channel_one);
194+
EXPECT_TRUE(channel_two);
195+
196+
EXPECT_CALL(*process_thread_, Stop);
197+
EXPECT_CALL(*process_thread_, DeRegisterModule).Times(2);
198+
199+
voip_core_->ReleaseChannel(*channel_one);
200+
voip_core_->ReleaseChannel(*channel_two);
201+
202+
EXPECT_CALL(*process_thread_, Start);
203+
EXPECT_CALL(*process_thread_, RegisterModule);
204+
205+
auto channel_three = voip_core_->CreateChannel(&transport_, absl::nullopt);
206+
EXPECT_TRUE(channel_three);
207+
208+
EXPECT_CALL(*process_thread_, Stop);
209+
EXPECT_CALL(*process_thread_, DeRegisterModule);
210+
211+
voip_core_->ReleaseChannel(*channel_three);
212+
}
213+
179214
} // namespace
180215
} // namespace webrtc

audio/voip/voip_core.cc

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,18 @@ bool VoipCore::Init(rtc::scoped_refptr<AudioEncoderFactory> encoder_factory,
4141
rtc::scoped_refptr<AudioDecoderFactory> decoder_factory,
4242
std::unique_ptr<TaskQueueFactory> task_queue_factory,
4343
rtc::scoped_refptr<AudioDeviceModule> audio_device_module,
44-
rtc::scoped_refptr<AudioProcessing> audio_processing) {
44+
rtc::scoped_refptr<AudioProcessing> audio_processing,
45+
std::unique_ptr<ProcessThread> process_thread) {
4546
encoder_factory_ = std::move(encoder_factory);
4647
decoder_factory_ = std::move(decoder_factory);
4748
task_queue_factory_ = std::move(task_queue_factory);
4849
audio_device_module_ = std::move(audio_device_module);
4950
audio_processing_ = std::move(audio_processing);
51+
process_thread_ = std::move(process_thread);
5052

51-
process_thread_ = ProcessThread::Create("ModuleProcessThread");
53+
if (!process_thread_) {
54+
process_thread_ = ProcessThread::Create("ModuleProcessThread");
55+
}
5256
audio_mixer_ = AudioMixerImpl::Create();
5357

5458
// AudioTransportImpl depends on audio mixer and audio processing instances.

audio/voip/voip_core.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,16 @@ class VoipCore : public VoipEngine,
5454
// Initialize VoipCore components with provided arguments.
5555
// Returns false only when |audio_device_module| fails to initialize which
5656
// would presumably render further processing useless.
57+
// ProcessThread implementation can be injected by |process_thread|
58+
// (mainly for testing purpose) and when set to nullptr, default
59+
// implementation will be used.
5760
// TODO([email protected]): Need to report audio device errors to user layer.
5861
bool Init(rtc::scoped_refptr<AudioEncoderFactory> encoder_factory,
5962
rtc::scoped_refptr<AudioDecoderFactory> decoder_factory,
6063
std::unique_ptr<TaskQueueFactory> task_queue_factory,
6164
rtc::scoped_refptr<AudioDeviceModule> audio_device_module,
62-
rtc::scoped_refptr<AudioProcessing> audio_processing);
65+
rtc::scoped_refptr<AudioProcessing> audio_processing,
66+
std::unique_ptr<ProcessThread> process_thread = nullptr);
6367

6468
// Implements VoipEngine interfaces.
6569
VoipBase& Base() override { return *this; }

0 commit comments

Comments
 (0)