OssMixerPlugin.cxx 4.14 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2017 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/Block.hxx"
23
#include "system/fd_util.h"
24
#include "system/Error.hxx"
25
#include "util/ASCII.hxx"
26
#include "util/Domain.hxx"
27
#include "util/RuntimeError.hxx"
28
#include "Log.hxx"
29

30
#include <assert.h>
Max Kellermann's avatar
Max Kellermann committed
31
#include <string.h>
32 33 34 35 36 37 38 39 40 41 42 43 44
#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"

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

49 50
	int device_fd;
	int volume_control;
51 52

public:
53 54 55 56
	OssMixer(MixerListener &_listener, const ConfigBlock &block)
		:Mixer(oss_mixer_plugin, _listener) {
		Configure(block);
	}
57

58
	void Configure(const ConfigBlock &block);
59

60
	/* virtual methods from class Mixer */
61 62 63 64
	void Open() override;
	void Close() override;
	int GetVolume() override;
	void SetVolume(unsigned volume) override;
65 66
};

67
static constexpr Domain oss_mixer_domain("oss_mixer");
68

69 70 71 72 73 74 75
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++) {
76
		if (StringEqualsCaseASCII(name, labels[i], name_length) &&
77 78 79 80 81 82 83
		    (labels[i][name_length] == 0 ||
		     labels[i][name_length] == ' '))
			return i;
	}
	return -1;
}

84 85
inline void
OssMixer::Configure(const ConfigBlock &block)
86
{
87 88
	device = block.GetBlockValue("mixer_device", VOLUME_MIXER_OSS_DEFAULT);
	control = block.GetBlockValue("mixer_control");
89

90 91
	if (control != NULL) {
		volume_control = oss_find_mixer(control);
92 93 94
		if (volume_control < 0)
			throw FormatRuntimeError("no such mixer control: %s",
						 control);
95
	} else
96
		volume_control = SOUND_MIXER_PCM;
97 98
}

99
static Mixer *
100
oss_mixer_init(gcc_unused EventLoop &event_loop, gcc_unused AudioOutput &ao,
101
	       MixerListener &listener,
102
	       const ConfigBlock &block)
103
{
104
	return new OssMixer(listener, block);
105 106
}

107 108 109 110
void
OssMixer::Close()
{
	assert(device_fd >= 0);
111

112
	close(device_fd);
113 114
}

115 116
void
OssMixer::Open()
117 118
{
	device_fd = open_cloexec(device, O_RDONLY, 0);
119 120
	if (device_fd < 0)
		throw FormatErrno("failed to open %s", device);
121

122 123 124
	try {
		if (control) {
			int devmask = 0;
125

126 127
			if (ioctl(device_fd, SOUND_MIXER_READ_DEVMASK, &devmask) < 0)
				throw MakeErrno("READ_DEVMASK failed");
128

129 130 131
			if (((1 << volume_control) & devmask) == 0)
				throw FormatErrno("mixer control \"%s\" not usable",
						  control);
132
		}
133 134 135
	} catch (...) {
		Close();
		throw;
136 137 138
	}
}

139
int
140
OssMixer::GetVolume()
141
{
142 143
	int left, right, level;
	int ret;
144

145
	assert(device_fd >= 0);
146

147
	ret = ioctl(device_fd, MIXER_READ(volume_control), &level);
148 149
	if (ret < 0)
		throw MakeErrno("failed to read OSS volume");
150

151 152
	left = level & 0xff;
	right = (level & 0xff00) >> 8;
153

154
	if (left != right) {
155 156 157
		FormatWarning(oss_mixer_domain,
			      "volume for left and right is not the same, \"%i\" and "
			      "\"%i\"\n", left, right);
158 159
	}

160 161
	return left;
}
162

163 164
void
OssMixer::SetVolume(unsigned volume)
165 166
{
	int level;
167

168
	assert(device_fd >= 0);
169
	assert(volume <= 100);
170 171 172

	level = (volume << 8) + volume;

173 174
	if (ioctl(device_fd, MIXER_WRITE(volume_control), &level) < 0)
		throw MakeErrno("failed to set OSS volume");
175
}
Viliam Mateicka's avatar
Viliam Mateicka committed
176

177
const MixerPlugin oss_mixer_plugin = {
178 179
	oss_mixer_init,
	true,
Viliam Mateicka's avatar
Viliam Mateicka committed
180
};