2009-03-12 18:34:37 +01:00
|
|
|
/*
|
2016-02-26 17:54:05 +01:00
|
|
|
* Copyright 2003-2016 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
|
|
|
*/
|
|
|
|
|
2009-11-12 09:12:38 +01:00
|
|
|
#include "config.h"
|
2013-02-22 20:51:23 +01:00
|
|
|
#include "MixerControl.hxx"
|
|
|
|
#include "MixerInternal.hxx"
|
2013-08-10 18:02:44 +02:00
|
|
|
#include "util/Error.hxx"
|
2013-04-16 23:46:36 +02:00
|
|
|
|
2009-03-12 18:34:38 +01:00
|
|
|
#include <assert.h>
|
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,
|
2014-02-06 21:10:12 +01:00
|
|
|
const MixerPlugin &plugin, AudioOutput &ao,
|
2014-02-05 23:20:33 +01:00
|
|
|
MixerListener &listener,
|
2015-01-21 22:13:44 +01:00
|
|
|
const ConfigBlock &block,
|
2013-08-10 18:02:44 +02:00
|
|
|
Error &error)
|
2009-03-14 11:36:50 +01:00
|
|
|
{
|
2015-01-21 22:13:44 +01:00
|
|
|
Mixer *mixer = plugin.init(event_loop, ao, listener, block, error);
|
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
|
2013-04-16 21:33:25 +02:00
|
|
|
mixer_free(Mixer *mixer)
|
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()) */
|
|
|
|
mixer_close(mixer);
|
|
|
|
|
2014-02-06 20:44:33 +01:00
|
|
|
delete mixer;
|
2009-03-14 11:36:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2013-08-10 18:02:44 +02:00
|
|
|
mixer_open(Mixer *mixer, Error &error)
|
2009-03-14 11:36:50 +01:00
|
|
|
{
|
2009-03-14 11:53:28 +01:00
|
|
|
bool success;
|
|
|
|
|
2013-10-19 18:19:03 +02:00
|
|
|
assert(mixer != nullptr);
|
2009-03-14 11:53:28 +01:00
|
|
|
|
2013-04-16 23:46:36 +02:00
|
|
|
const ScopeLock protect(mixer->mutex);
|
2009-03-26 19:43:18 +01:00
|
|
|
|
2014-02-06 20:44:33 +01:00
|
|
|
success = mixer->open || (mixer->open = mixer->Open(error));
|
2009-03-26 19:43:18 +01:00
|
|
|
|
2009-03-26 19:46:39 +01:00
|
|
|
mixer->failed = !success;
|
|
|
|
|
2009-03-14 11:53:28 +01:00
|
|
|
return success;
|
2009-03-14 11:36:50 +01:00
|
|
|
}
|
|
|
|
|
2009-03-27 16:44:31 +01:00
|
|
|
static void
|
2013-04-16 21:33:25 +02:00
|
|
|
mixer_close_internal(Mixer *mixer)
|
2009-03-27 16:44:31 +01:00
|
|
|
{
|
2013-10-19 18:19:03 +02:00
|
|
|
assert(mixer != nullptr);
|
2009-03-27 16:44:31 +01:00
|
|
|
assert(mixer->open);
|
|
|
|
|
2014-02-06 20:44:33 +01:00
|
|
|
mixer->Close();
|
2009-03-27 16:44:31 +01:00
|
|
|
mixer->open = false;
|
|
|
|
}
|
|
|
|
|
2009-03-14 11:36:50 +01:00
|
|
|
void
|
2013-04-16 21:33:25 +02:00
|
|
|
mixer_close(Mixer *mixer)
|
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
|
|
|
|
2013-04-16 23:46:36 +02:00
|
|
|
const ScopeLock protect(mixer->mutex);
|
2009-03-26 19:43:18 +01:00
|
|
|
|
2009-03-27 16:44:31 +01:00
|
|
|
if (mixer->open)
|
|
|
|
mixer_close_internal(mixer);
|
2009-03-14 11:36:50 +01:00
|
|
|
}
|
|
|
|
|
2009-03-26 19:43:18 +01:00
|
|
|
void
|
2013-04-16 21:33:25 +02:00
|
|
|
mixer_auto_close(Mixer *mixer)
|
2009-03-26 19:43:18 +01:00
|
|
|
{
|
2014-02-05 17:25:47 +01:00
|
|
|
if (!mixer->plugin.global)
|
2009-03-26 19:43:18 +01:00
|
|
|
mixer_close(mixer);
|
|
|
|
}
|
|
|
|
|
2009-03-26 19:46:33 +01:00
|
|
|
/*
|
|
|
|
* Close the mixer due to failure. The mutex must be locked before
|
|
|
|
* calling this function.
|
|
|
|
*/
|
|
|
|
static void
|
2013-04-16 21:33:25 +02:00
|
|
|
mixer_failed(Mixer *mixer)
|
2009-03-26 19:46:33 +01:00
|
|
|
{
|
|
|
|
assert(mixer->open);
|
|
|
|
|
2009-03-27 16:44:31 +01:00
|
|
|
mixer_close_internal(mixer);
|
2009-03-26 19:46:39 +01:00
|
|
|
|
|
|
|
mixer->failed = true;
|
2009-03-26 19:46:33 +01:00
|
|
|
}
|
|
|
|
|
2009-03-14 11:36:50 +01:00
|
|
|
int
|
2013-08-10 18:02:44 +02:00
|
|
|
mixer_get_volume(Mixer *mixer, Error &error)
|
2009-03-14 11:36:50 +01:00
|
|
|
{
|
2009-03-14 11:53:28 +01:00
|
|
|
int volume;
|
|
|
|
|
2013-10-19 18:19:03 +02:00
|
|
|
assert(mixer != nullptr);
|
2009-03-26 19:43:15 +01:00
|
|
|
|
2014-02-05 17:25:47 +01:00
|
|
|
if (mixer->plugin.global && !mixer->failed &&
|
2013-08-10 18:02:44 +02:00
|
|
|
!mixer_open(mixer, error))
|
2009-03-26 19:43:18 +01:00
|
|
|
return -1;
|
|
|
|
|
2013-04-16 23:46:36 +02:00
|
|
|
const ScopeLock protect(mixer->mutex);
|
2009-03-26 19:43:18 +01:00
|
|
|
|
|
|
|
if (mixer->open) {
|
2014-02-06 20:44:33 +01:00
|
|
|
volume = mixer->GetVolume(error);
|
2013-08-10 18:02:44 +02:00
|
|
|
if (volume < 0 && error.IsDefined())
|
2009-03-26 19:46:33 +01:00
|
|
|
mixer_failed(mixer);
|
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
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2013-08-10 18:02:44 +02:00
|
|
|
mixer_set_volume(Mixer *mixer, unsigned volume, Error &error)
|
2009-03-14 11:36:50 +01:00
|
|
|
{
|
2013-10-19 18:19:03 +02:00
|
|
|
assert(mixer != nullptr);
|
2009-03-27 20:10:39 +01:00
|
|
|
assert(volume <= 100);
|
2009-03-26 19:43:15 +01:00
|
|
|
|
2014-02-05 17:25:47 +01:00
|
|
|
if (mixer->plugin.global && !mixer->failed &&
|
2013-08-10 18:02:44 +02:00
|
|
|
!mixer_open(mixer, error))
|
2009-03-26 19:43:18 +01:00
|
|
|
return false;
|
|
|
|
|
2013-04-16 23:46:36 +02:00
|
|
|
const ScopeLock protect(mixer->mutex);
|
2009-03-14 11:53:28 +01:00
|
|
|
|
2014-02-06 20:44:33 +01:00
|
|
|
return mixer->open && mixer->SetVolume(volume, error);
|
2009-03-14 11:36:50 +01:00
|
|
|
}
|