oss_plugin.c 13 KB
Newer Older
1
/*
2
 * Copyright (C) 2003-2010 The Music Player Daemon Project
3
 * http://www.musicpd.org
4 5 6 7 8 9 10 11 12 13
 *
 * 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"
21
#include "output_api.h"
22
#include "mixer_list.h"
23
#include "fd_util.h"
24

25
#include <glib.h>
26

27 28 29
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <fcntl.h>
30 31 32 33 34 35
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>

#undef G_LOG_DOMAIN
#define G_LOG_DOMAIN "oss"
36

37 38
#if defined(__OpenBSD__) || defined(__NetBSD__)
# include <soundcard.h>
Avuton Olrich's avatar
Avuton Olrich committed
39
#else /* !(defined(__OpenBSD__) || defined(__NetBSD__) */
40
# include <sys/soundcard.h>
Avuton Olrich's avatar
Avuton Olrich committed
41
#endif /* !(defined(__OpenBSD__) || defined(__NetBSD__) */
42

Max Kellermann's avatar
Max Kellermann committed
43
#if G_BYTE_ORDER == G_BIG_ENDIAN
44 45 46
# define	AFMT_S16_MPD	 AFMT_S16_BE
#else
# define	AFMT_S16_MPD	 AFMT_S16_LE
47
#endif
48

Max Kellermann's avatar
Max Kellermann committed
49
struct oss_data {
50
	int fd;
51
	const char *device;
52
	struct audio_format audio_format;
53
	int bitFormat;
Avuton Olrich's avatar
Avuton Olrich committed
54
	int *supported[3];
55
	unsigned num_supported[3];
Avuton Olrich's avatar
Avuton Olrich committed
56
	int *unsupported[3];
57
	unsigned num_unsupported[3];
Max Kellermann's avatar
Max Kellermann committed
58
};
59

60 61 62 63 64 65 66 67 68 69 70 71
enum oss_support {
	OSS_SUPPORTED = 1,
	OSS_UNSUPPORTED = 0,
	OSS_UNKNOWN = -1,
};

enum oss_param {
	OSS_RATE = 0,
	OSS_CHANNELS = 1,
	OSS_BITS = 2,
};

72 73 74 75 76 77 78 79 80
/**
 * The quark used for GError.domain.
 */
static inline GQuark
oss_output_quark(void)
{
	return g_quark_from_static_string("oss_output");
}

81
static enum oss_param
Max Kellermann's avatar
Max Kellermann committed
82
oss_param_from_ioctl(unsigned param)
Avuton Olrich's avatar
Avuton Olrich committed
83
{
84
	enum oss_param idx = OSS_RATE;
Avuton Olrich's avatar
Avuton Olrich committed
85 86

	switch (param) {
87
	case SNDCTL_DSP_SPEED:
Max Kellermann's avatar
Max Kellermann committed
88
		idx = OSS_RATE;
89 90
		break;
	case SNDCTL_DSP_CHANNELS:
Max Kellermann's avatar
Max Kellermann committed
91
		idx = OSS_CHANNELS;
92 93
		break;
	case SNDCTL_DSP_SAMPLESIZE:
Max Kellermann's avatar
Max Kellermann committed
94
		idx = OSS_BITS;
95 96 97
		break;
	}

Max Kellermann's avatar
Max Kellermann committed
98
	return idx;
99 100
}

101
static bool
Max Kellermann's avatar
Max Kellermann committed
102
oss_find_supported_param(struct oss_data *od, unsigned param, int val)
Avuton Olrich's avatar
Avuton Olrich committed
103
{
Max Kellermann's avatar
Max Kellermann committed
104
	enum oss_param idx = oss_param_from_ioctl(param);
Avuton Olrich's avatar
Avuton Olrich committed
105

106
	for (unsigned i = 0; i < od->num_supported[idx]; i++)
Max Kellermann's avatar
Max Kellermann committed
107
		if (od->supported[idx][i] == val)
108
			return true;
109

110
	return false;
111 112
}

113
static bool
Max Kellermann's avatar
Max Kellermann committed
114
oss_can_convert(int idx, int val)
Avuton Olrich's avatar
Avuton Olrich committed
115
{
Max Kellermann's avatar
Max Kellermann committed
116
	switch (idx) {
117
	case OSS_BITS:
Avuton Olrich's avatar
Avuton Olrich committed
118
		if (val != 16)
119
			return false;
120 121
		break;
	case OSS_CHANNELS:
Avuton Olrich's avatar
Avuton Olrich committed
122
		if (val != 2)
123
			return false;
124 125 126
		break;
	}

127
	return true;
128 129
}

