audioOutput_oss.c 12.3 KB
Newer Older
1
/* the Music Player Daemon (MPD)
2
 * Copyright (C) 2003-2007 by Warren Dukes (warren.dukes@gmail.com)
3 4
 * This project's homepage is: http://www.musicpd.org
 *
5
 * OSS audio output (c) 2004, 2005, 2006, 2007 by Eric Wong <eric@petta-tech.com>
6
 *                   and Warren Dukes <warren.dukes@gmail.com>
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 *
 * 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

22
#include "../output_api.h"
23 24 25

#ifdef HAVE_OSS

26
#include "../utils.h"
27
#include "../log.h"
28 29 30

#if defined(__OpenBSD__) || defined(__NetBSD__)
# include <soundcard.h>
Avuton Olrich's avatar
Avuton Olrich committed
31
#else /* !(defined(__OpenBSD__) || defined(__NetBSD__) */
32
# include <sys/soundcard.h>
Avuton Olrich's avatar
Avuton Olrich committed
33
#endif /* !(defined(__OpenBSD__) || defined(__NetBSD__) */
34

35 36 37 38
#ifdef WORDS_BIGENDIAN
# define	AFMT_S16_MPD	 AFMT_S16_BE
#else
# define	AFMT_S16_MPD	 AFMT_S16_LE
Avuton Olrich's avatar
Avuton Olrich committed
39
#endif /* WORDS_BIGENDIAN */
40

41
typedef struct _OssData {
42
	int fd;
43
	const char *device;
44 45 46 47
	int channels;
	int sampleRate;
	int bitFormat;
	int bits;
Avuton Olrich's avatar
Avuton Olrich committed
48
	int *supported[3];
49
	int numSupported[3];
Avuton Olrich's avatar
Avuton Olrich committed
50
	int *unsupported[3];
51
	int numUnsupported[3];
52 53
} OssData;

54 55 56 57 58 59 60 61
#define OSS_SUPPORTED		1
#define OSS_UNSUPPORTED		0
#define OSS_UNKNOWN		-1

#define OSS_RATE		0
#define OSS_CHANNELS		1
#define OSS_BITS		2

Avuton Olrich's avatar
Avuton Olrich committed
62 63
static int getIndexForParam(int param)
{
Max Kellermann's avatar
Max Kellermann committed
64
	int idx = 0;
Avuton Olrich's avatar
Avuton Olrich committed
65 66

	switch (param) {
67
	case SNDCTL_DSP_SPEED:
Max Kellermann's avatar
Max Kellermann committed
68
		idx = OSS_RATE;
69 70
		break;
	case SNDCTL_DSP_CHANNELS:
Max Kellermann's avatar
Max Kellermann committed
71
		idx = OSS_CHANNELS;
72 73
		break;
	case SNDCTL_DSP_SAMPLESIZE:
Max Kellermann's avatar
Max Kellermann committed
74
		idx = OSS_BITS;
75 76 77
		break;
	}

Max Kellermann's avatar
Max Kellermann committed
78
	return idx;
79 80
}

Avuton Olrich's avatar
Avuton Olrich committed
81 82
static int findSupportedParam(OssData * od, int param, int val)
{
83
	int i;
Max Kellermann's avatar
Max Kellermann committed
84
	int idx = getIndexForParam(param);
Avuton Olrich's avatar
Avuton Olrich committed
85

Max Kellermann's avatar
Max Kellermann committed
86 87
	for (i = 0; i < od->numSupported[idx]; i++) {
		if (od->supported[idx][i] == val)
Avuton Olrich's avatar
Avuton Olrich committed
88
			return 1;
89 90 91 92 93
	}

	return 0;
}

Max Kellermann's avatar
Max Kellermann committed
94
static int canConvert(int idx, int val)
Avuton Olrich's avatar
Avuton Olrich committed
95
{
Max Kellermann's avatar
Max Kellermann committed
96
	switch (idx) {
97
	case OSS_BITS:
Avuton Olrich's avatar
Avuton Olrich committed
98 99
		if (val != 16)
			return 0;
100 101
		break;
	case OSS_CHANNELS:
Avuton Olrich's avatar
Avuton Olrich committed
102 103
		if (val != 2)
			return 0;
104 105 106 107 108 109
		break;
	}

	return 1;
}

