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