webrtc_m130/test/fuzzers/audio_processing_configs_fuzzer.cc
Alessio Bazzica 1db0a261ca Reland "Reland "Remove unused APM voice activity detection sub-module""
This reverts commit 09aaf6f7bcfb4da644bd86c76896a04a41f776e1.

Reason for revert: downstream fixed (see https://chromium-review.googlesource.com/c/chromium/src/+/3461371)

Original change's description:
> Revert "Reland "Remove unused APM voice activity detection sub-module""
>
> This reverts commit 54d1344d985b00d4d1580dd18057d4618c11ad1f.
>
> Reason for revert: Breaks chromium roll, see 
> https://ci.chromium.org/ui/p/chromium/builders/try/linux_chromium_tsan_rel_ng/1080583/overview
>
> https://chromium-review.googlesource.com/c/chromium/src/+/3461512
>
> Original change's description:
> > Reland "Remove unused APM voice activity detection sub-module"
> >
> > This reverts commit a751f167c68343f76528436defdbc61600a8d7b3.
> >
> > Reason for revert: dependency in a downstream project removed
> >
> > Original change's description:
> > > Revert "Remove unused APM voice activity detection sub-module"
> > >
> > > This reverts commit b4e06d032e6f82a65c52ed0c5364ae9e7c0a0215.
> > >
> > > Reason for revert: breaking downstream projects
> > >
> > > Original change's description:
> > > > Remove unused APM voice activity detection sub-module
> > > >
> > > > API changes:
> > > > - webrtc::AudioProcessing::Config::VoiceDetection removed
> > > > - webrtc::AudioProcessingStats::voice_detected deprecated
> > > > - cricket::AudioOptions::typing_detection deprecated
> > > > - webrtc::StatsReport::StatsValueName::
> > > >   kStatsValueNameTypingNoiseState deprecated
> > > >
> > > > PSA: https://groups.google.com/g/discuss-webrtc/c/7X6uwmJarE0
> > > >
> > > > Bug: webrtc:11226,webrtc:11292
> > > > Change-Id: I8d008b56708cf62961b9857ec052b59fda3b41bf
> > > > Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/250666
> > > > Reviewed-by: Harald Alvestrand <hta@webrtc.org>
> > > > Reviewed-by: Gustaf Ullberg <gustaf@webrtc.org>
> > > > Reviewed-by: Sam Zackrisson <saza@webrtc.org>
> > > > Reviewed-by: Björn Terelius <terelius@webrtc.org>
> > > > Commit-Queue: Alessio Bazzica <alessiob@webrtc.org>
> > > > Cr-Commit-Position: refs/heads/main@{#35975}
> > >
> > > TBR=gustaf@webrtc.org,saza@webrtc.org,alessiob@webrtc.org,terelius@webrtc.org,hta@webrtc.org,webrtc-scoped@luci-project-accounts.iam.gserviceaccount.com
> > >
> > > Change-Id: Iee01fdb874b4e0331277f3ffe60dacaabc3859a2
> > > No-Presubmit: true
> > > No-Tree-Checks: true
> > > No-Try: true
> > > Bug: webrtc:11226,webrtc:11292
> > > Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/251600
> > > Reviewed-by: Harald Alvestrand <hta@webrtc.org>
> > > Reviewed-by: Gustaf Ullberg <gustaf@webrtc.org>
> > > Commit-Queue: Mirko Bonadei <mbonadei@webrtc.org>
> > > Cr-Commit-Position: refs/heads/main@{#35977}
> >
> > # Not skipping CQ checks because this is a reland.
> >
> > Bug: webrtc:11226,webrtc:11292
> > Change-Id: I2fcbc5fdade16bfe6a0f0a02841a33a598d4f2ad
> > Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/251660
> > Reviewed-by: Alessio Bazzica <alessiob@webrtc.org>
> > Reviewed-by: Harald Alvestrand <hta@webrtc.org>
> > Commit-Queue: Alessio Bazzica <alessiob@webrtc.org>
> > Cr-Commit-Position: refs/heads/main@{#35984}
>
> TBR=mbonadei@webrtc.org,gustaf@webrtc.org,saza@webrtc.org,alessiob@webrtc.org,terelius@webrtc.org,hta@webrtc.org,webrtc-scoped@luci-project-accounts.iam.gserviceaccount.com
>
> Change-Id: Ib308a3af2dcce85a0074ef5a4680ccec3f82712f
> No-Presubmit: true
> No-Tree-Checks: true
> No-Try: true
> Bug: webrtc:11226,webrtc:11292
> Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/251688
> Reviewed-by: Henrik Boström <hbos@webrtc.org>
> Bot-Commit: rubber-stamper@appspot.gserviceaccount.com <rubber-stamper@appspot.gserviceaccount.com>
> Auto-Submit: Henrik Boström <hbos@webrtc.org>
> Reviewed-by: Harald Alvestrand <hta@webrtc.org>
> Commit-Queue: Harald Alvestrand <hta@webrtc.org>
> Cr-Commit-Position: refs/heads/main@{#35990}

# Not skipping CQ checks because original CL landed > 1 day ago.

Bug: webrtc:11226,webrtc:11292
Change-Id: Idfda6a517027ad323caf44c526a88468e5b52b65
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/251762
Reviewed-by: Alessio Bazzica <alessiob@webrtc.org>
Bot-Commit: rubber-stamper@appspot.gserviceaccount.com <rubber-stamper@appspot.gserviceaccount.com>
Reviewed-by: Harald Alvestrand <hta@webrtc.org>
Reviewed-by: Sam Zackrisson <saza@webrtc.org>
Commit-Queue: Alessio Bazzica <alessiob@webrtc.org>
Cr-Commit-Position: refs/heads/main@{#36012}
2022-02-16 08:41:30 +00:00

158 lines
6.2 KiB
C++

/*
* Copyright (c) 2017 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#include <bitset>
#include <string>
#include "absl/memory/memory.h"
#include "api/audio/echo_canceller3_factory.h"
#include "api/audio/echo_detector_creator.h"
#include "api/task_queue/default_task_queue_factory.h"
#include "modules/audio_processing/aec_dump/aec_dump_factory.h"
#include "modules/audio_processing/include/audio_processing.h"
#include "modules/audio_processing/test/audio_processing_builder_for_testing.h"
#include "rtc_base/arraysize.h"
#include "rtc_base/numerics/safe_minmax.h"
#include "rtc_base/task_queue.h"
#include "system_wrappers/include/field_trial.h"
#include "test/fuzzers/audio_processing_fuzzer_helper.h"
#include "test/fuzzers/fuzz_data_helper.h"
namespace webrtc {
namespace {
const std::string kFieldTrialNames[] = {
"WebRTC-Audio-Agc2ForceExtraSaturationMargin",
"WebRTC-Audio-Agc2ForceInitialSaturationMargin",
"WebRTC-Aec3MinErleDuringOnsetsKillSwitch",
"WebRTC-Aec3ShortHeadroomKillSwitch",
};
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
// configurable public components of APM.
static_cast<void>(fuzz_data->ReadOrDefaultValue(true));
bool use_ts = fuzz_data->ReadOrDefaultValue(true);
static_cast<void>(fuzz_data->ReadOrDefaultValue(true));
static_cast<void>(fuzz_data->ReadOrDefaultValue(true));
static_cast<void>(fuzz_data->ReadOrDefaultValue(true));
bool use_red = fuzz_data->ReadOrDefaultValue(true);
bool use_hpf = fuzz_data->ReadOrDefaultValue(true);
bool use_aec3 = fuzz_data->ReadOrDefaultValue(true);
bool use_aec = fuzz_data->ReadOrDefaultValue(true);
bool use_aecm = fuzz_data->ReadOrDefaultValue(true);
bool use_agc = fuzz_data->ReadOrDefaultValue(true);
bool use_ns = fuzz_data->ReadOrDefaultValue(true);
static_cast<void>(fuzz_data->ReadOrDefaultValue(true));
static_cast<void>(fuzz_data->ReadOrDefaultValue(true));
bool use_agc_limiter = fuzz_data->ReadOrDefaultValue(true);
bool use_agc2 = fuzz_data->ReadOrDefaultValue(true);
// Read an int8 value, but don't let it be too large or small.
const float gain_controller2_gain_db =
rtc::SafeClamp<int>(fuzz_data->ReadOrDefaultValue<int8_t>(0), -40, 40);
constexpr size_t kNumFieldTrials = arraysize(kFieldTrialNames);
// Verify that the read data type has enough bits to fuzz the field trials.
using FieldTrialBitmaskType = uint64_t;
static_assert(kNumFieldTrials <= sizeof(FieldTrialBitmaskType) * 8,
"FieldTrialBitmaskType is not large enough.");
std::bitset<kNumFieldTrials> field_trial_bitmask(
fuzz_data->ReadOrDefaultValue<FieldTrialBitmaskType>(0));
for (size_t i = 0; i < kNumFieldTrials; ++i) {
if (field_trial_bitmask[i]) {
*field_trial_string += kFieldTrialNames[i] + "/Enabled/";
}
}
field_trial::InitFieldTrialsFromString(field_trial_string->c_str());
bool use_agc2_adaptive_digital = fuzz_data->ReadOrDefaultValue(true);
static_cast<void>(fuzz_data->ReadOrDefaultValue(true));
static_cast<void>(fuzz_data->ReadOrDefaultValue(true));
// Ignore a few bytes. Bytes from this segment will be used for
// future config flag changes. We assume 40 bytes is enough for
// configuring the APM.
constexpr size_t kSizeOfConfigSegment = 40;
RTC_DCHECK(kSizeOfConfigSegment >= fuzz_data->BytesRead());
static_cast<void>(
fuzz_data->ReadByteArray(kSizeOfConfigSegment - fuzz_data->BytesRead()));
// Filter out incompatible settings that lead to CHECK failures.
if ((use_aecm && use_aec) || // These settings cause CHECK failure.
(use_aecm && use_aec3 && use_ns) // These settings trigger webrtc:9489.
) {
return nullptr;
}
std::unique_ptr<EchoControlFactory> echo_control_factory;
if (use_aec3) {
echo_control_factory.reset(new EchoCanceller3Factory());
}
webrtc::AudioProcessing::Config apm_config;
apm_config.pipeline.multi_channel_render = true;
apm_config.pipeline.multi_channel_capture = true;
apm_config.echo_canceller.enabled = use_aec || use_aecm;
apm_config.echo_canceller.mobile_mode = use_aecm;
apm_config.high_pass_filter.enabled = use_hpf;
apm_config.gain_controller1.enabled = use_agc;
apm_config.gain_controller1.enable_limiter = use_agc_limiter;
apm_config.gain_controller2.enabled = use_agc2;
apm_config.gain_controller2.fixed_digital.gain_db = gain_controller2_gain_db;
apm_config.gain_controller2.adaptive_digital.enabled =
use_agc2_adaptive_digital;
apm_config.noise_suppression.enabled = use_ns;
apm_config.transient_suppression.enabled = use_ts;
rtc::scoped_refptr<AudioProcessing> apm =
AudioProcessingBuilderForTesting()
.SetEchoControlFactory(std::move(echo_control_factory))
.SetEchoDetector(use_red ? CreateEchoDetector() : nullptr)
.SetConfig(apm_config)
.Create();
#ifdef WEBRTC_LINUX
apm->AttachAecDump(AecDumpFactory::Create("/dev/null", -1, worker_queue));
#endif
return apm;
}
TaskQueueFactory* GetTaskQueueFactory() {
static TaskQueueFactory* const factory =
CreateDefaultTaskQueueFactory().release();
return factory;
}
} // namespace
void FuzzOneInput(const uint8_t* data, size_t size) {
if (size > 400000) {
return;
}
test::FuzzDataHelper fuzz_data(rtc::ArrayView<const uint8_t>(data, size));
// This string must be in scope during execution, according to documentation
// for field_trial.h. Hence it's created here and not in CreateApm.
std::string field_trial_string = "";
rtc::TaskQueue worker_queue(GetTaskQueueFactory()->CreateTaskQueue(
"rtc-low-prio", rtc::TaskQueue::Priority::LOW));
auto apm = CreateApm(&fuzz_data, &field_trial_string, &worker_queue);
if (apm) {
FuzzAudioProcessing(&fuzz_data, std::move(apm));
}
}
} // namespace webrtc