mpd/src/mixer/Control.cxx

133 lines
2.6 KiB
C++
Raw Normal View History

/*
2022-07-14 17:58:12 +02:00
* Copyright 2003-2022 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 "Control.hxx"
#include "Mixer.hxx"
#include <cassert>
2013-04-16 21:33:25 +02:00
Mixer *
mixer_new(EventLoop &event_loop,
const MixerPlugin &plugin, AudioOutput &ao,
MixerListener &listener,
2016-09-09 12:52:51 +02:00
const ConfigBlock &block)
{
2016-09-09 12:52:51 +02:00
Mixer *mixer = plugin.init(event_loop, ao, listener, block);
assert(mixer == nullptr || mixer->IsPlugin(plugin));
return mixer;
}
void
2022-08-18 14:16:50 +02:00
mixer_free(Mixer *mixer) noexcept
{
2013-10-19 18:19:03 +02:00
assert(mixer != nullptr);
/* mixers with the "global" flag set might still be open at
this point (see mixer_auto_close()) */
mixer_close(*mixer);
delete mixer;
}
2016-09-09 12:52:51 +02:00
void
mixer_open(Mixer &mixer)
{
const std::scoped_lock<Mutex> protect(mixer.mutex);
if (mixer.open)
2016-09-09 12:52:51 +02:00
return;
try {
mixer.Open();
mixer.open = true;
mixer.failure = {};
2016-09-09 12:52:51 +02:00
} catch (...) {
mixer.failure = std::current_exception();
2016-09-09 12:52:51 +02:00
throw;
}
}
static void
2022-08-18 14:16:50 +02:00
mixer_close_internal(Mixer &mixer) noexcept
{
assert(mixer.open);
mixer.Close();
mixer.open = false;
mixer.failure = {};
}
void
2022-08-18 14:16:50 +02:00
mixer_close(Mixer &mixer) noexcept
{
const std::scoped_lock<Mutex> protect(mixer.mutex);
if (mixer.open)
mixer_close_internal(mixer);
}
void
2022-08-18 14:16:50 +02:00
mixer_auto_close(Mixer &mixer) noexcept
{
if (!mixer.IsGlobal())
mixer_close(mixer);
}
int
mixer_get_volume(Mixer &mixer)
{
int volume;
if (mixer.IsGlobal() && !mixer.failure)
2016-09-09 12:52:51 +02:00
mixer_open(mixer);
const std::scoped_lock<Mutex> protect(mixer.mutex);
if (mixer.open) {
2016-09-09 12:52:51 +02:00
try {
volume = mixer.GetVolume();
2016-09-09 12:52:51 +02:00
} catch (...) {
mixer_close_internal(mixer);
mixer.failure = std::current_exception();
2016-09-09 12:52:51 +02:00
throw;
}
} else
volume = -1;
return volume;
}
2016-09-09 12:52:51 +02:00
void
mixer_set_volume(Mixer &mixer, unsigned volume)
{
assert(volume <= 100);
if (mixer.IsGlobal() && !mixer.failure)
2016-09-09 12:52:51 +02:00
mixer_open(mixer);
const std::scoped_lock<Mutex> protect(mixer.mutex);
if (mixer.open)
mixer.SetVolume(volume);
else if (mixer.failure)
std::rethrow_exception(mixer.failure);
}