Avuton Olrich's avatar
Avuton Olrich committed
110 111
static int getSupportedParam(OssData * od, int param, int val)
{
112
	int i;
Max Kellermann's avatar
Max Kellermann committed
113
	int idx = getIndexForParam(param);
114 115 116
	int ret = -1;
	int least = val;
	int diff;
Avuton Olrich's avatar
Avuton Olrich committed
117

Max Kellermann's avatar
Max Kellermann committed
118 119
	for (i = 0; i < od->numSupported[idx]; i++) {
		diff = od->supported[idx][i] - val;
Avuton Olrich's avatar
Avuton Olrich committed
120 121 122
		if (diff < 0)
			diff = -diff;
		if (diff < least) {
Max Kellermann's avatar
Max Kellermann committed
123
			if (!canConvert(idx, od->supported[idx][i])) {
124 125 126
				continue;
			}
			least = diff;
Max Kellermann's avatar
Max Kellermann committed
127
			ret = od->supported[idx][i];
128 129 130 131 132 133
		}
	}

	return ret;
}

Avuton Olrich's avatar
Avuton Olrich committed
134 135
static int findUnsupportedParam(OssData * od, int param, int val)
{
136
	int i;
Max Kellermann's avatar
Max Kellermann committed
137
	int idx = getIndexForParam(param);
Avuton Olrich's avatar
Avuton Olrich committed
138

Max Kellermann's avatar
Max Kellermann committed
139 140
	for (i = 0; i < od->numUnsupported[idx]; i++) {
		if (od->unsupported[idx][i] == val)
Avuton Olrich's avatar
Avuton Olrich committed
141
			return 1;
142 143 144 145 146
	}

	return 0;
}

Avuton Olrich's avatar
Avuton Olrich committed
147 148
static void addSupportedParam(OssData * od, int param, int val)
{
Max Kellermann's avatar
Max Kellermann committed
149
	int idx = getIndexForParam(param);
150

Max Kellermann's avatar
Max Kellermann committed
151 152 153 154
	od->numSupported[idx]++;
	od->supported[idx] = xrealloc(od->supported[idx],
				      od->numSupported[idx] * sizeof(int));
	od->supported[idx][od->numSupported[idx] - 1] = val;
155 156
}

Avuton Olrich's avatar
Avuton Olrich committed
157 158
static void addUnsupportedParam(OssData * od, int param, int val)
{
Max Kellermann's avatar
Max Kellermann committed
159
	int idx = getIndexForParam(param);
160

Max Kellermann's avatar
Max Kellermann committed
161 162 163 164 165
	od->numUnsupported[idx]++;
	od->unsupported[idx] = xrealloc(od->unsupported[idx],
					od->numUnsupported[idx] *
					sizeof(int));
	od->unsupported[idx][od->numUnsupported[idx] - 1] = val;
166 167
}

Avuton Olrich's avatar
Avuton Olrich committed
168 169
static void removeSupportedParam(OssData * od, int param, int val)
{
170
	int i;
171
	int j = 0;
Max Kellermann's avatar
Max Kellermann committed
172
	int idx = getIndexForParam(param);
173

Max Kellermann's avatar
Max Kellermann committed
174 175
	for (i = 0; i < od->numSupported[idx] - 1; i++) {
		if (od->supported[idx][i] == val)
Avuton Olrich's avatar
Avuton Olrich committed
176
			j = 1;
Max Kellermann's avatar
Max Kellermann committed
177
		od->supported[idx][i] = od->supported[idx][i + j];
178 179
	}

Max Kellermann's avatar
Max Kellermann committed
180 181 182
	od->numSupported[idx]--;
	od->supported[idx] = xrealloc(od->supported[idx],
				      od->numSupported[idx] * sizeof(int));
183 184
}

