2009-03-13 18:43:16 +01:00
|
|
|
/*
|
2013-04-17 00:56:09 +02:00
|
|
|
* Copyright (C) 2003-2013 The Music Player Daemon Project
|
2009-03-13 18:43:16 +01:00
|
|
|
* http://www.musicpd.org
|
2004-09-08 04:16:08 +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.
|
2004-09-08 04:16:08 +02:00
|
|
|
*/
|
|
|
|
|
2009-11-12 09:12:38 +01:00
|
|
|
#include "config.h"
|
2013-04-17 00:56:09 +02:00
|
|
|
#include "AoOutputPlugin.hxx"
|
2013-07-30 08:34:10 +02:00
|
|
|
#include "OutputAPI.hxx"
|
2004-09-08 04:16:08 +02:00
|
|
|
|
|
|
|
#include <ao/ao.h>
|
2008-11-25 16:10:01 +01:00
|
|
|
#include <glib.h>
|
|
|
|
|
2013-07-30 20:11:57 +02:00
|
|
|
#include <string.h>
|
|
|
|
|
2008-11-25 16:10:01 +01:00
|
|
|
#undef G_LOG_DOMAIN
|
|
|
|
#define G_LOG_DOMAIN "ao"
|
2004-09-08 04:16:08 +02:00
|
|
|
|
2011-02-13 02:37:28 +01:00
|
|
|
/* An ao_sample_format, with all fields set to zero: */
|
2013-04-17 00:56:09 +02:00
|
|
|
static ao_sample_format OUR_AO_FORMAT_INITIALIZER;
|
2011-02-13 02:37:28 +01:00
|
|
|
|
2009-02-25 19:08:49 +01:00
|
|
|
static unsigned ao_output_ref;
|
2004-09-08 04:16:08 +02:00
|
|
|
|
2013-04-17 00:56:09 +02:00
|
|
|
struct AoOutput {
|
2011-09-16 23:31:48 +02:00
|
|
|
struct audio_output base;
|
|
|
|
|
2009-02-25 19:08:49 +01:00
|
|
|
size_t write_size;
|
|
|
|
int driver;
|
2006-07-20 18:02:40 +02:00
|
|
|
ao_option *options;
|
|
|
|
ao_device *device;
|
2013-04-17 00:56:09 +02:00
|
|
|
|
|
|
|
bool Initialize(const config_param *param, GError **error_r) {
|
|
|
|
return ao_base_init(&base, &ao_output_plugin, param,
|
|
|
|
error_r);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Deinitialize() {
|
|
|
|
ao_base_finish(&base);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Configure(const config_param *param, GError **error_r);
|
|
|
|
};
|
2004-09-08 04:16:08 +02:00
|
|
|
|
2009-02-26 22:04:59 +01:00
|
|
|
static inline GQuark
|
|
|
|
ao_output_quark(void)
|
|
|
|
{
|
|
|
|
return g_quark_from_static_string("ao_output");
|
|
|
|
}
|
|
|
|
|
2009-02-25 19:08:49 +01:00
|
|
|
static void
|
2009-02-26 22:04:59 +01:00
|
|
|
ao_output_error(GError **error_r)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2008-11-25 16:17:48 +01:00
|
|
|
const char *error;
|
|
|
|
|
|
|
|
switch (errno) {
|
|
|
|
case AO_ENODRIVER:
|
|
|
|
error = "No such libao driver";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case AO_ENOTLIVE:
|
|
|
|
error = "This driver is not a libao live device";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case AO_EBADOPTION:
|
|
|
|
error = "Invalid libao option";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case AO_EOPENDEVICE:
|
|
|
|
error = "Cannot open the libao device";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case AO_EFAIL:
|
|
|
|
error = "Generic libao failure";
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2012-03-06 22:06:08 +01:00
|
|
|
error = g_strerror(errno);
|
2004-10-20 18:16:50 +02:00
|
|
|
}
|
2008-11-25 16:17:48 +01:00
|
|
|
|
2009-02-26 22:04:59 +01:00
|
|
|
g_set_error(error_r, ao_output_quark(), errno,
|
|
|
|
"%s", error);
|
2004-10-20 18:16:50 +02:00
|
|
|
}
|
|
|
|
|
2013-04-17 00:56:09 +02:00
|
|
|
inline bool
|
|
|
|
AoOutput::Configure(const config_param *param, GError **error_r)
|
2004-10-28 07:14:55 +02:00
|
|
|
{
|
2009-01-18 19:37:27 +01:00
|
|
|
const char *value;
|
2004-09-08 04:16:08 +02:00
|
|
|
|
2013-04-17 00:56:09 +02:00
|
|
|
options = nullptr;
|
2009-02-25 19:08:49 +01:00
|
|
|
|
2013-04-17 00:56:09 +02:00
|
|
|
write_size = config_get_block_unsigned(param, "write_size", 1024);
|
2004-09-08 04:16:08 +02:00
|
|
|
|
2009-02-25 19:08:49 +01:00
|
|
|
if (ao_output_ref == 0) {
|
2004-10-20 18:05:13 +02:00
|
|
|
ao_initialize();
|
|
|
|
}
|
2009-02-25 19:08:49 +01:00
|
|
|
ao_output_ref++;
|
2004-10-28 07:14:55 +02:00
|
|
|
|
2009-01-18 19:37:27 +01:00
|
|
|
value = config_get_block_string(param, "driver", "default");
|
2009-02-26 22:04:59 +01:00
|
|
|
if (0 == strcmp(value, "default"))
|
2013-04-17 00:56:09 +02:00
|
|
|
driver = ao_default_driver_id();
|
2009-02-26 22:04:59 +01:00
|
|
|
else
|
2013-04-17 00:56:09 +02:00
|
|
|
driver = ao_driver_id(value);
|
2009-02-26 22:04:59 +01:00
|
|
|
|
2013-04-17 00:56:09 +02:00
|
|
|
if (driver < 0) {
|
|
|
|
g_set_error(error_r, ao_output_quark(), 0,
|
2009-02-26 22:04:59 +01:00
|
|
|
"\"%s\" is not a valid ao driver",
|
|
|
|
value);
|
2013-04-17 00:56:09 +02:00
|
|
|
return false;
|
2009-02-26 22:04:59 +01:00
|
|
|
}
|
2006-07-20 18:02:40 +02:00
|
|
|
|
2013-04-17 00:56:09 +02:00
|
|
|
ao_info *ai = ao_driver_info(driver);
|
|
|
|
if (ai == nullptr) {
|
|
|
|
g_set_error(error_r, ao_output_quark(), 0,
|
2009-02-26 22:04:59 +01:00
|
|
|
"problems getting driver info");
|
2013-04-17 00:56:09 +02:00
|
|
|
return false;
|
2004-09-08 04:16:08 +02:00
|
|
|
}
|
|
|
|
|
2008-11-25 16:10:01 +01:00
|
|
|
g_debug("using ao driver \"%s\" for \"%s\"\n", ai->short_name,
|
2013-04-17 00:56:09 +02:00
|
|
|
config_get_block_string(param, "name", nullptr));
|
2004-11-02 19:31:54 +01:00
|
|
|
|
2013-04-17 00:56:09 +02:00
|
|
|
value = config_get_block_string(param, "options", nullptr);
|
|
|
|
if (value != nullptr) {
|
|
|
|
gchar **_options = g_strsplit(value, ";", 0);
|
2009-01-03 13:20:12 +01:00
|
|
|
|
2013-04-17 00:56:09 +02:00
|
|
|
for (unsigned i = 0; _options[i] != nullptr; ++i) {
|
|
|
|
gchar **key_value = g_strsplit(_options[i], "=", 2);
|
2009-01-03 13:20:12 +01:00
|
|
|
|
2013-04-17 00:56:09 +02:00
|
|
|
if (key_value[0] == nullptr || key_value[1] == nullptr) {
|
|
|
|
g_set_error(error_r, ao_output_quark(), 0,
|
2009-02-26 22:04:59 +01:00
|
|
|
"problems parsing options \"%s\"",
|
2013-04-17 00:56:09 +02:00
|
|
|
_options[i]);
|
|
|
|
return false;
|
2009-02-26 22:04:59 +01:00
|
|
|
}
|
2009-01-03 13:20:12 +01:00
|
|
|
|
2013-04-17 00:56:09 +02:00
|
|
|
ao_append_option(&options, key_value[0],
|
2009-01-03 13:20:12 +01:00
|
|
|
key_value[1]);
|
|
|
|
|
|
|
|
g_strfreev(key_value);
|
2004-09-08 04:16:08 +02:00
|
|
|
}
|
2009-01-03 13:20:12 +01:00
|
|
|
|
2013-04-17 00:56:09 +02:00
|
|
|
g_strfreev(_options);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct audio_output *
|
|
|
|
ao_output_init(const config_param *param, GError **error_r)
|
|
|
|
{
|
|
|
|
AoOutput *ad = new AoOutput();
|
|
|
|
|
|
|
|
if (!ad->Initialize(param, error_r)) {
|
|
|
|
delete ad;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ad->Configure(param, error_r)) {
|
|
|
|
ad->Deinitialize();
|
|
|
|
delete ad;
|
|
|
|
return nullptr;
|
2004-09-08 04:16:08 +02:00
|
|
|
}
|
2004-10-20 22:41:21 +02:00
|
|
|
|
2011-09-16 23:31:48 +02:00
|
|
|
return &ad->base;
|
2004-09-08 04:16:08 +02:00
|
|
|
}
|
|
|
|
|
2009-02-25 19:08:49 +01:00
|
|
|
static void
|
2011-09-16 23:31:48 +02:00
|
|
|
ao_output_finish(struct audio_output *ao)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2013-04-17 00:56:09 +02:00
|
|
|
AoOutput *ad = (AoOutput *)ao;
|
2009-02-25 19:08:49 +01:00
|
|
|
|
2004-10-20 19:43:16 +02:00
|
|
|
ao_free_options(ad->options);
|
2013-04-17 00:56:09 +02:00
|
|
|
ad->Deinitialize();
|
|
|
|
delete ad;
|
2004-09-08 04:16:08 +02:00
|
|
|
|
2009-02-25 19:08:49 +01:00
|
|
|
ao_output_ref--;
|
2004-09-08 04:16:08 +02:00
|
|
|
|
2009-02-25 19:08:49 +01:00
|
|
|
if (ao_output_ref == 0)
|
2006-07-20 18:02:40 +02:00
|
|
|
ao_shutdown();
|
2004-09-08 04:16:08 +02:00
|
|
|
}
|
|
|
|
|
2009-02-25 19:08:49 +01:00
|
|
|
static void
|
2011-09-16 23:31:48 +02:00
|
|
|
ao_output_close(struct audio_output *ao)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2013-04-17 00:56:09 +02:00
|
|
|
AoOutput *ad = (AoOutput *)ao;
|
2004-09-08 04:16:08 +02:00
|
|
|
|
2009-02-25 18:48:27 +01:00
|
|
|
ao_close(ad->device);
|
2004-09-08 04:16:08 +02:00
|
|
|
}
|
|
|
|
|
2008-10-29 20:40:27 +01:00
|
|
|
static bool
|
2011-09-16 23:31:48 +02:00
|
|
|
ao_output_open(struct audio_output *ao, struct audio_format *audio_format,
|
2009-02-26 22:04:59 +01:00
|
|
|
GError **error)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2011-02-13 02:37:28 +01:00
|
|
|
ao_sample_format format = OUR_AO_FORMAT_INITIALIZER;
|
2013-04-17 00:56:09 +02:00
|
|
|
AoOutput *ad = (AoOutput *)ao;
|
2004-09-08 04:16:08 +02:00
|
|
|
|
2009-11-10 17:11:34 +01:00
|
|
|
switch (audio_format->format) {
|
|
|
|
case SAMPLE_FORMAT_S8:
|
|
|
|
format.bits = 8;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SAMPLE_FORMAT_S16:
|
|
|
|
format.bits = 16;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
/* support for 24 bit samples in libao is currently
|
|
|
|
dubious, and until we have sorted that out,
|
|
|
|
convert everything to 16 bit */
|
|
|
|
audio_format->format = SAMPLE_FORMAT_S16;
|
|
|
|
format.bits = 16;
|
|
|
|
break;
|
|
|
|
}
|
2008-12-09 07:39:24 +01:00
|
|
|
|
2008-10-10 14:40:54 +02:00
|
|
|
format.rate = audio_format->sample_rate;
|
2004-10-20 18:05:13 +02:00
|
|
|
format.byte_format = AO_FMT_NATIVE;
|
2008-09-24 07:20:36 +02:00
|
|
|
format.channels = audio_format->channels;
|
2004-09-08 04:16:08 +02:00
|
|
|
|
2009-02-25 19:08:49 +01:00
|
|
|
ad->device = ao_open_live(ad->driver, &format, ad->options);
|
2004-09-08 04:16:08 +02:00
|
|
|
|
2013-04-17 00:56:09 +02:00
|
|
|
if (ad->device == nullptr) {
|
2009-02-26 22:04:59 +01:00
|
|
|
ao_output_error(error);
|
2008-10-29 20:40:27 +01:00
|
|
|
return false;
|
2008-11-25 16:18:06 +01:00
|
|
|
}
|
2004-09-08 04:16:08 +02:00
|
|
|
|
2008-10-29 20:40:27 +01:00
|
|
|
return true;
|
2004-09-08 04:16:08 +02:00
|
|
|
}
|
|
|
|
|
2008-04-12 06:15:52 +02:00
|
|
|
/**
|
|
|
|
* For whatever reason, libao wants a non-const pointer. Let's hope
|
|
|
|
* it does not write to the buffer, and use the union deconst hack to
|
|
|
|
* work around this API misdesign.
|
|
|
|
*/
|
|
|
|
static int ao_play_deconst(ao_device *device, const void *output_samples,
|
|
|
|
uint_32 num_bytes)
|
|
|
|
{
|
|
|
|
union {
|
|
|
|
const void *in;
|
2013-04-17 00:56:09 +02:00
|
|
|
char *out;
|
2008-04-12 06:15:52 +02:00
|
|
|
} u;
|
|
|
|
|
|
|
|
u.in = output_samples;
|
|
|
|
return ao_play(device, u.out, num_bytes);
|
|
|
|
}
|
|
|
|
|
2009-02-23 09:29:56 +01:00
|
|
|
static size_t
|
2011-09-16 23:31:48 +02:00
|
|
|
ao_output_play(struct audio_output *ao, const void *chunk, size_t size,
|
2009-02-26 22:04:59 +01:00
|
|
|
GError **error)
|
2004-10-20 18:05:13 +02:00
|
|
|
{
|
2013-04-17 00:56:09 +02:00
|
|
|
AoOutput *ad = (AoOutput *)ao;
|
2006-07-20 18:02:40 +02:00
|
|
|
|
2009-02-25 19:08:49 +01:00
|
|
|
if (size > ad->write_size)
|
|
|
|
size = ad->write_size;
|
2004-09-08 04:16:08 +02:00
|
|
|
|
2009-02-23 09:34:26 +01:00
|
|
|
if (ao_play_deconst(ad->device, chunk, size) == 0) {
|
2009-02-26 22:04:59 +01:00
|
|
|
ao_output_error(error);
|
2009-02-23 09:29:56 +01:00
|
|
|
return 0;
|
2004-09-08 04:16:08 +02:00
|
|
|
}
|
|
|
|
|
2009-02-23 09:29:56 +01:00
|
|
|
return size;
|
2004-09-08 04:16:08 +02:00
|
|
|
}
|
|
|
|
|
2009-02-25 19:08:49 +01:00
|
|
|
const struct audio_output_plugin ao_output_plugin = {
|
2013-04-17 00:56:09 +02:00
|
|
|
"ao",
|
|
|
|
nullptr,
|
|
|
|
ao_output_init,
|
|
|
|
ao_output_finish,
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
ao_output_open,
|
|
|
|
ao_output_close,
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
ao_output_play,
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
2004-09-08 04:16:08 +02:00
|
|
|
};
|