output/ao: use AudioOutputWrapper

This commit is contained in:
Max Kellermann 2017-01-25 10:33:27 +01:00
parent 30d5186db4
commit 487e2618cd

View File

@ -20,6 +20,7 @@
#include "config.h" #include "config.h"
#include "AoOutputPlugin.hxx" #include "AoOutputPlugin.hxx"
#include "../OutputAPI.hxx" #include "../OutputAPI.hxx"
#include "../Wrapper.hxx"
#include "system/Error.hxx" #include "system/Error.hxx"
#include "util/DivideString.hxx" #include "util/DivideString.hxx"
#include "util/SplitString.hxx" #include "util/SplitString.hxx"
@ -45,6 +46,16 @@ struct AoOutput {
ao_device *device; ao_device *device;
AoOutput(const ConfigBlock &block); AoOutput(const ConfigBlock &block);
~AoOutput();
static AoOutput *Create(const ConfigBlock &block) {
return new AoOutput(block);
}
void Open(AudioFormat &audio_format);
void Close();
size_t Play(const void *chunk, size_t size);
}; };
static constexpr Domain ao_output_domain("ao_output"); static constexpr Domain ao_output_domain("ao_output");
@ -120,19 +131,9 @@ AoOutput::AoOutput(const ConfigBlock &block)
} }
} }
static AudioOutput * AoOutput::~AoOutput()
ao_output_init(const ConfigBlock &block)
{ {
return &(new AoOutput(block))->base; ao_free_options(options);
}
static void
ao_output_finish(AudioOutput *ao)
{
AoOutput *ad = (AoOutput *)ao;
ao_free_options(ad->options);
delete ad;
ao_output_ref--; ao_output_ref--;
@ -140,19 +141,10 @@ ao_output_finish(AudioOutput *ao)
ao_shutdown(); ao_shutdown();
} }
static void void
ao_output_close(AudioOutput *ao) AoOutput::Open(AudioFormat &audio_format)
{
AoOutput *ad = (AoOutput *)ao;
ao_close(ad->device);
}
static void
ao_output_open(AudioOutput *ao, AudioFormat &audio_format)
{ {
ao_sample_format format = OUR_AO_FORMAT_INITIALIZER; ao_sample_format format = OUR_AO_FORMAT_INITIALIZER;
AoOutput *ad = (AoOutput *)ao;
switch (audio_format.format) { switch (audio_format.format) {
case SampleFormat::S8: case SampleFormat::S8:
@ -176,43 +168,48 @@ ao_output_open(AudioOutput *ao, AudioFormat &audio_format)
format.byte_format = AO_FMT_NATIVE; format.byte_format = AO_FMT_NATIVE;
format.channels = audio_format.channels; format.channels = audio_format.channels;
ad->device = ao_open_live(ad->driver, &format, ad->options); device = ao_open_live(driver, &format, options);
if (device == nullptr)
if (ad->device == nullptr)
throw MakeAoError(); throw MakeAoError();
} }
static size_t void
ao_output_play(AudioOutput *ao, const void *chunk, size_t size) AoOutput::Close()
{ {
AoOutput *ad = (AoOutput *)ao; ao_close(device);
}
if (size > ad->write_size) size_t
size = ad->write_size; AoOutput::Play(const void *chunk, size_t size)
{
if (size > write_size)
size = write_size;
/* For whatever reason, libao wants a non-const pointer. /* For whatever reason, libao wants a non-const pointer.
Let's hope it does not write to the buffer, and use the Let's hope it does not write to the buffer, and use the
union deconst hack to * work around this API misdesign. */ union deconst hack to * work around this API misdesign. */
char *data = const_cast<char *>((const char *)chunk); char *data = const_cast<char *>((const char *)chunk);
if (ao_play(ad->device, data, size) == 0) if (ao_play(device, data, size) == 0)
throw MakeAoError(); throw MakeAoError();
return size; return size;
} }
typedef AudioOutputWrapper<AoOutput> Wrapper;
const struct AudioOutputPlugin ao_output_plugin = { const struct AudioOutputPlugin ao_output_plugin = {
"ao", "ao",
nullptr, nullptr,
ao_output_init, &Wrapper::Init,
ao_output_finish, &Wrapper::Finish,
nullptr, nullptr,
nullptr, nullptr,
ao_output_open, &Wrapper::Open,
ao_output_close, &Wrapper::Close,
nullptr, nullptr,
nullptr, nullptr,
ao_output_play, &Wrapper::Play,
nullptr, nullptr,
nullptr, nullptr,
nullptr, nullptr,