Max Kellermann's avatar
Max Kellermann committed
130 131
static int
oss_get_supported_param(struct oss_data *od, unsigned param, int val)
Avuton Olrich's avatar
Avuton Olrich committed
132
{
Max Kellermann's avatar
Max Kellermann committed
133
	enum oss_param idx = oss_param_from_ioctl(param);
134 135 136
	int ret = -1;
	int least = val;
	int diff;
Avuton Olrich's avatar
Avuton Olrich committed
137

138
	for (unsigned i = 0; i < od->num_supported[idx]; i++) {
Max Kellermann's avatar
Max Kellermann committed
139
		diff = od->supported[idx][i] - val;
Avuton Olrich's avatar
Avuton Olrich committed
140 141 142
		if (diff < 0)
			diff = -diff;
		if (diff < least) {
Max Kellermann's avatar
Max Kellermann committed
143
			if (!oss_can_convert(idx, od->supported[idx][i]))
144
				continue;
Max Kellermann's avatar
Max Kellermann committed
145

146
			least = diff;
Max Kellermann's avatar
Max Kellermann committed
147
			ret = od->supported[idx][i];
148 149 150 151 152 153
		}
	}

	return ret;
}

154
static bool
Max Kellermann's avatar
Max Kellermann committed
155
oss_find_unsupported_param(struct oss_data *od, unsigned param, int val)
Avuton Olrich's avatar
Avuton Olrich committed
156
{
Max Kellermann's avatar
Max Kellermann committed
157
	enum oss_param idx = oss_param_from_ioctl(param);
Avuton Olrich's avatar
Avuton Olrich committed
158

159
	for (unsigned i = 0; i < od->num_unsupported[idx]; i++) {
Max Kellermann's avatar
Max Kellermann committed
160
		if (od->unsupported[idx][i] == val)
161
			return true;
162 163
	}

164
	return false;
165 166
}

Max Kellermann's avatar
Max Kellermann committed
167 168
static void
oss_add_supported_param(struct oss_data *od, unsigned param, int val)
Avuton Olrich's avatar
Avuton Olrich committed
169
{
Max Kellermann's avatar
Max Kellermann committed
170
	enum oss_param idx = oss_param_from_ioctl(param);
171

Max Kellermann's avatar
Max Kellermann committed
172
	od->num_supported[idx]++;
173
	od->supported[idx] = g_realloc(od->supported[idx],
Max Kellermann's avatar
Max Kellermann committed
174 175
				       od->num_supported[idx] * sizeof(int));
	od->supported[idx][od->num_supported[idx] - 1] = val;
176 177
}

Max Kellermann's avatar
Max Kellermann committed
178 179
static void
oss_add_unsupported_param(struct oss_data *od, unsigned param, int val)
Avuton Olrich's avatar
Avuton Olrich committed
180
{
Max Kellermann's avatar
Max Kellermann committed
181
	enum oss_param idx = oss_param_from_ioctl(param);
182

Max Kellermann's avatar
Max Kellermann committed
183
	od->num_unsupported[idx]++;
184
	od->unsupported[idx] = g_realloc(od->unsupported[idx],
Max Kellermann's avatar
Max Kellermann committed
185
					 od->num_unsupported[idx] *
186
					 sizeof(int));
Max Kellermann's avatar
Max Kellermann committed
187
	od->unsupported[idx][od->num_unsupported[idx] - 1] = val;
188 189
}

Max Kellermann's avatar
Max Kellermann committed
190 191
static void
oss_remove_supported_param(struct oss_data *od, unsigned param, int val)
Avuton Olrich's avatar
Avuton Olrich committed
192
{
193
	unsigned j = 0;
Max Kellermann's avatar
Max Kellermann committed
194
	enum oss_param idx = oss_param_from_ioctl(param);
195

196
	for (unsigned i = 0; i < od->num_supported[idx] - 1; i++) {
Max Kellermann's avatar
Max Kellermann committed
197
		if (od->supported[idx][i] == val)
Avuton Olrich's avatar
Avuton Olrich committed
198
			j = 1;
Max Kellermann's avatar
Max Kellermann committed
199
		od->supported[idx][i] = od->supported[idx][i + j];
200 201
	}

Max Kellermann's avatar
Max Kellermann committed
202
	od->num_supported[idx]--;
203
	od->supported[idx] = g_realloc(od->supported[idx],
Max Kellermann's avatar
Max Kellermann committed
204
				       od->num_supported[idx] * sizeof(int));
205 206
}

