2009-03-13 18:43:16 +01:00
|
|
|
/*
|
2016-02-26 17:54:05 +01:00
|
|
|
* Copyright 2003-2016 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"
|
2013-01-29 14:32:32 +01:00
|
|
|
#include "OssOutputPlugin.hxx"
|
2014-01-23 23:49:50 +01:00
|
|
|
#include "../OutputAPI.hxx"
|
2015-01-06 16:24:33 +01:00
|
|
|
#include "../Wrapper.hxx"
|
2014-01-24 16:25:21 +01:00
|
|
|
#include "mixer/MixerList.hxx"
|
2013-08-07 10:31:31 +02:00
|
|
|
#include "system/fd_util.h"
|
2016-11-05 13:18:45 +01:00
|
|
|
#include "system/Error.hxx"
|
2014-08-12 21:40:06 +02:00
|
|
|
#include "util/ConstBuffer.hxx"
|
2013-08-10 18:02:44 +02:00
|
|
|
#include "util/Domain.hxx"
|
2013-10-15 22:04:17 +02:00
|
|
|
#include "util/Macros.hxx"
|
2013-10-16 21:09:19 +02:00
|
|
|
#include "system/ByteOrder.hxx"
|
2013-09-27 22:31:24 +02:00
|
|
|
#include "Log.hxx"
|
2004-10-29 05:30:23 +02:00
|
|
|
|
2016-11-05 13:18:45 +01:00
|
|
|
#include <stdexcept>
|
|
|
|
|
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
|
|
|
|
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
|
|
|
|
|
2012-03-21 19:19:40 +01:00
|
|
|
#ifdef AFMT_S24_PACKED
|
2013-04-09 01:24:32 +02:00
|
|
|
#include "pcm/PcmExport.hxx"
|
|
|
|
#include "util/Manual.hxx"
|
2012-03-21 19:19:40 +01:00
|
|
|
#endif
|
|
|
|
|
2015-01-07 18:40:30 +01:00
|
|
|
class OssOutput {
|
|
|
|
friend struct AudioOutputWrapper<OssOutput>;
|
|
|
|
|
2014-01-28 11:34:09 +01:00
|
|
|
AudioOutput base;
|
2011-09-16 23:31:48 +02:00
|
|
|
|
2012-03-21 19:19:40 +01:00
|
|
|
#ifdef AFMT_S24_PACKED
|
2013-04-09 01:24:32 +02:00
|
|
|
Manual<PcmExport> pcm_export;
|
2012-03-21 19:19:40 +01:00
|
|
|
#endif
|
|
|
|
|
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().
|
|
|
|
*/
|
2013-08-03 21:00:50 +02:00
|
|
|
AudioFormat audio_format;
|
2012-03-21 23:47:29 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The current OSS audio format. This is needed to reopen the
|
|
|
|
* device after cancel().
|
|
|
|
*/
|
|
|
|
int oss_format;
|
2013-04-17 01:21:33 +02:00
|
|
|
|
2015-01-07 18:40:30 +01:00
|
|
|
public:
|
2016-11-05 13:18:45 +01:00
|
|
|
OssOutput(const ConfigBlock &block, const char *_device=nullptr)
|
|
|
|
:base(oss_output_plugin, block),
|
2015-01-07 18:43:45 +01:00
|
|
|
fd(-1), device(_device) {}
|
2013-04-17 01:21:33 +02:00
|
|
|
|
2016-11-09 11:56:01 +01:00
|
|
|
static OssOutput *Create(const ConfigBlock &block);
|
2015-01-06 16:24:33 +01:00
|
|
|
|
|
|
|
#ifdef AFMT_S24_PACKED
|
2016-11-09 11:56:01 +01:00
|
|
|
void Enable() {
|
2015-01-06 16:24:33 +01:00
|
|
|
pcm_export.Construct();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Disable() {
|
|
|
|
pcm_export.Destruct();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2016-11-09 11:56:01 +01:00
|
|
|
void Open(AudioFormat &audio_format);
|
2015-01-06 16:24:33 +01:00
|
|
|
|
|
|
|
void Close() {
|
|
|
|
DoClose();
|
|
|
|
}
|
|
|
|
|
2016-11-09 11:56:01 +01:00
|
|
|
size_t Play(const void *chunk, size_t size);
|
2015-01-06 16:24:33 +01:00
|
|
|
void Cancel();
|
|
|
|
|
|
|
|
private:
|
|
|
|
/**
|
|
|
|
* Sets up the OSS device which was opened before.
|
|
|
|
*/
|
2016-11-05 13:18:45 +01:00
|
|
|
void Setup(AudioFormat &audio_format);
|
2015-01-06 16:24:33 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Reopen the device with the saved audio_format, without any probing.
|
2016-11-05 13:18:45 +01:00
|
|
|
*
|
|
|
|
* Throws #std::runtime_error on error.
|
2015-01-06 16:24:33 +01:00
|
|
|
*/
|
2016-11-05 13:18:45 +01:00
|
|
|
void Reopen();
|
2015-01-06 16:24:33 +01:00
|
|
|
|
|
|
|
void DoClose();
|
2008-10-14 17:21:53 +02:00
|
|
|
};
|
|
|
|
|
2013-08-10 18:02:44 +02:00
|
|
|
static constexpr Domain oss_output_domain("oss_output");
|
2009-02-26 22:04:59 +01: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
|
|
|
}
|
|
|
|
|
2014-12-09 22:02:18 +01:00
|
|
|
static const char *const default_devices[] = { "/dev/sound/dsp", "/dev/dsp" };
|
2006-08-01 14:39:10 +02:00
|
|
|
|
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
|
|
|
|
2013-10-15 22:04:17 +02:00
|
|
|
for (i = ARRAY_SIZE(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
|
|
|
}
|
2013-09-27 22:31:24 +02:00
|
|
|
|
|
|
|
FormatErrno(oss_output_domain,
|
|
|
|
"Error opening OSS device \"%s\"",
|
|
|
|
default_devices[i]);
|
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
|
|
|
|
2015-01-06 16:24:33 +01:00
|
|
|
static OssOutput *
|
2016-11-05 13:18:45 +01:00
|
|
|
oss_open_default()
|
2006-08-01 14:39:10 +02:00
|
|
|
{
|
2013-10-15 22:04:17 +02:00
|
|
|
int err[ARRAY_SIZE(default_devices)];
|
|
|
|
enum oss_stat ret[ARRAY_SIZE(default_devices)];
|
2005-03-12 04:10:09 +01:00
|
|
|
|
2015-01-21 22:13:44 +01:00
|
|
|
const ConfigBlock empty;
|
2013-10-15 22:04:17 +02:00
|
|
|
for (int i = ARRAY_SIZE(default_devices); --i >= 0; ) {
|
2009-02-26 19:16:33 +01:00
|
|
|
ret[i] = oss_stat_device(default_devices[i], &err[i]);
|
2016-11-05 13:18:45 +01:00
|
|
|
if (ret[i] == OSS_STAT_NO_ERROR)
|
|
|
|
return new OssOutput(empty, default_devices[i]);
|
2005-03-12 04:10:09 +01:00
|
|
|
}
|
|
|
|
|
2013-10-15 22:04:17 +02:00
|
|
|
for (int i = ARRAY_SIZE(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:
|
2013-09-27 22:31:24 +02:00
|
|
|
FormatWarning(oss_output_domain,
|
|
|
|
"%s not found", dev);
|
2006-08-01 14:39:10 +02:00
|
|
|
break;
|
|
|
|
case OSS_STAT_NOT_CHAR_DEV:
|
2013-09-27 22:31:24 +02:00
|
|
|
FormatWarning(oss_output_domain,
|
|
|
|
"%s is not a character device", dev);
|
2006-08-01 14:39:10 +02:00
|
|
|
break;
|
|
|
|
case OSS_STAT_NO_PERMS:
|
2013-09-27 22:31:24 +02:00
|
|
|
FormatWarning(oss_output_domain,
|
|
|
|
"%s: permission denied", dev);
|
2006-08-01 14:39:10 +02:00
|
|
|
break;
|
2009-02-26 19:18:13 +01:00
|
|
|
case OSS_STAT_OTHER:
|
2013-09-27 22:31:24 +02:00
|
|
|
FormatErrno(oss_output_domain, err[i],
|
|
|
|
"Error accessing %s", dev);
|
2006-08-01 14:39:10 +02:00
|
|
|
}
|
|
|
|
}
|
2009-02-26 22:04:59 +01:00
|
|
|
|
2016-11-05 13:18:45 +01:00
|
|
|
throw std::runtime_error("error trying to open default OSS device");
|
2005-03-12 04:10:09 +01:00
|
|
|
}
|
|
|
|
|
2015-01-06 16:24:33 +01:00
|
|
|
inline OssOutput *
|
2016-11-09 11:56:01 +01:00
|
|
|
OssOutput::Create(const ConfigBlock &block)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2015-01-21 22:13:44 +01:00
|
|
|
const char *device = block.GetBlockValue("device");
|
2016-11-05 13:18:45 +01:00
|
|
|
if (device != nullptr)
|
|
|
|
return new OssOutput(block, device);
|
2011-09-16 23:31:48 +02:00
|
|
|
|
2016-11-05 13:18:45 +01:00
|
|
|
return oss_open_default();
|
2004-10-29 05:30:23 +02:00
|
|
|
}
|
|
|
|
|
2015-01-06 16:24:33 +01:00
|
|
|
void
|
|
|
|
OssOutput::DoClose()
|
2012-03-21 19:19:40 +01:00
|
|
|
{
|
2015-01-06 16:24:33 +01:00
|
|
|
if (fd >= 0)
|
|
|
|
close(fd);
|
|
|
|
fd = -1;
|
2006-07-16 18:53:04 +02:00
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
UNSUPPORTED,
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Invoke an ioctl on the OSS file descriptor. On success, SUCCESS is
|
|
|
|
* returned. If the parameter is not supported, UNSUPPORTED is
|
2016-11-05 13:18:45 +01:00
|
|
|
* returned. Any other failure throws std::runtime_error.
|
2010-05-13 14:42:09 +02:00
|
|
|
*/
|
|
|
|
static enum oss_setup_result
|
|
|
|
oss_try_ioctl_r(int fd, unsigned long request, int *value_r,
|
2016-11-05 13:18:45 +01:00
|
|
|
const char *msg)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2010-05-13 14:42:09 +02:00
|
|
|
assert(fd >= 0);
|
2015-01-07 18:55:31 +01:00
|
|
|
assert(value_r != nullptr);
|
|
|
|
assert(msg != nullptr);
|
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;
|
|
|
|
|
2016-11-05 13:18:45 +01:00
|
|
|
throw MakeErrno(msg);
|
2010-05-13 14:42:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Invoke an ioctl on the OSS file descriptor. On success, SUCCESS is
|
|
|
|
* returned. If the parameter is not supported, UNSUPPORTED is
|
2016-11-05 13:18:45 +01:00
|
|
|
* returned. Any other failure throws std::runtime_error.
|
2010-05-13 14:42:09 +02:00
|
|
|
*/
|
|
|
|
static enum oss_setup_result
|
|
|
|
oss_try_ioctl(int fd, unsigned long request, int value,
|
2016-11-05 13:18:45 +01:00
|
|
|
const char *msg)
|
2010-05-13 14:42:09 +02:00
|
|
|
{
|
2016-11-05 13:18:45 +01:00
|
|
|
return oss_try_ioctl_r(fd, request, &value, msg);
|
2010-05-13 14:42:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set up the channel number, and attempts to find alternatives if the
|
|
|
|
* specified number is not supported.
|
2016-11-05 13:18:45 +01:00
|
|
|
*
|
|
|
|
* Throws #std::runtime_error on error.
|
2010-05-13 14:42:09 +02:00
|
|
|
*/
|
2016-11-05 13:18:45 +01:00
|
|
|
static void
|
|
|
|
oss_setup_channels(int fd, AudioFormat &audio_format)
|
2010-05-13 14:42:09 +02:00
|
|
|
{
|
|
|
|
const char *const msg = "Failed to set channel count";
|
2013-08-03 21:00:50 +02:00
|
|
|
int channels = audio_format.channels;
|
2010-05-13 14:42:09 +02:00
|
|
|
enum oss_setup_result result =
|
2016-11-05 13:18:45 +01:00
|
|
|
oss_try_ioctl_r(fd, SNDCTL_DSP_CHANNELS, &channels, msg);
|
2010-05-13 14:42:09 +02:00
|
|
|
switch (result) {
|
|
|
|
case SUCCESS:
|
|
|
|
if (!audio_valid_channel_count(channels))
|
|
|
|
break;
|
|
|
|
|
2013-08-03 21:00:50 +02:00
|
|
|
audio_format.channels = channels;
|
2016-11-05 13:18:45 +01:00
|
|
|
return;
|
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) {
|
2013-08-03 21:00:50 +02:00
|
|
|
if (i == audio_format.channels)
|
2010-05-13 14:42:09 +02:00
|
|
|
/* don't try that again */
|
|
|
|
continue;
|
|
|
|
|
|
|
|
channels = i;
|
|
|
|
result = oss_try_ioctl_r(fd, SNDCTL_DSP_CHANNELS, &channels,
|
2016-11-05 13:18:45 +01:00
|
|
|
msg);
|
2010-05-13 14:42:09 +02:00
|
|
|
switch (result) {
|
|
|
|
case SUCCESS:
|
|
|
|
if (!audio_valid_channel_count(channels))
|
|
|
|
break;
|
|
|
|
|
2013-08-03 21:00:50 +02:00
|
|
|
audio_format.channels = channels;
|
2016-11-05 13:18:45 +01:00
|
|
|
return;
|
2010-05-13 14:42:09 +02:00
|
|
|
|
|
|
|
case UNSUPPORTED:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-05 13:18:45 +01:00
|
|
|
throw std::runtime_error(msg);
|
2010-05-13 14:42:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set up the sample rate, and attempts to find alternatives if the
|
|
|
|
* specified sample rate is not supported.
|
2016-11-05 13:18:45 +01:00
|
|
|
*
|
|
|
|
* Throws #std::runtime_error on error.
|
2010-05-13 14:42:09 +02:00
|
|
|
*/
|
2016-11-05 13:18:45 +01:00
|
|
|
static void
|
|
|
|
oss_setup_sample_rate(int fd, AudioFormat &audio_format)
|
2010-05-13 14:42:09 +02:00
|
|
|
{
|
|
|
|
const char *const msg = "Failed to set sample rate";
|
2013-08-03 21:00:50 +02:00
|
|
|
int sample_rate = audio_format.sample_rate;
|
2010-05-13 14:42:09 +02:00
|
|
|
enum oss_setup_result result =
|
|
|
|
oss_try_ioctl_r(fd, SNDCTL_DSP_SPEED, &sample_rate,
|
2016-11-05 13:18:45 +01:00
|
|
|
msg);
|
2010-05-13 14:42:09 +02:00
|
|
|
switch (result) {
|
|
|
|
case SUCCESS:
|
|
|
|
if (!audio_valid_sample_rate(sample_rate))
|
|
|
|
break;
|
|
|
|
|
2013-08-03 21:00:50 +02:00
|
|
|
audio_format.sample_rate = sample_rate;
|
2016-11-05 13:18:45 +01:00
|
|
|
return;
|
2010-05-13 14:42:09 +02:00
|
|
|
|
|
|
|
case UNSUPPORTED:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2014-12-09 22:02:18 +01:00
|
|
|
static constexpr int sample_rates[] = { 48000, 44100, 0 };
|
2010-05-13 14:42:09 +02:00
|
|
|
for (unsigned i = 0; sample_rates[i] != 0; ++i) {
|
|
|
|
sample_rate = sample_rates[i];
|
2013-08-03 21:00:50 +02:00
|
|
|
if (sample_rate == (int)audio_format.sample_rate)
|
2010-05-13 14:42:09 +02:00
|
|
|
continue;
|
|
|
|
|
|
|
|
result = oss_try_ioctl_r(fd, SNDCTL_DSP_SPEED, &sample_rate,
|
2016-11-05 13:18:45 +01:00
|
|
|
msg);
|
2010-05-13 14:42:09 +02:00
|
|
|
switch (result) {
|
|
|
|
case SUCCESS:
|
|
|
|
if (!audio_valid_sample_rate(sample_rate))
|
|
|
|
break;
|
2011-02-04 10:39:21 +01:00
|
|
|
|
2013-08-03 21:00:50 +02:00
|
|
|
audio_format.sample_rate = sample_rate;
|
2016-11-05 13:18:45 +01:00
|
|
|
return;
|
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
|
|
|
|
2016-11-05 13:18:45 +01:00
|
|
|
throw std::runtime_error(msg);
|
2010-05-13 14:42:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert a MPD sample format to its OSS counterpart. Returns
|
|
|
|
* AFMT_QUERY if there is no direct counterpart.
|
|
|
|
*/
|
2015-01-07 18:50:38 +01:00
|
|
|
gcc_const
|
2010-05-13 14:42:09 +02:00
|
|
|
static int
|
2013-08-03 21:00:50 +02:00
|
|
|
sample_format_to_oss(SampleFormat format)
|
2010-05-13 14:42:09 +02:00
|
|
|
{
|
|
|
|
switch (format) {
|
2013-08-03 21:00:50 +02:00
|
|
|
case SampleFormat::UNDEFINED:
|
|
|
|
case SampleFormat::FLOAT:
|
|
|
|
case SampleFormat::DSD:
|
2010-05-13 14:42:09 +02:00
|
|
|
return AFMT_QUERY;
|
|
|
|
|
2013-08-03 21:00:50 +02:00
|
|
|
case SampleFormat::S8:
|
2010-05-13 14:42:09 +02:00
|
|
|
return AFMT_S8;
|
2009-11-10 17:11:34 +01:00
|
|
|
|
2013-08-03 21:00:50 +02:00
|
|
|
case SampleFormat::S16:
|
2010-05-13 14:42:09 +02:00
|
|
|
return AFMT_S16_NE;
|
|
|
|
|
2013-08-03 21:00:50 +02:00
|
|
|
case SampleFormat::S24_P32:
|
2010-05-13 15:42:20 +02:00
|
|
|
#ifdef AFMT_S24_NE
|
|
|
|
return AFMT_S24_NE;
|
|
|
|
#else
|
|
|
|
return AFMT_QUERY;
|
|
|
|
#endif
|
|
|
|
|
2013-08-03 21:00:50 +02:00
|
|
|
case SampleFormat::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
|
|
|
}
|
|
|
|
|
2015-01-07 18:49:29 +01:00
|
|
|
assert(false);
|
|
|
|
gcc_unreachable();
|
2010-05-13 14:42:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert an OSS sample format to its MPD counterpart. Returns
|
2013-08-03 21:00:50 +02:00
|
|
|
* SampleFormat::UNDEFINED if there is no direct counterpart.
|
2010-05-13 14:42:09 +02:00
|
|
|
*/
|
2015-01-07 18:50:38 +01:00
|
|
|
gcc_const
|
2013-08-03 21:00:50 +02:00
|
|
|
static SampleFormat
|
2010-05-13 14:42:09 +02:00
|
|
|
sample_format_from_oss(int format)
|
|
|
|
{
|
|
|
|
switch (format) {
|
|
|
|
case AFMT_S8:
|
2013-08-03 21:00:50 +02:00
|
|
|
return SampleFormat::S8;
|
2010-05-13 14:42:09 +02:00
|
|
|
|
|
|
|
case AFMT_S16_NE:
|
2013-08-03 21:00:50 +02:00
|
|
|
return SampleFormat::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:
|
2013-08-03 21:00:50 +02:00
|
|
|
return SampleFormat::S24_P32;
|
2010-05-13 15:42:20 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef AFMT_S24_NE
|
|
|
|
case AFMT_S24_NE:
|
2013-08-03 21:00:50 +02:00
|
|
|
return SampleFormat::S24_P32;
|
2010-05-13 15:42:20 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef AFMT_S32_NE
|
|
|
|
case AFMT_S32_NE:
|
2013-08-03 21:00:50 +02:00
|
|
|
return SampleFormat::S32;
|
2010-05-13 15:42:20 +02:00
|
|
|
#endif
|
|
|
|
|
2008-10-29 18:35:03 +01:00
|
|
|
default:
|
2013-08-03 21:00:50 +02:00
|
|
|
return SampleFormat::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
|
|
|
|
2012-03-21 22:28:18 +01:00
|
|
|
/**
|
|
|
|
* Probe one sample format.
|
|
|
|
*
|
2013-08-03 21:00:50 +02:00
|
|
|
* @return the selected sample format or SampleFormat::UNDEFINED on
|
2012-03-21 22:28:18 +01:00
|
|
|
* error
|
|
|
|
*/
|
|
|
|
static enum oss_setup_result
|
2013-08-03 21:00:50 +02:00
|
|
|
oss_probe_sample_format(int fd, SampleFormat sample_format,
|
|
|
|
SampleFormat *sample_format_r,
|
2016-11-05 13:18:45 +01:00
|
|
|
int *oss_format_r
|
2012-03-21 22:28:18 +01:00
|
|
|
#ifdef AFMT_S24_PACKED
|
2016-11-05 13:18:45 +01:00
|
|
|
, PcmExport &pcm_export
|
2012-03-21 22:28:18 +01:00
|
|
|
#endif
|
2016-11-05 13:18:45 +01:00
|
|
|
)
|
2012-03-21 22:28:18 +01:00
|
|
|
{
|
|
|
|
int oss_format = sample_format_to_oss(sample_format);
|
|
|
|
if (oss_format == AFMT_QUERY)
|
|
|
|
return UNSUPPORTED;
|
|
|
|
|
|
|
|
enum oss_setup_result result =
|
|
|
|
oss_try_ioctl_r(fd, SNDCTL_DSP_SAMPLESIZE,
|
|
|
|
&oss_format,
|
2016-11-05 13:18:45 +01:00
|
|
|
"Failed to set sample format");
|
2012-03-21 22:15:45 +01:00
|
|
|
|
|
|
|
#ifdef AFMT_S24_PACKED
|
2013-08-03 21:00:50 +02:00
|
|
|
if (result == UNSUPPORTED && sample_format == SampleFormat::S24_P32) {
|
2012-03-21 22:15:45 +01:00
|
|
|
/* if the driver doesn't support padded 24 bit, try
|
|
|
|
packed 24 bit */
|
|
|
|
oss_format = AFMT_S24_PACKED;
|
|
|
|
result = oss_try_ioctl_r(fd, SNDCTL_DSP_SAMPLESIZE,
|
|
|
|
&oss_format,
|
2016-11-05 13:18:45 +01:00
|
|
|
"Failed to set sample format");
|
2012-03-21 22:15:45 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-03-21 22:28:18 +01:00
|
|
|
if (result != SUCCESS)
|
|
|
|
return result;
|
|
|
|
|
|
|
|
sample_format = sample_format_from_oss(oss_format);
|
2013-08-03 21:00:50 +02:00
|
|
|
if (sample_format == SampleFormat::UNDEFINED)
|
2012-03-21 22:28:18 +01:00
|
|
|
return UNSUPPORTED;
|
|
|
|
|
|
|
|
*sample_format_r = sample_format;
|
2012-03-21 23:47:29 +01:00
|
|
|
*oss_format_r = oss_format;
|
2012-03-21 22:28:18 +01:00
|
|
|
|
|
|
|
#ifdef AFMT_S24_PACKED
|
2016-02-26 18:54:13 +01:00
|
|
|
PcmExport::Params params;
|
|
|
|
params.alsa_channel_order = true;
|
|
|
|
params.pack24 = oss_format == AFMT_S24_PACKED;
|
|
|
|
params.reverse_endian = oss_format == AFMT_S24_PACKED &&
|
|
|
|
!IsLittleEndian();
|
|
|
|
|
|
|
|
pcm_export.Open(sample_format, 0, params);
|
2012-03-21 22:28:18 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
return SUCCESS;
|
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
*/
|
2016-11-05 13:18:45 +01:00
|
|
|
static void
|
2013-08-03 21:00:50 +02:00
|
|
|
oss_setup_sample_format(int fd, AudioFormat &audio_format,
|
2016-11-05 13:18:45 +01:00
|
|
|
int *oss_format_r
|
2012-03-21 19:19:40 +01:00
|
|
|
#ifdef AFMT_S24_PACKED
|
2016-11-05 13:18:45 +01:00
|
|
|
, PcmExport &pcm_export
|
2012-03-21 19:19:40 +01:00
|
|
|
#endif
|
2016-11-05 13:18:45 +01:00
|
|
|
)
|
2010-05-13 14:42:09 +02:00
|
|
|
{
|
2013-08-03 21:00:50 +02:00
|
|
|
SampleFormat mpd_format;
|
2012-03-21 22:28:18 +01:00
|
|
|
enum oss_setup_result result =
|
2013-08-03 21:00:50 +02:00
|
|
|
oss_probe_sample_format(fd, audio_format.format,
|
2016-11-05 13:18:45 +01:00
|
|
|
&mpd_format, oss_format_r
|
2012-03-21 22:28:18 +01:00
|
|
|
#ifdef AFMT_S24_PACKED
|
2016-11-05 13:18:45 +01:00
|
|
|
, pcm_export
|
2012-03-21 22:28:18 +01:00
|
|
|
#endif
|
2016-11-05 13:18:45 +01:00
|
|
|
);
|
2010-05-13 14:42:09 +02:00
|
|
|
switch (result) {
|
|
|
|
case SUCCESS:
|
2013-08-03 21:00:50 +02:00
|
|
|
audio_format.format = mpd_format;
|
2016-11-05 13:18:45 +01:00
|
|
|
return;
|
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 */
|
|
|
|
|
2014-12-09 22:02:18 +01:00
|
|
|
static constexpr SampleFormat sample_formats[] = {
|
2013-08-03 21:00:50 +02:00
|
|
|
SampleFormat::S24_P32,
|
|
|
|
SampleFormat::S32,
|
|
|
|
SampleFormat::S16,
|
|
|
|
SampleFormat::S8,
|
|
|
|
SampleFormat::UNDEFINED /* sentinel */
|
2010-05-13 14:42:09 +02:00
|
|
|
};
|
|
|
|
|
2013-08-03 21:00:50 +02:00
|
|
|
for (unsigned i = 0; sample_formats[i] != SampleFormat::UNDEFINED; ++i) {
|
2010-05-13 14:42:09 +02:00
|
|
|
mpd_format = sample_formats[i];
|
2013-08-03 21:00:50 +02:00
|
|
|
if (mpd_format == audio_format.format)
|
2010-05-13 14:42:09 +02:00
|
|
|
/* don't try that again */
|
|
|
|
continue;
|
|
|
|
|
2012-03-21 22:28:18 +01:00
|
|
|
result = oss_probe_sample_format(fd, mpd_format,
|
2016-11-05 13:18:45 +01:00
|
|
|
&mpd_format, oss_format_r
|
2012-03-21 22:28:18 +01:00
|
|
|
#ifdef AFMT_S24_PACKED
|
2016-11-05 13:18:45 +01:00
|
|
|
, pcm_export
|
2012-03-21 22:28:18 +01:00
|
|
|
#endif
|
2016-11-05 13:18:45 +01:00
|
|
|
);
|
2010-05-13 14:42:09 +02:00
|
|
|
switch (result) {
|
|
|
|
case SUCCESS:
|
2013-08-03 21:00:50 +02:00
|
|
|
audio_format.format = mpd_format;
|
2016-11-05 13:18:45 +01:00
|
|
|
return;
|
2010-05-13 14:42:09 +02:00
|
|
|
|
|
|
|
case UNSUPPORTED:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-05 13:18:45 +01:00
|
|
|
throw std::runtime_error("Failed to set sample format");
|
2009-02-26 19:18:16 +01:00
|
|
|
}
|
2004-10-30 06:19:11 +02:00
|
|
|
|
2016-11-05 13:18:45 +01:00
|
|
|
inline void
|
|
|
|
OssOutput::Setup(AudioFormat &_audio_format)
|
2009-02-26 19:18:16 +01:00
|
|
|
{
|
2016-11-05 13:18:45 +01:00
|
|
|
oss_setup_channels(fd, _audio_format);
|
|
|
|
oss_setup_sample_rate(fd, _audio_format);
|
|
|
|
oss_setup_sample_format(fd, _audio_format, &oss_format
|
2012-03-21 19:19:40 +01:00
|
|
|
#ifdef AFMT_S24_PACKED
|
2016-11-05 13:18:45 +01:00
|
|
|
, pcm_export
|
2012-03-21 19:19:40 +01:00
|
|
|
#endif
|
2016-11-05 13:18:45 +01:00
|
|
|
);
|
2010-05-13 14:42:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reopen the device with the saved audio_format, without any probing.
|
|
|
|
*/
|
2016-11-05 13:18:45 +01:00
|
|
|
inline void
|
|
|
|
OssOutput::Reopen()
|
|
|
|
try {
|
2015-01-06 16:24:33 +01:00
|
|
|
assert(fd < 0);
|
2009-02-26 19:18:16 +01:00
|
|
|
|
2015-01-06 16:24:33 +01:00
|
|
|
fd = open_cloexec(device, O_WRONLY, 0);
|
2016-11-05 13:18:45 +01:00
|
|
|
if (fd < 0)
|
|
|
|
throw FormatErrno("Error opening OSS device \"%s\"", device);
|
2009-02-26 19:18:16 +01:00
|
|
|
|
2010-05-13 14:42:09 +02:00
|
|
|
enum oss_setup_result result;
|
|
|
|
|
|
|
|
const char *const msg1 = "Failed to set channel count";
|
2015-01-06 16:24:33 +01:00
|
|
|
result = oss_try_ioctl(fd, SNDCTL_DSP_CHANNELS,
|
2016-11-05 13:18:45 +01:00
|
|
|
audio_format.channels, msg1);
|
2010-05-13 14:42:09 +02:00
|
|
|
if (result != SUCCESS) {
|
2015-01-06 16:24:33 +01:00
|
|
|
DoClose();
|
2016-11-05 13:18:45 +01:00
|
|
|
throw std::runtime_error(msg1);
|
2010-05-13 14:42:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const char *const msg2 = "Failed to set sample rate";
|
2015-01-06 16:24:33 +01:00
|
|
|
result = oss_try_ioctl(fd, SNDCTL_DSP_SPEED,
|
2016-11-05 13:18:45 +01:00
|
|
|
audio_format.sample_rate, msg2);
|
2010-05-13 14:42:09 +02:00
|
|
|
if (result != SUCCESS) {
|
2015-01-06 16:24:33 +01:00
|
|
|
DoClose();
|
2016-11-05 13:18:45 +01:00
|
|
|
throw std::runtime_error(msg2);
|
2010-05-13 14:42:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const char *const msg3 = "Failed to set sample format";
|
2015-01-06 16:24:33 +01:00
|
|
|
result = oss_try_ioctl(fd, SNDCTL_DSP_SAMPLESIZE,
|
|
|
|
oss_format,
|
2016-11-05 13:18:45 +01:00
|
|
|
msg3);
|
2010-05-13 14:42:09 +02:00
|
|
|
if (result != SUCCESS) {
|
2015-01-06 16:24:33 +01:00
|
|
|
DoClose();
|
2016-11-05 13:18:45 +01:00
|
|
|
throw std::runtime_error(msg3);
|
2009-02-26 19:18:16 +01:00
|
|
|
}
|
2016-11-05 13:18:45 +01:00
|
|
|
} catch (...) {
|
|
|
|
DoClose();
|
|
|
|
throw;
|
2004-10-30 06:19:11 +02:00
|
|
|
}
|
|
|
|
|
2016-11-09 11:56:01 +01:00
|
|
|
inline void
|
|
|
|
OssOutput::Open(AudioFormat &_audio_format)
|
2016-11-05 13:18:45 +01:00
|
|
|
try {
|
2015-01-06 16:24:33 +01:00
|
|
|
fd = open_cloexec(device, O_WRONLY, 0);
|
2016-11-05 13:18:45 +01:00
|
|
|
if (fd < 0)
|
|
|
|
throw FormatErrno("Error opening OSS device \"%s\"", device);
|
2005-03-10 03:47:18 +01:00
|
|
|
|
2016-11-05 13:18:45 +01:00
|
|
|
Setup(_audio_format);
|
2005-03-10 03:47:18 +01:00
|
|
|
|
2015-01-06 16:24:33 +01:00
|
|
|
audio_format = _audio_format;
|
2016-11-05 13:18:45 +01:00
|
|
|
} catch (...) {
|
|
|
|
DoClose();
|
|
|
|
throw;
|
2005-03-05 15:01:13 +01:00
|
|
|
}
|
|
|
|
|
2015-01-06 16:24:33 +01:00
|
|
|
inline void
|
|
|
|
OssOutput::Cancel()
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2015-01-06 16:24:33 +01:00
|
|
|
if (fd >= 0) {
|
|
|
|
ioctl(fd, SNDCTL_DSP_RESET, 0);
|
|
|
|
DoClose();
|
2005-03-05 15:01:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-06 16:24:33 +01:00
|
|
|
inline size_t
|
2016-11-09 11:56:01 +01:00
|
|
|
OssOutput::Play(const void *chunk, size_t size)
|
2004-10-29 05:30:23 +02:00
|
|
|
{
|
2008-04-12 06:15:52 +02:00
|
|
|
ssize_t ret;
|
2004-10-29 05:30:23 +02:00
|
|
|
|
2014-08-31 14:00:09 +02:00
|
|
|
assert(size > 0);
|
|
|
|
|
2005-04-07 00:52:42 +02:00
|
|
|
/* reopen the device since it was closed by dropBufferedAudio */
|
2016-11-05 13:18:45 +01:00
|
|
|
if (fd < 0)
|
|
|
|
Reopen();
|
2005-04-07 00:52:42 +02:00
|
|
|
|
2012-03-21 19:19:40 +01:00
|
|
|
#ifdef AFMT_S24_PACKED
|
2015-01-06 16:24:33 +01:00
|
|
|
const auto e = pcm_export->Export({chunk, size});
|
2014-08-12 21:40:06 +02:00
|
|
|
chunk = e.data;
|
|
|
|
size = e.size;
|
2012-03-21 19:19:40 +01:00
|
|
|
#endif
|
|
|
|
|
2014-08-31 14:00:09 +02:00
|
|
|
assert(size > 0);
|
|
|
|
|
2009-02-23 09:29:56 +01:00
|
|
|
while (true) {
|
2015-01-06 16:24:33 +01:00
|
|
|
ret = write(fd, chunk, size);
|
2012-03-27 00:03:44 +02:00
|
|
|
if (ret > 0) {
|
|
|
|
#ifdef AFMT_S24_PACKED
|
2015-01-06 16:24:33 +01:00
|
|
|
ret = pcm_export->CalcSourceSize(ret);
|
2012-03-27 00:03:44 +02:00
|
|
|
#endif
|
|
|
|
return ret;
|
|
|
|
}
|
2009-02-23 09:29:56 +01:00
|
|
|
|
2016-11-05 13:18:45 +01:00
|
|
|
if (ret < 0 && errno != EINTR)
|
|
|
|
throw FormatErrno("Write error on %s", device);
|
2004-10-29 05:30:23 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-06 16:24:33 +01:00
|
|
|
typedef AudioOutputWrapper<OssOutput> Wrapper;
|
|
|
|
|
2014-01-28 11:22:27 +01:00
|
|
|
const struct AudioOutputPlugin oss_output_plugin = {
|
2013-01-29 14:32:32 +01:00
|
|
|
"oss",
|
|
|
|
oss_output_test_default_device,
|
2015-01-06 16:24:33 +01:00
|
|
|
&Wrapper::Init,
|
|
|
|
&Wrapper::Finish,
|
2012-03-21 19:19:40 +01:00
|
|
|
#ifdef AFMT_S24_PACKED
|
2015-01-06 16:24:33 +01:00
|
|
|
&Wrapper::Enable,
|
|
|
|
&Wrapper::Disable,
|
2013-01-29 14:32:32 +01:00
|
|
|
#else
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
2012-03-21 19:19:40 +01:00
|
|
|
#endif
|
2015-01-06 16:24:33 +01:00
|
|
|
&Wrapper::Open,
|
|
|
|
&Wrapper::Close,
|
2013-01-29 14:32:32 +01:00
|
|
|
nullptr,
|
|
|
|
nullptr,
|
2015-01-06 16:24:33 +01:00
|
|
|
&Wrapper::Play,
|
2013-01-29 14:32:32 +01:00
|
|
|
nullptr,
|
2015-01-06 16:24:33 +01:00
|
|
|
&Wrapper::Cancel,
|
2013-01-29 14:32:32 +01:00
|
|
|
nullptr,
|
|
|
|
|
|
|
|
&oss_mixer_plugin,
|
2004-10-29 05:30:23 +02:00
|
|
|
};
|