output/jack: migrate from class Error to C++ exceptions
This commit is contained in:
parent
cadd186f1b
commit
52aed3f8a1
@ -25,7 +25,7 @@
|
|||||||
#include "util/ScopeExit.hxx"
|
#include "util/ScopeExit.hxx"
|
||||||
#include "util/ConstBuffer.hxx"
|
#include "util/ConstBuffer.hxx"
|
||||||
#include "util/IterableSplitString.hxx"
|
#include "util/IterableSplitString.hxx"
|
||||||
#include "util/Error.hxx"
|
#include "util/RuntimeError.hxx"
|
||||||
#include "util/Domain.hxx"
|
#include "util/Domain.hxx"
|
||||||
#include "Log.hxx"
|
#include "Log.hxx"
|
||||||
|
|
||||||
@ -48,11 +48,11 @@ struct JackOutput {
|
|||||||
/**
|
/**
|
||||||
* libjack options passed to jack_client_open().
|
* libjack options passed to jack_client_open().
|
||||||
*/
|
*/
|
||||||
jack_options_t options;
|
jack_options_t options = JackNullOption;
|
||||||
|
|
||||||
const char *name;
|
const char *name;
|
||||||
|
|
||||||
const char *server_name;
|
const char *const server_name;
|
||||||
|
|
||||||
/* configuration */
|
/* configuration */
|
||||||
|
|
||||||
@ -80,12 +80,15 @@ struct JackOutput {
|
|||||||
*/
|
*/
|
||||||
bool pause;
|
bool pause;
|
||||||
|
|
||||||
JackOutput()
|
explicit JackOutput(const ConfigBlock &block);
|
||||||
:base(jack_output_plugin) {}
|
|
||||||
|
|
||||||
bool Configure(const ConfigBlock &block, Error &error);
|
/**
|
||||||
|
* Connect the JACK client and performs some basic setup
|
||||||
bool Connect(Error &error);
|
* (e.g. register callbacks).
|
||||||
|
*
|
||||||
|
* Throws #std::runtime_error on error.
|
||||||
|
*/
|
||||||
|
void Connect();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Disconnect the JACK client.
|
* Disconnect the JACK client.
|
||||||
@ -105,7 +108,10 @@ struct JackOutput {
|
|||||||
Stop();
|
Stop();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Start(Error &error);
|
/**
|
||||||
|
* Throws #std::runtime_error on error.
|
||||||
|
*/
|
||||||
|
void Start();
|
||||||
void Stop();
|
void Stop();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -135,6 +141,79 @@ struct JackOutput {
|
|||||||
|
|
||||||
static constexpr Domain jack_output_domain("jack_output");
|
static constexpr Domain jack_output_domain("jack_output");
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Throws #std::runtime_error on error.
|
||||||
|
*/
|
||||||
|
static unsigned
|
||||||
|
parse_port_list(const char *source, std::string dest[])
|
||||||
|
{
|
||||||
|
unsigned n = 0;
|
||||||
|
for (auto i : IterableSplitString(source, ',')) {
|
||||||
|
if (n >= MAX_PORTS)
|
||||||
|
throw std::runtime_error("too many port names");
|
||||||
|
|
||||||
|
dest[n++] = std::string(i.data, i.size);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (n == 0)
|
||||||
|
throw std::runtime_error("at least one port name expected");
|
||||||
|
|
||||||
|
return n;
|
||||||
|
}
|
||||||
|
|
||||||
|
JackOutput::JackOutput(const ConfigBlock &block)
|
||||||
|
:base(jack_output_plugin, block),
|
||||||
|
name(block.GetBlockValue("client_name", nullptr)),
|
||||||
|
server_name(block.GetBlockValue("server_name", nullptr))
|
||||||
|
{
|
||||||
|
if (name != nullptr)
|
||||||
|
options = jack_options_t(options | JackUseExactName);
|
||||||
|
else
|
||||||
|
/* if there's a no configured client name, we don't
|
||||||
|
care about the JackUseExactName option */
|
||||||
|
name = "Music Player Daemon";
|
||||||
|
|
||||||
|
if (server_name != nullptr)
|
||||||
|
options = jack_options_t(options | JackServerName);
|
||||||
|
|
||||||
|
if (!block.GetBlockValue("autostart", false))
|
||||||
|
options = jack_options_t(options | JackNoStartServer);
|
||||||
|
|
||||||
|
/* configure the source ports */
|
||||||
|
|
||||||
|
const char *value = block.GetBlockValue("source_ports", "left,right");
|
||||||
|
num_source_ports = parse_port_list(value, source_ports);
|
||||||
|
|
||||||
|
/* configure the destination ports */
|
||||||
|
|
||||||
|
value = block.GetBlockValue("destination_ports", nullptr);
|
||||||
|
if (value == nullptr) {
|
||||||
|
/* compatibility with MPD < 0.16 */
|
||||||
|
value = block.GetBlockValue("ports", nullptr);
|
||||||
|
if (value != nullptr)
|
||||||
|
FormatWarning(jack_output_domain,
|
||||||
|
"deprecated option 'ports' in line %d",
|
||||||
|
block.line);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (value != nullptr) {
|
||||||
|
num_destination_ports =
|
||||||
|
parse_port_list(value, destination_ports);
|
||||||
|
} else {
|
||||||
|
num_destination_ports = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (num_destination_ports > 0 &&
|
||||||
|
num_destination_ports != num_source_ports)
|
||||||
|
FormatWarning(jack_output_domain,
|
||||||
|
"number of source ports (%u) mismatches the "
|
||||||
|
"number of destination ports (%u) in line %d",
|
||||||
|
num_source_ports, num_destination_ports,
|
||||||
|
block.line);
|
||||||
|
|
||||||
|
ringbuffer_size = block.GetBlockValue("ringbuffer_size", 32768u);
|
||||||
|
}
|
||||||
|
|
||||||
inline jack_nframes_t
|
inline jack_nframes_t
|
||||||
JackOutput::GetAvailable() const
|
JackOutput::GetAvailable() const
|
||||||
{
|
{
|
||||||
@ -308,23 +387,16 @@ JackOutput::Disconnect()
|
|||||||
client = nullptr;
|
client = nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
void
|
||||||
* Connect the JACK client and performs some basic setup
|
JackOutput::Connect()
|
||||||
* (e.g. register callbacks).
|
|
||||||
*/
|
|
||||||
bool
|
|
||||||
JackOutput::Connect(Error &error)
|
|
||||||
{
|
{
|
||||||
shutdown = false;
|
shutdown = false;
|
||||||
|
|
||||||
jack_status_t status;
|
jack_status_t status;
|
||||||
client = jack_client_open(name, options, &status, server_name);
|
client = jack_client_open(name, options, &status, server_name);
|
||||||
if (client == nullptr) {
|
if (client == nullptr)
|
||||||
error.Format(jack_output_domain, status,
|
throw FormatRuntimeError("Failed to connect to JACK server, status=%d",
|
||||||
"Failed to connect to JACK server, status=%d",
|
status);
|
||||||
status);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
jack_set_process_callback(client, mpd_jack_process, this);
|
jack_set_process_callback(client, mpd_jack_process, this);
|
||||||
jack_on_shutdown(client, mpd_jack_shutdown, this);
|
jack_on_shutdown(client, mpd_jack_shutdown, this);
|
||||||
@ -335,15 +407,11 @@ JackOutput::Connect(Error &error)
|
|||||||
JACK_DEFAULT_AUDIO_TYPE,
|
JACK_DEFAULT_AUDIO_TYPE,
|
||||||
JackPortIsOutput, 0);
|
JackPortIsOutput, 0);
|
||||||
if (ports[i] == nullptr) {
|
if (ports[i] == nullptr) {
|
||||||
error.Format(jack_output_domain,
|
|
||||||
"Cannot register output port \"%s\"",
|
|
||||||
source_ports[i].c_str());
|
|
||||||
Disconnect();
|
Disconnect();
|
||||||
return false;
|
throw FormatRuntimeError("Cannot register output port \"%s\"",
|
||||||
|
source_ports[i].c_str());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool
|
static bool
|
||||||
@ -352,100 +420,14 @@ mpd_jack_test_default_device(void)
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
static unsigned
|
|
||||||
parse_port_list(const char *source, std::string dest[], Error &error)
|
|
||||||
{
|
|
||||||
unsigned n = 0;
|
|
||||||
for (auto i : IterableSplitString(source, ',')) {
|
|
||||||
if (n >= MAX_PORTS) {
|
|
||||||
error.Set(config_domain,
|
|
||||||
"too many port names");
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
dest[n++] = std::string(i.data, i.size);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (n == 0) {
|
|
||||||
error.Format(config_domain,
|
|
||||||
"at least one port name expected");
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
return n;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool
|
|
||||||
JackOutput::Configure(const ConfigBlock &block, Error &error)
|
|
||||||
{
|
|
||||||
if (!base.Configure(block, error))
|
|
||||||
return false;
|
|
||||||
|
|
||||||
options = JackNullOption;
|
|
||||||
|
|
||||||
name = block.GetBlockValue("client_name", nullptr);
|
|
||||||
if (name != nullptr)
|
|
||||||
options = jack_options_t(options | JackUseExactName);
|
|
||||||
else
|
|
||||||
/* if there's a no configured client name, we don't
|
|
||||||
care about the JackUseExactName option */
|
|
||||||
name = "Music Player Daemon";
|
|
||||||
|
|
||||||
server_name = block.GetBlockValue("server_name", nullptr);
|
|
||||||
if (server_name != nullptr)
|
|
||||||
options = jack_options_t(options | JackServerName);
|
|
||||||
|
|
||||||
if (!block.GetBlockValue("autostart", false))
|
|
||||||
options = jack_options_t(options | JackNoStartServer);
|
|
||||||
|
|
||||||
/* configure the source ports */
|
|
||||||
|
|
||||||
const char *value = block.GetBlockValue("source_ports", "left,right");
|
|
||||||
num_source_ports = parse_port_list(value, source_ports, error);
|
|
||||||
if (num_source_ports == 0)
|
|
||||||
return false;
|
|
||||||
|
|
||||||
/* configure the destination ports */
|
|
||||||
|
|
||||||
value = block.GetBlockValue("destination_ports", nullptr);
|
|
||||||
if (value == nullptr) {
|
|
||||||
/* compatibility with MPD < 0.16 */
|
|
||||||
value = block.GetBlockValue("ports", nullptr);
|
|
||||||
if (value != nullptr)
|
|
||||||
FormatWarning(jack_output_domain,
|
|
||||||
"deprecated option 'ports' in line %d",
|
|
||||||
block.line);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (value != nullptr) {
|
|
||||||
num_destination_ports =
|
|
||||||
parse_port_list(value, destination_ports, error);
|
|
||||||
if (num_destination_ports == 0)
|
|
||||||
return false;
|
|
||||||
} else {
|
|
||||||
num_destination_ports = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (num_destination_ports > 0 &&
|
|
||||||
num_destination_ports != num_source_ports)
|
|
||||||
FormatWarning(jack_output_domain,
|
|
||||||
"number of source ports (%u) mismatches the "
|
|
||||||
"number of destination ports (%u) in line %d",
|
|
||||||
num_source_ports, num_destination_ports,
|
|
||||||
block.line);
|
|
||||||
|
|
||||||
ringbuffer_size = block.GetBlockValue("ringbuffer_size", 32768u);
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
inline bool
|
inline bool
|
||||||
JackOutput::Enable(Error &error)
|
JackOutput::Enable(Error &)
|
||||||
{
|
{
|
||||||
for (unsigned i = 0; i < num_source_ports; ++i)
|
for (unsigned i = 0; i < num_source_ports; ++i)
|
||||||
ringbuffer[i] = nullptr;
|
ringbuffer[i] = nullptr;
|
||||||
|
|
||||||
return Connect(error);
|
Connect();
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void
|
inline void
|
||||||
@ -463,21 +445,15 @@ JackOutput::Disable()
|
|||||||
}
|
}
|
||||||
|
|
||||||
static AudioOutput *
|
static AudioOutput *
|
||||||
mpd_jack_init(const ConfigBlock &block, Error &error)
|
mpd_jack_init(const ConfigBlock &block, Error &)
|
||||||
{
|
{
|
||||||
JackOutput *jd = new JackOutput();
|
|
||||||
|
|
||||||
if (!jd->Configure(block, error)) {
|
|
||||||
delete jd;
|
|
||||||
return nullptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
jack_set_error_function(mpd_jack_error);
|
jack_set_error_function(mpd_jack_error);
|
||||||
|
|
||||||
#ifdef HAVE_JACK_SET_INFO_FUNCTION
|
#ifdef HAVE_JACK_SET_INFO_FUNCTION
|
||||||
jack_set_info_function(mpd_jack_info);
|
jack_set_info_function(mpd_jack_info);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
auto *jd = new JackOutput(block);
|
||||||
return &jd->base;
|
return &jd->base;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -498,8 +474,8 @@ JackOutput::Stop()
|
|||||||
jack_deactivate(client);
|
jack_deactivate(client);
|
||||||
}
|
}
|
||||||
|
|
||||||
inline bool
|
inline void
|
||||||
JackOutput::Start(Error &error)
|
JackOutput::Start()
|
||||||
{
|
{
|
||||||
assert(client != nullptr);
|
assert(client != nullptr);
|
||||||
assert(audio_format.channels <= num_source_ports);
|
assert(audio_format.channels <= num_source_ports);
|
||||||
@ -519,9 +495,8 @@ JackOutput::Start(Error &error)
|
|||||||
}
|
}
|
||||||
|
|
||||||
if ( jack_activate(client) ) {
|
if ( jack_activate(client) ) {
|
||||||
error.Set(jack_output_domain, "cannot activate client");
|
|
||||||
Stop();
|
Stop();
|
||||||
return false;
|
throw std::runtime_error("cannot activate client");
|
||||||
}
|
}
|
||||||
|
|
||||||
const char *dports[MAX_PORTS], **jports;
|
const char *dports[MAX_PORTS], **jports;
|
||||||
@ -532,9 +507,8 @@ JackOutput::Start(Error &error)
|
|||||||
jports = jack_get_ports(client, nullptr, nullptr,
|
jports = jack_get_ports(client, nullptr, nullptr,
|
||||||
JackPortIsPhysical | JackPortIsInput);
|
JackPortIsPhysical | JackPortIsInput);
|
||||||
if (jports == nullptr) {
|
if (jports == nullptr) {
|
||||||
error.Set(jack_output_domain, "no ports found");
|
|
||||||
Stop();
|
Stop();
|
||||||
return false;
|
throw std::runtime_error("no ports found");
|
||||||
}
|
}
|
||||||
|
|
||||||
assert(*jports != nullptr);
|
assert(*jports != nullptr);
|
||||||
@ -585,10 +559,9 @@ JackOutput::Start(Error &error)
|
|||||||
int ret = jack_connect(client, jack_port_name(ports[i]),
|
int ret = jack_connect(client, jack_port_name(ports[i]),
|
||||||
dports[i]);
|
dports[i]);
|
||||||
if (ret != 0) {
|
if (ret != 0) {
|
||||||
error.Format(jack_output_domain,
|
|
||||||
"Not a valid JACK port: %s", dports[i]);
|
|
||||||
Stop();
|
Stop();
|
||||||
return false;
|
throw FormatRuntimeError("Not a valid JACK port: %s",
|
||||||
|
dports[i]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -600,32 +573,29 @@ JackOutput::Start(Error &error)
|
|||||||
ret = jack_connect(client, jack_port_name(ports[0]),
|
ret = jack_connect(client, jack_port_name(ports[0]),
|
||||||
duplicate_port);
|
duplicate_port);
|
||||||
if (ret != 0) {
|
if (ret != 0) {
|
||||||
error.Format(jack_output_domain,
|
|
||||||
"Not a valid JACK port: %s",
|
|
||||||
duplicate_port);
|
|
||||||
Stop();
|
Stop();
|
||||||
return false;
|
throw FormatRuntimeError("Not a valid JACK port: %s",
|
||||||
|
duplicate_port);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
inline bool
|
inline bool
|
||||||
JackOutput::Open(AudioFormat &new_audio_format, Error &error)
|
JackOutput::Open(AudioFormat &new_audio_format, Error &)
|
||||||
{
|
{
|
||||||
pause = false;
|
pause = false;
|
||||||
|
|
||||||
if (client != nullptr && shutdown)
|
if (client != nullptr && shutdown)
|
||||||
Disconnect();
|
Disconnect();
|
||||||
|
|
||||||
if (client == nullptr && !Connect(error))
|
if (client == nullptr)
|
||||||
return false;
|
Connect();
|
||||||
|
|
||||||
set_audioformat(this, new_audio_format);
|
set_audioformat(this, new_audio_format);
|
||||||
audio_format = new_audio_format;
|
audio_format = new_audio_format;
|
||||||
|
|
||||||
return Start(error);
|
Start();
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline size_t
|
inline size_t
|
||||||
@ -670,7 +640,7 @@ JackOutput::WriteSamples(const float *src, size_t n_frames)
|
|||||||
}
|
}
|
||||||
|
|
||||||
inline size_t
|
inline size_t
|
||||||
JackOutput::Play(const void *chunk, size_t size, Error &error)
|
JackOutput::Play(const void *chunk, size_t size, Error &)
|
||||||
{
|
{
|
||||||
pause = false;
|
pause = false;
|
||||||
|
|
||||||
@ -679,12 +649,9 @@ JackOutput::Play(const void *chunk, size_t size, Error &error)
|
|||||||
size /= frame_size;
|
size /= frame_size;
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
if (shutdown) {
|
if (shutdown)
|
||||||
error.Set(jack_output_domain,
|
throw std::runtime_error("Refusing to play, because "
|
||||||
"Refusing to play, because "
|
"there is no client thread");
|
||||||
"there is no client thread");
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
size_t frames_written =
|
size_t frames_written =
|
||||||
WriteSamples((const float *)chunk, size);
|
WriteSamples((const float *)chunk, size);
|
||||||
|
Loading…
Reference in New Issue
Block a user