Add helper to inject custom implementation of audio processing as factory

This would simplify migrating from PeerConnectionFactoryDependencies::audio_processing
for users who use own implementation of the AudioProcessing

Bug: webrtc:369904700
Change-Id: Id05f7280fd01a3e8fd4953f1b24b2467335ab065
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/366120
Reviewed-by: Harald Alvestrand <hta@webrtc.org>
Commit-Queue: Danil Chapovalov <danilchap@webrtc.org>
Cr-Commit-Position: refs/heads/main@{#43273}
This commit is contained in:
Danil Chapovalov 2024-10-21 12:05:48 +02:00 committed by WebRTC LUCI CQ
parent 929c02a479
commit 10e4d86a91
7 changed files with 97 additions and 5 deletions

View File

@ -167,11 +167,16 @@ rtc_source_set("echo_detector_creator") {
if (rtc_include_tests) {
rtc_library("audio_api_unittests") {
testonly = true
sources = [ "builtin_audio_processing_factory_unittest.cc" ]
sources = [
"audio_processing_unittest.cc",
"builtin_audio_processing_factory_unittest.cc",
]
deps = [
":audio_processing",
":builtin_audio_processing_factory",
"..:make_ref_counted",
"..:scoped_refptr",
"../../modules/audio_processing:mocks",
"../../test:test_support",
"../environment",
"../environment:environment_factory",

View File

@ -9,8 +9,14 @@
*/
#include "api/audio/audio_processing.h"
#include <string>
#include <memory>
#include <string>
#include <utility>
#include "absl/base/nullability.h"
#include "api/environment/environment.h"
#include "api/scoped_refptr.h"
#include "rtc_base/checks.h"
#include "rtc_base/strings/string_builder.h"
@ -208,4 +214,24 @@ std::string AudioProcessing::Config::ToString() const {
return builder.str();
}
absl::Nonnull<std::unique_ptr<AudioProcessingFactory>> CustomAudioProcessing(
absl::Nonnull<scoped_refptr<AudioProcessing>> audio_processing) {
class Factory : public AudioProcessingFactory {
public:
explicit Factory(absl::Nonnull<scoped_refptr<AudioProcessing>> ap)
: ap_(std::move(ap)) {}
absl::Nullable<scoped_refptr<AudioProcessing>> Create(
const Environment& /*env*/) override {
return ap_;
}
private:
absl::Nonnull<scoped_refptr<AudioProcessing>> ap_;
};
RTC_CHECK(audio_processing);
return std::make_unique<Factory>(std::move(audio_processing));
}
} // namespace webrtc

View File

@ -742,6 +742,14 @@ class AudioProcessingFactory {
const Environment& env) = 0;
};
// Returns factory that always returns the same `audio_processing` ignoring the
// extra construction parameter `env`.
// nullptr `audio_processing` is not supported as in some scenarios that imply
// no audio processing, while in others - default builtin audio processing.
// Callers should be explicit which of these two behaviors they want.
absl::Nonnull<std::unique_ptr<AudioProcessingFactory>> CustomAudioProcessing(
absl::Nonnull<scoped_refptr<AudioProcessing>> audio_processing);
// Experimental interface for a custom analysis submodule.
class CustomAudioAnalyzer {
public:

View File

@ -0,0 +1,47 @@
/*
* Copyright (c) 2024 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 "api/audio/audio_processing.h"
#include <memory>
#include "api/environment/environment_factory.h"
#include "api/make_ref_counted.h"
#include "api/scoped_refptr.h"
#include "modules/audio_processing/include/mock_audio_processing.h"
#include "test/gmock.h"
#include "test/gtest.h"
namespace webrtc {
using ::testing::_;
using ::testing::NotNull;
TEST(CustomAudioProcessingTest, ReturnsTheSameAudioProcessing) {
scoped_refptr<AudioProcessing> ap =
make_ref_counted<test::MockAudioProcessing>();
std::unique_ptr<AudioProcessingFactory> factory = CustomAudioProcessing(ap);
ASSERT_THAT(factory, NotNull());
EXPECT_EQ(factory->Create(CreateEnvironment()), ap);
EXPECT_EQ(factory->Create(CreateEnvironment()), ap);
}
#if GTEST_HAS_DEATH_TEST
TEST(CustomAudioProcessingTest, NullptrAudioProcessingIsUnsupported) {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wnonnull"
EXPECT_DEATH(CustomAudioProcessing(nullptr), _);
#pragma clang diagnostic pop
}
#endif
} // namespace webrtc

View File

@ -58,8 +58,9 @@ rtc::scoped_refptr<PeerConnectionFactoryInterface> CreatePeerConnectionFactory(
dependencies.audio_encoder_factory = std::move(audio_encoder_factory);
dependencies.audio_decoder_factory = std::move(audio_decoder_factory);
dependencies.audio_frame_processor = std::move(audio_frame_processor);
if (audio_processing) {
dependencies.audio_processing = std::move(audio_processing);
if (audio_processing != nullptr) {
dependencies.audio_processing_factory =
CustomAudioProcessing(std::move(audio_processing));
} else {
#ifndef WEBRTC_EXCLUDE_AUDIO_PROCESSING_MODULE
dependencies.audio_processing_factory =

View File

@ -108,6 +108,7 @@ if (!build_with_chromium) {
"../../../api:scoped_refptr",
"../../../api:time_controller",
"../../../api/audio:audio_device",
"../../../api/audio:audio_processing",
"../../../api/rtc_event_log:rtc_event_log_factory",
"../../../api/task_queue",
"../../../api/task_queue:default_task_queue_factory",

View File

@ -18,6 +18,7 @@
#include "absl/memory/memory.h"
#include "absl/strings/string_view.h"
#include "api/audio/audio_device.h"
#include "api/audio/audio_processing.h"
#include "api/peer_connection_interface.h"
#include "api/rtc_event_log/rtc_event_log_factory.h"
#include "api/scoped_refptr.h"
@ -226,7 +227,10 @@ PeerConnectionFactoryDependencies CreatePCFDependencies(
// Media dependencies
pcf_deps.adm = std::move(audio_device_module);
pcf_deps.audio_processing = pcf_dependencies->audio_processing;
if (pcf_dependencies->audio_processing != nullptr) {
pcf_deps.audio_processing_factory =
CustomAudioProcessing(pcf_dependencies->audio_processing);
}
pcf_deps.audio_mixer = pcf_dependencies->audio_mixer;
pcf_deps.video_encoder_factory =
std::move(pcf_dependencies->video_encoder_factory);