2009-03-12 18:34:37 +01:00
|
|
|
/*
|
2022-07-14 17:58:12 +02:00
|
|
|
* Copyright 2003-2022 The Music Player Daemon Project
|
2009-03-12 18:34:37 +01:00
|
|
|
* 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.
|
2009-03-13 18:43:16 +01:00
|
|
|
*
|
|
|
|
* 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.
|
2009-03-12 18:34:37 +01:00
|
|
|
*/
|
|
|
|
|
2022-08-18 17:00:45 +02:00
|
|
|
#include "Control.hxx"
|
|
|
|
#include "Mixer.hxx"
|
2013-04-16 23:46:36 +02:00
|
|
|
|
2020-03-12 23:20:59 +01:00
|
|
|
#include <cassert>
|
2009-03-14 11:36:50 +01:00
|
|
|
|
2013-04-16 21:33:25 +02:00
|
|
|
Mixer *
|
2014-02-05 00:02:02 +01:00
|
|
|
mixer_new(EventLoop &event_loop,
|
2017-08-10 13:07:36 +02:00
|
|
|
const MixerPlugin &plugin, AudioOutput &ao,
|
2014-02-05 23:20:33 +01:00
|
|
|
MixerListener &listener,
|
2016-09-09 12:52:51 +02:00
|
|
|
const ConfigBlock &block)
|
2009-03-14 11:36:50 +01:00
|
|
|
{
|
2016-09-09 12:52:51 +02:00
|
|
|
Mixer *mixer = plugin.init(event_loop, ao, listener, block);
|
2009-03-14 11:36:50 +01:00
|
|
|
|
2014-02-05 17:25:47 +01:00
|
|
|
assert(mixer == nullptr || mixer->IsPlugin(plugin));
|
2009-03-14 11:36:50 +01:00
|
|
|
|
|
|
|
return mixer;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2022-08-18 14:16:50 +02:00
|
|
|
mixer_free(Mixer *mixer) noexcept
|
2009-03-14 11:36:50 +01:00
|
|
|
{
|
2013-10-19 18:19:03 +02:00
|
|
|
assert(mixer != nullptr);
|
2009-03-14 11:53:28 +01:00
|
|
|
|
2009-12-08 08:47:47 +01:00
|
|
|
/* mixers with the "global" flag set might still be open at
|
|
|
|
this point (see mixer_auto_close()) */
|
2022-08-18 14:14:58 +02:00
|
|
|
mixer_close(*mixer);
|
2009-12-08 08:47:47 +01:00
|
|
|
|
2014-02-06 20:44:33 +01:00
|
|
|
delete mixer;
|
2009-03-14 11:36:50 +01:00
|
|
|
}
|
|
|
|
|
2016-09-09 12:52:51 +02:00
|
|
|
void
|
2022-08-18 14:14:58 +02:00
|
|
|
mixer_open(Mixer &mixer)
|
2009-03-14 11:36:50 +01:00
|
|
|
{
|
2022-08-18 14:14:58 +02:00
|
|
|
const std::scoped_lock<Mutex> protect(mixer.mutex);
|
2009-03-26 19:43:18 +01:00
|
|
|
|
2022-08-18 14:14:58 +02:00
|
|
|
if (mixer.open)
|
2016-09-09 12:52:51 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
try {
|
2022-08-18 14:14:58 +02:00
|
|
|
mixer.Open();
|
|
|
|
mixer.open = true;
|
|
|
|
mixer.failure = {};
|
2016-09-09 12:52:51 +02:00
|
|
|
} catch (...) {
|
2022-08-18 14:14:58 +02:00
|
|
|
mixer.failure = std::current_exception();
|
2016-09-09 12:52:51 +02:00
|
|
|
throw;
|
|
|
|
}
|
2009-03-14 11:36:50 +01:00
|
|
|
}
|
|
|
|
|
2009-03-27 16:44:31 +01:00
|
|
|
static void
|
2022-08-18 14:16:50 +02:00
|
|
|
mixer_close_internal(Mixer &mixer) noexcept
|
2009-03-27 16:44:31 +01:00
|
|
|
{
|
2022-08-18 14:14:58 +02:00
|
|
|
assert(mixer.open);
|
2009-03-27 16:44:31 +01:00
|
|
|
|
2022-08-18 14:14:58 +02:00
|
|
|
mixer.Close();
|
|
|
|
mixer.open = false;
|
|
|
|
mixer.failure = {};
|
2009-03-27 16:44:31 +01:00
|
|
|
}
|
|
|
|
|
2009-03-14 11:36:50 +01:00
|
|
|
void
|
2022-08-18 14:16:50 +02:00
|
|
|
mixer_close(Mixer &mixer) noexcept
|
2009-03-14 11:36:50 +01:00
|
|
|
{
|
2022-08-18 14:14:58 +02:00
|
|
|
const std::scoped_lock<Mutex> protect(mixer.mutex);
|
2009-03-14 11:53:28 +01:00
|
|
|
|
2022-08-18 14:14:58 +02:00
|
|
|
if (mixer.open)
|
2009-03-27 16:44:31 +01:00
|
|
|
mixer_close_internal(mixer);
|
2009-03-14 11:36:50 +01:00
|
|
|
}
|
|
|
|
|
2009-03-26 19:43:18 +01:00
|
|
|
void
|
2022-08-18 14:16:50 +02:00
|
|
|
mixer_auto_close(Mixer &mixer) noexcept
|
2009-03-26 19:43:18 +01:00
|
|
|
{
|
2022-08-18 14:14:58 +02:00
|
|
|
if (!mixer.IsGlobal())
|
2009-03-26 19:43:18 +01:00
|
|
|
mixer_close(mixer);
|
|
|
|
}
|
|
|
|
|
2009-03-14 11:36:50 +01:00
|
|
|
int
|
2022-08-18 14:14:58 +02:00
|
|
|
mixer_get_volume(Mixer &mixer)
|
2009-03-14 11:36:50 +01:00
|
|
|
{
|
2009-03-14 11:53:28 +01:00
|
|
|
int volume;
|
|
|
|
|
2022-08-18 14:14:58 +02:00
|
|
|
if (mixer.IsGlobal() && !mixer.failure)
|
2016-09-09 12:52:51 +02:00
|
|
|
mixer_open(mixer);
|
2009-03-26 19:43:18 +01:00
|
|
|
|
2022-08-18 14:14:58 +02:00
|
|
|
const std::scoped_lock<Mutex> protect(mixer.mutex);
|
2009-03-26 19:43:18 +01:00
|
|
|
|
2022-08-18 14:14:58 +02:00
|
|
|
if (mixer.open) {
|
2016-09-09 12:52:51 +02:00
|
|
|
try {
|
2022-08-18 14:14:58 +02:00
|
|
|
volume = mixer.GetVolume();
|
2016-09-09 12:52:51 +02:00
|
|
|
} catch (...) {
|
2022-07-08 11:10:14 +02:00
|
|
|
mixer_close_internal(mixer);
|
2022-08-18 14:14:58 +02:00
|
|
|
mixer.failure = std::current_exception();
|
2016-09-09 12:52:51 +02:00
|
|
|
throw;
|
|
|
|
}
|
2009-03-26 19:43:18 +01:00
|
|
|
} else
|
|
|
|
volume = -1;
|
|
|
|
|
2009-03-14 11:53:28 +01:00
|
|
|
return volume;
|
2009-03-14 11:36:50 +01:00
|
|
|
}
|
|
|
|
|
2016-09-09 12:52:51 +02:00
|
|
|
void
|
2022-08-18 14:14:58 +02:00
|
|
|
mixer_set_volume(Mixer &mixer, unsigned volume)
|
2009-03-14 11:36:50 +01:00
|
|
|
{
|
2009-03-27 20:10:39 +01:00
|
|
|
assert(volume <= 100);
|
2009-03-26 19:43:15 +01:00
|
|
|
|
2022-08-18 14:14:58 +02:00
|
|
|
if (mixer.IsGlobal() && !mixer.failure)
|
2016-09-09 12:52:51 +02:00
|
|
|
mixer_open(mixer);
|
2009-03-26 19:43:18 +01:00
|
|
|
|
2022-08-18 14:14:58 +02:00
|
|
|
const std::scoped_lock<Mutex> protect(mixer.mutex);
|
2009-03-14 11:53:28 +01:00
|
|
|
|
2022-08-18 14:14:58 +02:00
|
|
|
if (mixer.open)
|
|
|
|
mixer.SetVolume(volume);
|
|
|
|
else if (mixer.failure)
|
|
|
|
std::rethrow_exception(mixer.failure);
|
2009-03-14 11:36:50 +01:00
|
|
|
}
|