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