summaryrefslogtreecommitdiff
path: root/src/audioOutputs/audioOutput_oss.c
blob: e4b6291a936ec3a0b089bb02df8e07b2b0ed5663 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
/* the Music Player Daemon (MPD)
 * (c)2003-2006 by Warren Dukes (warren.dukes@gmail.com)
 * This project's homepage is: http://www.musicpd.org
 *
 * OSS audio output (c) 2004, 2005, 2006 by Eric Wong <eric@petta-tech.com>
 *                   and Warren Dukes <warren.dukes@gmail.com>
 *
 * 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
 */

#include "../audioOutput.h"

#include <stdlib.h>

#ifdef HAVE_OSS

#include "../conf.h"
#include "../log.h"
#include "../sig_handlers.h"

#include <string.h>
#include <assert.h>
#include <signal.h>

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

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

#ifdef WORDS_BIGENDIAN
# define	AFMT_S16_MPD	 AFMT_S16_BE
#else
# define	AFMT_S16_MPD	 AFMT_S16_LE
#endif				/* WORDS_BIGENDIAN */

typedef struct _OssData {
	int fd;
	char *device;
	int channels;
	int sampleRate;
	int bitFormat;
	int bits;
	int *supported[3];
	int numSupported[3];
	int *unsupported[3];
	int numUnsupported[3];
} OssData;

#define OSS_SUPPORTED		1
#define OSS_UNSUPPORTED		0
#define OSS_UNKNOWN		-1

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

static int getIndexForParam(int param)
{
	int index = 0;

	switch (param) {
	case SNDCTL_DSP_SPEED:
		index = OSS_RATE;
		break;
	case SNDCTL_DSP_CHANNELS:
		index = OSS_CHANNELS;
		break;
	case SNDCTL_DSP_SAMPLESIZE:
		index = OSS_BITS;
		break;
	}

	return index;
}

static int findSupportedParam(OssData * od, int param, int val)
{
	int i;
	int index = getIndexForParam(param);

	for (i = 0; i < od->numSupported[index]; i++) {
		if (od->supported[index][i] == val)
			return 1;
	}

	return 0;
}

static int canConvert(int index, int val)
{
	switch (index) {
	case OSS_BITS:
		if (val != 16)
			return 0;
		break;
	case OSS_CHANNELS:
		if (val != 2)
			return 0;
		break;
	}

	return 1;
}

static int getSupportedParam(OssData * od, int param, int val)
{
	int i;
	int index = getIndexForParam(param);
	int ret = -1;
	int least = val;
	int diff;

	for (i = 0; i < od->numSupported[index]; i++) {
		diff = od->supported[index][i] - val;
		if (diff < 0)
			diff = -diff;
		if (diff < least) {
			if (!canConvert(index, od->supported[index][i])) {
				continue;
			}
			least = diff;
			ret = od->supported[index][i];
		}
	}

	return ret;
}

static int findUnsupportedParam(OssData * od, int param, int val)
{
	int i;
	int index = getIndexForParam(param);

	for (i = 0; i < od->numUnsupported[index]; i++) {
		if (od->unsupported[index][i] == val)
			return 1;
	}

	return 0;
}

static void addSupportedParam(OssData * od, int param, int val)
{
	int index = getIndexForParam(param);

	od->numSupported[index]++;
	od->supported[index] = realloc(od->supported[index],
				       od->numSupported[index] * sizeof(int));
	od->supported[index][od->numSupported[index] - 1] = val;
}

static void addUnsupportedParam(OssData * od, int param, int val)
{
	int index = getIndexForParam(param);

	od->numUnsupported[index]++;
	od->unsupported[index] = realloc(od->unsupported[index],
					 od->numUnsupported[index] *
					 sizeof(int));
	od->unsupported[index][od->numUnsupported[index] - 1] = val;
}

static void removeSupportedParam(OssData * od, int param, int val)
{
	int i = 0;
	int j = 0;
	int index = getIndexForParam(param);

	for (i = 0; i < od->numSupported[index] - 1; i++) {
		if (od->supported[index][i] == val)
			j = 1;
		od->supported[index][i] = od->supported[index][i + j];
	}

	od->numSupported[index]--;
	od->supported[index] = realloc(od->supported[index],
				       od->numSupported[index] * sizeof(int));
}

