Use make_ref_counted in AudioProcessingBuilder

Bug: webrtc:12701
Change-Id: I51ca5a54f812a1620ee2e6605c9ff67b92e2a5f8
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/224547
Reviewed-by: Ivo Creusen <ivoc@webrtc.org>
Reviewed-by: Sam Zackrisson <saza@webrtc.org>
Reviewed-by: Tommi <tommi@webrtc.org>
Commit-Queue: Niels Moller <nisse@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#34725}
This commit is contained in:
Niels Möller 2021-07-02 11:30:54 +02:00 committed by WebRTC LUCI CQ
parent 897bf9bff1
commit 4f776ac7de
15 changed files with 70 additions and 69 deletions

View File

@ -23,9 +23,9 @@ using ::testing::Exactly;
using ::testing::StrictMock;
namespace {
std::unique_ptr<webrtc::AudioProcessing> CreateAudioProcessing() {
rtc::scoped_refptr<webrtc::AudioProcessing> CreateAudioProcessing() {
webrtc::Config config;
std::unique_ptr<webrtc::AudioProcessing> apm(
rtc::scoped_refptr<webrtc::AudioProcessing> apm(
webrtc::AudioProcessingBuilderForTesting().Create(config));
RTC_DCHECK(apm);
return apm;

View File

@ -20,12 +20,13 @@ namespace webrtc {
AudioProcessingBuilder::AudioProcessingBuilder() = default;
AudioProcessingBuilder::~AudioProcessingBuilder() = default;
AudioProcessing* AudioProcessingBuilder::Create() {
rtc::scoped_refptr<AudioProcessing> AudioProcessingBuilder::Create() {
webrtc::Config config;
return Create(config);
}
AudioProcessing* AudioProcessingBuilder::Create(const webrtc::Config& config) {
rtc::scoped_refptr<AudioProcessing> AudioProcessingBuilder::Create(
const webrtc::Config& config) {
#ifdef WEBRTC_EXCLUDE_AUDIO_PROCESSING_MODULE
// Implementation returning a null pointer for using when the APM is excluded
@ -35,7 +36,7 @@ AudioProcessing* AudioProcessingBuilder::Create(const webrtc::Config& config) {
#else
// Standard implementation.
return new rtc::RefCountedObject<AudioProcessingImpl>(
return rtc::make_ref_counted<AudioProcessingImpl>(
config, std::move(capture_post_processing_),
std::move(render_pre_processing_), std::move(echo_control_factory_),
std::move(echo_detector_), std::move(capture_analyzer_));

View File

@ -428,7 +428,7 @@ class AudioProcessingImplLockTest
// Thread related variables.
mutable RandomGenerator rand_gen_;
std::unique_ptr<AudioProcessing> apm_;
rtc::scoped_refptr<AudioProcessing> apm_;
TestConfig test_config_;
FrameCounters frame_counters_;
RenderProcessor render_thread_state_;

View File

@ -173,8 +173,8 @@ TEST(AudioProcessingImplTest, AudioParameterChangeTriggersInit) {
}
TEST(AudioProcessingImplTest, UpdateCapturePreGainRuntimeSetting) {
std::unique_ptr<AudioProcessing> apm(
AudioProcessingBuilderForTesting().Create());
rtc::scoped_refptr<AudioProcessing> apm =
AudioProcessingBuilderForTesting().Create();
webrtc::AudioProcessing::Config apm_config;
apm_config.pre_amplifier.enabled = true;
apm_config.pre_amplifier.fixed_gain_factor = 1.f;
@ -206,8 +206,8 @@ TEST(AudioProcessingImplTest, UpdateCapturePreGainRuntimeSetting) {
TEST(AudioProcessingImplTest,
LevelAdjustmentUpdateCapturePreGainRuntimeSetting) {
std::unique_ptr<AudioProcessing> apm(
AudioProcessingBuilderForTesting().Create());
rtc::scoped_refptr<AudioProcessing> apm =
AudioProcessingBuilderForTesting().Create();
webrtc::AudioProcessing::Config apm_config;
apm_config.capture_level_adjustment.enabled = true;
apm_config.capture_level_adjustment.pre_gain_factor = 1.f;
@ -239,8 +239,8 @@ TEST(AudioProcessingImplTest,
TEST(AudioProcessingImplTest,
LevelAdjustmentUpdateCapturePostGainRuntimeSetting) {
std::unique_ptr<AudioProcessing> apm(
AudioProcessingBuilderForTesting().Create());
rtc::scoped_refptr<AudioProcessing> apm =
AudioProcessingBuilderForTesting().Create();
webrtc::AudioProcessing::Config apm_config;
apm_config.capture_level_adjustment.enabled = true;
apm_config.capture_level_adjustment.post_gain_factor = 1.f;
@ -277,10 +277,10 @@ TEST(AudioProcessingImplTest, EchoControllerObservesSetCaptureUsageChange) {
const MockEchoControlFactory* echo_control_factory_ptr =
echo_control_factory.get();
std::unique_ptr<AudioProcessing> apm(
rtc::scoped_refptr<AudioProcessing> apm =
AudioProcessingBuilderForTesting()
.SetEchoControlFactory(std::move(echo_control_factory))
.Create());
.Create();
constexpr int16_t kAudioLevel = 10000;
constexpr int kSampleRateHz = 48000;
@ -359,10 +359,10 @@ TEST(AudioProcessingImplTest,
auto echo_control_factory = std::make_unique<MockEchoControlFactory>();
const auto* echo_control_factory_ptr = echo_control_factory.get();
std::unique_ptr<AudioProcessing> apm(
rtc::scoped_refptr<AudioProcessing> apm =
AudioProcessingBuilderForTesting()
.SetEchoControlFactory(std::move(echo_control_factory))
.Create());
.Create();
// Disable AGC.
webrtc::AudioProcessing::Config apm_config;
apm_config.gain_controller1.enabled = false;
@ -402,10 +402,10 @@ TEST(AudioProcessingImplTest,
auto echo_control_factory = std::make_unique<MockEchoControlFactory>();
const auto* echo_control_factory_ptr = echo_control_factory.get();
std::unique_ptr<AudioProcessing> apm(
rtc::scoped_refptr<AudioProcessing> apm =
AudioProcessingBuilderForTesting()
.SetEchoControlFactory(std::move(echo_control_factory))
.Create());
.Create();
// Disable AGC.
webrtc::AudioProcessing::Config apm_config;
apm_config.gain_controller1.enabled = false;
@ -445,10 +445,10 @@ TEST(AudioProcessingImplTest,
auto echo_control_factory = std::make_unique<MockEchoControlFactory>();
const auto* echo_control_factory_ptr = echo_control_factory.get();
std::unique_ptr<AudioProcessing> apm(
rtc::scoped_refptr<AudioProcessing> apm =
AudioProcessingBuilderForTesting()
.SetEchoControlFactory(std::move(echo_control_factory))
.Create());
.Create();
webrtc::AudioProcessing::Config apm_config;
// Enable AGC1.
apm_config.gain_controller1.enabled = true;
@ -491,10 +491,10 @@ TEST(AudioProcessingImplTest, EchoControllerObservesPlayoutVolumeChange) {
auto echo_control_factory = std::make_unique<MockEchoControlFactory>();
const auto* echo_control_factory_ptr = echo_control_factory.get();
std::unique_ptr<AudioProcessing> apm(
rtc::scoped_refptr<AudioProcessing> apm =
AudioProcessingBuilderForTesting()
.SetEchoControlFactory(std::move(echo_control_factory))
.Create());
.Create();
// Disable AGC.
webrtc::AudioProcessing::Config apm_config;
apm_config.gain_controller1.enabled = false;
@ -549,11 +549,11 @@ TEST(AudioProcessingImplTest, RenderPreProcessorBeforeEchoDetector) {
std::unique_ptr<CustomProcessing> test_render_pre_processor(
new TestRenderPreProcessor());
// Create APM injecting the test echo detector and render pre-processor.
std::unique_ptr<AudioProcessing> apm(
rtc::scoped_refptr<AudioProcessing> apm =
AudioProcessingBuilderForTesting()
.SetEchoDetector(test_echo_detector)
.SetRenderPreProcessing(std::move(test_render_pre_processor))
.Create());
.Create();
webrtc::AudioProcessing::Config apm_config;
apm_config.pre_amplifier.enabled = true;
apm_config.residual_echo_detector.enabled = true;

View File

@ -476,34 +476,34 @@ class CallSimulator : public ::testing::TestWithParam<SimulationConfig> {
int num_capture_channels = 1;
switch (simulation_config_.simulation_settings) {
case SettingsType::kDefaultApmMobile: {
apm_.reset(AudioProcessingBuilderForTesting().Create());
apm_ = AudioProcessingBuilderForTesting().Create();
ASSERT_TRUE(!!apm_);
set_default_mobile_apm_runtime_settings(apm_.get());
break;
}
case SettingsType::kDefaultApmDesktop: {
Config config;
apm_.reset(AudioProcessingBuilderForTesting().Create(config));
apm_ = AudioProcessingBuilderForTesting().Create(config);
ASSERT_TRUE(!!apm_);
set_default_desktop_apm_runtime_settings(apm_.get());
break;
}
case SettingsType::kAllSubmodulesTurnedOff: {
apm_.reset(AudioProcessingBuilderForTesting().Create());
apm_ = AudioProcessingBuilderForTesting().Create();
ASSERT_TRUE(!!apm_);
turn_off_default_apm_runtime_settings(apm_.get());
break;
}
case SettingsType::kDefaultApmDesktopWithoutDelayAgnostic: {
Config config;
apm_.reset(AudioProcessingBuilderForTesting().Create(config));
apm_ = AudioProcessingBuilderForTesting().Create(config);
ASSERT_TRUE(!!apm_);
set_default_desktop_apm_runtime_settings(apm_.get());
break;
}
case SettingsType::kDefaultApmDesktopWithoutExtendedFilter: {
Config config;
apm_.reset(AudioProcessingBuilderForTesting().Create(config));
apm_ = AudioProcessingBuilderForTesting().Create(config);
ASSERT_TRUE(!!apm_);
set_default_desktop_apm_runtime_settings(apm_.get());
break;
@ -544,7 +544,7 @@ class CallSimulator : public ::testing::TestWithParam<SimulationConfig> {
// Thread related variables.
Random rand_gen_;
std::unique_ptr<AudioProcessing> apm_;
rtc::scoped_refptr<AudioProcessing> apm_;
const SimulationConfig simulation_config_;
FrameCounters frame_counters_;
LockedFlag capture_call_checker_;

View File

@ -415,7 +415,7 @@ class ApmTest : public ::testing::Test {
const std::string output_path_;
const std::string ref_filename_;
std::unique_ptr<AudioProcessing> apm_;
rtc::scoped_refptr<AudioProcessing> apm_;
Int16FrameData frame_;
Int16FrameData revframe_;
std::unique_ptr<ChannelBuffer<float> > float_cb_;
@ -435,7 +435,7 @@ ApmTest::ApmTest()
far_file_(NULL),
near_file_(NULL),
out_file_(NULL) {
apm_.reset(AudioProcessingBuilderForTesting().Create());
apm_ = AudioProcessingBuilderForTesting().Create();
AudioProcessing::Config apm_config = apm_->GetConfig();
apm_config.gain_controller1.analog_gain_controller.enabled = false;
apm_config.pipeline.maximum_internal_processing_rate = 48000;
@ -1312,7 +1312,7 @@ TEST_F(ApmTest, NoProcessingWhenAllComponentsDisabledFloat) {
auto src_channels = &src[0];
auto dest_channels = &dest[0];
apm_.reset(AudioProcessingBuilderForTesting().Create());
apm_ = AudioProcessingBuilderForTesting().Create();
EXPECT_NOERR(apm_->ProcessStream(&src_channels, StreamConfig(sample_rate, 1),
StreamConfig(sample_rate, 1),
&dest_channels));
@ -1773,7 +1773,7 @@ TEST_F(ApmTest, Process) {
if (test->num_input_channels() != test->num_output_channels())
continue;
apm_.reset(AudioProcessingBuilderForTesting().Create());
apm_ = AudioProcessingBuilderForTesting().Create();
AudioProcessing::Config apm_config = apm_->GetConfig();
apm_config.gain_controller1.analog_gain_controller.enabled = false;
apm_->ApplyConfig(apm_config);
@ -1942,8 +1942,8 @@ TEST_F(ApmTest, NoErrorsWithKeyboardChannel) {
{AudioProcessing::kStereoAndKeyboard, AudioProcessing::kStereo},
};
std::unique_ptr<AudioProcessing> ap(
AudioProcessingBuilderForTesting().Create());
rtc::scoped_refptr<AudioProcessing> ap =
AudioProcessingBuilderForTesting().Create();
// Enable one component just to ensure some processing takes place.
AudioProcessing::Config config;
config.noise_suppression.enabled = true;
@ -2069,8 +2069,8 @@ class AudioProcessingTest
size_t num_reverse_input_channels,
size_t num_reverse_output_channels,
const std::string& output_file_prefix) {
std::unique_ptr<AudioProcessing> ap(
AudioProcessingBuilderForTesting().Create());
rtc::scoped_refptr<AudioProcessing> ap =
AudioProcessingBuilderForTesting().Create();
AudioProcessing::Config apm_config = ap->GetConfig();
apm_config.gain_controller1.analog_gain_controller.enabled = false;
ap->ApplyConfig(apm_config);
@ -2454,8 +2454,8 @@ void RunApmRateAndChannelTest(
rtc::ArrayView<const int> sample_rates_hz,
rtc::ArrayView<const int> render_channel_counts,
rtc::ArrayView<const int> capture_channel_counts) {
std::unique_ptr<AudioProcessing> apm(
AudioProcessingBuilderForTesting().Create());
rtc::scoped_refptr<AudioProcessing> apm =
AudioProcessingBuilderForTesting().Create();
webrtc::AudioProcessing::Config apm_config;
apm_config.echo_canceller.enabled = true;
apm->ApplyConfig(apm_config);
@ -2699,10 +2699,10 @@ TEST(ApmConfiguration, EchoControlInjection) {
audio.data.data());
}
std::unique_ptr<AudioProcessing> CreateApm(bool mobile_aec) {
rtc::scoped_refptr<AudioProcessing> CreateApm(bool mobile_aec) {
Config old_config;
std::unique_ptr<AudioProcessing> apm(
AudioProcessingBuilderForTesting().Create(old_config));
rtc::scoped_refptr<AudioProcessing> apm =
AudioProcessingBuilderForTesting().Create(old_config);
if (!apm) {
return apm;
}
@ -2737,7 +2737,7 @@ std::unique_ptr<AudioProcessing> CreateApm(bool mobile_aec) {
TEST(MAYBE_ApmStatistics, AECEnabledTest) {
// Set up APM with AEC3 and process some audio.
std::unique_ptr<AudioProcessing> apm = CreateApm(false);
rtc::scoped_refptr<AudioProcessing> apm = CreateApm(false);
ASSERT_TRUE(apm);
AudioProcessing::Config apm_config;
apm_config.echo_canceller.enabled = true;
@ -2788,7 +2788,7 @@ TEST(MAYBE_ApmStatistics, AECEnabledTest) {
TEST(MAYBE_ApmStatistics, AECMEnabledTest) {
// Set up APM with AECM and process some audio.
std::unique_ptr<AudioProcessing> apm = CreateApm(true);
rtc::scoped_refptr<AudioProcessing> apm = CreateApm(true);
ASSERT_TRUE(apm);
// Set up an audioframe.
@ -2853,8 +2853,8 @@ TEST(ApmStatistics, ReportOutputRmsDbfs) {
ptr[i] = 10000 * ((i % 3) - 1);
}
std::unique_ptr<AudioProcessing> apm(
AudioProcessingBuilderForTesting().Create());
rtc::scoped_refptr<AudioProcessing> apm =
AudioProcessingBuilderForTesting().Create();
apm->Initialize(processing_config);
// If not enabled, no metric should be reported.
@ -2907,8 +2907,8 @@ TEST(ApmStatistics, ReportHasVoice) {
ptr[i] = 10000 * ((i % 3) - 1);
}
std::unique_ptr<AudioProcessing> apm(
AudioProcessingBuilderForTesting().Create());
rtc::scoped_refptr<AudioProcessing> apm =
AudioProcessingBuilderForTesting().Create();
apm->Initialize(processing_config);
// If not enabled, no metric should be reported.

View File

@ -815,8 +815,8 @@ class RTC_EXPORT AudioProcessingBuilder {
}
// This creates an APM instance using the previously set components. Calling
// the Create function resets the AudioProcessingBuilder to its initial state.
AudioProcessing* Create();
AudioProcessing* Create(const webrtc::Config& config);
rtc::scoped_refptr<AudioProcessing> Create();
rtc::scoped_refptr<AudioProcessing> Create(const webrtc::Config& config);
private:
std::unique_ptr<EchoControlFactory> echo_control_factory_;

View File

@ -23,14 +23,14 @@ AudioProcessingBuilderForTesting::~AudioProcessingBuilderForTesting() = default;
#ifdef WEBRTC_EXCLUDE_AUDIO_PROCESSING_MODULE
AudioProcessing* AudioProcessingBuilderForTesting::Create() {
rtc::scoped_refptr<AudioProcessing> AudioProcessingBuilderForTesting::Create() {
webrtc::Config config;
return Create(config);
}
AudioProcessing* AudioProcessingBuilderForTesting::Create(
rtc::scoped_refptr<AudioProcessing> AudioProcessingBuilderForTesting::Create(
const webrtc::Config& config) {
return new rtc::RefCountedObject<AudioProcessingImpl>(
return rtc::make_ref_counted<AudioProcessingImpl>(
config, std::move(capture_post_processing_),
std::move(render_pre_processing_), std::move(echo_control_factory_),
std::move(echo_detector_), std::move(capture_analyzer_));
@ -38,13 +38,13 @@ AudioProcessing* AudioProcessingBuilderForTesting::Create(
#else
AudioProcessing* AudioProcessingBuilderForTesting::Create() {
rtc::scoped_refptr<AudioProcessing> AudioProcessingBuilderForTesting::Create() {
AudioProcessingBuilder builder;
TransferOwnershipsToBuilder(&builder);
return builder.Create();
}
AudioProcessing* AudioProcessingBuilderForTesting::Create(
rtc::scoped_refptr<AudioProcessing> AudioProcessingBuilderForTesting::Create(
const webrtc::Config& config) {
AudioProcessingBuilder builder;
TransferOwnershipsToBuilder(&builder);

View File

@ -62,8 +62,8 @@ class AudioProcessingBuilderForTesting {
// This creates an APM instance using the previously set components. Calling
// the Create function resets the AudioProcessingBuilderForTesting to its
// initial state.
AudioProcessing* Create();
AudioProcessing* Create(const webrtc::Config& config);
rtc::scoped_refptr<AudioProcessing> Create();
rtc::scoped_refptr<AudioProcessing> Create(const webrtc::Config& config);
private:
// Transfers the ownership to a non-testing builder.

View File

@ -185,7 +185,7 @@ void DebugDumpReplayer::MaybeRecreateApm(const audioproc::Config& msg) {
// We only create APM once, since changes on these fields should not
// happen in current implementation.
if (!apm_.get()) {
apm_.reset(AudioProcessingBuilderForTesting().Create(config));
apm_ = AudioProcessingBuilderForTesting().Create(config);
}
}

View File

@ -60,7 +60,7 @@ class DebugDumpReplayer {
std::unique_ptr<ChannelBuffer<float>> reverse_;
std::unique_ptr<ChannelBuffer<float>> output_;
std::unique_ptr<AudioProcessing> apm_;
rtc::scoped_refptr<AudioProcessing> apm_;
FILE* debug_file_;

View File

@ -112,7 +112,7 @@ class DebugDumpGenerator {
bool enable_pre_amplifier_;
TaskQueueForTest worker_queue_;
std::unique_ptr<AudioProcessing> apm_;
rtc::scoped_refptr<AudioProcessing> apm_;
const std::string dump_file_name_;
};
@ -143,7 +143,7 @@ DebugDumpGenerator::DebugDumpGenerator(const std::string& input_file_name,
worker_queue_("debug_dump_generator_worker_queue"),
dump_file_name_(dump_file_name) {
AudioProcessingBuilderForTesting apm_builder;
apm_.reset(apm_builder.Create(config));
apm_ = apm_builder.Create(config);
}
DebugDumpGenerator::DebugDumpGenerator(

View File

@ -34,7 +34,7 @@ const std::string kFieldTrialNames[] = {
"WebRTC-Aec3ShortHeadroomKillSwitch",
};
std::unique_ptr<AudioProcessing> CreateApm(test::FuzzDataHelper* fuzz_data,
rtc::scoped_refptr<AudioProcessing> CreateApm(test::FuzzDataHelper* fuzz_data,
std::string* field_trial_string,
rtc::TaskQueue* worker_queue) {
// Parse boolean values for optionally enabling different
@ -108,10 +108,10 @@ std::unique_ptr<AudioProcessing> CreateApm(test::FuzzDataHelper* fuzz_data,
config.Set<ExperimentalAgc>(new ExperimentalAgc(exp_agc));
config.Set<ExperimentalNs>(new ExperimentalNs(exp_ns));
std::unique_ptr<AudioProcessing> apm(
rtc::scoped_refptr<AudioProcessing> apm =
AudioProcessingBuilderForTesting()
.SetEchoControlFactory(std::move(echo_control_factory))
.Create(config));
.Create(config);
#ifdef WEBRTC_LINUX
apm->AttachAecDump(AecDumpFactory::Create("/dev/null", -1, worker_queue));

View File

@ -70,7 +70,7 @@ void GenerateFixedFrame(test::FuzzDataHelper* fuzz_data,
} // namespace
void FuzzAudioProcessing(test::FuzzDataHelper* fuzz_data,
std::unique_ptr<AudioProcessing> apm) {
rtc::scoped_refptr<AudioProcessing> apm) {
AudioFrame fixed_frame;
// Normal usage is up to 8 channels. Allowing to fuzz one beyond this allows
// us to catch implicit assumptions about normal usage.

View File

@ -18,7 +18,7 @@
namespace webrtc {
void FuzzAudioProcessing(test::FuzzDataHelper* fuzz_data,
std::unique_ptr<AudioProcessing> apm);
rtc::scoped_refptr<AudioProcessing> apm);
} // namespace webrtc