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"
|
2009-01-04 17:35:51 +01:00
|
|
|
#include "../mixer_api.h"
|
2004-10-29 05:30:23 +02:00
|
|
|
|
2008-11-25 17:43:28 +01:00
|
|
|
#include <glib.h>
|
2009-01-01 18:08:29 +01:00
|
|
|
|
2008-10-08 10:49:29 +02:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <fcntl.h>
|
2008-11-25 17:43:28 +01:00
|
|
|
#include <errno.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#undef G_LOG_DOMAIN
|
|
|
|
#define G_LOG_DOMAIN "oss"
|
2008-10-08 10:49:29 +02:00
|
|
|
|
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
|
|
|
|
2009-01-05 08:17:22 +01:00
|
|
|
#ifdef G_BYTE_ORDER == G_BIG_ENDIAN
|
2006-03-25 06:33:14 +01:00
|
|
|
# define AFMT_S16_MPD AFMT_S16_BE
|
|
|
|
#else
|
|
|
|
# define AFMT_S16_MPD AFMT_S16_LE
|
2009-01-05 08:17:22 +01:00
|
|
|
#endif
|
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;
|
2008-10-10 14:42:30 +02:00
|
|
|
struct audio_format audio_format;
|
2005-03-05 15:01:13 +01:00
|
|
|
int bitFormat;
|
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];
|
2008-12-31 16:46:41 +01:00
|
|
|
struct oss_mixer *mixer;
|
2004-10-29 05:30:23 +02:00
|
|
|
} OssData;
|
|
|
|
|
2008-10-14 17:21:53 +02:00
|
|
|
enum oss_support {
|
|
|
|
OSS_SUPPORTED = 1,
|
|
|
|
OSS_UNSUPPORTED = 0,
|
|
|
|
OSS_UNKNOWN = -1,
|
|
|
|
};
|
|
|
|
|
|
|
|
enum oss_param {
|
|
|
|
OSS_RATE = 0,
|
|
|
|
OSS_CHANNELS = 1,
|
|
|
|
OSS_BITS = 2,
|
|
|
|
};
|
|
|
|
|
|
|
|
static enum oss_param
|
2008-10-14 17:21:57 +02:00
|
|
|
getIndexForParam(unsigned param)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2008-10-14 17:21:53 +02:00
|
|
|
enum oss_param idx = OSS_RATE;
|
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
|
|
|
}
|
|
|
|
|
2008-10-14 17:21:57 +02:00
|
|
|
static int findSupportedParam(OssData * od, unsigned param, int val)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2005-03-10 03:47:18 +01:00
|
|
|
int i;
|
2008-10-14 17:21:53 +02:00
|
|
|
enum oss_param 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;
|
|
|
|
}
|
|
|
|
|
2008-10-14 17:21:57 +02:00
|
|
|
static int getSupportedParam(OssData * od, unsigned param, int val)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2005-03-10 03:47:18 +01:00
|
|
|
int i;
|
2008-10-14 17:21:53 +02:00
|
|
|
enum oss_param 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;
|
|
|
|
}
|
|
|
|
|
2008-10-14 17:21:57 +02:00
|
|
|
static int findUnsupportedParam(OssData * od, unsigned param, int val)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2005-03-10 03:47:18 +01:00
|
|
|
int i;
|
2008-10-14 17:21:53 +02:00
|
|
|
enum oss_param 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;
|
|
|
|
}
|
|
|
|
|
2008-10-14 17:21:57 +02:00
|
|
|
static void addSupportedParam(OssData * od, unsigned param, int val)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2008-10-14 17:21:53 +02:00
|
|
|
enum oss_param idx = getIndexForParam(param);
|
2005-03-10 03:47:18 +01:00
|
|
|
|
2008-01-26 13:46:21 +01:00
|
|
|
od->numSupported[idx]++;
|
2008-11-25 17:43:28 +01:00
|
|
|
od->supported[idx] = g_realloc(od->supported[idx],
|
|
|
|
od->numSupported[idx] * sizeof(int));
|
2008-01-26 13:46:21 +01:00
|
|
|
od->supported[idx][od->numSupported[idx] - 1] = val;
|
2005-03-10 03:47:18 +01:00
|
|
|
}
|
|
|
|
|
2008-10-14 17:21:57 +02:00
|
|
|
static void addUnsupportedParam(OssData * od, unsigned param, int val)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2008-10-14 17:21:53 +02:00
|
|
|
enum oss_param idx = getIndexForParam(param);
|
2005-03-10 03:47:18 +01:00
|
|
|
|
2008-01-26 13:46:21 +01:00
|
|
|
od->numUnsupported[idx]++;
|
2008-11-25 17:43:28 +01:00
|
|
|
od->unsupported[idx] = g_realloc(od->unsupported[idx],
|
|
|
|
od->numUnsupported[idx] *
|
|
|
|
sizeof(int));
|
2008-01-26 13:46:21 +01:00
|
|
|
od->unsupported[idx][od->numUnsupported[idx] - 1] = val;
|
2005-03-10 03:47:18 +01:00
|
|
|
}
|
|
|
|
|
2008-10-14 17:21:57 +02:00
|
|
|
static void removeSupportedParam(OssData * od, unsigned param, int val)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2008-03-26 11:37:36 +01:00
|
|
|
int i;
|
2005-03-10 03:47:18 +01:00
|
|
|
int j = 0;
|
2008-10-14 17:21:53 +02:00
|
|
|
enum oss_param 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]--;
|
2008-11-25 17:43:28 +01:00
|
|
|
od->supported[idx] = g_realloc(od->supported[idx],
|
|
|
|
od->numSupported[idx] * sizeof(int));
|
2005-03-10 03:47:18 +01:00
|
|
|
}
|
|
|
|
|
2008-10-14 17:21:57 +02:00
|
|
|
static void removeUnsupportedParam(OssData * od, unsigned param, int val)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2008-03-26 11:37:36 +01:00
|
|
|
int i;
|
2005-03-10 03:47:18 +01:00
|
|
|
int j = 0;
|
2008-10-14 17:21:53 +02:00
|
|
|
enum oss_param 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]--;
|
2008-11-25 17:43:28 +01:00
|
|
|
od->unsupported[idx] = g_realloc(od->unsupported[idx],
|
|
|
|
od->numUnsupported[idx] *
|
|
|
|
sizeof(int));
|
2005-03-10 03:47:18 +01:00
|
|
|
}
|
|
|
|
|
2008-10-14 17:21:53 +02:00
|
|
|
static enum oss_support
|
2008-10-14 17:21:57 +02:00
|
|
|
isSupportedParam(OssData * od, unsigned param, int val)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2008-10-14 17:21:57 +02:00
|
|
|
static void supportParam(OssData * od, unsigned param, int val)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2008-10-14 17:21:53 +02:00
|
|
|
enum oss_support supported = isSupportedParam(od, param, val);
|
2005-03-10 03:47:18 +01:00
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2008-10-14 17:21:57 +02:00
|
|
|
static void unsupportParam(OssData * od, unsigned param, int val)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2008-10-14 17:21:53 +02:00
|
|
|
enum oss_support supported = isSupportedParam(od, param, val);
|
2005-03-10 03:47:18 +01:00
|
|
|
|
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)
|
|
|
|
{
|
2008-11-25 17:43:28 +01:00
|
|
|
OssData *ret = g_new(OssData, 1);
|
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
|
|
|
|
2008-12-31 16:46:41 +01:00
|
|
|
ret->mixer = oss_mixer_init();
|
|
|
|
|
2004-10-29 05:30:23 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
static void freeOssData(OssData * od)
|
|
|
|
{
|
2008-11-25 17:43:28 +01:00
|
|
|
g_free(od->supported[OSS_RATE]);
|
|
|
|
g_free(od->supported[OSS_CHANNELS]);
|
|
|
|
g_free(od->supported[OSS_BITS]);
|
|
|
|
g_free(od->unsupported[OSS_RATE]);
|
|
|
|
g_free(od->unsupported[OSS_CHANNELS]);
|
|
|
|
g_free(od->unsupported[OSS_BITS]);
|
2005-03-10 03:47:18 +01:00
|
|
|
|
2008-12-31 16:46:41 +01:00
|
|
|
oss_mixer_finish(od->mixer);
|
|
|
|
|
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" };
|
|
|
|
|
2008-10-29 20:40:27 +01:00
|
|
|
static bool oss_testDefault(void)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2006-08-01 14:39:10 +02:00
|
|
|
int fd, i;
|
2005-03-12 04:10:09 +01:00
|
|
|
|
2008-11-25 17:43:28 +01:00
|
|
|
for (i = G_N_ELEMENTS(default_devices); --i >= 0; ) {
|
2006-08-01 14:39:10 +02:00
|
|
|
if ((fd = open(default_devices[i], O_WRONLY)) >= 0) {
|
2008-11-25 17:43:28 +01:00
|
|
|
close(fd);
|
2008-10-29 20:40:27 +01:00
|
|
|
return true;
|
2006-08-01 14:39:10 +02:00
|
|
|
}
|
2008-11-25 17:43:28 +01:00
|
|
|
g_warning("Error opening OSS device \"%s\": %s\n",
|
|
|
|
default_devices[i], strerror(errno));
|
2005-03-12 04:10:09 +01:00
|
|
|
}
|
|
|
|
|
2008-10-29 20:40:27 +01:00
|
|
|
return false;
|
2006-08-01 14:39:10 +02:00
|
|
|
}
|
2005-03-12 04:10:09 +01:00
|
|
|
|
2008-09-24 07:20:55 +02:00
|
|
|
static void *oss_open_default(ConfigParam *param)
|
2006-08-01 14:39:10 +02:00
|
|
|
{
|
|
|
|
int i;
|
2008-11-25 17:43:28 +01:00
|
|
|
int err[G_N_ELEMENTS(default_devices)];
|
|
|
|
int ret[G_N_ELEMENTS(default_devices)];
|
2005-03-12 04:10:09 +01:00
|
|
|
|
2008-11-25 17:43:28 +01:00
|
|
|
for (i = G_N_ELEMENTS(default_devices); --i >= 0; ) {
|
2006-08-01 14:39:10 +02:00
|
|
|
ret[i] = oss_statDevice(default_devices[i], &err[i]);
|
|
|
|
if (ret[i] == 0) {
|
2008-09-24 07:20:55 +02:00
|
|
|
OssData *od = newOssData();
|
2006-08-01 14:39:10 +02:00
|
|
|
od->device = default_devices[i];
|
2008-12-31 16:46:41 +01:00
|
|
|
oss_mixer_configure(od->mixer, param);
|
2008-10-11 12:40:48 +02:00
|
|
|
return od;
|
2006-08-01 14:39:10 +02:00
|
|
|
}
|
2005-03-12 04:10:09 +01:00
|
|
|
}
|
|
|
|
|
2006-08-01 14:39:10 +02:00
|
|
|
if (param)
|
2008-11-25 17:43:28 +01:00
|
|
|
g_warning("error trying to open specified OSS device"
|
|
|
|
" at line %i\n", param->line);
|
2006-08-01 14:39:10 +02:00
|
|
|
else
|
2008-11-25 17:43:28 +01:00
|
|
|
g_warning("error trying to open default OSS device\n");
|
2006-08-01 14:39:10 +02:00
|
|
|
|
2008-11-25 17:43:28 +01:00
|
|
|
for (i = G_N_ELEMENTS(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:
|
2008-11-25 17:43:28 +01:00
|
|
|
g_warning("%s not found\n", dev);
|
2006-08-01 14:39:10 +02:00
|
|
|
break;
|
|
|
|
case OSS_STAT_NOT_CHAR_DEV:
|
2008-11-25 17:43:28 +01:00
|
|
|
g_warning("%s is not a character device\n", dev);
|
2006-08-01 14:39:10 +02:00
|
|
|
break;
|
|
|
|
case OSS_STAT_NO_PERMS:
|
2008-11-25 17:43:28 +01:00
|
|
|
g_warning("%s: permission denied\n", dev);
|
2006-08-01 14:39:10 +02:00
|
|
|
break;
|
|
|
|
default:
|
2008-11-25 17:43:28 +01:00
|
|
|
g_warning("Error accessing %s: %s\n",
|
|
|
|
dev, strerror(err[i]));
|
2006-08-01 14:39:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
exit(EXIT_FAILURE);
|
2008-09-24 07:20:55 +02:00
|
|
|
return NULL; /* some compilers can be dumb... */
|
2005-03-12 04:10:09 +01:00
|
|
|
}
|
|
|
|
|
2009-01-01 18:08:29 +01:00
|
|
|
static void *oss_initDriver(G_GNUC_UNUSED struct audio_output *audioOutput,
|
|
|
|
G_GNUC_UNUSED const struct audio_format *audio_format,
|
2008-09-24 07:20:55 +02:00
|
|
|
ConfigParam * param)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2006-08-01 14:39:10 +02:00
|
|
|
if (param) {
|
|
|
|
BlockParam *bp = getBlockParam(param, "device");
|
|
|
|
if (bp) {
|
2008-09-24 07:20:55 +02:00
|
|
|
OssData *od = newOssData();
|
2006-08-01 14:39:10 +02:00
|
|
|
od->device = bp->value;
|
2008-12-31 16:46:41 +01:00
|
|
|
oss_mixer_configure(od->mixer, param);
|
2008-09-24 07:20:55 +02:00
|
|
|
return od;
|
2004-10-29 06:11:10 +02:00
|
|
|
}
|
2006-08-01 14:39:10 +02:00
|
|
|
}
|
2008-09-24 07:20:55 +02:00
|
|
|
return oss_open_default(param);
|
2004-10-29 05:30:23 +02:00
|
|
|
}
|
|
|
|
|
2008-09-24 07:20:55 +02:00
|
|
|
static void oss_finishDriver(void *data)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2008-09-24 07:20:55 +02:00
|
|
|
OssData *od = data;
|
2004-10-29 05:30:23 +02:00
|
|
|
|
|
|
|
freeOssData(od);
|
|
|
|
}
|
|
|
|
|
2008-10-14 17:21:57 +02:00
|
|
|
static int setParam(OssData * od, unsigned param, int *value)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2005-03-10 03:47:18 +01:00
|
|
|
int val = *value;
|
|
|
|
int copy;
|
2008-10-14 17:21:53 +02:00
|
|
|
enum oss_support supported = isSupportedParam(od, param, val);
|
2005-03-10 03:47:18 +01:00
|
|
|
|
|
|
|
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-10-29 20:40:27 +01:00
|
|
|
static bool oss_open(OssData *od)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2006-03-25 06:33:14 +01:00
|
|
|
int tmp;
|
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) {
|
2008-11-25 17:43:28 +01:00
|
|
|
g_warning("Error opening OSS device \"%s\": %s\n", od->device,
|
|
|
|
strerror(errno));
|
2005-03-05 15:01:13 +01:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2008-10-10 14:42:30 +02:00
|
|
|
tmp = od->audio_format.channels;
|
|
|
|
if (setParam(od, SNDCTL_DSP_CHANNELS, &tmp)) {
|
2008-11-25 17:43:28 +01:00
|
|
|
g_warning("OSS device \"%s\" does not support %u channels: %s\n",
|
|
|
|
od->device, od->audio_format.channels,
|
|
|
|
strerror(errno));
|
2005-03-05 15:01:13 +01:00
|
|
|
goto fail;
|
|
|
|
}
|
2008-10-10 14:42:30 +02:00
|
|
|
od->audio_format.channels = tmp;
|
2005-03-05 04:36:24 +01:00
|
|
|
|
2008-10-10 14:42:30 +02:00
|
|
|
tmp = od->audio_format.sample_rate;
|
|
|
|
if (setParam(od, SNDCTL_DSP_SPEED, &tmp)) {
|
2008-11-25 17:43:28 +01:00
|
|
|
g_warning("OSS device \"%s\" does not support %u Hz audio: %s\n",
|
|
|
|
od->device, od->audio_format.sample_rate,
|
|
|
|
strerror(errno));
|
2005-03-05 15:01:13 +01:00
|
|
|
goto fail;
|
|
|
|
}
|
2008-10-10 14:42:30 +02:00
|
|
|
od->audio_format.sample_rate = tmp;
|
2005-03-05 04:36:24 +01:00
|
|
|
|
2008-10-10 14:42:30 +02:00
|
|
|
switch (od->audio_format.bits) {
|
2006-03-25 06:33:14 +01:00
|
|
|
case 8:
|
|
|
|
tmp = AFMT_S8;
|
|
|
|
break;
|
|
|
|
case 16:
|
|
|
|
tmp = AFMT_S16_MPD;
|
2008-10-29 18:35:03 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
/* not supported by OSS - fall back to 16 bit */
|
|
|
|
od->audio_format.bits = 16;
|
|
|
|
tmp = AFMT_S16_MPD;
|
|
|
|
break;
|
2006-03-25 06:33:14 +01:00
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (setParam(od, SNDCTL_DSP_SAMPLESIZE, &tmp)) {
|
2008-11-25 17:43:28 +01:00
|
|
|
g_warning("OSS device \"%s\" does not support %u 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
|
|
|
|
2008-10-29 20:40:27 +01:00
|
|
|
return true;
|
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);
|
2008-10-29 20:40:27 +01:00
|
|
|
return false;
|
2004-10-30 06:19:11 +02:00
|
|
|
}
|
|
|
|
|
2008-10-29 20:40:27 +01:00
|
|
|
static bool
|
|
|
|
oss_openDevice(void *data, struct audio_format *audioFormat)
|
2005-03-05 15:01:13 +01:00
|
|
|
{
|
2008-10-29 20:40:27 +01:00
|
|
|
bool ret;
|
2008-09-24 07:20:55 +02:00
|
|
|
OssData *od = data;
|
2006-03-25 06:33:14 +01:00
|
|
|
|
2008-10-10 14:42:30 +02:00
|
|
|
od->audio_format = *audioFormat;
|
2005-03-05 15:01:13 +01:00
|
|
|
|
2008-10-29 20:40:27 +01:00
|
|
|
ret = oss_open(od);
|
|
|
|
if (!ret)
|
|
|
|
return false;
|
2005-03-10 03:47:18 +01:00
|
|
|
|
2008-10-10 14:42:30 +02:00
|
|
|
*audioFormat = od->audio_format;
|
2005-03-10 03:47:18 +01:00
|
|
|
|
2008-11-25 17:43:28 +01:00
|
|
|
g_debug("device \"%s\" will be playing %u bit %u channel audio at "
|
|
|
|
"%u Hz\n", od->device,
|
|
|
|
od->audio_format.bits, od->audio_format.channels,
|
|
|
|
od->audio_format.sample_rate);
|
2005-03-10 03:47:18 +01:00
|
|
|
|
2008-12-31 16:46:41 +01:00
|
|
|
oss_mixer_open(od->mixer);
|
|
|
|
|
2005-03-10 03:47:18 +01:00
|
|
|
return ret;
|
2005-03-05 15:01:13 +01:00
|
|
|
}
|
|
|
|
|
2008-09-24 07:20:55 +02:00
|
|
|
static void oss_closeDevice(void *data)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2008-09-24 07:20:55 +02:00
|
|
|
OssData *od = data;
|
2004-10-30 06:19:11 +02:00
|
|
|
|
2005-04-07 00:52:42 +02:00
|
|
|
oss_close(od);
|
2008-12-31 16:46:41 +01:00
|
|
|
oss_mixer_close(od->mixer);
|
2004-10-29 05:30:23 +02:00
|
|
|
}
|
|
|
|
|
2008-09-24 07:20:55 +02:00
|
|
|
static void oss_dropBufferedAudio(void *data)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2008-09-24 07:20:55 +02:00
|
|
|
OssData *od = 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-10-29 20:40:27 +01:00
|
|
|
static bool
|
|
|
|
oss_playAudio(void *data, const char *playChunk, size_t size)
|
2004-10-29 05:30:23 +02:00
|
|
|
{
|
2008-09-24 07:20:55 +02:00
|
|
|
OssData *od = 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 */
|
2008-09-24 07:20:55 +02:00
|
|
|
if (od->fd < 0 && oss_open(od) < 0)
|
2008-10-29 20:40:27 +01:00
|
|
|
return false;
|
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;
|
2008-11-25 17:43:28 +01:00
|
|
|
g_warning("closing oss device \"%s\" due to write error: "
|
|
|
|
"%s\n", od->device, strerror(errno));
|
2008-10-29 20:40:27 +01:00
|
|
|
return false;
|
2004-10-29 05:30:23 +02:00
|
|
|
}
|
2004-10-30 06:19:11 +02:00
|
|
|
playChunk += ret;
|
|
|
|
size -= ret;
|
2004-10-29 05:30:23 +02:00
|
|
|
}
|
|
|
|
|
2008-10-29 20:40:27 +01:00
|
|
|
return true;
|
2004-10-29 05:30:23 +02:00
|
|
|
}
|
|
|
|
|
2008-12-31 16:46:41 +01:00
|
|
|
static bool
|
|
|
|
oss_control(void *data, int cmd, void *arg)
|
|
|
|
{
|
|
|
|
OssData *od = data;
|
|
|
|
return oss_mixer_control(od->mixer, cmd, arg);
|
|
|
|
}
|
|
|
|
|
2008-09-08 11:43:38 +02:00
|
|
|
const struct audio_output_plugin ossPlugin = {
|
2008-09-29 15:55:17 +02:00
|
|
|
.name = "oss",
|
|
|
|
.test_default_device = oss_testDefault,
|
|
|
|
.init = oss_initDriver,
|
|
|
|
.finish = oss_finishDriver,
|
|
|
|
.open = oss_openDevice,
|
|
|
|
.play = oss_playAudio,
|
|
|
|
.cancel = oss_dropBufferedAudio,
|
|
|
|
.close = oss_closeDevice,
|
2008-12-31 16:46:41 +01:00
|
|
|
.control = oss_control,
|
2004-10-29 05:30:23 +02:00
|
|
|
};
|