From f447b7615e1df27dc8c3bc643a5d83d4d5ec8dcf Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Wed, 20 Oct 2021 11:07:46 +0200 Subject: [PATCH] output/pipewire: check pw_stream_connect() errors --- src/lib/pipewire/Error.cxx | 34 ++++++++++++++++ src/lib/pipewire/Error.hxx | 45 +++++++++++++++++++++ src/lib/pipewire/meson.build | 14 +++++++ src/output/plugins/PipeWireOutputPlugin.cxx | 20 +++++---- 4 files changed, 105 insertions(+), 8 deletions(-) create mode 100644 src/lib/pipewire/Error.cxx create mode 100644 src/lib/pipewire/Error.hxx diff --git a/src/lib/pipewire/Error.cxx b/src/lib/pipewire/Error.cxx new file mode 100644 index 000000000..e3b677b9c --- /dev/null +++ b/src/lib/pipewire/Error.cxx @@ -0,0 +1,34 @@ +/* + * Copyright 2003-2021 The Music Player Daemon Project + * http://www.musicpd.org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include "Error.hxx" + +#include + +namespace PipeWire { + +ErrorCategory error_category; + +std::string +ErrorCategory::message(int condition) const +{ + return spa_strerror(condition); +} + +} // namespace Avahi diff --git a/src/lib/pipewire/Error.hxx b/src/lib/pipewire/Error.hxx new file mode 100644 index 000000000..c48a304bc --- /dev/null +++ b/src/lib/pipewire/Error.hxx @@ -0,0 +1,45 @@ +/* + * Copyright 2003-2021 The Music Player Daemon Project + * http://www.musicpd.org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#pragma once + +#include + +struct AvahiClient; + +namespace PipeWire { + +class ErrorCategory final : public std::error_category { +public: + const char *name() const noexcept override { + return "pipewire"; + } + + std::string message(int condition) const override; +}; + +extern ErrorCategory error_category; + +inline std::system_error +MakeError(int error, const char *msg) noexcept +{ + return std::system_error(error, error_category, msg); +} + +} // namespace PipeWire diff --git a/src/lib/pipewire/meson.build b/src/lib/pipewire/meson.build index 0a98ed82f..2d6424fdc 100644 --- a/src/lib/pipewire/meson.build +++ b/src/lib/pipewire/meson.build @@ -12,3 +12,17 @@ pipewire_dep = declare_dependency( # disable it at the command line compile_args: ['-Wno-pedantic'], ) + +pipewire = static_library( + 'pipewire', + 'Error.cxx', + include_directories: inc, + dependencies: [ + pipewire_dep, + ], +) + +pipewire_dep = declare_dependency( + link_with: pipewire, + dependencies: pipewire_dep, +) diff --git a/src/output/plugins/PipeWireOutputPlugin.cxx b/src/output/plugins/PipeWireOutputPlugin.cxx index a21a6a912..a532ca7f6 100644 --- a/src/output/plugins/PipeWireOutputPlugin.cxx +++ b/src/output/plugins/PipeWireOutputPlugin.cxx @@ -18,6 +18,7 @@ */ #include "PipeWireOutputPlugin.hxx" +#include "lib/pipewire/Error.hxx" #include "lib/pipewire/ThreadLoop.hxx" #include "../OutputAPI.hxx" #include "../Error.hxx" @@ -488,14 +489,17 @@ PipeWireOutput::Open(AudioFormat &audio_format) params[0] = spa_format_audio_raw_build(&pod_builder, SPA_PARAM_EnumFormat, &raw); - pw_stream_connect(stream, - PW_DIRECTION_OUTPUT, - target_id, - (enum pw_stream_flags)(PW_STREAM_FLAG_AUTOCONNECT | - PW_STREAM_FLAG_INACTIVE | - PW_STREAM_FLAG_MAP_BUFFERS | - PW_STREAM_FLAG_RT_PROCESS), - params, 1); + int error = + pw_stream_connect(stream, + PW_DIRECTION_OUTPUT, + target_id, + (enum pw_stream_flags)(PW_STREAM_FLAG_AUTOCONNECT | + PW_STREAM_FLAG_INACTIVE | + PW_STREAM_FLAG_MAP_BUFFERS | + PW_STREAM_FLAG_RT_PROCESS), + params, 1); + if (error < 0) + throw PipeWire::MakeError(error, "Failed to connect stream"); } void