SolarisOutputPlugin.cxx 4.16 KB
Newer Older
1
/*
2
 * Copyright 2003-2016 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 "config.h"
21
#include "SolarisOutputPlugin.hxx"
22
#include "../OutputAPI.hxx"
23
#include "system/fd_util.h"
24
#include "util/Error.hxx"
25 26 27 28 29 30 31 32

#include <sys/stropts.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>

33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
#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

52
struct SolarisOutput {
53
	AudioOutput base;
54

55 56 57 58
	/* configuration */
	const char *device;

	int fd;
59

60 61 62
	SolarisOutput()
		:base(solaris_output_plugin) {}

63 64
	bool Initialize(const ConfigBlock &block, Error &error_r) {
		return base.Configure(block, error_r);
65
	}
66 67 68 69 70 71 72 73 74 75 76
};

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

77
static AudioOutput *
78
solaris_output_init(const ConfigBlock &block, Error &error_r)
79
{
80
	SolarisOutput *so = new SolarisOutput();
81
	if (!so->Initialize(block, error_r)) {
82 83
		delete so;
		return nullptr;
84 85
	}

86
	so->device = block.GetBlockValue("device", "/dev/audio");
87

88
	return &so->base;
89 90 91
}

static void
92
solaris_output_finish(AudioOutput *ao)
93
{
94
	SolarisOutput *so = (SolarisOutput *)ao;
95

96
	delete so;
97 98 99
}

static bool
100
solaris_output_open(AudioOutput *ao, AudioFormat &audio_format,
101
		    Error &error)
102
{
103
	SolarisOutput *so = (SolarisOutput *)ao;
104 105 106 107 108
	struct audio_info info;
	int ret, flags;

	/* support only 16 bit mono/stereo for now; nothing else has
	   been tested */
109
	audio_format.format = SampleFormat::S16;
110 111 112

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

113
	so->fd = open_cloexec(so->device, O_WRONLY|O_NONBLOCK, 0);
114
	if (so->fd < 0) {
115 116
		error.FormatErrno("Failed to open %s",
				  so->device);
117 118 119 120 121 122 123 124 125 126 127 128 129
		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) {
130
		error.SetErrno("AUDIO_GETINFO failed");
131 132 133 134
		close(so->fd);
		return false;
	}

135 136
	info.play.sample_rate = audio_format.sample_rate;
	info.play.channels = audio_format.channels;
137
	info.play.precision = 16;
138 139 140 141
	info.play.encoding = AUDIO_ENCODING_LINEAR;

	ret = ioctl(so->fd, AUDIO_SETINFO, &info);
	if (ret < 0) {
142
		error.SetErrno("AUDIO_SETINFO failed");
143 144 145 146 147 148 149 150
		close(so->fd);
		return false;
	}

	return true;
}

static void
151
solaris_output_close(AudioOutput *ao)
152
{
153
	SolarisOutput *so = (SolarisOutput *)ao;
154 155 156 157 158

	close(so->fd);
}

static size_t
159
solaris_output_play(AudioOutput *ao, const void *chunk, size_t size,
160
		    Error &error)
161
{
162
	SolarisOutput *so = (SolarisOutput *)ao;
163 164 165 166
	ssize_t nbytes;

	nbytes = write(so->fd, chunk, size);
	if (nbytes <= 0) {
167
		error.SetErrno("Write failed");
168 169 170 171 172 173 174
		return 0;
	}

	return nbytes;
}

static void
175
solaris_output_cancel(AudioOutput *ao)
176
{
177
	SolarisOutput *so = (SolarisOutput *)ao;
178 179 180 181

	ioctl(so->fd, I_FLUSH);
}

182
const struct AudioOutputPlugin solaris_output_plugin = {
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
	"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,
198
};