OssMixerPlugin.cxx 5.01 KB
Newer Older
1
/*
2
 * Copyright (C) 2003-2013 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13
 * 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.
14 15 16 17
 *
 * 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.
18
 */
19

20
#include "config.h"
21
#include "MixerInternal.hxx"
22
#include "OutputAPI.hxx"
23
#include "system/fd_util.h"
24
#include "util/ASCII.hxx"
25 26
#include "util/Error.hxx"
#include "util/Domain.hxx"
27
#include "Log.hxx"
28

29
#include <assert.h>
Max Kellermann's avatar
Max Kellermann committed
30
#include <string.h>
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>

#if defined(__OpenBSD__) || defined(__NetBSD__)
# include <soundcard.h>
#else /* !(defined(__OpenBSD__) || defined(__NetBSD__) */
# include <sys/soundcard.h>
#endif /* !(defined(__OpenBSD__) || defined(__NetBSD__) */

#define VOLUME_MIXER_OSS_DEFAULT		"/dev/mixer"

46
class OssMixer : public Mixer {
47 48 49
	const char *device;
	const char *control;

50 51
	int device_fd;
	int volume_control;
52 53

public:
54
	OssMixer():Mixer(oss_mixer_plugin) {}
55

56 57
	bool Configure(const config_param &param, Error &error);
	bool Open(Error &error);
58 59
	void Close();

60 61
	int GetVolume(Error &error);
	bool SetVolume(unsigned volume, Error &error);
62 63
};

64
static constexpr Domain oss_mixer_domain("oss_mixer");
65

66 67 68 69 70 71 72
static int
oss_find_mixer(const char *name)
{
	const char *labels[SOUND_MIXER_NRDEVICES] = SOUND_DEVICE_LABELS;
	size_t name_length = strlen(name);

	for (unsigned i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
73
		if (StringEqualsCaseASCII(name, labels[i], name_length) &&
74 75 76 77 78 79 80
		    (labels[i][name_length] == 0 ||
		     labels[i][name_length] == ' '))
			return i;
	}
	return -1;
}

81
inline bool
82
OssMixer::Configure(const config_param &param, Error &error)
83
{
84
	device = param.GetBlockValue("mixer_device", VOLUME_MIXER_OSS_DEFAULT);
85
	control = param.GetBlockValue("mixer_control");
86

87 88 89
	if (control != NULL) {
		volume_control = oss_find_mixer(control);
		if (volume_control < 0) {
90 91
			error.Format(oss_mixer_domain, 0,
				     "no such mixer control: %s", control);
92
			return false;
93 94
		}
	} else
95
		volume_control = SOUND_MIXER_PCM;
96

97
	return true;
98 99
}

100
static Mixer *
101
oss_mixer_init(gcc_unused void *ao, const config_param &param,
102
	       Error &error)
103
{
104
	OssMixer *om = new OssMixer();
105

106
	if (!om->Configure(param, error)) {
107 108 109 110 111
		delete om;
		return nullptr;
	}

	return om;
112 113
}

Viliam Mateicka's avatar
Viliam Mateicka committed
114
static void
115
oss_mixer_finish(Mixer *data)
116
{
117 118 119 120
	OssMixer *om = (OssMixer *) data;

	delete om;
}
121

122 123 124 125
void
OssMixer::Close()
{
	assert(device_fd >= 0);
126

127
	close(device_fd);
128 129
}

130
static void
131
oss_mixer_close(Mixer *data)
132
{
133 134 135
	OssMixer *om = (OssMixer *) data;
	om->Close();
}
136

137
inline bool
138
OssMixer::Open(Error &error)
139 140 141
{
	device_fd = open_cloexec(device, O_RDONLY, 0);
	if (device_fd < 0) {
142
		error.FormatErrno("failed to open %s", device);
143 144 145
		return false;
	}

146
	if (control) {
147 148
		int devmask = 0;

149
		if (ioctl(device_fd, SOUND_MIXER_READ_DEVMASK, &devmask) < 0) {
150
			error.SetErrno("READ_DEVMASK failed");
151
			Close();
152 153 154
			return false;
		}

155
		if (((1 << volume_control) & devmask) == 0) {
156 157 158
			error.Format(oss_mixer_domain, 0,
				     "mixer control \"%s\" not usable",
				     control);
159
			Close();
160 161 162
			return false;
		}
	}
163

164 165 166
	return true;
}

167
static bool
168
oss_mixer_open(Mixer *data, Error &error)
169 170 171
{
	OssMixer *om = (OssMixer *) data;

172
	return om->Open(error);
173 174 175
}

inline int
176
OssMixer::GetVolume(Error &error)
177
{
178 179
	int left, right, level;
	int ret;
180

181
	assert(device_fd >= 0);
182

183
	ret = ioctl(device_fd, MIXER_READ(volume_control), &level);
184
	if (ret < 0) {
185
		error.SetErrno("failed to read OSS volume");
186 187
		return false;
	}
188

189 190
	left = level & 0xff;
	right = (level & 0xff00) >> 8;
191

192
	if (left != right) {
193 194 195
		FormatWarning(oss_mixer_domain,
			      "volume for left and right is not the same, \"%i\" and "
			      "\"%i\"\n", left, right);
196 197
	}

198 199
	return left;
}
200

201
static int
202
oss_mixer_get_volume(Mixer *mixer, Error &error)
203 204
{
	OssMixer *om = (OssMixer *)mixer;
205
	return om->GetVolume(error);
206 207 208
}

inline bool
209
OssMixer::SetVolume(unsigned volume, Error &error)
210 211 212
{
	int level;
	int ret;
213

214
	assert(device_fd >= 0);
215
	assert(volume <= 100);
216 217 218

	level = (volume << 8) + volume;

219
	ret = ioctl(device_fd, MIXER_WRITE(volume_control), &level);
220
	if (ret < 0) {
221
		error.SetErrno("failed to set OSS volume");
222
		return false;
223
	}
224 225

	return true;
226
}
Viliam Mateicka's avatar
Viliam Mateicka committed
227

228
static bool
229
oss_mixer_set_volume(Mixer *mixer, unsigned volume, Error &error)
230 231
{
	OssMixer *om = (OssMixer *)mixer;
232
	return om->SetVolume(volume, error);
233 234
}

235
const struct mixer_plugin oss_mixer_plugin = {
236 237 238 239 240 241 242
	oss_mixer_init,
	oss_mixer_finish,
	oss_mixer_open,
	oss_mixer_close,
	oss_mixer_get_volume,
	oss_mixer_set_volume,
	true,
Viliam Mateicka's avatar
Viliam Mateicka committed
243
};