From 9e503b21c1d7bd2288bd8834ffd437b9c2200825 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Wed, 8 Feb 2017 19:35:27 +0100 Subject: [PATCH] {input,mixer}/alsa: move code to lib/alsa/NonBlock.cxx --- Makefile.am | 5 +++ src/input/plugins/AlsaInputPlugin.cxx | 16 +------ src/lib/alsa/NonBlock.cxx | 62 +++++++++++++++++++++++++++ src/lib/alsa/NonBlock.hxx | 50 +++++++++++++++++++++ src/mixer/plugins/AlsaMixerPlugin.cxx | 14 +----- 5 files changed, 121 insertions(+), 26 deletions(-) create mode 100644 src/lib/alsa/NonBlock.cxx create mode 100644 src/lib/alsa/NonBlock.hxx diff --git a/Makefile.am b/Makefile.am index 72628ce9a..3df7fa2f4 100644 --- a/Makefile.am +++ b/Makefile.am @@ -256,6 +256,9 @@ UPNP_SOURCES = \ src/lib/upnp/WorkQueue.hxx \ src/lib/upnp/Action.hxx +ALSA_SOURCES = \ + src/lib/alsa/NonBlock.cxx src/lib/alsa/NonBlock.hxx + # # Android native library # @@ -1294,6 +1297,7 @@ INPUT_LIBS = \ if ENABLE_ALSA libinput_a_SOURCES += \ + $(ALSA_SOURCES) \ src/input/plugins/AlsaInputPlugin.cxx \ src/input/plugins/AlsaInputPlugin.hxx INPUT_LIBS += $(ALSA_LIBS) @@ -1412,6 +1416,7 @@ liboutput_plugins_a_SOURCES += \ src/output/plugins/AlsaOutputPlugin.cxx \ src/output/plugins/AlsaOutputPlugin.hxx libmixer_plugins_a_SOURCES += \ + $(ALSA_SOURCES) \ src/mixer/plugins/volume_mapping.h \ src/mixer/plugins/volume_mapping.c \ src/mixer/plugins/AlsaMixerPlugin.cxx diff --git a/src/input/plugins/AlsaInputPlugin.cxx b/src/input/plugins/AlsaInputPlugin.cxx index fbbd15176..50235080e 100644 --- a/src/input/plugins/AlsaInputPlugin.cxx +++ b/src/input/plugins/AlsaInputPlugin.cxx @@ -26,6 +26,7 @@ #include "config.h" #include "AlsaInputPlugin.hxx" +#include "lib/alsa/NonBlock.hxx" #include "../InputPlugin.hxx" #include "../AsyncInputStream.hxx" #include "event/Call.hxx" @@ -177,20 +178,7 @@ AlsaInputStream::PrepareSockets() return std::chrono::steady_clock::duration(-1); } - int count = snd_pcm_poll_descriptors_count(capture_handle); - if (count <= 0) { - ClearSocketList(); - return std::chrono::steady_clock::duration(-1); - } - - struct pollfd *pfds = pfd_buffer.Get(count); - - count = snd_pcm_poll_descriptors(capture_handle, pfds, count); - if (count < 0) - count = 0; - - ReplaceSocketList(pfds, count); - return std::chrono::steady_clock::duration(-1); + return PrepareAlsaPcmSockets(*this, capture_handle, pfd_buffer); } void diff --git a/src/lib/alsa/NonBlock.cxx b/src/lib/alsa/NonBlock.cxx new file mode 100644 index 000000000..b2f9027ea --- /dev/null +++ b/src/lib/alsa/NonBlock.cxx @@ -0,0 +1,62 @@ +/* + * Copyright 2003-2017 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 "config.h" +#include "NonBlock.hxx" +#include "event/MultiSocketMonitor.hxx" + +std::chrono::steady_clock::duration +PrepareAlsaPcmSockets(MultiSocketMonitor &m, snd_pcm_t *pcm, + ReusableArray &pfd_buffer) +{ + int count = snd_pcm_poll_descriptors_count(pcm); + if (count <= 0) { + m.ClearSocketList(); + return std::chrono::steady_clock::duration(-1); + } + + struct pollfd *pfds = pfd_buffer.Get(count); + + count = snd_pcm_poll_descriptors(pcm, pfds, count); + if (count < 0) + count = 0; + + m.ReplaceSocketList(pfds, count); + return std::chrono::steady_clock::duration(-1); +} + +std::chrono::steady_clock::duration +PrepareAlsaMixerSockets(MultiSocketMonitor &m, snd_mixer_t *mixer, + ReusableArray &pfd_buffer) +{ + int count = snd_mixer_poll_descriptors_count(mixer); + if (count <= 0) { + m.ClearSocketList(); + return std::chrono::steady_clock::duration(-1); + } + + struct pollfd *pfds = pfd_buffer.Get(count); + + count = snd_mixer_poll_descriptors(mixer, pfds, count); + if (count < 0) + count = 0; + + m.ReplaceSocketList(pfds, count); + return std::chrono::steady_clock::duration(-1); +} diff --git a/src/lib/alsa/NonBlock.hxx b/src/lib/alsa/NonBlock.hxx new file mode 100644 index 000000000..da741e246 --- /dev/null +++ b/src/lib/alsa/NonBlock.hxx @@ -0,0 +1,50 @@ +/* + * Copyright 2003-2017 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. + */ + +#ifndef MPD_ALSA_NON_BLOCK_HXX +#define MPD_ALSA_NON_BLOCK_HXX + +#include "check.h" +#include "util/ReusableArray.hxx" + +#include + +#include + +class MultiSocketMonitor; + +/** + * Update #MultiSocketMonitor's socket list from + * snd_pcm_poll_descriptors(). To be called from + * MultiSocketMonitor::PrepareSockets(). + */ +std::chrono::steady_clock::duration +PrepareAlsaPcmSockets(MultiSocketMonitor &m, snd_pcm_t *pcm, + ReusableArray &pfd_buffer); + +/** + * Update #MultiSocketMonitor's socket list from + * snd_mixer_poll_descriptors(). To be called from + * MultiSocketMonitor::PrepareSockets(). + */ +std::chrono::steady_clock::duration +PrepareAlsaMixerSockets(MultiSocketMonitor &m, snd_mixer_t *mixer, + ReusableArray &pfd_buffer); + +#endif diff --git a/src/mixer/plugins/AlsaMixerPlugin.cxx b/src/mixer/plugins/AlsaMixerPlugin.cxx index 852326926..77bbb8cd9 100644 --- a/src/mixer/plugins/AlsaMixerPlugin.cxx +++ b/src/mixer/plugins/AlsaMixerPlugin.cxx @@ -18,6 +18,7 @@ */ #include "config.h" +#include "lib/alsa/NonBlock.hxx" #include "mixer/MixerInternal.hxx" #include "mixer/Listener.hxx" #include "output/OutputAPI.hxx" @@ -108,18 +109,7 @@ AlsaMixerMonitor::PrepareSockets() return std::chrono::steady_clock::duration(-1); } - int count = snd_mixer_poll_descriptors_count(mixer); - if (count <= 0) - count = 0; - - struct pollfd *pfds = pfd_buffer.Get(count); - - count = snd_mixer_poll_descriptors(mixer, pfds, count); - if (count < 0) - count = 0; - - ReplaceSocketList(pfds, count); - return std::chrono::steady_clock::duration(-1); + return PrepareAlsaMixerSockets(*this, mixer, pfd_buffer); } void