static void removeUnsupportedParam(OssData * od, int param, int val)
{
	int i = 0;
	int j = 0;
	int index = getIndexForParam(param);

	for (i = 0; i < od->numUnsupported[index] - 1; i++) {
		if (od->unsupported[index][i] == val)
			j = 1;
		od->unsupported[index][i] = od->unsupported[index][i + j];
	}

	od->numUnsupported[index]--;
	od->unsupported[index] = realloc(od->unsupported[index],
					 od->numUnsupported[index] *
					 sizeof(int));
}

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

static void supportParam(OssData * od, int param, int val)
{
	int supported = isSupportedParam(od, param, val);

	if (supported == OSS_SUPPORTED)
		return;

	if (supported == OSS_UNSUPPORTED) {
		removeUnsupportedParam(od, param, val);
	}

	addSupportedParam(od, param, val);
}

static void unsupportParam(OssData * od, int param, int val)
{
	int supported = isSupportedParam(od, param, val);

	if (supported == OSS_UNSUPPORTED)
		return;

	if (supported == OSS_SUPPORTED) {
		removeSupportedParam(od, param, val);
	}

	addUnsupportedParam(od, param, val);
}

static OssData *newOssData(void)
{
	OssData *ret = malloc(sizeof(OssData));

	ret->device = NULL;
	ret->fd = -1;

	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;

	supportParam(ret, SNDCTL_DSP_SPEED, 48000);
	supportParam(ret, SNDCTL_DSP_SPEED, 44100);
	supportParam(ret, SNDCTL_DSP_CHANNELS, 2);
	supportParam(ret, SNDCTL_DSP_SAMPLESIZE, 16);

	return ret;
}

static void freeOssData(OssData * od)
{
	if (od->device)
		free(od->device);

	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]);

	free(od);
}

#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

static int oss_statDevice(char *device, int *stErrno)
{
	struct stat st;

	if (0 == stat(device, &st)) {
		if (!S_ISCHR(st.st_mode)) {
			return OSS_STAT_NOT_CHAR_DEV;
		}
	} else {
		*stErrno = errno;

		switch (errno) {
		case ENOENT:
		case ENOTDIR:
			return OSS_STAT_DOESN_T_EXIST;
		case EACCES:
			return OSS_STAT_NO_PERMS;
		default:
			return OSS_STAT_OTHER;
		}
	}

	return 0;
}

static int oss_testDefault(void)
{
	int fd;

	fd = open("/dev/sound/dsp", O_WRONLY);

	if (fd >= 0) {
		close(fd);
		return 0;
	}

	WARNING("Error opening OSS device \"/dev/sound/dsp\": %s\n",
		strerror(errno));

	fd = open("/dev/dsp", O_WRONLY);

	if (fd >= 0) {
		close(fd);
		return 0;
	}

	WARNING("Error opening OSS device \"/dev/dsp\": %s\n", strerror(errno));

	return -1;
}

static int oss_initDriver(AudioOutput * audioOutput, ConfigParam * param)
{
	BlockParam *bp = NULL;
	OssData *od;

	if (param)
		bp = getBlockParam(param, "device");

	od = newOssData();
	audioOutput->data = od;

	if (!bp) {
		int err[2];
		int ret[2];

		ret[0] = oss_statDevice("/dev/sound/dsp", err);
		ret[1] = oss_statDevice("/dev/dsp", err + 1);

		if (ret[0] == 0)
			od->device = strdup("/dev/sound/dsp");
		else if (ret[1] == 0)
			od->device = strdup("/dev/dsp");
		else {
			if (param) {
				ERROR("Error trying to open default OSS device "
				      "specified at line %i\n", param->line);
			} else {
				ERROR("Error trying to open default OSS "
				      "device\n");
			}

			if ((ret[0] == OSS_STAT_DOESN_T_EXIST) &&
			    (ret[1] == OSS_STAT_DOESN_T_EXIST)) {
				ERROR("Neither /dev/dsp nor /dev/sound/dsp "
				      "were found\n");
			} else if (ret[0] == OSS_STAT_NOT_CHAR_DEV) {
				ERROR("/dev/sound/dsp is not a char device");
			} else if (ret[1] == OSS_STAT_NOT_CHAR_DEV) {
				ERROR("/dev/dsp is not a char device");
			} else if (ret[0] == OSS_STAT_NO_PERMS) {
				ERROR("no permission to access /dev/sound/dsp");
			} else if (ret[1] == OSS_STAT_NO_PERMS) {
				ERROR("no permission to access /dev/dsp");
			} else if (ret[0] == OSS_STAT_OTHER) {
				ERROR("Error accessing /dev/sound/dsp: %s",
				      strerror(err[0]));
			} else if (ret[1] == OSS_STAT_OTHER) {
				ERROR("Error accessing /dev/dsp: %s",
				      strerror(err[1]));
			}

			exit(EXIT_FAILURE);
		}
	} else
		od->device = strdup(bp->value);

	return 0;
}

