filter/LoadChain: move code to class FilterFactory

Eliminate a use of GetGlobalConfig().
This commit is contained in:
Max Kellermann 2018-08-18 20:57:02 +02:00
parent 1ba35e1fd4
commit 4531e4cc55
8 changed files with 120 additions and 40 deletions

View File

@ -1834,6 +1834,7 @@ libfilter_plugins_a_SOURCES = \
libfilter_glue_a_SOURCES = \ libfilter_glue_a_SOURCES = \
src/filter/FilterRegistry.cxx src/filter/FilterRegistry.hxx \ src/filter/FilterRegistry.cxx src/filter/FilterRegistry.hxx \
src/filter/Factory.cxx src/filter/Factory.hxx \
src/filter/LoadOne.cxx src/filter/LoadOne.hxx \ src/filter/LoadOne.cxx src/filter/LoadOne.hxx \
src/filter/LoadChain.cxx src/filter/LoadChain.hxx src/filter/LoadChain.cxx src/filter/LoadChain.hxx

40
src/filter/Factory.cxx Normal file
View File

@ -0,0 +1,40 @@
/*
* Copyright 2003-2018 The Music Player Daemon Project
* http://www.musicpd.org
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "config.h"
#include "Factory.hxx"
#include "LoadOne.hxx"
#include "Prepared.hxx"
#include "config/Data.hxx"
#include "config/Block.hxx"
#include "util/RuntimeError.hxx"
std::unique_ptr<PreparedFilter>
FilterFactory::MakeFilter(const char *name)
{
const auto *cfg = config.FindBlock(ConfigBlockOption::AUDIO_FILTER,
"name", name);
if (cfg == nullptr)
throw FormatRuntimeError("Filter template not found: %s",
name);
cfg->SetUsed();
return filter_configured_new(*cfg);
}

40
src/filter/Factory.hxx Normal file
View File

@ -0,0 +1,40 @@
/*
* Copyright 2003-2018 The Music Player Daemon Project
* http://www.musicpd.org
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#ifndef MPD_FILTER_FACTORY_HXX
#define MPD_FILTER_FACTORY_HXX
#include "check.h"
#include <memory>
struct ConfigData;
class PreparedFilter;
class FilterFactory {
const ConfigData &config;
public:
explicit FilterFactory(const ConfigData &_config) noexcept
:config(_config) {}
std::unique_ptr<PreparedFilter> MakeFilter(const char *name);
};
#endif

View File

@ -19,43 +19,26 @@
#include "config.h" #include "config.h"
#include "LoadChain.hxx" #include "LoadChain.hxx"
#include "LoadOne.hxx" #include "Factory.hxx"
#include "Prepared.hxx" #include "Prepared.hxx"
#include "plugins/ChainFilterPlugin.hxx" #include "plugins/ChainFilterPlugin.hxx"
#include "config/Param.hxx"
#include "config/Option.hxx"
#include "config/Data.hxx"
#include "config/Domain.hxx"
#include "config/Block.hxx"
#include "util/RuntimeError.hxx"
#include <algorithm> #include <algorithm>
#include <string>
#include <string.h> #include <string.h>
static void static void
filter_chain_append_new(PreparedFilter &chain, const ConfigData &config, filter_chain_append_new(PreparedFilter &chain, FilterFactory &factory,
const char *template_name) const char *template_name)
{ {
const auto *cfg = config.FindBlock(ConfigBlockOption::AUDIO_FILTER, filter_chain_append(chain, template_name,
"name", template_name); factory.MakeFilter(template_name));
if (cfg == nullptr)
throw FormatRuntimeError("Filter template not found: %s",
template_name);
cfg->SetUsed();
// Instantiate one of those filter plugins with the template name as a hint
auto f = filter_configured_new(*cfg);
const char *plugin_name = cfg->GetBlockValue("plugin",
"unknown");
filter_chain_append(chain, plugin_name, std::move(f));
} }
void void
filter_chain_parse(PreparedFilter &chain, filter_chain_parse(PreparedFilter &chain,
const ConfigData &config, FilterFactory &factory,
const char *spec) const char *spec)
{ {
const char *const end = spec + strlen(spec); const char *const end = spec + strlen(spec);
@ -64,7 +47,7 @@ filter_chain_parse(PreparedFilter &chain,
const char *comma = std::find(spec, end, ','); const char *comma = std::find(spec, end, ',');
if (comma > spec) { if (comma > spec) {
const std::string name(spec, comma); const std::string name(spec, comma);
filter_chain_append_new(chain, config, name.c_str()); filter_chain_append_new(chain, factory, name.c_str());
} }
if (comma == end) if (comma == end)

View File

@ -20,7 +20,7 @@
#ifndef MPD_FILTER_LOAD_CHAIN_HXX #ifndef MPD_FILTER_LOAD_CHAIN_HXX
#define MPD_FILTER_LOAD_CHAIN_HXX #define MPD_FILTER_LOAD_CHAIN_HXX
struct ConfigData; class FilterFactory;
class PreparedFilter; class PreparedFilter;
/** /**
@ -36,7 +36,7 @@ class PreparedFilter;
*/ */
void void
filter_chain_parse(PreparedFilter &chain, filter_chain_parse(PreparedFilter &chain,
const ConfigData &config, FilterFactory &factory,
const char *spec); const char *spec);
#endif #endif

