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