static void oss_finishDriver(AudioOutput * audioOutput)
{
	OssData *od = audioOutput->data;

	freeOssData(od);
}

static int setParam(OssData * od, int param, int *value)
{
	int val = *value;
	int copy;
	int supported = isSupportedParam(od, param, val);

	do {
		if (supported == OSS_UNSUPPORTED) {
			val = getSupportedParam(od, param, val);
			if (copy < 0)
				return -1;
		}
		copy = val;
		if (ioctl(od->fd, param, &copy)) {
			unsupportParam(od, param, val);
			supported = OSS_UNSUPPORTED;
		} else {
			if (supported == OSS_UNKNOWN) {
				supportParam(od, param, val);
				supported = OSS_SUPPORTED;
			}
			val = copy;
		}
	} while (supported == OSS_UNSUPPORTED);

	*value = val;

	return 0;
}

static void oss_close(OssData * od)
{
	if (od->fd >= 0)
		while (close(od->fd) && errno == EINTR) ;
	od->fd = -1;
}

static int oss_open(AudioOutput * audioOutput)
{
	int tmp;
	OssData *od = audioOutput->data;

	if ((od->fd = open(od->device, O_WRONLY)) < 0) {
		ERROR("Error opening OSS device \"%s\": %s\n", od->device,
		      strerror(errno));
		goto fail;
	}

	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));
		goto fail;
	}

	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));
		goto fail;
	}

	switch (od->bits) {
	case 8:
		tmp = AFMT_S8;
		break;
	case 16:
		tmp = AFMT_S16_MPD;
	}

	if (setParam(od, SNDCTL_DSP_SAMPLESIZE, &tmp)) {
		ERROR("OSS device \"%s\" does not support %i bit audio: %s\n",
		      od->device, tmp, strerror(errno));
		goto fail;
	}

	audioOutput->open = 1;

	return 0;

      fail:
	oss_close(od);
	audioOutput->open = 0;
	return -1;
}

static int oss_openDevice(AudioOutput * audioOutput)
{
	int ret = -1;
	OssData *od = audioOutput->data;
	AudioFormat *audioFormat = &audioOutput->outAudioFormat;

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

	if ((ret = oss_open(audioOutput)) < 0)
		return ret;

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

	DEBUG("oss device \"%s\" will be playing %i bit %i channel audio at "
	      "%i Hz\n", od->device, od->bits, od->channels, od->sampleRate);

	return ret;
}

static void oss_closeDevice(AudioOutput * audioOutput)
{
	OssData *od = audioOutput->data;

	oss_close(od);

	audioOutput->open = 0;
}

static void oss_dropBufferedAudio(AudioOutput * audioOutput)
{
	OssData *od = audioOutput->data;

	if (od->fd >= 0) {
		ioctl(od->fd, SNDCTL_DSP_RESET, 0);
		oss_close(od);
	}
}

static int oss_playAudio(AudioOutput * audioOutput, char *playChunk, int size)
{
	OssData *od = audioOutput->data;
	int ret;

	/* reopen the device since it was closed by dropBufferedAudio */
	if (od->fd < 0 && oss_open(audioOutput) < 0)
		return -1;

	while (size > 0) {
		ret = write(od->fd, playChunk, size);
		if (ret < 0) {
			if (errno == EINTR)
				continue;
			ERROR("closing oss device \"%s\" due to write error: "
			      "%s\n", od->device, strerror(errno));
			oss_closeDevice(audioOutput);
			return -1;
		}
		playChunk += ret;
		size -= ret;
	}

	return 0;
}

AudioOutputPlugin ossPlugin = {
	"oss",
	oss_testDefault,
	oss_initDriver,
	oss_finishDriver,
	oss_openDevice,
	oss_playAudio,
	oss_dropBufferedAudio,
	oss_closeDevice,
	NULL,			/* sendMetadataFunc */
};

#else				/* HAVE OSS */

DISABLED_AUDIO_OUTPUT_PLUGIN(ossPlugin)
#endif				/* HAVE_OSS */