output/sndio: migrate from class Error to C++ exceptions

This commit is contained in:
Max Kellermann 2016-11-08 15:15:16 +01:00
parent 52aed3f8a1
commit db7eec042e

View File

@ -17,19 +17,17 @@
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/ */
#include <sndio.h>
#include <string.h>
#include <unistd.h>
#include "config.h" #include "config.h"
#include "SndioOutputPlugin.hxx" #include "SndioOutputPlugin.hxx"
#include "config/ConfigError.hxx"
#include "../OutputAPI.hxx" #include "../OutputAPI.hxx"
#include "../Wrapper.hxx" #include "../Wrapper.hxx"
#include "util/Error.hxx"
#include "util/Domain.hxx" #include "util/Domain.hxx"
#include "Log.hxx" #include "Log.hxx"
#include <sndio.h>
#include <stdexcept>
#ifndef SIO_DEVANY #ifndef SIO_DEVANY
/* this macro is missing in libroar-dev 1.0~beta2-3 (Debian Wheezy) */ /* this macro is missing in libroar-dev 1.0~beta2-3 (Debian Wheezy) */
#define SIO_DEVANY "default" #define SIO_DEVANY "default"
@ -42,14 +40,12 @@ static constexpr Domain sndio_output_domain("sndio_output");
class SndioOutput { class SndioOutput {
friend struct AudioOutputWrapper<SndioOutput>; friend struct AudioOutputWrapper<SndioOutput>;
AudioOutput base; AudioOutput base;
const char *device; const char *const device;
unsigned buffer_time; /* in ms */ const unsigned buffer_time; /* in ms */
struct sio_hdl *sio_hdl; struct sio_hdl *sio_hdl;
public: public:
SndioOutput() SndioOutput(const ConfigBlock &block);
:base(sndio_output_plugin) {}
~SndioOutput() {}
bool Configure(const ConfigBlock &block, Error &error); bool Configure(const ConfigBlock &block, Error &error);
@ -62,28 +58,18 @@ public:
void Cancel(); void Cancel();
}; };
bool SndioOutput::SndioOutput(const ConfigBlock &block)
SndioOutput::Configure(const ConfigBlock &block, Error &error) :base(sndio_output_plugin, block),
device(block.GetBlockValue("device", SIO_DEVANY)),
buffer_time(block.GetBlockValue("buffer_time",
MPD_SNDIO_BUFFER_TIME_MS))
{ {
if (!base.Configure(block, error))
return false;
device = block.GetBlockValue("device", SIO_DEVANY);
buffer_time = block.GetBlockValue("buffer_time",
MPD_SNDIO_BUFFER_TIME_MS);
return true;
} }
SndioOutput * SndioOutput *
SndioOutput::Create(const ConfigBlock &block, Error &error) SndioOutput::Create(const ConfigBlock &block, Error &)
{ {
SndioOutput *ao = new SndioOutput(); return new SndioOutput(block);
if (!ao->Configure(block, error)) {
delete ao;
return nullptr;
}
return ao;
} }
static bool static bool
@ -103,17 +89,14 @@ sndio_test_default_device()
} }
bool bool
SndioOutput::Open(AudioFormat &audio_format, Error &error) SndioOutput::Open(AudioFormat &audio_format, Error &)
{ {
struct sio_par par; struct sio_par par;
unsigned bits, rate, chans; unsigned bits, rate, chans;
sio_hdl = sio_open(device, SIO_PLAY, 0); sio_hdl = sio_open(device, SIO_PLAY, 0);
if (!sio_hdl) { if (!sio_hdl)
error.Format(sndio_output_domain, -1, throw std::runtime_error("Failed to open default sndio device");
"Failed to open default sndio device");
return false;
}
switch (audio_format.format) { switch (audio_format.format) {
case SampleFormat::S16: case SampleFormat::S16:
@ -144,10 +127,8 @@ SndioOutput::Open(AudioFormat &audio_format, Error &error)
if (!sio_setpar(sio_hdl, &par) || if (!sio_setpar(sio_hdl, &par) ||
!sio_getpar(sio_hdl, &par)) { !sio_getpar(sio_hdl, &par)) {
error.Format(sndio_output_domain, -1,
"Failed to set/get audio params");
sio_close(sio_hdl); sio_close(sio_hdl);
return false; throw std::runtime_error("Failed to set/get audio params");
} }
if (par.bits != bits || if (par.bits != bits ||
@ -156,17 +137,13 @@ SndioOutput::Open(AudioFormat &audio_format, Error &error)
par.pchan != chans || par.pchan != chans ||
par.sig != 1 || par.sig != 1 ||
par.le != SIO_LE_NATIVE) { par.le != SIO_LE_NATIVE) {
error.Format(sndio_output_domain, -1,
"Requested audio params cannot be satisfied");
sio_close(sio_hdl); sio_close(sio_hdl);
return false; throw std::runtime_error("Requested audio params cannot be satisfied");
} }
if (!sio_start(sio_hdl)) { if (!sio_start(sio_hdl)) {
error.Format(sndio_output_domain, -1,
"Failed to start audio device");
sio_close(sio_hdl); sio_close(sio_hdl);
return false; throw std::runtime_error("Failed to start audio device");
} }
return true; return true;
@ -179,13 +156,13 @@ SndioOutput::Close()
} }
size_t size_t
SndioOutput::Play(const void *chunk, size_t size, Error &error) SndioOutput::Play(const void *chunk, size_t size, Error &)
{ {
size_t n; size_t n;
n = sio_write(sio_hdl, chunk, size); n = sio_write(sio_hdl, chunk, size);
if (n == 0 && sio_eof(sio_hdl) != 0) if (n == 0 && sio_eof(sio_hdl) != 0)
error.Set(sndio_output_domain, -1, "sndio write failed"); throw std::runtime_error("sndio write failed");
return n; return n;
} }