2009-03-13 18:43:16 +01:00
|
|
|
/*
|
2011-01-29 10:13:54 +01:00
|
|
|
* Copyright (C) 2003-2011 The Music Player Daemon Project
|
2009-03-13 18:43:16 +01:00
|
|
|
* http://www.musicpd.org
|
2008-09-09 10:02:34 +02:00
|
|
|
*
|
|
|
|
* 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.
|
2008-09-09 10:02:34 +02:00
|
|
|
*/
|
|
|
|
|
2009-11-12 09:12:38 +01:00
|
|
|
#include "config.h"
|
2008-09-09 10:02:34 +02:00
|
|
|
#include "output_control.h"
|
|
|
|
#include "output_api.h"
|
2008-09-24 07:20:55 +02:00
|
|
|
#include "output_internal.h"
|
2008-09-09 10:02:34 +02:00
|
|
|
#include "output_list.h"
|
2009-02-11 18:00:41 +01:00
|
|
|
#include "audio_parser.h"
|
2009-03-26 18:23:23 +01:00
|
|
|
#include "mixer_control.h"
|
2009-07-06 22:00:50 +02:00
|
|
|
#include "mixer_type.h"
|
|
|
|
#include "mixer_list.h"
|
|
|
|
#include "mixer/software_mixer_plugin.h"
|
2009-07-06 10:01:47 +02:00
|
|
|
#include "filter_plugin.h"
|
|
|
|
#include "filter_registry.h"
|
2009-12-14 21:09:28 +01:00
|
|
|
#include "filter_config.h"
|
2009-07-06 10:01:47 +02:00
|
|
|
#include "filter/chain_filter_plugin.h"
|
2009-12-14 21:36:25 +01:00
|
|
|
#include "filter/autoconvert_filter_plugin.h"
|
2010-02-16 08:55:37 +01:00
|
|
|
#include "filter/replay_gain_filter_plugin.h"
|
2008-09-09 10:02:34 +02:00
|
|
|
|
2008-11-25 17:47:46 +01:00
|
|
|
#include <glib.h>
|
|
|
|
|
2009-07-06 10:01:47 +02:00
|
|
|
#include <assert.h>
|
|
|
|
|
2009-02-25 19:53:38 +01:00
|
|
|
#undef G_LOG_DOMAIN
|
|
|
|
#define G_LOG_DOMAIN "output"
|
|
|
|
|
2008-09-09 10:02:34 +02:00
|
|
|
#define AUDIO_OUTPUT_TYPE "type"
|
|
|
|
#define AUDIO_OUTPUT_NAME "name"
|
|
|
|
#define AUDIO_OUTPUT_FORMAT "format"
|
2009-12-13 22:49:39 +01:00
|
|
|
#define AUDIO_FILTERS "filters"
|
2008-09-09 10:02:34 +02:00
|
|
|
|
2009-03-01 13:31:32 +01:00
|
|
|
static const struct audio_output_plugin *
|
2009-03-01 13:31:56 +01:00
|
|
|
audio_output_detect(GError **error)
|
2009-03-01 13:31:32 +01:00
|
|
|
{
|
|
|
|
g_warning("Attempt to detect audio output device");
|
|
|
|
|
2012-06-12 20:29:47 +02:00
|
|
|
audio_output_plugins_for_each(plugin) {
|
2009-03-01 13:31:32 +01:00
|
|
|
if (plugin->test_default_device == NULL)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
g_warning("Attempting to detect a %s audio device",
|
|
|
|
plugin->name);
|
|
|
|
if (ao_plugin_test_default_device(plugin))
|
|
|
|
return plugin;
|
|
|
|
}
|
|
|
|
|
2009-03-01 13:31:56 +01:00
|
|
|
g_set_error(error, audio_output_quark(), 0,
|
|
|
|
"Unable to detect an audio device");
|
2009-03-01 13:31:32 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2009-07-06 22:00:50 +02:00
|
|
|
/**
|
|
|
|
* Determines the mixer type which should be used for the specified
|
|
|
|
* configuration block.
|
|
|
|
*
|
|
|
|
* This handles the deprecated options mixer_type (global) and
|
|
|
|
* mixer_enabled, if the mixer_type setting is not configured.
|
|
|
|
*/
|
|
|
|
static enum mixer_type
|
|
|
|
audio_output_mixer_type(const struct config_param *param)
|
|
|
|
{
|
|
|
|
/* read the local "mixer_type" setting */
|
|
|
|
const char *p = config_get_block_string(param, "mixer_type", NULL);
|
|
|
|
if (p != NULL)
|
|
|
|
return mixer_type_parse(p);
|
|
|
|
|
|
|
|
/* try the local "mixer_enabled" setting next (deprecated) */
|
|
|
|
if (!config_get_block_bool(param, "mixer_enabled", true))
|
|
|
|
return MIXER_TYPE_NONE;
|
|
|
|
|
|
|
|
/* fall back to the global "mixer_type" setting (also
|
|
|
|
deprecated) */
|
|
|
|
return mixer_type_parse(config_get_string("mixer_type", "hardware"));
|
|
|
|
}
|
|
|
|
|
2009-07-05 18:44:37 +02:00
|
|
|
static struct mixer *
|
2011-09-16 23:31:48 +02:00
|
|
|
audio_output_load_mixer(struct audio_output *ao,
|
|
|
|
const struct config_param *param,
|
2009-07-06 22:00:50 +02:00
|
|
|
const struct mixer_plugin *plugin,
|
2009-10-20 22:10:56 +02:00
|
|
|
struct filter *filter_chain,
|
|
|
|
GError **error_r)
|
2009-07-05 18:44:37 +02:00
|
|
|
{
|
2009-07-06 22:00:50 +02:00
|
|
|
struct mixer *mixer;
|
2009-07-05 18:44:37 +02:00
|
|
|
|
2009-07-06 22:00:50 +02:00
|
|
|
switch (audio_output_mixer_type(param)) {
|
|
|
|
case MIXER_TYPE_NONE:
|
|
|
|
case MIXER_TYPE_UNKNOWN:
|
2009-07-05 18:44:37 +02:00
|
|
|
return NULL;
|
|
|
|
|
2009-07-06 22:00:50 +02:00
|
|
|
case MIXER_TYPE_HARDWARE:
|
|
|
|
if (plugin == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
2009-10-21 09:48:41 +02:00
|
|
|
return mixer_new(plugin, ao, param, error_r);
|
2009-07-06 22:00:50 +02:00
|
|
|
|
|
|
|
case MIXER_TYPE_SOFTWARE:
|
2009-10-21 09:48:41 +02:00
|
|
|
mixer = mixer_new(&software_mixer_plugin, NULL, NULL, NULL);
|
2009-07-06 22:00:50 +02:00
|
|
|
assert(mixer != NULL);
|
|
|
|
|
|
|
|
filter_chain_append(filter_chain,
|
|
|
|
software_mixer_get_filter(mixer));
|
|
|
|
return mixer;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(false);
|
|
|
|
return NULL;
|
2009-07-05 18:44:37 +02:00
|
|
|
}
|
|
|
|
|
2009-02-25 19:53:56 +01:00
|
|
|
bool
|
2011-09-16 23:31:48 +02:00
|
|
|
ao_base_init(struct audio_output *ao,
|
|
|
|
const struct audio_output_plugin *plugin,
|
|
|
|
const struct config_param *param, GError **error_r)
|
2008-09-09 10:02:34 +02:00
|
|
|
{
|
2009-11-03 21:08:48 +01:00
|
|
|
assert(ao != NULL);
|
2011-09-16 23:31:48 +02:00
|
|
|
assert(plugin != NULL);
|
|
|
|
assert(plugin->finish != NULL);
|
|
|
|
assert(plugin->open != NULL);
|
|
|
|
assert(plugin->close != NULL);
|
|
|
|
assert(plugin->play != NULL);
|
2009-11-03 21:08:48 +01:00
|
|
|
|
2009-10-20 22:10:56 +02:00
|
|
|
GError *error = NULL;
|
2008-09-09 10:02:34 +02:00
|
|
|
|
|
|
|
if (param) {
|
2009-07-06 21:50:13 +02:00
|
|
|
const char *p;
|
2008-09-09 10:02:34 +02:00
|
|
|
|
2009-03-01 13:32:42 +01:00
|
|
|
ao->name = config_get_block_string(param, AUDIO_OUTPUT_NAME,
|
|
|
|
NULL);
|
|
|
|
if (ao->name == NULL) {
|
2009-10-20 21:26:28 +02:00
|
|
|
g_set_error(error_r, audio_output_quark(), 0,
|
2009-03-01 13:32:42 +01:00
|
|
|
"Missing \"name\" configuration");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-07-06 21:50:13 +02:00
|
|
|
p = config_get_block_string(param, AUDIO_OUTPUT_FORMAT,
|
2009-03-01 13:32:42 +01:00
|
|
|
NULL);
|
2009-07-06 21:50:13 +02:00
|
|
|
if (p != NULL) {
|
2009-07-06 21:40:43 +02:00
|
|
|
bool success =
|
2009-10-21 22:37:28 +02:00
|
|
|
audio_format_parse(&ao->config_audio_format,
|
2009-10-21 23:01:04 +02:00
|
|
|
p, true, error_r);
|
2009-07-06 21:40:43 +02:00
|
|
|
if (!success)
|
|
|
|
return false;
|
2009-10-21 22:37:28 +02:00
|
|
|
} else
|
|
|
|
audio_format_clear(&ao->config_audio_format);
|
2008-09-09 10:02:34 +02:00
|
|
|
} else {
|
2009-03-01 13:32:42 +01:00
|
|
|
ao->name = "default detected output";
|
2009-10-21 22:37:28 +02:00
|
|
|
|
|
|
|
audio_format_clear(&ao->config_audio_format);
|
2008-09-09 10:02:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ao->plugin = plugin;
|
2010-03-03 20:29:33 +01:00
|
|
|
ao->always_on = config_get_block_bool(param, "always_on", false);
|
2009-02-28 19:40:39 +01:00
|
|
|
ao->enabled = config_get_block_bool(param, "enabled", true);
|
2009-10-23 10:55:52 +02:00
|
|
|
ao->really_enabled = false;
|
2008-10-29 20:40:27 +01:00
|
|
|
ao->open = false;
|
2009-08-18 11:32:54 +02:00
|
|
|
ao->pause = false;
|
2011-09-01 07:13:21 +02:00
|
|
|
ao->allow_play = true;
|
2009-02-28 20:43:23 +01:00
|
|
|
ao->fail_timer = NULL;
|
2008-09-09 10:02:34 +02:00
|
|
|
|
2010-05-02 15:31:31 +02:00
|
|
|
pcm_buffer_init(&ao->cross_fade_buffer);
|
|
|
|
|
2009-07-06 10:01:47 +02:00
|
|
|
/* set up the filter chain */
|
|
|
|
|
|
|
|
ao->filter = filter_chain_new();
|
|
|
|
assert(ao->filter != NULL);
|
2009-12-14 21:36:25 +01:00
|
|
|
|
2010-02-14 17:04:39 +01:00
|
|
|
/* create the normalization filter (if configured) */
|
|
|
|
|
2009-12-14 21:36:25 +01:00
|
|
|
if (config_get_bool(CONF_VOLUME_NORMALIZATION, false)) {
|
|
|
|
struct filter *normalize_filter =
|
|
|
|
filter_new(&normalize_filter_plugin, NULL, NULL);
|
|
|
|
assert(normalize_filter != NULL);
|
|
|
|
|
|
|
|
filter_chain_append(ao->filter,
|
|
|
|
autoconvert_filter_new(normalize_filter));
|
|
|
|
}
|
|
|
|
|
2009-12-13 22:49:39 +01:00
|
|
|
filter_chain_parse(ao->filter,
|
2009-12-14 21:46:45 +01:00
|
|
|
config_get_block_string(param, AUDIO_FILTERS, ""),
|
|
|
|
&error
|
2009-12-13 22:49:39 +01:00
|
|
|
);
|
2009-07-06 10:01:47 +02:00
|
|
|
|
2009-12-14 21:46:45 +01:00
|
|
|
// It's not really fatal - Part of the filter chain has been set up already
|
|
|
|
// and even an empty one will work (if only with unexpected behaviour)
|
|
|
|
if (error != NULL) {
|
|
|
|
g_warning("Failed to initialize filter chain for '%s': %s",
|
|
|
|
ao->name, error->message);
|
|
|
|
g_error_free(error);
|
|
|
|
}
|
|
|
|
|
2008-12-28 22:09:42 +01:00
|
|
|
ao->thread = NULL;
|
2008-09-24 07:20:26 +02:00
|
|
|
ao->command = AO_COMMAND_NONE;
|
2009-03-09 19:25:26 +01:00
|
|
|
ao->mutex = g_mutex_new();
|
2009-10-29 17:06:40 +01:00
|
|
|
ao->cond = g_cond_new();
|
2008-09-24 07:20:26 +02:00
|
|
|
|
2011-09-16 23:31:48 +02:00
|
|
|
ao->mixer = NULL;
|
2011-10-23 08:51:33 +02:00
|
|
|
ao->replay_gain_filter = NULL;
|
|
|
|
ao->other_replay_gain_filter = NULL;
|
2011-09-16 23:31:48 +02:00
|
|
|
|
|
|
|
/* the "convert" filter must be the last one in the chain */
|
|
|
|
|
|
|
|
ao->convert_filter = filter_new(&convert_filter_plugin, NULL, NULL);
|
|
|
|
assert(ao->convert_filter != NULL);
|
|
|
|
|
|
|
|
filter_chain_append(ao->filter, ao->convert_filter);
|
|
|
|
|
|
|
|
/* done */
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
audio_output_setup(struct audio_output *ao, const struct config_param *param,
|
|
|
|
GError **error_r)
|
|
|
|
{
|
|
|
|
|
|
|
|
/* create the replay_gain filter */
|
2008-09-09 10:02:34 +02:00
|
|
|
|
2011-09-16 23:31:48 +02:00
|
|
|
const char *replay_gain_handler =
|
|
|
|
config_get_block_string(param, "replay_gain_handler",
|
|
|
|
"software");
|
|
|
|
|
|
|
|
if (strcmp(replay_gain_handler, "none") != 0) {
|
|
|
|
ao->replay_gain_filter = filter_new(&replay_gain_filter_plugin,
|
|
|
|
param, NULL);
|
|
|
|
assert(ao->replay_gain_filter != NULL);
|
|
|
|
|
|
|
|
ao->replay_gain_serial = 0;
|
|
|
|
|
|
|
|
ao->other_replay_gain_filter = filter_new(&replay_gain_filter_plugin,
|
|
|
|
param, NULL);
|
|
|
|
assert(ao->other_replay_gain_filter != NULL);
|
|
|
|
|
|
|
|
ao->other_replay_gain_serial = 0;
|
|
|
|
} else {
|
|
|
|
ao->replay_gain_filter = NULL;
|
|
|
|
ao->other_replay_gain_filter = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* set up the mixer */
|
|
|
|
|
|
|
|
GError *error = NULL;
|
|
|
|
ao->mixer = audio_output_load_mixer(ao, param,
|
|
|
|
ao->plugin->mixer_plugin,
|
2009-10-20 22:10:56 +02:00
|
|
|
ao->filter, &error);
|
|
|
|
if (ao->mixer == NULL && error != NULL) {
|
|
|
|
g_warning("Failed to initialize hardware mixer for '%s': %s",
|
|
|
|
ao->name, error->message);
|
|
|
|
g_error_free(error);
|
|
|
|
}
|
2009-03-26 18:23:23 +01:00
|
|
|
|
2010-02-16 08:55:37 +01:00
|
|
|
/* use the hardware mixer for replay gain? */
|
|
|
|
|
|
|
|
if (strcmp(replay_gain_handler, "mixer") == 0) {
|
|
|
|
if (ao->mixer != NULL)
|
|
|
|
replay_gain_filter_set_mixer(ao->replay_gain_filter,
|
|
|
|
ao->mixer, 100);
|
|
|
|
else
|
|
|
|
g_warning("No such mixer for output '%s'", ao->name);
|
|
|
|
} else if (strcmp(replay_gain_handler, "software") != 0 &&
|
|
|
|
ao->replay_gain_filter != NULL) {
|
|
|
|
g_set_error(error_r, audio_output_quark(), 0,
|
|
|
|
"Invalid \"replay_gain_handler\" value");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-09-16 23:31:48 +02:00
|
|
|
return true;
|
|
|
|
}
|
2009-07-06 21:50:25 +02:00
|
|
|
|
2011-09-16 23:31:48 +02:00
|
|
|
struct audio_output *
|
|
|
|
audio_output_new(const struct config_param *param,
|
|
|
|
struct player_control *pc,
|
|
|
|
GError **error_r)
|
|
|
|
{
|
|
|
|
const struct audio_output_plugin *plugin;
|
2009-07-06 21:50:25 +02:00
|
|
|
|
2011-09-16 23:31:48 +02:00
|
|
|
if (param) {
|
|
|
|
const char *p;
|
2009-07-06 21:50:25 +02:00
|
|
|
|
2011-09-16 23:31:48 +02:00
|
|
|
p = config_get_block_string(param, AUDIO_OUTPUT_TYPE, NULL);
|
|
|
|
if (p == NULL) {
|
|
|
|
g_set_error(error_r, audio_output_quark(), 0,
|
|
|
|
"Missing \"type\" configuration");
|
|
|
|
return false;
|
|
|
|
}
|
2009-07-06 21:50:25 +02:00
|
|
|
|
2011-09-16 23:31:48 +02:00
|
|
|
plugin = audio_output_plugin_get(p);
|
|
|
|
if (plugin == NULL) {
|
|
|
|
g_set_error(error_r, audio_output_quark(), 0,
|
|
|
|
"No such audio output plugin: %s", p);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
g_warning("No \"%s\" defined in config file\n",
|
|
|
|
CONF_AUDIO_OUTPUT);
|
|
|
|
|
|
|
|
plugin = audio_output_detect(error_r);
|
|
|
|
if (plugin == NULL)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
g_message("Successfully detected a %s audio device",
|
|
|
|
plugin->name);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct audio_output *ao = ao_plugin_init(plugin, param, error_r);
|
|
|
|
if (ao == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (!audio_output_setup(ao, param, error_r)) {
|
|
|
|
ao_plugin_finish(ao);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
ao->player_control = pc;
|
|
|
|
return ao;
|
2008-09-09 10:02:34 +02:00
|
|
|
}
|