]> code.delx.au - pulseaudio/blob - src/modules/module-oss-mmap.c
name the sink/source after the device file, just like we already do for the non-mmape...
[pulseaudio] / src / modules / module-oss-mmap.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 #include <sys/mman.h>
38
39 #include <pulse/xmalloc.h>
40 #include <pulse/util.h>
41
42 #include <pulsecore/core-error.h>
43 #include <pulsecore/iochannel.h>
44 #include <pulsecore/sink.h>
45 #include <pulsecore/source.h>
46 #include <pulsecore/module.h>
47 #include <pulsecore/sample-util.h>
48 #include <pulsecore/core-util.h>
49 #include <pulsecore/modargs.h>
50 #include <pulsecore/log.h>
51
52 #include "oss-util.h"
53 #include "module-oss-mmap-symdef.h"
54
55 PA_MODULE_AUTHOR("Lennart Poettering")
56 PA_MODULE_DESCRIPTION("OSS Sink/Source (mmap)")
57 PA_MODULE_VERSION(PACKAGE_VERSION)
58 PA_MODULE_USAGE(
59 "sink_name=<name for the sink> "
60 "source_name=<name for the source> "
61 "device=<OSS device> "
62 "record=<enable source?> "
63 "playback=<enable sink?> "
64 "format=<sample format> "
65 "channels=<number of channels> "
66 "rate=<sample rate> "
67 "fragments=<number of fragments> "
68 "fragment_size=<fragment size> "
69 "channel_map=<channel map>")
70
71 struct userdata {
72 pa_sink *sink;
73 pa_source *source;
74 pa_core *core;
75 pa_sample_spec sample_spec;
76
77 size_t in_fragment_size, out_fragment_size;
78 unsigned in_fragments, out_fragments;
79 unsigned out_blocks_saved, in_blocks_saved;
80
81 int fd;
82
83 void *in_mmap, *out_mmap;
84 size_t in_mmap_length, out_mmap_length;
85
86 pa_io_event *io_event;
87
88 pa_memblock **in_memblocks, **out_memblocks;
89 unsigned out_current, in_current;
90 pa_module *module;
91 };
92
93 static const char* const valid_modargs[] = {
94 "sink_name",
95 "source_name",
96 "device",
97 "record",
98 "playback",
99 "fragments",
100 "fragment_size",
101 "format",
102 "rate",
103 "channels",
104 "channel_map",
105 NULL
106 };
107
108 #define DEFAULT_DEVICE "/dev/dsp"
109 #define DEFAULT_NFRAGS 12
110 #define DEFAULT_FRAGSIZE 1024
111
112 static void update_usage(struct userdata *u) {
113 pa_module_set_used(u->module,
114 (u->sink ? pa_idxset_size(u->sink->inputs) : 0) +
115 (u->sink ? pa_idxset_size(u->sink->monitor_source->outputs) : 0) +
116 (u->source ? pa_idxset_size(u->source->outputs) : 0));
117 }
118
119 static void clear_up(struct userdata *u) {
120 assert(u);
121
122 if (u->sink) {
123 pa_sink_disconnect(u->sink);
124 pa_sink_unref(u->sink);
125 u->sink = NULL;
126 }
127
128 if (u->source) {
129 pa_source_disconnect(u->source);
130 pa_source_unref(u->source);
131 u->source = NULL;
132 }
133
134 if (u->in_mmap && u->in_mmap != MAP_FAILED) {
135 munmap(u->in_mmap, u->in_mmap_length);
136 u->in_mmap = NULL;
137 }
138
139 if (u->out_mmap && u->out_mmap != MAP_FAILED) {
140 munmap(u->out_mmap, u->out_mmap_length);
141 u->out_mmap = NULL;
142 }
143
144 if (u->io_event) {
145 u->core->mainloop->io_free(u->io_event);
146 u->io_event = NULL;
147 }
148
149 if (u->fd >= 0) {
150 close(u->fd);
151 u->fd = -1;
152 }
153 }
154
155 static void out_fill_memblocks(struct userdata *u, unsigned n) {
156 assert(u && u->out_memblocks);
157
158 while (n > 0) {
159 pa_memchunk chunk;
160
161 if (u->out_memblocks[u->out_current])
162 pa_memblock_unref_fixed(u->out_memblocks[u->out_current]);
163
164 chunk.memblock = u->out_memblocks[u->out_current] =
165 pa_memblock_new_fixed(
166 (uint8_t*) u->out_mmap+u->out_fragment_size*u->out_current,
167 u->out_fragment_size,
168 1,
169 u->core->memblock_stat);
170 assert(chunk.memblock);
171 chunk.length = chunk.memblock->length;
172 chunk.index = 0;
173
174 pa_sink_render_into_full(u->sink, &chunk);
175
176 u->out_current++;
177 while (u->out_current >= u->out_fragments)
178 u->out_current -= u->out_fragments;
179
180 n--;
181 }
182 }
183
184 static void do_write(struct userdata *u) {
185 struct count_info info;
186 assert(u && u->sink);
187
188 update_usage(u);
189
190 if (ioctl(u->fd, SNDCTL_DSP_GETOPTR, &info) < 0) {
191 pa_log(__FILE__": SNDCTL_DSP_GETOPTR: %s", pa_cstrerror(errno));
192
193 clear_up(u);
194 pa_module_unload_request(u->module);
195 return;
196 }
197
198 info.blocks += u->out_blocks_saved;
199 u->out_blocks_saved = 0;
200
201 if (!info.blocks)
202 return;
203
204 out_fill_memblocks(u, info.blocks);
205 }
206
207 static void in_post_memblocks(struct userdata *u, unsigned n) {
208 assert(u && u->in_memblocks);
209
210 while (n > 0) {
211 pa_memchunk chunk;
212
213 if (!u->in_memblocks[u->in_current]) {
214 chunk.memblock = u->in_memblocks[u->in_current] = pa_memblock_new_fixed((uint8_t*) u->in_mmap+u->in_fragment_size*u->in_current, u->in_fragment_size, 1, u->core->memblock_stat);
215 chunk.length = chunk.memblock->length;
216 chunk.index = 0;
217
218 pa_source_post(u->source, &chunk);
219 }
220
221 u->in_current++;
222 while (u->in_current >= u->in_fragments)
223 u->in_current -= u->in_fragments;
224
225 n--;
226 }
227 }
228
229 static void in_clear_memblocks(struct userdata*u, unsigned n) {
230 unsigned i = u->in_current;
231 assert(u && u->in_memblocks);
232
233 if (n > u->in_fragments)
234 n = u->in_fragments;
235
236 while (n > 0) {
237 if (u->in_memblocks[i]) {
238 pa_memblock_unref_fixed(u->in_memblocks[i]);
239 u->in_memblocks[i] = NULL;
240 }
241
242 i++;
243 while (i >= u->in_fragments)
244 i -= u->in_fragments;
245
246 n--;
247 }
248 }
249
250 static void do_read(struct userdata *u) {
251 struct count_info info;
252 assert(u && u->source);
253
254 update_usage(u);
255
256 if (ioctl(u->fd, SNDCTL_DSP_GETIPTR, &info) < 0) {
257 pa_log(__FILE__": SNDCTL_DSP_GETIPTR: %s", pa_cstrerror(errno));
258
259 clear_up(u);
260 pa_module_unload_request(u->module);
261 return;
262 }
263
264 info.blocks += u->in_blocks_saved;
265 u->in_blocks_saved = 0;
266
267 if (!info.blocks)
268 return;
269
270 in_post_memblocks(u, info.blocks);
271 in_clear_memblocks(u, u->in_fragments/2);
272 }
273
274 static void io_callback(pa_mainloop_api *m, pa_io_event *e, PA_GCC_UNUSED int fd, pa_io_event_flags_t f, void *userdata) {
275 struct userdata *u = userdata;
276 assert (u && u->core->mainloop == m && u->io_event == e);
277
278 if (f & PA_IO_EVENT_ERROR) {
279 clear_up(u);
280 pa_module_unload_request(u->module);
281 return;
282 }
283
284 if (f & PA_IO_EVENT_INPUT)
285 do_read(u);
286 if (f & PA_IO_EVENT_OUTPUT)
287 do_write(u);
288 }
289
290 static pa_usec_t sink_get_latency_cb(pa_sink *s) {
291 struct userdata *u = s->userdata;
292 struct count_info info;
293 size_t bpos, n, total;
294 assert(s && u);
295
296 if (ioctl(u->fd, SNDCTL_DSP_GETOPTR, &info) < 0) {
297 pa_log(__FILE__": SNDCTL_DSP_GETOPTR: %s", pa_cstrerror(errno));
298 return 0;
299 }
300
301 u->out_blocks_saved += info.blocks;
302
303 total = u->out_fragments * u->out_fragment_size;
304 bpos = ((u->out_current + u->out_blocks_saved) * u->out_fragment_size) % total;
305
306 if (bpos <= (size_t) info.ptr)
307 n = total - (info.ptr - bpos);
308 else
309 n = bpos - info.ptr;
310
311 /* pa_log("n = %u, bpos = %u, ptr = %u, total=%u, fragsize = %u, n_frags = %u\n", n, bpos, (unsigned) info.ptr, total, u->out_fragment_size, u->out_fragments); */
312
313 return pa_bytes_to_usec(n, &s->sample_spec);
314 }
315
316 static pa_usec_t source_get_latency_cb(pa_source *s) {
317 struct userdata *u = s->userdata;
318 struct count_info info;
319 size_t bpos, n, total;
320 assert(s && u);
321
322 if (ioctl(u->fd, SNDCTL_DSP_GETIPTR, &info) < 0) {
323 pa_log(__FILE__": SNDCTL_DSP_GETIPTR: %s", pa_cstrerror(errno));
324 return 0;
325 }
326
327 u->in_blocks_saved += info.blocks;
328
329 total = u->in_fragments * u->in_fragment_size;
330 bpos = ((u->in_current + u->in_blocks_saved) * u->in_fragment_size) % total;
331
332 if (bpos <= (size_t) info.ptr)
333 n = info.ptr - bpos;
334 else
335 n = (u->in_fragments * u->in_fragment_size) - bpos + info.ptr;
336
337 /* pa_log("n = %u, bpos = %u, ptr = %u, total=%u, fragsize = %u, n_frags = %u\n", n, bpos, (unsigned) info.ptr, total, u->in_fragment_size, u->in_fragments); */
338
339 return pa_bytes_to_usec(n, &s->sample_spec);
340 }
341
342 static int sink_get_hw_volume(pa_sink *s) {
343 struct userdata *u = s->userdata;
344
345 if (pa_oss_get_pcm_volume(u->fd, &s->sample_spec, &s->hw_volume) < 0) {
346 pa_log_info(__FILE__": device doesn't support reading mixer settings: %s", pa_cstrerror(errno));
347 s->get_hw_volume = NULL;
348 return -1;
349 }
350
351 return 0;
352 }
353
354 static int sink_set_hw_volume(pa_sink *s) {
355 struct userdata *u = s->userdata;
356
357 if (pa_oss_set_pcm_volume(u->fd, &s->sample_spec, &s->hw_volume) < 0) {
358 pa_log_info(__FILE__": device doesn't support writing mixer settings: %s", pa_cstrerror(errno));
359 s->set_hw_volume = NULL;
360 return -1;
361 }
362
363 return 0;
364 }
365
366 static int source_get_hw_volume(pa_source *s) {
367 struct userdata *u = s->userdata;
368
369 if (pa_oss_get_input_volume(u->fd, &s->sample_spec, &s->hw_volume) < 0) {
370 pa_log_info(__FILE__": device doesn't support reading mixer settings: %s", pa_cstrerror(errno));
371 s->get_hw_volume = NULL;
372 return -1;
373 }
374
375 return 0;
376 }
377
378 static int source_set_hw_volume(pa_source *s) {
379 struct userdata *u = s->userdata;
380
381 if (pa_oss_set_input_volume(u->fd, &s->sample_spec, &s->hw_volume) < 0) {
382 pa_log_info(__FILE__": device doesn't support writing mixer settings: %s", pa_cstrerror(errno));
383 s->set_hw_volume = NULL;
384 return -1;
385 }
386
387 return 0;
388 }
389
390 int pa__init(pa_core *c, pa_module*m) {
391 struct audio_buf_info info;
392 struct userdata *u = NULL;
393 const char *p;
394 int nfrags, frag_size;
395 int mode, caps;
396 int enable_bits = 0, zero = 0;
397 int playback = 1, record = 1;
398 pa_modargs *ma = NULL;
399 char hwdesc[64], *t;
400 pa_channel_map map;
401 const char *name;
402 char *name_buf = NULL;
403 int namereg_fail;
404
405 assert(c);
406 assert(m);
407
408 m->userdata = u = pa_xnew0(struct userdata, 1);
409 u->module = m;
410 u->fd = -1;
411 u->core = c;
412
413 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
414 pa_log(__FILE__": failed to parse module arguments.");
415 goto fail;
416 }
417
418 if (pa_modargs_get_value_boolean(ma, "record", &record) < 0 || pa_modargs_get_value_boolean(ma, "playback", &playback) < 0) {
419 pa_log(__FILE__": record= and playback= expect numeric arguments.");
420 goto fail;
421 }
422
423 if (!playback && !record) {
424 pa_log(__FILE__": neither playback nor record enabled for device.");
425 goto fail;
426 }
427
428 mode = (playback&&record) ? O_RDWR : (playback ? O_WRONLY : (record ? O_RDONLY : 0));
429
430 nfrags = DEFAULT_NFRAGS;
431 frag_size = DEFAULT_FRAGSIZE;
432 if (pa_modargs_get_value_s32(ma, "fragments", &nfrags) < 0 || pa_modargs_get_value_s32(ma, "fragment_size", &frag_size) < 0) {
433 pa_log(__FILE__": failed to parse fragments arguments");
434 goto fail;
435 }
436
437 u->sample_spec = c->default_sample_spec;
438 if (pa_modargs_get_sample_spec_and_channel_map(ma, &u->sample_spec, &map, PA_CHANNEL_MAP_OSS) < 0) {
439 pa_log(__FILE__": failed to parse sample specification or channel map");
440 goto fail;
441 }
442
443 if ((u->fd = pa_oss_open(p = pa_modargs_get_value(ma, "device", DEFAULT_DEVICE), &mode, &caps)) < 0)
444 goto fail;
445
446 if (!(caps & DSP_CAP_MMAP) || !(caps & DSP_CAP_TRIGGER)) {
447 pa_log(__FILE__": OSS device not mmap capable.");
448 goto fail;
449 }
450
451 pa_log_info(__FILE__": device opened in %s mode.", mode == O_WRONLY ? "O_WRONLY" : (mode == O_RDONLY ? "O_RDONLY" : "O_RDWR"));
452
453 if (pa_oss_get_hw_description(p, hwdesc, sizeof(hwdesc)) >= 0)
454 pa_log_info(__FILE__": hardware name is '%s'.", hwdesc);
455 else
456 hwdesc[0] = 0;
457
458 if (nfrags >= 2 && frag_size >= 1)
459 if (pa_oss_set_fragments(u->fd, nfrags, frag_size) < 0)
460 goto fail;
461
462 if (pa_oss_auto_format(u->fd, &u->sample_spec) < 0)
463 goto fail;
464
465 if (mode != O_WRONLY) {
466 if (ioctl(u->fd, SNDCTL_DSP_GETISPACE, &info) < 0) {
467 pa_log(__FILE__": SNDCTL_DSP_GETISPACE: %s", pa_cstrerror(errno));
468 goto fail;
469 }
470
471 pa_log_info(__FILE__": input -- %u fragments of size %u.", info.fragstotal, info.fragsize);
472 u->in_mmap_length = (u->in_fragment_size = info.fragsize) * (u->in_fragments = info.fragstotal);
473
474 if ((u->in_mmap = mmap(NULL, u->in_mmap_length, PROT_READ, MAP_SHARED, u->fd, 0)) == MAP_FAILED) {
475 if (mode == O_RDWR) {
476 pa_log(__FILE__": mmap failed for input. Changing to O_WRONLY mode.");
477 mode = O_WRONLY;
478 } else {
479 pa_log(__FILE__": mmap(): %s", pa_cstrerror(errno));
480 goto fail;
481 }
482 } else {
483 if ((name = pa_modargs_get_value(ma, "source_name", NULL)))
484 namereg_fail = 1;
485 else {
486 name = name_buf = pa_sprintf_malloc("oss_input.%s", pa_path_get_filename(p));
487 namereg_fail = 0;
488 }
489
490 if (!(u->source = pa_source_new(c, __FILE__, name, namereg_fail, &u->sample_spec, &map)))
491 goto fail;
492
493 u->source->userdata = u;
494 u->source->get_latency = source_get_latency_cb;
495 u->source->get_hw_volume = source_get_hw_volume;
496 u->source->set_hw_volume = source_set_hw_volume;
497 pa_source_set_owner(u->source, m);
498 pa_source_set_description(u->source, t = pa_sprintf_malloc("OSS PCM/mmap() on %s%s%s%s",
499 p,
500 hwdesc[0] ? " (" : "",
501 hwdesc[0] ? hwdesc : "",
502 hwdesc[0] ? ")" : ""));
503 pa_xfree(t);
504 u->source->is_hardware = 1;
505
506 u->in_memblocks = pa_xnew0(pa_memblock*, u->in_fragments);
507
508 enable_bits |= PCM_ENABLE_INPUT;
509 }
510 }
511
512 pa_xfree(name_buf);
513 name_buf = NULL;
514
515 if (mode != O_RDONLY) {
516 if (ioctl(u->fd, SNDCTL_DSP_GETOSPACE, &info) < 0) {
517 pa_log(__FILE__": SNDCTL_DSP_GETOSPACE: %s", pa_cstrerror(errno));
518 goto fail;
519 }
520
521 pa_log_info(__FILE__": output -- %u fragments of size %u.", info.fragstotal, info.fragsize);
522 u->out_mmap_length = (u->out_fragment_size = info.fragsize) * (u->out_fragments = info.fragstotal);
523
524 if ((u->out_mmap = mmap(NULL, u->out_mmap_length, PROT_WRITE, MAP_SHARED, u->fd, 0)) == MAP_FAILED) {
525 if (mode == O_RDWR) {
526 pa_log(__FILE__": mmap filed for output. Changing to O_RDONLY mode.");
527 mode = O_RDONLY;
528 } else {
529 pa_log(__FILE__": mmap(): %s", pa_cstrerror(errno));
530 goto fail;
531 }
532 } else {
533 pa_silence_memory(u->out_mmap, u->out_mmap_length, &u->sample_spec);
534
535 if ((name = pa_modargs_get_value(ma, "sink_name", NULL)))
536 namereg_fail = 1;
537 else {
538 name = name_buf = pa_sprintf_malloc("oss_output.%s", pa_path_get_filename(p));
539 namereg_fail = 0;
540 }
541
542 if (!(u->sink = pa_sink_new(c, __FILE__, name, namereg_fail, &u->sample_spec, &map)))
543 goto fail;
544
545 u->sink->get_latency = sink_get_latency_cb;
546 u->sink->get_hw_volume = sink_get_hw_volume;
547 u->sink->set_hw_volume = sink_set_hw_volume;
548 u->sink->userdata = u;
549 pa_sink_set_owner(u->sink, m);
550 pa_sink_set_description(u->sink, t = pa_sprintf_malloc("OSS PCM/mmap() on %s%s%s%s",
551 p,
552 hwdesc[0] ? " (" : "",
553 hwdesc[0] ? hwdesc : "",
554 hwdesc[0] ? ")" : ""));
555 pa_xfree(t);
556
557 u->sink->is_hardware = 1;
558 u->out_memblocks = pa_xmalloc0(sizeof(struct memblock *)*u->out_fragments);
559
560 enable_bits |= PCM_ENABLE_OUTPUT;
561 }
562 }
563
564 pa_xfree(name_buf);
565 name_buf = NULL;
566
567 zero = 0;
568 if (ioctl(u->fd, SNDCTL_DSP_SETTRIGGER, &zero) < 0) {
569 pa_log(__FILE__": SNDCTL_DSP_SETTRIGGER: %s", pa_cstrerror(errno));
570 goto fail;
571 }
572
573 if (ioctl(u->fd, SNDCTL_DSP_SETTRIGGER, &enable_bits) < 0) {
574 pa_log(__FILE__": SNDCTL_DSP_SETTRIGGER: %s", pa_cstrerror(errno));
575 goto fail;
576 }
577
578 assert(u->source || u->sink);
579
580 u->io_event = c->mainloop->io_new(c->mainloop, u->fd, (u->source ? PA_IO_EVENT_INPUT : 0) | (u->sink ? PA_IO_EVENT_OUTPUT : 0), io_callback, u);
581 assert(u->io_event);
582
583 pa_modargs_free(ma);
584
585 /* Read mixer settings */
586 if (u->source)
587 source_get_hw_volume(u->source);
588 if (u->sink)
589 sink_get_hw_volume(u->sink);
590
591 return 0;
592
593 fail:
594 pa__done(c, m);
595
596 if (ma)
597 pa_modargs_free(ma);
598
599 pa_xfree(name_buf);
600
601 return -1;
602 }
603
604 void pa__done(pa_core *c, pa_module*m) {
605 struct userdata *u;
606
607 assert(c);
608 assert(m);
609
610 if (!(u = m->userdata))
611 return;
612
613 clear_up(u);
614
615 if (u->out_memblocks) {
616 unsigned i;
617 for (i = 0; i < u->out_fragments; i++)
618 if (u->out_memblocks[i])
619 pa_memblock_unref_fixed(u->out_memblocks[i]);
620 pa_xfree(u->out_memblocks);
621 }
622
623 if (u->in_memblocks) {
624 unsigned i;
625 for (i = 0; i < u->in_fragments; i++)
626 if (u->in_memblocks[i])
627 pa_memblock_unref_fixed(u->in_memblocks[i]);
628 pa_xfree(u->in_memblocks);
629 }
630
631 pa_xfree(u);
632 }