ao_plugin.c 5.37 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2011 The Music Player Daemon Project
3
 * 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 21
#include "config.h"
#include "output_api.h"
22 23

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

#undef G_LOG_DOMAIN
#define G_LOG_DOMAIN "ao"
28

29 30 31
/* An ao_sample_format, with all fields set to zero: */
static const ao_sample_format OUR_AO_FORMAT_INITIALIZER;

Max Kellermann's avatar
Max Kellermann committed
32
static unsigned ao_output_ref;
33

Max Kellermann's avatar
Max Kellermann committed
34 35 36
struct ao_data {
	size_t write_size;
	int driver;
Avuton Olrich's avatar
Avuton Olrich committed
37 38
	ao_option *options;
	ao_device *device;
39
} AoData;
40

41 42 43 44 45 46
static inline GQuark
ao_output_quark(void)
{
	return g_quark_from_static_string("ao_output");
}

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

77 78
	g_set_error(error_r, ao_output_quark(), errno,
		    "%s", error);
79 80
}

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

Max Kellermann's avatar
Max Kellermann committed
90 91 92
	ad->options = NULL;

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

Max Kellermann's avatar
Max Kellermann committed
94
	if (ao_output_ref == 0) {
95 96
		ao_initialize();
	}
Max Kellermann's avatar
Max Kellermann committed
97
	ao_output_ref++;
98

99
	value = config_get_block_string(param, "driver", "default");
100
	if (0 == strcmp(value, "default"))
Max Kellermann's avatar
Max Kellermann committed
101
		ad->driver = ao_default_driver_id();
102 103 104 105 106 107 108
	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);
109
		g_free(ad);
110 111
		return NULL;
	}
Avuton Olrich's avatar
Avuton Olrich committed
112

Max Kellermann's avatar
Max Kellermann committed
113
	if ((ai = ao_driver_info(ad->driver)) == NULL) {
114 115
		g_set_error(error, ao_output_quark(), 0,
			    "problems getting driver info");
116
		g_free(ad);
117
		return NULL;
118 119
	}

120
	g_debug("using ao driver \"%s\" for \"%s\"\n", ai->short_name,
121
		config_get_block_string(param, "name", NULL));
122

123 124 125
	value = config_get_block_string(param, "options", NULL);
	if (value != NULL) {
		gchar **options = g_strsplit(value, ";", 0);
126 127 128 129

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

130 131 132 133
			if (key_value[0] == NULL || key_value[1] == NULL) {
				g_set_error(error, ao_output_quark(), 0,
					    "problems parsing options \"%s\"",
					    options[i]);
134
				g_free(ad);
135 136
				return NULL;
			}
137 138 139 140 141

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

			g_strfreev(key_value);
142
		}
143 144

		g_strfreev(options);
145
	}
146

147
	return ad;
148 149
}

Max Kellermann's avatar
Max Kellermann committed
150 151
static void
ao_output_finish(void *data)
Avuton Olrich's avatar
Avuton Olrich committed
152
{
Max Kellermann's avatar
Max Kellermann committed
153 154
	struct ao_data *ad = (struct ao_data *)data;

155
	ao_free_options(ad->options);
156
	g_free(ad);
157

Max Kellermann's avatar
Max Kellermann committed
158
	ao_output_ref--;
159

Max Kellermann's avatar
Max Kellermann committed
160
	if (ao_output_ref == 0)
Avuton Olrich's avatar
Avuton Olrich committed
161
		ao_shutdown();
162 163
}

Max Kellermann's avatar
Max Kellermann committed
164 165
static void
ao_output_close(void *data)
Avuton Olrich's avatar
Avuton Olrich committed
166
{
Max Kellermann's avatar
Max Kellermann committed
167
	struct ao_data *ad = (struct ao_data *)data;
168

169
	ao_close(ad->device);
170 171
}

172
static bool
173 174
ao_output_open(void *data, struct audio_format *audio_format,
	       GError **error)
Avuton Olrich's avatar
Avuton Olrich committed
175
{
176
	ao_sample_format format = OUR_AO_FORMAT_INITIALIZER;
Max Kellermann's avatar
Max Kellermann committed
177
	struct ao_data *ad = (struct ao_data *)data;
178

179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
	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;
	}
196

197
	format.rate = audio_format->sample_rate;
198
	format.byte_format = AO_FMT_NATIVE;
199
	format.channels = audio_format->channels;
200

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

203
	if (ad->device == NULL) {
204
		ao_output_error(error);
205
		return false;
206
	}
207

208
	return true;
209 210
}

211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
/**
 * 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);
}

228
static size_t
229 230
ao_output_play(void *data, const void *chunk, size_t size,
	       GError **error)
231
{
Max Kellermann's avatar
Max Kellermann committed
232
	struct ao_data *ad = (struct ao_data *)data;
Avuton Olrich's avatar
Avuton Olrich committed
233

Max Kellermann's avatar
Max Kellermann committed
234 235
	if (size > ad->write_size)
		size = ad->write_size;
236

237
	if (ao_play_deconst(ad->device, chunk, size) == 0) {
238
		ao_output_error(error);
239
		return 0;
240 241
	}

242
	return size;
243 244
}

Max Kellermann's avatar
Max Kellermann committed
245
const struct audio_output_plugin ao_output_plugin = {
246
	.name = "ao",
Max Kellermann's avatar
Max Kellermann committed
247 248 249 250 251
	.init = ao_output_init,
	.finish = ao_output_finish,
	.open = ao_output_open,
	.close = ao_output_close,
	.play = ao_output_play,
252
};