2006-07-13 21:20:34 +02:00
|
|
|
/* the Music Player Daemon (MPD)
|
2007-04-05 05:22:33 +02:00
|
|
|
* Copyright (C) 2003-2007 by Warren Dukes (warren.dukes@gmail.com)
|
2006-07-13 21:20:34 +02:00
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
2008-09-07 22:41:17 +02:00
|
|
|
#include "../output_api.h"
|
2006-07-19 18:48:24 +02:00
|
|
|
|
2008-10-29 20:37:36 +01:00
|
|
|
#include <glib.h>
|
2006-07-13 21:03:49 +02:00
|
|
|
#include <pulse/simple.h>
|
|
|
|
#include <pulse/error.h>
|
|
|
|
|
2006-07-19 18:48:24 +02:00
|
|
|
#define MPD_PULSE_NAME "mpd"
|
|
|
|
#define CONN_ATTEMPT_INTERVAL 60
|
|
|
|
|
2008-10-29 20:37:15 +01:00
|
|
|
struct pulse_data {
|
2008-09-24 07:20:55 +02:00
|
|
|
struct audio_output *ao;
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
pa_simple *s;
|
|
|
|
char *server;
|
|
|
|
char *sink;
|
2008-10-29 20:37:15 +01:00
|
|
|
int num_connect_attempts;
|
|
|
|
time_t last_connect_attempt;
|
|
|
|
};
|
2006-07-13 21:03:49 +02:00
|
|
|
|
2008-10-29 20:37:15 +01:00
|
|
|
static struct pulse_data *pulse_new_data(void)
|
2006-07-13 21:03:49 +02:00
|
|
|
{
|
2008-10-29 20:37:15 +01:00
|
|
|
struct pulse_data *ret;
|
2006-07-20 18:02:40 +02:00
|
|
|
|
2008-10-29 20:37:36 +01:00
|
|
|
ret = g_new(struct pulse_data, 1);
|
2006-07-19 18:48:24 +02:00
|
|
|
|
|
|
|
ret->s = NULL;
|
2006-07-13 21:03:49 +02:00
|
|
|
ret->server = NULL;
|
|
|
|
ret->sink = NULL;
|
2008-10-29 20:37:15 +01:00
|
|
|
ret->num_connect_attempts = 0;
|
|
|
|
ret->last_connect_attempt = 0;
|
2006-07-19 18:48:24 +02:00
|
|
|
|
2006-07-13 21:03:49 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2008-10-29 20:37:15 +01:00
|
|
|
static void pulse_free_data(struct pulse_data *pd)
|
2006-07-13 21:03:49 +02:00
|
|
|
{
|
2008-10-29 20:37:36 +01:00
|
|
|
g_free(pd->server);
|
|
|
|
g_free(pd->sink);
|
|
|
|
g_free(pd);
|
2006-07-13 21:03:49 +02:00
|
|
|
}
|
|
|
|
|
2008-10-29 20:37:15 +01:00
|
|
|
static void *
|
|
|
|
pulse_init(struct audio_output *ao,
|
|
|
|
mpd_unused const struct audio_format *audio_format,
|
|
|
|
ConfigParam *param)
|
2006-07-13 21:03:49 +02:00
|
|
|
{
|
2006-07-20 18:02:40 +02:00
|
|
|
BlockParam *server = NULL;
|
|
|
|
BlockParam *sink = NULL;
|
2008-10-29 20:37:15 +01:00
|
|
|
struct pulse_data *pd;
|
2006-07-13 21:03:49 +02:00
|
|
|
|
|
|
|
if (param) {
|
|
|
|
server = getBlockParam(param, "server");
|
|
|
|
sink = getBlockParam(param, "sink");
|
|
|
|
}
|
|
|
|
|
2008-10-29 20:37:15 +01:00
|
|
|
pd = pulse_new_data();
|
2008-09-24 07:20:55 +02:00
|
|
|
pd->ao = ao;
|
2008-10-29 20:37:36 +01:00
|
|
|
pd->server = g_strdup(server->value);
|
|
|
|
pd->sink = g_strdup(sink->value);
|
2006-07-13 21:03:49 +02:00
|
|
|
|
2008-09-24 07:20:55 +02:00
|
|
|
return pd;
|
2006-07-13 21:03:49 +02:00
|
|
|
}
|
|
|
|
|
2008-10-29 20:37:15 +01:00
|
|
|
static void pulse_finish(void *data)
|
2006-07-13 21:03:49 +02:00
|
|
|
{
|
2008-10-29 20:37:15 +01:00
|
|
|
struct pulse_data *pd = data;
|
2008-09-24 07:20:55 +02:00
|
|
|
|
2008-10-29 20:37:15 +01:00
|
|
|
pulse_free_data(pd);
|
2006-07-13 21:03:49 +02:00
|
|
|
}
|
|
|
|
|
2008-10-29 20:37:15 +01:00
|
|
|
static int pulse_test_default_device(void)
|
2006-07-13 21:03:49 +02:00
|
|
|
{
|
2006-07-20 18:02:40 +02:00
|
|
|
pa_simple *s;
|
2006-07-13 21:03:49 +02:00
|
|
|
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,
|
2006-07-20 18:02:40 +02:00
|
|
|
MPD_PULSE_NAME, &ss, NULL, NULL, &error);
|
2006-07-13 21:03:49 +02:00
|
|
|
if (!s) {
|
2008-10-29 20:37:36 +01:00
|
|
|
g_message("Cannot connect to default PulseAudio server: %s\n",
|
|
|
|
pa_strerror(error));
|
2006-07-13 21:03:49 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
pa_simple_free(s);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-10-29 20:37:15 +01:00
|
|
|
static int
|
|
|
|
pulse_open(void *data, struct audio_format *audio_format)
|
2006-07-13 21:03:49 +02:00
|
|
|
{
|
2008-10-29 20:37:15 +01:00
|
|
|
struct pulse_data *pd = data;
|
2006-07-13 21:03:49 +02:00
|
|
|
pa_sample_spec ss;
|
2006-07-19 18:48:24 +02:00
|
|
|
time_t t;
|
2006-07-13 21:03:49 +02:00
|
|
|
int error;
|
|
|
|
|
2006-07-19 18:48:24 +02:00
|
|
|
t = time(NULL);
|
2006-07-13 21:03:49 +02:00
|
|
|
|
2008-10-29 20:37:15 +01:00
|
|
|
if (pd->num_connect_attempts != 0 &&
|
|
|
|
(t - pd->last_connect_attempt) < CONN_ATTEMPT_INTERVAL)
|
2006-07-20 18:02:40 +02:00
|
|
|
return -1;
|
2006-07-19 18:48:24 +02:00
|
|
|
|
2008-10-29 20:37:15 +01:00
|
|
|
pd->num_connect_attempts++;
|
|
|
|
pd->last_connect_attempt = t;
|
2006-07-19 18:48:24 +02:00
|
|
|
|
2008-10-25 20:41:28 +02:00
|
|
|
/* MPD doesn't support the other pulseaudio sample formats, so
|
|
|
|
we just force MPD to send us everything as 16 bit */
|
2008-10-29 20:37:15 +01:00
|
|
|
audio_format->bits = 16;
|
2006-07-13 21:03:49 +02:00
|
|
|
|
|
|
|
ss.format = PA_SAMPLE_S16NE;
|
2008-10-29 20:37:15 +01:00
|
|
|
ss.rate = audio_format->sample_rate;
|
|
|
|
ss.channels = audio_format->channels;
|
2006-07-13 21:03:49 +02:00
|
|
|
|
2006-07-19 18:50:03 +02:00
|
|
|
pd->s = pa_simple_new(pd->server, MPD_PULSE_NAME, PA_STREAM_PLAYBACK,
|
2008-09-24 07:20:55 +02:00
|
|
|
pd->sink, audio_output_get_name(pd->ao),
|
|
|
|
&ss, NULL, NULL,
|
2006-07-20 18:02:40 +02:00
|
|
|
&error);
|
2006-07-19 18:50:03 +02:00
|
|
|
if (!pd->s) {
|
2008-10-29 20:37:36 +01:00
|
|
|
g_warning("Cannot connect to server in PulseAudio output "
|
|
|
|
"\"%s\" (attempt %i): %s\n",
|
|
|
|
audio_output_get_name(pd->ao),
|
|
|
|
pd->num_connect_attempts, pa_strerror(error));
|
2006-07-13 21:03:49 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2008-10-29 20:37:15 +01:00
|
|
|
pd->num_connect_attempts = 0;
|
2006-07-13 21:03:49 +02:00
|
|
|
|
2008-10-29 20:37:36 +01:00
|
|
|
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);
|
2006-07-13 21:03:49 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-10-29 20:37:15 +01:00
|
|
|
static void pulse_cancel(void *data)
|
2006-07-13 21:03:49 +02:00
|
|
|
{
|
2008-10-29 20:37:15 +01:00
|
|
|
struct pulse_data *pd = data;
|
2006-07-13 21:03:49 +02:00
|
|
|
int error;
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (pa_simple_flush(pd->s, &error) < 0)
|
2008-10-29 20:37:36 +01:00
|
|
|
g_warning("Flush failed in PulseAudio output \"%s\": %s\n",
|
|
|
|
audio_output_get_name(pd->ao),
|
|
|
|
pa_strerror(error));
|
2006-07-13 21:03:49 +02:00
|
|
|
}
|
|
|
|
|
2008-10-29 20:37:15 +01:00
|
|
|
static void pulse_close(void *data)
|
2006-07-13 21:03:49 +02:00
|
|
|
{
|
2008-10-29 20:37:15 +01:00
|
|
|
struct pulse_data *pd = data;
|
2006-07-13 21:03:49 +02:00
|
|
|
|
2006-07-19 18:50:03 +02:00
|
|
|
if (pd->s) {
|
|
|
|
pa_simple_drain(pd->s, NULL);
|
|
|
|
pa_simple_free(pd->s);
|
2008-10-29 20:38:20 +01:00
|
|
|
pd->s = NULL;
|
2006-07-13 21:03:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-10-29 20:37:15 +01:00
|
|
|
static int pulse_play(void *data,
|
2008-04-12 06:15:52 +02:00
|
|
|
const char *playChunk, size_t size)
|
2006-07-13 21:03:49 +02:00
|
|
|
{
|
2008-10-29 20:37:15 +01:00
|
|
|
struct pulse_data *pd = data;
|
2006-07-13 21:03:49 +02:00
|
|
|
int error;
|
|
|
|
|
2006-07-19 18:50:03 +02:00
|
|
|
if (pa_simple_write(pd->s, playChunk, size, &error) < 0) {
|
2008-10-29 20:37:36 +01:00
|
|
|
g_warning("PulseAudio output \"%s\" disconnecting due to "
|
|
|
|
"write error: %s\n",
|
|
|
|
audio_output_get_name(pd->ao),
|
|
|
|
pa_strerror(error));
|
2008-10-29 20:37:15 +01:00
|
|
|
pulse_close(pd);
|
2006-07-13 21:03:49 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-10-29 20:37:15 +01:00
|
|
|
const struct audio_output_plugin pulse_plugin = {
|
2008-09-29 15:55:17 +02:00
|
|
|
.name = "pulse",
|
2008-10-29 20:37:15 +01:00
|
|
|
.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,
|
2006-07-13 21:03:49 +02:00
|
|
|
};
|