ao_plugin.c 5.04 KB
Newer Older
1 2 3
/*
 * Copyright (C) 2003-2009 The Music Player Daemon Project
 * http://www.musicpd.org
4 5 6 7 8 9 10 11 12 13
 *
 * 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.
14 15 16 17
 *
 * 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.
18 19
 */

20
#include "../output_api.h"
21 22

#include <ao/ao.h>
23 24 25 26
#include <glib.h>

#undef G_LOG_DOMAIN
#define G_LOG_DOMAIN "ao"
27

Max Kellermann's avatar
Max Kellermann committed
28
static unsigned ao_output_ref;
29

Max Kellermann's avatar
Max Kellermann committed
30 31 32
struct ao_data {
	size_t write_size;
	int driver;
Avuton Olrich's avatar
Avuton Olrich committed
33 34
	ao_option *options;
	ao_device *device;
35
} AoData;
36

37 38 39 40 41 42
static inline GQuark
ao_output_quark(void)
{
	return g_quark_from_static_string("ao_output");
}

Max Kellermann's avatar
Max Kellermann committed
43
static void
44
ao_output_error(GError **error_r)
Avuton Olrich's avatar
Avuton Olrich committed
45
{
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
	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:
		error = strerror(errno);
71
	}
72

73 74
	g_set_error(error_r, ao_output_quark(), errno,
		    "%s", error);
75 76
}

77
static void *
Max Kellermann's avatar
Max Kellermann committed
78
ao_output_init(G_GNUC_UNUSED const struct audio_format *audio_format,
79 80
	       const struct config_param *param,
	       GError **error)
81
{
Max Kellermann's avatar
Max Kellermann committed
82
	struct ao_data *ad = g_new(struct ao_data, 1);
Avuton Olrich's avatar
Avuton Olrich committed
83
	ao_info *ai;
84
	const char *value;
85

Max Kellermann's avatar
Max Kellermann committed
86 87 88
	ad->options = NULL;

	ad->write_size = config_get_block_unsigned(param, "write_size", 1024);
89

Max Kellermann's avatar
Max Kellermann committed
90
	if (ao_output_ref == 0) {
91 92
		ao_initialize();
	}
Max Kellermann's avatar
Max Kellermann committed
93
	ao_output_ref++;
94

95
	value = config_get_block_string(param, "driver", "default");
96
	if (0 == strcmp(value, "default"))
Max Kellermann's avatar
Max Kellermann committed
97
		ad->driver = ao_default_driver_id();
98 99 100 101 102 103 104 105 106
	else
		ad->driver = ao_driver_id(value);

	if (ad->driver < 0) {
		g_set_error(error, ao_output_quark(), 0,
			    "\"%s\" is not a valid ao driver",
			    value);
		return NULL;
	}
Avuton Olrich's avatar
Avuton Olrich committed
107

Max Kellermann's avatar
Max Kellermann committed
108
	if ((ai = ao_driver_info(ad->driver)) == NULL) {
109 110 111
		g_set_error(error, ao_output_quark(), 0,
			    "problems getting driver info");
		return NULL;
112 113
	}

114
	g_debug("using ao driver \"%s\" for \"%s\"\n", ai->short_name,
115
		config_get_block_string(param, "name", NULL));
116

117 118 119
	value = config_get_block_string(param, "options", NULL);
	if (value != NULL) {
		gchar **options = g_strsplit(value, ";", 0);
120 121 122 123

		for (unsigned i = 0; options[i] != NULL; ++i) {
			gchar **key_value = g_strsplit(options[i], "=", 2);

124 125 126 127 128 129
			if (key_value[0] == NULL || key_value[1] == NULL) {
				g_set_error(error, ao_output_quark(), 0,
					    "problems parsing options \"%s\"",
					    options[i]);
				return NULL;
			}
130 131 132 133 134

			ao_append_option(&ad->options, key_value[0],
					 key_value[1]);

			g_strfreev(key_value);
135
		}
136 137

		g_strfreev(options);
138
	}
139

140
	return ad;
141 142
}

Max Kellermann's avatar
Max Kellermann committed
143 144
static void
ao_output_finish(void *data)
Avuton Olrich's avatar
Avuton Olrich committed
145
{
Max Kellermann's avatar
Max Kellermann committed
146 147
	struct ao_data *ad = (struct ao_data *)data;

148
	ao_free_options(ad->options);
149
	g_free(ad);
150

Max Kellermann's avatar
Max Kellermann committed
151
	ao_output_ref--;
152

Max Kellermann's avatar
Max Kellermann committed
153
	if (ao_output_ref == 0)
Avuton Olrich's avatar
Avuton Olrich committed
154
		ao_shutdown();
155 156
}

Max Kellermann's avatar
Max Kellermann committed
157 158
static void
ao_output_close(void *data)
Avuton Olrich's avatar
Avuton Olrich committed
159
{
Max Kellermann's avatar
Max Kellermann committed
160
	struct ao_data *ad = (struct ao_data *)data;
161

162
	ao_close(ad->device);
163 164
}

165
static bool
166 167
ao_output_open(void *data, struct audio_format *audio_format,
	       GError **error)
Avuton Olrich's avatar
Avuton Olrich committed
168
{
169
	ao_sample_format format;
Max Kellermann's avatar
Max Kellermann committed
170
	struct ao_data *ad = (struct ao_data *)data;
171

172 173 174 175 176 177
	/* support for 24 bit samples in libao is currently dubious,
	   and until we have sorted that out, resample everything to
	   16 bit */
	if (audio_format->bits > 16)
		audio_format->bits = 16;

178
	format.bits = audio_format->bits;
179
	format.rate = audio_format->sample_rate;
180
	format.byte_format = AO_FMT_NATIVE;
181
	format.channels = audio_format->channels;
182

Max Kellermann's avatar
Max Kellermann committed
183
	ad->device = ao_open_live(ad->driver, &format, ad->options);
184

185
	if (ad->device == NULL) {
186
		ao_output_error(error);
187
		return false;
188
	}
189

190
	return true;
191 192
}

193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
/**
 * 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);
}

210
static size_t
211 212
ao_output_play(void *data, const void *chunk, size_t size,
	       GError **error)
213
{
Max Kellermann's avatar
Max Kellermann committed
214
	struct ao_data *ad = (struct ao_data *)data;
Avuton Olrich's avatar
Avuton Olrich committed
215

Max Kellermann's avatar
Max Kellermann committed
216 217
	if (size > ad->write_size)
		size = ad->write_size;
218

219
	if (ao_play_deconst(ad->device, chunk, size) == 0) {
220
		ao_output_error(error);
221
		return 0;
222 223
	}

224
	return size;
225 226
}

Max Kellermann's avatar
Max Kellermann committed
227
const struct audio_output_plugin ao_output_plugin = {
228
	.name = "ao",
Max Kellermann's avatar
Max Kellermann committed
229 230 231 232 233
	.init = ao_output_init,
	.finish = ao_output_finish,
	.open = ao_output_open,
	.close = ao_output_close,
	.play = ao_output_play,
234
};