SolarisOutputPlugin.cxx 3.61 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2020 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
 * 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.
 */

20
#include "SolarisOutputPlugin.hxx"
21
#include "../OutputAPI.hxx"
22
#include "system/FileDescriptor.hxx"
23
#include "system/Error.hxx"
24

Rosen Penev's avatar
Rosen Penev committed
25 26
#include <cerrno>

27
#include <sys/ioctl.h>
28 29 30 31 32
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>

33
#if defined(__sun)
34
#include <sys/audio.h>
35
#include <sys/stropts.h>
36 37
#elif defined(__NetBSD__)
#include <sys/audioio.h>
38 39 40 41 42
#else

/* some fake declarations that allow build this plugin on systems
   other than Solaris, just to see if it compiles */

43 44 45 46
#ifndef I_FLUSH
#define I_FLUSH 0
#endif

47 48 49 50 51 52 53 54 55 56 57 58
#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

59
class SolarisOutput final : AudioOutput {
60
	/* configuration */
61
	const char *const device;
62

63
	FileDescriptor fd;
64

65
	explicit SolarisOutput(const ConfigBlock &block)
66
		:AudioOutput(0),
67
		 device(block.GetBlockValue("device", "/dev/audio")) {}
68

69
public:
70
	static AudioOutput *Create(EventLoop &, const ConfigBlock &block) {
71 72 73
		return new SolarisOutput(block);
	}

74 75 76
private:
	void Open(AudioFormat &audio_format) override;
	void Close() noexcept override;
77

78 79
	size_t Play(const void *chunk, size_t size) override;
	void Cancel() noexcept override;
80 81 82 83 84 85 86 87 88 89 90
};

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;
}

91 92
void
SolarisOutput::Open(AudioFormat &audio_format)
93 94
{
	struct audio_info info;
95
	int ret;
96 97 98

	/* support only 16 bit mono/stereo for now; nothing else has
	   been tested */
99
	audio_format.format = SampleFormat::S16;
100 101 102

	/* open the device in non-blocking mode */

103
	if (!fd.Open(device, O_WRONLY|O_NONBLOCK))
104
		throw FormatErrno("Failed to open %s",
105
				  device);
106 107 108

	/* restore blocking mode */

109
	fd.SetBlocking();
110 111 112

	/* configure the audio device */

113
	ret = ioctl(fd.Get(), AUDIO_GETINFO, &info);
114
	if (ret < 0) {
115
		const int e = errno;
116
		fd.Close();
117
		throw MakeErrno(e, "AUDIO_GETINFO failed");
118 119
	}

120 121
	info.play.sample_rate = audio_format.sample_rate;
	info.play.channels = audio_format.channels;
122
	info.play.precision = 16;
123 124
	info.play.encoding = AUDIO_ENCODING_LINEAR;

125
	ret = ioctl(fd.Get(), AUDIO_SETINFO, &info);
126
	if (ret < 0) {
127
		const int e = errno;
128
		fd.Close();
129
		throw MakeErrno(e, "AUDIO_SETINFO failed");
130 131 132
	}
}

133
void
134
SolarisOutput::Close() noexcept
135
{
136
	fd.Close();
137 138
}

139 140
size_t
SolarisOutput::Play(const void *chunk, size_t size)
141
{
142
	ssize_t nbytes = fd.Write(chunk, size);
143 144
	if (nbytes <= 0)
		throw MakeErrno("Write failed");
145 146 147 148

	return nbytes;
}

149
void
150
SolarisOutput::Cancel() noexcept
151
{
152 153 154
#if defined(AUDIO_FLUSH)
	ioctl(fd.Get(), AUDIO_FLUSH);
#elif defined(I_FLUSH)
155
	ioctl(fd.Get(), I_FLUSH);
156
#endif
157 158
}

159
const struct AudioOutputPlugin solaris_output_plugin = {
160 161
	"solaris",
	solaris_output_test_default_device,
162
	&SolarisOutput::Create,
163
	nullptr,
164
};