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

This commit is contained in:
Max Kellermann 2016-11-09 08:29:44 +01:00
parent f12fa7e20a
commit b4e5fa5c1b

View File

@ -24,7 +24,6 @@
#include "../Wrapper.hxx"
#include "mixer/MixerList.hxx"
#include "thread/Mutex.hxx"
#include "util/Error.hxx"
#include "util/Domain.hxx"
#include "Log.hxx"
@ -42,31 +41,23 @@ class RoarOutput {
AudioOutput base;
std::string host, name;
const std::string host, name;
roar_vs_t * vss;
int err;
int role;
int err = ROAR_ERROR_NONE;
const int role;
struct roar_connection con;
struct roar_audio_info info;
mutable Mutex mutex;
bool alive;
public:
RoarOutput()
:base(roar_output_plugin),
err(ROAR_ERROR_NONE) {}
RoarOutput(const ConfigBlock &block);
operator AudioOutput *() {
return &base;
}
bool Initialize(const ConfigBlock &block, Error &error) {
return base.Configure(block, error);
}
void Configure(const ConfigBlock &block);
bool Open(AudioFormat &audio_format, Error &error);
void Close();
@ -80,6 +71,24 @@ public:
static constexpr Domain roar_output_domain("roar_output");
gcc_pure
static int
GetConfiguredRole(const ConfigBlock &block)
{
const char *role = block.GetBlockValue("role");
return role != nullptr
? roar_str2role(role)
: ROAR_ROLE_MUSIC;
}
RoarOutput::RoarOutput(const ConfigBlock &block)
:base(roar_output_plugin, block),
host(block.GetBlockValue("server", "")),
name(block.GetBlockValue("name", "MPD")),
role(GetConfiguredRole(block))
{
}
inline int
RoarOutput::GetVolume() const
{
@ -124,30 +133,10 @@ roar_output_set_volume(RoarOutput &roar, unsigned volume)
roar.SetVolume(volume);
}
inline void
RoarOutput::Configure(const ConfigBlock &block)
{
host = block.GetBlockValue("server", "");
name = block.GetBlockValue("name", "MPD");
const char *_role = block.GetBlockValue("role", "music");
role = _role != nullptr
? roar_str2role(_role)
: ROAR_ROLE_MUSIC;
}
static AudioOutput *
roar_init(const ConfigBlock &block, Error &error)
roar_init(const ConfigBlock &block, Error &)
{
RoarOutput *self = new RoarOutput();
if (!self->Initialize(block, error)) {
delete self;
return nullptr;
}
self->Configure(block);
return *self;
return *new RoarOutput(block);
}
static void
@ -186,31 +175,24 @@ roar_use_audio_format(struct roar_audio_info *info,
}
inline bool
RoarOutput::Open(AudioFormat &audio_format, Error &error)
RoarOutput::Open(AudioFormat &audio_format, Error &)
{
const ScopeLock protect(mutex);
if (roar_simple_connect(&con,
host.empty() ? nullptr : host.c_str(),
name.c_str()) < 0) {
error.Set(roar_output_domain,
"Failed to connect to Roar server");
return false;
}
name.c_str()) < 0)
throw std::runtime_error("Failed to connect to Roar server");
vss = roar_vs_new_from_con(&con, &err);
if (vss == nullptr || err != ROAR_ERROR_NONE) {
error.Set(roar_output_domain, "Failed to connect to server");
return false;
}
if (vss == nullptr || err != ROAR_ERROR_NONE)
throw std::runtime_error("Failed to connect to server");
roar_use_audio_format(&info, audio_format);
if (roar_vs_stream(vss, &info, ROAR_DIR_PLAY, &err) < 0) {
error.Set(roar_output_domain, "Failed to start stream");
return false;
}
if (roar_vs_stream(vss, &info, ROAR_DIR_PLAY, &err) < 0)
throw std::runtime_error("Failed to start stream");
roar_vs_role(vss, role, &err);
alive = true;
@ -259,18 +241,14 @@ RoarOutput::Cancel()
}
inline size_t
RoarOutput::Play(const void *chunk, size_t size, Error &error)
RoarOutput::Play(const void *chunk, size_t size, Error &)
{
if (vss == nullptr) {
error.Set(roar_output_domain, "Connection is invalid");
return 0;
}
if (vss == nullptr)
throw std::runtime_error("Connection is invalid");
ssize_t nbytes = roar_vs_write(vss, chunk, size, &err);
if (nbytes <= 0) {
error.Set(roar_output_domain, "Failed to play data");
return 0;
}
if (nbytes <= 0)
throw std::runtime_error("Failed to play data");
return nbytes;
}