From 46406d6cca25c2e5a84bb45c75ee4ad4c7fcc42e Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Tue, 2 Jan 2018 12:11:02 +0100 Subject: [PATCH] output/ao: use class SafeSingleton for libao initialization --- src/output/plugins/AoOutputPlugin.cxx | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/output/plugins/AoOutputPlugin.cxx b/src/output/plugins/AoOutputPlugin.cxx index 30de2ead6..cd40df30b 100644 --- a/src/output/plugins/AoOutputPlugin.cxx +++ b/src/output/plugins/AoOutputPlugin.cxx @@ -20,6 +20,7 @@ #include "config.h" #include "AoOutputPlugin.hxx" #include "../OutputAPI.hxx" +#include "thread/SafeSingleton.hxx" #include "system/Error.hxx" #include "util/DivideString.hxx" #include "util/SplitString.hxx" @@ -34,9 +35,18 @@ /* An ao_sample_format, with all fields set to zero: */ static ao_sample_format OUR_AO_FORMAT_INITIALIZER; -static unsigned ao_output_ref; +class AoInit { +public: + AoInit() { + ao_initialize(); + } -class AoOutput final : AudioOutput { + ~AoInit() noexcept { + ao_shutdown(); + } +}; + +class AoOutput final : AudioOutput, SafeSingleton { const size_t write_size; int driver; ao_option *options = nullptr; @@ -93,11 +103,6 @@ AoOutput::AoOutput(const ConfigBlock &block) :AudioOutput(0), write_size(block.GetBlockValue("write_size", 1024u)) { - if (ao_output_ref == 0) { - ao_initialize(); - } - ao_output_ref++; - const char *value = block.GetBlockValue("driver", "default"); if (0 == strcmp(value, "default")) driver = ao_default_driver_id(); @@ -132,11 +137,6 @@ AoOutput::AoOutput(const ConfigBlock &block) AoOutput::~AoOutput() { ao_free_options(options); - - ao_output_ref--; - - if (ao_output_ref == 0) - ao_shutdown(); } void