OssMixerPlugin.cxx 5.01 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2014 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"
Max Kellermann's avatar
Max Kellermann committed
21
#include "mixer/MixerInternal.hxx"
22
#include "config/ConfigData.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
#include <sys/ioctl.h>
#include <fcntl.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"

44
class OssMixer : public Mixer {
45 46 47
	const char *device;
	const char *control;

48 49
	int device_fd;
	int volume_control;
50 51

public:
52
	OssMixer():Mixer(oss_mixer_plugin) {}
53

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

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

62
static constexpr Domain oss_mixer_domain("oss_mixer");
63

64 65 66 67 68 69 70
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++) {
71
		if (StringEqualsCaseASCII(name, labels[i], name_length) &&
72 73 74 75 76 77 78
		    (labels[i][name_length] == 0 ||
		     labels[i][name_length] == ' '))
			return i;
	}
	return -1;
}

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

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

95
	return true;
96 97
}

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

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

	return om;
111 112
}

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

	delete om;
}
120

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

126
	close(device_fd);
127 128
}

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

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

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

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

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

163 164 165
	return true;
}

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

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

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

180
	assert(device_fd >= 0);
181

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

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

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

197 198
	return left;
}
199

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

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

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

	level = (volume << 8) + volume;

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

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

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

234
const MixerPlugin oss_mixer_plugin = {
235 236 237 238 239 240 241
	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
242
};