]> code.delx.au - pulseaudio/blob - src/modules/module-oss.c
Move xmalloc to the public side (libpolyp).
[pulseaudio] / src / modules / module-oss.c
1 /* $Id$ */
2
3 /***
4 This file is part of polypaudio.
5
6 polypaudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published
8 by the Free Software Foundation; either version 2 of the License,
9 or (at your option) any later version.
10
11 polypaudio is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with polypaudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19 USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <sys/soundcard.h>
27 #include <sys/ioctl.h>
28 #include <stdlib.h>
29 #include <sys/stat.h>
30 #include <stdio.h>
31 #include <assert.h>
32 #include <errno.h>
33 #include <string.h>
34 #include <fcntl.h>
35 #include <unistd.h>
36 #include <limits.h>
37
38 #include <polyp/xmalloc.h>
39
40 #include <polypcore/iochannel.h>
41 #include <polypcore/sink.h>
42 #include <polypcore/source.h>
43 #include <polypcore/module.h>
44 #include <polypcore/sample-util.h>
45 #include <polypcore/util.h>
46 #include <polypcore/modargs.h>
47 #include <polypcore/log.h>
48
49 #include "oss-util.h"
50 #include "module-oss-symdef.h"
51
52 PA_MODULE_AUTHOR("Lennart Poettering")
53 PA_MODULE_DESCRIPTION("OSS Sink/Source")
54 PA_MODULE_VERSION(PACKAGE_VERSION)
55 PA_MODULE_USAGE(
56 "sink_name=<name for the sink> "
57 "source_name=<name for the source> "
58 "device=<OSS device> "
59 "record=<enable source?> "
60 "playback=<enable sink?> "
61 "format=<sample format> "
62 "channels=<number of channels> "
63 "rate=<sample rate> "
64 "fragments=<number of fragments> "
65 "fragment_size=<fragment size> "
66 "channel_map=<channel map>")
67
68 struct userdata {
69 pa_sink *sink;
70 pa_source *source;
71 pa_iochannel *io;
72 pa_core *core;
73
74 pa_memchunk memchunk, silence;
75
76 uint32_t in_fragment_size, out_fragment_size, sample_size;
77 int use_getospace, use_getispace;
78
79 int fd;
80 pa_module *module;
81 };
82
83 static const char* const valid_modargs[] = {
84 "sink_name",
85 "source_name",
86 "device",
87 "record",
88 "playback",
89 "fragments",
90 "fragment_size",
91 "format",
92 "rate",
93 "channels",
94 "channel_map",
95 NULL
96 };
97
98 #define DEFAULT_SINK_NAME "oss_output"
99 #define DEFAULT_SOURCE_NAME "oss_input"
100 #define DEFAULT_DEVICE "/dev/dsp"
101
102 static void update_usage(struct userdata *u) {
103 pa_module_set_used(u->module,
104 (u->sink ? pa_idxset_size(u->sink->inputs) : 0) +
105 (u->sink ? pa_idxset_size(u->sink->monitor_source->outputs) : 0) +
106 (u->source ? pa_idxset_size(u->source->outputs) : 0));
107 }
108
109 static void do_write(struct userdata *u) {
110 pa_memchunk *memchunk;
111 ssize_t r;
112 size_t l;
113 int loop = 0;
114
115 assert(u);
116
117 if (!u->sink || !pa_iochannel_is_writable(u->io))
118 return;
119
120 update_usage(u);
121
122 l = u->out_fragment_size;
123
124 if (u->use_getospace) {
125 audio_buf_info info;
126
127 if (ioctl(u->fd, SNDCTL_DSP_GETOSPACE, &info) < 0)
128 u->use_getospace = 0;
129 else {
130 if (info.bytes/l > 0) {
131 l = (info.bytes/l)*l;
132 loop = 1;
133 }
134 }
135 }
136
137 do {
138 memchunk = &u->memchunk;
139
140 if (!memchunk->length)
141 if (pa_sink_render(u->sink, l, memchunk) < 0)
142 memchunk = &u->silence;
143
144 assert(memchunk->memblock);
145 assert(memchunk->memblock->data);
146 assert(memchunk->length);
147
148 if ((r = pa_iochannel_write(u->io, (uint8_t*) memchunk->memblock->data + memchunk->index, memchunk->length)) < 0) {
149 pa_log(__FILE__": write() failed: %s", strerror(errno));
150 break;
151 }
152
153 if (memchunk == &u->silence)
154 assert(r % u->sample_size == 0);
155 else {
156 u->memchunk.index += r;
157 u->memchunk.length -= r;
158
159 if (u->memchunk.length <= 0) {
160 pa_memblock_unref(u->memchunk.memblock);
161 u->memchunk.memblock = NULL;
162 }
163 }
164
165 l = l > (size_t) r ? l - r : 0;
166 } while (loop && l > 0);
167 }
168
169 static void do_read(struct userdata *u) {
170 pa_memchunk memchunk;
171 ssize_t r;
172 size_t l;
173 int loop = 0;
174 assert(u);
175
176 if (!u->source || !pa_iochannel_is_readable(u->io) || !pa_idxset_size(u->source->outputs))
177 return;
178
179 update_usage(u);
180
181 l = u->in_fragment_size;
182
183 if (u->use_getispace) {
184 audio_buf_info info;
185
186 if (ioctl(u->fd, SNDCTL_DSP_GETISPACE, &info) < 0)
187 u->use_getispace = 0;
188 else {
189 if (info.bytes/l > 0) {
190 l = (info.bytes/l)*l;
191 loop = 1;
192 }
193 }
194 }
195
196 do {
197 memchunk.memblock = pa_memblock_new(l, u->core->memblock_stat);
198 assert(memchunk.memblock);
199 if ((r = pa_iochannel_read(u->io, memchunk.memblock->data, memchunk.memblock->length)) < 0) {
200 pa_memblock_unref(memchunk.memblock);
201 if (errno != EAGAIN)
202 pa_log(__FILE__": read() failed: %s", strerror(errno));
203 break;
204 }
205
206 assert(r <= (ssize_t) memchunk.memblock->length);
207 memchunk.length = memchunk.memblock->length = r;
208 memchunk.index = 0;
209
210 pa_source_post(u->source, &memchunk);
211 pa_memblock_unref(memchunk.memblock);
212
213 l = l > (size_t) r ? l - r : 0;
214 } while (loop && l > 0);
215 }
216
217 static void io_callback(PA_GCC_UNUSED pa_iochannel *io, void*userdata) {
218 struct userdata *u = userdata;
219 assert(u);
220 do_write(u);
221 do_read(u);
222 }
223
224 static void source_notify_cb(pa_source *s) {
225 struct userdata *u = s->userdata;
226 assert(u);
227 do_read(u);
228 }
229
230 static pa_usec_t sink_get_latency_cb(pa_sink *s) {
231 pa_usec_t r = 0;
232 int arg;
233 struct userdata *u = s->userdata;
234 assert(s && u && u->sink);
235
236 if (ioctl(u->fd, SNDCTL_DSP_GETODELAY, &arg) < 0) {
237 pa_log_info(__FILE__": device doesn't support SNDCTL_DSP_GETODELAY: %s", strerror(errno));
238 s->get_latency = NULL;
239 return 0;
240 }
241
242 r += pa_bytes_to_usec(arg, &s->sample_spec);
243
244 if (u->memchunk.memblock)
245 r += pa_bytes_to_usec(u->memchunk.length, &s->sample_spec);
246
247 return r;
248 }
249
250 static pa_usec_t source_get_latency_cb(pa_source *s) {
251 struct userdata *u = s->userdata;
252 audio_buf_info info;
253 assert(s && u && u->source);
254
255 if (!u->use_getispace)
256 return 0;
257
258 if (ioctl(u->fd, SNDCTL_DSP_GETISPACE, &info) < 0) {
259 u->use_getispace = 0;
260 return 0;
261 }
262
263 if (info.bytes <= 0)
264 return 0;
265
266 return pa_bytes_to_usec(info.bytes, &s->sample_spec);
267 }
268
269 static int sink_get_hw_volume(pa_sink *s) {
270 struct userdata *u = s->userdata;
271
272 if (pa_oss_get_pcm_volume(u->fd, &s->sample_spec, &s->hw_volume) < 0) {
273 pa_log_info(__FILE__": device doesn't support reading mixer settings: %s", strerror(errno));
274 s->get_hw_volume = NULL;
275 return -1;
276 }
277
278 return 0;
279 }
280
281 static int sink_set_hw_volume(pa_sink *s) {
282 struct userdata *u = s->userdata;
283
284 if (pa_oss_set_pcm_volume(u->fd, &s->sample_spec, &s->hw_volume) < 0) {
285 pa_log_info(__FILE__": device doesn't support writing mixer settings: %s", strerror(errno));
286 s->set_hw_volume = NULL;
287 return -1;
288 }
289
290 return 0;
291 }
292
293 static int source_get_hw_volume(pa_source *s) {
294 struct userdata *u = s->userdata;
295
296 if (pa_oss_get_input_volume(u->fd, &s->sample_spec, &s->hw_volume) < 0) {
297 pa_log_info(__FILE__": device doesn't support reading mixer settings: %s", strerror(errno));
298 s->get_hw_volume = NULL;
299 return -1;
300 }
301
302 return 0;
303 }
304
305 static int source_set_hw_volume(pa_source *s) {
306 struct userdata *u = s->userdata;
307
308 if (pa_oss_set_input_volume(u->fd, &s->sample_spec, &s->hw_volume) < 0) {
309 pa_log_info(__FILE__": device doesn't support writing mixer settings: %s", strerror(errno));
310 s->set_hw_volume = NULL;
311 return -1;
312 }
313
314 return 0;
315 }
316
317 int pa__init(pa_core *c, pa_module*m) {
318 struct audio_buf_info info;
319 struct userdata *u = NULL;
320 const char *p;
321 int fd = -1;
322 int nfrags, frag_size, in_frag_size, out_frag_size;
323 int mode;
324 int record = 1, playback = 1;
325 pa_sample_spec ss;
326 pa_channel_map map;
327 pa_modargs *ma = NULL;
328 char hwdesc[64];
329
330 assert(c);
331 assert(m);
332
333 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
334 pa_log(__FILE__": failed to parse module arguments.");
335 goto fail;
336 }
337
338 if (pa_modargs_get_value_boolean(ma, "record", &record) < 0 || pa_modargs_get_value_boolean(ma, "playback", &playback) < 0) {
339 pa_log(__FILE__": record= and playback= expect numeric argument.");
340 goto fail;
341 }
342
343 if (!playback && !record) {
344 pa_log(__FILE__": neither playback nor record enabled for device.");
345 goto fail;
346 }
347
348 mode = (playback&&record) ? O_RDWR : (playback ? O_WRONLY : (record ? O_RDONLY : 0));
349
350 ss = c->default_sample_spec;
351 if (pa_modargs_get_sample_spec_and_channel_map(ma, &ss, &map, PA_CHANNEL_MAP_OSS) < 0) {
352 pa_log(__FILE__": failed to parse sample specification or channel map");
353 goto fail;
354 }
355
356 /* Fix latency to 100ms */
357 nfrags = 12;
358 frag_size = pa_bytes_per_second(&ss)/128;
359
360 if (pa_modargs_get_value_s32(ma, "fragments", &nfrags) < 0 || pa_modargs_get_value_s32(ma, "fragment_size", &frag_size) < 0) {
361 pa_log(__FILE__": failed to parse fragments arguments");
362 goto fail;
363 }
364
365 if ((fd = pa_oss_open(p = pa_modargs_get_value(ma, "device", DEFAULT_DEVICE), &mode, NULL)) < 0)
366 goto fail;
367
368 if (pa_oss_get_hw_description(p, hwdesc, sizeof(hwdesc)) >= 0)
369 pa_log_info(__FILE__": hardware name is '%s'.", hwdesc);
370 else
371 hwdesc[0] = 0;
372
373 pa_log_info(__FILE__": device opened in %s mode.", mode == O_WRONLY ? "O_WRONLY" : (mode == O_RDONLY ? "O_RDONLY" : "O_RDWR"));
374
375 if (nfrags >= 2 && frag_size >= 1)
376 if (pa_oss_set_fragments(fd, nfrags, frag_size) < 0)
377 goto fail;
378
379 if (pa_oss_auto_format(fd, &ss) < 0)
380 goto fail;
381
382 if (ioctl(fd, SNDCTL_DSP_GETBLKSIZE, &frag_size) < 0) {
383 pa_log(__FILE__": SNDCTL_DSP_GETBLKSIZE: %s", strerror(errno));
384 goto fail;
385 }
386 assert(frag_size);
387 in_frag_size = out_frag_size = frag_size;
388
389 u = pa_xmalloc(sizeof(struct userdata));
390 u->core = c;
391 u->use_getospace = u->use_getispace = 0;
392
393 if (ioctl(fd, SNDCTL_DSP_GETISPACE, &info) >= 0) {
394 pa_log_info(__FILE__": input -- %u fragments of size %u.", info.fragstotal, info.fragsize);
395 in_frag_size = info.fragsize;
396 u->use_getispace = 1;
397 }
398
399 if (ioctl(fd, SNDCTL_DSP_GETOSPACE, &info) >= 0) {
400 pa_log_info(__FILE__": output -- %u fragments of size %u.", info.fragstotal, info.fragsize);
401 out_frag_size = info.fragsize;
402 u->use_getospace = 1;
403 }
404
405 if (mode != O_WRONLY) {
406 if (!(u->source = pa_source_new(c, __FILE__, pa_modargs_get_value(ma, "source_name", DEFAULT_SOURCE_NAME), 0, &ss, &map)))
407 goto fail;
408
409 u->source->userdata = u;
410 u->source->notify = source_notify_cb;
411 u->source->get_latency = source_get_latency_cb;
412 u->source->get_hw_volume = source_get_hw_volume;
413 u->source->set_hw_volume = source_set_hw_volume;
414 pa_source_set_owner(u->source, m);
415 u->source->description = pa_sprintf_malloc("Open Sound System PCM on '%s'%s%s%s",
416 p,
417 hwdesc[0] ? " (" : "",
418 hwdesc[0] ? hwdesc : "",
419 hwdesc[0] ? ")" : "");
420 } else
421 u->source = NULL;
422
423 if (mode != O_RDONLY) {
424 if (!(u->sink = pa_sink_new(c, __FILE__, pa_modargs_get_value(ma, "sink_name", DEFAULT_SINK_NAME), 0, &ss, &map)))
425 goto fail;
426
427 u->sink->get_latency = sink_get_latency_cb;
428 u->sink->get_hw_volume = sink_get_hw_volume;
429 u->sink->set_hw_volume = sink_set_hw_volume;
430 u->sink->userdata = u;
431 pa_sink_set_owner(u->sink, m);
432 u->sink->description = pa_sprintf_malloc("Open Sound System PCM on '%s'%s%s%s",
433 p,
434 hwdesc[0] ? " (" : "",
435 hwdesc[0] ? hwdesc : "",
436 hwdesc[0] ? ")" : "");
437 } else
438 u->sink = NULL;
439
440 assert(u->source || u->sink);
441
442 u->io = pa_iochannel_new(c->mainloop, u->source ? fd : -1, u->sink ? fd : -1);
443 assert(u->io);
444 pa_iochannel_set_callback(u->io, io_callback, u);
445 u->fd = fd;
446
447 u->memchunk.memblock = NULL;
448 u->memchunk.length = 0;
449 u->sample_size = pa_frame_size(&ss);
450
451 u->out_fragment_size = out_frag_size;
452 u->in_fragment_size = in_frag_size;
453 u->silence.memblock = pa_memblock_new(u->silence.length = u->out_fragment_size, u->core->memblock_stat);
454 assert(u->silence.memblock);
455 pa_silence_memblock(u->silence.memblock, &ss);
456 u->silence.index = 0;
457
458 u->module = m;
459 m->userdata = u;
460
461 pa_modargs_free(ma);
462
463 /*
464 * Some crappy drivers do not start the recording until we read something.
465 * Without this snippet, poll will never register the fd as ready.
466 */
467 if (u->source) {
468 char *buf = pa_xnew(char, u->sample_size);
469 read(u->fd, buf, u->sample_size);
470 pa_xfree(buf);
471 }
472
473 /* Read mixer settings */
474 if (u->source)
475 source_get_hw_volume(u->source);
476 if (u->sink)
477 sink_get_hw_volume(u->sink);
478
479 return 0;
480
481 fail:
482 if (fd >= 0)
483 close(fd);
484
485 if (ma)
486 pa_modargs_free(ma);
487
488 return -1;
489 }
490
491 void pa__done(pa_core *c, pa_module*m) {
492 struct userdata *u;
493
494 assert(c);
495 assert(m);
496
497 if (!(u = m->userdata))
498 return;
499
500 if (u->memchunk.memblock)
501 pa_memblock_unref(u->memchunk.memblock);
502 if (u->silence.memblock)
503 pa_memblock_unref(u->silence.memblock);
504
505 if (u->sink) {
506 pa_sink_disconnect(u->sink);
507 pa_sink_unref(u->sink);
508 }
509
510 if (u->source) {
511 pa_source_disconnect(u->source);
512 pa_source_unref(u->source);
513 }
514
515 pa_iochannel_free(u->io);
516 pa_xfree(u);
517 }