2009-03-16 09:55:10 +01:00
|
|
|
/*
|
2021-01-01 19:54:25 +01:00
|
|
|
* Copyright 2003-2021 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.
|
|
|
|
*/
|
|
|
|
|
2013-04-17 01:08:35 +02:00
|
|
|
#include "SolarisOutputPlugin.hxx"
|
2014-01-23 23:49:50 +01:00
|
|
|
#include "../OutputAPI.hxx"
|
2020-05-05 14:11:13 +02:00
|
|
|
#include "io/FileDescriptor.hxx"
|
2016-11-09 11:18:19 +01:00
|
|
|
#include "system/Error.hxx"
|
2009-03-16 09:55:10 +01:00
|
|
|
|
2020-03-12 23:51:16 +01:00
|
|
|
#include <cerrno>
|
|
|
|
|
2020-02-29 09:56:10 +01:00
|
|
|
#include <sys/ioctl.h>
|
2009-03-16 09:55:10 +01:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
2020-02-29 09:56:10 +01:00
|
|
|
#if defined(__sun)
|
2011-09-19 07:39:27 +02:00
|
|
|
#include <sys/audio.h>
|
2019-08-22 11:41:12 +02:00
|
|
|
#include <sys/stropts.h>
|
2020-02-29 09:56:10 +01:00
|
|
|
#elif defined(__NetBSD__)
|
|
|
|
#include <sys/audioio.h>
|
2011-09-19 07:39:27 +02:00
|
|
|
#else
|
|
|
|
|
|
|
|
/* some fake declarations that allow build this plugin on systems
|
|
|
|
other than Solaris, just to see if it compiles */
|
|
|
|
|
2019-08-22 11:41:12 +02:00
|
|
|
#ifndef I_FLUSH
|
|
|
|
#define I_FLUSH 0
|
|
|
|
#endif
|
|
|
|
|
2020-04-14 15:20:24 +02:00
|
|
|
#define AUDIO_INITINFO(v)
|
2011-09-19 07:39:27 +02:00
|
|
|
#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
|
|
|
|
|
2017-08-09 16:58:44 +02:00
|
|
|
class SolarisOutput final : AudioOutput {
|
2009-03-16 09:55:10 +01:00
|
|
|
/* configuration */
|
2016-11-09 11:18:19 +01:00
|
|
|
const char *const device;
|
2009-03-16 09:55:10 +01:00
|
|
|
|
2017-08-10 08:56:56 +02:00
|
|
|
FileDescriptor fd;
|
2013-04-17 01:08:35 +02:00
|
|
|
|
2016-11-09 11:18:19 +01:00
|
|
|
explicit SolarisOutput(const ConfigBlock &block)
|
2017-08-09 16:58:44 +02:00
|
|
|
:AudioOutput(0),
|
2016-11-09 11:18:19 +01:00
|
|
|
device(block.GetBlockValue("device", "/dev/audio")) {}
|
2017-01-25 10:29:42 +01:00
|
|
|
|
2017-01-25 10:33:38 +01:00
|
|
|
public:
|
2017-08-09 16:58:44 +02:00
|
|
|
static AudioOutput *Create(EventLoop &, const ConfigBlock &block) {
|
2017-01-25 10:29:42 +01:00
|
|
|
return new SolarisOutput(block);
|
|
|
|
}
|
|
|
|
|
2017-08-09 16:58:44 +02:00
|
|
|
private:
|
|
|
|
void Open(AudioFormat &audio_format) override;
|
|
|
|
void Close() noexcept override;
|
2017-01-25 10:29:42 +01:00
|
|
|
|
2017-08-09 16:58:44 +02:00
|
|
|
size_t Play(const void *chunk, size_t size) override;
|
|
|
|
void Cancel() noexcept override;
|
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;
|
|
|
|
}
|
|
|
|
|
2017-01-25 10:29:42 +01:00
|
|
|
void
|
|
|
|
SolarisOutput::Open(AudioFormat &audio_format)
|
2009-03-16 09:55:10 +01:00
|
|
|
{
|
|
|
|
struct audio_info info;
|
2017-08-10 08:56:56 +02:00
|
|
|
int ret;
|
2009-03-16 09:55:10 +01:00
|
|
|
|
2020-04-14 15:20:24 +02:00
|
|
|
AUDIO_INITINFO(&info);
|
|
|
|
|
2009-03-16 09:55:10 +01:00
|
|
|
/* open the device in non-blocking mode */
|
|
|
|
|
2017-08-10 08:56:56 +02:00
|
|
|
if (!fd.Open(device, O_WRONLY|O_NONBLOCK))
|
2016-11-09 11:18:19 +01:00
|
|
|
throw FormatErrno("Failed to open %s",
|
2017-01-25 10:29:42 +01:00
|
|
|
device);
|
2009-03-16 09:55:10 +01:00
|
|
|
|
|
|
|
/* restore blocking mode */
|
|
|
|
|
2017-08-10 08:56:56 +02:00
|
|
|
fd.SetBlocking();
|
2009-03-16 09:55:10 +01:00
|
|
|
|
2020-04-14 15:36:31 +02:00
|
|
|
/* configure the audio device */
|
|
|
|
|
2013-08-03 21:00:50 +02:00
|
|
|
info.play.sample_rate = audio_format.sample_rate;
|
|
|
|
info.play.channels = audio_format.channels;
|
2009-03-16 09:55:10 +01:00
|
|
|
info.play.encoding = AUDIO_ENCODING_LINEAR;
|
2020-04-14 15:36:31 +02:00
|
|
|
switch (audio_format.format) {
|
|
|
|
case SampleFormat::S8:
|
|
|
|
info.play.precision = 8;
|
|
|
|
break;
|
|
|
|
case SampleFormat::S16:
|
|
|
|
info.play.precision = 16;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
info.play.precision = 32;
|
|
|
|
audio_format.format = SampleFormat::S32;
|
|
|
|
break;
|
|
|
|
}
|
2009-03-16 09:55:10 +01:00
|
|
|
|
2017-08-10 08:56:56 +02:00
|
|
|
ret = ioctl(fd.Get(), AUDIO_SETINFO, &info);
|
2009-03-16 09:55:10 +01:00
|
|
|
if (ret < 0) {
|
2016-11-09 11:18:19 +01:00
|
|
|
const int e = errno;
|
2017-08-10 08:56:56 +02:00
|
|
|
fd.Close();
|
2016-11-09 11:18:19 +01:00
|
|
|
throw MakeErrno(e, "AUDIO_SETINFO failed");
|
2009-03-16 09:55:10 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-25 10:29:42 +01:00
|
|
|
void
|
2017-08-09 16:58:44 +02:00
|
|
|
SolarisOutput::Close() noexcept
|
2009-03-16 09:55:10 +01:00
|
|
|
{
|
2017-08-10 08:56:56 +02:00
|
|
|
fd.Close();
|
2009-03-16 09:55:10 +01:00
|
|
|
}
|
|
|
|
|
2017-01-25 10:29:42 +01:00
|
|
|
size_t
|
|
|
|
SolarisOutput::Play(const void *chunk, size_t size)
|
2009-03-16 09:55:10 +01:00
|
|
|
{
|
2017-08-10 08:56:56 +02:00
|
|
|
ssize_t nbytes = fd.Write(chunk, size);
|
2016-11-09 11:18:19 +01:00
|
|
|
if (nbytes <= 0)
|
|
|
|
throw MakeErrno("Write failed");
|
2009-03-16 09:55:10 +01:00
|
|
|
|
|
|
|
return nbytes;
|
|
|
|
}
|
|
|
|
|
2017-01-25 10:29:42 +01:00
|
|
|
void
|
2017-08-09 16:58:44 +02:00
|
|
|
SolarisOutput::Cancel() noexcept
|
2009-03-16 09:55:10 +01:00
|
|
|
{
|
2020-02-29 09:56:10 +01:00
|
|
|
#if defined(AUDIO_FLUSH)
|
|
|
|
ioctl(fd.Get(), AUDIO_FLUSH);
|
|
|
|
#elif defined(I_FLUSH)
|
2017-08-10 08:56:56 +02:00
|
|
|
ioctl(fd.Get(), I_FLUSH);
|
2020-02-29 09:56:10 +01:00
|
|
|
#endif
|
2009-03-16 09:55:10 +01:00
|
|
|
}
|
|
|
|
|
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,
|
2017-08-09 16:58:44 +02:00
|
|
|
&SolarisOutput::Create,
|
2013-04-17 01:08:35 +02:00
|
|
|
nullptr,
|
2009-03-16 09:55:10 +01:00
|
|
|
};
|