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-13 21:03:49 +02:00
|
|
|
|
|
|
|
#ifdef HAVE_PULSE
|
|
|
|
|
2008-09-07 22:41:17 +02:00
|
|
|
#include "../utils.h"
|
2006-07-13 21:03:49 +02:00
|
|
|
#include "../log.h"
|
2006-07-19 18:48:24 +02:00
|
|
|
|
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
|
|
|
|
|
2006-07-13 21:03:49 +02:00
|
|
|
typedef struct _PulseData {
|
2006-07-20 18:02:40 +02:00
|
|
|
pa_simple *s;
|
|
|
|
char *server;
|
|
|
|
char *sink;
|
2006-07-19 18:48:24 +02:00
|
|
|
int connAttempts;
|
|
|
|
time_t lastAttempt;
|
2006-07-13 21:03:49 +02:00
|
|
|
} PulseData;
|
|
|
|
|
2006-08-08 04:32:58 +02:00
|
|
|
static PulseData *newPulseData(void)
|
2006-07-13 21:03:49 +02:00
|
|
|
{
|
2006-07-20 18:02:40 +02:00
|
|
|
PulseData *ret;
|
|
|
|
|
2006-08-26 08:25:57 +02:00
|
|
|
ret = xmalloc(sizeof(PulseData));
|
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;
|
2006-07-19 18:48:24 +02:00
|
|
|
ret->connAttempts = 0;
|
|
|
|
ret->lastAttempt = 0;
|
|
|
|
|
2006-07-13 21:03:49 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2006-07-19 18:50:03 +02:00
|
|
|
static void freePulseData(PulseData * pd)
|
2006-07-13 21:03:49 +02:00
|
|
|
{
|
2006-07-20 18:02:40 +02:00
|
|
|
if (pd->server)
|
|
|
|
free(pd->server);
|
|
|
|
if (pd->sink)
|
|
|
|
free(pd->sink);
|
2006-07-19 18:50:03 +02:00
|
|
|
free(pd);
|
2006-07-13 21:03:49 +02:00
|
|
|
}
|
|
|
|
|
2008-09-07 22:41:22 +02:00
|
|
|
static int pulse_initDriver(struct audio_output *audioOutput,
|
|
|
|
ConfigParam * param)
|
2006-07-13 21:03:49 +02:00
|
|
|
{
|
2006-07-20 18:02:40 +02:00
|
|
|
BlockParam *server = NULL;
|
|
|
|
BlockParam *sink = NULL;
|
|
|
|
PulseData *pd;
|
2006-07-13 21:03:49 +02:00
|
|
|
|
|
|
|
if (param) {
|
|
|
|
server = getBlockParam(param, "server");
|
|
|
|
sink = getBlockParam(param, "sink");
|
|
|
|
}
|
|
|
|
|
2006-07-19 18:50:03 +02:00
|
|
|
pd = newPulseData();
|
2006-08-26 08:25:57 +02:00
|
|
|
pd->server = server ? xstrdup(server->value) : NULL;
|
|
|
|
pd->sink = sink ? xstrdup(sink->value) : NULL;
|
2006-07-19 18:50:03 +02:00
|
|
|
audioOutput->data = pd;
|
2006-07-13 21:03:49 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-09-07 22:41:22 +02:00
|
|
|
static void pulse_finishDriver(struct audio_output *audioOutput)
|
2006-07-13 21:03:49 +02:00
|
|
|
{
|
|
|
|
freePulseData((PulseData *) audioOutput->data);
|
|
|
|
}
|
|
|
|
|
2006-08-08 04:32:58 +02:00
|
|
|
static int pulse_testDefault(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) {
|
|
|
|
WARNING("Cannot connect to default PulseAudio server: %s\n",
|
2006-07-20 18:02:40 +02:00
|
|
|
pa_strerror(error));
|
2006-07-13 21:03:49 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
pa_simple_free(s);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-09-07 22:41:22 +02:00
|
|
|
static int pulse_openDevice(struct audio_output *audioOutput)
|
2006-07-13 21:03:49 +02:00
|
|
|
{
|
2006-07-20 18:02:40 +02:00
|
|
|
PulseData *pd;
|
2008-09-07 19:19:55 +02:00
|
|
|
struct audio_format *audioFormat;
|
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-19 18:50:03 +02:00
|
|
|
pd = audioOutput->data;
|
2006-07-13 21:03:49 +02:00
|
|
|
audioFormat = &audioOutput->outAudioFormat;
|
|
|
|
|
2006-07-19 18:50:03 +02:00
|
|
|
if (pd->connAttempts != 0 &&
|
2006-07-20 18:02:40 +02:00
|
|
|
(t - pd->lastAttempt) < CONN_ATTEMPT_INTERVAL)
|
|
|
|
return -1;
|
2006-07-19 18:48:24 +02:00
|
|
|
|
2006-07-19 18:50:03 +02:00
|
|
|
pd->connAttempts++;
|
|
|
|
pd->lastAttempt = t;
|
2006-07-19 18:48:24 +02:00
|
|
|
|
2006-07-13 21:03:49 +02:00
|
|
|
if (audioFormat->bits != 16) {
|
|
|
|
ERROR("PulseAudio doesn't support %i bit audio\n",
|
|
|
|
audioFormat->bits);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
ss.format = PA_SAMPLE_S16NE;
|
|
|
|
ss.rate = audioFormat->sampleRate;
|
|
|
|
ss.channels = audioFormat->channels;
|
|
|
|
|
2006-07-19 18:50:03 +02:00
|
|
|
pd->s = pa_simple_new(pd->server, MPD_PULSE_NAME, PA_STREAM_PLAYBACK,
|
2006-07-20 18:02:40 +02:00
|
|
|
pd->sink, audioOutput->name, &ss, NULL, NULL,
|
|
|
|
&error);
|
2006-07-19 18:50:03 +02:00
|
|
|
if (!pd->s) {
|
2006-07-20 18:02:40 +02:00
|
|
|
ERROR("Cannot connect to server in PulseAudio output "
|
2006-07-19 18:48:24 +02:00
|
|
|
"\"%s\" (attempt %i): %s\n", audioOutput->name,
|
2006-07-19 18:50:03 +02:00
|
|
|
pd->connAttempts, pa_strerror(error));
|
2006-07-13 21:03:49 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2006-07-19 18:50:03 +02:00
|
|
|
pd->connAttempts = 0;
|
2006-07-13 21:03:49 +02:00
|
|
|
audioOutput->open = 1;
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
DEBUG("PulseAudio output \"%s\" connected and playing %i bit, %i "
|
2006-07-13 21:03:49 +02:00
|
|
|
"channel audio at %i Hz\n", audioOutput->name, audioFormat->bits,
|
|
|
|
audioFormat->channels, audioFormat->sampleRate);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-09-07 22:41:22 +02:00
|
|
|
static void pulse_dropBufferedAudio(struct audio_output *audioOutput)
|
2006-07-13 21:03:49 +02:00
|
|
|
{
|
2006-07-20 18:02:40 +02:00
|
|
|
PulseData *pd;
|
2006-07-13 21:03:49 +02:00
|
|
|
int error;
|
|
|
|
|
2006-07-19 18:50:03 +02:00
|
|
|
pd = audioOutput->data;
|
2006-07-20 18:02:40 +02:00
|
|
|
if (pa_simple_flush(pd->s, &error) < 0)
|
2006-07-13 21:03:49 +02:00
|
|
|
WARNING("Flush failed in PulseAudio output \"%s\": %s\n",
|
2006-07-20 18:02:40 +02:00
|
|
|
audioOutput->name, pa_strerror(error));
|
2006-07-13 21:03:49 +02:00
|
|
|
}
|
|
|
|
|
2008-09-07 22:41:22 +02:00
|
|
|
static void pulse_closeDevice(struct audio_output *audioOutput)
|
2006-07-13 21:03:49 +02:00
|
|
|
{
|
2006-07-20 18:02:40 +02:00
|
|
|
PulseData *pd;
|
2006-07-13 21:03:49 +02:00
|
|
|
|
2006-07-19 18:50:03 +02:00
|
|
|
pd = audioOutput->data;
|
|
|
|
if (pd->s) {
|
|
|
|
pa_simple_drain(pd->s, NULL);
|
|
|
|
pa_simple_free(pd->s);
|
2006-07-13 21:03:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
audioOutput->open = 0;
|
|
|
|
}
|
|
|
|
|
2008-09-07 22:41:22 +02:00
|
|
|
static int pulse_playAudio(struct audio_output *audioOutput,
|
2008-04-12 06:15:52 +02:00
|
|
|
const char *playChunk, size_t size)
|
2006-07-13 21:03:49 +02:00
|
|
|
{
|
2006-07-20 18:02:40 +02:00
|
|
|
PulseData *pd;
|
2006-07-13 21:03:49 +02:00
|
|
|
int error;
|
|
|
|
|
2006-07-19 18:50:03 +02:00
|
|
|
pd = audioOutput->data;
|
2006-07-13 21:03:49 +02:00
|
|
|
|
2006-07-19 18:50:03 +02:00
|
|
|
if (pa_simple_write(pd->s, playChunk, size, &error) < 0) {
|
2006-07-20 18:02:40 +02:00
|
|
|
ERROR("PulseAudio output \"%s\" disconnecting due to write "
|
2006-07-13 21:03:49 +02:00
|
|
|
"error: %s\n", audioOutput->name, pa_strerror(error));
|
|
|
|
pulse_closeDevice(audioOutput);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-09-07 22:41:22 +02:00
|
|
|
struct audio_output_plugin pulsePlugin = {
|
2006-07-13 21:03:49 +02:00
|
|
|
"pulse",
|
|
|
|
pulse_testDefault,
|
|
|
|
pulse_initDriver,
|
|
|
|
pulse_finishDriver,
|
|
|
|
pulse_openDevice,
|
|
|
|
pulse_playAudio,
|
|
|
|
pulse_dropBufferedAudio,
|
|
|
|
pulse_closeDevice,
|
2006-07-20 20:53:56 +02:00
|
|
|
NULL, /* sendMetadataFunc */
|
2006-07-13 21:03:49 +02:00
|
|
|
};
|
|
|
|
|
2006-07-20 20:53:56 +02:00
|
|
|
#else /* HAVE_PULSE */
|
2006-07-13 21:03:49 +02:00
|
|
|
|
2006-07-14 19:39:14 +02:00
|
|
|
DISABLED_AUDIO_OUTPUT_PLUGIN(pulsePlugin)
|
2006-07-20 20:53:56 +02:00
|
|
|
#endif /* HAVE_PULSE */
|