Avuton Olrich's avatar
Avuton Olrich committed
185 186
static void removeUnsupportedParam(OssData * od, int param, int val)
{
187
	int i;
188
	int j = 0;
Max Kellermann's avatar
Max Kellermann committed
189
	int idx = getIndexForParam(param);
190

Max Kellermann's avatar
Max Kellermann committed
191 192
	for (i = 0; i < od->numUnsupported[idx] - 1; i++) {
		if (od->unsupported[idx][i] == val)
Avuton Olrich's avatar
Avuton Olrich committed
193
			j = 1;
Max Kellermann's avatar
Max Kellermann committed
194
		od->unsupported[idx][i] = od->unsupported[idx][i + j];
195 196
	}

Max Kellermann's avatar
Max Kellermann committed
197 198 199 200
	od->numUnsupported[idx]--;
	od->unsupported[idx] = xrealloc(od->unsupported[idx],
					od->numUnsupported[idx] *
					sizeof(int));
201 202
}

Avuton Olrich's avatar
Avuton Olrich committed
203 204 205 206 207 208
static int isSupportedParam(OssData * od, int param, int val)
{
	if (findSupportedParam(od, param, val))
		return OSS_SUPPORTED;
	if (findUnsupportedParam(od, param, val))
		return OSS_UNSUPPORTED;
209 210 211
	return OSS_UNKNOWN;
}

Avuton Olrich's avatar
Avuton Olrich committed
212 213
static void supportParam(OssData * od, int param, int val)
{
214 215
	int supported = isSupportedParam(od, param, val);

Avuton Olrich's avatar
Avuton Olrich committed
216 217
	if (supported == OSS_SUPPORTED)
		return;
218

Avuton Olrich's avatar
Avuton Olrich committed
219
	if (supported == OSS_UNSUPPORTED) {
220 221 222 223 224 225
		removeUnsupportedParam(od, param, val);
	}

	addSupportedParam(od, param, val);
}

Avuton Olrich's avatar
Avuton Olrich committed
226 227
static void unsupportParam(OssData * od, int param, int val)
{
228 229
	int supported = isSupportedParam(od, param, val);

Avuton Olrich's avatar
Avuton Olrich committed
230 231
	if (supported == OSS_UNSUPPORTED)
		return;
232

Avuton Olrich's avatar
Avuton Olrich committed
233
	if (supported == OSS_SUPPORTED) {
234 235 236 237 238 239
		removeSupportedParam(od, param, val);
	}

	addUnsupportedParam(od, param, val);
}

Avuton Olrich's avatar
Avuton Olrich committed
240 241
static OssData *newOssData(void)
{
242
	OssData *ret = xmalloc(sizeof(OssData));
243

244 245
	ret->device = NULL;
	ret->fd = -1;
246

247 248 249 250 251 252 253 254 255 256 257 258 259 260
	ret->supported[OSS_RATE] = NULL;
	ret->supported[OSS_CHANNELS] = NULL;
	ret->supported[OSS_BITS] = NULL;
	ret->unsupported[OSS_RATE] = NULL;
	ret->unsupported[OSS_CHANNELS] = NULL;
	ret->unsupported[OSS_BITS] = NULL;

	ret->numSupported[OSS_RATE] = 0;
	ret->numSupported[OSS_CHANNELS] = 0;
	ret->numSupported[OSS_BITS] = 0;
	ret->numUnsupported[OSS_RATE] = 0;
	ret->numUnsupported[OSS_CHANNELS] = 0;
	ret->numUnsupported[OSS_BITS] = 0;

Avuton Olrich's avatar
Avuton Olrich committed
261 262 263 264
	supportParam(ret, SNDCTL_DSP_SPEED, 48000);
	supportParam(ret, SNDCTL_DSP_SPEED, 44100);
	supportParam(ret, SNDCTL_DSP_CHANNELS, 2);
	supportParam(ret, SNDCTL_DSP_SAMPLESIZE, 16);
265

266 267 268
	return ret;
}

