2009-03-16 09:55:10 +01:00
|
|
|
/*
|
2015-01-01 19:48:13 +01:00
|
|
|
* Copyright (C) 2003-2015 The Music Player Daemon Project
|
2009-03-16 09:55:10 +01:00
|
|
|
* http://www.musicpd.org
|
|
|
|
*
|
|
|
|
* 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.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
|
|
|
|
2009-11-12 09:12:38 +01:00
|
|
|
#include "config.h"
|
2013-04-17 01:08:35 +02:00
|
|
|
#include "SolarisOutputPlugin.hxx"
|
2014-01-23 23:49:50 +01:00
|
|
|
#include "../OutputAPI.hxx"
|
2013-08-07 10:31:31 +02:00
|
|
|
#include "system/fd_util.h"
|
2013-08-10 18:02:44 +02:00
|
|
|
#include "util/Error.hxx"
|
2009-03-16 09:55:10 +01:00
|
|
|
|
|
|
|
#include <sys/stropts.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
2011-09-19 07:39:27 +02:00
|
|
|
#ifdef __sun
|
|
|
|
#include <sys/audio.h>
|
|
|
|
#else
|
|
|
|
|
|
|
|
/* some fake declarations that allow build this plugin on systems
|
|
|
|
other than Solaris, just to see if it compiles */
|
|
|
|
|
|
|
|
#define AUDIO_GETINFO 0
|
|
|
|
#define AUDIO_SETINFO 0
|
|
|
|
#define AUDIO_ENCODING_LINEAR 0
|
|
|
|
|
|
|
|
struct audio_info {
|
|
|
|
struct {
|
|
|
|
unsigned sample_rate, channels, precision, encoding;
|
|
|
|
} play;
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2013-04-17 01:08:35 +02:00
|
|
|
struct SolarisOutput {
|
2014-01-28 11:34:09 +01:00
|
|
|
AudioOutput base;
|
2011-09-16 23:31:48 +02:00
|
|
|
|
2009-03-16 09:55:10 +01:00
|
|
|
/* configuration */
|
|
|
|
const char *device;
|
|
|
|
|
|
|
|
int fd;
|
2013-04-17 01:08:35 +02:00
|
|
|
|
2014-01-28 23:39:48 +01:00
|
|
|
SolarisOutput()
|
|
|
|
:base(solaris_output_plugin) {}
|
|
|
|
|
2015-01-21 22:13:44 +01:00
|
|
|
bool Initialize(const ConfigBlock &block, Error &error_r) {
|
|
|
|
return base.Configure(block, error_r);
|
2013-04-17 01:08:35 +02:00
|
|
|
}
|
2009-03-16 09:55:10 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
static bool
|
|
|
|
solaris_output_test_default_device(void)
|
|
|
|
{
|
|
|
|
struct stat st;
|
|
|
|
|
|
|
|
return stat("/dev/audio", &st) == 0 && S_ISCHR(st.st_mode) &&
|
|
|
|
access("/dev/audio", W_OK) == 0;
|
|
|
|
}
|
|
|
|
|
2014-01-28 11:34:09 +01:00
|
|
|
static AudioOutput *
|
2015-01-21 22:13:44 +01:00
|
|
|
solaris_output_init(const ConfigBlock &block, Error &error_r)
|
2009-03-16 09:55:10 +01:00
|
|
|
{
|
2013-04-17 01:08:35 +02:00
|
|
|
SolarisOutput *so = new SolarisOutput();
|
2015-01-21 22:13:44 +01:00
|
|
|
if (!so->Initialize(block, error_r)) {
|
2013-04-17 01:08:35 +02:00
|
|
|
delete so;
|
|
|
|
return nullptr;
|
2011-09-16 23:31:48 +02:00
|
|
|
}
|
|
|
|
|
2015-01-21 22:13:44 +01:00
|
|
|
so->device = block.GetBlockValue("device", "/dev/audio");
|
2009-03-16 09:55:10 +01:00
|
|
|
|
2011-09-16 23:31:48 +02:00
|
|
|
return &so->base;
|
2009-03-16 09:55:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2014-01-28 11:34:09 +01:00
|
|
|
solaris_output_finish(AudioOutput *ao)
|
2009-03-16 09:55:10 +01:00
|
|
|
{
|
2013-04-17 01:08:35 +02:00
|
|
|
SolarisOutput *so = (SolarisOutput *)ao;
|
2009-03-16 09:55:10 +01:00
|
|
|
|
2013-04-17 01:08:35 +02:00
|
|
|
delete so;
|
2009-03-16 09:55:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
2014-01-28 11:34:09 +01:00
|
|
|
solaris_output_open(AudioOutput *ao, AudioFormat &audio_format,
|
2013-08-10 18:02:44 +02:00
|
|
|
Error &error)
|
2009-03-16 09:55:10 +01:00
|
|
|
{
|
2013-04-17 01:08:35 +02:00
|
|
|
SolarisOutput *so = (SolarisOutput *)ao;
|
2009-03-16 09:55:10 +01:00
|
|
|
struct audio_info info;
|
|
|
|
int ret, flags;
|
|
|
|
|
|
|
|
/* support only 16 bit mono/stereo for now; nothing else has
|
|
|
|
been tested */
|
2013-08-03 21:00:50 +02:00
|
|
|
audio_format.format = SampleFormat::S16;
|
2009-03-16 09:55:10 +01:00
|
|
|
|
|
|
|
/* open the device in non-blocking mode */
|
|
|
|
|
2010-12-21 07:27:35 +01:00
|
|
|
so->fd = open_cloexec(so->device, O_WRONLY|O_NONBLOCK, 0);
|
2009-03-16 09:55:10 +01:00
|
|
|
if (so->fd < 0) {
|
2013-08-10 18:02:44 +02:00
|
|
|
error.FormatErrno("Failed to open %s",
|
|
|
|
so->device);
|
2009-03-16 09:55:10 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* restore blocking mode */
|
|
|
|
|
|
|
|
flags = fcntl(so->fd, F_GETFL);
|
|
|
|
if (flags > 0 && (flags & O_NONBLOCK) != 0)
|
|
|
|
fcntl(so->fd, F_SETFL, flags & ~O_NONBLOCK);
|
|
|
|
|
|
|
|
/* configure the audio device */
|
|
|
|
|
|
|
|
ret = ioctl(so->fd, AUDIO_GETINFO, &info);
|
|
|
|
if (ret < 0) {
|
2013-08-10 18:02:44 +02:00
|
|
|
error.SetErrno("AUDIO_GETINFO failed");
|
2009-03-16 09:55:10 +01:00
|
|
|
close(so->fd);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-08-03 21:00:50 +02:00
|
|
|
info.play.sample_rate = audio_format.sample_rate;
|
|
|
|
info.play.channels = audio_format.channels;
|
2009-11-10 17:11:34 +01:00
|
|
|
info.play.precision = 16;
|
2009-03-16 09:55:10 +01:00
|
|
|
info.play.encoding = AUDIO_ENCODING_LINEAR;
|
|
|
|
|
|
|
|
ret = ioctl(so->fd, AUDIO_SETINFO, &info);
|
|
|
|
if (ret < 0) {
|
2013-08-10 18:02:44 +02:00
|
|
|
error.SetErrno("AUDIO_SETINFO failed");
|
2009-03-16 09:55:10 +01:00
|
|
|
close(so->fd);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2014-01-28 11:34:09 +01:00
|
|
|
solaris_output_close(AudioOutput *ao)
|
2009-03-16 09:55:10 +01:00
|
|
|
{
|
2013-04-17 01:08:35 +02:00
|
|
|
SolarisOutput *so = (SolarisOutput *)ao;
|
2009-03-16 09:55:10 +01:00
|
|
|
|
|
|
|
close(so->fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
static size_t
|
2014-01-28 11:34:09 +01:00
|
|
|
solaris_output_play(AudioOutput *ao, const void *chunk, size_t size,
|
2013-08-10 18:02:44 +02:00
|
|
|
Error &error)
|
2009-03-16 09:55:10 +01:00
|
|
|
{
|
2013-04-17 01:08:35 +02:00
|
|
|
SolarisOutput *so = (SolarisOutput *)ao;
|
2009-03-16 09:55:10 +01:00
|
|
|
ssize_t nbytes;
|
|
|
|
|
|
|
|
nbytes = write(so->fd, chunk, size);
|
|
|
|
if (nbytes <= 0) {
|
2013-08-10 18:02:44 +02:00
|
|
|
error.SetErrno("Write failed");
|
2009-03-16 09:55:10 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return nbytes;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2014-01-28 11:34:09 +01:00
|
|
|
solaris_output_cancel(AudioOutput *ao)
|
2009-03-16 09:55:10 +01:00
|
|
|
{
|
2013-04-17 01:08:35 +02:00
|
|
|
SolarisOutput *so = (SolarisOutput *)ao;
|
2009-03-16 09:55:10 +01:00
|
|
|
|
|
|
|
ioctl(so->fd, I_FLUSH);
|
|
|
|
}
|
|
|
|
|
2014-01-28 11:22:27 +01:00
|
|
|
const struct AudioOutputPlugin solaris_output_plugin = {
|
2013-04-17 01:08:35 +02:00
|
|
|
"solaris",
|
|
|
|
solaris_output_test_default_device,
|
|
|
|
solaris_output_init,
|
|
|
|
solaris_output_finish,
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
solaris_output_open,
|
|
|
|
solaris_output_close,
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
solaris_output_play,
|
|
|
|
nullptr,
|
|
|
|
solaris_output_cancel,
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
2009-03-16 09:55:10 +01:00
|
|
|
};
|