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