Avuton Olrich's avatar
Avuton Olrich committed
269 270 271 272 273 274 275 276 277 278 279 280 281 282
static void freeOssData(OssData * od)
{
	if (od->supported[OSS_RATE])
		free(od->supported[OSS_RATE]);
	if (od->supported[OSS_CHANNELS])
		free(od->supported[OSS_CHANNELS]);
	if (od->supported[OSS_BITS])
		free(od->supported[OSS_BITS]);
	if (od->unsupported[OSS_RATE])
		free(od->unsupported[OSS_RATE]);
	if (od->unsupported[OSS_CHANNELS])
		free(od->unsupported[OSS_CHANNELS]);
	if (od->unsupported[OSS_BITS])
		free(od->unsupported[OSS_BITS]);
283

284 285 286
	free(od);
}

287 288 289 290 291 292
#define OSS_STAT_NO_ERROR 	0
#define OSS_STAT_NOT_CHAR_DEV	-1
#define OSS_STAT_NO_PERMS	-2
#define OSS_STAT_DOESN_T_EXIST	-3
#define OSS_STAT_OTHER		-4

293
static int oss_statDevice(const char *device, int *stErrno)
Avuton Olrich's avatar
Avuton Olrich committed
294
{
295
	struct stat st;
Avuton Olrich's avatar
Avuton Olrich committed
296 297 298

	if (0 == stat(device, &st)) {
		if (!S_ISCHR(st.st_mode)) {
299 300
			return OSS_STAT_NOT_CHAR_DEV;
		}
Avuton Olrich's avatar
Avuton Olrich committed
301
	} else {
302 303
		*stErrno = errno;

Avuton Olrich's avatar
Avuton Olrich committed
304
		switch (errno) {
305 306 307 308 309 310 311 312 313 314 315 316 317
		case ENOENT:
		case ENOTDIR:
			return OSS_STAT_DOESN_T_EXIST;
		case EACCES:
			return OSS_STAT_NO_PERMS;
		default:
			return OSS_STAT_OTHER;
		}
	}

	return 0;
}

318 319
static const char *default_devices[] = { "/dev/sound/dsp", "/dev/dsp" };

Avuton Olrich's avatar
Avuton Olrich committed
320 321
static int oss_testDefault(void)
{
322
	int fd, i;
323

324 325 326 327 328 329 330
	for (i = ARRAY_SIZE(default_devices); --i >= 0; ) {
		if ((fd = open(default_devices[i], O_WRONLY)) >= 0) {
			xclose(fd);
			return 0;
		}
		WARNING("Error opening OSS device \"%s\": %s\n",
		        default_devices[i], strerror(errno));
331 332
	}

333 334
	return -1;
}
335

336 337
static int oss_open_default(mpd_unused struct audio_output *ao,
			    ConfigParam *param, OssData *od)
338 339 340 341
{
	int i;
	int err[ARRAY_SIZE(default_devices)];
	int ret[ARRAY_SIZE(default_devices)];
342

343 344 345 346 347 348
	for (i = ARRAY_SIZE(default_devices); --i >= 0; ) {
		ret[i] = oss_statDevice(default_devices[i], &err[i]);
		if (ret[i] == 0) {
			od->device = default_devices[i];
			return 0;
		}
349 350
	}

351
	if (param)
352
		ERROR("error trying to open specified OSS device"
353 354 355 356
	              " at line %i\n", param->line);
	else
		ERROR("error trying to open default OSS device\n");

357
	for (i = ARRAY_SIZE(default_devices); --i >= 0; ) {
358 359 360 361 362 363 364 365 366 367 368 369
		const char *dev = default_devices[i];
		switch(ret[i]) {
		case OSS_STAT_DOESN_T_EXIST:
			ERROR("%s not found\n", dev);
			break;
		case OSS_STAT_NOT_CHAR_DEV:
			ERROR("%s is not a character device\n", dev);
			break;
		case OSS_STAT_NO_PERMS:
			ERROR("%s: permission denied\n", dev);
			break;
		default:
370
			ERROR("Error accessing %s: %s\n", dev, strerror(err[i]));
371 372 373 374
		}
	}
	exit(EXIT_FAILURE);
	return 0; /* some compilers can be dumb... */
375 376
}

