mpd/src/output/plugins/SolarisOutputPlugin.cxx

164 lines
3.6 KiB
C++
Raw Normal View History

/*
2020-01-18 19:22:19 +01:00
* Copyright 2003-2020 The Music Player Daemon Project
* 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"
#include "system/FileDescriptor.hxx"
#include "system/Error.hxx"
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#if defined(__sun)
#include <sys/audio.h>
#include <sys/stropts.h>
#elif defined(__NetBSD__)
#include <sys/audioio.h>
#else
/* some fake declarations that allow build this plugin on systems
other than Solaris, just to see if it compiles */
#ifndef I_FLUSH
#define I_FLUSH 0
#endif
#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
class SolarisOutput final : AudioOutput {
/* configuration */
const char *const device;
FileDescriptor fd;
2013-04-17 01:08:35 +02:00
explicit SolarisOutput(const ConfigBlock &block)
:AudioOutput(0),
device(block.GetBlockValue("device", "/dev/audio")) {}
2017-01-25 10:29:42 +01:00
public:
static AudioOutput *Create(EventLoop &, const ConfigBlock &block) {
2017-01-25 10:29:42 +01:00
return new SolarisOutput(block);
}
private:
void Open(AudioFormat &audio_format) override;
void Close() noexcept override;
2017-01-25 10:29:42 +01:00
size_t Play(const void *chunk, size_t size) override;
void Cancel() noexcept override;
};
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)
{
struct audio_info info;
int ret;
/* 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;
/* open the device in non-blocking mode */
if (!fd.Open(device, O_WRONLY|O_NONBLOCK))
throw FormatErrno("Failed to open %s",
2017-01-25 10:29:42 +01:00
device);
/* restore blocking mode */
fd.SetBlocking();
/* configure the audio device */
ret = ioctl(fd.Get(), AUDIO_GETINFO, &info);
if (ret < 0) {
const int e = errno;
fd.Close();
throw MakeErrno(e, "AUDIO_GETINFO failed");
}
2013-08-03 21:00:50 +02:00
info.play.sample_rate = audio_format.sample_rate;
info.play.channels = audio_format.channels;
info.play.precision = 16;
info.play.encoding = AUDIO_ENCODING_LINEAR;
ret = ioctl(fd.Get(), AUDIO_SETINFO, &info);
if (ret < 0) {
const int e = errno;
fd.Close();
throw MakeErrno(e, "AUDIO_SETINFO failed");
}
}
2017-01-25 10:29:42 +01:00
void
SolarisOutput::Close() noexcept
{
fd.Close();
}
2017-01-25 10:29:42 +01:00
size_t
SolarisOutput::Play(const void *chunk, size_t size)
{
ssize_t nbytes = fd.Write(chunk, size);
if (nbytes <= 0)
throw MakeErrno("Write failed");
return nbytes;
}
2017-01-25 10:29:42 +01:00
void
SolarisOutput::Cancel() noexcept
{
#if defined(AUDIO_FLUSH)
ioctl(fd.Get(), AUDIO_FLUSH);
#elif defined(I_FLUSH)
ioctl(fd.Get(), I_FLUSH);
#endif
}
const struct AudioOutputPlugin solaris_output_plugin = {
2013-04-17 01:08:35 +02:00
"solaris",
solaris_output_test_default_device,
&SolarisOutput::Create,
2013-04-17 01:08:35 +02:00
nullptr,
};