2004-10-29 05:30:23 +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)
|
2004-10-29 05:30:23 +02:00
|
|
|
* This project's homepage is: http://www.musicpd.org
|
|
|
|
*
|
2007-04-05 05:22:33 +02:00
|
|
|
* OSS audio output (c) 2004, 2005, 2006, 2007 by Eric Wong <eric@petta-tech.com>
|
2006-07-12 04:34:23 +02:00
|
|
|
* and Warren Dukes <warren.dukes@gmail.com>
|
2004-10-29 05:30:23 +02:00
|
|
|
*
|
|
|
|
* 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"
|
2004-10-29 05:30:23 +02:00
|
|
|
|
|
|
|
#ifdef HAVE_OSS
|
|
|
|
|
2008-09-07 22:41:17 +02:00
|
|
|
#include "../utils.h"
|
2004-11-02 13:46:52 +01:00
|
|
|
#include "../log.h"
|
2004-10-29 05:30:23 +02:00
|
|
|
|
|
|
|
#if defined(__OpenBSD__) || defined(__NetBSD__)
|
|
|
|
# include <soundcard.h>
|
2006-07-20 20:53:56 +02:00
|
|
|
#else /* !(defined(__OpenBSD__) || defined(__NetBSD__) */
|
2004-10-29 05:30:23 +02:00
|
|
|
# include <sys/soundcard.h>
|
2006-07-20 20:53:56 +02:00
|
|
|
#endif /* !(defined(__OpenBSD__) || defined(__NetBSD__) */
|
2004-10-29 05:30:23 +02:00
|
|
|
|
2006-03-25 06:33:14 +01:00
|
|
|
#ifdef WORDS_BIGENDIAN
|
|
|
|
# define AFMT_S16_MPD AFMT_S16_BE
|
|
|
|
#else
|
|
|
|
# define AFMT_S16_MPD AFMT_S16_LE
|
2006-07-20 20:53:56 +02:00
|
|
|
#endif /* WORDS_BIGENDIAN */
|
2006-03-25 06:33:14 +01:00
|
|
|
|
2004-10-30 06:19:11 +02:00
|
|
|
typedef struct _OssData {
|
2004-10-29 06:11:10 +02:00
|
|
|
int fd;
|
2006-08-01 14:39:10 +02:00
|
|
|
const char *device;
|
2005-03-05 15:01:13 +01:00
|
|
|
int channels;
|
|
|
|
int sampleRate;
|
|
|
|
int bitFormat;
|
|
|
|
int bits;
|
2006-07-20 18:02:40 +02:00
|
|
|
int *supported[3];
|
2005-03-10 03:47:18 +01:00
|
|
|
int numSupported[3];
|
2006-07-20 18:02:40 +02:00
|
|
|
int *unsupported[3];
|
2005-03-10 03:47:18 +01:00
|
|
|
int numUnsupported[3];
|
2004-10-29 05:30:23 +02:00
|
|
|
} OssData;
|
|
|
|
|
2005-03-10 03:47:18 +01:00
|
|
|
#define OSS_SUPPORTED 1
|
|
|
|
#define OSS_UNSUPPORTED 0
|
|
|
|
#define OSS_UNKNOWN -1
|
|
|
|
|
|
|
|
#define OSS_RATE 0
|
|
|
|
#define OSS_CHANNELS 1
|
|
|
|
#define OSS_BITS 2
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
static int getIndexForParam(int param)
|
|
|
|
{
|
2008-01-26 13:46:21 +01:00
|
|
|
int idx = 0;
|
2006-07-20 18:02:40 +02:00
|
|
|
|
|
|
|
switch (param) {
|
2005-03-10 03:47:18 +01:00
|
|
|
case SNDCTL_DSP_SPEED:
|
2008-01-26 13:46:21 +01:00
|
|
|
idx = OSS_RATE;
|
2005-03-10 03:47:18 +01:00
|
|
|
break;
|
|
|
|
case SNDCTL_DSP_CHANNELS:
|
2008-01-26 13:46:21 +01:00
|
|
|
idx = OSS_CHANNELS;
|
2005-03-10 03:47:18 +01:00
|
|
|
break;
|
|
|
|
case SNDCTL_DSP_SAMPLESIZE:
|
2008-01-26 13:46:21 +01:00
|
|
|
idx = OSS_BITS;
|
2005-03-10 03:47:18 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2008-01-26 13:46:21 +01:00
|
|
|
return idx;
|
2005-03-10 03:47:18 +01:00
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
static int findSupportedParam(OssData * od, int param, int val)
|
|
|
|
{
|
2005-03-10 03:47:18 +01:00
|
|
|
int i;
|
2008-01-26 13:46:21 +01:00
|
|
|
int idx = getIndexForParam(param);
|
2006-07-20 18:02:40 +02:00
|
|
|
|
2008-01-26 13:46:21 +01:00
|
|
|
for (i = 0; i < od->numSupported[idx]; i++) {
|
|
|
|
if (od->supported[idx][i] == val)
|
2006-07-20 18:02:40 +02:00
|
|
|
return 1;
|
2005-03-10 03:47:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-01-26 13:46:21 +01:00
|
|
|
static int canConvert(int idx, int val)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2008-01-26 13:46:21 +01:00
|
|
|
switch (idx) {
|
2005-03-10 03:47:18 +01:00
|
|
|
case OSS_BITS:
|
2006-07-20 18:02:40 +02:00
|
|
|
if (val != 16)
|
|
|
|
return 0;
|
2005-03-10 03:47:18 +01:00
|
|
|
break;
|
|
|
|
case OSS_CHANNELS:
|
2006-07-20 18:02:40 +02:00
|
|
|
if (val != 2)
|
|
|
|
return 0;
|
2005-03-10 03:47:18 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
static int getSupportedParam(OssData * od, int param, int val)
|
|
|
|
{
|
2005-03-10 03:47:18 +01:00
|
|
|
int i;
|
2008-01-26 13:46:21 +01:00
|
|
|
int idx = getIndexForParam(param);
|
2005-03-10 03:47:18 +01:00
|
|
|
int ret = -1;
|
|
|
|
int least = val;
|
|
|
|
int diff;
|
2006-07-20 18:02:40 +02:00
|
|
|
|
2008-01-26 13:46:21 +01:00
|
|
|
for (i = 0; i < od->numSupported[idx]; i++) {
|
|
|
|
diff = od->supported[idx][i] - val;
|
2006-07-20 18:02:40 +02:00
|
|
|
if (diff < 0)
|
|
|
|
diff = -diff;
|
|
|
|
if (diff < least) {
|
2008-01-26 13:46:21 +01:00
|
|
|
if (!canConvert(idx, od->supported[idx][i])) {
|
2005-03-10 03:47:18 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
least = diff;
|
2008-01-26 13:46:21 +01:00
|
|
|
ret = od->supported[idx][i];
|
2005-03-10 03:47:18 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
static int findUnsupportedParam(OssData * od, int param, int val)
|
|
|
|
{
|
2005-03-10 03:47:18 +01:00
|
|
|
int i;
|
2008-01-26 13:46:21 +01:00
|
|
|
int idx = getIndexForParam(param);
|
2006-07-20 18:02:40 +02:00
|
|
|
|
2008-01-26 13:46:21 +01:00
|
|
|
for (i = 0; i < od->numUnsupported[idx]; i++) {
|
|
|
|
if (od->unsupported[idx][i] == val)
|
2006-07-20 18:02:40 +02:00
|
|
|
return 1;
|
2005-03-10 03:47:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
static void addSupportedParam(OssData * od, int param, int val)
|
|
|
|
{
|
2008-01-26 13:46:21 +01:00
|
|
|
int idx = getIndexForParam(param);
|
2005-03-10 03:47:18 +01:00
|
|
|
|
2008-01-26 13:46:21 +01:00
|
|
|
od->numSupported[idx]++;
|
|
|
|
od->supported[idx] = xrealloc(od->supported[idx],
|
|
|
|
od->numSupported[idx] * sizeof(int));
|
|
|
|
od->supported[idx][od->numSupported[idx] - 1] = val;
|
2005-03-10 03:47:18 +01:00
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
static void addUnsupportedParam(OssData * od, int param, int val)
|
|
|
|
{
|
2008-01-26 13:46:21 +01:00
|
|
|
int idx = getIndexForParam(param);
|
2005-03-10 03:47:18 +01:00
|
|
|
|
2008-01-26 13:46:21 +01:00
|
|
|
od->numUnsupported[idx]++;
|
|
|
|
od->unsupported[idx] = xrealloc(od->unsupported[idx],
|
|
|
|
od->numUnsupported[idx] *
|
|
|
|
sizeof(int));
|
|
|
|
od->unsupported[idx][od->numUnsupported[idx] - 1] = val;
|
2005-03-10 03:47:18 +01:00
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
static void removeSupportedParam(OssData * od, int param, int val)
|
|
|
|
{
|
2008-03-26 11:37:36 +01:00
|
|
|
int i;
|
2005-03-10 03:47:18 +01:00
|
|
|
int j = 0;
|
2008-01-26 13:46:21 +01:00
|
|
|
int idx = getIndexForParam(param);
|
2005-03-10 03:47:18 +01:00
|
|
|
|
2008-01-26 13:46:21 +01:00
|
|
|
for (i = 0; i < od->numSupported[idx] - 1; i++) {
|
|
|
|
if (od->supported[idx][i] == val)
|
2006-07-20 18:02:40 +02:00
|
|
|
j = 1;
|
2008-01-26 13:46:21 +01:00
|
|
|
od->supported[idx][i] = od->supported[idx][i + j];
|
2005-03-10 03:47:18 +01:00
|
|
|
}
|
|
|
|
|
2008-01-26 13:46:21 +01:00
|
|
|
od->numSupported[idx]--;
|
|
|
|
od->supported[idx] = xrealloc(od->supported[idx],
|
|
|
|
od->numSupported[idx] * sizeof(int));
|
2005-03-10 03:47:18 +01:00
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
static void removeUnsupportedParam(OssData * od, int param, int val)
|
|
|
|
{
|
2008-03-26 11:37:36 +01:00
|
|
|
int i;
|
2005-03-10 03:47:18 +01:00
|
|
|
int j = 0;
|
2008-01-26 13:46:21 +01:00
|
|
|
int idx = getIndexForParam(param);
|
2005-03-10 03:47:18 +01:00
|
|
|
|
2008-01-26 13:46:21 +01:00
|
|
|
for (i = 0; i < od->numUnsupported[idx] - 1; i++) {
|
|
|
|
if (od->unsupported[idx][i] == val)
|
2006-07-20 18:02:40 +02:00
|
|
|
j = 1;
|
2008-01-26 13:46:21 +01:00
|
|
|
od->unsupported[idx][i] = od->unsupported[idx][i + j];
|
2005-03-10 03:47:18 +01:00
|
|
|
}
|
|
|
|
|
2008-01-26 13:46:21 +01:00
|
|
|
od->numUnsupported[idx]--;
|
|
|
|
od->unsupported[idx] = xrealloc(od->unsupported[idx],
|
|
|
|
od->numUnsupported[idx] *
|
|
|
|
sizeof(int));
|
2005-03-10 03:47:18 +01:00
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
static int isSupportedParam(OssData * od, int param, int val)
|
|
|
|
{
|
|
|
|
if (findSupportedParam(od, param, val))
|
|
|
|
return OSS_SUPPORTED;
|
|
|
|
if (findUnsupportedParam(od, param, val))
|
|
|
|
return OSS_UNSUPPORTED;
|
2005-03-10 03:47:18 +01:00
|
|
|
return OSS_UNKNOWN;
|
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
static void supportParam(OssData * od, int param, int val)
|
|
|
|
{
|
2005-03-10 03:47:18 +01:00
|
|
|
int supported = isSupportedParam(od, param, val);
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (supported == OSS_SUPPORTED)
|
|
|
|
return;
|
2005-03-10 03:47:18 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (supported == OSS_UNSUPPORTED) {
|
2005-03-10 03:47:18 +01:00
|
|
|
removeUnsupportedParam(od, param, val);
|
|
|
|
}
|
|
|
|
|
|
|
|
addSupportedParam(od, param, val);
|
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
static void unsupportParam(OssData * od, int param, int val)
|
|
|
|
{
|
2005-03-10 03:47:18 +01:00
|
|
|
int supported = isSupportedParam(od, param, val);
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (supported == OSS_UNSUPPORTED)
|
|
|
|
return;
|
2005-03-10 03:47:18 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (supported == OSS_SUPPORTED) {
|
2005-03-10 03:47:18 +01:00
|
|
|
removeSupportedParam(od, param, val);
|
|
|
|
}
|
|
|
|
|
|
|
|
addUnsupportedParam(od, param, val);
|
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
static OssData *newOssData(void)
|
|
|
|
{
|
2006-08-26 08:25:57 +02:00
|
|
|
OssData *ret = xmalloc(sizeof(OssData));
|
2004-10-29 05:30:23 +02:00
|
|
|
|
2004-10-29 06:11:10 +02:00
|
|
|
ret->device = NULL;
|
|
|
|
ret->fd = -1;
|
2004-10-29 05:30:23 +02:00
|
|
|
|
2005-03-10 03:47:18 +01:00
|
|
|
ret->supported[OSS_RATE] = NULL;
|
|
|
|
ret->supported[OSS_CHANNELS] = NULL;
|
|
|
|
ret->supported[OSS_BITS] = NULL;
|
|
|
|
ret->unsupported[OSS_RATE] = NULL;
|
|
|
|
ret->unsupported[OSS_CHANNELS] = NULL;
|
|
|
|
ret->unsupported[OSS_BITS] = NULL;
|
|
|
|
|
|
|
|
ret->numSupported[OSS_RATE] = 0;
|
|
|
|
ret->numSupported[OSS_CHANNELS] = 0;
|
|
|
|
ret->numSupported[OSS_BITS] = 0;
|
|
|
|
ret->numUnsupported[OSS_RATE] = 0;
|
|
|
|
ret->numUnsupported[OSS_CHANNELS] = 0;
|
|
|
|
ret->numUnsupported[OSS_BITS] = 0;
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
supportParam(ret, SNDCTL_DSP_SPEED, 48000);
|
|
|
|
supportParam(ret, SNDCTL_DSP_SPEED, 44100);
|
|
|
|
supportParam(ret, SNDCTL_DSP_CHANNELS, 2);
|
|
|
|
supportParam(ret, SNDCTL_DSP_SAMPLESIZE, 16);
|
2005-03-10 03:47:18 +01:00
|
|
|
|
2004-10-29 05:30:23 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
static void freeOssData(OssData * od)
|
|
|
|
{
|
|
|
|
if (od->supported[OSS_RATE])
|
|
|
|
free(od->supported[OSS_RATE]);
|
|
|
|
if (od->supported[OSS_CHANNELS])
|
|
|
|
free(od->supported[OSS_CHANNELS]);
|
|
|
|
if (od->supported[OSS_BITS])
|
|
|
|
free(od->supported[OSS_BITS]);
|
|
|
|
if (od->unsupported[OSS_RATE])
|
|
|
|
free(od->unsupported[OSS_RATE]);
|
|
|
|
if (od->unsupported[OSS_CHANNELS])
|
|
|
|
free(od->unsupported[OSS_CHANNELS]);
|
|
|
|
if (od->unsupported[OSS_BITS])
|
|
|
|
free(od->unsupported[OSS_BITS]);
|
2005-03-10 03:47:18 +01:00
|
|
|
|
2004-10-29 05:30:23 +02:00
|
|
|
free(od);
|
|
|
|
}
|
|
|
|
|
2005-03-05 04:36:24 +01:00
|
|
|
#define OSS_STAT_NO_ERROR 0
|
|
|
|
#define OSS_STAT_NOT_CHAR_DEV -1
|
|
|
|
#define OSS_STAT_NO_PERMS -2
|
|
|
|
#define OSS_STAT_DOESN_T_EXIST -3
|
|
|
|
#define OSS_STAT_OTHER -4
|
|
|
|
|
2006-08-01 14:39:10 +02:00
|
|
|
static int oss_statDevice(const char *device, int *stErrno)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2005-03-05 04:36:24 +01:00
|
|
|
struct stat st;
|
2006-07-20 18:02:40 +02:00
|
|
|
|
|
|
|
if (0 == stat(device, &st)) {
|
|
|
|
if (!S_ISCHR(st.st_mode)) {
|
2005-03-05 04:36:24 +01:00
|
|
|
return OSS_STAT_NOT_CHAR_DEV;
|
|
|
|
}
|
2006-07-20 18:02:40 +02:00
|
|
|
} else {
|
2005-03-05 04:36:24 +01:00
|
|
|
*stErrno = errno;
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
switch (errno) {
|
2005-03-05 04:36:24 +01:00
|
|
|
case ENOENT:
|
|
|
|
case ENOTDIR:
|
|
|
|
return OSS_STAT_DOESN_T_EXIST;
|
|
|
|
case EACCES:
|
|
|
|
return OSS_STAT_NO_PERMS;
|
|
|
|
default:
|
|
|
|
return OSS_STAT_OTHER;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-08-01 14:39:10 +02:00
|
|
|
static const char *default_devices[] = { "/dev/sound/dsp", "/dev/dsp" };
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
static int oss_testDefault(void)
|
|
|
|
{
|
2006-08-01 14:39:10 +02:00
|
|
|
int fd, i;
|
2005-03-12 04:10:09 +01:00
|
|
|
|
2006-08-01 14:39:10 +02:00
|
|
|
for (i = ARRAY_SIZE(default_devices); --i >= 0; ) {
|
|
|
|
if ((fd = open(default_devices[i], O_WRONLY)) >= 0) {
|
|
|
|
xclose(fd);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
WARNING("Error opening OSS device \"%s\": %s\n",
|
|
|
|
default_devices[i], strerror(errno));
|
2005-03-12 04:10:09 +01:00
|
|
|
}
|
|
|
|
|
2006-08-01 14:39:10 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2005-03-12 04:10:09 +01:00
|
|
|
|
2008-09-07 22:41:22 +02:00
|
|
|
static int oss_open_default(mpd_unused struct audio_output *ao,
|
|
|
|
ConfigParam *param, OssData *od)
|
2006-08-01 14:39:10 +02:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int err[ARRAY_SIZE(default_devices)];
|
|
|
|
int ret[ARRAY_SIZE(default_devices)];
|
2005-03-12 04:10:09 +01:00
|
|
|
|
2006-08-01 14:39:10 +02:00
|
|
|
for (i = ARRAY_SIZE(default_devices); --i >= 0; ) {
|
|
|
|
ret[i] = oss_statDevice(default_devices[i], &err[i]);
|
|
|
|
if (ret[i] == 0) {
|
|
|
|
od->device = default_devices[i];
|
|
|
|
return 0;
|
|
|
|
}
|
2005-03-12 04:10:09 +01:00
|
|
|
}
|
|
|
|
|
2006-08-01 14:39:10 +02:00
|
|
|
if (param)
|
2007-05-26 20:15:54 +02:00
|
|
|
ERROR("error trying to open specified OSS device"
|
2006-08-01 14:39:10 +02:00
|
|
|
" at line %i\n", param->line);
|
|
|
|
else
|
|
|
|
ERROR("error trying to open default OSS device\n");
|
|
|
|
|
2006-08-26 08:25:52 +02:00
|
|
|
for (i = ARRAY_SIZE(default_devices); --i >= 0; ) {
|
2006-08-01 14:39:10 +02:00
|
|
|
const char *dev = default_devices[i];
|
|
|
|
switch(ret[i]) {
|
|
|
|
case OSS_STAT_DOESN_T_EXIST:
|
|
|
|
ERROR("%s not found\n", dev);
|
|
|
|
break;
|
|
|
|
case OSS_STAT_NOT_CHAR_DEV:
|
|
|
|
ERROR("%s is not a character device\n", dev);
|
|
|
|
break;
|
|
|
|
case OSS_STAT_NO_PERMS:
|
|
|
|
ERROR("%s: permission denied\n", dev);
|
|
|
|
break;
|
|
|
|
default:
|
2007-08-27 23:03:22 +02:00
|
|
|
ERROR("Error accessing %s: %s\n", dev, strerror(err[i]));
|
2006-08-01 14:39:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
return 0; /* some compilers can be dumb... */
|
2005-03-12 04:10:09 +01:00
|
|
|
}
|
|
|
|
|
2008-09-07 22:41:22 +02:00
|
|
|
static int oss_initDriver(struct audio_output *audioOutput,
|
|
|
|
ConfigParam * param)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2006-08-01 14:39:10 +02:00
|
|
|
OssData *od = newOssData();
|
2004-10-29 06:11:10 +02:00
|
|
|
audioOutput->data = od;
|
2006-08-01 14:39:10 +02:00
|
|
|
if (param) {
|
|
|
|
BlockParam *bp = getBlockParam(param, "device");
|
|
|
|
if (bp) {
|
|
|
|
od->device = bp->value;
|
|
|
|
return 0;
|
2004-10-29 06:11:10 +02:00
|
|
|
}
|
2006-08-01 14:39:10 +02:00
|
|
|
}
|
|
|
|
return oss_open_default(audioOutput, param, od);
|
2004-10-29 05:30:23 +02:00
|
|
|
}
|
|
|
|
|
2008-09-07 22:41:22 +02:00
|
|
|
static void oss_finishDriver(struct audio_output *audioOutput)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
|
|
|
OssData *od = audioOutput->data;
|
2004-10-29 05:30:23 +02:00
|
|
|
|
|
|
|
freeOssData(od);
|
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
static int setParam(OssData * od, int param, int *value)
|
|
|
|
{
|
2005-03-10 03:47:18 +01:00
|
|
|
int val = *value;
|
|
|
|
int copy;
|
|
|
|
int supported = isSupportedParam(od, param, val);
|
|
|
|
|
|
|
|
do {
|
2006-07-20 18:02:40 +02:00
|
|
|
if (supported == OSS_UNSUPPORTED) {
|
2005-03-10 03:47:18 +01:00
|
|
|
val = getSupportedParam(od, param, val);
|
2006-07-20 18:02:40 +02:00
|
|
|
if (copy < 0)
|
|
|
|
return -1;
|
2005-03-10 03:47:18 +01:00
|
|
|
}
|
|
|
|
copy = val;
|
2006-07-20 18:02:40 +02:00
|
|
|
if (ioctl(od->fd, param, ©)) {
|
2005-03-10 03:47:18 +01:00
|
|
|
unsupportParam(od, param, val);
|
|
|
|
supported = OSS_UNSUPPORTED;
|
2006-07-20 18:02:40 +02:00
|
|
|
} else {
|
|
|
|
if (supported == OSS_UNKNOWN) {
|
2005-03-10 03:47:18 +01:00
|
|
|
supportParam(od, param, val);
|
|
|
|
supported = OSS_SUPPORTED;
|
|
|
|
}
|
|
|
|
val = copy;
|
|
|
|
}
|
2006-07-20 18:02:40 +02:00
|
|
|
} while (supported == OSS_UNSUPPORTED);
|
2005-03-10 03:47:18 +01:00
|
|
|
|
|
|
|
*value = val;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-07-16 18:53:04 +02:00
|
|
|
static void oss_close(OssData * od)
|
|
|
|
{
|
2006-07-20 18:02:40 +02:00
|
|
|
if (od->fd >= 0)
|
|
|
|
while (close(od->fd) && errno == EINTR) ;
|
2006-07-16 18:53:04 +02:00
|
|
|
od->fd = -1;
|
|
|
|
}
|
|
|
|
|
2008-09-07 22:41:22 +02:00
|
|
|
static int oss_open(struct audio_output *audioOutput)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2006-03-25 06:33:14 +01:00
|
|
|
int tmp;
|
2006-07-20 18:02:40 +02:00
|
|
|
OssData *od = audioOutput->data;
|
2005-03-05 04:36:24 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if ((od->fd = open(od->device, O_WRONLY)) < 0) {
|
|
|
|
ERROR("Error opening OSS device \"%s\": %s\n", od->device,
|
|
|
|
strerror(errno));
|
2005-03-05 15:01:13 +01:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (setParam(od, SNDCTL_DSP_CHANNELS, &od->channels)) {
|
|
|
|
ERROR("OSS device \"%s\" does not support %i channels: %s\n",
|
|
|
|
od->device, od->channels, strerror(errno));
|
2005-03-05 15:01:13 +01:00
|
|
|
goto fail;
|
|
|
|
}
|
2005-03-05 04:36:24 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (setParam(od, SNDCTL_DSP_SPEED, &od->sampleRate)) {
|
|
|
|
ERROR("OSS device \"%s\" does not support %i Hz audio: %s\n",
|
|
|
|
od->device, od->sampleRate, strerror(errno));
|
2005-03-05 15:01:13 +01:00
|
|
|
goto fail;
|
|
|
|
}
|
2005-03-05 04:36:24 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
switch (od->bits) {
|
2006-03-25 06:33:14 +01:00
|
|
|
case 8:
|
|
|
|
tmp = AFMT_S8;
|
|
|
|
break;
|
|
|
|
case 16:
|
|
|
|
tmp = AFMT_S16_MPD;
|
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (setParam(od, SNDCTL_DSP_SAMPLESIZE, &tmp)) {
|
|
|
|
ERROR("OSS device \"%s\" does not support %i bit audio: %s\n",
|
|
|
|
od->device, tmp, strerror(errno));
|
2005-03-05 15:01:13 +01:00
|
|
|
goto fail;
|
|
|
|
}
|
2004-10-30 06:19:11 +02:00
|
|
|
|
|
|
|
audioOutput->open = 1;
|
2004-10-29 05:30:23 +02:00
|
|
|
|
|
|
|
return 0;
|
2004-10-30 06:19:11 +02:00
|
|
|
|
2006-08-20 12:13:54 +02:00
|
|
|
fail:
|
2006-07-16 18:53:04 +02:00
|
|
|
oss_close(od);
|
2004-10-30 06:19:11 +02:00
|
|
|
audioOutput->open = 0;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2008-09-07 22:41:22 +02:00
|
|
|
static int oss_openDevice(struct audio_output *audioOutput)
|
2005-03-05 15:01:13 +01:00
|
|
|
{
|
2008-03-26 11:37:36 +01:00
|
|
|
int ret;
|
2006-07-20 18:02:40 +02:00
|
|
|
OssData *od = audioOutput->data;
|
2008-09-07 19:19:55 +02:00
|
|
|
struct audio_format *audioFormat = &audioOutput->outAudioFormat;
|
2006-03-25 06:33:14 +01:00
|
|
|
|
2008-03-26 11:37:17 +01:00
|
|
|
od->channels = (mpd_sint8)audioFormat->channels;
|
2005-03-05 15:01:13 +01:00
|
|
|
od->sampleRate = audioFormat->sampleRate;
|
2008-03-26 11:37:17 +01:00
|
|
|
od->bits = (mpd_sint8)audioFormat->bits;
|
2005-03-05 15:01:13 +01:00
|
|
|
|
2006-07-16 18:53:04 +02:00
|
|
|
if ((ret = oss_open(audioOutput)) < 0)
|
|
|
|
return ret;
|
2005-03-10 03:47:18 +01:00
|
|
|
|
|
|
|
audioFormat->channels = od->channels;
|
|
|
|
audioFormat->sampleRate = od->sampleRate;
|
|
|
|
audioFormat->bits = od->bits;
|
|
|
|
|
2005-03-19 15:30:07 +01:00
|
|
|
DEBUG("oss device \"%s\" will be playing %i bit %i channel audio at "
|
2006-07-20 18:02:40 +02:00
|
|
|
"%i Hz\n", od->device, od->bits, od->channels, od->sampleRate);
|
2005-03-10 03:47:18 +01:00
|
|
|
|
|
|
|
return ret;
|
2005-03-05 15:01:13 +01:00
|
|
|
}
|
|
|
|
|
2008-09-07 22:41:22 +02:00
|
|
|
static void oss_closeDevice(struct audio_output *audioOutput)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
|
|
|
OssData *od = audioOutput->data;
|
2004-10-30 06:19:11 +02:00
|
|
|
|
2005-04-07 00:52:42 +02:00
|
|
|
oss_close(od);
|
2004-10-30 06:19:11 +02:00
|
|
|
|
|
|
|
audioOutput->open = 0;
|
2004-10-29 05:30:23 +02:00
|
|
|
}
|
|
|
|
|
2008-09-07 22:41:22 +02:00
|
|
|
static void oss_dropBufferedAudio(struct audio_output *audioOutput)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
|
|
|
OssData *od = audioOutput->data;
|
2005-03-05 15:01:13 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (od->fd >= 0) {
|
2005-03-05 15:01:13 +01:00
|
|
|
ioctl(od->fd, SNDCTL_DSP_RESET, 0);
|
2005-04-07 00:52:42 +02:00
|
|
|
oss_close(od);
|
2005-03-05 15:01:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-09-07 22:41:22 +02:00
|
|
|
static int oss_playAudio(struct audio_output *audioOutput,
|
2008-04-12 06:15:52 +02:00
|
|
|
const char *playChunk, size_t size)
|
2004-10-29 05:30:23 +02:00
|
|
|
{
|
2006-07-20 18:02:40 +02:00
|
|
|
OssData *od = audioOutput->data;
|
2008-04-12 06:15:52 +02:00
|
|
|
ssize_t ret;
|
2004-10-29 05:30:23 +02:00
|
|
|
|
2005-04-07 00:52:42 +02:00
|
|
|
/* reopen the device since it was closed by dropBufferedAudio */
|
2006-07-20 18:02:40 +02:00
|
|
|
if (od->fd < 0 && oss_open(audioOutput) < 0)
|
2006-07-16 18:53:04 +02:00
|
|
|
return -1;
|
2005-04-07 00:52:42 +02:00
|
|
|
|
2004-10-29 05:30:23 +02:00
|
|
|
while (size > 0) {
|
2004-10-30 06:19:11 +02:00
|
|
|
ret = write(od->fd, playChunk, size);
|
2006-07-20 18:02:40 +02:00
|
|
|
if (ret < 0) {
|
|
|
|
if (errno == EINTR)
|
|
|
|
continue;
|
2005-03-05 22:09:32 +01:00
|
|
|
ERROR("closing oss device \"%s\" due to write error: "
|
2006-07-20 18:02:40 +02:00
|
|
|
"%s\n", od->device, strerror(errno));
|
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;
|
|
|
|
}
|
|
|
|
|
2008-09-08 11:43:38 +02:00
|
|
|
const struct audio_output_plugin ossPlugin = {
|
2004-10-29 05:30:23 +02:00
|
|
|
"oss",
|
2005-03-12 04:10:09 +01:00
|
|
|
oss_testDefault,
|
2004-10-29 05:30:23 +02:00
|
|
|
oss_initDriver,
|
|
|
|
oss_finishDriver,
|
|
|
|
oss_openDevice,
|
|
|
|
oss_playAudio,
|
2005-03-05 15:01:13 +01:00
|
|
|
oss_dropBufferedAudio,
|
2004-10-29 05:30:23 +02:00
|
|
|
oss_closeDevice,
|
2006-07-20 20:53:56 +02:00
|
|
|
NULL, /* sendMetadataFunc */
|
2004-10-29 05:30:23 +02:00
|
|
|
};
|
|
|
|
|
2006-07-20 20:53:56 +02:00
|
|
|
#else /* HAVE OSS */
|
2004-10-29 05:30:23 +02:00
|
|
|
|
2006-07-14 19:39:14 +02:00
|
|
|
DISABLED_AUDIO_OUTPUT_PLUGIN(ossPlugin)
|
2006-07-20 20:53:56 +02:00
|
|
|
#endif /* HAVE_OSS */
|