Max Kellermann's avatar
Max Kellermann committed
207 208
static void
oss_remove_unsupported_param(struct oss_data *od, unsigned param, int val)
Avuton Olrich's avatar
Avuton Olrich committed
209
{
210
	unsigned j = 0;
Max Kellermann's avatar
Max Kellermann committed
211
	enum oss_param idx = oss_param_from_ioctl(param);
212

213
	for (unsigned i = 0; i < od->num_unsupported[idx] - 1; i++) {
Max Kellermann's avatar
Max Kellermann committed
214
		if (od->unsupported[idx][i] == val)
Avuton Olrich's avatar
Avuton Olrich committed
215
			j = 1;
Max Kellermann's avatar
Max Kellermann committed
216
		od->unsupported[idx][i] = od->unsupported[idx][i + j];
217 218
	}

Max Kellermann's avatar
Max Kellermann committed
219
	od->num_unsupported[idx]--;
220
	od->unsupported[idx] = g_realloc(od->unsupported[idx],
Max Kellermann's avatar
Max Kellermann committed
221
					 od->num_unsupported[idx] *
222
					 sizeof(int));
223 224
}

225
static enum oss_support
Max Kellermann's avatar
Max Kellermann committed
226
oss_param_is_supported(struct oss_data *od, unsigned param, int val)
Avuton Olrich's avatar
Avuton Olrich committed
227
{
Max Kellermann's avatar
Max Kellermann committed
228
	if (oss_find_supported_param(od, param, val))
Avuton Olrich's avatar
Avuton Olrich committed
229
		return OSS_SUPPORTED;
Max Kellermann's avatar
Max Kellermann committed
230
	if (oss_find_unsupported_param(od, param, val))
Avuton Olrich's avatar
Avuton Olrich committed
231
		return OSS_UNSUPPORTED;
232 233 234
	return OSS_UNKNOWN;
}

Max Kellermann's avatar
Max Kellermann committed
235 236
static void
oss_set_supported(struct oss_data *od, unsigned param, int val)
Avuton Olrich's avatar
Avuton Olrich committed
237
{
Max Kellermann's avatar
Max Kellermann committed
238
	enum oss_support supported = oss_param_is_supported(od, param, val);
239

Avuton Olrich's avatar
Avuton Olrich committed
240 241
	if (supported == OSS_SUPPORTED)
		return;
242

Max Kellermann's avatar
Max Kellermann committed
243 244
	if (supported == OSS_UNSUPPORTED)
		oss_remove_unsupported_param(od, param, val);
245

Max Kellermann's avatar
Max Kellermann committed
246
	oss_add_supported_param(od, param, val);
247 248
}

Max Kellermann's avatar
Max Kellermann committed
249 250
static void
oss_set_unsupported(struct oss_data *od, unsigned param, int val)
Avuton Olrich's avatar
Avuton Olrich committed
251
{
Max Kellermann's avatar
Max Kellermann committed
252
	enum oss_support supported = oss_param_is_supported(od, param, val);
253

Avuton Olrich's avatar
Avuton Olrich committed
254 255
	if (supported == OSS_UNSUPPORTED)
		return;
256

Max Kellermann's avatar
Max Kellermann committed
257 258
	if (supported == OSS_SUPPORTED)
		oss_remove_supported_param(od, param, val);
259

Max Kellermann's avatar
Max Kellermann committed
260
	oss_add_unsupported_param(od, param, val);
261 262
}

Max Kellermann's avatar
Max Kellermann committed
263 264
static struct oss_data *
oss_data_new(void)
Avuton Olrich's avatar
Avuton Olrich committed
265
{
Max Kellermann's avatar
Max Kellermann committed
266
	struct oss_data *ret = g_new(struct oss_data, 1);
267

268 269
	ret->device = NULL;
	ret->fd = -1;
270

271 272 273 274 275 276 277
	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;

Max Kellermann's avatar
Max Kellermann committed
278 279 280 281 282 283
	ret->num_supported[OSS_RATE] = 0;
	ret->num_supported[OSS_CHANNELS] = 0;
	ret->num_supported[OSS_BITS] = 0;
	ret->num_unsupported[OSS_RATE] = 0;
	ret->num_unsupported[OSS_CHANNELS] = 0;
	ret->num_unsupported[OSS_BITS] = 0;
284

Max Kellermann's avatar
Max Kellermann committed
285 286 287 288
	oss_set_supported(ret, SNDCTL_DSP_SPEED, 48000);
	oss_set_supported(ret, SNDCTL_DSP_SPEED, 44100);
	oss_set_supported(ret, SNDCTL_DSP_CHANNELS, 2);
	oss_set_supported(ret, SNDCTL_DSP_SAMPLESIZE, 16);
289

290 291 292
	return ret;
}

Max Kellermann's avatar
Max Kellermann committed
293 294
static void
oss_data_free(struct oss_data *od)
Avuton Olrich's avatar
Avuton Olrich committed
295
{
296 297 298 299 300 301
	g_free(od->supported[OSS_RATE]);
	g_free(od->supported[OSS_CHANNELS]);
	g_free(od->supported[OSS_BITS]);
	g_free(od->unsupported[OSS_RATE]);
	g_free(od->unsupported[OSS_CHANNELS]);
	g_free(od->unsupported[OSS_BITS]);
302

303
	g_free(od);
304 305
}

306 307 308 309 310 311 312
enum oss_stat {
	OSS_STAT_NO_ERROR = 0,
	OSS_STAT_NOT_CHAR_DEV = -1,
	OSS_STAT_NO_PERMS = -2,
	OSS_STAT_DOESN_T_EXIST = -3,
	OSS_STAT_OTHER = -4,
};
313

314
static enum oss_stat
Max Kellermann's avatar
Max Kellermann committed
315
oss_stat_device(const char *device, int *errno_r)
Avuton Olrich's avatar
Avuton Olrich committed
316
{
317
	struct stat st;
Avuton Olrich's avatar
Avuton Olrich committed
318 319 320

	if (0 == stat(device, &st)) {
		if (!S_ISCHR(st.st_mode)) {
321 322
			return OSS_STAT_NOT_CHAR_DEV;
		}
Avuton Olrich's avatar
Avuton Olrich committed
323
	} else {
Max Kellermann's avatar
Max Kellermann committed
324
		*errno_r = errno;
325

Avuton Olrich's avatar
Avuton Olrich committed
326
		switch (errno) {
327 328 329 330 331 332 333 334 335 336
		case ENOENT:
		case ENOTDIR:
			return OSS_STAT_DOESN_T_EXIST;
		case EACCES:
			return OSS_STAT_NO_PERMS;
		default:
			return OSS_STAT_OTHER;
		}
	}

337
	return OSS_STAT_NO_ERROR;
338 339
}

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

Max Kellermann's avatar
Max Kellermann committed
342 343
static bool
oss_output_test_default_device(void)
Avuton Olrich's avatar
Avuton Olrich committed
344
{
345
	int fd, i;
346

347
	for (i = G_N_ELEMENTS(default_devices); --i >= 0; ) {
348
		fd = open_cloexec(default_devices[i], O_WRONLY, 0);
349 350

		if (fd >= 0) {
351
			close(fd);
352
			return true;
353
		}
354 355
		g_warning("Error opening OSS device \"%s\": %s\n",
			  default_devices[i], strerror(errno));
356 357
	}

358
	return false;
359
}
360

Max Kellermann's avatar
Max Kellermann committed
361
static void *
362
oss_open_default(GError **error)
363 364
{
	int i;
365
	int err[G_N_ELEMENTS(default_devices)];
366
	enum oss_stat ret[G_N_ELEMENTS(default_devices)];
367

368
	for (i = G_N_ELEMENTS(default_devices); --i >= 0; ) {
Max Kellermann's avatar
Max Kellermann committed
369
		ret[i] = oss_stat_device(default_devices[i], &err[i]);
370
		if (ret[i] == OSS_STAT_NO_ERROR) {
Max Kellermann's avatar
Max Kellermann committed
371
			struct oss_data *od = oss_data_new();
372
			od->device = default_devices[i];
373
			return od;
374
		}
375 376
	}

377
	for (i = G_N_ELEMENTS(default_devices); --i >= 0; ) {
378 379
		const char *dev = default_devices[i];
		switch(ret[i]) {
380 381 382
		case OSS_STAT_NO_ERROR:
			/* never reached */
			break;
383
		case OSS_STAT_DOESN_T_EXIST:
384
			g_warning("%s not found\n", dev);
385 386
			break;
		case OSS_STAT_NOT_CHAR_DEV:
387
			g_warning("%s is not a character device\n", dev);
388 389
			break;
		case OSS_STAT_NO_PERMS:
390
			g_warning("%s: permission denied\n", dev);
391
			break;
392
		case OSS_STAT_OTHER:
393 394
			g_warning("Error accessing %s: %s\n",
				  dev, strerror(err[i]));
395 396
		}
	}
397 398 399 400

	g_set_error(error, oss_output_quark(), 0,
		    "error trying to open default OSS device");
	return NULL;
401 402
}

