pulse_plugin.c 4.05 KB
Newer Older
1
/* the Music Player Daemon (MPD)
2
 * Copyright (C) 2003-2007 by Warren Dukes (warren.dukes@gmail.com)
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 * 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
 */

19
#include "../output_api.h"
20

21
#include <glib.h>
22 23 24
#include <pulse/simple.h>
#include <pulse/error.h>

25 26
#define MPD_PULSE_NAME "mpd"

Max Kellermann's avatar
Max Kellermann committed
27
struct pulse_data {
28
	const char *name;
29

Avuton Olrich's avatar
Avuton Olrich committed
30 31 32
	pa_simple *s;
	char *server;
	char *sink;
Max Kellermann's avatar
Max Kellermann committed
33
};
34

35 36 37 38 39 40 41 42 43
/**
 * The quark used for GError.domain.
 */
static inline GQuark
pulse_output_quark(void)
{
	return g_quark_from_static_string("pulse_output");
}

Max Kellermann's avatar
Max Kellermann committed
44
static struct pulse_data *pulse_new_data(void)
45
{
Max Kellermann's avatar
Max Kellermann committed
46
	struct pulse_data *ret;
Avuton Olrich's avatar
Avuton Olrich committed
47

48
	ret = g_new(struct pulse_data, 1);
49

50 51
	ret->server = NULL;
	ret->sink = NULL;
52

53 54 55
	return ret;
}

Max Kellermann's avatar
Max Kellermann committed
56
static void pulse_free_data(struct pulse_data *pd)
57
{
58 59 60
	g_free(pd->server);
	g_free(pd->sink);
	g_free(pd);
61 62
}

Max Kellermann's avatar
Max Kellermann committed
63
static void *
64
pulse_init(G_GNUC_UNUSED const struct audio_format *audio_format,
65
	   const struct config_param *param, G_GNUC_UNUSED GError **error)
66
{
Max Kellermann's avatar
Max Kellermann committed
67
	struct pulse_data *pd;
68

Max Kellermann's avatar
Max Kellermann committed
69
	pd = pulse_new_data();
70
	pd->name = config_get_block_string(param, "name", "mpd_pulse");
71 72 73 74
	pd->server = param != NULL
		? config_dup_block_string(param, "server", NULL) : NULL;
	pd->sink = param != NULL
		? config_dup_block_string(param, "sink", NULL) : NULL;
75

76
	return pd;
77 78
}

Max Kellermann's avatar
Max Kellermann committed
79
static void pulse_finish(void *data)
80
{
Max Kellermann's avatar
Max Kellermann committed
81
	struct pulse_data *pd = data;
82

Max Kellermann's avatar
Max Kellermann committed
83
	pulse_free_data(pd);
84 85
}

86
static bool pulse_test_default_device(void)
87
{
Avuton Olrich's avatar
Avuton Olrich committed
88
	pa_simple *s;
89 90 91 92 93 94 95 96
	pa_sample_spec ss;
	int error;

	ss.format = PA_SAMPLE_S16NE;
	ss.rate = 44100;
	ss.channels = 2;

	s = pa_simple_new(NULL, MPD_PULSE_NAME, PA_STREAM_PLAYBACK, NULL,
Avuton Olrich's avatar
Avuton Olrich committed
97
			  MPD_PULSE_NAME, &ss, NULL, NULL, &error);
98
	if (!s) {
99 100
		g_message("Cannot connect to default PulseAudio server: %s\n",
			  pa_strerror(error));
101
		return false;
102 103 104 105
	}

	pa_simple_free(s);

106
	return true;
107 108
}

109
static bool
110
pulse_open(void *data, struct audio_format *audio_format, GError **error_r)
111
{
Max Kellermann's avatar
Max Kellermann committed
112
	struct pulse_data *pd = data;
113 114 115
	pa_sample_spec ss;
	int error;

116 117
	/* MPD doesn't support the other pulseaudio sample formats, so
	   we just force MPD to send us everything as 16 bit */
Max Kellermann's avatar
Max Kellermann committed
118
	audio_format->bits = 16;
119 120

	ss.format = PA_SAMPLE_S16NE;
Max Kellermann's avatar
Max Kellermann committed
121 122
	ss.rate = audio_format->sample_rate;
	ss.channels = audio_format->channels;
123

124
	pd->s = pa_simple_new(pd->server, MPD_PULSE_NAME, PA_STREAM_PLAYBACK,
125
			      pd->sink, pd->name,
126
			      &ss, NULL, NULL,
Avuton Olrich's avatar
Avuton Olrich committed
127
			      &error);
128
	if (!pd->s) {
129 130 131
		g_set_error(error_r, pulse_output_quark(), error,
			    "Cannot connect to PulseAudio server: %s",
			    pa_strerror(error));
132
		return false;
133 134
	}

135
	return true;
136 137
}

Max Kellermann's avatar
Max Kellermann committed
138
static void pulse_cancel(void *data)
139
{
Max Kellermann's avatar
Max Kellermann committed
140
	struct pulse_data *pd = data;
141 142
	int error;

Avuton Olrich's avatar
Avuton Olrich committed
143
	if (pa_simple_flush(pd->s, &error) < 0)
144
		g_warning("Flush failed in PulseAudio output \"%s\": %s\n",
145
			  pd->name, pa_strerror(error));
146 147
}

Max Kellermann's avatar
Max Kellermann committed
148
static void pulse_close(void *data)
149
{
Max Kellermann's avatar
Max Kellermann committed
150
	struct pulse_data *pd = data;
151

152 153
	pa_simple_drain(pd->s, NULL);
	pa_simple_free(pd->s);
154 155
}

156
static size_t
157
pulse_play(void *data, const void *chunk, size_t size, GError **error_r)
158
{
Max Kellermann's avatar
Max Kellermann committed
159
	struct pulse_data *pd = data;
160 161
	int error;

162
	if (pa_simple_write(pd->s, chunk, size, &error) < 0) {
163 164
		g_set_error(error_r, pulse_output_quark(), error,
			    "%s", pa_strerror(error));
165
		return 0;
166 167
	}

168
	return size;
169 170
}

Max Kellermann's avatar
Max Kellermann committed
171
const struct audio_output_plugin pulse_plugin = {
172
	.name = "pulse",
Max Kellermann's avatar
Max Kellermann committed
173 174 175 176 177 178 179
	.test_default_device = pulse_test_default_device,
	.init = pulse_init,
	.finish = pulse_finish,
	.open = pulse_open,
	.play = pulse_play,
	.cancel = pulse_cancel,
	.close = pulse_close,
180
};