File: oss.c

package info (click to toggle)
moc 1%3A2.6.0~svn-r2994-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 2,512 kB
  • sloc: ansic: 31,737; sh: 929; cpp: 487; makefile: 241
file content (481 lines) | stat: -rw-r--r-- 10,405 bytes parent folder | download | duplicates (5)
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
/*
 * MOC - music on console
 * Copyright (C) 2003 - 2005 Damian Pietras <daper@daper.net>
 *
 * 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.
 *
 */

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include <stdio.h>
#include <sys/ioctl.h>
#include <sys/time.h>
#include <sys/types.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <strings.h>
#include <unistd.h>
#include <assert.h>

#ifdef HAVE_SYS_SOUNDCARD_H
# include <sys/soundcard.h>
#else
# include <soundcard.h>
#endif

#include "common.h"
#include "server.h"
#include "audio.h"
#include "log.h"
#include "options.h"

#if OSS_VERSION >= 0x40000 || SOUND_VERSION >= 0x40000
#define OSSv4_MIXER
#else
#define OSSv3_MIXER
#endif

static bool started = false;
static int volatile dsp_fd = -1;
#ifdef OSSv3_MIXER
static int mixer_fd = -1;
static int mixer_channel1 = -1;
static int mixer_channel2 = -1;
static int mixer_channel_current;
#endif

static struct sound_params params = { 0, 0, 0 };

static const struct {
	const char *name;
	const int num;
} mixer_channels[] = {
	{ "pcm", SOUND_MIXER_PCM },
	{ "master", SOUND_MIXER_VOLUME },
	{ "speaker", SOUND_MIXER_SPEAKER }
};

#define MIXER_CHANNELS_NUM	(ARRAY_SIZE(mixer_channels))

static int open_dev ()
{
	if ((dsp_fd = open (options_get_str ("OSSDevice"), O_WRONLY)) == -1) {
		char *err = xstrerror (errno);
		error ("Can't open %s: %s", options_get_str ("OSSDevice"), err);
		free (err);
		return 0;
	}

	logit ("Audio device opened");

	return 1;
}

/* Fill caps with the device capabilities.  Return 0 on error. */
static int set_capabilities (struct output_driver_caps *caps)
{
	int format_mask;

	if (!open_dev ()) {
		error ("Can't open the device.");
		return 0;
	}

	if (ioctl (dsp_fd, SNDCTL_DSP_GETFMTS, &format_mask) == -1) {
		error_errno ("Can't get supported audio formats", errno);
		close (dsp_fd);
		return 0;
	}

	caps->formats = 0;
	if (format_mask & AFMT_S8)
		caps->formats |= SFMT_S8;
	if (format_mask & AFMT_U8)
		caps->formats |= SFMT_U8;

	if (format_mask & AFMT_S16_LE)
		caps->formats |= SFMT_S16 | SFMT_LE;
	if (format_mask & AFMT_S16_BE)
		caps->formats |= SFMT_S16 | SFMT_BE;

#if defined(AFMT_S32_LE) && defined(AFMT_S32_BE)
	if (format_mask & AFMT_S32_LE)
		caps->formats |= SFMT_S32 | SFMT_LE;
	if (format_mask & AFMT_S32_BE)
		caps->formats |= SFMT_S32 | SFMT_BE;
#endif

	if (!caps->formats) {
		/* Workaround for vmix which lies that it doesn't support any
		 * format. */
		error ("The driver claims that no format known to me is "
		       "supported. I will assume that SFMT_S8 and "
		       "SFMT_S16 (native endian) are supported.");
		caps->formats = SFMT_S8 | SFMT_S16 | SFMT_NE;
	}

	caps->min_channels = caps->max_channels = 1;
	if (ioctl (dsp_fd, SNDCTL_DSP_CHANNELS, &caps->min_channels)) {
		error_errno ("Can't set number of channels", errno);
		close (dsp_fd);
		return 0;
	}

	close (dsp_fd);
	if (!open_dev ()) {
		error ("Can't open the device.");
		return 0;
	}

	if (caps->min_channels != 1)
		caps->min_channels = 2;
	caps->max_channels = 2;
	if (ioctl (dsp_fd, SNDCTL_DSP_CHANNELS, &caps->max_channels)) {
		error_errno ("Can't set number of channels", errno);
		close (dsp_fd);
		return 0;
	}

	if (caps->max_channels != 2) {
		if (caps->min_channels == 2) {
			error ("Can't get any supported number of channels.");
			close (dsp_fd);
			return 0;
		}
		caps->max_channels = 1;
	}

	close (dsp_fd);

	return 1;
}