403
static void *
Max Kellermann's avatar
Max Kellermann committed
404
oss_output_init(G_GNUC_UNUSED const struct audio_format *audio_format,
405 406
		const struct config_param *param,
		GError **error)
Avuton Olrich's avatar
Avuton Olrich committed
407
{
Max Kellermann's avatar
Max Kellermann committed
408 409
	const char *device = config_get_block_string(param, "device", NULL);
	if (device != NULL) {
Max Kellermann's avatar
Max Kellermann committed
410
		struct oss_data *od = oss_data_new();
Max Kellermann's avatar
Max Kellermann committed
411 412
		od->device = device;
		return od;
413
	}
Max Kellermann's avatar
Max Kellermann committed
414

415
	return oss_open_default(error);
416 417
}

Max Kellermann's avatar
Max Kellermann committed
418 419
static void
oss_output_finish(void *data)
Avuton Olrich's avatar
Avuton Olrich committed
420
{
Max Kellermann's avatar
Max Kellermann committed
421
	struct oss_data *od = data;
422

Max Kellermann's avatar
Max Kellermann committed
423
	oss_data_free(od);
424 425
}

Max Kellermann's avatar
Max Kellermann committed
426 427
static int
oss_set_param(struct oss_data *od, unsigned param, int *value)
Avuton Olrich's avatar
Avuton Olrich committed
428
{
429 430
	int val = *value;
	int copy;
Max Kellermann's avatar
Max Kellermann committed
431
	enum oss_support supported = oss_param_is_supported(od, param, val);
432 433

	do {
Avuton Olrich's avatar
Avuton Olrich committed
434
		if (supported == OSS_UNSUPPORTED) {
Max Kellermann's avatar
Max Kellermann committed
435
			val = oss_get_supported_param(od, param, val);
Avuton Olrich's avatar
Avuton Olrich committed
436 437
			if (copy < 0)
				return -1;
438 439
		}
		copy = val;
Avuton Olrich's avatar
Avuton Olrich committed
440
		if (ioctl(od->fd, param, &copy)) {
Max Kellermann's avatar
Max Kellermann committed
441
			oss_set_unsupported(od, param, val);
442
			supported = OSS_UNSUPPORTED;
Avuton Olrich's avatar
Avuton Olrich committed
443 444
		} else {
			if (supported == OSS_UNKNOWN) {
Max Kellermann's avatar
Max Kellermann committed
445
				oss_set_supported(od, param, val);
446 447 448 449
				supported = OSS_SUPPORTED;
			}
			val = copy;
		}
Avuton Olrich's avatar
Avuton Olrich committed
450
	} while (supported == OSS_UNSUPPORTED);
451 452 453 454 455 456

	*value = val;

	return 0;
}

Max Kellermann's avatar
Max Kellermann committed
457 458
static void
oss_close(struct oss_data *od)
459
{
Avuton Olrich's avatar
Avuton Olrich committed
460 461
	if (od->fd >= 0)
		while (close(od->fd) && errno == EINTR) ;
462 463 464
	od->fd = -1;
}

465 466 467
/**
 * Sets up the OSS device which was opened before.
 */
