Commit d9fb0818 authored by Ken MacKay's avatar Ken MacKay Committed by Commit Bot

[Chromecast] Add postprocessor registry to allow them to be built-in

Bug: internal b/167462029

Change-Id: Ic0880bc93996ddf1be6b8b2d2b8c1a9c3aefb348
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2389162
Commit-Queue: Kenneth MacKay <kmackay@chromium.org>
Reviewed-by: default avatarYuchen Liu <yucliu@chromium.org>
Cr-Commit-Position: refs/heads/master@{#803742}
parent 0cc8373f
......@@ -18,6 +18,17 @@ cast_source_set("post_processor_paths") {
deps = [ "//base" ]
}
cast_source_set("post_processor_registry") {
sources = [
"post_processor_registry.cc",
"post_processor_registry.h",
]
deps = [
"//base",
"//chromecast/public/media",
]
}
cast_source_set("loopback") {
sources = [
"loopback_handler.cc",
......@@ -67,6 +78,7 @@ cast_source_set("mixer") {
deps = [
":loopback",
":post_processor_paths",
":post_processor_registry",
"//base",
"//chromecast/base",
"//chromecast/base:chromecast_switches",
......
......@@ -14,6 +14,7 @@
#include "base/strings/stringprintf.h"
#include "chromecast/media/audio/audio_log.h"
#include "chromecast/media/cma/backend/mixer/post_processor_paths.h"
#include "chromecast/media/cma/backend/mixer/post_processor_registry.h"
#include "chromecast/media/cma/backend/mixer/post_processors/post_processor_wrapper.h"
#include "chromecast/public/media/audio_post_processor2_shlib.h"
#include "chromecast/public/media/audio_post_processor_shlib.h"
......@@ -63,6 +64,13 @@ std::unique_ptr<AudioPostProcessor2> PostProcessorFactory::CreatePostProcessor(
const std::string& library_name,
const std::string& config,
int channels) {
std::unique_ptr<AudioPostProcessor2> builtin =
PostProcessorRegistry::Get()->Create(library_name, config, channels);
if (builtin) {
LOG(INFO) << "Loaded builtin " << library_name;
return builtin;
}
base::FilePath path = FindLibrary(library_name);
libraries_.push_back(std::make_unique<base::ScopedNativeLibrary>(path));
CHECK(libraries_.back()->is_valid())
......
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chromecast/media/cma/backend/mixer/post_processor_registry.h"
#include <utility>
#include "base/no_destructor.h"
#include "chromecast/public/media/audio_post_processor2_shlib.h"
namespace chromecast {
namespace media {
// static
PostProcessorRegistry* PostProcessorRegistry::Get() {
static base::NoDestructor<PostProcessorRegistry> g_registry;
return g_registry.get();
}
void PostProcessorRegistry::Register(const std::string& library_name,
CreateFunction create_function) {
creators_.emplace(library_name, std::move(create_function));
}
std::unique_ptr<AudioPostProcessor2> PostProcessorRegistry::Create(
const std::string& library_name,
const std::string& config,
int channels) {
auto it = creators_.find(library_name);
if (it == creators_.end()) {
return nullptr;
}
return it->second.Run(config, channels);
}
} // namespace media
} // namespace chromecast
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROMECAST_MEDIA_CMA_BACKEND_MIXER_POST_PROCESSOR_REGISTRY_H_
#define CHROMECAST_MEDIA_CMA_BACKEND_MIXER_POST_PROCESSOR_REGISTRY_H_
#include <memory>
#include <string>
#include "base/bind.h"
#include "base/callback.h"
#include "base/containers/flat_map.h"
namespace chromecast {
namespace media {
class AudioPostProcessor2;
// Global registry for built-in postprocessors.
class PostProcessorRegistry {
public:
using CreateFunction = base::RepeatingCallback<std::unique_ptr<
AudioPostProcessor2>(const std::string& config, int channels)>;
// Returns the global registry instance for the current process.
static PostProcessorRegistry* Get();
PostProcessorRegistry();
~PostProcessorRegistry();
PostProcessorRegistry(const PostProcessorRegistry&) = delete;
PostProcessorRegistry& operator=(const PostProcessorRegistry&) = delete;
// Registers a function to create a postprocessor for the given
// |library_name|, which should match the name used for the shlib for that
// postprocessor.
void Register(const std::string& library_name,
CreateFunction create_function);
// Creates a postprocessor instance for the given |library_name|, if it has
// been registered. Returns nullptr if no creation function has been
// registered for that name.
std::unique_ptr<AudioPostProcessor2> Create(const std::string& library_name,
const std::string& config,
int channels);
private:
base::flat_map<std::string /* library_name */, CreateFunction> creators_;
};
inline PostProcessorRegistry::PostProcessorRegistry() = default;
inline PostProcessorRegistry::~PostProcessorRegistry() = default;
// Helper class and macro for auto-registering postprocessors with simple
// constructors.
template <typename PostProcessor>
struct PostProcessorCreator {
PostProcessorCreator(const std::string& library_name) {
PostProcessorRegistry::Get()->Register(
library_name, base::BindRepeating(&PostProcessorCreator::Create));
}
static std::unique_ptr<AudioPostProcessor2> Create(const std::string& config,
int channels) {
return std::make_unique<PostProcessor>(config, channels);
}
};
#define REGISTER_POSTPROCESSOR(Type, library_name) \
static PostProcessorCreator<Type>* static_postprocessor_registration = \
new PostProcessorCreator<Type>(library_name)
} // namespace media
} // namespace chromecast
#endif // CHROMECAST_MEDIA_CMA_BACKEND_MIXER_POST_PROCESSOR_REGISTRY_H_
......@@ -42,6 +42,7 @@ cast_source_set("governor") {
"//chromecast/base",
"//chromecast/media/base",
"//chromecast/media/base:slew_volume",
"//chromecast/media/cma/backend/mixer:post_processor_registry",
"//chromecast/public/media",
]
public_configs = [ "//chromecast/public:public_config" ]
......@@ -73,6 +74,7 @@ cast_source_set("saturated_gain") {
"//chromecast/base",
"//chromecast/media/base",
"//chromecast/media/base:slew_volume",
"//chromecast/media/cma/backend/mixer:post_processor_registry",
"//chromecast/public/media",
]
public_configs = [ "//chromecast/public:public_config" ]
......
......@@ -13,6 +13,7 @@
#include "base/values.h"
#include "chromecast/base/serializers.h"
#include "chromecast/media/base/slew_volume.h"
#include "chromecast/media/cma/backend/mixer/post_processor_registry.h"
namespace chromecast {
namespace media {
......@@ -89,5 +90,7 @@ void Governor::SetSlewTimeMsForTest(int slew_time_ms) {
slew_volume_.SetMaxSlewTimeMs(slew_time_ms);
}
REGISTER_POSTPROCESSOR(Governor, "libcast_governor_2.0.so");
} // namespace media
} // namespace chromecast
......@@ -11,6 +11,7 @@
#include "base/values.h"
#include "chromecast/base/serializers.h"
#include "chromecast/media/base/slew_volume.h"
#include "chromecast/media/cma/backend/mixer/post_processor_registry.h"
namespace chromecast {
namespace media {
......@@ -68,5 +69,7 @@ bool SaturatedGain::UpdateParameters(const std::string& message) {
return false;
}
REGISTER_POSTPROCESSOR(SaturatedGain, "libcast_saturated_gain_2.0.so");
} // namespace media
} // namespace chromecast
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment