summaryrefslogtreecommitdiff
path: root/modules/oss/oss.c
blob: 4a8af920df5a2a1c7e7a203923c3b8eaf8f45329 (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
/**
 * @file oss.c  Open Sound System (OSS) driver
 *
 * Copyright (C) 2010 Creytiv.com
 */
#include <re.h>
#include <rem.h>
#include <baresip.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#if defined(NETBSD) || defined(OPENBSD)
#include <soundcard.h>
#elif defined (LINUX)
#include <linux/soundcard.h>
#else
#include <sys/soundcard.h>
#endif
#ifdef SOLARIS
#include <sys/filio.h>
#endif


/**
 * @defgroup oss oss
 *
 * Open Sound System (OSS) audio driver module
 *
 *
 * References:
 *
 *    http://www.4front-tech.com/linux.html
 */


struct ausrc_st {
	const struct ausrc *as;      /* inheritance */
	pthread_t thread;
	bool run;
	int fd;
	int16_t *sampv;
	size_t sampc;
	ausrc_read_h *rh;
	ausrc_error_h *errh;
	void *arg;
};

struct auplay_st {
	const struct auplay *ap;      /* inheritance */
	pthread_t thread;
	bool run;
	int fd;
	int16_t *sampv;
	size_t sampc;
	auplay_write_h *wh;
	void *arg;
};


static struct ausrc *ausrc;
static struct auplay *auplay;
static char oss_dev[64] = "/dev/dsp";


/*
 * Automatically calculate the fragment size depending on sampling rate
 * and number of channels. More entries can be added to the table below.
 *
 * NOTE. Powermac 8200 and linux 2.4.18 gives:
 *       SNDCTL_DSP_SETFRAGMENT: Invalid argument
 */
static int set_fragment(int fd, uint32_t sampc)
{
	static const struct {
		uint16_t max;
		uint16_t size;
	} fragv[] = {
		{10, 7},  /* 10 x 2^7 = 1280 =  4 x 320 */
		{15, 7},  /* 15 x 2^7 = 1920 =  6 x 320 */
		{20, 7},  /* 20 x 2^7 = 2560 =  8 x 320 */
		{25, 7},  /* 25 x 2^7 = 3200 = 10 x 320 */
		{15, 8},  /* 15 x 2^8 = 3840 = 12 x 320 */
		{20, 8},  /* 20 x 2^8 = 5120 = 16 x 320 */
		{25, 8}   /* 25 x 2^8 = 6400 = 20 x 320 */
	};
	size_t i;
	const uint32_t buf_size = 2 * sampc;

	for (i=0; i<ARRAY_SIZE(fragv); i++) {
		const uint16_t frag_max  = fragv[i].max;
		const uint16_t frag_size = fragv[i].size;
		const uint32_t fragment_size = frag_max * (1<<frag_size);

		if (0 == (fragment_size%buf_size)) {
			int fragment = (frag_max<<16) | frag_size;

			if (0 == ioctl(fd, SNDCTL_DSP_SETFRAGMENT,
				       &fragment)) {
				return 0;
			}
		}
	}

	return ENODEV;
}


static int oss_reset(int fd, uint32_t srate, uint8_t ch, int sampc,
		     int nonblock)
{
	int format    = AFMT_S16_NE; /* native endian */
	int speed     = srate;
	int channels  = ch;
	int blocksize = 0;
	int err;

	err = set_fragment(fd, sampc);
	if (err)
		return err;

	if (0 != ioctl(fd, FIONBIO, &nonblock))
		return errno;
	if (0 != ioctl(fd, SNDCTL_DSP_SETFMT, &format))
		return errno;
	if (0 != ioctl(fd, SNDCTL_DSP_CHANNELS, &channels))
		return errno;
	if (2 == channels) {
		int stereo = 1;
		if (0 != ioctl(fd, SNDCTL_DSP_STEREO, &stereo))
			return errno;
	}
	if (0 != ioctl(fd, SNDCTL_DSP_SPEED, &speed))
		return errno;

	(void)ioctl(fd, SNDCTL_DSP_GETBLKSIZE, &blocksize);

	info("oss: init: %d Hz %d ch, blocksize=%d\n",
	     speed, channels, blocksize);

	return 0;
}


static void auplay_destructor(void *arg)
{
	struct auplay_st *st = arg;

	if (st->run) {
		st->run = false;
		pthread_join(st->thread, NULL);
	}

	if (-1 != st->fd) {
		(void)close(st->fd);
	}

	mem_deref(st->sampv);
}


static void ausrc_destructor(void *arg)
{
	struct ausrc_st *st = arg;

	if (st->run) {
		st->run = false;
		pthread_join(st->thread, NULL);
	}

	if (-1 != st->fd) {
		(void)close(st->fd);
	}

	mem_deref(st->sampv);
}


static void *record_thread(void *arg)
{
	struct ausrc_st *st = arg;
	int n;

	while (st->run) {

		n = read(st->fd, st->sampv, st->sampc*2);
		if (n <= 0)
			continue;

		st->rh(st->sampv, n/2, st->arg);
	}

	return NULL;
}


static void *play_thread(void *arg)
{
	struct auplay_st *st = arg;
	int n;

	while (st->run) {

		st->wh(st->sampv, st->sampc, st->arg);

		n = write(st->fd, st->sampv, st->sampc*2);
		if (n < 0) {
			warning("oss: write: %m\n", errno);
			break;
		}
	}

	return NULL;
}


static int src_alloc(struct ausrc_st **stp, const struct ausrc *as,
		     struct media_ctx **ctx,
		     struct ausrc_prm *prm, const char *device,
		     ausrc_read_h *rh, ausrc_error_h *errh, void *arg)
{
	struct ausrc_st *st;
	int err;

	(void)ctx;
	(void)errh;

	if (!stp || !as || !prm || !rh)
		return EINVAL;

	st = mem_zalloc(sizeof(*st), ausrc_destructor);
	if (!st)
		return ENOMEM;

	st->fd   = -1;
	st->rh   = rh;
	st->errh = errh;
	st->arg  = arg;

	if (!device)
		device = oss_dev;

	st->sampc = prm->srate * prm->ch * prm->ptime / 1000;

	st->sampv = mem_alloc(2 * st->sampc, NULL);
	if (!st->sampv) {
		err = ENOMEM;
		goto out;
	}

	st->fd = open(device, O_RDONLY);
	if (st->fd < 0) {
		err = errno;
		goto out;
	}

	err = oss_reset(st->fd, prm->srate, prm->ch, st->sampc, 0);
	if (err)
		goto out;

	st->as = as;

	st->run = true;
	err = pthread_create(&st->thread, NULL, record_thread, st);
	if (err) {
		st->run = false;
		goto out;
	}

 out:
	if (err)
		mem_deref(st);
	else
		*stp = st;

	return err;
}


static int play_alloc(struct auplay_st **stp, const struct auplay *ap,
		      struct auplay_prm *prm, const char *device,
		      auplay_write_h *wh, void *arg)
{
	struct auplay_st *st;
	int err;

	if (!stp || !ap || !prm || !wh)
		return EINVAL;

	st = mem_zalloc(sizeof(*st), auplay_destructor);
	if (!st)
		return ENOMEM;

	st->fd  = -1;
	st->wh  = wh;
	st->arg = arg;

	if (!device)
		device = oss_dev;

	st->sampc = prm->srate * prm->ch * prm->ptime / 1000;

	st->sampv = mem_alloc(st->sampc * 2, NULL);
	if (!st->sampv) {
		err = ENOMEM;
		goto out;
	}

	st->fd = open(device, O_WRONLY);
	if (st->fd < 0) {
		err = errno;
		goto out;
	}

	err = oss_reset(st->fd, prm->srate, prm->ch, st->sampc, 0);
	if (err)
		goto out;

	st->ap = ap;

	st->run = true;
	err = pthread_create(&st->thread, NULL, play_thread, st);
	if (err) {
		st->run = false;
		goto out;
	}

 out:
	if (err)
		mem_deref(st);
	else
		*stp = st;

	return err;
}


static int module_init(void)
{
	int err;

	err  = ausrc_register(&ausrc, baresip_ausrcl(), "oss", src_alloc);
	err |= auplay_register(&auplay, baresip_auplayl(), "oss", play_alloc);

	return err;
}


static int module_close(void)
{
	ausrc  = mem_deref(ausrc);
	auplay = mem_deref(auplay);

	return 0;
}


EXPORT_SYM const struct mod_export DECL_EXPORTS(oss) = {
	"oss",
	"audio",
	module_init,
	module_close,
};