View File

@ -28,6 +28,7 @@
#include <map> #include <map>
#include <chrono> #include <chrono>
class FilterFactory;
class PreparedFilter; class PreparedFilter;
class MusicPipe; class MusicPipe;
class EventLoop; class EventLoop;
@ -127,12 +128,14 @@ public:
*/ */
FilteredAudioOutput(const char *_plugin_name, FilteredAudioOutput(const char *_plugin_name,
std::unique_ptr<AudioOutput> &&_output, std::unique_ptr<AudioOutput> &&_output,
const ConfigBlock &block); const ConfigBlock &block,
FilterFactory *filter_factory);
~FilteredAudioOutput(); ~FilteredAudioOutput();
private: private:
void Configure(const ConfigBlock &block); void Configure(const ConfigBlock &block,
FilterFactory *filter_factory);
public: public:
void Setup(EventLoop &event_loop, void Setup(EventLoop &event_loop,
@ -235,6 +238,7 @@ std::unique_ptr<FilteredAudioOutput>
audio_output_new(EventLoop &event_loop, audio_output_new(EventLoop &event_loop,
const ReplayGainConfig &replay_gain_config, const ReplayGainConfig &replay_gain_config,
const ConfigBlock &block, const ConfigBlock &block,
FilterFactory *filter_factory,
MixerListener &mixer_listener); MixerListener &mixer_listener);
#endif #endif

View File

@ -54,10 +54,11 @@
FilteredAudioOutput::FilteredAudioOutput(const char *_plugin_name, FilteredAudioOutput::FilteredAudioOutput(const char *_plugin_name,
std::unique_ptr<AudioOutput> &&_output, std::unique_ptr<AudioOutput> &&_output,
const ConfigBlock &block) const ConfigBlock &block,
FilterFactory *filter_factory)
:plugin_name(_plugin_name), output(std::move(_output)) :plugin_name(_plugin_name), output(std::move(_output))
{ {
Configure(block); Configure(block, filter_factory);
} }
static const AudioOutputPlugin * static const AudioOutputPlugin *
@ -148,7 +149,8 @@ audio_output_load_mixer(EventLoop &event_loop, FilteredAudioOutput &ao,
} }
void void
FilteredAudioOutput::Configure(const ConfigBlock &block) FilteredAudioOutput::Configure(const ConfigBlock &block,
FilterFactory *filter_factory)
{ {
if (!block.IsNull()) { if (!block.IsNull()) {
name = block.GetBlockValue(AUDIO_OUTPUT_NAME); name = block.GetBlockValue(AUDIO_OUTPUT_NAME);
@ -181,8 +183,9 @@ FilteredAudioOutput::Configure(const ConfigBlock &block)
} }
try { try {
filter_chain_parse(*prepared_filter, GetGlobalConfig(), if (filter_factory != nullptr)
block.GetBlockValue(AUDIO_FILTERS, "")); filter_chain_parse(*prepared_filter, *filter_factory,
block.GetBlockValue(AUDIO_FILTERS, ""));
} catch (...) { } catch (...) {
/* It's not really fatal - Part of the filter chain /* It's not really fatal - Part of the filter chain
has been set up already and even an empty one will has been set up already and even an empty one will
@ -256,6 +259,7 @@ std::unique_ptr<FilteredAudioOutput>
audio_output_new(EventLoop &event_loop, audio_output_new(EventLoop &event_loop,
const ReplayGainConfig &replay_gain_config, const ReplayGainConfig &replay_gain_config,
const ConfigBlock &block, const ConfigBlock &block,
FilterFactory *filter_factory,
MixerListener &mixer_listener) MixerListener &mixer_listener)
{ {
const AudioOutputPlugin *plugin; const AudioOutputPlugin *plugin;
@ -286,7 +290,8 @@ audio_output_new(EventLoop &event_loop,
assert(ao != nullptr); assert(ao != nullptr);
auto f = std::make_unique<FilteredAudioOutput>(plugin->name, auto f = std::make_unique<FilteredAudioOutput>(plugin->name,
std::move(ao), block); std::move(ao), block,
filter_factory);
f->Setup(event_loop, replay_gain_config, f->Setup(event_loop, replay_gain_config,
plugin->mixer_plugin, plugin->mixer_plugin,
mixer_listener, block); mixer_listener, block);

View File

@ -23,6 +23,7 @@
#include "Domain.hxx" #include "Domain.hxx"
#include "MusicPipe.hxx" #include "MusicPipe.hxx"
#include "MusicChunk.hxx" #include "MusicChunk.hxx"
#include "filter/Factory.hxx"
#include "config/Block.hxx" #include "config/Block.hxx"
#include "config/Data.hxx" #include "config/Data.hxx"
#include "config/Option.hxx" #include "config/Option.hxx"
@ -51,9 +52,11 @@ static std::unique_ptr<FilteredAudioOutput>
LoadOutput(EventLoop &event_loop, LoadOutput(EventLoop &event_loop,
const ReplayGainConfig &replay_gain_config, const ReplayGainConfig &replay_gain_config,
MixerListener &mixer_listener, MixerListener &mixer_listener,
const ConfigBlock &block) const ConfigBlock &block,
FilterFactory *filter_factory)
try { try {
return audio_output_new(event_loop, replay_gain_config, block, return audio_output_new(event_loop, replay_gain_config, block,
filter_factory,
mixer_listener); mixer_listener);
} catch (...) { } catch (...) {
if (block.line > 0) if (block.line > 0)
@ -67,11 +70,12 @@ static AudioOutputControl *
LoadOutputControl(EventLoop &event_loop, LoadOutputControl(EventLoop &event_loop,
const ReplayGainConfig &replay_gain_config, const ReplayGainConfig &replay_gain_config,
MixerListener &mixer_listener, MixerListener &mixer_listener,
AudioOutputClient &client, const ConfigBlock &block) AudioOutputClient &client, const ConfigBlock &block,
FilterFactory *filter_factory)
{ {
auto output = LoadOutput(event_loop, replay_gain_config, auto output = LoadOutput(event_loop, replay_gain_config,
mixer_listener, mixer_listener,
block); block, filter_factory);
auto *control = new AudioOutputControl(std::move(output), client); auto *control = new AudioOutputControl(std::move(output), client);
try { try {
@ -91,12 +95,14 @@ MultipleOutputs::Configure(EventLoop &event_loop,
const ReplayGainConfig &replay_gain_config, const ReplayGainConfig &replay_gain_config,
AudioOutputClient &client) AudioOutputClient &client)
{ {
FilterFactory filter_factory(config);
for (const auto &block : config.GetBlockList(ConfigBlockOption::AUDIO_OUTPUT)) { for (const auto &block : config.GetBlockList(ConfigBlockOption::AUDIO_OUTPUT)) {
block.SetUsed(); block.SetUsed();
auto *output = LoadOutputControl(event_loop, auto *output = LoadOutputControl(event_loop,
replay_gain_config, replay_gain_config,
mixer_listener, mixer_listener,
client, block); client, block, &filter_factory);
if (FindByName(output->GetName()) != nullptr) if (FindByName(output->GetName()) != nullptr)
throw FormatRuntimeError("output devices with identical " throw FormatRuntimeError("output devices with identical "
"names: %s", output->GetName()); "names: %s", output->GetName());
@ -110,7 +116,8 @@ MultipleOutputs::Configure(EventLoop &event_loop,
auto *output = LoadOutputControl(event_loop, auto *output = LoadOutputControl(event_loop,
replay_gain_config, replay_gain_config,
mixer_listener, mixer_listener,
client, empty); client, empty,
nullptr);
outputs.push_back(output); outputs.push_back(output);
} }
} }
@ -125,7 +132,7 @@ MultipleOutputs::AddNullOutput(EventLoop &event_loop,
auto *output = LoadOutputControl(event_loop, replay_gain_config, auto *output = LoadOutputControl(event_loop, replay_gain_config,
mixer_listener, mixer_listener,
client, block); client, block, nullptr);
outputs.push_back(output); outputs.push_back(output);
} }