pulse_plugin.c 4.35 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 29
	struct audio_output *ao;

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

Max Kellermann's avatar
Max Kellermann committed
35
static struct pulse_data *pulse_new_data(void)
36
{
Max Kellermann's avatar
Max Kellermann committed
37
	struct pulse_data *ret;
Avuton Olrich's avatar
Avuton Olrich committed
38

39
	ret = g_new(struct pulse_data, 1);
40 41

	ret->s = NULL;
42 43
	ret->server = NULL;
	ret->sink = NULL;
44

45 46 47
	return ret;
}

Max Kellermann's avatar
Max Kellermann committed
48
static void pulse_free_data(struct pulse_data *pd)
49
{
50 51 52
	g_free(pd->server);
	g_free(pd->sink);
	g_free(pd);
53 54
}

Max Kellermann's avatar
Max Kellermann committed
55 56 57 58
static void *
pulse_init(struct audio_output *ao,
	   mpd_unused const struct audio_format *audio_format,
	   ConfigParam *param)
59
{
Avuton Olrich's avatar
Avuton Olrich committed
60 61
	BlockParam *server = NULL;
	BlockParam *sink = NULL;
Max Kellermann's avatar
Max Kellermann committed
62
	struct pulse_data *pd;
63 64 65 66 67 68

	if (param) {
		server = getBlockParam(param, "server");
		sink = getBlockParam(param, "sink");
	}

Max Kellermann's avatar
Max Kellermann committed
69
	pd = pulse_new_data();
70
	pd->ao = ao;
71 72
	pd->server = server != NULL ? g_strdup(server->value) : NULL;
	pd->sink = sink != NULL ? g_strdup(sink->value) : NULL;
73

74
	return pd;
75 76
}

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

Max Kellermann's avatar
Max Kellermann committed
81
	pulse_free_data(pd);
82 83
}

84
static bool pulse_test_default_device(void)
85
{
Avuton Olrich's avatar
Avuton Olrich committed
86
	pa_simple *s;
87 88 89 90 91 92 93 94
	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
95
			  MPD_PULSE_NAME, &ss, NULL, NULL, &error);
96
	if (!s) {
97 98
		g_message("Cannot connect to default PulseAudio server: %s\n",
			  pa_strerror(error));
99
		return false;
100 101 102 103
	}

	pa_simple_free(s);

104
	return true;
105 106
}

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

114 115
	/* 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
116
	audio_format->bits = 16;
117 118

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

122
	pd->s = pa_simple_new(pd->server, MPD_PULSE_NAME, PA_STREAM_PLAYBACK,
123 124
			      pd->sink, audio_output_get_name(pd->ao),
			      &ss, NULL, NULL,
Avuton Olrich's avatar
Avuton Olrich committed
125
			      &error);
126
	if (!pd->s) {
127
		g_warning("Cannot connect to server in PulseAudio output "
128
			  "\"%s\": %s\n",
129
			  audio_output_get_name(pd->ao),
130
			  pa_strerror(error));
131
		return false;
132 133
	}

134 135 136 137 138
	g_debug("PulseAudio output \"%s\" connected and playing %i bit, %i "
		"channel audio at %i Hz\n",
		audio_output_get_name(pd->ao),
		audio_format->bits,
		audio_format->channels, audio_format->sample_rate);
139

140
	return true;
141 142
}

Max Kellermann's avatar
Max Kellermann committed
143
static void pulse_cancel(void *data)
144
{
Max Kellermann's avatar
Max Kellermann committed
145
	struct pulse_data *pd = data;
146 147
	int error;

148 149 150
	if (pd->s == NULL)
		return;

Avuton Olrich's avatar
Avuton Olrich committed
151
	if (pa_simple_flush(pd->s, &error) < 0)
152 153 154
		g_warning("Flush failed in PulseAudio output \"%s\": %s\n",
			  audio_output_get_name(pd->ao),
			  pa_strerror(error));
155 156
}

Max Kellermann's avatar
Max Kellermann committed
157
static void pulse_close(void *data)
158
{
Max Kellermann's avatar
Max Kellermann committed
159
	struct pulse_data *pd = data;
160

161 162 163
	if (pd->s) {
		pa_simple_drain(pd->s, NULL);
		pa_simple_free(pd->s);
164
		pd->s = NULL;
165 166 167
	}
}

168 169
static bool
pulse_play(void *data, const char *playChunk, size_t size)
170
{
Max Kellermann's avatar
Max Kellermann committed
171
	struct pulse_data *pd = data;
172 173
	int error;

174
	if (pa_simple_write(pd->s, playChunk, size, &error) < 0) {
175 176 177 178
		g_warning("PulseAudio output \"%s\" disconnecting due to "
			  "write error: %s\n",
			  audio_output_get_name(pd->ao),
			  pa_strerror(error));
Max Kellermann's avatar
Max Kellermann committed
179
		pulse_close(pd);
180
		return false;
181 182
	}

183
	return true;
184 185
}

Max Kellermann's avatar
Max Kellermann committed
186
const struct audio_output_plugin pulse_plugin = {
187
	.name = "pulse",
Max Kellermann's avatar
Max Kellermann committed
188 189 190 191 192 193 194
	.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,
195
};