/* Get PCM volume.  Return -1 on error. */
static int oss_read_mixer ()
{
	int vol;

	if (!started)
		return -1;

#ifdef OSSv3_MIXER
	if (mixer_fd != -1 && mixer_channel_current != -1) {
		if (ioctl (mixer_fd, MIXER_READ(mixer_channel_current), &vol) == -1)
#else
	if (dsp_fd != -1) {
		if (ioctl (dsp_fd, SNDCTL_DSP_GETPLAYVOL, &vol) == -1)
#endif
		{
			error ("Can't read from mixer");
		}
		else {
			/* Average between left and right */
			return ((vol & 0xFF) + ((vol >> 8) & 0xFF)) / 2;
		}
	}

	return -1;
}

static int oss_mixer_name_to_channel (const char *name)
{
	size_t ix;

	for (ix = 0; ix < MIXER_CHANNELS_NUM; ix += 1) {
		if (!strcasecmp (mixer_channels[ix].name, name))
			return ix;
	}

	return -1;
}

static int oss_init (struct output_driver_caps *caps)
{
#ifdef OSSv3_MIXER
	/* Open the mixer device */
	mixer_fd = open (options_get_str ("OSSMixerDevice"), O_RDWR);
	if (mixer_fd == -1) {
		char *err = xstrerror (errno);
		error ("Can't open mixer device %s: %s",
		        options_get_str ("OSSMixerDevice"), err);
		free (err);
	}
	else {
		mixer_channel1 = oss_mixer_name_to_channel (
				options_get_symb ("OSSMixerChannel1"));
		mixer_channel2 = oss_mixer_name_to_channel (
				options_get_symb ("OSSMixerChannel2"));

		if (mixer_channel1 == -1)
			fatal ("Bad first OSS mixer channel!");
		if (mixer_channel2 == -1)
			fatal ("Bad second OSS mixer channel!");

		/* test mixer channels */
		mixer_channel_current = mixer_channel1;
		if (oss_read_mixer () == -1)
			mixer_channel1 = -1;

		mixer_channel_current = mixer_channel2;
		if (oss_read_mixer () == -1)
			mixer_channel2 = -1;

		if (mixer_channel1 != -1)
			mixer_channel_current = mixer_channel1;
	}
#endif

	return set_capabilities (caps);
}

static void oss_shutdown ()
{
#ifdef OSSv3_MIXER
	if (mixer_fd != -1) {
		close (mixer_fd);
		mixer_fd = -1;
	}
#endif
}

static void oss_close ()
{
	if (dsp_fd != -1) {
		close (dsp_fd);
		dsp_fd = -1;
		logit ("Audio device closed");
	}

	started = false;
	params.channels = 0;
	params.rate = 0;
	params.fmt = 0;
}

/* Return 0 on error. */
static int oss_set_params ()
{
	int req_format;
	int req_channels;
	char fmt_name[SFMT_STR_MAX];

	/* Set format */
	switch (params.fmt & SFMT_MASK_FORMAT) {
		case SFMT_S8:
			req_format = AFMT_S8;
			break;
		case SFMT_U8:
			req_format = AFMT_U8;
			break;
		case SFMT_S16:
			if (params.fmt & SFMT_LE)
				req_format = AFMT_S16_LE;
			else
				req_format = AFMT_S16_BE;
			break;
#if defined(AFMT_S32_LE) && defined(AFMT_S32_BE)
		case SFMT_S32:
			if (params.fmt & SFMT_LE)
				req_format = AFMT_S32_LE;
			else
				req_format = AFMT_S32_BE;
			break;
#endif
		default:
			error ("Format %s is not supported by the device",
			        sfmt_str (params.fmt, fmt_name, sizeof (fmt_name)));
			return 0;
	}

	if (ioctl (dsp_fd, SNDCTL_DSP_SETFMT, &req_format) == -1) {
		error_errno ("Can't set audio format", errno);
		oss_close ();
		return 0;
	}

	/* Set number of channels */
	req_channels = params.channels;
	if (ioctl (dsp_fd, SNDCTL_DSP_CHANNELS, &req_channels) == -1) {
		char *err = xstrerror (errno);
		error ("Can't set number of channels to %d: %s",
		        params.channels, err);
		free (err);
		oss_close ();
		return 0;
	}
	if (params.channels != req_channels) {
		error ("Can't set number of channels to %d, "
		       "device doesn't support this value",
				params.channels);
		oss_close ();
		return 0;
	}

	/* Set sample rate */
	if (ioctl (dsp_fd, SNDCTL_DSP_SPEED, &params.rate) == -1) {
		char *err = xstrerror (errno);
		error ("Can't set sampling rate to %d: %s", params.rate, err);
		free (err);
		oss_close ();
		return 0;
	}

	logit ("Audio parameters set to: %s, %d channels, %dHz",
	        sfmt_str (params.fmt, fmt_name, sizeof (fmt_name)),
	        params.channels, params.rate);

	return 1;
}

/* Return 0 on failure. */
static int oss_open (struct sound_params *sound_params)
{
	params = *sound_params;

	if (!open_dev ())
		return 0;

	if (!oss_set_params ()) {
		oss_close ();
		return 0;
	}

	started = true;

	return 1;
}

/* Return -1 on error, or number of bytes played when okay. */
static int oss_play (const char *buff, const size_t size)
{
	ssize_t ssize = (ssize_t) size;
	ssize_t count = 0;

	if (dsp_fd == -1) {
		error ("Can't play: audio device isn't opened!");
		return -1;
	}

	while (count < ssize) {
		ssize_t rc;

		rc = write (dsp_fd, buff + count, ssize - count);
		if (rc == -1) {
			error_errno ("Error writing pcm sound", errno);
			return -1;
		}

		count += rc;
	}

	return count;
}

/* Set PCM volume */
static void oss_set_mixer (int vol)
{
#ifdef OSSv3_MIXER
	if (mixer_fd != -1)
#else
	if (dsp_fd != -1)
#endif
	{
		vol = CLAMP(0, vol, 100);
		vol |= vol << 8;
#ifdef OSSv3_MIXER
		if (ioctl (mixer_fd, MIXER_WRITE(mixer_channel_current), &vol) == -1)
#else
		if (ioctl (dsp_fd, SNDCTL_DSP_SETPLAYVOL, &vol) == -1)
#endif
		{
			error ("Can't set mixer: ioctl() failed");
		}
	}
}

/* Return number of bytes in device buffer. */
static int oss_get_buff_fill ()
{
	audio_buf_info buff_info;

	if (dsp_fd == -1)
		return 0;

	if (ioctl (dsp_fd, SNDCTL_DSP_GETOSPACE, &buff_info) == -1) {
		error ("SNDCTL_DSP_GETOSPACE failed");
		return 0;
	}

	return (buff_info.fragstotal * buff_info.fragsize) - buff_info.bytes;
}

/* Reset device buffer and stop playing immediately.  Return 0 on error. */
static int oss_reset ()
{
	if (dsp_fd == -1) {
		logit ("Reset when audio device is not opened");
		return 0;
	}

	logit ("Resetting audio device");

	if (ioctl (dsp_fd, SNDCTL_DSP_RESET, NULL) == -1)
		error ("Resetting audio device failed");
	close (dsp_fd);
	dsp_fd = -1;
	if (!open_dev () || !oss_set_params ()) {
		error ("Failed to open audio device after resetting");
		return 0;
	}

	return 1;
}

static void oss_toggle_mixer_channel ()
{
#ifdef OSSv3_MIXER
	if (mixer_channel_current == mixer_channel1 && mixer_channel2 != -1)
		mixer_channel_current = mixer_channel2;
	else if (mixer_channel1 != -1)
		mixer_channel_current = mixer_channel1;
#endif
}

static char *oss_get_mixer_channel_name ()
{
#ifdef OSSv3_MIXER
	if (mixer_channel_current == mixer_channel1)
		return xstrdup (options_get_symb ("OSSMixerChannel1"));
	return xstrdup (options_get_symb ("OSSMixerChannel2"));
#else
	return xstrdup ("moc");
#endif
}

static int oss_get_rate ()
{
	return params.rate;
}

void oss_funcs (struct hw_funcs *funcs)
{
	funcs->init = oss_init;
	funcs->shutdown = oss_shutdown;
	funcs->open = oss_open;
	funcs->close = oss_close;
	funcs->play = oss_play;
	funcs->read_mixer = oss_read_mixer;
	funcs->set_mixer = oss_set_mixer;
	funcs->get_buff_fill = oss_get_buff_fill;
	funcs->reset = oss_reset;
	funcs->get_rate = oss_get_rate;
	funcs->toggle_mixer_channel = oss_toggle_mixer_channel;
	funcs->get_mixer_channel_name = oss_get_mixer_channel_name;
}