2005-03-05 06:22:30 +01:00
|
|
|
/* the Music Player Daemon (MPD)
|
|
|
|
* (c)2003-2004 by Warren Dukes (shank@mercury.chem.pitt.edu)
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "../audioOutput.h"
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#ifdef HAVE_ALSA
|
|
|
|
|
|
|
|
#define ALSA_PCM_NEW_HW_PARAMS_API
|
|
|
|
#define ALSA_PCM_NEW_SW_PARAMS_API
|
|
|
|
|
|
|
|
#define MPD_ALSA_BUFFER_TIME 500000
|
|
|
|
#define MPD_ALSA_PERIOD_TIME 50000
|
|
|
|
|
|
|
|
#include "../conf.h"
|
|
|
|
#include "../log.h"
|
|
|
|
#include "../sig_handlers.h"
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <signal.h>
|
|
|
|
|
|
|
|
#include <alsa/asoundlib.h>
|
|
|
|
|
|
|
|
typedef snd_pcm_sframes_t alsa_writei_t(snd_pcm_t *pcm, const void *buffer,
|
|
|
|
snd_pcm_uframes_t size);
|
|
|
|
|
|
|
|
typedef struct _AlsaData {
|
|
|
|
char * device;
|
2005-03-05 21:51:36 +01:00
|
|
|
snd_pcm_t * pcmHandle;
|
2005-03-05 06:22:30 +01:00
|
|
|
alsa_writei_t * writei;
|
2005-03-05 06:45:39 +01:00
|
|
|
int sampleSize;
|
2005-03-05 21:51:36 +01:00
|
|
|
int useMmap;
|
|
|
|
int canPause;
|
|
|
|
int canResume;
|
2005-03-05 06:22:30 +01:00
|
|
|
} AlsaData;
|
|
|
|
|
|
|
|
static AlsaData * newAlsaData() {
|
|
|
|
AlsaData * ret = malloc(sizeof(AlsaData));
|
|
|
|
|
|
|
|
ret->device = NULL;
|
2005-03-05 21:51:36 +01:00
|
|
|
ret->pcmHandle = NULL;
|
2005-03-05 06:22:30 +01:00
|
|
|
ret->writei = snd_pcm_writei;
|
2005-03-05 21:51:36 +01:00
|
|
|
ret->useMmap = 0;
|
2005-03-05 06:22:30 +01:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void freeAlsaData(AlsaData * ad) {
|
|
|
|
if(ad->device) free(ad->device);
|
|
|
|
|
|
|
|
free(ad);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int alsa_initDriver(AudioOutput * audioOutput, ConfigParam * param) {
|
2005-03-12 04:10:09 +01:00
|
|
|
BlockParam * bp = NULL;
|
|
|
|
|
|
|
|
if(param) bp = getBlockParam(param, "device");
|
|
|
|
|
2005-03-05 06:22:30 +01:00
|
|
|
AlsaData * ad = newAlsaData();
|
|
|
|
|
|
|
|
audioOutput->data = ad;
|
|
|
|
|
|
|
|
ad->device = bp ? strdup(bp->value) : strdup("default");
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void alsa_finishDriver(AudioOutput * audioOutput) {
|
|
|
|
AlsaData * ad = audioOutput->data;
|
|
|
|
|
|
|
|
freeAlsaData(ad);
|
|
|
|
}
|
|
|
|
|
2005-03-12 04:10:09 +01:00
|
|
|
static int alsa_testDefault()
|
|
|
|
{
|
|
|
|
snd_pcm_t * handle;
|
|
|
|
|
|
|
|
int ret = snd_pcm_open(&handle, "default", SND_PCM_STREAM_PLAYBACK,
|
|
|
|
SND_PCM_NONBLOCK);
|
|
|
|
|
|
|
|
if(ret) {
|
|
|
|
WARNING("Error opening default alsa device: %s\n",
|
|
|
|
snd_strerror(-ret));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
else snd_pcm_close(handle);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-03-05 06:22:30 +01:00
|
|
|
static int alsa_openDevice(AudioOutput * audioOutput)
|
|
|
|
{
|
|
|
|
AlsaData * ad = audioOutput->data;
|
|
|
|
AudioFormat * audioFormat = &audioOutput->outAudioFormat;
|
|
|
|
snd_pcm_format_t bitformat;
|
|
|
|
snd_pcm_hw_params_t * hwparams;
|
|
|
|
snd_pcm_sw_params_t * swparams;
|
|
|
|
unsigned int sampleRate = audioFormat->sampleRate;
|
2005-03-08 02:53:13 +01:00
|
|
|
unsigned int channels = audioFormat->channels;
|
2005-03-05 06:22:30 +01:00
|
|
|
snd_pcm_uframes_t alsa_buffer_size;
|
|
|
|
snd_pcm_uframes_t alsa_period_size;
|
|
|
|
unsigned int alsa_buffer_time = MPD_ALSA_BUFFER_TIME;
|
|
|
|
unsigned int alsa_period_time = MPD_ALSA_PERIOD_TIME;
|
|
|
|
int err;
|
2005-03-13 17:42:04 +01:00
|
|
|
char * cmd = NULL;
|
2005-03-05 06:22:30 +01:00
|
|
|
|
|
|
|
switch(audioFormat->bits) {
|
|
|
|
case 8:
|
|
|
|
bitformat = SND_PCM_FORMAT_S8;
|
|
|
|
break;
|
|
|
|
case 16:
|
|
|
|
bitformat = SND_PCM_FORMAT_S16;
|
|
|
|
break;
|
|
|
|
case 24:
|
2005-03-08 02:53:13 +01:00
|
|
|
bitformat = SND_PCM_FORMAT_S24;
|
2005-03-05 06:22:30 +01:00
|
|
|
break;
|
|
|
|
case 32:
|
2005-03-08 02:53:13 +01:00
|
|
|
bitformat = SND_PCM_FORMAT_S32;
|
2005-03-05 06:22:30 +01:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ERROR("Alsa device \"%s\" doesn't support %i bit audio\n",
|
|
|
|
ad->device, audioFormat->bits);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2005-03-05 21:51:36 +01:00
|
|
|
err = snd_pcm_open(&ad->pcmHandle, ad->device,
|
2005-03-05 15:01:13 +01:00
|
|
|
SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK);
|
2005-03-05 06:22:30 +01:00
|
|
|
if(err < 0) {
|
2005-03-05 21:51:36 +01:00
|
|
|
ad->pcmHandle = NULL;
|
2005-03-05 06:22:30 +01:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2005-03-13 17:42:04 +01:00
|
|
|
cmd = "snd_pcm_nonblock";
|
2005-03-05 21:51:36 +01:00
|
|
|
err = snd_pcm_nonblock(ad->pcmHandle, 0);
|
2005-03-05 15:01:13 +01:00
|
|
|
if(err < 0) goto error;
|
2005-03-05 06:22:30 +01:00
|
|
|
|
2005-03-05 06:45:39 +01:00
|
|
|
/* configure HW params */
|
2005-03-05 06:22:30 +01:00
|
|
|
snd_pcm_hw_params_alloca(&hwparams);
|
|
|
|
|
2005-03-13 17:42:04 +01:00
|
|
|
cmd = "snd_pcm_hw_params_any";
|
2005-03-05 21:51:36 +01:00
|
|
|
err = snd_pcm_hw_params_any(ad->pcmHandle, hwparams);
|
2005-03-05 06:22:30 +01:00
|
|
|
if(err < 0) goto error;
|
|
|
|
|
2005-03-05 21:51:36 +01:00
|
|
|
if(ad->useMmap) {
|
|
|
|
err = snd_pcm_hw_params_set_access(ad->pcmHandle, hwparams,
|
2005-03-05 06:22:30 +01:00
|
|
|
SND_PCM_ACCESS_MMAP_INTERLEAVED);
|
|
|
|
if(err < 0) {
|
|
|
|
ERROR("Cannot set mmap'ed mode on alsa device \"%s\": "
|
|
|
|
" %s\n", ad->device,
|
|
|
|
snd_strerror(-err));
|
|
|
|
ERROR("Falling back to direct write mode\n");
|
2005-03-05 21:51:36 +01:00
|
|
|
ad->useMmap = 0;
|
2005-03-05 06:22:30 +01:00
|
|
|
}
|
|
|
|
else ad->writei = snd_pcm_mmap_writei;
|
|
|
|
}
|
|
|
|
|
2005-03-05 21:51:36 +01:00
|
|
|
if(!ad->useMmap) {
|
2005-03-13 17:42:04 +01:00
|
|
|
cmd = "snd_pcm_hw_params_set_access";
|
2005-03-05 21:51:36 +01:00
|
|
|
err = snd_pcm_hw_params_set_access(ad->pcmHandle, hwparams,
|
2005-03-05 06:22:30 +01:00
|
|
|
SND_PCM_ACCESS_RW_INTERLEAVED);
|
|
|
|
if(err < 0) goto error;
|
2005-03-05 06:45:39 +01:00
|
|
|
ad->writei = snd_pcm_writei;
|
2005-03-05 06:22:30 +01:00
|
|
|
}
|
|
|
|
|
2005-03-05 21:51:36 +01:00
|
|
|
err = snd_pcm_hw_params_set_format(ad->pcmHandle, hwparams, bitformat);
|
2005-03-05 06:22:30 +01:00
|
|
|
if(err < 0) {
|
|
|
|
ERROR("Alsa device \"%s\" does not support %i bit audio: "
|
|
|
|
"%s\n", ad->device, (int)bitformat,
|
|
|
|
snd_strerror(-err));
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2005-03-08 02:53:13 +01:00
|
|
|
err = snd_pcm_hw_params_set_channels_near(ad->pcmHandle, hwparams,
|
|
|
|
&channels);
|
2005-03-05 06:22:30 +01:00
|
|
|
if(err < 0) {
|
|
|
|
ERROR("Alsa device \"%s\" does not support %i channels: "
|
|
|
|
"%s\n", ad->device, (int)audioFormat->channels,
|
|
|
|
snd_strerror(-err));
|
|
|
|
goto fail;
|
|
|
|
}
|
2005-03-08 02:53:13 +01:00
|
|
|
audioFormat->channels = channels;
|
2005-03-05 06:22:30 +01:00
|
|
|
|
2005-03-05 21:51:36 +01:00
|
|
|
err = snd_pcm_hw_params_set_rate_near(ad->pcmHandle, hwparams,
|
2005-03-05 06:22:30 +01:00
|
|
|
&sampleRate, 0);
|
|
|
|
if(err < 0 || sampleRate == 0) {
|
|
|
|
ERROR("Alsa device \"%s\" does not support %i Hz audio\n",
|
|
|
|
ad->device, (int)audioFormat->sampleRate);
|
|
|
|
goto fail;
|
|
|
|
}
|
2005-03-08 02:53:13 +01:00
|
|
|
audioFormat->sampleRate = sampleRate;
|
2005-03-05 06:22:30 +01:00
|
|
|
|
2005-03-13 17:42:04 +01:00
|
|
|
cmd = "snd_pcm_hw_params_set_buffer_time_near";
|
2005-03-05 21:51:36 +01:00
|
|
|
err = snd_pcm_hw_params_set_buffer_time_near(ad->pcmHandle, hwparams,
|
2005-03-05 06:22:30 +01:00
|
|
|
&alsa_buffer_time, 0);
|
|
|
|
if(err < 0) goto error;
|
|
|
|
|
2005-03-13 17:42:04 +01:00
|
|
|
cmd = "snd_pcm_hw_params_set_period_time_near";
|
2005-03-05 21:51:36 +01:00
|
|
|
err = snd_pcm_hw_params_set_period_time_near(ad->pcmHandle, hwparams,
|
2005-03-05 06:22:30 +01:00
|
|
|
&alsa_period_time, 0);
|
|
|
|
if(err < 0) goto error;
|
|
|
|
|
2005-03-13 17:42:04 +01:00
|
|
|
cmd = "snd_pcm_hw_params";
|
2005-03-05 21:51:36 +01:00
|
|
|
err = snd_pcm_hw_params(ad->pcmHandle, hwparams);
|
2005-03-05 06:22:30 +01:00
|
|
|
if(err < 0) goto error;
|
|
|
|
|
2005-03-13 17:42:04 +01:00
|
|
|
cmd = "snd_pcm_hw_params_get_buffer_size";
|
2005-03-05 06:22:30 +01:00
|
|
|
err = snd_pcm_hw_params_get_buffer_size(hwparams, &alsa_buffer_size);
|
|
|
|
if(err < 0) goto error;
|
|
|
|
|
2005-03-13 17:42:04 +01:00
|
|
|
cmd = "snd_pcm_hw_params_get_period_size";
|
2005-03-05 06:22:30 +01:00
|
|
|
err = snd_pcm_hw_params_get_period_size(hwparams, &alsa_period_size, 0);
|
|
|
|
if(err < 0) goto error;
|
|
|
|
|
2005-03-05 21:51:36 +01:00
|
|
|
ad->canPause = snd_pcm_hw_params_can_pause(hwparams);
|
|
|
|
ad->canResume = snd_pcm_hw_params_can_resume(hwparams);
|
|
|
|
|
2005-03-05 06:45:39 +01:00
|
|
|
/* configure SW params */
|
2005-03-05 06:22:30 +01:00
|
|
|
snd_pcm_sw_params_alloca(&swparams);
|
|
|
|
|
2005-03-13 17:42:04 +01:00
|
|
|
cmd = "snd_pcm_sw_params_current";
|
|
|
|
err = snd_pcm_sw_params_current(ad->pcmHandle, swparams);
|
|
|
|
if(err < 0) goto error;
|
|
|
|
|
|
|
|
cmd = "snd_pcm_sw_params_set_start_threshold";
|
2005-03-05 21:51:36 +01:00
|
|
|
err = snd_pcm_sw_params_set_start_threshold(ad->pcmHandle, swparams,
|
2005-03-05 06:22:30 +01:00
|
|
|
alsa_buffer_size - alsa_period_size);
|
|
|
|
if(err < 0) goto error;
|
|
|
|
|
2005-03-13 17:42:04 +01:00
|
|
|
cmd = "snd_pcm_sw_params_set_avail_min";
|
|
|
|
err = snd_pcm_sw_params_set_avail_min(ad->pcmHandle, swparams,
|
|
|
|
alsa_period_size);
|
|
|
|
if(err < 0) goto error;
|
|
|
|
|
|
|
|
cmd = "snd_pcm_sw_params_set_xfer_align";
|
|
|
|
err = snd_pcm_sw_params_set_xfer_align(ad->pcmHandle, swparams, 1);
|
|
|
|
if(err < 0) goto error;
|
|
|
|
|
|
|
|
cmd = "snd_pcm_sw_params";
|
2005-03-05 21:51:36 +01:00
|
|
|
err = snd_pcm_sw_params(ad->pcmHandle, swparams);
|
2005-03-05 06:22:30 +01:00
|
|
|
if(err < 0) goto error;
|
|
|
|
|
2005-03-05 06:45:39 +01:00
|
|
|
ad->sampleSize = (audioFormat->bits/8)*audioFormat->channels;
|
|
|
|
|
2005-03-05 06:22:30 +01:00
|
|
|
audioOutput->open = 1;
|
|
|
|
|
2005-03-08 02:53:13 +01:00
|
|
|
DEBUG("alsa device \"%s\" will be playing %i channel audio at %i Hz\n",
|
|
|
|
ad->device, channels, sampleRate);
|
|
|
|
|
2005-03-05 06:22:30 +01:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
error:
|
2005-03-13 17:42:04 +01:00
|
|
|
if(cmd) {
|
|
|
|
ERROR("Error opening alsa device \"%s\" (%s): %s\n",
|
|
|
|
ad->device, cmd, snd_strerror(-err));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
ERROR("Error opening alsa device \"%s\": %s\n", ad->device,
|
|
|
|
snd_strerror(-err));
|
|
|
|
}
|
2005-03-05 06:22:30 +01:00
|
|
|
fail:
|
2005-03-05 21:51:36 +01:00
|
|
|
if(ad->pcmHandle) snd_pcm_close(ad->pcmHandle);
|
|
|
|
ad->pcmHandle = NULL;
|
2005-03-05 06:22:30 +01:00
|
|
|
audioOutput->open = 0;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2005-03-05 15:01:13 +01:00
|
|
|
static void alsa_dropBufferedAudio(AudioOutput * audioOutput) {
|
2005-03-05 06:22:30 +01:00
|
|
|
AlsaData * ad = audioOutput->data;
|
|
|
|
|
2005-03-05 21:51:36 +01:00
|
|
|
snd_pcm_drop(ad->pcmHandle);
|
2005-03-05 06:22:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
inline static int alsa_errorRecovery(AlsaData * ad, int err) {
|
|
|
|
if(err == -EPIPE) {
|
|
|
|
DEBUG("Underrun on alsa device \"%s\"\n", ad->device);
|
2005-03-05 15:01:13 +01:00
|
|
|
}
|
|
|
|
else if(err == -ESTRPIPE) {
|
|
|
|
DEBUG("alsa device \"%s\" was suspended\n", ad->device);
|
|
|
|
}
|
|
|
|
|
2005-03-05 21:51:36 +01:00
|
|
|
switch(snd_pcm_state(ad->pcmHandle)) {
|
|
|
|
case SND_PCM_STATE_PAUSED:
|
|
|
|
err = snd_pcm_pause(ad->pcmHandle, /* disable */ 0);
|
|
|
|
break;
|
|
|
|
case SND_PCM_STATE_SUSPENDED:
|
|
|
|
err = ad->canResume ?
|
|
|
|
snd_pcm_resume(ad->pcmHandle) :
|
|
|
|
snd_pcm_prepare(ad->pcmHandle);
|
|
|
|
break;
|
2005-03-05 15:01:13 +01:00
|
|
|
case SND_PCM_STATE_SETUP:
|
|
|
|
case SND_PCM_STATE_XRUN:
|
2005-03-05 21:51:36 +01:00
|
|
|
err = snd_pcm_prepare(ad->pcmHandle);
|
|
|
|
break;
|
2005-03-05 15:01:13 +01:00
|
|
|
default:
|
|
|
|
/* unknown state, do nothing */
|
|
|
|
break;
|
2005-03-05 06:22:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2005-03-05 15:01:13 +01:00
|
|
|
static void alsa_closeDevice(AudioOutput * audioOutput) {
|
|
|
|
AlsaData * ad = audioOutput->data;
|
|
|
|
|
2005-03-05 21:51:36 +01:00
|
|
|
if(ad->pcmHandle) {
|
|
|
|
snd_pcm_drain(ad->pcmHandle);
|
|
|
|
snd_pcm_close(ad->pcmHandle);
|
|
|
|
ad->pcmHandle = NULL;
|
2005-03-05 15:01:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
audioOutput->open = 0;
|
|
|
|
}
|
|
|
|
|
2005-03-05 06:22:30 +01:00
|
|
|
static int alsa_playAudio(AudioOutput * audioOutput, char * playChunk,
|
|
|
|
int size)
|
|
|
|
{
|
|
|
|
AlsaData * ad = audioOutput->data;
|
|
|
|
int ret;
|
|
|
|
|
2005-03-05 06:45:39 +01:00
|
|
|
size /= ad->sampleSize;
|
|
|
|
|
2005-03-05 06:22:30 +01:00
|
|
|
while (size > 0) {
|
2005-03-05 21:51:36 +01:00
|
|
|
ret = ad->writei(ad->pcmHandle, playChunk, size);
|
2005-03-05 06:22:30 +01:00
|
|
|
|
2005-03-08 01:17:33 +01:00
|
|
|
if(ret == -EAGAIN || ret == -EINTR) continue;
|
2005-03-05 06:22:30 +01:00
|
|
|
|
2005-03-05 21:51:36 +01:00
|
|
|
if(ret < 0) {
|
|
|
|
if( alsa_errorRecovery(ad, ret) < 0) {
|
|
|
|
ERROR("closing alsa device \"%s\" due to write "
|
|
|
|
"error: %s\n", ad->device,
|
|
|
|
snd_strerror(-errno));
|
|
|
|
alsa_closeDevice(audioOutput);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
continue;
|
2005-03-05 06:22:30 +01:00
|
|
|
}
|
2005-03-05 21:51:36 +01:00
|
|
|
|
2005-03-05 06:45:39 +01:00
|
|
|
playChunk += ret * ad->sampleSize;
|
2005-03-05 06:22:30 +01:00
|
|
|
size -= ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
AudioOutputPlugin alsaPlugin =
|
|
|
|
{
|
|
|
|
"alsa",
|
2005-03-12 04:10:09 +01:00
|
|
|
alsa_testDefault,
|
2005-03-05 06:22:30 +01:00
|
|
|
alsa_initDriver,
|
|
|
|
alsa_finishDriver,
|
|
|
|
alsa_openDevice,
|
|
|
|
alsa_playAudio,
|
2005-03-05 15:01:13 +01:00
|
|
|
alsa_dropBufferedAudio,
|
2005-03-05 06:22:30 +01:00
|
|
|
alsa_closeDevice,
|
|
|
|
NULL /* sendMetadataFunc */
|
|
|
|
};
|
|
|
|
|
|
|
|
#else /* HAVE ALSA */
|
|
|
|
|
|
|
|
AudioOutputPlugin alsaPlugin =
|
|
|
|
{
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
2005-03-12 04:10:09 +01:00
|
|
|
NULL,
|
2005-03-05 06:22:30 +01:00
|
|
|
NULL /* sendMetadataFunc */
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif /* HAVE_ALSA */
|
|
|
|
|
|
|
|
|