pulse_plugin.c 3.97 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
#include "mixer_list.h"
22

23
#include <glib.h>
24 25 26
#include <pulse/simple.h>
#include <pulse/error.h>

27 28
#define MPD_PULSE_NAME "mpd"

Max Kellermann's avatar
Max Kellermann committed
29
struct pulse_data {
30
	const char *name;
31 32
	const char *server;
	const char *sink;
33

Avuton Olrich's avatar
Avuton Olrich committed
34
	pa_simple *s;
Max Kellermann's avatar
Max Kellermann committed
35
};
36

37 38 39 40 41 42 43 44 45
/**
 * 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
46
static struct pulse_data *pulse_new_data(void)
47
{
Max Kellermann's avatar
Max Kellermann committed
48
	struct pulse_data *ret;
Avuton Olrich's avatar
Avuton Olrich committed
49

50
	ret = g_new(struct pulse_data, 1);
51

52 53
	ret->server = NULL;
	ret->sink = NULL;
54

55 56 57
	return ret;
}

Max Kellermann's avatar
Max Kellermann committed
58
static void pulse_free_data(struct pulse_data *pd)
59
{
60
	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
	pd->server = config_get_block_string(param, "server", NULL);
	pd->sink = config_get_block_string(param, "sink", 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
108
pulse_open(void *data, struct audio_format *audio_format, GError **error_r)
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
			      pd->sink, pd->name,
124
			      &ss, NULL, NULL,
Avuton Olrich's avatar
Avuton Olrich committed
125
			      &error);
126
	if (!pd->s) {
127 128 129
		g_set_error(error_r, pulse_output_quark(), error,
			    "Cannot connect to PulseAudio server: %s",
			    pa_strerror(error));
130
		return false;
131 132
	}

133
	return true;
134 135
}

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

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

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

150 151
	pa_simple_drain(pd->s, NULL);
	pa_simple_free(pd->s);
152 153
}

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

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

166
	return size;
167 168
}

Max Kellermann's avatar
Max Kellermann committed
169
const struct audio_output_plugin pulse_plugin = {
170
	.name = "pulse",
Max Kellermann's avatar
Max Kellermann committed
171 172 173 174 175 176 177
	.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,
178
	.mixer_plugin = &pulse_mixer,
179
};