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