lib/pulse/Error: use std::system_error

This commit is contained in:
Max Kellermann 2022-11-29 09:51:26 +01:00
parent e455b93fac
commit cf3f3a7750
3 changed files with 57 additions and 26 deletions

View File

@ -23,9 +23,20 @@
#include <pulse/context.h> #include <pulse/context.h>
#include <pulse/error.h> #include <pulse/error.h>
std::runtime_error namespace Pulse {
MakePulseError(pa_context *context, const char *prefix) noexcept
std::string
ErrorCategory::message(int condition) const
{ {
const int e = pa_context_errno(context); return pa_strerror(condition);
return FormatRuntimeError("%s: %s", prefix, pa_strerror(e));
} }
ErrorCategory error_category;
std::system_error
MakeError(pa_context *context, const char *msg) noexcept
{
return MakeError(pa_context_errno(context), msg);
}
} // namespace Pulse

View File

@ -17,14 +17,34 @@
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/ */
#ifndef MPD_PULSE_ERROR_HXX #pragma once
#define MPD_PULSE_ERROR_HXX
#include <stdexcept> #include <system_error>
struct pa_context; struct pa_context;
std::runtime_error namespace Pulse {
MakePulseError(pa_context *context, const char *prefix) noexcept;
#endif class ErrorCategory final : public std::error_category {
public:
const char *name() const noexcept override {
return "pulse";
}
std::string message(int condition) const override;
};
extern ErrorCategory error_category;
[[nodiscard]] [[gnu::pure]]
inline std::system_error
MakeError(int error, const char *msg) noexcept
{
return std::system_error(error, error_category, msg);
}
[[nodiscard]] [[gnu::pure]]
std::system_error
MakeError(pa_context *context, const char *msg) noexcept;
} // namespace Pulse

View File

@ -368,8 +368,8 @@ PulseOutput::Connect()
if (pa_context_connect(context, server, if (pa_context_connect(context, server,
(pa_context_flags_t)0, nullptr) < 0) (pa_context_flags_t)0, nullptr) < 0)
throw MakePulseError(context, throw Pulse::MakeError(context,
"pa_context_connect() has failed"); "pa_context_connect() has failed");
} }
void void
@ -501,8 +501,8 @@ PulseOutput::WaitConnection()
case PA_CONTEXT_FAILED: case PA_CONTEXT_FAILED:
/* failure */ /* failure */
{ {
auto e = MakePulseError(context, auto e = Pulse::MakeError(context,
"failed to connect"); "failed to connect");
DeleteContext(); DeleteContext();
throw e; throw e;
} }
@ -603,8 +603,8 @@ PulseOutput::SetupStream(const pa_sample_spec &ss)
PA_CHANNEL_MAP_WAVEEX); PA_CHANNEL_MAP_WAVEEX);
stream = pa_stream_new(context, name, &ss, &chan_map); stream = pa_stream_new(context, name, &ss, &chan_map);
if (stream == nullptr) if (stream == nullptr)
throw MakePulseError(context, throw Pulse::MakeError(context,
"pa_stream_new() has failed"); "pa_stream_new() has failed");
pa_stream_set_suspended_callback(stream, pa_stream_set_suspended_callback(stream,
pulse_output_stream_suspended_cb, pulse_output_stream_suspended_cb,
@ -682,8 +682,8 @@ PulseOutput::Open(AudioFormat &audio_format)
nullptr, nullptr) < 0) { nullptr, nullptr) < 0) {
DeleteStream(); DeleteStream();
throw MakePulseError(context, throw Pulse::MakeError(context,
"pa_stream_connect_playback() has failed"); "pa_stream_connect_playback() has failed");
} }
interrupted = false; interrupted = false;
@ -729,8 +729,8 @@ PulseOutput::WaitStream()
case PA_STREAM_FAILED: case PA_STREAM_FAILED:
case PA_STREAM_TERMINATED: case PA_STREAM_TERMINATED:
case PA_STREAM_UNCONNECTED: case PA_STREAM_UNCONNECTED:
throw MakePulseError(context, throw Pulse::MakeError(context,
"failed to connect the stream"); "failed to connect the stream");
case PA_STREAM_CREATING: case PA_STREAM_CREATING:
if (interrupted) if (interrupted)
@ -752,12 +752,12 @@ PulseOutput::StreamPause(bool _pause)
pa_operation *o = pa_stream_cork(stream, _pause, pa_operation *o = pa_stream_cork(stream, _pause,
pulse_output_stream_success_cb, this); pulse_output_stream_success_cb, this);
if (o == nullptr) if (o == nullptr)
throw MakePulseError(context, throw Pulse::MakeError(context,
"pa_stream_cork() has failed"); "pa_stream_cork() has failed");
if (!pulse_wait_for_operation(mainloop, o)) if (!pulse_wait_for_operation(mainloop, o))
throw MakePulseError(context, throw Pulse::MakeError(context,
"pa_stream_cork() has failed"); "pa_stream_cork() has failed");
} }
std::chrono::steady_clock::duration std::chrono::steady_clock::duration
@ -819,7 +819,7 @@ PulseOutput::Play(std::span<const std::byte> src)
int result = pa_stream_write(stream, src.data(), src.size(), nullptr, int result = pa_stream_write(stream, src.data(), src.size(), nullptr,
0, PA_SEEK_RELATIVE); 0, PA_SEEK_RELATIVE);
if (result < 0) if (result < 0)
throw MakePulseError(context, "pa_stream_write() failed"); throw Pulse::MakeError(context, "pa_stream_write() failed");
return src.size(); return src.size();
} }
@ -838,7 +838,7 @@ PulseOutput::Drain()
pa_stream_drain(stream, pa_stream_drain(stream,
pulse_output_stream_success_cb, this); pulse_output_stream_success_cb, this);
if (o == nullptr) if (o == nullptr)
throw MakePulseError(context, "pa_stream_drain() failed"); throw Pulse::MakeError(context, "pa_stream_drain() failed");
pulse_wait_for_operation(mainloop, o); pulse_wait_for_operation(mainloop, o);
} }