2004-09-08 04:16:08 +02:00
|
|
|
/* the Music Player Daemon (MPD)
|
2007-04-05 05:22:33 +02:00
|
|
|
* Copyright (C) 2003-2007 by Warren Dukes (warren.dukes@gmail.com)
|
2004-09-08 04:16:08 +02:00
|
|
|
* This project's homepage is: 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
|
2008-09-07 22:41:17 +02:00
|
|
|
#include "../output_api.h"
|
2004-09-08 04:16:08 +02:00
|
|
|
|
|
|
|
#include <ao/ao.h>
|
2008-11-25 16:10:01 +01:00
|
|
|
#include <glib.h>
|
|
|
|
|
|
|
|
#undef G_LOG_DOMAIN
|
|
|
|
#define G_LOG_DOMAIN "ao"
|
2004-09-08 04:16:08 +02:00
|
|
|
|
2007-01-14 04:07:53 +01:00
|
|
|
static int driverInitCount;
|
2004-09-08 04:16:08 +02:00
|
|
|
|
2004-10-20 18:05:13 +02:00
|
|
|
typedef struct _AoData {
|
|
|
|
int writeSize;
|
|
|
|
int driverId;
|
2006-07-20 18:02:40 +02:00
|
|
|
ao_option *options;
|
|
|
|
ao_device *device;
|
2004-10-20 18:05:13 +02:00
|
|
|
} AoData;
|
2004-09-08 04:16:08 +02:00
|
|
|
|
2006-08-08 04:23:21 +02:00
|
|
|
static AoData *newAoData(void)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2008-11-25 16:10:01 +01:00
|
|
|
AoData *ret = g_malloc(sizeof(AoData));
|
2004-10-20 18:05:13 +02:00
|
|
|
ret->device = NULL;
|
|
|
|
ret->options = NULL;
|
2004-09-08 04:16:08 +02:00
|
|
|
|
2004-10-20 18:05:13 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2006-08-08 04:23:21 +02:00
|
|
|
static void audioOutputAo_error(void)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
|
|
|
if (errno == AO_ENOTLIVE) {
|
2008-11-25 16:10:01 +01:00
|
|
|
g_warning("not a live ao device\n");
|
2006-07-20 18:02:40 +02:00
|
|
|
} else if (errno == AO_EOPENDEVICE) {
|
2008-11-25 16:10:01 +01:00
|
|
|
g_warning("not able to open audio device\n");
|
2006-07-20 18:02:40 +02:00
|
|
|
} else if (errno == AO_EBADOPTION) {
|
2008-11-25 16:10:01 +01:00
|
|
|
g_warning("bad driver option\n");
|
2004-10-20 18:16:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-09-24 07:20:55 +02:00
|
|
|
static void *audioOutputAo_initDriver(struct audio_output *ao,
|
|
|
|
mpd_unused const struct audio_format *audio_format,
|
|
|
|
ConfigParam * param)
|
2004-10-28 07:14:55 +02:00
|
|
|
{
|
2006-07-20 18:02:40 +02:00
|
|
|
ao_info *ai;
|
2008-04-12 06:17:22 +02:00
|
|
|
char *duplicated;
|
2006-07-20 18:02:40 +02:00
|
|
|
char *stk1;
|
|
|
|
char *stk2;
|
|
|
|
char *n1;
|
|
|
|
char *key;
|
|
|
|
char *value;
|
|
|
|
char *test;
|
|
|
|
AoData *ad = newAoData();
|
|
|
|
BlockParam *blockParam;
|
2004-09-08 04:16:08 +02:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if ((blockParam = getBlockParam(param, "write_size"))) {
|
2004-10-28 07:14:55 +02:00
|
|
|
ad->writeSize = strtol(blockParam->value, &test, 10);
|
2006-07-20 18:02:40 +02:00
|
|
|
if (*test != '\0') {
|
2008-11-25 16:10:01 +01:00
|
|
|
g_error("\"%s\" is not a valid write size at line %i\n",
|
|
|
|
blockParam->value, blockParam->line);
|
2004-10-28 07:14:55 +02:00
|
|
|
}
|
2006-07-20 18:02:40 +02:00
|
|
|
} else
|
|
|
|
ad->writeSize = 1024;
|
2004-09-08 04:16:08 +02:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (driverInitCount == 0) {
|
2004-10-20 18:05:13 +02:00
|
|
|
ao_initialize();
|
|
|
|
}
|
|
|
|
driverInitCount++;
|
2004-10-28 07:14:55 +02:00
|
|
|
|
|
|
|
blockParam = getBlockParam(param, "driver");
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (!blockParam || 0 == strcmp(blockParam->value, "default")) {
|
2004-10-20 18:05:13 +02:00
|
|
|
ad->driverId = ao_default_driver_id();
|
2006-07-20 18:02:40 +02:00
|
|
|
} else if ((ad->driverId = ao_driver_id(blockParam->value)) < 0) {
|
2008-11-25 16:10:01 +01:00
|
|
|
g_error("\"%s\" is not a valid ao driver at line %i\n",
|
|
|
|
blockParam->value, blockParam->line);
|
2004-09-08 04:16:08 +02:00
|
|
|
}
|
2006-07-20 18:02:40 +02:00
|
|
|
|
|
|
|
if ((ai = ao_driver_info(ad->driverId)) == NULL) {
|
2008-11-25 16:10:01 +01:00
|
|
|
g_error("problems getting driver info for device defined at line %i\n"
|
|
|
|
"you may not have permission to the audio device\n", param->line);
|
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,
|
|
|
|
audio_output_get_name(ao));
|
2004-11-02 19:31:54 +01:00
|
|
|
|
2004-10-28 07:14:55 +02:00
|
|
|
blockParam = getBlockParam(param, "options");
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (blockParam) {
|
2008-11-25 16:10:01 +01:00
|
|
|
duplicated = g_strdup(blockParam->value);
|
2006-07-20 18:02:40 +02:00
|
|
|
} else
|
2008-11-25 16:10:01 +01:00
|
|
|
duplicated = g_strdup("");
|
2004-10-28 07:14:55 +02:00
|
|
|
|
2008-04-12 06:17:22 +02:00
|
|
|
if (strlen(duplicated)) {
|
2004-09-08 04:16:08 +02:00
|
|
|
stk1 = NULL;
|
2008-04-12 06:17:22 +02:00
|
|
|
n1 = strtok_r(duplicated, ";", &stk1);
|
2006-07-20 18:02:40 +02:00
|
|
|
while (n1) {
|
2004-09-08 04:16:08 +02:00
|
|
|
stk2 = NULL;
|
2006-07-20 18:02:40 +02:00
|
|
|
key = strtok_r(n1, "=", &stk2);
|
2007-05-26 20:15:54 +02:00
|
|
|
if (!key)
|
2008-11-25 16:10:01 +01:00
|
|
|
g_error("problems parsing options \"%s\"\n", n1);
|
2004-09-08 04:16:08 +02:00
|
|
|
/*found = 0;
|
2006-07-20 18:02:40 +02:00
|
|
|
for(i=0;i<ai->option_count;i++) {
|
|
|
|
if(strcmp(ai->options[i],key)==0) {
|
|
|
|
found = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(!found) {
|
2007-05-26 20:15:54 +02:00
|
|
|
FATAL("\"%s\" is not an option for "
|
2006-07-20 18:02:40 +02:00
|
|
|
"\"%s\" ao driver\n",key,
|
|
|
|
ai->short_name);
|
|
|
|
} */
|
|
|
|
value = strtok_r(NULL, "", &stk2);
|
2007-05-26 20:15:54 +02:00
|
|
|
if (!value)
|
2008-11-25 16:10:01 +01:00
|
|
|
g_error("problems parsing options \"%s\"\n", n1);
|
2006-07-20 18:02:40 +02:00
|
|
|
ao_append_option(&ad->options, key, value);
|
|
|
|
n1 = strtok_r(NULL, ";", &stk1);
|
2004-09-08 04:16:08 +02:00
|
|
|
}
|
|
|
|
}
|
2008-04-12 06:17:22 +02:00
|
|
|
free(duplicated);
|
2004-10-20 22:41:21 +02:00
|
|
|
|
2008-09-24 07:20:55 +02:00
|
|
|
return ad;
|
2004-09-08 04:16:08 +02:00
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
static void freeAoData(AoData * ad)
|
|
|
|
{
|
2004-10-20 19:43:16 +02:00
|
|
|
ao_free_options(ad->options);
|
|
|
|
free(ad);
|
|
|
|
}
|
|
|
|
|
2008-09-24 07:20:55 +02:00
|
|
|
static void audioOutputAo_finishDriver(void *data)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2008-09-24 07:20:55 +02:00
|
|
|
AoData *ad = (AoData *)data;
|
2004-10-20 19:43:16 +02:00
|
|
|
freeAoData(ad);
|
2004-09-08 04:16:08 +02:00
|
|
|
|
2004-10-20 18:05:13 +02:00
|
|
|
driverInitCount--;
|
2004-09-08 04:16:08 +02:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (driverInitCount == 0)
|
|
|
|
ao_shutdown();
|
2004-09-08 04:16:08 +02:00
|
|
|
}
|
|
|
|
|
2008-09-24 07:20:55 +02:00
|
|
|
static void audioOutputAo_dropBufferedAudio(mpd_unused void *data)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2005-11-19 11:52:47 +01:00
|
|
|
/* not supported by libao */
|
2005-03-05 15:01:13 +01:00
|
|
|
}
|
|
|
|
|
2008-09-24 07:20:55 +02:00
|
|
|
static void audioOutputAo_closeDevice(void *data)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2008-09-24 07:20:55 +02:00
|
|
|
AoData *ad = (AoData *)data;
|
2004-09-08 04:16:08 +02:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (ad->device) {
|
2004-10-20 18:05:13 +02:00
|
|
|
ao_close(ad->device);
|
|
|
|
ad->device = NULL;
|
|
|
|
}
|
2004-09-08 04:16:08 +02:00
|
|
|
}
|
|
|
|
|
2008-10-29 20:40:27 +01:00
|
|
|
static bool
|
|
|
|
audioOutputAo_openDevice(void *data, struct audio_format *audio_format)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2004-09-08 04:16:08 +02:00
|
|
|
ao_sample_format format;
|
2008-09-24 07:20:55 +02:00
|
|
|
AoData *ad = (AoData *)data;
|
2004-09-08 04:16:08 +02:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (ad->device) {
|
2008-09-24 07:20:55 +02:00
|
|
|
audioOutputAo_closeDevice(ad);
|
2004-09-08 04:16:08 +02:00
|
|
|
}
|
|
|
|
|
2008-09-24 07:20:36 +02:00
|
|
|
format.bits = audio_format->bits;
|
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
|
|
|
|
2004-10-20 18:05:13 +02:00
|
|
|
ad->device = ao_open_live(ad->driverId, &format, ad->options);
|
2004-09-08 04:16:08 +02:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (ad->device == NULL)
|
2008-10-29 20:40:27 +01:00
|
|
|
return false;
|
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;
|
|
|
|
void *out;
|
|
|
|
} u;
|
|
|
|
|
|
|
|
u.in = output_samples;
|
|
|
|
return ao_play(device, u.out, num_bytes);
|
|
|
|
}
|
|
|
|
|
2008-10-29 20:40:27 +01:00
|
|
|
static bool
|
|
|
|
audioOutputAo_play(void *data, const char *playChunk, size_t size)
|
2004-10-20 18:05:13 +02:00
|
|
|
{
|
2008-09-24 07:20:55 +02:00
|
|
|
AoData *ad = (AoData *)data;
|
2008-04-12 06:17:22 +02:00
|
|
|
size_t chunk_size;
|
2006-07-20 18:02:40 +02:00
|
|
|
|
|
|
|
if (ad->device == NULL)
|
2008-10-29 20:40:27 +01:00
|
|
|
return false;
|
2004-09-08 04:16:08 +02:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
while (size > 0) {
|
2008-04-12 06:17:22 +02:00
|
|
|
chunk_size = (size_t)ad->writeSize > size
|
2008-04-12 06:15:52 +02:00
|
|
|
? size : (size_t)ad->writeSize;
|
2006-07-20 18:02:40 +02:00
|
|
|
|
2008-04-12 06:17:22 +02:00
|
|
|
if (ao_play_deconst(ad->device, playChunk, chunk_size) == 0) {
|
2004-10-20 18:16:50 +02:00
|
|
|
audioOutputAo_error();
|
2008-11-25 16:10:01 +01:00
|
|
|
g_warning("closing audio device due to write error\n");
|
2008-10-29 20:40:27 +01:00
|
|
|
return false;
|
2004-09-08 04:16:08 +02:00
|
|
|
}
|
|
|
|
|
2008-04-12 06:17:22 +02:00
|
|
|
playChunk += chunk_size;
|
|
|
|
size -= chunk_size;
|
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-09-08 11:43:38 +02:00
|
|
|
const struct audio_output_plugin aoPlugin = {
|
2008-09-29 15:55:17 +02:00
|
|
|
.name = "ao",
|
|
|
|
.init = audioOutputAo_initDriver,
|
|
|
|
.finish = audioOutputAo_finishDriver,
|
|
|
|
.open = audioOutputAo_openDevice,
|
|
|
|
.play = audioOutputAo_play,
|
|
|
|
.cancel = audioOutputAo_dropBufferedAudio,
|
|
|
|
.close = audioOutputAo_closeDevice,
|
2004-09-08 04:16:08 +02:00
|
|
|
};
|