Max Kellermann's avatar
Max Kellermann committed
468
static bool
469
oss_setup(struct oss_data *od, GError **error)
Avuton Olrich's avatar
Avuton Olrich committed
470
{
471
	int tmp;
472

473
	tmp = od->audio_format.channels;
Max Kellermann's avatar
Max Kellermann committed
474
	if (oss_set_param(od, SNDCTL_DSP_CHANNELS, &tmp)) {
475 476 477 478
		g_set_error(error, oss_output_quark(), errno,
			    "OSS device \"%s\" does not support %u channels: %s",
			    od->device, od->audio_format.channels,
			    strerror(errno));
479
		return false;
480
	}
481
	od->audio_format.channels = tmp;
482

483
	tmp = od->audio_format.sample_rate;
Max Kellermann's avatar
Max Kellermann committed
484
	if (oss_set_param(od, SNDCTL_DSP_SPEED, &tmp)) {
485 486 487 488
		g_set_error(error, oss_output_quark(), errno,
			    "OSS device \"%s\" does not support %u Hz audio: %s",
			    od->device, od->audio_format.sample_rate,
			    strerror(errno));
489
		return false;
490
	}
491
	od->audio_format.sample_rate = tmp;
492

493 494
	switch (od->audio_format.format) {
	case SAMPLE_FORMAT_S8:
495 496
		tmp = AFMT_S8;
		break;
497 498

	case SAMPLE_FORMAT_S16:
499
		tmp = AFMT_S16_MPD;
500 501 502 503
		break;

	default:
		/* not supported by OSS - fall back to 16 bit */
504
		od->audio_format.format = SAMPLE_FORMAT_S16;
505 506
		tmp = AFMT_S16_MPD;
		break;
507 508
	}

Max Kellermann's avatar
Max Kellermann committed
509
	if (oss_set_param(od, SNDCTL_DSP_SAMPLESIZE, &tmp)) {
510 511 512
		g_set_error(error, oss_output_quark(), errno,
			    "OSS device \"%s\" does not support %u bit audio: %s",
			    od->device, tmp, strerror(errno));
513
		return false;
514
	}
515

516
	return true;
517
}
518

519
static bool
520
oss_open(struct oss_data *od, GError **error)
521 522 523
{
	bool success;

524
	od->fd = open_cloexec(od->device, O_WRONLY, 0);
525
	if (od->fd < 0) {
526 527 528
		g_set_error(error, oss_output_quark(), errno,
			    "Error opening OSS device \"%s\": %s",
			    od->device, strerror(errno));
529 530 531
		return false;
	}

532
	success = oss_setup(od, error);
533 534 535 536 537 538
	if (!success) {
		oss_close(od);
		return false;
	}

	return true;
539 540
}

541
static bool
542
oss_output_open(void *data, struct audio_format *audio_format, GError **error)
543
{
544
	bool ret;
Max Kellermann's avatar
Max Kellermann committed
545
	struct oss_data *od = data;
546

Max Kellermann's avatar
Max Kellermann committed
547
	od->audio_format = *audio_format;
548

549
	ret = oss_open(od, error);
550 551
	if (!ret)
		return false;
552

Max Kellermann's avatar
Max Kellermann committed
553
	*audio_format = od->audio_format;
554 555

	return ret;
556 557
}

Max Kellermann's avatar
Max Kellermann committed
558 559
static void
oss_output_close(void *data)
Avuton Olrich's avatar
Avuton Olrich committed
560
{
Max Kellermann's avatar
Max Kellermann committed
561
	struct oss_data *od = data;
562

563
	oss_close(od);
564 565
}

Max Kellermann's avatar
Max Kellermann committed
566 567
static void
oss_output_cancel(void *data)
Avuton Olrich's avatar
Avuton Olrich committed
568
{
Max Kellermann's avatar
Max Kellermann committed
569
	struct oss_data *od = data;
570

Avuton Olrich's avatar
Avuton Olrich committed
571
	if (od->fd >= 0) {
572
		ioctl(od->fd, SNDCTL_DSP_RESET, 0);
573
		oss_close(od);
574 575 576
	}
}

577
static size_t
578
oss_output_play(void *data, const void *chunk, size_t size, GError **error)
579
{
Max Kellermann's avatar
Max Kellermann committed
580
	struct oss_data *od = data;
581
	ssize_t ret;
582

583
	/* reopen the device since it was closed by dropBufferedAudio */
584 585
	if (od->fd < 0 && !oss_open(od, error))
		return 0;
586

587
	while (true) {
588
		ret = write(od->fd, chunk, size);
589 590 591 592
		if (ret > 0)
			return (size_t)ret;

		if (ret < 0 && errno != EINTR) {
593 594 595
			g_set_error(error, oss_output_quark(), errno,
				    "Write error on %s: %s",
				    od->device, strerror(errno));
596
			return 0;
597 598 599 600
		}
	}
}

Max Kellermann's avatar
Max Kellermann committed
601
const struct audio_output_plugin oss_output_plugin = {
602
	.name = "oss",
Max Kellermann's avatar
Max Kellermann committed
603 604 605 606 607 608 609
	.test_default_device = oss_output_test_default_device,
	.init = oss_output_init,
	.finish = oss_output_finish,
	.open = oss_output_open,
	.close = oss_output_close,
	.play = oss_output_play,
	.cancel = oss_output_cancel,
610 611

	.mixer_plugin = &oss_mixer_plugin,
612
};