2009-03-13 18:43:16 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2003-2009 The Music Player Daemon Project
|
|
|
|
* http://www.musicpd.org
|
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.
|
2009-03-13 18:43:16 +01:00
|
|
|
*
|
|
|
|
* 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.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2004-10-29 05:30:23 +02:00
|
|
|
*/
|
|
|
|
|
2009-11-12 09:12:38 +01:00
|
|
|
#include "config.h"
|
2009-11-07 18:55:16 +01:00
|
|
|
#include "output_api.h"
|
2009-03-14 11:36:59 +01:00
|
|
|
#include "mixer_list.h"
|
2009-11-07 18:55:16 +01:00
|
|
|
#include "fd_util.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 12:40:57 +01:00
|
|
|
#if 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
|
|
|
|
2009-02-26 19:16:33 +01:00
|
|
|
struct oss_data {
|
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];
|
2009-02-26 19:17:09 +01:00
|
|
|
unsigned num_supported[3];
|
2006-07-20 18:02:40 +02:00
|
|
|
int *unsupported[3];
|
2009-02-26 19:17:09 +01:00
|
|
|
unsigned num_unsupported[3];
|
2009-02-26 19:16:33 +01:00
|
|
|
};
|
2004-10-29 05:30:23 +02:00
|
|
|
|
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,
|
|
|
|
};
|
|
|
|
|
2009-02-26 22:04:59 +01:00
|
|
|
/**
|
|
|
|
* The quark used for GError.domain.
|
|
|
|
*/
|
|
|
|
static inline GQuark
|
|
|
|
oss_output_quark(void)
|
|
|
|
{
|
|
|
|
return g_quark_from_static_string("oss_output");
|
|
|
|
}
|
|
|
|
|
2008-10-14 17:21:53 +02:00
|
|
|
static enum oss_param
|
2009-02-26 19:16:33 +01:00
|
|
|
oss_param_from_ioctl(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
|
|
|
}
|
|
|
|
|
2009-02-26 19:17:56 +01:00
|
|
|
static bool
|
2009-02-26 19:16:33 +01:00
|
|
|
oss_find_supported_param(struct oss_data *od, unsigned param, int val)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2009-02-26 19:16:33 +01:00
|
|
|
enum oss_param idx = oss_param_from_ioctl(param);
|
2006-07-20 18:02:40 +02:00
|
|
|
|
2009-02-26 19:17:09 +01:00
|
|
|
for (unsigned i = 0; i < od->num_supported[idx]; i++)
|
2008-01-26 13:46:21 +01:00
|
|
|
if (od->supported[idx][i] == val)
|
2009-02-26 19:17:56 +01:00
|
|
|
return true;
|
2005-03-10 03:47:18 +01:00
|
|
|
|
2009-02-26 19:17:56 +01:00
|
|
|
return false;
|
2005-03-10 03:47:18 +01:00
|
|
|
}
|
|
|
|
|
2009-02-26 19:17:56 +01:00
|
|
|
static bool
|
2009-02-26 19:16:33 +01:00
|
|
|
oss_can_convert(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)
|
2009-02-26 19:17:56 +01:00
|
|
|
return false;
|
2005-03-10 03:47:18 +01:00
|
|
|
break;
|
|
|
|
case OSS_CHANNELS:
|
2006-07-20 18:02:40 +02:00
|
|
|
if (val != 2)
|
2009-02-26 19:17:56 +01:00
|
|
|
return false;
|
2005-03-10 03:47:18 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2009-02-26 19:17:56 +01:00
|
|
|
return true;
|
2005-03-10 03:47:18 +01:00
|
|
|
}
|
|
|
|
|
2009-02-26 19:16:33 +01:00
|
|
|
static int
|
|
|
|
oss_get_supported_param(struct oss_data *od, unsigned param, int val)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2009-02-26 19:16:33 +01:00
|
|
|
enum oss_param idx = oss_param_from_ioctl(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
|
|
|
|
2009-02-26 19:17:09 +01:00
|
|
|
for (unsigned i = 0; i < od->num_supported[idx]; i++) {
|
2008-01-26 13:46:21 +01:00
|
|
|
diff = od->supported[idx][i] - val;
|
2006-07-20 18:02:40 +02:00
|
|
|
if (diff < 0)
|
|
|
|
diff = -diff;
|
|
|
|
if (diff < least) {
|
2009-02-26 19:16:33 +01:00
|
|
|
if (!oss_can_convert(idx, od->supported[idx][i]))
|
2005-03-10 03:47:18 +01:00
|
|
|
continue;
|
2009-02-26 19:16:33 +01:00
|
|
|
|
2005-03-10 03:47:18 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2009-02-26 19:17:56 +01:00
|
|
|
static bool
|
2009-02-26 19:16:33 +01:00
|
|
|
oss_find_unsupported_param(struct oss_data *od, unsigned param, int val)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2009-02-26 19:16:33 +01:00
|
|
|
enum oss_param idx = oss_param_from_ioctl(param);
|
2006-07-20 18:02:40 +02:00
|
|
|
|
2009-02-26 19:17:09 +01:00
|
|
|
for (unsigned i = 0; i < od->num_unsupported[idx]; i++) {
|
2008-01-26 13:46:21 +01:00
|
|
|
if (od->unsupported[idx][i] == val)
|
2009-02-26 19:17:56 +01:00
|
|
|
return true;
|
2005-03-10 03:47:18 +01:00
|
|
|
}
|
|
|
|
|
2009-02-26 19:17:56 +01:00
|
|
|
return false;
|
2005-03-10 03:47:18 +01:00
|
|
|
}
|
|
|
|
|
2009-02-26 19:16:33 +01:00
|
|
|
static void
|
|
|
|
oss_add_supported_param(struct oss_data *od, unsigned param, int val)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2009-02-26 19:16:33 +01:00
|
|
|
enum oss_param idx = oss_param_from_ioctl(param);
|
2005-03-10 03:47:18 +01:00
|
|
|
|
2009-02-26 19:16:33 +01:00
|
|
|
od->num_supported[idx]++;
|
2008-11-25 17:43:28 +01:00
|
|
|
od->supported[idx] = g_realloc(od->supported[idx],
|
2009-02-26 19:16:33 +01:00
|
|
|
od->num_supported[idx] * sizeof(int));
|
|
|
|
od->supported[idx][od->num_supported[idx] - 1] = val;
|
2005-03-10 03:47:18 +01:00
|
|
|
}
|
|
|
|
|
2009-02-26 19:16:33 +01:00
|
|
|
static void
|
|
|
|
oss_add_unsupported_param(struct oss_data *od, unsigned param, int val)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2009-02-26 19:16:33 +01:00
|
|
|
enum oss_param idx = oss_param_from_ioctl(param);
|
2005-03-10 03:47:18 +01:00
|
|
|
|
2009-02-26 19:16:33 +01:00
|
|
|
od->num_unsupported[idx]++;
|
2008-11-25 17:43:28 +01:00
|
|
|
od->unsupported[idx] = g_realloc(od->unsupported[idx],
|
2009-02-26 19:16:33 +01:00
|
|
|
od->num_unsupported[idx] *
|
2008-11-25 17:43:28 +01:00
|
|
|
sizeof(int));
|
2009-02-26 19:16:33 +01:00
|
|
|
od->unsupported[idx][od->num_unsupported[idx] - 1] = val;
|
2005-03-10 03:47:18 +01:00
|
|
|
}
|
|
|
|
|
2009-02-26 19:16:33 +01:00
|
|
|
static void
|
|
|
|
oss_remove_supported_param(struct oss_data *od, unsigned param, int val)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2009-02-26 19:17:09 +01:00
|
|
|
unsigned j = 0;
|
2009-02-26 19:16:33 +01:00
|
|
|
enum oss_param idx = oss_param_from_ioctl(param);
|
2005-03-10 03:47:18 +01:00
|
|
|
|
2009-02-26 19:17:09 +01:00
|
|
|
for (unsigned i = 0; i < od->num_supported[idx] - 1; i++) {
|
2008-01-26 13:46:21 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2009-02-26 19:16:33 +01:00
|
|
|
od->num_supported[idx]--;
|
2008-11-25 17:43:28 +01:00
|
|
|
od->supported[idx] = g_realloc(od->supported[idx],
|
2009-02-26 19:16:33 +01:00
|
|
|
od->num_supported[idx] * sizeof(int));
|
2005-03-10 03:47:18 +01:00
|
|
|
}
|
|
|
|
|
2009-02-26 19:16:33 +01:00
|
|
|
static void
|
|
|
|
oss_remove_unsupported_param(struct oss_data *od, unsigned param, int val)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2009-02-26 19:17:09 +01:00
|
|
|
unsigned j = 0;
|
2009-02-26 19:16:33 +01:00
|
|
|
enum oss_param idx = oss_param_from_ioctl(param);
|
2005-03-10 03:47:18 +01:00
|
|
|
|
2009-02-26 19:17:09 +01:00
|
|
|
for (unsigned i = 0; i < od->num_unsupported[idx] - 1; i++) {
|
2008-01-26 13:46:21 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2009-02-26 19:16:33 +01:00
|
|
|
od->num_unsupported[idx]--;
|
2008-11-25 17:43:28 +01:00
|
|
|
od->unsupported[idx] = g_realloc(od->unsupported[idx],
|
2009-02-26 19:16:33 +01:00
|
|
|
od->num_unsupported[idx] *
|
2008-11-25 17:43:28 +01:00
|
|
|
sizeof(int));
|
2005-03-10 03:47:18 +01:00
|
|
|
}
|
|
|
|
|
2008-10-14 17:21:53 +02:00
|
|
|
static enum oss_support
|
2009-02-26 19:16:33 +01:00
|
|
|
oss_param_is_supported(struct oss_data *od, unsigned param, int val)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2009-02-26 19:16:33 +01:00
|
|
|
if (oss_find_supported_param(od, param, val))
|
2006-07-20 18:02:40 +02:00
|
|
|
return OSS_SUPPORTED;
|
2009-02-26 19:16:33 +01:00
|
|
|
if (oss_find_unsupported_param(od, param, val))
|
2006-07-20 18:02:40 +02:00
|
|
|
return OSS_UNSUPPORTED;
|
2005-03-10 03:47:18 +01:00
|
|
|
return OSS_UNKNOWN;
|
|
|
|
}
|
|
|
|
|
2009-02-26 19:16:33 +01:00
|
|
|
static void
|
|
|
|
oss_set_supported(struct oss_data *od, unsigned param, int val)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2009-02-26 19:16:33 +01:00
|
|
|
enum oss_support supported = oss_param_is_supported(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
|
|
|
|
2009-02-26 19:16:33 +01:00
|
|
|
if (supported == OSS_UNSUPPORTED)
|
|
|
|
oss_remove_unsupported_param(od, param, val);
|
2005-03-10 03:47:18 +01:00
|
|
|
|
2009-02-26 19:16:33 +01:00
|
|
|
oss_add_supported_param(od, param, val);
|
2005-03-10 03:47:18 +01:00
|
|
|
}
|
|
|
|
|
2009-02-26 19:16:33 +01:00
|
|
|
static void
|
|
|
|
oss_set_unsupported(struct oss_data *od, unsigned param, int val)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2009-02-26 19:16:33 +01:00
|
|
|
enum oss_support supported = oss_param_is_supported(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
|
|
|
|
2009-02-26 19:16:33 +01:00
|
|
|
if (supported == OSS_SUPPORTED)
|
|
|
|
oss_remove_supported_param(od, param, val);
|
2005-03-10 03:47:18 +01:00
|
|
|
|
2009-02-26 19:16:33 +01:00
|
|
|
oss_add_unsupported_param(od, param, val);
|
2005-03-10 03:47:18 +01:00
|
|
|
}
|
|
|
|
|
2009-02-26 19:16:33 +01:00
|
|
|
static struct oss_data *
|
|
|
|
oss_data_new(void)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2009-02-26 19:16:33 +01:00
|
|
|
struct oss_data *ret = g_new(struct oss_data, 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;
|
|
|
|
|
2009-02-26 19:16:33 +01:00
|
|
|
ret->num_supported[OSS_RATE] = 0;
|
|
|
|
ret->num_supported[OSS_CHANNELS] = 0;
|
|
|
|
ret->num_supported[OSS_BITS] = 0;
|
|
|
|
ret->num_unsupported[OSS_RATE] = 0;
|
|
|
|
ret->num_unsupported[OSS_CHANNELS] = 0;
|
|
|
|
ret->num_unsupported[OSS_BITS] = 0;
|
2005-03-10 03:47:18 +01:00
|
|
|
|
2009-02-26 19:16:33 +01:00
|
|
|
oss_set_supported(ret, SNDCTL_DSP_SPEED, 48000);
|
|
|
|
oss_set_supported(ret, SNDCTL_DSP_SPEED, 44100);
|
|
|
|
oss_set_supported(ret, SNDCTL_DSP_CHANNELS, 2);
|
|
|
|
oss_set_supported(ret, SNDCTL_DSP_SAMPLESIZE, 16);
|
2005-03-10 03:47:18 +01:00
|
|
|
|
2004-10-29 05:30:23 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2009-02-26 19:16:33 +01:00
|
|
|
static void
|
|
|
|
oss_data_free(struct oss_data *od)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
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
|
|
|
|
2009-01-25 18:47:21 +01:00
|
|
|
g_free(od);
|
2004-10-29 05:30:23 +02:00
|
|
|
}
|
|
|
|
|
2009-02-26 19:18:13 +01:00
|
|
|
enum oss_stat {
|
|
|
|
OSS_STAT_NO_ERROR = 0,
|
|
|
|
OSS_STAT_NOT_CHAR_DEV = -1,
|
|
|
|
OSS_STAT_NO_PERMS = -2,
|
|
|
|
OSS_STAT_DOESN_T_EXIST = -3,
|
|
|
|
OSS_STAT_OTHER = -4,
|
|
|
|
};
|
2005-03-05 04:36:24 +01:00
|
|
|
|
2009-02-26 19:18:13 +01:00
|
|
|
static enum oss_stat
|
2009-02-26 19:16:33 +01:00
|
|
|
oss_stat_device(const char *device, int *errno_r)
|
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 {
|
2009-02-26 19:16:33 +01:00
|
|
|
*errno_r = errno;
|
2005-03-05 04:36:24 +01:00
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-02-26 19:18:13 +01:00
|
|
|
return OSS_STAT_NO_ERROR;
|
2005-03-05 04:36:24 +01:00
|
|
|
}
|
|
|
|
|
2006-08-01 14:39:10 +02:00
|
|
|
static const char *default_devices[] = { "/dev/sound/dsp", "/dev/dsp" };
|
|
|
|
|
2009-02-26 19:16:33 +01:00
|
|
|
static bool
|
|
|
|
oss_output_test_default_device(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; ) {
|
2009-11-10 16:53:24 +01:00
|
|
|
fd = open_cloexec(default_devices[i], O_WRONLY, 0);
|
2009-11-07 18:55:16 +01:00
|
|
|
|
|
|
|
if (fd >= 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
|
|
|
|
2009-02-26 19:16:33 +01:00
|
|
|
static void *
|
2009-02-26 22:04:59 +01:00
|
|
|
oss_open_default(GError **error)
|
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)];
|
2009-02-26 19:18:13 +01:00
|
|
|
enum oss_stat 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; ) {
|
2009-02-26 19:16:33 +01:00
|
|
|
ret[i] = oss_stat_device(default_devices[i], &err[i]);
|
2009-02-26 19:18:13 +01:00
|
|
|
if (ret[i] == OSS_STAT_NO_ERROR) {
|
2009-02-26 19:16:33 +01:00
|
|
|
struct oss_data *od = oss_data_new();
|
2006-08-01 14:39:10 +02:00
|
|
|
od->device = default_devices[i];
|
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
|
|
|
}
|
|
|
|
|
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]) {
|
2009-02-26 19:18:13 +01:00
|
|
|
case OSS_STAT_NO_ERROR:
|
|
|
|
/* never reached */
|
|
|
|
break;
|
2006-08-01 14:39:10 +02:00
|
|
|
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;
|
2009-02-26 19:18:13 +01:00
|
|
|
case OSS_STAT_OTHER:
|
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
|
|
|
}
|
|
|
|
}
|
2009-02-26 22:04:59 +01:00
|
|
|
|
|
|
|
g_set_error(error, oss_output_quark(), 0,
|
|
|
|
"error trying to open default OSS device");
|
|
|
|
return NULL;
|
2005-03-12 04:10:09 +01:00
|
|
|
}
|
|
|
|
|
2009-01-17 20:23:27 +01:00
|
|
|
static void *
|
2009-02-26 19:16:33 +01:00
|
|
|
oss_output_init(G_GNUC_UNUSED const struct audio_format *audio_format,
|
2009-02-26 22:04:59 +01:00
|
|
|
const struct config_param *param,
|
|
|
|
GError **error)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2009-01-25 16:04:03 +01:00
|
|
|
const char *device = config_get_block_string(param, "device", NULL);
|
|
|
|
if (device != NULL) {
|
2009-02-26 19:16:33 +01:00
|
|
|
struct oss_data *od = oss_data_new();
|
2009-01-25 16:04:03 +01:00
|
|
|
od->device = device;
|
|
|
|
return od;
|
2006-08-01 14:39:10 +02:00
|
|
|
}
|
2009-01-25 16:04:03 +01:00
|
|
|
|
2009-02-26 22:04:59 +01:00
|
|
|
return oss_open_default(error);
|
2004-10-29 05:30:23 +02:00
|
|
|
}
|
|
|
|
|
2009-02-26 19:16:33 +01:00
|
|
|
static void
|
|
|
|
oss_output_finish(void *data)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2009-02-26 19:16:33 +01:00
|
|
|
struct oss_data *od = data;
|
2004-10-29 05:30:23 +02:00
|
|
|
|
2009-02-26 19:16:33 +01:00
|
|
|
oss_data_free(od);
|
2004-10-29 05:30:23 +02:00
|
|
|
}
|
|
|
|
|
2009-02-26 19:16:33 +01:00
|
|
|
static int
|
|
|
|
oss_set_param(struct oss_data *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;
|
2009-02-26 19:16:33 +01:00
|
|
|
enum oss_support supported = oss_param_is_supported(od, param, val);
|
2005-03-10 03:47:18 +01:00
|
|
|
|
|
|
|
do {
|
2006-07-20 18:02:40 +02:00
|
|
|
if (supported == OSS_UNSUPPORTED) {
|
2009-02-26 19:16:33 +01:00
|
|
|
val = oss_get_supported_param(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, ©)) {
|
2009-02-26 19:16:33 +01:00
|
|
|
oss_set_unsupported(od, param, val);
|
2005-03-10 03:47:18 +01:00
|
|
|
supported = OSS_UNSUPPORTED;
|
2006-07-20 18:02:40 +02:00
|
|
|
} else {
|
|
|
|
if (supported == OSS_UNKNOWN) {
|
2009-02-26 19:16:33 +01:00
|
|
|
oss_set_supported(od, param, val);
|
2005-03-10 03:47:18 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2009-02-26 19:16:33 +01:00
|
|
|
static void
|
|
|
|
oss_close(struct oss_data *od)
|
2006-07-16 18:53:04 +02:00
|
|
|
{
|
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;
|
|
|
|
}
|
|
|
|
|
2009-02-26 19:18:16 +01:00
|
|
|
/**
|
|
|
|
* Sets up the OSS device which was opened before.
|
|
|
|
*/
|
2009-02-26 19:16:33 +01:00
|
|
|
static bool
|
2009-02-26 22:04:59 +01:00
|
|
|
oss_setup(struct oss_data *od, GError **error)
|
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
|
|
|
|
2008-10-10 14:42:30 +02:00
|
|
|
tmp = od->audio_format.channels;
|
2009-02-26 19:16:33 +01:00
|
|
|
if (oss_set_param(od, SNDCTL_DSP_CHANNELS, &tmp)) {
|
2009-02-26 22:04:59 +01:00
|
|
|
g_set_error(error, oss_output_quark(), errno,
|
|
|
|
"OSS device \"%s\" does not support %u channels: %s",
|
|
|
|
od->device, od->audio_format.channels,
|
|
|
|
strerror(errno));
|
2009-02-26 19:18:16 +01:00
|
|
|
return false;
|
2005-03-05 15:01:13 +01:00
|
|
|
}
|
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;
|
2009-02-26 19:16:33 +01:00
|
|
|
if (oss_set_param(od, SNDCTL_DSP_SPEED, &tmp)) {
|
2009-02-26 22:04:59 +01:00
|
|
|
g_set_error(error, oss_output_quark(), errno,
|
|
|
|
"OSS device \"%s\" does not support %u Hz audio: %s",
|
|
|
|
od->device, od->audio_format.sample_rate,
|
|
|
|
strerror(errno));
|
2009-02-26 19:18:16 +01:00
|
|
|
return false;
|
2005-03-05 15:01:13 +01:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2009-02-26 19:16:33 +01:00
|
|
|
if (oss_set_param(od, SNDCTL_DSP_SAMPLESIZE, &tmp)) {
|
2009-02-26 22:04:59 +01:00
|
|
|
g_set_error(error, oss_output_quark(), errno,
|
|
|
|
"OSS device \"%s\" does not support %u bit audio: %s",
|
|
|
|
od->device, tmp, strerror(errno));
|
2009-02-26 19:18:16 +01:00
|
|
|
return false;
|
2005-03-05 15:01:13 +01:00
|
|
|
}
|
2004-10-30 06:19:11 +02:00
|
|
|
|
2008-10-29 20:40:27 +01:00
|
|
|
return true;
|
2009-02-26 19:18:16 +01:00
|
|
|
}
|
2004-10-30 06:19:11 +02:00
|
|
|
|
2009-02-26 19:18:16 +01:00
|
|
|
static bool
|
2009-02-26 22:04:59 +01:00
|
|
|
oss_open(struct oss_data *od, GError **error)
|
2009-02-26 19:18:16 +01:00
|
|
|
{
|
|
|
|
bool success;
|
|
|
|
|
2009-11-10 16:53:24 +01:00
|
|
|
od->fd = open_cloexec(od->device, O_WRONLY, 0);
|
2009-11-07 18:55:16 +01:00
|
|
|
if (od->fd < 0) {
|
2009-02-26 22:04:59 +01:00
|
|
|
g_set_error(error, oss_output_quark(), errno,
|
|
|
|
"Error opening OSS device \"%s\": %s",
|
|
|
|
od->device, strerror(errno));
|
2009-02-26 19:18:16 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-02-26 22:04:59 +01:00
|
|
|
success = oss_setup(od, error);
|
2009-02-26 19:18:16 +01:00
|
|
|
if (!success) {
|
|
|
|
oss_close(od);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2004-10-30 06:19:11 +02:00
|
|
|
}
|
|
|
|
|
2008-10-29 20:40:27 +01:00
|
|
|
static bool
|
2009-02-26 22:04:59 +01:00
|
|
|
oss_output_open(void *data, struct audio_format *audio_format, GError **error)
|
2005-03-05 15:01:13 +01:00
|
|
|
{
|
2008-10-29 20:40:27 +01:00
|
|
|
bool ret;
|
2009-02-26 19:16:33 +01:00
|
|
|
struct oss_data *od = data;
|
2006-03-25 06:33:14 +01:00
|
|
|
|
2009-02-26 19:16:33 +01:00
|
|
|
od->audio_format = *audio_format;
|
2005-03-05 15:01:13 +01:00
|
|
|
|
2009-02-26 22:04:59 +01:00
|
|
|
ret = oss_open(od, error);
|
2008-10-29 20:40:27 +01:00
|
|
|
if (!ret)
|
|
|
|
return false;
|
2005-03-10 03:47:18 +01:00
|
|
|
|
2009-02-26 19:16:33 +01:00
|
|
|
*audio_format = od->audio_format;
|
2005-03-10 03:47:18 +01:00
|
|
|
|
|
|
|
return ret;
|
2005-03-05 15:01:13 +01:00
|
|
|
}
|
|
|
|
|
2009-02-26 19:16:33 +01:00
|
|
|
static void
|
|
|
|
oss_output_close(void *data)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2009-02-26 19:16:33 +01:00
|
|
|
struct oss_data *od = data;
|
2004-10-30 06:19:11 +02:00
|
|
|
|
2005-04-07 00:52:42 +02:00
|
|
|
oss_close(od);
|
2004-10-29 05:30:23 +02:00
|
|
|
}
|
|
|
|
|
2009-02-26 19:16:33 +01:00
|
|
|
static void
|
|
|
|
oss_output_cancel(void *data)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2009-02-26 19:16:33 +01:00
|
|
|
struct oss_data *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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-02-23 09:29:56 +01:00
|
|
|
static size_t
|
2009-02-26 22:04:59 +01:00
|
|
|
oss_output_play(void *data, const void *chunk, size_t size, GError **error)
|
2004-10-29 05:30:23 +02:00
|
|
|
{
|
2009-02-26 19:16:33 +01:00
|
|
|
struct oss_data *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 */
|
2009-02-26 22:04:59 +01:00
|
|
|
if (od->fd < 0 && !oss_open(od, error))
|
|
|
|
return 0;
|
2005-04-07 00:52:42 +02:00
|
|
|
|
2009-02-23 09:29:56 +01:00
|
|
|
while (true) {
|
2009-02-23 09:34:26 +01:00
|
|
|
ret = write(od->fd, chunk, size);
|
2009-02-23 09:29:56 +01:00
|
|
|
if (ret > 0)
|
|
|
|
return (size_t)ret;
|
|
|
|
|
|
|
|
if (ret < 0 && errno != EINTR) {
|
2009-02-26 22:04:59 +01:00
|
|
|
g_set_error(error, oss_output_quark(), errno,
|
|
|
|
"Write error on %s: %s",
|
|
|
|
od->device, strerror(errno));
|
2009-02-23 09:29:56 +01:00
|
|
|
return 0;
|
2004-10-29 05:30:23 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-02-26 19:16:33 +01:00
|
|
|
const struct audio_output_plugin oss_output_plugin = {
|
2008-09-29 15:55:17 +02:00
|
|
|
.name = "oss",
|
2009-02-26 19:16:33 +01:00
|
|
|
.test_default_device = oss_output_test_default_device,
|
|
|
|
.init = oss_output_init,
|
|
|
|
.finish = oss_output_finish,
|
|
|
|
.open = oss_output_open,
|
|
|
|
.close = oss_output_close,
|
|
|
|
.play = oss_output_play,
|
|
|
|
.cancel = oss_output_cancel,
|
2009-10-20 21:23:05 +02:00
|
|
|
|
|
|
|
.mixer_plugin = &oss_mixer_plugin,
|
2004-10-29 05:30:23 +02:00
|
|
|
};
|