pulse_plugin.c 4.26 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
static void *
pulse_init(struct audio_output *ao,
57
	   G_GNUC_UNUSED const struct audio_format *audio_format,
58
	   struct config_param *param)
59
{
Max Kellermann's avatar
Max Kellermann committed
60
	struct pulse_data *pd;
61

Max Kellermann's avatar
Max Kellermann committed
62
	pd = pulse_new_data();
63
	pd->ao = ao;
64 65 66 67
	pd->server = param != NULL
		? config_dup_block_string(param, "server", NULL) : NULL;
	pd->sink = param != NULL
		? config_dup_block_string(param, "sink", NULL) : NULL;
68

69
	return pd;
70 71
}

Max Kellermann's avatar
Max Kellermann committed
72
static void pulse_finish(void *data)
73
{
Max Kellermann's avatar
Max Kellermann committed
74
	struct pulse_data *pd = data;
75

Max Kellermann's avatar
Max Kellermann committed
76
	pulse_free_data(pd);
77 78
}

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

	pa_simple_free(s);

99
	return true;
100 101
}

102
static bool
Max Kellermann's avatar
Max Kellermann committed
103
pulse_open(void *data, struct audio_format *audio_format)
104
{
Max Kellermann's avatar
Max Kellermann committed
105
	struct pulse_data *pd = data;
106 107 108
	pa_sample_spec ss;
	int error;

109 110
	/* 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
111
	audio_format->bits = 16;
112 113

	ss.format = PA_SAMPLE_S16NE;
Max Kellermann's avatar
Max Kellermann committed
114 115
	ss.rate = audio_format->sample_rate;
	ss.channels = audio_format->channels;
116

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

129 130 131 132 133
	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);
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;

143 144 145
	if (pd->s == NULL)
		return;

Avuton Olrich's avatar
Avuton Olrich committed
146
	if (pa_simple_flush(pd->s, &error) < 0)
147 148 149
		g_warning("Flush failed in PulseAudio output \"%s\": %s\n",
			  audio_output_get_name(pd->ao),
			  pa_strerror(error));
150 151
}

Max Kellermann's avatar
Max Kellermann committed
152
static void pulse_close(void *data)
153
{
Max Kellermann's avatar
Max Kellermann committed
154
	struct pulse_data *pd = data;
155

156 157 158
	if (pd->s) {
		pa_simple_drain(pd->s, NULL);
		pa_simple_free(pd->s);
159
		pd->s = NULL;
160 161 162
	}
}

163 164
static bool
pulse_play(void *data, const char *playChunk, size_t size)
165
{
Max Kellermann's avatar
Max Kellermann committed
166
	struct pulse_data *pd = data;
167 168
	int error;

169
	if (pa_simple_write(pd->s, playChunk, size, &error) < 0) {
170 171 172 173
		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
174
		pulse_close(pd);
175
		return false;
176 177
	}

178
	return true;
179 180
}

Max Kellermann's avatar
Max Kellermann committed
181
const struct audio_output_plugin pulse_plugin = {
182
	.name = "pulse",
Max Kellermann's avatar
Max Kellermann committed
183 184 185 186 187 188 189
	.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,
190
};