2004-10-29 05:30:23 +02: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
|
|
|
|
*
|
|
|
|
* OSS audio output (c) 2004 by Eric Wong <eric@petta-tech.com>
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
2004-11-02 13:46:52 +01:00
|
|
|
#include "../audioOutput.h"
|
2004-10-29 05:30:23 +02:00
|
|
|
|
2004-10-30 06:19:11 +02:00
|
|
|
#include <stdlib.h>
|
2004-10-29 05:30:23 +02:00
|
|
|
|
|
|
|
#ifdef HAVE_OSS
|
|
|
|
|
2004-11-02 13:46:52 +01:00
|
|
|
#include "../conf.h"
|
|
|
|
#include "../log.h"
|
|
|
|
#include "../sig_handlers.h"
|
2004-10-29 05:30:23 +02:00
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <signal.h>
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#if defined(__OpenBSD__) || defined(__NetBSD__)
|
|
|
|
# include <soundcard.h>
|
|
|
|
#else /* !(defined(__OpenBSD__) || defined(__NetBSD__) */
|
|
|
|
# include <sys/soundcard.h>
|
|
|
|
#endif /* !(defined(__OpenBSD__) || defined(__NetBSD__) */
|
|
|
|
|
2004-10-30 06:19:11 +02:00
|
|
|
typedef struct _OssData {
|
2004-10-29 06:11:10 +02:00
|
|
|
int fd;
|
|
|
|
char * device;
|
2004-10-29 05:30:23 +02:00
|
|
|
} OssData;
|
|
|
|
|
|
|
|
static OssData * newOssData() {
|
|
|
|
OssData * ret = malloc(sizeof(OssData));
|
|
|
|
|
2004-10-29 06:11:10 +02:00
|
|
|
ret->device = NULL;
|
|
|
|
ret->fd = -1;
|
2004-10-29 05:30:23 +02:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void freeOssData(OssData * od) {
|
2004-10-29 06:11:10 +02:00
|
|
|
if(od->device) free(od->device);
|
|
|
|
|
2004-10-29 05:30:23 +02:00
|
|
|
free(od);
|
|
|
|
}
|
|
|
|
|
2004-10-30 06:19:11 +02:00
|
|
|
static int oss_initDriver(AudioOutput * audioOutput, ConfigParam * param) {
|
2004-10-29 06:11:10 +02:00
|
|
|
BlockParam * bp = getBlockParam(param, "device");
|
2004-10-30 06:19:11 +02:00
|
|
|
OssData * od = newOssData();
|
2004-10-29 06:11:10 +02:00
|
|
|
|
|
|
|
audioOutput->data = od;
|
|
|
|
|
|
|
|
if(!bp) {
|
|
|
|
int fd;
|
|
|
|
|
2004-11-02 03:03:00 +01:00
|
|
|
if(0 <= (fd = open("/dev/sound/dsp", O_WRONLY))) {
|
2004-10-29 06:11:10 +02:00
|
|
|
od->device = strdup("/dev/sound/dsp");
|
2004-11-02 03:03:00 +01:00
|
|
|
close(fd);
|
2004-10-29 06:11:10 +02:00
|
|
|
}
|
2004-11-02 03:03:00 +01:00
|
|
|
else if(0 <= (fd = open("/dev/dsp", O_WRONLY))) {
|
2004-10-29 06:11:10 +02:00
|
|
|
od->device = strdup("/dev/dsp");
|
2004-11-02 03:03:00 +01:00
|
|
|
close(fd);
|
2004-10-29 06:11:10 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
ERROR("Error trying to open default OSS device "
|
|
|
|
"specified at line %i\n", param->line);
|
|
|
|
ERROR("Specify a OSS device and/or check your "
|
|
|
|
"permissions\n");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
od->fd = -1;
|
|
|
|
|
2004-10-30 06:19:11 +02:00
|
|
|
return 0;
|
2004-10-29 05:30:23 +02:00
|
|
|
}
|
2004-10-29 06:11:10 +02:00
|
|
|
|
|
|
|
od->device = strdup(bp->value);
|
|
|
|
|
2004-10-30 06:19:11 +02:00
|
|
|
return 0;
|
2004-10-29 05:30:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void oss_finishDriver(AudioOutput * audioOutput) {
|
|
|
|
OssData * od = audioOutput->data;
|
|
|
|
|
|
|
|
freeOssData(od);
|
|
|
|
}
|
|
|
|
|
2004-10-30 06:19:11 +02:00
|
|
|
static int oss_openDevice(AudioOutput * audioOutput, AudioFormat * audioFormat)
|
2004-10-29 05:30:23 +02:00
|
|
|
{
|
2004-10-30 06:19:11 +02:00
|
|
|
OssData * od = audioOutput->data;
|
|
|
|
#ifdef WORDS_BIGENDIAN
|
|
|
|
int i = AFMT_S16_BE;
|
|
|
|
#else
|
|
|
|
int i = AFMT_S16_LE;
|
|
|
|
#endif
|
2004-10-29 05:30:23 +02:00
|
|
|
|
2004-10-30 06:19:11 +02:00
|
|
|
if((od->fd = open(od->device, O_WRONLY)) < 0)
|
|
|
|
goto fail;
|
|
|
|
if(ioctl(od->fd, SNDCTL_DSP_SETFMT, &i))
|
|
|
|
goto fail;
|
|
|
|
if(ioctl(od->fd, SNDCTL_DSP_CHANNELS, &audioFormat->channels))
|
|
|
|
goto fail;
|
|
|
|
if(ioctl(od->fd, SNDCTL_DSP_SPEED, &audioFormat->sampleRate))
|
|
|
|
goto fail;
|
|
|
|
if(ioctl(od->fd, SNDCTL_DSP_SAMPLESIZE, &audioFormat->bits))
|
|
|
|
goto fail;
|
|
|
|
/*i = 1; if (ioctl(od->fd,SNDCTL_DSP_STEREO,&i)) err != 32; */
|
|
|
|
|
|
|
|
audioOutput->open = 1;
|
2004-10-29 05:30:23 +02:00
|
|
|
|
|
|
|
return 0;
|
2004-10-30 06:19:11 +02:00
|
|
|
|
|
|
|
fail:
|
|
|
|
if(od->fd >= 0) close(od->fd);
|
|
|
|
audioOutput->open = 0;
|
|
|
|
ERROR("Error opening OSS device \"%s\": %s\n", od->device,
|
|
|
|
strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void oss_closeDevice(AudioOutput * audioOutput) {
|
|
|
|
OssData * od = audioOutput->data;
|
|
|
|
|
|
|
|
if(od->fd >= 0) {
|
|
|
|
close(od->fd);
|
|
|
|
od->fd = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
audioOutput->open = 0;
|
2004-10-29 05:30:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int oss_playAudio(AudioOutput * audioOutput, char * playChunk,
|
|
|
|
int size)
|
|
|
|
{
|
2004-10-30 06:19:11 +02:00
|
|
|
OssData * od = audioOutput->data;
|
2004-10-29 05:30:23 +02:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
while (size > 0) {
|
2004-10-30 06:19:11 +02:00
|
|
|
ret = write(od->fd, playChunk, size);
|
2004-10-29 05:30:23 +02:00
|
|
|
if(ret<0) {
|
|
|
|
ERROR("closing audio device due to write error\n");
|
2004-10-30 06:19:11 +02:00
|
|
|
oss_closeDevice(audioOutput);
|
2004-10-29 05:30:23 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2004-10-30 06:19:11 +02:00
|
|
|
playChunk += ret;
|
|
|
|
size -= ret;
|
2004-10-29 05:30:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-10-30 06:19:11 +02:00
|
|
|
AudioOutputPlugin ossPlugin =
|
2004-10-29 05:30:23 +02:00
|
|
|
{
|
|
|
|
"oss",
|
|
|
|
oss_initDriver,
|
|
|
|
oss_finishDriver,
|
|
|
|
oss_openDevice,
|
|
|
|
oss_playAudio,
|
|
|
|
oss_closeDevice,
|
|
|
|
NULL /* sendMetadataFunc */
|
|
|
|
};
|
|
|
|
|
|
|
|
#else /* HAVE OSS */
|
|
|
|
|
2004-10-30 06:19:11 +02:00
|
|
|
AudioOutputPlugin ossPlugin =
|
2004-10-29 05:30:23 +02:00
|
|
|
{
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL /* sendMetadataFunc */
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif /* HAVE_OSS */
|
|
|
|
|
|
|
|
|