377 378
static int oss_initDriver(struct audio_output *audioOutput,
			  ConfigParam * param)
Avuton Olrich's avatar
Avuton Olrich committed
379
{
380
	OssData *od = newOssData();
381
	audioOutput->data = od;
382 383 384 385 386
	if (param) {
		BlockParam *bp = getBlockParam(param, "device");
		if (bp) {
			od->device = bp->value;
			return 0;
387
		}
388 389
	}
	return oss_open_default(audioOutput, param, od);
390 391
}

392
static void oss_finishDriver(struct audio_output *audioOutput)
Avuton Olrich's avatar
Avuton Olrich committed
393 394
{
	OssData *od = audioOutput->data;
395 396 397 398

	freeOssData(od);
}

Avuton Olrich's avatar
Avuton Olrich committed
399 400
static int setParam(OssData * od, int param, int *value)
{
401 402 403 404 405
	int val = *value;
	int copy;
	int supported = isSupportedParam(od, param, val);

	do {
Avuton Olrich's avatar
Avuton Olrich committed
406
		if (supported == OSS_UNSUPPORTED) {
407
			val = getSupportedParam(od, param, val);
Avuton Olrich's avatar
Avuton Olrich committed
408 409
			if (copy < 0)
				return -1;
410 411
		}
		copy = val;
Avuton Olrich's avatar
Avuton Olrich committed
412
		if (ioctl(od->fd, param, &copy)) {
413 414
			unsupportParam(od, param, val);
			supported = OSS_UNSUPPORTED;
Avuton Olrich's avatar
Avuton Olrich committed
415 416
		} else {
			if (supported == OSS_UNKNOWN) {
417 418 419 420 421
				supportParam(od, param, val);
				supported = OSS_SUPPORTED;
			}
			val = copy;
		}
Avuton Olrich's avatar
Avuton Olrich committed
422
	} while (supported == OSS_UNSUPPORTED);
423 424 425 426 427 428

	*value = val;

	return 0;
}

429 430
static void oss_close(OssData * od)
{
Avuton Olrich's avatar
Avuton Olrich committed
431 432
	if (od->fd >= 0)
		while (close(od->fd) && errno == EINTR) ;
433 434 435
	od->fd = -1;
}

436
static int oss_open(struct audio_output *audioOutput)
Avuton Olrich's avatar
Avuton Olrich committed
437
{
438
	int tmp;
Avuton Olrich's avatar
Avuton Olrich committed
439
	OssData *od = audioOutput->data;
440

Avuton Olrich's avatar
Avuton Olrich committed
441 442 443
	if ((od->fd = open(od->device, O_WRONLY)) < 0) {
		ERROR("Error opening OSS device \"%s\": %s\n", od->device,
		      strerror(errno));
444 445 446
		goto fail;
	}

Avuton Olrich's avatar
Avuton Olrich committed
447 448 449
	if (setParam(od, SNDCTL_DSP_CHANNELS, &od->channels)) {
		ERROR("OSS device \"%s\" does not support %i channels: %s\n",
		      od->device, od->channels, strerror(errno));
450 451
		goto fail;
	}
452

Avuton Olrich's avatar
Avuton Olrich committed
453 454 455
	if (setParam(od, SNDCTL_DSP_SPEED, &od->sampleRate)) {
		ERROR("OSS device \"%s\" does not support %i Hz audio: %s\n",
		      od->device, od->sampleRate, strerror(errno));
456 457
		goto fail;
	}
458

Avuton Olrich's avatar
Avuton Olrich committed
459
	switch (od->bits) {
460 461 462 463 464 465 466
	case 8:
		tmp = AFMT_S8;
		break;
	case 16:
		tmp = AFMT_S16_MPD;
	}

Avuton Olrich's avatar
Avuton Olrich committed
467 468 469
	if (setParam(od, SNDCTL_DSP_SAMPLESIZE, &tmp)) {
		ERROR("OSS device \"%s\" does not support %i bit audio: %s\n",
		      od->device, tmp, strerror(errno));
470 471
		goto fail;
	}
472 473

	audioOutput->open = 1;
474 475

	return 0;
476

477
fail:
478
	oss_close(od);
479 480 481 482
	audioOutput->open = 0;
	return -1;
}

