From 704a28ca1703fe69ec33e4d39460810dbf6138b9 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Wed, 25 Jan 2017 10:29:42 +0100 Subject: [PATCH] output/solaris: use AudioOutputWrapper --- src/output/plugins/SolarisOutputPlugin.cxx | 88 ++++++++++------------ 1 file changed, 39 insertions(+), 49 deletions(-) diff --git a/src/output/plugins/SolarisOutputPlugin.cxx b/src/output/plugins/SolarisOutputPlugin.cxx index 8e9ca6441..3e475a6d7 100644 --- a/src/output/plugins/SolarisOutputPlugin.cxx +++ b/src/output/plugins/SolarisOutputPlugin.cxx @@ -20,6 +20,7 @@ #include "config.h" #include "SolarisOutputPlugin.hxx" #include "../OutputAPI.hxx" +#include "../Wrapper.hxx" #include "system/fd_util.h" #include "system/Error.hxx" @@ -60,6 +61,16 @@ struct SolarisOutput { explicit SolarisOutput(const ConfigBlock &block) :base(solaris_output_plugin, block), device(block.GetBlockValue("device", "/dev/audio")) {} + + static SolarisOutput *Create(const ConfigBlock &block) { + return new SolarisOutput(block); + } + + void Open(AudioFormat &audio_format); + void Close(); + + size_t Play(const void *chunk, size_t size); + void Cancel(); }; static bool @@ -71,25 +82,9 @@ solaris_output_test_default_device(void) access("/dev/audio", W_OK) == 0; } -static AudioOutput * -solaris_output_init(const ConfigBlock &block) +void +SolarisOutput::Open(AudioFormat &audio_format) { - SolarisOutput *so = new SolarisOutput(block); - return &so->base; -} - -static void -solaris_output_finish(AudioOutput *ao) -{ - SolarisOutput *so = (SolarisOutput *)ao; - - delete so; -} - -static void -solaris_output_open(AudioOutput *ao, AudioFormat &audio_format) -{ - SolarisOutput *so = (SolarisOutput *)ao; struct audio_info info; int ret, flags; @@ -99,23 +94,23 @@ solaris_output_open(AudioOutput *ao, AudioFormat &audio_format) /* open the device in non-blocking mode */ - so->fd = open_cloexec(so->device, O_WRONLY|O_NONBLOCK, 0); - if (so->fd < 0) + fd = open_cloexec(device, O_WRONLY|O_NONBLOCK, 0); + if (fd < 0) throw FormatErrno("Failed to open %s", - so->device); + device); /* restore blocking mode */ - flags = fcntl(so->fd, F_GETFL); + flags = fcntl(fd, F_GETFL); if (flags > 0 && (flags & O_NONBLOCK) != 0) - fcntl(so->fd, F_SETFL, flags & ~O_NONBLOCK); + fcntl(fd, F_SETFL, flags & ~O_NONBLOCK); /* configure the audio device */ - ret = ioctl(so->fd, AUDIO_GETINFO, &info); + ret = ioctl(fd, AUDIO_GETINFO, &info); if (ret < 0) { const int e = errno; - close(so->fd); + close(fd); throw MakeErrno(e, "AUDIO_GETINFO failed"); } @@ -124,57 +119,52 @@ solaris_output_open(AudioOutput *ao, AudioFormat &audio_format) info.play.precision = 16; info.play.encoding = AUDIO_ENCODING_LINEAR; - ret = ioctl(so->fd, AUDIO_SETINFO, &info); + ret = ioctl(fd, AUDIO_SETINFO, &info); if (ret < 0) { const int e = errno; - close(so->fd); + close(fd); throw MakeErrno(e, "AUDIO_SETINFO failed"); } } -static void -solaris_output_close(AudioOutput *ao) +void +SolarisOutput::Close() { - SolarisOutput *so = (SolarisOutput *)ao; - - close(so->fd); + close(fd); } -static size_t -solaris_output_play(AudioOutput *ao, const void *chunk, size_t size) +size_t +SolarisOutput::Play(const void *chunk, size_t size) { - SolarisOutput *so = (SolarisOutput *)ao; - ssize_t nbytes; - - nbytes = write(so->fd, chunk, size); + ssize_t nbytes = write(fd, chunk, size); if (nbytes <= 0) throw MakeErrno("Write failed"); return nbytes; } -static void -solaris_output_cancel(AudioOutput *ao) +void +SolarisOutput::Cancel() { - SolarisOutput *so = (SolarisOutput *)ao; - - ioctl(so->fd, I_FLUSH); + ioctl(fd, I_FLUSH); } +typedef AudioOutputWrapper Wrapper; + const struct AudioOutputPlugin solaris_output_plugin = { "solaris", solaris_output_test_default_device, - solaris_output_init, - solaris_output_finish, + &Wrapper::Init, + &Wrapper::Finish, nullptr, nullptr, - solaris_output_open, - solaris_output_close, + &Wrapper::Open, + &Wrapper::Close, nullptr, nullptr, - solaris_output_play, + &Wrapper::Play, nullptr, - solaris_output_cancel, + &Wrapper::Cancel, nullptr, nullptr, };