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