483
static int oss_openDevice(struct audio_output *audioOutput)
484
{
485
	int ret;
Avuton Olrich's avatar
Avuton Olrich committed
486
	OssData *od = audioOutput->data;
487
	struct audio_format *audioFormat = &audioOutput->outAudioFormat;
488

Max Kellermann's avatar
Max Kellermann committed
489
	od->channels = (mpd_sint8)audioFormat->channels;
490
	od->sampleRate = audioFormat->sampleRate;
Max Kellermann's avatar
Max Kellermann committed
491
	od->bits = (mpd_sint8)audioFormat->bits;
492

493 494
	if ((ret = oss_open(audioOutput)) < 0)
		return ret;
495 496 497 498 499

	audioFormat->channels = od->channels;
	audioFormat->sampleRate = od->sampleRate;
	audioFormat->bits = od->bits;

500
	DEBUG("oss device \"%s\" will be playing %i bit %i channel audio at "
Avuton Olrich's avatar
Avuton Olrich committed
501
	      "%i Hz\n", od->device, od->bits, od->channels, od->sampleRate);
502 503

	return ret;
504 505
}

506
static void oss_closeDevice(struct audio_output *audioOutput)
Avuton Olrich's avatar
Avuton Olrich committed
507 508
{
	OssData *od = audioOutput->data;
509

510
	oss_close(od);
511 512

	audioOutput->open = 0;
513 514
}

515
static void oss_dropBufferedAudio(struct audio_output *audioOutput)
Avuton Olrich's avatar
Avuton Olrich committed
516 517
{
	OssData *od = audioOutput->data;
518

Avuton Olrich's avatar
Avuton Olrich committed
519
	if (od->fd >= 0) {
520
		ioctl(od->fd, SNDCTL_DSP_RESET, 0);
521
		oss_close(od);
522 523 524
	}
}

525
static int oss_playAudio(struct audio_output *audioOutput,
526
			 const char *playChunk, size_t size)
527
{
Avuton Olrich's avatar
Avuton Olrich committed
528
	OssData *od = audioOutput->data;
529
	ssize_t ret;
530

531
	/* reopen the device since it was closed by dropBufferedAudio */
Avuton Olrich's avatar
Avuton Olrich committed
532
	if (od->fd < 0 && oss_open(audioOutput) < 0)
533
		return -1;
534

535
	while (size > 0) {
536
		ret = write(od->fd, playChunk, size);
Avuton Olrich's avatar
Avuton Olrich committed
537 538 539
		if (ret < 0) {
			if (errno == EINTR)
				continue;
Warren Dukes's avatar
Warren Dukes committed
540
			ERROR("closing oss device \"%s\" due to write error: "
Avuton Olrich's avatar
Avuton Olrich committed
541
			      "%s\n", od->device, strerror(errno));
542
			oss_closeDevice(audioOutput);
543 544
			return -1;
		}
545 546
		playChunk += ret;
		size -= ret;
547 548 549 550 551
	}

	return 0;
}

552
const struct audio_output_plugin ossPlugin = {
553
	"oss",
554
	oss_testDefault,
555 556 557 558
	oss_initDriver,
	oss_finishDriver,
	oss_openDevice,
	oss_playAudio,
559
	oss_dropBufferedAudio,
560
	oss_closeDevice,
Avuton Olrich's avatar
Avuton Olrich committed
561
	NULL,	/* sendMetadataFunc */
562 563
};

Avuton Olrich's avatar
Avuton Olrich committed
564
#else /* HAVE OSS */
565

566
DISABLED_AUDIO_OUTPUT_PLUGIN(ossPlugin)
Avuton Olrich's avatar
Avuton Olrich committed
567
#endif /* HAVE_OSS */