2009-03-13 18:43:16 +01:00
|
|
|
/*
|
2011-01-29 10:13:54 +01:00
|
|
|
* Copyright (C) 2003-2011 The Music Player Daemon Project
|
2009-03-13 18:43:16 +01:00
|
|
|
* 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"
|
2011-09-17 08:54:28 +02:00
|
|
|
#include "oss_output_plugin.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>
|
2010-05-13 14:42:09 +02:00
|
|
|
#include <assert.h>
|
2008-11-25 17:43:28 +01:00
|
|
|
|
|
|
|
#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
|
|
|
|
2011-02-28 00:09:45 +01:00
|
|
|
/* We got bug reports from FreeBSD users who said that the two 24 bit
|
|
|
|
formats generate white noise on FreeBSD, but 32 bit works. This is
|
|
|
|
a workaround until we know what exactly is expected by the kernel
|
|
|
|
audio drivers. */
|
|
|
|
#ifndef __linux__
|
|
|
|
#undef AFMT_S24_PACKED
|
|
|
|
#undef AFMT_S24_NE
|
|
|
|
#endif
|
|
|
|
|
2009-02-26 19:16:33 +01:00
|
|
|
struct oss_data {
|
2011-09-16 23:31:48 +02:00
|
|
|
struct audio_output base;
|
|
|
|
|
2004-10-29 06:11:10 +02:00
|
|
|
int fd;
|
2006-08-01 14:39:10 +02:00
|
|
|
const char *device;
|
2008-10-14 17:21:53 +02:00
|
|
|
|
2010-05-13 14:42:09 +02:00
|
|
|
/**
|
|
|
|
* The current input audio format. This is needed to reopen
|
|
|
|
* the device after cancel().
|
|
|
|
*/
|
|
|
|
struct audio_format audio_format;
|
2008-10-14 17:21:53 +02:00
|
|
|
};
|
|
|
|
|
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");
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
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
|
|
|
{
|
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
|
|
|
|
2011-09-16 23:31:48 +02:00
|
|
|
static struct audio_output *
|
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();
|
2011-09-16 23:31:48 +02:00
|
|
|
if (!ao_base_init(&od->base, &oss_output_plugin, NULL,
|
|
|
|
error)) {
|
|
|
|
g_free(od);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2006-08-01 14:39:10 +02:00
|
|
|
od->device = default_devices[i];
|
2011-09-16 23:31:48 +02:00
|
|
|
return &od->base;
|
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
|
|
|
}
|
|
|
|
|
2011-09-16 23:31:48 +02:00
|
|
|
static struct audio_output *
|
|
|
|
oss_output_init(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();
|
2011-09-16 23:31:48 +02:00
|
|
|
if (!ao_base_init(&od->base, &oss_output_plugin, param,
|
|
|
|
error)) {
|
|
|
|
g_free(od);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2009-01-25 16:04:03 +01:00
|
|
|
od->device = device;
|
2011-09-16 23:31:48 +02:00
|
|
|
return &od->base;
|
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
|
2011-09-16 23:31:48 +02:00
|
|
|
oss_output_finish(struct audio_output *ao)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2011-09-16 23:31:48 +02:00
|
|
|
struct oss_data *od = (struct oss_data *)ao;
|
2004-10-29 05:30:23 +02:00
|
|
|
|
2011-09-16 23:31:48 +02:00
|
|
|
ao_base_finish(&od->base);
|
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 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)
|
2010-07-25 11:09:13 +02:00
|
|
|
close(od->fd);
|
2006-07-16 18:53:04 +02:00
|
|
|
od->fd = -1;
|
|
|
|
}
|
|
|
|
|
2009-02-26 19:18:16 +01:00
|
|
|
/**
|
2010-05-13 14:42:09 +02:00
|
|
|
* A tri-state type for oss_try_ioctl().
|
2009-02-26 19:18:16 +01:00
|
|
|
*/
|
2010-05-13 14:42:09 +02:00
|
|
|
enum oss_setup_result {
|
|
|
|
SUCCESS,
|
|
|
|
ERROR,
|
|
|
|
UNSUPPORTED,
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Invoke an ioctl on the OSS file descriptor. On success, SUCCESS is
|
|
|
|
* returned. If the parameter is not supported, UNSUPPORTED is
|
|
|
|
* returned. Any other failure returns ERROR and allocates a GError.
|
|
|
|
*/
|
|
|
|
static enum oss_setup_result
|
|
|
|
oss_try_ioctl_r(int fd, unsigned long request, int *value_r,
|
|
|
|
const char *msg, GError **error_r)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2010-05-13 14:42:09 +02:00
|
|
|
assert(fd >= 0);
|
|
|
|
assert(value_r != NULL);
|
|
|
|
assert(msg != NULL);
|
|
|
|
assert(error_r == NULL || *error_r == NULL);
|
2005-03-05 04:36:24 +01:00
|
|
|
|
2010-05-13 14:42:09 +02:00
|
|
|
int ret = ioctl(fd, request, value_r);
|
|
|
|
if (ret >= 0)
|
|
|
|
return SUCCESS;
|
|
|
|
|
|
|
|
if (errno == EINVAL)
|
|
|
|
return UNSUPPORTED;
|
|
|
|
|
|
|
|
g_set_error(error_r, oss_output_quark(), errno,
|
|
|
|
"%s: %s", msg, g_strerror(errno));
|
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Invoke an ioctl on the OSS file descriptor. On success, SUCCESS is
|
|
|
|
* returned. If the parameter is not supported, UNSUPPORTED is
|
|
|
|
* returned. Any other failure returns ERROR and allocates a GError.
|
|
|
|
*/
|
|
|
|
static enum oss_setup_result
|
|
|
|
oss_try_ioctl(int fd, unsigned long request, int value,
|
|
|
|
const char *msg, GError **error_r)
|
|
|
|
{
|
|
|
|
return oss_try_ioctl_r(fd, request, &value, msg, error_r);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set up the channel number, and attempts to find alternatives if the
|
|
|
|
* specified number is not supported.
|
|
|
|
*/
|
|
|
|
static bool
|
|
|
|
oss_setup_channels(int fd, struct audio_format *audio_format, GError **error_r)
|
|
|
|
{
|
|
|
|
const char *const msg = "Failed to set channel count";
|
|
|
|
int channels = audio_format->channels;
|
|
|
|
enum oss_setup_result result =
|
|
|
|
oss_try_ioctl_r(fd, SNDCTL_DSP_CHANNELS, &channels, msg, error_r);
|
|
|
|
switch (result) {
|
|
|
|
case SUCCESS:
|
|
|
|
if (!audio_valid_channel_count(channels))
|
|
|
|
break;
|
|
|
|
|
|
|
|
audio_format->channels = channels;
|
|
|
|
return true;
|
|
|
|
|
|
|
|
case ERROR:
|
2009-02-26 19:18:16 +01:00
|
|
|
return false;
|
2010-05-13 14:42:09 +02:00
|
|
|
|
|
|
|
case UNSUPPORTED:
|
|
|
|
break;
|
2005-03-05 15:01:13 +01:00
|
|
|
}
|
2005-03-05 04:36:24 +01:00
|
|
|
|
2010-05-13 14:42:09 +02:00
|
|
|
for (unsigned i = 1; i < 2; ++i) {
|
|
|
|
if (i == audio_format->channels)
|
|
|
|
/* don't try that again */
|
|
|
|
continue;
|
|
|
|
|
|
|
|
channels = i;
|
|
|
|
result = oss_try_ioctl_r(fd, SNDCTL_DSP_CHANNELS, &channels,
|
|
|
|
msg, error_r);
|
|
|
|
switch (result) {
|
|
|
|
case SUCCESS:
|
|
|
|
if (!audio_valid_channel_count(channels))
|
|
|
|
break;
|
|
|
|
|
|
|
|
audio_format->channels = channels;
|
|
|
|
return true;
|
|
|
|
|
|
|
|
case ERROR:
|
|
|
|
return false;
|
|
|
|
|
|
|
|
case UNSUPPORTED:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
g_set_error(error_r, oss_output_quark(), EINVAL, "%s", msg);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set up the sample rate, and attempts to find alternatives if the
|
|
|
|
* specified sample rate is not supported.
|
|
|
|
*/
|
|
|
|
static bool
|
|
|
|
oss_setup_sample_rate(int fd, struct audio_format *audio_format,
|
|
|
|
GError **error_r)
|
|
|
|
{
|
|
|
|
const char *const msg = "Failed to set sample rate";
|
|
|
|
int sample_rate = audio_format->sample_rate;
|
|
|
|
enum oss_setup_result result =
|
|
|
|
oss_try_ioctl_r(fd, SNDCTL_DSP_SPEED, &sample_rate,
|
|
|
|
msg, error_r);
|
|
|
|
switch (result) {
|
|
|
|
case SUCCESS:
|
|
|
|
if (!audio_valid_sample_rate(sample_rate))
|
|
|
|
break;
|
|
|
|
|
|
|
|
audio_format->sample_rate = sample_rate;
|
|
|
|
return true;
|
|
|
|
|
|
|
|
case ERROR:
|
2009-02-26 19:18:16 +01:00
|
|
|
return false;
|
2010-05-13 14:42:09 +02:00
|
|
|
|
|
|
|
case UNSUPPORTED:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const int sample_rates[] = { 48000, 44100, 0 };
|
|
|
|
for (unsigned i = 0; sample_rates[i] != 0; ++i) {
|
|
|
|
sample_rate = sample_rates[i];
|
|
|
|
if (sample_rate == (int)audio_format->sample_rate)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
result = oss_try_ioctl_r(fd, SNDCTL_DSP_SPEED, &sample_rate,
|
|
|
|
msg, error_r);
|
|
|
|
switch (result) {
|
|
|
|
case SUCCESS:
|
|
|
|
if (!audio_valid_sample_rate(sample_rate))
|
|
|
|
break;
|
2011-02-04 10:39:21 +01:00
|
|
|
|
2010-05-13 14:42:09 +02:00
|
|
|
audio_format->sample_rate = sample_rate;
|
|
|
|
return true;
|
|
|
|
|
|
|
|
case ERROR:
|
|
|
|
return false;
|
|
|
|
|
|
|
|
case UNSUPPORTED:
|
|
|
|
break;
|
|
|
|
}
|
2005-03-05 15:01:13 +01:00
|
|
|
}
|
2005-03-05 04:36:24 +01:00
|
|
|
|
2010-05-13 14:42:09 +02:00
|
|
|
g_set_error(error_r, oss_output_quark(), EINVAL, "%s", msg);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert a MPD sample format to its OSS counterpart. Returns
|
|
|
|
* AFMT_QUERY if there is no direct counterpart.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
sample_format_to_oss(enum sample_format format)
|
|
|
|
{
|
|
|
|
switch (format) {
|
|
|
|
case SAMPLE_FORMAT_UNDEFINED:
|
2011-10-08 10:25:06 +02:00
|
|
|
case SAMPLE_FORMAT_FLOAT:
|
2012-03-01 00:25:08 +01:00
|
|
|
case SAMPLE_FORMAT_DSD:
|
|
|
|
case SAMPLE_FORMAT_DSD_LSBFIRST:
|
2010-05-13 14:42:09 +02:00
|
|
|
return AFMT_QUERY;
|
|
|
|
|
2009-11-10 17:11:34 +01:00
|
|
|
case SAMPLE_FORMAT_S8:
|
2010-05-13 14:42:09 +02:00
|
|
|
return AFMT_S8;
|
2009-11-10 17:11:34 +01:00
|
|
|
|
|
|
|
case SAMPLE_FORMAT_S16:
|
2010-05-13 14:42:09 +02:00
|
|
|
return AFMT_S16_NE;
|
|
|
|
|
|
|
|
case SAMPLE_FORMAT_S24:
|
2010-05-13 15:42:20 +02:00
|
|
|
#ifdef AFMT_S24_PACKED
|
|
|
|
return AFMT_S24_PACKED;
|
|
|
|
#else
|
|
|
|
return AFMT_QUERY;
|
|
|
|
#endif
|
|
|
|
|
2010-05-13 14:42:09 +02:00
|
|
|
case SAMPLE_FORMAT_S24_P32:
|
2010-05-13 15:42:20 +02:00
|
|
|
#ifdef AFMT_S24_NE
|
|
|
|
return AFMT_S24_NE;
|
|
|
|
#else
|
|
|
|
return AFMT_QUERY;
|
|
|
|
#endif
|
|
|
|
|
2010-05-13 14:42:09 +02:00
|
|
|
case SAMPLE_FORMAT_S32:
|
2010-05-13 15:42:20 +02:00
|
|
|
#ifdef AFMT_S32_NE
|
|
|
|
return AFMT_S32_NE;
|
|
|
|
#else
|
2010-05-13 14:42:09 +02:00
|
|
|
return AFMT_QUERY;
|
2010-05-13 15:42:20 +02:00
|
|
|
#endif
|
2010-05-13 14:42:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return AFMT_QUERY;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert an OSS sample format to its MPD counterpart. Returns
|
|
|
|
* SAMPLE_FORMAT_UNDEFINED if there is no direct counterpart.
|
|
|
|
*/
|
|
|
|
static enum sample_format
|
|
|
|
sample_format_from_oss(int format)
|
|
|
|
{
|
|
|
|
switch (format) {
|
|
|
|
case AFMT_S8:
|
|
|
|
return SAMPLE_FORMAT_S8;
|
|
|
|
|
|
|
|
case AFMT_S16_NE:
|
|
|
|
return SAMPLE_FORMAT_S16;
|
2008-10-29 18:35:03 +01:00
|
|
|
|
2010-05-13 15:42:20 +02:00
|
|
|
#ifdef AFMT_S24_PACKED
|
|
|
|
case AFMT_S24_PACKED:
|
|
|
|
return SAMPLE_FORMAT_S24;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef AFMT_S24_NE
|
|
|
|
case AFMT_S24_NE:
|
|
|
|
return SAMPLE_FORMAT_S24_P32;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef AFMT_S32_NE
|
|
|
|
case AFMT_S32_NE:
|
|
|
|
return SAMPLE_FORMAT_S32;
|
|
|
|
#endif
|
|
|
|
|
2008-10-29 18:35:03 +01:00
|
|
|
default:
|
2010-05-13 14:42:09 +02:00
|
|
|
return SAMPLE_FORMAT_UNDEFINED;
|
2006-03-25 06:33:14 +01:00
|
|
|
}
|
2010-05-13 14:42:09 +02:00
|
|
|
}
|
2006-03-25 06:33:14 +01:00
|
|
|
|
2010-05-13 14:42:09 +02:00
|
|
|
/**
|
|
|
|
* Set up the sample format, and attempts to find alternatives if the
|
|
|
|
* specified format is not supported.
|
|
|
|
*/
|
|
|
|
static bool
|
|
|
|
oss_setup_sample_format(int fd, struct audio_format *audio_format,
|
|
|
|
GError **error_r)
|
|
|
|
{
|
|
|
|
const char *const msg = "Failed to set sample format";
|
|
|
|
int oss_format = sample_format_to_oss(audio_format->format);
|
|
|
|
enum oss_setup_result result = oss_format != AFMT_QUERY
|
|
|
|
? oss_try_ioctl_r(fd, SNDCTL_DSP_SAMPLESIZE,
|
|
|
|
&oss_format, msg, error_r)
|
|
|
|
: UNSUPPORTED;
|
|
|
|
enum sample_format mpd_format;
|
|
|
|
switch (result) {
|
|
|
|
case SUCCESS:
|
|
|
|
mpd_format = sample_format_from_oss(oss_format);
|
|
|
|
if (mpd_format == SAMPLE_FORMAT_UNDEFINED)
|
|
|
|
break;
|
|
|
|
|
|
|
|
audio_format->format = mpd_format;
|
2011-02-28 00:00:41 +01:00
|
|
|
|
|
|
|
#ifdef AFMT_S24_PACKED
|
|
|
|
if (oss_format == AFMT_S24_PACKED)
|
|
|
|
audio_format->reverse_endian =
|
|
|
|
G_BYTE_ORDER != G_LITTLE_ENDIAN;
|
|
|
|
#endif
|
2010-05-13 14:42:09 +02:00
|
|
|
return true;
|
|
|
|
|
|
|
|
case ERROR:
|
2009-02-26 19:18:16 +01:00
|
|
|
return false;
|
2010-05-13 14:42:09 +02:00
|
|
|
|
|
|
|
case UNSUPPORTED:
|
|
|
|
break;
|
2005-03-05 15:01:13 +01:00
|
|
|
}
|
2004-10-30 06:19:11 +02:00
|
|
|
|
2010-05-13 14:42:09 +02:00
|
|
|
/* the requested sample format is not available - probe for
|
|
|
|
other formats supported by MPD */
|
|
|
|
|
|
|
|
static const enum sample_format sample_formats[] = {
|
2010-05-13 15:42:20 +02:00
|
|
|
SAMPLE_FORMAT_S24_P32,
|
|
|
|
SAMPLE_FORMAT_S32,
|
|
|
|
SAMPLE_FORMAT_S24,
|
2010-05-13 14:42:09 +02:00
|
|
|
SAMPLE_FORMAT_S16,
|
|
|
|
SAMPLE_FORMAT_S8,
|
|
|
|
SAMPLE_FORMAT_UNDEFINED /* sentinel */
|
|
|
|
};
|
|
|
|
|
|
|
|
for (unsigned i = 0; sample_formats[i] != SAMPLE_FORMAT_UNDEFINED; ++i) {
|
|
|
|
mpd_format = sample_formats[i];
|
|
|
|
if (mpd_format == audio_format->format)
|
|
|
|
/* don't try that again */
|
|
|
|
continue;
|
|
|
|
|
|
|
|
oss_format = sample_format_to_oss(mpd_format);
|
|
|
|
if (oss_format == AFMT_QUERY)
|
|
|
|
/* not supported by this OSS version */
|
|
|
|
continue;
|
|
|
|
|
|
|
|
result = oss_try_ioctl_r(fd, SNDCTL_DSP_SAMPLESIZE,
|
|
|
|
&oss_format, msg, error_r);
|
|
|
|
switch (result) {
|
|
|
|
case SUCCESS:
|
|
|
|
mpd_format = sample_format_from_oss(oss_format);
|
|
|
|
if (mpd_format == SAMPLE_FORMAT_UNDEFINED)
|
|
|
|
break;
|
|
|
|
|
|
|
|
audio_format->format = mpd_format;
|
2011-02-28 00:00:41 +01:00
|
|
|
|
|
|
|
#ifdef AFMT_S24_PACKED
|
|
|
|
if (oss_format == AFMT_S24_PACKED)
|
|
|
|
audio_format->reverse_endian =
|
|
|
|
G_BYTE_ORDER != G_LITTLE_ENDIAN;
|
|
|
|
#endif
|
2010-05-13 14:42:09 +02:00
|
|
|
return true;
|
|
|
|
|
|
|
|
case ERROR:
|
|
|
|
return false;
|
|
|
|
|
|
|
|
case UNSUPPORTED:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
g_set_error(error_r, oss_output_quark(), EINVAL, "%s", msg);
|
|
|
|
return false;
|
2009-02-26 19:18:16 +01:00
|
|
|
}
|
2004-10-30 06:19:11 +02:00
|
|
|
|
2010-05-13 14:42:09 +02:00
|
|
|
/**
|
|
|
|
* Sets up the OSS device which was opened before.
|
|
|
|
*/
|
2009-02-26 19:18:16 +01:00
|
|
|
static bool
|
2010-05-13 14:42:09 +02:00
|
|
|
oss_setup(struct oss_data *od, struct audio_format *audio_format,
|
|
|
|
GError **error_r)
|
2009-02-26 19:18:16 +01:00
|
|
|
{
|
2010-05-13 14:42:09 +02:00
|
|
|
return oss_setup_channels(od->fd, audio_format, error_r) &&
|
|
|
|
oss_setup_sample_rate(od->fd, audio_format, error_r) &&
|
|
|
|
oss_setup_sample_format(od->fd, audio_format, error_r);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reopen the device with the saved audio_format, without any probing.
|
|
|
|
*/
|
|
|
|
static bool
|
|
|
|
oss_reopen(struct oss_data *od, GError **error_r)
|
|
|
|
{
|
|
|
|
assert(od->fd < 0);
|
2009-02-26 19:18:16 +01:00
|
|
|
|
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) {
|
2010-05-13 14:42:09 +02:00
|
|
|
g_set_error(error_r, oss_output_quark(), errno,
|
2009-02-26 22:04:59 +01:00
|
|
|
"Error opening OSS device \"%s\": %s",
|
|
|
|
od->device, strerror(errno));
|
2009-02-26 19:18:16 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-05-13 14:42:09 +02:00
|
|
|
enum oss_setup_result result;
|
|
|
|
|
|
|
|
const char *const msg1 = "Failed to set channel count";
|
|
|
|
result = oss_try_ioctl(od->fd, SNDCTL_DSP_CHANNELS,
|
|
|
|
od->audio_format.channels, msg1, error_r);
|
|
|
|
if (result != SUCCESS) {
|
|
|
|
oss_close(od);
|
|
|
|
if (result == UNSUPPORTED)
|
|
|
|
g_set_error(error_r, oss_output_quark(), EINVAL,
|
|
|
|
"%s", msg1);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *const msg2 = "Failed to set sample rate";
|
|
|
|
result = oss_try_ioctl(od->fd, SNDCTL_DSP_SPEED,
|
|
|
|
od->audio_format.sample_rate, msg2, error_r);
|
|
|
|
if (result != SUCCESS) {
|
|
|
|
oss_close(od);
|
|
|
|
if (result == UNSUPPORTED)
|
|
|
|
g_set_error(error_r, oss_output_quark(), EINVAL,
|
|
|
|
"%s", msg2);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *const msg3 = "Failed to set sample format";
|
|
|
|
assert(sample_format_to_oss(od->audio_format.format) != AFMT_QUERY);
|
|
|
|
result = oss_try_ioctl(od->fd, SNDCTL_DSP_SAMPLESIZE,
|
|
|
|
sample_format_to_oss(od->audio_format.format),
|
|
|
|
msg3, error_r);
|
|
|
|
if (result != SUCCESS) {
|
2009-02-26 19:18:16 +01:00
|
|
|
oss_close(od);
|
2010-05-13 14:42:09 +02:00
|
|
|
if (result == UNSUPPORTED)
|
|
|
|
g_set_error(error_r, oss_output_quark(), EINVAL,
|
|
|
|
"%s", msg3);
|
2009-02-26 19:18:16 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2004-10-30 06:19:11 +02:00
|
|
|
}
|
|
|
|
|
2008-10-29 20:40:27 +01:00
|
|
|
static bool
|
2011-09-16 23:31:48 +02:00
|
|
|
oss_output_open(struct audio_output *ao, struct audio_format *audio_format,
|
|
|
|
GError **error)
|
2005-03-05 15:01:13 +01:00
|
|
|
{
|
2011-09-16 23:31:48 +02:00
|
|
|
struct oss_data *od = (struct oss_data *)ao;
|
2006-03-25 06:33:14 +01:00
|
|
|
|
2010-05-13 14:42:09 +02:00
|
|
|
od->fd = open_cloexec(od->device, O_WRONLY, 0);
|
|
|
|
if (od->fd < 0) {
|
|
|
|
g_set_error(error, oss_output_quark(), errno,
|
|
|
|
"Error opening OSS device \"%s\": %s",
|
|
|
|
od->device, strerror(errno));
|
2008-10-29 20:40:27 +01:00
|
|
|
return false;
|
2010-05-13 14:42:09 +02:00
|
|
|
}
|
2005-03-10 03:47:18 +01:00
|
|
|
|
2010-05-13 14:42:09 +02:00
|
|
|
if (!oss_setup(od, audio_format, error)) {
|
|
|
|
oss_close(od);
|
|
|
|
return false;
|
|
|
|
}
|
2005-03-10 03:47:18 +01:00
|
|
|
|
2010-05-13 14:42:09 +02:00
|
|
|
od->audio_format = *audio_format;
|
|
|
|
return true;
|
2005-03-05 15:01:13 +01:00
|
|
|
}
|
|
|
|
|
2009-02-26 19:16:33 +01:00
|
|
|
static void
|
2011-09-16 23:31:48 +02:00
|
|
|
oss_output_close(struct audio_output *ao)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2011-09-16 23:31:48 +02:00
|
|
|
struct oss_data *od = (struct oss_data *)ao;
|
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
|
2011-09-16 23:31:48 +02:00
|
|
|
oss_output_cancel(struct audio_output *ao)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2011-09-16 23:31:48 +02:00
|
|
|
struct oss_data *od = (struct oss_data *)ao;
|
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
|
2011-09-16 23:31:48 +02:00
|
|
|
oss_output_play(struct audio_output *ao, const void *chunk, size_t size,
|
|
|
|
GError **error)
|
2004-10-29 05:30:23 +02:00
|
|
|
{
|
2011-09-16 23:31:48 +02:00
|
|
|
struct oss_data *od = (struct oss_data *)ao;
|
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 */
|
2010-05-13 14:42:09 +02:00
|
|
|
if (od->fd < 0 && !oss_reopen(od, error))
|
2009-02-26 22:04:59 +01:00
|
|
|
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
|
|
|
};
|